From a0b464958cf1762bdcf4020580ed21594e83f1f4 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sun, 27 Feb 2022 12:24:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96rtp=E9=87=8D=E5=8F=91?= =?UTF-8?q?=E5=88=97=E9=98=9F=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webrtc/Nack.cpp | 38 +++++++++++++++++++++++--------------- webrtc/Nack.h | 1 + 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/webrtc/Nack.cpp b/webrtc/Nack.cpp index 8f6fd74b..1ac2ff9b 100644 --- a/webrtc/Nack.cpp +++ b/webrtc/Nack.cpp @@ -15,12 +15,17 @@ using namespace toolkit; using namespace mediakit; static constexpr uint32_t kMaxNackMS = 5 * 1000; +static constexpr uint32_t kRtpCacheCheckInterval = 100; void NackList::pushBack(RtpPacket::Ptr rtp) { auto seq = rtp->getSeq(); _nack_cache_seq.emplace_back(seq); _nack_cache_pkt.emplace(seq, std::move(rtp)); - while (getCacheMS() > kMaxNackMS) { + if (++_cache_ms_check < kRtpCacheCheckInterval) { + return; + } + _cache_ms_check = 0; + while (getCacheMS() >= kMaxNackMS) { //需要清除部分nack缓存 popFront(); } @@ -57,23 +62,26 @@ RtpPacket::Ptr *NackList::getRtp(uint16_t seq) { } uint32_t NackList::getCacheMS() { - auto back_stamp = getRtpStamp(_nack_cache_seq.back()); - if (back_stamp == -1) { - _nack_cache_seq.pop_back(); - return 0; - } + while (_nack_cache_seq.size() > 2) { + auto back_stamp = getRtpStamp(_nack_cache_seq.back()); + if (back_stamp == -1) { + _nack_cache_seq.pop_back(); + continue; + } - auto front_stamp = getRtpStamp(_nack_cache_seq.front()); - if (front_stamp == -1) { - _nack_cache_seq.pop_front(); - return 0; - } + auto front_stamp = getRtpStamp(_nack_cache_seq.front()); + if (front_stamp == -1) { + _nack_cache_seq.pop_front(); + continue; + } - if (back_stamp >= front_stamp) { - return back_stamp - front_stamp; + if (back_stamp >= front_stamp) { + return back_stamp - front_stamp; + } + //很有可能回环了 + return back_stamp + (UINT32_MAX - front_stamp); } - //很有可能回环了 - return back_stamp + (UINT32_MAX - front_stamp); + return 0; } int64_t NackList::getRtpStamp(uint16_t seq) { diff --git a/webrtc/Nack.h b/webrtc/Nack.h index 7f51bdba..179ed4f5 100644 --- a/webrtc/Nack.h +++ b/webrtc/Nack.h @@ -30,6 +30,7 @@ private: mediakit::RtpPacket::Ptr *getRtp(uint16_t seq); private: + int _cache_ms_check = 0; std::deque _nack_cache_seq; std::unordered_map _nack_cache_pkt; };