blob: dd51760fd1191dbba1b289713e397d55d3aba420 [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
Henrik Lundin84f75692023-02-01 12:07:10 +000058void TestPack::RegisterReceiverACM(acm2::AcmReceiver* acm_receiver) {
59 receiver_acm_ = acm_receiver;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +000060 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
Henrik Lundin84f75692023-02-01 12:07:10 +000086 status = receiver_acm_->InsertPacket(
87 rtp_header, rtc::ArrayView<const uint8_t>(payload_data_, payload_size));
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()
Henrik Lundin84f75692023-02-01 12:07:10 +0000109 : acm_a_(AudioCodingModule::Create()),
110 acm_b_(std::make_unique<acm2::AcmReceiver>(
111 acm2::AcmReceiver::Config(CreateBuiltinAudioDecoderFactory()))),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000112 channel_a_to_b_(NULL),
113 test_count_(0),
114 packet_size_samples_(0),
Jonas Olssona4d87372019-07-05 19:08:33 +0200115 packet_size_bytes_(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000117TestAllCodecs::~TestAllCodecs() {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000118 if (channel_a_to_b_ != NULL) {
119 delete channel_a_to_b_;
120 channel_a_to_b_ = NULL;
121 }
122}
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000124void TestAllCodecs::Perform() {
Yves Gerey665174f2018-06-19 15:03:05 +0200125 const std::string file_name =
126 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000127 infile_a_.Open(file_name, 32000, "rb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000128
Henrik Lundin84f75692023-02-01 12:07:10 +0000129 acm_b_->SetCodecs({{107, {"L16", 8000, 1}},
130 {108, {"L16", 16000, 1}},
131 {109, {"L16", 32000, 1}},
132 {111, {"L16", 8000, 2}},
133 {112, {"L16", 16000, 2}},
134 {113, {"L16", 32000, 2}},
135 {0, {"PCMU", 8000, 1}},
136 {110, {"PCMU", 8000, 2}},
137 {8, {"PCMA", 8000, 1}},
138 {118, {"PCMA", 8000, 2}},
139 {102, {"ILBC", 8000, 1}},
140 {9, {"G722", 8000, 1}},
141 {119, {"G722", 8000, 2}},
142 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}},
143 {13, {"CN", 8000, 1}},
144 {98, {"CN", 16000, 1}},
145 {99, {"CN", 32000, 1}}});
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.
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000154 test_count_++;
155 OpenOutFile(test_count_);
156 char codec_g722[] = "G722";
Henrik Lundin84f75692023-02-01 12:07:10 +0000157 RegisterSendCodec(codec_g722, 16000, 64000, 160, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000158 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000159 RegisterSendCodec(codec_g722, 16000, 64000, 320, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000160 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000161 RegisterSendCodec(codec_g722, 16000, 64000, 480, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000162 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000163 RegisterSendCodec(codec_g722, 16000, 64000, 640, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000164 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000165 RegisterSendCodec(codec_g722, 16000, 64000, 800, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000166 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000167 RegisterSendCodec(codec_g722, 16000, 64000, 960, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000168 Run(channel_a_to_b_);
169 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000170#ifdef WEBRTC_CODEC_ILBC
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000171 test_count_++;
172 OpenOutFile(test_count_);
173 char codec_ilbc[] = "ILBC";
Henrik Lundin84f75692023-02-01 12:07:10 +0000174 RegisterSendCodec(codec_ilbc, 8000, 13300, 240, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000175 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000176 RegisterSendCodec(codec_ilbc, 8000, 13300, 480, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000177 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000178 RegisterSendCodec(codec_ilbc, 8000, 15200, 160, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000179 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000180 RegisterSendCodec(codec_ilbc, 8000, 15200, 320, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000181 Run(channel_a_to_b_);
182 outfile_b_.Close();
niklase@google.com470e71d2011-07-07 08:21:25 +0000183#endif
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000184 test_count_++;
185 OpenOutFile(test_count_);
186 char codec_l16[] = "L16";
Henrik Lundin84f75692023-02-01 12:07:10 +0000187 RegisterSendCodec(codec_l16, 8000, 128000, 80, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000188 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000189 RegisterSendCodec(codec_l16, 8000, 128000, 160, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000190 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000191 RegisterSendCodec(codec_l16, 8000, 128000, 240, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000192 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000193 RegisterSendCodec(codec_l16, 8000, 128000, 320, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000194 Run(channel_a_to_b_);
195 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100196
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000197 test_count_++;
198 OpenOutFile(test_count_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000199 RegisterSendCodec(codec_l16, 16000, 256000, 160, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000200 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000201 RegisterSendCodec(codec_l16, 16000, 256000, 320, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000202 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000203 RegisterSendCodec(codec_l16, 16000, 256000, 480, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000204 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000205 RegisterSendCodec(codec_l16, 16000, 256000, 640, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000206 Run(channel_a_to_b_);
207 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100208
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000209 test_count_++;
210 OpenOutFile(test_count_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000211 RegisterSendCodec(codec_l16, 32000, 512000, 320, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000212 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000213 RegisterSendCodec(codec_l16, 32000, 512000, 640, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000214 Run(channel_a_to_b_);
215 outfile_b_.Close();
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100216
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000217 test_count_++;
218 OpenOutFile(test_count_);
219 char codec_pcma[] = "PCMA";
Henrik Lundin84f75692023-02-01 12:07:10 +0000220 RegisterSendCodec(codec_pcma, 8000, 64000, 80, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000221 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000222 RegisterSendCodec(codec_pcma, 8000, 64000, 160, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000223 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000224 RegisterSendCodec(codec_pcma, 8000, 64000, 240, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000225 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000226 RegisterSendCodec(codec_pcma, 8000, 64000, 320, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000227 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000228 RegisterSendCodec(codec_pcma, 8000, 64000, 400, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000229 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000230 RegisterSendCodec(codec_pcma, 8000, 64000, 480, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000231 Run(channel_a_to_b_);
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100232
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000233 char codec_pcmu[] = "PCMU";
Henrik Lundin84f75692023-02-01 12:07:10 +0000234 RegisterSendCodec(codec_pcmu, 8000, 64000, 80, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000235 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000236 RegisterSendCodec(codec_pcmu, 8000, 64000, 160, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000237 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000238 RegisterSendCodec(codec_pcmu, 8000, 64000, 240, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000239 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000240 RegisterSendCodec(codec_pcmu, 8000, 64000, 320, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000241 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000242 RegisterSendCodec(codec_pcmu, 8000, 64000, 400, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000243 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000244 RegisterSendCodec(codec_pcmu, 8000, 64000, 480, 0);
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000245 Run(channel_a_to_b_);
246 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000247#ifdef WEBRTC_CODEC_OPUS
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000248 test_count_++;
249 OpenOutFile(test_count_);
250 char codec_opus[] = "OPUS";
Henrik Lundin84f75692023-02-01 12:07:10 +0000251 RegisterSendCodec(codec_opus, 48000, 6000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000252 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000253 RegisterSendCodec(codec_opus, 48000, 20000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000254 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000255 RegisterSendCodec(codec_opus, 48000, 32000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000256 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000257 RegisterSendCodec(codec_opus, 48000, 48000, 480, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000258 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000259 RegisterSendCodec(codec_opus, 48000, 64000, 480 * 4, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000260 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000261 RegisterSendCodec(codec_opus, 48000, 96000, 480 * 6, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000262 Run(channel_a_to_b_);
Henrik Lundin84f75692023-02-01 12:07:10 +0000263 RegisterSendCodec(codec_opus, 48000, 500000, 480 * 2, kVariableSize);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000264 Run(channel_a_to_b_);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000265 outfile_b_.Close();
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000266#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000267}
268
269// Register Codec to use in the test
270//
Henrik Lundin84f75692023-02-01 12:07:10 +0000271// Input: codec_name - name to use when register the codec
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000272// sampling_freq_hz - sampling frequency in Herz
273// rate - bitrate in bytes
274// packet_size - packet size in samples
275// extra_byte - if extra bytes needed compared to the bitrate
niklase@google.com470e71d2011-07-07 08:21:25 +0000276// used when registering, can be an internal header
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000277// set to kVariableSize if the codec is a variable
278// rate codec
Henrik Lundin84f75692023-02-01 12:07:10 +0000279void TestAllCodecs::RegisterSendCodec(char* codec_name,
Yves Gerey665174f2018-06-19 15:03:05 +0200280 int32_t sampling_freq_hz,
281 int rate,
282 int packet_size,
283 size_t extra_byte) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000284 // Store packet-size in samples, used to validate the received packet.
285 // If G.722, store half the size to compensate for the timestamp bug in the
286 // RFC for G.722.
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100287 int clockrate_hz = sampling_freq_hz;
288 size_t num_channels = 1;
289 if (absl::EqualsIgnoreCase(codec_name, "G722")) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000290 packet_size_samples_ = packet_size / 2;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100291 clockrate_hz = sampling_freq_hz / 2;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100292 } else if (absl::EqualsIgnoreCase(codec_name, "OPUS")) {
293 packet_size_samples_ = packet_size;
294 num_channels = 2;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000295 } else {
296 packet_size_samples_ = packet_size;
297 }
298
299 // Store the expected packet size in bytes, used to validate the received
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000300 // packet. If variable rate codec (extra_byte == -1), set to -1.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000301 if (extra_byte != kVariableSize) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000302 // Add 0.875 to always round up to a whole byte
Yves Gerey665174f2018-06-19 15:03:05 +0200303 packet_size_bytes_ =
304 static_cast<size_t>(static_cast<float>(packet_size * rate) /
305 static_cast<float>(sampling_freq_hz * 8) +
306 0.875) +
307 extra_byte;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000308 } else {
309 // Packets will have a variable size.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000310 packet_size_bytes_ = kVariableSize;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000311 }
312
Karl Wiberg133cff02018-07-06 15:40:14 +0200313 auto factory = CreateBuiltinAudioEncoderFactory();
314 constexpr int payload_type = 17;
Jonas Olssona4d87372019-07-05 19:08:33 +0200315 SdpAudioFormat format = {codec_name, clockrate_hz, num_channels};
Karl Wiberg133cff02018-07-06 15:40:14 +0200316 format.parameters["ptime"] = rtc::ToString(rtc::CheckedDivExact(
317 packet_size, rtc::CheckedDivExact(sampling_freq_hz, 1000)));
Henrik Lundin84f75692023-02-01 12:07:10 +0000318 acm_a_->SetEncoder(
Karl Wiberg133cff02018-07-06 15:40:14 +0200319 factory->MakeAudioEncoder(payload_type, format, absl::nullopt));
niklase@google.com470e71d2011-07-07 08:21:25 +0000320}
321
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000322void TestAllCodecs::Run(TestPack* channel) {
323 AudioFrame audio_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +0000324
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000325 int32_t out_freq_hz = outfile_b_.SamplingFrequency();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000326 size_t receive_size;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000327 uint32_t timestamp_diff;
328 channel->reset_payload_size();
329 int error_count = 0;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000330 int counter = 0;
Henrik Lundin4d682082015-12-10 16:24:39 +0100331 // Set test length to 500 ms (50 blocks of 10 ms each).
332 infile_a_.SetNum10MsBlocksToRead(50);
333 // Fast-forward 1 second (100 blocks) since the file starts with silence.
334 infile_a_.FastForward(100);
335
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000336 while (!infile_a_.EndOfFile()) {
337 // Add 10 msec to ACM.
338 infile_a_.Read10MsData(audio_frame);
339 CHECK_ERROR(acm_a_->Add10MsData(audio_frame));
niklase@google.com470e71d2011-07-07 08:21:25 +0000340
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000341 // Verify that the received packet size matches the settings.
342 receive_size = channel->payload_size();
343 if (receive_size) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000344 if ((receive_size != packet_size_bytes_) &&
345 (packet_size_bytes_ != kVariableSize)) {
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000346 error_count++;
347 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000348
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000349 // Verify that the timestamp is updated with expected length. The counter
350 // is used to avoid problems when switching codec or frame size in the
351 // test.
352 timestamp_diff = channel->timestamp_diff();
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000353 if ((counter > 10) &&
354 (static_cast<int>(timestamp_diff) != packet_size_samples_) &&
355 (packet_size_samples_ > -1))
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000356 error_count++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000357 }
358
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000359 // Run received side of ACM.
henrik.lundind4ccb002016-05-17 12:21:55 -0700360 bool muted;
Henrik Lundin84f75692023-02-01 12:07:10 +0000361 CHECK_ERROR(acm_b_->GetAudio(out_freq_hz, &audio_frame, &muted));
henrik.lundind4ccb002016-05-17 12:21:55 -0700362 ASSERT_FALSE(muted);
niklase@google.com470e71d2011-07-07 08:21:25 +0000363
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000364 // Write output speech to file.
yujo36b1a5f2017-06-12 12:45:32 -0700365 outfile_b_.Write10MsData(audio_frame.data(),
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000366 audio_frame.samples_per_channel_);
367
368 // Update loop counter
369 counter++;
370 }
371
372 EXPECT_EQ(0, error_count);
373
374 if (infile_a_.EndOfFile()) {
375 infile_a_.Rewind();
376 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000377}
378
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000379void TestAllCodecs::OpenOutFile(int test_number) {
380 std::string filename = webrtc::test::OutputPath();
Jonas Olsson366a50c2018-09-06 13:41:30 +0200381 rtc::StringBuilder test_number_str;
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000382 test_number_str << test_number;
383 filename += "testallcodecs_out_";
384 filename += test_number_str.str();
385 filename += ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000386 outfile_b_.Open(filename, 32000, "wb");
niklase@google.com470e71d2011-07-07 08:21:25 +0000387}
388
tina.legrand@webrtc.org50d5ca52012-06-18 13:35:52 +0000389} // namespace webrtc