Chromium Code Reviews| 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_ENCODER_FACTORY_TEMPLATE_H_ | |
| 12 #define WEBRTC_API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_ | |
| 13 | |
| 14 #include <memory> | |
| 15 #include <vector> | |
| 16 | |
| 17 #include "webrtc/api/audio_codecs/audio_encoder_factory.h" | |
| 18 #include "webrtc/base/scoped_ref_ptr.h" | |
| 19 | |
| 20 namespace webrtc { | |
| 21 | |
| 22 namespace audio_encoder_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 AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs) {} | |
| 31 static rtc::Optional<AudioCodecInfo> QueryAudioEncoder( | |
| 32 const SdpAudioFormat& format) { | |
| 33 return rtc::Optional<AudioCodecInfo>(); | |
| 34 } | |
| 35 static std::unique_ptr<AudioEncoder> MakeAudioEncoder( | |
| 36 int payload_type, | |
| 37 const SdpAudioFormat& format) { | |
| 38 return nullptr; | |
| 39 } | |
| 40 }; | |
| 41 | |
| 42 // Inductive case: Called with n + 1 template parameters, calls subroutines | |
| 43 // with n template parameters. | |
| 44 template <typename T, typename... Ts> | |
| 45 struct Helper<T, Ts...> { | |
| 46 static void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs) { | |
| 47 T::AppendSupportedEncoders(specs); | |
| 48 Helper<Ts...>::AppendSupportedEncoders(specs); | |
| 49 } | |
| 50 static rtc::Optional<AudioCodecInfo> QueryAudioEncoder( | |
| 51 const SdpAudioFormat& format) { | |
| 52 auto opt_config = T::SdpToConfig(format); | |
| 53 return opt_config ? rtc::Optional<AudioCodecInfo>( | |
| 54 T::QueryAudioEncoder(*opt_config)) | |
| 55 : Helper<Ts...>::QueryAudioEncoder(format); | |
| 56 } | |
| 57 static std::unique_ptr<AudioEncoder> MakeAudioEncoder( | |
| 58 int payload_type, | |
| 59 const SdpAudioFormat& format) { | |
| 60 auto opt_config = T::SdpToConfig(format); | |
| 61 if (opt_config) { | |
| 62 auto enc = T::MakeAudioEncoder(*opt_config, payload_type); | |
| 63 RTC_CHECK(enc); | |
|
the sun
2017/06/16 09:18:06
Is this an expected runtime condition or not? If t
kwiberg-webrtc
2017/06/16 09:57:01
It's a CHECK, so it's not an expected runtime cond
ossu
2017/06/16 13:36:53
Should this be handled as failing to create an Aud
kwiberg-webrtc
2017/06/16 13:40:08
Well, in what other way could we possibly handle i
| |
| 64 return enc; | |
| 65 } else { | |
| 66 return Helper<Ts...>::MakeAudioEncoder(payload_type, format); | |
| 67 } | |
| 68 } | |
| 69 }; | |
| 70 | |
| 71 template <typename... Ts> | |
| 72 class AudioEncoderFactoryT : public AudioEncoderFactory { | |
| 73 public: | |
| 74 std::vector<AudioCodecSpec> GetSupportedEncoders() override { | |
| 75 static const std::vector<AudioCodecSpec> specs = [] { | |
|
the sun
2017/06/16 09:18:06
Caching the specs preclude dynamically changing th
kwiberg-webrtc
2017/06/16 09:57:01
Yes, I hadn't considered that. Fixed.
ossu
2017/06/16 13:36:53
I think this isn't the wrong place to handle that.
kwiberg-webrtc
2017/06/16 13:40:08
Acknowledged.
| |
| 76 std::vector<AudioCodecSpec> specs; | |
| 77 Helper<Ts...>::AppendSupportedEncoders(&specs); | |
| 78 return specs; | |
| 79 }(); | |
| 80 return specs; | |
| 81 } | |
| 82 | |
| 83 rtc::Optional<AudioCodecInfo> QueryAudioEncoder( | |
| 84 const SdpAudioFormat& format) override { | |
| 85 return Helper<Ts...>::QueryAudioEncoder(format); | |
| 86 } | |
| 87 | |
| 88 std::unique_ptr<AudioEncoder> MakeAudioEncoder( | |
| 89 int payload_type, | |
| 90 const SdpAudioFormat& format) override { | |
| 91 return Helper<Ts...>::MakeAudioEncoder(payload_type, format); | |
| 92 } | |
| 93 }; // namespace audio_encoder_factory_template_impl | |
|
ossu
2017/06/16 09:11:35
nit: It's just the class ending here, the namespac
the sun
2017/06/16 09:18:06
bad comment
kwiberg-webrtc
2017/06/16 09:57:01
Done.
| |
| 94 | |
| 95 } // namespace audio_encoder_factory_template_impl | |
| 96 | |
| 97 // Make an AudioEncoderFactory that can create instances of the given encoders. | |
|
the sun
2017/06/16 09:18:06
TODO: Point at builtin_audio_encoder_factory for a
kwiberg-webrtc
2017/06/16 09:57:01
Done.
| |
| 98 // | |
| 99 // Each encoder type is given as a template argument to the function; it should | |
| 100 // be a struct with the following static member functions: | |
| 101 // | |
| 102 // // Converts |audio_format| to a ConfigType instance. Returns an empty | |
| 103 // // optional if |audio_format| doesn't correctly specify an encoder of our | |
| 104 // // type. | |
| 105 // rtc::Optional<ConfigType> SdpToConfig(const SdpAudioFormat& audio_format); | |
| 106 // | |
| 107 // // Appends zero or more AudioCodecSpecs to the list that will be returned | |
| 108 // // by AudioEncoderFactory::GetSupportedEncoders(). | |
| 109 // void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs); | |
| 110 // | |
| 111 // // Returns information about how this format would be encoded. Used to | |
| 112 // // implement AudioEncoderFactory::QueryAudioEncoder(). | |
| 113 // AudioCodecInfo QueryAudioEncoder(const ConfigType& config); | |
| 114 // | |
| 115 // // Creates an AudioEncoder for the specified format. Used to implement | |
| 116 // // AudioEncoderFactory::MakeAudioEncoder(). | |
| 117 // std::unique_ptr<AudioEncoder> MakeAudioEncoder(const ConfigType& config, | |
| 118 // int payload_type); | |
| 119 // | |
| 120 // ConfigType should be a type that encapsulates all the settings needed to | |
| 121 // create an AudioDecoder. | |
|
ossu
2017/06/16 11:20:45
I think we should mention that the order of the co
kwiberg-webrtc
2017/06/16 13:03:24
Oh, yeah, that's an excellent idea! Done.
| |
| 122 // | |
| 123 // NOTE: This function is still under development and may change without notice. | |
| 124 template <typename... Ts> | |
| 125 rtc::scoped_refptr<AudioEncoderFactory> CreateAudioEncoderFactory() { | |
| 126 // There's no technical reason we couldn't allow zero template parameters, | |
| 127 // but such a factory couldn't create any encoders, and callers can do this | |
| 128 // by mistake by simply forgetting the <> altogether. So we forbid it in | |
| 129 // order to prevent caller foot-shooting. | |
| 130 static_assert(sizeof...(Ts) >= 1, | |
| 131 "Caller must give at least one template parameter"); | |
| 132 | |
| 133 return rtc::scoped_refptr<AudioEncoderFactory>( | |
| 134 new rtc::RefCountedObject< | |
| 135 audio_encoder_factory_template_impl::AudioEncoderFactoryT<Ts...>>()); | |
| 136 } | |
| 137 | |
| 138 } // namespace webrtc | |
| 139 | |
| 140 #endif // WEBRTC_API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_ | |
| OLD | NEW |