Add ability to specify if rate controller of video encoder is trusted.

If rate controller is trusted, we disable the frame dropper in the
media optimization module.

This is a re-land of
https://webrtc-review.googlesource.com/c/src/+/105020

Bug: webrtc:9890
Change-Id: I418e47a43a1a98cb2fd5295c03360b954f2288f2
Reviewed-on: https://webrtc-review.googlesource.com/c/109141
Commit-Queue: Erik Språng <sprang@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Reviewed-by: Rasmus Brandt <brandtr@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25570}
diff --git a/media/engine/simulcast_encoder_adapter_unittest.cc b/media/engine/simulcast_encoder_adapter_unittest.cc
index 4854725..5954175 100644
--- a/media/engine/simulcast_encoder_adapter_unittest.cc
+++ b/media/engine/simulcast_encoder_adapter_unittest.cc
@@ -216,6 +216,7 @@
     info.supports_native_handle = supports_native_handle_;
     info.implementation_name = implementation_name_;
     info.scaling_settings = scaling_settings_;
+    info.has_trusted_rate_controller = has_trusted_rate_controller_;
     return info;
   }
 
@@ -250,6 +251,10 @@
     scaling_settings_ = settings;
   }
 
+  void set_has_trusted_rate_controller(bool trusted) {
+    has_trusted_rate_controller_ = trusted;
+  }
+
   VideoBitrateAllocation last_set_bitrate() const { return last_set_bitrate_; }
 
  private:
@@ -257,6 +262,7 @@
   bool supports_native_handle_ = false;
   std::string implementation_name_ = "unknown";
   VideoEncoder::ScalingSettings scaling_settings_;
+  bool has_trusted_rate_controller_ = false;
   int32_t init_encode_return_value_ = 0;
   VideoBitrateAllocation last_set_bitrate_;
 
@@ -954,5 +960,50 @@
   frame_types.resize(3, kVideoFrameKey);
   EXPECT_EQ(0, adapter_->Encode(input_frame, nullptr, &frame_types));
 }
+
+TEST_F(TestSimulcastEncoderAdapterFake, TrustedRateControl) {
+  // Set up common settings for three streams.
+  SimulcastTestFixtureImpl::DefaultSettings(
+      &codec_, static_cast<const int*>(kTestTemporalLayerProfile),
+      kVideoCodecVP8);
+  rate_allocator_.reset(new SimulcastRateAllocator(codec_));
+  adapter_->RegisterEncodeCompleteCallback(this);
+
+  // Only enough start bitrate for the lowest stream.
+  ASSERT_EQ(3u, codec_.numberOfSimulcastStreams);
+  codec_.startBitrate = codec_.simulcastStream[0].targetBitrate +
+                        codec_.simulcastStream[1].minBitrate - 1;
+
+  // Input data.
+  rtc::scoped_refptr<VideoFrameBuffer> buffer(I420Buffer::Create(1280, 720));
+  VideoFrame input_frame(buffer, 100, 1000, kVideoRotation_180);
+
+  // No encoder trusted, so simulcast adapter should not be either.
+  EXPECT_EQ(0, adapter_->InitEncode(&codec_, 1, 1200));
+  EXPECT_FALSE(adapter_->GetEncoderInfo().has_trusted_rate_controller);
+
+  // Encode with three streams.
+  std::vector<MockVideoEncoder*> original_encoders =
+      helper_->factory()->encoders();
+
+  // All encoders are trusted, so simulcast adapter should be too.
+  original_encoders[0]->set_has_trusted_rate_controller(true);
+  original_encoders[1]->set_has_trusted_rate_controller(true);
+  original_encoders[2]->set_has_trusted_rate_controller(true);
+  EXPECT_EQ(0, adapter_->InitEncode(&codec_, 1, 1200));
+  EXPECT_TRUE(adapter_->GetEncoderInfo().has_trusted_rate_controller);
+
+  // One encoder not trusted, so simulcast adapter should not be either.
+  original_encoders[2]->set_has_trusted_rate_controller(false);
+  EXPECT_EQ(0, adapter_->InitEncode(&codec_, 1, 1200));
+  EXPECT_FALSE(adapter_->GetEncoderInfo().has_trusted_rate_controller);
+
+  // No encoder trusted, so simulcast adapter should not be either.
+  original_encoders[0]->set_has_trusted_rate_controller(false);
+  original_encoders[1]->set_has_trusted_rate_controller(false);
+  EXPECT_EQ(0, adapter_->InitEncode(&codec_, 1, 1200));
+  EXPECT_FALSE(adapter_->GetEncoderInfo().has_trusted_rate_controller);
+}
+
 }  // namespace test
 }  // namespace webrtc