http-ts直播减少一次内存拷贝

This commit is contained in:
ziyue 2021-07-07 16:15:42 +08:00
parent 6220db77e8
commit 4b694ccde8
5 changed files with 20 additions and 18 deletions

View File

@ -73,8 +73,12 @@ public:
} }
private: private:
void onTs(const void *packet, size_t bytes, uint32_t timestamp, bool is_idr_fast_packet) override { void onTs(std::shared_ptr<Buffer> buffer, uint32_t timestamp, bool is_idr_fast_packet) override {
_hls->inputData((char *) packet, bytes, timestamp, is_idr_fast_packet); if (!buffer) {
_hls->inputData(nullptr, 0, timestamp, is_idr_fast_packet);
} else {
_hls->inputData(buffer->data(), buffer->size(), timestamp, is_idr_fast_packet);
}
} }
private: private:

View File

@ -133,7 +133,7 @@ void TsMuxer::inputFrame(const Frame::Ptr &frame) {
void TsMuxer::resetTracks() { void TsMuxer::resetTracks() {
_have_video = false; _have_video = false;
//通知片段中断 //通知片段中断
onTs(nullptr, 0, _timestamp, 0); onTs(nullptr, _timestamp, 0);
uninit(); uninit();
init(); init();
} }
@ -160,12 +160,14 @@ void TsMuxer::init() {
} }
void TsMuxer::onTs_l(const void *packet, size_t bytes) { void TsMuxer::onTs_l(const void *packet, size_t bytes) {
_cache.append((char *) packet, bytes); if (!_cache) {
_cache = std::make_shared<BufferLikeString>();
}
_cache->append((char *) packet, bytes);
} }
void TsMuxer::flushCache() { void TsMuxer::flushCache() {
onTs(_cache.data(), _cache.size(), _timestamp, _is_idr_fast_packet); onTs(std::move(_cache), _timestamp, _is_idr_fast_packet);
_cache.clear();
_is_idr_fast_packet = false; _is_idr_fast_packet = false;
} }

View File

@ -45,12 +45,11 @@ public:
protected: protected:
/** /**
* mpegts数据回调 * mpegts数据回调
* @param packet mpegts数据 * @param buffer mpegts数据包
* @param bytes mpegts数据长度
* @param timestamp * @param timestamp
* @param is_idr_fast_packet TS包ts切片第一帧为关键帧 * @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> buffer, uint32_t timestamp,bool is_idr_fast_packet) = 0;
private: private:
void init(); void init();
@ -72,7 +71,7 @@ private:
}; };
unordered_map<int, track_info> _codec_to_trackid; unordered_map<int, track_info> _codec_to_trackid;
FrameMerger _frame_merger{FrameMerger::h264_prefix}; FrameMerger _frame_merger{FrameMerger::h264_prefix};
BufferLikeString _cache; std::shared_ptr<BufferLikeString> _cache;
}; };
}//namespace mediakit }//namespace mediakit

View File

@ -18,12 +18,12 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
//TS直播数据包 //TS直播数据包
class TSPacket : public BufferRaw{ class TSPacket : public BufferOffset<Buffer::Ptr>{
public: public:
using Ptr = std::shared_ptr<TSPacket>; using Ptr = std::shared_ptr<TSPacket>;
template<typename ...ARGS> template<typename ...ARGS>
TSPacket(ARGS && ...args) : BufferRaw(std::forward<ARGS>(args)...) {}; TSPacket(ARGS && ...args) : BufferOffset<Buffer::Ptr>(std::forward<ARGS>(args)...) {};
~TSPacket() override = default; ~TSPacket() override = default;
public: public:

View File

@ -25,7 +25,6 @@ public:
const string &app, const string &app,
const string &stream_id) { const string &stream_id) {
_media_src = std::make_shared<TSMediaSource>(vhost, app, stream_id); _media_src = std::make_shared<TSMediaSource>(vhost, app, stream_id);
_pool.setSize(256);
} }
~TSMediaSourceMuxer() override = default; ~TSMediaSourceMuxer() override = default;
@ -66,12 +65,11 @@ public:
} }
protected: protected:
void onTs(const void *data, size_t len,uint32_t timestamp,bool is_idr_fast_packet) override{ void onTs(std::shared_ptr<Buffer> buffer, uint32_t timestamp, bool is_idr_fast_packet) override {
if(!data || !len){ if (!buffer) {
return; return;
} }
TSPacket::Ptr packet = _pool.obtain(); auto packet = std::make_shared<TSPacket>(std::move(buffer));
packet->assign((char *) data, len);
packet->time_stamp = timestamp; packet->time_stamp = timestamp;
_media_src->onWrite(std::move(packet), is_idr_fast_packet); _media_src->onWrite(std::move(packet), is_idr_fast_packet);
} }
@ -79,7 +77,6 @@ protected:
private: private:
bool _enabled = true; bool _enabled = true;
bool _clear_cache = false; bool _clear_cache = false;
TSMediaSource::PoolType _pool;
TSMediaSource::Ptr _media_src; TSMediaSource::Ptr _media_src;
}; };