Throttle log frequency for RTCP decryption errors.

It is generating thousands of lines per second, and
we were already doing this for RTP.

Bug: b/367372987
Change-Id: I52fa751f8699e07ed82567fecadca9e978b01263
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/478000
Reviewed-by: Taylor Brandstetter <deadbeef@webrtc.org>
Commit-Queue: Junichi Uekawa <uekawa@google.com>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Auto-Submit: Junichi Uekawa <uekawa@google.com>
Cr-Commit-Position: refs/heads/main@{#47953}
diff --git a/pc/srtp_session.cc b/pc/srtp_session.cc
index e8618cc..be702c7 100644
--- a/pc/srtp_session.cc
+++ b/pc/srtp_session.cc
@@ -342,7 +342,15 @@
   int out_len = buffer.size();
   int err = srtp_unprotect_rtcp(session_, buffer.MutableData<char>(), &out_len);
   if (err != srtp_err_status_ok) {
-    RTC_LOG(LS_WARNING) << "Failed to unprotect SRTCP packet, err=" << err;
+    // Limit the error logging to avoid excessive logs when there are lots of
+    // bad packets.
+    const int kFailureLogThrottleCount = 100;
+    if (rtcp_decryption_failure_count_ % kFailureLogThrottleCount == 0) {
+      RTC_LOG(LS_WARNING) << "Failed to unprotect SRTCP packet, err=" << err
+                          << ", previous failure count: "
+                          << rtcp_decryption_failure_count_;
+    }
+    ++rtcp_decryption_failure_count_;
     RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SrtcpUnprotectError",
                               static_cast<int>(err), kSrtpErrorCodeBoundary);
     return false;
diff --git a/pc/srtp_session.h b/pc/srtp_session.h
index 0cabe94..7a88b8a 100644
--- a/pc/srtp_session.h
+++ b/pc/srtp_session.h
@@ -124,6 +124,7 @@
   bool inited_ RTC_GUARDED_BY(thread_checker_) = false;
   int last_send_seq_num_ RTC_GUARDED_BY(thread_checker_) = -1;
   int decryption_failure_count_ RTC_GUARDED_BY(thread_checker_) = 0;
+  int rtcp_decryption_failure_count_ RTC_GUARDED_BY(thread_checker_) = 0;
 
   // Supported since libsrtp v2.8.0.
   bool use_cryptex_ RTC_GUARDED_BY(thread_checker_) = false;
diff --git a/pc/srtp_transport.cc b/pc/srtp_transport.cc
index 63a6f69..ffa513b 100644
--- a/pc/srtp_transport.cc
+++ b/pc/srtp_transport.cc
@@ -122,10 +122,18 @@
   }
   CopyOnWriteBuffer payload(packet.payload());
   if (!UnprotectRtcp(payload)) {
-    int type = -1;
-    GetRtcpType(payload.data(), payload.size(), &type);
-    RTC_LOG(LS_ERROR) << "Failed to unprotect RTCP packet: size="
-                      << payload.size() << ", type=" << type;
+    // Limit the error logging to avoid excessive logs when there are lots of
+    // bad packets.
+    const int kFailureLogThrottleCount = 100;
+    if (rtcp_decryption_failure_count_ % kFailureLogThrottleCount == 0) {
+      int type = -1;
+      GetRtcpType(payload.data(), payload.size(), &type);
+      RTC_LOG(LS_ERROR) << "Failed to unprotect RTCP packet: size="
+                        << payload.size() << ", type=" << type
+                        << ", previous failure count: "
+                        << rtcp_decryption_failure_count_;
+    }
+    ++rtcp_decryption_failure_count_;
     return;
   }
   SendRtcpPacketReceived(std::move(payload), packet.arrival_time(),
diff --git a/pc/srtp_transport.h b/pc/srtp_transport.h
index 1605103..b3ee3b1 100644
--- a/pc/srtp_transport.h
+++ b/pc/srtp_transport.h
@@ -133,6 +133,7 @@
   bool writable_ = false;
 
   int decryption_failure_count_ = 0;
+  int rtcp_decryption_failure_count_ = 0;
 
   bool enable_cryptex_ = false;
   bool require_cryptex_ = false;