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/TestStereo.h"
12 
13 #include <assert.h>
14 
15 #include <string>
16 
17 #include "common_types.h"  // NOLINT(build/include)
18 #include "modules/audio_coding/codecs/audio_format_conversion.h"
19 #include "modules/audio_coding/include/audio_coding_module_typedefs.h"
20 #include "modules/audio_coding/test/utility.h"
21 #include "test/gtest.h"
22 #include "test/testsupport/fileutils.h"
23 #include "typedefs.h"  // NOLINT(build/include)
24 
25 namespace webrtc {
26 
27 // Class for simulating packet handling
TestPackStereo()28 TestPackStereo::TestPackStereo()
29     : receiver_acm_(NULL),
30       seq_no_(0),
31       timestamp_diff_(0),
32       last_in_timestamp_(0),
33       total_bytes_(0),
34       payload_size_(0),
35       codec_mode_(kNotSet),
36       lost_packet_(false) {
37 }
38 
~TestPackStereo()39 TestPackStereo::~TestPackStereo() {
40 }
41 
RegisterReceiverACM(AudioCodingModule * acm)42 void TestPackStereo::RegisterReceiverACM(AudioCodingModule* acm) {
43   receiver_acm_ = acm;
44   return;
45 }
46 
SendData(const FrameType frame_type,const uint8_t payload_type,const uint32_t timestamp,const uint8_t * payload_data,const size_t payload_size,const RTPFragmentationHeader * fragmentation)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 size_t payload_size,
52                                  const RTPFragmentationHeader* fragmentation) {
53   WebRtcRTPHeader rtp_info;
54   int32_t status = 0;
55 
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 == kEmptyFrame) {
62     // Skip this frame
63     return 0;
64   }
65 
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_);
70     } else {
71       rtp_info.type.Audio.isCNG = true;
72       rtp_info.type.Audio.channel = static_cast<int>(kMono);
73     }
74     status = receiver_acm_->IncomingPacket(payload_data, payload_size,
75                                            rtp_info);
76 
77     if (frame_type != kAudioFrameCN) {
78       payload_size_ = static_cast<int>(payload_size);
79     } else {
80       payload_size_ = -1;
81     }
82 
83     timestamp_diff_ = timestamp - last_in_timestamp_;
84     last_in_timestamp_ = timestamp;
85     total_bytes_ += payload_size;
86   }
87   return status;
88 }
89 
payload_size()90 uint16_t TestPackStereo::payload_size() {
91   return static_cast<uint16_t>(payload_size_);
92 }
93 
timestamp_diff()94 uint32_t TestPackStereo::timestamp_diff() {
95   return timestamp_diff_;
96 }
97 
reset_payload_size()98 void TestPackStereo::reset_payload_size() {
99   payload_size_ = 0;
100 }
101 
set_codec_mode(enum StereoMonoMode mode)102 void TestPackStereo::set_codec_mode(enum StereoMonoMode mode) {
103   codec_mode_ = mode;
104 }
105 
set_lost_packet(bool lost)106 void TestPackStereo::set_lost_packet(bool lost) {
107   lost_packet_ = lost;
108 }
109 
TestStereo(int test_mode)110 TestStereo::TestStereo(int test_mode)
111     : acm_a_(AudioCodingModule::Create()),
112       acm_b_(AudioCodingModule::Create()),
113       channel_a2b_(NULL),
114       test_cntr_(0),
115       pack_size_samp_(0),
116       pack_size_bytes_(0),
117       counter_(0),
118       g722_pltype_(0),
119       l16_8khz_pltype_(-1),
120       l16_16khz_pltype_(-1),
121       l16_32khz_pltype_(-1)
122 #ifdef PCMA_AND_PCMU
123       , pcma_pltype_(-1)
124       , pcmu_pltype_(-1)
125 #endif
126 #ifdef WEBRTC_CODEC_OPUS
127       , opus_pltype_(-1)
128 #endif
129 {
130   // test_mode = 0 for silent test (auto test)
131   test_mode_ = test_mode;
132 }
133 
~TestStereo()134 TestStereo::~TestStereo() {
135   if (channel_a2b_ != NULL) {
136     delete channel_a2b_;
137     channel_a2b_ = NULL;
138   }
139 }
140 
Perform()141 void TestStereo::Perform() {
142   uint16_t frequency_hz;
143   int audio_channels;
144   int codec_channels;
145   bool dtx;
146   bool vad;
147   ACMVADMode vad_mode;
148 
149   // Open both mono and stereo test files in 32 kHz.
150   const std::string file_name_stereo = webrtc::test::ResourcePath(
151       "audio_coding/teststereo32kHz", "pcm");
152   const std::string file_name_mono = webrtc::test::ResourcePath(
153       "audio_coding/testfile32kHz", "pcm");
154   frequency_hz = 32000;
155   in_file_stereo_ = new PCMFile();
156   in_file_mono_ = new PCMFile();
157   in_file_stereo_->Open(file_name_stereo, frequency_hz, "rb");
158   in_file_stereo_->ReadStereo(true);
159   in_file_mono_->Open(file_name_mono, frequency_hz, "rb");
160   in_file_mono_->ReadStereo(false);
161 
162   // Create and initialize two ACMs, one for each side of a one-to-one call.
163   ASSERT_TRUE((acm_a_.get() != NULL) && (acm_b_.get() != NULL));
164   EXPECT_EQ(0, acm_a_->InitializeReceiver());
165   EXPECT_EQ(0, acm_b_->InitializeReceiver());
166 
167   // Register all available codes as receiving codecs.
168   uint8_t num_encoders = acm_a_->NumberOfCodecs();
169   CodecInst my_codec_param;
170   for (uint8_t n = 0; n < num_encoders; n++) {
171     EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param));
172     EXPECT_EQ(true, acm_b_->RegisterReceiveCodec(
173                         my_codec_param.pltype, CodecInstToSdp(my_codec_param)));
174   }
175 
176   // Test that unregister all receive codecs works.
177   for (uint8_t n = 0; n < num_encoders; n++) {
178     EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param));
179     EXPECT_EQ(0, acm_b_->UnregisterReceiveCodec(my_codec_param.pltype));
180   }
181 
182   // Register all available codes as receiving codecs once more.
183   for (uint8_t n = 0; n < num_encoders; n++) {
184     EXPECT_EQ(0, acm_b_->Codec(n, &my_codec_param));
185     EXPECT_EQ(true, acm_b_->RegisterReceiveCodec(
186                         my_codec_param.pltype, CodecInstToSdp(my_codec_param)));
187   }
188 
189   // Create and connect the channel.
190   channel_a2b_ = new TestPackStereo;
191   EXPECT_EQ(0, acm_a_->RegisterTransportCallback(channel_a2b_));
192   channel_a2b_->RegisterReceiverACM(acm_b_.get());
193 
194   // Start with setting VAD/DTX, before we know we will send stereo.
195   // Continue with setting a stereo codec as send codec and verify that
196   // VAD/DTX gets turned off.
197   EXPECT_EQ(0, acm_a_->SetVAD(true, true, VADNormal));
198   EXPECT_EQ(0, acm_a_->VAD(&dtx, &vad, &vad_mode));
199   EXPECT_TRUE(dtx);
200   EXPECT_TRUE(vad);
201   char codec_pcma_temp[] = "PCMA";
202   RegisterSendCodec('A', codec_pcma_temp, 8000, 64000, 80, 2, pcma_pltype_);
203   EXPECT_EQ(0, acm_a_->VAD(&dtx, &vad, &vad_mode));
204   EXPECT_FALSE(dtx);
205   EXPECT_FALSE(vad);
206   if (test_mode_ != 0) {
207     printf("\n");
208   }
209 
210   //
211   // Test Stereo-To-Stereo for all codecs.
212   //
213   audio_channels = 2;
214   codec_channels = 2;
215 
216   // All codecs are tested for all allowed sampling frequencies, rates and
217   // packet sizes.
218   if (test_mode_ != 0) {
219     printf("===========================================================\n");
220     printf("Test number: %d\n", test_cntr_ + 1);
221     printf("Test type: Stereo-to-stereo\n");
222   }
223   channel_a2b_->set_codec_mode(kStereo);
224   test_cntr_++;
225   OpenOutFile(test_cntr_);
226   char codec_g722[] = "G722";
227   RegisterSendCodec('A', codec_g722, 16000, 64000, 160, codec_channels,
228       g722_pltype_);
229   Run(channel_a2b_, audio_channels, codec_channels);
230   RegisterSendCodec('A', codec_g722, 16000, 64000, 320, codec_channels,
231       g722_pltype_);
232   Run(channel_a2b_, audio_channels, codec_channels);
233   RegisterSendCodec('A', codec_g722, 16000, 64000, 480, codec_channels,
234       g722_pltype_);
235   Run(channel_a2b_, audio_channels, codec_channels);
236   RegisterSendCodec('A', codec_g722, 16000, 64000, 640, codec_channels,
237       g722_pltype_);
238   Run(channel_a2b_, audio_channels, codec_channels);
239   RegisterSendCodec('A', codec_g722, 16000, 64000, 800, codec_channels,
240       g722_pltype_);
241   Run(channel_a2b_, audio_channels, codec_channels);
242   RegisterSendCodec('A', codec_g722, 16000, 64000, 960, codec_channels,
243       g722_pltype_);
244   Run(channel_a2b_, audio_channels, codec_channels);
245   out_file_.Close();
246 
247   if (test_mode_ != 0) {
248     printf("===========================================================\n");
249     printf("Test number: %d\n", test_cntr_ + 1);
250     printf("Test type: Stereo-to-stereo\n");
251   }
252   channel_a2b_->set_codec_mode(kStereo);
253   test_cntr_++;
254   OpenOutFile(test_cntr_);
255   char codec_l16[] = "L16";
256   RegisterSendCodec('A', codec_l16, 8000, 128000, 80, codec_channels,
257       l16_8khz_pltype_);
258   Run(channel_a2b_, audio_channels, codec_channels);
259   RegisterSendCodec('A', codec_l16, 8000, 128000, 160, codec_channels,
260       l16_8khz_pltype_);
261   Run(channel_a2b_, audio_channels, codec_channels);
262   RegisterSendCodec('A', codec_l16, 8000, 128000, 240, codec_channels,
263       l16_8khz_pltype_);
264   Run(channel_a2b_, audio_channels, codec_channels);
265   RegisterSendCodec('A', codec_l16, 8000, 128000, 320, codec_channels,
266       l16_8khz_pltype_);
267   Run(channel_a2b_, audio_channels, codec_channels);
268   out_file_.Close();
269 
270   if (test_mode_ != 0) {
271     printf("===========================================================\n");
272     printf("Test number: %d\n", test_cntr_ + 1);
273     printf("Test type: Stereo-to-stereo\n");
274   }
275   test_cntr_++;
276   OpenOutFile(test_cntr_);
277   RegisterSendCodec('A', codec_l16, 16000, 256000, 160, codec_channels,
278       l16_16khz_pltype_);
279   Run(channel_a2b_, audio_channels, codec_channels);
280   RegisterSendCodec('A', codec_l16, 16000, 256000, 320, codec_channels,
281       l16_16khz_pltype_);
282   Run(channel_a2b_, audio_channels, codec_channels);
283   RegisterSendCodec('A', codec_l16, 16000, 256000, 480, codec_channels,
284       l16_16khz_pltype_);
285   Run(channel_a2b_, audio_channels, codec_channels);
286   RegisterSendCodec('A', codec_l16, 16000, 256000, 640, codec_channels,
287       l16_16khz_pltype_);
288   Run(channel_a2b_, audio_channels, codec_channels);
289   out_file_.Close();
290 
291   if (test_mode_ != 0) {
292     printf("===========================================================\n");
293     printf("Test number: %d\n", test_cntr_ + 1);
294     printf("Test type: Stereo-to-stereo\n");
295   }
296   test_cntr_++;
297   OpenOutFile(test_cntr_);
298   RegisterSendCodec('A', codec_l16, 32000, 512000, 320, codec_channels,
299       l16_32khz_pltype_);
300   Run(channel_a2b_, audio_channels, codec_channels);
301   RegisterSendCodec('A', codec_l16, 32000, 512000, 640, codec_channels,
302       l16_32khz_pltype_);
303   Run(channel_a2b_, audio_channels, codec_channels);
304   out_file_.Close();
305 #ifdef PCMA_AND_PCMU
306   if (test_mode_ != 0) {
307     printf("===========================================================\n");
308     printf("Test number: %d\n", test_cntr_ + 1);
309     printf("Test type: Stereo-to-stereo\n");
310   }
311   channel_a2b_->set_codec_mode(kStereo);
312   audio_channels = 2;
313   codec_channels = 2;
314   test_cntr_++;
315   OpenOutFile(test_cntr_);
316   char codec_pcma[] = "PCMA";
317   RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, codec_channels,
318                     pcma_pltype_);
319   Run(channel_a2b_, audio_channels, codec_channels);
320   RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, codec_channels,
321                     pcma_pltype_);
322   Run(channel_a2b_, audio_channels, codec_channels);
323   RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, codec_channels,
324                     pcma_pltype_);
325   Run(channel_a2b_, audio_channels, codec_channels);
326   RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, codec_channels,
327                     pcma_pltype_);
328   Run(channel_a2b_, audio_channels, codec_channels);
329   RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, codec_channels,
330                     pcma_pltype_);
331   Run(channel_a2b_, audio_channels, codec_channels);
332   RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, codec_channels,
333                     pcma_pltype_);
334   Run(channel_a2b_, audio_channels, codec_channels);
335 
336   // Test that VAD/DTX cannot be turned on while sending stereo.
337   EXPECT_EQ(-1, acm_a_->SetVAD(true, true, VADNormal));
338   EXPECT_EQ(0, acm_a_->VAD(&dtx, &vad, &vad_mode));
339   EXPECT_FALSE(dtx);
340   EXPECT_FALSE(vad);
341   EXPECT_EQ(0, acm_a_->SetVAD(false, false, VADNormal));
342   EXPECT_EQ(0, acm_a_->VAD(&dtx, &vad, &vad_mode));
343   EXPECT_FALSE(dtx);
344   EXPECT_FALSE(vad);
345 
346   out_file_.Close();
347   if (test_mode_ != 0) {
348     printf("===========================================================\n");
349     printf("Test number: %d\n", test_cntr_ + 1);
350     printf("Test type: Stereo-to-stereo\n");
351   }
352   test_cntr_++;
353   OpenOutFile(test_cntr_);
354   char codec_pcmu[] = "PCMU";
355   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, codec_channels,
356                     pcmu_pltype_);
357   Run(channel_a2b_, audio_channels, codec_channels);
358   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, codec_channels,
359                     pcmu_pltype_);
360   Run(channel_a2b_, audio_channels, codec_channels);
361   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, codec_channels,
362                     pcmu_pltype_);
363   Run(channel_a2b_, audio_channels, codec_channels);
364   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, codec_channels,
365                     pcmu_pltype_);
366   Run(channel_a2b_, audio_channels, codec_channels);
367   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, codec_channels,
368                     pcmu_pltype_);
369   Run(channel_a2b_, audio_channels, codec_channels);
370   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, codec_channels,
371                     pcmu_pltype_);
372   Run(channel_a2b_, audio_channels, codec_channels);
373   out_file_.Close();
374 #endif
375 #ifdef WEBRTC_CODEC_OPUS
376   if (test_mode_ != 0) {
377     printf("===========================================================\n");
378     printf("Test number: %d\n", test_cntr_ + 1);
379     printf("Test type: Stereo-to-stereo\n");
380   }
381   channel_a2b_->set_codec_mode(kStereo);
382   audio_channels = 2;
383   codec_channels = 2;
384   test_cntr_++;
385   OpenOutFile(test_cntr_);
386 
387   char codec_opus[] = "opus";
388   // Run Opus with 10 ms frame size.
389   RegisterSendCodec('A', codec_opus, 48000, 64000, 480, codec_channels,
390       opus_pltype_);
391   Run(channel_a2b_, audio_channels, codec_channels);
392   // Run Opus with 20 ms frame size.
393   RegisterSendCodec('A', codec_opus, 48000, 64000, 480*2, codec_channels,
394       opus_pltype_);
395   Run(channel_a2b_, audio_channels, codec_channels);
396   // Run Opus with 40 ms frame size.
397   RegisterSendCodec('A', codec_opus, 48000, 64000, 480*4, codec_channels,
398       opus_pltype_);
399   Run(channel_a2b_, audio_channels, codec_channels);
400   // Run Opus with 60 ms frame size.
401   RegisterSendCodec('A', codec_opus, 48000, 64000, 480*6, codec_channels,
402       opus_pltype_);
403   Run(channel_a2b_, audio_channels, codec_channels);
404   // Run Opus with 20 ms frame size and different bitrates.
405   RegisterSendCodec('A', codec_opus, 48000, 40000, 960, codec_channels,
406       opus_pltype_);
407   Run(channel_a2b_, audio_channels, codec_channels);
408   RegisterSendCodec('A', codec_opus, 48000, 510000, 960, codec_channels,
409       opus_pltype_);
410   Run(channel_a2b_, audio_channels, codec_channels);
411   out_file_.Close();
412 #endif
413   //
414   // Test Mono-To-Stereo for all codecs.
415   //
416   audio_channels = 1;
417   codec_channels = 2;
418 
419   if (test_mode_ != 0) {
420     printf("===============================================================\n");
421     printf("Test number: %d\n", test_cntr_ + 1);
422     printf("Test type: Mono-to-stereo\n");
423   }
424   test_cntr_++;
425   channel_a2b_->set_codec_mode(kStereo);
426   OpenOutFile(test_cntr_);
427   RegisterSendCodec('A', codec_g722, 16000, 64000, 160, codec_channels,
428       g722_pltype_);
429   Run(channel_a2b_, audio_channels, codec_channels);
430   out_file_.Close();
431 
432   if (test_mode_ != 0) {
433     printf("===============================================================\n");
434     printf("Test number: %d\n", test_cntr_ + 1);
435     printf("Test type: Mono-to-stereo\n");
436   }
437   test_cntr_++;
438   channel_a2b_->set_codec_mode(kStereo);
439   OpenOutFile(test_cntr_);
440   RegisterSendCodec('A', codec_l16, 8000, 128000, 80, codec_channels,
441       l16_8khz_pltype_);
442   Run(channel_a2b_, audio_channels, codec_channels);
443   out_file_.Close();
444   if (test_mode_ != 0) {
445     printf("===============================================================\n");
446     printf("Test number: %d\n", test_cntr_ + 1);
447     printf("Test type: Mono-to-stereo\n");
448   }
449   test_cntr_++;
450   OpenOutFile(test_cntr_);
451   RegisterSendCodec('A', codec_l16, 16000, 256000, 160, codec_channels,
452       l16_16khz_pltype_);
453   Run(channel_a2b_, audio_channels, codec_channels);
454   out_file_.Close();
455   if (test_mode_ != 0) {
456     printf("===============================================================\n");
457     printf("Test number: %d\n", test_cntr_ + 1);
458     printf("Test type: Mono-to-stereo\n");
459   }
460   test_cntr_++;
461   OpenOutFile(test_cntr_);
462   RegisterSendCodec('A', codec_l16, 32000, 512000, 320, codec_channels,
463       l16_32khz_pltype_);
464   Run(channel_a2b_, audio_channels, codec_channels);
465   out_file_.Close();
466 #ifdef PCMA_AND_PCMU
467   if (test_mode_ != 0) {
468     printf("===============================================================\n");
469     printf("Test number: %d\n", test_cntr_ + 1);
470     printf("Test type: Mono-to-stereo\n");
471   }
472   test_cntr_++;
473   channel_a2b_->set_codec_mode(kStereo);
474   OpenOutFile(test_cntr_);
475   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, codec_channels,
476                     pcmu_pltype_);
477   Run(channel_a2b_, audio_channels, codec_channels);
478   RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, codec_channels,
479                     pcma_pltype_);
480   Run(channel_a2b_, audio_channels, codec_channels);
481   out_file_.Close();
482 #endif
483 #ifdef WEBRTC_CODEC_OPUS
484   if (test_mode_ != 0) {
485     printf("===============================================================\n");
486     printf("Test number: %d\n", test_cntr_ + 1);
487     printf("Test type: Mono-to-stereo\n");
488   }
489 
490   // Keep encode and decode in stereo.
491   test_cntr_++;
492   channel_a2b_->set_codec_mode(kStereo);
493   OpenOutFile(test_cntr_);
494   RegisterSendCodec('A', codec_opus, 48000, 64000, 960, codec_channels,
495       opus_pltype_);
496   Run(channel_a2b_, audio_channels, codec_channels);
497 
498   // Encode in mono, decode in stereo mode.
499   RegisterSendCodec('A', codec_opus, 48000, 64000, 960, 1, opus_pltype_);
500   Run(channel_a2b_, audio_channels, codec_channels);
501   out_file_.Close();
502 #endif
503 
504   //
505   // Test Stereo-To-Mono for all codecs.
506   //
507   audio_channels = 2;
508   codec_channels = 1;
509   channel_a2b_->set_codec_mode(kMono);
510 
511   // Run stereo audio and mono codec.
512   if (test_mode_ != 0) {
513     printf("===============================================================\n");
514     printf("Test number: %d\n", test_cntr_ + 1);
515     printf("Test type: Stereo-to-mono\n");
516   }
517   test_cntr_++;
518   OpenOutFile(test_cntr_);
519   RegisterSendCodec('A', codec_g722, 16000, 64000, 160, codec_channels,
520       g722_pltype_);
521 
522   // Make sure it is possible to set VAD/CNG, now that we are sending mono
523   // again.
524   EXPECT_EQ(0, acm_a_->SetVAD(true, true, VADNormal));
525   EXPECT_EQ(0, acm_a_->VAD(&dtx, &vad, &vad_mode));
526   EXPECT_TRUE(dtx);
527   EXPECT_TRUE(vad);
528   EXPECT_EQ(0, acm_a_->SetVAD(false, false, VADNormal));
529   Run(channel_a2b_, audio_channels, codec_channels);
530   out_file_.Close();
531 
532   if (test_mode_ != 0) {
533     printf("===============================================================\n");
534     printf("Test number: %d\n", test_cntr_ + 1);
535     printf("Test type: Stereo-to-mono\n");
536   }
537   test_cntr_++;
538   OpenOutFile(test_cntr_);
539   RegisterSendCodec('A', codec_l16, 8000, 128000, 80, codec_channels,
540       l16_8khz_pltype_);
541   Run(channel_a2b_, audio_channels, codec_channels);
542   out_file_.Close();
543   if (test_mode_ != 0) {
544     printf("===============================================================\n");
545     printf("Test number: %d\n", test_cntr_ + 1);
546     printf("Test type: Stereo-to-mono\n");
547   }
548   test_cntr_++;
549   OpenOutFile(test_cntr_);
550   RegisterSendCodec('A', codec_l16, 16000, 256000, 160, codec_channels,
551       l16_16khz_pltype_);
552   Run(channel_a2b_, audio_channels, codec_channels);
553   out_file_.Close();
554   if (test_mode_ != 0) {
555     printf("==============================================================\n");
556     printf("Test number: %d\n", test_cntr_ + 1);
557     printf("Test type: Stereo-to-mono\n");
558   }
559   test_cntr_++;
560   OpenOutFile(test_cntr_);
561   RegisterSendCodec('A', codec_l16, 32000, 512000, 320, codec_channels,
562       l16_32khz_pltype_);
563   Run(channel_a2b_, audio_channels, codec_channels);
564   out_file_.Close();
565 #ifdef PCMA_AND_PCMU
566   if (test_mode_ != 0) {
567     printf("===============================================================\n");
568     printf("Test number: %d\n", test_cntr_ + 1);
569     printf("Test type: Stereo-to-mono\n");
570   }
571   test_cntr_++;
572   OpenOutFile(test_cntr_);
573   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, codec_channels,
574                     pcmu_pltype_);
575   Run(channel_a2b_, audio_channels, codec_channels);
576   RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, codec_channels,
577                     pcma_pltype_);
578   Run(channel_a2b_, audio_channels, codec_channels);
579   out_file_.Close();
580 #endif
581 #ifdef WEBRTC_CODEC_OPUS
582   if (test_mode_ != 0) {
583     printf("===============================================================\n");
584     printf("Test number: %d\n", test_cntr_ + 1);
585     printf("Test type: Stereo-to-mono\n");
586   }
587   test_cntr_++;
588   OpenOutFile(test_cntr_);
589   // Encode and decode in mono.
590   RegisterSendCodec('A', codec_opus, 48000, 32000, 960, codec_channels,
591       opus_pltype_);
592   CodecInst opus_codec_param;
593   for (uint8_t n = 0; n < num_encoders; n++) {
594     EXPECT_EQ(0, acm_b_->Codec(n, &opus_codec_param));
595     if (!strcmp(opus_codec_param.plname, "opus")) {
596       opus_codec_param.channels = 1;
597       EXPECT_EQ(true,
598                 acm_b_->RegisterReceiveCodec(opus_codec_param.pltype,
599                                              CodecInstToSdp(opus_codec_param)));
600       break;
601     }
602   }
603   Run(channel_a2b_, audio_channels, codec_channels);
604 
605   // Encode in stereo, decode in mono.
606   RegisterSendCodec('A', codec_opus, 48000, 32000, 960, 2, opus_pltype_);
607   Run(channel_a2b_, audio_channels, codec_channels);
608 
609   out_file_.Close();
610 
611   // Test switching between decoding mono and stereo for Opus.
612 
613   // Decode in mono.
614   test_cntr_++;
615   OpenOutFile(test_cntr_);
616   if (test_mode_ != 0) {
617     // Print out codec and settings
618     printf("Test number: %d\nCodec: Opus Freq: 48000 Rate :32000 PackSize: 960"
619         " Decode: mono\n", test_cntr_);
620   }
621   Run(channel_a2b_, audio_channels, codec_channels);
622   out_file_.Close();
623   // Decode in stereo.
624   test_cntr_++;
625   OpenOutFile(test_cntr_);
626   if (test_mode_ != 0) {
627     // Print out codec and settings
628     printf("Test number: %d\nCodec: Opus Freq: 48000 Rate :32000 PackSize: 960"
629         " Decode: stereo\n", test_cntr_);
630   }
631   opus_codec_param.channels = 2;
632   EXPECT_EQ(true,
633             acm_b_->RegisterReceiveCodec(opus_codec_param.pltype,
634                                          CodecInstToSdp(opus_codec_param)));
635   Run(channel_a2b_, audio_channels, 2);
636   out_file_.Close();
637   // Decode in mono.
638   test_cntr_++;
639   OpenOutFile(test_cntr_);
640   if (test_mode_ != 0) {
641     // Print out codec and settings
642     printf("Test number: %d\nCodec: Opus Freq: 48000 Rate :32000 PackSize: 960"
643         " Decode: mono\n", test_cntr_);
644   }
645   opus_codec_param.channels = 1;
646   EXPECT_EQ(true,
647             acm_b_->RegisterReceiveCodec(opus_codec_param.pltype,
648                                          CodecInstToSdp(opus_codec_param)));
649   Run(channel_a2b_, audio_channels, codec_channels);
650   out_file_.Close();
651 
652 #endif
653 
654   // Print out which codecs were tested, and which were not, in the run.
655   if (test_mode_ != 0) {
656     printf("\nThe following codecs was INCLUDED in the test:\n");
657     printf("   G.722\n");
658     printf("   PCM16\n");
659     printf("   G.711\n");
660 #ifdef WEBRTC_CODEC_OPUS
661     printf("   Opus\n");
662 #endif
663     printf("\nTo complete the test, listen to the %d number of output "
664            "files.\n",
665            test_cntr_);
666   }
667 
668   // Delete the file pointers.
669   delete in_file_stereo_;
670   delete in_file_mono_;
671 }
672 
673 // Register Codec to use in the test
674 //
675 // Input:   side             - which ACM to use, 'A' or 'B'
676 //          codec_name       - name to use when register the codec
677 //          sampling_freq_hz - sampling frequency in Herz
678 //          rate             - bitrate in bytes
679 //          pack_size        - packet size in samples
680 //          channels         - number of channels; 1 for mono, 2 for stereo
681 //          payload_type     - payload type for the codec
RegisterSendCodec(char side,char * codec_name,int32_t sampling_freq_hz,int rate,int pack_size,int channels,int payload_type)682 void TestStereo::RegisterSendCodec(char side, char* codec_name,
683                                    int32_t sampling_freq_hz, int rate,
684                                    int pack_size, int channels,
685                                    int payload_type) {
686   if (test_mode_ != 0) {
687     // Print out codec and settings
688     printf("Codec: %s Freq: %d Rate: %d PackSize: %d\n", codec_name,
689            sampling_freq_hz, rate, pack_size);
690   }
691 
692   // Store packet size in samples, used to validate the received packet
693   pack_size_samp_ = pack_size;
694 
695   // Store the expected packet size in bytes, used to validate the received
696   // packet. Add 0.875 to always round up to a whole byte.
697   pack_size_bytes_ = (uint16_t)(static_cast<float>(pack_size * rate) /
698                                     static_cast<float>(sampling_freq_hz * 8) +
699                                 0.875);
700 
701   // Set pointer to the ACM where to register the codec
702   AudioCodingModule* my_acm = NULL;
703   switch (side) {
704     case 'A': {
705       my_acm = acm_a_.get();
706       break;
707     }
708     case 'B': {
709       my_acm = acm_b_.get();
710       break;
711     }
712     default:
713       break;
714   }
715   ASSERT_TRUE(my_acm != NULL);
716 
717   CodecInst my_codec_param;
718   // Get all codec parameters before registering
719   EXPECT_GT(AudioCodingModule::Codec(codec_name, &my_codec_param,
720                                      sampling_freq_hz, channels), -1);
721   my_codec_param.rate = rate;
722   my_codec_param.pacsize = pack_size;
723   EXPECT_EQ(0, my_acm->RegisterSendCodec(my_codec_param));
724 
725   send_codec_name_ = codec_name;
726 }
727 
Run(TestPackStereo * channel,int in_channels,int out_channels,int percent_loss)728 void TestStereo::Run(TestPackStereo* channel, int in_channels, int out_channels,
729                      int percent_loss) {
730   AudioFrame audio_frame;
731 
732   int32_t out_freq_hz_b = out_file_.SamplingFrequency();
733   uint16_t rec_size;
734   uint32_t time_stamp_diff;
735   channel->reset_payload_size();
736   int error_count = 0;
737   int variable_bytes = 0;
738   int variable_packets = 0;
739   // Set test length to 500 ms (50 blocks of 10 ms each).
740   in_file_mono_->SetNum10MsBlocksToRead(50);
741   in_file_stereo_->SetNum10MsBlocksToRead(50);
742   // Fast-forward 1 second (100 blocks) since the files start with silence.
743   in_file_stereo_->FastForward(100);
744   in_file_mono_->FastForward(100);
745 
746   while (1) {
747     // Simulate packet loss by setting |packet_loss_| to "true" in
748     // |percent_loss| percent of the loops.
749     if (percent_loss > 0) {
750       if (counter_ == floor((100 / percent_loss) + 0.5)) {
751         counter_ = 0;
752         channel->set_lost_packet(true);
753       } else {
754         channel->set_lost_packet(false);
755       }
756       counter_++;
757     }
758 
759     // Add 10 msec to ACM
760     if (in_channels == 1) {
761       if (in_file_mono_->EndOfFile()) {
762         break;
763       }
764       in_file_mono_->Read10MsData(audio_frame);
765     } else {
766       if (in_file_stereo_->EndOfFile()) {
767         break;
768       }
769       in_file_stereo_->Read10MsData(audio_frame);
770     }
771     EXPECT_GE(acm_a_->Add10MsData(audio_frame), 0);
772 
773     // Verify that the received packet size matches the settings.
774     rec_size = channel->payload_size();
775     if ((0 < rec_size) & (rec_size < 65535)) {
776       if (strcmp(send_codec_name_, "opus") == 0) {
777         // Opus is a variable rate codec, hence calculate the average packet
778         // size, and later make sure the average is in the right range.
779         variable_bytes += rec_size;
780         variable_packets++;
781       } else {
782         // For fixed rate codecs, check that packet size is correct.
783         if ((rec_size != pack_size_bytes_ * out_channels)
784             && (pack_size_bytes_ < 65535)) {
785           error_count++;
786         }
787       }
788       // Verify that the timestamp is updated with expected length
789       time_stamp_diff = channel->timestamp_diff();
790       if ((counter_ > 10) && (time_stamp_diff != pack_size_samp_)) {
791         error_count++;
792       }
793     }
794 
795     // Run received side of ACM
796     bool muted;
797     EXPECT_EQ(0, acm_b_->PlayoutData10Ms(out_freq_hz_b, &audio_frame, &muted));
798     ASSERT_FALSE(muted);
799 
800     // Write output speech to file
801     out_file_.Write10MsData(
802         audio_frame.data(),
803         audio_frame.samples_per_channel_ * audio_frame.num_channels_);
804   }
805 
806   EXPECT_EQ(0, error_count);
807 
808   // Check that packet size is in the right range for variable rate codecs,
809   // such as Opus.
810   if (variable_packets > 0) {
811     variable_bytes /= variable_packets;
812     EXPECT_NEAR(variable_bytes, pack_size_bytes_, 18);
813   }
814 
815   if (in_file_mono_->EndOfFile()) {
816     in_file_mono_->Rewind();
817   }
818   if (in_file_stereo_->EndOfFile()) {
819     in_file_stereo_->Rewind();
820   }
821   // Reset in case we ended with a lost packet
822   channel->set_lost_packet(false);
823 }
824 
OpenOutFile(int16_t test_number)825 void TestStereo::OpenOutFile(int16_t test_number) {
826   std::string file_name;
827   std::stringstream file_stream;
828   file_stream << webrtc::test::OutputPath() << "teststereo_out_" << test_number
829       << ".pcm";
830   file_name = file_stream.str();
831   out_file_.Open(file_name, 32000, "wb");
832 }
833 
DisplaySendReceiveCodec()834 void TestStereo::DisplaySendReceiveCodec() {
835   auto send_codec = acm_a_->SendCodec();
836   if (test_mode_ != 0) {
837     ASSERT_TRUE(send_codec);
838     printf("%s -> ", send_codec->plname);
839   }
840   CodecInst receive_codec;
841   acm_b_->ReceiveCodec(&receive_codec);
842   if (test_mode_ != 0) {
843     printf("%s\n", receive_codec.plname);
844   }
845 }
846 
847 }  // namespace webrtc
848