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

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

Issue 2936773005: Templated AudioDecoderFactory (Closed)
Patch Set: rebase 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_DECODER_FACTORY_TEMPLATE_H_
12 #define WEBRTC_API_AUDIO_CODECS_AUDIO_DECODER_FACTORY_TEMPLATE_H_
13
14 #include <memory>
15 #include <vector>
16
17 #include "webrtc/api/audio_codecs/audio_decoder_factory.h"
18 #include "webrtc/base/scoped_ref_ptr.h"
19
20 namespace webrtc {
21
22 namespace audio_decoder_factory_template_impl {
23
24 template <typename... Ts>
25 class AudioDecoderFactoryT : public AudioDecoderFactory {
26 public:
27 std::vector<AudioCodecSpec> GetSupportedDecoders() override {
28 static const std::vector<AudioCodecSpec> specs = [] {
29 std::vector<AudioCodecSpec> specs;
30 DoAppendSupportedDecoders(Dummy<Ts...>(), &specs);
31 return specs;
32 }();
33 return specs;
34 }
35
36 bool IsSupportedDecoder(const SdpAudioFormat& format) override {
37 return DoIsSupportedDecoder(Dummy<Ts...>(), format);
38 }
39
40 std::unique_ptr<AudioDecoder> MakeAudioDecoder(
41 const SdpAudioFormat& format) override {
42 return DoMakeAudioDecoder(Dummy<Ts...>(), format);
43 }
44
45 private:
46 // Dummy type that's used to ensure we call the proper overloads.
47 template <typename...>
ossu 2017/06/16 11:25:18 As with the encoder factory, this one could do wit
kwiberg-webrtc 2017/06/16 13:17:06 Yes. The decoder factory is very similar to the en
48 struct Dummy {};
49
50 static void DoAppendSupportedDecoders(Dummy<>,
51 std::vector<AudioCodecSpec>* specs) {}
52
53 template <typename U, typename... Us>
54 static void DoAppendSupportedDecoders(Dummy<U, Us...>,
55 std::vector<AudioCodecSpec>* specs) {
56 U::AppendSupportedDecoders(specs);
57 DoAppendSupportedDecoders(Dummy<Us...>(), specs);
58 }
59
60 bool DoIsSupportedDecoder(Dummy<>, const SdpAudioFormat& format) {
61 return false;
62 }
63
64 template <typename U, typename... Us>
65 bool DoIsSupportedDecoder(Dummy<U, Us...>, const SdpAudioFormat& format) {
66 auto opt_config = U::SdpToConfig(format);
67 return opt_config ? true : DoIsSupportedDecoder(Dummy<Us...>(), format);
68 }
69
70 std::unique_ptr<AudioDecoder> DoMakeAudioDecoder(
71 Dummy<>,
72 const SdpAudioFormat& format) {
73 return nullptr;
74 }
75
76 template <typename U, typename... Us>
77 std::unique_ptr<AudioDecoder> DoMakeAudioDecoder(
78 Dummy<U, Us...>,
79 const SdpAudioFormat& format) {
80 auto opt_config = U::SdpToConfig(format);
81 if (opt_config) {
82 std::unique_ptr<AudioDecoder> dec = U::MakeAudioDecoder(*opt_config);
83 RTC_CHECK(dec);
84 return dec;
85 } else {
86 return DoMakeAudioDecoder(Dummy<Us...>(), format);
87 }
88 }
89 };
90
91 } // namespace audio_decoder_factory_template_impl
92
93 // Make an AudioDecoderFactory that can create instances of the given decoders.
94 //
95 // Each decoder type is given as a template argument to the function; it should
96 // be a struct with the following static member functions:
97 //
98 // // Converts |audio_format| to a ConfigType instance. Returns an empty
99 // // optional if |audio_format| doesn't correctly specify an decoder of our
100 // // type.
101 // rtc::Optional<ConfigType> SdpToConfig(const SdpAudioFormat& audio_format);
102 //
103 // // Appends zero or more AudioCodecSpecs to the list that will be returned
104 // // by AudioDecoderFactory::GetSupportedDecoders().
105 // void AppendSupportedDecoders(std::vector<AudioCodecSpec>* specs);
106 //
107 // // Creates an AudioDecoder for the specified format. Used to implement
108 // // AudioDecoderFactory::MakeAudioDecoder().
109 // std::unique_ptr<AudioDecoder> MakeAudioDecoder(const ConfigType& config);
the sun 2017/06/16 12:52:21 Inconsistent with the decoder impls which use by-v
kwiberg-webrtc 2017/06/16 13:17:06 It's just an implementation detail. const ref is p
110 //
111 // ConfigType should be a type that encapsulates all the settings needed to
112 // create an AudioDecoder.
113 //
114 // NOTE: This function is still under development and may change without notice.
ossu 2017/06/16 11:25:18 Should probably mention that the order of the code
kwiberg-webrtc 2017/06/16 13:17:06 Yes, that'll be among the things copied over from
115 template <typename... Ts>
116 rtc::scoped_refptr<AudioDecoderFactory> CreateAudioDecoderFactory() {
117 // There's no technical reason we couldn't allow zero template parameters,
118 // but such a factory couldn't create any decoders, and callers can do this
119 // by mistake by simply forgetting the <> altogether. So we forbid it in
120 // order to prevent caller foot-shooting.
121 static_assert(sizeof...(Ts) >= 1,
122 "Caller must give at least one template parameter");
123
124 return rtc::scoped_refptr<AudioDecoderFactory>(
125 new rtc::RefCountedObject<
126 audio_decoder_factory_template_impl::AudioDecoderFactoryT<Ts...>>());
127 }
128
129 } // namespace webrtc
130
131 #endif // WEBRTC_API_AUDIO_CODECS_AUDIO_DECODER_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