Chromium Code Reviews| Index: webrtc/api/audio_codecs/audio_decoder_factory_template.h | 
| diff --git a/webrtc/api/audio_codecs/audio_decoder_factory_template.h b/webrtc/api/audio_codecs/audio_decoder_factory_template.h | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..0ecd0f02a81b1ef8e7146e628962b2b092709046 | 
| --- /dev/null | 
| +++ b/webrtc/api/audio_codecs/audio_decoder_factory_template.h | 
| @@ -0,0 +1,131 @@ | 
| +/* | 
| + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 
| + * | 
| + * Use of this source code is governed by a BSD-style license | 
| + * that can be found in the LICENSE file in the root of the source | 
| + * tree. An additional intellectual property rights grant can be found | 
| + * in the file PATENTS. All contributing project authors may | 
| + * be found in the AUTHORS file in the root of the source tree. | 
| + */ | 
| + | 
| +#ifndef WEBRTC_API_AUDIO_CODECS_AUDIO_DECODER_FACTORY_TEMPLATE_H_ | 
| +#define WEBRTC_API_AUDIO_CODECS_AUDIO_DECODER_FACTORY_TEMPLATE_H_ | 
| + | 
| +#include <memory> | 
| +#include <vector> | 
| + | 
| +#include "webrtc/api/audio_codecs/audio_decoder_factory.h" | 
| +#include "webrtc/base/scoped_ref_ptr.h" | 
| + | 
| +namespace webrtc { | 
| + | 
| +namespace audio_decoder_factory_template_impl { | 
| + | 
| +template <typename... Ts> | 
| +class AudioDecoderFactoryT : public AudioDecoderFactory { | 
| + public: | 
| + std::vector<AudioCodecSpec> GetSupportedDecoders() override { | 
| + static const std::vector<AudioCodecSpec> specs = [] { | 
| + std::vector<AudioCodecSpec> specs; | 
| + DoAppendSupportedDecoders(Dummy<Ts...>(), &specs); | 
| + return specs; | 
| + }(); | 
| + return specs; | 
| + } | 
| + | 
| + bool IsSupportedDecoder(const SdpAudioFormat& format) override { | 
| + return DoIsSupportedDecoder(Dummy<Ts...>(), format); | 
| + } | 
| + | 
| + std::unique_ptr<AudioDecoder> MakeAudioDecoder( | 
| + const SdpAudioFormat& format) override { | 
| + return DoMakeAudioDecoder(Dummy<Ts...>(), format); | 
| + } | 
| + | 
| + private: | 
| + // Dummy type that's used to ensure we call the proper overloads. | 
| + template <typename...> | 
| 
 
ossu
2017/06/16 11:25:18
As with the encoder factory, this one could do wit
 
kwiberg-webrtc
2017/06/16 13:17:06
Yes. The decoder factory is very similar to the en
 
 | 
| + struct Dummy {}; | 
| + | 
| + static void DoAppendSupportedDecoders(Dummy<>, | 
| + std::vector<AudioCodecSpec>* specs) {} | 
| + | 
| + template <typename U, typename... Us> | 
| + static void DoAppendSupportedDecoders(Dummy<U, Us...>, | 
| + std::vector<AudioCodecSpec>* specs) { | 
| + U::AppendSupportedDecoders(specs); | 
| + DoAppendSupportedDecoders(Dummy<Us...>(), specs); | 
| + } | 
| + | 
| + bool DoIsSupportedDecoder(Dummy<>, const SdpAudioFormat& format) { | 
| + return false; | 
| + } | 
| + | 
| + template <typename U, typename... Us> | 
| + bool DoIsSupportedDecoder(Dummy<U, Us...>, const SdpAudioFormat& format) { | 
| + auto opt_config = U::SdpToConfig(format); | 
| + return opt_config ? true : DoIsSupportedDecoder(Dummy<Us...>(), format); | 
| + } | 
| + | 
| + std::unique_ptr<AudioDecoder> DoMakeAudioDecoder( | 
| + Dummy<>, | 
| + const SdpAudioFormat& format) { | 
| + return nullptr; | 
| + } | 
| + | 
| + template <typename U, typename... Us> | 
| + std::unique_ptr<AudioDecoder> DoMakeAudioDecoder( | 
| + Dummy<U, Us...>, | 
| + const SdpAudioFormat& format) { | 
| + auto opt_config = U::SdpToConfig(format); | 
| + if (opt_config) { | 
| + std::unique_ptr<AudioDecoder> dec = U::MakeAudioDecoder(*opt_config); | 
| + RTC_CHECK(dec); | 
| + return dec; | 
| + } else { | 
| + return DoMakeAudioDecoder(Dummy<Us...>(), format); | 
| + } | 
| + } | 
| +}; | 
| + | 
| +} // namespace audio_decoder_factory_template_impl | 
| + | 
| +// Make an AudioDecoderFactory that can create instances of the given decoders. | 
| +// | 
| +// Each decoder type is given as a template argument to the function; it should | 
| +// be a struct with the following static member functions: | 
| +// | 
| +// // Converts |audio_format| to a ConfigType instance. Returns an empty | 
| +// // optional if |audio_format| doesn't correctly specify an decoder of our | 
| +// // type. | 
| +// rtc::Optional<ConfigType> SdpToConfig(const SdpAudioFormat& audio_format); | 
| +// | 
| +// // Appends zero or more AudioCodecSpecs to the list that will be returned | 
| +// // by AudioDecoderFactory::GetSupportedDecoders(). | 
| +// void AppendSupportedDecoders(std::vector<AudioCodecSpec>* specs); | 
| +// | 
| +// // Creates an AudioDecoder for the specified format. Used to implement | 
| +// // AudioDecoderFactory::MakeAudioDecoder(). | 
| +// std::unique_ptr<AudioDecoder> MakeAudioDecoder(const ConfigType& config); | 
| 
 
the sun
2017/06/16 12:52:21
Inconsistent with the decoder impls which use by-v
 
kwiberg-webrtc
2017/06/16 13:17:06
It's just an implementation detail. const ref is p
 
 | 
| +// | 
| +// ConfigType should be a type that encapsulates all the settings needed to | 
| +// create an AudioDecoder. | 
| +// | 
| +// NOTE: This function is still under development and may change without notice. | 
| 
 
ossu
2017/06/16 11:25:18
Should probably mention that the order of the code
 
kwiberg-webrtc
2017/06/16 13:17:06
Yes, that'll be among the things copied over from
 
 | 
| +template <typename... Ts> | 
| +rtc::scoped_refptr<AudioDecoderFactory> CreateAudioDecoderFactory() { | 
| + // There's no technical reason we couldn't allow zero template parameters, | 
| + // but such a factory couldn't create any decoders, and callers can do this | 
| + // by mistake by simply forgetting the <> altogether. So we forbid it in | 
| + // order to prevent caller foot-shooting. | 
| + static_assert(sizeof...(Ts) >= 1, | 
| + "Caller must give at least one template parameter"); | 
| + | 
| + return rtc::scoped_refptr<AudioDecoderFactory>( | 
| + new rtc::RefCountedObject< | 
| + audio_decoder_factory_template_impl::AudioDecoderFactoryT<Ts...>>()); | 
| +} | 
| + | 
| +} // namespace webrtc | 
| + | 
| +#endif // WEBRTC_API_AUDIO_CODECS_AUDIO_DECODER_FACTORY_TEMPLATE_H_ |