blob: f8debe9ec155609b6743f0fcbf6f59b1b2ac3447 [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),
49 payload_size_(0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000050}
51
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000052TestPack::~TestPack() {
niklase@google.com470e71d2011-07-07 08:21:25 +000053}
54
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000055void TestPack::RegisterReceiverACM(AudioCodingModule* acm) {
56 receiver_acm_ = acm;
57 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000058}
59
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000060int32_t TestPack::SendData(FrameType frame_type, uint8_t payload_type,
61 uint32_t timestamp, 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;
72 if (frame_type == kAudioFrameCN) {
73 rtp_info.type.Audio.isCNG = true;
74 } else {
75 rtp_info.type.Audio.isCNG = false;
76 }
pbos22993e12015-10-19 02:39:06 -070077 if (frame_type == kEmptyFrame) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000078 // Skip this frame.
79 return 0;
80 }
81
82 // Only run mono for all test cases.
83 rtp_info.type.Audio.channel = 1;
84 memcpy(payload_data_, payload_data, payload_size);
85
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000086 status = receiver_acm_->IncomingPacket(payload_data_, payload_size, rtp_info);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000087
88 payload_size_ = payload_size;
89 timestamp_diff_ = timestamp - last_in_timestamp_;
90 last_in_timestamp_ = timestamp;
91 total_bytes_ += payload_size;
92 return status;
niklase@google.com470e71d2011-07-07 08:21:25 +000093}
94
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000095size_t TestPack::payload_size() {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000096 return payload_size_;
niklase@google.com470e71d2011-07-07 08:21:25 +000097}
98
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000099uint32_t TestPack::timestamp_diff() {
100 return timestamp_diff_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000101}
102
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000103void TestPack::reset_payload_size() {
104 payload_size_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000105}
106
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +0000107TestAllCodecs::TestAllCodecs(int test_mode)
Karl Wiberg5817d3d2018-04-06 10:06:42 +0200108 : acm_a_(AudioCodingModule::Create(
109 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
110 acm_b_(AudioCodingModule::Create(
111 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000112 channel_a_to_b_(NULL),
113 test_count_(0),
114 packet_size_samples_(0),
115 packet_size_bytes_(0) {
116 // test_mode = 0 for silent test (auto test)
117 test_mode_ = test_mode;
118}
niklase@google.com470e71d2011-07-07 08:21:25 +0000119
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000120TestAllCodecs::~TestAllCodecs() {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000121 if (channel_a_to_b_ != NULL) {
122 delete channel_a_to_b_;
123 channel_a_to_b_ = NULL;
124 }
125}
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000127void TestAllCodecs::Perform() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000128 const std::string file_name = webrtc::test::ResourcePath(
129 "audio_coding/testfile32kHz", "pcm");
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000130 infile_a_.Open(file_name, 32000, "rb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000132 if (test_mode_ == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100133 RTC_LOG(LS_INFO) << "---------- TestAllCodecs ----------";
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000134 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000136 acm_a_->InitializeReceiver();
137 acm_b_->InitializeReceiver();
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000139 uint8_t num_encoders = acm_a_->NumberOfCodecs();
140 CodecInst my_codec_param;
141 for (uint8_t n = 0; n < num_encoders; n++) {
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000142 acm_b_->Codec(n, &my_codec_param);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000143 if (!strcmp(my_codec_param.plname, "opus")) {
144 my_codec_param.channels = 1;
145 }
kwibergda2bf4e2016-10-24 13:47:09 -0700146 acm_b_->RegisterReceiveCodec(my_codec_param.pltype,
147 CodecInstToSdp(my_codec_param));
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000148 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000149
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000150 // Create and connect the channel
151 channel_a_to_b_ = new TestPack;
152 acm_a_->RegisterTransportCallback(channel_a_to_b_);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000153 channel_a_to_b_->RegisterReceiverACM(acm_b_.get());
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000154
155 // All codecs are tested for all allowed sampling frequencies, rates and
156 // packet sizes.
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000157 if (test_mode_ != 0) {
158 printf("===============================================================\n");
159 }
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();
niklase@google.com470e71d2011-07-07 08:21:25 +0000176#ifdef WEBRTC_CODEC_ILBC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000177 if (test_mode_ != 0) {
178 printf("===============================================================\n");
179 }
180 test_count_++;
181 OpenOutFile(test_count_);
182 char codec_ilbc[] = "ILBC";
183 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 240, 0);
184 Run(channel_a_to_b_);
185 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 480, 0);
186 Run(channel_a_to_b_);
187 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 160, 0);
188 Run(channel_a_to_b_);
189 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 320, 0);
190 Run(channel_a_to_b_);
191 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000192#endif
193#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000194 if (test_mode_ != 0) {
195 printf("===============================================================\n");
196 }
197 test_count_++;
198 OpenOutFile(test_count_);
199 char codec_isac[] = "ISAC";
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000200 RegisterSendCodec('A', codec_isac, 16000, -1, 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, -1, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000203 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000204 RegisterSendCodec('A', codec_isac, 16000, 15000, 480, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000205 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000206 RegisterSendCodec('A', codec_isac, 16000, 32000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000207 Run(channel_a_to_b_);
208 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000209#endif
210#ifdef WEBRTC_CODEC_ISAC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000211 if (test_mode_ != 0) {
212 printf("===============================================================\n");
213 }
214 test_count_++;
215 OpenOutFile(test_count_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000216 RegisterSendCodec('A', codec_isac, 32000, -1, 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, 56000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000219 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000220 RegisterSendCodec('A', codec_isac, 32000, 37000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000221 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000222 RegisterSendCodec('A', codec_isac, 32000, 32000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000223 Run(channel_a_to_b_);
224 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000225#endif
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000226 if (test_mode_ != 0) {
227 printf("===============================================================\n");
228 }
229 test_count_++;
230 OpenOutFile(test_count_);
231 char codec_l16[] = "L16";
232 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, 0);
233 Run(channel_a_to_b_);
234 RegisterSendCodec('A', codec_l16, 8000, 128000, 160, 0);
235 Run(channel_a_to_b_);
236 RegisterSendCodec('A', codec_l16, 8000, 128000, 240, 0);
237 Run(channel_a_to_b_);
238 RegisterSendCodec('A', codec_l16, 8000, 128000, 320, 0);
239 Run(channel_a_to_b_);
240 outfile_b_.Close();
241 if (test_mode_ != 0) {
242 printf("===============================================================\n");
243 }
244 test_count_++;
245 OpenOutFile(test_count_);
246 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, 0);
247 Run(channel_a_to_b_);
248 RegisterSendCodec('A', codec_l16, 16000, 256000, 320, 0);
249 Run(channel_a_to_b_);
250 RegisterSendCodec('A', codec_l16, 16000, 256000, 480, 0);
251 Run(channel_a_to_b_);
252 RegisterSendCodec('A', codec_l16, 16000, 256000, 640, 0);
253 Run(channel_a_to_b_);
254 outfile_b_.Close();
255 if (test_mode_ != 0) {
256 printf("===============================================================\n");
257 }
258 test_count_++;
259 OpenOutFile(test_count_);
260 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, 0);
261 Run(channel_a_to_b_);
262 RegisterSendCodec('A', codec_l16, 32000, 512000, 640, 0);
263 Run(channel_a_to_b_);
264 outfile_b_.Close();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000265 if (test_mode_ != 0) {
266 printf("===============================================================\n");
267 }
268 test_count_++;
269 OpenOutFile(test_count_);
270 char codec_pcma[] = "PCMA";
271 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, 0);
272 Run(channel_a_to_b_);
273 RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, 0);
274 Run(channel_a_to_b_);
275 RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, 0);
276 Run(channel_a_to_b_);
277 RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, 0);
278 Run(channel_a_to_b_);
279 RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, 0);
280 Run(channel_a_to_b_);
281 RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, 0);
282 Run(channel_a_to_b_);
283 if (test_mode_ != 0) {
284 printf("===============================================================\n");
285 }
286 char codec_pcmu[] = "PCMU";
287 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, 0);
288 Run(channel_a_to_b_);
289 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, 0);
290 Run(channel_a_to_b_);
291 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, 0);
292 Run(channel_a_to_b_);
293 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, 0);
294 Run(channel_a_to_b_);
295 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, 0);
296 Run(channel_a_to_b_);
297 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, 0);
298 Run(channel_a_to_b_);
299 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000300#ifdef WEBRTC_CODEC_OPUS
301 if (test_mode_ != 0) {
302 printf("===============================================================\n");
303 }
304 test_count_++;
305 OpenOutFile(test_count_);
306 char codec_opus[] = "OPUS";
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000307 RegisterSendCodec('A', codec_opus, 48000, 6000, 480, 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, 20000, 480*2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000310 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000311 RegisterSendCodec('A', codec_opus, 48000, 32000, 480*4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000312 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000313 RegisterSendCodec('A', codec_opus, 48000, 48000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000314 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000315 RegisterSendCodec('A', codec_opus, 48000, 64000, 480*4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000316 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000317 RegisterSendCodec('A', codec_opus, 48000, 96000, 480*6, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000318 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000319 RegisterSendCodec('A', codec_opus, 48000, 500000, 480*2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000320 Run(channel_a_to_b_);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000321 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000322#endif
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000323 if (test_mode_ != 0) {
324 printf("===============================================================\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000325
326 /* Print out all codecs that were not tested in the run */
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000327 printf("The following codecs was not included in the test:\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000328#ifndef WEBRTC_CODEC_ILBC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000329 printf(" iLBC\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000330#endif
331#ifndef WEBRTC_CODEC_ISAC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000332 printf(" ISAC float\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000333#endif
334#ifndef WEBRTC_CODEC_ISACFX
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000335 printf(" ISAC fix\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000336#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000337
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000338 printf("\nTo complete the test, listen to the %d number of output files.\n",
339 test_count_);
340 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000341}
342
343// Register Codec to use in the test
344//
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000345// Input: side - which ACM to use, 'A' or 'B'
346// codec_name - name to use when register the codec
347// sampling_freq_hz - sampling frequency in Herz
348// rate - bitrate in bytes
349// packet_size - packet size in samples
350// extra_byte - if extra bytes needed compared to the bitrate
niklase@google.com470e71d2011-07-07 08:21:25 +0000351// used when registering, can be an internal header
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000352// set to kVariableSize if the codec is a variable
353// rate codec
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000354void TestAllCodecs::RegisterSendCodec(char side, char* codec_name,
355 int32_t sampling_freq_hz, int rate,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000356 int packet_size, size_t extra_byte) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000357 if (test_mode_ != 0) {
358 // Print out codec and settings.
359 printf("codec: %s Freq: %d Rate: %d PackSize: %d\n", codec_name,
360 sampling_freq_hz, rate, packet_size);
361 }
362
363 // Store packet-size in samples, used to validate the received packet.
364 // If G.722, store half the size to compensate for the timestamp bug in the
365 // RFC for G.722.
366 // If iSAC runs in adaptive mode, packet size in samples can change on the
367 // fly, so we exclude this test by setting |packet_size_samples_| to -1.
368 if (!strcmp(codec_name, "G722")) {
369 packet_size_samples_ = packet_size / 2;
370 } else if (!strcmp(codec_name, "ISAC") && (rate == -1)) {
371 packet_size_samples_ = -1;
372 } else {
373 packet_size_samples_ = packet_size;
374 }
375
376 // Store the expected packet size in bytes, used to validate the received
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000377 // packet. If variable rate codec (extra_byte == -1), set to -1.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000378 if (extra_byte != kVariableSize) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000379 // Add 0.875 to always round up to a whole byte
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000380 packet_size_bytes_ = static_cast<size_t>(
381 static_cast<float>(packet_size * rate) /
382 static_cast<float>(sampling_freq_hz * 8) + 0.875) + 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 }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000399 default: {
400 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000401 }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000402 }
403 ASSERT_TRUE(my_acm != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000404
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000405 // Get all codec parameters before registering
406 CodecInst my_codec_param;
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000407 CHECK_ERROR(AudioCodingModule::Codec(codec_name, &my_codec_param,
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000408 sampling_freq_hz, 1));
409 my_codec_param.rate = rate;
410 my_codec_param.pacsize = packet_size;
411 CHECK_ERROR(my_acm->RegisterSendCodec(my_codec_param));
niklase@google.com470e71d2011-07-07 08:21:25 +0000412}
413
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000414void TestAllCodecs::Run(TestPack* channel) {
415 AudioFrame audio_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +0000416
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000417 int32_t out_freq_hz = outfile_b_.SamplingFrequency();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000418 size_t receive_size;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000419 uint32_t timestamp_diff;
420 channel->reset_payload_size();
421 int error_count = 0;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000422 int counter = 0;
Henrik Lundin4d682082015-12-10 16:24:39 +0100423 // Set test length to 500 ms (50 blocks of 10 ms each).
424 infile_a_.SetNum10MsBlocksToRead(50);
425 // Fast-forward 1 second (100 blocks) since the file starts with silence.
426 infile_a_.FastForward(100);
427
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000428 while (!infile_a_.EndOfFile()) {
429 // Add 10 msec to ACM.
430 infile_a_.Read10MsData(audio_frame);
431 CHECK_ERROR(acm_a_->Add10MsData(audio_frame));
niklase@google.com470e71d2011-07-07 08:21:25 +0000432
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000433 // Verify that the received packet size matches the settings.
434 receive_size = channel->payload_size();
435 if (receive_size) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000436 if ((receive_size != packet_size_bytes_) &&
437 (packet_size_bytes_ != kVariableSize)) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000438 error_count++;
439 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000440
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000441 // Verify that the timestamp is updated with expected length. The counter
442 // is used to avoid problems when switching codec or frame size in the
443 // test.
444 timestamp_diff = channel->timestamp_diff();
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000445 if ((counter > 10) &&
446 (static_cast<int>(timestamp_diff) != packet_size_samples_) &&
447 (packet_size_samples_ > -1))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000448 error_count++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000449 }
450
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000451 // Run received side of ACM.
henrik.lundind4ccb002016-05-17 12:21:55 -0700452 bool muted;
453 CHECK_ERROR(acm_b_->PlayoutData10Ms(out_freq_hz, &audio_frame, &muted));
454 ASSERT_FALSE(muted);
niklase@google.com470e71d2011-07-07 08:21:25 +0000455
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000456 // Write output speech to file.
yujo36b1a5f2017-06-12 12:45:32 -0700457 outfile_b_.Write10MsData(audio_frame.data(),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000458 audio_frame.samples_per_channel_);
459
460 // Update loop counter
461 counter++;
462 }
463
464 EXPECT_EQ(0, error_count);
465
466 if (infile_a_.EndOfFile()) {
467 infile_a_.Rewind();
468 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000469}
470
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000471void TestAllCodecs::OpenOutFile(int test_number) {
472 std::string filename = webrtc::test::OutputPath();
473 std::ostringstream test_number_str;
474 test_number_str << test_number;
475 filename += "testallcodecs_out_";
476 filename += test_number_str.str();
477 filename += ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000478 outfile_b_.Open(filename, 32000, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000479}
480
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000481void TestAllCodecs::DisplaySendReceiveCodec() {
482 CodecInst my_codec_param;
kwiberg1fd4a4a2015-11-03 11:20:50 -0800483 printf("%s -> ", acm_a_->SendCodec()->plname);
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000484 acm_b_->ReceiveCodec(&my_codec_param);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000485 printf("%s\n", my_codec_param.plname);
niklase@google.com470e71d2011-07-07 08:21:25 +0000486}
487
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000488} // namespace webrtc