• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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({{103, {"ISAC", 16000, 1}},
134                             {104, {"ISAC", 32000, 1}},
135                             {107, {"L16", 8000, 1}},
136                             {108, {"L16", 16000, 1}},
137                             {109, {"L16", 32000, 1}},
138                             {111, {"L16", 8000, 2}},
139                             {112, {"L16", 16000, 2}},
140                             {113, {"L16", 32000, 2}},
141                             {0, {"PCMU", 8000, 1}},
142                             {110, {"PCMU", 8000, 2}},
143                             {8, {"PCMA", 8000, 1}},
144                             {118, {"PCMA", 8000, 2}},
145                             {102, {"ILBC", 8000, 1}},
146                             {9, {"G722", 8000, 1}},
147                             {119, {"G722", 8000, 2}},
148                             {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}},
149                             {13, {"CN", 8000, 1}},
150                             {98, {"CN", 16000, 1}},
151                             {99, {"CN", 32000, 1}}});
152 
153   // Create and connect the channel
154   channel_a_to_b_ = new TestPack;
155   acm_a_->RegisterTransportCallback(channel_a_to_b_);
156   channel_a_to_b_->RegisterReceiverACM(acm_b_.get());
157 
158   // All codecs are tested for all allowed sampling frequencies, rates and
159   // packet sizes.
160   test_count_++;
161   OpenOutFile(test_count_);
162   char codec_g722[] = "G722";
163   RegisterSendCodec('A', codec_g722, 16000, 64000, 160, 0);
164   Run(channel_a_to_b_);
165   RegisterSendCodec('A', codec_g722, 16000, 64000, 320, 0);
166   Run(channel_a_to_b_);
167   RegisterSendCodec('A', codec_g722, 16000, 64000, 480, 0);
168   Run(channel_a_to_b_);
169   RegisterSendCodec('A', codec_g722, 16000, 64000, 640, 0);
170   Run(channel_a_to_b_);
171   RegisterSendCodec('A', codec_g722, 16000, 64000, 800, 0);
172   Run(channel_a_to_b_);
173   RegisterSendCodec('A', codec_g722, 16000, 64000, 960, 0);
174   Run(channel_a_to_b_);
175   outfile_b_.Close();
176 #ifdef WEBRTC_CODEC_ILBC
177   test_count_++;
178   OpenOutFile(test_count_);
179   char codec_ilbc[] = "ILBC";
180   RegisterSendCodec('A', codec_ilbc, 8000, 13300, 240, 0);
181   Run(channel_a_to_b_);
182   RegisterSendCodec('A', codec_ilbc, 8000, 13300, 480, 0);
183   Run(channel_a_to_b_);
184   RegisterSendCodec('A', codec_ilbc, 8000, 15200, 160, 0);
185   Run(channel_a_to_b_);
186   RegisterSendCodec('A', codec_ilbc, 8000, 15200, 320, 0);
187   Run(channel_a_to_b_);
188   outfile_b_.Close();
189 #endif
190 #if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
191   test_count_++;
192   OpenOutFile(test_count_);
193   char codec_isac[] = "ISAC";
194   RegisterSendCodec('A', codec_isac, 16000, -1, 480, kVariableSize);
195   Run(channel_a_to_b_);
196   RegisterSendCodec('A', codec_isac, 16000, -1, 960, kVariableSize);
197   Run(channel_a_to_b_);
198   RegisterSendCodec('A', codec_isac, 16000, 15000, 480, kVariableSize);
199   Run(channel_a_to_b_);
200   RegisterSendCodec('A', codec_isac, 16000, 32000, 960, kVariableSize);
201   Run(channel_a_to_b_);
202   outfile_b_.Close();
203 #endif
204 #ifdef WEBRTC_CODEC_ISAC
205   test_count_++;
206   OpenOutFile(test_count_);
207   RegisterSendCodec('A', codec_isac, 32000, -1, 960, kVariableSize);
208   Run(channel_a_to_b_);
209   RegisterSendCodec('A', codec_isac, 32000, 56000, 960, kVariableSize);
210   Run(channel_a_to_b_);
211   RegisterSendCodec('A', codec_isac, 32000, 37000, 960, kVariableSize);
212   Run(channel_a_to_b_);
213   RegisterSendCodec('A', codec_isac, 32000, 32000, 960, kVariableSize);
214   Run(channel_a_to_b_);
215   outfile_b_.Close();
216 #endif
217   test_count_++;
218   OpenOutFile(test_count_);
219   char codec_l16[] = "L16";
220   RegisterSendCodec('A', codec_l16, 8000, 128000, 80, 0);
221   Run(channel_a_to_b_);
222   RegisterSendCodec('A', codec_l16, 8000, 128000, 160, 0);
223   Run(channel_a_to_b_);
224   RegisterSendCodec('A', codec_l16, 8000, 128000, 240, 0);
225   Run(channel_a_to_b_);
226   RegisterSendCodec('A', codec_l16, 8000, 128000, 320, 0);
227   Run(channel_a_to_b_);
228   outfile_b_.Close();
229 
230   test_count_++;
231   OpenOutFile(test_count_);
232   RegisterSendCodec('A', codec_l16, 16000, 256000, 160, 0);
233   Run(channel_a_to_b_);
234   RegisterSendCodec('A', codec_l16, 16000, 256000, 320, 0);
235   Run(channel_a_to_b_);
236   RegisterSendCodec('A', codec_l16, 16000, 256000, 480, 0);
237   Run(channel_a_to_b_);
238   RegisterSendCodec('A', codec_l16, 16000, 256000, 640, 0);
239   Run(channel_a_to_b_);
240   outfile_b_.Close();
241 
242   test_count_++;
243   OpenOutFile(test_count_);
244   RegisterSendCodec('A', codec_l16, 32000, 512000, 320, 0);
245   Run(channel_a_to_b_);
246   RegisterSendCodec('A', codec_l16, 32000, 512000, 640, 0);
247   Run(channel_a_to_b_);
248   outfile_b_.Close();
249 
250   test_count_++;
251   OpenOutFile(test_count_);
252   char codec_pcma[] = "PCMA";
253   RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, 0);
254   Run(channel_a_to_b_);
255   RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, 0);
256   Run(channel_a_to_b_);
257   RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, 0);
258   Run(channel_a_to_b_);
259   RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, 0);
260   Run(channel_a_to_b_);
261   RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, 0);
262   Run(channel_a_to_b_);
263   RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, 0);
264   Run(channel_a_to_b_);
265 
266   char codec_pcmu[] = "PCMU";
267   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, 0);
268   Run(channel_a_to_b_);
269   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, 0);
270   Run(channel_a_to_b_);
271   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, 0);
272   Run(channel_a_to_b_);
273   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, 0);
274   Run(channel_a_to_b_);
275   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, 0);
276   Run(channel_a_to_b_);
277   RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, 0);
278   Run(channel_a_to_b_);
279   outfile_b_.Close();
280 #ifdef WEBRTC_CODEC_OPUS
281   test_count_++;
282   OpenOutFile(test_count_);
283   char codec_opus[] = "OPUS";
284   RegisterSendCodec('A', codec_opus, 48000, 6000, 480, kVariableSize);
285   Run(channel_a_to_b_);
286   RegisterSendCodec('A', codec_opus, 48000, 20000, 480 * 2, kVariableSize);
287   Run(channel_a_to_b_);
288   RegisterSendCodec('A', codec_opus, 48000, 32000, 480 * 4, kVariableSize);
289   Run(channel_a_to_b_);
290   RegisterSendCodec('A', codec_opus, 48000, 48000, 480, kVariableSize);
291   Run(channel_a_to_b_);
292   RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 4, kVariableSize);
293   Run(channel_a_to_b_);
294   RegisterSendCodec('A', codec_opus, 48000, 96000, 480 * 6, kVariableSize);
295   Run(channel_a_to_b_);
296   RegisterSendCodec('A', codec_opus, 48000, 500000, 480 * 2, kVariableSize);
297   Run(channel_a_to_b_);
298   outfile_b_.Close();
299 #endif
300 }
301 
302 // Register Codec to use in the test
303 //
304 // Input:  side             - which ACM to use, 'A' or 'B'
305 //         codec_name       - name to use when register the codec
306 //         sampling_freq_hz - sampling frequency in Herz
307 //         rate             - bitrate in bytes
308 //         packet_size      - packet size in samples
309 //         extra_byte       - if extra bytes needed compared to the bitrate
310 //                            used when registering, can be an internal header
311 //                            set to kVariableSize if the codec is a variable
312 //                            rate codec
RegisterSendCodec(char side,char * codec_name,int32_t sampling_freq_hz,int rate,int packet_size,size_t extra_byte)313 void TestAllCodecs::RegisterSendCodec(char side,
314                                       char* codec_name,
315                                       int32_t sampling_freq_hz,
316                                       int rate,
317                                       int packet_size,
318                                       size_t extra_byte) {
319   // Store packet-size in samples, used to validate the received packet.
320   // If G.722, store half the size to compensate for the timestamp bug in the
321   // RFC for G.722.
322   // If iSAC runs in adaptive mode, packet size in samples can change on the
323   // fly, so we exclude this test by setting |packet_size_samples_| to -1.
324   int clockrate_hz = sampling_freq_hz;
325   size_t num_channels = 1;
326   if (absl::EqualsIgnoreCase(codec_name, "G722")) {
327     packet_size_samples_ = packet_size / 2;
328     clockrate_hz = sampling_freq_hz / 2;
329   } else if (absl::EqualsIgnoreCase(codec_name, "ISAC") && (rate == -1)) {
330     packet_size_samples_ = -1;
331   } else if (absl::EqualsIgnoreCase(codec_name, "OPUS")) {
332     packet_size_samples_ = packet_size;
333     num_channels = 2;
334   } else {
335     packet_size_samples_ = packet_size;
336   }
337 
338   // Store the expected packet size in bytes, used to validate the received
339   // packet. If variable rate codec (extra_byte == -1), set to -1.
340   if (extra_byte != kVariableSize) {
341     // Add 0.875 to always round up to a whole byte
342     packet_size_bytes_ =
343         static_cast<size_t>(static_cast<float>(packet_size * rate) /
344                                 static_cast<float>(sampling_freq_hz * 8) +
345                             0.875) +
346         extra_byte;
347   } else {
348     // Packets will have a variable size.
349     packet_size_bytes_ = kVariableSize;
350   }
351 
352   // Set pointer to the ACM where to register the codec.
353   AudioCodingModule* my_acm = NULL;
354   switch (side) {
355     case 'A': {
356       my_acm = acm_a_.get();
357       break;
358     }
359     case 'B': {
360       my_acm = acm_b_.get();
361       break;
362     }
363     default: {
364       break;
365     }
366   }
367   ASSERT_TRUE(my_acm != NULL);
368 
369   auto factory = CreateBuiltinAudioEncoderFactory();
370   constexpr int payload_type = 17;
371   SdpAudioFormat format = {codec_name, clockrate_hz, num_channels};
372   format.parameters["ptime"] = rtc::ToString(rtc::CheckedDivExact(
373       packet_size, rtc::CheckedDivExact(sampling_freq_hz, 1000)));
374   my_acm->SetEncoder(
375       factory->MakeAudioEncoder(payload_type, format, absl::nullopt));
376 }
377 
Run(TestPack * channel)378 void TestAllCodecs::Run(TestPack* channel) {
379   AudioFrame audio_frame;
380 
381   int32_t out_freq_hz = outfile_b_.SamplingFrequency();
382   size_t receive_size;
383   uint32_t timestamp_diff;
384   channel->reset_payload_size();
385   int error_count = 0;
386   int counter = 0;
387   // Set test length to 500 ms (50 blocks of 10 ms each).
388   infile_a_.SetNum10MsBlocksToRead(50);
389   // Fast-forward 1 second (100 blocks) since the file starts with silence.
390   infile_a_.FastForward(100);
391 
392   while (!infile_a_.EndOfFile()) {
393     // Add 10 msec to ACM.
394     infile_a_.Read10MsData(audio_frame);
395     CHECK_ERROR(acm_a_->Add10MsData(audio_frame));
396 
397     // Verify that the received packet size matches the settings.
398     receive_size = channel->payload_size();
399     if (receive_size) {
400       if ((receive_size != packet_size_bytes_) &&
401           (packet_size_bytes_ != kVariableSize)) {
402         error_count++;
403       }
404 
405       // Verify that the timestamp is updated with expected length. The counter
406       // is used to avoid problems when switching codec or frame size in the
407       // test.
408       timestamp_diff = channel->timestamp_diff();
409       if ((counter > 10) &&
410           (static_cast<int>(timestamp_diff) != packet_size_samples_) &&
411           (packet_size_samples_ > -1))
412         error_count++;
413     }
414 
415     // Run received side of ACM.
416     bool muted;
417     CHECK_ERROR(acm_b_->PlayoutData10Ms(out_freq_hz, &audio_frame, &muted));
418     ASSERT_FALSE(muted);
419 
420     // Write output speech to file.
421     outfile_b_.Write10MsData(audio_frame.data(),
422                              audio_frame.samples_per_channel_);
423 
424     // Update loop counter
425     counter++;
426   }
427 
428   EXPECT_EQ(0, error_count);
429 
430   if (infile_a_.EndOfFile()) {
431     infile_a_.Rewind();
432   }
433 }
434 
OpenOutFile(int test_number)435 void TestAllCodecs::OpenOutFile(int test_number) {
436   std::string filename = webrtc::test::OutputPath();
437   rtc::StringBuilder test_number_str;
438   test_number_str << test_number;
439   filename += "testallcodecs_out_";
440   filename += test_number_str.str();
441   filename += ".pcm";
442   outfile_b_.Open(filename, 32000, "wb");
443 }
444 
445 }  // namespace webrtc
446