diff --git a/src/Record/HlsRecorder.h b/src/Record/HlsRecorder.h index d4c31d61..4c72b136 100644 --- a/src/Record/HlsRecorder.h +++ b/src/Record/HlsRecorder.h @@ -73,8 +73,12 @@ public: } private: - void onTs(const void *packet, size_t bytes, uint32_t timestamp, bool is_idr_fast_packet) override { - _hls->inputData((char *) packet, bytes, timestamp, is_idr_fast_packet); + void onTs(std::shared_ptr buffer, uint32_t timestamp, bool is_idr_fast_packet) override { + if (!buffer) { + _hls->inputData(nullptr, 0, timestamp, is_idr_fast_packet); + } else { + _hls->inputData(buffer->data(), buffer->size(), timestamp, is_idr_fast_packet); + } } private: diff --git a/src/Record/TsMuxer.cpp b/src/Record/TsMuxer.cpp index 98948132..56e7c169 100644 --- a/src/Record/TsMuxer.cpp +++ b/src/Record/TsMuxer.cpp @@ -133,7 +133,7 @@ void TsMuxer::inputFrame(const Frame::Ptr &frame) { void TsMuxer::resetTracks() { _have_video = false; //通知片段中断 - onTs(nullptr, 0, _timestamp, 0); + onTs(nullptr, _timestamp, 0); uninit(); init(); } @@ -160,12 +160,14 @@ void TsMuxer::init() { } void TsMuxer::onTs_l(const void *packet, size_t bytes) { - _cache.append((char *) packet, bytes); + if (!_cache) { + _cache = std::make_shared(); + } + _cache->append((char *) packet, bytes); } void TsMuxer::flushCache() { - onTs(_cache.data(), _cache.size(), _timestamp, _is_idr_fast_packet); - _cache.clear(); + onTs(std::move(_cache), _timestamp, _is_idr_fast_packet); _is_idr_fast_packet = false; } diff --git a/src/Record/TsMuxer.h b/src/Record/TsMuxer.h index ea540346..46e4a4ae 100644 --- a/src/Record/TsMuxer.h +++ b/src/Record/TsMuxer.h @@ -45,12 +45,11 @@ public: protected: /** * 输出mpegts数据回调 - * @param packet mpegts数据 - * @param bytes mpegts数据长度 + * @param buffer mpegts数据包 * @param timestamp 时间戳,单位毫秒 * @param is_idr_fast_packet 是否为关键帧的第一个TS包,用于确保ts切片第一帧为关键帧 */ - virtual void onTs(const void *packet, size_t bytes,uint32_t timestamp,bool is_idr_fast_packet) = 0; + virtual void onTs(std::shared_ptr buffer, uint32_t timestamp,bool is_idr_fast_packet) = 0; private: void init(); @@ -72,7 +71,7 @@ private: }; unordered_map _codec_to_trackid; FrameMerger _frame_merger{FrameMerger::h264_prefix}; - BufferLikeString _cache; + std::shared_ptr _cache; }; }//namespace mediakit diff --git a/src/TS/TSMediaSource.h b/src/TS/TSMediaSource.h index b0d2f03a..a79cc311 100644 --- a/src/TS/TSMediaSource.h +++ b/src/TS/TSMediaSource.h @@ -18,12 +18,12 @@ using namespace toolkit; namespace mediakit { //TS直播数据包 -class TSPacket : public BufferRaw{ +class TSPacket : public BufferOffset{ public: using Ptr = std::shared_ptr; template - TSPacket(ARGS && ...args) : BufferRaw(std::forward(args)...) {}; + TSPacket(ARGS && ...args) : BufferOffset(std::forward(args)...) {}; ~TSPacket() override = default; public: diff --git a/src/TS/TSMediaSourceMuxer.h b/src/TS/TSMediaSourceMuxer.h index 46a3725b..9e58d2dd 100644 --- a/src/TS/TSMediaSourceMuxer.h +++ b/src/TS/TSMediaSourceMuxer.h @@ -25,7 +25,6 @@ public: const string &app, const string &stream_id) { _media_src = std::make_shared(vhost, app, stream_id); - _pool.setSize(256); } ~TSMediaSourceMuxer() override = default; @@ -66,12 +65,11 @@ public: } protected: - void onTs(const void *data, size_t len,uint32_t timestamp,bool is_idr_fast_packet) override{ - if(!data || !len){ + void onTs(std::shared_ptr buffer, uint32_t timestamp, bool is_idr_fast_packet) override { + if (!buffer) { return; } - TSPacket::Ptr packet = _pool.obtain(); - packet->assign((char *) data, len); + auto packet = std::make_shared(std::move(buffer)); packet->time_stamp = timestamp; _media_src->onWrite(std::move(packet), is_idr_fast_packet); } @@ -79,7 +77,6 @@ protected: private: bool _enabled = true; bool _clear_cache = false; - TSMediaSource::PoolType _pool; TSMediaSource::Ptr _media_src; };