Reland of Add framerate to VideoSinkWants and ability to signal on overuse (patchset #1 id:1 of https://codereview.webrtc.org/2783183003/ )

Reason for revert:
Seem to be a flaky test rather than an issue with this cl. Creating reland, will add code to reduce flakiness to that test.

Original issue's description:
> Revert of Add framerate to VideoSinkWants and ability to signal on overuse (patchset #8 id:410001 of https://codereview.webrtc.org/2781433002/ )
>
> Reason for revert:
> This has resulted in failure of CallPerfTest.ReceivesCpuOveruseAndUnderuse test on the Win7 build bot https://build.chromium.org/p/client.webrtc.perf/builders/Win7/builds/1780
>
> Original issue's description:
> > Reland of Add framerate to VideoSinkWants and ability to signal on overuse (patchset #1 id:1 of https://codereview.webrtc.org/2764133002/ )
> >
> > Reason for revert:
> > Found issue with test case, will add fix to reland cl.
> >
> > Original issue's description:
> > > Revert of Add framerate to VideoSinkWants and ability to signal on overuse (patchset #14 id:250001 of https://codereview.webrtc.org/2716643002/ )
> > >
> > > Reason for revert:
> > > Breaks perf tests:
> > > https://build.chromium.org/p/client.webrtc.perf/builders/Win7/builds/1679
> > > https://build.chromium.org/p/client.webrtc.perf/builders/Android32%20Tests%20%28L%20Nexus5%29/builds/2325
> > >
> > > Original issue's description:
> > > > Add framerate to VideoSinkWants and ability to signal on overuse
> > > >
> > > > In ViEEncoder, try to reduce framerate instead of resolution if the
> > > > current degradation preference is maintain-resolution rather than
> > > > balanced.
> > > >
> > > > BUG=webrtc:4172
> > > >
> > > > Review-Url: https://codereview.webrtc.org/2716643002
> > > > Cr-Commit-Position: refs/heads/master@{#17327}
> > > > Committed: https://chromium.googlesource.com/external/webrtc/+/72acf2526177bb4dbb5103cd6e165eb4361a5ae6
> > >
> > > TBR=nisse@webrtc.org,magjed@webrtc.org,kthelgason@webrtc.org,ilnik@webrtc.org,stefan@webrtc.org,sprang@webrtc.org
> > > # Skipping CQ checks because original CL landed less than 1 days ago.
> > > NOPRESUBMIT=true
> > > NOTREECHECKS=true
> > > NOTRY=true
> > > BUG=webrtc:4172
> > >
> > > Review-Url: https://codereview.webrtc.org/2764133002
> > > Cr-Commit-Position: refs/heads/master@{#17331}
> > > Committed: https://chromium.googlesource.com/external/webrtc/+/8b45b11144c968b4173215c76f78c710c9a2ed0b
> >
> > TBR=nisse@webrtc.org,magjed@webrtc.org,kthelgason@webrtc.org,ilnik@webrtc.org,stefan@webrtc.org,skvlad@webrtc.org
> > # Not skipping CQ checks because original CL landed more than 1 days ago.
> > BUG=webrtc:4172
> >
> > Review-Url: https://codereview.webrtc.org/2781433002
> > Cr-Commit-Position: refs/heads/master@{#17474}
> > Committed: https://chromium.googlesource.com/external/webrtc/+/3ea3c77e93121b1ab9d5e46641e6764f2cca0d51
>
> TBR=ilnik@webrtc.org,stefan@webrtc.org,asapersson@webrtc.org,sprang@webrtc.org
> # Skipping CQ checks because original CL landed less than 1 days ago.
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
> BUG=webrtc:4172
>
> Review-Url: https://codereview.webrtc.org/2783183003
> Cr-Commit-Position: refs/heads/master@{#17477}
> Committed: https://chromium.googlesource.com/external/webrtc/+/f9ed235c9b7248694edcb46feb1f29ce7456ab59

R=ilnik@webrtc.org,stefan@webrtc.org
BUG=webrtc:4172

Review-Url: https://codereview.webrtc.org/2789823002
Cr-Commit-Position: refs/heads/master@{#17498}
diff --git a/webrtc/call/call_perf_tests.cc b/webrtc/call/call_perf_tests.cc
index 0bbb7d4..a31cb23 100644
--- a/webrtc/call/call_perf_tests.cc
+++ b/webrtc/call/call_perf_tests.cc
@@ -30,6 +30,7 @@
 #include "webrtc/test/fake_audio_device.h"
 #include "webrtc/test/fake_decoder.h"
 #include "webrtc/test/fake_encoder.h"
+#include "webrtc/test/field_trial.h"
 #include "webrtc/test/frame_generator.h"
 #include "webrtc/test/frame_generator_capturer.h"
 #include "webrtc/test/gtest.h"
@@ -477,13 +478,15 @@
 }
 
 TEST_F(CallPerfTest, ReceivesCpuOveruseAndUnderuse) {
+  // Minimal normal usage at the start, then 30s overuse to allow filter to
+  // settle, and then 80s underuse to allow plenty of time for rampup again.
+  test::ScopedFieldTrials fake_overuse_settings(
+      "WebRTC-ForceSimulatedOveruseIntervalMs/1-30000-80000/");
+
   class LoadObserver : public test::SendTest,
                        public test::FrameGeneratorCapturer::SinkWantsObserver {
    public:
-    LoadObserver()
-        : SendTest(kLongTimeoutMs),
-          expect_lower_resolution_wants_(true),
-          encoder_(Clock::GetRealTimeClock(), 60 /* delay_ms */) {}
+    LoadObserver() : SendTest(kLongTimeoutMs), test_phase_(TestPhase::kStart) {}
 
     void OnFrameGeneratorCapturerCreated(
         test::FrameGeneratorCapturer* frame_generator_capturer) override {
@@ -494,24 +497,43 @@
 
     // OnSinkWantsChanged is called when FrameGeneratorCapturer::AddOrUpdateSink
     // is called.
+    // TODO(sprang): Add integration test for maintain-framerate mode?
     void OnSinkWantsChanged(rtc::VideoSinkInterface<VideoFrame>* sink,
                             const rtc::VideoSinkWants& wants) override {
       // First expect CPU overuse. Then expect CPU underuse when the encoder
       // delay has been decreased.
-      if (wants.target_pixel_count &&
-          *wants.target_pixel_count <
-              wants.max_pixel_count.value_or(std::numeric_limits<int>::max())) {
-        // On adapting up, ViEEncoder::VideoSourceProxy will set the target
-        // pixel count to a step up from the current and the max value to
-        // something higher than the target.
-        EXPECT_FALSE(expect_lower_resolution_wants_);
-        observation_complete_.Set();
-      } else if (wants.max_pixel_count) {
-        // On adapting down, ViEEncoder::VideoSourceProxy will set only the max
-        // pixel count, leaving the target unset.
-        EXPECT_TRUE(expect_lower_resolution_wants_);
-        expect_lower_resolution_wants_ = false;
-        encoder_.SetDelay(2);
+      switch (test_phase_) {
+        case TestPhase::kStart:
+          if (wants.max_pixel_count < std::numeric_limits<int>::max()) {
+            // On adapting down, ViEEncoder::VideoSourceProxy will set only the
+            // max pixel count, leaving the target unset.
+            test_phase_ = TestPhase::kAdaptedDown;
+          } else {
+            ADD_FAILURE() << "Got unexpected adaptation request, max res = "
+                          << wants.max_pixel_count << ", target res = "
+                          << wants.target_pixel_count.value_or(-1)
+                          << ", max fps = " << wants.max_framerate_fps;
+          }
+          break;
+        case TestPhase::kAdaptedDown:
+          // On adapting up, the adaptation counter will again be at zero, and
+          // so all constraints will be reset.
+          if (wants.max_pixel_count == std::numeric_limits<int>::max() &&
+              !wants.target_pixel_count) {
+            test_phase_ = TestPhase::kAdaptedUp;
+            observation_complete_.Set();
+          } else {
+            ADD_FAILURE() << "Got unexpected adaptation request, max res = "
+                          << wants.max_pixel_count << ", target res = "
+                          << wants.target_pixel_count.value_or(-1)
+                          << ", max fps = " << wants.max_framerate_fps;
+          }
+          break;
+        case TestPhase::kAdaptedUp:
+          ADD_FAILURE() << "Got unexpected adaptation request, max res = "
+                        << wants.max_pixel_count << ", target res = "
+                        << wants.target_pixel_count.value_or(-1)
+                        << ", max fps = " << wants.max_framerate_fps;
       }
     }
 
@@ -519,15 +541,13 @@
         VideoSendStream::Config* send_config,
         std::vector<VideoReceiveStream::Config>* receive_configs,
         VideoEncoderConfig* encoder_config) override {
-      send_config->encoder_settings.encoder = &encoder_;
     }
 
     void PerformTest() override {
       EXPECT_TRUE(Wait()) << "Timed out before receiving an overuse callback.";
     }
 
-    bool expect_lower_resolution_wants_;
-    test::DelayedEncoder encoder_;
+    enum class TestPhase { kStart, kAdaptedDown, kAdaptedUp } test_phase_;
   } test;
 
   RunBaseTest(&test);