[Adaptation] Make resource most limited if kLimitReached hit

This occurs when a resource causes an adaptation down but the current
adaptations can not be adapted any more. Any further adaptation will result in the status kLimitReached,
and so any resource that adapts down should also be most limited.

Bug: webrtc:11695
Change-Id: Idfdf23f482b1b4a132cec49a9be76adc0aec4361
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/181586
Commit-Queue: Evan Shrubsole <eshr@google.com>
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31933}
diff --git a/call/adaptation/resource_adaptation_processor.cc b/call/adaptation/resource_adaptation_processor.cc
index 94a5b7f..fd81c9b 100644
--- a/call/adaptation/resource_adaptation_processor.cc
+++ b/call/adaptation/resource_adaptation_processor.cc
@@ -305,6 +305,13 @@
   if (adaptation.min_pixel_limit_reached()) {
     encoder_stats_observer_->OnMinPixelLimitReached();
   }
+  if (adaptation.status() == Adaptation::Status::kLimitReached) {
+    // Add resource as most limited.
+    VideoStreamAdapter::RestrictionsWithCounters restrictions;
+    std::tie(std::ignore, restrictions) = FindMostLimitedResources();
+    UpdateResourceLimitations(reason_resource, restrictions.restrictions,
+                              restrictions.counters);
+  }
   if (adaptation.status() != Adaptation::Status::kValid) {
     rtc::StringBuilder message;
     message << "Not adapting down because VideoStreamAdapter returned "
diff --git a/call/adaptation/resource_adaptation_processor_unittest.cc b/call/adaptation/resource_adaptation_processor_unittest.cc
index c27958c..17e0a2f 100644
--- a/call/adaptation/resource_adaptation_processor_unittest.cc
+++ b/call/adaptation/resource_adaptation_processor_unittest.cc
@@ -709,4 +709,29 @@
   resource_ = nullptr;
 }
 
+TEST_F(ResourceAdaptationProcessorTest,
+       ResourceOverusedAtLimitReachedWillShareMostLimited) {
+  video_stream_adapter_->SetDegradationPreference(
+      DegradationPreference::MAINTAIN_FRAMERATE);
+  SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
+
+  bool has_reached_min_pixels = false;
+  ON_CALL(frame_rate_provider_, OnMinPixelLimitReached())
+      .WillByDefault(testing::Assign(&has_reached_min_pixels, true));
+
+  // Adapt 10 times, which should make us hit the limit.
+  for (int i = 0; i < 10; ++i) {
+    resource_->SetUsageState(ResourceUsageState::kOveruse);
+    RestrictSource(restrictions_listener_.restrictions());
+  }
+  EXPECT_TRUE(has_reached_min_pixels);
+  auto last_update_count = restrictions_listener_.restrictions_updated_count();
+  other_resource_->SetUsageState(ResourceUsageState::kOveruse);
+  // Now both |resource_| and |other_resource_| are most limited. Underuse of
+  // |resource_| will not adapt up.
+  resource_->SetUsageState(ResourceUsageState::kUnderuse);
+  EXPECT_EQ(last_update_count,
+            restrictions_listener_.restrictions_updated_count());
+}
+
 }  // namespace webrtc