VAD/DTX tests: Don't let the ACM create audio encoders

It will soon lose the ability to do so.

Bug: webrtc:8396
Change-Id: I06dce417bba855b57130bd1a052988b2f235dcbd
Reviewed-on: https://webrtc-review.googlesource.com/102882
Commit-Queue: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Ivo Creusen <ivoc@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24921}
diff --git a/modules/audio_coding/BUILD.gn b/modules/audio_coding/BUILD.gn
index 14f45b1..78d9c6e 100644
--- a/modules/audio_coding/BUILD.gn
+++ b/modules/audio_coding/BUILD.gn
@@ -1325,6 +1325,7 @@
       ":audio_coding",
       ":audio_coding_module_typedefs",
       ":audio_format_conversion",
+      ":cng",
       ":pcm16b_c",
       "..:module_api",
       "../..:webrtc_common",
@@ -1332,7 +1333,13 @@
       "../../api/audio_codecs:audio_codecs_api",
       "../../api/audio_codecs:builtin_audio_decoder_factory",
       "../../api/audio_codecs:builtin_audio_encoder_factory",
+      "../../api/audio_codecs/ilbc:audio_decoder_ilbc",
+      "../../api/audio_codecs/ilbc:audio_encoder_ilbc",
+      "../../api/audio_codecs/isac:audio_decoder_isac_float",
       "../../api/audio_codecs/isac:audio_encoder_isac_float",
+      "../../api/audio_codecs/opus:audio_decoder_opus",
+      "../../api/audio_codecs/opus:audio_encoder_opus",
+      "../../common_audio",
       "../../rtc_base:checks",
       "../../rtc_base:rtc_base_approved",
       "../../rtc_base/synchronization:rw_lock_wrapper",
diff --git a/modules/audio_coding/test/TestVADDTX.cc b/modules/audio_coding/test/TestVADDTX.cc
index eefce0d..4f02eda 100644
--- a/modules/audio_coding/test/TestVADDTX.cc
+++ b/modules/audio_coding/test/TestVADDTX.cc
@@ -12,8 +12,15 @@
 
 #include <string>
 
-#include "api/audio_codecs/builtin_audio_decoder_factory.h"
-#include "modules/audio_coding/codecs/audio_format_conversion.h"
+#include "api/audio_codecs/audio_decoder_factory_template.h"
+#include "api/audio_codecs/audio_encoder_factory_template.h"
+#include "api/audio_codecs/ilbc/audio_decoder_ilbc.h"
+#include "api/audio_codecs/ilbc/audio_encoder_ilbc.h"
+#include "api/audio_codecs/isac/audio_decoder_isac_float.h"
+#include "api/audio_codecs/isac/audio_encoder_isac_float.h"
+#include "api/audio_codecs/opus/audio_decoder_opus.h"
+#include "api/audio_codecs/opus/audio_encoder_opus.h"
+#include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
 #include "modules/audio_coding/test/PCMFile.h"
 #include "modules/audio_coding/test/utility.h"
 #include "rtc_base/strings/string_builder.h"
@@ -21,20 +28,6 @@
 
 namespace webrtc {
 
-#ifdef WEBRTC_CODEC_ISAC
-const CodecInst kIsacWb = {103, "ISAC", 16000, 480, 1, 32000};
-const CodecInst kIsacSwb = {104, "ISAC", 32000, 960, 1, 56000};
-#endif
-
-#ifdef WEBRTC_CODEC_ILBC
-const CodecInst kIlbc = {102, "ILBC", 8000, 240, 1, 13300};
-#endif
-
-#ifdef WEBRTC_CODEC_OPUS
-const CodecInst kOpus = {120, "opus", 48000, 960, 1, 64000};
-const CodecInst kOpusStereo = {120, "opus", 48000, 960, 2, 64000};
-#endif
-
 ActivityMonitor::ActivityMonitor() {
   ResetStatistics();
 }
@@ -63,10 +56,16 @@
 }
 
 TestVadDtx::TestVadDtx()
