blob: 014a1d2d54613504fed441cbb60f8c11ba535bd7 [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
Henrik Lundin84f75692023-02-01 12:07:10 +0000105void Receiver::Setup(acm2::AcmReceiver* acm_receiver,
Jonas Olssona4d87372019-07-05 19:08:33 +0200106 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) {
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100110 if (channels == 1) {
Henrik Lundin84f75692023-02-01 12:07:10 +0000111 acm_receiver->SetCodecs({{107, {"L16", 8000, 1}},
112 {108, {"L16", 16000, 1}},
113 {109, {"L16", 32000, 1}},
114 {0, {"PCMU", 8000, 1}},
115 {8, {"PCMA", 8000, 1}},
116 {102, {"ILBC", 8000, 1}},
117 {9, {"G722", 8000, 1}},
118 {120, {"OPUS", 48000, 2}},
119 {13, {"CN", 8000, 1}},
120 {98, {"CN", 16000, 1}},
121 {99, {"CN", 32000, 1}}});
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100122 } else {
123 ASSERT_EQ(channels, 2u);
Henrik Lundin84f75692023-02-01 12:07:10 +0000124 acm_receiver->SetCodecs({{111, {"L16", 8000, 2}},
125 {112, {"L16", 16000, 2}},
126 {113, {"L16", 32000, 2}},
127 {110, {"PCMU", 8000, 2}},
128 {118, {"PCMA", 8000, 2}},
129 {119, {"G722", 8000, 2}},
130 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}});
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000131 }
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000132
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000133 int playSampFreq;
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000134 std::string file_name;
Jonas Olsson366a50c2018-09-06 13:41:30 +0200135 rtc::StringBuilder file_stream;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100136 file_stream << webrtc::test::OutputPath() << out_file_name << file_num
137 << ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000138 file_name = file_stream.str();
139 _rtpStream = rtpStream;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000140
Karl Wiberg88aee282018-06-14 13:12:05 +0200141 playSampFreq = 32000;
142 _pcmFile.Open(file_name, 32000, "wb+");
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000143
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000144 _realPayloadSizeBytes = 0;
Per Åhgrend82a02c2020-03-12 11:53:30 +0100145 _playoutBuffer = new int16_t[kWebRtc10MsPcmAudio];
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000146 _frequency = playSampFreq;
Henrik Lundin84f75692023-02-01 12:07:10 +0000147 _acm_receiver = acm_receiver;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000148 _firstTime = true;
149}
150
151void Receiver::Teardown() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000152 delete[] _playoutBuffer;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000153 _pcmFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000154}
155
156bool Receiver::IncomingPacket() {
157 if (!_rtpStream->EndOfFile()) {
158 if (_firstTime) {
159 _firstTime = false;
Niels Möllerbf474952019-02-18 12:00:06 +0100160 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000161 _payloadSizeBytes, &_nextTime);
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000162 if (_realPayloadSizeBytes == 0) {
163 if (_rtpStream->EndOfFile()) {
164 _firstTime = true;
165 return true;
166 } else {
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000167 return false;
168 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000169 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000170 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
Henrik Lundin84f75692023-02-01 12:07:10 +0000172 EXPECT_EQ(0, _acm_receiver->InsertPacket(
173 _rtpHeader, rtc::ArrayView<const uint8_t>(
174 _incomingPayload, _realPayloadSizeBytes)));
Niels Möllerbf474952019-02-18 12:00:06 +0100175 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000176 _payloadSizeBytes, &_nextTime);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000177 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
178 _firstTime = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000179 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000180 }
181 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000182}
183
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000184bool Receiver::PlayoutData() {
185 AudioFrame audioFrame;
henrik.lundind4ccb002016-05-17 12:21:55 -0700186 bool muted;
Henrik Lundin84f75692023-02-01 12:07:10 +0000187 int32_t ok = _acm_receiver->GetAudio(_frequency, &audioFrame, &muted);
henrik.lundind4ccb002016-05-17 12:21:55 -0700188 if (muted) {
189 ADD_FAILURE();
190 return false;
191 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000192 EXPECT_EQ(0, ok);
Jonas Olssona4d87372019-07-05 19:08:33 +0200193 if (ok < 0) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000194 return false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000195 }
196 if (_playoutLengthSmpls == 0) {
197 return false;
198 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200199 _pcmFile.Write10MsData(audioFrame.data(), audioFrame.samples_per_channel_ *
200 audioFrame.num_channels_);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000201 return true;
202}
203
204void Receiver::Run() {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000205 uint8_t counter500Ms = 50;
206 uint32_t clock = 0;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000207
208 while (counter500Ms > 0) {
209 if (clock == 0 || clock >= _nextTime) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000210 EXPECT_TRUE(IncomingPacket());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000211 if (clock == 0) {
212 clock = _nextTime;
213 }
214 }
215 if ((clock % 10) == 0) {
216 if (!PlayoutData()) {
217 clock++;
218 continue;
219 }
220 }
221 if (_rtpStream->EndOfFile()) {
222 counter500Ms--;
223 }
224 clock++;
225 }
226}
227
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100228EncodeDecodeTest::EncodeDecodeTest() = default;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000229
230void EncodeDecodeTest::Perform() {
Jonas Olssona4d87372019-07-05 19:08:33 +0200231 const std::map<int, SdpAudioFormat> send_codecs = {
Jonas Olssona4d87372019-07-05 19:08:33 +0200232 {107, {"L16", 8000, 1}}, {108, {"L16", 16000, 1}},
233 {109, {"L16", 32000, 1}}, {0, {"PCMU", 8000, 1}},
234 {8, {"PCMA", 8000, 1}},
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100235#ifdef WEBRTC_CODEC_ILBC
Jonas Olssona4d87372019-07-05 19:08:33 +0200236 {102, {"ILBC", 8000, 1}},
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100237#endif
Jonas Olssona4d87372019-07-05 19:08:33 +0200238 {9, {"G722", 8000, 1}}};
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100239 int file_num = 0;
240 for (const auto& send_codec : send_codecs) {
241 RTPFile rtpFile;
Henrik Lundin84f75692023-02-01 12:07:10 +0000242 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000243
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100244 std::string fileName = webrtc::test::TempFilename(
245 webrtc::test::OutputPath(), "encode_decode_rtp");
246 rtpFile.Open(fileName.c_str(), "wb+");
247 rtpFile.WriteHeader();
248 Sender sender;
249 sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000,
250 send_codec.first, send_codec.second);
251 sender.Run();
252 sender.Teardown();
253 rtpFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000254
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100255 rtpFile.Open(fileName.c_str(), "rb");
256 rtpFile.ReadHeader();
Henrik Lundin84f75692023-02-01 12:07:10 +0000257 std::unique_ptr<acm2::AcmReceiver> acm_receiver(
258 std::make_unique<acm2::AcmReceiver>(
259 acm2::AcmReceiver::Config(CreateBuiltinAudioDecoderFactory())));
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100260 Receiver receiver;
Henrik Lundin84f75692023-02-01 12:07:10 +0000261 receiver.Setup(acm_receiver.get(), &rtpFile, "encodeDecode_out", 1,
262 file_num);
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100263 receiver.Run();
264 receiver.Teardown();
265 rtpFile.Close();
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000266
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100267 file_num++;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000268 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000269}
270
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000271} // namespace webrtc