Revert "Remove CodecInst pt.1"

This reverts commit 056f9738bf7a3d16da45398239656e165c4e0851.

Reason for revert: breaks downstream

Original change's description:
> Remove CodecInst pt.1
> 
> Update audio_coding tests to not use CodecInst.
> 
> Bug: webrtc:7626
> Change-Id: I880fb8d72d7d0a915d274e67feb6106f023697c2
> Reviewed-on: https://webrtc-review.googlesource.com/c/112594
> Commit-Queue: Fredrik Solenberg <solenberg@webrtc.org>
> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#25879}

TBR=solenberg@webrtc.org,kwiberg@webrtc.org

Change-Id: I51d666969bcd63e2b7cb7d669ec2f59b5f8f9dde
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: webrtc:7626
Reviewed-on: https://webrtc-review.googlesource.com/c/112906
Reviewed-by: Fredrik Solenberg <solenberg@webrtc.org>
Commit-Queue: Fredrik Solenberg <solenberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25881}
diff --git a/modules/audio_coding/test/TestAllCodecs.cc b/modules/audio_coding/test/TestAllCodecs.cc
index aad80e8..0099b2a 100644
--- a/modules/audio_coding/test/TestAllCodecs.cc
+++ b/modules/audio_coding/test/TestAllCodecs.cc
@@ -14,11 +14,13 @@
 #include <limits>
 #include <string>
 
-#include "absl/strings/match.h"
 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
+#include "common_types.h"  // NOLINT(build/include)
+#include "modules/audio_coding/codecs/audio_format_conversion.h"
+#include "modules/audio_coding/include/audio_coding_module.h"
 #include "modules/audio_coding/include/audio_coding_module_typedefs.h"
-#include "modules/include/module_common_types.h"
+#include "modules/audio_coding/test/utility.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/stringencode.h"
 #include "rtc_base/strings/string_builder.h"
@@ -33,11 +35,6 @@
 // The test loops through all available mono codecs, encode at "a" sends over
 // the channel, and decodes at "b".
 
-#define CHECK_ERROR(f)                      \
-  do {                                      \
-    EXPECT_GE(f, 0) << "Error Calling API"; \
-  } while (0)
-
 namespace {
 const size_t kVariableSize = std::numeric_limits<size_t>::max();
 }
@@ -104,7 +101,7 @@
   payload_size_ = 0;
 }
 
