From 7a05ae5c697370ef19cad72c5443071caa4867af Mon Sep 17 00:00:00 2001
From: "turaj@webrtc.org"
 <turaj@webrtc.org@4adac7df-926f-26a2-2b94-8c16560cd09d>
Date: Mon, 18 Nov 2013 18:16:53 +0000
Subject: [PATCH] Adding back main() to the test. Now it is possible to choose
 between ACM1 and ACM2, furthermore, the test can simulate a channel with
 packet loss and FEC can be activated. Packet loss pattern is based on channel
 implementation in Channel{.cc,.h}, which currently is a determenistic pattern
 with 1 every 3rd packet is discarded.

The main() was deleted in r4731.

BUG=
R=andrew@webrtc.org, minyue@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/2370004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@5132 4adac7df-926f-26a2-2b94-8c16560cd09d
---
 .../main/source/audio_coding_module.gypi      |   5 +-
 .../audio_coding/main/test/delay_test.cc      | 135 +++++++++++-------
 2 files changed, 86 insertions(+), 54 deletions(-)

diff --git a/webrtc/modules/audio_coding/main/source/audio_coding_module.gypi b/webrtc/modules/audio_coding/main/source/audio_coding_module.gypi
index c6c10f693..a0389b03e 100644
--- a/webrtc/modules/audio_coding/main/source/audio_coding_module.gypi
+++ b/webrtc/modules/audio_coding/main/source/audio_coding_module.gypi
@@ -117,7 +117,7 @@
           'dependencies': [
             'audio_coding_module',
             '<(DEPTH)/testing/gtest.gyp:gtest',
-            '<(webrtc_root)/test/test.gyp:test_support_main',
+            '<(webrtc_root)/test/test.gyp:test_support',
             '<(webrtc_root)/system_wrappers/source/system_wrappers.gyp:system_wrappers',
             '<(DEPTH)/third_party/gflags/gflags.gyp:gflags',
           ],
@@ -125,6 +125,7 @@
              '../test/delay_test.cc',
              '../test/Channel.cc',
              '../test/PCMFile.cc',
