Use the factory instead of using the builtin code path in `VideoCodecInitializer`.

Bug: webrtc:9513
Change-Id: Ia299ae1044a3ff4c91e208200938cba540bdcea6
Reviewed-on: https://webrtc-review.googlesource.com/c/94782
Commit-Queue: Jiawei Ou <ouj@fb.com>
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Reviewed-by: Sebastian Jansson <srte@webrtc.org>
Reviewed-by: Anders Carlsson <andersc@webrtc.org>
Reviewed-by: Seth Hampson <shampson@webrtc.org>
Reviewed-by: 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@{#25456}
diff --git a/modules/video_coding/BUILD.gn b/modules/video_coding/BUILD.gn
index b92f775..246fed2 100644
--- a/modules/video_coding/BUILD.gn
+++ b/modules/video_coding/BUILD.gn
@@ -158,6 +158,7 @@
     "..:module_api_public",
     "../..:webrtc_common",
     "../../api:fec_controller_api",
+    "../../api/video:builtin_video_bitrate_allocator_factory",
     "../../api/video:encoded_frame",
     "../../api/video:video_bitrate_allocator",
     "../../api/video:video_frame",
@@ -642,6 +643,7 @@
       ":webrtc_vp9_helpers",
       "../..:webrtc_common",
       "../../api:videocodec_test_fixture_api",
+      "../../api/video:builtin_video_bitrate_allocator_factory",
       "../../api/video:video_bitrate_allocator",
       "../../api/video:video_frame",
       "../../api/video:video_frame_i420",
@@ -895,7 +897,9 @@
       "../../api:simulcast_test_fixture_api",
       "../../api:videocodec_test_fixture_api",
       "../../api/test/video:function_video_factory",
+      "../../api/video:builtin_video_bitrate_allocator_factory",
       "../../api/video:video_bitrate_allocator",
+      "../../api/video:video_bitrate_allocator_factory",
       "../../api/video:video_frame",
       "../../api/video:video_frame_i420",
       "../../api/video_codecs:create_vp8_temporal_layers",
diff --git a/modules/video_coding/codecs/test/videoprocessor.cc b/modules/video_coding/codecs/test/videoprocessor.cc
index 5e980cd..6958266 100644
--- a/modules/video_coding/codecs/test/videoprocessor.cc
+++ b/modules/video_coding/codecs/test/videoprocessor.cc
@@ -14,6 +14,7 @@
 #include <limits>
 #include <utility>
 
+#include "api/video/builtin_video_bitrate_allocator_factory.h"
 #include "api/video/i420_buffer.h"
 #include "common_types.h"  // NOLINT(build/include)
 #include "common_video/h264/h264_common.h"
@@ -173,8 +174,9 @@
       stats_(stats),
       encoder_(encoder),
       decoders_(decoders),
-      bitrate_allocator_(VideoCodecInitializer::CreateBitrateAllocator(
-          config_.codec_settings)),
+      bitrate_allocator_(
+          CreateBuiltinVideoBitrateAllocatorFactory()
+              ->CreateVideoBitrateAllocator(config_.codec_settings)),
       framerate_fps_(0),
       encode_callback_(this),
       input_frame_reader_(input_frame_reader),
diff --git a/modules/video_coding/include/video_codec_initializer.h b/modules/video_coding/include/video_codec_initializer.h
index ee70810..e979f9c 100644
--- a/modules/video_coding/include/video_codec_initializer.h
+++ b/modules/video_coding/include/video_codec_initializer.h
@@ -30,15 +30,9 @@
   // type used. For instance, VP8 will create an allocator than can handle
   // simulcast and temporal layering.
   // GetBitrateAllocator is called implicitly from here, no need to call again.
-  static bool SetupCodec(
-      const VideoEncoderConfig& config,
-      const std::vector<VideoStream>& streams,
-      VideoCodec* codec,
-      std::unique_ptr<VideoBitrateAllocator>* bitrate_allocator);
-
-  // Create a bitrate allocator for the specified codec.
-  static std::unique_ptr<VideoBitrateAllocator> CreateBitrateAllocator(
-      const VideoCodec& codec);
+  static bool SetupCodec(const VideoEncoderConfig& config,
+                         const std::vector<VideoStream>& streams,
+                         VideoCodec* codec);
 
  private:
   static VideoCodec VideoEncoderConfigToVideoCodec(
diff --git a/modules/video_coding/video_codec_initializer.cc b/modules/video_coding/video_codec_initializer.cc
index e7125ce..86a5ab2 100644
--- a/modules/video_coding/video_codec_initializer.cc
+++ b/modules/video_coding/video_codec_initializer.cc
@@ -24,15 +24,13 @@
 
 namespace webrtc {
 
-bool VideoCodecInitializer::SetupCodec(
-    const VideoEncoderConfig& config,
-    const std::vector<VideoStream>& streams,
-    VideoCodec* codec,
-    std::unique_ptr<VideoBitrateAllocator>* bitrate_allocator) {
+bool VideoCodecInitializer::SetupCodec(const VideoEncoderConfig& config,
+                                       const std::vector<VideoStream>& streams,
+                                       VideoCodec* codec) {
   if (config.codec_type == kVideoCodecMultiplex) {
     VideoEncoderConfig associated_config = config.Copy();
     associated_config.codec_type = kVideoCodecVP9;
-    if (!SetupCodec(associated_config, streams, codec, bitrate_allocator)) {
+    if (!SetupCodec(associated_config, streams, codec)) {
       RTC_LOG(LS_ERROR) << "Failed to create stereo encoder configuration.";
       return false;
     }
@@ -41,31 +39,9 @@
   }
 
   *codec = VideoEncoderConfigToVideoCodec(config, streams);
-  *bitrate_allocator = CreateBitrateAllocator(*codec);
-
   return true;
 }
 
-std::unique_ptr<VideoBitrateAllocator>
-VideoCodecInitializer::CreateBitrateAllocator(const VideoCodec& codec) {
-  std::unique_ptr<VideoBitrateAllocator> rate_allocator;
-
-  switch (codec.codecType) {
-    case kVideoCodecVP8:
-      RTC_FALLTHROUGH();
-    case kVideoCodecH264:
-      rate_allocator.reset(new SimulcastRateAllocator(codec));
-      break;
-    case kVideoCodecVP9:
-      rate_allocator.reset(new SvcRateAllocator(codec));
-      break;
-    default:
-      rate_allocator.reset(new DefaultVideoBitrateAllocator(codec));
-  }
-
-  return rate_allocator;
-}
-
 // TODO(sprang): Split this up and separate the codec specific parts.
 VideoCodec VideoCodecInitializer::VideoEncoderConfigToVideoCodec(
     const VideoEncoderConfig& config,
diff --git a/modules/video_coding/video_codec_initializer_unittest.cc b/modules/video_coding/video_codec_initializer_unittest.cc
index 335ca6b..ceff1eb 100644
--- a/modules/video_coding/video_codec_initializer_unittest.cc
+++ b/modules/video_coding/video_codec_initializer_unittest.cc
@@ -9,12 +9,13 @@
  */
 
 #include "modules/video_coding/include/video_codec_initializer.h"
-#include "api/video/video_bitrate_allocator.h"
+#include "api/video/builtin_video_bitrate_allocator_factory.h"
 #include "api/video_codecs/create_vp8_temporal_layers.h"
 #include "api/video_codecs/video_encoder.h"
 #include "api/video_codecs/vp8_temporal_layers.h"
 #include "common_types.h"  // NOLINT(build/include)
 #include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
+#include "rtc_base/checks.h"
 #include "rtc_base/refcountedobject.h"
 #include "test/gtest.h"
 
@@ -75,12 +76,13 @@
 
   bool InitializeCodec() {
     codec_out_ = VideoCodec();
-    bitrate_allocator_out_.reset();
     temporal_layers_.clear();
-    if (!VideoCodecInitializer::SetupCodec(config_, streams_, &codec_out_,
-                                           &bitrate_allocator_out_)) {
+    if (!VideoCodecInitializer::SetupCodec(config_, streams_, &codec_out_)) {
       return false;
     }
+    bitrate_allocator_ = CreateBuiltinVideoBitrateAllocatorFactory()
+                             ->CreateVideoBitrateAllocator(codec_out_);
+    RTC_CHECK(bitrate_allocator_);
     if (codec_out_.codecType == VideoCodecType::kVideoCodecMultiplex)
       return true;
 
@@ -126,7 +128,7 @@
 
   // Output.
   VideoCodec codec_out_;
-  std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_out_;
+  std::unique_ptr<VideoBitrateAllocator> bitrate_allocator_;
   std::vector<std::unique_ptr<Vp8TemporalLayers>> temporal_layers_;
 };
 
@@ -135,9 +137,8 @@
   streams_.push_back(DefaultStream());
   EXPECT_TRUE(InitializeCodec());
 
-  VideoBitrateAllocation bitrate_allocation =
-      bitrate_allocator_out_->GetAllocation(kDefaultTargetBitrateBps,
-                                            kDefaultFrameRate);
+  VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
+      kDefaultTargetBitrateBps, kDefaultFrameRate);
   EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
   EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
   EXPECT_EQ(kDefaultTargetBitrateBps, bitrate_allocation.get_sum_bps());
@@ -150,9 +151,8 @@
   streams_.push_back(inactive_stream);
   EXPECT_TRUE(InitializeCodec());
 
-  VideoBitrateAllocation bitrate_allocation =
-      bitrate_allocator_out_->GetAllocation(kDefaultTargetBitrateBps,
-                                            kDefaultFrameRate);
+  VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
+      kDefaultTargetBitrateBps, kDefaultFrameRate);
   EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
   EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
   EXPECT_EQ(0U, bitrate_allocation.get_sum_bps());
@@ -165,9 +165,8 @@
 
   EXPECT_EQ(1u, codec_out_.numberOfSimulcastStreams);
   EXPECT_EQ(2u, codec_out_.VP8()->numberOfTemporalLayers);
-  VideoBitrateAllocation bitrate_allocation =
-      bitrate_allocator_out_->GetAllocation(kScreenshareCodecTargetBitrateBps,
-                                            kScreenshareDefaultFramerate);
+  VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
+      kScreenshareCodecTargetBitrateBps, kScreenshareDefaultFramerate);
   EXPECT_EQ(kScreenshareCodecTargetBitrateBps,
             bitrate_allocation.get_sum_bps());
   EXPECT_EQ(kScreenshareTl0BitrateBps, bitrate_allocation.GetBitrate(0, 0));
@@ -185,9 +184,8 @@
   EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
   const uint32_t max_bitrate_bps =
       streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps;
-  VideoBitrateAllocation bitrate_allocation =
-      bitrate_allocator_out_->GetAllocation(max_bitrate_bps,
-                                            kScreenshareDefaultFramerate);
+  VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
+      max_bitrate_bps, kScreenshareDefaultFramerate);
   EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps());
   EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps),
             bitrate_allocation.GetSpatialLayerSum(0));
