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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "Codec2-AIDL-ParamTypes"
19 #include <android-base/logging.h>
20 
21 #include <android/binder_manager.h>
22 // NOTE: due to dependency from mainline modules cannot use libsysprop
23 // #include <android/sysprop/MediaProperties.sysprop.h>
24 #include <android-base/properties.h>
25 #include <codec2/aidl/ParamTypes.h>
26 #include <codec2/common/HalSelection.h>
27 #include <codec2/common/ParamTypes.h>
28 
29 #include "ParamTypes-specialization.h"
30 
31 namespace android {
32 
33 using ::aidl::android::hardware::media::c2::FieldId;
34 using ::aidl::android::hardware::media::c2::FieldSupportedValues;
35 using ::aidl::android::hardware::media::c2::Params;
36 using ::aidl::android::hardware::media::c2::Status;
37 using ::aidl::android::hardware::media::c2::ValueRange;
38 
39 // {offset, size} -> FieldId
40 template<>
SetFieldId(FieldId * d,uint32_t offset,uint32_t size)41 void SetFieldId(FieldId *d, uint32_t offset, uint32_t size) {
42     d->offset = offset;
43     d->sizeBytes = size;
44 }
45 
46 // FieldId -> offset
47 template<>
GetOffsetFromFieldId(const FieldId & s)48 uint32_t GetOffsetFromFieldId(const FieldId &s) {
49     return s.offset;
50 }
51 
52 // FieldId -> size
53 template<>
GetSizeFromFieldId(const FieldId & s)54 uint32_t GetSizeFromFieldId(const FieldId &s) {
55     return s.sizeBytes;
56 }
57 
58 template<>
SetStatus(Status * dst,c2_status_t src)59 void SetStatus(Status *dst, c2_status_t src) {
60     dst->status = src;
61 }
62 
63 template<>
GetStatus(const Status & status)64 c2_status_t GetStatus(const Status &status) {
65     return static_cast<c2_status_t>(status.status);
66 }
67 
68 static constexpr FieldSupportedValues::Tag EMPTY = FieldSupportedValues::empty;
69 static constexpr FieldSupportedValues::Tag RANGE = FieldSupportedValues::range;
70 static constexpr FieldSupportedValues::Tag VALUES = FieldSupportedValues::values;
71 static constexpr FieldSupportedValues::Tag FLAGS = FieldSupportedValues::flags;
72 
73 // C2FieldSupportedValues -> FieldSupportedValues
74 template<>
objcpy(FieldSupportedValues * d,const C2FieldSupportedValues & s)75 bool objcpy(FieldSupportedValues *d, const C2FieldSupportedValues &s) {
76     switch (s.type) {
77     case C2FieldSupportedValues::EMPTY: {
78             d->set<EMPTY>(true);
79             break;
80         }
81     case C2FieldSupportedValues::RANGE: {
82             ValueRange range{};
83             if (!objcpy(&range, s.range)) {
84                 LOG(ERROR) << "Invalid C2FieldSupportedValues::range.";
85                 d->set<RANGE>(range);
86                 return false;
87             }
88             d->set<RANGE>(range);
89             break;
90         }
91     case C2FieldSupportedValues::VALUES: {
92             std::vector<int64_t> values;
93             copyVector<int64_t>(&values, s.values);
94             d->set<VALUES>(values);
95             break;
96         }
97     case C2FieldSupportedValues::FLAGS: {
98             std::vector<int64_t> flags;
99             copyVector<int64_t>(&flags, s.values);
100             d->set<FLAGS>(flags);
101             break;
102         }
103     default:
104         LOG(DEBUG) << "Unrecognized C2FieldSupportedValues::type_t "
105                    << "with underlying value " << underlying_value(s.type)
106                    << ".";
107         return false;
108     }
109     return true;
110 }
111 
112 // FieldSupportedValues -> C2FieldSupportedValues
113 template<>
objcpy(C2FieldSupportedValues * d,const FieldSupportedValues & s)114 bool objcpy(C2FieldSupportedValues *d, const FieldSupportedValues &s) {
115     switch (s.getTag()) {
116     case FieldSupportedValues::empty: {
117             d->type = C2FieldSupportedValues::EMPTY;
118             break;
119         }
120     case FieldSupportedValues::range: {
121             d->type = C2FieldSupportedValues::RANGE;
122             if (!objcpy(&d->range, s.get<RANGE>())) {
123                 LOG(ERROR) << "Invalid FieldSupportedValues::range.";
124                 return false;
125             }
126             d->values.resize(0);
127             break;
128         }
129     case FieldSupportedValues::values: {
130             d->type = C2FieldSupportedValues::VALUES;
131             copyVector<uint64_t>(&d->values, s.get<VALUES>());
132             break;
133         }
134     case FieldSupportedValues::flags: {
135             d->type = C2FieldSupportedValues::FLAGS;
136             copyVector<uint64_t>(&d->values, s.get<FLAGS>());
137             break;
138         }
139     default:
140         LOG(WARNING) << "Unrecognized FieldSupportedValues::getDiscriminator()";
141         return false;
142     }
143     return true;
144 }
145 
146 template<>
GetBlob(const Params & params)147 const std::vector<uint8_t> &GetBlob<Params>(const Params &params) {
148     return params.params;
149 }
150 
151 template<>
GetBlob(Params * params)152 std::vector<uint8_t> *GetBlob<Params>(Params *params) {
153     return &params->params;
154 }
155 
156 } // namespace android
157 
158 namespace aidl {
159 namespace android {
160 namespace hardware {
161 namespace media {
162 namespace c2 {
163 namespace utils {
164 
IsSelected()165 bool IsSelected() {
166     return ::android::IsCodec2AidlHalSelected();
167 }
168 
asString(Status status,const char * def)169 const char* asString(Status status, const char* def) {
170     return asString(static_cast<c2_status_t>(status.status), def);
171 }
172 
173 // C2FieldSupportedValuesQuery -> FieldSupportedValuesQuery
ToAidl(FieldSupportedValuesQuery * d,const C2FieldSupportedValuesQuery & s)174 bool ToAidl(
175         FieldSupportedValuesQuery* d,
176         const C2FieldSupportedValuesQuery& s) {
177     return ::android::objcpy(d, nullptr, s);
178 }
179 
180 // FieldSupportedValuesQuery -> C2FieldSupportedValuesQuery
FromAidl(C2FieldSupportedValuesQuery * d,const FieldSupportedValuesQuery & s)181 bool FromAidl(
182         C2FieldSupportedValuesQuery* d,
183         const FieldSupportedValuesQuery& s) {
184     return ::android::objcpy(d, s);
185 }
186 
187 // C2FieldSupportedValuesQuery -> FieldSupportedValuesQueryResult
ToAidl(FieldSupportedValuesQueryResult * d,const C2FieldSupportedValuesQuery & s)188 bool ToAidl(
189         FieldSupportedValuesQueryResult* d,
190         const C2FieldSupportedValuesQuery& s) {
191     return ::android::objcpy(nullptr, d, s);
192 }
193 
194 // FieldSupportedValuesQuery, FieldSupportedValuesQueryResult ->
195 // C2FieldSupportedValuesQuery
FromAidl(C2FieldSupportedValuesQuery * d,const FieldSupportedValuesQuery & sq,const FieldSupportedValuesQueryResult & sr)196 bool FromAidl(
197         C2FieldSupportedValuesQuery* d,
198         const FieldSupportedValuesQuery& sq,
199         const FieldSupportedValuesQueryResult& sr) {
200     return ::android::objcpy(d, sq, sr);
201 }
202 
203 // C2Component::Traits -> IComponentStore::ComponentTraits
ToAidl(IComponentStore::ComponentTraits * d,const C2Component::Traits & s)204 bool ToAidl(
205         IComponentStore::ComponentTraits *d,
206         const C2Component::Traits &s) {
207     return ::android::objcpy(d, s);
208 }
209 
210 // ComponentTraits -> C2Component::Traits, std::unique_ptr<std::vector<std::string>>
FromAidl(C2Component::Traits * d,const IComponentStore::ComponentTraits & s)211 bool FromAidl(
212         C2Component::Traits* d,
213         const IComponentStore::ComponentTraits& s) {
214     return ::android::objcpy(d, s);
215 }
216 
217 // C2SettingResult -> SettingResult
ToAidl(SettingResult * d,const C2SettingResult & s)218 bool ToAidl(SettingResult *d, const C2SettingResult &s) {
219     return ::android::objcpy(d, s);
220 }
221 
222 // SettingResult -> std::unique_ptr<C2SettingResult>
FromAidl(std::unique_ptr<C2SettingResult> * d,const SettingResult & s)223 bool FromAidl(std::unique_ptr<C2SettingResult> *d, const SettingResult &s) {
224     return ::android::objcpy(d, s);
225 }
226 
227 // C2ParamDescriptor -> ParamDescriptor
ToAidl(ParamDescriptor * d,const C2ParamDescriptor & s)228 bool ToAidl(ParamDescriptor *d, const C2ParamDescriptor &s) {
229     return ::android::objcpy(d, s);
230 }
231 
232 // ParamDescriptor -> C2ParamDescriptor
FromAidl(std::shared_ptr<C2ParamDescriptor> * d,const ParamDescriptor & s)233 bool FromAidl(std::shared_ptr<C2ParamDescriptor> *d, const ParamDescriptor &s) {
234     return ::android::objcpy(d, s);
235 }
236 
237 // C2StructDescriptor -> StructDescriptor
ToAidl(StructDescriptor * d,const C2StructDescriptor & s)238 bool ToAidl(StructDescriptor *d, const C2StructDescriptor &s) {
239     return ::android::objcpy(d, s);
240 }
241 
242 // StructDescriptor -> C2StructDescriptor
FromAidl(std::unique_ptr<C2StructDescriptor> * d,const StructDescriptor & s)243 bool FromAidl(std::unique_ptr<C2StructDescriptor> *d, const StructDescriptor &s) {
244     return ::android::objcpy(d, s);
245 }
246 
247 // Params -> std::vector<C2Param*>
ParseParamsBlob(std::vector<C2Param * > * params,const Params & blob)248 bool ParseParamsBlob(std::vector<C2Param*> *params, const Params &blob) {
249     return ::android::parseParamsBlob(params, blob);
250 }
251 
252 // std::vector<const C2Param*> -> Params
CreateParamsBlob(Params * blob,const std::vector<const C2Param * > & params)253 bool CreateParamsBlob(
254         Params *blob,
255         const std::vector<const C2Param*> &params) {
256     return ::android::_createParamsBlob(blob, params);
257 }
258 
259 // std::vector<C2Param*> -> Params
CreateParamsBlob(Params * blob,const std::vector<C2Param * > & params)260 bool CreateParamsBlob(
261         Params *blob,
262         const std::vector<C2Param*> &params) {
263     return ::android::_createParamsBlob(blob, params);
264 }
265 
266 // std::vector<std::unique_ptr<C2Param>> -> Params
CreateParamsBlob(Params * blob,const std::vector<std::unique_ptr<C2Param>> & params)267 bool CreateParamsBlob(
268         Params *blob,
269         const std::vector<std::unique_ptr<C2Param>> &params) {
270     return ::android::_createParamsBlob(blob, params);
271 }
272 
273 // std::vector<std::unique_ptr<C2Tuning>> -> Params
CreateParamsBlob(Params * blob,const std::vector<std::unique_ptr<C2Tuning>> & params)274 bool CreateParamsBlob(
275         Params *blob,
276         const std::vector<std::unique_ptr<C2Tuning>> &params) {
277     return ::android::_createParamsBlob(blob, params);
278 }
279 
280 // std::vector<std::shared_ptr<const C2Info>> -> Params
CreateParamsBlob(Params * blob,const std::vector<std::shared_ptr<const C2Info>> & params)281 bool CreateParamsBlob(
282         Params *blob,
283         const std::vector<std::shared_ptr<const C2Info>> &params) {
284     return ::android::_createParamsBlob(blob, params);
285 }
286 
287 // Params -> std::vector<std::unique_ptr<C2Param>>
CopyParamsFromBlob(std::vector<std::unique_ptr<C2Param>> * params,Params blob)288 bool CopyParamsFromBlob(
289         std::vector<std::unique_ptr<C2Param>>* params,
290         Params blob) {
291     return ::android::_copyParamsFromBlob(params, blob);
292 }
293 
294 // Params -> std::vector<std::unique_ptr<C2Tuning>>
CopyParamsFromBlob(std::vector<std::unique_ptr<C2Tuning>> * params,Params blob)295 bool CopyParamsFromBlob(
296         std::vector<std::unique_ptr<C2Tuning>>* params,
297         Params blob) {
298     return ::android::_copyParamsFromBlob(params, blob);
299 }
300 
301 // Params -> update std::vector<std::unique_ptr<C2Param>>
UpdateParamsFromBlob(const std::vector<C2Param * > & params,const Params & blob)302 bool UpdateParamsFromBlob(
303         const std::vector<C2Param*>& params,
304         const Params& blob) {
305     return ::android::updateParamsFromBlob(params, blob);
306 }
307 
308 }  // namespace utils
309 }  // namespace c2
310 }  // namespace media
311 }  // namespace hardware
312 }  // namespace android
313 }  // namespace aidl
314