blob: 8d4bcce8df63322918d2922ed876cc132495bf3e [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) {
113 acm->SetReceiveCodecs({{103, {"ISAC", 16000, 1}},
114 {104, {"ISAC", 32000, 1}},
115 {107, {"L16", 8000, 1}},
116 {108, {"L16", 16000, 1}},
117 {109, {"L16", 32000, 1}},
118 {0, {"PCMU", 8000, 1}},
119 {8, {"PCMA", 8000, 1}},
120 {102, {"ILBC", 8000, 1}},
121 {9, {"G722", 8000, 1}},
122 {120, {"OPUS", 48000, 2}},
123 {13, {"CN", 8000, 1}},
124 {98, {"CN", 16000, 1}},
125 {99, {"CN", 32000, 1}}});
126 } else {
127 ASSERT_EQ(channels, 2u);
128 acm->SetReceiveCodecs({{111, {"L16", 8000, 2}},
129 {112, {"L16", 16000, 2}},
130 {113, {"L16", 32000, 2}},
131 {110, {"PCMU", 8000, 2}},
132 {118, {"PCMA", 8000, 2}},
133 {119, {"G722", 8000, 2}},
134 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}});
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000135 }
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000136
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000137 int playSampFreq;
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000138 std::string file_name;
Jonas Olsson366a50c2018-09-06 13:41:30 +0200139 rtc::StringBuilder file_stream;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100140 file_stream << webrtc::test::OutputPath() << out_file_name << file_num
141 << ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000142 file_name = file_stream.str();
143 _rtpStream = rtpStream;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000144
Karl Wiberg88aee282018-06-14 13:12:05 +0200145 playSampFreq = 32000;
146 _pcmFile.Open(file_name, 32000, "wb+");
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000147
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000148 _realPayloadSizeBytes = 0;
Per Åhgrend82a02c2020-03-12 11:53:30 +0100149 _playoutBuffer = new int16_t[kWebRtc10MsPcmAudio];
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000150 _frequency = playSampFreq;
151 _acm = acm;
152 _firstTime = true;
153}
154
155void Receiver::Teardown() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000156 delete[] _playoutBuffer;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000157 _pcmFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000158}
159
160bool Receiver::IncomingPacket() {
161 if (!_rtpStream->EndOfFile()) {
162 if (_firstTime) {
163 _firstTime = false;
Niels Möllerbf474952019-02-18 12:00:06 +0100164 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000165 _payloadSizeBytes, &_nextTime);
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000166 if (_realPayloadSizeBytes == 0) {
167 if (_rtpStream->EndOfFile()) {
168 _firstTime = true;
169 return true;
170 } else {
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000171 return false;
172 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000173 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000174 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000175
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000176 EXPECT_EQ(0, _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes,
Niels Möllerbf474952019-02-18 12:00:06 +0100177 _rtpHeader));
178 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000179 _payloadSizeBytes, &_nextTime);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000180 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
181 _firstTime = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000182 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000183 }
184 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000185}
186
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000187bool Receiver::PlayoutData() {
188 AudioFrame audioFrame;
henrik.lundind4ccb002016-05-17 12:21:55 -0700189 bool muted;
190 int32_t ok = _acm->PlayoutData10Ms(_frequency, &audioFrame, &muted);
191 if (muted) {
192 ADD_FAILURE();
193 return false;
194 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000195 EXPECT_EQ(0, ok);
Jonas Olssona4d87372019-07-05 19:08:33 +0200196 if (ok < 0) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000197 return false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000198 }
199 if (_playoutLengthSmpls == 0) {
200 return false;
201 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200202 _pcmFile.Write10MsData(audioFrame.data(), audioFrame.samples_per_channel_ *
203 audioFrame.num_channels_);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000204 return true;
205}
206
207void Receiver::Run() {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000208 uint8_t counter500Ms = 50;
209 uint32_t clock = 0;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000210
211 while (counter500Ms > 0) {
212 if (clock == 0 || clock >= _nextTime) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000213 EXPECT_TRUE(IncomingPacket());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000214 if (clock == 0) {
215 clock = _nextTime;
216 }
217 }
218 if ((clock % 10) == 0) {
219 if (!PlayoutData()) {
220 clock++;
221 continue;
222 }
223 }
224 if (_rtpStream->EndOfFile()) {
225 counter500Ms--;
226 }
227 clock++;
228 }
229}
230
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100231EncodeDecodeTest::EncodeDecodeTest() = default;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000232
233void EncodeDecodeTest::Perform() {
Jonas Olssona4d87372019-07-05 19:08:33 +0200234 const std::map<int, SdpAudioFormat> send_codecs = {
235 {103, {"ISAC", 16000, 1}}, {104, {"ISAC", 32000, 1}},
236 {107, {"L16", 8000, 1}}, {108, {"L16", 16000, 1}},
237 {109, {"L16", 32000, 1}}, {0, {"PCMU", 8000, 1}},
238 {8, {"PCMA", 8000, 1}},
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100239#ifdef WEBRTC_CODEC_ILBC
Jonas Olssona4d87372019-07-05 19:08:33 +0200240 {102, {"ILBC", 8000, 1}},
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100241#endif
Jonas Olssona4d87372019-07-05 19:08:33 +0200242 {9, {"G722", 8000, 1}}};
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100243 int file_num = 0;
244 for (const auto& send_codec : send_codecs) {
245 RTPFile rtpFile;
246 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
247 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000248
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100249 std::string fileName = webrtc::test::TempFilename(
250 webrtc::test::OutputPath(), "encode_decode_rtp");
251 rtpFile.Open(fileName.c_str(), "wb+");
252 rtpFile.WriteHeader();
253 Sender sender;
254 sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000,
255 send_codec.first, send_codec.second);
256 sender.Run();
257 sender.Teardown();
258 rtpFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000259
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100260 rtpFile.Open(fileName.c_str(), "rb");
261 rtpFile.ReadHeader();
262 Receiver receiver;
263 receiver.Setup(acm.get(), &rtpFile, "encodeDecode_out", 1, file_num);
264 receiver.Run();
265 receiver.Teardown();
266 rtpFile.Close();
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000267
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100268 file_num++;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000269 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000270}
271
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000272} // namespace webrtc