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