RtspPlayer.h 5.54 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
xzl committed
25 26 27 28
 */

#ifndef SRC_RTSPPLAYER_RTSPPLAYER_H_TXT_
#define SRC_RTSPPLAYER_RTSPPLAYER_H_TXT_
xiongzilaing committed
29

xzl committed
30
#include <string>
xiongzilaing committed
31
#include <memory>
xzl committed
32
#include "RtspSession.h"
xiongzilaing committed
33 34
#include "RtspMediaSource.h"
#include "Player/PlayerBase.h"
xzl committed
35
#include "Util/util.h"
xiongzilaing committed
36
#include "Util/logger.h"
xzl committed
37
#include "Util/TimeTicker.h"
xiongzilaing committed
38 39 40
#include "Poller/Timer.h"
#include "Network/Socket.h"
#include "Network/TcpClient.h"
41
#include "RtspSplitter.h"
xiongziliang committed
42
#include "RtpReceiver.h"
xzl committed
43 44

using namespace std;
xiongziliang committed
45
using namespace toolkit;
xiongzilaing committed
46

xiongziliang committed
47
namespace mediakit {
xzl committed
48 49

//实现了rtsp播放器协议部分的功能
xiongziliang committed
50
class RtspPlayer: public PlayerBase,public TcpClient, public RtspSplitter, public RtpReceiver {
xzl committed
51 52
public:
	typedef std::shared_ptr<RtspPlayer> Ptr;
xiongziliang committed
53

54
	RtspPlayer(const EventPoller::Ptr &poller) ;
xzl committed
55
	virtual ~RtspPlayer(void);
xiongziliang committed
56
	void play(const string &strUrl) override;
xzl committed
57 58
	void pause(bool bPause) override;
	void teardown() override;
59
	float getPacketLossRate(TrackType type) const override;
xzl committed
60 61
protected:
	//派生类回调函数
xiongziliang committed
62
	virtual bool onCheckSDP(const string &strSdp, const SdpParser &parser) = 0;
xiongziliang committed
63
	virtual void onRecvRTP(const RtpPacket::Ptr &pRtppt, const SdpTrack::Ptr &track) = 0;
64 65
    uint32_t getProgressMilliSecond() const;
    void seekToMilliSecond(uint32_t ms);
66 67 68 69 70 71 72 73 74 75 76 77 78

    /**
     * 收到完整的rtsp包回调,包括sdp等content数据
     * @param parser rtsp包
     */
    void onWholeRtspPacket(Parser &parser) override ;

    /**
     * 收到rtp包回调
     * @param data
     * @param len
     */
    void onRtpPacket(const char *data,uint64_t len) override ;
xiongziliang committed
79 80 81 82 83 84 85

    /**
     * rtp数据包排序后输出
     * @param rtppt rtp数据包
     * @param trackidx track索引
     */
	void onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx) override;
86 87 88 89 90 91 92 93 94


    /**
     * 收到RTCP包回调
     * @param iTrackidx
     * @param track
     * @param pucData
     * @param uiLen
     */
95
    virtual void onRtcpPacket(int iTrackidx, SdpTrack::Ptr &track, unsigned char *pucData, unsigned int uiLen);
96 97 98 99 100

	/////////////TcpClient override/////////////
	void onConnect(const SockException &err) override;
	void onRecv(const Buffer::Ptr &pBuf) override;
	void onErr(const SockException &ex) override;
xzl committed
101
private:
xiongziliang committed
102
	void onRecvRTP_l(const RtpPacket::Ptr &pRtppt, const SdpTrack::Ptr &track);
103 104
	void onPlayResult_l(const SockException &ex);

xiongziliang committed
105 106
    int getTrackIndexByControlSuffix(const string &controlSuffix) const;
    int getTrackIndexByInterleaved(int interleaved) const;
107
	int getTrackIndexByTrackType(TrackType trackType) const;
xzl committed
108

xiongziliang committed
109
	void play(const string &strUrl, const string &strUser, const string &strPwd,  Rtsp::eRtpType eType);
110 111 112 113 114
	void handleResSETUP(const Parser &parser, unsigned int uiTrackIndex);
	void handleResDESCRIBE(const Parser &parser);
	bool handleAuthenticationFailure(const string &wwwAuthenticateParamsStr);
	void handleResPAUSE(const Parser &parser, bool bPause);

xzl committed
115
	//发送SETUP命令
116 117 118 119
	void sendSetup(unsigned int uiTrackIndex);
	void sendPause(bool bPause,uint32_t ms);
	void sendOptions();
	void sendDescribe();
xiongziliang committed
120

121 122
    void sendRtspRequest(const string &cmd, const string &url ,const StrCaseMap &header = StrCaseMap());
	void sendRtspRequest(const string &cmd, const string &url ,const std::initializer_list<string> &header);
xiongziliang committed
123
    void sendReceiverReport(bool overTcp,int iTrackIndex);
124
	void createUdpSockIfNecessary(int track_idx);
125
private:
126
	string _strUrl;
xiongziliang committed
127
	SdpParser _sdpParser;
xiongziliang committed
128
	vector<SdpTrack::Ptr> _aTrackInfo;
129
	function<void(const Parser&)> _onHandshake;
130 131 132 133
    Socket::Ptr _apRtpSock[2]; //RTP端口,trackid idx 为数组下标
    Socket::Ptr _apRtcpSock[2];//RTCP端口,trackid idx 为数组下标

    //rtsp鉴权相关
xiongziliang committed
134 135
	string _rtspMd5Nonce;
	string _rtspRealm;
xzl committed
136
	//rtsp info
137 138 139
	string _strSession;
	unsigned int _uiCseq = 1;
	string _strContentBase;
xiongziliang committed
140
	Rtsp::eRtpType _eType = Rtsp::RTP_TCP;
xzl committed
141 142

	/* 丢包率统计需要用到的参数 */
143 144 145
	uint16_t _aui16FirstSeq[2] = { 0 , 0};
	uint16_t _aui16NowSeq[2] = { 0 , 0 };
	uint64_t _aui64RtpRecv[2] = { 0 , 0};
xzl committed
146 147

	//超时功能实现
148 149 150
	Ticker _rtpTicker;
	std::shared_ptr<Timer> _pPlayTimer;
	std::shared_ptr<Timer> _pRtpTimer;
xiongziliang committed
151

152 153 154 155 156 157
    //播放进度控制,单位毫秒
    uint32_t _iSeekTo = 0;

    //单位毫秒
	uint32_t _aiFistStamp[2] = {0,0};
	uint32_t _aiNowStamp[2] = {0,0};
xiongziliang committed
158 159 160 161

	//rtcp相关
    RtcpCounter _aRtcpCnt[2]; //rtcp统计,trackid idx 为数组下标
    Ticker _aRtcpTicker[2]; //rtcp发送时间,trackid idx 为数组下标
xzl committed
162 163
};

xiongziliang committed
164
} /* namespace mediakit */
xzl committed
165 166

#endif /* SRC_RTSPPLAYER_RTSPPLAYER_H_TXT_ */