MPEG: 优化ts/ps muxer内存malloc和拷贝性能

This commit is contained in:
ziyue 2021-12-31 11:01:56 +08:00
parent 038a604d4f
commit 389dd5f54b
2 changed files with 10 additions and 20 deletions

View File

@ -8,7 +8,7 @@
* may be found in the AUTHORS file in the root of the source tree. * may be found in the AUTHORS file in the root of the source tree.
*/ */
#include <cstdlib> #include <assert.h>
#include "MPEG.h" #include "MPEG.h"
#if defined(ENABLE_HLS) || defined(ENABLE_RTPPROXY) #if defined(ENABLE_HLS) || defined(ENABLE_RTPPROXY)
@ -19,8 +19,8 @@ namespace mediakit{
MpegMuxer::MpegMuxer(bool is_ps) { MpegMuxer::MpegMuxer(bool is_ps) {
_is_ps = is_ps; _is_ps = is_ps;
_buffer = BufferRaw::create();
createContext(); createContext();
_buffer_pool.setSize(64);
} }
MpegMuxer::~MpegMuxer() { MpegMuxer::~MpegMuxer() {
@ -65,7 +65,6 @@ bool MpegMuxer::inputFrame(const Frame::Ptr &frame) {
//取视频时间戳为TS的时间戳 //取视频时间戳为TS的时间戳
_timestamp = (uint32_t) dts; _timestamp = (uint32_t) dts;
mpeg_muxer_input(_context, track_id, have_idr ? 0x0001 : 0, pts * 90LL,dts * 90LL, buffer->data(), buffer->size()); 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(); _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()); mpeg_muxer_input(_context, track_id, frame->keyFrame() ? 0x0001 : 0, frame->pts() * 90LL, frame->dts() * 90LL, frame->data(), frame->size());
flushCache();
return true; return true;
} }
} }
@ -101,8 +99,9 @@ void MpegMuxer::createContext() {
/*alloc*/ /*alloc*/
[](void *param, size_t bytes) { [](void *param, size_t bytes) {
MpegMuxer *thiz = (MpegMuxer *) param; MpegMuxer *thiz = (MpegMuxer *) param;
thiz->_buffer->setCapacity(bytes + 1); thiz->_current_buffer = thiz->_buffer_pool.obtain();;
return (void *) thiz->_buffer->data(); thiz->_current_buffer->setCapacity(bytes + 1);
return (void *) thiz->_current_buffer->data();
}, },
/*free*/ /*free*/
[](void *param, void *packet) { [](void *param, void *packet) {
@ -121,17 +120,9 @@ void MpegMuxer::createContext() {
} }
void MpegMuxer::onWrite_l(const void *packet, size_t bytes) { void MpegMuxer::onWrite_l(const void *packet, size_t bytes) {
if (!_cache) { assert(_current_buffer && _current_buffer->data() == packet);
_cache = std::make_shared<BufferLikeString>(); _current_buffer->setSize(bytes);
} onWrite(std::move(_current_buffer), _timestamp, _key_pos);
_cache->append((char *) packet, bytes);
}
void MpegMuxer::flushCache() {
if (!_cache || _cache->empty()) {
return;
}
onWrite(std::move(_cache), _timestamp, _key_pos);
_key_pos = false; _key_pos = false;
} }

View File

@ -57,7 +57,6 @@ protected:
private: private:
void createContext(); void createContext();
void releaseContext(); void releaseContext();
void flushCache();
void onWrite_l(const void *packet, size_t bytes); void onWrite_l(const void *packet, size_t bytes);
private: private:
@ -66,10 +65,10 @@ private:
bool _key_pos = false; bool _key_pos = false;
uint32_t _timestamp = 0; uint32_t _timestamp = 0;
struct mpeg_muxer_t *_context = nullptr; struct mpeg_muxer_t *_context = nullptr;
BufferRaw::Ptr _buffer;
unordered_map<int, int/*track_id*/> _codec_to_trackid; unordered_map<int, int/*track_id*/> _codec_to_trackid;
FrameMerger _frame_merger{FrameMerger::h264_prefix}; FrameMerger _frame_merger{FrameMerger::h264_prefix};
std::shared_ptr<BufferLikeString> _cache; BufferRaw::Ptr _current_buffer;
ResourcePool<BufferRaw> _buffer_pool;
}; };
}//mediakit }//mediakit