2019-08-08 19:01:45 +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 SESSION_RTSPSESSION_H_
|
|
|
|
|
#define SESSION_RTSPSESSION_H_
|
|
|
|
|
|
|
|
|
|
#include <set>
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <vector>
|
2019-05-08 16:19:00 +08:00
|
|
|
|
#include <unordered_set>
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include <unordered_map>
|
2018-12-17 15:21:23 +08:00
|
|
|
|
#include "Util/util.h"
|
|
|
|
|
#include "Util/logger.h"
|
2017-05-02 17:15:12 +08:00
|
|
|
|
#include "Common/config.h"
|
2018-12-17 15:21:23 +08:00
|
|
|
|
#include "Network/TcpSession.h"
|
|
|
|
|
#include "Player/PlayerBase.h"
|
2019-08-22 16:25:19 +08:00
|
|
|
|
#include "RtpMultiCaster.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include "RtspMediaSource.h"
|
2018-12-17 15:21:23 +08:00
|
|
|
|
#include "RtspSplitter.h"
|
2018-12-14 18:13:05 +08:00
|
|
|
|
#include "RtpReceiver.h"
|
2019-12-26 11:53:19 +08:00
|
|
|
|
#include "RtspMediaSourceImp.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;
|
2018-09-25 09:55:41 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
class RtspSession;
|
2018-01-30 09:35:54 +08:00
|
|
|
|
|
2018-02-23 15:36:51 +08:00
|
|
|
|
class BufferRtp : public Buffer{
|
2018-01-30 09:35:54 +08:00
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<BufferRtp> Ptr;
|
|
|
|
|
BufferRtp(const RtpPacket::Ptr & pkt,uint32_t offset = 0 ):_rtp(pkt),_offset(offset){}
|
|
|
|
|
virtual ~BufferRtp(){}
|
|
|
|
|
|
2018-10-18 23:48:00 +08:00
|
|
|
|
char *data() const override {
|
2019-06-24 16:07:44 +08:00
|
|
|
|
return (char *)_rtp->data() + _offset;
|
2018-01-30 09:35:54 +08:00
|
|
|
|
}
|
|
|
|
|
uint32_t size() const override {
|
2019-06-24 16:07:44 +08:00
|
|
|
|
return _rtp->size() - _offset;
|
2018-01-30 09:35:54 +08:00
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
RtpPacket::Ptr _rtp;
|
|
|
|
|
uint32_t _offset;
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-17 15:21:23 +08:00
|
|
|
|
class RtspSession: public TcpSession, public RtspSplitter, public RtpReceiver , public MediaSourceEvent{
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
typedef std::shared_ptr<RtspSession> Ptr;
|
|
|
|
|
typedef std::function<void(const string &realm)> onGetRealm;
|
2017-12-10 01:34:43 +08:00
|
|
|
|
//encrypted为true是则表明是md5加密的密码,否则是明文密码
|
|
|
|
|
//在请求明文密码时如果提供md5密码者则会导致认证失败
|
2020-03-20 11:51:24 +08:00
|
|
|
|
typedef std::function<void(bool encrypted,const string &pwd_or_md5)> onAuth;
|
|
|
|
|
|
|
|
|
|
RtspSession(const Socket::Ptr &pSock);
|
|
|
|
|
virtual ~RtspSession();
|
|
|
|
|
////TcpSession override////
|
|
|
|
|
void onRecv(const Buffer::Ptr &pBuf) override;
|
|
|
|
|
void onError(const SockException &err) override;
|
|
|
|
|
void onManager() override;
|
2018-09-20 18:44:32 +08:00
|
|
|
|
protected:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//RtspSplitter override
|
2018-12-17 15:21:23 +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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从rtsp头中获取Content长度
|
|
|
|
|
* @param parser
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
int64_t getContentLength(Parser &parser) override;
|
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//RtpReceiver override
|
|
|
|
|
void onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx) override;
|
|
|
|
|
//MediaSourceEvent override
|
|
|
|
|
bool close(MediaSource &sender,bool force) override ;
|
|
|
|
|
int totalReaderCount(MediaSource &sender) override;
|
2018-12-19 16:54:11 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//TcpSession override
|
2018-12-19 16:54:11 +08:00
|
|
|
|
int send(const Buffer::Ptr &pkt) override;
|
2019-05-08 14:23:18 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 收到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);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
private:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//处理options方法,获取服务器能力
|
2019-08-22 15:23:14 +08:00
|
|
|
|
void handleReq_Options(const Parser &parser);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//处理describe方法,请求服务器rtsp sdp信息
|
2019-08-22 15:23:14 +08:00
|
|
|
|
void handleReq_Describe(const Parser &parser);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//处理ANNOUNCE方法,请求推流,附带sdp
|
2019-08-22 15:23:14 +08:00
|
|
|
|
void handleReq_ANNOUNCE(const Parser &parser);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//处理record方法,开始推流
|
2019-08-22 15:23:14 +08:00
|
|
|
|
void handleReq_RECORD(const Parser &parser);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//处理setup方法,播放和推流协商rtp传输方式用
|
2019-08-22 15:23:14 +08:00
|
|
|
|
void handleReq_Setup(const Parser &parser);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//处理play方法,开始或恢复播放
|
2019-08-22 15:23:14 +08:00
|
|
|
|
void handleReq_Play(const Parser &parser);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//处理pause方法,暂停播放
|
2019-08-22 15:23:14 +08:00
|
|
|
|
void handleReq_Pause(const Parser &parser);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//处理teardown方法,结束播放
|
2019-08-22 15:23:14 +08:00
|
|
|
|
void handleReq_Teardown(const Parser &parser);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//处理Get方法,rtp over http才用到
|
2019-08-22 15:23:14 +08:00
|
|
|
|
void handleReq_Get(const Parser &parser);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//处理Post方法,rtp over http才用到
|
2019-08-22 15:23:14 +08:00
|
|
|
|
void handleReq_Post(const Parser &parser);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//处理SET_PARAMETER、GET_PARAMETER方法,一般用于心跳
|
2019-08-22 15:23:14 +08:00
|
|
|
|
void handleReq_SET_PARAMETER(const Parser &parser);
|
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//rtsp资源未找到
|
|
|
|
|
void inline send_StreamNotFound();
|
|
|
|
|
//不支持的传输模式
|
|
|
|
|
void inline send_UnsupportedTransport();
|
|
|
|
|
//会话id错误
|
|
|
|
|
void inline send_SessionNotFound();
|
|
|
|
|
//一般rtsp服务器打开端口失败时触发
|
|
|
|
|
void inline send_NotAcceptable();
|
|
|
|
|
//ssrc转字符串
|
|
|
|
|
inline string printSSRC(uint32_t ui32Ssrc);
|
|
|
|
|
|
|
|
|
|
//获取track下标
|
|
|
|
|
inline int getTrackIndexByTrackType(TrackType type);
|
2018-12-14 14:59:12 +08:00
|
|
|
|
inline int getTrackIndexByControlSuffix(const string &controlSuffix);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
inline int getTrackIndexByInterleaved(int interleaved);
|
2018-03-26 18:56:22 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//一般用于接收udp打洞包,也用于rtsp推流
|
|
|
|
|
inline void onRcvPeerUdpData(int intervaled, const Buffer::Ptr &pBuf, const struct sockaddr &addr);
|
|
|
|
|
//配合onRcvPeerUdpData使用
|
|
|
|
|
inline void startListenPeerUdpData(int iTrackIdx);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-08-22 15:23:14 +08:00
|
|
|
|
////rtsp专有认证相关////
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//认证成功
|
2019-05-30 12:14:20 +08:00
|
|
|
|
void onAuthSuccess();
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//认证失败
|
2019-05-30 12:14:20 +08:00
|
|
|
|
void onAuthFailed(const string &realm,const string &why,bool close = true);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//开始走rtsp专有认证流程
|
2019-05-30 12:14:20 +08:00
|
|
|
|
void onAuthUser(const string &realm,const string &authorization);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//校验base64方式的认证加密
|
2019-05-30 12:14:20 +08:00
|
|
|
|
void onAuthBasic(const string &realm,const string &strBase64);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//校验md5方式的认证加密
|
2019-05-30 12:14:20 +08:00
|
|
|
|
void onAuthDigest(const string &realm,const string &strMd5);
|
2017-12-10 01:34:43 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//发送rtp给客户端
|
2019-06-26 10:01:04 +08:00
|
|
|
|
void sendRtpPacket(const RtpPacket::Ptr &pkt);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//回复客户端
|
|
|
|
|
bool sendRtspResponse(const string &res_code,const std::initializer_list<string> &header, const string &sdp = "" , const char *protocol = "RTSP/1.0");
|
|
|
|
|
bool sendRtspResponse(const string &res_code,const StrCaseMap &header = StrCaseMap(), const string &sdp = "",const char *protocol = "RTSP/1.0");
|
|
|
|
|
//服务器发送rtcp
|
|
|
|
|
void sendSenderReport(bool overTcp,int iTrackIndex);
|
|
|
|
|
//设置socket标志
|
|
|
|
|
void setSocketFlags();
|
2018-09-20 18:44:32 +08:00
|
|
|
|
private:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//用于判断客户端是否超时
|
|
|
|
|
Ticker _ticker;
|
|
|
|
|
//收到的seq,回复时一致
|
|
|
|
|
int _iCseq = 0;
|
|
|
|
|
//ContentBase
|
|
|
|
|
string _strContentBase;
|
|
|
|
|
//Session号
|
|
|
|
|
string _strSession;
|
|
|
|
|
//是否第一次播放,第一次播放需要鉴权,第二次播放属于暂停恢复
|
|
|
|
|
bool _bFirstPlay = true;
|
|
|
|
|
//url解析后保存的相关信息
|
2018-10-24 15:43:52 +08:00
|
|
|
|
MediaInfo _mediaInfo;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//rtsp播放器绑定的直播源
|
|
|
|
|
std::weak_ptr<RtspMediaSource> _pMediaSrc;
|
|
|
|
|
//直播源读取器
|
|
|
|
|
RingBuffer<RtpPacket::Ptr>::RingReader::Ptr _pRtpReader;
|
|
|
|
|
//推流或拉流客户端采用的rtp传输方式
|
|
|
|
|
Rtsp::eRtpType _rtpType = Rtsp::RTP_Invalid;
|
|
|
|
|
//sdp里面有效的track,包含音频或视频
|
|
|
|
|
vector<SdpTrack::Ptr> _aTrackInfo;
|
|
|
|
|
////////RTP over udp////////
|
|
|
|
|
//RTP端口,trackid idx 为数组下标
|
|
|
|
|
Socket::Ptr _apRtpSock[2];
|
|
|
|
|
//RTCP端口,trackid idx 为数组下标
|
|
|
|
|
Socket::Ptr _apRtcpSock[2];
|
|
|
|
|
//标记是否收到播放的udp打洞包,收到播放的udp打洞包后才能知道其外网udp端口号
|
2019-05-08 16:19:00 +08:00
|
|
|
|
unordered_set<int> _udpSockConnected;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
////////RTP over udp_multicast////////
|
|
|
|
|
//共享的rtp组播对象
|
|
|
|
|
RtpMultiCaster::Ptr _multicaster;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//登录认证
|
2018-10-24 15:43:52 +08:00
|
|
|
|
string _strNonce;
|
2018-12-14 14:59:12 +08:00
|
|
|
|
//消耗的总流量
|
|
|
|
|
uint64_t _ui64TotalBytes = 0;
|
2017-12-10 01:34:43 +08:00
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//RTSP over HTTP
|
|
|
|
|
//quicktime 请求rtsp会产生两次tcp连接,
|
|
|
|
|
//一次发送 get 一次发送post,需要通过x-sessioncookie关联起来
|
|
|
|
|
string _http_x_sessioncookie;
|
|
|
|
|
function<void(const Buffer::Ptr &pBuf)> _onRecv;
|
|
|
|
|
//是否开始发送rtp
|
2019-06-11 18:31:34 +08:00
|
|
|
|
bool _enableSendRtp;
|
2018-12-14 18:13:05 +08:00
|
|
|
|
//rtsp推流相关
|
2020-03-20 11:51:24 +08:00
|
|
|
|
RtspMediaSourceImp::Ptr _pushSrc;
|
|
|
|
|
//rtcp统计,trackid idx 为数组下标
|
|
|
|
|
RtcpCounter _aRtcpCnt[2];
|
|
|
|
|
//rtcp发送时间,trackid idx 为数组下标
|
|
|
|
|
Ticker _aRtcpTicker[2];
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-12-19 16:54:11 +08:00
|
|
|
|
/**
|
|
|
|
|
* 支持ssl加密的rtsp服务器,可用于诸如亚马逊echo show这样的设备访问
|
|
|
|
|
*/
|
|
|
|
|
typedef TcpSessionWithSSL<RtspSession> RtspSessionWithSSL;
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
#endif /* SESSION_RTSPSESSION_H_ */
|