blob: 0aeaf2003ec02df15136214be5dc4fd1ca04bf5b [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
minyue@webrtc.org05617162015-03-03 12:02:30 +000040int32_t ActivityMonitor::InFrameType(int16_t frame_type) {
41 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");
47 printf("kActiveNormalEncoded kPassiveNormalEncoded kPassiveDTXWB ");
minyue@webrtc.org05617162015-03-03 12:02:30 +000048 printf("kPassiveDTXNB kPassiveDTXSWB kNoEncoding\n");
49 printf("%19u", counter_[1]);
50 printf("%22u", counter_[2]);
51 printf("%14u", counter_[3]);
52 printf("%14u", counter_[4]);
53 printf("%14u", counter_[5]);
54 printf("%11u", counter_[0]);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000055 printf("\n\n");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000056}
57
58void ActivityMonitor::ResetStatistics() {
minyue@webrtc.org05617162015-03-03 12:02:30 +000059 memset(counter_, 0, sizeof(counter_));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000060}
61
minyue@webrtc.org05617162015-03-03 12:02:30 +000062void ActivityMonitor::GetStatistics(uint32_t* counter) {
63 memcpy(counter, counter_, sizeof(counter_));
64}
65
66TestVadDtx::TestVadDtx()
67 : acm_send_(AudioCodingModule::Create(0)),
68 acm_receive_(AudioCodingModule::Create(1)),
69 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()));
74 assert(monitor_->kPacketTypes == this->kPacketTypes);
75}
76
77void TestVadDtx::RegisterCodec(CodecInst codec_param) {
78 // Set the codec for sending and receiving.
79 EXPECT_EQ(0, acm_send_->RegisterSendCodec(codec_param));
80 EXPECT_EQ(0, acm_receive_->RegisterReceiveCodec(codec_param));
81 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.
86void TestVadDtx::Run(std::string in_filename, int frequency, int channels,
87 std::string out_filename, bool append,
88 const int* expects) {
89 monitor_->ResetStatistics();
90
91 PCMFile in_file;
92 in_file.Open(in_filename, frequency, "rb");
93 in_file.ReadStereo(channels > 1);
94
95 PCMFile out_file;
96 if (append) {
97 out_file.Open(out_filename, kOutputFreqHz, "ab");
98 } else {
99 out_file.Open(out_filename, kOutputFreqHz, "wb");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000100 }
minyue@webrtc.org05617162015-03-03 12:02:30 +0000101
102 uint16_t frame_size_samples = in_file.PayloadLength10Ms();
103 uint32_t time_stamp = 0x12345678;
104 AudioFrame audio_frame;
105 while (!in_file.EndOfFile()) {
106 in_file.Read10MsData(audio_frame);
107 audio_frame.timestamp_ = time_stamp;
108 time_stamp += frame_size_samples;
109 EXPECT_GE(acm_send_->Add10MsData(audio_frame), 0);
110 acm_receive_->PlayoutData10Ms(kOutputFreqHz, &audio_frame);
111 out_file.Write10MsData(audio_frame);
112 }
113
114 in_file.Close();
115 out_file.Close();
116
117#ifdef PRINT_STAT
118 monitor_->PrintStatistics();
119#endif
120
121 uint32_t stats[kPacketTypes];
122 monitor_->GetStatistics(stats);
123 monitor_->ResetStatistics();
124
125 for (int i = 0; i < kPacketTypes; i++) {
126 switch (expects[i]) {
127 case 0: {
128 EXPECT_EQ(static_cast<uint32_t>(0), stats[i]) << "stats["
129 << i
130 << "] error.";
131 break;
132 }
133 case 1: {
134 EXPECT_GT(stats[i], static_cast<uint32_t>(0)) << "stats["
135 << i
136 << "] error.";
137 break;
138 }
139 }
140 }
141}
142
143// Following is the implementation of TestWebRtcVadDtx.
144TestWebRtcVadDtx::TestWebRtcVadDtx()
145 : vad_enabled_(false),
146 dtx_enabled_(false),
147 use_webrtc_dtx_(false),
148 output_file_num_(0) {
149}
150
151void 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.
177void 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.
200void TestWebRtcVadDtx::Test(bool new_outfile) {
201 int expects[kPacketTypes];
202 int frequency = acm_send_->SendFrequency();
203 expects[0] = -1; // Do not care.
204 expects[1] = 1;
205 expects[2] = vad_enabled_ && !use_webrtc_dtx_;
206 expects[3] = use_webrtc_dtx_ && (frequency == 8000);
207 expects[4] = use_webrtc_dtx_ && (frequency == 16000);
208 expects[5] = use_webrtc_dtx_ && (frequency == 32000);
209 if (new_outfile) {
210 output_file_num_++;
211 }
212 std::stringstream out_filename;
213 out_filename << webrtc::test::OutputPath()
214 << "testWebRtcVadDtx_outFile_"
215 << output_file_num_
216 << ".pcm";
217 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
218 32000, 1, out_filename.str(), !new_outfile, expects);
219}
220
221void TestWebRtcVadDtx::SetVAD(bool enable_dtx, bool enable_vad,
222 ACMVADMode vad_mode) {
223 ACMVADMode mode;
224 EXPECT_EQ(0, acm_send_->SetVAD(enable_dtx, enable_vad, vad_mode));
225 EXPECT_EQ(0, acm_send_->VAD(&dtx_enabled_, &vad_enabled_, &mode));
226
227 CodecInst codec_param;
228 acm_send_->SendCodec(&codec_param);
229 if (STR_CASE_CMP(codec_param.plname, "opus") == 0) {
230 // If send codec is Opus, WebRTC VAD/DTX cannot be used.
231 enable_dtx = enable_vad = false;
232 }
233
234 EXPECT_EQ(dtx_enabled_ , enable_dtx); // DTX should be set as expected.
235
236 bool replaced = false;
237 acm_send_->IsInternalDTXReplacedWithWebRtc(&replaced);
238
239 use_webrtc_dtx_ = dtx_enabled_ && replaced;
240
241 if (use_webrtc_dtx_) {
242 EXPECT_TRUE(vad_enabled_); // WebRTC DTX cannot run without WebRTC VAD.
243 }
244
245 if (!dtx_enabled_ || !use_webrtc_dtx_) {
246 // Using no DTX or codec Internal DTX should not affect setting of VAD.
247 EXPECT_EQ(enable_vad, vad_enabled_);
248 }
249}
250
251// Following is the implementation of TestOpusDtx.
252void TestOpusDtx::Perform() {
253#ifdef WEBRTC_CODEC_OPUS
254 int expects[kPacketTypes] = {0, 1, 0, 0, 0, 0};
255
256 // Register Opus as send codec
257 std::string out_filename = webrtc::test::OutputPath() +
258 "testOpusDtx_outFile_mono.pcm";
259 RegisterCodec(kOpus);
260 EXPECT_EQ(0, acm_send_->DisableOpusDtx());
261
262 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
263 32000, 1, out_filename, false, expects);
264
265 EXPECT_EQ(0, acm_send_->EnableOpusDtx());
266 expects[0] = 1;
267 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
268 32000, 1, out_filename, true, expects);
269
270 // Register stereo Opus as send codec
271 out_filename = webrtc::test::OutputPath() + "testOpusDtx_outFile_stereo.pcm";
272 RegisterCodec(kOpusStereo);
273 EXPECT_EQ(0, acm_send_->DisableOpusDtx());
274 expects[0] = 0;
275 Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"),
276 32000, 2, out_filename, false, expects);
277
278 // Opus DTX should only work in Voip mode.
279 EXPECT_EQ(0, acm_send_->SetOpusApplication(kVoip));
280 EXPECT_EQ(0, acm_send_->EnableOpusDtx());
281
282 expects[0] = 1;
283 Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"),
284 32000, 2, out_filename, true, expects);
285#endif
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000286}
287
288} // namespace webrtc