blob: e544ae31c7fca71074d0a27369b294d4f5c43d1d [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
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000011#include "webrtc/modules/audio_coding/main/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
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000015#include "webrtc/engine_configurations.h"
minyue@webrtc.org05617162015-03-03 12:02:30 +000016#include "webrtc/modules/audio_coding/main/test/PCMFile.h"
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000017#include "webrtc/modules/audio_coding/main/test/utility.h"
turaj@webrtc.org6ea3d1c2013-10-02 21:44:33 +000018#include "webrtc/test/testsupport/fileutils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000020namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000021
niklase@google.com470e71d2011-07-07 08:21:25 +000022#ifdef WEBRTC_CODEC_ISAC
minyue@webrtc.org05617162015-03-03 12:02:30 +000023const CodecInst kIsacWb = {103, "ISAC", 16000, 480, 1, 32000};
24const CodecInst kIsacSwb = {104, "ISAC", 32000, 960, 1, 56000};
niklase@google.com470e71d2011-07-07 08:21:25 +000025#endif
minyue@webrtc.org05617162015-03-03 12:02:30 +000026
niklase@google.com470e71d2011-07-07 08:21:25 +000027#ifdef WEBRTC_CODEC_ILBC
minyue@webrtc.org05617162015-03-03 12:02:30 +000028const CodecInst kIlbc = {102, "ILBC", 8000, 240, 1, 13300};
niklase@google.com470e71d2011-07-07 08:21:25 +000029#endif
minyue@webrtc.org05617162015-03-03 12:02:30 +000030
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000031#ifdef WEBRTC_CODEC_OPUS
minyue@webrtc.org05617162015-03-03 12:02:30 +000032const CodecInst kOpus = {120, "opus", 48000, 960, 1, 64000};
33const CodecInst kOpusStereo = {120, "opus", 48000, 960, 2, 64000};
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000034#endif
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000035
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000036ActivityMonitor::ActivityMonitor() {
minyue@webrtc.org05617162015-03-03 12:02:30 +000037 ResetStatistics();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000038}
39
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +000040int32_t ActivityMonitor::InFrameType(FrameType frame_type) {
minyue@webrtc.org05617162015-03-03 12:02:30 +000041 counter_[frame_type]++;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000042 return 0;
43}
44
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000045void ActivityMonitor::PrintStatistics() {
46 printf("\n");
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +000047 printf("kFrameEmpty %u\n", counter_[kFrameEmpty]);
48 printf("kAudioFrameSpeech %u\n", counter_[kAudioFrameSpeech]);
49 printf("kAudioFrameCN %u\n", counter_[kAudioFrameCN]);
50 printf("kVideoFrameKey %u\n", counter_[kVideoFrameKey]);
51 printf("kVideoFrameDelta %u\n", counter_[kVideoFrameDelta]);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000052 printf("\n\n");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000053}
54
55void ActivityMonitor::ResetStatistics() {
minyue@webrtc.org05617162015-03-03 12:02:30 +000056 memset(counter_, 0, sizeof(counter_));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000057}
58
minyue@webrtc.org05617162015-03-03 12:02:30 +000059void ActivityMonitor::GetStatistics(uint32_t* counter) {
60 memcpy(counter, counter_, sizeof(counter_));
61}
62
63TestVadDtx::TestVadDtx()
64 : acm_send_(AudioCodingModule::Create(0)),
65 acm_receive_(AudioCodingModule::Create(1)),
66 channel_(new Channel),
67 monitor_(new ActivityMonitor) {
68 EXPECT_EQ(0, acm_send_->RegisterTransportCallback(channel_.get()));
69 channel_->RegisterReceiverACM(acm_receive_.get());
70 EXPECT_EQ(0, acm_send_->RegisterVADCallback(monitor_.get()));
minyue@webrtc.org05617162015-03-03 12:02:30 +000071}
72
73void TestVadDtx::RegisterCodec(CodecInst codec_param) {
74 // Set the codec for sending and receiving.
75 EXPECT_EQ(0, acm_send_->RegisterSendCodec(codec_param));
76 EXPECT_EQ(0, acm_receive_->RegisterReceiveCodec(codec_param));
77 channel_->SetIsStereo(codec_param.channels > 1);
78}
79
80// Encoding a file and see if the numbers that various packets occur follow
81// the expectation.
82void TestVadDtx::Run(std::string in_filename, int frequency, int channels,
83 std::string out_filename, bool append,
84 const int* expects) {
85 monitor_->ResetStatistics();
86
87 PCMFile in_file;
88 in_file.Open(in_filename, frequency, "rb");
89 in_file.ReadStereo(channels > 1);
90
91 PCMFile out_file;
92 if (append) {
93 out_file.Open(out_filename, kOutputFreqHz, "ab");
94 } else {
95 out_file.Open(out_filename, kOutputFreqHz, "wb");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000096 }
minyue@webrtc.org05617162015-03-03 12:02:30 +000097
98 uint16_t frame_size_samples = in_file.PayloadLength10Ms();
99 uint32_t time_stamp = 0x12345678;
100 AudioFrame audio_frame;
101 while (!in_file.EndOfFile()) {
102 in_file.Read10MsData(audio_frame);
103 audio_frame.timestamp_ = time_stamp;
104 time_stamp += frame_size_samples;
105 EXPECT_GE(acm_send_->Add10MsData(audio_frame), 0);
106 acm_receive_->PlayoutData10Ms(kOutputFreqHz, &audio_frame);
107 out_file.Write10MsData(audio_frame);
108 }
109
110 in_file.Close();
111 out_file.Close();
112
113#ifdef PRINT_STAT
114 monitor_->PrintStatistics();
115#endif
116
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000117 uint32_t stats[5];
minyue@webrtc.org05617162015-03-03 12:02:30 +0000118 monitor_->GetStatistics(stats);
119 monitor_->ResetStatistics();
120
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000121 for (const auto& st : stats) {
122 int i = &st - stats; // Calculate the current position in stats.
minyue@webrtc.org05617162015-03-03 12:02:30 +0000123 switch (expects[i]) {
124 case 0: {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000125 EXPECT_EQ(0u, st) << "stats[" << i << "] error.";
minyue@webrtc.org05617162015-03-03 12:02:30 +0000126 break;
127 }
128 case 1: {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000129 EXPECT_GT(st, 0u) << "stats[" << i << "] error.";
minyue@webrtc.org05617162015-03-03 12:02:30 +0000130 break;
131 }
132 }
133 }
134}
135
136// Following is the implementation of TestWebRtcVadDtx.
137TestWebRtcVadDtx::TestWebRtcVadDtx()
138 : vad_enabled_(false),
139 dtx_enabled_(false),
140 use_webrtc_dtx_(false),
141 output_file_num_(0) {
142}
143
144void TestWebRtcVadDtx::Perform() {
145 // Go through various test cases.
146#ifdef WEBRTC_CODEC_ISAC
147 // Register iSAC WB as send codec
148 RegisterCodec(kIsacWb);
149 RunTestCases();
150
151 // Register iSAC SWB as send codec
152 RegisterCodec(kIsacSwb);
153 RunTestCases();
154#endif
155
156#ifdef WEBRTC_CODEC_ILBC
157 // Register iLBC as send codec
158 RegisterCodec(kIlbc);
159 RunTestCases();
160#endif
161
162#ifdef WEBRTC_CODEC_OPUS
163 // Register Opus as send codec
164 RegisterCodec(kOpus);
165 RunTestCases();
166#endif
167}
168
169// Test various configurations on VAD/DTX.
170void TestWebRtcVadDtx::RunTestCases() {
171 // #1 DTX = OFF, VAD = OFF, VADNormal
172 SetVAD(false, false, VADNormal);
173 Test(true);
174
175 // #2 DTX = ON, VAD = ON, VADAggr
176 SetVAD(true, true, VADAggr);
177 Test(false);
178
179 // #3 DTX = ON, VAD = ON, VADLowBitrate
180 SetVAD(true, true, VADLowBitrate);
181 Test(false);
182
183 // #4 DTX = ON, VAD = ON, VADVeryAggr
184 SetVAD(true, true, VADVeryAggr);
185 Test(false);
186
187 // #5 DTX = ON, VAD = ON, VADNormal
188 SetVAD(true, true, VADNormal);
189 Test(false);
190}
191
192// Set the expectation and run the test.
193void TestWebRtcVadDtx::Test(bool new_outfile) {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000194 int expects[] = {-1, 1, use_webrtc_dtx_, 0, 0};
minyue@webrtc.org05617162015-03-03 12:02:30 +0000195 if (new_outfile) {
196 output_file_num_++;
197 }
198 std::stringstream out_filename;
199 out_filename << webrtc::test::OutputPath()
200 << "testWebRtcVadDtx_outFile_"
201 << output_file_num_
202 << ".pcm";
203 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
204 32000, 1, out_filename.str(), !new_outfile, expects);
205}
206
207void TestWebRtcVadDtx::SetVAD(bool enable_dtx, bool enable_vad,
208 ACMVADMode vad_mode) {
209 ACMVADMode mode;
210 EXPECT_EQ(0, acm_send_->SetVAD(enable_dtx, enable_vad, vad_mode));
211 EXPECT_EQ(0, acm_send_->VAD(&dtx_enabled_, &vad_enabled_, &mode));
212
213 CodecInst codec_param;
214 acm_send_->SendCodec(&codec_param);
215 if (STR_CASE_CMP(codec_param.plname, "opus") == 0) {
216 // If send codec is Opus, WebRTC VAD/DTX cannot be used.
217 enable_dtx = enable_vad = false;
218 }
219
220 EXPECT_EQ(dtx_enabled_ , enable_dtx); // DTX should be set as expected.
221
222 bool replaced = false;
223 acm_send_->IsInternalDTXReplacedWithWebRtc(&replaced);
224
225 use_webrtc_dtx_ = dtx_enabled_ && replaced;
226
227 if (use_webrtc_dtx_) {
228 EXPECT_TRUE(vad_enabled_); // WebRTC DTX cannot run without WebRTC VAD.
229 }
230
231 if (!dtx_enabled_ || !use_webrtc_dtx_) {
232 // Using no DTX or codec Internal DTX should not affect setting of VAD.
233 EXPECT_EQ(enable_vad, vad_enabled_);
234 }
235}
236
237// Following is the implementation of TestOpusDtx.
238void TestOpusDtx::Perform() {
minyue@webrtc.orge16bfde2015-03-12 15:28:41 +0000239#ifdef WEBRTC_CODEC_ISAC
240 // If we set other codec than Opus, DTX cannot be toggled.
241 RegisterCodec(kIsacWb);
242 EXPECT_EQ(-1, acm_send_->EnableOpusDtx(false));
243 EXPECT_EQ(-1, acm_send_->DisableOpusDtx());
244#endif
245
minyue@webrtc.org05617162015-03-03 12:02:30 +0000246#ifdef WEBRTC_CODEC_OPUS
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000247 int expects[] = {0, 1, 0, 0, 0};
minyue@webrtc.org05617162015-03-03 12:02:30 +0000248
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@webrtc.orge16bfde2015-03-12 15:28:41 +0000258 EXPECT_EQ(0, acm_send_->EnableOpusDtx(false));
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000259 expects[kFrameEmpty] = 1;
minyue@webrtc.org05617162015-03-03 12:02:30 +0000260 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());
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000267 expects[kFrameEmpty] = 0;
minyue@webrtc.org05617162015-03-03 12:02:30 +0000268 Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"),
269 32000, 2, out_filename, false, expects);
270
minyue@webrtc.orge16bfde2015-03-12 15:28:41 +0000271 // Opus should be now in kAudio mode. Opus DTX should not be set without
272 // forcing kVoip mode.
273 EXPECT_EQ(-1, acm_send_->EnableOpusDtx(false));
274 EXPECT_EQ(0, acm_send_->EnableOpusDtx(true));
minyue@webrtc.org05617162015-03-03 12:02:30 +0000275
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000276 expects[kFrameEmpty] = 1;
minyue@webrtc.org05617162015-03-03 12:02:30 +0000277 Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"),
278 32000, 2, out_filename, true, expects);
279#endif
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000280}
281
282} // namespace webrtc