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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
/*
* 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 <cstdint>
#include <map>
#include <memory>
#include <string>
#include "absl/strings/string_view.h"
#include "api/array_view.h"
#include "api/audio_codecs/audio_format.h"
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
#include "api/audio_codecs/builtin_audio_encoder_factory.h"
#include "api/environment/environment.h"
#include "api/environment/environment_factory.h"
#include "api/neteq/default_neteq_factory.h"
#include "api/neteq/neteq.h"
#include "api/units/timestamp.h"
#include "modules/audio_coding/include/audio_coding_module.h"
#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
#include "modules/audio_coding/test/RTPFile.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(nullptr), _pcmFile(), _audioFrame(), _packetization(nullptr) {}
void Sender::Setup(const Environment& env,
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 = 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()->Create(
env, format, {.payload_type = payload_type}));
_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(NetEq* neteq,
RTPStream* rtpStream,
absl::string_view out_file_name,
size_t channels,
int file_num) {
if (channels == 1) {
neteq->SetCodecs({{107, {"L16", 8000, 1}},
{108, {"L16", 16000, 1}},
{109, {"L16", 32000, 1}},
{0, {"PCMU", 8000, 1}},
{8, {"PCMA", 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);
neteq->SetCodecs({{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;
StringBuilder file_stream;
file_stream << 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;
_neteq = neteq;
_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_GE(
0, _neteq->InsertPacket(_rtpHeader,
ArrayView<const uint8_t>(_incomingPayload,
_realPayloadSizeBytes),
/*receive_time=*/Timestamp::Millis(_nextTime)));
_realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
_payloadSizeBytes, &_nextTime);
if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
_firstTime = true;
}
}
return true;
}
bool Receiver::PlayoutData() {
AudioFrame audioFrame;
bool muted;
int ok = _neteq->GetAudio(&audioFrame, &muted);
if (muted) {
ADD_FAILURE();
return false;
}
EXPECT_EQ(NetEq::kOK, ok);
if (ok < 0) {
return false;
}
if (_playoutLengthSmpls == 0) {
return false;
}
EXPECT_TRUE(_resampler_helper.MaybeResample(_frequency, &audioFrame));
_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}},
// TODO(bugs.webrtc.org/345525069): Either fix/enable or remove G722.
#if defined(__has_feature) && !__has_feature(undefined_behavior_sanitizer)
{9, {"G722", 8000, 1}},
#endif
};
const Environment env = CreateEnvironment();
int file_num = 0;
for (const auto& send_codec : send_codecs) {
RTPFile rtpFile;
std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create());
std::string fileName =
test::TempFilename(test::OutputPath(), "encode_decode_rtp");
rtpFile.Open(fileName.c_str(), "wb+");
rtpFile.WriteHeader();
Sender sender;
sender.Setup(env, 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();
std::unique_ptr<NetEq> neteq = DefaultNetEqFactory().Create(
env, NetEq::Config(), CreateBuiltinAudioDecoderFactory());
Receiver receiver;
receiver.Setup(neteq.get(), &rtpFile, "encodeDecode_out", 1, file_num);
receiver.Run();
receiver.Teardown();
rtpFile.Close();
file_num++;
}
}
} // namespace webrtc
|