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/TestAllCodecs.h"
12 
13 #include <cstdio>
14 #include <limits>
15 #include <string>
16 
17 #include "testing/gtest/include/gtest/gtest.h"
18 
19 #include "webrtc/common_types.h"
20 #include "webrtc/engine_configurations.h"
21 #include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
22 #include "webrtc/modules/audio_coding/main/interface/audio_coding_module_typedefs.h"
23 #include "webrtc/modules/audio_coding/main/test/utility.h"
24 #include "webrtc/system_wrappers/interface/trace.h"
25 #include "webrtc/test/testsupport/fileutils.h"
26 #include "webrtc/typedefs.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 namespace {
37 const size_t kVariableSize = std::numeric_limits<size_t>::max();
38 }
39 
40 namespace webrtc {
41 
42 // Class for simulating packet handling.
TestPack()43 TestPack::TestPack()
44     : receiver_acm_(NULL),
45       sequence_number_(0),
46       timestamp_diff_(0),
47       last_in_timestamp_(0),
48       total_bytes_(0),
49       payload_size_(0) {
50 }
51 
~TestPack()52 TestPack::~TestPack() {
53 }
54 
RegisterReceiverACM(AudioCodingModule * acm)55 void TestPack::RegisterReceiverACM(AudioCodingModule* acm) {
56   receiver_acm_ = acm;
57   return;
58 }
59 
SendData(FrameType frame_type,uint8_t payload_type,uint32_t timestamp,const uint8_t * payload_data,size_t payload_size,const RTPFragmentationHeader * fragmentation)60 int32_t TestPack::SendData(FrameType frame_type, uint8_t payload_type,
61                            uint32_t timestamp, const uint8_t* payload_data,
62                            size_t payload_size,
63                            const RTPFragmentationHeader* fragmentation) {
64   WebRtcRTPHeader rtp_info;
65   int32_t status;
66 
67   rtp_info.header.markerBit = false;
68   rtp_info.header.ssrc = 0;
69   rtp_info.header.sequenceNumber = sequence_number_++;
70   rtp_info.header.payloadType = payload_type;
71   rtp_info.header.timestamp = timestamp;
72   if (frame_type == kAudioFrameCN) {
73     rtp_info.type.Audio.isCNG = true;
74   } else {
75     rtp_info.type.Audio.isCNG = false;
76   }
77   if (frame_type == kFrameEmpty) {
78     // Skip this frame.
79     return 0;
80   }
81 
82   // Only run mono for all test cases.
83   rtp_info.type.Audio.channel = 1;
84   memcpy(payload_data_, payload_data, payload_size);
85 
86   status = receiver_acm_->IncomingPacket(payload_data_, payload_size, rtp_info);
87 
88   payload_size_ = payload_size;
89   timestamp_diff_ = timestamp - last_in_timestamp_;
90   last_in_timestamp_ = timestamp;
91   total_bytes_ += payload_size;
92   return status;
93 }
94 
payload_size()95 size_t TestPack::payload_size() {
96   return payload_size_;
97 }
98 
timestamp_diff()99 uint32_t TestPack::timestamp_diff() {
100   return timestamp_diff_;
101 }
102 
reset_payload_size()103 void TestPack::reset_payload_size() {
104   payload_size_ = 0;
105 }
106 
TestAllCodecs(int test_mode)107 TestAllCodecs::TestAllCodecs(int test_mode)
108     : acm_a_(AudioCodingModule::Create(0)),
109       acm_b_(AudioCodingModule::Create(1)),
110       channel_a_to_b_(NULL),
111       test_count_(0),
112       packet_size_samples_(0),
113       packet_size_bytes_(0) {
114   // test_mode = 0 for silent test (auto test)
115   test_mode_ = test_mode;
116 }
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 = webrtc::test::ResourcePath(
127       "audio_coding/testfile32kHz", "pcm");
128   infile_a_.Open(file_name, 32000, "rb");
129 
130   if (test_mode_ == 0) {
131     WEBRTC_TRACE(kTraceStateInfo, kTraceAudioCoding, -1,
132                  "---------- TestAllCodecs ----------");
133   }
134 
135   acm_a_->InitializeReceiver();
136   acm_b_->InitializeReceiver();
137 
138   uint8_t num_encoders = acm_a_->NumberOfCodecs();
139   CodecInst my_codec_param;
140   for (uint8_t n = 0; n < num_encoders; n++) {
141     acm_b_->Codec(n, &my_codec_param);
142     if (!strcmp(my_codec_param.plname, "opus")) {
143       my_codec_param.channels = 1;
144     }
145     acm_b_->RegisterReceiveCodec(my_codec_param);
146   }
147 
148   // Create and connect the channel
149   channel_a_to_b_ = new TestPack;
150   acm_a_->RegisterTransportCallback(channel_a_to_b_);
151   channel_a_to_b_->RegisterReceiverACM(acm_b_.get());
152 
153   // All codecs are tested for all allowed sampling frequencies, rates and
154   // packet sizes.
155 #ifdef WEBRTC_CODEC_G722
156   if (test_mode_ != 0) {
157     printf("===============================================================\n");
158   }
159   test_count_++;
160   OpenOutFile(test_count_);
161   char codec_g722[] = "G722";
162   RegisterSendCodec('A', codec_g722, 16000, 64000, 160, 0);
163   Run(channel_a_to_b_);
164   RegisterSendCodec('A', codec_g722, 16000, 64000, 320, 0);
165   Run(channel_a_to_b_);
166   RegisterSendCodec('A', codec_g722, 16000, 64000, 480, 0);
167   Run(channel_a_to_b_);
168   RegisterSendCodec('A', codec_g722, 16000, 64000, 640, 0);
169   Run(channel_a_to_b_);
170   RegisterSendCodec('A', codec_g722, 16000, 64000, 800, 0);
171   Run(channel_a_to_b_);
172   RegisterSendCodec('A', codec_g722, 16000, 64000, 960, 0);
173   Run(channel_a_to_b_);
174   outfile_b_.Close();
175 #endif
176 #ifdef WEBRTC_CODEC_ILBC
177   if (test_mode_ != 0) {
178     printf("===============================================================\n");
179   }
180   test_count_++;
181   OpenOutFile(test_count_);
182   char codec_ilbc[] = "ILBC";
183   RegisterSendCodec('A', codec_ilbc, 8000, 13300, 240, 0);
184   Run(channel_a_to_b_);
185   RegisterSendCodec('A', codec_ilbc, 8000, 13300, 480, 0);
186   Run(channel_a_to_b_);
187   RegisterSendCodec('A', codec_ilbc, 8000, 15200, 160, 0);
188   Run(channel_a_to_b_);
189   RegisterSendCodec('A', codec_ilbc, 8000, 15200, 320, 0);
190   Run(channel_a_to_b_);
191   outfile_b_.Close();
192 #endif
193 #if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
194   if (test_mode_ != 0) {
195     printf("===============================================================\n");
196   }
197   test_count_++;
198   OpenOutFile(test_count_);
199   char codec_isac[] = "ISAC";
200   RegisterSendCodec('A', codec_isac, 16000, -1, 480, kVariableSize);
201   Run(channel_a_to_b_);
202   RegisterSendCodec('A', codec_isac, 16000, -1, 960, kVariableSize);
203   Run(channel_a_to_b_);
204   RegisterSendCodec('A', codec_isac, 16000, 15000, 480, kVariableSize);
205   Run(channel_a_to_b_);
206   RegisterSendCodec('A', codec_isac, 16000, 32000, 960, kVariableSize);
207   Run(channel_a_to_b_);
208   outfile_b_.Close();
209 #endif
210 #ifdef WEBRTC_CODEC_ISAC
211   if (test_mode_ != 0) {
212     printf("===============================================================\n");
213   }
214   test_count_++;
215   OpenOutFile(test_count_);
216   RegisterSendCodec('A', codec_isac, 32000, -1, 960, kVariableSize);
217   Run(channel_a_to_b_);
218   RegisterSendCodec('A', codec_isac, 32000, 56000, 960, kVariableSize);
219   Run(channel_a_to_b_);
220   RegisterSendCodec('A', codec_isac, 32000, 37000, 960, kVariableSize);
221   Run(channel_a_to_b_);
222   RegisterSendCodec('A', codec_isac, 32000, 32000, 960, kVariableSize);
223   Run(channel_a_to_b_);
224   outfile_b_.Close();
225 #endif
226 #ifdef WEBRTC_CODEC_PCM16
227   if (test_mode_ != 0) {
228     printf("===============================================================\n");
229   }
230   test_count_++;
231   OpenOutFile(test_count_);
232   char codec_l16[] = "L16";
233   RegisterSendCodec('A', codec_l16, 8000, 128000, 80, 0);
234   Run(channel_a_to_b_);
235   RegisterSendCodec('A', codec_l16, 8000, 128000, 160, 0);
236   Run(channel_a_to_b_);
237   RegisterSendCodec('A', codec_l16, 8000, 128000, 240, 0);
238   Run(channel_a_to_b_);
239   RegisterSendCodec('A', codec_l16, 8000, 128000, 320, 0);
240   Run(channel_a_to_b_);
241   outfile_b_.Close();
242   if (test_mode_ != 0) {
243     printf("===============================================================\n");
244   }
245   test_count_++;
246   OpenOutFile(test_count_);
247   RegisterSendCodec('A', codec_l16, 16000, 256000, 160, 0);
248   Run(channel_a_to_b_);
249   RegisterSendCodec('A', codec_l16, 16000, 256000, 320, 0);
250   Run(channel_a_to_b_);
251   RegisterSendCodec('A', codec_l16, 16000, 256000, 480, 0);
252   Run(channel_a_to_b_);
253   RegisterSendCodec('A', codec_l16, 16000, 256000, 640, 0);
254   Run(channel_a_to_b_);
255   outfile_b_.Close();
256   if (test_mode_ != 0) {
257     printf("===============================================================\n");
258   }
259   test_count_++;
260   OpenOutFile(test_count_);
261   RegisterSendCodec('A', codec_l16, 32000, 512000, 320, 0);
262   Run(channel_a_to_b_);
263   RegisterSendCodec('A', codec_l16, 32000, 512000, 640, 0);
264   Run(channel_a_to_b_);
265   outfile_b_.Close();
266 #endif
267   if (test_mode_ != 0) {
268     printf("===============================================================\n");
269   }
270   test_count_++;
271   OpenOutFile(test_count_);
272   char codec_pcma[] = "PCMA";
273   RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, 0);
274   Run(channel_a_to_b_);
275   RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, 0);
276   Run(channel_a_to_b_);
277   RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, 0);
278   Run(channel_a_to_b_);
279   RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, 0);
280   Run(channel_a_to_b_);
281   RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, 0);
282   Run(channel_a_to_b_);
283   RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, 0);
284   Run(channel_a_to_b_);
285   if (test_mode_ != 0) {
286     printf("===============================================================\n");
287   }
288   char codec_pcmu[] = "PCMU";
289   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, 0);
290   Run(channel_a_to_b_);
291   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, 0);
292   Run(channel_a_to_b_);
293   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, 0);
294   Run(channel_a_to_b_);
295   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, 0);
296   Run(channel_a_to_b_);
297   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, 0);
298   Run(channel_a_to_b_);
299   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, 0);
300   Run(channel_a_to_b_);
301   outfile_b_.Close();
302 #ifdef WEBRTC_CODEC_OPUS
303   if (test_mode_ != 0) {
304     printf("===============================================================\n");
305   }
306   test_count_++;
307   OpenOutFile(test_count_);
308   char codec_opus[] = "OPUS";
309   RegisterSendCodec('A', codec_opus, 48000, 6000, 480, kVariableSize);
310   Run(channel_a_to_b_);
311   RegisterSendCodec('A', codec_opus, 48000, 20000, 480*2, kVariableSize);
312   Run(channel_a_to_b_);
313   RegisterSendCodec('A', codec_opus, 48000, 32000, 480*4, kVariableSize);
314   Run(channel_a_to_b_);
315   RegisterSendCodec('A', codec_opus, 48000, 48000, 480, kVariableSize);
316   Run(channel_a_to_b_);
317   RegisterSendCodec('A', codec_opus, 48000, 64000, 480*4, kVariableSize);
318   Run(channel_a_to_b_);
319   RegisterSendCodec('A', codec_opus, 48000, 96000, 480*6, kVariableSize);
320   Run(channel_a_to_b_);
321   RegisterSendCodec('A', codec_opus, 48000, 500000, 480*2, kVariableSize);
322   Run(channel_a_to_b_);
323   outfile_b_.Close();
324 #endif
325   if (test_mode_ != 0) {
326     printf("===============================================================\n");
327 
328     /* Print out all codecs that were not tested in the run */
329     printf("The following codecs was not included in the test:\n");
330 #ifndef WEBRTC_CODEC_G722
331     printf("   G.722\n");
332 #endif
333 #ifndef WEBRTC_CODEC_ILBC
334     printf("   iLBC\n");
335 #endif
336 #ifndef WEBRTC_CODEC_ISAC
337     printf("   ISAC float\n");
338 #endif
339 #ifndef WEBRTC_CODEC_ISACFX
340     printf("   ISAC fix\n");
341 #endif
342 #ifndef WEBRTC_CODEC_PCM16
343     printf("   PCM16\n");
344 #endif
345 
346     printf("\nTo complete the test, listen to the %d number of output files.\n",
347            test_count_);
348   }
349 }
350 
351 // Register Codec to use in the test
352 //
353 // Input:  side             - which ACM to use, 'A' or 'B'
354 //         codec_name       - name to use when register the codec
355 //         sampling_freq_hz - sampling frequency in Herz
356 //         rate             - bitrate in bytes
357 //         packet_size      - packet size in samples
358 //         extra_byte       - if extra bytes needed compared to the bitrate
359 //                            used when registering, can be an internal header
360 //                            set to kVariableSize if the codec is a variable
361 //                            rate codec
RegisterSendCodec(char side,char * codec_name,int32_t sampling_freq_hz,int rate,int packet_size,size_t extra_byte)362 void TestAllCodecs::RegisterSendCodec(char side, char* codec_name,
363                                       int32_t sampling_freq_hz, int rate,
364                                       int packet_size, size_t extra_byte) {
365   if (test_mode_ != 0) {
366     // Print out codec and settings.
367     printf("codec: %s Freq: %d Rate: %d PackSize: %d\n", codec_name,
368            sampling_freq_hz, rate, packet_size);
369   }
370 
371   // Store packet-size in samples, used to validate the received packet.
372   // If G.722, store half the size to compensate for the timestamp bug in the
373   // RFC for G.722.
374   // If iSAC runs in adaptive mode, packet size in samples can change on the
375   // fly, so we exclude this test by setting |packet_size_samples_| to -1.
376   if (!strcmp(codec_name, "G722")) {
377     packet_size_samples_ = packet_size / 2;
378   } else if (!strcmp(codec_name, "ISAC") && (rate == -1)) {
379     packet_size_samples_ = -1;
380   } else {
381     packet_size_samples_ = packet_size;
382   }
383 
384   // Store the expected packet size in bytes, used to validate the received
385   // packet. If variable rate codec (extra_byte == -1), set to -1.
386   if (extra_byte != kVariableSize) {
387     // Add 0.875 to always round up to a whole byte
388     packet_size_bytes_ = static_cast<size_t>(
389         static_cast<float>(packet_size * rate) /
390         static_cast<float>(sampling_freq_hz * 8) + 0.875) + extra_byte;
391   } else {
392     // Packets will have a variable size.
393     packet_size_bytes_ = kVariableSize;
394   }
395 
396   // Set pointer to the ACM where to register the codec.
397   AudioCodingModule* my_acm = NULL;
398   switch (side) {
399     case 'A': {
400       my_acm = acm_a_.get();
401       break;
402     }
403     case 'B': {
404       my_acm = acm_b_.get();
405       break;
406     }
407     default: {
408       break;
409     }
410   }
411   ASSERT_TRUE(my_acm != NULL);
412 
413   // Get all codec parameters before registering
414   CodecInst my_codec_param;
415   CHECK_ERROR(AudioCodingModule::Codec(codec_name, &my_codec_param,
416                                        sampling_freq_hz, 1));
417   my_codec_param.rate = rate;
418   my_codec_param.pacsize = packet_size;
419   CHECK_ERROR(my_acm->RegisterSendCodec(my_codec_param));
420 }
421 
Run(TestPack * channel)422 void TestAllCodecs::Run(TestPack* channel) {
423   AudioFrame audio_frame;
424 
425   int32_t out_freq_hz = outfile_b_.SamplingFrequency();
426   size_t receive_size;
427   uint32_t timestamp_diff;
428   channel->reset_payload_size();
429   int error_count = 0;
430 
431   int counter = 0;
432   while (!infile_a_.EndOfFile()) {
433     // Add 10 msec to ACM.
434     infile_a_.Read10MsData(audio_frame);
435     CHECK_ERROR(acm_a_->Add10MsData(audio_frame));
436 
437     // Verify that the received packet size matches the settings.
438     receive_size = channel->payload_size();
439     if (receive_size) {
440       if ((receive_size != packet_size_bytes_) &&
441           (packet_size_bytes_ != kVariableSize)) {
442         error_count++;
443       }
444 
445       // Verify that the timestamp is updated with expected length. The counter
446       // is used to avoid problems when switching codec or frame size in the
447       // test.
448       timestamp_diff = channel->timestamp_diff();
449       if ((counter > 10) &&
450           (static_cast<int>(timestamp_diff) != packet_size_samples_) &&
451           (packet_size_samples_ > -1))
452         error_count++;
453     }
454 
455     // Run received side of ACM.
456     CHECK_ERROR(acm_b_->PlayoutData10Ms(out_freq_hz, &audio_frame));
457 
458     // Write output speech to file.
459     outfile_b_.Write10MsData(audio_frame.data_,
460                              audio_frame.samples_per_channel_);
461 
462     // Update loop counter
463     counter++;
464   }
465 
466   EXPECT_EQ(0, error_count);
467 
468   if (infile_a_.EndOfFile()) {
469     infile_a_.Rewind();
470   }
471 }
472 
OpenOutFile(int test_number)473 void TestAllCodecs::OpenOutFile(int test_number) {
474   std::string filename = webrtc::test::OutputPath();
475   std::ostringstream test_number_str;
476   test_number_str << test_number;
477   filename += "testallcodecs_out_";
478   filename += test_number_str.str();
479   filename += ".pcm";
480   outfile_b_.Open(filename, 32000, "wb");
481 }
482 
DisplaySendReceiveCodec()483 void TestAllCodecs::DisplaySendReceiveCodec() {
484   CodecInst my_codec_param;
485   acm_a_->SendCodec(&my_codec_param);
486   printf("%s -> ", my_codec_param.plname);
487   acm_b_->ReceiveCodec(&my_codec_param);
488   printf("%s\n", my_codec_param.plname);
489 }
490 
491 }  // namespace webrtc
492