OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | |
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 | |
11 #include "webrtc/modules/audio_coding/main/test/TestVADDTX.h" | |
12 | |
13 #include <string> | |
14 | |
15 #include "webrtc/engine_configurations.h" | |
16 #include "webrtc/modules/audio_coding/main/test/PCMFile.h" | |
17 #include "webrtc/modules/audio_coding/main/test/utility.h" | |
18 #include "webrtc/test/testsupport/fileutils.h" | |
19 | |
20 namespace webrtc { | |
21 | |
22 #ifdef WEBRTC_CODEC_ISAC | |
23 const CodecInst kIsacWb = {103, "ISAC", 16000, 480, 1, 32000}; | |
24 const CodecInst kIsacSwb = {104, "ISAC", 32000, 960, 1, 56000}; | |
25 #endif | |
26 | |
27 #ifdef WEBRTC_CODEC_ILBC | |
28 const CodecInst kIlbc = {102, "ILBC", 8000, 240, 1, 13300}; | |
29 #endif | |
30 | |
31 #ifdef WEBRTC_CODEC_OPUS | |
32 const CodecInst kOpus = {120, "opus", 48000, 960, 1, 64000}; | |
33 const CodecInst kOpusStereo = {120, "opus", 48000, 960, 2, 64000}; | |
34 #endif | |
35 | |
36 ActivityMonitor::ActivityMonitor() { | |
37 ResetStatistics(); | |
38 } | |
39 | |
40 int32_t ActivityMonitor::InFrameType(FrameType frame_type) { | |
41 counter_[frame_type]++; | |
42 return 0; | |
43 } | |
44 | |
45 void ActivityMonitor::PrintStatistics() { | |
46 printf("\n"); | |
47 printf("kEmptyFrame %u\n", counter_[kEmptyFrame]); | |
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]); | |
52 printf("\n\n"); | |
53 } | |
54 | |
55 void ActivityMonitor::ResetStatistics() { | |
56 memset(counter_, 0, sizeof(counter_)); | |
57 } | |
58 | |
59 void ActivityMonitor::GetStatistics(uint32_t* counter) { | |
60 memcpy(counter, counter_, sizeof(counter_)); | |
61 } | |
62 | |
63 TestVadDtx::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())); | |
71 } | |
72 | |
73 void 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. | |
82 void 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"); | |
96 } | |
97 | |
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 | |
117 uint32_t stats[5]; | |
118 monitor_->GetStatistics(stats); | |
119 monitor_->ResetStatistics(); | |
120 | |
121 for (const auto& st : stats) { | |
122 int i = &st - stats; // Calculate the current position in stats. | |
123 switch (expects[i]) { | |
124 case 0: { | |
125 EXPECT_EQ(0u, st) << "stats[" << i << "] error."; | |
126 break; | |
127 } | |
128 case 1: { | |
129 EXPECT_GT(st, 0u) << "stats[" << i << "] error."; | |
130 break; | |
131 } | |
132 } | |
133 } | |
134 } | |
135 | |
136 // Following is the implementation of TestWebRtcVadDtx. | |
137 TestWebRtcVadDtx::TestWebRtcVadDtx() | |
138 : vad_enabled_(false), | |
139 dtx_enabled_(false), | |
140 output_file_num_(0) { | |
141 } | |
142 | |
143 void TestWebRtcVadDtx::Perform() { | |
144 // Go through various test cases. | |
145 #ifdef WEBRTC_CODEC_ISAC | |
146 // Register iSAC WB as send codec | |
147 RegisterCodec(kIsacWb); | |
148 RunTestCases(); | |
149 | |
150 // Register iSAC SWB as send codec | |
151 RegisterCodec(kIsacSwb); | |
152 RunTestCases(); | |
153 #endif | |
154 | |
155 #ifdef WEBRTC_CODEC_ILBC | |
156 // Register iLBC as send codec | |
157 RegisterCodec(kIlbc); | |
158 RunTestCases(); | |
159 #endif | |
160 | |
161 #ifdef WEBRTC_CODEC_OPUS | |
162 // Register Opus as send codec | |
163 RegisterCodec(kOpus); | |
164 RunTestCases(); | |
165 #endif | |
166 } | |
167 | |
168 // Test various configurations on VAD/DTX. | |
169 void TestWebRtcVadDtx::RunTestCases() { | |
170 // #1 DTX = OFF, VAD = OFF, VADNormal | |
171 SetVAD(false, false, VADNormal); | |
172 Test(true); | |
173 | |
174 // #2 DTX = ON, VAD = ON, VADAggr | |
175 SetVAD(true, true, VADAggr); | |
176 Test(false); | |
177 | |
178 // #3 DTX = ON, VAD = ON, VADLowBitrate | |
179 SetVAD(true, true, VADLowBitrate); | |
180 Test(false); | |
181 | |
182 // #4 DTX = ON, VAD = ON, VADVeryAggr | |
183 SetVAD(true, true, VADVeryAggr); | |
184 Test(false); | |
185 | |
186 // #5 DTX = ON, VAD = ON, VADNormal | |
187 SetVAD(true, true, VADNormal); | |
188 Test(false); | |
189 } | |
190 | |
191 // Set the expectation and run the test. | |
192 void TestWebRtcVadDtx::Test(bool new_outfile) { | |
193 int expects[] = {-1, 1, dtx_enabled_, 0, 0}; | |
194 if (new_outfile) { | |
195 output_file_num_++; | |
196 } | |
197 std::stringstream out_filename; | |
198 out_filename << webrtc::test::OutputPath() | |
199 << "testWebRtcVadDtx_outFile_" | |
200 << output_file_num_ | |
201 << ".pcm"; | |
202 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), | |
203 32000, 1, out_filename.str(), !new_outfile, expects); | |
204 } | |
205 | |
206 void TestWebRtcVadDtx::SetVAD(bool enable_dtx, bool enable_vad, | |
207 ACMVADMode vad_mode) { | |
208 ACMVADMode mode; | |
209 EXPECT_EQ(0, acm_send_->SetVAD(enable_dtx, enable_vad, vad_mode)); | |
210 EXPECT_EQ(0, acm_send_->VAD(&dtx_enabled_, &vad_enabled_, &mode)); | |
211 | |
212 auto codec_param = acm_send_->SendCodec(); | |
213 ASSERT_TRUE(codec_param); | |
214 if (STR_CASE_CMP(codec_param->plname, "opus") == 0) { | |
215 // If send codec is Opus, WebRTC VAD/DTX cannot be used. | |
216 enable_dtx = enable_vad = false; | |
217 } | |
218 | |
219 EXPECT_EQ(dtx_enabled_ , enable_dtx); // DTX should be set as expected. | |
220 | |
221 if (dtx_enabled_) { | |
222 EXPECT_TRUE(vad_enabled_); // WebRTC DTX cannot run without WebRTC VAD. | |
223 } else { | |
224 // Using no DTX should not affect setting of VAD. | |
225 EXPECT_EQ(enable_vad, vad_enabled_); | |
226 } | |
227 } | |
228 | |
229 // Following is the implementation of TestOpusDtx. | |
230 void TestOpusDtx::Perform() { | |
231 #ifdef WEBRTC_CODEC_ISAC | |
232 // If we set other codec than Opus, DTX cannot be toggled. | |
233 RegisterCodec(kIsacWb); | |
234 EXPECT_EQ(-1, acm_send_->EnableOpusDtx()); | |
235 EXPECT_EQ(-1, acm_send_->DisableOpusDtx()); | |
236 #endif | |
237 | |
238 #ifdef WEBRTC_CODEC_OPUS | |
239 int expects[] = {0, 1, 0, 0, 0}; | |
240 | |
241 // Register Opus as send codec | |
242 std::string out_filename = webrtc::test::OutputPath() + | |
243 "testOpusDtx_outFile_mono.pcm"; | |
244 RegisterCodec(kOpus); | |
245 EXPECT_EQ(0, acm_send_->DisableOpusDtx()); | |
246 | |
247 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), | |
248 32000, 1, out_filename, false, expects); | |
249 | |
250 EXPECT_EQ(0, acm_send_->EnableOpusDtx()); | |
251 expects[kEmptyFrame] = 1; | |
252 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), | |
253 32000, 1, out_filename, true, expects); | |
254 | |
255 // Register stereo Opus as send codec | |
256 out_filename = webrtc::test::OutputPath() + "testOpusDtx_outFile_stereo.pcm"; | |
257 RegisterCodec(kOpusStereo); | |
258 EXPECT_EQ(0, acm_send_->DisableOpusDtx()); | |
259 expects[kEmptyFrame] = 0; | |
260 Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"), | |
261 32000, 2, out_filename, false, expects); | |
262 | |
263 EXPECT_EQ(0, acm_send_->EnableOpusDtx()); | |
264 | |
265 expects[kEmptyFrame] = 1; | |
266 Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"), | |
267 32000, 2, out_filename, true, expects); | |
268 #endif | |
269 } | |
270 | |
271 } // namespace webrtc | |
OLD | NEW |