1 /*
2  * Copyright 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 CODEC2_AIDL_UTILS_CONFIGURABLE_H
18 #define CODEC2_AIDL_UTILS_CONFIGURABLE_H
19 
20 #include <aidl/android/hardware/media/c2/BnConfigurable.h>
21 
22 #include <C2Component.h>
23 #include <C2Param.h>
24 #include <C2.h>
25 
26 #include <memory>
27 
28 namespace aidl {
29 namespace android {
30 namespace hardware {
31 namespace media {
32 namespace c2 {
33 namespace utils {
34 
35 struct ComponentStore;
36 
37 /**
38  * Codec2 objects of different types may have different querying and configuring
39  * functions, but across the Treble boundary, they share the same HIDL
40  * interface, IConfigurable.
41  *
42  * ConfigurableC2Intf is an abstract class that a Codec2 object can implement to
43  * easily expose an IConfigurable instance. See CachedConfigurable below.
44  */
45 struct ConfigurableC2Intf {
getNameConfigurableC2Intf46     C2String getName() const { return mName; }
getIdConfigurableC2Intf47     uint32_t getId() const { return mId; }
48     /** C2ComponentInterface::query_vb sans stack params */
49     virtual c2_status_t query(
50             const std::vector<C2Param::Index> &indices,
51             c2_blocking_t mayBlock,
52             std::vector<std::unique_ptr<C2Param>>* const params) const = 0;
53     /** C2ComponentInterface::config_vb */
54     virtual c2_status_t config(
55             const std::vector<C2Param*> &params,
56             c2_blocking_t mayBlock,
57             std::vector<std::unique_ptr<C2SettingResult>>* const failures) = 0;
58     /** C2ComponentInterface::querySupportedParams_nb */
59     virtual c2_status_t querySupportedParams(
60             std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const = 0;
61     /** C2ComponentInterface::querySupportedParams_nb */
62     virtual c2_status_t querySupportedValues(
63             std::vector<C2FieldSupportedValuesQuery>& fields, c2_blocking_t mayBlock) const = 0;
64 
65     virtual ~ConfigurableC2Intf() = default;
66 
ConfigurableC2IntfConfigurableC2Intf67     ConfigurableC2Intf(const C2String& name, uint32_t id)
68           : mName{name}, mId{id} {}
69 
70 protected:
71     C2String mName; /* cached component name */
72     uint32_t mId;
73 };
74 
75 /**
76  * Type for validating and caching parameters when CachedConfigurable is
77  * initialized.
78  *
79  * This is meant to be created by the ComponentStore. The purpose of abstracting
80  * this is to allow different versions of ComponentStore to work with this
81  * CachedConfigurable.
82  */
83 struct ParameterCache {
84     virtual c2_status_t validate(
85             const std::vector<std::shared_ptr<C2ParamDescriptor>>&) = 0;
86     virtual ~ParameterCache() = default;
87 };
88 
89 /**
90  * Implementation of the IConfigurable interface that supports caching of
91  * supported parameters from a supplied ComponentStore.
92  *
93  * CachedConfigurable essentially converts a ConfigurableC2Intf into HIDL's
94  * IConfigurable. A Codec2 object generally implements ConfigurableC2Intf and
95  * passes the implementation to the constructor of CachedConfigurable.
96  *
97  * Note that caching happens
98  */
99 struct CachedConfigurable : public BnConfigurable {
100     CachedConfigurable(std::unique_ptr<ConfigurableC2Intf>&& intf);
101 
102     // Populates mSupportedParams.
103     c2_status_t init(const std::shared_ptr<ParameterCache> &cache);
104 
105     // Methods from ::android::hardware::media::c2::V1_0::IConfigurable
106 
107     virtual ::ndk::ScopedAStatus getId(int32_t* id) override;
108 
109     virtual ::ndk::ScopedAStatus getName(std::string* name) override;
110 
111     virtual ::ndk::ScopedAStatus query(
112             const std::vector<int32_t>& indices,
113             bool mayBlock,
114             QueryResult* result) override;
115 
116     virtual ::ndk::ScopedAStatus config(
117             const ::aidl::android::hardware::media::c2::Params& params,
118             bool mayBlock,
119             ConfigResult* result) override;
120 
121     virtual ::ndk::ScopedAStatus querySupportedParams(
122             int32_t start,
123             int32_t count,
124             std::vector<ParamDescriptor>* paramDesc) override;
125 
126     virtual ::ndk::ScopedAStatus querySupportedValues(
127             const std::vector<FieldSupportedValuesQuery>& fields,
128             bool mayBlock,
129             QuerySupportedValuesResult* result) override;
130 
131 protected:
132     // Common Codec2.0 interface wrapper
133     std::unique_ptr<ConfigurableC2Intf> mIntf;
134 
135     // Cached supported params
136     std::vector<std::shared_ptr<C2ParamDescriptor>> mSupportedParams;
137 };
138 
139 }  // namespace utils
140 }  // namespace c2
141 }  // namespace media
142 }  // namespace hardware
143 }  // namespace android
144 }  // namespace aidl
145 
146 #endif  // CODEC2_AIDL_UTILS_CONFIGURABLE_H
147 
148