RtspPlayer.h 4.99 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * 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 32
#include <memory>
#include "Rtsp.h"
xzl committed
33
#include "RtspSession.h"
xiongzilaing committed
34 35
#include "RtspMediaSource.h"
#include "Player/PlayerBase.h"
xzl committed
36
#include "Util/util.h"
xiongzilaing committed
37
#include "Util/logger.h"
xzl committed
38
#include "Util/TimeTicker.h"
xiongzilaing committed
39 40 41
#include "Poller/Timer.h"
#include "Network/Socket.h"
#include "Network/TcpClient.h"
xzl committed
42 43 44 45

using namespace std;
using namespace ZL::Rtsp;
using namespace ZL::Player;
xiongzilaing committed
46 47 48 49
using namespace ZL::Util;
using namespace ZL::Poller;
using namespace ZL::Network;

xzl committed
50 51 52 53 54 55 56
namespace ZL {
namespace Rtsp {

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

xzl committed
58 59
	RtspPlayer();
	virtual ~RtspPlayer(void);
xiongziliang committed
60
	void play(const char* strUrl) override;
xzl committed
61 62
	void pause(bool bPause) override;
	void teardown() override;
xiongziliang committed
63
	float getRtpLossRate(int iTrackType) const override;
xzl committed
64 65 66 67 68 69 70
protected:
	//派生类回调函数
	virtual bool onCheckSDP(const string &strSdp, const RtspTrack *pTrack, int iTrackCnt) = 0;
	virtual void onRecvRTP(const RtpPacket::Ptr &pRtppt, const RtspTrack &track) = 0;
    float getProgressTime() const;
    void seekToTime(float fTime);
private:
71 72 73 74 75
	void onShutdown_l(const SockException &ex);
    void onRecvRTP_l(const RtpPacket::Ptr &pRtppt, int iTrackidx);
	void onRecvRTP_l(const RtpPacket::Ptr &pRtppt, const RtspTrack &track);
	void onPlayResult_l(const SockException &ex);

xiongziliang committed
76 77 78
    int getTrackIndexByControlSuffix(const string &controlSuffix) const;
    int getTrackIndexByInterleaved(int interleaved) const;
	int getTrackIndexByTrackType(TrackType trackId) const;
xzl committed
79

xiongziliang committed
80
	void play(const char* strUrl, const char *strUser, const char *strPwd,  eRtpType eType);
xzl committed
81
	void onConnect(const SockException &err) override;
82
	void onRecv(const Buffer::Ptr &pBuf) override;
xzl committed
83 84
	void onErr(const SockException &ex) override;

85 86 87 88
	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
89 90

	//发数据给服务器
91
	int onProcess(const char* strBuf);
xzl committed
92
	//生成rtp包结构体
93 94 95 96
	void splitRtp(unsigned char *pucData, unsigned int uiLen);
    //处理一个rtp包
    bool handleOneRtp(int iTrackidx, unsigned char *ucData, unsigned int uiLen);

xzl committed
97
	//发送SETUP命令
98 99
    bool sendSetup(unsigned int uiTrackIndex);
    bool sendPause(bool bPause,float fTime);
xzl committed
100
	bool sendOptions();
101 102 103
	bool sendDescribe();
    bool sendRtspRequest(const string &cmd, const string &url ,const StrCaseMap &header = StrCaseMap());
private:
xzl committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	string m_strUrl;
	unsigned int m_uiTrackCnt = 0;
	RtspTrack m_aTrackInfo[2];

	function<void(const Parser&)> m_onHandshake;
	RtspMediaSource::PoolType m_pktPool;

	uint8_t *m_pucRtpBuf = nullptr;
	unsigned int m_uiRtpBufLen = 0;
	Socket::Ptr m_apUdpSock[2];
	//rtsp info
	string m_strSession;
	unsigned int m_uiCseq = 1;
	uint32_t m_aui32SsrcErrorCnt[2] = { 0, 0 };
	string m_strContentBase;
	eRtpType m_eType = RTP_TCP;
	/* RTP包排序所用参数 */
	uint16_t m_aui16LastSeq[2] = { 0 , 0 };
	uint64_t m_aui64SeqOkCnt[2] = { 0 , 0};
	bool m_abSortStarted[2] = { 0 , 0};
	map<uint32_t , RtpPacket::Ptr> m_amapRtpSort[2];

	/* 丢包率统计需要用到的参数 */
	uint16_t m_aui16FirstSeq[2] = { 0 , 0};
	uint16_t m_aui16NowSeq[2] = { 0 , 0 };
	uint64_t m_aui64RtpRecv[2] = { 0 , 0};

	//超时功能实现
	Ticker m_rtpTicker;
	std::shared_ptr<Timer> m_pPlayTimer;
	std::shared_ptr<Timer> m_pRtpTimer;
	//心跳定时器
	std::shared_ptr<Timer> m_pBeatTimer;
    
    //播放进度控制
    float m_fSeekTo = 0;
    double m_adFistStamp[2] = {0,0};
    double m_adNowStamp[2] = {0,0};
    Ticker m_aNowStampTicker[2];
};

} /* namespace Rtsp */
} /* namespace ZL */

#endif /* SRC_RTSPPLAYER_RTSPPLAYER_H_TXT_ */