2018-09-23 00:55:00 +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>
|
2017-05-02 17:15:12 +08:00
|
|
|
|
#include "Common/config.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include "Rtsp.h"
|
|
|
|
|
#include "RtpBroadCaster.h"
|
|
|
|
|
#include "RtspMediaSource.h"
|
|
|
|
|
#include "Player/PlayerBase.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Util/util.h"
|
|
|
|
|
#include "Util/logger.h"
|
2018-02-23 15:36:51 +08:00
|
|
|
|
#include "Network/TcpSession.h"
|
2018-09-20 18:44:32 +08:00
|
|
|
|
#include "Http/HttpRequestSplitter.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace ZL::Util;
|
|
|
|
|
using namespace ZL::Rtsp;
|
|
|
|
|
using namespace ZL::Player;
|
|
|
|
|
using namespace ZL::Network;
|
2018-09-25 09:55:41 +08:00
|
|
|
|
using namespace ZL::Http;
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
namespace ZL {
|
|
|
|
|
namespace Rtsp {
|
|
|
|
|
|
|
|
|
|
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-09-20 18:44:32 +08:00
|
|
|
|
class RtspSession: public TcpSession, public HttpRequestSplitter {
|
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;
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
RtspSession(const std::shared_ptr<ThreadPool> &pTh, const Socket::Ptr &pSock);
|
|
|
|
|
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:
|
|
|
|
|
//HttpRequestSplitter override
|
2018-09-23 00:55:00 +08:00
|
|
|
|
int64_t onRecvHeader(const char *data,uint64_t len) override ;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
private:
|
2018-09-23 00:55:00 +08:00
|
|
|
|
void inputRtspOrRtcp(const char *data,uint64_t len);
|
2018-02-23 15:36:51 +08:00
|
|
|
|
int send(const Buffer::Ptr &pkt) override{
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_ui64TotalBytes += pkt->size();
|
|
|
|
|
return _pSender->send(pkt,_flags);
|
2018-01-30 09:35:54 +08:00
|
|
|
|
}
|
2018-09-14 18:04:41 +08:00
|
|
|
|
void shutdown() override ;
|
|
|
|
|
void shutdown_l(bool close);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
bool handleReq_Options(); //处理options方法
|
|
|
|
|
bool handleReq_Describe(); //处理describe方法
|
|
|
|
|
bool handleReq_Setup(); //处理setup方法
|
|
|
|
|
bool handleReq_Play(); //处理play方法
|
|
|
|
|
bool handleReq_Pause(); //处理pause方法
|
|
|
|
|
bool handleReq_Teardown(); //处理teardown方法
|
|
|
|
|
bool handleReq_Get(); //处理Get方法
|
|
|
|
|
bool handleReq_Post(); //处理Post方法
|
|
|
|
|
bool handleReq_SET_PARAMETER(); //处理SET_PARAMETER方法
|
|
|
|
|
|
|
|
|
|
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 initSender(const std::shared_ptr<RtspSession> &pSession); //处理rtsp over http,quicktime使用的
|
2018-01-30 09:35:54 +08:00
|
|
|
|
inline void sendRtpPacket(const RtpPacket::Ptr &pkt);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
inline string printSSRC(uint32_t ui32Ssrc) {
|
|
|
|
|
char tmp[9] = { 0 };
|
|
|
|
|
ui32Ssrc = htonl(ui32Ssrc);
|
|
|
|
|
uint8_t *pSsrc = (uint8_t *) &ui32Ssrc;
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
sprintf(tmp + 2 * i, "%02X", pSsrc[i]);
|
|
|
|
|
}
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
2018-07-05 18:48:08 +08:00
|
|
|
|
inline int getTrackIndexByTrackType(TrackType type) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
for (unsigned int i = 0; i < _uiTrackCnt; i++) {
|
|
|
|
|
if (type == _aTrackInfo[i].type) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2018-03-26 18:56:22 +08:00
|
|
|
|
inline int getTrackIndexByControlSuffix(const string &controlSuffix) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
for (unsigned int i = 0; i < _uiTrackCnt; i++) {
|
|
|
|
|
if (controlSuffix == _aTrackInfo[i].controlSuffix) {
|
2018-03-26 18:56:22 +08:00
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-23 15:36:51 +08:00
|
|
|
|
inline void onRcvPeerUdpData(int iTrackIdx, const Buffer::Ptr &pBuf, const struct sockaddr &addr);
|
2017-11-08 16:56:02 +08:00
|
|
|
|
inline void startListenPeerUdpData();
|
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);
|
|
|
|
|
|
2018-09-20 18:44:32 +08:00
|
|
|
|
private:
|
2018-10-24 15:43:52 +08:00
|
|
|
|
char *_pcBuf = nullptr;
|
|
|
|
|
Ticker _ticker;
|
|
|
|
|
Parser _parser; //rtsp解析类
|
|
|
|
|
string _strUrl;
|
|
|
|
|
string _strSdp;
|
|
|
|
|
string _strSession;
|
|
|
|
|
bool _bFirstPlay = true;
|
|
|
|
|
MediaInfo _mediaInfo;
|
|
|
|
|
std::weak_ptr<RtspMediaSource> _pMediaSrc;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
//RTP缓冲
|
2018-10-24 15:43:52 +08:00
|
|
|
|
weak_ptr<RingBuffer<RtpPacket::Ptr> > _pWeakRing;
|
|
|
|
|
RingBuffer<RtpPacket::Ptr>::RingReader::Ptr _pRtpReader;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
PlayerBase::eRtpType _rtpType = PlayerBase::RTP_UDP;
|
|
|
|
|
bool _bSetUped = false;
|
|
|
|
|
int _iCseq = 0;
|
|
|
|
|
unsigned int _uiTrackCnt = 0; //媒体track个数
|
|
|
|
|
RtspTrack _aTrackInfo[2]; //媒体track信息,trackid idx 为数组下标
|
|
|
|
|
bool _bGotAllPeerUdp = false;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
#ifdef RTSP_SEND_RTCP
|
2018-10-24 15:43:52 +08:00
|
|
|
|
RtcpCounter _aRtcpCnt[2]; //rtcp统计,trackid idx 为数组下标
|
|
|
|
|
Ticker _aRtcpTicker[2]; //rtcp发送时间,trackid idx 为数组下标
|
2017-04-01 16:35:56 +08:00
|
|
|
|
inline void sendRTCP();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//RTP over UDP
|
2018-10-24 15:43:52 +08:00
|
|
|
|
bool _abGotPeerUdp[2] = { false, false }; //获取客户端udp端口计数
|
|
|
|
|
weak_ptr<Socket> _apUdpSock[2]; //发送RTP的UDP端口,trackid idx 为数组下标
|
|
|
|
|
std::shared_ptr<struct sockaddr> _apPeerUdpAddr[2]; //播放器接收RTP的地址,trackid idx 为数组下标
|
|
|
|
|
bool _bListenPeerUdpData = false;
|
|
|
|
|
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;
|
2017-12-10 01:34:43 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//RTSP over HTTP
|
2018-10-24 15:43:52 +08:00
|
|
|
|
function<void(void)> _onDestory;
|
|
|
|
|
bool _bBase64need = false; //是否需要base64解码
|
|
|
|
|
Socket::Ptr _pSender; //回复rtsp时走的tcp通道,供quicktime用
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//quicktime 请求rtsp会产生两次tcp连接,
|
|
|
|
|
//一次发送 get 一次发送post,需要通过sessioncookie关联起来
|
2018-10-24 15:43:52 +08:00
|
|
|
|
string _strSessionCookie;
|
2018-02-06 15:28:27 +08:00
|
|
|
|
|
|
|
|
|
//消耗的总流量
|
2018-10-24 15:43:52 +08:00
|
|
|
|
uint64_t _ui64TotalBytes = 0;
|
2018-02-06 15:28:27 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
static recursive_mutex g_mtxGetter; //对quicktime上锁保护
|
|
|
|
|
static recursive_mutex g_mtxPostter; //对quicktime上锁保护
|
|
|
|
|
static unordered_map<string, weak_ptr<RtspSession> > g_mapGetter;
|
|
|
|
|
static unordered_map<void *, std::shared_ptr<RtspSession> > g_mapPostter;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} /* namespace Session */
|
|
|
|
|
} /* namespace ZL */
|
|
|
|
|
|
|
|
|
|
#endif /* SESSION_RTSPSESSION_H_ */
|