Report available instead of encoding bitrate to VideoEncoderSelector.

The encoding bitrate might be limited depending on the current encoder.

Bug: webrtc:11341
Change-Id: I734fce12734b1e703e7948847cdb1365c08a137b
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/169123
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Reviewed-by: Åsa Persson <asapersson@webrtc.org>
Reviewed-by: Rasmus Brandt <brandtr@webrtc.org>
Commit-Queue: Mirta Dvornicic <mirtad@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30619}
diff --git a/api/video_codecs/video_encoder_factory.h b/api/video_codecs/video_encoder_factory.h
index a84a297..630b7aa 100644
--- a/api/video_codecs/video_encoder_factory.h
+++ b/api/video_codecs/video_encoder_factory.h
@@ -49,9 +49,9 @@
     // used.
     virtual void OnCurrentEncoder(const SdpVideoFormat& format) = 0;
 
-    // Called every time the encoding bitrate is updated. Should return a
+    // Called every time the available bitrate is updated. Should return a
     // non-empty if an encoder switch should be performed.
-    virtual absl::optional<SdpVideoFormat> OnEncodingBitrate(
+    virtual absl::optional<SdpVideoFormat> OnAvailableBitrate(
         const DataRate& rate) = 0;
 
     // Called if the currently used encoder reports itself as broken. Should
diff --git a/sdk/android/api/org/webrtc/VideoEncoderFactory.java b/sdk/android/api/org/webrtc/VideoEncoderFactory.java
index 9c0f2b4..3c59869 100644
--- a/sdk/android/api/org/webrtc/VideoEncoderFactory.java
+++ b/sdk/android/api/org/webrtc/VideoEncoderFactory.java
@@ -19,16 +19,32 @@
     @CalledByNative("VideoEncoderSelector") void onCurrentEncoder(VideoCodecInfo info);
 
     /**
-     * Called with the current encoding bitrate. Returns null if the encoder
-     * selector which to keep the current encoder or a VideoCodecInfo if a
-     * new encoder is preferred.
+     * Called with the current encoding bitrate. Returns null if the encoder selector prefers to
+     * keep the current encoder or a VideoCodecInfo if a new encoder is preferred.
+     *
+     * <p>TODO(bugs.webrtc.org/11341): Delete onEncodingBitrate and remove the default
+     * implementation for onAvailableBitrate once downstream project is updated.
      */
-    @Nullable @CalledByNative("VideoEncoderSelector") VideoCodecInfo onEncodingBitrate(int kbps);
+    @Deprecated
+    @Nullable
+    default VideoCodecInfo onEncodingBitrate(int kbps) {
+      throw new UnsupportedOperationException("Not implemented.");
+    }
 
     /**
-     * Called when the currently used encoder signal itself as broken. Returns
-     * null if the encoder selector which to keep the current encoder or a
-     * VideoCodecInfo if a new encoder is preferred.
+     * Called with the current available bitrate. Returns null if the encoder selector prefers to
+     * keep the current encoder or a VideoCodecInfo if a new encoder is preferred.
+     */
+    @Nullable
+    @CalledByNative("VideoEncoderSelector")
+    default VideoCodecInfo onAvailableBitrate(int kbps) {
+      return onEncodingBitrate(kbps);
+    }
+
+    /**
+     * Called when the currently used encoder signal itself as broken. Returns null if the encoder
+     * selector prefers to keep the current encoder or a VideoCodecInfo if a new encoder is
+     * preferred.
      */
     @Nullable @CalledByNative("VideoEncoderSelector") VideoCodecInfo onEncoderBroken();
   }
diff --git a/sdk/android/src/jni/video_encoder_factory_wrapper.cc b/sdk/android/src/jni/video_encoder_factory_wrapper.cc
index 795f82b..d6a6cfa 100644
--- a/sdk/android/src/jni/video_encoder_factory_wrapper.cc
+++ b/sdk/android/src/jni/video_encoder_factory_wrapper.cc
@@ -36,12 +36,12 @@
                                                j_codec_info);
   }
 
-  absl::optional<SdpVideoFormat> OnEncodingBitrate(
+  absl::optional<SdpVideoFormat> OnAvailableBitrate(
       const DataRate& rate) override {
     JNIEnv* jni = AttachCurrentThreadIfNeeded();
     ScopedJavaLocalRef<jobject> codec_info =
-        Java_VideoEncoderSelector_onEncodingBitrate(jni, encoder_selector_,
-                                                    rate.kbps<int>());
+        Java_VideoEncoderSelector_onAvailableBitrate(jni, encoder_selector_,
+                                                     rate.kbps<int>());
     if (codec_info.is_null()) {
       return absl::nullopt;
     }
diff --git a/test/video_encoder_proxy_factory.h b/test/video_encoder_proxy_factory.h
index 46caf8d..70e2c8a 100644
--- a/test/video_encoder_proxy_factory.h
+++ b/test/video_encoder_proxy_factory.h
@@ -140,9 +140,9 @@
       encoder_selector_->OnCurrentEncoder(format);
     }
 
-    absl::optional<SdpVideoFormat> OnEncodingBitrate(
+    absl::optional<SdpVideoFormat> OnAvailableBitrate(
         const DataRate& rate) override {
-      return encoder_selector_->OnEncodingBitrate(rate);
+      return encoder_selector_->OnAvailableBitrate(rate);
     }
 
     absl::optional<SdpVideoFormat> OnEncoderBroken() override {
diff --git a/video/video_stream_encoder.cc b/video/video_stream_encoder.cc
index eae5c0f..b99e911 100644
--- a/video/video_stream_encoder.cc
+++ b/video/video_stream_encoder.cc
@@ -1565,7 +1565,8 @@
 
   if (!video_is_suspended && settings_.encoder_switch_request_callback) {
     if (encoder_selector_) {
-      if (auto encoder = encoder_selector_->OnEncodingBitrate(target_bitrate)) {
+      if (auto encoder =
+              encoder_selector_->OnAvailableBitrate(link_allocation)) {
         settings_.encoder_switch_request_callback->RequestEncoderSwitch(
             *encoder);
       }
diff --git a/video/video_stream_encoder_unittest.cc b/video/video_stream_encoder_unittest.cc
index a8e51e8..485543a 100644
--- a/video/video_stream_encoder_unittest.cc
+++ b/video/video_stream_encoder_unittest.cc
@@ -430,7 +430,7 @@
     : public VideoEncoderFactory::EncoderSelectorInterface {
  public:
   MOCK_METHOD1(OnCurrentEncoder, void(const SdpVideoFormat& format));
-  MOCK_METHOD1(OnEncodingBitrate,
+  MOCK_METHOD1(OnAvailableBitrate,
                absl::optional<SdpVideoFormat>(const DataRate& rate));
   MOCK_METHOD0(OnEncoderBroken, absl::optional<SdpVideoFormat>());
 };
@@ -5414,7 +5414,7 @@
   // Reset encoder for new configuration to take effect.
   ConfigureEncoder(video_encoder_config_.Copy());
 
-  ON_CALL(encoder_selector, OnEncodingBitrate(_))
+  ON_CALL(encoder_selector, OnAvailableBitrate(_))
       .WillByDefault(Return(SdpVideoFormat("AV1")));
   EXPECT_CALL(switch_callback,
               RequestEncoderSwitch(Matcher<const SdpVideoFormat&>(