1ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org/*
2ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org *
4ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org *  Use of this source code is governed by a BSD-style license
5ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org *  that can be found in the LICENSE file in the root of the source
6ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org *  tree. An additional intellectual property rights grant can be found
7ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org *  in the file PATENTS.  All contributing project authors may
8ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org */
10ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
113f45c2e0ac4cb280f941efa3a3476895795e3dd6pbos@webrtc.org#include <assert.h>
12ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org#include <math.h>
13ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
14ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org#include <iostream>
15ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
16ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org#include "gflags/gflags.h"
17cf9ab12db22e50271900386e12d52974f8f455fetina.legrand@webrtc.org#include "testing/gtest/include/gtest/gtest.h"
18eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org#include "webrtc/common.h"
19ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org#include "webrtc/common_types.h"
20ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org#include "webrtc/engine_configurations.h"
21eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
22ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org#include "webrtc/modules/audio_coding/main/interface/audio_coding_module_typedefs.h"
234d081990603c0fc3c08a81523efc811b8e0828baturaj@webrtc.org#include "webrtc/modules/audio_coding/main/acm2/acm_common_defs.h"
24ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org#include "webrtc/modules/audio_coding/main/test/Channel.h"
25ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org#include "webrtc/modules/audio_coding/main/test/PCMFile.h"
26ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org#include "webrtc/modules/audio_coding/main/test/utility.h"
27ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org#include "webrtc/system_wrappers/interface/event_wrapper.h"
28ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org#include "webrtc/system_wrappers/interface/scoped_ptr.h"
29cf9ab12db22e50271900386e12d52974f8f455fetina.legrand@webrtc.org#include "webrtc/test/testsupport/fileutils.h"
30ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
31ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.orgDEFINE_string(codec, "isac", "Codec Name");
32ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.orgDEFINE_int32(sample_rate_hz, 16000, "Sampling rate in Hertz.");
33ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.orgDEFINE_int32(num_channels, 1, "Number of Channels.");
34ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.orgDEFINE_string(input_file, "", "Input file, PCM16 32 kHz, optional.");
35ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.orgDEFINE_int32(delay, 0, "Delay in millisecond.");
36ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.orgDEFINE_int32(init_delay, 0, "Initial delay in millisecond.");
37ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.orgDEFINE_bool(dtx, false, "Enable DTX at the sender side.");
38eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.orgDEFINE_bool(packet_loss, false, "Apply packet loss, c.f. Channel{.cc, .h}.");
39eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.orgDEFINE_bool(fec, false, "Use Forward Error Correction (FEC).");
40ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
41ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.orgnamespace webrtc {
42eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org
43ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.orgnamespace {
44ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
45eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.orgstruct CodecSettings {
46ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  char name[50];
47ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  int sample_rate_hz;
48ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  int num_channels;
49ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org};
50ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
51eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.orgstruct AcmSettings {
52ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  bool dtx;
53ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  bool fec;
54ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org};
55ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
56eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.orgstruct TestSettings {
57eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  CodecSettings codec;
58eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  AcmSettings acm;
59ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  bool packet_loss;
60ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org};
61ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
62eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org}  // namespace
63ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
64ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.orgclass DelayTest {
65ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org public:
66c68f88e9287a1e4e07f7739f39574a203b1c7750henrik.lundin@webrtc.org  DelayTest()
67c68f88e9287a1e4e07f7739f39574a203b1c7750henrik.lundin@webrtc.org      : acm_a_(AudioCodingModule::Create(0)),
68c68f88e9287a1e4e07f7739f39574a203b1c7750henrik.lundin@webrtc.org        acm_b_(AudioCodingModule::Create(1)),
69eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org        channel_a2b_(new Channel),
70b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org        test_cntr_(0),
71b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org        encoding_sample_rate_hz_(8000) {}
72ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
73eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  ~DelayTest() {
74b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org    if (channel_a2b_ != NULL) {
75ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      delete channel_a2b_;
76ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      channel_a2b_ = NULL;
77ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    }
78eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org    in_file_a_.Close();
79ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  }
80ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
81eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  void Initialize() {
82ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    test_cntr_ = 0;
83b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org    std::string file_name = webrtc::test::ResourcePath(
84b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org        "audio_coding/testfile32kHz", "pcm");
85ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    if (FLAGS_input_file.size() > 0)
86ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      file_name = FLAGS_input_file;
87ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    in_file_a_.Open(file_name, 32000, "rb");
88eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org    ASSERT_EQ(0, acm_a_->InitializeReceiver()) <<
89eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org        "Couldn't initialize receiver.\n";
90eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org    ASSERT_EQ(0, acm_b_->InitializeReceiver()) <<
91eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org        "Couldn't initialize receiver.\n";
92ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    if (FLAGS_init_delay > 0) {
93eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org      ASSERT_EQ(0, acm_b_->SetInitialPlayoutDelay(FLAGS_init_delay)) <<
94eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org          "Failed to set initial delay.\n";
95ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    }
96ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
97ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    if (FLAGS_delay > 0) {
98eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org      ASSERT_EQ(0, acm_b_->SetMinimumPlayoutDelay(FLAGS_delay)) <<
99eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org          "Failed to set minimum delay.\n";
100ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    }
101ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
102eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org    int num_encoders = acm_a_->NumberOfCodecs();
103ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    CodecInst my_codec_param;
104b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org    for (int n = 0; n < num_encoders; n++) {
105eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org      EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param)) <<
106eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org          "Failed to get codec.";
107ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      if (STR_CASE_CMP(my_codec_param.plname, "opus") == 0)
108ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org        my_codec_param.channels = 1;
109b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org      else if (my_codec_param.channels > 1)
110ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org        continue;
111ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      if (STR_CASE_CMP(my_codec_param.plname, "CN") == 0 &&
112ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org          my_codec_param.plfreq == 48000)
113b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org        continue;
114ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      if (STR_CASE_CMP(my_codec_param.plname, "telephone-event") == 0)
115ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org        continue;
116eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org      ASSERT_EQ(0, acm_b_->RegisterReceiveCodec(my_codec_param)) <<
117eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org          "Couldn't register receive codec.\n";
118ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    }
119ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
120ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    // Create and connect the channel
121eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org    ASSERT_EQ(0, acm_a_->RegisterTransportCallback(channel_a2b_)) <<
122eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org        "Couldn't register Transport callback.\n";
1235e3379e47b802f3aaef17b9b34308e057bf39675andrew@webrtc.org    channel_a2b_->RegisterReceiverACM(acm_b_.get());
124ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  }
125ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
126eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  void Perform(const TestSettings* config, size_t num_tests, int duration_sec,
127ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org               const char* output_prefix) {
128ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    for (size_t n = 0; n < num_tests; ++n) {
129ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      ApplyConfig(config[n]);
130ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      Run(duration_sec, output_prefix);
131ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    }
132ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  }
133ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
134ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org private:
135eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  void ApplyConfig(const TestSettings& config) {
136ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    printf("====================================\n");
137ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    printf("Test %d \n"
138b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org           "Codec: %s, %d kHz, %d channel(s)\n"
139b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org           "ACM: DTX %s, FEC %s\n"
140b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org           "Channel: %s\n",
141b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org           ++test_cntr_, config.codec.name, config.codec.sample_rate_hz,
142b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org           config.codec.num_channels, config.acm.dtx ? "on" : "off",
143b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org           config.acm.fec ? "on" : "off",
144b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org           config.packet_loss ? "with packet-loss" : "no packet-loss");
145ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    SendCodec(config.codec);
146ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    ConfigAcm(config.acm);
147ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    ConfigChannel(config.packet_loss);
148ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  }
149ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
150eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  void SendCodec(const CodecSettings& config) {
151ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    CodecInst my_codec_param;
152eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org    ASSERT_EQ(0, AudioCodingModule::Codec(
153eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org              config.name, &my_codec_param, config.sample_rate_hz,
154eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org              config.num_channels)) << "Specified codec is not supported.\n";
155eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org
156ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    encoding_sample_rate_hz_ = my_codec_param.plfreq;
157eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org    ASSERT_EQ(0, acm_a_->RegisterSendCodec(my_codec_param)) <<
158eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org        "Failed to register send-codec.\n";
159ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  }
160ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
161eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  void ConfigAcm(const AcmSettings& config) {
162eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org    ASSERT_EQ(0, acm_a_->SetVAD(config.dtx, config.dtx, VADAggr)) <<
163eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org        "Failed to set VAD.\n";
16491c0a2592faf1e68f658c98e5faa1acef7124d1aminyue@webrtc.org    ASSERT_EQ(0, acm_a_->SetREDStatus(config.fec)) <<
16591c0a2592faf1e68f658c98e5faa1acef7124d1aminyue@webrtc.org        "Failed to set RED.\n";
166ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  }
167ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
168ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  void ConfigChannel(bool packet_loss) {
169ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    channel_a2b_->SetFECTestWithPacketLoss(packet_loss);
170ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  }
171ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
172ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  void OpenOutFile(const char* output_id) {
173ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    std::stringstream file_stream;
174b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org    file_stream << "delay_test_" << FLAGS_codec << "_" << FLAGS_sample_rate_hz
175b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org        << "Hz" << "_" << FLAGS_init_delay << "ms_" << FLAGS_delay << "ms.pcm";
176b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org    std::cout << "Output file: " << file_stream.str() << std::endl << std::endl;
177ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    std::string file_name = webrtc::test::OutputPath() + file_stream.str();
178ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    out_file_b_.Open(file_name.c_str(), 32000, "wb");
179ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  }
180ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
181ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  void Run(int duration_sec, const char* output_prefix) {
182ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    OpenOutFile(output_prefix);
183ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    AudioFrame audio_frame;
184ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    uint32_t out_freq_hz_b = out_file_b_.SamplingFrequency();
185ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
186ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    int num_frames = 0;
187ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    int in_file_frames = 0;
188ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    uint32_t playout_ts;
189ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    uint32_t received_ts;
190ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    double average_delay = 0;
191ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    double inst_delay_sec = 0;
192b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org    while (num_frames < (duration_sec * 100)) {
193ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      if (in_file_a_.EndOfFile()) {
194ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org        in_file_a_.Rewind();
195ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      }
196ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
197ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      // Print delay information every 16 frame
198ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      if ((num_frames & 0x3F) == 0x3F) {
199ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org        ACMNetworkStatistics statistics;
200e9bb4e54a1f7b8b9dcbd3f47b5d99e3ea6012cc8tina.legrand@webrtc.org        acm_b_->NetworkStatistics(&statistics);
201ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org        fprintf(stdout, "delay: min=%3d  max=%3d  mean=%3d  median=%3d"
202ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org                " ts-based average = %6.3f, "
203ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org                "curr buff-lev = %4u opt buff-lev = %4u \n",
204b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org                statistics.minWaitingTimeMs, statistics.maxWaitingTimeMs,
205b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org                statistics.meanWaitingTimeMs, statistics.medianWaitingTimeMs,
206b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org                average_delay, statistics.currentBufferSize,
207ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org                statistics.preferredBufferSize);
208b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org        fflush (stdout);
209ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      }
210ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
211ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      in_file_a_.Read10MsData(audio_frame);
212ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      ASSERT_EQ(0, acm_a_->Add10MsData(audio_frame));
213ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      ASSERT_LE(0, acm_a_->Process());
214e9bb4e54a1f7b8b9dcbd3f47b5d99e3ea6012cc8tina.legrand@webrtc.org      ASSERT_EQ(0, acm_b_->PlayoutData10Ms(out_freq_hz_b, &audio_frame));
215b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org      out_file_b_.Write10MsData(
216b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org          audio_frame.data_,
217b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org          audio_frame.samples_per_channel_ * audio_frame.num_channels_);
218e9bb4e54a1f7b8b9dcbd3f47b5d99e3ea6012cc8tina.legrand@webrtc.org      acm_b_->PlayoutTimestamp(&playout_ts);
219ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      received_ts = channel_a2b_->LastInTimestamp();
220b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org      inst_delay_sec = static_cast<uint32_t>(received_ts - playout_ts)
221b31332e75f97ef3d41d5393772be11f24b0f174ftina.legrand@webrtc.org          / static_cast<double>(encoding_sample_rate_hz_);
222ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
223ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      if (num_frames > 10)
224ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org        average_delay = 0.95 * average_delay + 0.05 * inst_delay_sec;
225ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
226ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      ++num_frames;
227ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org      ++in_file_frames;
228ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    }
229ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org    out_file_b_.Close();
230ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  }
231ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
2325e3379e47b802f3aaef17b9b34308e057bf39675andrew@webrtc.org  scoped_ptr<AudioCodingModule> acm_a_;
2335e3379e47b802f3aaef17b9b34308e057bf39675andrew@webrtc.org  scoped_ptr<AudioCodingModule> acm_b_;
234ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
235ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  Channel* channel_a2b_;
236ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
237ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  PCMFile in_file_a_;
238ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  PCMFile out_file_b_;
239ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  int test_cntr_;
240ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org  int encoding_sample_rate_hz_;
241ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org};
242ead8a5bbdcdf3ac429c23af04d4a91f3de334f28turaj@webrtc.org
243ee6f8a27fd52a1843cd0d4df570b91b8fd542b82andresp@webrtc.org}  // namespace webrtc
244eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org
245eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.orgint main(int argc, char* argv[]) {
246eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  google::ParseCommandLineFlags(&argc, &argv, true);
247eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  webrtc::TestSettings test_setting;
248eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  strcpy(test_setting.codec.name, FLAGS_codec.c_str());
249eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org
250eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  if (FLAGS_sample_rate_hz != 8000 &&
251eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org      FLAGS_sample_rate_hz != 16000 &&
252eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org      FLAGS_sample_rate_hz != 32000 &&
253eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org      FLAGS_sample_rate_hz != 48000) {
254eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org    std::cout << "Invalid sampling rate.\n";
255eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org    return 1;
256eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  }
257eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  test_setting.codec.sample_rate_hz = FLAGS_sample_rate_hz;
258eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  if (FLAGS_num_channels < 1 || FLAGS_num_channels > 2) {
259eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org    std::cout << "Only mono and stereo are supported.\n";
260eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org    return 1;
261eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  }
262eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  test_setting.codec.num_channels = FLAGS_num_channels;
263eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  test_setting.acm.dtx = FLAGS_dtx;
264eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  test_setting.acm.fec = FLAGS_fec;
265eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  test_setting.packet_loss = FLAGS_packet_loss;
266eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org
267c68f88e9287a1e4e07f7739f39574a203b1c7750henrik.lundin@webrtc.org  webrtc::DelayTest delay_test;
268eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  delay_test.Initialize();
269eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  delay_test.Perform(&test_setting, 1, 240, "delay_test");
270eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org  return 0;
271eb45a2069c8defa699b298cad99e3f3f8d3eb97cturaj@webrtc.org}
272