blob: 9f9c4aa74ce97d25fefbbbeb751d6e8f729fa8cf [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
tina.legrand@webrtc.orgae1c4542012-03-12 08:41:30 +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/EncodeDecodeTest.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000013#include <stdio.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014#include <stdlib.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Yves Gerey665174f2018-06-19 15:03:05 +020016#include <memory>
tina.legrand@webrtc.org5e7ca602012-06-12 07:16:24 +000017
Ali Tofigh714e3cb2022-07-20 12:53:07 +020018#include "absl/strings/string_view.h"
Karl Wiberg5817d3d2018-04-06 10:06:42 +020019#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Karl Wiberg658a5522018-08-15 15:20:49 +020020#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_coding/include/audio_coding_module.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020022#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "test/testsupport/file_utils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000025
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000026namespace webrtc {
27
Per Åhgrend82a02c2020-03-12 11:53:30 +010028namespace {
29// Buffer size for stereo 48 kHz audio.
30constexpr size_t kWebRtc10MsPcmAudio = 960;
31
32} // namespace
33
Jonas Olssona4d87372019-07-05 19:08:33 +020034TestPacketization::TestPacketization(RTPStream* rtpStream, uint16_t frequency)
35 : _rtpStream(rtpStream), _frequency(frequency), _seqNo(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000036
Jonas Olssona4d87372019-07-05 19:08:33 +020037TestPacketization::~TestPacketization() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000038
Niels Möllerc35b6e62019-04-25 16:31:18 +020039int32_t TestPacketization::SendData(const AudioFrameType /* frameType */,
40 const uint8_t payloadType,
41 const uint32_t timeStamp,
42 const uint8_t* payloadData,
Minyue Liff0e4db2020-01-23 13:45:50 +010043 const size_t payloadSize,
44 int64_t absolute_capture_timestamp_ms) {
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000045 _rtpStream->Write(payloadType, timeStamp, _seqNo++, payloadData, payloadSize,
46 _frequency);
47 return 1;
48}
niklase@google.com470e71d2011-07-07 08:21:25 +000049
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000050Sender::Sender()
Jonas Olssona4d87372019-07-05 19:08:33 +020051 : _acm(NULL), _pcmFile(), _audioFrame(), _packetization(NULL) {}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000052
Jonas Olssona4d87372019-07-05 19:08:33 +020053void Sender::Setup(AudioCodingModule* acm,
54 RTPStream* rtpStream,
Ali Tofigh714e3cb2022-07-20 12:53:07 +020055 absl::string_view in_file_name,
Jonas Olssona4d87372019-07-05 19:08:33 +020056 int in_sample_rate,
57 int payload_type,
58 SdpAudioFormat format) {
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000059 // Open input file
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000060 const std::string file_name = webrtc::test::ResourcePath(in_file_name, "pcm");
Fredrik Solenberg657b2962018-12-05 10:30:25 +010061 _pcmFile.Open(file_name, in_sample_rate, "rb");
62 if (format.num_channels == 2) {
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000063 _pcmFile.ReadStereo(true);
64 }
Henrik Lundin4d682082015-12-10 16:24:39 +010065 // Set test length to 500 ms (50 blocks of 10 ms each).
66 _pcmFile.SetNum10MsBlocksToRead(50);
67 // Fast-forward 1 second (100 blocks) since the file starts with silence.
68 _pcmFile.FastForward(100);
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000069
Karl Wiberg658a5522018-08-15 15:20:49 +020070 acm->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
Fredrik Solenberg657b2962018-12-05 10:30:25 +010071 payload_type, format, absl::nullopt));
72 _packetization = new TestPacketization(rtpStream, format.clockrate_hz);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000073 EXPECT_EQ(0, acm->RegisterTransportCallback(_packetization));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000074
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000075 _acm = acm;
76}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000077
78void Sender::Teardown() {
79 _pcmFile.Close();
80 delete _packetization;
niklase@google.com470e71d2011-07-07 08:21:25 +000081}
82
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000083bool Sender::Add10MsData() {
84 if (!_pcmFile.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000085 EXPECT_GT(_pcmFile.Read10MsData(_audioFrame), 0);
pbos@webrtc.org0946a562013-04-09 00:28:06 +000086 int32_t ok = _acm->Add10MsData(_audioFrame);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +000087 EXPECT_GE(ok, 0);
88 return ok >= 0 ? true : false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000089 }
90 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +000091}
92
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000093void Sender::Run() {
94 while (true) {
95 if (!Add10MsData()) {
96 break;
niklase@google.com470e71d2011-07-07 08:21:25 +000097 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000098 }
99}
100
101Receiver::Receiver()
Per Åhgrend82a02c2020-03-12 11:53:30 +0100102 : _playoutLengthSmpls(kWebRtc10MsPcmAudio),
Jonas Olssona4d87372019-07-05 19:08:33 +0200103 _payloadSizeBytes(MAX_INCOMING_PAYLOAD) {}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000104
Jonas Olssona4d87372019-07-05 19:08:33 +0200105void Receiver::Setup(AudioCodingModule* acm,
106 RTPStream* rtpStream,
Ali Tofigh714e3cb2022-07-20 12:53:07 +0200107 absl::string_view out_file_name,
Jonas Olssona4d87372019-07-05 19:08:33 +0200108 size_t channels,
109 int file_num) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000110 EXPECT_EQ(0, acm->InitializeReceiver());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000111
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100112 if (channels == 1) {
Alessio Bazzica17887eb2022-11-11 16:52:46 +0100113 acm->SetReceiveCodecs({{107, {"L16", 8000, 1}},
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100114 {108, {"L16", 16000, 1}},
115 {109, {"L16", 32000, 1}},
116 {0, {"PCMU", 8000, 1}},
117 {8, {"PCMA", 8000, 1}},
118 {102, {"ILBC", 8000, 1}},
119 {9, {"G722", 8000, 1}},
120 {120, {"OPUS", 48000, 2}},
121 {13, {"CN", 8000, 1}},
122 {98, {"CN", 16000, 1}},
123 {99, {"CN", 32000, 1}}});
124 } else {
125 ASSERT_EQ(channels, 2u);
126 acm->SetReceiveCodecs({{111, {"L16", 8000, 2}},
127 {112, {"L16", 16000, 2}},
128 {113, {"L16", 32000, 2}},
129 {110, {"PCMU", 8000, 2}},
130 {118, {"PCMA", 8000, 2}},
131 {119, {"G722", 8000, 2}},
132 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}});
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000133 }
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000134
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000135 int playSampFreq;
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000136 std::string file_name;
Jonas Olsson366a50c2018-09-06 13:41:30 +0200137 rtc::StringBuilder file_stream;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100138 file_stream << webrtc::test::OutputPath() << out_file_name << file_num
139 << ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000140 file_name = file_stream.str();
141 _rtpStream = rtpStream;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000142
Karl Wiberg88aee282018-06-14 13:12:05 +0200143 playSampFreq = 32000;
144 _pcmFile.Open(file_name, 32000, "wb+");
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000145
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000146 _realPayloadSizeBytes = 0;
Per Åhgrend82a02c2020-03-12 11:53:30 +0100147 _playoutBuffer = new int16_t[kWebRtc10MsPcmAudio];
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000148 _frequency = playSampFreq;
149 _acm = acm;
150 _firstTime = true;
151}
152
153void Receiver::Teardown() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000154 delete[] _playoutBuffer;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000155 _pcmFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000156}
157
158bool Receiver::IncomingPacket() {
159 if (!_rtpStream->EndOfFile()) {
160 if (_firstTime) {
161 _firstTime = false;
Niels Möllerbf474952019-02-18 12:00:06 +0100162 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000163 _payloadSizeBytes, &_nextTime);
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000164 if (_realPayloadSizeBytes == 0) {
165 if (_rtpStream->EndOfFile()) {
166 _firstTime = true;
167 return true;
168 } else {
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000169 return false;
170 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000171 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000172 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000173
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000174 EXPECT_EQ(0, _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes,
Niels Möllerbf474952019-02-18 12:00:06 +0100175 _rtpHeader));
176 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000177 _payloadSizeBytes, &_nextTime);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000178 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
179 _firstTime = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000180 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000181 }
182 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000183}
184
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000185bool Receiver::PlayoutData() {
186 AudioFrame audioFrame;
henrik.lundind4ccb002016-05-17 12:21:55 -0700187 bool muted;
188 int32_t ok = _acm->PlayoutData10Ms(_frequency, &audioFrame, &muted);
189 if (muted) {
190 ADD_FAILURE();
191 return false;
192 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000193 EXPECT_EQ(0, ok);
Jonas Olssona4d87372019-07-05 19:08:33 +0200194 if (ok < 0) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000195 return false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000196 }
197 if (_playoutLengthSmpls == 0) {
198 return false;
199 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200200 _pcmFile.Write10MsData(audioFrame.data(), audioFrame.samples_per_channel_ *
201 audioFrame.num_channels_);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000202 return true;
203}
204
205void Receiver::Run() {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000206 uint8_t counter500Ms = 50;
207 uint32_t clock = 0;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000208
209 while (counter500Ms > 0) {
210 if (clock == 0 || clock >= _nextTime) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000211 EXPECT_TRUE(IncomingPacket());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000212 if (clock == 0) {
213 clock = _nextTime;
214 }
215 }
216 if ((clock % 10) == 0) {
217 if (!PlayoutData()) {
218 clock++;
219 continue;
220 }
221 }
222 if (_rtpStream->EndOfFile()) {
223 counter500Ms--;
224 }
225 clock++;
226 }
227}
228
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100229EncodeDecodeTest::EncodeDecodeTest() = default;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000230
231void EncodeDecodeTest::Perform() {
Jonas Olssona4d87372019-07-05 19:08:33 +0200232 const std::map<int, SdpAudioFormat> send_codecs = {
Jonas Olssona4d87372019-07-05 19:08:33 +0200233 {107, {"L16", 8000, 1}}, {108, {"L16", 16000, 1}},
234 {109, {"L16", 32000, 1}}, {0, {"PCMU", 8000, 1}},
235 {8, {"PCMA", 8000, 1}},
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100236#ifdef WEBRTC_CODEC_ILBC
Jonas Olssona4d87372019-07-05 19:08:33 +0200237 {102, {"ILBC", 8000, 1}},
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100238#endif
Jonas Olssona4d87372019-07-05 19:08:33 +0200239 {9, {"G722", 8000, 1}}};
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100240 int file_num = 0;
241 for (const auto& send_codec : send_codecs) {
242 RTPFile rtpFile;
243 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
244 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000245
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100246 std::string fileName = webrtc::test::TempFilename(
247 webrtc::test::OutputPath(), "encode_decode_rtp");
248 rtpFile.Open(fileName.c_str(), "wb+");
249 rtpFile.WriteHeader();
250 Sender sender;
251 sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000,
252 send_codec.first, send_codec.second);
253 sender.Run();
254 sender.Teardown();
255 rtpFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000256
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100257 rtpFile.Open(fileName.c_str(), "rb");
258 rtpFile.ReadHeader();
259 Receiver receiver;
260 receiver.Setup(acm.get(), &rtpFile, "encodeDecode_out", 1, file_num);
261 receiver.Run();
262 receiver.Teardown();
263 rtpFile.Close();
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000264
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100265 file_num++;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000266 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000267}
268
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000269} // namespace webrtc