-TestAllCodecs::TestAllCodecs()
+TestAllCodecs::TestAllCodecs(int test_mode)
     : acm_a_(AudioCodingModule::Create(
           AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
       acm_b_(AudioCodingModule::Create(
@@ -113,6 +110,8 @@
       test_count_(0),
       packet_size_samples_(0),
       packet_size_bytes_(0) {
+  // test_mode = 0 for silent test (auto test)
+  test_mode_ = test_mode;
 }
 
 TestAllCodecs::~TestAllCodecs() {
@@ -127,28 +126,23 @@
       webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
   infile_a_.Open(file_name, 32000, "rb");
 
+  if (test_mode_ == 0) {
+    RTC_LOG(LS_INFO) << "---------- TestAllCodecs ----------";
+  }
+
   acm_a_->InitializeReceiver();
   acm_b_->InitializeReceiver();
 
-  acm_b_->SetReceiveCodecs({{103, {"ISAC", 16000, 1}},
-                            {104, {"ISAC", 32000, 1}},
-                            {107, {"L16", 8000, 1}},
-                            {108, {"L16", 16000, 1}},
-                            {109, {"L16", 32000, 1}},
-                            {111, {"L16", 8000, 2}},
-                            {112, {"L16", 16000, 2}},
-                            {113, {"L16", 32000, 2}},
-                            {0, {"PCMU", 8000, 1}},
-                            {110, {"PCMU", 8000, 2}},
-                            {8, {"PCMA", 8000, 1}},
-                            {118, {"PCMA", 8000, 2}},
-                            {102, {"ILBC", 8000, 1}},
-                            {9, {"G722", 8000, 1}},
-                            {119, {"G722", 8000, 2}},
-                            {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}},
-                            {13, {"CN", 8000, 1}},
-                            {98, {"CN", 16000, 1}},
-                            {99, {"CN", 32000, 1}}});
+  uint8_t num_encoders = acm_a_->NumberOfCodecs();
+  CodecInst my_codec_param;
+  for (uint8_t n = 0; n < num_encoders; n++) {
+    acm_b_->Codec(n, &my_codec_param);
+    if (!strcmp(my_codec_param.plname, "opus")) {
+      my_codec_param.channels = 1;
+    }
+    acm_b_->RegisterReceiveCodec(my_codec_param.pltype,
+                                 CodecInstToSdp(my_codec_param));
+  }
 
   // Create and connect the channel
   channel_a_to_b_ = new TestPack;
@@ -157,6 +151,9 @@
 
   // All codecs are tested for all allowed sampling frequencies, rates and
   // packet sizes.
+  if (test_mode_ != 0) {
+    printf("===============================================================\n");
+  }
   test_count_++;
   OpenOutFile(test_count_);
   char codec_g722[] = "G722";
@@ -174,6 +171,9 @@
   Run(channel_a_to_b_);
   outfile_b_.Close();
 #ifdef WEBRTC_CODEC_ILBC
+  if (test_mode_ != 0) {
+    printf("===============================================================\n");
+  }
   test_count_++;
   OpenOutFile(test_count_);
   char codec_ilbc[] = "ILBC";
@@ -188,6 +188,9 @@
   outfile_b_.Close();
 #endif
 #if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
+  if (test_mode_ != 0) {
+    printf("===============================================================\n");
+  }
   test_count_++;
   OpenOutFile(test_count_);
   char codec_isac[] = "ISAC";
@@ -202,6 +205,9 @@
   outfile_b_.Close();
 #endif
 #ifdef WEBRTC_CODEC_ISAC
+  if (test_mode_ != 0) {
+    printf("===============================================================\n");
+  }
   test_count_++;
   OpenOutFile(test_count_);
   RegisterSendCodec('A', codec_isac, 32000, -1, 960, kVariableSize);
@@ -214,6 +220,9 @@
   Run(channel_a_to_b_);
   outfile_b_.Close();
 #endif
+  if (test_mode_ != 0) {
+    printf("===============================================================\n");
+  }
   test_count_++;
   OpenOutFile(test_count_);
   char codec_l16[] = "L16";
@@ -226,7 +235,9 @@
   RegisterSendCodec('A', codec_l16, 8000, 128000, 320, 0);
   Run(channel_a_to_b_);
   outfile_b_.Close();
-
+  if (test_mode_ != 0) {
+    printf("===============================================================\n");
+  }
   test_count_++;
   OpenOutFile(test_count_);
   RegisterSendCodec('A', codec_l16, 16000, 256000, 160, 0);
@@ -238,7 +249,9 @@
   RegisterSendCodec('A', codec_l16, 16000, 256000, 640, 0);
   Run(channel_a_to_b_);
   outfile_b_.Close();
-
+  if (test_mode_ != 0) {
+    printf("===============================================================\n");
+  }
   test_count_++;
   OpenOutFile(test_count_);
   RegisterSendCodec('A', codec_l16, 32000, 512000, 320, 0);
@@ -246,7 +259,9 @@
   RegisterSendCodec('A', codec_l16, 32000, 512000, 640, 0);
   Run(channel_a_to_b_);
   outfile_b_.Close();
-
+  if (test_mode_ != 0) {
+    printf("===============================================================\n");
+  }
   test_count_++;
   OpenOutFile(test_count_);
   char codec_pcma[] = "PCMA";
@@ -262,7 +277,9 @@
   Run(channel_a_to_b_);
   RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, 0);
   Run(channel_a_to_b_);
-
+  if (test_mode_ != 0) {
+    printf("===============================================================\n");
+  }
   char codec_pcmu[] = "PCMU";
   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, 0);
   Run(channel_a_to_b_);
@@ -278,6 +295,9 @@
   Run(channel_a_to_b_);
   outfile_b_.Close();
 #ifdef WEBRTC_CODEC_OPUS
+  if (test_mode_ != 0) {
+    printf("===============================================================\n");
+  }
   test_count_++;
   OpenOutFile(test_count_);
   char codec_opus[] = "OPUS";
@@ -297,6 +317,24 @@
   Run(channel_a_to_b_);
   outfile_b_.Close();
 #endif
+  if (test_mode_ != 0) {
+    printf("===============================================================\n");
+
+    /* Print out all codecs that were not tested in the run */
+    printf("The following codecs was not included in the test:\n");
+#ifndef WEBRTC_CODEC_ILBC
+    printf("   iLBC\n");
+#endif
+#ifndef WEBRTC_CODEC_ISAC
+    printf("   ISAC float\n");
+#endif
+#ifndef WEBRTC_CODEC_ISACFX
+    printf("   ISAC fix\n");
+#endif
+
+    printf("\nTo complete the test, listen to the %d number of output files.\n",
+           test_count_);
+  }
 }
 
 // Register Codec to use in the test
@@ -316,21 +354,21 @@
                                       int rate,
                                       int packet_size,
                                       size_t extra_byte) {
+  if (test_mode_ != 0) {
+    // Print out codec and settings.
+    printf("codec: %s Freq: %d Rate: %d PackSize: %d\n", codec_name,
+           sampling_freq_hz, rate, packet_size);
+  }
+
   // Store packet-size in samples, used to validate the received packet.
   // If G.722, store half the size to compensate for the timestamp bug in the
   // RFC for G.722.
   // If iSAC runs in adaptive mode, packet size in samples can change on the
   // fly, so we exclude this test by setting |packet_size_samples_| to -1.
-  int clockrate_hz = sampling_freq_hz;
-  size_t num_channels = 1;
-  if (absl::EqualsIgnoreCase(codec_name, "G722")) {
+  if (!strcmp(codec_name, "G722")) {
     packet_size_samples_ = packet_size / 2;
-    clockrate_hz = sampling_freq_hz / 2;
-  } else if (absl::EqualsIgnoreCase(codec_name, "ISAC") && (rate == -1)) {
+  } else if (!strcmp(codec_name, "ISAC") && (rate == -1)) {
     packet_size_samples_ = -1;
-  } else if (absl::EqualsIgnoreCase(codec_name, "OPUS")) {
-    packet_size_samples_ = packet_size;
-    num_channels = 2;
   } else {
     packet_size_samples_ = packet_size;
   }
@@ -364,9 +402,16 @@
   }
   ASSERT_TRUE(my_acm != NULL);
 
+  // Get all codec parameters before registering
+  CodecInst my_codec_param;
+  CHECK_ERROR(AudioCodingModule::Codec(codec_name, &my_codec_param,
+                                       sampling_freq_hz, 1));
+  my_codec_param.rate = rate;
+  my_codec_param.pacsize = packet_size;
+
   auto factory = CreateBuiltinAudioEncoderFactory();
   constexpr int payload_type = 17;
-  SdpAudioFormat format = { codec_name, clockrate_hz, num_channels };
+  SdpAudioFormat format = CodecInstToSdp(my_codec_param);
   format.parameters["ptime"] = rtc::ToString(rtc::CheckedDivExact(
       packet_size, rtc::CheckedDivExact(sampling_freq_hz, 1000)));
   my_acm->SetEncoder(
@@ -440,4 +485,11 @@
   outfile_b_.Open(filename, 32000, "wb");
 }
 
+void TestAllCodecs::DisplaySendReceiveCodec() {
+  CodecInst my_codec_param;
+  printf("%s -> ", acm_a_->SendCodec()->plname);
+  acm_b_->ReceiveCodec(&my_codec_param);
+  printf("%s\n", my_codec_param.plname);
+}
+
 }  // namespace webrtc