Refactor and remove media_optimization::MediaOptimization.

This CL removes MediaOptmization and folds some of its functionality
into VideoStreamEncoder.

The FPS tracking is now handled by a RateStatistics instance. Frame
dropping is still handled by FrameDropper. Both of these now live
directly in VideoStreamEncoder.
There is no intended change in behavior from this CL, but due to a new
way of measuring frame rate, some minor perf changes can be expected.

A small change in behavior is that OnBitrateUpdated is now called
directly rather than on the next frame. Since both encoding frame and
setting rate allocations happen on the encoder worker thread, there's
really no reason to cache bitrates and wait until the next frame.
An edge case though is that if a new bitrate is set before the first
frame, we must remember that bitrate and then apply it after the video
bitrate allocator has been first created.

In addition to existing unit tests, manual tests have been used to
confirm that frame dropping works as expected with misbehaving encoders.

Bug: webrtc:10164
Change-Id: I7ee9c8d3c4f2bcf23c8c420310b05a4d35d94744
Reviewed-on: https://webrtc-review.googlesource.com/c/115620
Commit-Queue: Erik Språng <sprang@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26147}
diff --git a/modules/video_coding/generic_encoder.cc b/modules/video_coding/generic_encoder.cc
index 9dd07bc..2d992ef 100644
--- a/modules/video_coding/generic_encoder.cc
+++ b/modules/video_coding/generic_encoder.cc
@@ -22,7 +22,6 @@
 #include "api/video/video_timing.h"
 #include "modules/include/module_common_types_public.h"
 #include "modules/video_coding/include/video_coding_defines.h"
-#include "modules/video_coding/media_optimization.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/experiments/alr_experiment.h"
 #include "rtc_base/logging.h"
@@ -46,6 +45,7 @@
     : encoder_(encoder),
       vcm_encoded_frame_callback_(encoded_frame_callback),
       internal_source_(internal_source),
+      input_frame_rate_(0),
       streams_or_svc_num_(0),
       codec_type_(VideoCodecType::kVideoCodecGeneric) {}
 
@@ -103,38 +103,34 @@
   return encoder_->Encode(frame, codec_specific, &frame_types);
 }
 
-void VCMGenericEncoder::SetEncoderParameters(const EncoderParameters& params) {
+void VCMGenericEncoder::SetEncoderParameters(
+    const VideoBitrateAllocation& target_bitrate,
+    uint32_t input_frame_rate) {
   RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
   bool rates_have_changed;
   {
     rtc::CritScope lock(&params_lock_);
-    rates_have_changed =
-        params.target_bitrate != encoder_params_.target_bitrate ||
-        params.input_frame_rate != encoder_params_.input_frame_rate;
-    encoder_params_ = params;
+    rates_have_changed = target_bitrate != bitrate_allocation_ ||
+                         input_frame_rate != input_frame_rate_;
+    bitrate_allocation_ = target_bitrate;
+    input_frame_rate_ = input_frame_rate;
   }
   if (rates_have_changed) {
-    int res = encoder_->SetRateAllocation(params.target_bitrate,
-                                          params.input_frame_rate);
+    int res = encoder_->SetRateAllocation(target_bitrate, input_frame_rate);
     if (res != 0) {
       RTC_LOG(LS_WARNING) << "Error set encoder rate (total bitrate bps = "
-                          << params.target_bitrate.get_sum_bps()
-                          << ", framerate = " << params.input_frame_rate
+                          << target_bitrate.get_sum_bps()
+                          << ", framerate = " << input_frame_rate
                           << "): " << res;
     }
-    vcm_encoded_frame_callback_->OnFrameRateChanged(params.input_frame_rate);
+    vcm_encoded_frame_callback_->OnFrameRateChanged(input_frame_rate);
     for (size_t i = 0; i < streams_or_svc_num_; ++i) {
       vcm_encoded_frame_callback_->OnTargetBitrateChanged(
-          params.target_bitrate.GetSpatialLayerSum(i) / 8, i);
+          target_bitrate.GetSpatialLayerSum(i) / 8, i);
     }
   }
 }
 
-EncoderParameters VCMGenericEncoder::GetEncoderParameters() const {
-  rtc::CritScope lock(&params_lock_);
-  return encoder_params_;
-}
-
 int32_t VCMGenericEncoder::RequestFrame(
     const std::vector<FrameType>& frame_types) {
   RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
@@ -164,11 +160,9 @@
 }
 
 VCMEncodedFrameCallback::VCMEncodedFrameCallback(
-    EncodedImageCallback* post_encode_callback,
-    media_optimization::MediaOptimization* media_opt)
+    EncodedImageCallback* post_encode_callback)
     : internal_source_(false),
       post_encode_callback_(post_encode_callback),
-      media_opt_(media_opt),
       framerate_(1),
       last_timing_frame_time_ms_(-1),
       timing_frames_thresholds_({-1, 0}),
@@ -406,20 +400,8 @@
   RTC_CHECK(videocontenttypehelpers::SetSimulcastId(
       &image_copy.content_type_, static_cast<uint8_t>(spatial_idx + 1)));
 
-  Result result = post_encode_callback_->OnEncodedImage(
-      image_copy, codec_specific, fragmentation_header);
-  if (result.error != Result::OK)
-    return result;
-
-  if (media_opt_) {
-    media_opt_->UpdateWithEncodedData(image_copy._length,
-                                      image_copy._frameType);
-    if (internal_source_) {
-      // Signal to encoder to drop next frame.
-      result.drop_next_frame = media_opt_->DropFrame();
-    }
-  }
-  return result;
+  return post_encode_callback_->OnEncodedImage(image_copy, codec_specific,
+                                               fragmentation_header);
 }
 
 }  // namespace webrtc