blob: 41c4a3af1141e8840db68115e8fed32e080a3aea [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
tina.legrand@webrtc.orgdf697752012-02-08 10:22:212 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:253 *
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 04:47:3111#include "modules/audio_coding/test/TestAllCodecs.h"
niklase@google.com470e71d2011-07-07 08:21:2512
pkasting@chromium.org4591fbd2014-11-20 22:28:1413#include <cstdio>
14#include <limits>
tina.legrand@webrtc.org5e7ca602012-06-12 07:16:2415#include <string>
kjellander@webrtc.org5490c712011-12-21 13:34:1816
Fredrik Solenberg657b2962018-12-05 09:30:2517#include "absl/strings/match.h"
Karl Wiberg5817d3d2018-04-06 08:06:4218#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Karl Wiberg133cff02018-07-06 13:40:1419#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Danil Chapovalov1030eaa2024-06-18 09:20:4020#include "api/environment/environment_factory.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3121#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
Fredrik Solenberg657b2962018-12-05 09:30:2522#include "modules/include/module_common_types.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 17:11:0024#include "rtc_base/string_encode.h"
Jonas Olsson366a50c2018-09-06 11:41:3025#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3126#include "test/gtest.h"
Steve Anton10542f22019-01-11 17:11:0027#include "test/testsupport/file_utils.h"
niklase@google.com470e71d2011-07-07 08:21:2528
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:5229// Description of the test:
30// In this test we set up a one-way communication channel from a participant
31// called "a" to a participant called "b".
32// a -> channel_a_to_b -> b
33//
34// The test loops through all available mono codecs, encode at "a" sends over
35// the channel, and decodes at "b".
36
Fredrik Solenberg657b2962018-12-05 09:30:2537#define CHECK_ERROR(f) \
38 do { \
39 EXPECT_GE(f, 0) << "Error Calling API"; \
40 } while (0)
41
pkasting@chromium.org4591fbd2014-11-20 22:28:1442namespace {
43const size_t kVariableSize = std::numeric_limits<size_t>::max();
44}
45
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:0446namespace webrtc {
47
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:5248// Class for simulating packet handling.
49TestPack::TestPack()
50 : receiver_acm_(NULL),
51 sequence_number_(0),
52 timestamp_diff_(0),
53 last_in_timestamp_(0),
54 total_bytes_(0),
Yves Gerey665174f2018-06-19 13:03:0555 payload_size_(0) {}
niklase@google.com470e71d2011-07-07 08:21:2556
Yves Gerey665174f2018-06-19 13:03:0557TestPack::~TestPack() {}
niklase@google.com470e71d2011-07-07 08:21:2558
Henrik Lundin84f75692023-02-01 12:07:1059void TestPack::RegisterReceiverACM(acm2::AcmReceiver* acm_receiver) {
60 receiver_acm_ = acm_receiver;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:5261 return;
niklase@google.com470e71d2011-07-07 08:21:2562}
63
Niels Möller87e2d782019-03-07 09:18:2364int32_t TestPack::SendData(AudioFrameType frame_type,
Yves Gerey665174f2018-06-19 13:03:0565 uint8_t payload_type,
66 uint32_t timestamp,
67 const uint8_t* payload_data,
Minyue Liff0e4db2020-01-23 12:45:5068 size_t payload_size,
69 int64_t absolute_capture_timestamp_ms) {
Niels Möllerafb5dbb2019-02-15 14:21:4770 RTPHeader rtp_header;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:5271 int32_t status;
niklase@google.com470e71d2011-07-07 08:21:2572
Niels Möllerafb5dbb2019-02-15 14:21:4773 rtp_header.markerBit = false;
74 rtp_header.ssrc = 0;
75 rtp_header.sequenceNumber = sequence_number_++;
76 rtp_header.payloadType = payload_type;
77 rtp_header.timestamp = timestamp;
philipel0a5fe772018-06-19 14:18:3178
Niels Möllerc936cb62019-03-19 13:10:1679 if (frame_type == AudioFrameType::kEmptyFrame) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:5280 // Skip this frame.
81 return 0;
82 }
83
84 // Only run mono for all test cases.
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:5285 memcpy(payload_data_, payload_data, payload_size);
86
Henrik Lundin84f75692023-02-01 12:07:1087 status = receiver_acm_->InsertPacket(
Lionel Koenig5889cf52024-05-27 09:22:2788 rtp_header, rtc::ArrayView<const uint8_t>(payload_data_, payload_size),
89 /*receive_time=*/Timestamp::MinusInfinity());
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:5290
91 payload_size_ = payload_size;
92 timestamp_diff_ = timestamp - last_in_timestamp_;
93 last_in_timestamp_ = timestamp;
94 total_bytes_ += payload_size;
95 return status;
niklase@google.com470e71d2011-07-07 08:21:2596}
97
pkasting@chromium.org4591fbd2014-11-20 22:28:1498size_t TestPack::payload_size() {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:5299 return payload_size_;
niklase@google.com470e71d2011-07-07 08:21:25100}
101
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52102uint32_t TestPack::timestamp_diff() {
103 return timestamp_diff_;
niklase@google.com470e71d2011-07-07 08:21:25104}
105
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52106void TestPack::reset_payload_size() {
107 payload_size_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25108}
109
Fredrik Solenberg657b2962018-12-05 09:30:25110TestAllCodecs::TestAllCodecs()
Danil Chapovalov1030eaa2024-06-18 09:20:40111 : env_(CreateEnvironment()),
112 acm_a_(AudioCodingModule::Create()),
Henrik Lundin84f75692023-02-01 12:07:10113 acm_b_(std::make_unique<acm2::AcmReceiver>(
Danil Chapovalov33582ea2024-08-05 10:40:46114 env_,
Henrik Lundin84f75692023-02-01 12:07:10115 acm2::AcmReceiver::Config(CreateBuiltinAudioDecoderFactory()))),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52116 channel_a_to_b_(NULL),
117 test_count_(0),
118 packet_size_samples_(0),
Jonas Olssona4d87372019-07-05 17:08:33119 packet_size_bytes_(0) {}
niklase@google.com470e71d2011-07-07 08:21:25120
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52121TestAllCodecs::~TestAllCodecs() {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52122 if (channel_a_to_b_ != NULL) {
123 delete channel_a_to_b_;
124 channel_a_to_b_ = NULL;
125 }
126}
niklase@google.com470e71d2011-07-07 08:21:25127
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52128void TestAllCodecs::Perform() {
Yves Gerey665174f2018-06-19 13:03:05129 const std::string file_name =
130 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
tina.legrand@webrtc.orgba468042012-08-17 10:38:28131 infile_a_.Open(file_name, 32000, "rb");
niklase@google.com470e71d2011-07-07 08:21:25132
Henrik Lundin84f75692023-02-01 12:07:10133 acm_b_->SetCodecs({{107, {"L16", 8000, 1}},
134 {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:25150
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52151 // 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:43154 channel_a_to_b_->RegisterReceiverACM(acm_b_.get());
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52155
156 // All codecs are tested for all allowed sampling frequencies, rates and
157 // packet sizes.
Mirko Bonadeibd4dd672024-06-07 08:38:55158
159// TODO(bugs.webrtc.org/345525069): Either fix/enable or remove G722.
160#if defined(__has_feature) && !__has_feature(undefined_behavior_sanitizer)
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52161 test_count_++;
162 OpenOutFile(test_count_);
163 char codec_g722[] = "G722";
Henrik Lundin84f75692023-02-01 12:07:10164 RegisterSendCodec(codec_g722, 16000, 64000, 160, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52165 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10166 RegisterSendCodec(codec_g722, 16000, 64000, 320, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52167 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10168 RegisterSendCodec(codec_g722, 16000, 64000, 480, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52169 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10170 RegisterSendCodec(codec_g722, 16000, 64000, 640, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52171 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10172 RegisterSendCodec(codec_g722, 16000, 64000, 800, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52173 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10174 RegisterSendCodec(codec_g722, 16000, 64000, 960, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52175 Run(channel_a_to_b_);
176 outfile_b_.Close();
Mirko Bonadeibd4dd672024-06-07 08:38:55177#endif
178// TODO(bugs.webrtc.org/345525069): Either fix/enable or remove iLBC.
179#if defined(__has_feature) && !__has_feature(undefined_behavior_sanitizer)
niklase@google.com470e71d2011-07-07 08:21:25180#ifdef WEBRTC_CODEC_ILBC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52181 test_count_++;
182 OpenOutFile(test_count_);
183 char codec_ilbc[] = "ILBC";
Henrik Lundin84f75692023-02-01 12:07:10184 RegisterSendCodec(codec_ilbc, 8000, 13300, 240, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52185 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10186 RegisterSendCodec(codec_ilbc, 8000, 13300, 480, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52187 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10188 RegisterSendCodec(codec_ilbc, 8000, 15200, 160, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52189 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10190 RegisterSendCodec(codec_ilbc, 8000, 15200, 320, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52191 Run(channel_a_to_b_);
192 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25193#endif
Mirko Bonadeibd4dd672024-06-07 08:38:55194#endif
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52195 test_count_++;
196 OpenOutFile(test_count_);
197 char codec_l16[] = "L16";
Henrik Lundin84f75692023-02-01 12:07:10198 RegisterSendCodec(codec_l16, 8000, 128000, 80, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52199 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10200 RegisterSendCodec(codec_l16, 8000, 128000, 160, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52201 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10202 RegisterSendCodec(codec_l16, 8000, 128000, 240, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52203 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10204 RegisterSendCodec(codec_l16, 8000, 128000, 320, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52205 Run(channel_a_to_b_);
206 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 09:30:25207
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52208 test_count_++;
209 OpenOutFile(test_count_);
Henrik Lundin84f75692023-02-01 12:07:10210 RegisterSendCodec(codec_l16, 16000, 256000, 160, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52211 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10212 RegisterSendCodec(codec_l16, 16000, 256000, 320, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52213 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10214 RegisterSendCodec(codec_l16, 16000, 256000, 480, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52215 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10216 RegisterSendCodec(codec_l16, 16000, 256000, 640, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52217 Run(channel_a_to_b_);
218 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 09:30:25219
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52220 test_count_++;
221 OpenOutFile(test_count_);
Henrik Lundin84f75692023-02-01 12:07:10222 RegisterSendCodec(codec_l16, 32000, 512000, 320, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52223 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10224 RegisterSendCodec(codec_l16, 32000, 512000, 640, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52225 Run(channel_a_to_b_);
226 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 09:30:25227
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52228 test_count_++;
229 OpenOutFile(test_count_);
230 char codec_pcma[] = "PCMA";
Henrik Lundin84f75692023-02-01 12:07:10231 RegisterSendCodec(codec_pcma, 8000, 64000, 80, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52232 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10233 RegisterSendCodec(codec_pcma, 8000, 64000, 160, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52234 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10235 RegisterSendCodec(codec_pcma, 8000, 64000, 240, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52236 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10237 RegisterSendCodec(codec_pcma, 8000, 64000, 320, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52238 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10239 RegisterSendCodec(codec_pcma, 8000, 64000, 400, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52240 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10241 RegisterSendCodec(codec_pcma, 8000, 64000, 480, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52242 Run(channel_a_to_b_);
Fredrik Solenberg657b2962018-12-05 09:30:25243
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52244 char codec_pcmu[] = "PCMU";
Henrik Lundin84f75692023-02-01 12:07:10245 RegisterSendCodec(codec_pcmu, 8000, 64000, 80, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52246 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10247 RegisterSendCodec(codec_pcmu, 8000, 64000, 160, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52248 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10249 RegisterSendCodec(codec_pcmu, 8000, 64000, 240, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52250 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10251 RegisterSendCodec(codec_pcmu, 8000, 64000, 320, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52252 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10253 RegisterSendCodec(codec_pcmu, 8000, 64000, 400, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52254 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10255 RegisterSendCodec(codec_pcmu, 8000, 64000, 480, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52256 Run(channel_a_to_b_);
257 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52258#ifdef WEBRTC_CODEC_OPUS
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52259 test_count_++;
260 OpenOutFile(test_count_);
261 char codec_opus[] = "OPUS";
Henrik Lundin84f75692023-02-01 12:07:10262 RegisterSendCodec(codec_opus, 48000, 6000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52263 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10264 RegisterSendCodec(codec_opus, 48000, 20000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52265 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10266 RegisterSendCodec(codec_opus, 48000, 32000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52267 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10268 RegisterSendCodec(codec_opus, 48000, 48000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52269 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10270 RegisterSendCodec(codec_opus, 48000, 64000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52271 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10272 RegisterSendCodec(codec_opus, 48000, 96000, 480 * 6, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52273 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10274 RegisterSendCodec(codec_opus, 48000, 500000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52275 Run(channel_a_to_b_);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29276 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52277#endif
niklase@google.com470e71d2011-07-07 08:21:25278}
279
280// Register Codec to use in the test
281//
Henrik Lundin84f75692023-02-01 12:07:10282// Input: codec_name - name to use when register the codec
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52283// sampling_freq_hz - sampling frequency in Herz
284// rate - bitrate in bytes
285// packet_size - packet size in samples
286// extra_byte - if extra bytes needed compared to the bitrate
niklase@google.com470e71d2011-07-07 08:21:25287// used when registering, can be an internal header
pkasting@chromium.org4591fbd2014-11-20 22:28:14288// set to kVariableSize if the codec is a variable
289// rate codec
Henrik Lundin84f75692023-02-01 12:07:10290void TestAllCodecs::RegisterSendCodec(char* codec_name,
Yves Gerey665174f2018-06-19 13:03:05291 int32_t sampling_freq_hz,
292 int rate,
293 int packet_size,
294 size_t extra_byte) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52295 // Store packet-size in samples, used to validate the received packet.
296 // If G.722, store half the size to compensate for the timestamp bug in the
297 // RFC for G.722.
Fredrik Solenberg657b2962018-12-05 09:30:25298 int clockrate_hz = sampling_freq_hz;
299 size_t num_channels = 1;
300 if (absl::EqualsIgnoreCase(codec_name, "G722")) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52301 packet_size_samples_ = packet_size / 2;
Fredrik Solenberg657b2962018-12-05 09:30:25302 clockrate_hz = sampling_freq_hz / 2;
Fredrik Solenberg657b2962018-12-05 09:30:25303 } else if (absl::EqualsIgnoreCase(codec_name, "OPUS")) {
304 packet_size_samples_ = packet_size;
305 num_channels = 2;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52306 } else {
307 packet_size_samples_ = packet_size;
308 }
309
310 // Store the expected packet size in bytes, used to validate the received
henrike@webrtc.org6ac22e62014-08-11 21:06:30311 // packet. If variable rate codec (extra_byte == -1), set to -1.
pkasting@chromium.org4591fbd2014-11-20 22:28:14312 if (extra_byte != kVariableSize) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52313 // Add 0.875 to always round up to a whole byte
Yves Gerey665174f2018-06-19 13:03:05314 packet_size_bytes_ =
315 static_cast<size_t>(static_cast<float>(packet_size * rate) /
316 static_cast<float>(sampling_freq_hz * 8) +
317 0.875) +
318 extra_byte;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52319 } else {
320 // Packets will have a variable size.
pkasting@chromium.org4591fbd2014-11-20 22:28:14321 packet_size_bytes_ = kVariableSize;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52322 }
323
Karl Wiberg133cff02018-07-06 13:40:14324 auto factory = CreateBuiltinAudioEncoderFactory();
Jonas Olssona4d87372019-07-05 17:08:33325 SdpAudioFormat format = {codec_name, clockrate_hz, num_channels};
Karl Wiberg133cff02018-07-06 13:40:14326 format.parameters["ptime"] = rtc::ToString(rtc::CheckedDivExact(
327 packet_size, rtc::CheckedDivExact(sampling_freq_hz, 1000)));
Danil Chapovalov1030eaa2024-06-18 09:20:40328 acm_a_->SetEncoder(factory->Create(env_, format, {.payload_type = 17}));
niklase@google.com470e71d2011-07-07 08:21:25329}
330
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52331void TestAllCodecs::Run(TestPack* channel) {
332 AudioFrame audio_frame;
niklase@google.com470e71d2011-07-07 08:21:25333
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52334 int32_t out_freq_hz = outfile_b_.SamplingFrequency();
pkasting@chromium.org4591fbd2014-11-20 22:28:14335 size_t receive_size;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52336 uint32_t timestamp_diff;
337 channel->reset_payload_size();
338 int error_count = 0;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52339 int counter = 0;
Henrik Lundin4d682082015-12-10 15:24:39340 // Set test length to 500 ms (50 blocks of 10 ms each).
341 infile_a_.SetNum10MsBlocksToRead(50);
342 // Fast-forward 1 second (100 blocks) since the file starts with silence.
343 infile_a_.FastForward(100);
344
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52345 while (!infile_a_.EndOfFile()) {
346 // Add 10 msec to ACM.
347 infile_a_.Read10MsData(audio_frame);
348 CHECK_ERROR(acm_a_->Add10MsData(audio_frame));
niklase@google.com470e71d2011-07-07 08:21:25349
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52350 // Verify that the received packet size matches the settings.
351 receive_size = channel->payload_size();
352 if (receive_size) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14353 if ((receive_size != packet_size_bytes_) &&
354 (packet_size_bytes_ != kVariableSize)) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52355 error_count++;
356 }
niklase@google.com470e71d2011-07-07 08:21:25357
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52358 // Verify that the timestamp is updated with expected length. The counter
359 // is used to avoid problems when switching codec or frame size in the
360 // test.
361 timestamp_diff = channel->timestamp_diff();
henrike@webrtc.org6ac22e62014-08-11 21:06:30362 if ((counter > 10) &&
363 (static_cast<int>(timestamp_diff) != packet_size_samples_) &&
364 (packet_size_samples_ > -1))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52365 error_count++;
niklase@google.com470e71d2011-07-07 08:21:25366 }
367
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52368 // Run received side of ACM.
henrik.lundind4ccb002016-05-17 19:21:55369 bool muted;
Henrik Lundin84f75692023-02-01 12:07:10370 CHECK_ERROR(acm_b_->GetAudio(out_freq_hz, &audio_frame, &muted));
henrik.lundind4ccb002016-05-17 19:21:55371 ASSERT_FALSE(muted);
niklase@google.com470e71d2011-07-07 08:21:25372
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52373 // Write output speech to file.
yujo36b1a5f2017-06-12 19:45:32374 outfile_b_.Write10MsData(audio_frame.data(),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52375 audio_frame.samples_per_channel_);
376
377 // Update loop counter
378 counter++;
379 }
380
381 EXPECT_EQ(0, error_count);
382
383 if (infile_a_.EndOfFile()) {
384 infile_a_.Rewind();
385 }
niklase@google.com470e71d2011-07-07 08:21:25386}
387
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52388void TestAllCodecs::OpenOutFile(int test_number) {
389 std::string filename = webrtc::test::OutputPath();
Jonas Olsson366a50c2018-09-06 11:41:30390 rtc::StringBuilder test_number_str;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52391 test_number_str << test_number;
392 filename += "testallcodecs_out_";
393 filename += test_number_str.str();
394 filename += ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28395 outfile_b_.Open(filename, 32000, "wb");
niklase@google.com470e71d2011-07-07 08:21:25396}
397
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52398} // namespace webrtc