Move bandwidth overuse detection out of VideoStreamEncoder

Bug: webrtc:11222
Change-Id: I12ccd008c848a0146fb22292f8dac46d1f7be9b4
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/166531
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Commit-Queue: Evan Shrubsole <eshr@google.com>
Cr-Commit-Position: refs/heads/master@{#30329}
diff --git a/video/overuse_frame_detector_resource_adaptation_module.cc b/video/overuse_frame_detector_resource_adaptation_module.cc
index ea082a5..2ca3435 100644
--- a/video/overuse_frame_detector_resource_adaptation_module.cc
+++ b/video/overuse_frame_detector_resource_adaptation_module.cc
@@ -450,6 +450,24 @@
                                encode_duration_us);
 }
 
+void OveruseFrameDetectorResourceAdaptationModule::FrameDroppedDueToSize() {
+  int fps_count = GetConstAdaptCounter().FramerateCount(
+      AdaptationObserverInterface::AdaptReason::kQuality);
+  int res_count = GetConstAdaptCounter().ResolutionCount(
+      AdaptationObserverInterface::AdaptReason::kQuality);
+  AdaptDown(AdaptationObserverInterface::AdaptReason::kQuality);
+  if (degradation_preference() == DegradationPreference::BALANCED &&
+      GetConstAdaptCounter().FramerateCount(
+          AdaptationObserverInterface::AdaptReason::kQuality) > fps_count) {
+    // Adapt framerate in same step as resolution.
+    AdaptDown(AdaptationObserverInterface::AdaptReason::kQuality);
+  }
+  if (GetConstAdaptCounter().ResolutionCount(
+          AdaptationObserverInterface::AdaptReason::kQuality) > res_count) {
+    encoder_stats_observer_->OnInitialQualityResolutionAdaptDown();
+  }
+}
+
 void OveruseFrameDetectorResourceAdaptationModule::SetLastFramePixelCount(
     absl::optional<int> last_frame_pixel_count) {
   last_frame_pixel_count_ = last_frame_pixel_count;
diff --git a/video/overuse_frame_detector_resource_adaptation_module.h b/video/overuse_frame_detector_resource_adaptation_module.h
index d3b88e5..798d386 100644
--- a/video/overuse_frame_detector_resource_adaptation_module.h
+++ b/video/overuse_frame_detector_resource_adaptation_module.h
@@ -89,6 +89,7 @@
                  int64_t time_sent_in_us,
                  int64_t capture_time_us,
                  absl::optional<int> encode_duration_us);
+  void FrameDroppedDueToSize();
 
   // Various other settings and feedback mechanisms.
   // TODO(hbos): Find a common interface that would make sense for a generic
diff --git a/video/video_stream_encoder.cc b/video/video_stream_encoder.cc
index 2a6f14c..96d0d4d 100644
--- a/video/video_stream_encoder.cc
+++ b/video/video_stream_encoder.cc
@@ -1084,24 +1084,7 @@
 
   if (DropDueToSize(video_frame.size())) {
     RTC_LOG(LS_INFO) << "Dropping frame. Too large for target bitrate.";
-    int fps_count =
-        resource_adaptation_module_->GetConstAdaptCounter().FramerateCount(
-            AdaptationObserverInterface::AdaptReason::kQuality);
-    int res_count =
-        resource_adaptation_module_->GetConstAdaptCounter().ResolutionCount(
-            AdaptationObserverInterface::AdaptReason::kQuality);
-    TriggerAdaptDown(AdaptationObserverInterface::AdaptReason::kQuality);
-    if (resource_adaptation_module_->degradation_preference() ==
-            DegradationPreference::BALANCED &&
-        resource_adaptation_module_->GetConstAdaptCounter().FramerateCount(
-            AdaptationObserverInterface::AdaptReason::kQuality) > fps_count) {
-      // Adapt framerate in same step as resolution.
-      TriggerAdaptDown(AdaptationObserverInterface::AdaptReason::kQuality);
-    }
-    if (resource_adaptation_module_->GetConstAdaptCounter().ResolutionCount(
-            AdaptationObserverInterface::AdaptReason::kQuality) > res_count) {
-      encoder_stats_observer_->OnInitialQualityResolutionAdaptDown();
-    }
+    resource_adaptation_module_->FrameDroppedDueToSize();
     ++initial_framedrop_;
     // Storing references to a native buffer risks blocking frame capture.
     if (video_frame.video_frame_buffer()->type() !=