From 5c52c636a35268f03b468b9f35a0a8996d5a2671 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Wed, 28 Jul 2021 11:18:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84nack/srtp=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webrtc/Nack.cpp | 2 +- webrtc/Nack.h | 4 +++- webrtc/WebRtcTransport.cpp | 16 +++------------- webrtc/WebRtcTransport.h | 3 +-- 4 files changed, 8 insertions(+), 17 deletions(-) diff --git a/webrtc/Nack.cpp b/webrtc/Nack.cpp index d2447f7a..a5269996 100644 --- a/webrtc/Nack.cpp +++ b/webrtc/Nack.cpp @@ -192,7 +192,7 @@ uint64_t NackContext::reSendNack() { it = _nack_send_status.erase(it); continue; } - if (now - it->second.update_stamp < 2 * _rtt) { + if (now - it->second.update_stamp < kNackIntervalRatio * _rtt) { //距离上次nack不足2倍的rtt,不用再发送nack ++it; continue; diff --git a/webrtc/Nack.h b/webrtc/Nack.h index e6d01093..6d987bfd 100644 --- a/webrtc/Nack.h +++ b/webrtc/Nack.h @@ -39,11 +39,13 @@ public: using Ptr = std::shared_ptr; using onNack = function; //最大保留的rtp丢包状态个数 - static constexpr auto kNackMaxSize = 100; + static constexpr auto kNackMaxSize = 1024; //rtp丢包状态最长保留时间 static constexpr auto kNackMaxMS = 3 * 1000; //nack最多请求重传10次 static constexpr auto kNackMaxCount = 10; + //nack重传频率,rtt的倍数 + static constexpr auto kNackIntervalRatio = 2.0f; NackContext() = default; ~NackContext() = default; diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 7d40cbeb..8ba3a252 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -97,12 +97,7 @@ void WebRtcTransport::OnDtlsTransportConnected( std::string &remoteCert) { InfoL; _srtp_session_send = std::make_shared(RTC::SrtpSession::Type::OUTBOUND, srtpCryptoSuite, srtpLocalKey, srtpLocalKeyLen); - - string srtpRemoteKey_str((char *) srtpRemoteKey, srtpRemoteKeyLen); - _srtp_session_recv_alloc = [srtpCryptoSuite, srtpRemoteKey_str]() { - return std::make_shared(RTC::SrtpSession::Type::INBOUND, srtpCryptoSuite, - (uint8_t *) srtpRemoteKey_str.data(), srtpRemoteKey_str.size()); - }; + _srtp_session_recv = std::make_shared(RTC::SrtpSession::Type::INBOUND, srtpCryptoSuite, srtpRemoteKey, srtpRemoteKeyLen); onStartWebRTC(); } @@ -255,19 +250,14 @@ void WebRtcTransport::inputSockData(char *buf, size_t len, RTC::TransportTuple * _dtls_transport->ProcessDtlsData((uint8_t *) buf, len); return; } - RtpHeader *rtp = (RtpHeader *) buf; - auto it = _srtp_session_recv.find(rtp->pt); - if (it == _srtp_session_recv.end()) { - it = _srtp_session_recv.emplace((uint8_t) rtp->pt, _srtp_session_recv_alloc()).first; - } if (is_rtp(buf)) { - if (it->second->DecryptSrtp((uint8_t *) buf, &len)) { + if (_srtp_session_recv->DecryptSrtp((uint8_t *) buf, &len)) { onRtp(buf, len); } return; } if (is_rtcp(buf)) { - if (it->second->DecryptSrtcp((uint8_t *) buf, &len)) { + if (_srtp_session_recv->DecryptSrtcp((uint8_t *) buf, &len)) { onRtcp(buf, len); } return; diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index ba87729b..30ba2f95 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -122,10 +122,9 @@ private: std::shared_ptr _ice_server; std::shared_ptr _dtls_transport; std::shared_ptr _srtp_session_send; + std::shared_ptr _srtp_session_recv; RtcSession::Ptr _offer_sdp; RtcSession::Ptr _answer_sdp; - function() > _srtp_session_recv_alloc; - std::unordered_map > _srtp_session_recv; }; class RtpChannel;