blob: 3ed1789e938c2c0ca75cd9950a8a1164fe59ea8f [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
Karl Wiberg5817d3d2018-04-06 10:06:42 +020018#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Karl Wiberg658a5522018-08-15 15:20:49 +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.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020021#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "test/testsupport/file_utils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000024
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000025namespace webrtc {
26
Per Åhgrend82a02c2020-03-12 11:53:30 +010027namespace {
28// Buffer size for stereo 48 kHz audio.
29constexpr size_t kWebRtc10MsPcmAudio = 960;
30
31} // namespace
32
Jonas Olssona4d87372019-07-05 19:08:33 +020033TestPacketization::TestPacketization(RTPStream* rtpStream, uint16_t frequency)
34 : _rtpStream(rtpStream), _frequency(frequency), _seqNo(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000035
Jonas Olssona4d87372019-07-05 19:08:33 +020036TestPacketization::~TestPacketization() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000037
Niels Möllerc35b6e62019-04-25 16:31:18 +020038int32_t TestPacketization::SendData(const AudioFrameType /* frameType */,
39 const uint8_t payloadType,
40 const uint32_t timeStamp,
41 const uint8_t* payloadData,
Minyue Liff0e4db2020-01-23 13:45:50 +010042 const size_t payloadSize,
43 int64_t absolute_capture_timestamp_ms) {
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000044 _rtpStream->Write(payloadType, timeStamp, _seqNo++, payloadData, payloadSize,
45 _frequency);
46 return 1;
47}
niklase@google.com470e71d2011-07-07 08:21:25 +000048
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000049Sender::Sender()
Jonas Olssona4d87372019-07-05 19:08:33 +020050 : _acm(NULL), _pcmFile(), _audioFrame(), _packetization(NULL) {}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000051
Jonas Olssona4d87372019-07-05 19:08:33 +020052void Sender::Setup(AudioCodingModule* acm,
53 RTPStream* rtpStream,
54 std::string in_file_name,
55 int in_sample_rate,
56 int payload_type,
57 SdpAudioFormat format) {
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000058 // Open input file
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000059 const std::string file_name = webrtc::test::ResourcePath(in_file_name, "pcm");
Fredrik Solenberg657b2962018-12-05 10:30:25 +010060 _pcmFile.Open(file_name, in_sample_rate, "rb");
61 if (format.num_channels == 2) {
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000062 _pcmFile.ReadStereo(true);
63 }
Henrik Lundin4d682082015-12-10 16:24:39 +010064 // Set test length to 500 ms (50 blocks of 10 ms each).
65 _pcmFile.SetNum10MsBlocksToRead(50);
66 // Fast-forward 1 second (100 blocks) since the file starts with silence.
67 _pcmFile.FastForward(100);
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000068
Karl Wiberg658a5522018-08-15 15:20:49 +020069 acm->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
Fredrik Solenberg657b2962018-12-05 10:30:25 +010070 payload_type, format, absl::nullopt));
71 _packetization = new TestPacketization(rtpStream, format.clockrate_hz);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000072 EXPECT_EQ(0, acm->RegisterTransportCallback(_packetization));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000073
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000074 _acm = acm;
75}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000076
77void Sender::Teardown() {
78 _pcmFile.Close();
79 delete _packetization;
niklase@google.com470e71d2011-07-07 08:21:25 +000080}
81
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000082bool Sender::Add10MsData() {
83 if (!_pcmFile.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000084 EXPECT_GT(_pcmFile.Read10MsData(_audioFrame), 0);
pbos@webrtc.org0946a562013-04-09 00:28:06 +000085 int32_t ok = _acm->Add10MsData(_audioFrame);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +000086 EXPECT_GE(ok, 0);
87 return ok >= 0 ? true : false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000088 }
89 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +000090}
91
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000092void Sender::Run() {
93 while (true) {
94 if (!Add10MsData()) {
95 break;
niklase@google.com470e71d2011-07-07 08:21:25 +000096 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000097 }
98}
99
100Receiver::Receiver()
Per Åhgrend82a02c2020-03-12 11:53:30 +0100101 : _playoutLengthSmpls(kWebRtc10MsPcmAudio),
Jonas Olssona4d87372019-07-05 19:08:33 +0200102 _payloadSizeBytes(MAX_INCOMING_PAYLOAD) {}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000103
Jonas Olssona4d87372019-07-05 19:08:33 +0200104void Receiver::Setup(AudioCodingModule* acm,
105 RTPStream* rtpStream,
106 std::string out_file_name,
107 size_t channels,
108 int file_num) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000109 EXPECT_EQ(0, acm->InitializeReceiver());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000110
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100111 if (channels == 1) {
112 acm->SetReceiveCodecs({{103, {"ISAC", 16000, 1}},
113 {104, {"ISAC", 32000, 1}},
114 {107, {"L16", 8000, 1}},
115 {108, {"L16", 16000, 1}},
116 {109, {"L16", 32000, 1}},
117 {0, {"PCMU", 8000, 1}},
118 {8, {"PCMA", 8000, 1}},
119 {102, {"ILBC", 8000, 1}},
120 {9, {"G722", 8000, 1}},
121 {120, {"OPUS", 48000, 2}},
122 {13, {"CN", 8000, 1}},
123 {98, {"CN", 16000, 1}},
124 {99, {"CN", 32000, 1}}});
125 } else {
126 ASSERT_EQ(channels, 2u);
127 acm->SetReceiveCodecs({{111, {"L16", 8000, 2}},
128 {112, {"L16", 16000, 2}},
129 {113, {"L16", 32000, 2}},
130 {110, {"PCMU", 8000, 2}},
131 {118, {"PCMA", 8000, 2}},
132 {119, {"G722", 8000, 2}},
133 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}});
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000134 }
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000135
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000136 int playSampFreq;
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000137 std::string file_name;
Jonas Olsson366a50c2018-09-06 13:41:30 +0200138 rtc::StringBuilder file_stream;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100139 file_stream << webrtc::test::OutputPath() << out_file_name << file_num
140 << ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000141 file_name = file_stream.str();
142 _rtpStream = rtpStream;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000143
Karl Wiberg88aee282018-06-14 13:12:05 +0200144 playSampFreq = 32000;
145 _pcmFile.Open(file_name, 32000, "wb+");
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000146
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000147 _realPayloadSizeBytes = 0;
Per Åhgrend82a02c2020-03-12 11:53:30 +0100148 _playoutBuffer = new int16_t[kWebRtc10MsPcmAudio];
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000149 _frequency = playSampFreq;
150 _acm = acm;
151 _firstTime = true;
152}
153
154void Receiver::Teardown() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000155 delete[] _playoutBuffer;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000156 _pcmFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000157}
158
159bool Receiver::IncomingPacket() {
160 if (!_rtpStream->EndOfFile()) {
161 if (_firstTime) {
162 _firstTime = false;
Niels Möllerbf474952019-02-18 12:00:06 +0100163 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000164 _payloadSizeBytes, &_nextTime);
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000165 if (_realPayloadSizeBytes == 0) {
166 if (_rtpStream->EndOfFile()) {
167 _firstTime = true;
168 return true;
169 } else {
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000170 return false;
171 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000172 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000173 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000174
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000175 EXPECT_EQ(0, _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes,
Niels Möllerbf474952019-02-18 12:00:06 +0100176 _rtpHeader));
177 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000178 _payloadSizeBytes, &_nextTime);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000179 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
180 _firstTime = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000182 }
183 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000184}
185
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000186bool Receiver::PlayoutData() {
187 AudioFrame audioFrame;
henrik.lundind4ccb002016-05-17 12:21:55 -0700188 bool muted;
189 int32_t ok = _acm->PlayoutData10Ms(_frequency, &audioFrame, &muted);
190 if (muted) {
191 ADD_FAILURE();
192 return false;
193 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000194 EXPECT_EQ(0, ok);
Jonas Olssona4d87372019-07-05 19:08:33 +0200195 if (ok < 0) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000196 return false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000197 }
198 if (_playoutLengthSmpls == 0) {
199 return false;
200 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200201 _pcmFile.Write10MsData(audioFrame.data(), audioFrame.samples_per_channel_ *
202 audioFrame.num_channels_);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000203 return true;
204}
205
206void Receiver::Run() {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000207 uint8_t counter500Ms = 50;
208 uint32_t clock = 0;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000209
210 while (counter500Ms > 0) {
211 if (clock == 0 || clock >= _nextTime) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000212 EXPECT_TRUE(IncomingPacket());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000213 if (clock == 0) {
214 clock = _nextTime;
215 }
216 }
217 if ((clock % 10) == 0) {
218 if (!PlayoutData()) {
219 clock++;
220 continue;
221 }
222 }
223 if (_rtpStream->EndOfFile()) {
224 counter500Ms--;
225 }
226 clock++;
227 }
228}
229
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100230EncodeDecodeTest::EncodeDecodeTest() = default;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000231
232void EncodeDecodeTest::Perform() {
Jonas Olssona4d87372019-07-05 19:08:33 +0200233 const std::map<int, SdpAudioFormat> send_codecs = {
234 {103, {"ISAC", 16000, 1}}, {104, {"ISAC", 32000, 1}},
235 {107, {"L16", 8000, 1}}, {108, {"L16", 16000, 1}},
236 {109, {"L16", 32000, 1}}, {0, {"PCMU", 8000, 1}},
237 {8, {"PCMA", 8000, 1}},
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100238#ifdef WEBRTC_CODEC_ILBC
Jonas Olssona4d87372019-07-05 19:08:33 +0200239 {102, {"ILBC", 8000, 1}},
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100240#endif
Jonas Olssona4d87372019-07-05 19:08:33 +0200241 {9, {"G722", 8000, 1}}};
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100242 int file_num = 0;
243 for (const auto& send_codec : send_codecs) {
244 RTPFile rtpFile;
245 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
246 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000247
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100248 std::string fileName = webrtc::test::TempFilename(
249 webrtc::test::OutputPath(), "encode_decode_rtp");
250 rtpFile.Open(fileName.c_str(), "wb+");
251 rtpFile.WriteHeader();
252 Sender sender;
253 sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000,
254 send_codec.first, send_codec.second);
255 sender.Run();
256 sender.Teardown();
257 rtpFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000258
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100259 rtpFile.Open(fileName.c_str(), "rb");
260 rtpFile.ReadHeader();
261 Receiver receiver;
262 receiver.Setup(acm.get(), &rtpFile, "encodeDecode_out", 1, file_num);
263 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