@@ -210,9 +208,8 @@
   EXPECT_EQ(1u, codec_out_.VP8()->numberOfTemporalLayers);
   const uint32_t target_bitrate =
       streams_[0].target_bitrate_bps + streams_[1].target_bitrate_bps;
-  VideoBitrateAllocation bitrate_allocation =
-      bitrate_allocator_out_->GetAllocation(target_bitrate,
-                                            kScreenshareDefaultFramerate);
+  VideoBitrateAllocation bitrate_allocation = bitrate_allocator_->GetAllocation(
+      target_bitrate, kScreenshareDefaultFramerate);
   EXPECT_EQ(static_cast<uint32_t>(streams_[0].max_bitrate_bps),
             bitrate_allocation.get_sum_bps());
   EXPECT_EQ(static_cast<uint32_t>(streams_[0].max_bitrate_bps),
@@ -235,7 +232,7 @@
   const uint32_t max_bitrate_bps =
       streams_[0].target_bitrate_bps + streams_[1].max_bitrate_bps;
   VideoBitrateAllocation bitrate_allocation =
-      bitrate_allocator_out_->GetAllocation(max_bitrate_bps, kDefaultFrameRate);
+      bitrate_allocator_->GetAllocation(max_bitrate_bps, kDefaultFrameRate);
   EXPECT_EQ(max_bitrate_bps, bitrate_allocation.get_sum_bps());
   EXPECT_EQ(static_cast<uint32_t>(streams_[0].target_bitrate_bps),
             bitrate_allocation.GetSpatialLayerSum(0));
diff --git a/modules/video_coding/video_coding_impl.cc b/modules/video_coding/video_coding_impl.cc
index a061c18..1460f6a 100644
--- a/modules/video_coding/video_coding_impl.cc
+++ b/modules/video_coding/video_coding_impl.cc
@@ -13,6 +13,7 @@
 #include <algorithm>
 #include <utility>
 
+#include "api/video/builtin_video_bitrate_allocator_factory.h"
 #include "api/video/video_bitrate_allocator.h"
 #include "common_types.h"  // NOLINT(build/include)
 #include "common_video/libyuv/include/webrtc_libyuv.h"
@@ -86,6 +87,7 @@
                         KeyFrameRequestSender* keyframe_request_sender)
       : VideoCodingModule(),
         sender_(clock, &post_encode_callback_),
