blob: fbbc9d32f0be7952d3340673b5cb297fdddabc68 [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>
Yves Gerey665174f2018-06-19 15:03:05 +020015#include <memory>
tina.legrand@webrtc.org5e7ca602012-06-12 07:16:24 +000016
Karl Wiberg5817d3d2018-04-06 10:06:42 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Karl Wiberg658a5522018-08-15 15:20:49 +020018#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/include/audio_coding_module.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020020#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "test/testsupport/file_utils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000024namespace webrtc {
25
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000026TestPacketization::TestPacketization(RTPStream *rtpStream, uint16_t frequency)
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000027 : _rtpStream(rtpStream),
28 _frequency(frequency),
29 _seqNo(0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000030}
31
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000032TestPacketization::~TestPacketization() {
33}
niklase@google.com470e71d2011-07-07 08:21:25 +000034
pbos@webrtc.org0946a562013-04-09 00:28:06 +000035int32_t TestPacketization::SendData(
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000036 const FrameType /* frameType */, const uint8_t payloadType,
37 const uint32_t timeStamp, const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000038 const size_t payloadSize,
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000039 const RTPFragmentationHeader* /* fragmentation */) {
40 _rtpStream->Write(payloadType, timeStamp, _seqNo++, payloadData, payloadSize,
41 _frequency);
42 return 1;
43}
niklase@google.com470e71d2011-07-07 08:21:25 +000044
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000045Sender::Sender()
46 : _acm(NULL),
47 _pcmFile(),
48 _audioFrame(),
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000049 _packetization(NULL) {
50}
51
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000052void Sender::Setup(AudioCodingModule *acm, RTPStream *rtpStream,
Fredrik Solenberg657b2962018-12-05 10:30:25 +010053 std::string in_file_name, int in_sample_rate,
54 int payload_type, SdpAudioFormat format) {
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000055 // Open input file
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000056 const std::string file_name = webrtc::test::ResourcePath(in_file_name, "pcm");
Fredrik Solenberg657b2962018-12-05 10:30:25 +010057 _pcmFile.Open(file_name, in_sample_rate, "rb");
58 if (format.num_channels == 2) {
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000059 _pcmFile.ReadStereo(true);
60 }
Henrik Lundin4d682082015-12-10 16:24:39 +010061 // Set test length to 500 ms (50 blocks of 10 ms each).
62 _pcmFile.SetNum10MsBlocksToRead(50);
63 // Fast-forward 1 second (100 blocks) since the file starts with silence.
64 _pcmFile.FastForward(100);
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000065
Karl Wiberg658a5522018-08-15 15:20:49 +020066 acm->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
Fredrik Solenberg657b2962018-12-05 10:30:25 +010067 payload_type, format, absl::nullopt));
68 _packetization = new TestPacketization(rtpStream, format.clockrate_hz);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000069 EXPECT_EQ(0, acm->RegisterTransportCallback(_packetization));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000070
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000071 _acm = acm;
72}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000073
74void Sender::Teardown() {
75 _pcmFile.Close();
76 delete _packetization;
niklase@google.com470e71d2011-07-07 08:21:25 +000077}
78
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000079bool Sender::Add10MsData() {
80 if (!_pcmFile.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000081 EXPECT_GT(_pcmFile.Read10MsData(_audioFrame), 0);
pbos@webrtc.org0946a562013-04-09 00:28:06 +000082 int32_t ok = _acm->Add10MsData(_audioFrame);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +000083 EXPECT_GE(ok, 0);
84 return ok >= 0 ? true : false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000085 }
86 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000089void Sender::Run() {
90 while (true) {
91 if (!Add10MsData()) {
92 break;
niklase@google.com470e71d2011-07-07 08:21:25 +000093 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000094 }
95}
96
97Receiver::Receiver()
98 : _playoutLengthSmpls(WEBRTC_10MS_PCM_AUDIO),
99 _payloadSizeBytes(MAX_INCOMING_PAYLOAD) {
100}
101
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000102void Receiver::Setup(AudioCodingModule *acm, RTPStream *rtpStream,
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100103 std::string out_file_name, size_t channels, int file_num) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000104 EXPECT_EQ(0, acm->InitializeReceiver());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000105
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100106 if (channels == 1) {
107 acm->SetReceiveCodecs({{103, {"ISAC", 16000, 1}},
108 {104, {"ISAC", 32000, 1}},
109 {107, {"L16", 8000, 1}},
110 {108, {"L16", 16000, 1}},
111 {109, {"L16", 32000, 1}},
112 {0, {"PCMU", 8000, 1}},
113 {8, {"PCMA", 8000, 1}},
114 {102, {"ILBC", 8000, 1}},
115 {9, {"G722", 8000, 1}},
116 {120, {"OPUS", 48000, 2}},
117 {13, {"CN", 8000, 1}},
118 {98, {"CN", 16000, 1}},
119 {99, {"CN", 32000, 1}}});
120 } else {
121 ASSERT_EQ(channels, 2u);
122 acm->SetReceiveCodecs({{111, {"L16", 8000, 2}},
123 {112, {"L16", 16000, 2}},
124 {113, {"L16", 32000, 2}},
125 {110, {"PCMU", 8000, 2}},
126 {118, {"PCMA", 8000, 2}},
127 {119, {"G722", 8000, 2}},
128 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}});
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000129 }
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000130
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000131 int playSampFreq;
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000132 std::string file_name;
Jonas Olsson366a50c2018-09-06 13:41:30 +0200133 rtc::StringBuilder file_stream;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100134 file_stream << webrtc::test::OutputPath() << out_file_name << file_num
135 << ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000136 file_name = file_stream.str();
137 _rtpStream = rtpStream;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000138
Karl Wiberg88aee282018-06-14 13:12:05 +0200139 playSampFreq = 32000;
140 _pcmFile.Open(file_name, 32000, "wb+");
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000141
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000142 _realPayloadSizeBytes = 0;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000143 _playoutBuffer = new int16_t[WEBRTC_10MS_PCM_AUDIO];
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000144 _frequency = playSampFreq;
145 _acm = acm;
146 _firstTime = true;
147}
148
149void Receiver::Teardown() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000150 delete[] _playoutBuffer;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000151 _pcmFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000152}
153
154bool Receiver::IncomingPacket() {
155 if (!_rtpStream->EndOfFile()) {
156 if (_firstTime) {
157 _firstTime = false;
158 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload,
159 _payloadSizeBytes, &_nextTime);
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000160 if (_realPayloadSizeBytes == 0) {
161 if (_rtpStream->EndOfFile()) {
162 _firstTime = true;
163 return true;
164 } else {
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000165 return false;
166 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000167 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000168 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000169
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000170 EXPECT_EQ(0, _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes,
171 _rtpInfo));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000172 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload,
173 _payloadSizeBytes, &_nextTime);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000174 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
175 _firstTime = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000176 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000177 }
178 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000179}
180
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000181bool Receiver::PlayoutData() {
182 AudioFrame audioFrame;
henrik.lundind4ccb002016-05-17 12:21:55 -0700183 bool muted;
184 int32_t ok = _acm->PlayoutData10Ms(_frequency, &audioFrame, &muted);
185 if (muted) {
186 ADD_FAILURE();
187 return false;
188 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000189 EXPECT_EQ(0, ok);
190 if (ok < 0){
191 return false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000192 }
193 if (_playoutLengthSmpls == 0) {
194 return false;
195 }
yujo36b1a5f2017-06-12 12:45:32 -0700196 _pcmFile.Write10MsData(audioFrame.data(),
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000197 audioFrame.samples_per_channel_ * audioFrame.num_channels_);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000198 return true;
199}
200
201void Receiver::Run() {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000202 uint8_t counter500Ms = 50;
203 uint32_t clock = 0;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000204
205 while (counter500Ms > 0) {
206 if (clock == 0 || clock >= _nextTime) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000207 EXPECT_TRUE(IncomingPacket());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000208 if (clock == 0) {
209 clock = _nextTime;
210 }
211 }
212 if ((clock % 10) == 0) {
213 if (!PlayoutData()) {
214 clock++;
215 continue;
216 }
217 }
218 if (_rtpStream->EndOfFile()) {
219 counter500Ms--;
220 }
221 clock++;
222 }
223}
224
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100225EncodeDecodeTest::EncodeDecodeTest() = default;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000226
227void EncodeDecodeTest::Perform() {
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100228 const std::map<int, SdpAudioFormat> send_codecs = {{103, {"ISAC", 16000, 1}},
229 {104, {"ISAC", 32000, 1}},
230 {107, {"L16", 8000, 1}},
231 {108, {"L16", 16000, 1}},
232 {109, {"L16", 32000, 1}},
233 {0, {"PCMU", 8000, 1}},
234 {8, {"PCMA", 8000, 1}},
235#ifdef WEBRTC_CODEC_ILBC
236 {102, {"ILBC", 8000, 1}},
237#endif
238 {9, {"G722", 8000, 1}}};
239 int file_num = 0;
240 for (const auto& send_codec : send_codecs) {
241 RTPFile rtpFile;
242 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
243 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000244
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100245 std::string fileName = webrtc::test::TempFilename(
246 webrtc::test::OutputPath(), "encode_decode_rtp");
247 rtpFile.Open(fileName.c_str(), "wb+");
248 rtpFile.WriteHeader();
249 Sender sender;
250 sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000,
251 send_codec.first, send_codec.second);
252 sender.Run();
253 sender.Teardown();
254 rtpFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000255
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100256 rtpFile.Open(fileName.c_str(), "rb");
257 rtpFile.ReadHeader();
258 Receiver receiver;
259 receiver.Setup(acm.get(), &rtpFile, "encodeDecode_out", 1, file_num);
260 receiver.Run();
261 receiver.Teardown();
262 rtpFile.Close();
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000263
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100264 file_num++;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000265 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000266}
267
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000268} // namespace webrtc