Fix incorrect FPS measure when frame dropper kicks in

Bug: webrtc:10302
Change-Id: I4f8df7d41d8750e0810c2300fcd90b3eff7fb56d
Reviewed-on: https://webrtc-review.googlesource.com/c/121954
Commit-Queue: Erik Språng <sprang@webrtc.org>
Reviewed-by: Stefan Holmer <stefan@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26610}
diff --git a/video/video_stream_encoder.cc b/video/video_stream_encoder.cc
index f563f4f..168b267 100644
--- a/video/video_stream_encoder.cc
+++ b/video/video_stream_encoder.cc
@@ -884,6 +884,9 @@
   // InitialFrameDropOffWhenEncoderDisabledScaling, the return value
   // from GetScalingSettings should enable or disable the frame drop.
 
+  // Update input frame rate before we start using it. If we update it after
+  // any potential frame drop we are going to artifically increase frame sizes.
+  input_framerate_.Update(1u, clock_->TimeInMilliseconds());
   uint32_t framerate_fps = GetInputFramerateFps();
 
   int64_t now_ms = clock_->TimeInMilliseconds();
@@ -1016,8 +1019,6 @@
   }
 
   encoder_info_ = info;
-
-  input_framerate_.Update(1u, clock_->TimeInMilliseconds());
   video_sender_.AddVideoFrame(out_frame, nullptr, encoder_info_);
 }
 
diff --git a/video/video_stream_encoder_unittest.cc b/video/video_stream_encoder_unittest.cc
index 68a30a1..3497db8 100644
--- a/video/video_stream_encoder_unittest.cc
+++ b/video/video_stream_encoder_unittest.cc
@@ -3295,8 +3295,11 @@
     timestamp_ms += 1000 / kFps;
   }
 
-  // Frame drops should be less than 5%
-  EXPECT_LT(num_dropped, 5 * kNumFramesInRun / 100);
+  // Framerate should be measured to be near the expected target rate.
+  EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
+
+  // Frame drops should be within 5% of expected 0%.
+  EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100);
 
   // Make encoder produce frames at double the expected bitrate during 3 seconds
   // of video, verify number of drops. Rate needs to be slightly changed in
@@ -3320,8 +3323,14 @@
     timestamp_ms += 1000 / kFps;
   }
 
-  // Frame drops should be more than 40%.
-  EXPECT_GT(num_dropped, 40 * kNumFramesInRun / 100);
+  video_stream_encoder_->OnBitrateUpdated(kTargetBitrateBps, 0, 0);
+
+  // Target framerate should be still be near the expected target, despite
+  // the frame drops.
+  EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1);
+
+  // Frame drops should be within 5% of expected 50%.
+  EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100);
 
   video_stream_encoder_->Stop();
 }