blob: a8c405357b7c4b0819bd746f0db03de02441e602 [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>
16#include <sstream> // no-presubmit-check TODO(webrtc:8982)
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 Bonadei71207422017-09-15 13:58:09 +020020#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_coding/codecs/audio_format_conversion.h"
22#include "modules/audio_coding/include/audio_coding_module.h"
23#include "modules/audio_coding/test/utility.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "test/gtest.h"
25#include "test/testsupport/fileutils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000026
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000027namespace webrtc {
28
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000029TestPacketization::TestPacketization(RTPStream *rtpStream, uint16_t frequency)
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000030 : _rtpStream(rtpStream),
31 _frequency(frequency),
32 _seqNo(0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000033}
34
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000035TestPacketization::~TestPacketization() {
36}
niklase@google.com470e71d2011-07-07 08:21:25 +000037
pbos@webrtc.org0946a562013-04-09 00:28:06 +000038int32_t TestPacketization::SendData(
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000039 const FrameType /* frameType */, const uint8_t payloadType,
40 const uint32_t timeStamp, const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000041 const size_t payloadSize,
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000042 const RTPFragmentationHeader* /* fragmentation */) {
43 _rtpStream->Write(payloadType, timeStamp, _seqNo++, payloadData, payloadSize,
44 _frequency);
45 return 1;
46}
niklase@google.com470e71d2011-07-07 08:21:25 +000047
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000048Sender::Sender()
49 : _acm(NULL),
50 _pcmFile(),
51 _audioFrame(),
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000052 _packetization(NULL) {
53}
54
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000055void Sender::Setup(AudioCodingModule *acm, RTPStream *rtpStream,
Peter Kasting69558702016-01-12 16:26:35 -080056 std::string in_file_name, int sample_rate, size_t channels) {
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000057 struct CodecInst sendCodec;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000058 int codecNo;
59
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000060 // Open input file
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000061 const std::string file_name = webrtc::test::ResourcePath(in_file_name, "pcm");
62 _pcmFile.Open(file_name, sample_rate, "rb");
63 if (channels == 2) {
64 _pcmFile.ReadStereo(true);
65 }
Henrik Lundin4d682082015-12-10 16:24:39 +010066 // Set test length to 500 ms (50 blocks of 10 ms each).
67 _pcmFile.SetNum10MsBlocksToRead(50);
68 // Fast-forward 1 second (100 blocks) since the file starts with silence.
69 _pcmFile.FastForward(100);
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000070
71 // Set the codec for the current test.
Karl Wiberg88aee282018-06-14 13:12:05 +020072 codecNo = codeId;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000073
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000074 EXPECT_EQ(0, acm->Codec(codecNo, &sendCodec));
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000075
76 sendCodec.channels = channels;
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000077
Karl Wiberg658a5522018-08-15 15:20:49 +020078 acm->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
79 sendCodec.pltype, CodecInstToSdp(sendCodec), absl::nullopt));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000080 _packetization = new TestPacketization(rtpStream, sendCodec.plfreq);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000081 EXPECT_EQ(0, acm->RegisterTransportCallback(_packetization));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000082
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000083 _acm = acm;
84}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000085
86void Sender::Teardown() {
87 _pcmFile.Close();
88 delete _packetization;
niklase@google.com470e71d2011-07-07 08:21:25 +000089}
90
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000091bool Sender::Add10MsData() {
92 if (!_pcmFile.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000093 EXPECT_GT(_pcmFile.Read10MsData(_audioFrame), 0);
pbos@webrtc.org0946a562013-04-09 00:28:06 +000094 int32_t ok = _acm->Add10MsData(_audioFrame);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +000095 EXPECT_GE(ok, 0);
96 return ok >= 0 ? true : false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000097 }
98 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +000099}
100
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000101void Sender::Run() {
102 while (true) {
103 if (!Add10MsData()) {
104 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000105 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000106 }
107}
108
109Receiver::Receiver()
110 : _playoutLengthSmpls(WEBRTC_10MS_PCM_AUDIO),
111 _payloadSizeBytes(MAX_INCOMING_PAYLOAD) {
112}
113
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000114void Receiver::Setup(AudioCodingModule *acm, RTPStream *rtpStream,
Peter Kasting69558702016-01-12 16:26:35 -0800115 std::string out_file_name, size_t channels) {
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000116 struct CodecInst recvCodec = CodecInst();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000117 int noOfCodecs;
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000118 EXPECT_EQ(0, acm->InitializeReceiver());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000119
120 noOfCodecs = acm->NumberOfCodecs();
121 for (int i = 0; i < noOfCodecs; i++) {
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000122 EXPECT_EQ(0, acm->Codec(i, &recvCodec));
123 if (recvCodec.channels == channels)
kwibergda2bf4e2016-10-24 13:47:09 -0700124 EXPECT_EQ(true, acm->RegisterReceiveCodec(recvCodec.pltype,
125 CodecInstToSdp(recvCodec)));
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000126 // Forces mono/stereo for Opus.
127 if (!strcmp(recvCodec.plname, "opus")) {
128 recvCodec.channels = channels;
kwibergda2bf4e2016-10-24 13:47:09 -0700129 EXPECT_EQ(true, acm->RegisterReceiveCodec(recvCodec.pltype,
130 CodecInstToSdp(recvCodec)));
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000131 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000132 }
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000133
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000134 int playSampFreq;
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000135 std::string file_name;
136 std::stringstream file_stream;
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000137 file_stream << webrtc::test::OutputPath() << out_file_name
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000138 << static_cast<int>(codeId) << ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000139 file_name = file_stream.str();
140 _rtpStream = rtpStream;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000141
Karl Wiberg88aee282018-06-14 13:12:05 +0200142 playSampFreq = 32000;
143 _pcmFile.Open(file_name, 32000, "wb+");
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000144
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000145 _realPayloadSizeBytes = 0;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000146 _playoutBuffer = new int16_t[WEBRTC_10MS_PCM_AUDIO];
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000147 _frequency = playSampFreq;
148 _acm = acm;
149 _firstTime = true;
150}
151
152void Receiver::Teardown() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000153 delete[] _playoutBuffer;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000154 _pcmFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000155}
156
157bool Receiver::IncomingPacket() {
158 if (!_rtpStream->EndOfFile()) {
159 if (_firstTime) {
160 _firstTime = false;
161 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload,
162 _payloadSizeBytes, &_nextTime);
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000163 if (_realPayloadSizeBytes == 0) {
164 if (_rtpStream->EndOfFile()) {
165 _firstTime = true;
166 return true;
167 } else {
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000168 return false;
169 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000170 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000171 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000172
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000173 EXPECT_EQ(0, _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes,
174 _rtpInfo));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000175 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload,
176 _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;
187 int32_t ok = _acm->PlayoutData10Ms(_frequency, &audioFrame, &muted);
188 if (muted) {
189 ADD_FAILURE();
190 return false;
191 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000192 EXPECT_EQ(0, ok);
193 if (ok < 0){
194 return false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000195 }
196 if (_playoutLengthSmpls == 0) {
197 return false;
198 }
yujo36b1a5f2017-06-12 12:45:32 -0700199 _pcmFile.Write10MsData(audioFrame.data(),
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000200 audioFrame.samples_per_channel_ * 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
Karl Wiberg88aee282018-06-14 13:12:05 +0200228EncodeDecodeTest::EncodeDecodeTest(int test_mode) {
229 // There used to be different test modes. The only one still supported is the
230 // "autotest" mode.
231 RTC_CHECK_EQ(0, test_mode);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000232}
233
234void EncodeDecodeTest::Perform() {
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000235 int numCodecs = 1;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000236 int codePars[3]; // Frequency, packet size, rate.
237 int numPars[52]; // Number of codec parameters sets (freq, pacsize, rate)
238 // to test, for a given codec.
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000239
240 codePars[0] = 0;
241 codePars[1] = 0;
242 codePars[2] = 0;
243
Karl Wiberg5817d3d2018-04-06 10:06:42 +0200244 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
245 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000246 struct CodecInst sendCodecTmp;
tina.legrand@webrtc.org5b4f36d2012-06-01 14:51:28 +0000247 numCodecs = acm->NumberOfCodecs();
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000248
Karl Wiberg88aee282018-06-14 13:12:05 +0200249 for (int n = 0; n < numCodecs; n++) {
250 EXPECT_EQ(0, acm->Codec(n, &sendCodecTmp));
251 if (STR_CASE_CMP(sendCodecTmp.plname, "telephone-event") == 0) {
252 numPars[n] = 0;
253 } else if (STR_CASE_CMP(sendCodecTmp.plname, "cn") == 0) {
254 numPars[n] = 0;
255 } else if (STR_CASE_CMP(sendCodecTmp.plname, "red") == 0) {
256 numPars[n] = 0;
257 } else if (sendCodecTmp.channels == 2) {
258 numPars[n] = 0;
259 } else {
260 numPars[n] = 1;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000261 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000262 }
263
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000264 // Loop over all mono codecs:
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000265 for (int codeId = 0; codeId < numCodecs; codeId++) {
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000266 // Only encode using real mono encoders, not telephone-event and cng.
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000267 for (int loopPars = 1; loopPars <= numPars[codeId]; loopPars++) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000268 // Encode all data to file.
Karl Wiberg88aee282018-06-14 13:12:05 +0200269 std::string fileName = EncodeToFile(1, codeId, codePars);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000270
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000271 RTPFile rtpFile;
kjellander@webrtc.org5490c712011-12-21 13:34:18 +0000272 rtpFile.Open(fileName.c_str(), "rb");
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000273
274 _receiver.codeId = codeId;
275
276 rtpFile.ReadHeader();
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000277 _receiver.Setup(acm.get(), &rtpFile, "encodeDecode_out", 1);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000278 _receiver.Run();
279 _receiver.Teardown();
280 rtpFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000281 }
282 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000283}
284
pbos@webrtc.orgc86e45d2014-10-01 10:05:40 +0000285std::string EncodeDecodeTest::EncodeToFile(int fileType,
286 int codeId,
Karl Wiberg88aee282018-06-14 13:12:05 +0200287 int* codePars) {
Karl Wiberg5817d3d2018-04-06 10:06:42 +0200288 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
289 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000290 RTPFile rtpFile;
pbos@webrtc.orgc86e45d2014-10-01 10:05:40 +0000291 std::string fileName = webrtc::test::TempFilename(webrtc::test::OutputPath(),
292 "encode_decode_rtp");
kjellander@webrtc.org5490c712011-12-21 13:34:18 +0000293 rtpFile.Open(fileName.c_str(), "wb+");
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000294 rtpFile.WriteHeader();
295
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000296 // Store for auto_test and logging.
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000297 _sender.codeId = codeId;
298
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000299 _sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000, 1);
kwiberg1fd4a4a2015-11-03 11:20:50 -0800300 if (acm->SendCodec()) {
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000301 _sender.Run();
302 }
303 _sender.Teardown();
304 rtpFile.Close();
pbos@webrtc.orgc86e45d2014-10-01 10:05:40 +0000305
306 return fileName;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000307}
308
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000309} // namespace webrtc