From 4b694ccde8eda6f5162ab6a5cf34bcbfbfbee346 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Wed, 7 Jul 2021 16:15:42 +0800 Subject: [PATCH] =?UTF-8?q?http-ts=E7=9B=B4=E6=92=AD=E5=87=8F=E5=B0=91?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E5=86=85=E5=AD=98=E6=8B=B7=E8=B4=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/HlsRecorder.h | 8 ++++++-- src/Record/TsMuxer.cpp | 10 ++++++---- src/Record/TsMuxer.h | 7 +++---- src/TS/TSMediaSource.h | 4 ++-- src/TS/TSMediaSourceMuxer.h | 9 +++------ 5 files changed, 20 insertions(+), 18 deletions(-) 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; };