blob: 586563895b023b55e6bc0959ac51a4ceb8d31b6f [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"
Mirko Bonadei71207422017-09-15 13:58:09 +020020#include "typedefs.h" // NOLINT(build/include)
niklase@google.com470e71d2011-07-07 08:21:25 +000021
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000022namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
niklase@google.com470e71d2011-07-07 08:21:25 +000024#ifdef WEBRTC_CODEC_ISAC
minyue@webrtc.org05617162015-03-03 12:02:30 +000025const CodecInst kIsacWb = {103, "ISAC", 16000, 480, 1, 32000};
26const CodecInst kIsacSwb = {104, "ISAC", 32000, 960, 1, 56000};
niklase@google.com470e71d2011-07-07 08:21:25 +000027#endif
minyue@webrtc.org05617162015-03-03 12:02:30 +000028
niklase@google.com470e71d2011-07-07 08:21:25 +000029#ifdef WEBRTC_CODEC_ILBC
minyue@webrtc.org05617162015-03-03 12:02:30 +000030const CodecInst kIlbc = {102, "ILBC", 8000, 240, 1, 13300};
niklase@google.com470e71d2011-07-07 08:21:25 +000031#endif
minyue@webrtc.org05617162015-03-03 12:02:30 +000032
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000033#ifdef WEBRTC_CODEC_OPUS
minyue@webrtc.org05617162015-03-03 12:02:30 +000034const CodecInst kOpus = {120, "opus", 48000, 960, 1, 64000};
35const CodecInst kOpusStereo = {120, "opus", 48000, 960, 2, 64000};
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000036#endif
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000037
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000038ActivityMonitor::ActivityMonitor() {
minyue@webrtc.org05617162015-03-03 12:02:30 +000039 ResetStatistics();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000040}
41
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +000042int32_t ActivityMonitor::InFrameType(FrameType frame_type) {
minyue@webrtc.org05617162015-03-03 12:02:30 +000043 counter_[frame_type]++;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000044 return 0;
45}
46
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000047void ActivityMonitor::PrintStatistics() {
48 printf("\n");
pbos22993e12015-10-19 02:39:06 -070049 printf("kEmptyFrame %u\n", counter_[kEmptyFrame]);
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +000050 printf("kAudioFrameSpeech %u\n", counter_[kAudioFrameSpeech]);
51 printf("kAudioFrameCN %u\n", counter_[kAudioFrameCN]);
52 printf("kVideoFrameKey %u\n", counter_[kVideoFrameKey]);
53 printf("kVideoFrameDelta %u\n", counter_[kVideoFrameDelta]);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000054 printf("\n\n");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000055}
56
57void ActivityMonitor::ResetStatistics() {
minyue@webrtc.org05617162015-03-03 12:02:30 +000058 memset(counter_, 0, sizeof(counter_));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000059}
60
minyue@webrtc.org05617162015-03-03 12:02:30 +000061void ActivityMonitor::GetStatistics(uint32_t* counter) {
62 memcpy(counter, counter_, sizeof(counter_));
63}
64
65TestVadDtx::TestVadDtx()
Karl Wiberg5817d3d2018-04-06 10:06:42 +020066 : acm_send_(AudioCodingModule::Create(
67 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
68 acm_receive_(AudioCodingModule::Create(
69 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
minyue@webrtc.org05617162015-03-03 12:02:30 +000070 channel_(new Channel),
71 monitor_(new ActivityMonitor) {
72 EXPECT_EQ(0, acm_send_->RegisterTransportCallback(channel_.get()));
73 channel_->RegisterReceiverACM(acm_receive_.get());
74 EXPECT_EQ(0, acm_send_->RegisterVADCallback(monitor_.get()));
minyue@webrtc.org05617162015-03-03 12:02:30 +000075}
76
77void TestVadDtx::RegisterCodec(CodecInst codec_param) {
78 // Set the codec for sending and receiving.
79 EXPECT_EQ(0, acm_send_->RegisterSendCodec(codec_param));
kwibergda2bf4e2016-10-24 13:47:09 -070080 EXPECT_EQ(true, acm_receive_->RegisterReceiveCodec(
81 codec_param.pltype, CodecInstToSdp(codec_param)));
minyue@webrtc.org05617162015-03-03 12:02:30 +000082 channel_->SetIsStereo(codec_param.channels > 1);
83}
84
85// Encoding a file and see if the numbers that various packets occur follow
86// the expectation.
87void TestVadDtx::Run(std::string in_filename, int frequency, int channels,
88 std::string out_filename, bool append,
89 const int* expects) {
90 monitor_->ResetStatistics();
91
92 PCMFile in_file;
93 in_file.Open(in_filename, frequency, "rb");
94 in_file.ReadStereo(channels > 1);
Henrik Lundin4d682082015-12-10 16:24:39 +010095 // Set test length to 1000 ms (100 blocks of 10 ms each).
96 in_file.SetNum10MsBlocksToRead(100);
97 // Fast-forward both files 500 ms (50 blocks). The first second of the file is
98 // silence, but we want to keep half of that to test silence periods.
99 in_file.FastForward(50);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000100
101 PCMFile out_file;
102 if (append) {
103 out_file.Open(out_filename, kOutputFreqHz, "ab");
104 } else {
105 out_file.Open(out_filename, kOutputFreqHz, "wb");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000106 }
minyue@webrtc.org05617162015-03-03 12:02:30 +0000107
108 uint16_t frame_size_samples = in_file.PayloadLength10Ms();
minyue@webrtc.org05617162015-03-03 12:02:30 +0000109 AudioFrame audio_frame;
110 while (!in_file.EndOfFile()) {
111 in_file.Read10MsData(audio_frame);
ossu63fb95a2016-07-06 09:34:22 -0700112 audio_frame.timestamp_ = time_stamp_;
113 time_stamp_ += frame_size_samples;
minyue@webrtc.org05617162015-03-03 12:02:30 +0000114 EXPECT_GE(acm_send_->Add10MsData(audio_frame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700115 bool muted;
116 acm_receive_->PlayoutData10Ms(kOutputFreqHz, &audio_frame, &muted);
117 ASSERT_FALSE(muted);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000118 out_file.Write10MsData(audio_frame);
119 }
120
121 in_file.Close();
122 out_file.Close();
123
124#ifdef PRINT_STAT
125 monitor_->PrintStatistics();
126#endif
127
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000128 uint32_t stats[5];
minyue@webrtc.org05617162015-03-03 12:02:30 +0000129 monitor_->GetStatistics(stats);
130 monitor_->ResetStatistics();
131
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000132 for (const auto& st : stats) {
133 int i = &st - stats; // Calculate the current position in stats.
minyue@webrtc.org05617162015-03-03 12:02:30 +0000134 switch (expects[i]) {
135 case 0: {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000136 EXPECT_EQ(0u, st) << "stats[" << i << "] error.";
minyue@webrtc.org05617162015-03-03 12:02:30 +0000137 break;
138 }
139 case 1: {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000140 EXPECT_GT(st, 0u) << "stats[" << i << "] error.";
minyue@webrtc.org05617162015-03-03 12:02:30 +0000141 break;
142 }
143 }
144 }
145}
146
147// Following is the implementation of TestWebRtcVadDtx.
148TestWebRtcVadDtx::TestWebRtcVadDtx()
149 : vad_enabled_(false),
150 dtx_enabled_(false),
minyue@webrtc.org05617162015-03-03 12:02:30 +0000151 output_file_num_(0) {
152}
153
154void TestWebRtcVadDtx::Perform() {
155 // Go through various test cases.
156#ifdef WEBRTC_CODEC_ISAC
157 // Register iSAC WB as send codec
158 RegisterCodec(kIsacWb);
159 RunTestCases();
160
161 // Register iSAC SWB as send codec
162 RegisterCodec(kIsacSwb);
163 RunTestCases();
164#endif
165
166#ifdef WEBRTC_CODEC_ILBC
167 // Register iLBC as send codec
168 RegisterCodec(kIlbc);
169 RunTestCases();
170#endif
171
172#ifdef WEBRTC_CODEC_OPUS
173 // Register Opus as send codec
174 RegisterCodec(kOpus);
175 RunTestCases();
176#endif
177}
178
179// Test various configurations on VAD/DTX.
180void TestWebRtcVadDtx::RunTestCases() {
181 // #1 DTX = OFF, VAD = OFF, VADNormal
182 SetVAD(false, false, VADNormal);
183 Test(true);
184
185 // #2 DTX = ON, VAD = ON, VADAggr
186 SetVAD(true, true, VADAggr);
187 Test(false);
188
189 // #3 DTX = ON, VAD = ON, VADLowBitrate
190 SetVAD(true, true, VADLowBitrate);
191 Test(false);
192
193 // #4 DTX = ON, VAD = ON, VADVeryAggr
194 SetVAD(true, true, VADVeryAggr);
195 Test(false);
196
197 // #5 DTX = ON, VAD = ON, VADNormal
198 SetVAD(true, true, VADNormal);
199 Test(false);
200}
201
202// Set the expectation and run the test.
203void TestWebRtcVadDtx::Test(bool new_outfile) {
Karl Wibergdd00f112015-08-25 09:37:04 +0200204 int expects[] = {-1, 1, dtx_enabled_, 0, 0};
minyue@webrtc.org05617162015-03-03 12:02:30 +0000205 if (new_outfile) {
206 output_file_num_++;
207 }
208 std::stringstream out_filename;
209 out_filename << webrtc::test::OutputPath()
210 << "testWebRtcVadDtx_outFile_"
211 << output_file_num_
212 << ".pcm";
213 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
214 32000, 1, out_filename.str(), !new_outfile, expects);
215}
216
217void TestWebRtcVadDtx::SetVAD(bool enable_dtx, bool enable_vad,
218 ACMVADMode vad_mode) {
219 ACMVADMode mode;
220 EXPECT_EQ(0, acm_send_->SetVAD(enable_dtx, enable_vad, vad_mode));
221 EXPECT_EQ(0, acm_send_->VAD(&dtx_enabled_, &vad_enabled_, &mode));
222
kwiberg1fd4a4a2015-11-03 11:20:50 -0800223 auto codec_param = acm_send_->SendCodec();
224 ASSERT_TRUE(codec_param);
225 if (STR_CASE_CMP(codec_param->plname, "opus") == 0) {
minyue@webrtc.org05617162015-03-03 12:02:30 +0000226 // If send codec is Opus, WebRTC VAD/DTX cannot be used.
227 enable_dtx = enable_vad = false;
228 }
229
230 EXPECT_EQ(dtx_enabled_ , enable_dtx); // DTX should be set as expected.
231
Karl Wibergdd00f112015-08-25 09:37:04 +0200232 if (dtx_enabled_) {
minyue@webrtc.org05617162015-03-03 12:02:30 +0000233 EXPECT_TRUE(vad_enabled_); // WebRTC DTX cannot run without WebRTC VAD.
Karl Wibergdd00f112015-08-25 09:37:04 +0200234 } else {
235 // Using no DTX should not affect setting of VAD.
minyue@webrtc.org05617162015-03-03 12:02:30 +0000236 EXPECT_EQ(enable_vad, vad_enabled_);
237 }
238}
239
240// Following is the implementation of TestOpusDtx.
241void TestOpusDtx::Perform() {
minyue@webrtc.orge16bfde2015-03-12 15:28:41 +0000242#ifdef WEBRTC_CODEC_ISAC
kwiberg44307632015-12-16 06:24:05 -0800243 // If we set other codec than Opus, DTX cannot be switched on.
minyue@webrtc.orge16bfde2015-03-12 15:28:41 +0000244 RegisterCodec(kIsacWb);
Minyue Li092041c2015-05-11 12:19:35 +0200245 EXPECT_EQ(-1, acm_send_->EnableOpusDtx());
kwiberg44307632015-12-16 06:24:05 -0800246 EXPECT_EQ(0, acm_send_->DisableOpusDtx());
minyue@webrtc.orge16bfde2015-03-12 15:28:41 +0000247#endif
248
minyue@webrtc.org05617162015-03-03 12:02:30 +0000249#ifdef WEBRTC_CODEC_OPUS
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000250 int expects[] = {0, 1, 0, 0, 0};
minyue@webrtc.org05617162015-03-03 12:02:30 +0000251
252 // Register Opus as send codec
253 std::string out_filename = webrtc::test::OutputPath() +
254 "testOpusDtx_outFile_mono.pcm";
255 RegisterCodec(kOpus);
256 EXPECT_EQ(0, acm_send_->DisableOpusDtx());
257
258 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
259 32000, 1, out_filename, false, expects);
260
Minyue Li092041c2015-05-11 12:19:35 +0200261 EXPECT_EQ(0, acm_send_->EnableOpusDtx());
pbos22993e12015-10-19 02:39:06 -0700262 expects[kEmptyFrame] = 1;
Gustaf Ullberg36de62e2017-11-20 14:55:41 +0100263 expects[kAudioFrameCN] = 1;
minyue@webrtc.org05617162015-03-03 12:02:30 +0000264 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
265 32000, 1, out_filename, true, expects);
266
267 // Register stereo Opus as send codec
268 out_filename = webrtc::test::OutputPath() + "testOpusDtx_outFile_stereo.pcm";
269 RegisterCodec(kOpusStereo);
270 EXPECT_EQ(0, acm_send_->DisableOpusDtx());
pbos22993e12015-10-19 02:39:06 -0700271 expects[kEmptyFrame] = 0;
Gustaf Ullberg36de62e2017-11-20 14:55:41 +0100272 expects[kAudioFrameCN] = 0;
minyue@webrtc.org05617162015-03-03 12:02:30 +0000273 Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"),
274 32000, 2, out_filename, false, expects);
275
Minyue Li092041c2015-05-11 12:19:35 +0200276 EXPECT_EQ(0, acm_send_->EnableOpusDtx());
minyue@webrtc.org05617162015-03-03 12:02:30 +0000277
pbos22993e12015-10-19 02:39:06 -0700278 expects[kEmptyFrame] = 1;
Gustaf Ullberg36de62e2017-11-20 14:55:41 +0100279 expects[kAudioFrameCN] = 1;
minyue@webrtc.org05617162015-03-03 12:02:30 +0000280 Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"),
281 32000, 2, out_filename, true, expects);
282#endif
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000283}
284
285} // namespace webrtc