Recreate WebrtcVideoSendStream if screen content setting is changed.

This avoids the situation where an encoder, not supporting certain
screen content settings, is created for a config where screencast is
off, and later ReconfigureEncoder() is called updating the configuration
but not the encoder instance, causing an inconsistency in the encoder's
InitEncode() call.

TBR=pthatcher@webrtc.org
BUG=webrtc:4172

Review-Url: https://codereview.webrtc.org/2710493008
Cr-Commit-Position: refs/heads/master@{#16921}
diff --git a/webrtc/media/engine/fakewebrtcvideoengine.h b/webrtc/media/engine/fakewebrtcvideoengine.h
index 497d701..2dce2c5 100644
--- a/webrtc/media/engine/fakewebrtcvideoengine.h
+++ b/webrtc/media/engine/fakewebrtcvideoengine.h
@@ -196,10 +196,13 @@
   }
 
   bool WaitForCreatedVideoEncoders(int num_encoders) {
-    while (created_video_encoder_event_.Wait(kEventTimeoutMs)) {
+    int64_t start_offset_ms = rtc::TimeMillis();
+    int64_t wait_time = kEventTimeoutMs;
+    do {
       if (GetNumCreatedEncoders() >= num_encoders)
         return true;
-    }
+      wait_time = kEventTimeoutMs - (rtc::TimeMillis() - start_offset_ms);
+    } while (wait_time > 0 && created_video_encoder_event_.Wait(wait_time));
     return false;
   }
 
diff --git a/webrtc/media/engine/webrtcvideoengine2.cc b/webrtc/media/engine/webrtcvideoengine2.cc
index 39bec64..9f37f33 100644
--- a/webrtc/media/engine/webrtcvideoengine2.cc
+++ b/webrtc/media/engine/webrtcvideoengine2.cc
@@ -1588,7 +1588,8 @@
                                          ? webrtc::RtcpMode::kReducedSize
                                          : webrtc::RtcpMode::kCompound;
   if (codec_settings) {
-    SetCodec(*codec_settings);
+    bool force_encoder_allocation = false;
+    SetCodec(*codec_settings, force_encoder_allocation);
   }
 }
 
