ZLMediaKit/src/Rtsp/RtspPlayer.h

168 lines
5.4 KiB
C
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2017-09-27 16:20:30 +08:00
* MIT License
2017-04-01 16:35:56 +08:00
*
2019-05-08 15:40:07 +08:00
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
2017-09-27 16:20:30 +08:00
*
* 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.
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>
#include "Rtsp.h"
2017-04-01 16:35:56 +08:00
#include "RtspSession.h"
2017-04-25 11:35:41 +08:00
#include "RtspMediaSource.h"
#include "Player/PlayerBase.h"
2017-04-01 16:35:56 +08:00
#include "Util/util.h"
2017-04-25 11:35:41 +08:00
#include "Util/logger.h"
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"
#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
using namespace std;
2018-10-24 17:17:55 +08:00
using namespace toolkit;
2017-04-25 11:35:41 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit {
2017-04-01 16:35:56 +08:00
//实现了rtsp播放器协议部分的功能
2018-12-14 17:46:12 +08:00
class RtspPlayer: public PlayerBase,public TcpClient, public RtspSplitter, public RtpReceiver {
2017-04-01 16:35:56 +08:00
public:
typedef std::shared_ptr<RtspPlayer> Ptr;
2017-08-03 13:55:46 +08:00
RtspPlayer(const EventPoller::Ptr &poller) ;
2017-04-01 16:35:56 +08:00
virtual ~RtspPlayer(void);
2019-03-27 18:41:52 +08:00
void play(const string &strUrl) override;
2017-04-01 16:35:56 +08:00
void pause(bool bPause) override;
void teardown() override;
2018-10-26 14:12:16 +08:00
float getPacketLossRate(TrackType type) const override;
2017-04-01 16:35:56 +08:00
protected:
//派生类回调函数
2018-10-26 09:56:29 +08:00
virtual bool onCheckSDP(const string &strSdp, const SdpAttr &sdpAttr) = 0;
virtual void onRecvRTP(const RtpPacket::Ptr &pRtppt, 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
*/
void onRtpPacket(const char *data,uint64_t len) override ;
2018-12-14 17:46:12 +08:00
/**
* rtp数据包排序后输出
* @param rtppt rtp数据包
* @param trackidx track索引
*/
void onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx) override;
/**
* RTCP包回调
* @param iTrackidx
* @param track
* @param pucData
* @param uiLen
*/
virtual void onRecvRtcp(int iTrackidx,SdpTrack::Ptr &track, unsigned char *pucData, unsigned int uiLen);
/**
* rtcp包维持心跳
*/
virtual void sendRtcpPacket();
2017-04-01 16:35:56 +08:00
private:
2018-10-26 09:56:29 +08:00
void onRecvRTP_l(const RtpPacket::Ptr &pRtppt, const SdpTrack::Ptr &track);
void onPlayResult_l(const SockException &ex);
2018-07-05 18:48:08 +08:00
int getTrackIndexByControlSuffix(const string &controlSuffix) const;
int getTrackIndexByInterleaved(int interleaved) const;
int getTrackIndexByTrackType(TrackType trackType) const;
2017-04-01 16:35:56 +08:00
2019-03-27 18:41:52 +08:00
void play(const string &strUrl, const string &strUser, const string &strPwd, Rtsp::eRtpType eType);
2017-04-01 16:35:56 +08:00
void onConnect(const SockException &err) override;
2018-02-23 15:36:51 +08:00
void onRecv(const Buffer::Ptr &pBuf) override;
2017-04-01 16:35:56 +08:00
void onErr(const SockException &ex) override;
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);
2017-04-01 16:35:56 +08:00
//发送SETUP命令
void sendSetup(unsigned int uiTrackIndex);
void sendPause(bool bPause,uint32_t ms);
void sendOptions();
void sendDescribe();
2018-12-17 13:48:19 +08:00
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);
private:
2018-10-24 15:43:52 +08:00
string _strUrl;
2018-10-26 09:56:29 +08:00
SdpAttr _sdpAttr;
vector<SdpTrack::Ptr> _aTrackInfo;
2018-10-24 15:43:52 +08:00
function<void(const Parser&)> _onHandshake;
Socket::Ptr _apRtpSock[2]; //RTP端口,trackid idx 为数组下标
Socket::Ptr _apRtcpSock[2];//RTCP端口,trackid idx 为数组下标
//rtsp鉴权相关
2019-03-27 18:41:52 +08:00
string _rtspMd5Nonce;
string _rtspRealm;
2017-04-01 16:35:56 +08:00
//rtsp info
2018-10-24 15:43:52 +08:00
string _strSession;
unsigned int _uiCseq = 1;
string _strContentBase;
2019-03-27 18:41:52 +08:00
Rtsp::eRtpType _eType = Rtsp::RTP_TCP;
2017-04-01 16:35:56 +08:00
/* 丢包率统计需要用到的参数 */
2018-10-24 15:43:52 +08:00
uint16_t _aui16FirstSeq[2] = { 0 , 0};
uint16_t _aui16NowSeq[2] = { 0 , 0 };
uint64_t _aui64RtpRecv[2] = { 0 , 0};
2017-04-01 16:35:56 +08:00
//超时功能实现
2018-10-24 15:43:52 +08:00
Ticker _rtpTicker;
std::shared_ptr<Timer> _pPlayTimer;
std::shared_ptr<Timer> _pRtpTimer;
2017-04-01 16:35:56 +08:00
//心跳定时器
2018-10-24 15:43:52 +08:00
std::shared_ptr<Timer> _pBeatTimer;
2017-04-01 16:35:56 +08:00
2018-10-26 14:12:16 +08:00
//播放进度控制,单位毫秒
uint32_t _iSeekTo = 0;
//单位毫秒
uint32_t _aiFistStamp[2] = {0,0};
uint32_t _aiNowStamp[2] = {0,0};
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_ */