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 "modules/audio_coding/test/TestAllCodecs.h"
12
13 #include <cstdio>
14 #include <limits>
15 #include <string>
16
17 #include "absl/strings/match.h"
18 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
19 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
20 #include "modules/audio_coding/include/audio_coding_module_typedefs.h"
21 #include "modules/include/module_common_types.h"
22 #include "rtc_base/logging.h"
23 #include "rtc_base/string_encode.h"
24 #include "rtc_base/strings/string_builder.h"
25 #include "test/gtest.h"
26 #include "test/testsupport/file_utils.h"
27
28 // Description of the test:
29 // In this test we set up a one-way communication channel from a participant
30 // called "a" to a participant called "b".
31 // a -> channel_a_to_b -> b
32 //
33 // The test loops through all available mono codecs, encode at "a" sends over
34 // the channel, and decodes at "b".
35
36 #define CHECK_ERROR(f) \
37 do { \
38 EXPECT_GE(f, 0) << "Error Calling API"; \
39 } while (0)
40
41 namespace {
42 const size_t kVariableSize = std::numeric_limits<size_t>::max();
43 }
44
45 namespace webrtc {
46
47 // Class for simulating packet handling.
TestPack()48 TestPack::TestPack()
49 : receiver_acm_(NULL),
50 sequence_number_(0),
51 timestamp_diff_(0),
52 last_in_timestamp_(0),
53 total_bytes_(0),
54 payload_size_(0) {}
55
~TestPack()56 TestPack::~TestPack() {}
57
RegisterReceiverACM(AudioCodingModule * acm)58 void TestPack::RegisterReceiverACM(AudioCodingModule* acm) {
59 receiver_acm_ = acm;
60 return;
61 }
62
SendData(AudioFrameType frame_type,uint8_t payload_type,uint32_t timestamp,const uint8_t * payload_data,size_t payload_size,int64_t absolute_capture_timestamp_ms)63 int32_t TestPack::SendData(AudioFrameType frame_type,
64 uint8_t payload_type,
65 uint32_t timestamp,
66 const uint8_t* payload_data,
67 size_t payload_size,
68 int64_t absolute_capture_timestamp_ms) {
69 RTPHeader rtp_header;
70 int32_t status;
71
72 rtp_header.markerBit = false;
73 rtp_header.ssrc = 0;
74 rtp_header.sequenceNumber = sequence_number_++;
75 rtp_header.payloadType = payload_type;
76 rtp_header.timestamp = timestamp;
77
78 if (frame_type == AudioFrameType::kEmptyFrame) {
79 // Skip this frame.
80 return 0;
81 }
82
83 // Only run mono for all test cases.
84 memcpy(payload_data_, payload_data, payload_size);
85
86 status =
87 receiver_acm_->IncomingPacket(payload_data_, payload_size, rtp_header);
88
89 payload_size_ = payload_size;
90 timestamp_diff_ = timestamp - last_in_timestamp_;
91 last_in_timestamp_ = timestamp;
92 total_bytes_ += payload_size;
93 return status;
94 }
95
payload_size()96 size_t TestPack::payload_size() {
97 return payload_size_;
98 }
99
timestamp_diff()100 uint32_t TestPack::timestamp_diff() {
101 return timestamp_diff_;
102 }
103
reset_payload_size()104 void TestPack::reset_payload_size() {
105 payload_size_ = 0;
106 }
107
TestAllCodecs()108 TestAllCodecs::TestAllCodecs()
109 : acm_a_(AudioCodingModule::Create(
110 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
111 acm_b_(AudioCodingModule::Create(
112 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
113 channel_a_to_b_(NULL),
114 test_count_(0),
115 packet_size_samples_(0),
116 packet_size_bytes_(0) {}
117
~TestAllCodecs()118 TestAllCodecs::~TestAllCodecs() {
119 if (channel_a_to_b_ != NULL) {
120 delete channel_a_to_b_;
121 channel_a_to_b_ = NULL;
122 }
123 }
124
Perform()125 void TestAllCodecs::Perform() {
126 const std::string file_name =
127 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
128 infile_a_.Open(file_name, 32000, "rb");
129
130 acm_a_->InitializeReceiver();
131 acm_b_->InitializeReceiver();
132
133 acm_b_->SetReceiveCodecs({{107, {"L16", 8000, 1}},
134 {108, {"L16", 16000, 1}},
135 {109, {"L16", 32000, 1}},
136 {111, {"L16", 8000, 2}},
137 {112, {"L16", 16000, 2}},
138 {113, {"L16", 32000, 2}},
139 {0, {"PCMU", 8000, 1}},
140 {110, {"PCMU", 8000, 2}},
141 {8, {"PCMA", 8000, 1}},
142 {118, {"PCMA", 8000, 2}},
143 {102, {"ILBC", 8000, 1}},
144 {9, {"G722", 8000, 1}},
145 {119, {"G722", 8000, 2}},
146 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}},
147 {13, {"CN", 8000, 1}},
148 {98, {"CN", 16000, 1}},
149 {99, {"CN", 32000, 1}}});
150
151 // Create and connect the channel
152 channel_a_to_b_ = new TestPack;
153 acm_a_->RegisterTransportCallback(channel_a_to_b_);
154 channel_a_to_b_->RegisterReceiverACM(acm_b_.get());
155
156 // All codecs are tested for all allowed sampling frequencies, rates and
157 // packet sizes.
158 test_count_++;
159 OpenOutFile(test_count_);
160 char codec_g722[] = "G722";
161 RegisterSendCodec('A', codec_g722, 16000, 64000, 160, 0);
162 Run(channel_a_to_b_);
163 RegisterSendCodec('A', codec_g722, 16000, 64000, 320, 0);
164 Run(channel_a_to_b_);
165 RegisterSendCodec('A', codec_g722, 16000, 64000, 480, 0);
166 Run(channel_a_to_b_);
167 RegisterSendCodec('A', codec_g722, 16000, 64000, 640, 0);
168 Run(channel_a_to_b_);
169 RegisterSendCodec('A', codec_g722, 16000, 64000, 800, 0);
170 Run(channel_a_to_b_);
171 RegisterSendCodec('A', codec_g722, 16000, 64000, 960, 0);
172 Run(channel_a_to_b_);
173 outfile_b_.Close();
174 #ifdef WEBRTC_CODEC_ILBC
175 test_count_++;
176 OpenOutFile(test_count_);
177 char codec_ilbc[] = "ILBC";
178 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 240, 0);
179 Run(channel_a_to_b_);
180 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 480, 0);
181 Run(channel_a_to_b_);
182 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 160, 0);
183 Run(channel_a_to_b_);
184 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 320, 0);
185 Run(channel_a_to_b_);
186 outfile_b_.Close();
187 #endif
188 test_count_++;
189 OpenOutFile(test_count_);
190 char codec_l16[] = "L16";
191 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, 0);
192 Run(channel_a_to_b_);
193 RegisterSendCodec('A', codec_l16, 8000, 128000, 160, 0);
194 Run(channel_a_to_b_);
195 RegisterSendCodec('A', codec_l16, 8000, 128000, 240, 0);
196 Run(channel_a_to_b_);
197 RegisterSendCodec('A', codec_l16, 8000, 128000, 320, 0);
198 Run(channel_a_to_b_);
199 outfile_b_.Close();
200
201 test_count_++;
202 OpenOutFile(test_count_);
203 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, 0);
204 Run(channel_a_to_b_);
205 RegisterSendCodec('A', codec_l16, 16000, 256000, 320, 0);
206 Run(channel_a_to_b_);
207 RegisterSendCodec('A', codec_l16, 16000, 256000, 480, 0);
208 Run(channel_a_to_b_);
209 RegisterSendCodec('A', codec_l16, 16000, 256000, 640, 0);
210 Run(channel_a_to_b_);
211 outfile_b_.Close();
212
213 test_count_++;
214 OpenOutFile(test_count_);
215 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, 0);
216 Run(channel_a_to_b_);
217 RegisterSendCodec('A', codec_l16, 32000, 512000, 640, 0);
218 Run(channel_a_to_b_);
219 outfile_b_.Close();
220
221 test_count_++;
222 OpenOutFile(test_count_);
223 char codec_pcma[] = "PCMA";
224 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, 0);
225 Run(channel_a_to_b_);
226 RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, 0);
227 Run(channel_a_to_b_);
228 RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, 0);
229 Run(channel_a_to_b_);
230 RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, 0);
231 Run(channel_a_to_b_);
232 RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, 0);
233 Run(channel_a_to_b_);
234 RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, 0);
235 Run(channel_a_to_b_);
236
237 char codec_pcmu[] = "PCMU";
238 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, 0);
239 Run(channel_a_to_b_);
240 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, 0);
241 Run(channel_a_to_b_);
242 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, 0);
243 Run(channel_a_to_b_);
244 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, 0);
245 Run(channel_a_to_b_);
246 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, 0);
247 Run(channel_a_to_b_);
248 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, 0);
249 Run(channel_a_to_b_);
250 outfile_b_.Close();
251 #ifdef WEBRTC_CODEC_OPUS
252 test_count_++;
253 OpenOutFile(test_count_);
254 char codec_opus[] = "OPUS";
255 RegisterSendCodec('A', codec_opus, 48000, 6000, 480, kVariableSize);
256 Run(channel_a_to_b_);
257 RegisterSendCodec('A', codec_opus, 48000, 20000, 480 * 2, kVariableSize);
258 Run(channel_a_to_b_);
259 RegisterSendCodec('A', codec_opus, 48000, 32000, 480 * 4, kVariableSize);
260 Run(channel_a_to_b_);
261 RegisterSendCodec('A', codec_opus, 48000, 48000, 480, kVariableSize);
262 Run(channel_a_to_b_);
263 RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 4, kVariableSize);
264 Run(channel_a_to_b_);
265 RegisterSendCodec('A', codec_opus, 48000, 96000, 480 * 6, kVariableSize);
266 Run(channel_a_to_b_);
267 RegisterSendCodec('A', codec_opus, 48000, 500000, 480 * 2, kVariableSize);
268 Run(channel_a_to_b_);
269 outfile_b_.Close();
270 #endif
271 }
272
273 // Register Codec to use in the test
274 //
275 // Input: side - which ACM to use, 'A' or 'B'
276 // codec_name - name to use when register the codec
277 // sampling_freq_hz - sampling frequency in Herz
278 // rate - bitrate in bytes
279 // packet_size - packet size in samples
280 // extra_byte - if extra bytes needed compared to the bitrate
281 // used when registering, can be an internal header
282 // set to kVariableSize if the codec is a variable
283 // rate codec
RegisterSendCodec(char side,char * codec_name,int32_t sampling_freq_hz,int rate,int packet_size,size_t extra_byte)284 void TestAllCodecs::RegisterSendCodec(char side,
285 char* codec_name,
286 int32_t sampling_freq_hz,
287 int rate,
288 int packet_size,
289 size_t extra_byte) {
290 // Store packet-size in samples, used to validate the received packet.
291 // If G.722, store half the size to compensate for the timestamp bug in the
292 // RFC for G.722.
293 int clockrate_hz = sampling_freq_hz;
294 size_t num_channels = 1;
295 if (absl::EqualsIgnoreCase(codec_name, "G722")) {
296 packet_size_samples_ = packet_size / 2;
297 clockrate_hz = sampling_freq_hz / 2;
298 } else if (absl::EqualsIgnoreCase(codec_name, "OPUS")) {
299 packet_size_samples_ = packet_size;
300 num_channels = 2;
301 } else {
302 packet_size_samples_ = packet_size;
303 }
304
305 // Store the expected packet size in bytes, used to validate the received
306 // packet. If variable rate codec (extra_byte == -1), set to -1.
307 if (extra_byte != kVariableSize) {
308 // Add 0.875 to always round up to a whole byte
309 packet_size_bytes_ =
310 static_cast<size_t>(static_cast<float>(packet_size * rate) /
311 static_cast<float>(sampling_freq_hz * 8) +
312 0.875) +
313 extra_byte;
314 } else {
315 // Packets will have a variable size.
316 packet_size_bytes_ = kVariableSize;
317 }
318
319 // Set pointer to the ACM where to register the codec.
320 AudioCodingModule* my_acm = NULL;
321 switch (side) {
322 case 'A': {
323 my_acm = acm_a_.get();
324 break;
325 }
326 case 'B': {
327 my_acm = acm_b_.get();
328 break;
329 }
330 default: {
331 break;
332 }
333 }
334 ASSERT_TRUE(my_acm != NULL);
335
336 auto factory = CreateBuiltinAudioEncoderFactory();
337 constexpr int payload_type = 17;
338 SdpAudioFormat format = {codec_name, clockrate_hz, num_channels};
339 format.parameters["ptime"] = rtc::ToString(rtc::CheckedDivExact(
340 packet_size, rtc::CheckedDivExact(sampling_freq_hz, 1000)));
341 my_acm->SetEncoder(
342 factory->MakeAudioEncoder(payload_type, format, absl::nullopt));
343 }
344
Run(TestPack * channel)345 void TestAllCodecs::Run(TestPack* channel) {
346 AudioFrame audio_frame;
347
348 int32_t out_freq_hz = outfile_b_.SamplingFrequency();
349 size_t receive_size;
350 uint32_t timestamp_diff;
351 channel->reset_payload_size();
352 int error_count = 0;
353 int counter = 0;
354 // Set test length to 500 ms (50 blocks of 10 ms each).
355 infile_a_.SetNum10MsBlocksToRead(50);
356 // Fast-forward 1 second (100 blocks) since the file starts with silence.
357 infile_a_.FastForward(100);
358
359 while (!infile_a_.EndOfFile()) {
360 // Add 10 msec to ACM.
361 infile_a_.Read10MsData(audio_frame);
362 CHECK_ERROR(acm_a_->Add10MsData(audio_frame));
363
364 // Verify that the received packet size matches the settings.
365 receive_size = channel->payload_size();
366 if (receive_size) {
367 if ((receive_size != packet_size_bytes_) &&
368 (packet_size_bytes_ != kVariableSize)) {
369 error_count++;
370 }
371
372 // Verify that the timestamp is updated with expected length. The counter
373 // is used to avoid problems when switching codec or frame size in the
374 // test.
375 timestamp_diff = channel->timestamp_diff();
376 if ((counter > 10) &&
377 (static_cast<int>(timestamp_diff) != packet_size_samples_) &&
378 (packet_size_samples_ > -1))
379 error_count++;
380 }
381
382 // Run received side of ACM.
383 bool muted;
384 CHECK_ERROR(acm_b_->PlayoutData10Ms(out_freq_hz, &audio_frame, &muted));
385 ASSERT_FALSE(muted);
386
387 // Write output speech to file.
388 outfile_b_.Write10MsData(audio_frame.data(),
389 audio_frame.samples_per_channel_);
390
391 // Update loop counter
392 counter++;
393 }
394
395 EXPECT_EQ(0, error_count);
396
397 if (infile_a_.EndOfFile()) {
398 infile_a_.Rewind();
399 }
400 }
401
OpenOutFile(int test_number)402 void TestAllCodecs::OpenOutFile(int test_number) {
403 std::string filename = webrtc::test::OutputPath();
404 rtc::StringBuilder test_number_str;
405 test_number_str << test_number;
406 filename += "testallcodecs_out_";
407 filename += test_number_str.str();
408 filename += ".pcm";
409 outfile_b_.Open(filename, 32000, "wb");
410 }
411
412 } // namespace webrtc
413