Chromium Code Reviews| Index: webrtc/api/audio_codecs/audio_encoder_factory_template.h |
| diff --git a/webrtc/api/audio_codecs/audio_encoder_factory_template.h b/webrtc/api/audio_codecs/audio_encoder_factory_template.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5d629ddd98ea16b1b5e4c4fcd7400108a6b6dfc9 |
| --- /dev/null |
| +++ b/webrtc/api/audio_codecs/audio_encoder_factory_template.h |
| @@ -0,0 +1,149 @@ |
| +/* |
| + * 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_ENCODER_FACTORY_TEMPLATE_H_ |
| +#define WEBRTC_API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_ |
| + |
| +#include <memory> |
| +#include <vector> |
| + |
| +#include "webrtc/api/audio_codecs/audio_encoder_factory.h" |
| +#include "webrtc/base/scoped_ref_ptr.h" |
| + |
| +namespace webrtc { |
| + |
| +namespace audio_encoder_factory_template_impl { |
| + |
| +template <typename... Ts> |
| +class AudioEncoderFactoryT : public AudioEncoderFactory { |
| + public: |
| + std::vector<AudioCodecSpec> GetSupportedEncoders() override { |
| + static const std::vector<AudioCodecSpec> specs = [] { |
| + std::vector<AudioCodecSpec> specs; |
| + DoAppendSupportedEncoders(Dummy<Ts...>(), &specs); |
| + return specs; |
| + }(); |
| + return specs; |
| + } |
| + |
| + rtc::Optional<AudioCodecInfo> QueryAudioEncoder( |
| + const SdpAudioFormat& format) override { |
| + return DoQueryAudioEncoder(Dummy<Ts...>(), format); |
| + } |
| + |
| + std::unique_ptr<AudioEncoder> MakeAudioEncoder( |
| + int payload_type, |
| + const SdpAudioFormat& format) override { |
| + return DoMakeAudioEncoder(Dummy<Ts...>(), payload_type, format); |
| + } |
| + |
| + private: |
| + // Dummy type that's used to ensure we call the proper overloads. |
| + template <typename...> |
| + struct Dummy {}; |
| + |
| + static void DoAppendSupportedEncoders(Dummy<>, |
| + std::vector<AudioCodecSpec>* specs) {} |
| + |
| + template <typename U, typename... Us> |
| + static void DoAppendSupportedEncoders(Dummy<U, Us...>, |
| + std::vector<AudioCodecSpec>* specs) { |
| + U::AppendSupportedEncoders(specs); |
| + DoAppendSupportedEncoders(Dummy<Us...>(), specs); |
| + } |
| + |
| + rtc::Optional<AudioCodecInfo> DoQueryAudioEncoder( |
| + Dummy<>, |
| + const SdpAudioFormat& format) { |
| + return rtc::Optional<AudioCodecInfo>(); |
| + } |
| + |
| + template <typename U, typename... Us> |
| + rtc::Optional<AudioCodecInfo> DoQueryAudioEncoder( |
| + Dummy<U, Us...>, |
| + const SdpAudioFormat& format) { |
| + auto opt_config = U::SdpToConfig(format); |
| + if (opt_config) { |
| + RTC_CHECK(opt_config->IsOk()); |
| + return rtc::Optional<AudioCodecInfo>(U::QueryAudioEncoder(*opt_config)); |
| + } else { |
| + return DoQueryAudioEncoder(Dummy<Us...>(), format); |
| + } |
| + } |
| + |
| + std::unique_ptr<AudioEncoder> |
| + DoMakeAudioEncoder(Dummy<>, int payload_type, const SdpAudioFormat& format) { |
| + return nullptr; |
| + } |
| + |
| + template <typename U, typename... Us> |
| + std::unique_ptr<AudioEncoder> DoMakeAudioEncoder( |
| + Dummy<U, Us...>, |
| + int payload_type, |
| + const SdpAudioFormat& format) { |
| + auto opt_config = U::SdpToConfig(format); |
| + if (opt_config) { |
| + RTC_CHECK(opt_config->IsOk()); |
| + auto enc = U::MakeAudioEncoder(*opt_config, payload_type); |
| + RTC_CHECK(enc); |
| + return enc; |
| + } else { |
| + return DoMakeAudioEncoder(Dummy<Us...>(), payload_type, format); |
| + } |
| + } |
| +}; |
| + |
| +} // namespace audio_encoder_factory_template_impl |
| + |
| +// Make an AudioEncoderFactory that can create instances of the given encoders. |
| +// |
| +// Each encoder 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 encoder of our |
| +// // type. |
| +// rtc::Optional<ConfigType> SdpToConfig(const SdpAudioFormat& audio_format); |
| +// |
| +// // Appends zero or more AudioCodecSpecs to the list that will be returned |
| +// // by AudioEncoderFactory::GetSupportedEncoders(). |
| +// void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs); |
| +// |
| +// // Returns information about how this format would be encoded. Used to |
| +// // implement AudioEncoderFactory::QueryAudioEncoder(). |
| +// AudioCodecInfo QueryAudioEncoder(const ConfigType&); |
|
the sun
2017/06/13 14:56:14
missing param name
kwiberg-webrtc
2017/06/13 19:28:35
Done.
|
| +// |
| +// // Creates an AudioEncoder for the specified format. Used to implement |
| +// // AudioEncoderFactory::MakeAudioEncoder(). |
| +// std::unique_ptr<AudioEncoder> MakeAudioEncoder(const ConfigType&, |
|
the sun
2017/06/13 14:56:14
here too
kwiberg-webrtc
2017/06/13 19:28:35
Done.
|
| +// int payload_type); |
| +// |
| +// ConfigType should be a type that encapsulates all the settings needed to |
| +// create an AudioDecoder. It must have an IsOk() member function that returns |
| +// true if the settings are good, false if they are not. |
| +// |
| +// NOTE: This function is still under development and may change without notice. |
| +template <typename... Ts> |
| +rtc::scoped_refptr<AudioEncoderFactory> CreateAudioEncoderFactory() { |
|
the sun
2017/06/13 14:56:14
Could this go first in the file, above impl?
kwiberg-webrtc
2017/06/13 19:28:35
No... maybe. It's subclassing AudioEncoderFactoryT
the sun
2017/06/13 20:30:06
Agree. I can live without such beauty.
|
| + // There's no technical reason we couldn't allow zero template parameters, |
| + // but such a factory couldn't create any encoders, 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<AudioEncoderFactory>( |
| + new rtc::RefCountedObject< |
| + audio_encoder_factory_template_impl::AudioEncoderFactoryT<Ts...>>()); |
| +} |
| + |
| +} // namespace webrtc |
| + |
| +#endif // WEBRTC_API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_ |