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