1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_SIMPLE_C2_INTERFACE_H_
18 #define ANDROID_SIMPLE_C2_INTERFACE_H_
19 
20 #include <C2Component.h>
21 #include <C2Config.h>
22 #include <util/C2InterfaceHelper.h>
23 
24 namespace android {
25 
26 /**
27  * Wrap a common interface object (such as Codec2Client::Interface, or
28  * C2InterfaceHelper into a C2ComponentInterface.
29  *
30  * \param T common interface type
31  */
32 template <typename T> class SimpleC2Interface : public C2ComponentInterface {
33   public:
SimpleC2Interface(const char * name,c2_node_id_t id,const std::shared_ptr<T> & impl)34     SimpleC2Interface(const char *name, c2_node_id_t id,
35                       const std::shared_ptr<T> &impl)
36         : mName(name), mId(id), mImpl(impl) {}
37 
38     ~SimpleC2Interface() override = default;
39 
40     // From C2ComponentInterface
getName()41     C2String getName() const override { return mName; }
getId()42     c2_node_id_t getId() const override { return mId; }
query_vb(const std::vector<C2Param * > & stackParams,const std::vector<C2Param::Index> & heapParamIndices,c2_blocking_t mayBlock,std::vector<std::unique_ptr<C2Param>> * const heapParams)43     c2_status_t query_vb(const std::vector<C2Param *> &stackParams,
44                          const std::vector<C2Param::Index> &heapParamIndices,
45                          c2_blocking_t mayBlock,
46                          std::vector<std::unique_ptr<C2Param>>
47                              *const heapParams) const override {
48         return mImpl->query(stackParams, heapParamIndices, mayBlock,
49                             heapParams);
50     }
51     c2_status_t
config_vb(const std::vector<C2Param * > & params,c2_blocking_t mayBlock,std::vector<std::unique_ptr<C2SettingResult>> * const failures)52     config_vb(const std::vector<C2Param *> &params, c2_blocking_t mayBlock,
53               std::vector<std::unique_ptr<C2SettingResult>> *const failures)
54         override {
55         return mImpl->config(params, mayBlock, failures);
56     }
createTunnel_sm(c2_node_id_t)57     c2_status_t createTunnel_sm(c2_node_id_t) override { return C2_OMITTED; }
releaseTunnel_sm(c2_node_id_t)58     c2_status_t releaseTunnel_sm(c2_node_id_t) override { return C2_OMITTED; }
querySupportedParams_nb(std::vector<std::shared_ptr<C2ParamDescriptor>> * const params)59     c2_status_t querySupportedParams_nb(
60         std::vector<std::shared_ptr<C2ParamDescriptor>> *const params)
61         const override {
62         return mImpl->querySupportedParams(params);
63     }
64     c2_status_t
querySupportedValues_vb(std::vector<C2FieldSupportedValuesQuery> & fields,c2_blocking_t mayBlock)65     querySupportedValues_vb(std::vector<C2FieldSupportedValuesQuery> &fields,
66                             c2_blocking_t mayBlock) const override {
67         return mImpl->querySupportedValues(fields, mayBlock);
68     }
69 
70   private:
71     C2String mName;
72     const c2_node_id_t mId;
73     const std::shared_ptr<T> mImpl;
74 };
75 
76 /**
77  * Utility classes for common interfaces.
78  */
79 template <> class SimpleC2Interface<void> {
80   public:
81     /**
82      * Base Codec 2.0 parameters required for all components.
83      */
84     struct BaseParams : C2InterfaceHelper {
85         explicit BaseParams(
86             const std::shared_ptr<C2ReflectorHelper> &helper, C2String name,
87             C2Component::kind_t kind, C2Component::domain_t domain,
88             C2String mediaType,
89             std::vector<C2String> aliases = std::vector<C2String>());
90 
91         /// Marks that this component has no input latency. Otherwise, component
92         /// must add support for C2PortRequestedDelayTuning::input and
93         /// C2PortActualDelayTuning::input.
94         void noInputLatency();
95 
96         /// Marks that this component has no output latency. Otherwise,
97         /// component must add support for C2PortRequestedDelayTuning::output
98         /// and C2PortActualDelayTuning::output.
99         void noOutputLatency();
100 
101         /// Marks that this component has no pipeline latency. Otherwise,
102         /// component must add support for C2RequestedPipelineDelayTuning and
103         /// C2ActualPipelineDelayTuning.
104         void noPipelineLatency();
105 
106         /// Marks that this component has no need for private buffers.
107         /// Otherwise, component must add support for
108         /// C2MaxPrivateBufferCountTuning, C2PrivateAllocatorsTuning and
109         /// C2PrivateBlockPoolsTuning.
110         void noPrivateBuffers();
111 
112         /// Marks that this component holds no references to input buffers.
113         /// Otherwise, component must add support for
114         /// C2StreamMaxReferenceAgeTuning::input and
115         /// C2StreamMaxReferenceCountTuning::input.
116         void noInputReferences();
117 
118         /// Marks that this component holds no references to output buffers.
119         /// Otherwise, component must add support for
120         /// C2StreamMaxReferenceAgeTuning::output and
121         /// C2StreamMaxReferenceCountTuning::output.
122         void noOutputReferences();
123 
124         /// Marks that this component does not stretch time. Otherwise,
125         /// component must add support for C2ComponentTimeStretchTuning.
126         void noTimeStretch();
127 
128         std::shared_ptr<C2ApiLevelSetting> mApiLevel;
129         std::shared_ptr<C2ApiFeaturesSetting> mApiFeatures;
130 
131         std::shared_ptr<C2PlatformLevelSetting> mPlatformLevel;
132         std::shared_ptr<C2PlatformFeaturesSetting> mPlatformFeatures;
133 
134         std::shared_ptr<C2ComponentNameSetting> mName;
135         std::shared_ptr<C2ComponentAliasesSetting> mAliases;
136         std::shared_ptr<C2ComponentKindSetting> mKind;
137         std::shared_ptr<C2ComponentDomainSetting> mDomain;
138         std::shared_ptr<C2ComponentAttributesSetting> mAttrib;
139         std::shared_ptr<C2ComponentTimeStretchTuning> mTimeStretch;
140 
141         std::shared_ptr<C2PortMediaTypeSetting::input> mInputMediaType;
142         std::shared_ptr<C2PortMediaTypeSetting::output> mOutputMediaType;
143         std::shared_ptr<C2StreamBufferTypeSetting::input> mInputFormat;
144         std::shared_ptr<C2StreamBufferTypeSetting::output> mOutputFormat;
145 
146         std::shared_ptr<C2PortRequestedDelayTuning::input> mRequestedInputDelay;
147         std::shared_ptr<C2PortRequestedDelayTuning::output>
148             mRequestedOutputDelay;
149         std::shared_ptr<C2RequestedPipelineDelayTuning> mRequestedPipelineDelay;
150 
151         std::shared_ptr<C2PortActualDelayTuning::input> mActualInputDelay;
152         std::shared_ptr<C2PortActualDelayTuning::output> mActualOutputDelay;
153         std::shared_ptr<C2ActualPipelineDelayTuning> mActualPipelineDelay;
154 
155         std::shared_ptr<C2StreamMaxReferenceAgeTuning::input>
156             mMaxInputReferenceAge;
157         std::shared_ptr<C2StreamMaxReferenceCountTuning::input>
158             mMaxInputReferenceCount;
159         std::shared_ptr<C2StreamMaxReferenceAgeTuning::output>
160             mMaxOutputReferenceAge;
161         std::shared_ptr<C2StreamMaxReferenceCountTuning::output>
162             mMaxOutputReferenceCount;
163         std::shared_ptr<C2MaxPrivateBufferCountTuning> mMaxPrivateBufferCount;
164 
165         std::shared_ptr<C2PortStreamCountTuning::input> mInputStreamCount;
166         std::shared_ptr<C2PortStreamCountTuning::output> mOutputStreamCount;
167 
168         std::shared_ptr<C2SubscribedParamIndicesTuning> mSubscribedParamIndices;
169         std::shared_ptr<C2PortSuggestedBufferCountTuning::input>
170             mSuggestedInputBufferCount;
171         std::shared_ptr<C2PortSuggestedBufferCountTuning::output>
172             mSuggestedOutputBufferCount;
173 
174         std::shared_ptr<C2CurrentWorkTuning> mCurrentWorkOrdinal;
175         std::shared_ptr<C2LastWorkQueuedTuning::input>
176             mLastInputQueuedWorkOrdinal;
177         std::shared_ptr<C2LastWorkQueuedTuning::output>
178             mLastOutputQueuedWorkOrdinal;
179 
180         std::shared_ptr<C2PortAllocatorsTuning::input> mInputAllocators;
181         std::shared_ptr<C2PortAllocatorsTuning::output> mOutputAllocators;
182         std::shared_ptr<C2PrivateAllocatorsTuning> mPrivateAllocators;
183         std::shared_ptr<C2PortBlockPoolsTuning::output> mOutputPoolIds;
184         std::shared_ptr<C2PrivateBlockPoolsTuning> mPrivatePoolIds;
185 
186         std::shared_ptr<C2TrippedTuning> mTripped;
187         std::shared_ptr<C2OutOfMemoryTuning> mOutOfMemory;
188 
189         std::shared_ptr<C2PortConfigCounterTuning::input> mInputConfigCounter;
190         std::shared_ptr<C2PortConfigCounterTuning::output> mOutputConfigCounter;
191         std::shared_ptr<C2ConfigCounterTuning> mDirectConfigCounter;
192     };
193 };
194 
195 template <typename T> using SimpleInterface = SimpleC2Interface<T>;
196 
197 template <typename T, typename... Args>
AllocSharedString(const Args (&...args),const char * str)198 std::shared_ptr<T> AllocSharedString(const Args(&...args), const char *str) {
199     size_t len = strlen(str) + 1;
200     std::shared_ptr<T> ret = T::AllocShared(len, args...);
201     strcpy(ret->m.value, str);
202     return ret;
203 }
204 
205 template <typename T, typename... Args>
AllocSharedString(const Args (&...args),const std::string & str)206 std::shared_ptr<T> AllocSharedString(const Args(&...args),
207                                      const std::string &str) {
208     std::shared_ptr<T> ret = T::AllocShared(str.length() + 1, args...);
209     strcpy(ret->m.value, str.c_str());
210     return ret;
211 }
212 
213 template <typename T> struct Setter {
214     typedef typename std::remove_reference<T>::type type;
215 
NonStrictValueWithNoDepsSetter216     static C2R NonStrictValueWithNoDeps(bool mayBlock,
217                                         C2InterfaceHelper::C2P<type> &me) {
218         (void)mayBlock;
219         return me.F(me.v.value).validatePossible(me.v.value);
220     }
221 
NonStrictValuesWithNoDepsSetter222     static C2R NonStrictValuesWithNoDeps(bool mayBlock,
223                                          C2InterfaceHelper::C2P<type> &me) {
224         (void)mayBlock;
225         C2R res = C2R::Ok();
226         for (size_t ix = 0; ix < me.v.flexCount(); ++ix) {
227             res.plus(
228                 me.F(me.v.m.values[ix]).validatePossible(me.v.m.values[ix]));
229         }
230         return res;
231     }
232 
StrictValueWithNoDepsSetter233     static C2R StrictValueWithNoDeps(bool mayBlock,
234                                      const C2InterfaceHelper::C2P<type> &old,
235                                      C2InterfaceHelper::C2P<type> &me) {
236         (void)mayBlock;
237         if (!me.F(me.v.value).supportsNow(me.v.value)) {
238             me.set().value = old.v.value;
239         }
240         return me.F(me.v.value).validatePossible(me.v.value);
241     }
242 };
243 
244 } // namespace android
245 
246 #endif // ANDROID_SIMPLE_C2_INTERFACE_H_
247