Provide Environment to consturct AudioDecoder in tests

Bug: webrtc:356878416
Change-Id: Id2803736d06445b536f2ced02509eaaaf8fd804c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/359361
Reviewed-by: Florent Castelli <orphis@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Jakob Ivarsson‎ <jakobi@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#42792}
This commit is contained in:
Danil Chapovalov 2024-08-14 19:17:05 +02:00 committed by WebRTC LUCI CQ
parent aa9e557c81
commit e0fe4200eb
10 changed files with 138 additions and 162 deletions

View File

@ -46,9 +46,10 @@ class AudioDecoderFactory : public RefCountInterface {
// Note: Implementations need to be robust against combinations other than
// one encoder, one decoder getting the same ID; such decoders must still
// work.
virtual std::unique_ptr<AudioDecoder> MakeAudioDecoder(
const SdpAudioFormat& format,
absl::optional<AudioCodecPairId> codec_pair_id) {
[[deprecated("bugs.webrtc.org/356878416 - Use `Create` instead")]] //
virtual std::unique_ptr<AudioDecoder>
MakeAudioDecoder(const SdpAudioFormat& format,
absl::optional<AudioCodecPairId> codec_pair_id) {
RTC_DCHECK_NOTREACHED();
return nullptr;
}
@ -59,7 +60,10 @@ class AudioDecoderFactory : public RefCountInterface {
const Environment& env,
const SdpAudioFormat& format,
absl::optional<AudioCodecPairId> codec_pair_id) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
return MakeAudioDecoder(format, codec_pair_id);
#pragma clang diagnostic pop
}
};

View File

