blob: 79b2d31b9205af13e87d22bac1a54effc44f62f7 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/test/TestVADDTX.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
minyue@webrtc.org05617162015-03-03 12:02:30 +000013#include <string>
kjellander@webrtc.org5490c712011-12-21 13:34:18 +000014
Karl Wiberg5817d3d2018-04-06 10:06:42 +020015#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_coding/codecs/audio_format_conversion.h"
17#include "modules/audio_coding/test/PCMFile.h"
18#include "modules/audio_coding/test/utility.h"
19#include "test/testsupport/fileutils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000021namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000022
niklase@google.com470e71d2011-07-07 08:21:25 +000023#ifdef WEBRTC_CODEC_ISAC
minyue@webrtc.org05617162015-03-03 12:02:30 +000024const CodecInst kIsacWb = {103, "ISAC", 16000, 480, 1, 32000};
25const CodecInst kIsacSwb = {104, "ISAC", 32000, 960, 1, 56000};
niklase@google.com470e71d2011-07-07 08:21:25 +000026#endif
minyue@webrtc.org05617162015-03-03 12:02:30 +000027
niklase@google.com470e71d2011-07-07 08:21:25 +000028#ifdef WEBRTC_CODEC_ILBC
minyue@webrtc.org05617162015-03-03 12:02:30 +000029const CodecInst kIlbc = {102, "ILBC", 8000, 240, 1, 13300};
niklase@google.com470e71d2011-07-07 08:21:25 +000030#endif
minyue@webrtc.org05617162015-03-03 12:02:30 +000031
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000032#ifdef WEBRTC_CODEC_OPUS
minyue@webrtc.org05617162015-03-03 12:02:30 +000033const CodecInst kOpus = {120, "opus", 48000, 960, 1, 64000};
34const CodecInst kOpusStereo = {120, "opus", 48000, 960, 2, 64000};
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000035#endif
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000036
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000037ActivityMonitor::ActivityMonitor() {
minyue@webrtc.org05617162015-03-03 12:02:30 +000038 ResetStatistics();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000039}
40
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +000041int32_t ActivityMonitor::InFrameType(FrameType frame_type) {
minyue@webrtc.org05617162015-03-03 12:02:30 +000042 counter_[frame_type]++;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000043 return 0;
44}
45
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000046void ActivityMonitor::PrintStatistics() {
47 printf("\n");
pbos22993e12015-10-19 02:39:06 -070048 printf("kEmptyFrame %u\n", counter_[kEmptyFrame]);
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +000049 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.orgee92b662013-08-27 07:33:51 +000053 printf("\n\n");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000054}
55
56void ActivityMonitor::ResetStatistics() {
minyue@webrtc.org05617162015-03-03 12:02:30 +000057 memset(counter_, 0, sizeof(counter_));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000058}
59
minyue@webrtc.org05617162015-03-03 12:02:30 +000060void ActivityMonitor::GetStatistics(uint32_t* counter) {
61 memcpy(counter, counter_, sizeof(counter_));
62}
63
64TestVadDtx::TestVadDtx()
Karl Wiberg5817d3d2018-04-06 10:06:42 +020065 : acm_send_(AudioCodingModule::Create(
66 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
67 acm_receive_(AudioCodingModule::Create(
68 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
minyue@webrtc.org05617162015-03-03 12:02:30 +000069 channel_(new Channel),
70 monitor_(new ActivityMonitor) {
71 EXPECT_EQ(0, acm_send_->RegisterTransportCallback(channel_.get()));
72 channel_->RegisterReceiverACM(acm_receive_.get());
73 EXPECT_EQ(0, acm_send_->RegisterVADCallback(monitor_.get()));
minyue@webrtc.org05617162015-03-03 12:02:30 +000074}
75
76void TestVadDtx::RegisterCodec(CodecInst codec_param) {
77 // Set the codec for sending and receiving.
78 EXPECT_EQ(0, acm_send_->RegisterSendCodec(codec_param));
kwibergda2bf4e2016-10-24 13:47:09 -070079 EXPECT_EQ(true, acm_receive_->RegisterReceiveCodec(
80 codec_param.pltype, CodecInstToSdp(codec_param)));
minyue@webrtc.org05617162015-03-03 12:02:30 +000081 channel_->SetIsStereo(codec_param.channels > 1);
82}
83
84// Encoding a file and see if the numbers that various packets occur follow
85// the expectation.
Yves Gerey665174f2018-06-19 15:03:05 +020086void TestVadDtx::Run(std::string in_filename,
87 int frequency,
88 int channels,
89 std::string out_filename,
90 bool append,
minyue@webrtc.org05617162015-03-03 12:02:30 +000091 const int* expects) {
92 monitor_->ResetStatistics();
93
94 PCMFile in_file;
95 in_file.Open(in_filename, frequency, "rb");
96 in_file.ReadStereo(channels > 1);
Henrik Lundin4d682082015-12-10 16:24:39 +010097 // Set test length to 1000 ms (100 blocks of 10 ms each).
98 in_file.SetNum10MsBlocksToRead(100);
99 // Fast-forward both files 500 ms (50 blocks). The first second of the file is
100 // silence, but we want to keep half of that to test silence periods.
101 in_file.FastForward(50);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000102
103 PCMFile out_file;
104 if (append) {
105 out_file.Open(out_filename, kOutputFreqHz, "ab");
106 } else {
107 out_file.Open(out_filename, kOutputFreqHz, "wb");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000108 }
minyue@webrtc.org05617162015-03-03 12:02:30 +0000109
110 uint16_t frame_size_samples = in_file.PayloadLength10Ms();
minyue@webrtc.org05617162015-03-03 12:02:30 +0000111 AudioFrame audio_frame;
112 while (!in_file.EndOfFile()) {
113 in_file.Read10MsData(audio_frame);
ossu63fb95a2016-07-06 09:34:22 -0700114 audio_frame.timestamp_ = time_stamp_;
115 time_stamp_ += frame_size_samples;
minyue@webrtc.org05617162015-03-03 12:02:30 +0000116 EXPECT_GE(acm_send_->Add10MsData(audio_frame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700117 bool muted;
118 acm_receive_->PlayoutData10Ms(kOutputFreqHz, &audio_frame, &muted);
119 ASSERT_FALSE(muted);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000120 out_file.Write10MsData(audio_frame);
121 }
122
123 in_file.Close();
124 out_file.Close();
125
126#ifdef PRINT_STAT
127 monitor_->PrintStatistics();
128#endif
129
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000130 uint32_t stats[5];
minyue@webrtc.org05617162015-03-03 12:02:30 +0000131 monitor_->GetStatistics(stats);
132 monitor_->ResetStatistics();
133
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000134 for (const auto& st : stats) {
135 int i = &st - stats; // Calculate the current position in stats.
minyue@webrtc.org05617162015-03-03 12:02:30 +0000136 switch (expects[i]) {
137 case 0: {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000138 EXPECT_EQ(0u, st) << "stats[" << i << "] error.";
minyue@webrtc.org05617162015-03-03 12:02:30 +0000139 break;
140 }
141 case 1: {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000142 EXPECT_GT(st, 0u) << "stats[" << i << "] error.";
minyue@webrtc.org05617162015-03-03 12:02:30 +0000143 break;
144 }
145 }
146 }
147}
148
149// Following is the implementation of TestWebRtcVadDtx.
150TestWebRtcVadDtx::TestWebRtcVadDtx()
Yves Gerey665174f2018-06-19 15:03:05 +0200151 : vad_enabled_(false), dtx_enabled_(false), output_file_num_(0) {}
minyue@webrtc.org05617162015-03-03 12:02:30 +0000152
153void TestWebRtcVadDtx::Perform() {
Yves Gerey665174f2018-06-19 15:03:05 +0200154// Go through various test cases.
minyue@webrtc.org05617162015-03-03 12:02:30 +0000155#ifdef WEBRTC_CODEC_ISAC
156 // Register iSAC WB as send codec
157 RegisterCodec(kIsacWb);
158 RunTestCases();
159
160 // Register iSAC SWB as send codec
161 RegisterCodec(kIsacSwb);
162 RunTestCases();
163#endif
164
165#ifdef WEBRTC_CODEC_ILBC
166 // Register iLBC as send codec
167 RegisterCodec(kIlbc);
168 RunTestCases();
169#endif
170
171#ifdef WEBRTC_CODEC_OPUS
172 // Register Opus as send codec
173 RegisterCodec(kOpus);
174 RunTestCases();
175#endif
176}
177
178// Test various configurations on VAD/DTX.
179void TestWebRtcVadDtx::RunTestCases() {
180 // #1 DTX = OFF, VAD = OFF, VADNormal
181 SetVAD(false, false, VADNormal);
182 Test(true);
183
184 // #2 DTX = ON, VAD = ON, VADAggr
185 SetVAD(true, true, VADAggr);
186 Test(false);
187
188 // #3 DTX = ON, VAD = ON, VADLowBitrate
189 SetVAD(true, true, VADLowBitrate);
190 Test(false);
191
192 // #4 DTX = ON, VAD = ON, VADVeryAggr
193 SetVAD(true, true, VADVeryAggr);
194 Test(false);
195
196 // #5 DTX = ON, VAD = ON, VADNormal
197 SetVAD(true, true, VADNormal);
198 Test(false);
199}
200
201// Set the expectation and run the test.
202void TestWebRtcVadDtx::Test(bool new_outfile) {
Karl Wibergdd00f112015-08-25 09:37:04 +0200203 int expects[] = {-1, 1, dtx_enabled_, 0, 0};
minyue@webrtc.org05617162015-03-03 12:02:30 +0000204 if (new_outfile) {
205 output_file_num_++;
206 }
207 std::stringstream out_filename;
Yves Gerey665174f2018-06-19 15:03:05 +0200208 out_filename << webrtc::test::OutputPath() << "testWebRtcVadDtx_outFile_"
209 << output_file_num_ << ".pcm";
210 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 32000, 1,
211 out_filename.str(), !new_outfile, expects);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000212}
213
Yves Gerey665174f2018-06-19 15:03:05 +0200214void TestWebRtcVadDtx::SetVAD(bool enable_dtx,
215 bool enable_vad,
minyue@webrtc.org05617162015-03-03 12:02:30 +0000216 ACMVADMode vad_mode) {
217 ACMVADMode mode;
218 EXPECT_EQ(0, acm_send_->SetVAD(enable_dtx, enable_vad, vad_mode));
219 EXPECT_EQ(0, acm_send_->VAD(&dtx_enabled_, &vad_enabled_, &mode));
220
kwiberg1fd4a4a2015-11-03 11:20:50 -0800221 auto codec_param = acm_send_->SendCodec();
222 ASSERT_TRUE(codec_param);
223 if (STR_CASE_CMP(codec_param->plname, "opus") == 0) {
minyue@webrtc.org05617162015-03-03 12:02:30 +0000224 // If send codec is Opus, WebRTC VAD/DTX cannot be used.
225 enable_dtx = enable_vad = false;
226 }
227
Yves Gerey665174f2018-06-19 15:03:05 +0200228 EXPECT_EQ(dtx_enabled_, enable_dtx); // DTX should be set as expected.
minyue@webrtc.org05617162015-03-03 12:02:30 +0000229
Karl Wibergdd00f112015-08-25 09:37:04 +0200230 if (dtx_enabled_) {
Yves Gerey665174f2018-06-19 15:03:05 +0200231 EXPECT_TRUE(vad_enabled_); // WebRTC DTX cannot run without WebRTC VAD.
Karl Wibergdd00f112015-08-25 09:37:04 +0200232 } else {
233 // Using no DTX should not affect setting of VAD.
minyue@webrtc.org05617162015-03-03 12:02:30 +0000234 EXPECT_EQ(enable_vad, vad_enabled_);
235 }
236}
237
238// Following is the implementation of TestOpusDtx.
239void TestOpusDtx::Perform() {
minyue@webrtc.orge16bfde2015-03-12 15:28:41 +0000240#ifdef WEBRTC_CODEC_ISAC
kwiberg44307632015-12-16 06:24:05 -0800241 // If we set other codec than Opus, DTX cannot be switched on.
minyue@webrtc.orge16bfde2015-03-12 15:28:41 +0000242 RegisterCodec(kIsacWb);
Minyue Li092041c2015-05-11 12:19:35 +0200243 EXPECT_EQ(-1, acm_send_->EnableOpusDtx());
kwiberg44307632015-12-16 06:24:05 -0800244 EXPECT_EQ(0, acm_send_->DisableOpusDtx());
minyue@webrtc.orge16bfde2015-03-12 15:28:41 +0000245#endif
246
minyue@webrtc.org05617162015-03-03 12:02:30 +0000247#ifdef WEBRTC_CODEC_OPUS
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000248 int expects[] = {0, 1, 0, 0, 0};
minyue@webrtc.org05617162015-03-03 12:02:30 +0000249
250 // Register Opus as send codec
Yves Gerey665174f2018-06-19 15:03:05 +0200251 std::string out_filename =
252 webrtc::test::OutputPath() + "testOpusDtx_outFile_mono.pcm";
minyue@webrtc.org05617162015-03-03 12:02:30 +0000253 RegisterCodec(kOpus);
254 EXPECT_EQ(0, acm_send_->DisableOpusDtx());
255
Yves Gerey665174f2018-06-19 15:03:05 +0200256 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 32000, 1,
257 out_filename, false, expects);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000258
Minyue Li092041c2015-05-11 12:19:35 +0200259 EXPECT_EQ(0, acm_send_->EnableOpusDtx());
pbos22993e12015-10-19 02:39:06 -0700260 expects[kEmptyFrame] = 1;
Gustaf Ullberg36de62e2017-11-20 14:55:41 +0100261 expects[kAudioFrameCN] = 1;
Yves Gerey665174f2018-06-19 15:03:05 +0200262 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 32000, 1,
263 out_filename, true, expects);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000264
265 // Register stereo Opus as send codec
266 out_filename = webrtc::test::OutputPath() + "testOpusDtx_outFile_stereo.pcm";
267 RegisterCodec(kOpusStereo);
268 EXPECT_EQ(0, acm_send_->DisableOpusDtx());
pbos22993e12015-10-19 02:39:06 -0700269 expects[kEmptyFrame] = 0;
Gustaf Ullberg36de62e2017-11-20 14:55:41 +0100270 expects[kAudioFrameCN] = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200271 Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"), 32000,
272 2, out_filename, false, expects);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000273
Minyue Li092041c2015-05-11 12:19:35 +0200274 EXPECT_EQ(0, acm_send_->EnableOpusDtx());
minyue@webrtc.org05617162015-03-03 12:02:30 +0000275
pbos22993e12015-10-19 02:39:06 -0700276 expects[kEmptyFrame] = 1;
Gustaf Ullberg36de62e2017-11-20 14:55:41 +0100277 expects[kAudioFrameCN] = 1;
Yves Gerey665174f2018-06-19 15:03:05 +0200278 Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"), 32000,
279 2, out_filename, true, expects);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000280#endif
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000281}
282
283} // namespace webrtc