audio_encoder_factory_template.h (7625B)
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_ENCODER_FACTORY_TEMPLATE_H_ 12 #define API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_ 13 14 #include <memory> 15 #include <optional> 16 #include <type_traits> 17 #include <utility> 18 #include <vector> 19 20 #include "absl/base/nullability.h" 21 #include "api/audio_codecs/audio_codec_pair_id.h" 22 #include "api/audio_codecs/audio_encoder.h" 23 #include "api/audio_codecs/audio_encoder_factory.h" 24 #include "api/audio_codecs/audio_format.h" 25 #include "api/environment/environment.h" 26 #include "api/make_ref_counted.h" 27 #include "api/scoped_refptr.h" 28 29 namespace webrtc { 30 31 namespace audio_encoder_factory_template_impl { 32 33 template <typename... Ts> 34 struct Helper; 35 36 // Base case: 0 template parameters. 37 template <> 38 struct Helper<> { 39 static void AppendSupportedEncoders( 40 std::vector<AudioCodecSpec>* /* specs */) {} 41 static std::optional<AudioCodecInfo> QueryAudioEncoder( 42 const SdpAudioFormat& /* format */) { 43 return std::nullopt; 44 } 45 static absl_nullable std::unique_ptr<AudioEncoder> CreateAudioEncoder( 46 const Environment& /* env */, 47 const SdpAudioFormat& /* format */, 48 const AudioEncoderFactory::Options& /* options */) { 49 return nullptr; 50 } 51 }; 52 53 // Use ranked overloads (abseil.io/tips/229) for dispatching. 54 struct Rank0 {}; 55 struct Rank1 : Rank0 {}; 56 57 template <typename Trait, 58 typename = std::enable_if_t<std::is_convertible_v< 59 decltype(Trait::MakeAudioEncoder( 60 std::declval<Environment>(), 61 std::declval<typename Trait::Config>(), 62 std::declval<AudioEncoderFactory::Options>())), 63 std::unique_ptr<AudioEncoder>>>> 64 absl_nullable std::unique_ptr<AudioEncoder> CreateEncoder( 65 Rank1, 66 const Environment& env, 67 const typename Trait::Config& config, 68 const AudioEncoderFactory::Options& options) { 69 return Trait::MakeAudioEncoder(env, config, options); 70 } 71 72 template <typename Trait, 73 typename = std::enable_if_t<std::is_convertible_v< 74 decltype(Trait::MakeAudioEncoder( 75 std::declval<typename Trait::Config>(), 76 int{}, 77 std::declval<std::optional<AudioCodecPairId>>())), 78 std::unique_ptr<AudioEncoder>>>> 79 absl_nullable std::unique_ptr<AudioEncoder> CreateEncoder( 80 Rank0, 81 const Environment& /* env */, 82 const typename Trait::Config& config, 83 const AudioEncoderFactory::Options& options) { 84 return Trait::MakeAudioEncoder(config, options.payload_type, 85 options.codec_pair_id); 86 } 87 88 // Inductive case: Called with n + 1 template parameters; calls subroutines 89 // with n template parameters. 90 template <typename T, typename... Ts> 91 struct Helper<T, Ts...> { 92 static void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs) { 93 T::AppendSupportedEncoders(specs); 94 Helper<Ts...>::AppendSupportedEncoders(specs); 95 } 96 static std::optional<AudioCodecInfo> QueryAudioEncoder( 97 const SdpAudioFormat& format) { 98 auto opt_config = T::SdpToConfig(format); 99 static_assert(std::is_same<decltype(opt_config), 100 std::optional<typename T::Config>>::value, 101 "T::SdpToConfig() must return a value of type " 102 "std::optional<T::Config>"); 103 return opt_config ? std::optional<AudioCodecInfo>( 104 T::QueryAudioEncoder(*opt_config)) 105 : Helper<Ts...>::QueryAudioEncoder(format); 106 } 107 108 static absl_nullable std::unique_ptr<AudioEncoder> CreateAudioEncoder( 109 const Environment& env, 110 const SdpAudioFormat& format, 111 const AudioEncoderFactory::Options& options) { 112 if (auto opt_config = T::SdpToConfig(format); opt_config.has_value()) { 113 return CreateEncoder<T>(Rank1{}, env, *opt_config, options); 114 } 115 return Helper<Ts...>::CreateAudioEncoder(env, format, options); 116 } 117 }; 118 119 template <typename... Ts> 120 class AudioEncoderFactoryT : public AudioEncoderFactory { 121 public: 122 std::vector<AudioCodecSpec> GetSupportedEncoders() override { 123 std::vector<AudioCodecSpec> specs; 124 Helper<Ts...>::AppendSupportedEncoders(&specs); 125 return specs; 126 } 127 128 std::optional<AudioCodecInfo> QueryAudioEncoder( 129 const SdpAudioFormat& format) override { 130 return Helper<Ts...>::QueryAudioEncoder(format); 131 } 132 133 absl_nullable std::unique_ptr<AudioEncoder> Create( 134 const Environment& env, 135 const SdpAudioFormat& format, 136 Options options) override { 137 return Helper<Ts...>::CreateAudioEncoder(env, format, options); 138 } 139 }; 140 141 } // namespace audio_encoder_factory_template_impl 142 143 // Make an AudioEncoderFactory that can create instances of the given encoders. 144 // 145 // Each encoder type is given as a template argument to the function; it should 146 // be a struct with the following static member functions: 147 // 148 // // Converts `audio_format` to a ConfigType instance. Returns an empty 149 // // optional if `audio_format` doesn't correctly specify an encoder of our 150 // // type. 151 // std::optional<ConfigType> SdpToConfig(const SdpAudioFormat& audio_format); 152 // 153 // // Appends zero or more AudioCodecSpecs to the list that will be returned 154 // // by AudioEncoderFactory::GetSupportedEncoders(). 155 // void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs); 156 // 157 // // Returns information about how this format would be encoded. Used to 158 // // implement AudioEncoderFactory::QueryAudioEncoder(). 159 // AudioCodecInfo QueryAudioEncoder(const ConfigType& config); 160 // 161 // // Creates an AudioEncoder for the specified format. Used to implement 162 // // AudioEncoderFactory::Create. 163 // std::unique_ptr<AudioEncoder> MakeAudioEncoder( 164 // const Environment& env, 165 // const ConfigType& config, 166 // const AudioEncoderFactory::Options& options); 167 // or 168 // std::unique_ptr<AudioEncoder> MakeAudioEncoder( 169 // const ConfigType& config, 170 // int payload_type, 171 // std::optional<AudioCodecPairId> codec_pair_id); 172 // 173 // ConfigType should be a type that encapsulates all the settings needed to 174 // create an AudioEncoder. T::Config (where T is the encoder struct) should 175 // either be the config type, or an alias for it. 176 // When both MakeAudioEncoder signatures are present, 1st one is preferred. 177 // 178 // Whenever it tries to do something, the new factory will try each of the 179 // encoders in the order they were specified in the template argument list, 180 // stopping at the first one that claims to be able to do the job. 181 // 182 // TODO(kwiberg): Point at CreateBuiltinAudioEncoderFactory() for an example of 183 // how it is used. 184 template <typename... Ts> 185 scoped_refptr<AudioEncoderFactory> CreateAudioEncoderFactory() { 186 // There's no technical reason we couldn't allow zero template parameters, 187 // but such a factory couldn't create any encoders, and callers can do this 188 // by mistake by simply forgetting the <> altogether. So we forbid it in 189 // order to prevent caller foot-shooting. 190 static_assert(sizeof...(Ts) >= 1, 191 "Caller must give at least one template parameter"); 192 193 return make_ref_counted< 194 audio_encoder_factory_template_impl::AudioEncoderFactoryT<Ts...>>(); 195 } 196 197 } // namespace webrtc 198 199 #endif // API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_