niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/test/TestVADDTX.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 13 | #include <string> |
kjellander@webrtc.org | 5490c71 | 2011-12-21 13:34:18 +0000 | [diff] [blame] | 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "modules/audio_coding/codecs/audio_format_conversion.h" |
| 16 | #include "modules/audio_coding/test/PCMFile.h" |
| 17 | #include "modules/audio_coding/test/utility.h" |
| 18 | #include "test/testsupport/fileutils.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame^] | 19 | #include "typedefs.h" // NOLINT(build/include) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 21 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | #ifdef WEBRTC_CODEC_ISAC |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 24 | const CodecInst kIsacWb = {103, "ISAC", 16000, 480, 1, 32000}; |
| 25 | const CodecInst kIsacSwb = {104, "ISAC", 32000, 960, 1, 56000}; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | #endif |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 27 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | #ifdef WEBRTC_CODEC_ILBC |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 29 | const CodecInst kIlbc = {102, "ILBC", 8000, 240, 1, 13300}; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | #endif |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 31 | |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 32 | #ifdef WEBRTC_CODEC_OPUS |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 33 | const CodecInst kOpus = {120, "opus", 48000, 960, 1, 64000}; |
| 34 | const CodecInst kOpusStereo = {120, "opus", 48000, 960, 2, 64000}; |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 35 | #endif |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 36 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 37 | ActivityMonitor::ActivityMonitor() { |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 38 | ResetStatistics(); |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 39 | } |
| 40 | |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 41 | int32_t ActivityMonitor::InFrameType(FrameType frame_type) { |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 42 | counter_[frame_type]++; |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
tina.legrand@webrtc.org | ee92b66 | 2013-08-27 07:33:51 +0000 | [diff] [blame] | 46 | void ActivityMonitor::PrintStatistics() { |
| 47 | printf("\n"); |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 48 | printf("kEmptyFrame %u\n", counter_[kEmptyFrame]); |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 49 | printf("kAudioFrameSpeech %u\n", counter_[kAudioFrameSpeech]); |
| 50 | printf("kAudioFrameCN %u\n", counter_[kAudioFrameCN]); |
| 51 | printf("kVideoFrameKey %u\n", counter_[kVideoFrameKey]); |
| 52 | printf("kVideoFrameDelta %u\n", counter_[kVideoFrameDelta]); |
tina.legrand@webrtc.org | ee92b66 | 2013-08-27 07:33:51 +0000 | [diff] [blame] | 53 | printf("\n\n"); |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void ActivityMonitor::ResetStatistics() { |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 57 | memset(counter_, 0, sizeof(counter_)); |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 58 | } |
| 59 | |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 60 | void ActivityMonitor::GetStatistics(uint32_t* counter) { |
| 61 | memcpy(counter, counter_, sizeof(counter_)); |
| 62 | } |
| 63 | |
| 64 | TestVadDtx::TestVadDtx() |
| 65 | : acm_send_(AudioCodingModule::Create(0)), |
| 66 | acm_receive_(AudioCodingModule::Create(1)), |
| 67 | channel_(new Channel), |
| 68 | monitor_(new ActivityMonitor) { |
| 69 | EXPECT_EQ(0, acm_send_->RegisterTransportCallback(channel_.get())); |
| 70 | channel_->RegisterReceiverACM(acm_receive_.get()); |
| 71 | EXPECT_EQ(0, acm_send_->RegisterVADCallback(monitor_.get())); |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void TestVadDtx::RegisterCodec(CodecInst codec_param) { |
| 75 | // Set the codec for sending and receiving. |
| 76 | EXPECT_EQ(0, acm_send_->RegisterSendCodec(codec_param)); |
kwiberg | da2bf4e | 2016-10-24 13:47:09 -0700 | [diff] [blame] | 77 | EXPECT_EQ(true, acm_receive_->RegisterReceiveCodec( |
| 78 | codec_param.pltype, CodecInstToSdp(codec_param))); |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 79 | channel_->SetIsStereo(codec_param.channels > 1); |
| 80 | } |
| 81 | |
| 82 | // Encoding a file and see if the numbers that various packets occur follow |
| 83 | // the expectation. |
| 84 | void TestVadDtx::Run(std::string in_filename, int frequency, int channels, |
| 85 | std::string out_filename, bool append, |
| 86 | const int* expects) { |
| 87 | monitor_->ResetStatistics(); |
| 88 | |
| 89 | PCMFile in_file; |
| 90 | in_file.Open(in_filename, frequency, "rb"); |
| 91 | in_file.ReadStereo(channels > 1); |
Henrik Lundin | 4d68208 | 2015-12-10 16:24:39 +0100 | [diff] [blame] | 92 | // Set test length to 1000 ms (100 blocks of 10 ms each). |
| 93 | in_file.SetNum10MsBlocksToRead(100); |
| 94 | // Fast-forward both files 500 ms (50 blocks). The first second of the file is |
| 95 | // silence, but we want to keep half of that to test silence periods. |
| 96 | in_file.FastForward(50); |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 97 | |
| 98 | PCMFile out_file; |
| 99 | if (append) { |
| 100 | out_file.Open(out_filename, kOutputFreqHz, "ab"); |
| 101 | } else { |
| 102 | out_file.Open(out_filename, kOutputFreqHz, "wb"); |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 103 | } |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 104 | |
| 105 | uint16_t frame_size_samples = in_file.PayloadLength10Ms(); |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 106 | AudioFrame audio_frame; |
| 107 | while (!in_file.EndOfFile()) { |
| 108 | in_file.Read10MsData(audio_frame); |
ossu | 63fb95a | 2016-07-06 09:34:22 -0700 | [diff] [blame] | 109 | audio_frame.timestamp_ = time_stamp_; |
| 110 | time_stamp_ += frame_size_samples; |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 111 | EXPECT_GE(acm_send_->Add10MsData(audio_frame), 0); |
henrik.lundin | d4ccb00 | 2016-05-17 12:21:55 -0700 | [diff] [blame] | 112 | bool muted; |
| 113 | acm_receive_->PlayoutData10Ms(kOutputFreqHz, &audio_frame, &muted); |
| 114 | ASSERT_FALSE(muted); |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 115 | out_file.Write10MsData(audio_frame); |
| 116 | } |
| 117 | |
| 118 | in_file.Close(); |
| 119 | out_file.Close(); |
| 120 | |
| 121 | #ifdef PRINT_STAT |
| 122 | monitor_->PrintStatistics(); |
| 123 | #endif |
| 124 | |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 125 | uint32_t stats[5]; |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 126 | monitor_->GetStatistics(stats); |
| 127 | monitor_->ResetStatistics(); |
| 128 | |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 129 | for (const auto& st : stats) { |
| 130 | int i = &st - stats; // Calculate the current position in stats. |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 131 | switch (expects[i]) { |
| 132 | case 0: { |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 133 | EXPECT_EQ(0u, st) << "stats[" << i << "] error."; |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | case 1: { |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 137 | EXPECT_GT(st, 0u) << "stats[" << i << "] error."; |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 138 | break; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Following is the implementation of TestWebRtcVadDtx. |
| 145 | TestWebRtcVadDtx::TestWebRtcVadDtx() |
| 146 | : vad_enabled_(false), |
| 147 | dtx_enabled_(false), |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 148 | output_file_num_(0) { |
| 149 | } |
| 150 | |
| 151 | void TestWebRtcVadDtx::Perform() { |
| 152 | // Go through various test cases. |
| 153 | #ifdef WEBRTC_CODEC_ISAC |
| 154 | // Register iSAC WB as send codec |
| 155 | RegisterCodec(kIsacWb); |
| 156 | RunTestCases(); |
| 157 | |
| 158 | // Register iSAC SWB as send codec |
| 159 | RegisterCodec(kIsacSwb); |
| 160 | RunTestCases(); |
| 161 | #endif |
| 162 | |
| 163 | #ifdef WEBRTC_CODEC_ILBC |
| 164 | // Register iLBC as send codec |
| 165 | RegisterCodec(kIlbc); |
| 166 | RunTestCases(); |
| 167 | #endif |
| 168 | |
| 169 | #ifdef WEBRTC_CODEC_OPUS |
| 170 | // Register Opus as send codec |
| 171 | RegisterCodec(kOpus); |
| 172 | RunTestCases(); |
| 173 | #endif |
| 174 | } |
| 175 | |
| 176 | // Test various configurations on VAD/DTX. |
| 177 | void TestWebRtcVadDtx::RunTestCases() { |
| 178 | // #1 DTX = OFF, VAD = OFF, VADNormal |
| 179 | SetVAD(false, false, VADNormal); |
| 180 | Test(true); |
| 181 | |
| 182 | // #2 DTX = ON, VAD = ON, VADAggr |
| 183 | SetVAD(true, true, VADAggr); |
| 184 | Test(false); |
| 185 | |
| 186 | // #3 DTX = ON, VAD = ON, VADLowBitrate |
| 187 | SetVAD(true, true, VADLowBitrate); |
| 188 | Test(false); |
| 189 | |
| 190 | // #4 DTX = ON, VAD = ON, VADVeryAggr |
| 191 | SetVAD(true, true, VADVeryAggr); |
| 192 | Test(false); |
| 193 | |
| 194 | // #5 DTX = ON, VAD = ON, VADNormal |
| 195 | SetVAD(true, true, VADNormal); |
| 196 | Test(false); |
| 197 | } |
| 198 | |
| 199 | // Set the expectation and run the test. |
| 200 | void TestWebRtcVadDtx::Test(bool new_outfile) { |
Karl Wiberg | dd00f11 | 2015-08-25 09:37:04 +0200 | [diff] [blame] | 201 | int expects[] = {-1, 1, dtx_enabled_, 0, 0}; |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 202 | if (new_outfile) { |
| 203 | output_file_num_++; |
| 204 | } |
| 205 | std::stringstream out_filename; |
| 206 | out_filename << webrtc::test::OutputPath() |
| 207 | << "testWebRtcVadDtx_outFile_" |
| 208 | << output_file_num_ |
| 209 | << ".pcm"; |
| 210 | Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), |
| 211 | 32000, 1, out_filename.str(), !new_outfile, expects); |
| 212 | } |
| 213 | |
| 214 | void TestWebRtcVadDtx::SetVAD(bool enable_dtx, bool enable_vad, |
| 215 | ACMVADMode vad_mode) { |
| 216 | ACMVADMode mode; |
| 217 | EXPECT_EQ(0, acm_send_->SetVAD(enable_dtx, enable_vad, vad_mode)); |
| 218 | EXPECT_EQ(0, acm_send_->VAD(&dtx_enabled_, &vad_enabled_, &mode)); |
| 219 | |
kwiberg | 1fd4a4a | 2015-11-03 11:20:50 -0800 | [diff] [blame] | 220 | auto codec_param = acm_send_->SendCodec(); |
| 221 | ASSERT_TRUE(codec_param); |
| 222 | if (STR_CASE_CMP(codec_param->plname, "opus") == 0) { |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 223 | // If send codec is Opus, WebRTC VAD/DTX cannot be used. |
| 224 | enable_dtx = enable_vad = false; |
| 225 | } |
| 226 | |
| 227 | EXPECT_EQ(dtx_enabled_ , enable_dtx); // DTX should be set as expected. |
| 228 | |
Karl Wiberg | dd00f11 | 2015-08-25 09:37:04 +0200 | [diff] [blame] | 229 | if (dtx_enabled_) { |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 230 | EXPECT_TRUE(vad_enabled_); // WebRTC DTX cannot run without WebRTC VAD. |
Karl Wiberg | dd00f11 | 2015-08-25 09:37:04 +0200 | [diff] [blame] | 231 | } else { |
| 232 | // Using no DTX should not affect setting of VAD. |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 233 | EXPECT_EQ(enable_vad, vad_enabled_); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // Following is the implementation of TestOpusDtx. |
| 238 | void TestOpusDtx::Perform() { |
minyue@webrtc.org | e16bfde | 2015-03-12 15:28:41 +0000 | [diff] [blame] | 239 | #ifdef WEBRTC_CODEC_ISAC |
kwiberg | 4430763 | 2015-12-16 06:24:05 -0800 | [diff] [blame] | 240 | // If we set other codec than Opus, DTX cannot be switched on. |
minyue@webrtc.org | e16bfde | 2015-03-12 15:28:41 +0000 | [diff] [blame] | 241 | RegisterCodec(kIsacWb); |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 242 | EXPECT_EQ(-1, acm_send_->EnableOpusDtx()); |
kwiberg | 4430763 | 2015-12-16 06:24:05 -0800 | [diff] [blame] | 243 | EXPECT_EQ(0, acm_send_->DisableOpusDtx()); |
minyue@webrtc.org | e16bfde | 2015-03-12 15:28:41 +0000 | [diff] [blame] | 244 | #endif |
| 245 | |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 246 | #ifdef WEBRTC_CODEC_OPUS |
henrik.lundin@webrtc.org | e9217b4 | 2015-03-06 07:50:34 +0000 | [diff] [blame] | 247 | int expects[] = {0, 1, 0, 0, 0}; |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 248 | |
| 249 | // Register Opus as send codec |
| 250 | std::string out_filename = webrtc::test::OutputPath() + |
| 251 | "testOpusDtx_outFile_mono.pcm"; |
| 252 | RegisterCodec(kOpus); |
| 253 | EXPECT_EQ(0, acm_send_->DisableOpusDtx()); |
| 254 | |
| 255 | Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), |
| 256 | 32000, 1, out_filename, false, expects); |
| 257 | |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 258 | EXPECT_EQ(0, acm_send_->EnableOpusDtx()); |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 259 | expects[kEmptyFrame] = 1; |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 260 | Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), |
| 261 | 32000, 1, out_filename, true, expects); |
| 262 | |
| 263 | // Register stereo Opus as send codec |
| 264 | out_filename = webrtc::test::OutputPath() + "testOpusDtx_outFile_stereo.pcm"; |
| 265 | RegisterCodec(kOpusStereo); |
| 266 | EXPECT_EQ(0, acm_send_->DisableOpusDtx()); |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 267 | expects[kEmptyFrame] = 0; |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 268 | Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"), |
| 269 | 32000, 2, out_filename, false, expects); |
| 270 | |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 271 | EXPECT_EQ(0, acm_send_->EnableOpusDtx()); |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 272 | |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 273 | expects[kEmptyFrame] = 1; |
minyue@webrtc.org | 0561716 | 2015-03-03 12:02:30 +0000 | [diff] [blame] | 274 | Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"), |
| 275 | 32000, 2, out_filename, true, expects); |
| 276 | #endif |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | } // namespace webrtc |