blob: a1c005cbba1f99e0d254d96e161d26b8a4b7fceb [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Yves Gerey665174f2018-06-19 15:03:05 +020016#include <memory>
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 Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/include/audio_coding_module.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020021#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "test/testsupport/file_utils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000024
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000025namespace webrtc {
26
Jonas Olssona4d87372019-07-05 19:08:33 +020027TestPacketization::TestPacketization(RTPStream* rtpStream, uint16_t frequency)
28 : _rtpStream(rtpStream), _frequency(frequency), _seqNo(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000029
Jonas Olssona4d87372019-07-05 19:08:33 +020030TestPacketization::~TestPacketization() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000031
Niels Möllerc35b6e62019-04-25 16:31:18 +020032int32_t TestPacketization::SendData(const AudioFrameType /* frameType */,
33 const uint8_t payloadType,
34 const uint32_t timeStamp,
35 const uint8_t* payloadData,
Minyue Liff0e4db2020-01-23 13:45:50 +010036 const size_t payloadSize,
37 int64_t absolute_capture_timestamp_ms) {
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000038 _rtpStream->Write(payloadType, timeStamp, _seqNo++, payloadData, payloadSize,
39 _frequency);
40 return 1;
41}
niklase@google.com470e71d2011-07-07 08:21:25 +000042
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000043Sender::Sender()
Jonas Olssona4d87372019-07-05 19:08:33 +020044 : _acm(NULL), _pcmFile(), _audioFrame(), _packetization(NULL) {}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000045
Jonas Olssona4d87372019-07-05 19:08:33 +020046void Sender::Setup(AudioCodingModule* acm,
47 RTPStream* rtpStream,
48 std::string in_file_name,
49 int in_sample_rate,
50 int payload_type,
51 SdpAudioFormat format) {
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000052 // Open input file
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000053 const std::string file_name = webrtc::test::ResourcePath(in_file_name, "pcm");
Fredrik Solenberg657b2962018-12-05 10:30:25 +010054 _pcmFile.Open(file_name, in_sample_rate, "rb");
55 if (format.num_channels == 2) {
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000056 _pcmFile.ReadStereo(true);
57 }
Henrik Lundin4d682082015-12-10 16:24:39 +010058 // Set test length to 500 ms (50 blocks of 10 ms each).
59 _pcmFile.SetNum10MsBlocksToRead(50);
60 // Fast-forward 1 second (100 blocks) since the file starts with silence.
61 _pcmFile.FastForward(100);
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000062
Karl Wiberg658a5522018-08-15 15:20:49 +020063 acm->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
Fredrik Solenberg657b2962018-12-05 10:30:25 +010064 payload_type, format, absl::nullopt));
65 _packetization = new TestPacketization(rtpStream, format.clockrate_hz);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000066 EXPECT_EQ(0, acm->RegisterTransportCallback(_packetization));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000067
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000068 _acm = acm;
69}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000070
71void Sender::Teardown() {
72 _pcmFile.Close();
73 delete _packetization;
niklase@google.com470e71d2011-07-07 08:21:25 +000074}
75
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000076bool Sender::Add10MsData() {
77 if (!_pcmFile.EndOfFile()) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000078 EXPECT_GT(_pcmFile.Read10MsData(_audioFrame), 0);
pbos@webrtc.org0946a562013-04-09 00:28:06 +000079 int32_t ok = _acm->Add10MsData(_audioFrame);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +000080 EXPECT_GE(ok, 0);
81 return ok >= 0 ? true : false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000082 }
83 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +000084}
85
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000086void Sender::Run() {
87 while (true) {
88 if (!Add10MsData()) {
89 break;
niklase@google.com470e71d2011-07-07 08:21:25 +000090 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000091 }
92}
93
94Receiver::Receiver()
95 : _playoutLengthSmpls(WEBRTC_10MS_PCM_AUDIO),
Jonas Olssona4d87372019-07-05 19:08:33 +020096 _payloadSizeBytes(MAX_INCOMING_PAYLOAD) {}
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000097
Jonas Olssona4d87372019-07-05 19:08:33 +020098void Receiver::Setup(AudioCodingModule* acm,
99 RTPStream* rtpStream,
100 std::string out_file_name,
101 size_t channels,
102 int file_num) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000103 EXPECT_EQ(0, acm->InitializeReceiver());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000104
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100105 if (channels == 1) {
106 acm->SetReceiveCodecs({{103, {"ISAC", 16000, 1}},
107 {104, {"ISAC", 32000, 1}},
108 {107, {"L16", 8000, 1}},
109 {108, {"L16", 16000, 1}},
110 {109, {"L16", 32000, 1}},
111 {0, {"PCMU", 8000, 1}},
112 {8, {"PCMA", 8000, 1}},
113 {102, {"ILBC", 8000, 1}},
114 {9, {"G722", 8000, 1}},
115 {120, {"OPUS", 48000, 2}},
116 {13, {"CN", 8000, 1}},
117 {98, {"CN", 16000, 1}},
118 {99, {"CN", 32000, 1}}});
119 } else {
120 ASSERT_EQ(channels, 2u);
121 acm->SetReceiveCodecs({{111, {"L16", 8000, 2}},
122 {112, {"L16", 16000, 2}},
123 {113, {"L16", 32000, 2}},
124 {110, {"PCMU", 8000, 2}},
125 {118, {"PCMA", 8000, 2}},
126 {119, {"G722", 8000, 2}},
127 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}});
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000128 }
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000129
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000130 int playSampFreq;
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000131 std::string file_name;
Jonas Olsson366a50c2018-09-06 13:41:30 +0200132 rtc::StringBuilder file_stream;
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100133 file_stream << webrtc::test::OutputPath() << out_file_name << file_num
134 << ".pcm";
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000135 file_name = file_stream.str();
136 _rtpStream = rtpStream;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000137
Karl Wiberg88aee282018-06-14 13:12:05 +0200138 playSampFreq = 32000;
139 _pcmFile.Open(file_name, 32000, "wb+");
phoglund@webrtc.orgd1a860b2012-01-26 14:49:28 +0000140
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000141 _realPayloadSizeBytes = 0;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000142 _playoutBuffer = new int16_t[WEBRTC_10MS_PCM_AUDIO];
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000143 _frequency = playSampFreq;
144 _acm = acm;
145 _firstTime = true;
146}
147
148void Receiver::Teardown() {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000149 delete[] _playoutBuffer;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000150 _pcmFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000151}
152
153bool Receiver::IncomingPacket() {
154 if (!_rtpStream->EndOfFile()) {
155 if (_firstTime) {
156 _firstTime = false;
Niels Möllerbf474952019-02-18 12:00:06 +0100157 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000158 _payloadSizeBytes, &_nextTime);
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000159 if (_realPayloadSizeBytes == 0) {
160 if (_rtpStream->EndOfFile()) {
161 _firstTime = true;
162 return true;
163 } else {
andrew@webrtc.org975e4a32012-01-17 19:27:33 +0000164 return false;
165 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000166 }
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000167 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000168
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000169 EXPECT_EQ(0, _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes,
Niels Möllerbf474952019-02-18 12:00:06 +0100170 _rtpHeader));
171 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000172 _payloadSizeBytes, &_nextTime);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000173 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
174 _firstTime = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000175 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000176 }
177 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000178}
179
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000180bool Receiver::PlayoutData() {
181 AudioFrame audioFrame;
henrik.lundind4ccb002016-05-17 12:21:55 -0700182 bool muted;
183 int32_t ok = _acm->PlayoutData10Ms(_frequency, &audioFrame, &muted);
184 if (muted) {
185 ADD_FAILURE();
186 return false;
187 }
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000188 EXPECT_EQ(0, ok);
Jonas Olssona4d87372019-07-05 19:08:33 +0200189 if (ok < 0) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000190 return false;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000191 }
192 if (_playoutLengthSmpls == 0) {
193 return false;
194 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200195 _pcmFile.Write10MsData(audioFrame.data(), audioFrame.samples_per_channel_ *
196 audioFrame.num_channels_);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000197 return true;
198}
199
200void Receiver::Run() {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000201 uint8_t counter500Ms = 50;
202 uint32_t clock = 0;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000203
204 while (counter500Ms > 0) {
205 if (clock == 0 || clock >= _nextTime) {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +0000206 EXPECT_TRUE(IncomingPacket());
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000207 if (clock == 0) {
208 clock = _nextTime;
209 }
210 }
211 if ((clock % 10) == 0) {
212 if (!PlayoutData()) {
213 clock++;
214 continue;
215 }
216 }
217 if (_rtpStream->EndOfFile()) {
218 counter500Ms--;
219 }
220 clock++;
221 }
222}
223
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100224EncodeDecodeTest::EncodeDecodeTest() = default;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000225
226void EncodeDecodeTest::Perform() {
Jonas Olssona4d87372019-07-05 19:08:33 +0200227 const std::map<int, SdpAudioFormat> send_codecs = {
228 {103, {"ISAC", 16000, 1}}, {104, {"ISAC", 32000, 1}},
229 {107, {"L16", 8000, 1}}, {108, {"L16", 16000, 1}},
230 {109, {"L16", 32000, 1}}, {0, {"PCMU", 8000, 1}},
231 {8, {"PCMA", 8000, 1}},
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100232#ifdef WEBRTC_CODEC_ILBC
Jonas Olssona4d87372019-07-05 19:08:33 +0200233 {102, {"ILBC", 8000, 1}},
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100234#endif
Jonas Olssona4d87372019-07-05 19:08:33 +0200235 {9, {"G722", 8000, 1}}};
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100236 int file_num = 0;
237 for (const auto& send_codec : send_codecs) {
238 RTPFile rtpFile;
239 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
240 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000241
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100242 std::string fileName = webrtc::test::TempFilename(
243 webrtc::test::OutputPath(), "encode_decode_rtp");
244 rtpFile.Open(fileName.c_str(), "wb+");
245 rtpFile.WriteHeader();
246 Sender sender;
247 sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000,
248 send_codec.first, send_codec.second);
249 sender.Run();
250 sender.Teardown();
251 rtpFile.Close();
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000252
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100253 rtpFile.Open(fileName.c_str(), "rb");
254 rtpFile.ReadHeader();
255 Receiver receiver;
256 receiver.Setup(acm.get(), &rtpFile, "encodeDecode_out", 1, file_num);
257 receiver.Run();
258 receiver.Teardown();
259 rtpFile.Close();
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000260
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100261 file_num++;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000262 }
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000263}
264
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000265} // namespace webrtc