blob: be4460e83bc410e9dc7d3daa1e8fa97bfc47c183 [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,
Niels Möllerc35b6e62019-04-25 16:31:18 +020067 size_t payload_size) {
Niels Möllerafb5dbb2019-02-15 15:21:47 +010068 RTPHeader rtp_header;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000069 int32_t status;
niklase@google.com470e71d2011-07-07 08:21:25 +000070
Niels Möllerafb5dbb2019-02-15 15:21:47 +010071 rtp_header.markerBit = false;
72 rtp_header.ssrc = 0;
73 rtp_header.sequenceNumber = sequence_number_++;
74 rtp_header.payloadType = payload_type;
75 rtp_header.timestamp = timestamp;
philipel0a5fe772018-06-19 16:18:31 +020076
Niels Möllerc936cb62019-03-19 14:10:16 +010077 if (frame_type == AudioFrameType::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.
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000083 memcpy(payload_data_, payload_data, payload_size);
84
Niels Möllerafb5dbb2019-02-15 15:21:47 +010085 status =
86 receiver_acm_->IncomingPacket(payload_data_, payload_size, rtp_header);
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
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100107TestAllCodecs::TestAllCodecs()
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),
Jonas Olssona4d87372019-07-05 19:08:33 +0200115 packet_size_bytes_(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000117TestAllCodecs::~TestAllCodecs() {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000118 if (channel_a_to_b_ != NULL) {
119 delete channel_a_to_b_;
120 channel_a_to_b_ = NULL;
121 }
122}
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000124void TestAllCodecs::Perform() {
Yves Gerey665174f2018-06-19 15:03:05 +0200125 const std::string file_name =
126 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000127 infile_a_.Open(file_name, 32000, "rb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000128
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000129 acm_a_->InitializeReceiver();
130 acm_b_->InitializeReceiver();
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100132 acm_b_->SetReceiveCodecs({{103, {"ISAC", 16000, 1}},
133 {104, {"ISAC", 32000, 1}},
134 {107, {"L16", 8000, 1}},
135 {108, {"L16", 16000, 1}},
136 {109, {"L16", 32000, 1}},
137 {111, {"L16", 8000, 2}},
138 {112, {"L16", 16000, 2}},
139 {113, {"L16", 32000, 2}},
140 {0, {"PCMU", 8000, 1}},
141 {110, {"PCMU", 8000, 2}},
142 {8, {"PCMA", 8000, 1}},
143 {118, {"PCMA", 8000, 2}},
144 {102, {"ILBC", 8000, 1}},
145 {9, {"G722", 8000, 1}},
146 {119, {"G722", 8000, 2}},
147 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}},
148 {13, {"CN", 8000, 1}},
149 {98, {"CN", 16000, 1}},
150 {99, {"CN", 32000, 1}}});
niklase@google.com470e71d2011-07-07 08:21:25 +0000151
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000152 // Create and connect the channel
153 channel_a_to_b_ = new TestPack;
154 acm_a_->RegisterTransportCallback(channel_a_to_b_);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000155 channel_a_to_b_->RegisterReceiverACM(acm_b_.get());
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000156
157 // All codecs are tested for all allowed sampling frequencies, rates and
158 // packet sizes.
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000159 test_count_++;
160 OpenOutFile(test_count_);
161 char codec_g722[] = "G722";
162 RegisterSendCodec('A', codec_g722, 16000, 64000, 160, 0);
163 Run(channel_a_to_b_);
164 RegisterSendCodec('A', codec_g722, 16000, 64000, 320, 0);
165 Run(channel_a_to_b_);
166 RegisterSendCodec('A', codec_g722, 16000, 64000, 480, 0);
167 Run(channel_a_to_b_);
168 RegisterSendCodec('A', codec_g722, 16000, 64000, 640, 0);
169 Run(channel_a_to_b_);
170 RegisterSendCodec('A', codec_g722, 16000, 64000, 800, 0);
171 Run(channel_a_to_b_);
172 RegisterSendCodec('A', codec_g722, 16000, 64000, 960, 0);
173 Run(channel_a_to_b_);
174 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000175#ifdef WEBRTC_CODEC_ILBC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000176 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 test_count_++;
191 OpenOutFile(test_count_);
192 char codec_isac[] = "ISAC";
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000193 RegisterSendCodec('A', codec_isac, 16000, -1, 480, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000194 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000195 RegisterSendCodec('A', codec_isac, 16000, -1, 960, 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, 15000, 480, 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, 32000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000200 Run(channel_a_to_b_);
201 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000202#endif
203#ifdef WEBRTC_CODEC_ISAC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000204 test_count_++;
205 OpenOutFile(test_count_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000206 RegisterSendCodec('A', codec_isac, 32000, -1, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000207 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000208 RegisterSendCodec('A', codec_isac, 32000, 56000, 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, 37000, 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, 32000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000213 Run(channel_a_to_b_);
214 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000215#endif
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000216 test_count_++;
217 OpenOutFile(test_count_);
218 char codec_l16[] = "L16";
219 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, 0);
220 Run(channel_a_to_b_);
221 RegisterSendCodec('A', codec_l16, 8000, 128000, 160, 0);
222 Run(channel_a_to_b_);
223 RegisterSendCodec('A', codec_l16, 8000, 128000, 240, 0);
224 Run(channel_a_to_b_);
225 RegisterSendCodec('A', codec_l16, 8000, 128000, 320, 0);
226 Run(channel_a_to_b_);
227 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100228
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000229 test_count_++;
230 OpenOutFile(test_count_);
231 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, 0);
232 Run(channel_a_to_b_);
233 RegisterSendCodec('A', codec_l16, 16000, 256000, 320, 0);
234 Run(channel_a_to_b_);
235 RegisterSendCodec('A', codec_l16, 16000, 256000, 480, 0);
236 Run(channel_a_to_b_);
237 RegisterSendCodec('A', codec_l16, 16000, 256000, 640, 0);
238 Run(channel_a_to_b_);
239 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100240
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000241 test_count_++;
242 OpenOutFile(test_count_);
243 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, 0);
244 Run(channel_a_to_b_);
245 RegisterSendCodec('A', codec_l16, 32000, 512000, 640, 0);
246 Run(channel_a_to_b_);
247 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100248
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000249 test_count_++;
250 OpenOutFile(test_count_);
251 char codec_pcma[] = "PCMA";
252 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, 0);
253 Run(channel_a_to_b_);
254 RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, 0);
255 Run(channel_a_to_b_);
256 RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, 0);
257 Run(channel_a_to_b_);
258 RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, 0);
259 Run(channel_a_to_b_);
260 RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, 0);
261 Run(channel_a_to_b_);
262 RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, 0);
263 Run(channel_a_to_b_);
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100264
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000265 char codec_pcmu[] = "PCMU";
266 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, 0);
267 Run(channel_a_to_b_);
268 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, 0);
269 Run(channel_a_to_b_);
270 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, 0);
271 Run(channel_a_to_b_);
272 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, 0);
273 Run(channel_a_to_b_);
274 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, 0);
275 Run(channel_a_to_b_);
276 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, 0);
277 Run(channel_a_to_b_);
278 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000279#ifdef WEBRTC_CODEC_OPUS
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000280 test_count_++;
281 OpenOutFile(test_count_);
282 char codec_opus[] = "OPUS";
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000283 RegisterSendCodec('A', codec_opus, 48000, 6000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000284 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200285 RegisterSendCodec('A', codec_opus, 48000, 20000, 480 * 2, 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, 32000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000288 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000289 RegisterSendCodec('A', codec_opus, 48000, 48000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000290 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200291 RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 4, 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, 96000, 480 * 6, 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, 500000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000296 Run(channel_a_to_b_);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000297 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000298#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000299}
300
301// Register Codec to use in the test
302//
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000303// Input: side - which ACM to use, 'A' or 'B'
304// codec_name - name to use when register the codec
305// sampling_freq_hz - sampling frequency in Herz
306// rate - bitrate in bytes
307// packet_size - packet size in samples
308// extra_byte - if extra bytes needed compared to the bitrate
niklase@google.com470e71d2011-07-07 08:21:25 +0000309// used when registering, can be an internal header
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000310// set to kVariableSize if the codec is a variable
311// rate codec
Yves Gerey665174f2018-06-19 15:03:05 +0200312void TestAllCodecs::RegisterSendCodec(char side,
313 char* codec_name,
314 int32_t sampling_freq_hz,
315 int rate,
316 int packet_size,
317 size_t extra_byte) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000318 // Store packet-size in samples, used to validate the received packet.
319 // If G.722, store half the size to compensate for the timestamp bug in the
320 // RFC for G.722.
321 // If iSAC runs in adaptive mode, packet size in samples can change on the
322 // fly, so we exclude this test by setting |packet_size_samples_| to -1.
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100323 int clockrate_hz = sampling_freq_hz;
324 size_t num_channels = 1;
325 if (absl::EqualsIgnoreCase(codec_name, "G722")) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000326 packet_size_samples_ = packet_size / 2;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100327 clockrate_hz = sampling_freq_hz / 2;
328 } else if (absl::EqualsIgnoreCase(codec_name, "ISAC") && (rate == -1)) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000329 packet_size_samples_ = -1;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100330 } else if (absl::EqualsIgnoreCase(codec_name, "OPUS")) {
331 packet_size_samples_ = packet_size;
332 num_channels = 2;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000333 } else {
334 packet_size_samples_ = packet_size;
335 }
336
337 // Store the expected packet size in bytes, used to validate the received
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000338 // packet. If variable rate codec (extra_byte == -1), set to -1.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000339 if (extra_byte != kVariableSize) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000340 // Add 0.875 to always round up to a whole byte
Yves Gerey665174f2018-06-19 15:03:05 +0200341 packet_size_bytes_ =
342 static_cast<size_t>(static_cast<float>(packet_size * rate) /
343 static_cast<float>(sampling_freq_hz * 8) +
344 0.875) +
345 extra_byte;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000346 } else {
347 // Packets will have a variable size.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000348 packet_size_bytes_ = kVariableSize;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000349 }
350
351 // Set pointer to the ACM where to register the codec.
352 AudioCodingModule* my_acm = NULL;
353 switch (side) {
354 case 'A': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000355 my_acm = acm_a_.get();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000356 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000357 }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000358 case 'B': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000359 my_acm = acm_b_.get();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000360 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000361 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200362 default: {
363 break;
364 }
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;
Jonas Olssona4d87372019-07-05 19:08:33 +0200370 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