blob: a3f0964651311ebc5891a97d1f6a900b9a44c54a [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),
115 packet_size_bytes_(0) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000116}
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000118TestAllCodecs::~TestAllCodecs() {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000119 if (channel_a_to_b_ != NULL) {
120 delete channel_a_to_b_;
121 channel_a_to_b_ = NULL;
122 }
123}
niklase@google.com470e71d2011-07-07 08:21:25 +0000124
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000125void TestAllCodecs::Perform() {
Yves Gerey665174f2018-06-19 15:03:05 +0200126 const std::string file_name =
127 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000128 infile_a_.Open(file_name, 32000, "rb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000130 acm_a_->InitializeReceiver();
131 acm_b_->InitializeReceiver();
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100133 acm_b_->SetReceiveCodecs({{103, {"ISAC", 16000, 1}},
134 {104, {"ISAC", 32000, 1}},
135 {107, {"L16", 8000, 1}},
136 {108, {"L16", 16000, 1}},
137 {109, {"L16", 32000, 1}},
138 {111, {"L16", 8000, 2}},
139 {112, {"L16", 16000, 2}},
140 {113, {"L16", 32000, 2}},
141 {0, {"PCMU", 8000, 1}},
142 {110, {"PCMU", 8000, 2}},
143 {8, {"PCMA", 8000, 1}},
144 {118, {"PCMA", 8000, 2}},
145 {102, {"ILBC", 8000, 1}},
146 {9, {"G722", 8000, 1}},
147 {119, {"G722", 8000, 2}},
148 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}},
149 {13, {"CN", 8000, 1}},
150 {98, {"CN", 16000, 1}},
151 {99, {"CN", 32000, 1}}});
niklase@google.com470e71d2011-07-07 08:21:25 +0000152
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000153 // Create and connect the channel
154 channel_a_to_b_ = new TestPack;
155 acm_a_->RegisterTransportCallback(channel_a_to_b_);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000156 channel_a_to_b_->RegisterReceiverACM(acm_b_.get());
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000157
158 // All codecs are tested for all allowed sampling frequencies, rates and
159 // packet sizes.
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000160 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 test_count_++;
178 OpenOutFile(test_count_);
179 char codec_ilbc[] = "ILBC";
180 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 240, 0);
181 Run(channel_a_to_b_);
182 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 480, 0);
183 Run(channel_a_to_b_);
184 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 160, 0);
185 Run(channel_a_to_b_);
186 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 320, 0);
187 Run(channel_a_to_b_);
188 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000189#endif
190#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000191 test_count_++;
192 OpenOutFile(test_count_);
193 char codec_isac[] = "ISAC";
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000194 RegisterSendCodec('A', codec_isac, 16000, -1, 480, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000195 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000196 RegisterSendCodec('A', codec_isac, 16000, -1, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000197 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000198 RegisterSendCodec('A', codec_isac, 16000, 15000, 480, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000199 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000200 RegisterSendCodec('A', codec_isac, 16000, 32000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000201 Run(channel_a_to_b_);
202 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000203#endif
204#ifdef WEBRTC_CODEC_ISAC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000205 test_count_++;
206 OpenOutFile(test_count_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000207 RegisterSendCodec('A', codec_isac, 32000, -1, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000208 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000209 RegisterSendCodec('A', codec_isac, 32000, 56000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000210 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000211 RegisterSendCodec('A', codec_isac, 32000, 37000, 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, 32000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000214 Run(channel_a_to_b_);
215 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000216#endif
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000217 test_count_++;
218 OpenOutFile(test_count_);
219 char codec_l16[] = "L16";
220 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, 0);
221 Run(channel_a_to_b_);
222 RegisterSendCodec('A', codec_l16, 8000, 128000, 160, 0);
223 Run(channel_a_to_b_);
224 RegisterSendCodec('A', codec_l16, 8000, 128000, 240, 0);
225 Run(channel_a_to_b_);
226 RegisterSendCodec('A', codec_l16, 8000, 128000, 320, 0);
227 Run(channel_a_to_b_);
228 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100229
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000230 test_count_++;
231 OpenOutFile(test_count_);
232 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, 0);
233 Run(channel_a_to_b_);
234 RegisterSendCodec('A', codec_l16, 16000, 256000, 320, 0);
235 Run(channel_a_to_b_);
236 RegisterSendCodec('A', codec_l16, 16000, 256000, 480, 0);
237 Run(channel_a_to_b_);
238 RegisterSendCodec('A', codec_l16, 16000, 256000, 640, 0);
239 Run(channel_a_to_b_);
240 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100241
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000242 test_count_++;
243 OpenOutFile(test_count_);
244 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, 0);
245 Run(channel_a_to_b_);
246 RegisterSendCodec('A', codec_l16, 32000, 512000, 640, 0);
247 Run(channel_a_to_b_);
248 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100249
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000250 test_count_++;
251 OpenOutFile(test_count_);
252 char codec_pcma[] = "PCMA";
253 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, 0);
254 Run(channel_a_to_b_);
255 RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, 0);
256 Run(channel_a_to_b_);
257 RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, 0);
258 Run(channel_a_to_b_);
259 RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, 0);
260 Run(channel_a_to_b_);
261 RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, 0);
262 Run(channel_a_to_b_);
263 RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, 0);
264 Run(channel_a_to_b_);
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100265
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000266 char codec_pcmu[] = "PCMU";
267 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, 0);
268 Run(channel_a_to_b_);
269 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, 0);
270 Run(channel_a_to_b_);
271 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, 0);
272 Run(channel_a_to_b_);
273 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, 0);
274 Run(channel_a_to_b_);
275 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, 0);
276 Run(channel_a_to_b_);
277 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, 0);
278 Run(channel_a_to_b_);
279 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000280#ifdef WEBRTC_CODEC_OPUS
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000281 test_count_++;
282 OpenOutFile(test_count_);
283 char codec_opus[] = "OPUS";
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000284 RegisterSendCodec('A', codec_opus, 48000, 6000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000285 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200286 RegisterSendCodec('A', codec_opus, 48000, 20000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000287 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200288 RegisterSendCodec('A', codec_opus, 48000, 32000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000289 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000290 RegisterSendCodec('A', codec_opus, 48000, 48000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000291 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200292 RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000293 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200294 RegisterSendCodec('A', codec_opus, 48000, 96000, 480 * 6, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000295 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200296 RegisterSendCodec('A', codec_opus, 48000, 500000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000297 Run(channel_a_to_b_);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000298 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000299#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000300}
301
302// Register Codec to use in the test
303//
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000304// Input: side - which ACM to use, 'A' or 'B'
305// codec_name - name to use when register the codec
306// sampling_freq_hz - sampling frequency in Herz
307// rate - bitrate in bytes
308// packet_size - packet size in samples
309// extra_byte - if extra bytes needed compared to the bitrate
niklase@google.com470e71d2011-07-07 08:21:25 +0000310// used when registering, can be an internal header
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000311// set to kVariableSize if the codec is a variable
312// rate codec
Yves Gerey665174f2018-06-19 15:03:05 +0200313void TestAllCodecs::RegisterSendCodec(char side,
314 char* codec_name,
315 int32_t sampling_freq_hz,
316 int rate,
317 int packet_size,
318 size_t extra_byte) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000319 // Store packet-size in samples, used to validate the received packet.
320 // If G.722, store half the size to compensate for the timestamp bug in the
321 // RFC for G.722.
322 // If iSAC runs in adaptive mode, packet size in samples can change on the
323 // fly, so we exclude this test by setting |packet_size_samples_| to -1.
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100324 int clockrate_hz = sampling_freq_hz;
325 size_t num_channels = 1;
326 if (absl::EqualsIgnoreCase(codec_name, "G722")) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000327 packet_size_samples_ = packet_size / 2;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100328 clockrate_hz = sampling_freq_hz / 2;
329 } else if (absl::EqualsIgnoreCase(codec_name, "ISAC") && (rate == -1)) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000330 packet_size_samples_ = -1;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100331 } else if (absl::EqualsIgnoreCase(codec_name, "OPUS")) {
332 packet_size_samples_ = packet_size;
333 num_channels = 2;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000334 } else {
335 packet_size_samples_ = packet_size;
336 }
337
338 // Store the expected packet size in bytes, used to validate the received
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000339 // packet. If variable rate codec (extra_byte == -1), set to -1.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000340 if (extra_byte != kVariableSize) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000341 // Add 0.875 to always round up to a whole byte
Yves Gerey665174f2018-06-19 15:03:05 +0200342 packet_size_bytes_ =
343 static_cast<size_t>(static_cast<float>(packet_size * rate) /
344 static_cast<float>(sampling_freq_hz * 8) +
345 0.875) +
346 extra_byte;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000347 } else {
348 // Packets will have a variable size.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000349 packet_size_bytes_ = kVariableSize;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000350 }
351
352 // Set pointer to the ACM where to register the codec.
353 AudioCodingModule* my_acm = NULL;
354 switch (side) {
355 case 'A': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000356 my_acm = acm_a_.get();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000357 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000358 }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000359 case 'B': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000360 my_acm = acm_b_.get();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000361 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000362 }
Yves Gerey665174f2018-06-19 15:03:05 +0200363 default: { break; }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000364 }
365 ASSERT_TRUE(my_acm != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000366
Karl Wiberg133cff02018-07-06 15:40:14 +0200367 auto factory = CreateBuiltinAudioEncoderFactory();
368 constexpr int payload_type = 17;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100369 SdpAudioFormat format = { codec_name, clockrate_hz, num_channels };
Karl Wiberg133cff02018-07-06 15:40:14 +0200370 format.parameters["ptime"] = rtc::ToString(rtc::CheckedDivExact(
371 packet_size, rtc::CheckedDivExact(sampling_freq_hz, 1000)));
372 my_acm->SetEncoder(
373 factory->MakeAudioEncoder(payload_type, format, absl::nullopt));
niklase@google.com470e71d2011-07-07 08:21:25 +0000374}
375
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000376void TestAllCodecs::Run(TestPack* channel) {
377 AudioFrame audio_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +0000378
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000379 int32_t out_freq_hz = outfile_b_.SamplingFrequency();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000380 size_t receive_size;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000381 uint32_t timestamp_diff;
382 channel->reset_payload_size();
383 int error_count = 0;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000384 int counter = 0;
Henrik Lundin4d682082015-12-10 16:24:39 +0100385 // Set test length to 500 ms (50 blocks of 10 ms each).
386 infile_a_.SetNum10MsBlocksToRead(50);
387 // Fast-forward 1 second (100 blocks) since the file starts with silence.
388 infile_a_.FastForward(100);
389
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000390 while (!infile_a_.EndOfFile()) {
391 // Add 10 msec to ACM.
392 infile_a_.Read10MsData(audio_frame);
393 CHECK_ERROR(acm_a_->Add10MsData(audio_frame));
niklase@google.com470e71d2011-07-07 08:21:25 +0000394
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000395 // Verify that the received packet size matches the settings.
396 receive_size = channel->payload_size();
397 if (receive_size) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000398 if ((receive_size != packet_size_bytes_) &&
399 (packet_size_bytes_ != kVariableSize)) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000400 error_count++;
401 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000402
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000403 // Verify that the timestamp is updated with expected length. The counter
404 // is used to avoid problems when switching codec or frame size in the
405 // test.
406 timestamp_diff = channel->timestamp_diff();
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000407 if ((counter > 10) &&
408 (static_cast<int>(timestamp_diff) != packet_size_samples_) &&
409 (packet_size_samples_ > -1))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000410 error_count++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000411 }
412
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000413 // Run received side of ACM.
henrik.lundind4ccb002016-05-17 12:21:55 -0700414 bool muted;
415 CHECK_ERROR(acm_b_->PlayoutData10Ms(out_freq_hz, &audio_frame, &muted));
416 ASSERT_FALSE(muted);
niklase@google.com470e71d2011-07-07 08:21:25 +0000417
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000418 // Write output speech to file.
yujo36b1a5f2017-06-12 12:45:32 -0700419 outfile_b_.Write10MsData(audio_frame.data(),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000420 audio_frame.samples_per_channel_);
421
422 // Update loop counter
423 counter++;
424 }
425
426 EXPECT_EQ(0, error_count);
427
428 if (infile_a_.EndOfFile()) {
429 infile_a_.Rewind();
430 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000431}
432
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000433void TestAllCodecs::OpenOutFile(int test_number) {
434 std::string filename = webrtc::test::OutputPath();
Jonas Olsson366a50c2018-09-06 13:41:30 +0200435 rtc::StringBuilder test_number_str;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000436 test_number_str << test_number;
437 filename += "testallcodecs_out_";
438 filename += test_number_str.str();
439 filename += ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000440 outfile_b_.Open(filename, 32000, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000441}
442
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000443} // namespace webrtc