-    : acm_send_(AudioCodingModule::Create(
-          AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
+    : encoder_factory_(CreateAudioEncoderFactory<AudioEncoderIlbc,
+                                                 AudioEncoderIsacFloat,
+                                                 AudioEncoderOpus>()),
+      decoder_factory_(CreateAudioDecoderFactory<AudioDecoderIlbc,
+                                                 AudioDecoderIsacFloat,
+                                                 AudioDecoderOpus>()),
+      acm_send_(AudioCodingModule::Create(
+          AudioCodingModule::Config(decoder_factory_))),
       acm_receive_(AudioCodingModule::Create(
-          AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
+          AudioCodingModule::Config(decoder_factory_))),
       channel_(new Channel),
       monitor_(new ActivityMonitor) {
   EXPECT_EQ(0, acm_send_->RegisterTransportCallback(channel_.get()));
@@ -74,12 +73,29 @@
   EXPECT_EQ(0, acm_send_->RegisterVADCallback(monitor_.get()));
 }
 
-void TestVadDtx::RegisterCodec(CodecInst codec_param) {
-  // Set the codec for sending and receiving.
-  EXPECT_EQ(0, acm_send_->RegisterSendCodec(codec_param));
-  EXPECT_EQ(true, acm_receive_->RegisterReceiveCodec(
-                      codec_param.pltype, CodecInstToSdp(codec_param)));
-  channel_->SetIsStereo(codec_param.channels > 1);
+bool TestVadDtx::RegisterCodec(const SdpAudioFormat& codec_format,
+                               absl::optional<Vad::Aggressiveness> vad_mode) {
+  constexpr int payload_type = 17, cn_payload_type = 117;
+  bool added_comfort_noise = false;
+
+  auto encoder = encoder_factory_->MakeAudioEncoder(payload_type, codec_format,
+                                                    absl::nullopt);
+  if (vad_mode.has_value() &&
+      STR_CASE_CMP(codec_format.name.c_str(), "opus") != 0) {
+    AudioEncoderCng::Config config;
+    config.speech_encoder = std::move(encoder);
+    config.num_channels = 1;
+    config.payload_type = cn_payload_type;
+    config.vad_mode = vad_mode.value();
+    encoder = absl::make_unique<AudioEncoderCng>(std::move(config));
+    added_comfort_noise = true;
+  }
+  channel_->SetIsStereo(encoder->NumChannels() > 1);
+  acm_send_->SetEncoder(std::move(encoder));
+
+  EXPECT_EQ(true,
+            acm_receive_->RegisterReceiveCodec(payload_type, codec_format));
+  return added_comfort_noise;
 }
 
 // Encoding a file and see if the numbers that various packets occur follow
@@ -148,60 +164,36 @@
 }
 
 // Following is the implementation of TestWebRtcVadDtx.
-TestWebRtcVadDtx::TestWebRtcVadDtx()
-    : vad_enabled_(false), dtx_enabled_(false), output_file_num_(0) {}
+TestWebRtcVadDtx::TestWebRtcVadDtx() : output_file_num_(0) {}
 
 void TestWebRtcVadDtx::Perform() {
-// Go through various test cases.
-#ifdef WEBRTC_CODEC_ISAC
-  // Register iSAC WB as send codec
-  RegisterCodec(kIsacWb);
-  RunTestCases();
-
-  // Register iSAC SWB as send codec
-  RegisterCodec(kIsacSwb);
-  RunTestCases();
-#endif
-
-#ifdef WEBRTC_CODEC_ILBC
-  // Register iLBC as send codec
-  RegisterCodec(kIlbc);
-  RunTestCases();
-#endif
-
-#ifdef WEBRTC_CODEC_OPUS
-  // Register Opus as send codec
-  RegisterCodec(kOpus);
-  RunTestCases();
-#endif
+  RunTestCases({"ISAC", 16000, 1});
+  RunTestCases({"ISAC", 32000, 1});
+  RunTestCases({"ILBC", 8000, 1});
+  RunTestCases({"opus", 48000, 2});
 }
 
 // Test various configurations on VAD/DTX.
-void TestWebRtcVadDtx::RunTestCases() {
-  // #1 DTX = OFF, VAD = OFF, VADNormal
-  SetVAD(false, false, VADNormal);
-  Test(true);
+void TestWebRtcVadDtx::RunTestCases(const SdpAudioFormat& codec_format) {
+  Test(/*new_outfile=*/true,
+       /*expect_dtx_enabled=*/RegisterCodec(codec_format, absl::nullopt));
 
-  // #2 DTX = ON, VAD = ON, VADAggr
-  SetVAD(true, true, VADAggr);
-  Test(false);
+  Test(/*new_outfile=*/false,
+       /*expect_dtx_enabled=*/RegisterCodec(codec_format, Vad::kVadAggressive));
 
-  // #3 DTX = ON, VAD = ON, VADLowBitrate
-  SetVAD(true, true, VADLowBitrate);
-  Test(false);
+  Test(/*new_outfile=*/false,
+       /*expect_dtx_enabled=*/RegisterCodec(codec_format, Vad::kVadLowBitrate));
 
-  // #4 DTX = ON, VAD = ON, VADVeryAggr
-  SetVAD(true, true, VADVeryAggr);
-  Test(false);
+  Test(/*new_outfile=*/false, /*expect_dtx_enabled=*/RegisterCodec(
+           codec_format, Vad::kVadVeryAggressive));
 
-  // #5 DTX = ON, VAD = ON, VADNormal
-  SetVAD(true, true, VADNormal);
-  Test(false);
+  Test(/*new_outfile=*/false,
+       /*expect_dtx_enabled=*/RegisterCodec(codec_format, Vad::kVadNormal));
 }
 
 // Set the expectation and run the test.
-void TestWebRtcVadDtx::Test(bool new_outfile) {
-  int expects[] = {-1, 1, dtx_enabled_, 0, 0};
+void TestWebRtcVadDtx::Test(bool new_outfile, bool expect_dtx_enabled) {
+  int expects[] = {-1, 1, expect_dtx_enabled, 0, 0};
   if (new_outfile) {
     output_file_num_++;
   }
@@ -212,46 +204,19 @@
       out_filename.str(), !new_outfile, expects);
 }
 
-void TestWebRtcVadDtx::SetVAD(bool enable_dtx,
-                              bool enable_vad,
-                              ACMVADMode vad_mode) {
-  ACMVADMode mode;
-  EXPECT_EQ(0, acm_send_->SetVAD(enable_dtx, enable_vad, vad_mode));
-  EXPECT_EQ(0, acm_send_->VAD(&dtx_enabled_, &vad_enabled_, &mode));
-
-  auto codec_param = acm_send_->SendCodec();
-  ASSERT_TRUE(codec_param);
-  if (STR_CASE_CMP(codec_param->plname, "opus") == 0) {
-    // If send codec is Opus, WebRTC VAD/DTX cannot be used.
-    enable_dtx = enable_vad = false;
-  }
-
-  EXPECT_EQ(dtx_enabled_, enable_dtx);  // DTX should be set as expected.
-
-  if (dtx_enabled_) {
-    EXPECT_TRUE(vad_enabled_);  // WebRTC DTX cannot run without WebRTC VAD.
-  } else {
-    // Using no DTX should not affect setting of VAD.
-    EXPECT_EQ(enable_vad, vad_enabled_);
-  }
-}
-
 // Following is the implementation of TestOpusDtx.
 void TestOpusDtx::Perform() {
-#ifdef WEBRTC_CODEC_ISAC
   // If we set other codec than Opus, DTX cannot be switched on.
-  RegisterCodec(kIsacWb);
+  RegisterCodec({"ISAC", 16000, 1}, absl::nullopt);
   EXPECT_EQ(-1, acm_send_->EnableOpusDtx());
   EXPECT_EQ(0, acm_send_->DisableOpusDtx());
-#endif
 
-#ifdef WEBRTC_CODEC_OPUS
   int expects[] = {0, 1, 0, 0, 0};
 
   // Register Opus as send codec
   std::string out_filename =
       webrtc::test::OutputPath() + "testOpusDtx_outFile_mono.pcm";
-  RegisterCodec(kOpus);
+  RegisterCodec({"opus", 48000, 2}, absl::nullopt);
   EXPECT_EQ(0, acm_send_->DisableOpusDtx());
 
   Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 32000, 1,
@@ -265,7 +230,7 @@
 
   // Register stereo Opus as send codec
   out_filename = webrtc::test::OutputPath() + "testOpusDtx_outFile_stereo.pcm";
-  RegisterCodec(kOpusStereo);
+  RegisterCodec({"opus", 48000, 2, {{"stereo", "1"}}}, absl::nullopt);
   EXPECT_EQ(0, acm_send_->DisableOpusDtx());
   expects[kEmptyFrame] = 0;
   expects[kAudioFrameCN] = 0;
@@ -278,7 +243,6 @@
   expects[kAudioFrameCN] = 1;
   Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"), 32000,
       2, out_filename, true, expects);
-#endif
 }
 
 }  // namespace webrtc
diff --git a/modules/audio_coding/test/TestVADDTX.h b/modules/audio_coding/test/TestVADDTX.h
index 26a695c..68b2c1e 100644
--- a/modules/audio_coding/test/TestVADDTX.h
+++ b/modules/audio_coding/test/TestVADDTX.h
@@ -13,6 +13,9 @@
 
 #include <memory>
 
+#include "api/audio_codecs/audio_decoder_factory.h"
+#include "api/audio_codecs/audio_encoder_factory.h"
+#include "common_audio/vad/include/vad.h"
 #include "common_types.h"  // NOLINT(build/include)
 #include "modules/audio_coding/include/audio_coding_module.h"
 #include "modules/audio_coding/include/audio_coding_module_typedefs.h"
@@ -48,7 +51,9 @@
   TestVadDtx();
 
  protected:
-  void RegisterCodec(CodecInst codec_param);
+  // Returns true iff CN was added.
+  bool RegisterCodec(const SdpAudioFormat& codec_format,
+                     absl::optional<Vad::Aggressiveness> vad_mode);
 
   // Encoding a file and see if the numbers that various packets occur follow
   // the expectation. Saves result to a file.
@@ -69,6 +74,8 @@
            bool append,
            const int* expects);
 
