diff --git a/src/Record/MPEG.cpp b/src/Record/MPEG.cpp index f9c10157..658d6700 100644 --- a/src/Record/MPEG.cpp +++ b/src/Record/MPEG.cpp @@ -8,7 +8,7 @@ * may be found in the AUTHORS file in the root of the source tree. */ -#include +#include #include "MPEG.h" #if defined(ENABLE_HLS) || defined(ENABLE_RTPPROXY) @@ -19,8 +19,8 @@ namespace mediakit{ MpegMuxer::MpegMuxer(bool is_ps) { _is_ps = is_ps; - _buffer = BufferRaw::create(); createContext(); + _buffer_pool.setSize(64); } MpegMuxer::~MpegMuxer() { @@ -65,7 +65,6 @@ bool MpegMuxer::inputFrame(const Frame::Ptr &frame) { //取视频时间戳为TS的时间戳 _timestamp = (uint32_t) dts; mpeg_muxer_input(_context, track_id, have_idr ? 0x0001 : 0, pts * 90LL,dts * 90LL, buffer->data(), buffer->size()); - flushCache(); }); } @@ -82,7 +81,6 @@ bool MpegMuxer::inputFrame(const Frame::Ptr &frame) { _timestamp = (uint32_t) frame->dts(); } mpeg_muxer_input(_context, track_id, frame->keyFrame() ? 0x0001 : 0, frame->pts() * 90LL, frame->dts() * 90LL, frame->data(), frame->size()); - flushCache(); return true; } } @@ -101,8 +99,9 @@ void MpegMuxer::createContext() { /*alloc*/ [](void *param, size_t bytes) { MpegMuxer *thiz = (MpegMuxer *) param; - thiz->_buffer->setCapacity(bytes + 1); - return (void *) thiz->_buffer->data(); + thiz->_current_buffer = thiz->_buffer_pool.obtain();; + thiz->_current_buffer->setCapacity(bytes + 1); + return (void *) thiz->_current_buffer->data(); }, /*free*/ [](void *param, void *packet) { @@ -121,17 +120,9 @@ void MpegMuxer::createContext() { } void MpegMuxer::onWrite_l(const void *packet, size_t bytes) { - if (!_cache) { - _cache = std::make_shared(); - } - _cache->append((char *) packet, bytes); -} - -void MpegMuxer::flushCache() { - if (!_cache || _cache->empty()) { - return; - } - onWrite(std::move(_cache), _timestamp, _key_pos); + assert(_current_buffer && _current_buffer->data() == packet); + _current_buffer->setSize(bytes); + onWrite(std::move(_current_buffer), _timestamp, _key_pos); _key_pos = false; } diff --git a/src/Record/MPEG.h b/src/Record/MPEG.h index 4b96a800..b4eaf39d 100644 --- a/src/Record/MPEG.h +++ b/src/Record/MPEG.h @@ -57,7 +57,6 @@ protected: private: void createContext(); void releaseContext(); - void flushCache(); void onWrite_l(const void *packet, size_t bytes); private: @@ -66,10 +65,10 @@ private: bool _key_pos = false; uint32_t _timestamp = 0; struct mpeg_muxer_t *_context = nullptr; - BufferRaw::Ptr _buffer; unordered_map _codec_to_trackid; FrameMerger _frame_merger{FrameMerger::h264_prefix}; - std::shared_ptr _cache; + BufferRaw::Ptr _current_buffer; + ResourcePool _buffer_pool; }; }//mediakit