From 6b54228e8b591d0ad7162196afefdc628ca0da21 Mon Sep 17 00:00:00 2001 From: Karl Wiberg Date: Mon, 5 Mar 2018 20:24:57 +0100 Subject: [PATCH] Adapt audio codec factory templates to the new codec pair ID arguments We use template magic to let them handle both the presence and absence of the new argument. This will be removed in a later CL, when we can assume that new argument is always present. Bug: webrtc:8941 Change-Id: I2d47f7c8572a9f03e742401dcf491b948b161f63 Reviewed-on: https://webrtc-review.googlesource.com/58081 Commit-Queue: Karl Wiberg Reviewed-by: Oskar Sundbom Cr-Commit-Position: refs/heads/master@{#22301} --- .../audio_decoder_factory_template.h | 52 ++++++++++++++-- .../audio_encoder_factory_template.h | 59 +++++++++++++++++-- 2 files changed, 99 insertions(+), 12 deletions(-) diff --git a/api/audio_codecs/audio_decoder_factory_template.h b/api/audio_codecs/audio_decoder_factory_template.h index f40c271ec8..20a56d3771 100644 --- a/api/audio_codecs/audio_decoder_factory_template.h +++ b/api/audio_codecs/audio_decoder_factory_template.h @@ -22,6 +22,22 @@ namespace webrtc { namespace audio_decoder_factory_template_impl { +template +class MakeAudioDecoderTakesTwoArgs { + private: + template + static auto Test(int) -> decltype( + U::MakeAudioDecoder(std::declval(), + std::declval>()), + std::true_type()); + + template + static std::false_type Test(...); + + public: + static constexpr bool value = decltype(Test(0))::value; +}; + template struct Helper; @@ -31,7 +47,8 @@ struct Helper<> { static void AppendSupportedDecoders(std::vector* specs) {} static bool IsSupportedDecoder(const SdpAudioFormat& format) { return false; } static std::unique_ptr MakeAudioDecoder( - const SdpAudioFormat& format) { + const SdpAudioFormat& format, + rtc::Optional codec_pair_id) { return nullptr; } }; @@ -53,10 +70,28 @@ struct Helper { return opt_config ? true : Helper::IsSupportedDecoder(format); } static std::unique_ptr MakeAudioDecoder( - const SdpAudioFormat& format) { + const SdpAudioFormat& format, + rtc::Optional codec_pair_id) { auto opt_config = T::SdpToConfig(format); - return opt_config ? T::MakeAudioDecoder(*opt_config) - : Helper::MakeAudioDecoder(format); + return opt_config ? CallMakeAudioDecoder(*opt_config, codec_pair_id) + : Helper::MakeAudioDecoder(format, codec_pair_id); + } + template < + typename ConfigT, + typename std::enable_if< + !MakeAudioDecoderTakesTwoArgs::value>::type* = nullptr> + static decltype(T::MakeAudioDecoder(std::declval())) + CallMakeAudioDecoder(const ConfigT& config, + rtc::Optional codec_pair_id) { + return T::MakeAudioDecoder(config); + } + template + static decltype( + T::MakeAudioDecoder(std::declval(), + std::declval>())) + CallMakeAudioDecoder(const ConfigT& config, + rtc::Optional codec_pair_id) { + return T::MakeAudioDecoder(config, codec_pair_id); } }; @@ -74,8 +109,9 @@ class AudioDecoderFactoryT : public AudioDecoderFactory { } std::unique_ptr MakeAudioDecoder( - const SdpAudioFormat& format) override { - return Helper::MakeAudioDecoder(format); + const SdpAudioFormat& format, + rtc::Optional codec_pair_id) override { + return Helper::MakeAudioDecoder(format, codec_pair_id); } }; @@ -98,6 +134,10 @@ class AudioDecoderFactoryT : public AudioDecoderFactory { // // Creates an AudioDecoder for the specified format. Used to implement // // AudioDecoderFactory::MakeAudioDecoder(). // std::unique_ptr MakeAudioDecoder(const ConfigType& config); +// OR +// std::unique_ptr MakeAudioDecoder( +// const ConfigType& config, +// rtc::Optional codec_pair_id); // // ConfigType should be a type that encapsulates all the settings needed to // create an AudioDecoder. T::Config (where T is the decoder struct) should diff --git a/api/audio_codecs/audio_encoder_factory_template.h b/api/audio_codecs/audio_encoder_factory_template.h index 060ba8cb04..6d4d9d61b0 100644 --- a/api/audio_codecs/audio_encoder_factory_template.h +++ b/api/audio_codecs/audio_encoder_factory_template.h @@ -22,6 +22,23 @@ namespace webrtc { namespace audio_encoder_factory_template_impl { +template +class MakeAudioEncoderTakesThreeArgs { + private: + template + static auto Test(int) -> decltype( + U::MakeAudioEncoder(std::declval(), + std::declval(), + std::declval>()), + std::true_type()); + + template + static std::false_type Test(...); + + public: + static constexpr bool value = decltype(Test(0))::value; +}; + template struct Helper; @@ -35,7 +52,8 @@ struct Helper<> { } static std::unique_ptr MakeAudioEncoder( int payload_type, - const SdpAudioFormat& format) { + const SdpAudioFormat& format, + rtc::Optional codec_pair_id) { return nullptr; } }; @@ -61,14 +79,37 @@ struct Helper { } static std::unique_ptr MakeAudioEncoder( int payload_type, - const SdpAudioFormat& format) { + const SdpAudioFormat& format, + rtc::Optional codec_pair_id) { auto opt_config = T::SdpToConfig(format); if (opt_config) { - return T::MakeAudioEncoder(*opt_config, payload_type); + return CallMakeAudioEncoder(*opt_config, payload_type, codec_pair_id); } else { - return Helper::MakeAudioEncoder(payload_type, format); + return Helper::MakeAudioEncoder(payload_type, format, + codec_pair_id); } } + template < + typename ConfigT, + typename std::enable_if< + !MakeAudioEncoderTakesThreeArgs::value>::type* = nullptr> + static decltype(T::MakeAudioEncoder(std::declval(), + std::declval())) + CallMakeAudioEncoder(const ConfigT& config, + int payload_type, + rtc::Optional codec_pair_id) { + return T::MakeAudioEncoder(config, payload_type); + } + template + static decltype( + T::MakeAudioEncoder(std::declval(), + std::declval(), + std::declval>())) + CallMakeAudioEncoder(const ConfigT& config, + int payload_type, + rtc::Optional codec_pair_id) { + return T::MakeAudioEncoder(config, payload_type, codec_pair_id); + } }; template @@ -87,8 +128,9 @@ class AudioEncoderFactoryT : public AudioEncoderFactory { std::unique_ptr MakeAudioEncoder( int payload_type, - const SdpAudioFormat& format) override { - return Helper::MakeAudioEncoder(payload_type, format); + const SdpAudioFormat& format, + rtc::Optional codec_pair_id) override { + return Helper::MakeAudioEncoder(payload_type, format, codec_pair_id); } }; @@ -116,6 +158,11 @@ class AudioEncoderFactoryT : public AudioEncoderFactory { // // AudioEncoderFactory::MakeAudioEncoder(). // std::unique_ptr MakeAudioEncoder(const ConfigType& config, // int payload_type); +// OR +// std::unique_ptr MakeAudioEncoder( +// const ConfigType& config, +// int payload_type, +// rtc::Optional codec_pair_id); // // ConfigType should be a type that encapsulates all the settings needed to // create an AudioEncoder. T::Config (where T is the encoder struct) should