RtspSession.h 8.24 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
xiongziliang committed
6 7 8
 * 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.
xzl committed
9 10 11 12 13 14
 */

#ifndef SESSION_RTSPSESSION_H_
#define SESSION_RTSPSESSION_H_

#include <set>
xiongzilaing committed
15
#include <vector>
16
#include <unordered_set>
xzl committed
17
#include <unordered_map>
xiongziliang committed
18 19
#include "Util/util.h"
#include "Util/logger.h"
xiongziliang committed
20
#include "Common/config.h"
xiongziliang committed
21 22
#include "Network/TcpSession.h"
#include "Player/PlayerBase.h"
xiongziliang committed
23
#include "RtpMultiCaster.h"
xzl committed
24
#include "RtspMediaSource.h"
xiongziliang committed
25
#include "RtspSplitter.h"
xiongziliang committed
26
#include "RtpReceiver.h"
27
#include "RtspMediaSourceImp.h"
xiongziliang committed
28
#include "Common/Stamp.h"
xiongziliang committed
29
#include "Rtcp/RtcpContext.h"
xzl committed
30 31

using namespace std;
xiongziliang committed
32
using namespace toolkit;
33

xiongziliang committed
34
namespace mediakit {
xzl committed
35 36

class RtspSession;
37

38
class BufferRtp : public Buffer{
39 40
public:
    typedef std::shared_ptr<BufferRtp> Ptr;
41
    BufferRtp(Buffer::Ptr pkt, size_t offset = 0) : _offset(offset),_rtp(std::move(pkt)) {}
xiongziliang committed
42
    ~BufferRtp() override{}
43

44
    char *data() const override {
xiongziliang committed
45
        return (char *)_rtp->data() + _offset;
46
    }
xiongziliang committed
47

48
    size_t size() const override {
xiongziliang committed
49
        return _rtp->size() - _offset;
50
    }
xiongziliang committed
51

52
private:
53
    size_t _offset;
xiongziliang committed
54
    Buffer::Ptr _rtp;
55 56
};

xiongziliang committed
57
class RtspSession: public TcpSession, public RtspSplitter, public RtpReceiver , public MediaSourceEvent{
xzl committed
58
public:
59 60
    typedef std::shared_ptr<RtspSession> Ptr;
    typedef std::function<void(const string &realm)> onGetRealm;
61 62
    //encrypted为true是则表明是md5加密的密码,否则是明文密码
    //在请求明文密码时如果提供md5密码者则会导致认证失败
63 64
    typedef std::function<void(bool encrypted,const string &pwd_or_md5)> onAuth;

65
    RtspSession(const Socket::Ptr &sock);
66 67
    virtual ~RtspSession();
    ////TcpSession override////
68
    void onRecv(const Buffer::Ptr &buf) override;
69 70
    void onError(const SockException &err) override;
    void onManager() override;
71

xiongziliang committed
72
protected:
73 74
    /////RtspSplitter override/////
    //收到完整的rtsp包回调,包括sdp等content数据
xiongziliang committed
75
    void onWholeRtspPacket(Parser &parser) override;
76
    //收到rtp包回调
77
    void onRtpPacket(const char *data, size_t len) override;
78
    //从rtsp头中获取Content长度
79
    ssize_t getContentLength(Parser &parser) override;
80

81
    ////RtpReceiver override////
xia-chu committed
82
    void onRtpSorted(RtpPacket::Ptr rtp, int track_idx) override;
xiongziliang committed
83
    void onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_index) override;
84 85 86 87 88

