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