RtspSession.h 7.49 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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.
xzl committed
25 26 27 28 29 30
 */

#ifndef SESSION_RTSPSESSION_H_
#define SESSION_RTSPSESSION_H_

#include <set>
xiongzilaing committed
31
#include <vector>
xzl committed
32
#include <unordered_map>
xiongziliang committed
33 34
#include "Util/util.h"
#include "Util/logger.h"
xiongziliang committed
35
#include "Common/config.h"
xiongziliang committed
36 37
#include "Network/TcpSession.h"
#include "Player/PlayerBase.h"
xzl committed
38 39 40
#include "Rtsp.h"
#include "RtpBroadCaster.h"
#include "RtspMediaSource.h"
xiongziliang committed
41
#include "RtspSplitter.h"
xiongziliang committed
42 43
#include "RtpReceiver.h"
#include "RtspToRtmpMediaSource.h"
xzl committed
44 45

using namespace std;
xiongziliang committed
46
using namespace toolkit;
47

xiongziliang committed
48
namespace mediakit {
xzl committed
49 50

class RtspSession;
51

52
class BufferRtp : public Buffer{
53 54 55 56 57
public:
    typedef std::shared_ptr<BufferRtp> Ptr;
    BufferRtp(const RtpPacket::Ptr & pkt,uint32_t offset = 0 ):_rtp(pkt),_offset(offset){}
    virtual ~BufferRtp(){}

58
    char *data() const override {
59 60 61 62 63 64 65 66 67 68
        return (char *)_rtp->payload + _offset;
    }
    uint32_t size() const override {
        return _rtp->length - _offset;
    }
private:
    RtpPacket::Ptr _rtp;
    uint32_t _offset;
};

xiongziliang committed
69
class RtspSession: public TcpSession, public RtspSplitter, public RtpReceiver , public MediaSourceEvent{
xzl committed
70 71
public:
	typedef std::shared_ptr<RtspSession> Ptr;
72 73 74 75 76
	typedef std::function<void(const string &realm)> onGetRealm;
    //encrypted为true是则表明是md5加密的密码,否则是明文密码
    //在请求明文密码时如果提供md5密码者则会导致认证失败
	typedef std::function<void(bool encrypted,const string &pwd_or_md5)> onAuth;

xiongziliang committed
77
	RtspSession(const Socket::Ptr &pSock);
xzl committed
78
	virtual ~RtspSession();
79
	void onRecv(const Buffer::Ptr &pBuf) override;
xzl committed
80 81
	void onError(const SockException &err) override;
	void onManager() override;
xiongziliang committed
82
protected:
xiongziliang committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	//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;

xiongziliang committed
104 105
	//RtpReceiver override
	void onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx) override;
106 107
	//MediaSourceEvent override
	bool close() override ;
108 109 110

	//TcpSession override
    int send(const Buffer::Ptr &pkt) override;
xzl committed
111
private:
xiongziliang committed
112 113 114 115 116 117 118 119 120 121 122
	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方法
xzl committed
123 124 125 126 127 128

	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实例
129
    inline void findStream(const function<void(bool)> &cb); //根据rtsp url查找 MediaSource实例
xzl committed
130

131 132 133
	inline string printSSRC(uint32_t ui32Ssrc);
	inline int getTrackIndexByTrackType(TrackType type);
    inline int getTrackIndexByControlSuffix(const string &controlSuffix);
xiongziliang committed
134
	inline int getTrackIndexByInterleaved(int interleaved);
135

136
	inline void onRcvPeerUdpData(int iTrackIdx, const Buffer::Ptr &pBuf, const struct sockaddr &addr);
137
	inline void startListenPeerUdpData(int iTrackIdx);
xzl committed
138

139 140 141 142 143 144 145
    //认证相关
    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);

146 147
    void doDelay(int delaySec,const std::function<void()> &fun);
    void cancelDelyaTask();
148 149 150 151

	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");
xiongziliang committed
152
private:
153
	Ticker _ticker;
154
	int _iCseq = 0;
xiongziliang committed
155
	string _strContentBase;
156 157 158 159 160 161
	string _strSdp;
	string _strSession;
	bool _bFirstPlay = true;
    MediaInfo _mediaInfo;
	std::weak_ptr<RtspMediaSource> _pMediaSrc;
	RingBuffer<RtpPacket::Ptr>::RingReader::Ptr _pRtpReader;
xiongziliang committed
162
	Rtsp::eRtpType _rtpType = Rtsp::RTP_Invalid;
xiongziliang committed
163 164
	vector<SdpTrack::Ptr> _aTrackInfo;

165
	//RTP over udp
166 167
	bool _bGotAllPeerUdp = false;
	bool _abGotPeerUdp[2] = { false, false }; //获取客户端udp端口计数
168 169
	Socket::Ptr _apRtpSock[2]; //RTP端口,trackid idx 为数组下标
	Socket::Ptr _apRtcpSock[2];//RTCP端口,trackid idx 为数组下标
170 171

	//RTP over udp_multicast
172
	RtpBroadCaster::Ptr _pBrdcaster;
xzl committed
173

174
	//登录认证
175
    string _strNonce;
176 177
    //消耗的总流量
    uint64_t _ui64TotalBytes = 0;
178

xzl committed
179 180
	//RTSP over HTTP
	//quicktime 请求rtsp会产生两次tcp连接,
181 182 183
	//一次发送 get 一次发送post,需要通过x-sessioncookie关联起来
	string _http_x_sessioncookie;
	function<void(const Buffer::Ptr &pBuf)> _onRecv;
xzl committed
184

185 186
    std::function<void()> _delayTask;
    uint32_t _iTaskTimeLine = 0;
187
    atomic<bool> _enableSendRtp;
188

xiongziliang committed
189 190 191
    //rtsp推流相关
	RtspToRtmpMediaSource::Ptr _pushSrc;

192 193 194 195 196
#ifdef RTSP_SEND_RTCP
	RtcpCounter _aRtcpCnt[2]; //rtcp统计,trackid idx 为数组下标
	Ticker _aRtcpTicker[2]; //rtcp发送时间,trackid idx 为数组下标
	inline void sendRTCP();
#endif
xzl committed
197 198
};

199 200 201 202 203
/**
 * 支持ssl加密的rtsp服务器,可用于诸如亚马逊echo show这样的设备访问
 */
typedef TcpSessionWithSSL<RtspSession> RtspSessionWithSSL;

xiongziliang committed
204
} /* namespace mediakit */
xzl committed
205 206

#endif /* SESSION_RTSPSESSION_H_ */