xref: /aosp_15_r20/external/webrtc/api/audio_codecs/audio_encoder_factory_template.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_
12 #define API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_
13 
14 #include <memory>
15 #include <vector>
16 
17 #include "api/audio_codecs/audio_encoder_factory.h"
18 #include "api/field_trials_view.h"
19 #include "api/make_ref_counted.h"
20 #include "api/scoped_refptr.h"
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) {}
33   static absl::optional<AudioCodecInfo> QueryAudioEncoder(
34       const SdpAudioFormat& format) {
35     return absl::nullopt;
36   }
37   static std::unique_ptr<AudioEncoder> MakeAudioEncoder(
38       int payload_type,
39       const SdpAudioFormat& format,
40       absl::optional<AudioCodecPairId> codec_pair_id,
41       const FieldTrialsView* field_trials) {
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   }
54   static absl::optional<AudioCodecInfo> QueryAudioEncoder(
55       const SdpAudioFormat& format) {
56     auto opt_config = T::SdpToConfig(format);
57     static_assert(std::is_same<decltype(opt_config),
58                                absl::optional<typename T::Config>>::value,
59                   "T::SdpToConfig() must return a value of type "
60                   "absl::optional<T::Config>");
61     return opt_config ? absl::optional<AudioCodecInfo>(
62                             T::QueryAudioEncoder(*opt_config))
63                       : Helper<Ts...>::QueryAudioEncoder(format);
64   }
65   static std::unique_ptr<AudioEncoder> MakeAudioEncoder(
66       int payload_type,
67       const SdpAudioFormat& format,
68       absl::optional<AudioCodecPairId> codec_pair_id,
69       const FieldTrialsView* field_trials) {
70     auto opt_config = T::SdpToConfig(format);
71     if (opt_config) {
72       return T::MakeAudioEncoder(*opt_config, payload_type, codec_pair_id);
73     } else {
74       return Helper<Ts...>::MakeAudioEncoder(payload_type, format,
75                                              codec_pair_id, field_trials);
76     }
77   }
78 };
79 
80 template <typename... Ts>
81 class AudioEncoderFactoryT : public AudioEncoderFactory {
82  public:
83   explicit AudioEncoderFactoryT(const FieldTrialsView* field_trials) {
84     field_trials_ = field_trials;
85   }
86 
87   std::vector<AudioCodecSpec> GetSupportedEncoders() override {
88     std::vector<AudioCodecSpec> specs;
89     Helper<Ts...>::AppendSupportedEncoders(&specs);
90     return specs;
91   }
92 
93   absl::optional<AudioCodecInfo> QueryAudioEncoder(
94       const SdpAudioFormat& format) override {
95     return Helper<Ts...>::QueryAudioEncoder(format);
96   }
97 
98   std::unique_ptr<AudioEncoder> MakeAudioEncoder(
99       int payload_type,
100       const SdpAudioFormat& format,
101       absl::optional<AudioCodecPairId> codec_pair_id) override {
102     return Helper<Ts...>::MakeAudioEncoder(payload_type, format, codec_pair_id,
103                                            field_trials_);
104   }
105 
106   const FieldTrialsView* field_trials_;
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 //
116 //   // Converts `audio_format` to a ConfigType instance. Returns an empty
117 //   // optional if `audio_format` doesn't correctly specify an encoder of our
118 //   // type.
119 //   absl::optional<ConfigType> SdpToConfig(const SdpAudioFormat& audio_format);
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().
131 //   std::unique_ptr<AudioDecoder> MakeAudioEncoder(
132 //       const ConfigType& config,
133 //       int payload_type,
134 //       absl::optional<AudioCodecPairId> codec_pair_id);
135 //
136 // ConfigType should be a type that encapsulates all the settings needed to
137 // create an AudioEncoder. T::Config (where T is the encoder struct) should
138 // either be the config type, or an alias for it.
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 //
144 // TODO(kwiberg): Point at CreateBuiltinAudioEncoderFactory() for an example of
145 // how it is used.
146 template <typename... Ts>
147 rtc::scoped_refptr<AudioEncoderFactory> CreateAudioEncoderFactory(
148     const FieldTrialsView* field_trials = nullptr) {
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 
156   return rtc::make_ref_counted<
157       audio_encoder_factory_template_impl::AudioEncoderFactoryT<Ts...>>(
158       field_trials);
159 }
160 
161 }  // namespace webrtc
162 
163 #endif  // API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_
164