Switch to using new ACM methods for encoder management

BUG=webrtc:5028

Review URL: https://codereview.webrtc.org/1677013002

Cr-Commit-Position: refs/heads/master@{#12267}
diff --git a/webrtc/modules/audio_coding/acm2/codec_manager.h b/webrtc/modules/audio_coding/acm2/codec_manager.h
index fbd3a18..f6c6cd4 100644
--- a/webrtc/modules/audio_coding/acm2/codec_manager.h
+++ b/webrtc/modules/audio_coding/acm2/codec_manager.h
@@ -17,6 +17,7 @@
 #include "webrtc/base/optional.h"
 #include "webrtc/base/thread_checker.h"
 #include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
+#include "webrtc/modules/audio_coding/include/audio_coding_module.h"
 #include "webrtc/modules/audio_coding/include/audio_coding_module_typedefs.h"
 #include "webrtc/common_types.h"
 
@@ -55,6 +56,30 @@
 
   bool SetCodecFEC(bool enable_codec_fec);
 
+  // Uses the provided Rent-A-Codec to create a new encoder stack, if we have a
+  // complete specification; if so, it is then passed to set_encoder. On error,
+  // returns false.
+  bool MakeEncoder(RentACodec* rac, AudioCodingModule* acm) {
+    RTC_DCHECK(rac);
+    RTC_DCHECK(acm);
+    if (!codec_stack_params_.speech_encoder && send_codec_inst_) {
+      // We have no speech encoder, but we have a specification for making one.
+      auto enc = rac->RentEncoder(*send_codec_inst_);
+      if (!enc)
+        return false;
+      codec_stack_params_.speech_encoder = std::move(enc);
+    }
+    auto stack = rac->RentEncoderStack(&codec_stack_params_);
+    if (stack) {
+      // Give new encoder stack to the ACM.
+      acm->SetEncoder(std::move(stack));
+    } else {
+      // The specification was good but incomplete, so we have no encoder stack
+      // to give to the ACM.
+    }
+    return true;
+  }
+
  private:
   rtc::ThreadChecker thread_checker_;
   rtc::Optional<CodecInst> send_codec_inst_;
diff --git a/webrtc/modules/audio_coding/acm2/rent_a_codec.cc b/webrtc/modules/audio_coding/acm2/rent_a_codec.cc
index 3bf959e..7f1e520 100644
--- a/webrtc/modules/audio_coding/acm2/rent_a_codec.cc
+++ b/webrtc/modules/audio_coding/acm2/rent_a_codec.cc
@@ -263,7 +263,8 @@
 
 std::unique_ptr<AudioEncoder> RentACodec::RentEncoderStack(
     StackParameters* param) {
-  RTC_DCHECK(param->speech_encoder);
+  if (!param->speech_encoder)
+    return nullptr;
 
   if (param->use_codec_fec) {
     // Switch FEC on. On failure, remember that FEC is off.
diff --git a/webrtc/modules/audio_coding/acm2/rent_a_codec.h b/webrtc/modules/audio_coding/acm2/rent_a_codec.h
index 2c5873d..a4026ac 100644
--- a/webrtc/modules/audio_coding/acm2/rent_a_codec.h
+++ b/webrtc/modules/audio_coding/acm2/rent_a_codec.h
@@ -212,7 +212,7 @@
   // Creates and returns an audio encoder stack constructed to the given
   // specification. If the specification isn't compatible with the encoder, it
   // will be changed to match (things will be switched off). The speech encoder
-  // will be stolen.
+  // will be stolen. If the specification isn't complete, returns nullptr.
   std::unique_ptr<AudioEncoder> RentEncoderStack(StackParameters* param);
 
   // Creates and returns an iSAC decoder.
diff --git a/webrtc/modules/audio_coding/acm2/rent_a_codec_unittest.cc b/webrtc/modules/audio_coding/acm2/rent_a_codec_unittest.cc
index d0a94cf..028a271 100644
--- a/webrtc/modules/audio_coding/acm2/rent_a_codec_unittest.cc
+++ b/webrtc/modules/audio_coding/acm2/rent_a_codec_unittest.cc
@@ -222,13 +222,11 @@
   EXPECT_FALSE(rent_a_codec.RentEncoder(codec_inst));
 }
 
-#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
 TEST(RentACodecTest, RentEncoderStackWithoutSpeechEncoder) {
   RentACodec::StackParameters sp;
   EXPECT_EQ(nullptr, sp.speech_encoder);
-  EXPECT_DEATH(RentACodec().RentEncoderStack(&sp), "");
+  EXPECT_EQ(nullptr, RentACodec().RentEncoderStack(&sp));
 }
-#endif
 
 }  // namespace acm2
 }  // namespace webrtc