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 class AudioEncoderFactoryT : public AudioEncoderFactory { | |
26 public: | |
27 std::vector<AudioCodecSpec> GetSupportedEncoders() override { | |
28 static const std::vector<AudioCodecSpec> specs = [] { | |
29 std::vector<AudioCodecSpec> specs; | |
30 DoAppendSupportedEncoders(Dummy<Ts...>(), &specs); | |
31 return specs; | |
32 }(); | |
33 return specs; | |
34 } | |
35 | |
36 rtc::Optional<AudioCodecInfo> QueryAudioEncoder( | |
37 const SdpAudioFormat& format) override { | |
38 return DoQueryAudioEncoder(Dummy<Ts...>(), format); | |
39 } | |
40 | |
41 std::unique_ptr<AudioEncoder> MakeAudioEncoder( | |
42 int payload_type, | |
43 const SdpAudioFormat& format) override { | |
44 return DoMakeAudioEncoder(Dummy<Ts...>(), payload_type, format); | |
45 } | |
46 | |
47 private: | |
48 // Dummy type that's used to ensure we call the proper overloads. | |
49 template <typename...> | |
ossu
2017/06/15 13:57:59
You can get rid of the Dummy argument by making a
kwiberg-webrtc
2017/06/16 02:19:50
Done.
| |
50 struct Dummy {}; | |
51 | |
52 static void DoAppendSupportedEncoders(Dummy<>, | |
53 std::vector<AudioCodecSpec>* specs) {} | |
54 | |
55 template <typename U, typename... Us> | |
56 static void DoAppendSupportedEncoders(Dummy<U, Us...>, | |
57 std::vector<AudioCodecSpec>* specs) { | |
58 U::AppendSupportedEncoders(specs); | |
59 DoAppendSupportedEncoders(Dummy<Us...>(), specs); | |
60 } | |
61 | |
62 rtc::Optional<AudioCodecInfo> DoQueryAudioEncoder( | |
63 Dummy<>, | |
64 const SdpAudioFormat& format) { | |
65 return rtc::Optional<AudioCodecInfo>(); | |
66 } | |
67 | |
68 template <typename U, typename... Us> | |
69 rtc::Optional<AudioCodecInfo> DoQueryAudioEncoder( | |
70 Dummy<U, Us...>, | |
71 const SdpAudioFormat& format) { | |
72 auto opt_config = U::SdpToConfig(format); | |
73 if (opt_config) { | |
74 return rtc::Optional<AudioCodecInfo>(U::QueryAudioEncoder(*opt_config)); | |
75 } else { | |
76 return DoQueryAudioEncoder(Dummy<Us...>(), format); | |
77 } | |
78 } | |
79 | |
80 std::unique_ptr<AudioEncoder> | |
81 DoMakeAudioEncoder(Dummy<>, int payload_type, const SdpAudioFormat& format) { | |
82 return nullptr; | |
83 } | |
84 | |
85 template <typename U, typename... Us> | |
86 std::unique_ptr<AudioEncoder> DoMakeAudioEncoder( | |
87 Dummy<U, Us...>, | |
88 int payload_type, | |
89 const SdpAudioFormat& format) { | |
90 auto opt_config = U::SdpToConfig(format); | |
91 if (opt_config) { | |
92 auto enc = U::MakeAudioEncoder(*opt_config, payload_type); | |
93 RTC_CHECK(enc); | |
94 return enc; | |
95 } else { | |
96 return DoMakeAudioEncoder(Dummy<Us...>(), payload_type, format); | |
97 } | |
98 } | |
99 }; | |
100 | |
101 } // namespace audio_encoder_factory_template_impl | |
102 | |
103 // Make an AudioEncoderFactory that can create instances of the given encoders. | |
104 // | |
105 // Each encoder type is given as a template argument to the function; it should | |
106 // be a struct with the following static member functions: | |
107 // | |
108 // // Converts |audio_format| to a ConfigType instance. Returns an empty | |
109 // // optional if |audio_format| doesn't correctly specify an encoder of our | |
110 // // type. | |
111 // rtc::Optional<ConfigType> SdpToConfig(const SdpAudioFormat& audio_format); | |
112 // | |
113 // // Appends zero or more AudioCodecSpecs to the list that will be returned | |
114 // // by AudioEncoderFactory::GetSupportedEncoders(). | |
115 // void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs); | |
116 // | |
117 // // Returns information about how this format would be encoded. Used to | |
118 // // implement AudioEncoderFactory::QueryAudioEncoder(). | |
119 // AudioCodecInfo QueryAudioEncoder(const ConfigType& config); | |
120 // | |
121 // // Creates an AudioEncoder for the specified format. Used to implement | |
122 // // AudioEncoderFactory::MakeAudioEncoder(). | |
123 // std::unique_ptr<AudioEncoder> MakeAudioEncoder(const ConfigType& config, | |
124 // int payload_type); | |
125 // | |
126 // ConfigType should be a type that encapsulates all the settings needed to | |
127 // create an AudioDecoder. | |
128 // | |
129 // NOTE: This function is still under development and may change without notice. | |
130 template <typename... Ts> | |
131 rtc::scoped_refptr<AudioEncoderFactory> CreateAudioEncoderFactory() { | |
132 // There's no technical reason we couldn't allow zero template parameters, | |
133 // but such a factory couldn't create any encoders, and callers can do this | |
134 // by mistake by simply forgetting the <> altogether. So we forbid it in | |
135 // order to prevent caller foot-shooting. | |
136 static_assert(sizeof...(Ts) >= 1, | |
137 "Caller must give at least one template parameter"); | |
138 | |
139 return rtc::scoped_refptr<AudioEncoderFactory>( | |
140 new rtc::RefCountedObject< | |
141 audio_encoder_factory_template_impl::AudioEncoderFactoryT<Ts...>>()); | |
142 } | |
143 | |
144 } // namespace webrtc | |
145 | |
146 #endif // WEBRTC_API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_ | |
OLD | NEW |