blob: c961fe591a6077e56499ef8b3250856d7508c41a [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(
Niels Möller87e2d782019-03-07 10:18:23 +010036 const AudioFrameType /* frameType */,
37 const uint8_t payloadType,
38 const uint32_t timeStamp,
39 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,
Fredrik Solenberg657b2962018-12-05 10:30:25 +010055 std::string in_file_name, int in_sample_rate,
56 int payload_type, SdpAudioFormat format) {
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000057 // Open input file
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000058 const std::string file_name = webrtc::test::ResourcePath(in_file_name, "pcm");
Fredrik Solenberg657b2962018-12-05 10:30:25 +010059 _pcmFile.Open(file_name, in_sample_rate, "rb");
60 if (format.num_channels == 2) {
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000061 _pcmFile.ReadStereo(true);
62 }
Henrik Lundin4d682082015-12-10 16:24:39 +010063 // Set test length to 500 ms (50 blocks of 10 ms each).
64 _pcmFile.SetNum10MsBlocksToRead(50);
65 // Fast-forward 1 second (100 blocks) since the file starts with silence.
66 _pcmFile.FastForward(100);
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000067
Karl Wiberg658a5522018-08-15 15:20:49 +020068 acm->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
Fredrik Solenberg657b2962018-12-05 10:30:25 +010069 payload_type, format, absl::nullopt));
70 _packetization = new TestPacketization(rtpStream, format.clockrate_hz);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000071 EXPECT_EQ(0, acm->RegisterTransportCallback(_packetization));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000072
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000073 _acm = acm;
74}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000075
76void Sender::Teardown() {
77 _pcmFile.Close();
78 delete _packetization;
niklase@google.com470e71d2011-07-07 08:21:25 +000079}
80
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000081bool Sender::Add10MsData() {
82 if (!_pcmFile.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000083 EXPECT_GT(_pcmFile.Read10MsData(_audioFrame), 0);
pbos@webrtc.org0946a562013-04-09 00:28:06 +000084 int32_t ok = _acm->Add10MsData(_audioFrame);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +000085 EXPECT_GE(ok, 0);
86 return ok >= 0 ? true : false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000087 }
88 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +000089}
90
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000091void Sender::Run() {
92 while (true) {
93 if (!Add10MsData()) {
94 break;
niklase@google.com470e71d2011-07-07 08:21:25 +000095 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000096 }
97}
98
99Receiver::Receiver()
100 : _playoutLengthSmpls(WEBRTC_10MS_PCM_AUDIO),
101 _payloadSizeBytes(MAX_INCOMING_PAYLOAD) {
102}
103
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000104void Receiver::Setup(AudioCodingModule *acm, RTPStream *rtpStream,
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100105 std::string out_file_name, size_t channels, int file_num) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000106 EXPECT_EQ(0, acm->InitializeReceiver());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000107
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100108 if (channels == 1) {
109 acm->SetReceiveCodecs({{103, {"ISAC", 16000, 1}},
110 {104, {"ISAC", 32000, 1}},
111 {107, {"L16", 8000, 1}},
112 {108, {"L16", 16000, 1}},
113 {109, {"L16", 32000, 1}},
114 {0, {"PCMU", 8000, 1}},
115 {8, {"PCMA", 8000, 1}},
116 {102, {"ILBC", 8000, 1}},
117 {9, {"G722", 8000, 1}},
118 {120, {"OPUS", 48000, 2}},
119 {13, {"CN", 8000, 1}},
120 {98, {"CN", 16000, 1}},
121 {99, {"CN", 32000, 1}}});
122 } else {
123 ASSERT_EQ(channels, 2u);
124 acm->SetReceiveCodecs({{111, {"L16", 8000, 2}},
125 {112, {"L16", 16000, 2}},
126 {113, {"L16", 32000, 2}},
127 {110, {"PCMU", 8000, 2}},
128 {118, {"PCMA", 8000, 2}},
129 {119, {"G722", 8000, 2}},
130 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}});
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000131 }
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000132
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000133 int playSampFreq;
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000134 std::string file_name;
Jonas Olsson366a50c2018-09-06 13:41:30 +0200135 rtc::StringBuilder file_stream;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100136 file_stream << webrtc::test::OutputPath() << out_file_name << file_num
137 << ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000138 file_name = file_stream.str();
139 _rtpStream = rtpStream;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000140
Karl Wiberg88aee282018-06-14 13:12:05 +0200141 playSampFreq = 32000;
142 _pcmFile.Open(file_name, 32000, "wb+");
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000143
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000144 _realPayloadSizeBytes = 0;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000145 _playoutBuffer = new int16_t[WEBRTC_10MS_PCM_AUDIO];
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000146 _frequency = playSampFreq;
147 _acm = acm;
148 _firstTime = true;
149}
150
151void Receiver::Teardown() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000152 delete[] _playoutBuffer;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000153 _pcmFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000154}
155
156bool Receiver::IncomingPacket() {
157 if (!_rtpStream->EndOfFile()) {
158 if (_firstTime) {
159 _firstTime = false;
Niels Möllerbf474952019-02-18 12:00:06 +0100160 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000161 _payloadSizeBytes, &_nextTime);
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000162 if (_realPayloadSizeBytes == 0) {
163 if (_rtpStream->EndOfFile()) {
164 _firstTime = true;
165 return true;
166 } else {
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000167 return false;
168 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000169 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000170 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000172 EXPECT_EQ(0, _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes,
Niels Möllerbf474952019-02-18 12:00:06 +0100173 _rtpHeader));
174 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000175 _payloadSizeBytes, &_nextTime);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000176 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
177 _firstTime = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000178 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000179 }
180 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181}
182
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000183bool Receiver::PlayoutData() {
184 AudioFrame audioFrame;
henrik.lundind4ccb002016-05-17 12:21:55 -0700185 bool muted;
186 int32_t ok = _acm->PlayoutData10Ms(_frequency, &audioFrame, &muted);
187 if (muted) {
188 ADD_FAILURE();
189 return false;
190 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000191 EXPECT_EQ(0, ok);
192 if (ok < 0){
193 return false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000194 }
195 if (_playoutLengthSmpls == 0) {
196 return false;
197 }
yujo36b1a5f2017-06-12 12:45:32 -0700198 _pcmFile.Write10MsData(audioFrame.data(),
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000199 audioFrame.samples_per_channel_ * audioFrame.num_channels_);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000200 return true;
201}
202
203void Receiver::Run() {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000204 uint8_t counter500Ms = 50;
205 uint32_t clock = 0;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000206
207 while (counter500Ms > 0) {
208 if (clock == 0 || clock >= _nextTime) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000209 EXPECT_TRUE(IncomingPacket());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000210 if (clock == 0) {
211 clock = _nextTime;
212 }
213 }
214 if ((clock % 10) == 0) {
215 if (!PlayoutData()) {
216 clock++;
217 continue;
218 }
219 }
220 if (_rtpStream->EndOfFile()) {
221 counter500Ms--;
222 }
223 clock++;
224 }
225}
226
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100227EncodeDecodeTest::EncodeDecodeTest() = default;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000228
229void EncodeDecodeTest::Perform() {
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100230 const std::map<int, SdpAudioFormat> send_codecs = {{103, {"ISAC", 16000, 1}},
231 {104, {"ISAC", 32000, 1}},
232 {107, {"L16", 8000, 1}},
233 {108, {"L16", 16000, 1}},
234 {109, {"L16", 32000, 1}},
235 {0, {"PCMU", 8000, 1}},
236 {8, {"PCMA", 8000, 1}},
237#ifdef WEBRTC_CODEC_ILBC
238 {102, {"ILBC", 8000, 1}},
239#endif
240 {9, {"G722", 8000, 1}}};
241 int file_num = 0;
242 for (const auto& send_codec : send_codecs) {
243 RTPFile rtpFile;
244 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
245 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000246
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100247 std::string fileName = webrtc::test::TempFilename(
248 webrtc::test::OutputPath(), "encode_decode_rtp");
249 rtpFile.Open(fileName.c_str(), "wb+");
250 rtpFile.WriteHeader();
251 Sender sender;
252 sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000,
253 send_codec.first, send_codec.second);
254 sender.Run();
255 sender.Teardown();
256 rtpFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000257
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100258 rtpFile.Open(fileName.c_str(), "rb");
259 rtpFile.ReadHeader();
260 Receiver receiver;
261 receiver.Setup(acm.get(), &rtpFile, "encodeDecode_out", 1, file_num);
262 receiver.Run();
263 receiver.Teardown();
264 rtpFile.Close();
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000265
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100266 file_num++;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000267 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000268}
269
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000270} // namespace webrtc