blob: 3c20a54c474dd0aff9bd89f957bbe106f5c3ebe6 [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;
Yves Gerey665174f2018-06-19 15:03:05 +020086 std::string file_name =
87 webrtc::test::ResourcePath("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");
Yves Gerey665174f2018-06-19 15:03:05 +020091 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) {
Yves Gerey665174f2018-06-19 15:03:05 +020097 ASSERT_EQ(0, acm_b_->SetMinimumPlayoutDelay(FLAG_delay))
98 << "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++) {
Yves Gerey665174f2018-06-19 15:03:05 +0200104 EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param)) << "Failed to get codec.";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000105 if (STR_CASE_CMP(my_codec_param.plname, "opus") == 0)
106 my_codec_param.channels = 1;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000107 else if (my_codec_param.channels > 1)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000108 continue;
109 if (STR_CASE_CMP(my_codec_param.plname, "CN") == 0 &&
110 my_codec_param.plfreq == 48000)
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000111 continue;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000112 if (STR_CASE_CMP(my_codec_param.plname, "telephone-event") == 0)
113 continue;
kwibergda2bf4e2016-10-24 13:47:09 -0700114 ASSERT_EQ(true,
115 acm_b_->RegisterReceiveCodec(my_codec_param.pltype,
116 CodecInstToSdp(my_codec_param)));
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000117 }
118
119 // Create and connect the channel
Yves Gerey665174f2018-06-19 15:03:05 +0200120 ASSERT_EQ(0, acm_a_->RegisterTransportCallback(channel_a2b_))
121 << "Couldn't register Transport callback.\n";
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000122 channel_a2b_->RegisterReceiverACM(acm_b_.get());
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000123 }
124
Yves Gerey665174f2018-06-19 15:03:05 +0200125 void Perform(const TestSettings* config,
126 size_t num_tests,
127 int duration_sec,
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000128 const char* output_prefix) {
129 for (size_t n = 0; n < num_tests; ++n) {
130 ApplyConfig(config[n]);
131 Run(duration_sec, output_prefix);
132 }
133 }
134
135 private:
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000136 void ApplyConfig(const TestSettings& config) {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000137 printf("====================================\n");
Yves Gerey665174f2018-06-19 15:03:05 +0200138 printf(
139 "Test %d \n"
140 "Codec: %s, %d kHz, %d channel(s)\n"
141 "ACM: DTX %s, FEC %s\n"
142 "Channel: %s\n",
143 ++test_cntr_, config.codec.name, config.codec.sample_rate_hz,
144 config.codec.num_channels, config.acm.dtx ? "on" : "off",
145 config.acm.fec ? "on" : "off",
146 config.packet_loss ? "with packet-loss" : "no packet-loss");
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000147 SendCodec(config.codec);
148 ConfigAcm(config.acm);
149 ConfigChannel(config.packet_loss);
150 }
151
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000152 void SendCodec(const CodecSettings& config) {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000153 CodecInst my_codec_param;
Yves Gerey665174f2018-06-19 15:03:05 +0200154 ASSERT_EQ(
155 0, AudioCodingModule::Codec(config.name, &my_codec_param,
156 config.sample_rate_hz, config.num_channels))
157 << "Specified codec is not supported.\n";
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000158
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000159 encoding_sample_rate_hz_ = my_codec_param.plfreq;
Yves Gerey665174f2018-06-19 15:03:05 +0200160 ASSERT_EQ(0, acm_a_->RegisterSendCodec(my_codec_param))
161 << "Failed to register send-codec.\n";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000162 }
163
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000164 void ConfigAcm(const AcmSettings& config) {
Yves Gerey665174f2018-06-19 15:03:05 +0200165 ASSERT_EQ(0, acm_a_->SetVAD(config.dtx, config.dtx, VADAggr))
166 << "Failed to set VAD.\n";
167 ASSERT_EQ(0, acm_a_->SetREDStatus(config.fec)) << "Failed to set RED.\n";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000168 }
169
170 void ConfigChannel(bool packet_loss) {
171 channel_a2b_->SetFECTestWithPacketLoss(packet_loss);
172 }
173
174 void OpenOutFile(const char* output_id) {
175 std::stringstream file_stream;
oprypin6e09d872017-08-31 03:21:39 -0700176 file_stream << "delay_test_" << FLAG_codec << "_" << FLAG_sample_rate_hz
Yves Gerey665174f2018-06-19 15:03:05 +0200177 << "Hz"
178 << "_" << FLAG_delay << "ms.pcm";
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000179 std::cout << "Output file: " << file_stream.str() << std::endl << std::endl;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000180 std::string file_name = webrtc::test::OutputPath() + file_stream.str();
181 out_file_b_.Open(file_name.c_str(), 32000, "wb");
182 }
183
184 void Run(int duration_sec, const char* output_prefix) {
185 OpenOutFile(output_prefix);
186 AudioFrame audio_frame;
187 uint32_t out_freq_hz_b = out_file_b_.SamplingFrequency();
188
189 int num_frames = 0;
190 int in_file_frames = 0;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000191 uint32_t received_ts;
192 double average_delay = 0;
193 double inst_delay_sec = 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000194 while (num_frames < (duration_sec * 100)) {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000195 if (in_file_a_.EndOfFile()) {
196 in_file_a_.Rewind();
197 }
198
199 // Print delay information every 16 frame
200 if ((num_frames & 0x3F) == 0x3F) {
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000201 NetworkStatistics statistics;
202 acm_b_->GetNetworkStatistics(&statistics);
Yves Gerey665174f2018-06-19 15:03:05 +0200203 fprintf(stdout,
204 "delay: min=%3d max=%3d mean=%3d median=%3d"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000205 " ts-based average = %6.3f, "
206 "curr buff-lev = %4u opt buff-lev = %4u \n",
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000207 statistics.minWaitingTimeMs, statistics.maxWaitingTimeMs,
208 statistics.meanWaitingTimeMs, statistics.medianWaitingTimeMs,
209 average_delay, statistics.currentBufferSize,
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000210 statistics.preferredBufferSize);
Yves Gerey665174f2018-06-19 15:03:05 +0200211 fflush(stdout);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000212 }
213
214 in_file_a_.Read10MsData(audio_frame);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000215 ASSERT_GE(acm_a_->Add10MsData(audio_frame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700216 bool muted;
217 ASSERT_EQ(0,
218 acm_b_->PlayoutData10Ms(out_freq_hz_b, &audio_frame, &muted));
219 RTC_DCHECK(!muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000220 out_file_b_.Write10MsData(
yujo36b1a5f2017-06-12 12:45:32 -0700221 audio_frame.data(),
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000222 audio_frame.samples_per_channel_ * audio_frame.num_channels_);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000223 received_ts = channel_a2b_->LastInTimestamp();
Danil Chapovalovb6021232018-06-19 13:26:36 +0200224 absl::optional<uint32_t> playout_timestamp = acm_b_->PlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700225 ASSERT_TRUE(playout_timestamp);
226 inst_delay_sec = static_cast<uint32_t>(received_ts - *playout_timestamp) /
227 static_cast<double>(encoding_sample_rate_hz_);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000228
229 if (num_frames > 10)
230 average_delay = 0.95 * average_delay + 0.05 * inst_delay_sec;
231
232 ++num_frames;
233 ++in_file_frames;
234 }
235 out_file_b_.Close();
236 }
237
kwiberg37478382016-02-14 20:40:57 -0800238 std::unique_ptr<AudioCodingModule> acm_a_;
239 std::unique_ptr<AudioCodingModule> acm_b_;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000240
241 Channel* channel_a2b_;
242
243 PCMFile in_file_a_;
244 PCMFile out_file_b_;
245 int test_cntr_;
246 int encoding_sample_rate_hz_;
247};
248
andresp@webrtc.org185bae42013-05-14 08:02:25 +0000249} // namespace webrtc
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000250
251int main(int argc, char* argv[]) {
oprypin6e09d872017-08-31 03:21:39 -0700252 if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true)) {
253 return 1;
254 }
255 if (FLAG_help) {
256 rtc::FlagList::Print(nullptr, false);
257 return 0;
258 }
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000259
oprypin6e09d872017-08-31 03:21:39 -0700260 webrtc::TestSettings test_setting;
261 strcpy(test_setting.codec.name, FLAG_codec);
262
Yves Gerey665174f2018-06-19 15:03:05 +0200263 if (FLAG_sample_rate_hz != 8000 && FLAG_sample_rate_hz != 16000 &&
264 FLAG_sample_rate_hz != 32000 && FLAG_sample_rate_hz != 48000) {
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000265 std::cout << "Invalid sampling rate.\n";
266 return 1;
267 }
oprypin6e09d872017-08-31 03:21:39 -0700268 test_setting.codec.sample_rate_hz = FLAG_sample_rate_hz;
269 if (FLAG_num_channels < 1 || FLAG_num_channels > 2) {
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000270 std::cout << "Only mono and stereo are supported.\n";
271 return 1;
272 }
oprypin6e09d872017-08-31 03:21:39 -0700273 test_setting.codec.num_channels = FLAG_num_channels;
274 test_setting.acm.dtx = FLAG_dtx;
275 test_setting.acm.fec = FLAG_fec;
276 test_setting.packet_loss = FLAG_packet_loss;
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000277
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +0000278 webrtc::DelayTest delay_test;
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000279 delay_test.Initialize();
280 delay_test.Perform(&test_setting, 1, 240, "delay_test");
281 return 0;
282}