优化rtp性能

This commit is contained in:
xia-chu 2021-02-05 11:28:50 +08:00
parent b0e1d5d6aa
commit c5cfbce241
13 changed files with 44 additions and 31 deletions

View File

@ -34,7 +34,7 @@ bool GB28181Process::inputRtp(bool, const char *data, size_t data_len) {
return handleOneRtp(0, TrackVideo, 90000, (unsigned char *) data, data_len); return handleOneRtp(0, TrackVideo, 90000, (unsigned char *) data, data_len);
} }
void GB28181Process::onRtpSorted(const RtpPacket::Ptr &rtp, int) { void GB28181Process::onRtpSorted(RtpPacket::Ptr rtp, int) {
auto pt = rtp->getHeader()->pt; auto pt = rtp->getHeader()->pt;
if (!_rtp_decoder) { if (!_rtp_decoder) {
switch (pt) { switch (pt) {

View File

@ -36,7 +36,7 @@ public:
bool inputRtp(bool, const char *data, size_t data_len) override; bool inputRtp(bool, const char *data, size_t data_len) override;
protected: protected:
void onRtpSorted(const RtpPacket::Ptr &rtp, int track_index) override ; void onRtpSorted(RtpPacket::Ptr rtp, int track_index) override ;
const char *onSearchPacketTail(const char *data,size_t len) override; const char *onSearchPacketTail(const char *data,size_t len) override;
ssize_t onRecvHeader(const char *data,size_t len) override { return 0; }; ssize_t onRecvHeader(const char *data,size_t len) override { return 0; };

View File

@ -14,7 +14,7 @@ namespace mediakit{
RtpPacket::Ptr RtpInfo::makeRtp(TrackType type, const void* data, size_t len, bool mark, uint32_t stamp) { RtpPacket::Ptr RtpInfo::makeRtp(TrackType type, const void* data, size_t len, bool mark, uint32_t stamp) {
uint16_t payload_len = (uint16_t) (len + RtpPacket::kRtpHeaderSize); uint16_t payload_len = (uint16_t) (len + RtpPacket::kRtpHeaderSize);
auto rtp = ResourcePoolHelper<RtpPacket>::obtainObj(); auto rtp = RtpPacket::create();
rtp->setCapacity(payload_len + RtpPacket::kRtpTcpHeaderSize); rtp->setCapacity(payload_len + RtpPacket::kRtpTcpHeaderSize);
rtp->setSize(payload_len + RtpPacket::kRtpTcpHeaderSize); rtp->setSize(payload_len + RtpPacket::kRtpTcpHeaderSize);
rtp->sample_rate = _sample_rate; rtp->sample_rate = _sample_rate;

View File

@ -58,9 +58,9 @@ protected:
RingType::Ptr _rtpRing; RingType::Ptr _rtpRing;
}; };
class RtpInfo : public ResourcePoolHelper<RtpPacket>{ class RtpInfo{
public: public:
typedef std::shared_ptr<RtpInfo> Ptr; using Ptr = std::shared_ptr<RtpInfo>;
RtpInfo(uint32_t ssrc, size_t mtu_size, uint32_t sample_rate, uint8_t pt, uint8_t interleaved) { RtpInfo(uint32_t ssrc, size_t mtu_size, uint32_t sample_rate, uint8_t pt, uint8_t interleaved) {
if (ssrc == 0) { if (ssrc == 0) {
@ -73,7 +73,7 @@ public:
_interleaved = interleaved; _interleaved = interleaved;
} }
~RtpInfo() override {} virtual ~RtpInfo() {}
//返回rtp负载最大长度 //返回rtp负载最大长度
size_t getMaxSize() const { size_t getMaxSize() const {

View File

@ -19,7 +19,7 @@ RtpReceiver::RtpReceiver() {
int index = 0; int index = 0;
for (auto &sortor : _rtp_sortor) { for (auto &sortor : _rtp_sortor) {
sortor.setOnSort([this, index](uint16_t seq, RtpPacket::Ptr &packet) { sortor.setOnSort([this, index](uint16_t seq, RtpPacket::Ptr &packet) {
onRtpSorted(packet, index); onRtpSorted(std::move(packet), index);
}); });
++index; ++index;
} }
@ -61,7 +61,7 @@ bool RtpReceiver::handleOneRtp(int index, TrackType type, int sample_rate, uint8
return false; return false;
} }
auto rtp = _rtp_pool.obtain(); auto rtp = RtpPacket::create();
//需要添加4个字节的rtp over tcp头 //需要添加4个字节的rtp over tcp头
rtp->setCapacity(RtpPacket::kRtpTcpHeaderSize + len); rtp->setCapacity(RtpPacket::kRtpTcpHeaderSize + len);
rtp->setSize(RtpPacket::kRtpTcpHeaderSize + len); rtp->setSize(RtpPacket::kRtpTcpHeaderSize + len);
@ -90,10 +90,6 @@ void RtpReceiver::clear() {
} }
} }
void RtpReceiver::setPoolSize(size_t size) {
_rtp_pool.setSize(size);
}
size_t RtpReceiver::getJitterSize(int index) const{ size_t RtpReceiver::getJitterSize(int index) const{
return _rtp_sortor[index].getJitterSize(); return _rtp_sortor[index].getJitterSize();
} }

View File

@ -182,7 +182,7 @@ protected:
* @param rtp rtp数据包 * @param rtp rtp数据包
* @param track_index track索引 * @param track_index track索引
*/ */
virtual void onRtpSorted(const RtpPacket::Ptr &rtp, int track_index) {} virtual void onRtpSorted(RtpPacket::Ptr rtp, int track_index) {}
/** /**
* rtp但还未排序 * rtp但还未排序
@ -192,7 +192,6 @@ protected:
virtual void onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_index) {} virtual void onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_index) {}
void clear(); void clear();
void setPoolSize(size_t size);
size_t getJitterSize(int track_index) const; size_t getJitterSize(int track_index) const;
size_t getCycleCount(int track_index) const; size_t getCycleCount(int track_index) const;
uint32_t getSSRC(int track_index) const; uint32_t getSSRC(int track_index) const;
@ -201,8 +200,6 @@ private:
uint32_t _ssrc[2] = {0, 0}; uint32_t _ssrc[2] = {0, 0};
//rtp排序缓存根据seq排序 //rtp排序缓存根据seq排序
PacketSortor<RtpPacket::Ptr> _rtp_sortor[2]; PacketSortor<RtpPacket::Ptr> _rtp_sortor[2];
//rtp循环池
RtspMediaSource::PoolType _rtp_pool;
}; };
}//namespace mediakit }//namespace mediakit

View File

@ -531,4 +531,18 @@ size_t RtpPacket::getPayloadSize(){
return getHeader()->getPayloadSize(size() - kRtpTcpHeaderSize); return getHeader()->getPayloadSize(size() - kRtpTcpHeaderSize);
} }
RtpPacket::Ptr RtpPacket::create(){
#if 0
static ResourcePool<RtpPacket> packet_pool;
static onceToken token([]() {
packet_pool.setSize(1024);
});
auto ret = packet_pool.obtain();
ret->setSize(0);
return ret;
#else
return Ptr(new RtpPacket);
#endif
}
}//namespace mediakit }//namespace mediakit

View File

@ -166,6 +166,12 @@ public:
TrackType type; TrackType type;
//音频为采样率视频一般为90000 //音频为采样率视频一般为90000
uint32_t sample_rate; uint32_t sample_rate;
static Ptr create();
private:
friend class ResourcePool_l<RtpPacket>;
RtpPacket() = default;
}; };
class RtpPayload { class RtpPayload {

View File

@ -494,10 +494,10 @@ void RtspPlayer::onRtcpPacket(int track_idx, SdpTrack::Ptr &track, uint8_t *data
} }
} }
void RtspPlayer::onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx){ void RtspPlayer::onRtpSorted(RtpPacket::Ptr rtppt, int trackidx){
_stamp[trackidx] = rtppt->getStampMS(); _stamp[trackidx] = rtppt->getStampMS();
_rtp_recv_ticker.resetTime(); _rtp_recv_ticker.resetTime();
onRecvRTP(rtppt, _sdp_track[trackidx]); onRecvRTP(std::move(rtppt), _sdp_track[trackidx]);
} }
float RtspPlayer::getPacketLossRate(TrackType type) const{ float RtspPlayer::getPacketLossRate(TrackType type) const{

View File

@ -33,7 +33,7 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
//实现了rtsp播放器协议部分的功能及数据接收功能 //实现了rtsp播放器协议部分的功能及数据接收功能
class RtspPlayer: public PlayerBase,public TcpClient, public RtspSplitter, public RtpReceiver { class RtspPlayer: public PlayerBase, public TcpClient, public RtspSplitter, public RtpReceiver {
public: public:
typedef std::shared_ptr<RtspPlayer> Ptr; typedef std::shared_ptr<RtspPlayer> Ptr;
@ -48,7 +48,7 @@ public:
protected: protected:
//派生类回调函数 //派生类回调函数
virtual bool onCheckSDP(const string &sdp) = 0; virtual bool onCheckSDP(const string &sdp) = 0;
virtual void onRecvRTP(const RtpPacket::Ptr &rtp, const SdpTrack::Ptr &track) = 0; virtual void onRecvRTP(RtpPacket::Ptr rtp, const SdpTrack::Ptr &track) = 0;
uint32_t getProgressMilliSecond() const; uint32_t getProgressMilliSecond() const;
void seekToMilliSecond(uint32_t ms); void seekToMilliSecond(uint32_t ms);
@ -70,7 +70,7 @@ protected:
* @param rtp rtp数据包 * @param rtp rtp数据包
* @param track_idx track索引 * @param track_idx track索引
*/ */
void onRtpSorted(const RtpPacket::Ptr &rtp, int track_idx) override; void onRtpSorted(RtpPacket::Ptr rtp, int track_idx) override;
/** /**
* rtp但还未排序 * rtp但还未排序

View File

@ -59,18 +59,18 @@ private:
return true; return true;
} }
void onRecvRTP(const RtpPacket::Ptr &rtp, const SdpTrack::Ptr &track) override { void onRecvRTP(RtpPacket::Ptr rtp, const SdpTrack::Ptr &track) override {
if (_rtsp_media_src) {
// rtsp直接代理是无法判断该rtp是否是I帧所以GOP缓存基本是无效的
// 为了减少内存使用那么我们设置为一直关键帧以便清空GOP缓存
_rtsp_media_src->onWrite(rtp, true);
}
_delegate->inputRtp(rtp); _delegate->inputRtp(rtp);
if (_max_analysis_ms && _delegate->isInited(_max_analysis_ms)) { if (_max_analysis_ms && _delegate->isInited(_max_analysis_ms)) {
PlayerImp<RtspPlayer, RtspDemuxer>::onPlayResult(SockException(Err_success, "play rtsp success")); PlayerImp<RtspPlayer, RtspDemuxer>::onPlayResult(SockException(Err_success, "play rtsp success"));
_max_analysis_ms = 0; _max_analysis_ms = 0;
} }
if (_rtsp_media_src) {
// rtsp直接代理是无法判断该rtp是否是I帧所以GOP缓存基本是无效的
// 为了减少内存使用那么我们设置为一直关键帧以便清空GOP缓存
_rtsp_media_src->onWrite(std::move(rtp), true);
}
} }
//在RtspPlayer中触发onPlayResult事件只是代表收到play回复了 //在RtspPlayer中触发onPlayResult事件只是代表收到play回复了

View File

@ -913,8 +913,8 @@ void RtspSession::send_NotAcceptable() {
sendRtspResponse("406 Not Acceptable",{"Connection","Close"}); sendRtspResponse("406 Not Acceptable",{"Connection","Close"});
} }
void RtspSession::onRtpSorted(const RtpPacket::Ptr &rtp, int track_idx) { void RtspSession::onRtpSorted(RtpPacket::Ptr rtp, int track_idx) {
_push_src->onWrite(rtp, false); _push_src->onWrite(std::move(rtp), false);
} }
void RtspSession::onRcvPeerUdpData(int interleaved, const Buffer::Ptr &buf, const struct sockaddr &addr) { void RtspSession::onRcvPeerUdpData(int interleaved, const Buffer::Ptr &buf, const struct sockaddr &addr) {

View File

@ -79,7 +79,7 @@ protected:
ssize_t getContentLength(Parser &parser) override; ssize_t getContentLength(Parser &parser) override;
////RtpReceiver override//// ////RtpReceiver override////
void onRtpSorted(const RtpPacket::Ptr &rtp, int track_idx) override; void onRtpSorted(RtpPacket::Ptr rtp, int track_idx) override;
void onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_index) override; void onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_index) override;
///////MediaSourceEvent override/////// ///////MediaSourceEvent override///////