OLD | NEW |
(Empty) | |
| 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 "webrtc/api/audio_codecs/audio_encoder_factory_template.h" |
| 12 #include "webrtc/base/ptr_util.h" |
| 13 #include "webrtc/test/gmock.h" |
| 14 #include "webrtc/test/gtest.h" |
| 15 #include "webrtc/test/mock_audio_encoder.h" |
| 16 |
| 17 namespace webrtc { |
| 18 |
| 19 namespace { |
| 20 |
| 21 struct BogusParams { |
| 22 static SdpAudioFormat AudioFormat() { return {"bogus", 8000, 1}; } |
| 23 static AudioCodecInfo CodecInfo() { return {8000, 1, 12345}; } |
| 24 }; |
| 25 |
| 26 struct ShamParams { |
| 27 static SdpAudioFormat AudioFormat() { |
| 28 return {"sham", 16000, 2, {{"param", "value"}}}; |
| 29 } |
| 30 static AudioCodecInfo CodecInfo() { return {16000, 2, 23456}; } |
| 31 }; |
| 32 |
| 33 struct MyLittleConfig { |
| 34 bool IsOk() const { return true; } |
| 35 SdpAudioFormat audio_format; |
| 36 }; |
| 37 |
| 38 template <typename Params> |
| 39 struct AudioEncoderFakeApi { |
| 40 static rtc::Optional<MyLittleConfig> SdpToConfig( |
| 41 const SdpAudioFormat& audio_format) { |
| 42 if (Params::AudioFormat() == audio_format) { |
| 43 MyLittleConfig config = {audio_format}; |
| 44 return rtc::Optional<MyLittleConfig>(config); |
| 45 } else { |
| 46 return rtc::Optional<MyLittleConfig>(); |
| 47 } |
| 48 } |
| 49 |
| 50 static void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs) { |
| 51 specs->push_back({Params::AudioFormat(), Params::CodecInfo()}); |
| 52 } |
| 53 |
| 54 static AudioCodecInfo QueryAudioEncoder(const MyLittleConfig&) { |
| 55 return Params::CodecInfo(); |
| 56 } |
| 57 |
| 58 static std::unique_ptr<AudioEncoder> MakeAudioEncoder(const MyLittleConfig&, |
| 59 int payload_type) { |
| 60 auto enc = rtc::MakeUnique<testing::StrictMock<MockAudioEncoder>>(); |
| 61 EXPECT_CALL(*enc, SampleRateHz()) |
| 62 .WillOnce(testing::Return(Params::CodecInfo().sample_rate_hz)); |
| 63 EXPECT_CALL(*enc, Die()); |
| 64 return std::move(enc); |
| 65 } |
| 66 }; |
| 67 |
| 68 } // namespace |
| 69 |
| 70 TEST(AudioEncoderFactoryTemplateTest, NoEncoderTypes) { |
| 71 rtc::scoped_refptr<AudioEncoderFactory> factory( |
| 72 new rtc::RefCountedObject< |
| 73 audio_encoder_factory_template_impl::AudioEncoderFactoryT<>>()); |
| 74 EXPECT_THAT(factory->GetSupportedEncoders(), testing::IsEmpty()); |
| 75 EXPECT_EQ(rtc::Optional<AudioCodecInfo>(), |
| 76 factory->QueryAudioEncoder({"foo", 8000, 1})); |
| 77 EXPECT_EQ(nullptr, factory->MakeAudioEncoder(17, {"bar", 16000, 1})); |
| 78 } |
| 79 |
| 80 TEST(AudioEncoderFactoryTemplateTest, OneEncoderType) { |
| 81 auto factory = CreateAudioEncoderFactory<AudioEncoderFakeApi<BogusParams>>(); |
| 82 EXPECT_THAT(factory->GetSupportedEncoders(), |
| 83 testing::ElementsAre( |
| 84 AudioCodecSpec{{"bogus", 8000, 1}, {8000, 1, 12345}})); |
| 85 EXPECT_EQ(rtc::Optional<AudioCodecInfo>(), |
| 86 factory->QueryAudioEncoder({"foo", 8000, 1})); |
| 87 EXPECT_EQ(rtc::Optional<AudioCodecInfo>({8000, 1, 12345}), |
| 88 factory->QueryAudioEncoder({"bogus", 8000, 1})); |
| 89 EXPECT_EQ(nullptr, factory->MakeAudioEncoder(17, {"bar", 16000, 1})); |
| 90 auto enc = factory->MakeAudioEncoder(17, {"bogus", 8000, 1}); |
| 91 ASSERT_NE(nullptr, enc); |
| 92 EXPECT_EQ(8000, enc->SampleRateHz()); |
| 93 } |
| 94 |
| 95 TEST(AudioEncoderFactoryTemplateTest, TwoEncoderTypes) { |
| 96 auto factory = CreateAudioEncoderFactory<AudioEncoderFakeApi<BogusParams>, |
| 97 AudioEncoderFakeApi<ShamParams>>(); |
| 98 EXPECT_THAT(factory->GetSupportedEncoders(), |
| 99 testing::ElementsAre( |
| 100 AudioCodecSpec{{"bogus", 8000, 1}, {8000, 1, 12345}}, |
| 101 AudioCodecSpec{{"sham", 16000, 2, {{"param", "value"}}}, |
| 102 {16000, 2, 23456}})); |
| 103 EXPECT_EQ(rtc::Optional<AudioCodecInfo>(), |
| 104 factory->QueryAudioEncoder({"foo", 8000, 1})); |
| 105 EXPECT_EQ(rtc::Optional<AudioCodecInfo>({8000, 1, 12345}), |
| 106 factory->QueryAudioEncoder({"bogus", 8000, 1})); |
| 107 EXPECT_EQ( |
| 108 rtc::Optional<AudioCodecInfo>({16000, 2, 23456}), |
| 109 factory->QueryAudioEncoder({"sham", 16000, 2, {{"param", "value"}}})); |
| 110 EXPECT_EQ(nullptr, factory->MakeAudioEncoder(17, {"bar", 16000, 1})); |
| 111 auto enc1 = factory->MakeAudioEncoder(17, {"bogus", 8000, 1}); |
| 112 ASSERT_NE(nullptr, enc1); |
| 113 EXPECT_EQ(8000, enc1->SampleRateHz()); |
| 114 EXPECT_EQ(nullptr, factory->MakeAudioEncoder(17, {"sham", 16000, 2})); |
| 115 auto enc2 = |
| 116 factory->MakeAudioEncoder(17, {"sham", 16000, 2, {{"param", "value"}}}); |
| 117 ASSERT_NE(nullptr, enc2); |
| 118 EXPECT_EQ(16000, enc2->SampleRateHz()); |
| 119 } |
| 120 |
| 121 } // namespace webrtc |
OLD | NEW |