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