@@ -1612,6 +1613,18 @@
   if (options_present) {
     VideoOptions old_options = parameters_.options;
     parameters_.options.SetAll(*options);
+    if (parameters_.options.is_screencast.value_or(false) !=
+            old_options.is_screencast.value_or(false) &&
+        parameters_.codec_settings) {
+      // If screen content settings change, we may need to recreate the codec
+      // instance so that the correct type is used.
+
+      bool force_encoder_allocation = true;
+      SetCodec(*parameters_.codec_settings, force_encoder_allocation);
+      // Mark screenshare parameter as being updated, then test for any other
+      // changes that may require codec reconfiguration.
+      old_options.is_screencast = options->is_screencast;
+    }
     if (parameters_.options != old_options) {
       ReconfigureEncoder();
     }
@@ -1645,10 +1658,11 @@
 
 WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder
 WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder(
-    const VideoCodec& codec) {
+    const VideoCodec& codec,
+    bool force_encoder_allocation) {
   RTC_DCHECK_RUN_ON(&thread_checker_);
   // Do not re-create encoders of the same type.
-  if (codec == allocated_encoder_.codec &&
+  if (!force_encoder_allocation && codec == allocated_encoder_.codec &&
       allocated_encoder_.encoder != nullptr) {
     return allocated_encoder_;
   }
@@ -1695,12 +1709,14 @@
 }
 
 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec(
-    const VideoCodecSettings& codec_settings) {
+    const VideoCodecSettings& codec_settings,
+    bool force_encoder_allocation) {
   RTC_DCHECK_RUN_ON(&thread_checker_);
   parameters_.encoder_config = CreateVideoEncoderConfig(codec_settings.codec);
   RTC_DCHECK_GT(parameters_.encoder_config.number_of_streams, 0);
 
-  AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec);
+  AllocatedEncoder new_encoder =
+      CreateVideoEncoder(codec_settings.codec, force_encoder_allocation);
   parameters_.config.encoder_settings.encoder = new_encoder.encoder;
   parameters_.config.encoder_settings.full_overuse_time = new_encoder.external;
   parameters_.config.encoder_settings.payload_name = codec_settings.codec.name;
@@ -1767,10 +1783,12 @@
 
   // Set codecs and options.
   if (params.codec) {
-    SetCodec(*params.codec);
+    bool force_encoder_allocation = false;
+    SetCodec(*params.codec, force_encoder_allocation);
     recreate_stream = false;  // SetCodec has already recreated the stream.
   } else if (params.conference_mode && parameters_.codec_settings) {
-    SetCodec(*parameters_.codec_settings);
+    bool force_encoder_allocation = false;
+    SetCodec(*parameters_.codec_settings, force_encoder_allocation);
     recreate_stream = false;  // SetCodec has already recreated the stream.
   }
   if (recreate_stream) {
diff --git a/webrtc/media/engine/webrtcvideoengine2.h b/webrtc/media/engine/webrtcvideoengine2.h
index 7043e03..309a84d 100644
--- a/webrtc/media/engine/webrtcvideoengine2.h
+++ b/webrtc/media/engine/webrtcvideoengine2.h
@@ -306,9 +306,14 @@
 
     rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings>
     ConfigureVideoEncoderSettings(const VideoCodec& codec);
-    AllocatedEncoder CreateVideoEncoder(const VideoCodec& codec);
+    // If force_encoder_allocation is true, a new AllocatedEncoder is always
+    // created. If false, the allocated encoder may be reused, if the type
+    // matches.
+    AllocatedEncoder CreateVideoEncoder(const VideoCodec& codec,
+                                        bool force_encoder_allocation);
     void DestroyVideoEncoder(AllocatedEncoder* encoder);
-    void SetCodec(const VideoCodecSettings& codec);
+    void SetCodec(const VideoCodecSettings& codec,
+                  bool force_encoder_allocation);
     void RecreateWebRtcStream();
     webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
         const VideoCodec& codec) const;
diff --git a/webrtc/media/engine/webrtcvideoengine2_unittest.cc b/webrtc/media/engine/webrtcvideoengine2_unittest.cc
index 5c560c1..57ba5d2 100644
--- a/webrtc/media/engine/webrtcvideoengine2_unittest.cc
+++ b/webrtc/media/engine/webrtcvideoengine2_unittest.cc
@@ -906,6 +906,68 @@
   EXPECT_EQ(sp.id, params[0].receive_stream_id);
 }
 
+TEST_F(WebRtcVideoEngine2Test, RecreatesEncoderOnContentTypeChange) {
+  cricket::FakeWebRtcVideoEncoderFactory encoder_factory;
+  encoder_factory.AddSupportedVideoCodecType("VP8");
+  std::unique_ptr<FakeCall> fake_call(
+      new FakeCall(webrtc::Call::Config(&event_log_)));
+  std::unique_ptr<VideoMediaChannel> channel(
+      SetUpForExternalEncoderFactory(&encoder_factory));
+  ASSERT_TRUE(
+      channel->AddSendStream(cricket::StreamParams::CreateLegacy(kSsrc)));
+  cricket::VideoCodec codec = GetEngineCodec("VP8");
+  cricket::VideoSendParameters parameters;
+  parameters.codecs.push_back(codec);
+  channel->OnReadyToSend(true);
+  channel->SetSend(true);
+  ASSERT_TRUE(channel->SetSendParameters(parameters));
+
+  cricket::FakeVideoCapturer capturer;
+  VideoOptions options;
+  EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, &options, &capturer));
+
+  EXPECT_EQ(cricket::CS_RUNNING,
+            capturer.Start(capturer.GetSupportedFormats()->front()));
+  EXPECT_TRUE(capturer.CaptureFrame());
+  ASSERT_TRUE(encoder_factory.WaitForCreatedVideoEncoders(1));
+  EXPECT_EQ(webrtc::kRealtimeVideo,
+            encoder_factory.encoders().back()->GetCodecSettings().mode);
+
+  EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, &options, &capturer));
+  EXPECT_TRUE(capturer.CaptureFrame());
+  // No change in content type, keep current encoder.
+  EXPECT_EQ(1, encoder_factory.GetNumCreatedEncoders());
+
+  options.is_screencast.emplace(true);
+  EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, &options, &capturer));
+  EXPECT_TRUE(capturer.CaptureFrame());
+  // Change to screen content, recreate encoder. For the simulcast encoder
+  // adapter case, this will result in two calls since InitEncode triggers a
+  // a new instance.
+  ASSERT_TRUE(encoder_factory.WaitForCreatedVideoEncoders(2));
+  EXPECT_EQ(webrtc::kScreensharing,
+            encoder_factory.encoders().back()->GetCodecSettings().mode);
+
+  EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, &options, &capturer));
+  EXPECT_TRUE(capturer.CaptureFrame());
+  // Still screen content, no need to update encoder.
+  EXPECT_EQ(2, encoder_factory.GetNumCreatedEncoders());
+
+  options.is_screencast.emplace(false);
+  options.video_noise_reduction.emplace(false);
+  EXPECT_TRUE(channel->SetVideoSend(kSsrc, true, &options, &capturer));
+  // Change back to regular video content, update encoder. Also change
+  // a non |is_screencast| option just to verify it doesn't affect recreation.
+  EXPECT_TRUE(capturer.CaptureFrame());
+  ASSERT_TRUE(encoder_factory.WaitForCreatedVideoEncoders(3));
+  EXPECT_EQ(webrtc::kRealtimeVideo,
+            encoder_factory.encoders().back()->GetCodecSettings().mode);
+
+  // Remove stream previously added to free the external encoder instance.
+  EXPECT_TRUE(channel->RemoveSendStream(kSsrc));
+  EXPECT_EQ(0u, encoder_factory.encoders().size());
+}
+
 #define WEBRTC_BASE_TEST(test) \
   TEST_F(WebRtcVideoChannel2BaseTest, test) { Base::test(); }
 
