blob: d49cd3ced7ff7ed6e4d61144904e33381f13cb0c [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"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000030
31DEFINE_string(codec, "isac", "Codec Name");
oprypin6e09d872017-08-31 03:21:39 -070032DEFINE_int(sample_rate_hz, 16000, "Sampling rate in Hertz.");
33DEFINE_int(num_channels, 1, "Number of Channels.");
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000034DEFINE_string(input_file, "", "Input file, PCM16 32 kHz, optional.");
oprypin6e09d872017-08-31 03:21:39 -070035DEFINE_int(delay, 0, "Delay in millisecond.");
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000036DEFINE_bool(dtx, false, "Enable DTX at the sender side.");
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000037DEFINE_bool(packet_loss, false, "Apply packet loss, c.f. Channel{.cc, .h}.");
38DEFINE_bool(fec, false, "Use Forward Error Correction (FEC).");
oprypin6e09d872017-08-31 03:21:39 -070039DEFINE_bool(help, false, "Print this message.");
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000040
41namespace webrtc {
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000042
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000043namespace {
44
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000045struct CodecSettings {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000046 char name[50];
47 int sample_rate_hz;
48 int num_channels;
49};
50
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000051struct AcmSettings {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000052 bool dtx;
53 bool fec;
54};
55
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000056struct TestSettings {
57 CodecSettings codec;
58 AcmSettings acm;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000059 bool packet_loss;
60};
61
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000062} // namespace
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000063
64class DelayTest {
65 public:
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +000066 DelayTest()
Karl Wiberg5817d3d2018-04-06 10:06:42 +020067 : acm_a_(AudioCodingModule::Create(
68 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
69 acm_b_(AudioCodingModule::Create(
70 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000071 channel_a2b_(new Channel),
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000072 test_cntr_(0),
73 encoding_sample_rate_hz_(8000) {}
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000074
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000075 ~DelayTest() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000076 if (channel_a2b_ != NULL) {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000077 delete channel_a2b_;
78 channel_a2b_ = NULL;
79 }
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000080 in_file_a_.Close();
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000081 }
82
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000083 void Initialize() {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000084 test_cntr_ = 0;
Yves Gerey665174f2018-06-19 15:03:05 +020085 std::string file_name =
86 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
oprypin6e09d872017-08-31 03:21:39 -070087 if (strlen(FLAG_input_file) > 0)
88 file_name = FLAG_input_file;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000089 in_file_a_.Open(file_name, 32000, "rb");
Yves Gerey665174f2018-06-19 15:03:05 +020090 ASSERT_EQ(0, acm_a_->InitializeReceiver())
91 << "Couldn't initialize receiver.\n";
92 ASSERT_EQ(0, acm_b_->InitializeReceiver())
93 << "Couldn't initialize receiver.\n";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000094
oprypin6e09d872017-08-31 03:21:39 -070095 if (FLAG_delay > 0) {
Yves Gerey665174f2018-06-19 15:03:05 +020096 ASSERT_EQ(0, acm_b_->SetMinimumPlayoutDelay(FLAG_delay))
97 << "Failed to set minimum delay.\n";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000098 }
99
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000100 int num_encoders = acm_a_->NumberOfCodecs();
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000101 CodecInst my_codec_param;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000102 for (int n = 0; n < num_encoders; n++) {
Yves Gerey665174f2018-06-19 15:03:05 +0200103 EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param)) << "Failed to get codec.";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000104 if (STR_CASE_CMP(my_codec_param.plname, "opus") == 0)
105 my_codec_param.channels = 1;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000106 else if (my_codec_param.channels > 1)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000107 continue;
108 if (STR_CASE_CMP(my_codec_param.plname, "CN") == 0 &&
109 my_codec_param.plfreq == 48000)
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000110 continue;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000111 if (STR_CASE_CMP(my_codec_param.plname, "telephone-event") == 0)
112 continue;
kwibergda2bf4e2016-10-24 13:47:09 -0700113 ASSERT_EQ(true,
114 acm_b_->RegisterReceiveCodec(my_codec_param.pltype,
115 CodecInstToSdp(my_codec_param)));
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000116 }
117
118 // Create and connect the channel
Yves Gerey665174f2018-06-19 15:03:05 +0200119 ASSERT_EQ(0, acm_a_->RegisterTransportCallback(channel_a2b_))
120 << "Couldn't register Transport callback.\n";
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000121 channel_a2b_->RegisterReceiverACM(acm_b_.get());
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000122 }
123
Yves Gerey665174f2018-06-19 15:03:05 +0200124 void Perform(const TestSettings* config,
125 size_t num_tests,
126 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");
Yves Gerey665174f2018-06-19 15:03:05 +0200137 printf(
138 "Test %d \n"
139 "Codec: %s, %d kHz, %d channel(s)\n"
140 "ACM: DTX %s, FEC %s\n"
141 "Channel: %s\n",
142 ++test_cntr_, config.codec.name, config.codec.sample_rate_hz,
143 config.codec.num_channels, config.acm.dtx ? "on" : "off",
144 config.acm.fec ? "on" : "off",
145 config.packet_loss ? "with packet-loss" : "no packet-loss");
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000146 SendCodec(config.codec);
147 ConfigAcm(config.acm);
148 ConfigChannel(config.packet_loss);
149 }
150
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000151 void SendCodec(const CodecSettings& config) {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000152 CodecInst my_codec_param;
Yves Gerey665174f2018-06-19 15:03:05 +0200153 ASSERT_EQ(
154 0, AudioCodingModule::Codec(config.name, &my_codec_param,
155 config.sample_rate_hz, config.num_channels))
156 << "Specified codec is not supported.\n";
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000157
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000158 encoding_sample_rate_hz_ = my_codec_param.plfreq;
Yves Gerey665174f2018-06-19 15:03:05 +0200159 ASSERT_EQ(0, acm_a_->RegisterSendCodec(my_codec_param))
160 << "Failed to register send-codec.\n";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000161 }
162
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000163 void ConfigAcm(const AcmSettings& config) {
Yves Gerey665174f2018-06-19 15:03:05 +0200164 ASSERT_EQ(0, acm_a_->SetVAD(config.dtx, config.dtx, VADAggr))
165 << "Failed to set VAD.\n";
166 ASSERT_EQ(0, acm_a_->SetREDStatus(config.fec)) << "Failed to set RED.\n";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000167 }
168
169 void ConfigChannel(bool packet_loss) {
170 channel_a2b_->SetFECTestWithPacketLoss(packet_loss);
171 }
172
173 void OpenOutFile(const char* output_id) {
174 std::stringstream file_stream;
oprypin6e09d872017-08-31 03:21:39 -0700175 file_stream << "delay_test_" << FLAG_codec << "_" << FLAG_sample_rate_hz
Yves Gerey665174f2018-06-19 15:03:05 +0200176 << "Hz"
177 << "_" << FLAG_delay << "ms.pcm";
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000178 std::cout << "Output file: " << file_stream.str() << std::endl << std::endl;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000179 std::string file_name = webrtc::test::OutputPath() + file_stream.str();
180 out_file_b_.Open(file_name.c_str(), 32000, "wb");
181 }
182
183 void Run(int duration_sec, const char* output_prefix) {
184 OpenOutFile(output_prefix);
185 AudioFrame audio_frame;
186 uint32_t out_freq_hz_b = out_file_b_.SamplingFrequency();
187
188 int num_frames = 0;
189 int in_file_frames = 0;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000190 uint32_t received_ts;
191 double average_delay = 0;
192 double inst_delay_sec = 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000193 while (num_frames < (duration_sec * 100)) {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000194 if (in_file_a_.EndOfFile()) {
195 in_file_a_.Rewind();
196 }
197
198 // Print delay information every 16 frame
199 if ((num_frames & 0x3F) == 0x3F) {
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000200 NetworkStatistics statistics;
201 acm_b_->GetNetworkStatistics(&statistics);
Yves Gerey665174f2018-06-19 15:03:05 +0200202 fprintf(stdout,
203 "delay: min=%3d max=%3d mean=%3d median=%3d"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000204 " ts-based average = %6.3f, "
205 "curr buff-lev = %4u opt buff-lev = %4u \n",
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000206 statistics.minWaitingTimeMs, statistics.maxWaitingTimeMs,
207 statistics.meanWaitingTimeMs, statistics.medianWaitingTimeMs,
208 average_delay, statistics.currentBufferSize,
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000209 statistics.preferredBufferSize);
Yves Gerey665174f2018-06-19 15:03:05 +0200210 fflush(stdout);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000211 }
212
213 in_file_a_.Read10MsData(audio_frame);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000214 ASSERT_GE(acm_a_->Add10MsData(audio_frame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700215 bool muted;
216 ASSERT_EQ(0,
217 acm_b_->PlayoutData10Ms(out_freq_hz_b, &audio_frame, &muted));
218 RTC_DCHECK(!muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000219 out_file_b_.Write10MsData(
yujo36b1a5f2017-06-12 12:45:32 -0700220 audio_frame.data(),
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000221 audio_frame.samples_per_channel_ * audio_frame.num_channels_);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000222 received_ts = channel_a2b_->LastInTimestamp();
Danil Chapovalovb6021232018-06-19 13:26:36 +0200223 absl::optional<uint32_t> playout_timestamp = acm_b_->PlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700224 ASSERT_TRUE(playout_timestamp);
225 inst_delay_sec = static_cast<uint32_t>(received_ts - *playout_timestamp) /
226 static_cast<double>(encoding_sample_rate_hz_);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000227
228 if (num_frames > 10)
229 average_delay = 0.95 * average_delay + 0.05 * inst_delay_sec;
230
231 ++num_frames;
232 ++in_file_frames;
233 }
234 out_file_b_.Close();
235 }
236
kwiberg37478382016-02-14 20:40:57 -0800237 std::unique_ptr<AudioCodingModule> acm_a_;
238 std::unique_ptr<AudioCodingModule> acm_b_;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000239
240 Channel* channel_a2b_;
241
242 PCMFile in_file_a_;
243 PCMFile out_file_b_;
244 int test_cntr_;
245 int encoding_sample_rate_hz_;
246};
247
andresp@webrtc.org185bae42013-05-14 08:02:25 +0000248} // namespace webrtc
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000249
250int main(int argc, char* argv[]) {
oprypin6e09d872017-08-31 03:21:39 -0700251 if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true)) {
252 return 1;
253 }
254 if (FLAG_help) {
255 rtc::FlagList::Print(nullptr, false);
256 return 0;
257 }
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000258
oprypin6e09d872017-08-31 03:21:39 -0700259 webrtc::TestSettings test_setting;
260 strcpy(test_setting.codec.name, FLAG_codec);
261
Yves Gerey665174f2018-06-19 15:03:05 +0200262 if (FLAG_sample_rate_hz != 8000 && FLAG_sample_rate_hz != 16000 &&
263 FLAG_sample_rate_hz != 32000 && FLAG_sample_rate_hz != 48000) {
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000264 std::cout << "Invalid sampling rate.\n";
265 return 1;
266 }
oprypin6e09d872017-08-31 03:21:39 -0700267 test_setting.codec.sample_rate_hz = FLAG_sample_rate_hz;
268 if (FLAG_num_channels < 1 || FLAG_num_channels > 2) {
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000269 std::cout << "Only mono and stereo are supported.\n";
270 return 1;
271 }
oprypin6e09d872017-08-31 03:21:39 -0700272 test_setting.codec.num_channels = FLAG_num_channels;
273 test_setting.acm.dtx = FLAG_dtx;
274 test_setting.acm.fec = FLAG_fec;
275 test_setting.packet_loss = FLAG_packet_loss;
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000276
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +0000277 webrtc::DelayTest delay_test;
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000278 delay_test.Initialize();
279 delay_test.Perform(&test_setting, 1, 240, "delay_test");
280 return 0;
281}