Move reencode logic for screenshare bitrate overshoot from generic
encoder to vp8impl.
BUG=none

Review-Url: https://codereview.webrtc.org/3011213002
Cr-Original-Commit-Position: refs/heads/master@{#19803}
Cr-Mirrored-From: https://chromium.googlesource.com/external/webrtc
Cr-Mirrored-Commit: e99738174344a7d172d9391f3bd9f485b9e64d67
diff --git a/modules/video_coding/codecs/vp8/vp8_impl.cc b/modules/video_coding/codecs/vp8/vp8_impl.cc
index c84049f..8d10594 100644
--- a/modules/video_coding/codecs/vp8/vp8_impl.cc
+++ b/modules/video_coding/codecs/vp8/vp8_impl.cc
@@ -827,20 +827,31 @@
   assert(codec_.maxFramerate > 0);
   uint32_t duration = 90000 / codec_.maxFramerate;
 
-  // Note we must pass 0 for |flags| field in encode call below since they are
-  // set above in |vpx_codec_control| function for each encoder/spatial layer.
-  int error = vpx_codec_encode(&encoders_[0], &raw_images_[0], timestamp_,
-                               duration, 0, VPX_DL_REALTIME);
-  // Reset specific intra frame thresholds, following the key frame.
-  if (send_key_frame) {
-    vpx_codec_control(&(encoders_[0]), VP8E_SET_MAX_INTRA_BITRATE_PCT,
-                      rc_max_intra_target_);
+  int error = WEBRTC_VIDEO_CODEC_OK;
+  int num_tries = 0;
+  // If the first try returns WEBRTC_VIDEO_CODEC_TARGET_BITRATE_OVERSHOOT
+  // the frame must be reencoded with the same parameters again because
+  // target bitrate is exceeded and encoder state has been reset.
+  while (num_tries == 0 ||
+      (num_tries == 1 &&
+          error == WEBRTC_VIDEO_CODEC_TARGET_BITRATE_OVERSHOOT)) {
+    ++num_tries;
+    // Note we must pass 0 for |flags| field in encode call below since they are
+    // set above in |vpx_codec_control| function for each encoder/spatial layer.
+    error = vpx_codec_encode(&encoders_[0], &raw_images_[0], timestamp_,
+                                 duration, 0, VPX_DL_REALTIME);
+    // Reset specific intra frame thresholds, following the key frame.
+    if (send_key_frame) {
+      vpx_codec_control(&(encoders_[0]), VP8E_SET_MAX_INTRA_BITRATE_PCT,
+                        rc_max_intra_target_);
+    }
+    if (error)
+      return WEBRTC_VIDEO_CODEC_ERROR;
+    timestamp_ += duration;
+    // Examines frame timestamps only.
+    error = GetEncodedPartitions(tl_configs, frame);
   }
-  if (error)
-    return WEBRTC_VIDEO_CODEC_ERROR;
-  timestamp_ += duration;
-  // Examines frame timestamps only.
-  return GetEncodedPartitions(tl_configs, frame);
+  return error;
 }
 
 void VP8EncoderImpl::PopulateCodecSpecific(
diff --git a/modules/video_coding/generic_encoder.cc b/modules/video_coding/generic_encoder.cc
index 339e1b5..142c885 100644
--- a/modules/video_coding/generic_encoder.cc
+++ b/modules/video_coding/generic_encoder.cc
@@ -33,7 +33,6 @@
       vcm_encoded_frame_callback_(encoded_frame_callback),
       internal_source_(internal_source),
       encoder_params_({BitrateAllocation(), 0, 0, 0}),
-      is_screenshare_(false),
       streams_or_svc_num_(0) {}
 
 VCMGenericEncoder::~VCMGenericEncoder() {}
@@ -49,7 +48,6 @@
                                       size_t max_payload_size) {
   RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
   TRACE_EVENT0("webrtc", "VCMGenericEncoder::InitEncode");
-  is_screenshare_ = settings->mode == VideoCodecMode::kScreensharing;
   streams_or_svc_num_ = settings->numberOfSimulcastStreams;
   if (settings->codecType == kVideoCodecVP9) {
     streams_or_svc_num_ = settings->VP9().numberOfSpatialLayers;
@@ -83,15 +81,8 @@
 
   for (size_t i = 0; i < streams_or_svc_num_; ++i)
     vcm_encoded_frame_callback_->OnEncodeStarted(frame.render_time_ms(), i);
-  int32_t result = encoder_->Encode(frame, codec_specific, &frame_types);
 
-  if (is_screenshare_ &&
-      result == WEBRTC_VIDEO_CODEC_TARGET_BITRATE_OVERSHOOT) {
-    // Target bitrate exceeded, encoder state has been reset - try again.
-    return encoder_->Encode(frame, codec_specific, &frame_types);
-  }
-
-  return result;
+  return encoder_->Encode(frame, codec_specific, &frame_types);
 }
 
 void VCMGenericEncoder::SetEncoderParameters(const EncoderParameters& params) {
diff --git a/modules/video_coding/generic_encoder.h b/modules/video_coding/generic_encoder.h
index 32db2e7..050d60d 100644
--- a/modules/video_coding/generic_encoder.h
+++ b/modules/video_coding/generic_encoder.h
@@ -122,7 +122,6 @@
   const bool internal_source_;
   rtc::CriticalSection params_lock_;
   EncoderParameters encoder_params_ RTC_GUARDED_BY(params_lock_);
-  bool is_screenshare_;
   size_t streams_or_svc_num_;
 };