mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-07-02 08:17:33 +08:00
整理命名空间 (#1409)
* feat: remove using namespace mediakit in header files. (cherry picked from commit d44aeb339a8a0e1f0455be82b21fe4b1b536299f) * feat: remove using namespace mediakit in FFmpegSource.h * feat: remove using namespace mediakit in RtpExt.h * feat: remove using namespace mediakit in header files. * feat: remove using namespace std in header files. * feat: remove using namespace std in header files when zltoolkit remove std in header * 补充命名空间 * 整理命名空间 * 整理命名空间2 * 修复macos ci * 修复编译问题 * 修复编译问题2 * 修复编译问题3 Co-authored-by: Johnny <hellojinqiang@gmail.com> Co-authored-by: Xiaofeng Wang <wasphin@gmail.com>
This commit is contained in:
@@ -14,32 +14,31 @@
|
||||
#include <memory>
|
||||
#include "Util/RingBuffer.h"
|
||||
#include "Player/PlayerBase.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
namespace mediakit {
|
||||
|
||||
class RtpRing{
|
||||
class RtpRing {
|
||||
public:
|
||||
typedef std::shared_ptr<RtpRing> Ptr;
|
||||
typedef RingBuffer<RtpPacket::Ptr> RingType;
|
||||
using Ptr = std::shared_ptr<RtpRing>;
|
||||
using RingType = toolkit::RingBuffer<RtpPacket::Ptr>;
|
||||
|
||||
RtpRing(){}
|
||||
virtual ~RtpRing(){}
|
||||
RtpRing() = default;
|
||||
virtual ~RtpRing() = default;
|
||||
|
||||
/**
|
||||
* 获取rtp环形缓存
|
||||
* @return
|
||||
*/
|
||||
virtual RingType::Ptr getRtpRing() const {
|
||||
return _rtpRing;
|
||||
return _ring;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置rtp环形缓存
|
||||
* @param ring
|
||||
*/
|
||||
virtual void setRtpRing(const RingType::Ptr &ring){
|
||||
_rtpRing = ring;
|
||||
virtual void setRtpRing(const RingType::Ptr &ring) {
|
||||
_ring = ring;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,14 +47,15 @@ public:
|
||||
* @param key_pos 是否为关键帧第一个rtp包
|
||||
* @return 是否为关键帧第一个rtp包
|
||||
*/
|
||||
virtual bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos){
|
||||
if(_rtpRing){
|
||||
_rtpRing->write(rtp,key_pos);
|
||||
virtual bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
|
||||
if (_ring) {
|
||||
_ring->write(rtp, key_pos);
|
||||
}
|
||||
return key_pos;
|
||||
}
|
||||
|
||||
protected:
|
||||
RingType::Ptr _rtpRing;
|
||||
RingType::Ptr _ring;
|
||||
};
|
||||
|
||||
class RtpInfo{
|
||||
@@ -95,11 +95,12 @@ private:
|
||||
size_t _mtu_size;
|
||||
};
|
||||
|
||||
class RtpCodec : public RtpRing, public FrameDispatcher , public CodecInfo{
|
||||
class RtpCodec : public RtpRing, public FrameDispatcher, public CodecInfo {
|
||||
public:
|
||||
typedef std::shared_ptr<RtpCodec> Ptr;
|
||||
RtpCodec(){}
|
||||
virtual ~RtpCodec(){}
|
||||
using Ptr = std::shared_ptr<RtpCodec>;
|
||||
|
||||
RtpCodec() = default;
|
||||
~RtpCodec() override = default;
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
@@ -19,8 +19,6 @@
|
||||
#include "RtspMediaSource.h"
|
||||
#include "Util/mini.h"
|
||||
#include "Network/Socket.h"
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
@@ -29,7 +27,7 @@ public:
|
||||
~MultiCastAddressMaker() {}
|
||||
static MultiCastAddressMaker& Instance();
|
||||
static bool isMultiCastAddress(uint32_t addr);
|
||||
static string toString(uint32_t addr);
|
||||
static std::string toString(uint32_t addr);
|
||||
|
||||
std::shared_ptr<uint32_t> obtain(uint32_t max_try = 10);
|
||||
|
||||
@@ -39,30 +37,30 @@ private:
|
||||
|
||||
private:
|
||||
uint32_t _addr = 0;
|
||||
recursive_mutex _mtx;
|
||||
unordered_set<uint32_t> _used_addr;
|
||||
std::recursive_mutex _mtx;
|
||||
std::unordered_set<uint32_t> _used_addr;
|
||||
};
|
||||
|
||||
class RtpMultiCaster {
|
||||
public:
|
||||
typedef std::shared_ptr<RtpMultiCaster> Ptr;
|
||||
typedef function<void()> onDetach;
|
||||
typedef std::function<void()> onDetach;
|
||||
~RtpMultiCaster();
|
||||
|
||||
static Ptr get(SocketHelper &helper, const string &local_ip, const string &vhost, const string &app, const string &stream);
|
||||
static Ptr get(toolkit::SocketHelper &helper, const std::string &local_ip, const std::string &vhost, const std::string &app, const std::string &stream);
|
||||
void setDetachCB(void *listener,const onDetach &cb);
|
||||
|
||||
string getMultiCasterIP();
|
||||
std::string getMultiCasterIP();
|
||||
uint16_t getMultiCasterPort(TrackType trackType);
|
||||
|
||||
private:
|
||||
RtpMultiCaster(SocketHelper &helper, const string &local_ip, const string &vhost, const string &app, const string &stream);
|
||||
RtpMultiCaster(toolkit::SocketHelper &helper, const std::string &local_ip, const std::string &vhost, const std::string &app, const std::string &stream);
|
||||
|
||||
private:
|
||||
recursive_mutex _mtx;
|
||||
Socket::Ptr _udp_sock[2];
|
||||
std::recursive_mutex _mtx;
|
||||
toolkit::Socket::Ptr _udp_sock[2];
|
||||
std::shared_ptr<uint32_t> _multicast_ip;
|
||||
unordered_map<void * , onDetach > _detach_map;
|
||||
std::unordered_map<void * , onDetach > _detach_map;
|
||||
RtspMediaSource::RingType::RingReader::Ptr _rtp_reader;
|
||||
};
|
||||
|
||||
|
||||
@@ -17,8 +17,6 @@
|
||||
#include "RtpCodec.h"
|
||||
#include "RtspMediaSource.h"
|
||||
#include "Common/Stamp.h"
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
@@ -28,7 +26,7 @@ public:
|
||||
PacketSortor() = default;
|
||||
~PacketSortor() = default;
|
||||
|
||||
void setOnSort(function<void(SEQ seq, T &packet)> cb) {
|
||||
void setOnSort(std::function<void(SEQ seq, T &packet)> cb) {
|
||||
_cb = std::move(cb);
|
||||
}
|
||||
|
||||
@@ -116,7 +114,7 @@ private:
|
||||
_pkt_sort_cache_map.erase(it);
|
||||
}
|
||||
|
||||
void popIterator(typename map<SEQ, T>::iterator it) {
|
||||
void popIterator(typename std::map<SEQ, T>::iterator it) {
|
||||
auto seq = it->first;
|
||||
auto data = std::move(it->second);
|
||||
_pkt_sort_cache_map.erase(it);
|
||||
@@ -156,14 +154,14 @@ private:
|
||||
//排序缓存长度
|
||||
size_t _max_sort_size = kMin;
|
||||
//pkt排序缓存,根据seq排序
|
||||
map<SEQ, T> _pkt_sort_cache_map;
|
||||
std::map<SEQ, T> _pkt_sort_cache_map;
|
||||
//回调
|
||||
function<void(SEQ seq, T &packet)> _cb;
|
||||
std::function<void(SEQ seq, T &packet)> _cb;
|
||||
};
|
||||
|
||||
class RtpTrack : private PacketSortor<RtpPacket::Ptr>{
|
||||
public:
|
||||
class BadRtpException : public invalid_argument {
|
||||
class BadRtpException : public std::invalid_argument {
|
||||
public:
|
||||
template<typename Type>
|
||||
BadRtpException(Type &&type) : invalid_argument(std::forward<Type>(type)) {}
|
||||
@@ -185,14 +183,14 @@ protected:
|
||||
private:
|
||||
bool _disable_ntp = false;
|
||||
uint32_t _ssrc = 0;
|
||||
Ticker _ssrc_alive;
|
||||
toolkit::Ticker _ssrc_alive;
|
||||
NtpStamp _ntp_stamp;
|
||||
};
|
||||
|
||||
class RtpTrackImp : public RtpTrack{
|
||||
public:
|
||||
using OnSorted = function<void(RtpPacket::Ptr)>;
|
||||
using BeforeSorted = function<void(const RtpPacket::Ptr &)>;
|
||||
using OnSorted = std::function<void(RtpPacket::Ptr)>;
|
||||
using BeforeSorted = std::function<void(const RtpPacket::Ptr &)>;
|
||||
|
||||
RtpTrackImp() = default;
|
||||
~RtpTrackImp() override = default;
|
||||
|
||||
@@ -12,6 +12,9 @@
|
||||
#include "Rtsp.h"
|
||||
#include "Common/Parser.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
int RtpPayload::getClockRate(int pt) {
|
||||
|
||||
@@ -20,10 +20,6 @@
|
||||
#include "Common/macros.h"
|
||||
#include "Extension/Frame.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
using namespace mediakit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
namespace Rtsp {
|
||||
@@ -129,7 +125,7 @@ public:
|
||||
//返回有效负载总长度,不包括csrc、ext、padding
|
||||
size_t getPayloadSize(size_t rtp_size) const;
|
||||
//打印调试信息
|
||||
string dumpString(size_t rtp_size) const;
|
||||
std::string dumpString(size_t rtp_size) const;
|
||||
|
||||
private:
|
||||
//返回有效负载偏移量
|
||||
@@ -143,7 +139,7 @@ private:
|
||||
#endif // defined(_WIN32)
|
||||
|
||||
//此rtp为rtp over tcp形式,需要忽略前4个字节
|
||||
class RtpPacket : public BufferRaw{
|
||||
class RtpPacket : public toolkit::BufferRaw{
|
||||
public:
|
||||
using Ptr = std::shared_ptr<RtpPacket>;
|
||||
enum {
|
||||
@@ -157,7 +153,7 @@ public:
|
||||
const RtpHeader* getHeader() const;
|
||||
|
||||
//打印调试信息
|
||||
string dumpString() const;
|
||||
std::string dumpString() const;
|
||||
|
||||
//主机字节序的seq
|
||||
uint16_t getSeq() const;
|
||||
@@ -181,12 +177,12 @@ public:
|
||||
static Ptr create();
|
||||
|
||||
private:
|
||||
friend class ResourcePool_l<RtpPacket>;
|
||||
friend class toolkit::ResourcePool_l<RtpPacket>;
|
||||
RtpPacket() = default;
|
||||
|
||||
private:
|
||||
//对象个数统计
|
||||
ObjectStatistic<RtpPacket> _statistic;
|
||||
toolkit::ObjectStatistic<RtpPacket> _statistic;
|
||||
};
|
||||
|
||||
class RtpPayload {
|
||||
@@ -205,29 +201,29 @@ private:
|
||||
class SdpTrack {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<SdpTrack>;
|
||||
string _t;
|
||||
string _b;
|
||||
std::string _t;
|
||||
std::string _b;
|
||||
uint16_t _port;
|
||||
|
||||
float _duration = 0;
|
||||
float _start = 0;
|
||||
float _end = 0;
|
||||
|
||||
map<char, string> _other;
|
||||
multimap<string, string> _attr;
|
||||
std::map<char, std::string> _other;
|
||||
std::multimap<std::string, std::string> _attr;
|
||||
|
||||
string toString(uint16_t port = 0) const;
|
||||
string getName() const;
|
||||
string getControlUrl(const string &base_url) const;
|
||||
std::string toString(uint16_t port = 0) const;
|
||||
std::string getName() const;
|
||||
std::string getControlUrl(const std::string &base_url) const;
|
||||
|
||||
public:
|
||||
int _pt;
|
||||
int _channel;
|
||||
int _samplerate;
|
||||
TrackType _type;
|
||||
string _codec;
|
||||
string _fmtp;
|
||||
string _control;
|
||||
std::string _codec;
|
||||
std::string _fmtp;
|
||||
std::string _control;
|
||||
|
||||
public:
|
||||
bool _inited = false;
|
||||
@@ -243,17 +239,17 @@ public:
|
||||
using Ptr = std::shared_ptr<SdpParser>;
|
||||
|
||||
SdpParser() {}
|
||||
SdpParser(const string &sdp) { load(sdp); }
|
||||
SdpParser(const std::string &sdp) { load(sdp); }
|
||||
~SdpParser() {}
|
||||
|
||||
void load(const string &sdp);
|
||||
void load(const std::string &sdp);
|
||||
bool available() const;
|
||||
SdpTrack::Ptr getTrack(TrackType type) const;
|
||||
vector<SdpTrack::Ptr> getAvailableTrack() const;
|
||||
string toString() const;
|
||||
std::vector<SdpTrack::Ptr> getAvailableTrack() const;
|
||||
std::string toString() const;
|
||||
|
||||
private:
|
||||
vector<SdpTrack::Ptr> _track_vec;
|
||||
std::vector<SdpTrack::Ptr> _track_vec;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -263,18 +259,18 @@ class RtspUrl{
|
||||
public:
|
||||
bool _is_ssl;
|
||||
uint16_t _port;
|
||||
string _url;
|
||||
string _user;
|
||||
string _passwd;
|
||||
string _host;
|
||||
std::string _url;
|
||||
std::string _user;
|
||||
std::string _passwd;
|
||||
std::string _host;
|
||||
|
||||
public:
|
||||
RtspUrl() = default;
|
||||
~RtspUrl() = default;
|
||||
bool parse(const string &url);
|
||||
bool parse(const std::string &url);
|
||||
|
||||
private:
|
||||
bool setup(bool,const string &, const string &, const string &);
|
||||
bool setup(bool,const std::string &, const std::string &, const std::string &);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -300,7 +296,7 @@ public:
|
||||
* 获取sdp字符串
|
||||
* @return
|
||||
*/
|
||||
virtual string getSdp() const = 0;
|
||||
virtual std::string getSdp() const = 0;
|
||||
|
||||
/**
|
||||
* 获取pt
|
||||
@@ -336,7 +332,7 @@ public:
|
||||
* @param version sdp版本
|
||||
*/
|
||||
TitleSdp(float dur_sec = 0,
|
||||
const map<string, string> &header = map<string, string>(),
|
||||
const std::map<std::string, std::string> &header = std::map<std::string, std::string>(),
|
||||
int version = 0) : Sdp(0, 0) {
|
||||
_printer << "v=" << version << "\r\n";
|
||||
|
||||
@@ -362,7 +358,7 @@ public:
|
||||
_printer << "a=control:*\r\n";
|
||||
}
|
||||
|
||||
string getSdp() const override {
|
||||
std::string getSdp() const override {
|
||||
return _printer;
|
||||
}
|
||||
|
||||
@@ -376,15 +372,15 @@ public:
|
||||
|
||||
private:
|
||||
float _dur_sec = 0;
|
||||
_StrPrinter _printer;
|
||||
toolkit::_StrPrinter _printer;
|
||||
};
|
||||
|
||||
//创建rtp over tcp4个字节的头
|
||||
Buffer::Ptr makeRtpOverTcpPrefix(uint16_t size, uint8_t interleaved);
|
||||
toolkit::Buffer::Ptr makeRtpOverTcpPrefix(uint16_t size, uint8_t interleaved);
|
||||
//创建rtp-rtcp端口对
|
||||
void makeSockPair(std::pair<Socket::Ptr, Socket::Ptr> &pair, const string &local_ip);
|
||||
void makeSockPair(std::pair<toolkit::Socket::Ptr, toolkit::Socket::Ptr> &pair, const std::string &local_ip);
|
||||
//十六进制方式打印ssrc
|
||||
string printSSRC(uint32_t ui32Ssrc);
|
||||
std::string printSSRC(uint32_t ui32Ssrc);
|
||||
|
||||
} //namespace mediakit
|
||||
#endif //RTSP_RTSP_H_
|
||||
|
||||
@@ -16,9 +16,6 @@
|
||||
#include "Util/TimeTicker.h"
|
||||
#include "RtpCodec.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
class RtspDemuxer : public Demuxer {
|
||||
@@ -30,7 +27,7 @@ public:
|
||||
/**
|
||||
* 加载sdp
|
||||
*/
|
||||
void loadSdp(const string &sdp);
|
||||
void loadSdp(const std::string &sdp);
|
||||
|
||||
/**
|
||||
* 开始解复用
|
||||
|
||||
@@ -25,9 +25,9 @@
|
||||
#include "Util/ResourcePool.h"
|
||||
#include "Util/NoticeCenter.h"
|
||||
#include "Thread/ThreadPool.h"
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
#define RTP_GOP_SIZE 512
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
/**
|
||||
@@ -36,12 +36,12 @@ namespace mediakit {
|
||||
* 只要生成了这两要素,那么要实现rtsp推流、rtsp服务器就很简单了
|
||||
* rtsp推拉流协议中,先传递sdp,然后再协商传输方式(tcp/udp/组播),最后一直传递rtp
|
||||
*/
|
||||
class RtspMediaSource : public MediaSource, public RingDelegate<RtpPacket::Ptr>, private PacketCache<RtpPacket> {
|
||||
class RtspMediaSource : public MediaSource, public toolkit::RingDelegate<RtpPacket::Ptr>, private PacketCache<RtpPacket> {
|
||||
public:
|
||||
typedef ResourcePool<RtpPacket> PoolType;
|
||||
typedef std::shared_ptr<RtspMediaSource> Ptr;
|
||||
typedef std::shared_ptr<List<RtpPacket::Ptr> > RingDataType;
|
||||
typedef RingBuffer<RingDataType> RingType;
|
||||
using PoolType = toolkit::ResourcePool<RtpPacket>;
|
||||
using Ptr = std::shared_ptr<RtspMediaSource>;
|
||||
using RingDataType = std::shared_ptr<toolkit::List<RtpPacket::Ptr> >;
|
||||
using RingType = toolkit::RingBuffer<RingDataType>;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
@@ -50,9 +50,9 @@ public:
|
||||
* @param stream_id 流id
|
||||
* @param ring_size 可以设置固定的环形缓冲大小,0则自适应
|
||||
*/
|
||||
RtspMediaSource(const string &vhost,
|
||||
const string &app,
|
||||
const string &stream_id,
|
||||
RtspMediaSource(const std::string &vhost,
|
||||
const std::string &app,
|
||||
const std::string &stream_id,
|
||||
int ring_size = RTP_GOP_SIZE) :
|
||||
MediaSource(RTSP_SCHEMA, vhost, app, stream_id), _ring_size(ring_size) {}
|
||||
|
||||
@@ -75,7 +75,7 @@ public:
|
||||
/**
|
||||
* 获取该源的sdp
|
||||
*/
|
||||
const string &getSdp() const {
|
||||
const std::string &getSdp() const {
|
||||
return _sdp;
|
||||
}
|
||||
|
||||
@@ -140,7 +140,7 @@ public:
|
||||
/**
|
||||
* 设置sdp
|
||||
*/
|
||||
virtual void setSdp(const string &sdp) {
|
||||
virtual void setSdp(const std::string &sdp) {
|
||||
SdpParser sdp_parser(sdp);
|
||||
_tracks[TrackVideo] = sdp_parser.getTrack(TrackVideo);
|
||||
_tracks[TrackAudio] = sdp_parser.getTrack(TrackAudio);
|
||||
@@ -167,7 +167,7 @@ public:
|
||||
track->_ssrc = rtp->getSSRC();
|
||||
}
|
||||
if (!_ring) {
|
||||
weak_ptr<RtspMediaSource> weakSelf = dynamic_pointer_cast<RtspMediaSource>(shared_from_this());
|
||||
std::weak_ptr<RtspMediaSource> weakSelf = std::dynamic_pointer_cast<RtspMediaSource>(shared_from_this());
|
||||
auto lam = [weakSelf](int size) {
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if (!strongSelf) {
|
||||
@@ -198,7 +198,7 @@ private:
|
||||
* @param rtp_list rtp包列表
|
||||
* @param key_pos 是否包含关键帧
|
||||
*/
|
||||
void onFlush(std::shared_ptr<List<RtpPacket::Ptr> > rtp_list, bool key_pos) override {
|
||||
void onFlush(std::shared_ptr<toolkit::List<RtpPacket::Ptr> > rtp_list, bool key_pos) override {
|
||||
//如果不存在视频,那么就没有存在GOP缓存的意义,所以is_key一直为true确保一直清空GOP缓存
|
||||
_ring->write(std::move(rtp_list), _have_video ? key_pos : true);
|
||||
}
|
||||
@@ -206,7 +206,7 @@ private:
|
||||
private:
|
||||
bool _have_video = false;
|
||||
int _ring_size;
|
||||
string _sdp;
|
||||
std::string _sdp;
|
||||
RingType::Ptr _ring;
|
||||
SdpTrack::Ptr _tracks[TrackMax];
|
||||
};
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
#include "RtspMediaSource.h"
|
||||
#include "RtspDemuxer.h"
|
||||
#include "Common/MultiMediaSourceMuxer.h"
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
class RtspMediaSourceImp : public RtspMediaSource, private TrackListener, public MultiMediaSourceMuxer::Listener {
|
||||
@@ -29,7 +28,7 @@ public:
|
||||
* @param id 流id
|
||||
* @param ringSize 环形缓存大小
|
||||
*/
|
||||
RtspMediaSourceImp(const string &vhost, const string &app, const string &id, int ringSize = RTP_GOP_SIZE) : RtspMediaSource(vhost, app, id,ringSize) {
|
||||
RtspMediaSourceImp(const std::string &vhost, const std::string &app, const std::string &id, int ringSize = RTP_GOP_SIZE) : RtspMediaSource(vhost, app, id,ringSize) {
|
||||
_demuxer = std::make_shared<RtspDemuxer>();
|
||||
_demuxer->setTrackListener(this);
|
||||
}
|
||||
@@ -39,7 +38,7 @@ public:
|
||||
/**
|
||||
* 设置sdp
|
||||
*/
|
||||
void setSdp(const string &strSdp) override {
|
||||
void setSdp(const std::string &strSdp) override {
|
||||
if (!getSdp().empty()) {
|
||||
return;
|
||||
}
|
||||
@@ -84,7 +83,7 @@ public:
|
||||
//导致rtc无法播放,所以在rtsp推流rtc播放时,建议关闭直接代理模式
|
||||
_muxer = std::make_shared<MultiMediaSourceMuxer>(getVhost(), getApp(), getId(), _demuxer->getDuration(), !directProxy, true, enableHls, enableMP4);
|
||||
_muxer->setMediaListener(getListener());
|
||||
_muxer->setTrackListener(static_pointer_cast<RtspMediaSourceImp>(shared_from_this()));
|
||||
_muxer->setTrackListener(std::static_pointer_cast<RtspMediaSourceImp>(shared_from_this()));
|
||||
//让_muxer对象拦截一部分事件(比如说录像相关事件)
|
||||
MediaSource::setListener(_muxer);
|
||||
|
||||
|
||||
@@ -21,9 +21,9 @@ class RtspMediaSourceMuxer : public RtspMuxer, public MediaSourceEventIntercepto
|
||||
public:
|
||||
typedef std::shared_ptr<RtspMediaSourceMuxer> Ptr;
|
||||
|
||||
RtspMediaSourceMuxer(const string &vhost,
|
||||
const string &strApp,
|
||||
const string &strId,
|
||||
RtspMediaSourceMuxer(const std::string &vhost,
|
||||
const std::string &strApp,
|
||||
const std::string &strId,
|
||||
const TitleSdp::Ptr &title = nullptr) : RtspMuxer(title){
|
||||
_media_src = std::make_shared<RtspMediaSource>(vhost,strApp,strId);
|
||||
getRtpRing()->setDelegate(_media_src);
|
||||
|
||||
@@ -11,6 +11,9 @@
|
||||
#include "RtspMuxer.h"
|
||||
#include "Extension/Factory.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
void RtspMuxer::onRtp(RtpPacket::Ptr in, bool is_key) {
|
||||
|
||||
@@ -18,15 +18,17 @@
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
class RingDelegateHelper : public RingDelegate<RtpPacket::Ptr> {
|
||||
class RingDelegateHelper : public toolkit::RingDelegate<RtpPacket::Ptr> {
|
||||
public:
|
||||
typedef function<void(RtpPacket::Ptr in, bool is_key)> onRtp;
|
||||
using onRtp = std::function<void(RtpPacket::Ptr in, bool is_key)> ;
|
||||
|
||||
~RingDelegateHelper() override{}
|
||||
RingDelegateHelper(onRtp on_rtp){
|
||||
~RingDelegateHelper() override {}
|
||||
|
||||
RingDelegateHelper(onRtp on_rtp) {
|
||||
_on_rtp = std::move(on_rtp);
|
||||
}
|
||||
void onWrite(RtpPacket::Ptr in, bool is_key) override{
|
||||
|
||||
void onWrite(RtpPacket::Ptr in, bool is_key) override {
|
||||
_on_rtp(std::move(in), is_key);
|
||||
}
|
||||
|
||||
@@ -52,7 +54,7 @@ public:
|
||||
* 获取完整的SDP字符串
|
||||
* @return SDP字符串
|
||||
*/
|
||||
string getSdp() ;
|
||||
std::string getSdp() ;
|
||||
|
||||
/**
|
||||
* 获取rtp环形缓存
|
||||
@@ -85,7 +87,7 @@ private:
|
||||
uint32_t _rtp_stamp[TrackMax]{0};
|
||||
uint64_t _ntp_stamp[TrackMax]{0};
|
||||
uint64_t _ntp_stamp_start;
|
||||
string _sdp;
|
||||
std::string _sdp;
|
||||
Stamp _stamp[TrackMax];
|
||||
RtpCodec::Ptr _encoder[TrackMax];
|
||||
RtpRing::RingType::Ptr _rtpRing;
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
#include "Util/base64.h"
|
||||
#include "Rtcp/Rtcp.h"
|
||||
using namespace toolkit;
|
||||
using namespace mediakit::Client;
|
||||
using namespace std;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
@@ -74,19 +74,19 @@ void RtspPlayer::play(const string &strUrl){
|
||||
teardown();
|
||||
|
||||
if (url._user.size()) {
|
||||
(*this)[kRtspUser] = url._user;
|
||||
(*this)[Client::kRtspUser] = url._user;
|
||||
}
|
||||
if (url._passwd.size()) {
|
||||
(*this)[kRtspPwd] = url._passwd;
|
||||
(*this)[kRtspPwdIsMD5] = false;
|
||||
(*this)[Client::kRtspPwd] = url._passwd;
|
||||
(*this)[Client::kRtspPwdIsMD5] = false;
|
||||
}
|
||||
|
||||
_play_url = url._url;
|
||||
_rtp_type = (Rtsp::eRtpType)(int)(*this)[kRtpType];
|
||||
_rtp_type = (Rtsp::eRtpType)(int)(*this)[Client::kRtpType];
|
||||
DebugL << url._url << " " << (url._user.size() ? url._user : "null") << " " << (url._passwd.size() ? url._passwd : "null") << " " << _rtp_type;
|
||||
|
||||
weak_ptr<RtspPlayer> weakSelf = dynamic_pointer_cast<RtspPlayer>(shared_from_this());
|
||||
float playTimeOutSec = (*this)[kTimeoutMS].as<int>() / 1000.0f;
|
||||
float playTimeOutSec = (*this)[Client::kTimeoutMS].as<int>() / 1000.0f;
|
||||
_play_check_timer.reset(new Timer(playTimeOutSec, [weakSelf]() {
|
||||
auto strongSelf=weakSelf.lock();
|
||||
if(!strongSelf) {
|
||||
@@ -96,8 +96,8 @@ void RtspPlayer::play(const string &strUrl){
|
||||
return false;
|
||||
}, getPoller()));
|
||||
|
||||
if(!(*this)[kNetAdapter].empty()){
|
||||
setNetAdapter((*this)[kNetAdapter]);
|
||||
if(!(*this)[Client::kNetAdapter].empty()){
|
||||
setNetAdapter((*this)[Client::kNetAdapter]);
|
||||
}
|
||||
startConnect(url._host, url._port, playTimeOutSec);
|
||||
}
|
||||
@@ -571,7 +571,7 @@ void RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrC
|
||||
header.emplace("Session", _session_id);
|
||||
}
|
||||
|
||||
if(!_realm.empty() && !(*this)[kRtspUser].empty()){
|
||||
if(!_realm.empty() && !(*this)[Client::kRtspUser].empty()){
|
||||
if(!_md5_nonce.empty()){
|
||||
//MD5认证
|
||||
/*
|
||||
@@ -582,22 +582,22 @@ void RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrC
|
||||
(2)当password为ANSI字符串,则
|
||||
response= md5( md5(username:realm:password):nonce:md5(public_method:url) );
|
||||
*/
|
||||
string encrypted_pwd = (*this)[kRtspPwd];
|
||||
if(!(*this)[kRtspPwdIsMD5].as<bool>()){
|
||||
encrypted_pwd = MD5((*this)[kRtspUser] + ":" + _realm + ":" + encrypted_pwd).hexdigest();
|
||||
string encrypted_pwd = (*this)[Client::kRtspPwd];
|
||||
if(!(*this)[Client::kRtspPwdIsMD5].as<bool>()){
|
||||
encrypted_pwd = MD5((*this)[Client::kRtspUser] + ":" + _realm + ":" + encrypted_pwd).hexdigest();
|
||||
}
|
||||
auto response = MD5(encrypted_pwd + ":" + _md5_nonce + ":" + MD5(cmd + ":" + url).hexdigest()).hexdigest();
|
||||
_StrPrinter printer;
|
||||
printer << "Digest ";
|
||||
printer << "username=\"" << (*this)[kRtspUser] << "\", ";
|
||||
printer << "username=\"" << (*this)[Client::kRtspUser] << "\", ";
|
||||
printer << "realm=\"" << _realm << "\", ";
|
||||
printer << "nonce=\"" << _md5_nonce << "\", ";
|
||||
printer << "uri=\"" << url << "\", ";
|
||||
printer << "response=\"" << response << "\"";
|
||||
header.emplace("Authorization",printer);
|
||||
}else if(!(*this)[kRtspPwdIsMD5].as<bool>()){
|
||||
}else if(!(*this)[Client::kRtspPwdIsMD5].as<bool>()){
|
||||
//base64认证
|
||||
string authStr = StrPrinter << (*this)[kRtspUser] << ":" << (*this)[kRtspPwd];
|
||||
string authStr = StrPrinter << (*this)[Client::kRtspUser] << ":" << (*this)[Client::kRtspPwd];
|
||||
char authStrBase64[1024] = {0};
|
||||
av_base64_encode(authStrBase64, sizeof(authStrBase64), (uint8_t *) authStr.data(), (int) authStr.size());
|
||||
header.emplace("Authorization",StrPrinter << "Basic " << authStrBase64 );
|
||||
@@ -685,7 +685,7 @@ void RtspPlayer::onPlayResult_l(const SockException &ex , bool handshake_done) {
|
||||
if (!ex) {
|
||||
//播放成功,恢复rtp接收超时定时器
|
||||
_rtp_recv_ticker.resetTime();
|
||||
auto timeoutMS = (*this)[kMediaTimeoutMS].as<uint64_t>();
|
||||
auto timeoutMS = (*this)[Client::kMediaTimeoutMS].as<uint64_t>();
|
||||
weak_ptr<RtspPlayer> weakSelf = dynamic_pointer_cast<RtspPlayer>(shared_from_this());
|
||||
auto lam = [weakSelf, timeoutMS]() {
|
||||
auto strongSelf = weakSelf.lock();
|
||||
|
||||
@@ -27,20 +27,17 @@
|
||||
#include "Common/Stamp.h"
|
||||
#include "Rtcp/RtcpContext.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
//实现了rtsp播放器协议部分的功能,及数据接收功能
|
||||
class RtspPlayer: public PlayerBase, public TcpClient, public RtspSplitter, public RtpReceiver {
|
||||
class RtspPlayer : public PlayerBase, public toolkit::TcpClient, public RtspSplitter, public RtpReceiver {
|
||||
public:
|
||||
typedef std::shared_ptr<RtspPlayer> Ptr;
|
||||
using Ptr = std::shared_ptr<RtspPlayer>;
|
||||
|
||||
RtspPlayer(const EventPoller::Ptr &poller);
|
||||
RtspPlayer(const toolkit::EventPoller::Ptr &poller);
|
||||
~RtspPlayer() override;
|
||||
|
||||
void play(const string &strUrl) override;
|
||||
void play(const std::string &strUrl) override;
|
||||
void pause(bool pause) override;
|
||||
void speed(float speed) override;
|
||||
void teardown() override;
|
||||
@@ -48,7 +45,7 @@ public:
|
||||
|
||||
protected:
|
||||
//派生类回调函数
|
||||
virtual bool onCheckSDP(const string &sdp) = 0;
|
||||
virtual bool onCheckSDP(const std::string &sdp) = 0;
|
||||
virtual void onRecvRTP(RtpPacket::Ptr rtp, const SdpTrack::Ptr &track) = 0;
|
||||
uint32_t getProgressMilliSecond() const;
|
||||
void seekToMilliSecond(uint32_t ms);
|
||||
@@ -90,21 +87,21 @@ protected:
|
||||
virtual void onRtcpPacket(int track_idx, SdpTrack::Ptr &track, uint8_t *data, size_t len);
|
||||
|
||||
/////////////TcpClient override/////////////
|
||||
void onConnect(const SockException &err) override;
|
||||
void onRecv(const Buffer::Ptr &buf) override;
|
||||
void onErr(const SockException &ex) override;
|
||||
void onConnect(const toolkit::SockException &err) override;
|
||||
void onRecv(const toolkit::Buffer::Ptr &buf) override;
|
||||
void onErr(const toolkit::SockException &ex) override;
|
||||
|
||||
private:
|
||||
void onPlayResult_l(const SockException &ex , bool handshake_done);
|
||||
void onPlayResult_l(const toolkit::SockException &ex , bool handshake_done);
|
||||
|
||||
int getTrackIndexByInterleaved(int interleaved) const;
|
||||
int getTrackIndexByTrackType(TrackType track_type) const;
|
||||
|
||||
void handleResSETUP(const Parser &parser, unsigned int track_idx);
|
||||
void handleResDESCRIBE(const Parser &parser);
|
||||
bool handleAuthenticationFailure(const string &wwwAuthenticateParamsStr);
|
||||
bool handleAuthenticationFailure(const std::string &wwwAuthenticateParamsStr);
|
||||
void handleResPAUSE(const Parser &parser, int type);
|
||||
bool handleResponse(const string &cmd, const Parser &parser);
|
||||
bool handleResponse(const std::string &cmd, const Parser &parser);
|
||||
|
||||
void sendOptions();
|
||||
void sendSetup(unsigned int track_idx);
|
||||
@@ -112,8 +109,8 @@ private:
|
||||
void sendDescribe();
|
||||
void sendTeardown();
|
||||
void sendKeepAlive();
|
||||
void sendRtspRequest(const string &cmd, const string &url ,const StrCaseMap &header = StrCaseMap());
|
||||
void sendRtspRequest(const string &cmd, const string &url ,const std::initializer_list<string> &header);
|
||||
void sendRtspRequest(const std::string &cmd, const std::string &url ,const StrCaseMap &header = StrCaseMap());
|
||||
void sendRtspRequest(const std::string &cmd, const std::string &url ,const std::initializer_list<std::string> &header);
|
||||
void createUdpSockIfNecessary(int track_idx);
|
||||
|
||||
private:
|
||||
@@ -122,37 +119,37 @@ private:
|
||||
//轮流发送rtcp与GET_PARAMETER保活
|
||||
bool _send_rtcp[2] = {true, true};
|
||||
|
||||
string _play_url;
|
||||
vector<SdpTrack::Ptr> _sdp_track;
|
||||
function<void(const Parser&)> _on_response;
|
||||
std::string _play_url;
|
||||
std::vector<SdpTrack::Ptr> _sdp_track;
|
||||
std::function<void(const Parser&)> _on_response;
|
||||
//RTP端口,trackid idx 为数组下标
|
||||
Socket::Ptr _rtp_sock[2];
|
||||
toolkit::Socket::Ptr _rtp_sock[2];
|
||||
//RTCP端口,trackid idx 为数组下标
|
||||
Socket::Ptr _rtcp_sock[2];
|
||||
toolkit::Socket::Ptr _rtcp_sock[2];
|
||||
|
||||
//rtsp鉴权相关
|
||||
string _md5_nonce;
|
||||
string _realm;
|
||||
std::string _md5_nonce;
|
||||
std::string _realm;
|
||||
//rtsp info
|
||||
string _session_id;
|
||||
std::string _session_id;
|
||||
uint32_t _cseq_send = 1;
|
||||
string _content_base;
|
||||
std::string _content_base;
|
||||
Rtsp::eRtpType _rtp_type = Rtsp::RTP_TCP;
|
||||
|
||||
//当前rtp时间戳
|
||||
uint32_t _stamp[2] = {0, 0};
|
||||
|
||||
//超时功能实现
|
||||
Ticker _rtp_recv_ticker;
|
||||
std::shared_ptr<Timer> _play_check_timer;
|
||||
std::shared_ptr<Timer> _rtp_check_timer;
|
||||
toolkit::Ticker _rtp_recv_ticker;
|
||||
std::shared_ptr<toolkit::Timer> _play_check_timer;
|
||||
std::shared_ptr<toolkit::Timer> _rtp_check_timer;
|
||||
//服务器支持的命令
|
||||
set<string> _supported_cmd;
|
||||
std::set<std::string> _supported_cmd;
|
||||
////////// rtcp ////////////////
|
||||
//rtcp发送时间,trackid idx 为数组下标
|
||||
Ticker _rtcp_send_ticker[2];
|
||||
toolkit::Ticker _rtcp_send_ticker[2];
|
||||
//统计rtp并发送rtcp
|
||||
vector<RtcpContext::Ptr> _rtcp_context;
|
||||
std::vector<RtcpContext::Ptr> _rtcp_context;
|
||||
};
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
@@ -20,9 +20,6 @@
|
||||
#include "Poller/Timer.h"
|
||||
#include "Util/TimeTicker.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
class RtspPlayerImp : public PlayerImp<RtspPlayer, PlayerBase> ,private TrackListener {
|
||||
@@ -30,10 +27,10 @@ public:
|
||||
using Ptr = std::shared_ptr<RtspPlayerImp>;
|
||||
using Super = PlayerImp<RtspPlayer, PlayerBase>;
|
||||
|
||||
RtspPlayerImp(const EventPoller::Ptr &poller) : Super(poller) {}
|
||||
RtspPlayerImp(const toolkit::EventPoller::Ptr &poller) : Super(poller) {}
|
||||
|
||||
~RtspPlayerImp() override {
|
||||
DebugL << endl;
|
||||
DebugL << std::endl;
|
||||
}
|
||||
|
||||
float getProgress() const override {
|
||||
@@ -64,14 +61,14 @@ public:
|
||||
return _demuxer ? _demuxer->getDuration() : 0;
|
||||
}
|
||||
|
||||
vector<Track::Ptr> getTracks(bool ready = true) const override {
|
||||
std::vector<Track::Ptr> getTracks(bool ready = true) const override {
|
||||
return _demuxer ? _demuxer->getTracks(ready) : Super::getTracks(ready);
|
||||
}
|
||||
|
||||
private:
|
||||
//派生类回调函数
|
||||
bool onCheckSDP(const string &sdp) override {
|
||||
_rtsp_media_src = dynamic_pointer_cast<RtspMediaSource>(_media_src);
|
||||
bool onCheckSDP(const std::string &sdp) override {
|
||||
_rtsp_media_src = std::dynamic_pointer_cast<RtspMediaSource>(_media_src);
|
||||
if (_rtsp_media_src) {
|
||||
_rtsp_media_src->setSdp(sdp);
|
||||
}
|
||||
@@ -90,7 +87,7 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
void onPlayResult(const SockException &ex) override {
|
||||
void onPlayResult(const toolkit::SockException &ex) override {
|
||||
if (!(*this)[Client::kWaitTrackReady].as<bool>() || ex) {
|
||||
Super::onPlayResult(ex);
|
||||
return;
|
||||
@@ -101,7 +98,7 @@ private:
|
||||
|
||||
void addTrackCompleted() override {
|
||||
if ((*this)[Client::kWaitTrackReady].as<bool>()) {
|
||||
Super::onPlayResult(SockException(Err_success, "play success"));
|
||||
Super::onPlayResult(toolkit::SockException(toolkit::Err_success, "play success"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,8 @@
|
||||
#include "RtspPusher.h"
|
||||
#include "RtspSession.h"
|
||||
|
||||
using namespace mediakit::Client;
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
@@ -63,20 +64,20 @@ void RtspPusher::publish(const string &url_str) {
|
||||
teardown();
|
||||
|
||||
if (url._user.size()) {
|
||||
(*this)[kRtspUser] = url._user;
|
||||
(*this)[Client::kRtspUser] = url._user;
|
||||
}
|
||||
if (url._passwd.size()) {
|
||||
(*this)[kRtspPwd] = url._passwd;
|
||||
(*this)[kRtspPwdIsMD5] = false;
|
||||
(*this)[Client::kRtspPwd] = url._passwd;
|
||||
(*this)[Client::kRtspPwdIsMD5] = false;
|
||||
}
|
||||
|
||||
_url = url_str;
|
||||
_rtp_type = (Rtsp::eRtpType) (int) (*this)[kRtpType];
|
||||
_rtp_type = (Rtsp::eRtpType) (int) (*this)[Client::kRtpType];
|
||||
DebugL << url._url << " " << (url._user.size() ? url._user : "null") << " "
|
||||
<< (url._passwd.size() ? url._passwd : "null") << " " << _rtp_type;
|
||||
|
||||
weak_ptr<RtspPusher> weak_self = dynamic_pointer_cast<RtspPusher>(shared_from_this());
|
||||
float publish_timeout_sec = (*this)[kTimeoutMS].as<int>() / 1000.0f;
|
||||
float publish_timeout_sec = (*this)[Client::kTimeoutMS].as<int>() / 1000.0f;
|
||||
_publish_timer.reset(new Timer(publish_timeout_sec, [weak_self]() {
|
||||
auto strong_self = weak_self.lock();
|
||||
if (!strong_self) {
|
||||
@@ -86,8 +87,8 @@ void RtspPusher::publish(const string &url_str) {
|
||||
return false;
|
||||
}, getPoller()));
|
||||
|
||||
if (!(*this)[kNetAdapter].empty()) {
|
||||
setNetAdapter((*this)[kNetAdapter]);
|
||||
if (!(*this)[Client::kNetAdapter].empty()) {
|
||||
setNetAdapter((*this)[Client::kNetAdapter]);
|
||||
}
|
||||
|
||||
startConnect(url._host, url._port, publish_timeout_sec);
|
||||
@@ -467,7 +468,7 @@ void RtspPusher::sendRecord() {
|
||||
});
|
||||
if (_rtp_type != Rtsp::RTP_TCP) {
|
||||
/////////////////////////心跳/////////////////////////////////
|
||||
_beat_timer.reset(new Timer((*this)[kBeatIntervalMS].as<int>() / 1000.0f, [weak_self]() {
|
||||
_beat_timer.reset(new Timer((*this)[Client::kBeatIntervalMS].as<int>() / 1000.0f, [weak_self]() {
|
||||
auto strong_self = weak_self.lock();
|
||||
if (!strong_self) {
|
||||
return false;
|
||||
@@ -514,7 +515,7 @@ void RtspPusher::sendRtspRequest(const string &cmd, const string &url,const StrC
|
||||
header.emplace("Session", _session_id);
|
||||
}
|
||||
|
||||
if (!_realm.empty() && !(*this)[kRtspUser].empty()) {
|
||||
if (!_realm.empty() && !(*this)[Client::kRtspUser].empty()) {
|
||||
if (!_nonce.empty()) {
|
||||
//MD5认证
|
||||
/*
|
||||
@@ -525,22 +526,22 @@ void RtspPusher::sendRtspRequest(const string &cmd, const string &url,const StrC
|
||||
(2)当password为ANSI字符串,则
|
||||
response= md5( md5(username:realm:password):nonce:md5(public_method:url) );
|
||||
*/
|
||||
string encrypted_pwd = (*this)[kRtspPwd];
|
||||
if (!(*this)[kRtspPwdIsMD5].as<bool>()) {
|
||||
encrypted_pwd = MD5((*this)[kRtspUser] + ":" + _realm + ":" + encrypted_pwd).hexdigest();
|
||||
string encrypted_pwd = (*this)[Client::kRtspPwd];
|
||||
if (!(*this)[Client::kRtspPwdIsMD5].as<bool>()) {
|
||||
encrypted_pwd = MD5((*this)[Client::kRtspUser] + ":" + _realm + ":" + encrypted_pwd).hexdigest();
|
||||
}
|
||||
auto response = MD5(encrypted_pwd + ":" + _nonce + ":" + MD5(cmd + ":" + url).hexdigest()).hexdigest();
|
||||
_StrPrinter printer;
|
||||
printer << "Digest ";
|
||||
printer << "username=\"" << (*this)[kRtspUser] << "\", ";
|
||||
printer << "username=\"" << (*this)[Client::kRtspUser] << "\", ";
|
||||
printer << "realm=\"" << _realm << "\", ";
|
||||
printer << "nonce=\"" << _nonce << "\", ";
|
||||
printer << "uri=\"" << url << "\", ";
|
||||
printer << "response=\"" << response << "\"";
|
||||
header.emplace("Authorization", printer);
|
||||
} else if (!(*this)[kRtspPwdIsMD5].as<bool>()) {
|
||||
} else if (!(*this)[Client::kRtspPwdIsMD5].as<bool>()) {
|
||||
//base64认证
|
||||
string authStr = StrPrinter << (*this)[kRtspUser] << ":" << (*this)[kRtspPwd];
|
||||
string authStr = StrPrinter << (*this)[Client::kRtspUser] << ":" << (*this)[Client::kRtspPwd];
|
||||
char authStrBase64[1024] = {0};
|
||||
av_base64_encode(authStrBase64, sizeof(authStrBase64), (uint8_t *) authStr.data(), (int)authStr.size());
|
||||
header.emplace("Authorization", StrPrinter << "Basic " << authStrBase64);
|
||||
|
||||
@@ -23,24 +23,21 @@
|
||||
#include "Pusher/PusherBase.h"
|
||||
#include "Rtcp/RtcpContext.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
class RtspPusher : public TcpClient, public RtspSplitter, public PusherBase {
|
||||
class RtspPusher : public toolkit::TcpClient, public RtspSplitter, public PusherBase {
|
||||
public:
|
||||
typedef std::shared_ptr<RtspPusher> Ptr;
|
||||
RtspPusher(const EventPoller::Ptr &poller,const RtspMediaSource::Ptr &src);
|
||||
RtspPusher(const toolkit::EventPoller::Ptr &poller,const RtspMediaSource::Ptr &src);
|
||||
~RtspPusher() override;
|
||||
void publish(const string &url) override;
|
||||
void publish(const std::string &url) override;
|
||||
void teardown() override;
|
||||
|
||||
protected:
|
||||
//for Tcpclient override
|
||||
void onRecv(const Buffer::Ptr &buf) override;
|
||||
void onConnect(const SockException &err) override;
|
||||
void onErr(const SockException &ex) override;
|
||||
void onRecv(const toolkit::Buffer::Ptr &buf) override;
|
||||
void onConnect(const toolkit::SockException &err) override;
|
||||
void onErr(const toolkit::SockException &ex) override;
|
||||
|
||||
//RtspSplitter override
|
||||
void onWholeRtspPacket(Parser &parser) override ;
|
||||
@@ -49,7 +46,7 @@ protected:
|
||||
virtual void onRtcpPacket(int track_idx, SdpTrack::Ptr &track, uint8_t *data, size_t len);
|
||||
|
||||
private:
|
||||
void onPublishResult_l(const SockException &ex, bool handshake_done);
|
||||
void onPublishResult_l(const toolkit::SockException &ex, bool handshake_done);
|
||||
|
||||
void sendAnnounce();
|
||||
void sendSetup(unsigned int track_idx);
|
||||
@@ -59,14 +56,14 @@ private:
|
||||
|
||||
void handleResAnnounce(const Parser &parser);
|
||||
void handleResSetup(const Parser &parser, unsigned int track_idx);
|
||||
bool handleAuthenticationFailure(const string ¶ms_str);
|
||||
bool handleAuthenticationFailure(const std::string ¶ms_str);
|
||||
|
||||
int getTrackIndexByInterleaved(int interleaved) const;
|
||||
int getTrackIndexByTrackType(TrackType type) const;
|
||||
|
||||
void sendRtpPacket(const RtspMediaSource::RingDataType & pkt) ;
|
||||
void sendRtspRequest(const string &cmd, const string &url ,const StrCaseMap &header = StrCaseMap(),const string &sdp = "" );
|
||||
void sendRtspRequest(const string &cmd, const string &url ,const std::initializer_list<string> &header,const string &sdp = "");
|
||||
void sendRtspRequest(const std::string &cmd, const std::string &url ,const StrCaseMap &header = StrCaseMap(),const std::string &sdp = "" );
|
||||
void sendRtspRequest(const std::string &cmd, const std::string &url ,const std::initializer_list<std::string> &header,const std::string &sdp = "");
|
||||
|
||||
void createUdpSockIfNecessary(int track_idx);
|
||||
void setSocketFlags();
|
||||
@@ -77,29 +74,29 @@ private:
|
||||
Rtsp::eRtpType _rtp_type = Rtsp::RTP_TCP;
|
||||
|
||||
//rtsp鉴权相关
|
||||
string _nonce;
|
||||
string _realm;
|
||||
string _url;
|
||||
string _session_id;
|
||||
string _content_base;
|
||||
std::string _nonce;
|
||||
std::string _realm;
|
||||
std::string _url;
|
||||
std::string _session_id;
|
||||
std::string _content_base;
|
||||
SdpParser _sdp_parser;
|
||||
vector<SdpTrack::Ptr> _track_vec;
|
||||
std::vector<SdpTrack::Ptr> _track_vec;
|
||||
//RTP端口,trackid idx 为数组下标
|
||||
Socket::Ptr _rtp_sock[2];
|
||||
toolkit::Socket::Ptr _rtp_sock[2];
|
||||
//RTCP端口,trackid idx 为数组下标
|
||||
Socket::Ptr _rtcp_sock[2];
|
||||
toolkit::Socket::Ptr _rtcp_sock[2];
|
||||
//超时功能实现
|
||||
std::shared_ptr<Timer> _publish_timer;
|
||||
std::shared_ptr<toolkit::Timer> _publish_timer;
|
||||
//心跳定时器
|
||||
std::shared_ptr<Timer> _beat_timer;
|
||||
std::shared_ptr<toolkit::Timer> _beat_timer;
|
||||
std::weak_ptr<RtspMediaSource> _push_src;
|
||||
RtspMediaSource::RingType::RingReader::Ptr _rtsp_reader;
|
||||
function<void(const Parser&)> _on_res_func;
|
||||
std::function<void(const Parser&)> _on_res_func;
|
||||
////////// rtcp ////////////////
|
||||
//rtcp发送时间,trackid idx 为数组下标
|
||||
Ticker _rtcp_send_ticker[2];
|
||||
toolkit::Ticker _rtcp_send_ticker[2];
|
||||
//统计rtp并发送rtcp
|
||||
vector<RtcpContext::Ptr> _rtcp_context;
|
||||
std::vector<RtcpContext::Ptr> _rtcp_context;
|
||||
};
|
||||
|
||||
using RtspPusherImp = PusherImp<RtspPusher, PusherBase>;
|
||||
|
||||
@@ -28,18 +28,16 @@
|
||||
#include "Common/Stamp.h"
|
||||
#include "Rtcp/RtcpContext.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
class RtspSession;
|
||||
|
||||
class BufferRtp : public Buffer{
|
||||
class BufferRtp : public toolkit::Buffer{
|
||||
public:
|
||||
typedef std::shared_ptr<BufferRtp> Ptr;
|
||||
BufferRtp(Buffer::Ptr pkt, size_t offset = 0) : _offset(offset),_rtp(std::move(pkt)) {}
|
||||
~BufferRtp() override{}
|
||||
using Ptr = std::shared_ptr<BufferRtp>;
|
||||
|
||||
BufferRtp(Buffer::Ptr pkt, size_t offset = 0) : _offset(offset), _rtp(std::move(pkt)) {}
|
||||
~BufferRtp() override {}
|
||||
|
||||
char *data() const override {
|
||||
return (char *)_rtp->data() + _offset;
|
||||
@@ -54,19 +52,19 @@ private:
|
||||
Buffer::Ptr _rtp;
|
||||
};
|
||||
|
||||
class RtspSession: public TcpSession, public RtspSplitter, public RtpReceiver , public MediaSourceEvent{
|
||||
class RtspSession : public toolkit::TcpSession, public RtspSplitter, public RtpReceiver, public MediaSourceEvent {
|
||||
public:
|
||||
typedef std::shared_ptr<RtspSession> Ptr;
|
||||
typedef std::function<void(const string &realm)> onGetRealm;
|
||||
using Ptr = std::shared_ptr<RtspSession>;
|
||||
using onGetRealm = std::function<void(const std::string &realm)>;
|
||||
//encrypted为true是则表明是md5加密的密码,否则是明文密码
|
||||
//在请求明文密码时如果提供md5密码者则会导致认证失败
|
||||
typedef std::function<void(bool encrypted,const string &pwd_or_md5)> onAuth;
|
||||
using onAuth = std::function<void(bool encrypted, const std::string &pwd_or_md5)>;
|
||||
|
||||
RtspSession(const Socket::Ptr &sock);
|
||||
RtspSession(const toolkit::Socket::Ptr &sock);
|
||||
virtual ~RtspSession();
|
||||
////TcpSession override////
|
||||
void onRecv(const Buffer::Ptr &buf) override;
|
||||
void onError(const SockException &err) override;
|
||||
void onRecv(const toolkit::Buffer::Ptr &buf) override;
|
||||
void onError(const toolkit::SockException &err) override;
|
||||
void onManager() override;
|
||||
|
||||
protected:
|
||||
@@ -90,12 +88,12 @@ protected:
|
||||
// 获取媒体源类型
|
||||
MediaOriginType getOriginType(MediaSource &sender) const override;
|
||||
// 获取媒体源url或者文件路径
|
||||
string getOriginUrl(MediaSource &sender) const override;
|
||||
std::string getOriginUrl(MediaSource &sender) const override;
|
||||
// 获取媒体源客户端相关信息
|
||||
std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const override;
|
||||
|
||||
/////TcpSession override////
|
||||
ssize_t send(Buffer::Ptr pkt) override;
|
||||
ssize_t send(toolkit::Buffer::Ptr pkt) override;
|
||||
//收到RTCP包回调
|
||||
virtual void onRtcpPacket(int track_idx, SdpTrack::Ptr &track, const char *data, size_t len);
|
||||
|
||||
@@ -132,23 +130,23 @@ private:
|
||||
void send_NotAcceptable();
|
||||
//获取track下标
|
||||
int getTrackIndexByTrackType(TrackType type);
|
||||
int getTrackIndexByControlUrl(const string &control_url);
|
||||
int getTrackIndexByControlUrl(const std::string &control_url);
|
||||
int getTrackIndexByInterleaved(int interleaved);
|
||||
//一般用于接收udp打洞包,也用于rtsp推流
|
||||
void onRcvPeerUdpData(int interleaved, const Buffer::Ptr &buf, const struct sockaddr &addr);
|
||||
void onRcvPeerUdpData(int interleaved, const toolkit::Buffer::Ptr &buf, const struct sockaddr &addr);
|
||||
//配合onRcvPeerUdpData使用
|
||||
void startListenPeerUdpData(int track_idx);
|
||||
////rtsp专有认证相关////
|
||||
//认证成功
|
||||
void onAuthSuccess();
|
||||
//认证失败
|
||||
void onAuthFailed(const string &realm, const string &why, bool close = true);
|
||||
void onAuthFailed(const std::string &realm, const std::string &why, bool close = true);
|
||||
//开始走rtsp专有认证流程
|
||||
void onAuthUser(const string &realm, const string &authorization);
|
||||
void onAuthUser(const std::string &realm, const std::string &authorization);
|
||||
//校验base64方式的认证加密
|
||||
void onAuthBasic(const string &realm, const string &auth_base64);
|
||||
void onAuthBasic(const std::string &realm, const std::string &auth_base64);
|
||||
//校验md5方式的认证加密
|
||||
void onAuthDigest(const string &realm, const string &auth_md5);
|
||||
void onAuthDigest(const std::string &realm, const std::string &auth_md5);
|
||||
//触发url鉴权事件
|
||||
void emitOnPlay();
|
||||
//发送rtp给客户端
|
||||
@@ -156,8 +154,8 @@ private:
|
||||
//触发rtcp发送
|
||||
void updateRtcpContext(const RtpPacket::Ptr &rtp);
|
||||
//回复客户端
|
||||
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");
|
||||
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");
|
||||
|
||||
//设置socket标志
|
||||
void setSocketFlags();
|
||||
@@ -172,15 +170,15 @@ private:
|
||||
//消耗的总流量
|
||||
uint64_t _bytes_usage = 0;
|
||||
//ContentBase
|
||||
string _content_base;
|
||||
std::string _content_base;
|
||||
//Session号
|
||||
string _sessionid;
|
||||
std::string _sessionid;
|
||||
//记录是否需要rtsp专属鉴权,防止重复触发事件
|
||||
string _rtsp_realm;
|
||||
std::string _rtsp_realm;
|
||||
//登录认证
|
||||
string _auth_nonce;
|
||||
std::string _auth_nonce;
|
||||
//用于判断客户端是否超时
|
||||
Ticker _alive_ticker;
|
||||
toolkit::Ticker _alive_ticker;
|
||||
|
||||
//url解析后保存的相关信息
|
||||
MediaInfo _media_info;
|
||||
@@ -193,35 +191,35 @@ private:
|
||||
//直播源读取器
|
||||
RtspMediaSource::RingType::RingReader::Ptr _play_reader;
|
||||
//sdp里面有效的track,包含音频或视频
|
||||
vector<SdpTrack::Ptr> _sdp_track;
|
||||
std::vector<SdpTrack::Ptr> _sdp_track;
|
||||
|
||||
////////RTP over udp////////
|
||||
//RTP端口,trackid idx 为数组下标
|
||||
Socket::Ptr _rtp_socks[2];
|
||||
toolkit::Socket::Ptr _rtp_socks[2];
|
||||
//RTCP端口,trackid idx 为数组下标
|
||||
Socket::Ptr _rtcp_socks[2];
|
||||
toolkit::Socket::Ptr _rtcp_socks[2];
|
||||
//标记是否收到播放的udp打洞包,收到播放的udp打洞包后才能知道其外网udp端口号
|
||||
unordered_set<int> _udp_connected_flags;
|
||||
std::unordered_set<int> _udp_connected_flags;
|
||||
////////RTP over udp_multicast////////
|
||||
//共享的rtp组播对象
|
||||
RtpMultiCaster::Ptr _multicaster;
|
||||
////////RTSP over HTTP ////////
|
||||
//quicktime 请求rtsp会产生两次tcp连接,
|
||||
//一次发送 get 一次发送post,需要通过x-sessioncookie关联起来
|
||||
string _http_x_sessioncookie;
|
||||
function<void(const Buffer::Ptr &)> _on_recv;
|
||||
std::string _http_x_sessioncookie;
|
||||
std::function<void(const toolkit::Buffer::Ptr &)> _on_recv;
|
||||
////////// rtcp ////////////////
|
||||
//rtcp发送时间,trackid idx 为数组下标
|
||||
Ticker _rtcp_send_tickers[2];
|
||||
toolkit::Ticker _rtcp_send_tickers[2];
|
||||
//统计rtp并发送rtcp
|
||||
vector<RtcpContext::Ptr> _rtcp_context;
|
||||
std::vector<RtcpContext::Ptr> _rtcp_context;
|
||||
bool _send_sr_rtcp[2] = {true, true};
|
||||
};
|
||||
|
||||
/**
|
||||
* 支持ssl加密的rtsp服务器,可用于诸如亚马逊echo show这样的设备访问
|
||||
*/
|
||||
typedef TcpSessionWithSSL<RtspSession> RtspSessionWithSSL;
|
||||
using RtspSessionWithSSL = toolkit::TcpSessionWithSSL<RtspSession>;
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
|
||||
@@ -13,6 +13,9 @@
|
||||
#include "Util/logger.h"
|
||||
#include "Util/util.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
const char *RtspSplitter::onSearchPacketTail(const char *data, size_t len) {
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "Util/onceToken.h"
|
||||
|
||||
using namespace toolkit;
|
||||
using namespace std;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
|
||||
@@ -20,30 +20,27 @@
|
||||
#include "Util/logger.h"
|
||||
#include "Network/Socket.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
class UDPServer : public std::enable_shared_from_this<UDPServer> {
|
||||
public:
|
||||
typedef function< bool(int intervaled, const Buffer::Ptr &buffer, struct sockaddr *peer_addr)> onRecvData;
|
||||
using onRecvData = std::function<bool(int intervaled, const toolkit::Buffer::Ptr &buffer, struct sockaddr *peer_addr)> ;
|
||||
~UDPServer();
|
||||
static UDPServer &Instance();
|
||||
Socket::Ptr getSock(SocketHelper &helper, const char *local_ip, int interleaved, uint16_t local_port = 0);
|
||||
toolkit::Socket::Ptr getSock(toolkit::SocketHelper &helper, const char *local_ip, int interleaved, uint16_t local_port = 0);
|
||||
void listenPeer(const char *peer_ip, void *obj, const onRecvData &cb);
|
||||
void stopListenPeer(const char *peer_ip, void *obj);
|
||||
|
||||
private:
|
||||
UDPServer();
|
||||
void onRecv(int interleaved, const Buffer::Ptr &buf, struct sockaddr *peer_addr);
|
||||
void onErr(const string &strKey,const SockException &err);
|
||||
void onRecv(int interleaved, const toolkit::Buffer::Ptr &buf, struct sockaddr *peer_addr);
|
||||
void onErr(const std::string &strKey, const toolkit::SockException &err);
|
||||
|
||||
private:
|
||||
mutex _mtx_udp_sock;
|
||||
mutex _mtx_on_recv;
|
||||
unordered_map<string, Socket::Ptr> _udp_sock_map;
|
||||
unordered_map<string, unordered_map<void *, onRecvData> > _on_recv_map;
|
||||
std::mutex _mtx_udp_sock;
|
||||
std::mutex _mtx_on_recv;
|
||||
std::unordered_map<std::string, toolkit::Socket::Ptr> _udp_sock_map;
|
||||
std::unordered_map<std::string, std::unordered_map<void *, onRecvData> > _on_recv_map;
|
||||
};
|
||||
|
||||
} /* namespace mediakit */
|
||||
|
||||
Reference in New Issue
Block a user