From fd6e4b6342a1acd30df420422f84cc21def535a3 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Thu, 6 Jan 2022 15:30:09 +0800 Subject: [PATCH] =?UTF-8?q?MPEG:=20=E4=BC=98=E5=8C=96http-ts=E6=80=A7?= =?UTF-8?q?=E8=83=BD:=20#1342?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/MPEG.cpp | 27 +++++++++++++++++++++------ src/Record/MPEG.h | 2 ++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/Record/MPEG.cpp b/src/Record/MPEG.cpp index c338b031..6d8d4894 100644 --- a/src/Record/MPEG.cpp +++ b/src/Record/MPEG.cpp @@ -64,7 +64,9 @@ bool MpegMuxer::inputFrame(const Frame::Ptr &frame) { _key_pos = have_idr; //取视频时间戳为TS的时间戳 _timestamp = (uint32_t) dts; + _max_cache_size = 512 + 1.2 * buffer->size(); mpeg_muxer_input(_context, track_id, have_idr ? 0x0001 : 0, pts * 90LL,dts * 90LL, buffer->data(), buffer->size()); + flushCache(); }); } @@ -80,7 +82,9 @@ bool MpegMuxer::inputFrame(const Frame::Ptr &frame) { //没有视频时,才以音频时间戳为TS的时间戳 _timestamp = (uint32_t) frame->dts(); } + _max_cache_size = 512 + 1.2 * frame->size(); mpeg_muxer_input(_context, track_id, frame->keyFrame() ? 0x0001 : 0, frame->pts() * 90LL, frame->dts() * 90LL, frame->data(), frame->size()); + flushCache(); return true; } } @@ -98,10 +102,18 @@ void MpegMuxer::createContext() { static mpeg_muxer_func_t func = { /*alloc*/ [](void *param, size_t bytes) { - MpegMuxer *thiz = (MpegMuxer *) param; - thiz->_current_buffer = thiz->_buffer_pool.obtain2(); - thiz->_current_buffer->setCapacity(bytes + 1); - return (void *) thiz->_current_buffer->data(); + MpegMuxer *thiz = (MpegMuxer *)param; + if (!thiz->_current_buffer + || thiz->_current_buffer->size() + bytes > thiz->_current_buffer->getCapacity()) { + if (thiz->_current_buffer) { + //WarnL << "need realloc mpeg buffer" << thiz->_current_buffer->size() + bytes << " > " << thiz->_current_buffer->getCapacity(); + thiz->flushCache(); + } + thiz->_current_buffer = thiz->_buffer_pool.obtain2(); + thiz->_current_buffer->setSize(0); + thiz->_current_buffer->setCapacity(MAX(thiz->_max_cache_size, bytes)); + } + return (void *)(thiz->_current_buffer->data() + thiz->_current_buffer->size()); }, /*free*/ [](void *param, void *packet) { @@ -120,8 +132,11 @@ void MpegMuxer::createContext() { } void MpegMuxer::onWrite_l(const void *packet, size_t bytes) { - assert(_current_buffer && _current_buffer->data() == packet); - _current_buffer->setSize(bytes); + assert(_current_buffer && _current_buffer->data() + _current_buffer->size() == packet); + _current_buffer->setSize(_current_buffer->size() + bytes); +} + +void MpegMuxer::flushCache() { onWrite(std::move(_current_buffer), _timestamp, _key_pos); _key_pos = false; } diff --git a/src/Record/MPEG.h b/src/Record/MPEG.h index b4eaf39d..3bb41a91 100644 --- a/src/Record/MPEG.h +++ b/src/Record/MPEG.h @@ -58,11 +58,13 @@ private: void createContext(); void releaseContext(); void onWrite_l(const void *packet, size_t bytes); + void flushCache(); private: bool _is_ps = false; bool _have_video = false; bool _key_pos = false; + uint32_t _max_cache_size = 0; uint32_t _timestamp = 0; struct mpeg_muxer_t *_context = nullptr; unordered_map _codec_to_trackid;