blob: b44037d73253d6371f35909bf78d4d1b15ce8cd3 [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,
Minyue Liff0e4db2020-01-23 13:45:50 +010067 size_t payload_size,
68 int64_t absolute_capture_timestamp_ms) {
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
Niels Möllerc936cb62019-03-19 14:10:16 +010078 if (frame_type == AudioFrameType::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),
Jonas Olssona4d87372019-07-05 19:08:33 +0200116 packet_size_bytes_(0) {}
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
Alessio Bazzicab46c4bf2022-11-11 16:52:46 +0100133 acm_b_->SetReceiveCodecs({{107, {"L16", 8000, 1}},
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100134 {108, {"L16", 16000, 1}},
135 {109, {"L16", 32000, 1}},
136 {111, {"L16", 8000, 2}},
137 {112, {"L16", 16000, 2}},
138 {113, {"L16", 32000, 2}},
139 {0, {"PCMU", 8000, 1}},
140 {110, {"PCMU", 8000, 2}},
141 {8, {"PCMA", 8000, 1}},
142 {118, {"PCMA", 8000, 2}},
143 {102, {"ILBC", 8000, 1}},
144 {9, {"G722", 8000, 1}},
145 {119, {"G722", 8000, 2}},
146 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}},
147 {13, {"CN", 8000, 1}},
148 {98, {"CN", 16000, 1}},
149 {99, {"CN", 32000, 1}}});
niklase@google.com470e71d2011-07-07 08:21:25 +0000150
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000151 // Create and connect the channel
152 channel_a_to_b_ = new TestPack;
153 acm_a_->RegisterTransportCallback(channel_a_to_b_);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000154 channel_a_to_b_->RegisterReceiverACM(acm_b_.get());
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000155
156 // All codecs are tested for all allowed sampling frequencies, rates and
157 // packet sizes.
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000158 test_count_++;
159 OpenOutFile(test_count_);
160 char codec_g722[] = "G722";
161 RegisterSendCodec('A', codec_g722, 16000, 64000, 160, 0);
162 Run(channel_a_to_b_);
163 RegisterSendCodec('A', codec_g722, 16000, 64000, 320, 0);
164 Run(channel_a_to_b_);
165 RegisterSendCodec('A', codec_g722, 16000, 64000, 480, 0);
166 Run(channel_a_to_b_);
167 RegisterSendCodec('A', codec_g722, 16000, 64000, 640, 0);
168 Run(channel_a_to_b_);
169 RegisterSendCodec('A', codec_g722, 16000, 64000, 800, 0);
170 Run(channel_a_to_b_);
171 RegisterSendCodec('A', codec_g722, 16000, 64000, 960, 0);
172 Run(channel_a_to_b_);
173 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000174#ifdef WEBRTC_CODEC_ILBC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000175 test_count_++;
176 OpenOutFile(test_count_);
177 char codec_ilbc[] = "ILBC";
178 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 240, 0);
179 Run(channel_a_to_b_);
180 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 480, 0);
181 Run(channel_a_to_b_);
182 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 160, 0);
183 Run(channel_a_to_b_);
184 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 320, 0);
185 Run(channel_a_to_b_);
186 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000187#endif
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000188 test_count_++;
189 OpenOutFile(test_count_);
190 char codec_l16[] = "L16";
191 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, 0);
192 Run(channel_a_to_b_);
193 RegisterSendCodec('A', codec_l16, 8000, 128000, 160, 0);
194 Run(channel_a_to_b_);
195 RegisterSendCodec('A', codec_l16, 8000, 128000, 240, 0);
196 Run(channel_a_to_b_);
197 RegisterSendCodec('A', codec_l16, 8000, 128000, 320, 0);
198 Run(channel_a_to_b_);
199 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100200
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000201 test_count_++;
202 OpenOutFile(test_count_);
203 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, 0);
204 Run(channel_a_to_b_);
205 RegisterSendCodec('A', codec_l16, 16000, 256000, 320, 0);
206 Run(channel_a_to_b_);
207 RegisterSendCodec('A', codec_l16, 16000, 256000, 480, 0);
208 Run(channel_a_to_b_);
209 RegisterSendCodec('A', codec_l16, 16000, 256000, 640, 0);
210 Run(channel_a_to_b_);
211 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100212
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000213 test_count_++;
214 OpenOutFile(test_count_);
215 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, 0);
216 Run(channel_a_to_b_);
217 RegisterSendCodec('A', codec_l16, 32000, 512000, 640, 0);
218 Run(channel_a_to_b_);
219 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100220
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000221 test_count_++;
222 OpenOutFile(test_count_);
223 char codec_pcma[] = "PCMA";
224 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, 0);
225 Run(channel_a_to_b_);
226 RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, 0);
227 Run(channel_a_to_b_);
228 RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, 0);
229 Run(channel_a_to_b_);
230 RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, 0);
231 Run(channel_a_to_b_);
232 RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, 0);
233 Run(channel_a_to_b_);
234 RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, 0);
235 Run(channel_a_to_b_);
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100236
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000237 char codec_pcmu[] = "PCMU";
238 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, 0);
239 Run(channel_a_to_b_);
240 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, 0);
241 Run(channel_a_to_b_);
242 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, 0);
243 Run(channel_a_to_b_);
244 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, 0);
245 Run(channel_a_to_b_);
246 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, 0);
247 Run(channel_a_to_b_);
248 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, 0);
249 Run(channel_a_to_b_);
250 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000251#ifdef WEBRTC_CODEC_OPUS
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000252 test_count_++;
253 OpenOutFile(test_count_);
254 char codec_opus[] = "OPUS";
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000255 RegisterSendCodec('A', codec_opus, 48000, 6000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000256 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200257 RegisterSendCodec('A', codec_opus, 48000, 20000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000258 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200259 RegisterSendCodec('A', codec_opus, 48000, 32000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000260 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000261 RegisterSendCodec('A', codec_opus, 48000, 48000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000262 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200263 RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000264 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200265 RegisterSendCodec('A', codec_opus, 48000, 96000, 480 * 6, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000266 Run(channel_a_to_b_);
Yves Gerey665174f2018-06-19 15:03:05 +0200267 RegisterSendCodec('A', codec_opus, 48000, 500000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000268 Run(channel_a_to_b_);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000269 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000270#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000271}
272
273// Register Codec to use in the test
274//
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000275// Input: side - which ACM to use, 'A' or 'B'
276// codec_name - name to use when register the codec
277// sampling_freq_hz - sampling frequency in Herz
278// rate - bitrate in bytes
279// packet_size - packet size in samples
280// extra_byte - if extra bytes needed compared to the bitrate
niklase@google.com470e71d2011-07-07 08:21:25 +0000281// used when registering, can be an internal header
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000282// set to kVariableSize if the codec is a variable
283// rate codec
Yves Gerey665174f2018-06-19 15:03:05 +0200284void TestAllCodecs::RegisterSendCodec(char side,
285 char* codec_name,
286 int32_t sampling_freq_hz,
287 int rate,
288 int packet_size,
289 size_t extra_byte) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000290 // Store packet-size in samples, used to validate the received packet.
291 // If G.722, store half the size to compensate for the timestamp bug in the
292 // RFC for G.722.
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100293 int clockrate_hz = sampling_freq_hz;
294 size_t num_channels = 1;
295 if (absl::EqualsIgnoreCase(codec_name, "G722")) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000296 packet_size_samples_ = packet_size / 2;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100297 clockrate_hz = sampling_freq_hz / 2;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100298 } else if (absl::EqualsIgnoreCase(codec_name, "OPUS")) {
299 packet_size_samples_ = packet_size;
300 num_channels = 2;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000301 } else {
302 packet_size_samples_ = packet_size;
303 }
304
305 // Store the expected packet size in bytes, used to validate the received
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000306 // packet. If variable rate codec (extra_byte == -1), set to -1.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000307 if (extra_byte != kVariableSize) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000308 // Add 0.875 to always round up to a whole byte
Yves Gerey665174f2018-06-19 15:03:05 +0200309 packet_size_bytes_ =
310 static_cast<size_t>(static_cast<float>(packet_size * rate) /
311 static_cast<float>(sampling_freq_hz * 8) +
312 0.875) +
313 extra_byte;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000314 } else {
315 // Packets will have a variable size.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000316 packet_size_bytes_ = kVariableSize;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000317 }
318
319 // Set pointer to the ACM where to register the codec.
320 AudioCodingModule* my_acm = NULL;
321 switch (side) {
322 case 'A': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000323 my_acm = acm_a_.get();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000324 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000325 }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000326 case 'B': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000327 my_acm = acm_b_.get();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000328 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000329 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200330 default: {
331 break;
332 }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000333 }
334 ASSERT_TRUE(my_acm != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000335
Karl Wiberg133cff02018-07-06 15:40:14 +0200336 auto factory = CreateBuiltinAudioEncoderFactory();
337 constexpr int payload_type = 17;
Jonas Olssona4d87372019-07-05 19:08:33 +0200338 SdpAudioFormat format = {codec_name, clockrate_hz, num_channels};
Karl Wiberg133cff02018-07-06 15:40:14 +0200339 format.parameters["ptime"] = rtc::ToString(rtc::CheckedDivExact(
340 packet_size, rtc::CheckedDivExact(sampling_freq_hz, 1000)));
341 my_acm->SetEncoder(
342 factory->MakeAudioEncoder(payload_type, format, absl::nullopt));
niklase@google.com470e71d2011-07-07 08:21:25 +0000343}
344
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000345void TestAllCodecs::Run(TestPack* channel) {
346 AudioFrame audio_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +0000347
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000348 int32_t out_freq_hz = outfile_b_.SamplingFrequency();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000349 size_t receive_size;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000350 uint32_t timestamp_diff;
351 channel->reset_payload_size();
352 int error_count = 0;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000353 int counter = 0;
Henrik Lundin4d682082015-12-10 16:24:39 +0100354 // Set test length to 500 ms (50 blocks of 10 ms each).
355 infile_a_.SetNum10MsBlocksToRead(50);
356 // Fast-forward 1 second (100 blocks) since the file starts with silence.
357 infile_a_.FastForward(100);
358
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000359 while (!infile_a_.EndOfFile()) {
360 // Add 10 msec to ACM.
361 infile_a_.Read10MsData(audio_frame);
362 CHECK_ERROR(acm_a_->Add10MsData(audio_frame));
niklase@google.com470e71d2011-07-07 08:21:25 +0000363
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000364 // Verify that the received packet size matches the settings.
365 receive_size = channel->payload_size();
366 if (receive_size) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000367 if ((receive_size != packet_size_bytes_) &&
368 (packet_size_bytes_ != kVariableSize)) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000369 error_count++;
370 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000371
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000372 // Verify that the timestamp is updated with expected length. The counter
373 // is used to avoid problems when switching codec or frame size in the
374 // test.
375 timestamp_diff = channel->timestamp_diff();
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000376 if ((counter > 10) &&
377 (static_cast<int>(timestamp_diff) != packet_size_samples_) &&
378 (packet_size_samples_ > -1))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000379 error_count++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000380 }
381
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000382 // Run received side of ACM.
henrik.lundind4ccb002016-05-17 12:21:55 -0700383 bool muted;
384 CHECK_ERROR(acm_b_->PlayoutData10Ms(out_freq_hz, &audio_frame, &muted));
385 ASSERT_FALSE(muted);
niklase@google.com470e71d2011-07-07 08:21:25 +0000386
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000387 // Write output speech to file.
yujo36b1a5f2017-06-12 12:45:32 -0700388 outfile_b_.Write10MsData(audio_frame.data(),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000389 audio_frame.samples_per_channel_);
390
391 // Update loop counter
392 counter++;
393 }
394
395 EXPECT_EQ(0, error_count);
396
397 if (infile_a_.EndOfFile()) {
398 infile_a_.Rewind();
399 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000400}
401
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000402void TestAllCodecs::OpenOutFile(int test_number) {
403 std::string filename = webrtc::test::OutputPath();
Jonas Olsson366a50c2018-09-06 13:41:30 +0200404 rtc::StringBuilder test_number_str;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000405 test_number_str << test_number;
406 filename += "testallcodecs_out_";
407 filename += test_number_str.str();
408 filename += ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000409 outfile_b_.Open(filename, 32000, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000410}
411
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000412} // namespace webrtc