diff --git a/src/Rtmp/FlvMuxer.cpp b/src/Rtmp/FlvMuxer.cpp index 84f0eb1a..5728d30f 100644 --- a/src/Rtmp/FlvMuxer.cpp +++ b/src/Rtmp/FlvMuxer.cpp @@ -16,6 +16,10 @@ namespace mediakit { +FlvMuxer::FlvMuxer(){ + _packet_pool.setSize(64); +} + void FlvMuxer::start(const EventPoller::Ptr &poller, const RtmpMediaSource::Ptr &media) { if (!media) { throw std::runtime_error("RtmpMediaSource 无效"); @@ -62,15 +66,19 @@ void FlvMuxer::start(const EventPoller::Ptr &poller, const RtmpMediaSource::Ptr }); } +BufferRaw::Ptr FlvMuxer::obtainBuffer() { + return _packet_pool.obtain(); +} + BufferRaw::Ptr FlvMuxer::obtainBuffer(const void *data, size_t len) { - auto buffer = BufferRaw::create(); + auto buffer = obtainBuffer(); buffer->assign((const char *) data, len); return buffer; } void FlvMuxer::onWriteFlvHeader(const RtmpMediaSource::Ptr &src) { //发送flv文件头 - auto buffer = BufferRaw::create(); + auto buffer = obtainBuffer(); buffer->setCapacity(sizeof(FLVHeader)); buffer->setSize(sizeof(FLVHeader)); diff --git a/src/Rtmp/FlvMuxer.h b/src/Rtmp/FlvMuxer.h index 2f4a389c..caf1903c 100644 --- a/src/Rtmp/FlvMuxer.h +++ b/src/Rtmp/FlvMuxer.h @@ -22,7 +22,7 @@ namespace mediakit { class FlvMuxer { public: using Ptr = std::shared_ptr; - FlvMuxer() = default; + FlvMuxer(); virtual ~FlvMuxer() = default; void stop(); @@ -39,8 +39,10 @@ private: void onWriteFlvTag(const RtmpPacket::Ptr &pkt, uint32_t time_stamp, bool flush); void onWriteFlvTag(uint8_t type, const Buffer::Ptr &buffer, uint32_t time_stamp, bool flush); BufferRaw::Ptr obtainBuffer(const void *data, size_t len); + BufferRaw::Ptr obtainBuffer(); private: + ResourcePool _packet_pool; //时间戳修整器 Stamp _stamp[2]; RtmpMediaSource::RingType::RingReader::Ptr _ring_reader; diff --git a/src/Rtmp/RtmpProtocol.cpp b/src/Rtmp/RtmpProtocol.cpp index d0c5f027..4f9a8af7 100644 --- a/src/Rtmp/RtmpProtocol.cpp +++ b/src/Rtmp/RtmpProtocol.cpp @@ -55,6 +55,7 @@ static string openssl_HMACsha256(const void *key, size_t key_len, const void *da namespace mediakit { RtmpProtocol::RtmpProtocol() { + _packet_pool.setSize(64); _next_step_func = [this](const char *data, size_t len) { return handle_C0C1(data, len); }; @@ -776,7 +777,7 @@ void RtmpProtocol::handle_chunk(RtmpPacket::Ptr packet) { } BufferRaw::Ptr RtmpProtocol::obtainBuffer(const void *data, size_t len) { - auto buffer = BufferRaw::create(); + auto buffer = _packet_pool.obtain(); if (data && len) { buffer->assign((const char *) data, len); } diff --git a/src/Rtmp/RtmpProtocol.h b/src/Rtmp/RtmpProtocol.h index 5a004874..fa51b191 100644 --- a/src/Rtmp/RtmpProtocol.h +++ b/src/Rtmp/RtmpProtocol.h @@ -107,6 +107,8 @@ private: function _next_step_func; ////////////Chunk//////////// unordered_map > _map_chunk_data; + //循环池 + ResourcePool _packet_pool; }; } /* namespace mediakit */ diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 159fec74..2f396374 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -313,6 +313,7 @@ void WebRtcTransportImp::onCreate(){ WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller) : WebRtcTransport(poller) { InfoL << getIdentifier(); + _packet_pool.setSize(64); } WebRtcTransportImp::~WebRtcTransportImp() { @@ -329,7 +330,7 @@ void WebRtcTransportImp::onSendSockData(const char *buf, size_t len, struct sock WarnL << "send data failed:" << len; return; } - auto ptr = BufferRaw::create(); + auto ptr = _packet_pool.obtain(); ptr->assign(buf, len); //一次性发送一帧的rtp数据,提高网络io性能 _session->setSendFlushFlag(flush); @@ -385,10 +386,10 @@ void WebRtcTransportImp::onStartWebRTC() { _ssrc_to_track[track->offer_ssrc_rtx] = track; //rtp pt --> MediaTrack - _pt_to_track.emplace(track->plan_rtp->pt, new WrappedRtpTrack(track, _twcc_ctx, *this)); + _pt_to_track.emplace(track->plan_rtp->pt, std::unique_ptr(new WrappedRtpTrack(track, _twcc_ctx, *this))); if (track->plan_rtx) { //rtx pt --> MediaTrack - _pt_to_track.emplace(track->plan_rtx->pt, new WrappedRtxTrack(track)); + _pt_to_track.emplace(track->plan_rtx->pt, std::unique_ptr(new WrappedRtxTrack(track))); } if (m_offer->type != TrackApplication) { //记录rtp ext类型与id的关系,方便接收或发送rtp时修改rtp ext id diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index 45fd2e72..2879944a 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -214,8 +214,6 @@ struct WrappedRtpTrack : public WrappedMediaTrack { void inputRtp(const char *buf, size_t len, uint64_t stamp_ms, RtpHeader *rtp) override; }; - - class WebRtcTransportImp : public WebRtcTransport { public: using Ptr = std::shared_ptr; @@ -281,6 +279,8 @@ private: unordered_map _ssrc_to_track; //根据接收rtp的pt获取相关信息 unordered_map> _pt_to_track; + //循环池 + ResourcePool _packet_pool; }; class WebRtcTransportManager {