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 #ifndef API_AUDIO_CODECS_AUDIO_DECODER_FACTORY_TEMPLATE_H_ 12 #define API_AUDIO_CODECS_AUDIO_DECODER_FACTORY_TEMPLATE_H_ 13 14 #include <memory> 15 #include <vector> 16 17 #include "api/audio_codecs/audio_decoder_factory.h" 18 #include "api/scoped_refptr.h" 19 #include "rtc_base/ref_counted_object.h" 20 21 namespace webrtc { 22 23 namespace audio_decoder_factory_template_impl { 24 25 template <typename... Ts> 26 struct Helper; 27 28 // Base case: 0 template parameters. 29 template <> 30 struct Helper<> { 31 static void AppendSupportedDecoders(std::vector<AudioCodecSpec>* specs) {} 32 static bool IsSupportedDecoder(const SdpAudioFormat& format) { return false; } 33 static std::unique_ptr<AudioDecoder> MakeAudioDecoder( 34 const SdpAudioFormat& format, 35 absl::optional<AudioCodecPairId> codec_pair_id) { 36 return nullptr; 37 } 38 }; 39 40 // Inductive case: Called with n + 1 template parameters; calls subroutines 41 // with n template parameters. 42 template <typename T, typename... Ts> 43 struct Helper<T, Ts...> { 44 static void AppendSupportedDecoders(std::vector<AudioCodecSpec>* specs) { 45 T::AppendSupportedDecoders(specs); 46 Helper<Ts...>::AppendSupportedDecoders(specs); 47 } 48 static bool IsSupportedDecoder(const SdpAudioFormat& format) { 49 auto opt_config = T::SdpToConfig(format); 50 static_assert(std::is_same<decltype(opt_config), 51 absl::optional<typename T::Config>>::value, 52 "T::SdpToConfig() must return a value of type " 53 "absl::optional<T::Config>"); 54 return opt_config ? true : Helper<Ts...>::IsSupportedDecoder(format); 55 } 56 static std::unique_ptr<AudioDecoder> MakeAudioDecoder( 57 const SdpAudioFormat& format, 58 absl::optional<AudioCodecPairId> codec_pair_id) { 59 auto opt_config = T::SdpToConfig(format); 60 return opt_config ? T::MakeAudioDecoder(*opt_config, codec_pair_id) 61 : Helper<Ts...>::MakeAudioDecoder(format, codec_pair_id); 62 } 63 }; 64 65 template <typename... Ts> 66 class AudioDecoderFactoryT : public AudioDecoderFactory { 67 public: 68 std::vector<AudioCodecSpec> GetSupportedDecoders() override { 69 std::vector<AudioCodecSpec> specs; 70 Helper<Ts...>::AppendSupportedDecoders(&specs); 71 return specs; 72 } 73 74 bool IsSupportedDecoder(const SdpAudioFormat& format) override { 75 return Helper<Ts...>::IsSupportedDecoder(format); 76 } 77 78 std::unique_ptr<AudioDecoder> MakeAudioDecoder( 79 const SdpAudioFormat& format, 80 absl::optional<AudioCodecPairId> codec_pair_id) override { 81 return Helper<Ts...>::MakeAudioDecoder(format, codec_pair_id); 82 } 83 }; 84 85 } // namespace audio_decoder_factory_template_impl 86 87 // Make an AudioDecoderFactory that can create instances of the given decoders. 88 // 89 // Each decoder type is given as a template argument to the function; it should 90 // be a struct with the following static member functions: 91 // 92 // // Converts |audio_format| to a ConfigType instance. Returns an empty 93 // // optional if |audio_format| doesn't correctly specify a decoder of our 94 // // type. 95 // absl::optional<ConfigType> SdpToConfig(const SdpAudioFormat& audio_format); 96 // 97 // // Appends zero or more AudioCodecSpecs to the list that will be returned 98 // // by AudioDecoderFactory::GetSupportedDecoders(). 99 // void AppendSupportedDecoders(std::vector<AudioCodecSpec>* specs); 100 // 101 // // Creates an AudioDecoder for the specified format. Used to implement 102 // // AudioDecoderFactory::MakeAudioDecoder(). 103 // std::unique_ptr<AudioDecoder> MakeAudioDecoder( 104 // const ConfigType& config, 105 // absl::optional<AudioCodecPairId> codec_pair_id); 106 // 107 // ConfigType should be a type that encapsulates all the settings needed to 108 // create an AudioDecoder. T::Config (where T is the decoder struct) should 109 // either be the config type, or an alias for it. 110 // 111 // Whenever it tries to do something, the new factory will try each of the 112 // decoder types in the order they were specified in the template argument 113 // list, stopping at the first one that claims to be able to do the job. 114 // 115 // TODO(kwiberg): Point at CreateBuiltinAudioDecoderFactory() for an example of 116 // how it is used. 117 template <typename... Ts> 118 rtc::scoped_refptr<AudioDecoderFactory> CreateAudioDecoderFactory() { 119 // There's no technical reason we couldn't allow zero template parameters, 120 // but such a factory couldn't create any decoders, and callers can do this 121 // by mistake by simply forgetting the <> altogether. So we forbid it in 122 // order to prevent caller foot-shooting. 123 static_assert(sizeof...(Ts) >= 1, 124 "Caller must give at least one template parameter"); 125 126 return rtc::scoped_refptr<AudioDecoderFactory>( 127 new rtc::RefCountedObject< 128 audio_decoder_factory_template_impl::AudioDecoderFactoryT<Ts...>>()); 129 } 130 131 } // namespace webrtc 132 133 #endif // API_AUDIO_CODECS_AUDIO_DECODER_FACTORY_TEMPLATE_H_ 134