blob: 74de1d9f22ed07198949e22fe95a54cb6210cacc [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"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/codecs/audio_format_conversion.h"
20#include "modules/audio_coding/include/audio_coding_module.h"
21#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
22#include "modules/audio_coding/test/utility.h"
23#include "rtc_base/logging.h"
24#include "test/gtest.h"
25#include "test/testsupport/fileutils.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020026#include "typedefs.h" // NOLINT(build/include)
niklase@google.com470e71d2011-07-07 08:21:25 +000027
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000028// 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
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000036namespace {
37const size_t kVariableSize = std::numeric_limits<size_t>::max();
38}
39
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000040namespace webrtc {
41
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000042// Class for simulating packet handling.
43TestPack::TestPack()
44 : receiver_acm_(NULL),
45 sequence_number_(0),
46 timestamp_diff_(0),
47 last_in_timestamp_(0),
48 total_bytes_(0),
Yves Gerey665174f2018-06-19 15:03:05 +020049 payload_size_(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000050
Yves Gerey665174f2018-06-19 15:03:05 +020051TestPack::~TestPack() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000052
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000053void TestPack::RegisterReceiverACM(AudioCodingModule* acm) {
54 receiver_acm_ = acm;
55 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000056}
57
Yves Gerey665174f2018-06-19 15:03:05 +020058int32_t TestPack::SendData(FrameType frame_type,
59 uint8_t payload_type,
60 uint32_t timestamp,
61 const uint8_t* payload_data,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000062 size_t payload_size,
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000063 const RTPFragmentationHeader* fragmentation) {
64 WebRtcRTPHeader rtp_info;
65 int32_t status;
niklase@google.com470e71d2011-07-07 08:21:25 +000066
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000067 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;
philipel0a5fe772018-06-19 16:18:31 +020072
pbos22993e12015-10-19 02:39:06 -070073 if (frame_type == kEmptyFrame) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000074 // Skip this frame.
75 return 0;
76 }
77
78 // Only run mono for all test cases.
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000079 memcpy(payload_data_, payload_data, payload_size);
80
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000081 status = receiver_acm_->IncomingPacket(payload_data_, payload_size, rtp_info);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000082
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;
niklase@google.com470e71d2011-07-07 08:21:25 +000088}
89
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000090size_t TestPack::payload_size() {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000091 return payload_size_;
niklase@google.com470e71d2011-07-07 08:21:25 +000092}
93
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000094uint32_t TestPack::timestamp_diff() {
95 return timestamp_diff_;
niklase@google.com470e71d2011-07-07 08:21:25 +000096}
97
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000098void TestPack::reset_payload_size() {
99 payload_size_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000100}
101
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +0000102TestAllCodecs::TestAllCodecs(int test_mode)
Karl Wiberg5817d3d2018-04-06 10:06:42 +0200103 : acm_a_(AudioCodingModule::Create(
104 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
105 acm_b_(AudioCodingModule::Create(
106 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000107 channel_a_to_b_(NULL),
108 test_count_(0),
109 packet_size_samples_(0),
110 packet_size_bytes_(0) {
111 // test_mode = 0 for silent test (auto test)
112 test_mode_ = test_mode;
113}
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000115TestAllCodecs::~TestAllCodecs() {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000116 if (channel_a_to_b_ != NULL) {
117 delete channel_a_to_b_;
118 channel_a_to_b_ = NULL;
119 }
120}
niklase@google.com470e71d2011-07-07 08:21:25 +0000121
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000122void TestAllCodecs::Perform() {
Yves Gerey665174f2018-06-19 15:03:05 +0200123 const std::string file_name =
124 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000125 infile_a_.Open(file_name, 32000, "rb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000127 if (test_mode_ == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100128 RTC_LOG(LS_INFO) << "---------- TestAllCodecs ----------";
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000129 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000131 acm_a_->InitializeReceiver();
132 acm_b_->InitializeReceiver();
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000134 uint8_t num_encoders = acm_a_->NumberOfCodecs();
135 CodecInst my_codec_param;
136 for (uint8_t n = 0; n < num_encoders; n++) {
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000137 acm_b_->Codec(n, &my_codec_param);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000138 if (!strcmp(my_codec_param.plname, "opus")) {
139 my_codec_param.channels = 1;
140 }
kwibergda2bf4e2016-10-24 13:47:09 -0700141 acm_b_->RegisterReceiveCodec(my_codec_param.pltype,
142 CodecInstToSdp(my_codec_param));
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000143 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000145 // Create and connect the channel
146 channel_a_to_b_ = new TestPack;
147 acm_a_->RegisterTransportCallback(channel_a_to_b_);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000148 channel_a_to_b_->RegisterReceiverACM(acm_b_.get());
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000149
150 // All codecs are tested for all allowed sampling frequencies, rates and
151 // packet sizes.
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000152 if (test_mode_ != 0) {
153 printf("===============================================================\n");
154 }
155 test_count_++;
156 OpenOutFile(test_count_);
157 char codec_g722[] = "G722";
158 RegisterSendCodec('A', codec_g722, 16000, 64000, 160, 0);
159 Run(channel_a_to_b_);
160 RegisterSendCodec('A', codec_g722, 16000, 64000, 320, 0);
161 Run(channel_a_to_b_);
162 RegisterSendCodec('A', codec_g722, 16000, 64000, 480, 0);
163 Run(channel_a_to_b_);
164 RegisterSendCodec('A', codec_g722, 16000, 64000, 640, 0);
165 Run(channel_a_to_b_);
166 RegisterSendCodec('A', codec_g722, 16000, 64000, 800, 0);
167 Run(channel_a_to_b_);
168 RegisterSendCodec('A', codec_g722, 16000, 64000, 960, 0);
169 Run(channel_a_to_b_);
170 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000171#ifdef WEBRTC_CODEC_ILBC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000172 if (test_mode_ != 0) {
173 printf("===============================================================\n");
174 }
175 test_count_++;
176 OpenOutFile(test_count_);
177 char codec_ilbc[] = "ILBC";
178 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 240, 0);
179 Run(channel_a_to_b_);
180 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 480, 0);
181 Run(channel_a_to_b_);
182 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 160, 0);
183 Run(channel_a_to_b_);
184 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 320, 0);
185 Run(channel_a_to_b_);
186 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000187#endif
188#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000189 if (test_mode_ != 0) {
190 printf("===============================================================\n");
191 }
192 test_count_++;
193 OpenOutFile(test_count_);
194 char codec_isac[] = "ISAC";
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000195 RegisterSendCodec('A', codec_isac, 16000, -1, 480, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000196 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000197 RegisterSendCodec('A', codec_isac, 16000, -1, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000198 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000199 RegisterSendCodec('A', codec_isac, 16000, 15000, 480, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000200 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000201 RegisterSendCodec('A', codec_isac, 16000, 32000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000202 Run(channel_a_to_b_);
203 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000204#endif
205#ifdef WEBRTC_CODEC_ISAC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000206 if (test_mode_ != 0) {
207 printf("===============================================================\n");
208 }
209 test_count_++;
210 OpenOutFile(test_count_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000211 RegisterSendCodec('A', codec_isac, 32000, -1, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000212 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000213 RegisterSendCodec('A', codec_isac, 32000, 56000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000214 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000215 RegisterSendCodec('A', codec_isac, 32000, 37000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000216 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000217 RegisterSendCodec('A', codec_isac, 32000, 32000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000218 Run(channel_a_to_b_);
219 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000220#endif
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000221 if (test_mode_ != 0) {
222 printf("===============================================================\n");
223 }
224 test_count_++;
225 OpenOutFile(test_count_);
226 char codec_l16[] = "L16";
227 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, 0);
228 Run(channel_a_to_b_);
229 RegisterSendCodec('A', codec_l16, 8000, 128000, 160, 0);
230 Run(channel_a_to_b_);
231 RegisterSendCodec('A', codec_l16, 8000, 128000, 240, 0);
232 Run(channel_a_to_b_);
233 RegisterSendCodec('A', codec_l16, 8000, 128000, 320, 0);
234 Run(channel_a_to_b_);
235 outfile_b_.Close();
236 if (test_mode_ != 0) {
237 printf("===============================================================\n");
238 }
239 test_count_++;
240 OpenOutFile(test_count_);
241 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, 0);
242 Run(channel_a_to_b_);
243 RegisterSendCodec('A', codec_l16, 16000, 256000, 320, 0);
244 Run(channel_a_to_b_);
245 RegisterSendCodec('A', codec_l16, 16000, 256000, 480, 0);
246 Run(channel_a_to_b_);
247 RegisterSendCodec('A', codec_l16, 16000, 256000, 640, 0);
248 Run(channel_a_to_b_);
249 outfile_b_.Close();
250 if (test_mode_ != 0) {
251 printf("===============================================================\n");
252 }
253 test_count_++;
254 OpenOutFile(test_count_);
255 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, 0);
256 Run(channel_a_to_b_);
257 RegisterSendCodec('A', codec_l16, 32000, 512000, 640, 0);
258 Run(channel_a_to_b_);
259 outfile_b_.Close();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000260 if (test_mode_ != 0) {
261 printf("===============================================================\n");
262 }
263 test_count_++;
264 OpenOutFile(test_count_);
265 char codec_pcma[] = "PCMA";
266 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, 0);
267 Run(channel_a_to_b_);
268 RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, 0);
269 Run(channel_a_to_b_);
270 RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, 0);
271 Run(channel_a_to_b_);
272 RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, 0);
273 Run(channel_a_to_b_);
274 RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, 0);
275 Run(channel_a_to_b_);
276 RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, 0);
277 Run(channel_a_to_b_);
278 if (test_mode_ != 0) {
279 printf("===============================================================\n");
280 }
281 char codec_pcmu[] = "PCMU";
282 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, 0);
283 Run(channel_a_to_b_);
284 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, 0);
285 Run(channel_a_to_b_);
286 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, 0);
287 Run(channel_a_to_b_);
288 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, 0);
289 Run(channel_a_to_b_);
290 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, 0);
291 Run(channel_a_to_b_);
292 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, 0);
293 Run(channel_a_to_b_);
294 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000295#ifdef WEBRTC_CODEC_OPUS
296 if (test_mode_ != 0) {
297 printf("===============================================================\n");
298 }
299 test_count_++;
300 OpenOutFile(test_count_);
301 char codec_opus[] = "OPUS";
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000302 RegisterSendCodec('A', codec_opus, 48000, 6000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000303 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200304 RegisterSendCodec('A', codec_opus, 48000, 20000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000305 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200306 RegisterSendCodec('A', codec_opus, 48000, 32000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000307 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000308 RegisterSendCodec('A', codec_opus, 48000, 48000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000309 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200310 RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000311 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200312 RegisterSendCodec('A', codec_opus, 48000, 96000, 480 * 6, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000313 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200314 RegisterSendCodec('A', codec_opus, 48000, 500000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000315 Run(channel_a_to_b_);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000316 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000317#endif
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000318 if (test_mode_ != 0) {
319 printf("===============================================================\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000320
321 /* Print out all codecs that were not tested in the run */
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000322 printf("The following codecs was not included in the test:\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000323#ifndef WEBRTC_CODEC_ILBC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000324 printf(" iLBC\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000325#endif
326#ifndef WEBRTC_CODEC_ISAC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000327 printf(" ISAC float\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000328#endif
329#ifndef WEBRTC_CODEC_ISACFX
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000330 printf(" ISAC fix\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000331#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000332
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000333 printf("\nTo complete the test, listen to the %d number of output files.\n",
334 test_count_);
335 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000336}
337
338// Register Codec to use in the test
339//
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000340// Input: side - which ACM to use, 'A' or 'B'
341// codec_name - name to use when register the codec
342// sampling_freq_hz - sampling frequency in Herz
343// rate - bitrate in bytes
344// packet_size - packet size in samples
345// extra_byte - if extra bytes needed compared to the bitrate
niklase@google.com470e71d2011-07-07 08:21:25 +0000346// used when registering, can be an internal header
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000347// set to kVariableSize if the codec is a variable
348// rate codec
Yves Gerey665174f2018-06-19 15:03:05 +0200349void TestAllCodecs::RegisterSendCodec(char side,
350 char* codec_name,
351 int32_t sampling_freq_hz,
352 int rate,
353 int packet_size,
354 size_t extra_byte) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000355 if (test_mode_ != 0) {
356 // Print out codec and settings.
357 printf("codec: %s Freq: %d Rate: %d PackSize: %d\n", codec_name,
358 sampling_freq_hz, rate, packet_size);
359 }
360
361 // Store packet-size in samples, used to validate the received packet.
362 // If G.722, store half the size to compensate for the timestamp bug in the
363 // RFC for G.722.
364 // If iSAC runs in adaptive mode, packet size in samples can change on the
365 // fly, so we exclude this test by setting |packet_size_samples_| to -1.
366 if (!strcmp(codec_name, "G722")) {
367 packet_size_samples_ = packet_size / 2;
368 } else if (!strcmp(codec_name, "ISAC") && (rate == -1)) {
369 packet_size_samples_ = -1;
370 } else {
371 packet_size_samples_ = packet_size;
372 }
373
374 // Store the expected packet size in bytes, used to validate the received
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000375 // packet. If variable rate codec (extra_byte == -1), set to -1.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000376 if (extra_byte != kVariableSize) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000377 // Add 0.875 to always round up to a whole byte
Yves Gerey665174f2018-06-19 15:03:05 +0200378 packet_size_bytes_ =
379 static_cast<size_t>(static_cast<float>(packet_size * rate) /
380 static_cast<float>(sampling_freq_hz * 8) +
381 0.875) +
382 extra_byte;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000383 } else {
384 // Packets will have a variable size.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000385 packet_size_bytes_ = kVariableSize;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000386 }
387
388 // Set pointer to the ACM where to register the codec.
389 AudioCodingModule* my_acm = NULL;
390 switch (side) {
391 case 'A': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000392 my_acm = acm_a_.get();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000393 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000394 }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000395 case 'B': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000396 my_acm = acm_b_.get();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000397 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000398 }
Yves Gerey665174f2018-06-19 15:03:05 +0200399 default: { break; }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000400 }
401 ASSERT_TRUE(my_acm != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000402
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000403 // Get all codec parameters before registering
404 CodecInst my_codec_param;
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000405 CHECK_ERROR(AudioCodingModule::Codec(codec_name, &my_codec_param,
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000406 sampling_freq_hz, 1));
407 my_codec_param.rate = rate;
408 my_codec_param.pacsize = packet_size;
409 CHECK_ERROR(my_acm->RegisterSendCodec(my_codec_param));
niklase@google.com470e71d2011-07-07 08:21:25 +0000410}
411
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000412void TestAllCodecs::Run(TestPack* channel) {
413 AudioFrame audio_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000415 int32_t out_freq_hz = outfile_b_.SamplingFrequency();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000416 size_t receive_size;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000417 uint32_t timestamp_diff;
418 channel->reset_payload_size();
419 int error_count = 0;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000420 int counter = 0;
Henrik Lundin4d682082015-12-10 16:24:39 +0100421 // Set test length to 500 ms (50 blocks of 10 ms each).
422 infile_a_.SetNum10MsBlocksToRead(50);
423 // Fast-forward 1 second (100 blocks) since the file starts with silence.
424 infile_a_.FastForward(100);
425
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000426 while (!infile_a_.EndOfFile()) {
427 // Add 10 msec to ACM.
428 infile_a_.Read10MsData(audio_frame);
429 CHECK_ERROR(acm_a_->Add10MsData(audio_frame));
niklase@google.com470e71d2011-07-07 08:21:25 +0000430
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000431 // Verify that the received packet size matches the settings.
432 receive_size = channel->payload_size();
433 if (receive_size) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000434 if ((receive_size != packet_size_bytes_) &&
435 (packet_size_bytes_ != kVariableSize)) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000436 error_count++;
437 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000438
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000439 // Verify that the timestamp is updated with expected length. The counter
440 // is used to avoid problems when switching codec or frame size in the
441 // test.
442 timestamp_diff = channel->timestamp_diff();
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000443 if ((counter > 10) &&
444 (static_cast<int>(timestamp_diff) != packet_size_samples_) &&
445 (packet_size_samples_ > -1))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000446 error_count++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000447 }
448
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000449 // Run received side of ACM.
henrik.lundind4ccb002016-05-17 12:21:55 -0700450 bool muted;
451 CHECK_ERROR(acm_b_->PlayoutData10Ms(out_freq_hz, &audio_frame, &muted));
452 ASSERT_FALSE(muted);
niklase@google.com470e71d2011-07-07 08:21:25 +0000453
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000454 // Write output speech to file.
yujo36b1a5f2017-06-12 12:45:32 -0700455 outfile_b_.Write10MsData(audio_frame.data(),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000456 audio_frame.samples_per_channel_);
457
458 // Update loop counter
459 counter++;
460 }
461
462 EXPECT_EQ(0, error_count);
463
464 if (infile_a_.EndOfFile()) {
465 infile_a_.Rewind();
466 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000467}
468
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000469void TestAllCodecs::OpenOutFile(int test_number) {
470 std::string filename = webrtc::test::OutputPath();
471 std::ostringstream test_number_str;
472 test_number_str << test_number;
473 filename += "testallcodecs_out_";
474 filename += test_number_str.str();
475 filename += ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000476 outfile_b_.Open(filename, 32000, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000477}
478
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000479void TestAllCodecs::DisplaySendReceiveCodec() {
480 CodecInst my_codec_param;
kwiberg1fd4a4a2015-11-03 11:20:50 -0800481 printf("%s -> ", acm_a_->SendCodec()->plname);
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000482 acm_b_->ReceiveCodec(&my_codec_param);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000483 printf("%s\n", my_codec_param.plname);
niklase@google.com470e71d2011-07-07 08:21:25 +0000484}
485
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000486} // namespace webrtc