• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #include <cstdint>
18 #include <inttypes.h>
19 #include <utility>
20 
21 #define LOG_TAG "AidlConversionEffect"
22 //#define LOG_NDEBUG 0
23 #include <utils/Log.h>
24 
25 #include <aidl/android/hardware/audio/effect/DefaultExtension.h>
26 #include <aidl/android/hardware/audio/effect/VendorExtension.h>
27 #include <media/AidlConversionCppNdk.h>
28 #include <media/AidlConversionEffect.h>
29 
30 ////////////////////////////////////////////////////////////////////////////////////////////////////
31 // AIDL NDK backend to legacy audio data structure conversion utilities.
32 
33 namespace aidl {
34 namespace android {
35 
36 using ::aidl::android::hardware::audio::effect::AcousticEchoCanceler;
37 using ::aidl::android::hardware::audio::effect::AutomaticGainControlV2;
38 using ::aidl::android::hardware::audio::effect::BassBoost;
39 using ::aidl::android::hardware::audio::effect::DefaultExtension;
40 using ::aidl::android::hardware::audio::effect::Descriptor;
41 using ::aidl::android::hardware::audio::effect::Downmix;
42 using ::aidl::android::hardware::audio::effect::DynamicsProcessing;
43 using ::aidl::android::hardware::audio::effect::Flags;
44 using ::aidl::android::hardware::audio::effect::Parameter;
45 using ::aidl::android::hardware::audio::effect::PresetReverb;
46 using ::aidl::android::hardware::audio::effect::VendorExtension;
47 using ::aidl::android::hardware::audio::effect::Visualizer;
48 using ::aidl::android::media::audio::common::AudioDeviceDescription;
49 
50 using ::android::BAD_VALUE;
51 using ::android::OK;
52 using ::android::status_t;
53 using ::android::base::unexpected;
54 using ::android::effect::utils::EffectParamReader;
55 using ::android::effect::utils::EffectParamWrapper;
56 using ::android::effect::utils::EffectParamWriter;
57 
58 ////////////////////////////////////////////////////////////////////////////////////////////////////
59 // Converters
60 
aidl2legacy_Flags_Type_uint32(Flags::Type type)61 ConversionResult<uint32_t> aidl2legacy_Flags_Type_uint32(Flags::Type type) {
62     switch (type) {
63         case Flags::Type::INSERT:
64             return EFFECT_FLAG_TYPE_INSERT;
65         case Flags::Type::AUXILIARY:
66             return EFFECT_FLAG_TYPE_AUXILIARY;
67         case Flags::Type::REPLACE:
68             return EFFECT_FLAG_TYPE_REPLACE;
69         case Flags::Type::PRE_PROC:
70             return EFFECT_FLAG_TYPE_PRE_PROC;
71         case Flags::Type::POST_PROC:
72             return EFFECT_FLAG_TYPE_POST_PROC;
73     }
74     return unexpected(BAD_VALUE);
75 }
76 
legacy2aidl_uint32_Flags_Type(uint32_t legacy)77 ConversionResult<Flags::Type> legacy2aidl_uint32_Flags_Type(uint32_t legacy) {
78     switch (legacy & EFFECT_FLAG_TYPE_MASK) {
79         case EFFECT_FLAG_TYPE_INSERT:
80             return Flags::Type::INSERT;
81         case EFFECT_FLAG_TYPE_AUXILIARY:
82             return Flags::Type::AUXILIARY;
83         case EFFECT_FLAG_TYPE_REPLACE:
84             return Flags::Type::REPLACE;
85         case EFFECT_FLAG_TYPE_PRE_PROC:
86             return Flags::Type::PRE_PROC;
87         case EFFECT_FLAG_TYPE_POST_PROC:
88             return Flags::Type::POST_PROC;
89     }
90     return unexpected(BAD_VALUE);
91 }
92 
aidl2legacy_Flags_Insert_uint32(Flags::Insert insert)93 ConversionResult<uint32_t> aidl2legacy_Flags_Insert_uint32(Flags::Insert insert) {
94     switch (insert) {
95         case Flags::Insert::ANY:
96             return EFFECT_FLAG_INSERT_ANY;
97         case Flags::Insert::FIRST:
98             return EFFECT_FLAG_INSERT_FIRST;
99         case Flags::Insert::LAST:
100             return EFFECT_FLAG_INSERT_LAST;
101         case Flags::Insert::EXCLUSIVE:
102             return EFFECT_FLAG_INSERT_EXCLUSIVE;
103     }
104     return unexpected(BAD_VALUE);
105 }
106 
legacy2aidl_uint32_Flags_Insert(uint32_t legacy)107 ConversionResult<Flags::Insert> legacy2aidl_uint32_Flags_Insert(uint32_t legacy) {
108     switch (legacy & EFFECT_FLAG_INSERT_MASK) {
109         case EFFECT_FLAG_INSERT_ANY:
110             return Flags::Insert::ANY;
111         case EFFECT_FLAG_INSERT_FIRST:
112             return Flags::Insert::FIRST;
113         case EFFECT_FLAG_INSERT_LAST:
114             return Flags::Insert::LAST;
115         case EFFECT_FLAG_INSERT_EXCLUSIVE:
116             return Flags::Insert::EXCLUSIVE;
117     }
118     return unexpected(BAD_VALUE);
119 }
120 
aidl2legacy_Flags_Volume_uint32(Flags::Volume volume)121 ConversionResult<uint32_t> aidl2legacy_Flags_Volume_uint32(Flags::Volume volume) {
122     switch (volume) {
123         case Flags::Volume::NONE:
124             return 0;
125         case Flags::Volume::CTRL:
126             return EFFECT_FLAG_VOLUME_CTRL;
127         case Flags::Volume::IND:
128             return EFFECT_FLAG_VOLUME_IND;
129         case Flags::Volume::MONITOR:
130             return EFFECT_FLAG_VOLUME_MONITOR;
131     }
132     return unexpected(BAD_VALUE);
133 }
134 
legacy2aidl_uint32_Flags_Volume(uint32_t legacy)135 ConversionResult<Flags::Volume> legacy2aidl_uint32_Flags_Volume(uint32_t legacy) {
136     switch (legacy & EFFECT_FLAG_VOLUME_MASK) {
137         case EFFECT_FLAG_VOLUME_CTRL:
138             return Flags::Volume::CTRL;
139         case EFFECT_FLAG_VOLUME_IND:
140             return Flags::Volume::IND;
141         case EFFECT_FLAG_VOLUME_MONITOR:
142             return Flags::Volume::MONITOR;
143         case EFFECT_FLAG_VOLUME_NONE:
144             return Flags::Volume::NONE;
145     }
146     return unexpected(BAD_VALUE);
147 }
148 
aidl2legacy_Flags_uint32(Flags aidl)149 ConversionResult<uint32_t> aidl2legacy_Flags_uint32(Flags aidl) {
150     uint32_t legacy = 0;
151     legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_Type_uint32(aidl.type));
152     legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_Insert_uint32(aidl.insert));
153     legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_Volume_uint32(aidl.volume));
154     legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_HardwareAccelerator_uint32(aidl.hwAcceleratorMode));
155 
156     if (aidl.offloadIndication) {
157         legacy |= EFFECT_FLAG_OFFLOAD_SUPPORTED;
158     }
159     if (aidl.deviceIndication) {
160         legacy |= EFFECT_FLAG_DEVICE_IND;
161     }
162     if (aidl.audioModeIndication) {
163         legacy |= EFFECT_FLAG_AUDIO_MODE_IND;
164     }
165     if (aidl.audioSourceIndication) {
166         legacy |= EFFECT_FLAG_AUDIO_SOURCE_IND;
167     }
168     if (aidl.bypass) {
169         legacy |= EFFECT_FLAG_NO_PROCESS;
170     }
171     return legacy;
172 }
173 
legacy2aidl_uint32_Flags(uint32_t legacy)174 ConversionResult<Flags> legacy2aidl_uint32_Flags(uint32_t legacy) {
175     Flags aidl;
176 
177     aidl.type = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_Type(legacy));
178     aidl.insert = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_Insert(legacy));
179     aidl.volume = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_Volume(legacy));
180     aidl.hwAcceleratorMode = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_HardwareAccelerator(legacy));
181     aidl.offloadIndication = (legacy & EFFECT_FLAG_OFFLOAD_SUPPORTED);
182     aidl.deviceIndication = (legacy & EFFECT_FLAG_DEVICE_IND);
183     aidl.audioModeIndication = (legacy & EFFECT_FLAG_AUDIO_MODE_IND);
184     aidl.audioSourceIndication = (legacy & EFFECT_FLAG_AUDIO_SOURCE_IND);
185     aidl.bypass = (legacy & EFFECT_FLAG_NO_PROCESS);
186     return aidl;
187 }
188 
aidl2legacy_Flags_HardwareAccelerator_uint32(Flags::HardwareAccelerator hwAcceleratorMode)189 ConversionResult<uint32_t> aidl2legacy_Flags_HardwareAccelerator_uint32(
190         Flags::HardwareAccelerator hwAcceleratorMode) {
191     switch (hwAcceleratorMode) {
192         case Flags::HardwareAccelerator::NONE:
193             return 0;
194         case Flags::HardwareAccelerator::SIMPLE:
195             return EFFECT_FLAG_HW_ACC_SIMPLE;
196         case Flags::HardwareAccelerator::TUNNEL:
197             return EFFECT_FLAG_HW_ACC_TUNNEL;
198     }
199     return unexpected(BAD_VALUE);
200 }
201 
legacy2aidl_uint32_Flags_HardwareAccelerator(uint32_t legacy)202 ConversionResult<Flags::HardwareAccelerator> legacy2aidl_uint32_Flags_HardwareAccelerator(
203         uint32_t legacy) {
204     switch (legacy & EFFECT_FLAG_HW_ACC_MASK) {
205         case EFFECT_FLAG_HW_ACC_SIMPLE:
206             return Flags::HardwareAccelerator::SIMPLE;
207         case EFFECT_FLAG_HW_ACC_TUNNEL:
208             return Flags::HardwareAccelerator::TUNNEL;
209         case 0:
210             return Flags::HardwareAccelerator::NONE;
211     }
212     return unexpected(BAD_VALUE);
213 }
214 
215 ConversionResult<effect_descriptor_t>
aidl2legacy_Descriptor_effect_descriptor(const Descriptor & aidl)216 aidl2legacy_Descriptor_effect_descriptor(const Descriptor& aidl) {
217     effect_descriptor_t legacy;
218     legacy.type = VALUE_OR_RETURN(aidl2legacy_AudioUuid_audio_uuid_t(aidl.common.id.type));
219     legacy.uuid = VALUE_OR_RETURN(aidl2legacy_AudioUuid_audio_uuid_t(aidl.common.id.uuid));
220     // legacy descriptor doesn't have proxy information
221     // proxy = VALUE_OR_RETURN(aidl2legacy_AudioUuid_audio_uuid_t(aidl.proxy));
222     legacy.apiVersion = EFFECT_CONTROL_API_VERSION;
223     legacy.flags = VALUE_OR_RETURN(aidl2legacy_Flags_uint32(aidl.common.flags));
224     legacy.cpuLoad = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.common.cpuLoad));
225     legacy.memoryUsage = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.common.memoryUsage));
226     RETURN_IF_ERROR(aidl2legacy_string(aidl.common.name, legacy.name, sizeof(legacy.name)));
227     RETURN_IF_ERROR(aidl2legacy_string(aidl.common.implementor, legacy.implementor,
228                                         sizeof(legacy.implementor)));
229     return legacy;
230 }
231 
232 ConversionResult<Descriptor>
legacy2aidl_effect_descriptor_Descriptor(const effect_descriptor_t & legacy)233 legacy2aidl_effect_descriptor_Descriptor(const effect_descriptor_t& legacy) {
234     Descriptor aidl;
235     aidl.common.id.type = VALUE_OR_RETURN(legacy2aidl_audio_uuid_t_AudioUuid(legacy.type));
236     aidl.common.id.uuid = VALUE_OR_RETURN(legacy2aidl_audio_uuid_t_AudioUuid(legacy.uuid));
237     // legacy descriptor doesn't have proxy information
238     // aidl.common.id.proxy
239     aidl.common.flags = VALUE_OR_RETURN(legacy2aidl_uint32_Flags(legacy.flags));
240     aidl.common.cpuLoad = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.cpuLoad));
241     aidl.common.memoryUsage = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.memoryUsage));
242     aidl.common.name = VALUE_OR_RETURN(legacy2aidl_string(legacy.name, sizeof(legacy.name)));
243     aidl.common.implementor =
244             VALUE_OR_RETURN(legacy2aidl_string(legacy.implementor, sizeof(legacy.implementor)));
245     return aidl;
246 }
247 
aidl2legacy_Parameter_aec_uint32_echoDelay(const Parameter & aidl)248 ConversionResult<uint32_t> aidl2legacy_Parameter_aec_uint32_echoDelay(const Parameter& aidl) {
249     int echoDelay = VALUE_OR_RETURN(
250             GET_PARAMETER_SPECIFIC_FIELD(aidl, AcousticEchoCanceler, acousticEchoCanceler,
251                                          AcousticEchoCanceler::echoDelayUs, int));
252     return VALUE_OR_RETURN(convertReinterpret<uint32_t>(echoDelay));
253 }
254 
legacy2aidl_uint32_echoDelay_Parameter_aec(uint32_t legacy)255 ConversionResult<Parameter> legacy2aidl_uint32_echoDelay_Parameter_aec(uint32_t legacy) {
256     int delay = VALUE_OR_RETURN(convertReinterpret<int32_t>(legacy));
257     return MAKE_SPECIFIC_PARAMETER(AcousticEchoCanceler, acousticEchoCanceler, echoDelayUs, delay);
258 }
259 
aidl2legacy_Parameter_aec_uint32_mobileMode(const Parameter & aidl)260 ConversionResult<uint32_t> aidl2legacy_Parameter_aec_uint32_mobileMode(const Parameter& aidl) {
261     bool mobileMode = VALUE_OR_RETURN(
262             GET_PARAMETER_SPECIFIC_FIELD(aidl, AcousticEchoCanceler, acousticEchoCanceler,
263                                          AcousticEchoCanceler::mobileMode, bool));
264     return VALUE_OR_RETURN(convertIntegral<uint32_t>(mobileMode));
265 }
266 
legacy2aidl_uint32_mobileMode_Parameter_aec(uint32_t legacy)267 ConversionResult<Parameter> legacy2aidl_uint32_mobileMode_Parameter_aec(uint32_t legacy) {
268     bool mode = VALUE_OR_RETURN(convertIntegral<bool>(legacy));
269     return MAKE_SPECIFIC_PARAMETER(AcousticEchoCanceler, acousticEchoCanceler, mobileMode, mode);
270 }
271 
aidl2legacy_Parameter_agc_uint32_fixedDigitalGain(const Parameter & aidl)272 ConversionResult<uint32_t> aidl2legacy_Parameter_agc_uint32_fixedDigitalGain(
273         const Parameter& aidl) {
274     int gain = VALUE_OR_RETURN(
275             GET_PARAMETER_SPECIFIC_FIELD(aidl, AutomaticGainControlV2, automaticGainControlV2,
276                                          AutomaticGainControlV2::fixedDigitalGainMb, int));
277     return VALUE_OR_RETURN(convertReinterpret<uint32_t>(gain));
278 }
279 
legacy2aidl_uint32_fixedDigitalGain_Parameter_agc(uint32_t legacy)280 ConversionResult<Parameter> legacy2aidl_uint32_fixedDigitalGain_Parameter_agc(uint32_t legacy) {
281     int gain = VALUE_OR_RETURN(convertReinterpret<int>(legacy));
282     return MAKE_SPECIFIC_PARAMETER(AutomaticGainControlV2, automaticGainControlV2,
283                                    fixedDigitalGainMb, gain);
284 }
285 
aidl2legacy_Parameter_agc_uint32_levelEstimator(const Parameter & aidl)286 ConversionResult<uint32_t> aidl2legacy_Parameter_agc_uint32_levelEstimator(
287         const Parameter& aidl) {
288     const auto& le = VALUE_OR_RETURN(GET_PARAMETER_SPECIFIC_FIELD(
289             aidl, AutomaticGainControlV2, automaticGainControlV2,
290             AutomaticGainControlV2::levelEstimator, AutomaticGainControlV2::LevelEstimator));
291     return static_cast<uint32_t>(le);
292 }
293 
legacy2aidl_uint32_levelEstimator_Parameter_agc(uint32_t legacy)294 ConversionResult<Parameter> legacy2aidl_uint32_levelEstimator_Parameter_agc(uint32_t legacy) {
295     if (legacy > (uint32_t)AutomaticGainControlV2::LevelEstimator::PEAK) {
296         return unexpected(BAD_VALUE);
297     }
298     AutomaticGainControlV2::LevelEstimator le =
299             static_cast<AutomaticGainControlV2::LevelEstimator>(legacy);
300     return MAKE_SPECIFIC_PARAMETER(AutomaticGainControlV2, automaticGainControlV2, levelEstimator,
301                                    le);
302 }
303 
aidl2legacy_Parameter_agc_uint32_saturationMargin(const Parameter & aidl)304 ConversionResult<uint32_t> aidl2legacy_Parameter_agc_uint32_saturationMargin(
305         const Parameter& aidl) {
306     int saturationMargin = VALUE_OR_RETURN(
307             GET_PARAMETER_SPECIFIC_FIELD(aidl, AutomaticGainControlV2, automaticGainControlV2,
308                                          AutomaticGainControlV2::saturationMarginMb, int));
309     return VALUE_OR_RETURN(convertIntegral<uint32_t>(saturationMargin));
310 }
311 
legacy2aidl_uint32_saturationMargin_Parameter_agc(uint32_t legacy)312 ConversionResult<Parameter> legacy2aidl_uint32_saturationMargin_Parameter_agc(uint32_t legacy) {
313     int saturationMargin = VALUE_OR_RETURN(convertIntegral<int>(legacy));
314     return MAKE_SPECIFIC_PARAMETER(AutomaticGainControlV2, automaticGainControlV2,
315                                    saturationMarginMb, saturationMargin);
316 }
317 
aidl2legacy_Parameter_BassBoost_uint16_strengthPm(const Parameter & aidl)318 ConversionResult<uint16_t> aidl2legacy_Parameter_BassBoost_uint16_strengthPm(
319         const Parameter& aidl) {
320     int strength = VALUE_OR_RETURN(
321             GET_PARAMETER_SPECIFIC_FIELD(aidl, BassBoost, bassBoost, BassBoost::strengthPm, int));
322     return VALUE_OR_RETURN(convertIntegral<uint16_t>(strength));
323 }
324 
legacy2aidl_uint16_strengthPm_Parameter_BassBoost(uint16_t legacy)325 ConversionResult<Parameter> legacy2aidl_uint16_strengthPm_Parameter_BassBoost(uint16_t legacy) {
326     int strength = VALUE_OR_RETURN(convertIntegral<int>(legacy));
327     return MAKE_SPECIFIC_PARAMETER(BassBoost, bassBoost, strengthPm, strength);
328 }
329 
aidl2legacy_Parameter_Downmix_int16_type(const Parameter & aidl)330 ConversionResult<int16_t> aidl2legacy_Parameter_Downmix_int16_type(const Parameter& aidl) {
331     Downmix::Type aidlType = VALUE_OR_RETURN(
332             GET_PARAMETER_SPECIFIC_FIELD(aidl, Downmix, downmix, Downmix::type, Downmix::Type));
333     return VALUE_OR_RETURN(convertIntegral<int16_t>(static_cast<uint32_t>(aidlType)));
334 }
335 
legacy2aidl_int16_type_Parameter_Downmix(int16_t legacy)336 ConversionResult<Parameter> legacy2aidl_int16_type_Parameter_Downmix(int16_t legacy) {
337     if (legacy > (uint32_t) Downmix::Type::FOLD) {
338         return unexpected(BAD_VALUE);
339     }
340     Downmix::Type aidlType = static_cast<Downmix::Type>(legacy);
341     return MAKE_SPECIFIC_PARAMETER(Downmix, downmix, type, aidlType);
342 }
343 
aidl2legacy_DynamicsProcessing_ResolutionPreference_uint32_(const Parameter & aidl)344 ConversionResult<int16_t> aidl2legacy_DynamicsProcessing_ResolutionPreference_uint32_(
345         const Parameter& aidl) {
346     Downmix::Type aidlType = VALUE_OR_RETURN(
347             GET_PARAMETER_SPECIFIC_FIELD(aidl, Downmix, downmix, Downmix::type, Downmix::Type));
348     return VALUE_OR_RETURN(convertIntegral<int16_t>(static_cast<uint32_t>(aidlType)));
349 }
350 
351 ConversionResult<DynamicsProcessing::ResolutionPreference>
legacy2aidl_int32_DynamicsProcessing_ResolutionPreference(int32_t legacy)352 legacy2aidl_int32_DynamicsProcessing_ResolutionPreference(int32_t legacy) {
353     if (legacy > (int32_t)DynamicsProcessing::ResolutionPreference::FAVOR_TIME_RESOLUTION) {
354         return unexpected(BAD_VALUE);
355     }
356     return static_cast<DynamicsProcessing::ResolutionPreference>(legacy);
357 }
358 
aidl2legacy_DynamicsProcessing_ResolutionPreference_int32(DynamicsProcessing::ResolutionPreference aidl)359 ConversionResult<int32_t> aidl2legacy_DynamicsProcessing_ResolutionPreference_int32(
360         DynamicsProcessing::ResolutionPreference aidl) {
361     return static_cast<int32_t>(aidl);
362 }
363 
aidl2legacy_Parameter_Visualizer_ScalingMode_uint32(Visualizer::ScalingMode aidl)364 ConversionResult<uint32_t> aidl2legacy_Parameter_Visualizer_ScalingMode_uint32(
365         Visualizer::ScalingMode aidl) {
366     switch (aidl) {
367         case Visualizer::ScalingMode::NORMALIZED: {
368             return 0;
369         }
370         case Visualizer::ScalingMode::AS_PLAYED: {
371             return 1;
372         }
373     }
374     return unexpected(BAD_VALUE);
375 }
376 
legacy2aidl_Parameter_Visualizer_uint32_ScalingMode(uint32_t legacy)377 ConversionResult<Visualizer::ScalingMode> legacy2aidl_Parameter_Visualizer_uint32_ScalingMode(
378         uint32_t legacy) {
379     if (legacy == 0) {
380         return Visualizer::ScalingMode::NORMALIZED;
381     } else if (legacy == 1) {
382         return Visualizer::ScalingMode::AS_PLAYED;
383     } else {
384         return unexpected(BAD_VALUE);
385     }
386 }
387 
aidl2legacy_Parameter_Visualizer_MeasurementMode_uint32(Visualizer::MeasurementMode aidl)388 ConversionResult<uint32_t> aidl2legacy_Parameter_Visualizer_MeasurementMode_uint32(
389         Visualizer::MeasurementMode aidl) {
390     switch (aidl) {
391         case Visualizer::MeasurementMode::NONE: {
392             return 0;
393         }
394         case Visualizer::MeasurementMode::PEAK_RMS: {
395             return 1;
396         }
397     }
398     return unexpected(BAD_VALUE);
399 }
400 
401 ConversionResult<Visualizer::MeasurementMode>
legacy2aidl_Parameter_Visualizer_uint32_MeasurementMode(uint32_t legacy)402 legacy2aidl_Parameter_Visualizer_uint32_MeasurementMode(uint32_t legacy) {
403     if (legacy == 0) {
404         return Visualizer::MeasurementMode::NONE;
405     } else if (legacy == 1) {
406         return Visualizer::MeasurementMode::PEAK_RMS;
407     } else {
408         return unexpected(BAD_VALUE);
409     }
410 }
411 
aidl2legacy_VendorExtension_EffectParameterWriter(EffectParamWriter & param,VendorExtension ext)412 ConversionResult<status_t> aidl2legacy_VendorExtension_EffectParameterWriter(
413         EffectParamWriter& param, VendorExtension ext) {
414     std::optional<DefaultExtension> defaultExt;
415     RETURN_IF_ERROR(ext.extension.getParcelable(&defaultExt));
416     if (!defaultExt.has_value()) {
417         return unexpected(BAD_VALUE);
418     }
419 
420     // DefaultExtension defaultValue = defaultExt->get();
421     if (defaultExt->bytes.size() < sizeof(effect_param_t)) {
422         return unexpected(BAD_VALUE);
423     }
424     // verify data length with EffectParamWrapper, DefaultExtension array size should not smaller
425     // than (sizeof(effect_param_t) + paddedPSize + vSize)
426     EffectParamWrapper wrapper(*(effect_param_t*)defaultExt->bytes.data());
427     if (sizeof(effect_param_t) + wrapper.getPaddedParameterSize() + wrapper.getValueSize() >
428         defaultExt->bytes.size()) {
429         return unexpected(BAD_VALUE);
430     }
431 
432     RETURN_IF_ERROR(param.overwrite(wrapper.getEffectParam()));
433     return OK;
434 }
435 
legacy2aidl_EffectParameterReader_VendorExtension(EffectParamReader & param)436 ConversionResult<VendorExtension> legacy2aidl_EffectParameterReader_VendorExtension(
437         EffectParamReader& param) {
438     size_t len = param.getTotalSize();
439     DefaultExtension defaultExt;
440     defaultExt.bytes.resize(len);
441 
442     std::memcpy(defaultExt.bytes.data(), (void *)&param.getEffectParam(), len);
443 
444     VendorExtension ext;
445     ext.extension.setParcelable(defaultExt);
446     return ext;
447 }
448 
aidl2legacy_Parameter_EffectParameterWriter(const::aidl::android::hardware::audio::effect::Parameter & aidl,EffectParamWriter & legacy)449 ConversionResult<::android::status_t> aidl2legacy_Parameter_EffectParameterWriter(
450         const ::aidl::android::hardware::audio::effect::Parameter& aidl,
451         EffectParamWriter& legacy) {
452     VendorExtension ext = VALUE_OR_RETURN(
453             (::aidl::android::getParameterSpecific<Parameter, VendorExtension,
454                                                    Parameter::Specific::vendorEffect>(aidl)));
455     return VALUE_OR_RETURN_STATUS(aidl2legacy_VendorExtension_EffectParameterWriter(legacy, ext));
456 }
457 
legacy2aidl_EffectParameterReader_Parameter(EffectParamReader & param)458 ConversionResult<Parameter> legacy2aidl_EffectParameterReader_Parameter(EffectParamReader& param) {
459     VendorExtension ext = VALUE_OR_RETURN(legacy2aidl_EffectParameterReader_VendorExtension(param));
460     return UNION_MAKE(Parameter, specific, UNION_MAKE(Parameter::Specific, vendorEffect, ext));
461 }
462 
463 }  // namespace android
464 }  // aidl
465