blob: ce244932c82bd1040553a8739710c69c31c686bd [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>
13
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000014#include <iostream>
kwiberg37478382016-02-14 20:40:57 -080015#include <memory>
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000016
17#include "gflags/gflags.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000018#include "webrtc/common_types.h"
kwibergda2bf4e2016-10-24 13:47:09 -070019#include "webrtc/modules/audio_coding/codecs/audio_format_conversion.h"
kjellander3e6db232015-11-26 04:44:54 -080020#include "webrtc/modules/audio_coding/include/audio_coding_module.h"
21#include "webrtc/modules/audio_coding/include/audio_coding_module_typedefs.h"
kjellander3e6db232015-11-26 04:44:54 -080022#include "webrtc/modules/audio_coding/test/Channel.h"
23#include "webrtc/modules/audio_coding/test/PCMFile.h"
24#include "webrtc/modules/audio_coding/test/utility.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010025#include "webrtc/system_wrappers/include/event_wrapper.h"
kwibergac9f8762016-09-30 22:29:43 -070026#include "webrtc/test/gtest.h"
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000027#include "webrtc/test/testsupport/fileutils.h"
henrik.lundina9a6d4b2016-12-12 05:03:02 -080028#include "webrtc/typedefs.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000029
30DEFINE_string(codec, "isac", "Codec Name");
31DEFINE_int32(sample_rate_hz, 16000, "Sampling rate in Hertz.");
32DEFINE_int32(num_channels, 1, "Number of Channels.");
33DEFINE_string(input_file, "", "Input file, PCM16 32 kHz, optional.");
34DEFINE_int32(delay, 0, "Delay in millisecond.");
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000035DEFINE_bool(dtx, false, "Enable DTX at the sender side.");
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000036DEFINE_bool(packet_loss, false, "Apply packet loss, c.f. Channel{.cc, .h}.");
37DEFINE_bool(fec, false, "Use Forward Error Correction (FEC).");
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000038
39namespace webrtc {
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000040
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000041namespace {
42
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000043struct CodecSettings {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000044 char name[50];
45 int sample_rate_hz;
46 int num_channels;
47};
48
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000049struct AcmSettings {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000050 bool dtx;
51 bool fec;
52};
53
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000054struct TestSettings {
55 CodecSettings codec;
56 AcmSettings acm;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000057 bool packet_loss;
58};
59
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000060} // namespace
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000061
62class DelayTest {
63 public:
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +000064 DelayTest()
65 : acm_a_(AudioCodingModule::Create(0)),
66 acm_b_(AudioCodingModule::Create(1)),
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000067 channel_a2b_(new Channel),
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000068 test_cntr_(0),
69 encoding_sample_rate_hz_(8000) {}
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000070
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000071 ~DelayTest() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000072 if (channel_a2b_ != NULL) {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000073 delete channel_a2b_;
74 channel_a2b_ = NULL;
75 }
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000076 in_file_a_.Close();
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000077 }
78
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000079 void Initialize() {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000080 test_cntr_ = 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000081 std::string file_name = webrtc::test::ResourcePath(
82 "audio_coding/testfile32kHz", "pcm");
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000083 if (FLAGS_input_file.size() > 0)
84 file_name = FLAGS_input_file;
85 in_file_a_.Open(file_name, 32000, "rb");
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000086 ASSERT_EQ(0, acm_a_->InitializeReceiver()) <<
87 "Couldn't initialize receiver.\n";
88 ASSERT_EQ(0, acm_b_->InitializeReceiver()) <<
89 "Couldn't initialize receiver.\n";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000090
91 if (FLAGS_delay > 0) {
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000092 ASSERT_EQ(0, acm_b_->SetMinimumPlayoutDelay(FLAGS_delay)) <<
93 "Failed to set minimum delay.\n";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000094 }
95
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000096 int num_encoders = acm_a_->NumberOfCodecs();
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000097 CodecInst my_codec_param;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000098 for (int n = 0; n < num_encoders; n++) {
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +000099 EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param)) <<
100 "Failed to get codec.";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000101 if (STR_CASE_CMP(my_codec_param.plname, "opus") == 0)
102 my_codec_param.channels = 1;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000103 else if (my_codec_param.channels > 1)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000104 continue;
105 if (STR_CASE_CMP(my_codec_param.plname, "CN") == 0 &&
106 my_codec_param.plfreq == 48000)
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000107 continue;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000108 if (STR_CASE_CMP(my_codec_param.plname, "telephone-event") == 0)
109 continue;
kwibergda2bf4e2016-10-24 13:47:09 -0700110 ASSERT_EQ(true,
111 acm_b_->RegisterReceiveCodec(my_codec_param.pltype,
112 CodecInstToSdp(my_codec_param)));
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000113 }
114
115 // Create and connect the channel
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000116 ASSERT_EQ(0, acm_a_->RegisterTransportCallback(channel_a2b_)) <<
117 "Couldn't register Transport callback.\n";
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000118 channel_a2b_->RegisterReceiverACM(acm_b_.get());
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000119 }
120
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000121 void Perform(const TestSettings* config, size_t num_tests, int duration_sec,
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000122 const char* output_prefix) {
123 for (size_t n = 0; n < num_tests; ++n) {
124 ApplyConfig(config[n]);
125 Run(duration_sec, output_prefix);
126 }
127 }
128
129 private:
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000130 void ApplyConfig(const TestSettings& config) {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000131 printf("====================================\n");
132 printf("Test %d \n"
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000133 "Codec: %s, %d kHz, %d channel(s)\n"
134 "ACM: DTX %s, FEC %s\n"
135 "Channel: %s\n",
136 ++test_cntr_, config.codec.name, config.codec.sample_rate_hz,
137 config.codec.num_channels, config.acm.dtx ? "on" : "off",
138 config.acm.fec ? "on" : "off",
139 config.packet_loss ? "with packet-loss" : "no packet-loss");
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000140 SendCodec(config.codec);
141 ConfigAcm(config.acm);
142 ConfigChannel(config.packet_loss);
143 }
144
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000145 void SendCodec(const CodecSettings& config) {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000146 CodecInst my_codec_param;
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000147 ASSERT_EQ(0, AudioCodingModule::Codec(
148 config.name, &my_codec_param, config.sample_rate_hz,
149 config.num_channels)) << "Specified codec is not supported.\n";
150
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000151 encoding_sample_rate_hz_ = my_codec_param.plfreq;
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000152 ASSERT_EQ(0, acm_a_->RegisterSendCodec(my_codec_param)) <<
153 "Failed to register send-codec.\n";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000154 }
155
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000156 void ConfigAcm(const AcmSettings& config) {
157 ASSERT_EQ(0, acm_a_->SetVAD(config.dtx, config.dtx, VADAggr)) <<
158 "Failed to set VAD.\n";
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000159 ASSERT_EQ(0, acm_a_->SetREDStatus(config.fec)) <<
160 "Failed to set RED.\n";
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000161 }
162
163 void ConfigChannel(bool packet_loss) {
164 channel_a2b_->SetFECTestWithPacketLoss(packet_loss);
165 }
166
167 void OpenOutFile(const char* output_id) {
168 std::stringstream file_stream;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000169 file_stream << "delay_test_" << FLAGS_codec << "_" << FLAGS_sample_rate_hz
henrik.lundin74f0f352015-11-01 11:43:30 -0800170 << "Hz" << "_" << FLAGS_delay << "ms.pcm";
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000171 std::cout << "Output file: " << file_stream.str() << std::endl << std::endl;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000172 std::string file_name = webrtc::test::OutputPath() + file_stream.str();
173 out_file_b_.Open(file_name.c_str(), 32000, "wb");
174 }
175
176 void Run(int duration_sec, const char* output_prefix) {
177 OpenOutFile(output_prefix);
178 AudioFrame audio_frame;
179 uint32_t out_freq_hz_b = out_file_b_.SamplingFrequency();
180
181 int num_frames = 0;
182 int in_file_frames = 0;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000183 uint32_t received_ts;
184 double average_delay = 0;
185 double inst_delay_sec = 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000186 while (num_frames < (duration_sec * 100)) {
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000187 if (in_file_a_.EndOfFile()) {
188 in_file_a_.Rewind();
189 }
190
191 // Print delay information every 16 frame
192 if ((num_frames & 0x3F) == 0x3F) {
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000193 NetworkStatistics statistics;
194 acm_b_->GetNetworkStatistics(&statistics);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000195 fprintf(stdout, "delay: min=%3d max=%3d mean=%3d median=%3d"
196 " ts-based average = %6.3f, "
197 "curr buff-lev = %4u opt buff-lev = %4u \n",
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000198 statistics.minWaitingTimeMs, statistics.maxWaitingTimeMs,
199 statistics.meanWaitingTimeMs, statistics.medianWaitingTimeMs,
200 average_delay, statistics.currentBufferSize,
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000201 statistics.preferredBufferSize);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000202 fflush (stdout);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000203 }
204
205 in_file_a_.Read10MsData(audio_frame);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000206 ASSERT_GE(acm_a_->Add10MsData(audio_frame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700207 bool muted;
208 ASSERT_EQ(0,
209 acm_b_->PlayoutData10Ms(out_freq_hz_b, &audio_frame, &muted));
210 RTC_DCHECK(!muted);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000211 out_file_b_.Write10MsData(
yujo36b1a5f2017-06-12 12:45:32 -0700212 audio_frame.data(),
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000213 audio_frame.samples_per_channel_ * audio_frame.num_channels_);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000214 received_ts = channel_a2b_->LastInTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700215 rtc::Optional<uint32_t> playout_timestamp = acm_b_->PlayoutTimestamp();
216 ASSERT_TRUE(playout_timestamp);
217 inst_delay_sec = static_cast<uint32_t>(received_ts - *playout_timestamp) /
218 static_cast<double>(encoding_sample_rate_hz_);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000219
220 if (num_frames > 10)
221 average_delay = 0.95 * average_delay + 0.05 * inst_delay_sec;
222
223 ++num_frames;
224 ++in_file_frames;
225 }
226 out_file_b_.Close();
227 }
228
kwiberg37478382016-02-14 20:40:57 -0800229 std::unique_ptr<AudioCodingModule> acm_a_;
230 std::unique_ptr<AudioCodingModule> acm_b_;
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000231
232 Channel* channel_a2b_;
233
234 PCMFile in_file_a_;
235 PCMFile out_file_b_;
236 int test_cntr_;
237 int encoding_sample_rate_hz_;
238};
239
andresp@webrtc.org185bae42013-05-14 08:02:25 +0000240} // namespace webrtc
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000241
242int main(int argc, char* argv[]) {
243 google::ParseCommandLineFlags(&argc, &argv, true);
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000244 webrtc::TestSettings test_setting;
245 strcpy(test_setting.codec.name, FLAGS_codec.c_str());
246
247 if (FLAGS_sample_rate_hz != 8000 &&
248 FLAGS_sample_rate_hz != 16000 &&
249 FLAGS_sample_rate_hz != 32000 &&
250 FLAGS_sample_rate_hz != 48000) {
251 std::cout << "Invalid sampling rate.\n";
252 return 1;
253 }
254 test_setting.codec.sample_rate_hz = FLAGS_sample_rate_hz;
255 if (FLAGS_num_channels < 1 || FLAGS_num_channels > 2) {
256 std::cout << "Only mono and stereo are supported.\n";
257 return 1;
258 }
259 test_setting.codec.num_channels = FLAGS_num_channels;
260 test_setting.acm.dtx = FLAGS_dtx;
261 test_setting.acm.fec = FLAGS_fec;
262 test_setting.packet_loss = FLAGS_packet_loss;
263
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +0000264 webrtc::DelayTest delay_test;
turaj@webrtc.org7a05ae52013-11-18 18:16:53 +0000265 delay_test.Initialize();
266 delay_test.Perform(&test_setting, 1, 240, "delay_test");
267 return 0;
268}