    ///////MediaSourceEvent override///////
    // 关闭
    bool close(MediaSource &sender, bool force) override;
    // 播放总人数
89
    int totalReaderCount(MediaSource &sender) override;
90 91 92 93 94 95 96
    // 获取媒体源类型
    MediaOriginType getOriginType(MediaSource &sender) const override;
    // 获取媒体源url或者文件路径
    string getOriginUrl(MediaSource &sender) const override;
    // 获取媒体源客户端相关信息
    std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const override;

97
    /////TcpSession override////
98
    ssize_t send(Buffer::Ptr pkt) override;
99
    //收到RTCP包回调
100
    virtual void onRtcpPacket(int track_idx, SdpTrack::Ptr &track, const char *data, size_t len);
101

xzl committed
102
private:
103
    //处理options方法,获取服务器能力
xiongziliang committed
104
    void handleReq_Options(const Parser &parser);
105
    //处理describe方法,请求服务器rtsp sdp信息
xiongziliang committed
106
    void handleReq_Describe(const Parser &parser);
107
    //处理ANNOUNCE方法,请求推流,附带sdp
xiongziliang committed
108
    void handleReq_ANNOUNCE(const Parser &parser);
109
    //处理record方法,开始推流
xiongziliang committed
110
    void handleReq_RECORD(const Parser &parser);
111
    //处理setup方法,播放和推流协商rtp传输方式用
xiongziliang committed
112
    void handleReq_Setup(const Parser &parser);
113
    //处理play方法,开始或恢复播放
xiongziliang committed
114
    void handleReq_Play(const Parser &parser);
115
    //处理pause方法,暂停播放
xiongziliang committed
116
    void handleReq_Pause(const Parser &parser);
117
    //处理teardown方法,结束播放
xiongziliang committed
118
    void handleReq_Teardown(const Parser &parser);
119
    //处理Get方法,rtp over http才用到
xiongziliang committed
120
    void handleReq_Get(const Parser &parser);
121
    //处理Post方法,rtp over http才用到
xiongziliang committed
122
    void handleReq_Post(const Parser &parser);
123
    //处理SET_PARAMETER、GET_PARAMETER方法,一般用于心跳
xiongziliang committed
124
    void handleReq_SET_PARAMETER(const Parser &parser);
125
    //rtsp资源未找到
126
    void send_StreamNotFound();
127
    //不支持的传输模式
128
    void send_UnsupportedTransport();
129
    //会话id错误
130
    void send_SessionNotFound();
131
    //一般rtsp服务器打开端口失败时触发
132
    void send_NotAcceptable();
133
    //获取track下标
134 135 136
    int getTrackIndexByTrackType(TrackType type);
    int getTrackIndexByControlSuffix(const string &control_suffix);
    int getTrackIndexByInterleaved(int interleaved);
137
    //一般用于接收udp打洞包,也用于rtsp推流
138
    void onRcvPeerUdpData(int interleaved, const Buffer::Ptr &buf, const struct sockaddr &addr);
139
    //配合onRcvPeerUdpData使用
140
    void startListenPeerUdpData(int track_idx);
xiongziliang committed
141
    ////rtsp专有认证相关////
142
    //认证成功
143
    void onAuthSuccess();
144
    //认证失败
145
    void onAuthFailed(const string &realm, const string &why, bool close = true);
146
    //开始走rtsp专有认证流程
147
    void onAuthUser(const string &realm, const string &authorization);
148
    //校验base64方式的认证加密
149
    void onAuthBasic(const string &realm, const string &auth_base64);
150
    //校验md5方式的认证加密
151
    void onAuthDigest(const string &realm, const string &auth_md5);
xiongziliang committed
152 153
    //触发url鉴权事件
    void emitOnPlay();
154
    //发送rtp给客户端
155
    void sendRtpPacket(const RtspMediaSource::RingDataType &pkt);
156
    //触发rtcp发送
xiongziliang committed
157
    void updateRtcpContext(const RtpPacket::Ptr &rtp);
158
    //回复客户端
159 160
    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
161

162 163
    //设置socket标志
    void setSocketFlags();
164

xiongziliang committed
165
private:
166 167 168 169 170 171
    //是否已经触发on_play事件
    bool _emit_on_play = false;
    //是否开始发送rtp
    bool _enable_send_rtp;
    //推流或拉流客户端采用的rtp传输方式
    Rtsp::eRtpType _rtp_type = Rtsp::RTP_Invalid;
172
    //收到的seq,回复时一致
173 174 175
    int _cseq = 0;
    //消耗的总流量
    uint64_t _bytes_usage = 0;
176
    //ContentBase
177
    string _content_base;
178
    //Session号
179
    string _sessionid;
xiongziliang committed
180 181
    //记录是否需要rtsp专属鉴权,防止重复触发事件
    string _rtsp_realm;
182 183 184 185 186
    //登录认证
    string _auth_nonce;
    //用于判断客户端是否超时
    Ticker _alive_ticker;

187
    //url解析后保存的相关信息
188 189 190
    MediaInfo _media_info;
    //rtsp推流相关绑定的源
    RtspMediaSourceImp::Ptr _push_src;
191
    //rtsp播放器绑定的直播源
192
    std::weak_ptr<RtspMediaSource> _play_src;
193
    //直播源读取器
194
    RtspMediaSource::RingType::RingReader::Ptr _play_reader;
195
    //sdp里面有效的track,包含音频或视频
196 197
    vector<SdpTrack::Ptr> _sdp_track;

198 199
    ////////RTP over udp////////
    //RTP端口,trackid idx 为数组下标
200
    Socket::Ptr _rtp_socks[2];
201
    //RTCP端口,trackid idx 为数组下标
202
    Socket::Ptr _rtcp_socks[2];
203
    //标记是否收到播放的udp打洞包,收到播放的udp打洞包后才能知道其外网udp端口号
204
    unordered_set<int> _udp_connected_flags;
205 206 207
    ////////RTP over udp_multicast////////
    //共享的rtp组播对象
    RtpMultiCaster::Ptr _multicaster;
208
    ////////RTSP over HTTP  ////////
209 210 211
    //quicktime 请求rtsp会产生两次tcp连接,
    //一次发送 get 一次发送post,需要通过x-sessioncookie关联起来
    string _http_x_sessioncookie;
212
    function<void(const Buffer::Ptr &)> _on_recv;
xiongziliang committed
213 214 215 216 217
    ////////// rtcp ////////////////
    //rtcp发送时间,trackid idx 为数组下标
    Ticker _rtcp_send_tickers[2];
    //统计rtp并发送rtcp
    vector<RtcpContext::Ptr> _rtcp_context;
xzl committed
218 219
};

220 221 222 223 224
/**
 * 支持ssl加密的rtsp服务器,可用于诸如亚马逊echo show这样的设备访问
 */
typedef TcpSessionWithSSL<RtspSession> RtspSessionWithSSL;

xiongziliang committed
225
} /* namespace mediakit */
xzl committed
226 227

#endif /* SESSION_RTSPSESSION_H_ */