ZLMediaKit/src/Rtsp/RtspSession.h

217 lines
7.7 KiB
C
Raw Normal View History

2018-12-21 17:12:26 +08:00
/*
2017-09-27 16:20:30 +08:00
* MIT License
2017-04-01 16:35:56 +08:00
*
2017-09-27 16:20:30 +08:00
* 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.
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>
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"
2017-04-01 16:35:56 +08:00
#include "Rtsp.h"
#include "RtpBroadCaster.h"
#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"
#include "RtspToRtmpMediaSource.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 {
2018-01-30 09:35:54 +08:00
return (char *)_rtp->payload + _offset;
}
uint32_t size() const override {
return _rtp->length - _offset;
}
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:
typedef std::shared_ptr<RtspSession> Ptr;
2017-12-10 01:34:43 +08:00
typedef std::function<void(const string &realm)> onGetRealm;
//encrypted为true是则表明是md5加密的密码否则是明文密码
//在请求明文密码时如果提供md5密码者则会导致认证失败
typedef std::function<void(bool encrypted,const string &pwd_or_md5)> onAuth;
2018-12-20 10:42:51 +08:00
RtspSession(const Socket::Ptr &pSock);
2017-04-01 16:35:56 +08:00
virtual ~RtspSession();
2018-02-23 15:36:51 +08:00
void onRecv(const Buffer::Ptr &pBuf) override;
2017-04-01 16:35:56 +08:00
void onError(const SockException &err) override;
void onManager() override;
2018-09-20 18:44:32 +08:00
protected:
2018-12-17 15:21:23 +08:00
//RtspSplitter override
/**
* 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;
2018-12-14 18:13:05 +08:00
//RtpReceiver override
void onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx) override;
//MediaSourceEvent override
bool close() override ;
2018-12-19 16:54:11 +08:00
//TcpSession override
int send(const Buffer::Ptr &pkt) override;
2019-05-08 14:23:18 +08:00
/**
* RTCP包回调
* @param iTrackidx
* @param track
* @param pucData
* @param uiLen
*/
void onRecvRtcp(int iTrackidx,SdpTrack::Ptr &track, unsigned char *pucData, unsigned int uiLen);
2017-04-01 16:35:56 +08:00
private:
2018-12-17 15:21:23 +08:00
bool handleReq_Options(const Parser &parser); //处理options方法
bool handleReq_Describe(const Parser &parser); //处理describe方法
bool handleReq_ANNOUNCE(const Parser &parser); //处理options方法
bool handleReq_RECORD(const Parser &parser); //处理options方法
bool handleReq_Setup(const Parser &parser); //处理setup方法
bool handleReq_Play(const Parser &parser); //处理play方法
bool handleReq_Pause(const Parser &parser); //处理pause方法
bool handleReq_Teardown(const Parser &parser); //处理teardown方法
bool handleReq_Get(const Parser &parser); //处理Get方法
bool handleReq_Post(const Parser &parser); //处理Post方法
bool handleReq_SET_PARAMETER(const Parser &parser); //处理SET_PARAMETER方法
2017-04-01 16:35:56 +08:00
void inline send_StreamNotFound(); //rtsp资源未找到
void inline send_UnsupportedTransport(); //不支持的传输模式
void inline send_SessionNotFound(); //会话id错误
void inline send_NotAcceptable(); //rtsp同时播放数限制
inline bool findStream(); //根据rtsp url查找 MediaSource实例
inline void findStream(const function<void(bool)> &cb); //根据rtsp url查找 MediaSource实例
2017-04-01 16:35:56 +08:00
inline string printSSRC(uint32_t ui32Ssrc);
inline int getTrackIndexByTrackType(TrackType type);
inline int getTrackIndexByControlSuffix(const string &controlSuffix);
2018-12-17 15:21:23 +08:00
inline int getTrackIndexByInterleaved(int interleaved);
2018-03-26 18:56:22 +08:00
2019-05-08 14:23:18 +08:00
inline void onRcvPeerUdpData(int intervaled, const Buffer::Ptr &pBuf, const struct sockaddr &addr);
inline void startListenPeerUdpData(int iTrackIdx);
2017-04-01 16:35:56 +08:00
2017-12-10 01:34:43 +08:00
//认证相关
static void onAuthSuccess(const weak_ptr<RtspSession> &weakSelf);
static void onAuthFailed(const weak_ptr<RtspSession> &weakSelf,const string &realm);
static void onAuthUser(const weak_ptr<RtspSession> &weakSelf,const string &realm,const string &authorization);
static void onAuthBasic(const weak_ptr<RtspSession> &weakSelf,const string &realm,const string &strBase64);
static void onAuthDigest(const weak_ptr<RtspSession> &weakSelf,const string &realm,const string &strMd5);
void doDelay(int delaySec,const std::function<void()> &fun);
void cancelDelyaTask();
inline void sendRtpPacket(const RtpPacket::Ptr &pkt);
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");
2018-09-20 18:44:32 +08:00
private:
2018-10-24 15:43:52 +08:00
Ticker _ticker;
int _iCseq = 0;
2018-12-21 17:12:26 +08:00
string _strContentBase;
2018-10-24 15:43:52 +08:00
string _strSdp;
string _strSession;
bool _bFirstPlay = true;
MediaInfo _mediaInfo;
std::weak_ptr<RtspMediaSource> _pMediaSrc;
RingBuffer<RtpPacket::Ptr>::RingReader::Ptr _pRtpReader;
2019-03-27 18:41:52 +08:00
Rtsp::eRtpType _rtpType = Rtsp::RTP_Invalid;
2018-10-26 09:56:29 +08:00
vector<SdpTrack::Ptr> _aTrackInfo;
//RTP over udp
2018-10-24 15:43:52 +08:00
bool _bGotAllPeerUdp = false;
bool _abGotPeerUdp[2] = { false, false }; //获取客户端udp端口计数
Socket::Ptr _apRtpSock[2]; //RTP端口,trackid idx 为数组下标
Socket::Ptr _apRtcpSock[2];//RTCP端口,trackid idx 为数组下标
//RTP over udp_multicast
2018-10-24 15:43:52 +08:00
RtpBroadCaster::Ptr _pBrdcaster;
2017-04-01 16:35:56 +08:00
2017-12-10 01:34:43 +08:00
//登录认证
2018-10-24 15:43:52 +08:00
string _strNonce;
//消耗的总流量
uint64_t _ui64TotalBytes = 0;
2017-12-10 01:34:43 +08:00
2017-04-01 16:35:56 +08:00
//RTSP over HTTP
//quicktime 请求rtsp会产生两次tcp连接
//一次发送 get 一次发送post需要通过x-sessioncookie关联起来
string _http_x_sessioncookie;
function<void(const Buffer::Ptr &pBuf)> _onRecv;
2017-04-01 16:35:56 +08:00
std::function<void()> _delayTask;
uint32_t _iTaskTimeLine = 0;
atomic<bool> _enableSendRtp;
2018-12-14 18:13:05 +08:00
//rtsp推流相关
RtspToRtmpMediaSource::Ptr _pushSrc;
#ifdef RTSP_SEND_RTCP
RtcpCounter _aRtcpCnt[2]; //rtcp统计,trackid idx 为数组下标
Ticker _aRtcpTicker[2]; //rtcp发送时间,trackid idx 为数组下标
inline void sendRTCP();
#endif
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_ */