Make RTC_LOG_THREAD_BLOCK_COUNT less spammy for known call counts

Also removing a count check from DestroyTransceiverChannel that's
not useful right now. We can bring it back when we have
DestroyChannelInterface better under control as far as Invokes goes.

Bug: none
Change-Id: I8e9c55a980f8f20e8b996fdc461fd90b0fbd4f3d
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/215201
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Tommi <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33730}
diff --git a/pc/sdp_offer_answer.cc b/pc/sdp_offer_answer.cc
index 53770ea..385d052 100644
--- a/pc/sdp_offer_answer.cc
+++ b/pc/sdp_offer_answer.cc
@@ -4725,7 +4725,6 @@
     // worker thread. Can DestroyTransceiverChannel be purely posted to the
     // worker?
     DestroyChannelInterface(channel);
-    RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(3);
   }
 }
 
diff --git a/rtc_base/thread.cc b/rtc_base/thread.cc
index 039f82a..b0b0e52 100644
--- a/rtc_base/thread.cc
+++ b/rtc_base/thread.cc
@@ -362,7 +362,9 @@
       result_callback_(std::move(callback)) {}
 
 Thread::ScopedCountBlockingCalls::~ScopedCountBlockingCalls() {
-  result_callback_(GetBlockingCallCount(), GetCouldBeBlockingCallCount());
+  if (GetTotalBlockedCallCount() >= min_blocking_calls_for_callback_) {
+    result_callback_(GetBlockingCallCount(), GetCouldBeBlockingCallCount());
+  }
 }
 
 uint32_t Thread::ScopedCountBlockingCalls::GetBlockingCallCount() const {
diff --git a/rtc_base/thread.h b/rtc_base/thread.h
index 6d3c39b..e031677f 100644
--- a/rtc_base/thread.h
+++ b/rtc_base/thread.h
@@ -61,8 +61,11 @@
 // number of blocking thread calls.
 // Note: Use of this macro, requires RTC_LOG_THREAD_BLOCK_COUNT() to be called
 // first.
-#define RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(x) \
-  RTC_DCHECK_LE(blocked_call_count_printer.GetTotalBlockedCallCount(), x)
+#define RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(x)                               \
+  do {                                                                       \
+    blocked_call_count_printer.set_minimum_call_count_for_callback(x + 1);   \
+    RTC_DCHECK_LE(blocked_call_count_printer.GetTotalBlockedCallCount(), x); \
+  } while (0)
 #else
 #define RTC_LOG_THREAD_BLOCK_COUNT()
 #define RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(x)
@@ -251,10 +254,19 @@
     uint32_t GetCouldBeBlockingCallCount() const;
     uint32_t GetTotalBlockedCallCount() const;
 
+    void set_minimum_call_count_for_callback(uint32_t minimum) {
+      min_blocking_calls_for_callback_ = minimum;
+    }
+
    private:
     Thread* const thread_;
     const uint32_t base_blocking_call_count_;
     const uint32_t base_could_be_blocking_call_count_;
+    // The minimum number of blocking calls required in order to issue the
+    // result_callback_. This is used by RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN to
+    // tame log spam.
+    // By default we always issue the callback, regardless of callback count.
+    uint32_t min_blocking_calls_for_callback_ = 0;
     std::function<void(uint32_t, uint32_t)> result_callback_;
   };
 
diff --git a/rtc_base/thread_unittest.cc b/rtc_base/thread_unittest.cc
index 86e429e..2a24d6c 100644
--- a/rtc_base/thread_unittest.cc
+++ b/rtc_base/thread_unittest.cc
@@ -297,6 +297,40 @@
 #endif
 }
 
+#if RTC_DCHECK_IS_ON
+TEST(ThreadTest, CountBlockingCallsOneCallback) {
+  rtc::Thread* current = rtc::Thread::Current();
+  ASSERT_TRUE(current);
+  bool was_called_back = false;
+  {
+    rtc::Thread::ScopedCountBlockingCalls blocked_calls(
+        [&](uint32_t actual_block, uint32_t could_block) {
+          was_called_back = true;
+        });
+    current->Invoke<void>(RTC_FROM_HERE, []() {});
+  }
+  EXPECT_TRUE(was_called_back);
+}
+
+TEST(ThreadTest, CountBlockingCallsSkipCallback) {
+  rtc::Thread* current = rtc::Thread::Current();
+  ASSERT_TRUE(current);
+  bool was_called_back = false;
+  {
+    rtc::Thread::ScopedCountBlockingCalls blocked_calls(
+        [&](uint32_t actual_block, uint32_t could_block) {
+          was_called_back = true;
+        });
+    // Changed `blocked_calls` to not issue the callback if there are 1 or
+    // fewer blocking calls (i.e. we set the minimum required number to 2).
+    blocked_calls.set_minimum_call_count_for_callback(2);
+    current->Invoke<void>(RTC_FROM_HERE, []() {});
+  }
+  // We should not have gotten a call back.
+  EXPECT_FALSE(was_called_back);
+}
+#endif
+
 // Test that setting thread names doesn't cause a malfunction.
 // There's no easy way to verify the name was set properly at this time.
 TEST(ThreadTest, Names) {