ZLMediaKit/srt/SrtTransportImp.hpp

96 lines
3.0 KiB
C++
Raw Normal View History

#ifndef ZLMEDIAKIT_SRT_TRANSPORT_IMP_H
#define ZLMEDIAKIT_SRT_TRANSPORT_IMP_H
2022-06-07 09:52:20 +08:00
#include "Common/MultiMediaSourceMuxer.h"
#include "Rtp/Decoder.h"
#include "SrtTransport.hpp"
2022-06-07 09:52:20 +08:00
#include "TS/TSMediaSource.h"
#include <mutex>
namespace SRT {
2022-06-07 09:52:20 +08:00
using namespace std;
using namespace toolkit;
using namespace mediakit;
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); }
/// 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-09-20 00:39:42 +08:00
float getTimeOutSec() override;
void onSRTData(DataPacket::Ptr pkt) override;
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-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-07 09:52:20 +08:00
bool isPusher() override { return _is_pusher; }
///////MediaSourceEvent override///////
// 关闭
bool close(mediakit::MediaSource &sender) 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///////
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;
private:
bool parseStreamid(std::string &streamid);
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;
std::unique_ptr<sockaddr_storage> _addr;
2022-06-07 09:52:20 +08:00
// for player
TSMediaSource::RingType::RingReader::Ptr _ts_reader;
2022-06-07 09:52:20 +08:00
// for pusher
MultiMediaSourceMuxer::Ptr _muxer;
DecoderImp::Ptr _decoder;
std::recursive_mutex _func_mtx;
std::deque<std::function<void()>> _cached_func;
std::unordered_map<int, Stamp> _type_to_stamp;
};
} // namespace SRT
#endif // ZLMEDIAKIT_SRT_TRANSPORT_IMP_H