kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_ |
| 12 | #define API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_ |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 13 | |
| 14 | #include <memory> |
| 15 | #include <vector> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 17 | #include "api/audio_codecs/audio_encoder_factory.h" |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 18 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 19 | #include "rtc_base/ref_counted_object.h" |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | namespace audio_encoder_factory_template_impl { |
| 24 | |
| 25 | template <typename... Ts> |
| 26 | struct Helper; |
| 27 | |
| 28 | // Base case: 0 template parameters. |
| 29 | template <> |
| 30 | struct Helper<> { |
| 31 | static void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs) {} |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 32 | static absl::optional<AudioCodecInfo> QueryAudioEncoder( |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 33 | const SdpAudioFormat& format) { |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 34 | return absl::nullopt; |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 35 | } |
| 36 | static std::unique_ptr<AudioEncoder> MakeAudioEncoder( |
| 37 | int payload_type, |
Karl Wiberg | 6b54228 | 2018-03-05 19:24:57 | [diff] [blame] | 38 | const SdpAudioFormat& format, |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 39 | absl::optional<AudioCodecPairId> codec_pair_id) { |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 40 | return nullptr; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | // Inductive case: Called with n + 1 template parameters; calls subroutines |
| 45 | // with n template parameters. |
| 46 | template <typename T, typename... Ts> |
| 47 | struct Helper<T, Ts...> { |
| 48 | static void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs) { |
| 49 | T::AppendSupportedEncoders(specs); |
| 50 | Helper<Ts...>::AppendSupportedEncoders(specs); |
| 51 | } |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 52 | static absl::optional<AudioCodecInfo> QueryAudioEncoder( |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 53 | const SdpAudioFormat& format) { |
| 54 | auto opt_config = T::SdpToConfig(format); |
Karl Wiberg | 7eaf2f9 | 2017-11-24 13:17:27 | [diff] [blame] | 55 | static_assert(std::is_same<decltype(opt_config), |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 56 | absl::optional<typename T::Config>>::value, |
Karl Wiberg | 7eaf2f9 | 2017-11-24 13:17:27 | [diff] [blame] | 57 | "T::SdpToConfig() must return a value of type " |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 58 | "absl::optional<T::Config>"); |
| 59 | return opt_config ? absl::optional<AudioCodecInfo>( |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 60 | T::QueryAudioEncoder(*opt_config)) |
| 61 | : Helper<Ts...>::QueryAudioEncoder(format); |
| 62 | } |
| 63 | static std::unique_ptr<AudioEncoder> MakeAudioEncoder( |
| 64 | int payload_type, |
Karl Wiberg | 6b54228 | 2018-03-05 19:24:57 | [diff] [blame] | 65 | const SdpAudioFormat& format, |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 66 | absl::optional<AudioCodecPairId> codec_pair_id) { |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 67 | auto opt_config = T::SdpToConfig(format); |
| 68 | if (opt_config) { |
Karl Wiberg | f1c470e | 2018-06-01 09:24:55 | [diff] [blame] | 69 | return T::MakeAudioEncoder(*opt_config, payload_type, codec_pair_id); |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 70 | } else { |
Karl Wiberg | 6b54228 | 2018-03-05 19:24:57 | [diff] [blame] | 71 | return Helper<Ts...>::MakeAudioEncoder(payload_type, format, |
| 72 | codec_pair_id); |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | template <typename... Ts> |
| 78 | class AudioEncoderFactoryT : public AudioEncoderFactory { |
| 79 | public: |
| 80 | std::vector<AudioCodecSpec> GetSupportedEncoders() override { |
| 81 | std::vector<AudioCodecSpec> specs; |
| 82 | Helper<Ts...>::AppendSupportedEncoders(&specs); |
| 83 | return specs; |
| 84 | } |
| 85 | |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 86 | absl::optional<AudioCodecInfo> QueryAudioEncoder( |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 87 | const SdpAudioFormat& format) override { |
| 88 | return Helper<Ts...>::QueryAudioEncoder(format); |
| 89 | } |
| 90 | |
| 91 | std::unique_ptr<AudioEncoder> MakeAudioEncoder( |
| 92 | int payload_type, |
Karl Wiberg | 6b54228 | 2018-03-05 19:24:57 | [diff] [blame] | 93 | const SdpAudioFormat& format, |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 94 | absl::optional<AudioCodecPairId> codec_pair_id) override { |
Karl Wiberg | 6b54228 | 2018-03-05 19:24:57 | [diff] [blame] | 95 | return Helper<Ts...>::MakeAudioEncoder(payload_type, format, codec_pair_id); |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 96 | } |
| 97 | }; |
| 98 | |
| 99 | } // namespace audio_encoder_factory_template_impl |
| 100 | |
| 101 | // Make an AudioEncoderFactory that can create instances of the given encoders. |
| 102 | // |
| 103 | // Each encoder type is given as a template argument to the function; it should |
| 104 | // be a struct with the following static member functions: |
| 105 | // |
| 106 | // // Converts |audio_format| to a ConfigType instance. Returns an empty |
| 107 | // // optional if |audio_format| doesn't correctly specify an encoder of our |
| 108 | // // type. |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 109 | // absl::optional<ConfigType> SdpToConfig(const SdpAudioFormat& audio_format); |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 110 | // |
| 111 | // // Appends zero or more AudioCodecSpecs to the list that will be returned |
| 112 | // // by AudioEncoderFactory::GetSupportedEncoders(). |
| 113 | // void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs); |
| 114 | // |
| 115 | // // Returns information about how this format would be encoded. Used to |
| 116 | // // implement AudioEncoderFactory::QueryAudioEncoder(). |
| 117 | // AudioCodecInfo QueryAudioEncoder(const ConfigType& config); |
| 118 | // |
| 119 | // // Creates an AudioEncoder for the specified format. Used to implement |
| 120 | // // AudioEncoderFactory::MakeAudioEncoder(). |
Karl Wiberg | 6b54228 | 2018-03-05 19:24:57 | [diff] [blame] | 121 | // std::unique_ptr<AudioDecoder> MakeAudioEncoder( |
| 122 | // const ConfigType& config, |
| 123 | // int payload_type, |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 124 | // absl::optional<AudioCodecPairId> codec_pair_id); |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 125 | // |
| 126 | // ConfigType should be a type that encapsulates all the settings needed to |
Karl Wiberg | 7eaf2f9 | 2017-11-24 13:17:27 | [diff] [blame] | 127 | // create an AudioEncoder. T::Config (where T is the encoder struct) should |
| 128 | // either be the config type, or an alias for it. |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 129 | // |
| 130 | // Whenever it tries to do something, the new factory will try each of the |
| 131 | // encoders in the order they were specified in the template argument list, |
| 132 | // stopping at the first one that claims to be able to do the job. |
| 133 | // |
kwiberg | 6531583 | 2017-06-16 17:42:05 | [diff] [blame] | 134 | // TODO(kwiberg): Point at CreateBuiltinAudioEncoderFactory() for an example of |
| 135 | // how it is used. |
| 136 | template <typename... Ts> |
| 137 | rtc::scoped_refptr<AudioEncoderFactory> CreateAudioEncoderFactory() { |
| 138 | // There's no technical reason we couldn't allow zero template parameters, |
| 139 | // but such a factory couldn't create any encoders, and callers can do this |
| 140 | // by mistake by simply forgetting the <> altogether. So we forbid it in |
| 141 | // order to prevent caller foot-shooting. |
| 142 | static_assert(sizeof...(Ts) >= 1, |
| 143 | "Caller must give at least one template parameter"); |
| 144 | |
| 145 | return rtc::scoped_refptr<AudioEncoderFactory>( |
| 146 | new rtc::RefCountedObject< |
| 147 | audio_encoder_factory_template_impl::AudioEncoderFactoryT<Ts...>>()); |
| 148 | } |
| 149 | |
| 150 | } // namespace webrtc |
| 151 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 152 | #endif // API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_ |