@@ -1668,9 +1730,10 @@
   EXPECT_TRUE(
       channel_->SetVideoSend(last_ssrc_, true, &screencast_options, &capturer));
   EXPECT_TRUE(capturer.CaptureFrame());
-  // Send stream not recreated after option change.
-  ASSERT_EQ(send_stream, fake_call_->GetVideoSendStreams().front());
-  EXPECT_EQ(2, send_stream->GetNumberOfSwappedFrames());
+  // Send stream recreated after option change.
+  ASSERT_EQ(2, fake_call_->GetNumCreatedSendStreams());
+  send_stream = fake_call_->GetVideoSendStreams().front();
+  EXPECT_EQ(1, send_stream->GetNumberOfSwappedFrames());
 
   // Verify screencast settings.
   encoder_config = send_stream->GetEncoderConfig().Copy();
@@ -1686,57 +1749,6 @@
   EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, nullptr, nullptr));
 }
 
-TEST_F(WebRtcVideoChannel2Test, NoRecreateStreamForScreencast) {
-  EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
-  ASSERT_TRUE(
-      channel_->AddSendStream(cricket::StreamParams::CreateLegacy(kSsrc)));
-  EXPECT_TRUE(channel_->SetSend(true));
-
-  cricket::FakeVideoCapturer capturer;
-  EXPECT_TRUE(channel_->SetVideoSend(kSsrc, true, nullptr, &capturer));
-  EXPECT_EQ(cricket::CS_RUNNING,
-            capturer.Start(capturer.GetSupportedFormats()->front()));
-  EXPECT_TRUE(capturer.CaptureFrame());
-
-  ASSERT_EQ(1, fake_call_->GetNumCreatedSendStreams());
-  FakeVideoSendStream* stream = fake_call_->GetVideoSendStreams().front();
-  webrtc::VideoEncoderConfig encoder_config = stream->GetEncoderConfig().Copy();
-  EXPECT_EQ(webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo,
-            encoder_config.content_type);
-
-  EXPECT_EQ(1, stream->GetNumberOfSwappedFrames());
-
-  /* Switch to screencast source. We expect a reconfigure of the
-   * encoder, but no change of the send stream. */
-  struct VideoOptions video_options;
-  video_options.is_screencast = rtc::Optional<bool>(true);
-  EXPECT_TRUE(channel_->SetVideoSend(kSsrc, true, &video_options, &capturer));
-
-  EXPECT_TRUE(capturer.CaptureFrame());
-  ASSERT_EQ(1, fake_call_->GetNumCreatedSendStreams());
-  ASSERT_EQ(stream, fake_call_->GetVideoSendStreams().front());
-  EXPECT_EQ(2, stream->GetNumberOfSwappedFrames());
-
-  encoder_config = stream->GetEncoderConfig().Copy();
-  EXPECT_EQ(webrtc::VideoEncoderConfig::ContentType::kScreen,
-            encoder_config.content_type);
-
-  /* Switch back. */
-  video_options.is_screencast = rtc::Optional<bool>(false);
-  EXPECT_TRUE(channel_->SetVideoSend(kSsrc, true, &video_options, &capturer));
-
-  EXPECT_TRUE(capturer.CaptureFrame());
-  ASSERT_EQ(1, fake_call_->GetNumCreatedSendStreams());
-  ASSERT_EQ(stream, fake_call_->GetVideoSendStreams().front());
-  EXPECT_EQ(3, stream->GetNumberOfSwappedFrames());
-
-  encoder_config = stream->GetEncoderConfig().Copy();
-  EXPECT_EQ(webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo,
-            encoder_config.content_type);
-
-  EXPECT_TRUE(channel_->SetVideoSend(kSsrc, true, nullptr, nullptr));
-}
-
 TEST_F(WebRtcVideoChannel2Test,
        ConferenceModeScreencastConfiguresTemporalLayer) {
   static const int kConferenceScreencastTemporalBitrateBps =
@@ -1910,7 +1922,7 @@
   EXPECT_EQ(1, send_stream->num_encoder_reconfigurations());
 
   // Change |options| and expect 2 reconfigurations.
-  options.is_screencast = rtc::Optional<bool>(true);
+  options.video_noise_reduction = rtc::Optional<bool>(true);
   EXPECT_TRUE(channel_->SetVideoSend(last_ssrc_, true, &options, &capturer));
   EXPECT_EQ(2, send_stream->num_encoder_reconfigurations());
 
@@ -2204,15 +2216,24 @@
   channel_->SetVideoSend(last_ssrc_, true /* enable */, &screenshare_options,
                          &screen_share);
   EXPECT_TRUE(screen_share.CaptureCustomFrame(1284, 724, cricket::FOURCC_I420));
-  EXPECT_EQ(3, send_stream->GetNumberOfSwappedFrames());
+  ASSERT_EQ(2, fake_call_->GetNumCreatedSendStreams());
+  send_stream = fake_call_->GetVideoSendStreams().front();
+  EXPECT_EQ(1, send_stream->GetNumberOfSwappedFrames());
   EXPECT_EQ(1284, send_stream->GetLastWidth());
   EXPECT_EQ(724, send_stream->GetLastHeight());
 
   // Switch back to the normal capturer. Expect the frame to be CPU adapted.
   channel_->SetVideoSend(last_ssrc_, true /* enable */, &camera_options,
                          &capturer);
+  send_stream = fake_call_->GetVideoSendStreams().front();
+  // We have a new fake send stream, so it doesn't remember the old sink wants.
+  // In practice, it will be populated from
+  // ViEEncoder::VideoSourceProxy::SetSource(), so simulate that here.
+  send_stream->InjectVideoSinkWants(wants);
   EXPECT_TRUE(capturer.CaptureCustomFrame(1280, 720, cricket::FOURCC_I420));
-  EXPECT_EQ(4, send_stream->GetNumberOfSwappedFrames());
+  ASSERT_EQ(3, fake_call_->GetNumCreatedSendStreams());
+  send_stream = fake_call_->GetVideoSendStreams().front();
+  EXPECT_EQ(1, send_stream->GetNumberOfSwappedFrames());
   EXPECT_EQ(1280 * 3 / 4, send_stream->GetLastWidth());
   EXPECT_EQ(720 * 3 / 4, send_stream->GetLastHeight());
 
@@ -4170,25 +4191,26 @@
                                bool screenshare,
                                bool conference_mode) {
     cricket::VideoSendParameters parameters;
-    VideoOptions options;
     parameters.codecs.push_back(codec);
     parameters.conference_mode = conference_mode;
-    if (screenshare) {
-      options.is_screencast = rtc::Optional<bool>(screenshare);
-    }
     ASSERT_TRUE(channel_->SetSendParameters(parameters));
 
     std::vector<uint32_t> ssrcs = MAKE_VECTOR(kSsrcs3);
     RTC_DCHECK(num_configured_streams <= ssrcs.size());
     ssrcs.resize(num_configured_streams);
 
-    FakeVideoSendStream* stream =
-        AddSendStream(CreateSimStreamParams("cname", ssrcs));
+    AddSendStream(CreateSimStreamParams("cname", ssrcs));
     // Send a full-size frame to trigger a stream reconfiguration to use all
     // expected simulcast layers.
     cricket::FakeVideoCapturer capturer;
+    VideoOptions options;
+    if (screenshare)
+      options.is_screencast = rtc::Optional<bool>(screenshare);
     EXPECT_TRUE(
         channel_->SetVideoSend(ssrcs.front(), true, &options, &capturer));
+    // Fetch the latest stream since SetVideoSend() may recreate it if the
+    // screen content setting is changed.
+    FakeVideoSendStream* stream = fake_call_.GetVideoSendStreams().front();
     EXPECT_EQ(cricket::CS_RUNNING, capturer.Start(cricket::VideoFormat(
                                        capture_width, capture_height,
                                        cricket::VideoFormat::FpsToInterval(30),
diff --git a/webrtc/video/video_send_stream.cc b/webrtc/video/video_send_stream.cc
index c777ce8..3f2bba8 100644
--- a/webrtc/video/video_send_stream.cc
+++ b/webrtc/video/video_send_stream.cc
@@ -629,7 +629,8 @@
       stats_proxy_(Clock::GetRealTimeClock(),
                    config,
                    encoder_config.content_type),
-      config_(std::move(config)) {
+      config_(std::move(config)),
+      content_type_(encoder_config.content_type) {
   vie_encoder_.reset(new ViEEncoder(
       num_cpu_cores, &stats_proxy_, config_.encoder_settings,
       config_.pre_encode_callback, config_.post_encode_callback));
@@ -692,6 +693,7 @@
   // TODO(perkj): Some test cases in VideoSendStreamTest call
   // ReconfigureVideoEncoder from the network thread.
   // RTC_DCHECK_RUN_ON(&thread_checker_);
+  RTC_DCHECK(content_type_ == config.content_type);
   vie_encoder_->ConfigureEncoder(std::move(config), config_.rtp.max_packet_size,
                                  config_.rtp.nack.rtp_history_ms > 0);
 }
diff --git a/webrtc/video/video_send_stream.h b/webrtc/video/video_send_stream.h
index be62a2e..9617580 100644
--- a/webrtc/video/video_send_stream.h
+++ b/webrtc/video/video_send_stream.h
@@ -104,6 +104,7 @@
 
   SendStatisticsProxy stats_proxy_;
   const VideoSendStream::Config config_;
+  const VideoEncoderConfig::ContentType content_type_;
   std::unique_ptr<VideoSendStreamImpl> send_stream_;
   std::unique_ptr<ViEEncoder> vie_encoder_;
 };
diff --git a/webrtc/video/video_send_stream_tests.cc b/webrtc/video/video_send_stream_tests.cc
index 189f030..4617622 100644
--- a/webrtc/video/video_send_stream_tests.cc
+++ b/webrtc/video/video_send_stream_tests.cc
@@ -1513,22 +1513,32 @@
   RunBaseTest(&test);
 }
 
+// Test class takes takes as argument a switch selecting if type switch should
+// occur and a function pointer to reset the send stream. This is necessary
+// since you cannot change the content type of a VideoSendStream, you need to
+// recreate it. Stopping and recreating the stream can only be done on the main
+// thread and in the context of VideoSendStreamTest (not BaseTest).
+template <typename T>
 class MaxPaddingSetTest : public test::SendTest {
  public:
   static const uint32_t kMinTransmitBitrateBps = 400000;
   static const uint32_t kActualEncodeBitrateBps = 40000;
   static const uint32_t kMinPacketsToSend = 50;
 
-  explicit MaxPaddingSetTest(bool test_switch_content_type)
+  explicit MaxPaddingSetTest(bool test_switch_content_type, T* stream_reset_fun)
       : SendTest(test::CallTest::kDefaultTimeoutMs),
+        content_switch_event_(false, false),
         call_(nullptr),
         send_stream_(nullptr),
+        send_stream_config_(nullptr),
         packets_sent_(0),
-        running_without_padding_(test_switch_content_type) {}
+        running_without_padding_(test_switch_content_type),
+        stream_resetter_(stream_reset_fun) {}
 
   void OnVideoStreamsCreated(
       VideoSendStream* send_stream,
       const std::vector<VideoReceiveStream*>& receive_streams) override {
+    rtc::CritScope lock(&crit_);
     send_stream_ = send_stream;
   }
 
@@ -1537,7 +1547,7 @@
       std::vector<VideoReceiveStream::Config>* receive_configs,
       VideoEncoderConfig* encoder_config) override {
     RTC_DCHECK_EQ(1, encoder_config->number_of_streams);
-    if (running_without_padding_) {
+    if (RunningWithoutPadding()) {
       encoder_config->min_transmit_bitrate_bps = 0;
       encoder_config->content_type =
           VideoEncoderConfig::ContentType::kRealtimeVideo;
@@ -1545,6 +1555,7 @@
       encoder_config->min_transmit_bitrate_bps = kMinTransmitBitrateBps;
       encoder_config->content_type = VideoEncoderConfig::ContentType::kScreen;
     }
+    send_stream_config_ = send_config->Copy();
     encoder_config_ = encoder_config->Copy();
   }
 
@@ -1554,7 +1565,6 @@
 
   Action OnSendRtp(const uint8_t* packet, size_t length) override {
     rtc::CritScope lock(&crit_);
-
     if (running_without_padding_)
       EXPECT_EQ(0, call_->GetStats().max_padding_bitrate_bps);
 
@@ -1566,11 +1576,12 @@
     if (running_without_padding_) {
       // We've sent kMinPacketsToSend packets with default configuration, switch
       // to enabling screen content and setting min transmit bitrate.
+      // Note that we need to recreate the stream if changing content type.
       packets_sent_ = 0;
       encoder_config_.min_transmit_bitrate_bps = kMinTransmitBitrateBps;
       encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
-      send_stream_->ReconfigureVideoEncoder(encoder_config_.Copy());
       running_without_padding_ = false;
+      content_switch_event_.Set();
       return SEND_PACKET;
     }
 
@@ -1582,25 +1593,57 @@
   }
 
   void PerformTest() override {
+    if (RunningWithoutPadding()) {
+      ASSERT_TRUE(
+          content_switch_event_.Wait(test::CallTest::kDefaultTimeoutMs));
+      rtc::CritScope lock(&crit_);
+      RTC_DCHECK(stream_resetter_);
+      (*stream_resetter_)(send_stream_config_, encoder_config_);
+    }
+
     ASSERT_TRUE(Wait()) << "Timed out waiting for a valid padding bitrate.";
   }
 
  private:
+  bool RunningWithoutPadding() const {
+    rtc::CritScope lock(&crit_);
+    return running_without_padding_;
+  }
+
   rtc::CriticalSection crit_;
+  rtc::Event content_switch_event_;
   Call* call_;
-  VideoSendStream* send_stream_;
+  VideoSendStream* send_stream_ GUARDED_BY(crit_);
+  VideoSendStream::Config send_stream_config_;
   VideoEncoderConfig encoder_config_;
   uint32_t packets_sent_ GUARDED_BY(crit_);
   bool running_without_padding_;
+  T* const stream_resetter_;
 };
 
 TEST_F(VideoSendStreamTest, RespectsMinTransmitBitrate) {
-  MaxPaddingSetTest test(false);
+  auto reset_fun = [](const VideoSendStream::Config& send_stream_config,
+                      const VideoEncoderConfig& encoder_config) {};
+  MaxPaddingSetTest<decltype(reset_fun)> test(false, &reset_fun);
   RunBaseTest(&test);
 }
 
 TEST_F(VideoSendStreamTest, RespectsMinTransmitBitrateAfterContentSwitch) {
-  MaxPaddingSetTest test(true);
+  // Function for removing and recreating the send stream with a new config.
+  auto reset_fun = [this](const VideoSendStream::Config& send_stream_config,
+                          const VideoEncoderConfig& encoder_config) {
+    Stop();
+    sender_call_->DestroyVideoSendStream(video_send_stream_);
+    video_send_config_ = send_stream_config.Copy();
+    video_encoder_config_ = encoder_config.Copy();
+    video_send_stream_ = sender_call_->CreateVideoSendStream(
+        video_send_config_.Copy(), video_encoder_config_.Copy());
+    video_send_stream_->SetSource(
+        frame_generator_capturer_.get(),
+        VideoSendStream::DegradationPreference::kMaintainResolution);
+    Start();
+  };
+  MaxPaddingSetTest<decltype(reset_fun)> test(true, &reset_fun);
   RunBaseTest(&test);
 }
 
@@ -2070,6 +2113,7 @@
         std::vector<VideoReceiveStream::Config>* receive_configs,
         VideoEncoderConfig* encoder_config) override {
       send_config->encoder_settings.encoder = this;
+      encoder_config->max_bitrate_bps = kFirstMaxBitrateBps;
       encoder_config_ = encoder_config->Copy();
     }
 
@@ -2084,10 +2128,10 @@
                        size_t max_payload_size) override {
       if (num_initializations_ == 0) {
         // Verify default values.
-        EXPECT_EQ(kRealtimeVideo, config->mode);
+        EXPECT_EQ(kFirstMaxBitrateBps / 1000, config->maxBitrate);
       } else {
         // Verify that changed values are propagated.
-        EXPECT_EQ(kScreensharing, config->mode);
+        EXPECT_EQ(kSecondMaxBitrateBps / 1000, config->maxBitrate);
       }
       ++num_initializations_;
       init_encode_event_.Set();
@@ -2098,7 +2142,7 @@
       EXPECT_TRUE(init_encode_event_.Wait(kDefaultTimeoutMs));
       EXPECT_EQ(1u, num_initializations_) << "VideoEncoder not initialized.";
 
-      encoder_config_.content_type = VideoEncoderConfig::ContentType::kScreen;
+      encoder_config_.max_bitrate_bps = kSecondMaxBitrateBps;
       stream_->ReconfigureVideoEncoder(std::move(encoder_config_));
       EXPECT_TRUE(init_encode_event_.Wait(kDefaultTimeoutMs));
       EXPECT_EQ(2u, num_initializations_)
@@ -2106,6 +2150,9 @@
              "new encoder settings.";
     }
 
+    const uint32_t kFirstMaxBitrateBps = 1000000;
+    const uint32_t kSecondMaxBitrateBps = 2000000;
+
     rtc::Event init_encode_event_;
     size_t num_initializations_;
     VideoSendStream* stream_;