blob: 4e16dc8989c7b458efee9d0314c8e7eb42d956b1 [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"
Mirko Bonadei71207422017-09-15 13:58:09 +020019#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/codecs/audio_format_conversion.h"
21#include "modules/audio_coding/include/audio_coding_module.h"
22#include "modules/audio_coding/test/utility.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "test/gtest.h"
24#include "test/testsupport/fileutils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000025
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000026namespace webrtc {
27
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000028TestPacketization::TestPacketization(RTPStream *rtpStream, uint16_t frequency)
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000029 : _rtpStream(rtpStream),
30 _frequency(frequency),
31 _seqNo(0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000032}
33
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000034TestPacketization::~TestPacketization() {
35}
niklase@google.com470e71d2011-07-07 08:21:25 +000036
pbos@webrtc.org0946a562013-04-09 00:28:06 +000037int32_t TestPacketization::SendData(
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000038 const FrameType /* frameType */, const uint8_t payloadType,
39 const uint32_t timeStamp, const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000040 const size_t payloadSize,
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000041 const RTPFragmentationHeader* /* fragmentation */) {
42 _rtpStream->Write(payloadType, timeStamp, _seqNo++, payloadData, payloadSize,
43 _frequency);
44 return 1;
45}
niklase@google.com470e71d2011-07-07 08:21:25 +000046
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000047Sender::Sender()
48 : _acm(NULL),
49 _pcmFile(),
50 _audioFrame(),
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000051 _packetization(NULL) {
52}
53
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000054void Sender::Setup(AudioCodingModule *acm, RTPStream *rtpStream,
Peter Kasting69558702016-01-12 16:26:35 -080055 std::string in_file_name, int sample_rate, size_t channels) {
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000056 struct CodecInst sendCodec;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000057 int codecNo;
58
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");
61 _pcmFile.Open(file_name, sample_rate, "rb");
62 if (channels == 2) {
63 _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
70 // Set the codec for the current test.
Karl Wiberg88aee282018-06-14 13:12:05 +020071 codecNo = codeId;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000072
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000073 EXPECT_EQ(0, acm->Codec(codecNo, &sendCodec));
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000074
75 sendCodec.channels = channels;
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000076
77 EXPECT_EQ(0, acm->RegisterSendCodec(sendCodec));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000078 _packetization = new TestPacketization(rtpStream, sendCodec.plfreq);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000079 EXPECT_EQ(0, acm->RegisterTransportCallback(_packetization));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000080
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000081 _acm = acm;
82}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000083
84void Sender::Teardown() {
85 _pcmFile.Close();
86 delete _packetization;
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000089bool Sender::Add10MsData() {
90 if (!_pcmFile.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000091 EXPECT_GT(_pcmFile.Read10MsData(_audioFrame), 0);
pbos@webrtc.org0946a562013-04-09 00:28:06 +000092 int32_t ok = _acm->Add10MsData(_audioFrame);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +000093 EXPECT_GE(ok, 0);
94 return ok >= 0 ? true : false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000095 }
96 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +000097}
98
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000099void Sender::Run() {
100 while (true) {
101 if (!Add10MsData()) {
102 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000103 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000104 }
105}
106
107Receiver::Receiver()
108 : _playoutLengthSmpls(WEBRTC_10MS_PCM_AUDIO),
109 _payloadSizeBytes(MAX_INCOMING_PAYLOAD) {
110}
111
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000112void Receiver::Setup(AudioCodingModule *acm, RTPStream *rtpStream,
Peter Kasting69558702016-01-12 16:26:35 -0800113 std::string out_file_name, size_t channels) {
henrike@webrtc.org6ac22e62014-08-11 21:06:30 +0000114 struct CodecInst recvCodec = CodecInst();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000115 int noOfCodecs;
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000116 EXPECT_EQ(0, acm->InitializeReceiver());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000117
118 noOfCodecs = acm->NumberOfCodecs();
119 for (int i = 0; i < noOfCodecs; i++) {
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000120 EXPECT_EQ(0, acm->Codec(i, &recvCodec));
121 if (recvCodec.channels == channels)
kwibergda2bf4e2016-10-24 13:47:09 -0700122 EXPECT_EQ(true, acm->RegisterReceiveCodec(recvCodec.pltype,
123 CodecInstToSdp(recvCodec)));
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000124 // Forces mono/stereo for Opus.
125 if (!strcmp(recvCodec.plname, "opus")) {
126 recvCodec.channels = channels;
kwibergda2bf4e2016-10-24 13:47:09 -0700127 EXPECT_EQ(true, acm->RegisterReceiveCodec(recvCodec.pltype,
128 CodecInstToSdp(recvCodec)));
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000129 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000130 }
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000131
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000132 int playSampFreq;
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000133 std::string file_name;
134 std::stringstream file_stream;
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000135 file_stream << webrtc::test::OutputPath() << out_file_name
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000136 << static_cast<int>(codeId) << ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000137 file_name = file_stream.str();
138 _rtpStream = rtpStream;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000139
Karl Wiberg88aee282018-06-14 13:12:05 +0200140 playSampFreq = 32000;
141 _pcmFile.Open(file_name, 32000, "wb+");
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000142
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000143 _realPayloadSizeBytes = 0;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000144 _playoutBuffer = new int16_t[WEBRTC_10MS_PCM_AUDIO];
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000145 _frequency = playSampFreq;
146 _acm = acm;
147 _firstTime = true;
148}
149
150void Receiver::Teardown() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000151 delete[] _playoutBuffer;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000152 _pcmFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000153}
154
155bool Receiver::IncomingPacket() {
156 if (!_rtpStream->EndOfFile()) {
157 if (_firstTime) {
158 _firstTime = false;
159 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload,
160 _payloadSizeBytes, &_nextTime);
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000161 if (_realPayloadSizeBytes == 0) {
162 if (_rtpStream->EndOfFile()) {
163 _firstTime = true;
164 return true;
165 } else {
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000166 return false;
167 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000168 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000169 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000170
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000171 EXPECT_EQ(0, _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes,
172 _rtpInfo));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000173 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload,
174 _payloadSizeBytes, &_nextTime);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000175 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
176 _firstTime = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000177 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000178 }
179 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000180}
181
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000182bool Receiver::PlayoutData() {
183 AudioFrame audioFrame;
henrik.lundind4ccb002016-05-17 12:21:55 -0700184 bool muted;
185 int32_t ok = _acm->PlayoutData10Ms(_frequency, &audioFrame, &muted);
186 if (muted) {
187 ADD_FAILURE();
188 return false;
189 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000190 EXPECT_EQ(0, ok);
191 if (ok < 0){
192 return false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000193 }
194 if (_playoutLengthSmpls == 0) {
195 return false;
196 }
yujo36b1a5f2017-06-12 12:45:32 -0700197 _pcmFile.Write10MsData(audioFrame.data(),
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000198 audioFrame.samples_per_channel_ * audioFrame.num_channels_);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000199 return true;
200}
201
202void Receiver::Run() {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000203 uint8_t counter500Ms = 50;
204 uint32_t clock = 0;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000205
206 while (counter500Ms > 0) {
207 if (clock == 0 || clock >= _nextTime) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000208 EXPECT_TRUE(IncomingPacket());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000209 if (clock == 0) {
210 clock = _nextTime;
211 }
212 }
213 if ((clock % 10) == 0) {
214 if (!PlayoutData()) {
215 clock++;
216 continue;
217 }
218 }
219 if (_rtpStream->EndOfFile()) {
220 counter500Ms--;
221 }
222 clock++;
223 }
224}
225
Karl Wiberg88aee282018-06-14 13:12:05 +0200226EncodeDecodeTest::EncodeDecodeTest(int test_mode) {
227 // There used to be different test modes. The only one still supported is the
228 // "autotest" mode.
229 RTC_CHECK_EQ(0, test_mode);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000230}
231
232void EncodeDecodeTest::Perform() {
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000233 int numCodecs = 1;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000234 int codePars[3]; // Frequency, packet size, rate.
235 int numPars[52]; // Number of codec parameters sets (freq, pacsize, rate)
236 // to test, for a given codec.
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000237
238 codePars[0] = 0;
239 codePars[1] = 0;
240 codePars[2] = 0;
241
Karl Wiberg5817d3d2018-04-06 10:06:42 +0200242 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
243 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000244 struct CodecInst sendCodecTmp;
tina.legrand@webrtc.org5b4f36d2012-06-01 14:51:28 +0000245 numCodecs = acm->NumberOfCodecs();
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000246
Karl Wiberg88aee282018-06-14 13:12:05 +0200247 for (int n = 0; n < numCodecs; n++) {
248 EXPECT_EQ(0, acm->Codec(n, &sendCodecTmp));
249 if (STR_CASE_CMP(sendCodecTmp.plname, "telephone-event") == 0) {
250 numPars[n] = 0;
251 } else if (STR_CASE_CMP(sendCodecTmp.plname, "cn") == 0) {
252 numPars[n] = 0;
253 } else if (STR_CASE_CMP(sendCodecTmp.plname, "red") == 0) {
254 numPars[n] = 0;
255 } else if (sendCodecTmp.channels == 2) {
256 numPars[n] = 0;
257 } else {
258 numPars[n] = 1;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000259 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000260 }
261
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000262 // Loop over all mono codecs:
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000263 for (int codeId = 0; codeId < numCodecs; codeId++) {
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000264 // Only encode using real mono encoders, not telephone-event and cng.
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000265 for (int loopPars = 1; loopPars <= numPars[codeId]; loopPars++) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000266 // Encode all data to file.
Karl Wiberg88aee282018-06-14 13:12:05 +0200267 std::string fileName = EncodeToFile(1, codeId, codePars);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000268
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000269 RTPFile rtpFile;
kjellander@webrtc.org5490c712011-12-21 13:34:18 +0000270 rtpFile.Open(fileName.c_str(), "rb");
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000271
272 _receiver.codeId = codeId;
273
274 rtpFile.ReadHeader();
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000275 _receiver.Setup(acm.get(), &rtpFile, "encodeDecode_out", 1);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000276 _receiver.Run();
277 _receiver.Teardown();
278 rtpFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000279 }
280 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000281}
282
pbos@webrtc.orgc86e45d2014-10-01 10:05:40 +0000283std::string EncodeDecodeTest::EncodeToFile(int fileType,
284 int codeId,
Karl Wiberg88aee282018-06-14 13:12:05 +0200285 int* codePars) {
Karl Wiberg5817d3d2018-04-06 10:06:42 +0200286 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
287 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000288 RTPFile rtpFile;
pbos@webrtc.orgc86e45d2014-10-01 10:05:40 +0000289 std::string fileName = webrtc::test::TempFilename(webrtc::test::OutputPath(),
290 "encode_decode_rtp");
kjellander@webrtc.org5490c712011-12-21 13:34:18 +0000291 rtpFile.Open(fileName.c_str(), "wb+");
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000292 rtpFile.WriteHeader();
293
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000294 // Store for auto_test and logging.
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000295 _sender.codeId = codeId;
296
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000297 _sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000, 1);
kwiberg1fd4a4a2015-11-03 11:20:50 -0800298 if (acm->SendCodec()) {
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000299 _sender.Run();
300 }
301 _sender.Teardown();
302 rtpFile.Close();
pbos@webrtc.orgc86e45d2014-10-01 10:05:40 +0000303
304 return fileName;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000305}
306
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000307} // namespace webrtc