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:
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> 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:

View File

@ -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<BufferLikeString>();
}
_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;
}

View File

@ -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> buffer, uint32_t timestamp,bool is_idr_fast_packet) = 0;
private:
void init();
@ -72,7 +71,7 @@ private:
};
unordered_map<int, track_info> _codec_to_trackid;
FrameMerger _frame_merger{FrameMerger::h264_prefix};
BufferLikeString _cache;
std::shared_ptr<BufferLikeString> _cache;
};
}//namespace mediakit

View File

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

View File

@ -25,7 +25,6 @@ public:
const string &app,
const string &stream_id) {
_media_src = std::make_shared<TSMediaSource>(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> 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<TSPacket>(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;
};