+        rate_allocator_factory_(CreateBuiltinVideoBitrateAllocatorFactory()),
         timing_(new VCMTiming(clock)),
         receiver_(clock,
                   event_factory,
@@ -114,7 +116,8 @@
       // asynchronously keep the instance alive until destruction or until a
       // new send codec is registered.
       VideoCodec codec = *sendCodec;
-      rate_allocator_ = VideoCodecInitializer::CreateBitrateAllocator(codec);
+      rate_allocator_ =
+          rate_allocator_factory_->CreateVideoBitrateAllocator(codec);
       return sender_.RegisterSendCodec(&codec, numberOfCores, maxPayloadSize);
     }
     return sender_.RegisterSendCodec(sendCodec, numberOfCores, maxPayloadSize);
@@ -213,8 +216,9 @@
   rtc::ThreadChecker construction_thread_;
   EncodedImageCallbackWrapper post_encode_callback_;
   vcm::VideoSender sender_;
+  const std::unique_ptr<VideoBitrateAllocatorFactory> rate_allocator_factory_;
   std::unique_ptr<VideoBitrateAllocator> rate_allocator_;
-  std::unique_ptr<VCMTiming> timing_;
+  const std::unique_ptr<VCMTiming> timing_;
   vcm::VideoReceiver receiver_;
 };
 }  // namespace