+             '../test/utility.cc',
            ],
         }, # delay_test
         {
@@ -133,7 +134,7 @@
           'dependencies': [
             'audio_coding_module',
             '<(DEPTH)/testing/gtest.gyp:gtest',
-            '<(webrtc_root)/test/test.gyp:test_support_main',
+            '<(webrtc_root)/test/test.gyp:test_support',
             '<(webrtc_root)/system_wrappers/source/system_wrappers.gyp:system_wrappers',
             '<(DEPTH)/third_party/gflags/gflags.gyp:gflags',
           ],
diff --git a/webrtc/modules/audio_coding/main/test/delay_test.cc b/webrtc/modules/audio_coding/main/test/delay_test.cc
index 1a0f8f829..63bfe2be4 100644
--- a/webrtc/modules/audio_coding/main/test/delay_test.cc
+++ b/webrtc/modules/audio_coding/main/test/delay_test.cc
@@ -8,8 +8,6 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
-
 #include <assert.h>
 #include <math.h>
 
@@ -17,8 +15,10 @@
 
 #include "gflags/gflags.h"
 #include "testing/gtest/include/gtest/gtest.h"
+#include "webrtc/common.h"
 #include "webrtc/common_types.h"
 #include "webrtc/engine_configurations.h"
+#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
 #include "webrtc/modules/audio_coding/main/interface/audio_coding_module_typedefs.h"
 #include "webrtc/modules/audio_coding/main/acm2/acm_common_defs.h"
 #include "webrtc/modules/audio_coding/main/test/Channel.h"
@@ -35,68 +35,76 @@ DEFINE_string(input_file, "", "Input file, PCM16 32 kHz, optional.");
 DEFINE_int32(delay, 0, "Delay in millisecond.");
 DEFINE_int32(init_delay, 0, "Initial delay in millisecond.");
 DEFINE_bool(dtx, false, "Enable DTX at the sender side.");
+DEFINE_bool(acm2, false, "Run the test with ACM2.");
+DEFINE_bool(packet_loss, false, "Apply packet loss, c.f. Channel{.cc, .h}.");
+DEFINE_bool(fec, false, "Use Forward Error Correction (FEC).");
 
 namespace webrtc {
+
 namespace {
 
-struct CodecConfig {
+struct CodecSettings {
   char name[50];
   int sample_rate_hz;
   int num_channels;
 };
 
-struct AcmConfig {
+struct AcmSettings {
   bool dtx;
   bool fec;
 };
 
-struct Config {
-  CodecConfig codec;
-  AcmConfig acm;
+struct TestSettings {
+  CodecSettings codec;
+  AcmSettings acm;
   bool packet_loss;
 };
 
+}  // namespace
 
 class DelayTest {
  public:
-
-  DelayTest()
-      : acm_a_(AudioCodingModule::Create(0)),
-        acm_b_(AudioCodingModule::Create(1)),
-        channel_a2b_(NULL),
+  explicit DelayTest(const Config& config)
+      : acm_a_(config.Get<AudioCodingModuleFactory>().Create(0)),
+        acm_b_(config.Get<AudioCodingModuleFactory>().Create(1)),
+        channel_a2b_(new Channel),
         test_cntr_(0),
         encoding_sample_rate_hz_(8000) {}
 
-  ~DelayTest() {}
-
-  void TearDown() {
+  ~DelayTest() {
     if (channel_a2b_ != NULL) {
       delete channel_a2b_;
       channel_a2b_ = NULL;
     }
+    in_file_a_.Close();
   }
 
-  void SetUp() {
+  void Initialize() {
     test_cntr_ = 0;
     std::string file_name = webrtc::test::ResourcePath(
         "audio_coding/testfile32kHz", "pcm");
     if (FLAGS_input_file.size() > 0)
       file_name = FLAGS_input_file;
     in_file_a_.Open(file_name, 32000, "rb");
-    acm_a_->InitializeReceiver();
-    acm_b_->InitializeReceiver();
+    ASSERT_EQ(0, acm_a_->InitializeReceiver()) <<
+        "Couldn't initialize receiver.\n";
+    ASSERT_EQ(0, acm_b_->InitializeReceiver()) <<
+        "Couldn't initialize receiver.\n";
     if (FLAGS_init_delay > 0) {
-      ASSERT_EQ(0, acm_b_->SetInitialPlayoutDelay(FLAGS_init_delay));
+      ASSERT_EQ(0, acm_b_->SetInitialPlayoutDelay(FLAGS_init_delay)) <<
+          "Failed to set initial delay.\n";
     }
 
     if (FLAGS_delay > 0) {
-      ASSERT_EQ(0, acm_b_->SetMinimumPlayoutDelay(FLAGS_delay));
+      ASSERT_EQ(0, acm_b_->SetMinimumPlayoutDelay(FLAGS_delay)) <<
+          "Failed to set minimum delay.\n";
     }
 
-    uint8_t num_encoders = acm_a_->NumberOfCodecs();
+    int num_encoders = acm_a_->NumberOfCodecs();
     CodecInst my_codec_param;
     for (int n = 0; n < num_encoders; n++) {
-      acm_b_->Codec(n, &my_codec_param);
+      EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param)) <<
+          "Failed to get codec.";
       if (STR_CASE_CMP(my_codec_param.plname, "opus") == 0)
         my_codec_param.channels = 1;
       else if (my_codec_param.channels > 1)
@@ -106,16 +114,17 @@ class DelayTest {
         continue;
       if (STR_CASE_CMP(my_codec_param.plname, "telephone-event") == 0)
         continue;
-      acm_b_->RegisterReceiveCodec(my_codec_param);
+      ASSERT_EQ(0, acm_b_->RegisterReceiveCodec(my_codec_param)) <<
+          "Couldn't register receive codec.\n";
     }
 
     // Create and connect the channel
-    channel_a2b_ = new Channel;
-    acm_a_->RegisterTransportCallback(channel_a2b_);
+    ASSERT_EQ(0, acm_a_->RegisterTransportCallback(channel_a2b_)) <<
+        "Couldn't register Transport callback.\n";
     channel_a2b_->RegisterReceiverACM(acm_b_.get());
   }
 
-  void Perform(const Config* config, size_t num_tests, int duration_sec,
+  void Perform(const TestSettings* config, size_t num_tests, int duration_sec,
                const char* output_prefix) {
     for (size_t n = 0; n < num_tests; ++n) {
       ApplyConfig(config[n]);
@@ -124,8 +133,7 @@ class DelayTest {
   }
 
  private:
-
-  void ApplyConfig(const Config& config) {
+  void ApplyConfig(const TestSettings& config) {
     printf("====================================\n");
     printf("Test %d \n"
            "Codec: %s, %d kHz, %d channel(s)\n"
@@ -140,19 +148,22 @@ class DelayTest {
     ConfigChannel(config.packet_loss);
   }
 
-  void SendCodec(const CodecConfig& config) {
+  void SendCodec(const CodecSettings& config) {
     CodecInst my_codec_param;
-    ASSERT_EQ(
-        0,
-        AudioCodingModule::Codec(config.name, &my_codec_param,
-                                 config.sample_rate_hz, config.num_channels));
+    ASSERT_EQ(0, AudioCodingModule::Codec(
+              config.name, &my_codec_param, config.sample_rate_hz,
+              config.num_channels)) << "Specified codec is not supported.\n";
+
     encoding_sample_rate_hz_ = my_codec_param.plfreq;
-    ASSERT_EQ(0, acm_a_->RegisterSendCodec(my_codec_param));
+    ASSERT_EQ(0, acm_a_->RegisterSendCodec(my_codec_param)) <<
+        "Failed to register send-codec.\n";
   }
 
-  void ConfigAcm(const AcmConfig& config) {
-    ASSERT_EQ(0, acm_a_->SetVAD(config.dtx, config.dtx, VADAggr));
-    ASSERT_EQ(0, acm_a_->SetFECStatus(config.fec));
+  void ConfigAcm(const AcmSettings& config) {
+    ASSERT_EQ(0, acm_a_->SetVAD(config.dtx, config.dtx, VADAggr)) <<
+        "Failed to set VAD.\n";
+    ASSERT_EQ(0, acm_a_->SetFECStatus(config.fec)) <<
+        "Failed to set FEC.\n";
   }
 
   void ConfigChannel(bool packet_loss) {
@@ -230,19 +241,39 @@ class DelayTest {
   int encoding_sample_rate_hz_;
 };
 
-void RunTest() {
-  Config config;
-  strcpy(config.codec.name, FLAGS_codec.c_str());
-  config.codec.sample_rate_hz = FLAGS_sample_rate_hz;
-  config.codec.num_channels = FLAGS_num_channels;
-  config.acm.dtx = FLAGS_dtx;
-  config.acm.fec = false;
-  config.packet_loss = false;
-
-  DelayTest delay_test;
-  delay_test.SetUp();
-  delay_test.Perform(&config, 1, 240, "delay_test");
-  delay_test.TearDown();
-}
-}  // namespace
 }  // namespace webrtc
+
+int main(int argc, char* argv[]) {
+  google::ParseCommandLineFlags(&argc, &argv, true);
+  webrtc::Config config;
+  webrtc::TestSettings test_setting;
+  strcpy(test_setting.codec.name, FLAGS_codec.c_str());
+
+  if (FLAGS_sample_rate_hz != 8000 &&
+      FLAGS_sample_rate_hz != 16000 &&
+      FLAGS_sample_rate_hz != 32000 &&
+      FLAGS_sample_rate_hz != 48000) {
+    std::cout << "Invalid sampling rate.\n";
+    return 1;
+  }
+  test_setting.codec.sample_rate_hz = FLAGS_sample_rate_hz;
+  if (FLAGS_num_channels < 1 || FLAGS_num_channels > 2) {
+    std::cout << "Only mono and stereo are supported.\n";
+    return 1;
+  }
+  test_setting.codec.num_channels = FLAGS_num_channels;
+  test_setting.acm.dtx = FLAGS_dtx;
+  test_setting.acm.fec = FLAGS_fec;
+  test_setting.packet_loss = FLAGS_packet_loss;
+
+  if (FLAGS_acm2) {
+    webrtc::UseNewAcm(&config);
+  } else {
+    webrtc::UseLegacyAcm(&config);
+  }
+
+  webrtc::DelayTest delay_test(config);
+  delay_test.Initialize();
+  delay_test.Perform(&test_setting, 1, 240, "delay_test");
+  return 0;
+}