2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/audio_coding/main/test/TestStereo.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "webrtc/common_types.h"
19 #include "webrtc/engine_configurations.h"
20 #include "webrtc/modules/audio_coding/main/interface/audio_coding_module_typedefs.h"
21 #include "webrtc/modules/audio_coding/main/test/utility.h"
22 #include "webrtc/system_wrappers/interface/trace.h"
23 #include "webrtc/test/testsupport/fileutils.h"
27 // Class for simulating packet handling
28 TestPackStereo::TestPackStereo()
29 : receiver_acm_(NULL),
32 last_in_timestamp_(0),
39 TestPackStereo::~TestPackStereo() {
42 void TestPackStereo::RegisterReceiverACM(AudioCodingModule* acm) {
47 int32_t TestPackStereo::SendData(const FrameType frame_type,
48 const uint8_t payload_type,
49 const uint32_t timestamp,
50 const uint8_t* payload_data,
51 const uint16_t payload_size,
52 const RTPFragmentationHeader* fragmentation) {
53 WebRtcRTPHeader rtp_info;
56 rtp_info.header.markerBit = false;
57 rtp_info.header.ssrc = 0;
58 rtp_info.header.sequenceNumber = seq_no_++;
59 rtp_info.header.payloadType = payload_type;
60 rtp_info.header.timestamp = timestamp;
61 if (frame_type == kFrameEmpty) {
66 if (lost_packet_ == false) {
67 if (frame_type != kAudioFrameCN) {
68 rtp_info.type.Audio.isCNG = false;
69 rtp_info.type.Audio.channel = static_cast<int>(codec_mode_);
71 rtp_info.type.Audio.isCNG = true;
72 rtp_info.type.Audio.channel = static_cast<int>(kMono);
74 status = receiver_acm_->IncomingPacket(payload_data, payload_size,
77 if (frame_type != kAudioFrameCN) {
78 payload_size_ = static_cast<int>(payload_size);
83 timestamp_diff_ = timestamp - last_in_timestamp_;
84 last_in_timestamp_ = timestamp;
85 total_bytes_ += payload_size;
90 uint16_t TestPackStereo::payload_size() {
91 return static_cast<uint16_t>(payload_size_);
94 uint32_t TestPackStereo::timestamp_diff() {
95 return timestamp_diff_;
98 void TestPackStereo::reset_payload_size() {
102 void TestPackStereo::set_codec_mode(enum StereoMonoMode mode) {
106 void TestPackStereo::set_lost_packet(bool lost) {
110 TestStereo::TestStereo(int test_mode)
111 : acm_a_(AudioCodingModule::Create(0)),
112 acm_b_(AudioCodingModule::Create(1)),
119 l16_8khz_pltype_(-1),
120 l16_16khz_pltype_(-1),
121 l16_32khz_pltype_(-1),
127 cn_16khz_pltype_(-1),
128 cn_32khz_pltype_(-1) {
129 // test_mode = 0 for silent test (auto test)
130 test_mode_ = test_mode;
133 TestStereo::~TestStereo() {
134 if (channel_a2b_ != NULL) {
140 void TestStereo::Perform() {
141 uint16_t frequency_hz;
148 // Open both mono and stereo test files in 32 kHz.
149 const std::string file_name_stereo = webrtc::test::ResourcePath(
150 "audio_coding/teststereo32kHz", "pcm");
151 const std::string file_name_mono = webrtc::test::ResourcePath(
152 "audio_coding/testfile32kHz", "pcm");
153 frequency_hz = 32000;
154 in_file_stereo_ = new PCMFile();
155 in_file_mono_ = new PCMFile();
156 in_file_stereo_->Open(file_name_stereo, frequency_hz, "rb");
157 in_file_stereo_->ReadStereo(true);
158 in_file_mono_->Open(file_name_mono, frequency_hz, "rb");
159 in_file_mono_->ReadStereo(false);
161 // Create and initialize two ACMs, one for each side of a one-to-one call.
162 ASSERT_TRUE((acm_a_.get() != NULL) && (acm_b_.get() != NULL));
163 EXPECT_EQ(0, acm_a_->InitializeReceiver());
164 EXPECT_EQ(0, acm_b_->InitializeReceiver());
166 // Register all available codes as receiving codecs.
167 uint8_t num_encoders = acm_a_->NumberOfCodecs();
168 CodecInst my_codec_param;
169 for (uint8_t n = 0; n < num_encoders; n++) {
170 EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param));
171 EXPECT_EQ(0, acm_b_->RegisterReceiveCodec(my_codec_param));
174 // Test that unregister all receive codecs works.
175 for (uint8_t n = 0; n < num_encoders; n++) {
176 EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param));
177 EXPECT_EQ(0, acm_b_->UnregisterReceiveCodec(my_codec_param.pltype));
180 // Register all available codes as receiving codecs once more.
181 for (uint8_t n = 0; n < num_encoders; n++) {
182 EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param));
183 EXPECT_EQ(0, acm_b_->RegisterReceiveCodec(my_codec_param));
186 // Create and connect the channel.
187 channel_a2b_ = new TestPackStereo;
188 EXPECT_EQ(0, acm_a_->RegisterTransportCallback(channel_a2b_));
189 channel_a2b_->RegisterReceiverACM(acm_b_.get());
191 // Start with setting VAD/DTX, before we know we will send stereo.
192 // Continue with setting a stereo codec as send codec and verify that
193 // VAD/DTX gets turned off.
194 EXPECT_EQ(0, acm_a_->SetVAD(true, true, VADNormal));
195 EXPECT_EQ(0, acm_a_->VAD(&dtx, &vad, &vad_mode));
198 char codec_pcma_temp[] = "PCMA";
199 RegisterSendCodec('A', codec_pcma_temp, 8000, 64000, 80, 2, pcma_pltype_);
200 EXPECT_EQ(0, acm_a_->VAD(&dtx, &vad, &vad_mode));
203 if (test_mode_ != 0) {
208 // Test Stereo-To-Stereo for all codecs.
213 // All codecs are tested for all allowed sampling frequencies, rates and
215 #ifdef WEBRTC_CODEC_G722
216 if (test_mode_ != 0) {
217 printf("===========================================================\n");
218 printf("Test number: %d\n", test_cntr_ + 1);
219 printf("Test type: Stereo-to-stereo\n");
221 channel_a2b_->set_codec_mode(kStereo);
223 OpenOutFile(test_cntr_);
224 char codec_g722[] = "G722";
225 RegisterSendCodec('A', codec_g722, 16000, 64000, 160, codec_channels,
227 Run(channel_a2b_, audio_channels, codec_channels);
228 RegisterSendCodec('A', codec_g722, 16000, 64000, 320, codec_channels,
230 Run(channel_a2b_, audio_channels, codec_channels);
231 RegisterSendCodec('A', codec_g722, 16000, 64000, 480, codec_channels,
233 Run(channel_a2b_, audio_channels, codec_channels);
234 RegisterSendCodec('A', codec_g722, 16000, 64000, 640, codec_channels,
236 Run(channel_a2b_, audio_channels, codec_channels);
237 RegisterSendCodec('A', codec_g722, 16000, 64000, 800, codec_channels,
239 Run(channel_a2b_, audio_channels, codec_channels);
240 RegisterSendCodec('A', codec_g722, 16000, 64000, 960, codec_channels,
242 Run(channel_a2b_, audio_channels, codec_channels);
245 #ifdef WEBRTC_CODEC_PCM16
246 if (test_mode_ != 0) {
247 printf("===========================================================\n");
248 printf("Test number: %d\n", test_cntr_ + 1);
249 printf("Test type: Stereo-to-stereo\n");
251 channel_a2b_->set_codec_mode(kStereo);
253 OpenOutFile(test_cntr_);
254 char codec_l16[] = "L16";
255 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, codec_channels,
257 Run(channel_a2b_, audio_channels, codec_channels);
258 RegisterSendCodec('A', codec_l16, 8000, 128000, 160, codec_channels,
260 Run(channel_a2b_, audio_channels, codec_channels);
261 RegisterSendCodec('A', codec_l16, 8000, 128000, 240, codec_channels,
263 Run(channel_a2b_, audio_channels, codec_channels);
264 RegisterSendCodec('A', codec_l16, 8000, 128000, 320, codec_channels,
266 Run(channel_a2b_, audio_channels, codec_channels);
269 if (test_mode_ != 0) {
270 printf("===========================================================\n");
271 printf("Test number: %d\n", test_cntr_ + 1);
272 printf("Test type: Stereo-to-stereo\n");
275 OpenOutFile(test_cntr_);
276 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, codec_channels,
278 Run(channel_a2b_, audio_channels, codec_channels);
279 RegisterSendCodec('A', codec_l16, 16000, 256000, 320, codec_channels,
281 Run(channel_a2b_, audio_channels, codec_channels);
282 RegisterSendCodec('A', codec_l16, 16000, 256000, 480, codec_channels,
284 Run(channel_a2b_, audio_channels, codec_channels);
285 RegisterSendCodec('A', codec_l16, 16000, 256000, 640, codec_channels,
287 Run(channel_a2b_, audio_channels, codec_channels);
290 if (test_mode_ != 0) {
291 printf("===========================================================\n");
292 printf("Test number: %d\n", test_cntr_ + 1);
293 printf("Test type: Stereo-to-stereo\n");
296 OpenOutFile(test_cntr_);
297 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, codec_channels,
299 Run(channel_a2b_, audio_channels, codec_channels);
300 RegisterSendCodec('A', codec_l16, 32000, 512000, 640, codec_channels,
302 Run(channel_a2b_, audio_channels, codec_channels);
305 #define PCMA_AND_PCMU
307 if (test_mode_ != 0) {
308 printf("===========================================================\n");
309 printf("Test number: %d\n", test_cntr_ + 1);
310 printf("Test type: Stereo-to-stereo\n");
312 channel_a2b_->set_codec_mode(kStereo);
316 OpenOutFile(test_cntr_);
317 char codec_pcma[] = "PCMA";
318 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, codec_channels,
320 Run(channel_a2b_, audio_channels, codec_channels);
321 RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, codec_channels,
323 Run(channel_a2b_, audio_channels, codec_channels);
324 RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, codec_channels,
326 Run(channel_a2b_, audio_channels, codec_channels);
327 RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, codec_channels,
329 Run(channel_a2b_, audio_channels, codec_channels);
330 RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, codec_channels,
332 Run(channel_a2b_, audio_channels, codec_channels);
333 RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, codec_channels,
335 Run(channel_a2b_, audio_channels, codec_channels);
337 // Test that VAD/DTX cannot be turned on while sending stereo.
338 EXPECT_EQ(-1, acm_a_->SetVAD(true, true, VADNormal));
339 EXPECT_EQ(0, acm_a_->VAD(&dtx, &vad, &vad_mode));
342 EXPECT_EQ(-1, acm_a_->SetVAD(true, false, VADNormal));
343 EXPECT_EQ(0, acm_a_->VAD(&dtx, &vad, &vad_mode));
346 EXPECT_EQ(-1, acm_a_->SetVAD(false, true, VADNormal));
347 EXPECT_EQ(0, acm_a_->VAD(&dtx, &vad, &vad_mode));
350 EXPECT_EQ(0, acm_a_->SetVAD(false, false, VADNormal));
351 EXPECT_EQ(0, acm_a_->VAD(&dtx, &vad, &vad_mode));
356 if (test_mode_ != 0) {
357 printf("===========================================================\n");
358 printf("Test number: %d\n", test_cntr_ + 1);
359 printf("Test type: Stereo-to-stereo\n");
362 OpenOutFile(test_cntr_);
363 char codec_pcmu[] = "PCMU";
364 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, codec_channels,
366 Run(channel_a2b_, audio_channels, codec_channels);
367 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, codec_channels,
369 Run(channel_a2b_, audio_channels, codec_channels);
370 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, codec_channels,
372 Run(channel_a2b_, audio_channels, codec_channels);
373 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, codec_channels,
375 Run(channel_a2b_, audio_channels, codec_channels);
376 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, codec_channels,
378 Run(channel_a2b_, audio_channels, codec_channels);
379 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, codec_channels,
381 Run(channel_a2b_, audio_channels, codec_channels);
384 #ifdef WEBRTC_CODEC_CELT
385 if (test_mode_ != 0) {
386 printf("===========================================================\n");
387 printf("Test number: %d\n", test_cntr_ + 1);
388 printf("Test type: Stereo-to-stereo\n");
390 channel_a2b_->set_codec_mode(kStereo);
394 OpenOutFile(test_cntr_);
395 char codec_celt[] = "CELT";
396 RegisterSendCodec('A', codec_celt, 32000, 48000, 640, codec_channels,
398 Run(channel_a2b_, audio_channels, codec_channels);
399 RegisterSendCodec('A', codec_celt, 32000, 64000, 640, codec_channels,
401 Run(channel_a2b_, audio_channels, codec_channels);
402 RegisterSendCodec('A', codec_celt, 32000, 128000, 640, codec_channels,
404 Run(channel_a2b_, audio_channels, codec_channels);
407 #ifdef WEBRTC_CODEC_OPUS
408 if (test_mode_ != 0) {
409 printf("===========================================================\n");
410 printf("Test number: %d\n", test_cntr_ + 1);
411 printf("Test type: Stereo-to-stereo\n");
413 channel_a2b_->set_codec_mode(kStereo);
417 OpenOutFile(test_cntr_);
419 char codec_opus[] = "opus";
420 // Run Opus with 10 ms frame size.
421 RegisterSendCodec('A', codec_opus, 48000, 64000, 480, codec_channels,
423 Run(channel_a2b_, audio_channels, codec_channels);
424 // Run Opus with 20 ms frame size.
425 RegisterSendCodec('A', codec_opus, 48000, 64000, 480*2, codec_channels,
427 Run(channel_a2b_, audio_channels, codec_channels);
428 // Run Opus with 40 ms frame size.
429 RegisterSendCodec('A', codec_opus, 48000, 64000, 480*4, codec_channels,
431 Run(channel_a2b_, audio_channels, codec_channels);
432 // Run Opus with 60 ms frame size.
433 RegisterSendCodec('A', codec_opus, 48000, 64000, 480*6, codec_channels,
435 Run(channel_a2b_, audio_channels, codec_channels);
436 // Run Opus with 20 ms frame size and different bitrates.
437 RegisterSendCodec('A', codec_opus, 48000, 40000, 960, codec_channels,
439 Run(channel_a2b_, audio_channels, codec_channels);
440 RegisterSendCodec('A', codec_opus, 48000, 510000, 960, codec_channels,
442 Run(channel_a2b_, audio_channels, codec_channels);
446 // Test Mono-To-Stereo for all codecs.
451 #ifdef WEBRTC_CODEC_G722
452 if (test_mode_ != 0) {
453 printf("===============================================================\n");
454 printf("Test number: %d\n", test_cntr_ + 1);
455 printf("Test type: Mono-to-stereo\n");
458 channel_a2b_->set_codec_mode(kStereo);
459 OpenOutFile(test_cntr_);
460 RegisterSendCodec('A', codec_g722, 16000, 64000, 160, codec_channels,
462 Run(channel_a2b_, audio_channels, codec_channels);
465 #ifdef WEBRTC_CODEC_PCM16
466 if (test_mode_ != 0) {
467 printf("===============================================================\n");
468 printf("Test number: %d\n", test_cntr_ + 1);
469 printf("Test type: Mono-to-stereo\n");
472 channel_a2b_->set_codec_mode(kStereo);
473 OpenOutFile(test_cntr_);
474 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, codec_channels,
476 Run(channel_a2b_, audio_channels, codec_channels);
478 if (test_mode_ != 0) {
479 printf("===============================================================\n");
480 printf("Test number: %d\n", test_cntr_ + 1);
481 printf("Test type: Mono-to-stereo\n");
484 OpenOutFile(test_cntr_);
485 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, codec_channels,
487 Run(channel_a2b_, audio_channels, codec_channels);
489 if (test_mode_ != 0) {
490 printf("===============================================================\n");
491 printf("Test number: %d\n", test_cntr_ + 1);
492 printf("Test type: Mono-to-stereo\n");
495 OpenOutFile(test_cntr_);
496 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, codec_channels,
498 Run(channel_a2b_, audio_channels, codec_channels);
502 if (test_mode_ != 0) {
503 printf("===============================================================\n");
504 printf("Test number: %d\n", test_cntr_ + 1);
505 printf("Test type: Mono-to-stereo\n");
508 channel_a2b_->set_codec_mode(kStereo);
509 OpenOutFile(test_cntr_);
510 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, codec_channels,
512 Run(channel_a2b_, audio_channels, codec_channels);
513 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, codec_channels,
515 Run(channel_a2b_, audio_channels, codec_channels);
518 #ifdef WEBRTC_CODEC_CELT
519 if (test_mode_ != 0) {
520 printf("===============================================================\n");
521 printf("Test number: %d\n", test_cntr_ + 1);
522 printf("Test type: Mono-to-stereo\n");
525 channel_a2b_->set_codec_mode(kStereo);
526 OpenOutFile(test_cntr_);
527 RegisterSendCodec('A', codec_celt, 32000, 64000, 640, codec_channels,
529 Run(channel_a2b_, audio_channels, codec_channels);
532 #ifdef WEBRTC_CODEC_OPUS
533 if (test_mode_ != 0) {
534 printf("===============================================================\n");
535 printf("Test number: %d\n", test_cntr_ + 1);
536 printf("Test type: Mono-to-stereo\n");
539 // Keep encode and decode in stereo.
541 channel_a2b_->set_codec_mode(kStereo);
542 OpenOutFile(test_cntr_);
543 RegisterSendCodec('A', codec_opus, 48000, 64000, 960, codec_channels,
545 Run(channel_a2b_, audio_channels, codec_channels);
547 // Encode in mono, decode in stereo mode.
548 RegisterSendCodec('A', codec_opus, 48000, 64000, 960, 1, opus_pltype_);
549 Run(channel_a2b_, audio_channels, codec_channels);
554 // Test Stereo-To-Mono for all codecs.
558 channel_a2b_->set_codec_mode(kMono);
560 #ifdef WEBRTC_CODEC_G722
561 // Run stereo audio and mono codec.
562 if (test_mode_ != 0) {
563 printf("===============================================================\n");
564 printf("Test number: %d\n", test_cntr_ + 1);
565 printf("Test type: Stereo-to-mono\n");
568 OpenOutFile(test_cntr_);
569 RegisterSendCodec('A', codec_g722, 16000, 64000, 160, codec_channels,
572 // Make sure it is possible to set VAD/CNG, now that we are sending mono
574 EXPECT_EQ(0, acm_a_->SetVAD(true, true, VADNormal));
575 EXPECT_EQ(0, acm_a_->VAD(&dtx, &vad, &vad_mode));
578 EXPECT_EQ(0, acm_a_->SetVAD(false, false, VADNormal));
579 Run(channel_a2b_, audio_channels, codec_channels);
582 #ifdef WEBRTC_CODEC_PCM16
583 if (test_mode_ != 0) {
584 printf("===============================================================\n");
585 printf("Test number: %d\n", test_cntr_ + 1);
586 printf("Test type: Stereo-to-mono\n");
589 OpenOutFile(test_cntr_);
590 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, codec_channels,
592 Run(channel_a2b_, audio_channels, codec_channels);
594 if (test_mode_ != 0) {
595 printf("===============================================================\n");
596 printf("Test number: %d\n", test_cntr_ + 1);
597 printf("Test type: Stereo-to-mono\n");
600 OpenOutFile(test_cntr_);
601 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, codec_channels,
603 Run(channel_a2b_, audio_channels, codec_channels);
605 if (test_mode_ != 0) {
606 printf("==============================================================\n");
607 printf("Test number: %d\n", test_cntr_ + 1);
608 printf("Test type: Stereo-to-mono\n");
611 OpenOutFile(test_cntr_);
612 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, codec_channels,
614 Run(channel_a2b_, audio_channels, codec_channels);
618 if (test_mode_ != 0) {
619 printf("===============================================================\n");
620 printf("Test number: %d\n", test_cntr_ + 1);
621 printf("Test type: Stereo-to-mono\n");
624 OpenOutFile(test_cntr_);
625 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, codec_channels,
627 Run(channel_a2b_, audio_channels, codec_channels);
628 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, codec_channels,
630 Run(channel_a2b_, audio_channels, codec_channels);
633 #ifdef WEBRTC_CODEC_CELT
634 if (test_mode_ != 0) {
635 printf("===============================================================\n");
636 printf("Test number: %d\n", test_cntr_ + 1);
637 printf("Test type: Stereo-to-mono\n");
640 OpenOutFile(test_cntr_);
641 RegisterSendCodec('A', codec_celt, 32000, 64000, 640, codec_channels,
643 Run(channel_a2b_, audio_channels, codec_channels);
646 #ifdef WEBRTC_CODEC_OPUS
647 if (test_mode_ != 0) {
648 printf("===============================================================\n");
649 printf("Test number: %d\n", test_cntr_ + 1);
650 printf("Test type: Stereo-to-mono\n");
653 OpenOutFile(test_cntr_);
654 // Encode and decode in mono.
655 RegisterSendCodec('A', codec_opus, 48000, 32000, 960, codec_channels,
657 CodecInst opus_codec_param;
658 for (uint8_t n = 0; n < num_encoders; n++) {
659 EXPECT_EQ(0, acm_b_->Codec(n, &opus_codec_param));
660 if (!strcmp(opus_codec_param.plname, "opus")) {
661 opus_codec_param.channels = 1;
662 EXPECT_EQ(0, acm_b_->RegisterReceiveCodec(opus_codec_param));
666 Run(channel_a2b_, audio_channels, codec_channels);
668 // Encode in stereo, decode in mono.
669 RegisterSendCodec('A', codec_opus, 48000, 32000, 960, 2, opus_pltype_);
670 Run(channel_a2b_, audio_channels, codec_channels);
674 // Test switching between decoding mono and stereo for Opus.
678 OpenOutFile(test_cntr_);
679 if (test_mode_ != 0) {
680 // Print out codec and settings
681 printf("Test number: %d\nCodec: Opus Freq: 48000 Rate :32000 PackSize: 960"
682 " Decode: mono\n", test_cntr_);
684 Run(channel_a2b_, audio_channels, codec_channels);
688 OpenOutFile(test_cntr_);
689 if (test_mode_ != 0) {
690 // Print out codec and settings
691 printf("Test number: %d\nCodec: Opus Freq: 48000 Rate :32000 PackSize: 960"
692 " Decode: stereo\n", test_cntr_);
694 opus_codec_param.channels = 2;
695 EXPECT_EQ(0, acm_b_->RegisterReceiveCodec(opus_codec_param));
696 Run(channel_a2b_, audio_channels, 2);
700 OpenOutFile(test_cntr_);
701 if (test_mode_ != 0) {
702 // Print out codec and settings
703 printf("Test number: %d\nCodec: Opus Freq: 48000 Rate :32000 PackSize: 960"
704 " Decode: mono\n", test_cntr_);
706 opus_codec_param.channels = 1;
707 EXPECT_EQ(0, acm_b_->RegisterReceiveCodec(opus_codec_param));
708 Run(channel_a2b_, audio_channels, codec_channels);
713 // Print out which codecs were tested, and which were not, in the run.
714 if (test_mode_ != 0) {
715 printf("\nThe following codecs was INCLUDED in the test:\n");
716 #ifdef WEBRTC_CODEC_G722
719 #ifdef WEBRTC_CODEC_PCM16
723 #ifdef WEBRTC_CODEC_CELT
726 #ifdef WEBRTC_CODEC_OPUS
729 printf("\nTo complete the test, listen to the %d number of output "
734 // Delete the file pointers.
735 delete in_file_stereo_;
736 delete in_file_mono_;
739 // Register Codec to use in the test
741 // Input: side - which ACM to use, 'A' or 'B'
742 // codec_name - name to use when register the codec
743 // sampling_freq_hz - sampling frequency in Herz
744 // rate - bitrate in bytes
745 // pack_size - packet size in samples
746 // channels - number of channels; 1 for mono, 2 for stereo
747 // payload_type - payload type for the codec
748 void TestStereo::RegisterSendCodec(char side, char* codec_name,
749 int32_t sampling_freq_hz, int rate,
750 int pack_size, int channels,
752 if (test_mode_ != 0) {
753 // Print out codec and settings
754 printf("Codec: %s Freq: %d Rate: %d PackSize: %d\n", codec_name,
755 sampling_freq_hz, rate, pack_size);
758 // Store packet size in samples, used to validate the received packet
759 pack_size_samp_ = pack_size;
761 // Store the expected packet size in bytes, used to validate the received
762 // packet. Add 0.875 to always round up to a whole byte.
763 // For Celt the packet size in bytes is already counting the stereo part.
764 if (!strcmp(codec_name, "CELT")) {
765 pack_size_bytes_ = (uint16_t)(
766 static_cast<float>(pack_size * rate) /
767 static_cast<float>(sampling_freq_hz * 8) + 0.875) / channels;
769 pack_size_bytes_ = (uint16_t)(
770 static_cast<float>(pack_size * rate) /
771 static_cast<float>(sampling_freq_hz * 8) + 0.875);
774 // Set pointer to the ACM where to register the codec
775 AudioCodingModule* my_acm = NULL;
778 my_acm = acm_a_.get();
782 my_acm = acm_b_.get();
788 ASSERT_TRUE(my_acm != NULL);
790 CodecInst my_codec_param;
791 // Get all codec parameters before registering
792 EXPECT_GT(AudioCodingModule::Codec(codec_name, &my_codec_param,
793 sampling_freq_hz, channels), -1);
794 my_codec_param.rate = rate;
795 my_codec_param.pacsize = pack_size;
796 EXPECT_EQ(0, my_acm->RegisterSendCodec(my_codec_param));
798 send_codec_name_ = codec_name;
801 void TestStereo::Run(TestPackStereo* channel, int in_channels, int out_channels,
803 AudioFrame audio_frame;
805 int32_t out_freq_hz_b = out_file_.SamplingFrequency();
807 uint32_t time_stamp_diff;
808 channel->reset_payload_size();
810 int variable_bytes = 0;
811 int variable_packets = 0;
814 // Simulate packet loss by setting |packet_loss_| to "true" in
815 // |percent_loss| percent of the loops.
816 if (percent_loss > 0) {
817 if (counter_ == floor((100 / percent_loss) + 0.5)) {
819 channel->set_lost_packet(true);
821 channel->set_lost_packet(false);
826 // Add 10 msec to ACM
827 if (in_channels == 1) {
828 if (in_file_mono_->EndOfFile()) {
831 in_file_mono_->Read10MsData(audio_frame);
833 if (in_file_stereo_->EndOfFile()) {
836 in_file_stereo_->Read10MsData(audio_frame);
838 EXPECT_EQ(0, acm_a_->Add10MsData(audio_frame));
840 // Run sender side of ACM
841 EXPECT_GT(acm_a_->Process(), -1);
843 // Verify that the received packet size matches the settings.
844 rec_size = channel->payload_size();
845 if ((0 < rec_size) & (rec_size < 65535)) {
846 if (strcmp(send_codec_name_, "opus") == 0) {
847 // Opus is a variable rate codec, hence calculate the average packet
848 // size, and later make sure the average is in the right range.
849 variable_bytes += rec_size;
852 // For fixed rate codecs, check that packet size is correct.
853 if ((rec_size != pack_size_bytes_ * out_channels)
854 && (pack_size_bytes_ < 65535)) {
858 // Verify that the timestamp is updated with expected length
859 time_stamp_diff = channel->timestamp_diff();
860 if ((counter_ > 10) && (time_stamp_diff != pack_size_samp_)) {
865 // Run received side of ACM
866 EXPECT_EQ(0, acm_b_->PlayoutData10Ms(out_freq_hz_b, &audio_frame));
868 // Write output speech to file
869 out_file_.Write10MsData(
871 audio_frame.samples_per_channel_ * audio_frame.num_channels_);
874 EXPECT_EQ(0, error_count);
876 // Check that packet size is in the right range for variable rate codecs,
878 if (variable_packets > 0) {
879 variable_bytes /= variable_packets;
880 EXPECT_NEAR(variable_bytes, pack_size_bytes_, 3);
883 if (in_file_mono_->EndOfFile()) {
884 in_file_mono_->Rewind();
886 if (in_file_stereo_->EndOfFile()) {
887 in_file_stereo_->Rewind();
889 // Reset in case we ended with a lost packet
890 channel->set_lost_packet(false);
893 void TestStereo::OpenOutFile(int16_t test_number) {
894 std::string file_name;
895 std::stringstream file_stream;
896 file_stream << webrtc::test::OutputPath() << "teststereo_out_" << test_number
898 file_name = file_stream.str();
899 out_file_.Open(file_name, 32000, "wb");
902 void TestStereo::DisplaySendReceiveCodec() {
903 CodecInst my_codec_param;
904 acm_a_->SendCodec(&my_codec_param);
905 if (test_mode_ != 0) {
906 printf("%s -> ", my_codec_param.plname);
908 acm_b_->ReceiveCodec(&my_codec_param);
909 if (test_mode_ != 0) {
910 printf("%s\n", my_codec_param.plname);
914 } // namespace webrtc