ZLMediaKit/src/Rtsp/RtspSession.h

229 lines
8.6 KiB
C++
Raw Normal View History

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/xia-chu/ZLMediaKit).
2017-09-27 16:20:30 +08:00
*
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"
#include "RtspMediaSourceImp.h"
2019-12-04 10:45:38 +08:00
#include "Common/Stamp.h"
2021-01-31 19:18:17 +08:00
#include "Rtcp/RtcpContext.h"
2017-04-01 16:35:56 +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
class BufferRtp : public toolkit::Buffer{
2018-01-30 09:35:54 +08:00
public:
using Ptr = std::shared_ptr<BufferRtp>;
BufferRtp(Buffer::Ptr pkt, size_t offset = 0) : _offset(offset), _rtp(std::move(pkt)) {}
~BufferRtp() override {}
2018-01-30 09:35:54 +08:00
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
}
2020-10-24 23:28:25 +08:00
size_t size() const override {
2019-06-24 16:07:44 +08:00
return _rtp->size() - _offset;
2018-01-30 09:35:54 +08:00
}
2020-10-24 23:28:25 +08:00
2018-01-30 09:35:54 +08:00
private:
size_t _offset;
2020-10-24 23:28:25 +08:00
Buffer::Ptr _rtp;
2018-01-30 09:35:54 +08:00
};
class RtspSession : public toolkit::TcpSession, public RtspSplitter, public RtpReceiver, public MediaSourceEvent {
2017-04-01 16:35:56 +08:00
public:
using Ptr = std::shared_ptr<RtspSession>;
using onGetRealm = std::function<void(const std::string &realm)>;
2017-12-10 01:34:43 +08:00
//encrypted为true是则表明是md5加密的密码否则是明文密码
//在请求明文密码时如果提供md5密码者则会导致认证失败
using onAuth = std::function<void(bool encrypted, const std::string &pwd_or_md5)>;
2020-03-20 11:51:24 +08:00
RtspSession(const toolkit::Socket::Ptr &sock);
2020-03-20 11:51:24 +08:00
virtual ~RtspSession();
////TcpSession override////
void onRecv(const toolkit::Buffer::Ptr &buf) override;
void onError(const toolkit::SockException &err) override;
2020-03-20 11:51:24 +08:00
void onManager() override;
2020-07-10 10:42:23 +08:00
2018-09-20 18:44:32 +08:00
protected:
2020-07-10 10:42:23 +08:00
/////RtspSplitter override/////
//收到完整的rtsp包回调包括sdp等content数据
2018-12-17 15:21:23 +08:00
void onWholeRtspPacket(Parser &parser) override;
2020-07-10 10:42:23 +08:00
//收到rtp包回调
void onRtpPacket(const char *data, size_t len) override;
2020-07-10 10:42:23 +08:00
//从rtsp头中获取Content长度
2021-01-19 16:05:38 +08:00
ssize_t getContentLength(Parser &parser) override;
2020-09-27 11:32:49 +08:00
2020-07-10 10:42:23 +08:00
////RtpReceiver override////
2021-02-05 11:28:50 +08:00
void onRtpSorted(RtpPacket::Ptr rtp, int track_idx) override;
2021-01-31 19:18:17 +08:00
void onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_index) override;
2020-09-27 11:32:49 +08:00
///////MediaSourceEvent override///////
// 关闭
bool close(MediaSource &sender, bool force) override;
// 播放总人数
2020-03-20 11:51:24 +08:00
int totalReaderCount(MediaSource &sender) override;
2020-09-27 11:32:49 +08:00
// 获取媒体源类型
MediaOriginType getOriginType(MediaSource &sender) const override;
// 获取媒体源url或者文件路径
std::string getOriginUrl(MediaSource &sender) const override;
2020-09-27 11:32:49 +08:00
// 获取媒体源客户端相关信息
std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const override;
2020-07-10 10:42:23 +08:00
/////TcpSession override////
ssize_t send(toolkit::Buffer::Ptr pkt) override;
2020-07-10 10:42:23 +08:00
//收到RTCP包回调
virtual void onRtcpPacket(int track_idx, SdpTrack::Ptr &track, const char *data, size_t len);
2019-05-08 14:23:18 +08:00
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资源未找到
2020-07-10 10:42:23 +08:00
void send_StreamNotFound();
2020-03-20 11:51:24 +08:00
//不支持的传输模式
2020-07-10 10:42:23 +08:00
void send_UnsupportedTransport();
2020-03-20 11:51:24 +08:00
//会话id错误
2020-07-10 10:42:23 +08:00
void send_SessionNotFound();
2020-03-20 11:51:24 +08:00
//一般rtsp服务器打开端口失败时触发
2020-07-10 10:42:23 +08:00
void send_NotAcceptable();
2020-03-20 11:51:24 +08:00
//获取track下标
2020-07-10 10:42:23 +08:00
int getTrackIndexByTrackType(TrackType type);
int getTrackIndexByControlUrl(const std::string &control_url);
2020-07-10 10:42:23 +08:00
int getTrackIndexByInterleaved(int interleaved);
2020-03-20 11:51:24 +08:00
//一般用于接收udp打洞包也用于rtsp推流
2022-05-08 00:26:01 +08:00
void onRcvPeerUdpData(int interleaved, const toolkit::Buffer::Ptr &buf, const struct sockaddr_storage &addr);
2020-03-20 11:51:24 +08:00
//配合onRcvPeerUdpData使用
2020-07-10 10:42:23 +08:00
void startListenPeerUdpData(int track_idx);
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
//认证失败
void onAuthFailed(const std::string &realm, const std::string &why, bool close = true);
2020-03-20 11:51:24 +08:00
//开始走rtsp专有认证流程
void onAuthUser(const std::string &realm, const std::string &authorization);
2020-03-20 11:51:24 +08:00
//校验base64方式的认证加密
void onAuthBasic(const std::string &realm, const std::string &auth_base64);
2020-03-20 11:51:24 +08:00
//校验md5方式的认证加密
void onAuthDigest(const std::string &realm, const std::string &auth_md5);
2020-05-25 18:22:21 +08:00
//触发url鉴权事件
void emitOnPlay();
2020-03-20 11:51:24 +08:00
//发送rtp给客户端
2020-04-07 13:03:53 +08:00
void sendRtpPacket(const RtspMediaSource::RingDataType &pkt);
2020-07-10 10:42:23 +08:00
//触发rtcp发送
2021-01-31 19:18:17 +08:00
void updateRtcpContext(const RtpPacket::Ptr &rtp);
2020-03-20 11:51:24 +08:00
//回复客户端
bool sendRtspResponse(const std::string &res_code, const std::initializer_list<std::string> &header, const std::string &sdp = "", const char *protocol = "RTSP/1.0");
bool sendRtspResponse(const std::string &res_code, const StrCaseMap &header = StrCaseMap(), const std::string &sdp = "", const char *protocol = "RTSP/1.0");
2021-01-31 19:18:17 +08:00
2020-03-20 11:51:24 +08:00
//设置socket标志
void setSocketFlags();
2020-07-10 10:42:23 +08:00
2018-09-20 18:44:32 +08:00
private:
2020-07-10 10:42:23 +08:00
//是否已经触发on_play事件
bool _emit_on_play = false;
bool _send_sr_rtcp[2] = {true, true};
//断连续推延时
uint32_t _continue_push_ms = 0;
2020-07-10 10:42:23 +08:00
//推流或拉流客户端采用的rtp传输方式
Rtsp::eRtpType _rtp_type = Rtsp::RTP_Invalid;
2020-03-20 11:51:24 +08:00
//收到的seq回复时一致
2020-07-10 10:42:23 +08:00
int _cseq = 0;
//消耗的总流量
uint64_t _bytes_usage = 0;
2020-03-20 11:51:24 +08:00
//ContentBase
std::string _content_base;
2020-03-20 11:51:24 +08:00
//Session号
std::string _sessionid;
2020-05-25 18:22:21 +08:00
//记录是否需要rtsp专属鉴权防止重复触发事件
std::string _rtsp_realm;
2020-07-10 10:42:23 +08:00
//登录认证
std::string _auth_nonce;
2020-07-10 10:42:23 +08:00
//用于判断客户端是否超时
toolkit::Ticker _alive_ticker;
2020-07-10 10:42:23 +08:00
2020-03-20 11:51:24 +08:00
//url解析后保存的相关信息
2020-07-10 10:42:23 +08:00
MediaInfo _media_info;
//rtsp推流相关绑定的源
RtspMediaSourceImp::Ptr _push_src;
//推流器所有权
std::shared_ptr<void> _push_src_ownership;
2020-03-20 11:51:24 +08:00
//rtsp播放器绑定的直播源
2020-07-10 10:42:23 +08:00
std::weak_ptr<RtspMediaSource> _play_src;
2020-03-20 11:51:24 +08:00
//直播源读取器
2020-07-10 10:42:23 +08:00
RtspMediaSource::RingType::RingReader::Ptr _play_reader;
2020-03-20 11:51:24 +08:00
//sdp里面有效的track,包含音频或视频
std::vector<SdpTrack::Ptr> _sdp_track;
2020-07-10 10:42:23 +08:00
2020-03-20 11:51:24 +08:00
////////RTP over udp////////
//RTP端口,trackid idx 为数组下标
toolkit::Socket::Ptr _rtp_socks[2];
2020-03-20 11:51:24 +08:00
//RTCP端口,trackid idx 为数组下标
toolkit::Socket::Ptr _rtcp_socks[2];
2020-03-20 11:51:24 +08:00
//标记是否收到播放的udp打洞包,收到播放的udp打洞包后才能知道其外网udp端口号
std::unordered_set<int> _udp_connected_flags;
2020-03-20 11:51:24 +08:00
////////RTP over udp_multicast////////
//共享的rtp组播对象
RtpMultiCaster::Ptr _multicaster;
2020-07-10 10:42:23 +08:00
////////RTSP over HTTP ////////
2020-03-20 11:51:24 +08:00
//quicktime 请求rtsp会产生两次tcp连接
//一次发送 get 一次发送post需要通过x-sessioncookie关联起来
std::string _http_x_sessioncookie;
std::function<void(const toolkit::Buffer::Ptr &)> _on_recv;
2021-01-31 19:18:17 +08:00
////////// rtcp ////////////////
//rtcp发送时间,trackid idx 为数组下标
toolkit::Ticker _rtcp_send_tickers[2];
2021-01-31 19:18:17 +08:00
//统计rtp并发送rtcp
std::vector<RtcpContext::Ptr> _rtcp_context;
2017-04-01 16:35:56 +08:00
};
2018-12-19 16:54:11 +08:00
/**
* ssl加密的rtsp服务器echo show这样的设备访问
*/
using RtspSessionWithSSL = toolkit::TcpSessionWithSSL<RtspSession>;
2018-12-19 16:54:11 +08:00
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */
2017-04-01 16:35:56 +08:00
#endif /* SESSION_RTSPSESSION_H_ */