blob: 781337c1078e2f74de2292c873389a2d2045f7bf [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
tina.legrand@webrtc.orgdf697752012-02-08 10:22:21 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/test/TestAllCodecs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000013#include <cstdio>
14#include <limits>
tina.legrand@webrtc.org5e7ca602012-06-12 07:16:24 +000015#include <string>
kjellander@webrtc.org5490c712011-12-21 13:34:18 +000016
Karl Wiberg5817d3d2018-04-06 10:06:42 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Karl Wiberg133cff02018-07-06 15:40:14 +020018#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020019#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/codecs/audio_format_conversion.h"
21#include "modules/audio_coding/include/audio_coding_module.h"
22#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
23#include "modules/audio_coding/test/utility.h"
24#include "rtc_base/logging.h"
Karl Wiberg133cff02018-07-06 15:40:14 +020025#include "rtc_base/stringencode.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "test/gtest.h"
27#include "test/testsupport/fileutils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000028
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000029// Description of the test:
30// In this test we set up a one-way communication channel from a participant
31// called "a" to a participant called "b".
32// a -> channel_a_to_b -> b
33//
34// The test loops through all available mono codecs, encode at "a" sends over
35// the channel, and decodes at "b".
36
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000037namespace {
38const size_t kVariableSize = std::numeric_limits<size_t>::max();
39}
40
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000041namespace webrtc {
42
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000043// Class for simulating packet handling.
44TestPack::TestPack()
45 : receiver_acm_(NULL),
46 sequence_number_(0),
47 timestamp_diff_(0),
48 last_in_timestamp_(0),
49 total_bytes_(0),
Yves Gerey665174f2018-06-19 15:03:05 +020050 payload_size_(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000051
Yves Gerey665174f2018-06-19 15:03:05 +020052TestPack::~TestPack() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000053
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000054void TestPack::RegisterReceiverACM(AudioCodingModule* acm) {
55 receiver_acm_ = acm;
56 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000057}
58
Yves Gerey665174f2018-06-19 15:03:05 +020059int32_t TestPack::SendData(FrameType frame_type,
60 uint8_t payload_type,
61 uint32_t timestamp,
62 const uint8_t* payload_data,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000063 size_t payload_size,
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000064 const RTPFragmentationHeader* fragmentation) {
65 WebRtcRTPHeader rtp_info;
66 int32_t status;
niklase@google.com470e71d2011-07-07 08:21:25 +000067
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000068 rtp_info.header.markerBit = false;
69 rtp_info.header.ssrc = 0;
70 rtp_info.header.sequenceNumber = sequence_number_++;
71 rtp_info.header.payloadType = payload_type;
72 rtp_info.header.timestamp = timestamp;
philipel0a5fe772018-06-19 16:18:31 +020073
pbos22993e12015-10-19 02:39:06 -070074 if (frame_type == kEmptyFrame) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000075 // Skip this frame.
76 return 0;
77 }
78
79 // Only run mono for all test cases.
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000080 memcpy(payload_data_, payload_data, payload_size);
81
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000082 status = receiver_acm_->IncomingPacket(payload_data_, payload_size, rtp_info);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000083
84 payload_size_ = payload_size;
85 timestamp_diff_ = timestamp - last_in_timestamp_;
86 last_in_timestamp_ = timestamp;
87 total_bytes_ += payload_size;
88 return status;
niklase@google.com470e71d2011-07-07 08:21:25 +000089}
90
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000091size_t TestPack::payload_size() {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000092 return payload_size_;
niklase@google.com470e71d2011-07-07 08:21:25 +000093}
94
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000095uint32_t TestPack::timestamp_diff() {
96 return timestamp_diff_;
niklase@google.com470e71d2011-07-07 08:21:25 +000097}
98
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000099void TestPack::reset_payload_size() {
100 payload_size_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000101}
102
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +0000103TestAllCodecs::TestAllCodecs(int test_mode)
Karl Wiberg5817d3d2018-04-06 10:06:42 +0200104 : acm_a_(AudioCodingModule::Create(
105 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
106 acm_b_(AudioCodingModule::Create(
107 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000108 channel_a_to_b_(NULL),
109 test_count_(0),
110 packet_size_samples_(0),
111 packet_size_bytes_(0) {
112 // test_mode = 0 for silent test (auto test)
113 test_mode_ = test_mode;
114}
niklase@google.com470e71d2011-07-07 08:21:25 +0000115
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000116TestAllCodecs::~TestAllCodecs() {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000117 if (channel_a_to_b_ != NULL) {
118 delete channel_a_to_b_;
119 channel_a_to_b_ = NULL;
120 }
121}
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000123void TestAllCodecs::Perform() {
Yves Gerey665174f2018-06-19 15:03:05 +0200124 const std::string file_name =
125 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000126 infile_a_.Open(file_name, 32000, "rb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000128 if (test_mode_ == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100129 RTC_LOG(LS_INFO) << "---------- TestAllCodecs ----------";
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000130 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000132 acm_a_->InitializeReceiver();
133 acm_b_->InitializeReceiver();
niklase@google.com470e71d2011-07-07 08:21:25 +0000134
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000135 uint8_t num_encoders = acm_a_->NumberOfCodecs();
136 CodecInst my_codec_param;
137 for (uint8_t n = 0; n < num_encoders; n++) {
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000138 acm_b_->Codec(n, &my_codec_param);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000139 if (!strcmp(my_codec_param.plname, "opus")) {
140 my_codec_param.channels = 1;
141 }
kwibergda2bf4e2016-10-24 13:47:09 -0700142 acm_b_->RegisterReceiveCodec(my_codec_param.pltype,
143 CodecInstToSdp(my_codec_param));
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000144 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000146 // Create and connect the channel
147 channel_a_to_b_ = new TestPack;
148 acm_a_->RegisterTransportCallback(channel_a_to_b_);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000149 channel_a_to_b_->RegisterReceiverACM(acm_b_.get());
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000150
151 // All codecs are tested for all allowed sampling frequencies, rates and
152 // packet sizes.
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000153 if (test_mode_ != 0) {
154 printf("===============================================================\n");
155 }
156 test_count_++;
157 OpenOutFile(test_count_);
158 char codec_g722[] = "G722";
159 RegisterSendCodec('A', codec_g722, 16000, 64000, 160, 0);
160 Run(channel_a_to_b_);
161 RegisterSendCodec('A', codec_g722, 16000, 64000, 320, 0);
162 Run(channel_a_to_b_);
163 RegisterSendCodec('A', codec_g722, 16000, 64000, 480, 0);
164 Run(channel_a_to_b_);
165 RegisterSendCodec('A', codec_g722, 16000, 64000, 640, 0);
166 Run(channel_a_to_b_);
167 RegisterSendCodec('A', codec_g722, 16000, 64000, 800, 0);
168 Run(channel_a_to_b_);
169 RegisterSendCodec('A', codec_g722, 16000, 64000, 960, 0);
170 Run(channel_a_to_b_);
171 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000172#ifdef WEBRTC_CODEC_ILBC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000173 if (test_mode_ != 0) {
174 printf("===============================================================\n");
175 }
176 test_count_++;
177 OpenOutFile(test_count_);
178 char codec_ilbc[] = "ILBC";
179 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 240, 0);
180 Run(channel_a_to_b_);
181 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 480, 0);
182 Run(channel_a_to_b_);
183 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 160, 0);
184 Run(channel_a_to_b_);
185 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 320, 0);
186 Run(channel_a_to_b_);
187 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000188#endif
189#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000190 if (test_mode_ != 0) {
191 printf("===============================================================\n");
192 }
193 test_count_++;
194 OpenOutFile(test_count_);
195 char codec_isac[] = "ISAC";
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000196 RegisterSendCodec('A', codec_isac, 16000, -1, 480, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000197 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000198 RegisterSendCodec('A', codec_isac, 16000, -1, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000199 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000200 RegisterSendCodec('A', codec_isac, 16000, 15000, 480, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000201 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000202 RegisterSendCodec('A', codec_isac, 16000, 32000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000203 Run(channel_a_to_b_);
204 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000205#endif
206#ifdef WEBRTC_CODEC_ISAC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000207 if (test_mode_ != 0) {
208 printf("===============================================================\n");
209 }
210 test_count_++;
211 OpenOutFile(test_count_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000212 RegisterSendCodec('A', codec_isac, 32000, -1, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000213 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000214 RegisterSendCodec('A', codec_isac, 32000, 56000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000215 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000216 RegisterSendCodec('A', codec_isac, 32000, 37000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000217 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000218 RegisterSendCodec('A', codec_isac, 32000, 32000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000219 Run(channel_a_to_b_);
220 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000221#endif
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000222 if (test_mode_ != 0) {
223 printf("===============================================================\n");
224 }
225 test_count_++;
226 OpenOutFile(test_count_);
227 char codec_l16[] = "L16";
228 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, 0);
229 Run(channel_a_to_b_);
230 RegisterSendCodec('A', codec_l16, 8000, 128000, 160, 0);
231 Run(channel_a_to_b_);
232 RegisterSendCodec('A', codec_l16, 8000, 128000, 240, 0);
233 Run(channel_a_to_b_);
234 RegisterSendCodec('A', codec_l16, 8000, 128000, 320, 0);
235 Run(channel_a_to_b_);
236 outfile_b_.Close();
237 if (test_mode_ != 0) {
238 printf("===============================================================\n");
239 }
240 test_count_++;
241 OpenOutFile(test_count_);
242 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, 0);
243 Run(channel_a_to_b_);
244 RegisterSendCodec('A', codec_l16, 16000, 256000, 320, 0);
245 Run(channel_a_to_b_);
246 RegisterSendCodec('A', codec_l16, 16000, 256000, 480, 0);
247 Run(channel_a_to_b_);
248 RegisterSendCodec('A', codec_l16, 16000, 256000, 640, 0);
249 Run(channel_a_to_b_);
250 outfile_b_.Close();
251 if (test_mode_ != 0) {
252 printf("===============================================================\n");
253 }
254 test_count_++;
255 OpenOutFile(test_count_);
256 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, 0);
257 Run(channel_a_to_b_);
258 RegisterSendCodec('A', codec_l16, 32000, 512000, 640, 0);
259 Run(channel_a_to_b_);
260 outfile_b_.Close();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000261 if (test_mode_ != 0) {
262 printf("===============================================================\n");
263 }
264 test_count_++;
265 OpenOutFile(test_count_);
266 char codec_pcma[] = "PCMA";
267 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, 0);
268 Run(channel_a_to_b_);
269 RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, 0);
270 Run(channel_a_to_b_);
271 RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, 0);
272 Run(channel_a_to_b_);
273 RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, 0);
274 Run(channel_a_to_b_);
275 RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, 0);
276 Run(channel_a_to_b_);
277 RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, 0);
278 Run(channel_a_to_b_);
279 if (test_mode_ != 0) {
280 printf("===============================================================\n");
281 }
282 char codec_pcmu[] = "PCMU";
283 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, 0);
284 Run(channel_a_to_b_);
285 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, 0);
286 Run(channel_a_to_b_);
287 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, 0);
288 Run(channel_a_to_b_);
289 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, 0);
290 Run(channel_a_to_b_);
291 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, 0);
292 Run(channel_a_to_b_);
293 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, 0);
294 Run(channel_a_to_b_);
295 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000296#ifdef WEBRTC_CODEC_OPUS
297 if (test_mode_ != 0) {
298 printf("===============================================================\n");
299 }
300 test_count_++;
301 OpenOutFile(test_count_);
302 char codec_opus[] = "OPUS";
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000303 RegisterSendCodec('A', codec_opus, 48000, 6000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000304 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200305 RegisterSendCodec('A', codec_opus, 48000, 20000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000306 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200307 RegisterSendCodec('A', codec_opus, 48000, 32000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000308 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000309 RegisterSendCodec('A', codec_opus, 48000, 48000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000310 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200311 RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000312 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200313 RegisterSendCodec('A', codec_opus, 48000, 96000, 480 * 6, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000314 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200315 RegisterSendCodec('A', codec_opus, 48000, 500000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000316 Run(channel_a_to_b_);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000317 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000318#endif
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000319 if (test_mode_ != 0) {
320 printf("===============================================================\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000321
322 /* Print out all codecs that were not tested in the run */
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000323 printf("The following codecs was not included in the test:\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000324#ifndef WEBRTC_CODEC_ILBC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000325 printf(" iLBC\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000326#endif
327#ifndef WEBRTC_CODEC_ISAC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000328 printf(" ISAC float\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000329#endif
330#ifndef WEBRTC_CODEC_ISACFX
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000331 printf(" ISAC fix\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000332#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000333
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000334 printf("\nTo complete the test, listen to the %d number of output files.\n",
335 test_count_);
336 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000337}
338
339// Register Codec to use in the test
340//
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000341// Input: side - which ACM to use, 'A' or 'B'
342// codec_name - name to use when register the codec
343// sampling_freq_hz - sampling frequency in Herz
344// rate - bitrate in bytes
345// packet_size - packet size in samples
346// extra_byte - if extra bytes needed compared to the bitrate
niklase@google.com470e71d2011-07-07 08:21:25 +0000347// used when registering, can be an internal header
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000348// set to kVariableSize if the codec is a variable
349// rate codec
Yves Gerey665174f2018-06-19 15:03:05 +0200350void TestAllCodecs::RegisterSendCodec(char side,
351 char* codec_name,
352 int32_t sampling_freq_hz,
353 int rate,
354 int packet_size,
355 size_t extra_byte) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000356 if (test_mode_ != 0) {
357 // Print out codec and settings.
358 printf("codec: %s Freq: %d Rate: %d PackSize: %d\n", codec_name,
359 sampling_freq_hz, rate, packet_size);
360 }
361
362 // Store packet-size in samples, used to validate the received packet.
363 // If G.722, store half the size to compensate for the timestamp bug in the
364 // RFC for G.722.
365 // If iSAC runs in adaptive mode, packet size in samples can change on the
366 // fly, so we exclude this test by setting |packet_size_samples_| to -1.
367 if (!strcmp(codec_name, "G722")) {
368 packet_size_samples_ = packet_size / 2;
369 } else if (!strcmp(codec_name, "ISAC") && (rate == -1)) {
370 packet_size_samples_ = -1;
371 } else {
372 packet_size_samples_ = packet_size;
373 }
374
375 // Store the expected packet size in bytes, used to validate the received
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000376 // packet. If variable rate codec (extra_byte == -1), set to -1.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000377 if (extra_byte != kVariableSize) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000378 // Add 0.875 to always round up to a whole byte
Yves Gerey665174f2018-06-19 15:03:05 +0200379 packet_size_bytes_ =
380 static_cast<size_t>(static_cast<float>(packet_size * rate) /
381 static_cast<float>(sampling_freq_hz * 8) +
382 0.875) +
383 extra_byte;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000384 } else {
385 // Packets will have a variable size.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000386 packet_size_bytes_ = kVariableSize;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000387 }
388
389 // Set pointer to the ACM where to register the codec.
390 AudioCodingModule* my_acm = NULL;
391 switch (side) {
392 case 'A': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000393 my_acm = acm_a_.get();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000394 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000395 }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000396 case 'B': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000397 my_acm = acm_b_.get();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000398 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000399 }
Yves Gerey665174f2018-06-19 15:03:05 +0200400 default: { break; }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000401 }
402 ASSERT_TRUE(my_acm != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000403
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000404 // Get all codec parameters before registering
405 CodecInst my_codec_param;
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000406 CHECK_ERROR(AudioCodingModule::Codec(codec_name, &my_codec_param,
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000407 sampling_freq_hz, 1));
408 my_codec_param.rate = rate;
409 my_codec_param.pacsize = packet_size;
Karl Wiberg133cff02018-07-06 15:40:14 +0200410
411 auto factory = CreateBuiltinAudioEncoderFactory();
412 constexpr int payload_type = 17;
413 SdpAudioFormat format = CodecInstToSdp(my_codec_param);
414 format.parameters["ptime"] = rtc::ToString(rtc::CheckedDivExact(
415 packet_size, rtc::CheckedDivExact(sampling_freq_hz, 1000)));
416 my_acm->SetEncoder(
417 factory->MakeAudioEncoder(payload_type, format, absl::nullopt));
niklase@google.com470e71d2011-07-07 08:21:25 +0000418}
419
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000420void TestAllCodecs::Run(TestPack* channel) {
421 AudioFrame audio_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +0000422
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000423 int32_t out_freq_hz = outfile_b_.SamplingFrequency();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000424 size_t receive_size;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000425 uint32_t timestamp_diff;
426 channel->reset_payload_size();
427 int error_count = 0;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000428 int counter = 0;
Henrik Lundin4d682082015-12-10 16:24:39 +0100429 // Set test length to 500 ms (50 blocks of 10 ms each).
430 infile_a_.SetNum10MsBlocksToRead(50);
431 // Fast-forward 1 second (100 blocks) since the file starts with silence.
432 infile_a_.FastForward(100);
433
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000434 while (!infile_a_.EndOfFile()) {
435 // Add 10 msec to ACM.
436 infile_a_.Read10MsData(audio_frame);
437 CHECK_ERROR(acm_a_->Add10MsData(audio_frame));
niklase@google.com470e71d2011-07-07 08:21:25 +0000438
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000439 // Verify that the received packet size matches the settings.
440 receive_size = channel->payload_size();
441 if (receive_size) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000442 if ((receive_size != packet_size_bytes_) &&
443 (packet_size_bytes_ != kVariableSize)) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000444 error_count++;
445 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000446
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000447 // Verify that the timestamp is updated with expected length. The counter
448 // is used to avoid problems when switching codec or frame size in the
449 // test.
450 timestamp_diff = channel->timestamp_diff();
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000451 if ((counter > 10) &&
452 (static_cast<int>(timestamp_diff) != packet_size_samples_) &&
453 (packet_size_samples_ > -1))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000454 error_count++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000455 }
456
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000457 // Run received side of ACM.
henrik.lundind4ccb002016-05-17 12:21:55 -0700458 bool muted;
459 CHECK_ERROR(acm_b_->PlayoutData10Ms(out_freq_hz, &audio_frame, &muted));
460 ASSERT_FALSE(muted);
niklase@google.com470e71d2011-07-07 08:21:25 +0000461
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000462 // Write output speech to file.
yujo36b1a5f2017-06-12 12:45:32 -0700463 outfile_b_.Write10MsData(audio_frame.data(),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000464 audio_frame.samples_per_channel_);
465
466 // Update loop counter
467 counter++;
468 }
469
470 EXPECT_EQ(0, error_count);
471
472 if (infile_a_.EndOfFile()) {
473 infile_a_.Rewind();
474 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000475}
476
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000477void TestAllCodecs::OpenOutFile(int test_number) {
478 std::string filename = webrtc::test::OutputPath();
479 std::ostringstream test_number_str;
480 test_number_str << test_number;
481 filename += "testallcodecs_out_";
482 filename += test_number_str.str();
483 filename += ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000484 outfile_b_.Open(filename, 32000, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000485}
486
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000487void TestAllCodecs::DisplaySendReceiveCodec() {
488 CodecInst my_codec_param;
kwiberg1fd4a4a2015-11-03 11:20:50 -0800489 printf("%s -> ", acm_a_->SendCodec()->plname);
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000490 acm_b_->ReceiveCodec(&my_codec_param);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000491 printf("%s\n", my_codec_param.plname);
niklase@google.com470e71d2011-07-07 08:21:25 +0000492}
493
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000494} // namespace webrtc