Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(535)

Side by Side Diff: webrtc/api/audio_codecs/audio_encoder_factory_template.h

Issue 2935643002: Templated AudioEncoderFactory (Closed)
Patch Set: better docs Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/api/audio_codecs/BUILD.gn ('k') | webrtc/api/audio_codecs/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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...>
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 RTC_CHECK(opt_config->IsOk());
75 return rtc::Optional<AudioCodecInfo>(U::QueryAudioEncoder(*opt_config));
76 } else {
77 return DoQueryAudioEncoder(Dummy<Us...>(), format);
78 }
79 }
80
81 std::unique_ptr<AudioEncoder>
82 DoMakeAudioEncoder(Dummy<>, int payload_type, const SdpAudioFormat& format) {
83 return nullptr;
84 }
85
86 template <typename U, typename... Us>
87 std::unique_ptr<AudioEncoder> DoMakeAudioEncoder(
88 Dummy<U, Us...>,
89 int payload_type,
90 const SdpAudioFormat& format) {
91 auto opt_config = U::SdpToConfig(format);
92 if (opt_config) {
93 RTC_CHECK(opt_config->IsOk());
94 auto enc = U::MakeAudioEncoder(*opt_config, payload_type);
95 RTC_CHECK(enc);
96 return enc;
97 } else {
98 return DoMakeAudioEncoder(Dummy<Us...>(), payload_type, format);
99 }
100 }
101 };
102
103 } // namespace audio_encoder_factory_template_impl
104
105 // Make an AudioEncoderFactory that can create instances of the given encoders.
106 //
107 // Each encoder type is given as a template argument to the function; it should
108 // be a struct with the following static member functions:
109 //
110 // // Converts |audio_format| to a ConfigType instance. Returns an empty
111 // // optional if |audio_format| doesn't correctly specify an encoder of our
112 // // type.
113 // rtc::Optional<ConfigType> SdpToConfig(const SdpAudioFormat& audio_format);
114 //
115 // // Appends zero or more AudioCodecSpecs to the list that will be returned
116 // // by AudioEncoderFactory::GetSupportedEncoders().
117 // void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs);
118 //
119 // // Returns information about how this format would be encoded. Used to
120 // // implement AudioEncoderFactory::QueryAudioEncoder().
121 // AudioCodecInfo QueryAudioEncoder(const ConfigType&);
the sun 2017/06/13 14:56:14 missing param name
kwiberg-webrtc 2017/06/13 19:28:35 Done.
122 //
123 // // Creates an AudioEncoder for the specified format. Used to implement
124 // // AudioEncoderFactory::MakeAudioEncoder().
125 // std::unique_ptr<AudioEncoder> MakeAudioEncoder(const ConfigType&,
the sun 2017/06/13 14:56:14 here too
kwiberg-webrtc 2017/06/13 19:28:35 Done.
126 // int payload_type);
127 //
128 // ConfigType should be a type that encapsulates all the settings needed to
129 // create an AudioDecoder. It must have an IsOk() member function that returns
130 // true if the settings are good, false if they are not.
131 //
132 // NOTE: This function is still under development and may change without notice.
133 template <typename... Ts>
134 rtc::scoped_refptr<AudioEncoderFactory> CreateAudioEncoderFactory() {
the sun 2017/06/13 14:56:14 Could this go first in the file, above impl?
kwiberg-webrtc 2017/06/13 19:28:35 No... maybe. It's subclassing AudioEncoderFactoryT
the sun 2017/06/13 20:30:06 Agree. I can live without such beauty.
135 // There's no technical reason we couldn't allow zero template parameters,
136 // but such a factory couldn't create any encoders, and callers can do this
137 // by mistake by simply forgetting the <> altogether. So we forbid it in
138 // order to prevent caller foot-shooting.
139 static_assert(sizeof...(Ts) >= 1,
140 "Caller must give at least one template parameter");
141
142 return rtc::scoped_refptr<AudioEncoderFactory>(
143 new rtc::RefCountedObject<
144 audio_encoder_factory_template_impl::AudioEncoderFactoryT<Ts...>>());
145 }
146
147 } // namespace webrtc
148
149 #endif // WEBRTC_API_AUDIO_CODECS_AUDIO_ENCODER_FACTORY_TEMPLATE_H_
OLDNEW
« no previous file with comments | « webrtc/api/audio_codecs/BUILD.gn ('k') | webrtc/api/audio_codecs/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698