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