@ -24,6 +24,8 @@
#include "api/audio_codecs/g722/audio_decoder_g722.h"
#include "api/audio_codecs/ilbc/audio_decoder_ilbc.h"
#include "api/audio_codecs/opus/audio_decoder_opus.h"
#include "api/environment/environment.h"
#include "api/environment/environment_factory.h"
#include "api/make_ref_counted.h"
#include "api/scoped_refptr.h"
#include "test/gmock.h"
@ -84,30 +86,31 @@ struct AudioDecoderFakeApi {
} // namespace
TEST(AudioDecoderFactoryTemplateTest, NoDecoderTypes) {
const Environment env = CreateEnvironment();
rtc::scoped_refptr<AudioDecoderFactory> factory(
rtc::make_ref_counted<
audio_decoder_factory_template_impl::AudioDecoderFactoryT<>>());
EXPECT_THAT(factory->GetSupportedDecoders(), ::testing::IsEmpty());
EXPECT_FALSE(factory->IsSupportedDecoder({"foo", 8000, 1}));
EXPECT_EQ(nullptr,
factory->MakeAudioDecoder({"bar", 16000, 1}, absl::nullopt));
EXPECT_EQ(nullptr, factory->Create(env, {"bar", 16000, 1}, absl::nullopt));
}
TEST(AudioDecoderFactoryTemplateTest, OneDecoderType) {
const Environment env = CreateEnvironment();
auto factory = CreateAudioDecoderFactory<AudioDecoderFakeApi<BogusParams>>();
EXPECT_THAT(factory->GetSupportedDecoders(),
::testing::ElementsAre(
AudioCodecSpec{{"bogus", 8000, 1}, {8000, 1, 12345}}));
EXPECT_FALSE(factory->IsSupportedDecoder({"foo", 8000, 1}));
EXPECT_TRUE(factory->IsSupportedDecoder({"bogus", 8000, 1}));
EXPECT_EQ(nullptr,
factory->MakeAudioDecoder({"bar", 16000, 1}, absl::nullopt));
auto dec = factory->MakeAudioDecoder({"bogus", 8000, 1}, absl::nullopt);
EXPECT_EQ(nullptr, factory->Create(env, {"bar", 16000, 1}, absl::nullopt));
auto dec = factory->Create(env, {"bogus", 8000, 1}, absl::nullopt);
ASSERT_NE(nullptr, dec);
EXPECT_EQ(8000, dec->SampleRateHz());
}
TEST(AudioDecoderFactoryTemplateTest, TwoDecoderTypes) {
const Environment env = CreateEnvironment();
auto factory = CreateAudioDecoderFactory<AudioDecoderFakeApi<BogusParams>,
AudioDecoderFakeApi<ShamParams>>();
EXPECT_THAT(factory->GetSupportedDecoders(),
@ -119,20 +122,19 @@ TEST(AudioDecoderFactoryTemplateTest, TwoDecoderTypes) {
EXPECT_TRUE(factory->IsSupportedDecoder({"bogus", 8000, 1}));
EXPECT_TRUE(
factory->IsSupportedDecoder({"sham", 16000, 2, {{"param", "value"}}}));
EXPECT_EQ(nullptr,
factory->MakeAudioDecoder({"bar", 16000, 1}, absl::nullopt));
auto dec1 = factory->MakeAudioDecoder({"bogus", 8000, 1}, absl::nullopt);
EXPECT_EQ(nullptr, factory->Create(env, {"bar", 16000, 1}, absl::nullopt));
auto dec1 = factory->Create(env, {"bogus", 8000, 1}, absl::nullopt);
ASSERT_NE(nullptr, dec1);
EXPECT_EQ(8000, dec1->SampleRateHz());
EXPECT_EQ(nullptr,
factory->MakeAudioDecoder({"sham", 16000, 2}, absl::nullopt));
auto dec2 = factory->MakeAudioDecoder(
{"sham", 16000, 2, {{"param", "value"}}}, absl::nullopt);
EXPECT_EQ(nullptr, factory->Create(env, {"sham", 16000, 2}, absl::nullopt));
auto dec2 = factory->Create(env, {"sham", 16000, 2, {{"param", "value"}}},
absl::nullopt);
ASSERT_NE(nullptr, dec2);
EXPECT_EQ(16000, dec2->SampleRateHz());
}
TEST(AudioDecoderFactoryTemplateTest, G711) {
const Environment env = CreateEnvironment();
auto factory = CreateAudioDecoderFactory<AudioDecoderG711>();
EXPECT_THAT(factory->GetSupportedDecoders(),
::testing::ElementsAre(
@ -141,52 +143,52 @@ TEST(AudioDecoderFactoryTemplateTest, G711) {
EXPECT_FALSE(factory->IsSupportedDecoder({"G711", 8000, 1}));
EXPECT_TRUE(factory->IsSupportedDecoder({"PCMU", 8000, 1}));
EXPECT_TRUE(factory->IsSupportedDecoder({"pcma", 8000, 1}));
EXPECT_EQ(nullptr,
factory->MakeAudioDecoder({"pcmu", 16000, 1}, absl::nullopt));
auto dec1 = factory->MakeAudioDecoder({"pcmu", 8000, 1}, absl::nullopt);
EXPECT_EQ(nullptr, factory->Create(env, {"pcmu", 16000, 1}, absl::nullopt));
auto dec1 = factory->Create(env, {"pcmu", 8000, 1}, absl::nullopt);
ASSERT_NE(nullptr, dec1);
EXPECT_EQ(8000, dec1->SampleRateHz());
auto dec2 = factory->MakeAudioDecoder({"PCMA", 8000, 1}, absl::nullopt);
auto dec2 = factory->Create(env, {"PCMA", 8000, 1}, absl::nullopt);
ASSERT_NE(nullptr, dec2);
EXPECT_EQ(8000, dec2->SampleRateHz());
}
TEST(AudioDecoderFactoryTemplateTest, G722) {
const Environment env = CreateEnvironment();
auto factory = CreateAudioDecoderFactory<AudioDecoderG722>();
EXPECT_THAT(factory->GetSupportedDecoders(),
::testing::ElementsAre(
AudioCodecSpec{{"G722", 8000, 1}, {16000, 1, 64000}}));
EXPECT_FALSE(factory->IsSupportedDecoder({"foo", 8000, 1}));
EXPECT_TRUE(factory->IsSupportedDecoder({"G722", 8000, 1}));
EXPECT_EQ(nullptr,
factory->MakeAudioDecoder({"bar", 16000, 1}, absl::nullopt));
auto dec1 = factory->MakeAudioDecoder({"G722", 8000, 1}, absl::nullopt);
EXPECT_EQ(nullptr, factory->Create(env, {"bar", 16000, 1}, absl::nullopt));
auto dec1 = factory->Create(env, {"G722", 8000, 1}, absl::nullopt);
ASSERT_NE(nullptr, dec1);
EXPECT_EQ(16000, dec1->SampleRateHz());
EXPECT_EQ(1u, dec1->Channels());
auto dec2 = factory->MakeAudioDecoder({"G722", 8000, 2}, absl::nullopt);
auto dec2 = factory->Create(env, {"G722", 8000, 2}, absl::nullopt);
ASSERT_NE(nullptr, dec2);
EXPECT_EQ(16000, dec2->SampleRateHz());
EXPECT_EQ(2u, dec2->Channels());
auto dec3 = factory->MakeAudioDecoder({"G722", 8000, 3}, absl::nullopt);
auto dec3 = factory->Create(env, {"G722", 8000, 3}, absl::nullopt);
ASSERT_EQ(nullptr, dec3);
}
TEST(AudioDecoderFactoryTemplateTest, Ilbc) {
const Environment env = CreateEnvironment();
auto factory = CreateAudioDecoderFactory<AudioDecoderIlbc>();
EXPECT_THAT(factory->GetSupportedDecoders(),
::testing::ElementsAre(
AudioCodecSpec{{"ILBC", 8000, 1}, {8000, 1, 13300}}));
EXPECT_FALSE(factory->IsSupportedDecoder({"foo", 8000, 1}));
EXPECT_TRUE(factory->IsSupportedDecoder({"ilbc", 8000, 1}));
EXPECT_EQ(nullptr,
factory->MakeAudioDecoder({"bar", 8000, 1}, absl::nullopt));
auto dec = factory->MakeAudioDecoder({"ilbc", 8000, 1}, absl::nullopt);
EXPECT_EQ(nullptr, factory->Create(env, {"bar", 8000, 1}, absl::nullopt));
auto dec = factory->Create(env, {"ilbc", 8000, 1}, absl::nullopt);
ASSERT_NE(nullptr, dec);
EXPECT_EQ(8000, dec->SampleRateHz());
}
TEST(AudioDecoderFactoryTemplateTest, L16) {
const Environment env = CreateEnvironment();
auto factory = CreateAudioDecoderFactory<AudioDecoderL16>();
EXPECT_THAT(
factory->GetSupportedDecoders(),
@ -200,14 +202,14 @@ TEST(AudioDecoderFactoryTemplateTest, L16) {
EXPECT_FALSE(factory->IsSupportedDecoder({"foo", 8000, 1}));
EXPECT_TRUE(factory->IsSupportedDecoder({"L16", 48000, 1}));
EXPECT_FALSE(factory->IsSupportedDecoder({"L16", 96000, 1}));
EXPECT_EQ(nullptr,
factory->MakeAudioDecoder({"L16", 8000, 0}, absl::nullopt));
auto dec = factory->MakeAudioDecoder({"L16", 48000, 2}, absl::nullopt);
EXPECT_EQ(nullptr, factory->Create(env, {"L16", 8000, 0}, absl::nullopt));
auto dec = factory->Create(env, {"L16", 48000, 2}, absl::nullopt);
ASSERT_NE(nullptr, dec);
EXPECT_EQ(48000, dec->SampleRateHz());
}
TEST(AudioDecoderFactoryTemplateTest, Opus) {
const Environment env = CreateEnvironment();
auto factory = CreateAudioDecoderFactory<AudioDecoderOpus>();
AudioCodecInfo opus_info{48000, 1, 64000, 6000, 510000};
opus_info.allow_comfort_noise = false;
@ -218,9 +220,8 @@ TEST(AudioDecoderFactoryTemplateTest, Opus) {
::testing::ElementsAre(AudioCodecSpec{opus_format, opus_info}));
EXPECT_FALSE(factory->IsSupportedDecoder({"opus", 48000, 1}));
EXPECT_TRUE(factory->IsSupportedDecoder({"opus", 48000, 2}));
EXPECT_EQ(nullptr,
factory->MakeAudioDecoder({"bar", 16000, 1}, absl::nullopt));
auto dec = factory->MakeAudioDecoder({"opus", 48000, 2}, absl::nullopt);
EXPECT_EQ(nullptr, factory->Create(env, {"bar", 16000, 1}, absl::nullopt));
auto dec = factory->Create(env, {"opus", 48000, 2}, absl::nullopt);
ASSERT_NE(nullptr, dec);
EXPECT_EQ(48000, dec->SampleRateHz());
}

View File

@ -12,70 +12,69 @@
#include <memory>
#include "api/environment/environment.h"
#include "api/environment/environment_factory.h"
#include "test/gtest.h"
namespace webrtc {
TEST(AudioDecoderFactoryTest, CreateUnknownDecoder) {
const Environment env = CreateEnvironment();
rtc::scoped_refptr<AudioDecoderFactory> adf =
CreateBuiltinAudioDecoderFactory();
ASSERT_TRUE(adf);
EXPECT_FALSE(
adf->MakeAudioDecoder(SdpAudioFormat("rey", 8000, 1), absl::nullopt));
EXPECT_FALSE(adf->Create(env, SdpAudioFormat("rey", 8000, 1), absl::nullopt));
}
TEST(AudioDecoderFactoryTest, CreatePcmu) {
const Environment env = CreateEnvironment();
rtc::scoped_refptr<AudioDecoderFactory> adf =
CreateBuiltinAudioDecoderFactory();
ASSERT_TRUE(adf);
// PCMu supports 8 kHz, and any number of channels.
EXPECT_FALSE(
adf->MakeAudioDecoder(SdpAudioFormat("pcmu", 8000, 0), absl::nullopt));
EXPECT_TRUE(
adf->MakeAudioDecoder(SdpAudioFormat("pcmu", 8000, 1), absl::nullopt));
EXPECT_TRUE(
adf->MakeAudioDecoder(SdpAudioFormat("pcmu", 8000, 2), absl::nullopt));
EXPECT_TRUE(
adf->MakeAudioDecoder(SdpAudioFormat("pcmu", 8000, 3), absl::nullopt));
adf->Create(env, SdpAudioFormat("pcmu", 8000, 0), absl::nullopt));
EXPECT_TRUE(adf->Create(env, SdpAudioFormat("pcmu", 8000, 1), absl::nullopt));
EXPECT_TRUE(adf->Create(env, SdpAudioFormat("pcmu", 8000, 2), absl::nullopt));
EXPECT_TRUE(adf->Create(env, SdpAudioFormat("pcmu", 8000, 3), absl::nullopt));
EXPECT_FALSE(
adf->MakeAudioDecoder(SdpAudioFormat("pcmu", 16000, 1), absl::nullopt));
adf->Create(env, SdpAudioFormat("pcmu", 16000, 1), absl::nullopt));
}
TEST(AudioDecoderFactoryTest, CreatePcma) {
const Environment env = CreateEnvironment();
rtc::scoped_refptr<AudioDecoderFactory> adf =
CreateBuiltinAudioDecoderFactory();
ASSERT_TRUE(adf);
// PCMa supports 8 kHz, and any number of channels.
EXPECT_FALSE(
adf->MakeAudioDecoder(SdpAudioFormat("pcma", 8000, 0), absl::nullopt));
EXPECT_TRUE(
adf->MakeAudioDecoder(SdpAudioFormat("pcma", 8000, 1), absl::nullopt));
EXPECT_TRUE(
adf->MakeAudioDecoder(SdpAudioFormat("pcma", 8000, 2), absl::nullopt));
EXPECT_TRUE(
adf->MakeAudioDecoder(SdpAudioFormat("pcma", 8000, 3), absl::nullopt));
adf->Create(env, SdpAudioFormat("pcma", 8000, 0), absl::nullopt));
EXPECT_TRUE(adf->Create(env, SdpAudioFormat("pcma", 8000, 1), absl::nullopt));
EXPECT_TRUE(adf->Create(env, SdpAudioFormat("pcma", 8000, 2), absl::nullopt));
EXPECT_TRUE(adf->Create(env, SdpAudioFormat("pcma", 8000, 3), absl::nullopt));
EXPECT_FALSE(
adf->MakeAudioDecoder(SdpAudioFormat("pcma", 16000, 1), absl::nullopt));
adf->Create(env, SdpAudioFormat("pcma", 16000, 1), absl::nullopt));
}
TEST(AudioDecoderFactoryTest, CreateIlbc) {
const Environment env = CreateEnvironment();
rtc::scoped_refptr<AudioDecoderFactory> adf =
CreateBuiltinAudioDecoderFactory();
ASSERT_TRUE(adf);
// iLBC supports 8 kHz, 1 channel.
EXPECT_FALSE(
adf->MakeAudioDecoder(SdpAudioFormat("ilbc", 8000, 0), absl::nullopt));
adf->Create(env, SdpAudioFormat("ilbc", 8000, 0), absl::nullopt));
#ifdef WEBRTC_CODEC_ILBC
EXPECT_TRUE(
adf->MakeAudioDecoder(SdpAudioFormat("ilbc", 8000, 1), absl::nullopt));
EXPECT_TRUE(adf->Create(env, SdpAudioFormat("ilbc", 8000, 1), absl::nullopt));
#endif
EXPECT_FALSE(
adf->MakeAudioDecoder(SdpAudioFormat("ilbc", 8000, 2), absl::nullopt));
adf->Create(env, SdpAudioFormat("ilbc", 8000, 2), absl::nullopt));
EXPECT_FALSE(
adf->MakeAudioDecoder(SdpAudioFormat("ilbc", 16000, 1), absl::nullopt));
adf->Create(env, SdpAudioFormat("ilbc", 16000, 1), absl::nullopt));
}
TEST(AudioDecoderFactoryTest, CreateL16) {
const Environment env = CreateEnvironment();
rtc::scoped_refptr<AudioDecoderFactory> adf =
CreateBuiltinAudioDecoderFactory();
ASSERT_TRUE(adf);
@ -83,17 +82,18 @@ TEST(AudioDecoderFactoryTest, CreateL16) {
const int clockrates[] = {8000, 16000, 32000, 48000};
const int num_channels[] = {1, 2, 3, 24};
for (int clockrate : clockrates) {
EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("l16", clockrate, 0),
absl::nullopt));
EXPECT_FALSE(
adf->Create(env, SdpAudioFormat("l16", clockrate, 0), absl::nullopt));
for (int channels : num_channels) {
EXPECT_TRUE(adf->MakeAudioDecoder(
SdpAudioFormat("l16", clockrate, channels), absl::nullopt));
EXPECT_TRUE(adf->Create(env, SdpAudioFormat("l16", clockrate, channels),
absl::nullopt));
}
}
}
// Tests that using more channels than the maximum does not work
TEST(AudioDecoderFactoryTest, MaxNrOfChannels) {
const Environment env = CreateEnvironment();
rtc::scoped_refptr<AudioDecoderFactory> adf =
CreateBuiltinAudioDecoderFactory();
std::vector<std::string> codecs = {
@ -107,37 +107,38 @@ TEST(AudioDecoderFactoryTest, MaxNrOfChannels) {
};
for (auto codec : codecs) {
EXPECT_FALSE(adf->MakeAudioDecoder(
EXPECT_FALSE(adf->Create(
env,
SdpAudioFormat(codec, 32000, AudioDecoder::kMaxNumberOfChannels + 1),
absl::nullopt));
}
}
TEST(AudioDecoderFactoryTest, CreateG722) {
const Environment env = CreateEnvironment();
rtc::scoped_refptr<AudioDecoderFactory> adf =
CreateBuiltinAudioDecoderFactory();
ASSERT_TRUE(adf);
// g722 supports 8 kHz, 1-2 channels.
EXPECT_FALSE(
adf->MakeAudioDecoder(SdpAudioFormat("g722", 8000, 0), absl::nullopt));
EXPECT_TRUE(
adf->MakeAudioDecoder(SdpAudioFormat("g722", 8000, 1), absl::nullopt));
EXPECT_TRUE(
adf->MakeAudioDecoder(SdpAudioFormat("g722", 8000, 2), absl::nullopt));
adf->Create(env, SdpAudioFormat("g722", 8000, 0), absl::nullopt));
EXPECT_TRUE(adf->Create(env, SdpAudioFormat("g722", 8000, 1), absl::nullopt));
EXPECT_TRUE(adf->Create(env, SdpAudioFormat("g722", 8000, 2), absl::nullopt));
EXPECT_FALSE(
adf->MakeAudioDecoder(SdpAudioFormat("g722", 8000, 3), absl::nullopt));
adf->Create(env, SdpAudioFormat("g722", 8000, 3), absl::nullopt));
EXPECT_FALSE(
adf->MakeAudioDecoder(SdpAudioFormat("g722", 16000, 1), absl::nullopt));
adf->Create(env, SdpAudioFormat("g722", 16000, 1), absl::nullopt));
EXPECT_FALSE(
adf->MakeAudioDecoder(SdpAudioFormat("g722", 32000, 1), absl::nullopt));
adf->Create(env, SdpAudioFormat("g722", 32000, 1), absl::nullopt));
// g722 actually uses a 16 kHz sample rate instead of the nominal 8 kHz.
std::unique_ptr<AudioDecoder> dec =
adf->MakeAudioDecoder(SdpAudioFormat("g722", 8000, 1), absl::nullopt);
adf->Create(env, SdpAudioFormat("g722", 8000, 1), absl::nullopt);
EXPECT_EQ(16000, dec->SampleRateHz());
}
TEST(AudioDecoderFactoryTest, CreateOpus) {
const Environment env = CreateEnvironment();
rtc::scoped_refptr<AudioDecoderFactory> adf =
CreateBuiltinAudioDecoderFactory();
ASSERT_TRUE(adf);
@ -152,10 +153,11 @@ TEST(AudioDecoderFactoryTest, CreateOpus) {
}
const bool good = (hz == 48000 && channels == 2 &&
(stereo == "XX" || stereo == "0" || stereo == "1"));
EXPECT_EQ(good,
static_cast<bool>(adf->MakeAudioDecoder(
SdpAudioFormat("opus", hz, channels, std::move(params)),
absl::nullopt)));
EXPECT_EQ(
good,
static_cast<bool>(adf->Create(
env, SdpAudioFormat("opus", hz, channels, std::move(params)),
absl::nullopt)));
}
}
}