+  const rtc::scoped_refptr<AudioEncoderFactory> encoder_factory_;
+  const rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
   std::unique_ptr<AudioCodingModule> acm_send_;
   std::unique_ptr<AudioCodingModule> acm_receive_;
   std::unique_ptr<Channel> channel_;
@@ -84,12 +91,9 @@
   void Perform();
 
  private:
-  void RunTestCases();
-  void Test(bool new_outfile);
-  void SetVAD(bool enable_dtx, bool enable_vad, ACMVADMode vad_mode);
+  void RunTestCases(const SdpAudioFormat& codec_format);
+  void Test(bool new_outfile, bool expect_dtx_enabled);
 
-  bool vad_enabled_;
-  bool dtx_enabled_;
   int output_file_num_;
 };
 
diff --git a/modules/audio_coding/test/Tester.cc b/modules/audio_coding/test/Tester.cc
index 4b15fcb..1ec3887 100644
--- a/modules/audio_coding/test/Tester.cc
+++ b/modules/audio_coding/test/Tester.cc
@@ -79,12 +79,7 @@
   webrtc::TestStereo(ACM_TEST_MODE).Perform();
 }
 
-// Disabled on ios as flaky, see https://crbug.com/webrtc/7057
-#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
-TEST(AudioCodingModuleTest, DISABLED_TestWebRtcVadDtx) {
-#else
 TEST(AudioCodingModuleTest, TestWebRtcVadDtx) {
-#endif
   webrtc::TestWebRtcVadDtx().Perform();
 }