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