2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef SRC_RTSPPLAYER_RTSPPLAYER_H_TXT_
|
|
|
|
|
#define SRC_RTSPPLAYER_RTSPPLAYER_H_TXT_
|
2017-04-25 11:35:41 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include <string>
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <memory>
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include "Util/TimeTicker.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Poller/Timer.h"
|
|
|
|
|
#include "Network/Socket.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Player/PlayerBase.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Network/TcpClient.h"
|
2018-10-30 10:31:27 +08:00
|
|
|
|
#include "RtspSplitter.h"
|
2018-12-14 17:46:12 +08:00
|
|
|
|
#include "RtpReceiver.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2022-11-29 11:07:13 +08:00
|
|
|
|
class RtcpContext;
|
2019-08-30 11:17:27 +08:00
|
|
|
|
//实现了rtsp播放器协议部分的功能,及数据接收功能
|
2022-02-02 20:34:50 +08:00
|
|
|
|
class RtspPlayer : public PlayerBase, public toolkit::TcpClient, public RtspSplitter, public RtpReceiver {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using Ptr = std::shared_ptr<RtspPlayer>;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
RtspPlayer(const toolkit::EventPoller::Ptr &poller);
|
2020-08-01 10:14:42 +08:00
|
|
|
|
~RtspPlayer() override;
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void play(const std::string &strUrl) override;
|
2021-08-12 16:07:31 +08:00
|
|
|
|
void pause(bool pause) override;
|
|
|
|
|
void speed(float speed) override;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
void teardown() override;
|
|
|
|
|
float getPacketLossRate(TrackType type) const override;
|
2020-08-01 10:14:42 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
protected:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//派生类回调函数
|
2022-02-02 20:34:50 +08:00
|
|
|
|
virtual bool onCheckSDP(const std::string &sdp) = 0;
|
2021-02-05 11:28:50 +08:00
|
|
|
|
virtual void onRecvRTP(RtpPacket::Ptr rtp, const SdpTrack::Ptr &track) = 0;
|
2018-10-26 14:12:16 +08:00
|
|
|
|
uint32_t getProgressMilliSecond() const;
|
|
|
|
|
void seekToMilliSecond(uint32_t ms);
|
2018-10-30 10:31:27 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 收到完整的rtsp包回调,包括sdp等content数据
|
|
|
|
|
* @param parser rtsp包
|
|
|
|
|
*/
|
|
|
|
|
void onWholeRtspPacket(Parser &parser) override ;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 收到rtp包回调
|
|
|
|
|
* @param data
|
|
|
|
|
* @param len
|
|
|
|
|
*/
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void onRtpPacket(const char *data,size_t len) override ;
|
2018-12-14 17:46:12 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* rtp数据包排序后输出
|
2020-08-01 10:14:42 +08:00
|
|
|
|
* @param rtp rtp数据包
|
|
|
|
|
* @param track_idx track索引
|
2018-12-14 17:46:12 +08:00
|
|
|
|
*/
|
2021-02-05 11:28:50 +08:00
|
|
|
|
void onRtpSorted(RtpPacket::Ptr rtp, int track_idx) override;
|
2019-05-08 15:08:57 +08:00
|
|
|
|
|
2021-01-31 19:18:17 +08:00
|
|
|
|
/**
|
|
|
|
|
* 解析出rtp但还未排序
|
|
|
|
|
* @param rtp rtp数据包
|
|
|
|
|
* @param track_index track索引
|
|
|
|
|
*/
|
|
|
|
|
void onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_index) override;
|
|
|
|
|
|
2019-05-08 15:08:57 +08:00
|
|
|
|
/**
|
|
|
|
|
* 收到RTCP包回调
|
2020-08-01 10:14:42 +08:00
|
|
|
|
* @param track_idx track索引
|
|
|
|
|
* @param track sdp相关信息
|
|
|
|
|
* @param data rtcp内容
|
|
|
|
|
* @param len rtcp内容长度
|
2019-05-08 15:08:57 +08:00
|
|
|
|
*/
|
2021-01-31 19:18:17 +08:00
|
|
|
|
virtual void onRtcpPacket(int track_idx, SdpTrack::Ptr &track, uint8_t *data, size_t len);
|
2019-07-20 20:53:50 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
/////////////TcpClient override/////////////
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onConnect(const toolkit::SockException &err) override;
|
|
|
|
|
void onRecv(const toolkit::Buffer::Ptr &buf) override;
|
|
|
|
|
void onErr(const toolkit::SockException &ex) override;
|
2020-08-01 10:14:42 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
private:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onPlayResult_l(const toolkit::SockException &ex , bool handshake_done);
|
2018-07-02 15:43:37 +08:00
|
|
|
|
|
2018-07-05 18:48:08 +08:00
|
|
|
|
int getTrackIndexByInterleaved(int interleaved) const;
|
2020-08-01 10:14:42 +08:00
|
|
|
|
int getTrackIndexByTrackType(TrackType track_type) const;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2020-08-01 10:14:42 +08:00
|
|
|
|
void handleResSETUP(const Parser &parser, unsigned int track_idx);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
void handleResDESCRIBE(const Parser &parser);
|
2022-02-02 20:34:50 +08:00
|
|
|
|
bool handleAuthenticationFailure(const std::string &wwwAuthenticateParamsStr);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
void handleResPAUSE(const Parser &parser, int type);
|
2022-02-02 20:34:50 +08:00
|
|
|
|
bool handleResponse(const std::string &cmd, const Parser &parser);
|
2018-07-02 15:43:37 +08:00
|
|
|
|
|
2020-05-15 09:53:17 +08:00
|
|
|
|
void sendOptions();
|
2020-08-01 10:14:42 +08:00
|
|
|
|
void sendSetup(unsigned int track_idx);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
void sendPause(int type , uint32_t ms);
|
|
|
|
|
void sendDescribe();
|
2021-01-17 10:25:00 +08:00
|
|
|
|
void sendTeardown();
|
2020-05-15 09:53:17 +08:00
|
|
|
|
void sendKeepAlive();
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void sendRtspRequest(const std::string &cmd, const std::string &url ,const StrCaseMap &header = StrCaseMap());
|
|
|
|
|
void sendRtspRequest(const std::string &cmd, const std::string &url ,const std::initializer_list<std::string> &header);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
void createUdpSockIfNecessary(int track_idx);
|
2020-08-01 10:14:42 +08:00
|
|
|
|
|
2018-07-02 15:43:37 +08:00
|
|
|
|
private:
|
2021-01-02 22:32:13 +08:00
|
|
|
|
//是否为性能测试模式
|
|
|
|
|
bool _benchmark_mode = false;
|
|
|
|
|
//轮流发送rtcp与GET_PARAMETER保活
|
2021-01-31 19:18:17 +08:00
|
|
|
|
bool _send_rtcp[2] = {true, true};
|
2021-01-02 22:32:13 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _play_url;
|
|
|
|
|
std::vector<SdpTrack::Ptr> _sdp_track;
|
|
|
|
|
std::function<void(const Parser&)> _on_response;
|
2020-05-15 20:15:43 +08:00
|
|
|
|
//RTP端口,trackid idx 为数组下标
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Socket::Ptr _rtp_sock[2];
|
2020-05-15 20:15:43 +08:00
|
|
|
|
//RTCP端口,trackid idx 为数组下标
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Socket::Ptr _rtcp_sock[2];
|
2019-05-08 15:08:57 +08:00
|
|
|
|
|
|
|
|
|
//rtsp鉴权相关
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _md5_nonce;
|
|
|
|
|
std::string _realm;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//rtsp info
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _session_id;
|
2020-05-15 20:15:43 +08:00
|
|
|
|
uint32_t _cseq_send = 1;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string _content_base;
|
2020-05-15 20:15:43 +08:00
|
|
|
|
Rtsp::eRtpType _rtp_type = Rtsp::RTP_TCP;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
2020-05-15 20:15:43 +08:00
|
|
|
|
//当前rtp时间戳
|
|
|
|
|
uint32_t _stamp[2] = {0, 0};
|
2020-03-20 11:51:24 +08:00
|
|
|
|
|
|
|
|
|
//超时功能实现
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Ticker _rtp_recv_ticker;
|
|
|
|
|
std::shared_ptr<toolkit::Timer> _play_check_timer;
|
|
|
|
|
std::shared_ptr<toolkit::Timer> _rtp_check_timer;
|
2020-05-15 09:53:17 +08:00
|
|
|
|
//服务器支持的命令
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::set<std::string> _supported_cmd;
|
2021-01-31 19:18:17 +08:00
|
|
|
|
////////// rtcp ////////////////
|
|
|
|
|
//rtcp发送时间,trackid idx 为数组下标
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Ticker _rtcp_send_ticker[2];
|
2021-01-31 19:18:17 +08:00
|
|
|
|
//统计rtp并发送rtcp
|
2022-11-29 11:07:13 +08:00
|
|
|
|
std::vector<std::shared_ptr<RtcpContext>> _rtcp_context;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#endif /* SRC_RTSPPLAYER_RTSPPLAYER_H_TXT_ */
|