View File

@ -14,6 +14,7 @@
#include <string>
#include "absl/memory/memory.h"
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
#include "api/environment/environment_factory.h"
#include "test/gmock.h"
@ -21,11 +22,10 @@
#include "test/mock_audio_decoder.h"
#include "test/mock_audio_decoder_factory.h"
using ::testing::_;
using ::testing::Invoke;
namespace webrtc {
using ::testing::WithArg;
TEST(DecoderDatabase, CreateAndDestroy) {
DecoderDatabase db(CreateEnvironment(),
make_ref_counted<MockAudioDecoderFactory>(),
@ -70,12 +70,10 @@ TEST(DecoderDatabase, InsertAndRemoveAll) {
TEST(DecoderDatabase, GetDecoderInfo) {
auto factory = rtc::make_ref_counted<MockAudioDecoderFactory>();
auto* decoder = new MockAudioDecoder;
EXPECT_CALL(*factory, MakeAudioDecoderMock(_, _, _))
.WillOnce(Invoke([decoder](const SdpAudioFormat& format,
absl::optional<AudioCodecPairId> codec_pair_id,
std::unique_ptr<AudioDecoder>* dec) {
EXPECT_CALL(*factory, Create)
.WillOnce(WithArg<1>([decoder](const SdpAudioFormat& format) {
EXPECT_EQ("pcmu", format.name);
dec->reset(decoder);
return absl::WrapUnique(decoder);
}));
DecoderDatabase db(CreateEnvironment(), std::move(factory), absl::nullopt);
const uint8_t kPayloadType = 0;

View File

@ -308,10 +308,8 @@ TEST_F(NetEqImplTest, InsertPacket) {
const Environment env = CreateEnvironment();
auto mock_decoder_factory = rtc::make_ref_counted<MockAudioDecoderFactory>();
EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
.WillOnce(Invoke([&](const SdpAudioFormat& format,
absl::optional<AudioCodecPairId> codec_pair_id,
std::unique_ptr<AudioDecoder>* dec) {
EXPECT_CALL(*mock_decoder_factory, Create)
.WillOnce(WithArg<1>([&](const SdpAudioFormat& format) {
EXPECT_EQ("pcmu", format.name);
std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
@ -319,7 +317,7 @@ TEST_F(NetEqImplTest, InsertPacket) {
EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
*dec = std::move(mock_decoder);
return mock_decoder;
}));
DecoderDatabase::DecoderInfo info(env, SdpAudioFormat("pcmu", 8000, 1),
absl::nullopt, mock_decoder_factory.get());
@ -1646,13 +1644,12 @@ TEST_F(NetEqImplTest, NoCrashWith1000Channels) {
const Environment env = CreateEnvironment();
auto mock_decoder_factory = rtc::make_ref_counted<MockAudioDecoderFactory>();
EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
.WillOnce(Invoke([&](const SdpAudioFormat& format,
absl::optional<AudioCodecPairId> codec_pair_id,
std::unique_ptr<AudioDecoder>* dec) {
EXPECT_CALL(*mock_decoder_factory, Create)
.WillOnce(WithArg<1>([&](const SdpAudioFormat& format) {
EXPECT_EQ("pcmu", format.name);
*dec = std::make_unique<AudioDecoderPcmU>(1000);
decoder = dec->get();
auto dec = std::make_unique<AudioDecoderPcmU>(1000);
decoder = dec.get();
return dec;
}));
DecoderDatabase::DecoderInfo info(env, SdpAudioFormat("pcmu", 8000, 1),
absl::nullopt, mock_decoder_factory.get());

View File

@ -299,10 +299,10 @@ std::unique_ptr<NetEqTest> NetEqTestFactory::InitializeTest(
// decoder_factory before it's reassigned on the left-hand side.
decoder_factory = rtc::make_ref_counted<FunctionAudioDecoderFactory>(
[decoder_factory, config](
const SdpAudioFormat& format,
const Environment& env, const SdpAudioFormat& format,
absl::optional<AudioCodecPairId> codec_pair_id) {
std::unique_ptr<AudioDecoder> decoder =
decoder_factory->MakeAudioDecoder(format, codec_pair_id);
decoder_factory->Create(env, format, codec_pair_id);
if (!decoder && format.name == "replacement") {
decoder = std::make_unique<FakeDecodeFromFile>(
std::make_unique<InputAudioFile>(config.replacement_audio_file),

View File

@ -299,20 +299,19 @@ CreateForwardingMockDecoderFactory(
Invoke([real_decoder_factory](const webrtc::SdpAudioFormat& format) {
return real_decoder_factory->IsSupportedDecoder(format);
}));
EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
EXPECT_CALL(*mock_decoder_factory, Create)
.Times(AtLeast(2))
.WillRepeatedly(
Invoke([real_decoder_factory](
const webrtc::SdpAudioFormat& format,
absl::optional<webrtc::AudioCodecPairId> codec_pair_id,
std::unique_ptr<webrtc::AudioDecoder>* return_value) {
[real_decoder_factory](
const webrtc::Environment& env,
const webrtc::SdpAudioFormat& format,
absl::optional<webrtc::AudioCodecPairId> codec_pair_id) {
auto real_decoder =
real_decoder_factory->MakeAudioDecoder(format, codec_pair_id);
*return_value =
real_decoder
? CreateForwardingMockDecoder(std::move(real_decoder))
: nullptr;
}));
real_decoder_factory->Create(env, format, codec_pair_id);
return real_decoder
? CreateForwardingMockDecoder(std::move(real_decoder))
: nullptr;
});
return mock_decoder_factory;
}
@ -446,12 +445,13 @@ TEST_P(PeerConnectionEndToEndTest, CallWithCustomCodec) {
bool IsSupportedDecoder(const webrtc::SdpAudioFormat& format) override {
return fact_->IsSupportedDecoder(format);
}
std::unique_ptr<webrtc::AudioDecoder> MakeAudioDecoder(
std::unique_ptr<webrtc::AudioDecoder> Create(
const Environment& env,
const webrtc::SdpAudioFormat& format,
absl::optional<webrtc::AudioCodecPairId> codec_pair_id) override {
EXPECT_TRUE(codec_pair_id.has_value());
codec_ids_->push_back(*codec_pair_id);
return fact_->MakeAudioDecoder(format, codec_pair_id);
return fact_->Create(env, format, codec_pair_id);
}
private:

View File

@ -226,6 +226,7 @@ rtc_library("audio_test_common") {
]
deps = [
"../api/audio_codecs:audio_codecs_api",
"../api/environment",
"../rtc_base:checks",
"//third_party/abseil-cpp/absl/memory",
]

View File

@ -19,6 +19,7 @@
#include "absl/memory/memory.h"
#include "api/audio_codecs/audio_decoder_factory.h"
#include "api/audio_codecs/audio_format.h"
#include "api/environment/environment.h"
#include "rtc_base/checks.h"
namespace webrtc {
@ -29,12 +30,14 @@ class FunctionAudioDecoderFactory : public AudioDecoderFactory {
public:
explicit FunctionAudioDecoderFactory(
std::function<std::unique_ptr<AudioDecoder>()> create)
: create_([create](const SdpAudioFormat&,
: create_([create](const Environment&,
const SdpAudioFormat&,
absl::optional<AudioCodecPairId> codec_pair_id) {
return create();
}) {}
explicit FunctionAudioDecoderFactory(
std::function<std::unique_ptr<AudioDecoder>(
const Environment&,
const SdpAudioFormat&,
absl::optional<AudioCodecPairId> codec_pair_id)> create)
: create_(std::move(create)) {}
@ -49,14 +52,16 @@ class FunctionAudioDecoderFactory : public AudioDecoderFactory {
return true;
}
std::unique_ptr<AudioDecoder> MakeAudioDecoder(
std::unique_ptr<AudioDecoder> Create(
const Environment& env,
const SdpAudioFormat& format,
absl::optional<AudioCodecPairId> codec_pair_id) override {
return create_(format, codec_pair_id);
return create_(env, format, codec_pair_id);
}
private:
const std::function<std::unique_ptr<AudioDecoder>(
const Environment&,
const SdpAudioFormat&,
absl::optional<AudioCodecPairId> codec_pair_id)>
create_;

View File

@ -16,6 +16,7 @@
#include "api/audio_codecs/audio_decoder_factory.h"
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
#include "api/environment/environment.h"
#include "api/make_ref_counted.h"
#include "api/scoped_refptr.h"
#include "test/gmock.h"
@ -24,67 +25,34 @@ namespace webrtc {
class MockAudioDecoderFactory : public AudioDecoderFactory {
public:
MOCK_METHOD(std::vector<AudioCodecSpec>,
GetSupportedDecoders,
(),
(override));
MOCK_METHOD(bool, IsSupportedDecoder, (const SdpAudioFormat&), (override));
std::unique_ptr<AudioDecoder> MakeAudioDecoder(
const SdpAudioFormat& format,
absl::optional<AudioCodecPairId> codec_pair_id) override {
std::unique_ptr<AudioDecoder> return_value;
MakeAudioDecoderMock(format, codec_pair_id, &return_value);
return return_value;
}
MOCK_METHOD(void,
MakeAudioDecoderMock,
(const SdpAudioFormat& format,
absl::optional<AudioCodecPairId> codec_pair_id,
std::unique_ptr<AudioDecoder>*));
// Creates a MockAudioDecoderFactory with no formats and that may not be
// invoked to create a codec - useful for initializing a voice engine, for
// example.
static rtc::scoped_refptr<webrtc::MockAudioDecoderFactory>
CreateUnusedFactory() {
using ::testing::_;
using ::testing::AnyNumber;
using ::testing::Return;
rtc::scoped_refptr<webrtc::MockAudioDecoderFactory> factory =
rtc::make_ref_counted<webrtc::MockAudioDecoderFactory>();
ON_CALL(*factory.get(), GetSupportedDecoders())
.WillByDefault(Return(std::vector<webrtc::AudioCodecSpec>()));
EXPECT_CALL(*factory.get(), GetSupportedDecoders()).Times(AnyNumber());
ON_CALL(*factory, IsSupportedDecoder(_)).WillByDefault(Return(false));
EXPECT_CALL(*factory, IsSupportedDecoder(_)).Times(AnyNumber());
EXPECT_CALL(*factory.get(), MakeAudioDecoderMock(_, _, _)).Times(0);
static scoped_refptr<AudioDecoderFactory> CreateUnusedFactory() {
auto factory =
make_ref_counted<testing::NiceMock<MockAudioDecoderFactory>>();
EXPECT_CALL(*factory, Create).Times(0);
return factory;
}
// Creates a MockAudioDecoderFactory with no formats that may be invoked to
// create a codec any number of times. It will, though, return nullptr on each
// call, since it supports no codecs.
static rtc::scoped_refptr<webrtc::MockAudioDecoderFactory>
CreateEmptyFactory() {
using ::testing::_;
using ::testing::AnyNumber;
using ::testing::Return;
using ::testing::SetArgPointee;
rtc::scoped_refptr<webrtc::MockAudioDecoderFactory> factory =
rtc::make_ref_counted<webrtc::MockAudioDecoderFactory>();
ON_CALL(*factory.get(), GetSupportedDecoders())
.WillByDefault(Return(std::vector<webrtc::AudioCodecSpec>()));
EXPECT_CALL(*factory.get(), GetSupportedDecoders()).Times(AnyNumber());
ON_CALL(*factory, IsSupportedDecoder(_)).WillByDefault(Return(false));
EXPECT_CALL(*factory, IsSupportedDecoder(_)).Times(AnyNumber());
ON_CALL(*factory.get(), MakeAudioDecoderMock(_, _, _))
.WillByDefault(SetArgPointee<2>(nullptr));
EXPECT_CALL(*factory.get(), MakeAudioDecoderMock(_, _, _))
.Times(AnyNumber());
return factory;
static scoped_refptr<AudioDecoderFactory> CreateEmptyFactory() {
return make_ref_counted<testing::NiceMock<MockAudioDecoderFactory>>();
}
MOCK_METHOD(std::vector<AudioCodecSpec>,
GetSupportedDecoders,
(),
(override));
MOCK_METHOD(bool, IsSupportedDecoder, (const SdpAudioFormat&), (override));
MOCK_METHOD(std::unique_ptr<AudioDecoder>,
Create,
(const Environment&,
const SdpAudioFormat&,
absl::optional<AudioCodecPairId>),
(override));
};
} // namespace webrtc