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
|
/*
* 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/TestRedFec.h"
#include <cstddef>
#include <cstdint>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "absl/strings/match.h"
#include "api/audio/audio_frame.h"
#include "api/audio_codecs/L16/audio_decoder_L16.h"
#include "api/audio_codecs/L16/audio_encoder_L16.h"
#include "api/audio_codecs/audio_decoder_factory_template.h"
#include "api/audio_codecs/audio_encoder.h"
#include "api/audio_codecs/audio_encoder_factory_template.h"
#include "api/audio_codecs/audio_format.h"
#include "api/audio_codecs/g711/audio_decoder_g711.h"
#include "api/audio_codecs/g711/audio_encoder_g711.h"
#include "api/audio_codecs/g722/audio_decoder_g722.h"
#include "api/audio_codecs/g722/audio_encoder_g722.h"
#include "api/audio_codecs/opus/audio_decoder_opus.h"
#include "api/audio_codecs/opus/audio_encoder_opus.h"
#include "api/environment/environment_factory.h"
#include "api/neteq/default_neteq_factory.h"
#include "api/neteq/neteq.h"
#include "common_audio/vad/include/vad.h"
#include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
#include "modules/audio_coding/codecs/red/audio_encoder_copy_red.h"
#include "modules/audio_coding/include/audio_coding_module.h"
#include "modules/audio_coding/test/Channel.h"
#include "rtc_base/strings/string_builder.h"
#include "test/gtest.h"
#include "test/testsupport/file_utils.h"
namespace webrtc {
TestRedFec::TestRedFec()
: env_(CreateEnvironment(&field_trials_)),
encoder_factory_(CreateAudioEncoderFactory<AudioEncoderG711,
AudioEncoderG722,
AudioEncoderL16,
AudioEncoderOpus>()),
decoder_factory_(CreateAudioDecoderFactory<AudioDecoderG711,
AudioDecoderG722,
AudioDecoderL16,
AudioDecoderOpus>()),
_acmA(AudioCodingModule::Create()),
_neteq(DefaultNetEqFactory().Create(env_,
NetEq::Config(),
decoder_factory_)),
_channelA2B(nullptr),
_testCntr(0) {}
TestRedFec::~TestRedFec() {
if (_channelA2B != nullptr) {
delete _channelA2B;
_channelA2B = nullptr;
}
}
void TestRedFec::Perform() {
const std::string file_name =
test::ResourcePath("audio_coding/testfile32kHz", "pcm");
_inFileA.Open(file_name, 32000, "rb");
// Create and connect the channel
_channelA2B = new Channel;
_acmA->RegisterTransportCallback(_channelA2B);
_channelA2B->RegisterReceiverNetEq(_neteq.get());
RegisterSendCodec(_acmA, {"L16", 8000, 1}, Vad::kVadAggressive, true);
OpenOutFile(_testCntr);
Run();
_outFileB.Close();
// Switch to another 8 kHz codec; RED should remain switched on.
RegisterSendCodec(_acmA, {"PCMU", 8000, 1}, Vad::kVadAggressive, true);
OpenOutFile(_testCntr);
Run();
_outFileB.Close();
// TODO(bugs.webrtc.org/345525069): Either fix/enable or remove G722.
#if defined(__has_feature) && !__has_feature(undefined_behavior_sanitizer)
// Switch to a 16 kHz codec; RED should be switched off.
RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false);
OpenOutFile(_testCntr);
RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false);
Run();
RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false);
Run();
_outFileB.Close();
_channelA2B->SetFECTestWithPacketLoss(true);
// Following tests are under packet losses.
// Switch to a 16 kHz codec; RED should be switched off.
RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false);
OpenOutFile(_testCntr);
Run();
_outFileB.Close();
#endif
RegisterSendCodec(_acmA, {"opus", 48000, 2}, std::nullopt, false);
// _channelA2B imposes 25% packet loss rate.
EXPECT_EQ(0, _acmA->SetPacketLossRate(25));
_acmA->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
EXPECT_EQ(true, (*enc)->SetFec(true));
});
OpenOutFile(_testCntr);
Run();
// Switch to L16 with RED.
RegisterSendCodec(_acmA, {"L16", 8000, 1}, std::nullopt, true);
Run();
// Switch to Opus again.
RegisterSendCodec(_acmA, {"opus", 48000, 2}, std::nullopt, false);
_acmA->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
EXPECT_EQ(true, (*enc)->SetFec(false));
});
Run();
_acmA->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
EXPECT_EQ(true, (*enc)->SetFec(true));
});
_outFileB.Close();
}
void TestRedFec::RegisterSendCodec(
const std::unique_ptr<AudioCodingModule>& acm,
const SdpAudioFormat& codec_format,
std::optional<Vad::Aggressiveness> vad_mode,
bool use_red) {
constexpr int payload_type = 17, cn_payload_type = 27, red_payload_type = 37;
auto encoder = encoder_factory_->Create(env_, codec_format,
{.payload_type = payload_type});
EXPECT_NE(encoder, nullptr);
std::map<int, SdpAudioFormat> receive_codecs = {{payload_type, codec_format}};
if (!absl::EqualsIgnoreCase(codec_format.name, "opus")) {
if (vad_mode.has_value()) {
AudioEncoderCngConfig config;
config.speech_encoder = std::move(encoder);
config.num_channels = 1;
config.payload_type = cn_payload_type;
config.vad_mode = vad_mode.value();
encoder = CreateComfortNoiseEncoder(std::move(config));
receive_codecs.emplace(std::make_pair(
cn_payload_type, SdpAudioFormat("CN", codec_format.clockrate_hz, 1)));
}
if (use_red) {
AudioEncoderCopyRed::Config config;
config.payload_type = red_payload_type;
config.speech_encoder = std::move(encoder);
encoder = std::make_unique<AudioEncoderCopyRed>(std::move(config),
field_trials_);
receive_codecs.emplace(
std::make_pair(red_payload_type,
SdpAudioFormat("red", codec_format.clockrate_hz, 1)));
}
}
acm->SetEncoder(std::move(encoder));
_neteq->SetCodecs(receive_codecs);
}
void TestRedFec::Run() {
AudioFrame audioFrame;
int32_t outFreqHzB = _outFileB.SamplingFrequency();
// Set test length to 500 ms (50 blocks of 10 ms each).
_inFileA.SetNum10MsBlocksToRead(50);
// Fast-forward 1 second (100 blocks) since the file starts with silence.
_inFileA.FastForward(100);
while (!_inFileA.EndOfFile()) {
EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
EXPECT_GE(_acmA->Add10MsData(audioFrame), 0);
bool muted;
EXPECT_EQ(NetEq::kOK, _neteq->GetAudio(&audioFrame, &muted));
EXPECT_TRUE(_resampler_helper.MaybeResample(outFreqHzB, &audioFrame));
ASSERT_FALSE(muted);
_outFileB.Write10MsData(audioFrame.data(), audioFrame.samples_per_channel_);
}
_inFileA.Rewind();
}
void TestRedFec::OpenOutFile(int16_t test_number) {
std::string file_name;
StringBuilder file_stream;
file_stream << test::OutputPath();
file_stream << "TestRedFec_outFile_";
file_stream << test_number << ".pcm";
file_name = file_stream.str();
_outFileB.Open(file_name, 16000, "wb");
}
} // namespace webrtc
|