2022-06-03 16:47:53 +08:00
|
|
|
|
#ifndef ZLMEDIAKIT_SRT_TRANSPORT_IMP_H
|
|
|
|
|
#define ZLMEDIAKIT_SRT_TRANSPORT_IMP_H
|
2022-06-07 09:52:20 +08:00
|
|
|
|
|
2022-06-11 21:37:41 +08:00
|
|
|
|
#include "Common/MultiMediaSourceMuxer.h"
|
2022-06-03 16:47:53 +08:00
|
|
|
|
#include "Rtp/Decoder.h"
|
|
|
|
|
#include "SrtTransport.hpp"
|
2022-06-07 09:52:20 +08:00
|
|
|
|
#include "TS/TSMediaSource.h"
|
2022-06-11 21:37:41 +08:00
|
|
|
|
#include <mutex>
|
2022-06-03 22:47:52 +08:00
|
|
|
|
|
2022-06-03 16:47:53 +08:00
|
|
|
|
namespace SRT {
|
2022-06-07 09:52:20 +08:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
using namespace mediakit;
|
2022-06-03 16:47:53 +08:00
|
|
|
|
class SrtTransportImp
|
|
|
|
|
: public SrtTransport
|
|
|
|
|
, public toolkit::SockInfo
|
|
|
|
|
, public MediaSinkInterface
|
|
|
|
|
, public mediakit::MediaSourceEvent {
|
|
|
|
|
public:
|
|
|
|
|
SrtTransportImp(const EventPoller::Ptr &poller);
|
|
|
|
|
~SrtTransportImp();
|
2022-06-07 09:52:20 +08:00
|
|
|
|
|
|
|
|
|
void inputSockData(uint8_t *buf, int len, struct sockaddr_storage *addr) override {
|
|
|
|
|
SrtTransport::inputSockData(buf, len, addr);
|
2022-06-03 21:17:01 +08:00
|
|
|
|
_total_bytes += len;
|
|
|
|
|
}
|
2022-06-07 09:52:20 +08:00
|
|
|
|
void onSendTSData(const Buffer::Ptr &buffer, bool flush) override { SrtTransport::onSendTSData(buffer, flush); }
|
2022-06-03 16:47:53 +08:00
|
|
|
|
/// SockInfo override
|
|
|
|
|
std::string get_local_ip() override;
|
|
|
|
|
uint16_t get_local_port() override;
|
|
|
|
|
std::string get_peer_ip() override;
|
|
|
|
|
uint16_t get_peer_port() override;
|
|
|
|
|
std::string getIdentifier() const override;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
///////SrtTransport override///////
|
2022-06-07 09:52:20 +08:00
|
|
|
|
int getLatencyMul() override;
|
2022-06-11 22:51:11 +08:00
|
|
|
|
int getPktBufSize() override;
|
2022-06-04 20:51:49 +08:00
|
|
|
|
void onSRTData(DataPacket::Ptr pkt) override;
|
2022-06-03 16:47:53 +08:00
|
|
|
|
void onShutdown(const SockException &ex) override;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
void onHandShakeFinished(std::string &streamid, struct sockaddr_storage *addr) override;
|
2022-06-03 16:47:53 +08:00
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
void sendPacket(Buffer::Ptr pkt, bool flush = true) override {
|
2022-06-04 11:23:18 +08:00
|
|
|
|
_total_bytes += pkt->size();
|
2022-06-07 09:52:20 +08:00
|
|
|
|
SrtTransport::sendPacket(pkt, flush);
|
2022-06-04 20:51:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
bool isPusher() override { return _is_pusher; }
|
|
|
|
|
|
2022-06-03 16:47:53 +08:00
|
|
|
|
///////MediaSourceEvent override///////
|
|
|
|
|
// 关闭
|
|
|
|
|
bool close(mediakit::MediaSource &sender, bool force) override;
|
|
|
|
|
// 获取媒体源类型
|
|
|
|
|
mediakit::MediaOriginType getOriginType(mediakit::MediaSource &sender) const override;
|
|
|
|
|
// 获取媒体源url或者文件路径
|
|
|
|
|
std::string getOriginUrl(mediakit::MediaSource &sender) const override;
|
|
|
|
|
// 获取媒体源客户端相关信息
|
|
|
|
|
std::shared_ptr<SockInfo> getOriginSock(mediakit::MediaSource &sender) const override;
|
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
///////MediaSinkInterface override///////
|
2022-06-03 16:47:53 +08:00
|
|
|
|
void resetTracks() override {};
|
2022-06-07 09:52:20 +08:00
|
|
|
|
void addTrackCompleted() override;
|
|
|
|
|
bool addTrack(const Track::Ptr &track) override;
|
|
|
|
|
bool inputFrame(const Frame::Ptr &frame) override;
|
2022-06-03 16:47:53 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2022-06-11 21:37:41 +08:00
|
|
|
|
bool parseStreamid(std::string &streamid);
|
2022-06-03 16:47:53 +08:00
|
|
|
|
void emitOnPublish();
|
|
|
|
|
void emitOnPlay();
|
|
|
|
|
|
|
|
|
|
void doPlay();
|
|
|
|
|
void doCachedFunc();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool _is_pusher = true;
|
|
|
|
|
MediaInfo _media_info;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
uint64_t _total_bytes = 0;
|
2022-06-03 21:17:01 +08:00
|
|
|
|
Ticker _alive_ticker;
|
2022-06-03 16:47:53 +08:00
|
|
|
|
std::unique_ptr<sockaddr_storage> _addr;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
// for player
|
2022-06-03 22:47:52 +08:00
|
|
|
|
TSMediaSource::RingType::RingReader::Ptr _ts_reader;
|
2022-06-07 09:52:20 +08:00
|
|
|
|
// for pusher
|
2022-06-03 16:47:53 +08:00
|
|
|
|
MultiMediaSourceMuxer::Ptr _muxer;
|
|
|
|
|
DecoderImp::Ptr _decoder;
|
|
|
|
|
std::recursive_mutex _func_mtx;
|
2022-06-06 20:40:04 +08:00
|
|
|
|
std::deque<std::function<void()>> _cached_func;
|
2022-06-26 00:54:31 +08:00
|
|
|
|
|
|
|
|
|
std::unordered_map<int, Stamp> _type_to_stamp;
|
2022-06-03 16:47:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace SRT
|
|
|
|
|
|
|
|
|
|
#endif // ZLMEDIAKIT_SRT_TRANSPORT_IMP_H
|