blob: 20e415d2836921359a60dde4a5708afa79fdaa5d [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
Jonas Olssona4d87372019-07-05 19:08:33 +020027TestPacketization::TestPacketization(RTPStream* rtpStream, uint16_t frequency)
28 : _rtpStream(rtpStream), _frequency(frequency), _seqNo(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000029
Jonas Olssona4d87372019-07-05 19:08:33 +020030TestPacketization::~TestPacketization() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000031
Niels Möllerc35b6e62019-04-25 16:31:18 +020032int32_t TestPacketization::SendData(const AudioFrameType /* frameType */,
33 const uint8_t payloadType,
34 const uint32_t timeStamp,
35 const uint8_t* payloadData,
36 const size_t payloadSize) {
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000037 _rtpStream->Write(payloadType, timeStamp, _seqNo++, payloadData, payloadSize,
38 _frequency);
39 return 1;
40}
niklase@google.com470e71d2011-07-07 08:21:25 +000041
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000042Sender::Sender()
Jonas Olssona4d87372019-07-05 19:08:33 +020043 : _acm(NULL), _pcmFile(), _audioFrame(), _packetization(NULL) {}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000044
Jonas Olssona4d87372019-07-05 19:08:33 +020045void Sender::Setup(AudioCodingModule* acm,
46 RTPStream* rtpStream,
47 std::string in_file_name,
48 int in_sample_rate,
49 int payload_type,
50 SdpAudioFormat format) {
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000051 // Open input file
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000052 const std::string file_name = webrtc::test::ResourcePath(in_file_name, "pcm");
Fredrik Solenberg657b2962018-12-05 10:30:25 +010053 _pcmFile.Open(file_name, in_sample_rate, "rb");
54 if (format.num_channels == 2) {
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000055 _pcmFile.ReadStereo(true);
56 }
Henrik Lundin4d682082015-12-10 16:24:39 +010057 // Set test length to 500 ms (50 blocks of 10 ms each).
58 _pcmFile.SetNum10MsBlocksToRead(50);
59 // Fast-forward 1 second (100 blocks) since the file starts with silence.
60 _pcmFile.FastForward(100);
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000061
Karl Wiberg658a5522018-08-15 15:20:49 +020062 acm->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
Fredrik Solenberg657b2962018-12-05 10:30:25 +010063 payload_type, format, absl::nullopt));
64 _packetization = new TestPacketization(rtpStream, format.clockrate_hz);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000065 EXPECT_EQ(0, acm->RegisterTransportCallback(_packetization));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000066
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000067 _acm = acm;
68}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000069
70void Sender::Teardown() {
71 _pcmFile.Close();
72 delete _packetization;
niklase@google.com470e71d2011-07-07 08:21:25 +000073}
74
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000075bool Sender::Add10MsData() {
76 if (!_pcmFile.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000077 EXPECT_GT(_pcmFile.Read10MsData(_audioFrame), 0);
pbos@webrtc.org0946a562013-04-09 00:28:06 +000078 int32_t ok = _acm->Add10MsData(_audioFrame);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +000079 EXPECT_GE(ok, 0);
80 return ok >= 0 ? true : false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000081 }
82 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000085void Sender::Run() {
86 while (true) {
87 if (!Add10MsData()) {
88 break;
niklase@google.com470e71d2011-07-07 08:21:25 +000089 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000090 }
91}
92
93Receiver::Receiver()
94 : _playoutLengthSmpls(WEBRTC_10MS_PCM_AUDIO),
Jonas Olssona4d87372019-07-05 19:08:33 +020095 _payloadSizeBytes(MAX_INCOMING_PAYLOAD) {}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000096
Jonas Olssona4d87372019-07-05 19:08:33 +020097void Receiver::Setup(AudioCodingModule* acm,
98 RTPStream* rtpStream,
99 std::string out_file_name,
100 size_t channels,
101 int file_num) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000102 EXPECT_EQ(0, acm->InitializeReceiver());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000103
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100104 if (channels == 1) {
105 acm->SetReceiveCodecs({{103, {"ISAC", 16000, 1}},
106 {104, {"ISAC", 32000, 1}},
107 {107, {"L16", 8000, 1}},
108 {108, {"L16", 16000, 1}},
109 {109, {"L16", 32000, 1}},
110 {0, {"PCMU", 8000, 1}},
111 {8, {"PCMA", 8000, 1}},
112 {102, {"ILBC", 8000, 1}},
113 {9, {"G722", 8000, 1}},
114 {120, {"OPUS", 48000, 2}},
115 {13, {"CN", 8000, 1}},
116 {98, {"CN", 16000, 1}},
117 {99, {"CN", 32000, 1}}});
118 } else {
119 ASSERT_EQ(channels, 2u);
120 acm->SetReceiveCodecs({{111, {"L16", 8000, 2}},
121 {112, {"L16", 16000, 2}},
122 {113, {"L16", 32000, 2}},
123 {110, {"PCMU", 8000, 2}},
124 {118, {"PCMA", 8000, 2}},
125 {119, {"G722", 8000, 2}},
126 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}});
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000127 }
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000128
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000129 int playSampFreq;
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000130 std::string file_name;
Jonas Olsson366a50c2018-09-06 13:41:30 +0200131 rtc::StringBuilder file_stream;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100132 file_stream << webrtc::test::OutputPath() << out_file_name << file_num
133 << ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000134 file_name = file_stream.str();
135 _rtpStream = rtpStream;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000136
Karl Wiberg88aee282018-06-14 13:12:05 +0200137 playSampFreq = 32000;
138 _pcmFile.Open(file_name, 32000, "wb+");
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000139
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000140 _realPayloadSizeBytes = 0;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000141 _playoutBuffer = new int16_t[WEBRTC_10MS_PCM_AUDIO];
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000142 _frequency = playSampFreq;
143 _acm = acm;
144 _firstTime = true;
145}
146
147void Receiver::Teardown() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000148 delete[] _playoutBuffer;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000149 _pcmFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000150}
151
152bool Receiver::IncomingPacket() {
153 if (!_rtpStream->EndOfFile()) {
154 if (_firstTime) {
155 _firstTime = false;
Niels Möllerbf474952019-02-18 12:00:06 +0100156 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000157 _payloadSizeBytes, &_nextTime);
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000158 if (_realPayloadSizeBytes == 0) {
159 if (_rtpStream->EndOfFile()) {
160 _firstTime = true;
161 return true;
162 } else {
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000163 return false;
164 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000165 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000166 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000167
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000168 EXPECT_EQ(0, _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes,
Niels Möllerbf474952019-02-18 12:00:06 +0100169 _rtpHeader));
170 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000171 _payloadSizeBytes, &_nextTime);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000172 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
173 _firstTime = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000174 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000175 }
176 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000177}
178
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000179bool Receiver::PlayoutData() {
180 AudioFrame audioFrame;
henrik.lundind4ccb002016-05-17 12:21:55 -0700181 bool muted;
182 int32_t ok = _acm->PlayoutData10Ms(_frequency, &audioFrame, &muted);
183 if (muted) {
184 ADD_FAILURE();
185 return false;
186 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000187 EXPECT_EQ(0, ok);
Jonas Olssona4d87372019-07-05 19:08:33 +0200188 if (ok < 0) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000189 return false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000190 }
191 if (_playoutLengthSmpls == 0) {
192 return false;
193 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200194 _pcmFile.Write10MsData(audioFrame.data(), audioFrame.samples_per_channel_ *
195 audioFrame.num_channels_);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000196 return true;
197}
198
199void Receiver::Run() {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000200 uint8_t counter500Ms = 50;
201 uint32_t clock = 0;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000202
203 while (counter500Ms > 0) {
204 if (clock == 0 || clock >= _nextTime) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000205 EXPECT_TRUE(IncomingPacket());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000206 if (clock == 0) {
207 clock = _nextTime;
208 }
209 }
210 if ((clock % 10) == 0) {
211 if (!PlayoutData()) {
212 clock++;
213 continue;
214 }
215 }
216 if (_rtpStream->EndOfFile()) {
217 counter500Ms--;
218 }
219 clock++;
220 }
221}
222
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100223EncodeDecodeTest::EncodeDecodeTest() = default;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000224
225void EncodeDecodeTest::Perform() {
Jonas Olssona4d87372019-07-05 19:08:33 +0200226 const std::map<int, SdpAudioFormat> send_codecs = {
227 {103, {"ISAC", 16000, 1}}, {104, {"ISAC", 32000, 1}},
228 {107, {"L16", 8000, 1}}, {108, {"L16", 16000, 1}},
229 {109, {"L16", 32000, 1}}, {0, {"PCMU", 8000, 1}},
230 {8, {"PCMA", 8000, 1}},
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100231#ifdef WEBRTC_CODEC_ILBC
Jonas Olssona4d87372019-07-05 19:08:33 +0200232 {102, {"ILBC", 8000, 1}},
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100233#endif
Jonas Olssona4d87372019-07-05 19:08:33 +0200234 {9, {"G722", 8000, 1}}};
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100235 int file_num = 0;
236 for (const auto& send_codec : send_codecs) {
237 RTPFile rtpFile;
238 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
239 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000240
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100241 std::string fileName = webrtc::test::TempFilename(
242 webrtc::test::OutputPath(), "encode_decode_rtp");
243 rtpFile.Open(fileName.c_str(), "wb+");
244 rtpFile.WriteHeader();
245 Sender sender;
246 sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000,
247 send_codec.first, send_codec.second);
248 sender.Run();
249 sender.Teardown();
250 rtpFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000251
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100252 rtpFile.Open(fileName.c_str(), "rb");
253 rtpFile.ReadHeader();
254 Receiver receiver;
255 receiver.Setup(acm.get(), &rtpFile, "encodeDecode_out", 1, file_num);
256 receiver.Run();
257 receiver.Teardown();
258 rtpFile.Close();
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000259
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100260 file_num++;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000261 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000262}
263
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000264} // namespace webrtc