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 <stdio.h>
14#include <string>
15
16#include "testing/gtest/include/gtest/gtest.h"
17
18#include "webrtc/common_types.h"
19#include "webrtc/engine_configurations.h"
20#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
21#include "webrtc/modules/audio_coding/main/interface/audio_coding_module_typedefs.h"
22#include "webrtc/modules/audio_coding/main/test/utility.h"
23#include "webrtc/system_wrappers/interface/trace.h"
24#include "webrtc/test/testsupport/fileutils.h"
25#include "webrtc/typedefs.h"
26
27// Description of the test:
28// In this test we set up a one-way communication channel from a participant
29// called "a" to a participant called "b".
30// a -> channel_a_to_b -> b
31//
32// The test loops through all available mono codecs, encode at "a" sends over
33// the channel, and decodes at "b".
34
35namespace webrtc {
36
37// Class for simulating packet handling.
38TestPack::TestPack()
39    : receiver_acm_(NULL),
40      sequence_number_(0),
41      timestamp_diff_(0),
42      last_in_timestamp_(0),
43      total_bytes_(0),
44      payload_size_(0) {
45}
46
47TestPack::~TestPack() {
48}
49
50void TestPack::RegisterReceiverACM(AudioCodingModule* acm) {
51  receiver_acm_ = acm;
52  return;
53}
54
55int32_t TestPack::SendData(FrameType frame_type, uint8_t payload_type,
56                           uint32_t timestamp, const uint8_t* payload_data,
57                           uint16_t payload_size,
58                           const RTPFragmentationHeader* fragmentation) {
59  WebRtcRTPHeader rtp_info;
60  int32_t status;
61
62  rtp_info.header.markerBit = false;
63  rtp_info.header.ssrc = 0;
64  rtp_info.header.sequenceNumber = sequence_number_++;
65  rtp_info.header.payloadType = payload_type;
66  rtp_info.header.timestamp = timestamp;
67  if (frame_type == kAudioFrameCN) {
68    rtp_info.type.Audio.isCNG = true;
69  } else {
70    rtp_info.type.Audio.isCNG = false;
71  }
72  if (frame_type == kFrameEmpty) {
73    // Skip this frame.
74    return 0;
75  }
76
77  // Only run mono for all test cases.
78  rtp_info.type.Audio.channel = 1;
79  memcpy(payload_data_, payload_data, payload_size);
80
81  status = receiver_acm_->IncomingPacket(payload_data_, payload_size, rtp_info);
82
83  payload_size_ = payload_size;
84  timestamp_diff_ = timestamp - last_in_timestamp_;
85  last_in_timestamp_ = timestamp;
86  total_bytes_ += payload_size;
87  return status;
88}
89
90uint16_t TestPack::payload_size() {
91  return payload_size_;
92}
93
94uint32_t TestPack::timestamp_diff() {
95  return timestamp_diff_;
96}
97
98void TestPack::reset_payload_size() {
99  payload_size_ = 0;
100}
101
102TestAllCodecs::TestAllCodecs(int test_mode)
103    : acm_a_(AudioCodingModule::Create(0)),
104      acm_b_(AudioCodingModule::Create(1)),
105      channel_a_to_b_(NULL),
106      test_count_(0),
107      packet_size_samples_(0),
108      packet_size_bytes_(0) {
109  // test_mode = 0 for silent test (auto test)
110  test_mode_ = test_mode;
111}
112
113TestAllCodecs::~TestAllCodecs() {
114  if (channel_a_to_b_ != NULL) {
115    delete channel_a_to_b_;
116    channel_a_to_b_ = NULL;
117  }
118}
119
120void TestAllCodecs::Perform() {
121  const std::string file_name = webrtc::test::ResourcePath(
122      "audio_coding/testfile32kHz", "pcm");
123  infile_a_.Open(file_name, 32000, "rb");
124
125  if (test_mode_ == 0) {
126    WEBRTC_TRACE(kTraceStateInfo, kTraceAudioCoding, -1,
127                 "---------- TestAllCodecs ----------");
128  }
129
130  acm_a_->InitializeReceiver();
131  acm_b_->InitializeReceiver();
132
133  uint8_t num_encoders = acm_a_->NumberOfCodecs();
134  CodecInst my_codec_param;
135  for (uint8_t n = 0; n < num_encoders; n++) {
136    acm_b_->Codec(n, &my_codec_param);
137    if (!strcmp(my_codec_param.plname, "opus")) {
138      my_codec_param.channels = 1;
139    }
140    acm_b_->RegisterReceiveCodec(my_codec_param);
141  }
142
143  // Create and connect the channel
144  channel_a_to_b_ = new TestPack;
145  acm_a_->RegisterTransportCallback(channel_a_to_b_);
146  channel_a_to_b_->RegisterReceiverACM(acm_b_.get());
147
148  // All codecs are tested for all allowed sampling frequencies, rates and
149  // packet sizes.
150#ifdef WEBRTC_CODEC_AMR
151  if (test_mode_ != 0) {
152    printf("===============================================================\n");
153  }
154  test_count_++;
155  OpenOutFile(test_count_);
156  char codec_amr[] = "AMR";
157  RegisterSendCodec('A', codec_amr, 8000, 4750, 160, 2);
158  Run(channel_a_to_b_);
159  RegisterSendCodec('A', codec_amr, 8000, 4750, 320, 2);
160  Run(channel_a_to_b_);
161  RegisterSendCodec('A', codec_amr, 8000, 4750, 480, 3);
162  Run(channel_a_to_b_);
163  RegisterSendCodec('A', codec_amr, 8000, 5150, 160, 2);
164  Run(channel_a_to_b_);
165  RegisterSendCodec('A', codec_amr, 8000, 5150, 320, 2);
166  Run(channel_a_to_b_);
167  RegisterSendCodec('A', codec_amr, 8000, 5150, 480, 3);
168  Run(channel_a_to_b_);
169  RegisterSendCodec('A', codec_amr, 8000, 5900, 160, 1);
170  Run(channel_a_to_b_);
171  RegisterSendCodec('A', codec_amr, 8000, 5900, 320, 2);
172  Run(channel_a_to_b_);
173  RegisterSendCodec('A', codec_amr, 8000, 5900, 480, 2);
174  Run(channel_a_to_b_);
175  RegisterSendCodec('A', codec_amr, 8000, 6700, 160, 1);
176  Run(channel_a_to_b_);
177  RegisterSendCodec('A', codec_amr, 8000, 6700, 320, 2);
178  Run(channel_a_to_b_);
179  RegisterSendCodec('A', codec_amr, 8000, 6700, 480, 2);
180  Run(channel_a_to_b_);
181  RegisterSendCodec('A', codec_amr, 8000, 7400, 160, 1);
182  Run(channel_a_to_b_);
183  RegisterSendCodec('A', codec_amr, 8000, 7400, 320, 2);
184  Run(channel_a_to_b_);
185  RegisterSendCodec('A', codec_amr, 8000, 7400, 480, 3);
186  Run(channel_a_to_b_);
187  RegisterSendCodec('A', codec_amr, 8000, 7950, 160, 2);
188  Run(channel_a_to_b_);
189  RegisterSendCodec('A', codec_amr, 8000, 7950, 320, 2);
190  Run(channel_a_to_b_);
191  RegisterSendCodec('A', codec_amr, 8000, 7950, 480, 3);
192  Run(channel_a_to_b_);
193  RegisterSendCodec('A', codec_amr, 8000, 10200, 160, 1);
194  Run(channel_a_to_b_);
195  RegisterSendCodec('A', codec_amr, 8000, 10200, 320, 2);
196  Run(channel_a_to_b_);
197  RegisterSendCodec('A', codec_amr, 8000, 10200, 480, 3);
198  Run(channel_a_to_b_);
199  RegisterSendCodec('A', codec_amr, 8000, 12200, 160, 1);
200  Run(channel_a_to_b_);
201  RegisterSendCodec('A', codec_amr, 8000, 12200, 320, 2);
202  Run(channel_a_to_b_);
203  RegisterSendCodec('A', codec_amr, 8000, 12200, 480, 3);
204  Run(channel_a_to_b_);
205  outfile_b_.Close();
206#endif
207#ifdef WEBRTC_CODEC_AMRWB
208  if (test_mode_ != 0) {
209    printf("===============================================================\n");
210  }
211  test_count_++;
212  char codec_amrwb[] = "AMR-WB";
213  OpenOutFile(test_count_);
214  RegisterSendCodec('A', codec_amrwb, 16000, 7000, 320, 0);
215  Run(channel_a_to_b_);
216  RegisterSendCodec('A', codec_amrwb, 16000, 7000, 640, 0);
217  Run(channel_a_to_b_);
218  RegisterSendCodec('A', codec_amrwb, 16000, 7000, 960, 0);
219  Run(channel_a_to_b_);
220  RegisterSendCodec('A', codec_amrwb, 16000, 9000, 320, 1);
221  Run(channel_a_to_b_);
222  RegisterSendCodec('A', codec_amrwb, 16000, 9000, 640, 2);
223  Run(channel_a_to_b_);
224  RegisterSendCodec('A', codec_amrwb, 16000, 9000, 960, 2);
225  Run(channel_a_to_b_);
226  RegisterSendCodec('A', codec_amrwb, 16000, 12000, 320, 3);
227  Run(channel_a_to_b_);
228  RegisterSendCodec('A', codec_amrwb, 16000, 12000, 640, 6);
229  Run(channel_a_to_b_);
230  RegisterSendCodec('A', codec_amrwb, 16000, 12000, 960, 8);
231  Run(channel_a_to_b_);
232  RegisterSendCodec('A', codec_amrwb, 16000, 14000, 320, 2);
233  Run(channel_a_to_b_);
234  RegisterSendCodec('A', codec_amrwb, 16000, 14000, 640, 4);
235  Run(channel_a_to_b_);
236  RegisterSendCodec('A', codec_amrwb, 16000, 14000, 960, 5);
237  Run(channel_a_to_b_);
238  RegisterSendCodec('A', codec_amrwb, 16000, 16000, 320, 1);
239  Run(channel_a_to_b_);
240  RegisterSendCodec('A', codec_amrwb, 16000, 16000, 640, 2);
241  Run(channel_a_to_b_);
242  RegisterSendCodec('A', codec_amrwb, 16000, 16000, 960, 2);
243  Run(channel_a_to_b_);
244  RegisterSendCodec('A', codec_amrwb, 16000, 18000, 320, 2);
245  Run(channel_a_to_b_);
246  RegisterSendCodec('A', codec_amrwb, 16000, 18000, 640, 4);
247  Run(channel_a_to_b_);
248  RegisterSendCodec('A', codec_amrwb, 16000, 18000, 960, 5);
249  Run(channel_a_to_b_);
250  RegisterSendCodec('A', codec_amrwb, 16000, 20000, 320, 1);
251  Run(channel_a_to_b_);
252  RegisterSendCodec('A', codec_amrwb, 16000, 20000, 640, 2);
253  Run(channel_a_to_b_);
254  RegisterSendCodec('A', codec_amrwb, 16000, 20000, 960, 2);
255  Run(channel_a_to_b_);
256  RegisterSendCodec('A', codec_amrwb, 16000, 23000, 320, 1);
257  Run(channel_a_to_b_);
258  RegisterSendCodec('A', codec_amrwb, 16000, 23000, 640, 3);
259  Run(channel_a_to_b_);
260  RegisterSendCodec('A', codec_amrwb, 16000, 23000, 960, 3);
261  Run(channel_a_to_b_);
262  RegisterSendCodec('A', codec_amrwb, 16000, 24000, 320, 1);
263  Run(channel_a_to_b_);
264  RegisterSendCodec('A', codec_amrwb, 16000, 24000, 640, 2);
265  Run(channel_a_to_b_);
266  RegisterSendCodec('A', codec_amrwb, 16000, 24000, 960, 2);
267  Run(channel_a_to_b_);
268  outfile_b_.Close();
269#endif
270#ifdef WEBRTC_CODEC_G722
271  if (test_mode_ != 0) {
272    printf("===============================================================\n");
273  }
274  test_count_++;
275  OpenOutFile(test_count_);
276  char codec_g722[] = "G722";
277  RegisterSendCodec('A', codec_g722, 16000, 64000, 160, 0);
278  Run(channel_a_to_b_);
279  RegisterSendCodec('A', codec_g722, 16000, 64000, 320, 0);
280  Run(channel_a_to_b_);
281  RegisterSendCodec('A', codec_g722, 16000, 64000, 480, 0);
282  Run(channel_a_to_b_);
283  RegisterSendCodec('A', codec_g722, 16000, 64000, 640, 0);
284  Run(channel_a_to_b_);
285  RegisterSendCodec('A', codec_g722, 16000, 64000, 800, 0);
286  Run(channel_a_to_b_);
287  RegisterSendCodec('A', codec_g722, 16000, 64000, 960, 0);
288  Run(channel_a_to_b_);
289  outfile_b_.Close();
290#endif
291#ifdef WEBRTC_CODEC_G722_1
292  if (test_mode_ != 0) {
293    printf("===============================================================\n");
294  }
295  test_count_++;
296  OpenOutFile(test_count_);
297  char codec_g722_1[] = "G7221";
298  RegisterSendCodec('A', codec_g722_1, 16000, 32000, 320, 0);
299  Run(channel_a_to_b_);
300  RegisterSendCodec('A', codec_g722_1, 16000, 24000, 320, 0);
301  Run(channel_a_to_b_);
302  RegisterSendCodec('A', codec_g722_1, 16000, 16000, 320, 0);
303  Run(channel_a_to_b_);
304  outfile_b_.Close();
305#endif
306#ifdef WEBRTC_CODEC_G722_1C
307  if (test_mode_ != 0) {
308    printf("===============================================================\n");
309  }
310  test_count_++;
311  OpenOutFile(test_count_);
312  char codec_g722_1c[] = "G7221";
313  RegisterSendCodec('A', codec_g722_1c, 32000, 48000, 640, 0);
314  Run(channel_a_to_b_);
315  RegisterSendCodec('A', codec_g722_1c, 32000, 32000, 640, 0);
316  Run(channel_a_to_b_);
317  RegisterSendCodec('A', codec_g722_1c, 32000, 24000, 640, 0);
318  Run(channel_a_to_b_);
319  outfile_b_.Close();
320#endif
321#ifdef WEBRTC_CODEC_G729
322  if (test_mode_ != 0) {
323    printf("===============================================================\n");
324  }
325  test_count_++;
326  OpenOutFile(test_count_);
327  char codec_g729[] = "G729";
328  RegisterSendCodec('A', codec_g729, 8000, 8000, 80, 0);
329  Run(channel_a_to_b_);
330  RegisterSendCodec('A', codec_g729, 8000, 8000, 160, 0);
331  Run(channel_a_to_b_);
332  RegisterSendCodec('A', codec_g729, 8000, 8000, 240, 0);
333  Run(channel_a_to_b_);
334  RegisterSendCodec('A', codec_g729, 8000, 8000, 320, 0);
335  Run(channel_a_to_b_);
336  RegisterSendCodec('A', codec_g729, 8000, 8000, 400, 0);
337  Run(channel_a_to_b_);
338  RegisterSendCodec('A', codec_g729, 8000, 8000, 480, 0);
339  Run(channel_a_to_b_);
340  outfile_b_.Close();
341#endif
342#ifdef WEBRTC_CODEC_G729_1
343  if (test_mode_ != 0) {
344    printf("===============================================================\n");
345  }
346  test_count_++;
347  OpenOutFile(test_count_);
348  char codec_g729_1[] = "G7291";
349  RegisterSendCodec('A', codec_g729_1, 16000, 8000, 320, 1);
350  Run(channel_a_to_b_);
351  RegisterSendCodec('A', codec_g729_1, 16000, 8000, 640, 1);
352  Run(channel_a_to_b_);
353  RegisterSendCodec('A', codec_g729_1, 16000, 8000, 960, 1);
354  Run(channel_a_to_b_);
355  RegisterSendCodec('A', codec_g729_1, 16000, 12000, 320, 1);
356  Run(channel_a_to_b_);
357  RegisterSendCodec('A', codec_g729_1, 16000, 12000, 640, 1);
358  Run(channel_a_to_b_);
359  RegisterSendCodec('A', codec_g729_1, 16000, 12000, 960, 1);
360  Run(channel_a_to_b_);
361  RegisterSendCodec('A', codec_g729_1, 16000, 14000, 320, 1);
362  Run(channel_a_to_b_);
363  RegisterSendCodec('A', codec_g729_1, 16000, 14000, 640, 1);
364  Run(channel_a_to_b_);
365  RegisterSendCodec('A', codec_g729_1, 16000, 14000, 960, 1);
366  Run(channel_a_to_b_);
367  RegisterSendCodec('A', codec_g729_1, 16000, 16000, 320, 1);
368  Run(channel_a_to_b_);
369  RegisterSendCodec('A', codec_g729_1, 16000, 16000, 640, 1);
370  Run(channel_a_to_b_);
371  RegisterSendCodec('A', codec_g729_1, 16000, 16000, 960, 1);
372  Run(channel_a_to_b_);
373  RegisterSendCodec('A', codec_g729_1, 16000, 18000, 320, 1);
374  Run(channel_a_to_b_);
375  RegisterSendCodec('A', codec_g729_1, 16000, 18000, 640, 1);
376  Run(channel_a_to_b_);
377  RegisterSendCodec('A', codec_g729_1, 16000, 18000, 960, 1);
378  Run(channel_a_to_b_);
379  RegisterSendCodec('A', codec_g729_1, 16000, 20000, 320, 1);
380  Run(channel_a_to_b_);
381  RegisterSendCodec('A', codec_g729_1, 16000, 20000, 640, 1);
382  Run(channel_a_to_b_);
383  RegisterSendCodec('A', codec_g729_1, 16000, 20000, 960, 1);
384  Run(channel_a_to_b_);
385  RegisterSendCodec('A', codec_g729_1, 16000, 22000, 320, 1);
386  Run(channel_a_to_b_);
387  RegisterSendCodec('A', codec_g729_1, 16000, 22000, 640, 1);
388  Run(channel_a_to_b_);
389  RegisterSendCodec('A', codec_g729_1, 16000, 22000, 960, 1);
390  Run(channel_a_to_b_);
391  RegisterSendCodec('A', codec_g729_1, 16000, 24000, 320, 1);
392  Run(channel_a_to_b_);
393  RegisterSendCodec('A', codec_g729_1, 16000, 24000, 640, 1);
394  Run(channel_a_to_b_);
395  RegisterSendCodec('A', codec_g729_1, 16000, 24000, 960, 1);
396  Run(channel_a_to_b_);
397  RegisterSendCodec('A', codec_g729_1, 16000, 26000, 320, 1);
398  Run(channel_a_to_b_);
399  RegisterSendCodec('A', codec_g729_1, 16000, 26000, 640, 1);
400  Run(channel_a_to_b_);
401  RegisterSendCodec('A', codec_g729_1, 16000, 26000, 960, 1);
402  Run(channel_a_to_b_);
403  RegisterSendCodec('A', codec_g729_1, 16000, 28000, 320, 1);
404  Run(channel_a_to_b_);
405  RegisterSendCodec('A', codec_g729_1, 16000, 28000, 640, 1);
406  Run(channel_a_to_b_);
407  RegisterSendCodec('A', codec_g729_1, 16000, 28000, 960, 1);
408  Run(channel_a_to_b_);
409  RegisterSendCodec('A', codec_g729_1, 16000, 30000, 320, 1);
410  Run(channel_a_to_b_);
411  RegisterSendCodec('A', codec_g729_1, 16000, 30000, 640, 1);
412  Run(channel_a_to_b_);
413  RegisterSendCodec('A', codec_g729_1, 16000, 30000, 960, 1);
414  Run(channel_a_to_b_);
415  RegisterSendCodec('A', codec_g729_1, 16000, 32000, 320, 1);
416  Run(channel_a_to_b_);
417  RegisterSendCodec('A', codec_g729_1, 16000, 32000, 640, 1);
418  Run(channel_a_to_b_);
419  RegisterSendCodec('A', codec_g729_1, 16000, 32000, 960, 1);
420  Run(channel_a_to_b_);
421  outfile_b_.Close();
422#endif
423#ifdef WEBRTC_CODEC_GSMFR
424  if (test_mode_ != 0) {
425    printf("===============================================================\n");
426  }
427  test_count_++;
428  OpenOutFile(test_count_);
429  char codec_gsmfr[] = "GSM";
430  RegisterSendCodec('A', codec_gsmfr, 8000, 13200, 160, 0);
431  Run(channel_a_to_b_);
432  RegisterSendCodec('A', codec_gsmfr, 8000, 13200, 320, 0);
433  Run(channel_a_to_b_);
434  RegisterSendCodec('A', codec_gsmfr, 8000, 13200, 480, 0);
435  Run(channel_a_to_b_);
436  outfile_b_.Close();
437#endif
438#ifdef WEBRTC_CODEC_ILBC
439  if (test_mode_ != 0) {
440    printf("===============================================================\n");
441  }
442  test_count_++;
443  OpenOutFile(test_count_);
444  char codec_ilbc[] = "ILBC";
445  RegisterSendCodec('A', codec_ilbc, 8000, 13300, 240, 0);
446  Run(channel_a_to_b_);
447  RegisterSendCodec('A', codec_ilbc, 8000, 13300, 480, 0);
448  Run(channel_a_to_b_);
449  RegisterSendCodec('A', codec_ilbc, 8000, 15200, 160, 0);
450  Run(channel_a_to_b_);
451  RegisterSendCodec('A', codec_ilbc, 8000, 15200, 320, 0);
452  Run(channel_a_to_b_);
453  outfile_b_.Close();
454#endif
455#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
456  if (test_mode_ != 0) {
457    printf("===============================================================\n");
458  }
459  test_count_++;
460  OpenOutFile(test_count_);
461  char codec_isac[] = "ISAC";
462  RegisterSendCodec('A', codec_isac, 16000, -1, 480, -1);
463  Run(channel_a_to_b_);
464  RegisterSendCodec('A', codec_isac, 16000, -1, 960, -1);
465  Run(channel_a_to_b_);
466  RegisterSendCodec('A', codec_isac, 16000, 15000, 480, -1);
467  Run(channel_a_to_b_);
468  RegisterSendCodec('A', codec_isac, 16000, 32000, 960, -1);
469  Run(channel_a_to_b_);
470  outfile_b_.Close();
471#endif
472#ifdef WEBRTC_CODEC_ISAC
473  if (test_mode_ != 0) {
474    printf("===============================================================\n");
475  }
476  test_count_++;
477  OpenOutFile(test_count_);
478  RegisterSendCodec('A', codec_isac, 32000, -1, 960, -1);
479  Run(channel_a_to_b_);
480  RegisterSendCodec('A', codec_isac, 32000, 56000, 960, -1);
481  Run(channel_a_to_b_);
482  RegisterSendCodec('A', codec_isac, 32000, 37000, 960, -1);
483  Run(channel_a_to_b_);
484  RegisterSendCodec('A', codec_isac, 32000, 32000, 960, -1);
485  Run(channel_a_to_b_);
486  outfile_b_.Close();
487#endif
488#ifdef WEBRTC_CODEC_PCM16
489  if (test_mode_ != 0) {
490    printf("===============================================================\n");
491  }
492  test_count_++;
493  OpenOutFile(test_count_);
494  char codec_l16[] = "L16";
495  RegisterSendCodec('A', codec_l16, 8000, 128000, 80, 0);
496  Run(channel_a_to_b_);
497  RegisterSendCodec('A', codec_l16, 8000, 128000, 160, 0);
498  Run(channel_a_to_b_);
499  RegisterSendCodec('A', codec_l16, 8000, 128000, 240, 0);
500  Run(channel_a_to_b_);
501  RegisterSendCodec('A', codec_l16, 8000, 128000, 320, 0);
502  Run(channel_a_to_b_);
503  outfile_b_.Close();
504  if (test_mode_ != 0) {
505    printf("===============================================================\n");
506  }
507  test_count_++;
508  OpenOutFile(test_count_);
509  RegisterSendCodec('A', codec_l16, 16000, 256000, 160, 0);
510  Run(channel_a_to_b_);
511  RegisterSendCodec('A', codec_l16, 16000, 256000, 320, 0);
512  Run(channel_a_to_b_);
513  RegisterSendCodec('A', codec_l16, 16000, 256000, 480, 0);
514  Run(channel_a_to_b_);
515  RegisterSendCodec('A', codec_l16, 16000, 256000, 640, 0);
516  Run(channel_a_to_b_);
517  outfile_b_.Close();
518  if (test_mode_ != 0) {
519    printf("===============================================================\n");
520  }
521  test_count_++;
522  OpenOutFile(test_count_);
523  RegisterSendCodec('A', codec_l16, 32000, 512000, 320, 0);
524  Run(channel_a_to_b_);
525  RegisterSendCodec('A', codec_l16, 32000, 512000, 640, 0);
526  Run(channel_a_to_b_);
527  outfile_b_.Close();
528#endif
529  if (test_mode_ != 0) {
530    printf("===============================================================\n");
531  }
532  test_count_++;
533  OpenOutFile(test_count_);
534  char codec_pcma[] = "PCMA";
535  RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, 0);
536  Run(channel_a_to_b_);
537  RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, 0);
538  Run(channel_a_to_b_);
539  RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, 0);
540  Run(channel_a_to_b_);
541  RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, 0);
542  Run(channel_a_to_b_);
543  RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, 0);
544  Run(channel_a_to_b_);
545  RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, 0);
546  Run(channel_a_to_b_);
547  if (test_mode_ != 0) {
548    printf("===============================================================\n");
549  }
550  char codec_pcmu[] = "PCMU";
551  RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, 0);
552  Run(channel_a_to_b_);
553  RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, 0);
554  Run(channel_a_to_b_);
555  RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, 0);
556  Run(channel_a_to_b_);
557  RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, 0);
558  Run(channel_a_to_b_);
559  RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, 0);
560  Run(channel_a_to_b_);
561  RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, 0);
562  Run(channel_a_to_b_);
563  outfile_b_.Close();
564#ifdef WEBRTC_CODEC_SPEEX
565  if (test_mode_ != 0) {
566    printf("===============================================================\n");
567  }
568  test_count_++;
569  OpenOutFile(test_count_);
570  char codec_speex[] = "SPEEX";
571  RegisterSendCodec('A', codec_speex, 8000, 2400, 160, 0);
572  Run(channel_a_to_b_);
573  RegisterSendCodec('A', codec_speex, 8000, 8000, 320, 0);
574  Run(channel_a_to_b_);
575  RegisterSendCodec('A', codec_speex, 8000, 18200, 480, 0);
576  Run(channel_a_to_b_);
577  outfile_b_.Close();
578
579  if (test_mode_ != 0) {
580    printf("===============================================================\n");
581  }
582  test_count_++;
583  OpenOutFile(test_count_);
584  RegisterSendCodec('A', codec_speex, 16000, 4000, 320, 0);
585  Run(channel_a_to_b_);
586  RegisterSendCodec('A', codec_speex, 16000, 12800, 640, 0);
587  Run(channel_a_to_b_);
588  RegisterSendCodec('A', codec_speex, 16000, 34200, 960, 0);
589  Run(channel_a_to_b_);
590  outfile_b_.Close();
591#endif
592#ifdef WEBRTC_CODEC_CELT
593  if (test_mode_ != 0) {
594    printf("===============================================================\n");
595  }
596  test_count_++;
597  OpenOutFile(test_count_);
598  char codec_celt[] = "CELT";
599  RegisterSendCodec('A', codec_celt, 32000, 48000, 640, 0);
600  Run(channel_a_to_b_);
601  RegisterSendCodec('A', codec_celt, 32000, 64000, 640, 0);
602  Run(channel_a_to_b_);
603  RegisterSendCodec('A', codec_celt, 32000, 128000, 640, 0);
604  Run(channel_a_to_b_);
605  outfile_b_.Close();
606#endif
607#ifdef WEBRTC_CODEC_OPUS
608  if (test_mode_ != 0) {
609    printf("===============================================================\n");
610  }
611  test_count_++;
612  OpenOutFile(test_count_);
613  char codec_opus[] = "OPUS";
614  RegisterSendCodec('A', codec_opus, 48000, 6000, 480, -1);
615  Run(channel_a_to_b_);
616  RegisterSendCodec('A', codec_opus, 48000, 20000, 480*2, -1);
617  Run(channel_a_to_b_);
618  RegisterSendCodec('A', codec_opus, 48000, 32000, 480*4, -1);
619  Run(channel_a_to_b_);
620  RegisterSendCodec('A', codec_opus, 48000, 48000, 480, -1);
621  Run(channel_a_to_b_);
622  RegisterSendCodec('A', codec_opus, 48000, 64000, 480*4, -1);
623  Run(channel_a_to_b_);
624  RegisterSendCodec('A', codec_opus, 48000, 96000, 480*6, -1);
625  Run(channel_a_to_b_);
626  RegisterSendCodec('A', codec_opus, 48000, 500000, 480*2, -1);
627  Run(channel_a_to_b_);
628  outfile_b_.Close();
629#endif
630  if (test_mode_ != 0) {
631    printf("===============================================================\n");
632
633    /* Print out all codecs that were not tested in the run */
634    printf("The following codecs was not included in the test:\n");
635#ifndef WEBRTC_CODEC_AMR
636    printf("   GSMAMR\n");
637#endif
638#ifndef WEBRTC_CODEC_AMRWB
639    printf("   GSMAMR-wb\n");
640#endif
641#ifndef WEBRTC_CODEC_G722
642    printf("   G.722\n");
643#endif
644#ifndef WEBRTC_CODEC_G722_1
645    printf("   G.722.1\n");
646#endif
647#ifndef WEBRTC_CODEC_G722_1C
648    printf("   G.722.1C\n");
649#endif
650#ifndef WEBRTC_CODEC_G729
651    printf("   G.729\n");
652#endif
653#ifndef WEBRTC_CODEC_G729_1
654    printf("   G.729.1\n");
655#endif
656#ifndef WEBRTC_CODEC_GSMFR
657    printf("   GSMFR\n");
658#endif
659#ifndef WEBRTC_CODEC_ILBC
660    printf("   iLBC\n");
661#endif
662#ifndef WEBRTC_CODEC_ISAC
663    printf("   ISAC float\n");
664#endif
665#ifndef WEBRTC_CODEC_ISACFX
666    printf("   ISAC fix\n");
667#endif
668#ifndef WEBRTC_CODEC_PCM16
669    printf("   PCM16\n");
670#endif
671#ifndef WEBRTC_CODEC_SPEEX
672    printf("   Speex\n");
673#endif
674
675    printf("\nTo complete the test, listen to the %d number of output files.\n",
676           test_count_);
677  }
678}
679
680// Register Codec to use in the test
681//
682// Input:  side             - which ACM to use, 'A' or 'B'
683//         codec_name       - name to use when register the codec
684//         sampling_freq_hz - sampling frequency in Herz
685//         rate             - bitrate in bytes
686//         packet_size      - packet size in samples
687//         extra_byte       - if extra bytes needed compared to the bitrate
688//                            used when registering, can be an internal header
689//                            set to -1 if the codec is a variable rate codec
690void TestAllCodecs::RegisterSendCodec(char side, char* codec_name,
691                                      int32_t sampling_freq_hz, int rate,
692                                      int packet_size, int extra_byte) {
693  if (test_mode_ != 0) {
694    // Print out codec and settings.
695    printf("codec: %s Freq: %d Rate: %d PackSize: %d\n", codec_name,
696           sampling_freq_hz, rate, packet_size);
697  }
698
699  // Store packet-size in samples, used to validate the received packet.
700  // If G.722, store half the size to compensate for the timestamp bug in the
701  // RFC for G.722.
702  // If iSAC runs in adaptive mode, packet size in samples can change on the
703  // fly, so we exclude this test by setting |packet_size_samples_| to -1.
704  if (!strcmp(codec_name, "G722")) {
705    packet_size_samples_ = packet_size / 2;
706  } else if (!strcmp(codec_name, "ISAC") && (rate == -1)) {
707    packet_size_samples_ = -1;
708  } else {
709    packet_size_samples_ = packet_size;
710  }
711
712  // Store the expected packet size in bytes, used to validate the received
713  // packet. If variable rate codec (extra_byte == -1), set to -1.
714  if (extra_byte != -1) {
715    // Add 0.875 to always round up to a whole byte
716    packet_size_bytes_ = static_cast<int>(static_cast<float>(packet_size
717        * rate) / static_cast<float>(sampling_freq_hz * 8) + 0.875)
718        + extra_byte;
719  } else {
720    // Packets will have a variable size.
721    packet_size_bytes_ = -1;
722  }
723
724  // Set pointer to the ACM where to register the codec.
725  AudioCodingModule* my_acm = NULL;
726  switch (side) {
727    case 'A': {
728      my_acm = acm_a_.get();
729      break;
730    }
731    case 'B': {
732      my_acm = acm_b_.get();
733      break;
734    }
735    default: {
736      break;
737    }
738  }
739  ASSERT_TRUE(my_acm != NULL);
740
741  // Get all codec parameters before registering
742  CodecInst my_codec_param;
743  CHECK_ERROR(AudioCodingModule::Codec(codec_name, &my_codec_param,
744                                       sampling_freq_hz, 1));
745  my_codec_param.rate = rate;
746  my_codec_param.pacsize = packet_size;
747  CHECK_ERROR(my_acm->RegisterSendCodec(my_codec_param));
748}
749
750void TestAllCodecs::Run(TestPack* channel) {
751  AudioFrame audio_frame;
752
753  int32_t out_freq_hz = outfile_b_.SamplingFrequency();
754  uint16_t receive_size;
755  uint32_t timestamp_diff;
756  channel->reset_payload_size();
757  int error_count = 0;
758
759  int counter = 0;
760  while (!infile_a_.EndOfFile()) {
761    // Add 10 msec to ACM.
762    infile_a_.Read10MsData(audio_frame);
763    CHECK_ERROR(acm_a_->Add10MsData(audio_frame));
764
765    // Run sender side of ACM.
766    CHECK_ERROR(acm_a_->Process());
767
768    // Verify that the received packet size matches the settings.
769    receive_size = channel->payload_size();
770    if (receive_size) {
771      if ((static_cast<int>(receive_size) != packet_size_bytes_) &&
772          (packet_size_bytes_ > -1)) {
773        error_count++;
774      }
775
776      // Verify that the timestamp is updated with expected length. The counter
777      // is used to avoid problems when switching codec or frame size in the
778      // test.
779      timestamp_diff = channel->timestamp_diff();
780      if ((counter > 10) &&
781          (static_cast<int>(timestamp_diff) != packet_size_samples_) &&
782          (packet_size_samples_ > -1))
783        error_count++;
784    }
785
786    // Run received side of ACM.
787    CHECK_ERROR(acm_b_->PlayoutData10Ms(out_freq_hz, &audio_frame));
788
789    // Write output speech to file.
790    outfile_b_.Write10MsData(audio_frame.data_,
791                             audio_frame.samples_per_channel_);
792
793    // Update loop counter
794    counter++;
795  }
796
797  EXPECT_EQ(0, error_count);
798
799  if (infile_a_.EndOfFile()) {
800    infile_a_.Rewind();
801  }
802}
803
804void TestAllCodecs::OpenOutFile(int test_number) {
805  std::string filename = webrtc::test::OutputPath();
806  std::ostringstream test_number_str;
807  test_number_str << test_number;
808  filename += "testallcodecs_out_";
809  filename += test_number_str.str();
810  filename += ".pcm";
811  outfile_b_.Open(filename, 32000, "wb");
812}
813
814void TestAllCodecs::DisplaySendReceiveCodec() {
815  CodecInst my_codec_param;
816  acm_a_->SendCodec(&my_codec_param);
817  printf("%s -> ", my_codec_param.plname);
818  acm_b_->ReceiveCodec(&my_codec_param);
819  printf("%s\n", my_codec_param.plname);
820}
821
822}  // namespace webrtc
823