blob: 52518ac8a58ae953f51c16fc352190d463f43d31 [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
Fredrik Solenberg657b2962018-12-05 10:30:25 +010017#include "absl/strings/match.h"
Karl Wiberg5817d3d2018-04-06 10:06:42 +020018#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Karl Wiberg133cff02018-07-06 15:40:14 +020019#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
Fredrik Solenberg657b2962018-12-05 10:30:25 +010021#include "modules/include/module_common_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/string_encode.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020024#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "test/testsupport/file_utils.h"
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
Fredrik Solenberg657b2962018-12-05 10:30:25 +010036#define CHECK_ERROR(f) \
37 do { \
38 EXPECT_GE(f, 0) << "Error Calling API"; \
39 } while (0)
40
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000041namespace {
42const size_t kVariableSize = std::numeric_limits<size_t>::max();
43}
44
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000045namespace webrtc {
46
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000047// Class for simulating packet handling.
48TestPack::TestPack()
49 : receiver_acm_(NULL),
50 sequence_number_(0),
51 timestamp_diff_(0),
52 last_in_timestamp_(0),
53 total_bytes_(0),
Yves Gerey665174f2018-06-19 15:03:05 +020054 payload_size_(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000055
Yves Gerey665174f2018-06-19 15:03:05 +020056TestPack::~TestPack() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000057
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000058void TestPack::RegisterReceiverACM(AudioCodingModule* acm) {
59 receiver_acm_ = acm;
60 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000061}
62
Niels Möller87e2d782019-03-07 10:18:23 +010063int32_t TestPack::SendData(AudioFrameType frame_type,
Yves Gerey665174f2018-06-19 15:03:05 +020064 uint8_t payload_type,
65 uint32_t timestamp,
66 const uint8_t* payload_data,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000067 size_t payload_size,
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000068 const RTPFragmentationHeader* fragmentation) {
Niels Möllerafb5dbb2019-02-15 15:21:47 +010069 RTPHeader rtp_header;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000070 int32_t status;
niklase@google.com470e71d2011-07-07 08:21:25 +000071
Niels Möllerafb5dbb2019-02-15 15:21:47 +010072 rtp_header.markerBit = false;
73 rtp_header.ssrc = 0;
74 rtp_header.sequenceNumber = sequence_number_++;
75 rtp_header.payloadType = payload_type;
76 rtp_header.timestamp = timestamp;
philipel0a5fe772018-06-19 16:18:31 +020077
pbos22993e12015-10-19 02:39:06 -070078 if (frame_type == kEmptyFrame) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000079 // Skip this frame.
80 return 0;
81 }
82
83 // Only run mono for all test cases.
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000084 memcpy(payload_data_, payload_data, payload_size);
85
Niels Möllerafb5dbb2019-02-15 15:21:47 +010086 status =
87 receiver_acm_->IncomingPacket(payload_data_, payload_size, rtp_header);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000088
89 payload_size_ = payload_size;
90 timestamp_diff_ = timestamp - last_in_timestamp_;
91 last_in_timestamp_ = timestamp;
92 total_bytes_ += payload_size;
93 return status;
niklase@google.com470e71d2011-07-07 08:21:25 +000094}
95
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000096size_t TestPack::payload_size() {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000097 return payload_size_;
niklase@google.com470e71d2011-07-07 08:21:25 +000098}
99
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000100uint32_t TestPack::timestamp_diff() {
101 return timestamp_diff_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000102}
103
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000104void TestPack::reset_payload_size() {
105 payload_size_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106}
107
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100108TestAllCodecs::TestAllCodecs()
Karl Wiberg5817d3d2018-04-06 10:06:42 +0200109 : acm_a_(AudioCodingModule::Create(
110 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
111 acm_b_(AudioCodingModule::Create(
112 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000113 channel_a_to_b_(NULL),
114 test_count_(0),
115 packet_size_samples_(0),
116 packet_size_bytes_(0) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000117}
niklase@google.com470e71d2011-07-07 08:21:25 +0000118
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000119TestAllCodecs::~TestAllCodecs() {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000120 if (channel_a_to_b_ != NULL) {
121 delete channel_a_to_b_;
122 channel_a_to_b_ = NULL;
123 }
124}
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000126void TestAllCodecs::Perform() {
Yves Gerey665174f2018-06-19 15:03:05 +0200127 const std::string file_name =
128 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000129 infile_a_.Open(file_name, 32000, "rb");
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
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100134 acm_b_->SetReceiveCodecs({{103, {"ISAC", 16000, 1}},
135 {104, {"ISAC", 32000, 1}},
136 {107, {"L16", 8000, 1}},
137 {108, {"L16", 16000, 1}},
138 {109, {"L16", 32000, 1}},
139 {111, {"L16", 8000, 2}},
140 {112, {"L16", 16000, 2}},
141 {113, {"L16", 32000, 2}},
142 {0, {"PCMU", 8000, 1}},
143 {110, {"PCMU", 8000, 2}},
144 {8, {"PCMA", 8000, 1}},
145 {118, {"PCMA", 8000, 2}},
146 {102, {"ILBC", 8000, 1}},
147 {9, {"G722", 8000, 1}},
148 {119, {"G722", 8000, 2}},
149 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}},
150 {13, {"CN", 8000, 1}},
151 {98, {"CN", 16000, 1}},
152 {99, {"CN", 32000, 1}}});
niklase@google.com470e71d2011-07-07 08:21:25 +0000153
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000154 // Create and connect the channel
155 channel_a_to_b_ = new TestPack;
156 acm_a_->RegisterTransportCallback(channel_a_to_b_);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000157 channel_a_to_b_->RegisterReceiverACM(acm_b_.get());
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000158
159 // All codecs are tested for all allowed sampling frequencies, rates and
160 // packet sizes.
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000161 test_count_++;
162 OpenOutFile(test_count_);
163 char codec_g722[] = "G722";
164 RegisterSendCodec('A', codec_g722, 16000, 64000, 160, 0);
165 Run(channel_a_to_b_);
166 RegisterSendCodec('A', codec_g722, 16000, 64000, 320, 0);
167 Run(channel_a_to_b_);
168 RegisterSendCodec('A', codec_g722, 16000, 64000, 480, 0);
169 Run(channel_a_to_b_);
170 RegisterSendCodec('A', codec_g722, 16000, 64000, 640, 0);
171 Run(channel_a_to_b_);
172 RegisterSendCodec('A', codec_g722, 16000, 64000, 800, 0);
173 Run(channel_a_to_b_);
174 RegisterSendCodec('A', codec_g722, 16000, 64000, 960, 0);
175 Run(channel_a_to_b_);
176 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000177#ifdef WEBRTC_CODEC_ILBC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000178 test_count_++;
179 OpenOutFile(test_count_);
180 char codec_ilbc[] = "ILBC";
181 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 240, 0);
182 Run(channel_a_to_b_);
183 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 480, 0);
184 Run(channel_a_to_b_);
185 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 160, 0);
186 Run(channel_a_to_b_);
187 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 320, 0);
188 Run(channel_a_to_b_);
189 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000190#endif
191#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000192 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 test_count_++;
207 OpenOutFile(test_count_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000208 RegisterSendCodec('A', codec_isac, 32000, -1, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000209 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000210 RegisterSendCodec('A', codec_isac, 32000, 56000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000211 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000212 RegisterSendCodec('A', codec_isac, 32000, 37000, 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, 32000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000215 Run(channel_a_to_b_);
216 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000217#endif
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000218 test_count_++;
219 OpenOutFile(test_count_);
220 char codec_l16[] = "L16";
221 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, 0);
222 Run(channel_a_to_b_);
223 RegisterSendCodec('A', codec_l16, 8000, 128000, 160, 0);
224 Run(channel_a_to_b_);
225 RegisterSendCodec('A', codec_l16, 8000, 128000, 240, 0);
226 Run(channel_a_to_b_);
227 RegisterSendCodec('A', codec_l16, 8000, 128000, 320, 0);
228 Run(channel_a_to_b_);
229 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100230
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000231 test_count_++;
232 OpenOutFile(test_count_);
233 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, 0);
234 Run(channel_a_to_b_);
235 RegisterSendCodec('A', codec_l16, 16000, 256000, 320, 0);
236 Run(channel_a_to_b_);
237 RegisterSendCodec('A', codec_l16, 16000, 256000, 480, 0);
238 Run(channel_a_to_b_);
239 RegisterSendCodec('A', codec_l16, 16000, 256000, 640, 0);
240 Run(channel_a_to_b_);
241 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100242
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000243 test_count_++;
244 OpenOutFile(test_count_);
245 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, 0);
246 Run(channel_a_to_b_);
247 RegisterSendCodec('A', codec_l16, 32000, 512000, 640, 0);
248 Run(channel_a_to_b_);
249 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100250
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000251 test_count_++;
252 OpenOutFile(test_count_);
253 char codec_pcma[] = "PCMA";
254 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, 0);
255 Run(channel_a_to_b_);
256 RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, 0);
257 Run(channel_a_to_b_);
258 RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, 0);
259 Run(channel_a_to_b_);
260 RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, 0);
261 Run(channel_a_to_b_);
262 RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, 0);
263 Run(channel_a_to_b_);
264 RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, 0);
265 Run(channel_a_to_b_);
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100266
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000267 char codec_pcmu[] = "PCMU";
268 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, 0);
269 Run(channel_a_to_b_);
270 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, 0);
271 Run(channel_a_to_b_);
272 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, 0);
273 Run(channel_a_to_b_);
274 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, 0);
275 Run(channel_a_to_b_);
276 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, 0);
277 Run(channel_a_to_b_);
278 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, 0);
279 Run(channel_a_to_b_);
280 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000281#ifdef WEBRTC_CODEC_OPUS
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000282 test_count_++;
283 OpenOutFile(test_count_);
284 char codec_opus[] = "OPUS";
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000285 RegisterSendCodec('A', codec_opus, 48000, 6000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000286 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200287 RegisterSendCodec('A', codec_opus, 48000, 20000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000288 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200289 RegisterSendCodec('A', codec_opus, 48000, 32000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000290 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000291 RegisterSendCodec('A', codec_opus, 48000, 48000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000292 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200293 RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000294 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200295 RegisterSendCodec('A', codec_opus, 48000, 96000, 480 * 6, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000296 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200297 RegisterSendCodec('A', codec_opus, 48000, 500000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000298 Run(channel_a_to_b_);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000299 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000300#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000301}
302
303// Register Codec to use in the test
304//
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000305// Input: side - which ACM to use, 'A' or 'B'
306// codec_name - name to use when register the codec
307// sampling_freq_hz - sampling frequency in Herz
308// rate - bitrate in bytes
309// packet_size - packet size in samples
310// extra_byte - if extra bytes needed compared to the bitrate
niklase@google.com470e71d2011-07-07 08:21:25 +0000311// used when registering, can be an internal header
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000312// set to kVariableSize if the codec is a variable
313// rate codec
Yves Gerey665174f2018-06-19 15:03:05 +0200314void TestAllCodecs::RegisterSendCodec(char side,
315 char* codec_name,
316 int32_t sampling_freq_hz,
317 int rate,
318 int packet_size,
319 size_t extra_byte) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000320 // Store packet-size in samples, used to validate the received packet.
321 // If G.722, store half the size to compensate for the timestamp bug in the
322 // RFC for G.722.
323 // If iSAC runs in adaptive mode, packet size in samples can change on the
324 // fly, so we exclude this test by setting |packet_size_samples_| to -1.
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100325 int clockrate_hz = sampling_freq_hz;
326 size_t num_channels = 1;
327 if (absl::EqualsIgnoreCase(codec_name, "G722")) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000328 packet_size_samples_ = packet_size / 2;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100329 clockrate_hz = sampling_freq_hz / 2;
330 } else if (absl::EqualsIgnoreCase(codec_name, "ISAC") && (rate == -1)) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000331 packet_size_samples_ = -1;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100332 } else if (absl::EqualsIgnoreCase(codec_name, "OPUS")) {
333 packet_size_samples_ = packet_size;
334 num_channels = 2;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000335 } else {
336 packet_size_samples_ = packet_size;
337 }
338
339 // Store the expected packet size in bytes, used to validate the received
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000340 // packet. If variable rate codec (extra_byte == -1), set to -1.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000341 if (extra_byte != kVariableSize) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000342 // Add 0.875 to always round up to a whole byte
Yves Gerey665174f2018-06-19 15:03:05 +0200343 packet_size_bytes_ =
344 static_cast<size_t>(static_cast<float>(packet_size * rate) /
345 static_cast<float>(sampling_freq_hz * 8) +
346 0.875) +
347 extra_byte;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000348 } else {
349 // Packets will have a variable size.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000350 packet_size_bytes_ = kVariableSize;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000351 }
352
353 // Set pointer to the ACM where to register the codec.
354 AudioCodingModule* my_acm = NULL;
355 switch (side) {
356 case 'A': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000357 my_acm = acm_a_.get();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000358 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000359 }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000360 case 'B': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000361 my_acm = acm_b_.get();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000362 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000363 }
Yves Gerey665174f2018-06-19 15:03:05 +0200364 default: { break; }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000365 }
366 ASSERT_TRUE(my_acm != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000367
Karl Wiberg133cff02018-07-06 15:40:14 +0200368 auto factory = CreateBuiltinAudioEncoderFactory();
369 constexpr int payload_type = 17;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100370 SdpAudioFormat format = { codec_name, clockrate_hz, num_channels };
Karl Wiberg133cff02018-07-06 15:40:14 +0200371 format.parameters["ptime"] = rtc::ToString(rtc::CheckedDivExact(
372 packet_size, rtc::CheckedDivExact(sampling_freq_hz, 1000)));
373 my_acm->SetEncoder(
374 factory->MakeAudioEncoder(payload_type, format, absl::nullopt));
niklase@google.com470e71d2011-07-07 08:21:25 +0000375}
376
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000377void TestAllCodecs::Run(TestPack* channel) {
378 AudioFrame audio_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +0000379
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000380 int32_t out_freq_hz = outfile_b_.SamplingFrequency();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000381 size_t receive_size;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000382 uint32_t timestamp_diff;
383 channel->reset_payload_size();
384 int error_count = 0;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000385 int counter = 0;
Henrik Lundin4d682082015-12-10 16:24:39 +0100386 // Set test length to 500 ms (50 blocks of 10 ms each).
387 infile_a_.SetNum10MsBlocksToRead(50);
388 // Fast-forward 1 second (100 blocks) since the file starts with silence.
389 infile_a_.FastForward(100);
390
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000391 while (!infile_a_.EndOfFile()) {
392 // Add 10 msec to ACM.
393 infile_a_.Read10MsData(audio_frame);
394 CHECK_ERROR(acm_a_->Add10MsData(audio_frame));
niklase@google.com470e71d2011-07-07 08:21:25 +0000395
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000396 // Verify that the received packet size matches the settings.
397 receive_size = channel->payload_size();
398 if (receive_size) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000399 if ((receive_size != packet_size_bytes_) &&
400 (packet_size_bytes_ != kVariableSize)) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000401 error_count++;
402 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000403
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000404 // Verify that the timestamp is updated with expected length. The counter
405 // is used to avoid problems when switching codec or frame size in the
406 // test.
407 timestamp_diff = channel->timestamp_diff();
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000408 if ((counter > 10) &&
409 (static_cast<int>(timestamp_diff) != packet_size_samples_) &&
410 (packet_size_samples_ > -1))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000411 error_count++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000412 }
413
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000414 // Run received side of ACM.
henrik.lundind4ccb002016-05-17 12:21:55 -0700415 bool muted;
416 CHECK_ERROR(acm_b_->PlayoutData10Ms(out_freq_hz, &audio_frame, &muted));
417 ASSERT_FALSE(muted);
niklase@google.com470e71d2011-07-07 08:21:25 +0000418
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000419 // Write output speech to file.
yujo36b1a5f2017-06-12 12:45:32 -0700420 outfile_b_.Write10MsData(audio_frame.data(),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000421 audio_frame.samples_per_channel_);
422
423 // Update loop counter
424 counter++;
425 }
426
427 EXPECT_EQ(0, error_count);
428
429 if (infile_a_.EndOfFile()) {
430 infile_a_.Rewind();
431 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000432}
433
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000434void TestAllCodecs::OpenOutFile(int test_number) {
435 std::string filename = webrtc::test::OutputPath();
Jonas Olsson366a50c2018-09-06 13:41:30 +0200436 rtc::StringBuilder test_number_str;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000437 test_number_str << test_number;
438 filename += "testallcodecs_out_";
439 filename += test_number_str.str();
440 filename += ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000441 outfile_b_.Open(filename, 32000, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000442}
443
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000444} // namespace webrtc