blob: 532a8eb14eb10c3ce32b2795bd83abc025bb160a [file] [log] [blame]
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00001/*
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
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000011#include <assert.h>
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000012#include <math.h>
oprypin6e09d872017-08-31 03:21:39 -070013#include <string.h>
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000014
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000015#include <iostream>
kwiberg37478382016-02-14 20:40:57 -080016#include <memory>
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000017
Karl Wiberg5817d3d2018-04-06 10:06:42 +020018#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020019#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/codecs/audio_format_conversion.h"
21#include "modules/audio_coding/include/audio_coding_module.h"
22#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
23#include "modules/audio_coding/test/Channel.h"
24#include "modules/audio_coding/test/PCMFile.h"
25#include "modules/audio_coding/test/utility.h"
26#include "rtc_base/flags.h"
27#include "system_wrappers/include/event_wrapper.h"
28#include "test/gtest.h"
29#include "test/testsupport/fileutils.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020030#include "typedefs.h" // NOLINT(build/include)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000031
32DEFINE_string(codec, "isac", "Codec Name");
oprypin6e09d872017-08-31 03:21:39 -070033DEFINE_int(sample_rate_hz, 16000, "Sampling rate in Hertz.");
34DEFINE_int(num_channels, 1, "Number of Channels.");
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000035DEFINE_string(input_file, "", "Input file, PCM16 32 kHz, optional.");
oprypin6e09d872017-08-31 03:21:39 -070036DEFINE_int(delay, 0, "Delay in millisecond.");
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000037DEFINE_bool(dtx, false, "Enable DTX at the sender side.");
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000038DEFINE_bool(packet_loss, false, "Apply packet loss, c.f. Channel{.cc, .h}.");
39DEFINE_bool(fec, false, "Use Forward Error Correction (FEC).");
oprypin6e09d872017-08-31 03:21:39 -070040DEFINE_bool(help, false, "Print this message.");
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000041
42namespace webrtc {
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000043
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000044namespace {
45
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000046struct CodecSettings {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000047 char name[50];
48 int sample_rate_hz;
49 int num_channels;
50};
51
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000052struct AcmSettings {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000053 bool dtx;
54 bool fec;
55};
56
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000057struct TestSettings {
58 CodecSettings codec;
59 AcmSettings acm;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000060 bool packet_loss;
61};
62
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000063} // namespace
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000064
65class DelayTest {
66 public:
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +000067 DelayTest()
Karl Wiberg5817d3d2018-04-06 10:06:42 +020068 : acm_a_(AudioCodingModule::Create(
69 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
70 acm_b_(AudioCodingModule::Create(
71 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000072 channel_a2b_(new Channel),
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000073 test_cntr_(0),
74 encoding_sample_rate_hz_(8000) {}
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000075
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000076 ~DelayTest() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000077 if (channel_a2b_ != NULL) {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000078 delete channel_a2b_;
79 channel_a2b_ = NULL;
80 }
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000081 in_file_a_.Close();
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000082 }
83
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000084 void Initialize() {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000085 test_cntr_ = 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000086 std::string file_name = webrtc::test::ResourcePath(
87 "audio_coding/testfile32kHz", "pcm");
oprypin6e09d872017-08-31 03:21:39 -070088 if (strlen(FLAG_input_file) > 0)
89 file_name = FLAG_input_file;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000090 in_file_a_.Open(file_name, 32000, "rb");
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000091 ASSERT_EQ(0, acm_a_->InitializeReceiver()) <<
92 "Couldn't initialize receiver.\n";
93 ASSERT_EQ(0, acm_b_->InitializeReceiver()) <<
94 "Couldn't initialize receiver.\n";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000095
oprypin6e09d872017-08-31 03:21:39 -070096 if (FLAG_delay > 0) {
97 ASSERT_EQ(0, acm_b_->SetMinimumPlayoutDelay(FLAG_delay)) <<
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000098 "Failed to set minimum delay.\n";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000099 }
100
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000101 int num_encoders = acm_a_->NumberOfCodecs();
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000102 CodecInst my_codec_param;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000103 for (int n = 0; n < num_encoders; n++) {
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000104 EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param)) <<
105 "Failed to get codec.";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000106 if (STR_CASE_CMP(my_codec_param.plname, "opus") == 0)
107 my_codec_param.channels = 1;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000108 else if (my_codec_param.channels > 1)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000109 continue;
110 if (STR_CASE_CMP(my_codec_param.plname, "CN") == 0 &&
111 my_codec_param.plfreq == 48000)
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000112 continue;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000113 if (STR_CASE_CMP(my_codec_param.plname, "telephone-event") == 0)
114 continue;
kwibergda2bf4e2016-10-24 13:47:09 -0700115 ASSERT_EQ(true,
116 acm_b_->RegisterReceiveCodec(my_codec_param.pltype,
117 CodecInstToSdp(my_codec_param)));
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000118 }
119
120 // Create and connect the channel
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000121 ASSERT_EQ(0, acm_a_->RegisterTransportCallback(channel_a2b_)) <<
122 "Couldn't register Transport callback.\n";
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000123 channel_a2b_->RegisterReceiverACM(acm_b_.get());
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000124 }
125
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000126 void Perform(const TestSettings* config, size_t num_tests, int duration_sec,
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000127 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:
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000135 void ApplyConfig(const TestSettings& config) {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000136 printf("====================================\n");
137 printf("Test %d \n"
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000138 "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");
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000145 SendCodec(config.codec);
146 ConfigAcm(config.acm);
147 ConfigChannel(config.packet_loss);
148 }
149
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000150 void SendCodec(const CodecSettings& config) {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000151 CodecInst my_codec_param;
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000152 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
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000156 encoding_sample_rate_hz_ = my_codec_param.plfreq;
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000157 ASSERT_EQ(0, acm_a_->RegisterSendCodec(my_codec_param)) <<
158 "Failed to register send-codec.\n";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000159 }
160
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000161 void ConfigAcm(const AcmSettings& config) {
162 ASSERT_EQ(0, acm_a_->SetVAD(config.dtx, config.dtx, VADAggr)) <<
163 "Failed to set VAD.\n";
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000164 ASSERT_EQ(0, acm_a_->SetREDStatus(config.fec)) <<
165 "Failed to set RED.\n";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000166 }
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;
oprypin6e09d872017-08-31 03:21:39 -0700174 file_stream << "delay_test_" << FLAG_codec << "_" << FLAG_sample_rate_hz
175 << "Hz" << "_" << FLAG_delay << "ms.pcm";
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000176 std::cout << "Output file: " << file_stream.str() << std::endl << std::endl;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000177 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;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000188 uint32_t received_ts;
189 double average_delay = 0;
190 double inst_delay_sec = 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000191 while (num_frames < (duration_sec * 100)) {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000192 if (in_file_a_.EndOfFile()) {
193 in_file_a_.Rewind();
194 }
195
196 // Print delay information every 16 frame
197 if ((num_frames & 0x3F) == 0x3F) {
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000198 NetworkStatistics statistics;
199 acm_b_->GetNetworkStatistics(&statistics);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000200 fprintf(stdout, "delay: min=%3d max=%3d mean=%3d median=%3d"
201 " ts-based average = %6.3f, "
202 "curr buff-lev = %4u opt buff-lev = %4u \n",
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000203 statistics.minWaitingTimeMs, statistics.maxWaitingTimeMs,
204 statistics.meanWaitingTimeMs, statistics.medianWaitingTimeMs,
205 average_delay, statistics.currentBufferSize,
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000206 statistics.preferredBufferSize);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000207 fflush (stdout);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000208 }
209
210 in_file_a_.Read10MsData(audio_frame);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000211 ASSERT_GE(acm_a_->Add10MsData(audio_frame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700212 bool muted;
213 ASSERT_EQ(0,
214 acm_b_->PlayoutData10Ms(out_freq_hz_b, &audio_frame, &muted));
215 RTC_DCHECK(!muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000216 out_file_b_.Write10MsData(
yujo36b1a5f2017-06-12 12:45:32 -0700217 audio_frame.data(),
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000218 audio_frame.samples_per_channel_ * audio_frame.num_channels_);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000219 received_ts = channel_a2b_->LastInTimestamp();
Danil Chapovalovb6021232018-06-19 13:26:36 +0200220 absl::optional<uint32_t> playout_timestamp = acm_b_->PlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700221 ASSERT_TRUE(playout_timestamp);
222 inst_delay_sec = static_cast<uint32_t>(received_ts - *playout_timestamp) /
223 static_cast<double>(encoding_sample_rate_hz_);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000224
225 if (num_frames > 10)
226 average_delay = 0.95 * average_delay + 0.05 * inst_delay_sec;
227
228 ++num_frames;
229 ++in_file_frames;
230 }
231 out_file_b_.Close();
232 }
233
kwiberg37478382016-02-14 20:40:57 -0800234 std::unique_ptr<AudioCodingModule> acm_a_;
235 std::unique_ptr<AudioCodingModule> acm_b_;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000236
237 Channel* channel_a2b_;
238
239 PCMFile in_file_a_;
240 PCMFile out_file_b_;
241 int test_cntr_;
242 int encoding_sample_rate_hz_;
243};
244
andresp@webrtc.org185bae42013-05-14 08:02:25 +0000245} // namespace webrtc
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000246
247int main(int argc, char* argv[]) {
oprypin6e09d872017-08-31 03:21:39 -0700248 if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true)) {
249 return 1;
250 }
251 if (FLAG_help) {
252 rtc::FlagList::Print(nullptr, false);
253 return 0;
254 }
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000255
oprypin6e09d872017-08-31 03:21:39 -0700256 webrtc::TestSettings test_setting;
257 strcpy(test_setting.codec.name, FLAG_codec);
258
259 if (FLAG_sample_rate_hz != 8000 &&
260 FLAG_sample_rate_hz != 16000 &&
261 FLAG_sample_rate_hz != 32000 &&
262 FLAG_sample_rate_hz != 48000) {
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000263 std::cout << "Invalid sampling rate.\n";
264 return 1;
265 }
oprypin6e09d872017-08-31 03:21:39 -0700266 test_setting.codec.sample_rate_hz = FLAG_sample_rate_hz;
267 if (FLAG_num_channels < 1 || FLAG_num_channels > 2) {
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000268 std::cout << "Only mono and stereo are supported.\n";
269 return 1;
270 }
oprypin6e09d872017-08-31 03:21:39 -0700271 test_setting.codec.num_channels = FLAG_num_channels;
272 test_setting.acm.dtx = FLAG_dtx;
273 test_setting.acm.fec = FLAG_fec;
274 test_setting.packet_loss = FLAG_packet_loss;
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000275
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +0000276 webrtc::DelayTest delay_test;
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000277 delay_test.Initialize();
278 delay_test.Perform(&test_setting, 1, 240, "delay_test");
279 return 0;
280}