blob: 6ba61868a95236e974be502a29bf4874b8f7f24f [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
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000011#include "webrtc/modules/audio_coding/main/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>
15#include <string.h>
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000016
17#include <sstream>
tina.legrand@webrtc.org5e7ca602012-06-12 07:16:24 +000018#include <string>
19
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000020#include "testing/gtest/include/gtest/gtest.h"
21#include "webrtc/common_types.h"
22#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
23#include "webrtc/modules/audio_coding/main/source/acm_common_defs.h"
24#include "webrtc/modules/audio_coding/main/test/utility.h"
25#include "webrtc/system_wrappers/interface/trace.h"
26#include "webrtc/test/testsupport/fileutils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000027
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000028namespace webrtc {
29
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000030TestPacketization::TestPacketization(RTPStream *rtpStream, uint16_t frequency)
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000031 : _rtpStream(rtpStream),
32 _frequency(frequency),
33 _seqNo(0) {
niklase@google.com470e71d2011-07-07 08:21:25 +000034}
35
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000036TestPacketization::~TestPacketization() {
37}
niklase@google.com470e71d2011-07-07 08:21:25 +000038
pbos@webrtc.org0946a562013-04-09 00:28:06 +000039int32_t TestPacketization::SendData(
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000040 const FrameType /* frameType */, const uint8_t payloadType,
41 const uint32_t timeStamp, const uint8_t* payloadData,
pbos@webrtc.org0946a562013-04-09 00:28:06 +000042 const uint16_t payloadSize,
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000043 const RTPFragmentationHeader* /* fragmentation */) {
44 _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()
50 : _acm(NULL),
51 _pcmFile(),
52 _audioFrame(),
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000053 _packetization(NULL) {
54}
55
56void Sender::Setup(AudioCodingModule *acm, RTPStream *rtpStream) {
57 acm->InitializeSender();
58 struct CodecInst sendCodec;
59 int noOfCodecs = acm->NumberOfCodecs();
60 int codecNo;
61
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000062 // Open input file
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000063 const std::string file_name = webrtc::test::ResourcePath(
64 "audio_coding/testfile32kHz", "pcm");
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000065 _pcmFile.Open(file_name, 32000, "rb");
66
67 // Set the codec for the current test.
68 if ((testMode == 0) || (testMode == 1)) {
69 // Set the codec id.
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000070 codecNo = codeId;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000071 } else {
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000072 // Choose codec on command line.
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000073 printf("List of supported codec.\n");
74 for (int n = 0; n < noOfCodecs; n++) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000075 EXPECT_EQ(0, acm->Codec(n, &sendCodec));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000076 printf("%d %s\n", n, sendCodec.plname);
niklase@google.com470e71d2011-07-07 08:21:25 +000077 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000078 printf("Choose your codec:");
79 ASSERT_GT(scanf("%d", &codecNo), 0);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000080 }
81
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000082 EXPECT_EQ(0, acm->Codec(codecNo, &sendCodec));
83 // Default number of channels is 2 for CELT, so we change to 1 in this test.
tina.legrand@webrtc.orgae1c4542012-03-12 08:41:30 +000084 if (!strcmp(sendCodec.plname, "CELT")) {
85 sendCodec.channels = 1;
86 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000087
88 EXPECT_EQ(0, acm->RegisterSendCodec(sendCodec));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000089 _packetization = new TestPacketization(rtpStream, sendCodec.plfreq);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000090 EXPECT_EQ(0, acm->RegisterTransportCallback(_packetization));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000091
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000092 _acm = acm;
93}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000094
95void Sender::Teardown() {
96 _pcmFile.Close();
97 delete _packetization;
niklase@google.com470e71d2011-07-07 08:21:25 +000098}
99
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000100bool Sender::Add10MsData() {
101 if (!_pcmFile.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000102 EXPECT_GT(_pcmFile.Read10MsData(_audioFrame), 0);
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000103 int32_t ok = _acm->Add10MsData(_audioFrame);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000104 EXPECT_EQ(0, ok);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000105 if (ok != 0) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000106 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 }
108 return true;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000109 }
110 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000111}
112
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000113void Sender::Run() {
114 while (true) {
115 if (!Add10MsData()) {
116 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000117 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000118 EXPECT_GT(_acm->Process(), -1);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000119 }
120}
121
122Receiver::Receiver()
123 : _playoutLengthSmpls(WEBRTC_10MS_PCM_AUDIO),
124 _payloadSizeBytes(MAX_INCOMING_PAYLOAD) {
125}
126
127void Receiver::Setup(AudioCodingModule *acm, RTPStream *rtpStream) {
128 struct CodecInst recvCodec;
129 int noOfCodecs;
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000130 EXPECT_EQ(0, acm->InitializeReceiver());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000131
132 noOfCodecs = acm->NumberOfCodecs();
133 for (int i = 0; i < noOfCodecs; i++) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000134 EXPECT_EQ(0, acm->Codec(static_cast<uint8_t>(i), &recvCodec));
135 EXPECT_EQ(0, acm->RegisterReceiveCodec(recvCodec));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000136 }
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000137
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000138 int playSampFreq;
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000139 std::string file_name;
140 std::stringstream file_stream;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000141 file_stream << webrtc::test::OutputPath() << "encodeDecode_out"
142 << static_cast<int>(codeId) << ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000143 file_name = file_stream.str();
144 _rtpStream = rtpStream;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000145
146 if (testMode == 1) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000147 playSampFreq = recvCodec.plfreq;
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000148 _pcmFile.Open(file_name, recvCodec.plfreq, "wb+");
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000149 } else if (testMode == 0) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000150 playSampFreq = 32000;
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000151 _pcmFile.Open(file_name, 32000, "wb+");
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000152 } else {
153 printf("\nValid output frequencies:\n");
154 printf("8000\n16000\n32000\n-1,");
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000155 printf("which means output frequency equal to received signal frequency");
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000156 printf("\n\nChoose output sampling frequency: ");
157 ASSERT_GT(scanf("%d", &playSampFreq), 0);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000158 file_name = webrtc::test::OutputPath() + "encodeDecode_out.pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000159 _pcmFile.Open(file_name, playSampFreq, "wb+");
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000160 }
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000161
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000162 _realPayloadSizeBytes = 0;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000163 _playoutBuffer = new int16_t[WEBRTC_10MS_PCM_AUDIO];
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000164 _frequency = playSampFreq;
165 _acm = acm;
166 _firstTime = true;
167}
168
169void Receiver::Teardown() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000170 delete[] _playoutBuffer;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000171 _pcmFile.Close();
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000172 if (testMode > 1) {
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000173 Trace::ReturnTrace();
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000174 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000175}
176
177bool Receiver::IncomingPacket() {
178 if (!_rtpStream->EndOfFile()) {
179 if (_firstTime) {
180 _firstTime = false;
181 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload,
182 _payloadSizeBytes, &_nextTime);
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000183 if (_realPayloadSizeBytes == 0) {
184 if (_rtpStream->EndOfFile()) {
185 _firstTime = true;
186 return true;
187 } else {
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000188 return false;
189 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000190 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000191 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000192
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000193 EXPECT_EQ(0, _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes,
194 _rtpInfo));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000195 _realPayloadSizeBytes = _rtpStream->Read(&_rtpInfo, _incomingPayload,
196 _payloadSizeBytes, &_nextTime);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000197 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
198 _firstTime = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000199 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000200 }
201 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000202}
203
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000204bool Receiver::PlayoutData() {
205 AudioFrame audioFrame;
206
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000207 int32_t ok =_acm->PlayoutData10Ms(_frequency, &audioFrame);
208 EXPECT_EQ(0, ok);
209 if (ok < 0){
210 return false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000211 }
212 if (_playoutLengthSmpls == 0) {
213 return false;
214 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000215 _pcmFile.Write10MsData(audioFrame.data_, audioFrame.samples_per_channel_);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000216 return true;
217}
218
219void Receiver::Run() {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000220 uint8_t counter500Ms = 50;
221 uint32_t clock = 0;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000222
223 while (counter500Ms > 0) {
224 if (clock == 0 || clock >= _nextTime) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000225 EXPECT_TRUE(IncomingPacket());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000226 if (clock == 0) {
227 clock = _nextTime;
228 }
229 }
230 if ((clock % 10) == 0) {
231 if (!PlayoutData()) {
232 clock++;
233 continue;
234 }
235 }
236 if (_rtpStream->EndOfFile()) {
237 counter500Ms--;
238 }
239 clock++;
240 }
241}
242
243EncodeDecodeTest::EncodeDecodeTest() {
244 _testMode = 2;
245 Trace::CreateTrace();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000246 Trace::SetTraceFile(
247 (webrtc::test::OutputPath() + "acm_encdec_trace.txt").c_str());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000248}
249
250EncodeDecodeTest::EncodeDecodeTest(int testMode) {
251 //testMode == 0 for autotest
252 //testMode == 1 for testing all codecs/parameters
253 //testMode > 1 for specific user-input test (as it was used before)
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000254 _testMode = testMode;
255 if (_testMode != 0) {
256 Trace::CreateTrace();
257 Trace::SetTraceFile(
258 (webrtc::test::OutputPath() + "acm_encdec_trace.txt").c_str());
259 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000260}
261
262void EncodeDecodeTest::Perform() {
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000263 int numCodecs = 1;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000264 int codePars[3]; // Frequency, packet size, rate.
265 int numPars[52]; // Number of codec parameters sets (freq, pacsize, rate)
266 // to test, for a given codec.
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000267
268 codePars[0] = 0;
269 codePars[1] = 0;
270 codePars[2] = 0;
271
tina.legrand@webrtc.org5b4f36d2012-06-01 14:51:28 +0000272 AudioCodingModule* acm = AudioCodingModule::Create(0);
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000273 struct CodecInst sendCodecTmp;
tina.legrand@webrtc.org5b4f36d2012-06-01 14:51:28 +0000274 numCodecs = acm->NumberOfCodecs();
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000275
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000276 if (_testMode != 2) {
277 for (int n = 0; n < numCodecs; n++) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000278 EXPECT_EQ(0, acm->Codec(n, &sendCodecTmp));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000279 if (STR_CASE_CMP(sendCodecTmp.plname, "telephone-event") == 0) {
280 numPars[n] = 0;
281 } else if (STR_CASE_CMP(sendCodecTmp.plname, "cn") == 0) {
282 numPars[n] = 0;
283 } else if (STR_CASE_CMP(sendCodecTmp.plname, "red") == 0) {
284 numPars[n] = 0;
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000285 } else if (sendCodecTmp.channels == 2) {
286 numPars[n] = 0;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000287 } else {
288 numPars[n] = 1;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000289 }
290 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000291 } else {
292 numCodecs = 1;
293 numPars[0] = 1;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000294 }
295
296 _receiver.testMode = _testMode;
297
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000298 // Loop over all mono codecs:
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000299 for (int codeId = 0; codeId < numCodecs; codeId++) {
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000300 // Only encode using real mono encoders, not telephone-event and cng.
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000301 for (int loopPars = 1; loopPars <= numPars[codeId]; loopPars++) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000302 // Encode all data to file.
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000303 EncodeToFile(1, codeId, codePars, _testMode);
304
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000305 RTPFile rtpFile;
kjellander@webrtc.org5490c712011-12-21 13:34:18 +0000306 std::string fileName = webrtc::test::OutputPath() + "outFile.rtp";
307 rtpFile.Open(fileName.c_str(), "rb");
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000308
309 _receiver.codeId = codeId;
310
311 rtpFile.ReadHeader();
312 _receiver.Setup(acm, &rtpFile);
313 _receiver.Run();
314 _receiver.Teardown();
315 rtpFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000316 }
317 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000318
tina.legrand@webrtc.org5b4f36d2012-06-01 14:51:28 +0000319 AudioCodingModule::Destroy(acm);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000320
321 // End tracing.
322 if (_testMode == 1) {
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000323 Trace::ReturnTrace();
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000324 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000325}
326
327void EncodeDecodeTest::EncodeToFile(int fileType, int codeId, int* codePars,
328 int testMode) {
tina.legrand@webrtc.org5b4f36d2012-06-01 14:51:28 +0000329 AudioCodingModule* acm = AudioCodingModule::Create(1);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000330 RTPFile rtpFile;
kjellander@webrtc.org5490c712011-12-21 13:34:18 +0000331 std::string fileName = webrtc::test::OutputPath() + "outFile.rtp";
332 rtpFile.Open(fileName.c_str(), "wb+");
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000333 rtpFile.WriteHeader();
334
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000335 // Store for auto_test and logging.
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000336 _sender.testMode = testMode;
337 _sender.codeId = codeId;
338
339 _sender.Setup(acm, &rtpFile);
340 struct CodecInst sendCodecInst;
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000341 if (acm->SendCodec(&sendCodecInst) >= 0) {
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000342 _sender.Run();
343 }
344 _sender.Teardown();
345 rtpFile.Close();
346 AudioCodingModule::Destroy(acm);
347}
348
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000349} // namespace webrtc