blob: f840676f46eb28251d527a7e9b2d293df6351e6b [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "common_types.h"
18#include "modules/audio_coding/codecs/audio_format_conversion.h"
19#include "modules/audio_coding/include/audio_coding_module.h"
20#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
21#include "modules/audio_coding/test/utility.h"
22#include "rtc_base/logging.h"
23#include "test/gtest.h"
24#include "test/testsupport/fileutils.h"
25#include "typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000026
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000027// Description of the test:
28// In this test we set up a one-way communication channel from a participant
29// called "a" to a participant called "b".
30// a -> channel_a_to_b -> b
31//
32// The test loops through all available mono codecs, encode at "a" sends over
33// the channel, and decodes at "b".
34
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000035namespace {
36const size_t kVariableSize = std::numeric_limits<size_t>::max();
37}
38
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000039namespace webrtc {
40
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000041// Class for simulating packet handling.
42TestPack::TestPack()
43 : receiver_acm_(NULL),
44 sequence_number_(0),
45 timestamp_diff_(0),
46 last_in_timestamp_(0),
47 total_bytes_(0),
48 payload_size_(0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000049}
50
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000051TestPack::~TestPack() {
niklase@google.com470e71d2011-07-07 08:21:25 +000052}
53
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000054void TestPack::RegisterReceiverACM(AudioCodingModule* acm) {
55 receiver_acm_ = acm;
56 return;
niklase@google.com470e71d2011-07-07 08:21:25 +000057}
58
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000059int32_t TestPack::SendData(FrameType frame_type, uint8_t payload_type,
60 uint32_t timestamp, const uint8_t* payload_data,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000061 size_t payload_size,
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000062 const RTPFragmentationHeader* fragmentation) {
63 WebRtcRTPHeader rtp_info;
64 int32_t status;
niklase@google.com470e71d2011-07-07 08:21:25 +000065
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000066 rtp_info.header.markerBit = false;
67 rtp_info.header.ssrc = 0;
68 rtp_info.header.sequenceNumber = sequence_number_++;
69 rtp_info.header.payloadType = payload_type;
70 rtp_info.header.timestamp = timestamp;
71 if (frame_type == kAudioFrameCN) {
72 rtp_info.type.Audio.isCNG = true;
73 } else {
74 rtp_info.type.Audio.isCNG = false;
75 }
pbos22993e12015-10-19 02:39:06 -070076 if (frame_type == kEmptyFrame) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000077 // Skip this frame.
78 return 0;
79 }
80
81 // Only run mono for all test cases.
82 rtp_info.type.Audio.channel = 1;
83 memcpy(payload_data_, payload_data, payload_size);
84
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000085 status = receiver_acm_->IncomingPacket(payload_data_, payload_size, rtp_info);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000086
87 payload_size_ = payload_size;
88 timestamp_diff_ = timestamp - last_in_timestamp_;
89 last_in_timestamp_ = timestamp;
90 total_bytes_ += payload_size;
91 return status;
niklase@google.com470e71d2011-07-07 08:21:25 +000092}
93
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000094size_t TestPack::payload_size() {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000095 return payload_size_;
niklase@google.com470e71d2011-07-07 08:21:25 +000096}
97
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000098uint32_t TestPack::timestamp_diff() {
99 return timestamp_diff_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000100}
101
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000102void TestPack::reset_payload_size() {
103 payload_size_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104}
105
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +0000106TestAllCodecs::TestAllCodecs(int test_mode)
107 : acm_a_(AudioCodingModule::Create(0)),
108 acm_b_(AudioCodingModule::Create(1)),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000109 channel_a_to_b_(NULL),
110 test_count_(0),
111 packet_size_samples_(0),
112 packet_size_bytes_(0) {
113 // test_mode = 0 for silent test (auto test)
114 test_mode_ = test_mode;
115}
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() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000125 const std::string file_name = webrtc::test::ResourcePath(
126 "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 if (test_mode_ == 0) {
Alex Loiko300ec8c2017-05-30 17:23:28 +0200130 LOG(LS_INFO) << "---------- TestAllCodecs ----------";
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000131 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000133 acm_a_->InitializeReceiver();
134 acm_b_->InitializeReceiver();
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000136 uint8_t num_encoders = acm_a_->NumberOfCodecs();
137 CodecInst my_codec_param;
138 for (uint8_t n = 0; n < num_encoders; n++) {
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000139 acm_b_->Codec(n, &my_codec_param);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000140 if (!strcmp(my_codec_param.plname, "opus")) {
141 my_codec_param.channels = 1;
142 }
kwibergda2bf4e2016-10-24 13:47:09 -0700143 acm_b_->RegisterReceiveCodec(my_codec_param.pltype,
144 CodecInstToSdp(my_codec_param));
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000145 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000147 // Create and connect the channel
148 channel_a_to_b_ = new TestPack;
149 acm_a_->RegisterTransportCallback(channel_a_to_b_);
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000150 channel_a_to_b_->RegisterReceiverACM(acm_b_.get());
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000151
152 // All codecs are tested for all allowed sampling frequencies, rates and
153 // packet sizes.
niklase@google.com470e71d2011-07-07 08:21:25 +0000154#ifdef WEBRTC_CODEC_G722
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000155 if (test_mode_ != 0) {
156 printf("===============================================================\n");
157 }
158 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#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000175#ifdef WEBRTC_CODEC_ILBC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000176 if (test_mode_ != 0) {
177 printf("===============================================================\n");
178 }
179 test_count_++;
180 OpenOutFile(test_count_);
181 char codec_ilbc[] = "ILBC";
182 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 240, 0);
183 Run(channel_a_to_b_);
184 RegisterSendCodec('A', codec_ilbc, 8000, 13300, 480, 0);
185 Run(channel_a_to_b_);
186 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 160, 0);
187 Run(channel_a_to_b_);
188 RegisterSendCodec('A', codec_ilbc, 8000, 15200, 320, 0);
189 Run(channel_a_to_b_);
190 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000191#endif
192#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000193 if (test_mode_ != 0) {
194 printf("===============================================================\n");
195 }
196 test_count_++;
197 OpenOutFile(test_count_);
198 char codec_isac[] = "ISAC";
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000199 RegisterSendCodec('A', codec_isac, 16000, -1, 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, -1, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000202 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000203 RegisterSendCodec('A', codec_isac, 16000, 15000, 480, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000204 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000205 RegisterSendCodec('A', codec_isac, 16000, 32000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000206 Run(channel_a_to_b_);
207 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000208#endif
209#ifdef WEBRTC_CODEC_ISAC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000210 if (test_mode_ != 0) {
211 printf("===============================================================\n");
212 }
213 test_count_++;
214 OpenOutFile(test_count_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000215 RegisterSendCodec('A', codec_isac, 32000, -1, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000216 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000217 RegisterSendCodec('A', codec_isac, 32000, 56000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000218 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000219 RegisterSendCodec('A', codec_isac, 32000, 37000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000220 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000221 RegisterSendCodec('A', codec_isac, 32000, 32000, 960, kVariableSize);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000222 Run(channel_a_to_b_);
223 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000224#endif
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000225 if (test_mode_ != 0) {
226 printf("===============================================================\n");
227 }
228 test_count_++;
229 OpenOutFile(test_count_);
230 char codec_l16[] = "L16";
231 RegisterSendCodec('A', codec_l16, 8000, 128000, 80, 0);
232 Run(channel_a_to_b_);
233 RegisterSendCodec('A', codec_l16, 8000, 128000, 160, 0);
234 Run(channel_a_to_b_);
235 RegisterSendCodec('A', codec_l16, 8000, 128000, 240, 0);
236 Run(channel_a_to_b_);
237 RegisterSendCodec('A', codec_l16, 8000, 128000, 320, 0);
238 Run(channel_a_to_b_);
239 outfile_b_.Close();
240 if (test_mode_ != 0) {
241 printf("===============================================================\n");
242 }
243 test_count_++;
244 OpenOutFile(test_count_);
245 RegisterSendCodec('A', codec_l16, 16000, 256000, 160, 0);
246 Run(channel_a_to_b_);
247 RegisterSendCodec('A', codec_l16, 16000, 256000, 320, 0);
248 Run(channel_a_to_b_);
249 RegisterSendCodec('A', codec_l16, 16000, 256000, 480, 0);
250 Run(channel_a_to_b_);
251 RegisterSendCodec('A', codec_l16, 16000, 256000, 640, 0);
252 Run(channel_a_to_b_);
253 outfile_b_.Close();
254 if (test_mode_ != 0) {
255 printf("===============================================================\n");
256 }
257 test_count_++;
258 OpenOutFile(test_count_);
259 RegisterSendCodec('A', codec_l16, 32000, 512000, 320, 0);
260 Run(channel_a_to_b_);
261 RegisterSendCodec('A', codec_l16, 32000, 512000, 640, 0);
262 Run(channel_a_to_b_);
263 outfile_b_.Close();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000264 if (test_mode_ != 0) {
265 printf("===============================================================\n");
266 }
267 test_count_++;
268 OpenOutFile(test_count_);
269 char codec_pcma[] = "PCMA";
270 RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, 0);
271 Run(channel_a_to_b_);
272 RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, 0);
273 Run(channel_a_to_b_);
274 RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, 0);
275 Run(channel_a_to_b_);
276 RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, 0);
277 Run(channel_a_to_b_);
278 RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, 0);
279 Run(channel_a_to_b_);
280 RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, 0);
281 Run(channel_a_to_b_);
282 if (test_mode_ != 0) {
283 printf("===============================================================\n");
284 }
285 char codec_pcmu[] = "PCMU";
286 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, 0);
287 Run(channel_a_to_b_);
288 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, 0);
289 Run(channel_a_to_b_);
290 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, 0);
291 Run(channel_a_to_b_);
292 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, 0);
293 Run(channel_a_to_b_);
294 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, 0);
295 Run(channel_a_to_b_);
296 RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, 0);
297 Run(channel_a_to_b_);
298 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000299#ifdef WEBRTC_CODEC_OPUS
300 if (test_mode_ != 0) {
301 printf("===============================================================\n");
302 }
303 test_count_++;
304 OpenOutFile(test_count_);
305 char codec_opus[] = "OPUS";
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000306 RegisterSendCodec('A', codec_opus, 48000, 6000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000307 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000308 RegisterSendCodec('A', codec_opus, 48000, 20000, 480*2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000309 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000310 RegisterSendCodec('A', codec_opus, 48000, 32000, 480*4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000311 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000312 RegisterSendCodec('A', codec_opus, 48000, 48000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000313 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000314 RegisterSendCodec('A', codec_opus, 48000, 64000, 480*4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000315 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000316 RegisterSendCodec('A', codec_opus, 48000, 96000, 480*6, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000317 Run(channel_a_to_b_);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000318 RegisterSendCodec('A', codec_opus, 48000, 500000, 480*2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000319 Run(channel_a_to_b_);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000320 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000321#endif
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000322 if (test_mode_ != 0) {
323 printf("===============================================================\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000324
325 /* Print out all codecs that were not tested in the run */
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000326 printf("The following codecs was not included in the test:\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000327#ifndef WEBRTC_CODEC_G722
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000328 printf(" G.722\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000329#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000330#ifndef WEBRTC_CODEC_ILBC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000331 printf(" iLBC\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000332#endif
333#ifndef WEBRTC_CODEC_ISAC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000334 printf(" ISAC float\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000335#endif
336#ifndef WEBRTC_CODEC_ISACFX
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000337 printf(" ISAC fix\n");
niklase@google.com470e71d2011-07-07 08:21:25 +0000338#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000339
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000340 printf("\nTo complete the test, listen to the %d number of output files.\n",
341 test_count_);
342 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000343}
344
345// Register Codec to use in the test
346//
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000347// Input: side - which ACM to use, 'A' or 'B'
348// codec_name - name to use when register the codec
349// sampling_freq_hz - sampling frequency in Herz
350// rate - bitrate in bytes
351// packet_size - packet size in samples
352// extra_byte - if extra bytes needed compared to the bitrate
niklase@google.com470e71d2011-07-07 08:21:25 +0000353// used when registering, can be an internal header
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000354// set to kVariableSize if the codec is a variable
355// rate codec
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000356void TestAllCodecs::RegisterSendCodec(char side, char* codec_name,
357 int32_t sampling_freq_hz, int rate,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000358 int packet_size, size_t extra_byte) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000359 if (test_mode_ != 0) {
360 // Print out codec and settings.
361 printf("codec: %s Freq: %d Rate: %d PackSize: %d\n", codec_name,
362 sampling_freq_hz, rate, packet_size);
363 }
364
365 // Store packet-size in samples, used to validate the received packet.
366 // If G.722, store half the size to compensate for the timestamp bug in the
367 // RFC for G.722.
368 // If iSAC runs in adaptive mode, packet size in samples can change on the
369 // fly, so we exclude this test by setting |packet_size_samples_| to -1.
370 if (!strcmp(codec_name, "G722")) {
371 packet_size_samples_ = packet_size / 2;
372 } else if (!strcmp(codec_name, "ISAC") && (rate == -1)) {
373 packet_size_samples_ = -1;
374 } else {
375 packet_size_samples_ = packet_size;
376 }
377
378 // Store the expected packet size in bytes, used to validate the received
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000379 // packet. If variable rate codec (extra_byte == -1), set to -1.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000380 if (extra_byte != kVariableSize) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000381 // Add 0.875 to always round up to a whole byte
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000382 packet_size_bytes_ = static_cast<size_t>(
383 static_cast<float>(packet_size * rate) /
384 static_cast<float>(sampling_freq_hz * 8) + 0.875) + extra_byte;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000385 } else {
386 // Packets will have a variable size.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000387 packet_size_bytes_ = kVariableSize;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000388 }
389
390 // Set pointer to the ACM where to register the codec.
391 AudioCodingModule* my_acm = NULL;
392 switch (side) {
393 case 'A': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000394 my_acm = acm_a_.get();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000395 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000396 }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000397 case 'B': {
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000398 my_acm = acm_b_.get();
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000399 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000400 }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000401 default: {
402 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000403 }
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000404 }
405 ASSERT_TRUE(my_acm != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000406
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000407 // Get all codec parameters before registering
408 CodecInst my_codec_param;
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000409 CHECK_ERROR(AudioCodingModule::Codec(codec_name, &my_codec_param,
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000410 sampling_freq_hz, 1));
411 my_codec_param.rate = rate;
412 my_codec_param.pacsize = packet_size;
413 CHECK_ERROR(my_acm->RegisterSendCodec(my_codec_param));
niklase@google.com470e71d2011-07-07 08:21:25 +0000414}
415
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000416void TestAllCodecs::Run(TestPack* channel) {
417 AudioFrame audio_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +0000418
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000419 int32_t out_freq_hz = outfile_b_.SamplingFrequency();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000420 size_t receive_size;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000421 uint32_t timestamp_diff;
422 channel->reset_payload_size();
423 int error_count = 0;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000424 int counter = 0;
Henrik Lundin4d682082015-12-10 16:24:39 +0100425 // Set test length to 500 ms (50 blocks of 10 ms each).
426 infile_a_.SetNum10MsBlocksToRead(50);
427 // Fast-forward 1 second (100 blocks) since the file starts with silence.
428 infile_a_.FastForward(100);
429
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000430 while (!infile_a_.EndOfFile()) {
431 // Add 10 msec to ACM.
432 infile_a_.Read10MsData(audio_frame);
433 CHECK_ERROR(acm_a_->Add10MsData(audio_frame));
niklase@google.com470e71d2011-07-07 08:21:25 +0000434
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000435 // Verify that the received packet size matches the settings.
436 receive_size = channel->payload_size();
437 if (receive_size) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000438 if ((receive_size != packet_size_bytes_) &&
439 (packet_size_bytes_ != kVariableSize)) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000440 error_count++;
441 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000442
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000443 // Verify that the timestamp is updated with expected length. The counter
444 // is used to avoid problems when switching codec or frame size in the
445 // test.
446 timestamp_diff = channel->timestamp_diff();
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000447 if ((counter > 10) &&
448 (static_cast<int>(timestamp_diff) != packet_size_samples_) &&
449 (packet_size_samples_ > -1))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000450 error_count++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000451 }
452
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000453 // Run received side of ACM.
henrik.lundind4ccb002016-05-17 12:21:55 -0700454 bool muted;
455 CHECK_ERROR(acm_b_->PlayoutData10Ms(out_freq_hz, &audio_frame, &muted));
456 ASSERT_FALSE(muted);
niklase@google.com470e71d2011-07-07 08:21:25 +0000457
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000458 // Write output speech to file.
yujo36b1a5f2017-06-12 12:45:32 -0700459 outfile_b_.Write10MsData(audio_frame.data(),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000460 audio_frame.samples_per_channel_);
461
462 // Update loop counter
463 counter++;
464 }
465
466 EXPECT_EQ(0, error_count);
467
468 if (infile_a_.EndOfFile()) {
469 infile_a_.Rewind();
470 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000471}
472
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000473void TestAllCodecs::OpenOutFile(int test_number) {
474 std::string filename = webrtc::test::OutputPath();
475 std::ostringstream test_number_str;
476 test_number_str << test_number;
477 filename += "testallcodecs_out_";
478 filename += test_number_str.str();
479 filename += ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000480 outfile_b_.Open(filename, 32000, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000481}
482
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000483void TestAllCodecs::DisplaySendReceiveCodec() {
484 CodecInst my_codec_param;
kwiberg1fd4a4a2015-11-03 11:20:50 -0800485 printf("%s -> ", acm_a_->SendCodec()->plname);
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000486 acm_b_->ReceiveCodec(&my_codec_param);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000487 printf("%s\n", my_codec_param.plname);
niklase@google.com470e71d2011-07-07 08:21:25 +0000488}
489
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000490} // namespace webrtc