ZLMediaKit/src/Rtsp/RtspPlayer.h

153 lines
4.8 KiB
C++
Raw Normal View History

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
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
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 "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"
2019-12-04 10:45:38 +08:00
#include "Common/Stamp.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
2019-08-30 11:17:27 +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:
2020-03-20 11:51:24 +08:00
typedef std::shared_ptr<RtspPlayer> Ptr;
RtspPlayer(const EventPoller::Ptr &poller) ;
virtual ~RtspPlayer(void);
void play(const string &strUrl) override;
void pause(bool bPause) override;
void teardown() override;
float getPacketLossRate(TrackType type) const override;
2017-04-01 16:35:56 +08:00
protected:
2020-03-20 11:51:24 +08:00
//派生类回调函数
virtual bool onCheckSDP(const string &strSdp) = 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索引
*/
2020-03-20 11:51:24 +08:00
void onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx) override;
/**
* RTCP包回调
* @param iTrackidx
* @param track
* @param pucData
* @param uiLen
*/
2019-05-09 10:49:50 +08:00
virtual void onRtcpPacket(int iTrackidx, SdpTrack::Ptr &track, unsigned char *pucData, unsigned int uiLen);
2019-07-20 20:53:50 +08:00
2020-03-20 11:51:24 +08:00
/////////////TcpClient override/////////////
void onConnect(const SockException &err) override;
void onRecv(const Buffer::Ptr &pBuf) override;
void onErr(const SockException &ex) override;
2017-04-01 16:35:56 +08:00
private:
2020-03-20 11:51:24 +08:00
void onRecvRTP_l(const RtpPacket::Ptr &pRtppt, const SdpTrack::Ptr &track);
void onPlayResult_l(const SockException &ex , bool handshakeCompleted);
2018-07-05 18:48:08 +08:00
int getTrackIndexByInterleaved(int interleaved) const;
2020-03-20 11:51:24 +08:00
int getTrackIndexByTrackType(TrackType trackType) const;
2017-04-01 16:35:56 +08:00
2020-03-20 11:51:24 +08:00
void handleResSETUP(const Parser &parser, unsigned int uiTrackIndex);
void handleResDESCRIBE(const Parser &parser);
bool handleAuthenticationFailure(const string &wwwAuthenticateParamsStr);
void handleResPAUSE(const Parser &parser, int type);
bool handleResponse(const string &cmd, const Parser &parser);
2020-05-15 09:53:17 +08:00
void sendOptions();
2020-03-20 11:51:24 +08:00
void sendSetup(unsigned int uiTrackIndex);
void sendPause(int type , uint32_t ms);
void sendDescribe();
2020-05-15 09:53:17 +08:00
void sendKeepAlive();
void sendRtspRequest(const string &cmd, const string &url ,const StrCaseMap &header = StrCaseMap());
2020-03-20 11:51:24 +08:00
void sendRtspRequest(const string &cmd, const string &url ,const std::initializer_list<string> &header);
2019-05-09 13:35:54 +08:00
void sendReceiverReport(bool overTcp,int iTrackIndex);
2020-03-20 11:51:24 +08:00
void createUdpSockIfNecessary(int track_idx);
private:
string _play_url;
vector<SdpTrack::Ptr> _sdp_track;
function<void(const Parser&)> _on_response;
//RTP端口,trackid idx 为数组下标
Socket::Ptr _rtp_sock[2];
//RTCP端口,trackid idx 为数组下标
Socket::Ptr _rtcp_sock[2];
//rtsp鉴权相关
string _md5_nonce;
string _realm;
2020-03-20 11:51:24 +08:00
//rtsp info
string _session_id;
uint32_t _cseq_send = 1;
string _content_base;
Rtsp::eRtpType _rtp_type = Rtsp::RTP_TCP;
2020-03-20 11:51:24 +08:00
/* 丢包率统计需要用到的参数 */
uint16_t _rtp_seq_start[2] = {0, 0};
uint16_t _rtp_seq_now[2] = {0, 0};
uint64_t _rtp_recv_count[2] = {0, 0};
//当前rtp时间戳
uint32_t _stamp[2] = {0, 0};
2020-03-20 11:51:24 +08:00
//超时功能实现
Ticker _rtp_recv_ticker;
std::shared_ptr<Timer> _play_check_timer;
std::shared_ptr<Timer> _rtp_check_timer;
2020-03-20 11:51:24 +08:00
//rtcp统计,trackid idx 为数组下标
RtcpCounter _rtcp_counter[2];
//rtcp发送时间,trackid idx 为数组下标
Ticker _rtcp_send_ticker[2];
2020-03-20 11:51:24 +08:00
2020-04-08 11:16:09 +08:00
//是否为性能测试模式
bool _benchmark_mode = false;
2020-05-15 09:53:17 +08:00
//服务器支持的命令
set<string> _supported_cmd;
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_ */