xref: /aosp_15_r20/external/webrtc/api/audio_codecs/test/audio_decoder_factory_template_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2017 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 "api/audio_codecs/audio_decoder_factory_template.h"
12 
13 #include <memory>
14 
15 #include "api/audio_codecs/L16/audio_decoder_L16.h"
16 #include "api/audio_codecs/g711/audio_decoder_g711.h"
17 #include "api/audio_codecs/g722/audio_decoder_g722.h"
18 #include "api/audio_codecs/ilbc/audio_decoder_ilbc.h"
19 #include "api/audio_codecs/opus/audio_decoder_opus.h"
20 #include "test/gmock.h"
21 #include "test/gtest.h"
22 #include "test/mock_audio_decoder.h"
23 #include "test/scoped_key_value_config.h"
24 
25 namespace webrtc {
26 
27 namespace {
28 
29 struct BogusParams {
AudioFormatwebrtc::__anon2475fb720111::BogusParams30   static SdpAudioFormat AudioFormat() { return {"bogus", 8000, 1}; }
CodecInfowebrtc::__anon2475fb720111::BogusParams31   static AudioCodecInfo CodecInfo() { return {8000, 1, 12345}; }
32 };
33 
34 struct ShamParams {
AudioFormatwebrtc::__anon2475fb720111::ShamParams35   static SdpAudioFormat AudioFormat() {
36     return {"sham", 16000, 2, {{"param", "value"}}};
37   }
CodecInfowebrtc::__anon2475fb720111::ShamParams38   static AudioCodecInfo CodecInfo() { return {16000, 2, 23456}; }
39 };
40 
41 template <typename Params>
42 struct AudioDecoderFakeApi {
43   struct Config {
44     SdpAudioFormat audio_format;
45   };
46 
SdpToConfigwebrtc::__anon2475fb720111::AudioDecoderFakeApi47   static absl::optional<Config> SdpToConfig(
48       const SdpAudioFormat& audio_format) {
49     if (Params::AudioFormat() == audio_format) {
50       Config config = {audio_format};
51       return config;
52     } else {
53       return absl::nullopt;
54     }
55   }
56 
AppendSupportedDecoderswebrtc::__anon2475fb720111::AudioDecoderFakeApi57   static void AppendSupportedDecoders(std::vector<AudioCodecSpec>* specs) {
58     specs->push_back({Params::AudioFormat(), Params::CodecInfo()});
59   }
60 
QueryAudioDecoderwebrtc::__anon2475fb720111::AudioDecoderFakeApi61   static AudioCodecInfo QueryAudioDecoder(const Config&) {
62     return Params::CodecInfo();
63   }
64 
MakeAudioDecoderwebrtc::__anon2475fb720111::AudioDecoderFakeApi65   static std::unique_ptr<AudioDecoder> MakeAudioDecoder(
66       const Config&,
67       absl::optional<AudioCodecPairId> /*codec_pair_id*/ = absl::nullopt) {
68     auto dec = std::make_unique<testing::StrictMock<MockAudioDecoder>>();
69     EXPECT_CALL(*dec, SampleRateHz())
70         .WillOnce(::testing::Return(Params::CodecInfo().sample_rate_hz));
71     EXPECT_CALL(*dec, Die());
72     return std::move(dec);
73   }
74 };
75 
76 }  // namespace
77 
TEST(AudioDecoderFactoryTemplateTest,NoDecoderTypes)78 TEST(AudioDecoderFactoryTemplateTest, NoDecoderTypes) {
79   test::ScopedKeyValueConfig field_trials;
80   rtc::scoped_refptr<AudioDecoderFactory> factory(
81       rtc::make_ref_counted<
82           audio_decoder_factory_template_impl::AudioDecoderFactoryT<>>(
83           &field_trials));
84   EXPECT_THAT(factory->GetSupportedDecoders(), ::testing::IsEmpty());
85   EXPECT_FALSE(factory->IsSupportedDecoder({"foo", 8000, 1}));
86   EXPECT_EQ(nullptr,
87             factory->MakeAudioDecoder({"bar", 16000, 1}, absl::nullopt));
88 }
89 
TEST(AudioDecoderFactoryTemplateTest,OneDecoderType)90 TEST(AudioDecoderFactoryTemplateTest, OneDecoderType) {
91   auto factory = CreateAudioDecoderFactory<AudioDecoderFakeApi<BogusParams>>();
92   EXPECT_THAT(factory->GetSupportedDecoders(),
93               ::testing::ElementsAre(
94                   AudioCodecSpec{{"bogus", 8000, 1}, {8000, 1, 12345}}));
95   EXPECT_FALSE(factory->IsSupportedDecoder({"foo", 8000, 1}));
96   EXPECT_TRUE(factory->IsSupportedDecoder({"bogus", 8000, 1}));
97   EXPECT_EQ(nullptr,
98             factory->MakeAudioDecoder({"bar", 16000, 1}, absl::nullopt));
99   auto dec = factory->MakeAudioDecoder({"bogus", 8000, 1}, absl::nullopt);
100   ASSERT_NE(nullptr, dec);
101   EXPECT_EQ(8000, dec->SampleRateHz());
102 }
103 
TEST(AudioDecoderFactoryTemplateTest,TwoDecoderTypes)104 TEST(AudioDecoderFactoryTemplateTest, TwoDecoderTypes) {
105   auto factory = CreateAudioDecoderFactory<AudioDecoderFakeApi<BogusParams>,
106                                            AudioDecoderFakeApi<ShamParams>>();
107   EXPECT_THAT(factory->GetSupportedDecoders(),
108               ::testing::ElementsAre(
109                   AudioCodecSpec{{"bogus", 8000, 1}, {8000, 1, 12345}},
110                   AudioCodecSpec{{"sham", 16000, 2, {{"param", "value"}}},
111                                  {16000, 2, 23456}}));
112   EXPECT_FALSE(factory->IsSupportedDecoder({"foo", 8000, 1}));
113   EXPECT_TRUE(factory->IsSupportedDecoder({"bogus", 8000, 1}));
114   EXPECT_TRUE(
115       factory->IsSupportedDecoder({"sham", 16000, 2, {{"param", "value"}}}));
116   EXPECT_EQ(nullptr,
117             factory->MakeAudioDecoder({"bar", 16000, 1}, absl::nullopt));
118   auto dec1 = factory->MakeAudioDecoder({"bogus", 8000, 1}, absl::nullopt);
119   ASSERT_NE(nullptr, dec1);
120   EXPECT_EQ(8000, dec1->SampleRateHz());
121   EXPECT_EQ(nullptr,
122             factory->MakeAudioDecoder({"sham", 16000, 2}, absl::nullopt));
123   auto dec2 = factory->MakeAudioDecoder(
124       {"sham", 16000, 2, {{"param", "value"}}}, absl::nullopt);
125   ASSERT_NE(nullptr, dec2);
126   EXPECT_EQ(16000, dec2->SampleRateHz());
127 }
128 
TEST(AudioDecoderFactoryTemplateTest,G711)129 TEST(AudioDecoderFactoryTemplateTest, G711) {
130   auto factory = CreateAudioDecoderFactory<AudioDecoderG711>();
131   EXPECT_THAT(factory->GetSupportedDecoders(),
132               ::testing::ElementsAre(
133                   AudioCodecSpec{{"PCMU", 8000, 1}, {8000, 1, 64000}},
134                   AudioCodecSpec{{"PCMA", 8000, 1}, {8000, 1, 64000}}));
135   EXPECT_FALSE(factory->IsSupportedDecoder({"G711", 8000, 1}));
136   EXPECT_TRUE(factory->IsSupportedDecoder({"PCMU", 8000, 1}));
137   EXPECT_TRUE(factory->IsSupportedDecoder({"pcma", 8000, 1}));
138   EXPECT_EQ(nullptr,
139             factory->MakeAudioDecoder({"pcmu", 16000, 1}, absl::nullopt));
140   auto dec1 = factory->MakeAudioDecoder({"pcmu", 8000, 1}, absl::nullopt);
141   ASSERT_NE(nullptr, dec1);
142   EXPECT_EQ(8000, dec1->SampleRateHz());
143   auto dec2 = factory->MakeAudioDecoder({"PCMA", 8000, 1}, absl::nullopt);
144   ASSERT_NE(nullptr, dec2);
145   EXPECT_EQ(8000, dec2->SampleRateHz());
146 }
147 
TEST(AudioDecoderFactoryTemplateTest,G722)148 TEST(AudioDecoderFactoryTemplateTest, G722) {
149   auto factory = CreateAudioDecoderFactory<AudioDecoderG722>();
150   EXPECT_THAT(factory->GetSupportedDecoders(),
151               ::testing::ElementsAre(
152                   AudioCodecSpec{{"G722", 8000, 1}, {16000, 1, 64000}}));
153   EXPECT_FALSE(factory->IsSupportedDecoder({"foo", 8000, 1}));
154   EXPECT_TRUE(factory->IsSupportedDecoder({"G722", 8000, 1}));
155   EXPECT_EQ(nullptr,
156             factory->MakeAudioDecoder({"bar", 16000, 1}, absl::nullopt));
157   auto dec1 = factory->MakeAudioDecoder({"G722", 8000, 1}, absl::nullopt);
158   ASSERT_NE(nullptr, dec1);
159   EXPECT_EQ(16000, dec1->SampleRateHz());
160   EXPECT_EQ(1u, dec1->Channels());
161   auto dec2 = factory->MakeAudioDecoder({"G722", 8000, 2}, absl::nullopt);
162   ASSERT_NE(nullptr, dec2);
163   EXPECT_EQ(16000, dec2->SampleRateHz());
164   EXPECT_EQ(2u, dec2->Channels());
165   auto dec3 = factory->MakeAudioDecoder({"G722", 8000, 3}, absl::nullopt);
166   ASSERT_EQ(nullptr, dec3);
167 }
168 
TEST(AudioDecoderFactoryTemplateTest,Ilbc)169 TEST(AudioDecoderFactoryTemplateTest, Ilbc) {
170   auto factory = CreateAudioDecoderFactory<AudioDecoderIlbc>();
171   EXPECT_THAT(factory->GetSupportedDecoders(),
172               ::testing::ElementsAre(
173                   AudioCodecSpec{{"ILBC", 8000, 1}, {8000, 1, 13300}}));
174   EXPECT_FALSE(factory->IsSupportedDecoder({"foo", 8000, 1}));
175   EXPECT_TRUE(factory->IsSupportedDecoder({"ilbc", 8000, 1}));
176   EXPECT_EQ(nullptr,
177             factory->MakeAudioDecoder({"bar", 8000, 1}, absl::nullopt));
178   auto dec = factory->MakeAudioDecoder({"ilbc", 8000, 1}, absl::nullopt);
179   ASSERT_NE(nullptr, dec);
180   EXPECT_EQ(8000, dec->SampleRateHz());
181 }
182 
TEST(AudioDecoderFactoryTemplateTest,L16)183 TEST(AudioDecoderFactoryTemplateTest, L16) {
184   auto factory = CreateAudioDecoderFactory<AudioDecoderL16>();
185   EXPECT_THAT(
186       factory->GetSupportedDecoders(),
187       ::testing::ElementsAre(
188           AudioCodecSpec{{"L16", 8000, 1}, {8000, 1, 8000 * 16}},
189           AudioCodecSpec{{"L16", 16000, 1}, {16000, 1, 16000 * 16}},
190           AudioCodecSpec{{"L16", 32000, 1}, {32000, 1, 32000 * 16}},
191           AudioCodecSpec{{"L16", 8000, 2}, {8000, 2, 8000 * 16 * 2}},
192           AudioCodecSpec{{"L16", 16000, 2}, {16000, 2, 16000 * 16 * 2}},
193           AudioCodecSpec{{"L16", 32000, 2}, {32000, 2, 32000 * 16 * 2}}));
194   EXPECT_FALSE(factory->IsSupportedDecoder({"foo", 8000, 1}));
195   EXPECT_TRUE(factory->IsSupportedDecoder({"L16", 48000, 1}));
196   EXPECT_FALSE(factory->IsSupportedDecoder({"L16", 96000, 1}));
197   EXPECT_EQ(nullptr,
198             factory->MakeAudioDecoder({"L16", 8000, 0}, absl::nullopt));
199   auto dec = factory->MakeAudioDecoder({"L16", 48000, 2}, absl::nullopt);
200   ASSERT_NE(nullptr, dec);
201   EXPECT_EQ(48000, dec->SampleRateHz());
202 }
203 
TEST(AudioDecoderFactoryTemplateTest,Opus)204 TEST(AudioDecoderFactoryTemplateTest, Opus) {
205   auto factory = CreateAudioDecoderFactory<AudioDecoderOpus>();
206   AudioCodecInfo opus_info{48000, 1, 64000, 6000, 510000};
207   opus_info.allow_comfort_noise = false;
208   opus_info.supports_network_adaption = true;
209   const SdpAudioFormat opus_format(
210       {"opus", 48000, 2, {{"minptime", "10"}, {"useinbandfec", "1"}}});
211   EXPECT_THAT(factory->GetSupportedDecoders(),
212               ::testing::ElementsAre(AudioCodecSpec{opus_format, opus_info}));
213   EXPECT_FALSE(factory->IsSupportedDecoder({"opus", 48000, 1}));
214   EXPECT_TRUE(factory->IsSupportedDecoder({"opus", 48000, 2}));
215   EXPECT_EQ(nullptr,
216             factory->MakeAudioDecoder({"bar", 16000, 1}, absl::nullopt));
217   auto dec = factory->MakeAudioDecoder({"opus", 48000, 2}, absl::nullopt);
218   ASSERT_NE(nullptr, dec);
219   EXPECT_EQ(48000, dec->SampleRateHz());
220 }
221 
222 }  // namespace webrtc
223