1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
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
11 #include "modules/audio_coding/test/EncodeDecodeTest.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 #include <memory>
17
18 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
19 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
20 #include "modules/audio_coding/include/audio_coding_module.h"
21 #include "rtc_base/strings/string_builder.h"
22 #include "test/gtest.h"
23 #include "test/testsupport/file_utils.h"
24
25 namespace webrtc {
26
27 namespace {
28 // Buffer size for stereo 48 kHz audio.
29 constexpr size_t kWebRtc10MsPcmAudio = 960;
30
31 } // namespace
32
TestPacketization(RTPStream * rtpStream,uint16_t frequency)33 TestPacketization::TestPacketization(RTPStream* rtpStream, uint16_t frequency)
34 : _rtpStream(rtpStream), _frequency(frequency), _seqNo(0) {}
35
~TestPacketization()36 TestPacketization::~TestPacketization() {}
37
SendData(const AudioFrameType,const uint8_t payloadType,const uint32_t timeStamp,const uint8_t * payloadData,const size_t payloadSize,int64_t absolute_capture_timestamp_ms)38 int32_t TestPacketization::SendData(const AudioFrameType /* frameType */,
39 const uint8_t payloadType,
40 const uint32_t timeStamp,
41 const uint8_t* payloadData,
42 const size_t payloadSize,
43 int64_t absolute_capture_timestamp_ms) {
44 _rtpStream->Write(payloadType, timeStamp, _seqNo++, payloadData, payloadSize,
45 _frequency);
46 return 1;
47 }
48
Sender()49 Sender::Sender()
50 : _acm(NULL), _pcmFile(), _audioFrame(), _packetization(NULL) {}
51
Setup(AudioCodingModule * acm,RTPStream * rtpStream,std::string in_file_name,int in_sample_rate,int payload_type,SdpAudioFormat format)52 void Sender::Setup(AudioCodingModule* acm,
53 RTPStream* rtpStream,
54 std::string in_file_name,
55 int in_sample_rate,
56 int payload_type,
57 SdpAudioFormat format) {
58 // Open input file
59 const std::string file_name = webrtc::test::ResourcePath(in_file_name, "pcm");
60 _pcmFile.Open(file_name, in_sample_rate, "rb");
61 if (format.num_channels == 2) {
62 _pcmFile.ReadStereo(true);
63 }
64 // Set test length to 500 ms (50 blocks of 10 ms each).
65 _pcmFile.SetNum10MsBlocksToRead(50);
66 // Fast-forward 1 second (100 blocks) since the file starts with silence.
67 _pcmFile.FastForward(100);
68
69 acm->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
70 payload_type, format, absl::nullopt));
71 _packetization = new TestPacketization(rtpStream, format.clockrate_hz);
72 EXPECT_EQ(0, acm->RegisterTransportCallback(_packetization));
73
74 _acm = acm;
75 }
76
Teardown()77 void Sender::Teardown() {
78 _pcmFile.Close();
79 delete _packetization;
80 }
81
Add10MsData()82 bool Sender::Add10MsData() {
83 if (!_pcmFile.EndOfFile()) {
84 EXPECT_GT(_pcmFile.Read10MsData(_audioFrame), 0);
85 int32_t ok = _acm->Add10MsData(_audioFrame);
86 EXPECT_GE(ok, 0);
87 return ok >= 0 ? true : false;
88 }
89 return false;
90 }
91
Run()92 void Sender::Run() {
93 while (true) {
94 if (!Add10MsData()) {
95 break;
96 }
97 }
98 }
99
Receiver()100 Receiver::Receiver()
101 : _playoutLengthSmpls(kWebRtc10MsPcmAudio),
102 _payloadSizeBytes(MAX_INCOMING_PAYLOAD) {}
103
Setup(AudioCodingModule * acm,RTPStream * rtpStream,std::string out_file_name,size_t channels,int file_num)104 void Receiver::Setup(AudioCodingModule* acm,
105 RTPStream* rtpStream,
106 std::string out_file_name,
107 size_t channels,
108 int file_num) {
109 EXPECT_EQ(0, acm->InitializeReceiver());
110
111 if (channels == 1) {
112 acm->SetReceiveCodecs({{103, {"ISAC", 16000, 1}},
113 {104, {"ISAC", 32000, 1}},
114 {107, {"L16", 8000, 1}},
115 {108, {"L16", 16000, 1}},
116 {109, {"L16", 32000, 1}},
117 {0, {"PCMU", 8000, 1}},
118 {8, {"PCMA", 8000, 1}},
119 {102, {"ILBC", 8000, 1}},
120 {9, {"G722", 8000, 1}},
121 {120, {"OPUS", 48000, 2}},
122 {13, {"CN", 8000, 1}},
123 {98, {"CN", 16000, 1}},
124 {99, {"CN", 32000, 1}}});
125 } else {
126 ASSERT_EQ(channels, 2u);
127 acm->SetReceiveCodecs({{111, {"L16", 8000, 2}},
128 {112, {"L16", 16000, 2}},
129 {113, {"L16", 32000, 2}},
130 {110, {"PCMU", 8000, 2}},
131 {118, {"PCMA", 8000, 2}},
132 {119, {"G722", 8000, 2}},
133 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}});
134 }
135
136 int playSampFreq;
137 std::string file_name;
138 rtc::StringBuilder file_stream;
139 file_stream << webrtc::test::OutputPath() << out_file_name << file_num
140 << ".pcm";
141 file_name = file_stream.str();
142 _rtpStream = rtpStream;
143
144 playSampFreq = 32000;
145 _pcmFile.Open(file_name, 32000, "wb+");
146
147 _realPayloadSizeBytes = 0;
148 _playoutBuffer = new int16_t[kWebRtc10MsPcmAudio];
149 _frequency = playSampFreq;
150 _acm = acm;
151 _firstTime = true;
152 }
153
Teardown()154 void Receiver::Teardown() {
155 delete[] _playoutBuffer;
156 _pcmFile.Close();
157 }
158
IncomingPacket()159 bool Receiver::IncomingPacket() {
160 if (!_rtpStream->EndOfFile()) {
161 if (_firstTime) {
162 _firstTime = false;
163 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
164 _payloadSizeBytes, &_nextTime);
165 if (_realPayloadSizeBytes == 0) {
166 if (_rtpStream->EndOfFile()) {
167 _firstTime = true;
168 return true;
169 } else {
170 return false;
171 }
172 }
173 }
174
175 EXPECT_EQ(0, _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes,
176 _rtpHeader));
177 _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
178 _payloadSizeBytes, &_nextTime);
179 if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
180 _firstTime = true;
181 }
182 }
183 return true;
184 }
185
PlayoutData()186 bool Receiver::PlayoutData() {
187 AudioFrame audioFrame;
188 bool muted;
189 int32_t ok = _acm->PlayoutData10Ms(_frequency, &audioFrame, &muted);
190 if (muted) {
191 ADD_FAILURE();
192 return false;
193 }
194 EXPECT_EQ(0, ok);
195 if (ok < 0) {
196 return false;
197 }
198 if (_playoutLengthSmpls == 0) {
199 return false;
200 }
201 _pcmFile.Write10MsData(audioFrame.data(), audioFrame.samples_per_channel_ *
202 audioFrame.num_channels_);
203 return true;
204 }
205
Run()206 void Receiver::Run() {
207 uint8_t counter500Ms = 50;
208 uint32_t clock = 0;
209
210 while (counter500Ms > 0) {
211 if (clock == 0 || clock >= _nextTime) {
212 EXPECT_TRUE(IncomingPacket());
213 if (clock == 0) {
214 clock = _nextTime;
215 }
216 }
217 if ((clock % 10) == 0) {
218 if (!PlayoutData()) {
219 clock++;
220 continue;
221 }
222 }
223 if (_rtpStream->EndOfFile()) {
224 counter500Ms--;
225 }
226 clock++;
227 }
228 }
229
230 EncodeDecodeTest::EncodeDecodeTest() = default;
231
Perform()232 void EncodeDecodeTest::Perform() {
233 const std::map<int, SdpAudioFormat> send_codecs = {
234 {103, {"ISAC", 16000, 1}}, {104, {"ISAC", 32000, 1}},
235 {107, {"L16", 8000, 1}}, {108, {"L16", 16000, 1}},
236 {109, {"L16", 32000, 1}}, {0, {"PCMU", 8000, 1}},
237 {8, {"PCMA", 8000, 1}},
238 #ifdef WEBRTC_CODEC_ILBC
239 {102, {"ILBC", 8000, 1}},
240 #endif
241 {9, {"G722", 8000, 1}}};
242 int file_num = 0;
243 for (const auto& send_codec : send_codecs) {
244 RTPFile rtpFile;
245 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
246 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
247
248 std::string fileName = webrtc::test::TempFilename(
249 webrtc::test::OutputPath(), "encode_decode_rtp");
250 rtpFile.Open(fileName.c_str(), "wb+");
251 rtpFile.WriteHeader();
252 Sender sender;
253 sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000,
254 send_codec.first, send_codec.second);
255 sender.Run();
256 sender.Teardown();
257 rtpFile.Close();
258
259 rtpFile.Open(fileName.c_str(), "rb");
260 rtpFile.ReadHeader();
261 Receiver receiver;
262 receiver.Setup(acm.get(), &rtpFile, "encodeDecode_out", 1, file_num);
263 receiver.Run();
264 receiver.Teardown();
265 rtpFile.Close();
266
267 file_num++;
268 }
269 }
270
271 } // namespace webrtc
272