blob: 8fec6609a423e94c76feec7321876929c0d5c55e [file] [log] [blame]
Shunkai Yao51202502022-12-12 06:11:46 +00001/*
2 * Copyright (C) 2022 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 <utility>
18
19#define LOG_TAG "AidlConversionNdk"
20//#define LOG_NDEBUG 0
21#include <utils/Log.h>
22
23#include <media/AidlConversionCppNdk.h>
24#include <media/AidlConversionNdk.h>
25
26////////////////////////////////////////////////////////////////////////////////////////////////////
27// AIDL NDK backend to legacy audio data structure conversion utilities.
28
29namespace aidl {
30namespace android {
31
Shunkai Yao284bb0d2023-01-10 00:42:36 +000032using ::aidl::android::hardware::audio::effect::AcousticEchoCanceler;
Shunkai Yao44bdbad2023-01-14 05:11:58 +000033using ::aidl::android::hardware::audio::effect::AutomaticGainControl;
34using ::aidl::android::hardware::audio::effect::BassBoost;
Shunkai Yao51202502022-12-12 06:11:46 +000035using ::aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yao44bdbad2023-01-14 05:11:58 +000036using ::aidl::android::hardware::audio::effect::Downmix;
Shunkai Yao51202502022-12-12 06:11:46 +000037using ::aidl::android::hardware::audio::effect::Flags;
Shunkai Yao284bb0d2023-01-10 00:42:36 +000038using ::aidl::android::hardware::audio::effect::Parameter;
39using ::aidl::android::media::audio::common::AudioDeviceDescription;
Shunkai Yao51202502022-12-12 06:11:46 +000040
41using ::android::BAD_VALUE;
42using ::android::base::unexpected;
43
44////////////////////////////////////////////////////////////////////////////////////////////////////
45// Converters
46
47ConversionResult<uint32_t> aidl2legacy_Flags_Type_uint32(Flags::Type type) {
48 switch (type) {
49 case Flags::Type::INSERT:
50 return EFFECT_FLAG_TYPE_INSERT;
51 case Flags::Type::AUXILIARY:
52 return EFFECT_FLAG_TYPE_AUXILIARY;
53 case Flags::Type::REPLACE:
54 return EFFECT_FLAG_TYPE_REPLACE;
55 case Flags::Type::PRE_PROC:
56 return EFFECT_FLAG_TYPE_PRE_PROC;
57 case Flags::Type::POST_PROC:
58 return EFFECT_FLAG_TYPE_POST_PROC;
59 }
60 return unexpected(BAD_VALUE);
61}
62
Shunkai Yao44bdbad2023-01-14 05:11:58 +000063ConversionResult<Flags::Type> legacy2aidl_uint32_Flags_Type(uint32_t legacy) {
64 switch (legacy & EFFECT_FLAG_TYPE_MASK) {
65 case EFFECT_FLAG_TYPE_INSERT:
66 return Flags::Type::INSERT;
67 case EFFECT_FLAG_TYPE_AUXILIARY:
68 return Flags::Type::AUXILIARY;
69 case EFFECT_FLAG_TYPE_REPLACE:
70 return Flags::Type::REPLACE;
71 case EFFECT_FLAG_TYPE_PRE_PROC:
72 return Flags::Type::PRE_PROC;
73 case EFFECT_FLAG_TYPE_POST_PROC:
74 return Flags::Type::POST_PROC;
75 }
76 return unexpected(BAD_VALUE);
77}
78
Shunkai Yao51202502022-12-12 06:11:46 +000079ConversionResult<uint32_t> aidl2legacy_Flags_Insert_uint32(Flags::Insert insert) {
80 switch (insert) {
81 case Flags::Insert::ANY:
82 return EFFECT_FLAG_INSERT_ANY;
83 case Flags::Insert::FIRST:
84 return EFFECT_FLAG_INSERT_FIRST;
85 case Flags::Insert::LAST:
86 return EFFECT_FLAG_INSERT_LAST;
87 case Flags::Insert::EXCLUSIVE:
88 return EFFECT_FLAG_INSERT_EXCLUSIVE;
89 }
90 return unexpected(BAD_VALUE);
91}
92
Shunkai Yao44bdbad2023-01-14 05:11:58 +000093ConversionResult<Flags::Insert> legacy2aidl_uint32_Flags_Insert(uint32_t legacy) {
94 switch (legacy & EFFECT_FLAG_INSERT_MASK) {
95 case EFFECT_FLAG_INSERT_ANY:
96 return Flags::Insert::ANY;
97 case EFFECT_FLAG_INSERT_FIRST:
98 return Flags::Insert::FIRST;
99 case EFFECT_FLAG_INSERT_LAST:
100 return Flags::Insert::LAST;
101 case EFFECT_FLAG_INSERT_EXCLUSIVE:
102 return Flags::Insert::EXCLUSIVE;
103 }
104 return unexpected(BAD_VALUE);
105}
106
Shunkai Yao51202502022-12-12 06:11:46 +0000107ConversionResult<uint32_t> aidl2legacy_Flags_Volume_uint32(Flags::Volume volume) {
108 switch (volume) {
109 case Flags::Volume::NONE:
110 return 0;
111 case Flags::Volume::CTRL:
112 return EFFECT_FLAG_VOLUME_CTRL;
113 case Flags::Volume::IND:
114 return EFFECT_FLAG_VOLUME_IND;
115 case Flags::Volume::MONITOR:
116 return EFFECT_FLAG_VOLUME_MONITOR;
117 }
118 return unexpected(BAD_VALUE);
119}
Shunkai Yao44bdbad2023-01-14 05:11:58 +0000120
121ConversionResult<Flags::Volume> legacy2aidl_uint32_Flags_Volume(uint32_t legacy) {
122 switch (legacy & EFFECT_FLAG_VOLUME_MASK) {
123 case EFFECT_FLAG_VOLUME_CTRL:
124 return Flags::Volume::CTRL;
125 case EFFECT_FLAG_VOLUME_IND:
126 return Flags::Volume::IND;
127 case EFFECT_FLAG_VOLUME_MONITOR:
128 return Flags::Volume::MONITOR;
129 case EFFECT_FLAG_VOLUME_NONE:
130 return Flags::Volume::NONE;
Shunkai Yao51202502022-12-12 06:11:46 +0000131 }
132 return unexpected(BAD_VALUE);
133}
134
135ConversionResult<uint32_t> aidl2legacy_Flags_uint32(Flags aidl) {
136 uint32_t legacy = 0;
137 legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_Type_uint32(aidl.type));
138 legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_Insert_uint32(aidl.insert));
139 legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_Volume_uint32(aidl.volume));
140 legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_HardwareAccelerator_uint32(aidl.hwAcceleratorMode));
141
142 if (aidl.offloadIndication) {
143 legacy |= EFFECT_FLAG_OFFLOAD_SUPPORTED;
144 }
145 if (aidl.deviceIndication) {
146 legacy |= EFFECT_FLAG_DEVICE_IND;
147 }
148 if (aidl.audioModeIndication) {
149 legacy |= EFFECT_FLAG_AUDIO_MODE_IND;
150 }
151 if (aidl.audioSourceIndication) {
152 legacy |= EFFECT_FLAG_AUDIO_SOURCE_IND;
153 }
154 if (aidl.noProcessing) {
155 legacy |= EFFECT_FLAG_NO_PROCESS;
156 }
157 return legacy;
158}
159
Shunkai Yao51202502022-12-12 06:11:46 +0000160ConversionResult<Flags> legacy2aidl_uint32_Flags(uint32_t legacy) {
161 Flags aidl;
162
163 aidl.type = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_Type(legacy));
164 aidl.insert = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_Insert(legacy));
165 aidl.volume = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_Volume(legacy));
166 aidl.hwAcceleratorMode = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_HardwareAccelerator(legacy));
167 aidl.offloadIndication = (legacy & EFFECT_FLAG_OFFLOAD_SUPPORTED);
168 aidl.deviceIndication = (legacy & EFFECT_FLAG_DEVICE_IND);
169 aidl.audioModeIndication = (legacy & EFFECT_FLAG_AUDIO_MODE_IND);
170 aidl.audioSourceIndication = (legacy & EFFECT_FLAG_AUDIO_SOURCE_IND);
171 aidl.noProcessing = (legacy & EFFECT_FLAG_NO_PROCESS);
172 return aidl;
173}
174
Shunkai Yao44bdbad2023-01-14 05:11:58 +0000175ConversionResult<uint32_t> aidl2legacy_Flags_HardwareAccelerator_uint32(
176 Flags::HardwareAccelerator hwAcceleratorMode) {
177 switch (hwAcceleratorMode) {
178 case Flags::HardwareAccelerator::NONE:
179 return 0;
180 case Flags::HardwareAccelerator::SIMPLE:
181 return EFFECT_FLAG_HW_ACC_SIMPLE;
182 case Flags::HardwareAccelerator::TUNNEL:
183 return EFFECT_FLAG_HW_ACC_TUNNEL;
184 }
185 return unexpected(BAD_VALUE);
186}
187
188ConversionResult<Flags::HardwareAccelerator> legacy2aidl_uint32_Flags_HardwareAccelerator(
189 uint32_t legacy) {
190 switch (legacy & EFFECT_FLAG_HW_ACC_MASK) {
191 case EFFECT_FLAG_HW_ACC_SIMPLE:
192 return Flags::HardwareAccelerator::SIMPLE;
193 case EFFECT_FLAG_HW_ACC_TUNNEL:
194 return Flags::HardwareAccelerator::TUNNEL;
195 case 0:
196 return Flags::HardwareAccelerator::NONE;
197 }
198 return unexpected(BAD_VALUE);
199}
200
Shunkai Yao51202502022-12-12 06:11:46 +0000201ConversionResult<effect_descriptor_t>
202aidl2legacy_Descriptor_effect_descriptor(const Descriptor& aidl) {
203 effect_descriptor_t legacy;
204 legacy.type = VALUE_OR_RETURN(aidl2legacy_AudioUuid_audio_uuid_t(aidl.common.id.type));
205 legacy.uuid = VALUE_OR_RETURN(aidl2legacy_AudioUuid_audio_uuid_t(aidl.common.id.uuid));
206 // legacy descriptor doesn't have proxy information
207 // proxy = VALUE_OR_RETURN(aidl2legacy_AudioUuid_audio_uuid_t(aidl.proxy));
208 legacy.apiVersion = EFFECT_CONTROL_API_VERSION;
209 legacy.flags = VALUE_OR_RETURN(aidl2legacy_Flags_uint32(aidl.common.flags));
210 legacy.cpuLoad = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.common.cpuLoad));
211 legacy.memoryUsage = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.common.memoryUsage));
212 RETURN_IF_ERROR(aidl2legacy_string(aidl.common.name, legacy.name, sizeof(legacy.name)));
213 RETURN_IF_ERROR(aidl2legacy_string(aidl.common.implementor, legacy.implementor,
214 sizeof(legacy.implementor)));
215 return legacy;
216}
217
218ConversionResult<Descriptor>
219legacy2aidl_effect_descriptor_Descriptor(const effect_descriptor_t& legacy) {
220 Descriptor aidl;
221 aidl.common.id.type = VALUE_OR_RETURN(legacy2aidl_audio_uuid_t_AudioUuid(legacy.type));
222 aidl.common.id.uuid = VALUE_OR_RETURN(legacy2aidl_audio_uuid_t_AudioUuid(legacy.uuid));
223 // legacy descriptor doesn't have proxy information
224 // aidl.common.id.proxy
225 aidl.common.flags = VALUE_OR_RETURN(legacy2aidl_uint32_Flags(legacy.flags));
226 aidl.common.cpuLoad = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.cpuLoad));
227 aidl.common.memoryUsage = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.memoryUsage));
228 aidl.common.name = VALUE_OR_RETURN(legacy2aidl_string(legacy.name, sizeof(legacy.name)));
229 aidl.common.implementor =
230 VALUE_OR_RETURN(legacy2aidl_string(legacy.implementor, sizeof(legacy.implementor)));
231 return aidl;
232}
233
Shunkai Yao44bdbad2023-01-14 05:11:58 +0000234// buffer_provider_t is not supported thus skipped
Shunkai Yao51202502022-12-12 06:11:46 +0000235ConversionResult<buffer_config_t> aidl2legacy_AudioConfigBase_buffer_config_t(
236 const media::audio::common::AudioConfigBase& aidl, bool isInput) {
237 buffer_config_t legacy;
238
239 legacy.samplingRate = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.sampleRate));
240 legacy.mask |= EFFECT_CONFIG_SMP_RATE;
241
242 legacy.channels = VALUE_OR_RETURN(
243 aidl2legacy_AudioChannelLayout_audio_channel_mask_t(aidl.channelMask, isInput));
244 legacy.mask |= EFFECT_CONFIG_CHANNELS;
245
246 legacy.format = VALUE_OR_RETURN(aidl2legacy_AudioFormatDescription_audio_format_t(aidl.format));
247 legacy.mask |= EFFECT_CONFIG_FORMAT;
248
Shunkai Yao44bdbad2023-01-14 05:11:58 +0000249 // TODO: add accessMode and mask
Shunkai Yao51202502022-12-12 06:11:46 +0000250 return legacy;
251}
252
253ConversionResult<media::audio::common::AudioConfigBase>
Shunkai Yao284bb0d2023-01-10 00:42:36 +0000254legacy2aidl_buffer_config_t_AudioConfigBase(const buffer_config_t& legacy, bool isInput) {
Shunkai Yao51202502022-12-12 06:11:46 +0000255 media::audio::common::AudioConfigBase aidl;
256
257 if (legacy.mask & EFFECT_CONFIG_SMP_RATE) {
258 aidl.sampleRate = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.samplingRate));
259 }
260 if (legacy.mask & EFFECT_CONFIG_CHANNELS) {
261 aidl.channelMask = VALUE_OR_RETURN(legacy2aidl_audio_channel_mask_t_AudioChannelLayout(
262 static_cast<audio_channel_mask_t>(legacy.channels), isInput));
263 }
264 if (legacy.mask & EFFECT_CONFIG_FORMAT) {
265 aidl.format = VALUE_OR_RETURN(legacy2aidl_audio_format_t_AudioFormatDescription(
266 static_cast<audio_format_t>(legacy.format)));
267 }
Shunkai Yao44bdbad2023-01-14 05:11:58 +0000268
269 // TODO: add accessMode and mask
Shunkai Yao51202502022-12-12 06:11:46 +0000270 return aidl;
271}
272
Shunkai Yao44bdbad2023-01-14 05:11:58 +0000273ConversionResult<uint32_t> aidl2legacy_Parameter_aec_uint32_echoDelay(const Parameter& aidl) {
274 int echoDelay = VALUE_OR_RETURN(GET_PARAMETER_SPECIFIC_FIELD(
275 aidl, AcousticEchoCanceler, acousticEchoCanceler, echoDelayUs, int));
276 return VALUE_OR_RETURN(convertReinterpret<uint32_t>(echoDelay));
Shunkai Yao284bb0d2023-01-10 00:42:36 +0000277}
278
Shunkai Yao44bdbad2023-01-14 05:11:58 +0000279ConversionResult<Parameter> legacy2aidl_uint32_echoDelay_Parameter_aec(uint32_t legacy) {
Shunkai Yao284bb0d2023-01-10 00:42:36 +0000280 int delay = VALUE_OR_RETURN(convertReinterpret<int32_t>(legacy));
Shunkai Yao44bdbad2023-01-14 05:11:58 +0000281 return MAKE_SPECIFIC_PARAMETER(AcousticEchoCanceler, acousticEchoCanceler, echoDelayUs, delay);
Shunkai Yao284bb0d2023-01-10 00:42:36 +0000282}
283
Shunkai Yao44bdbad2023-01-14 05:11:58 +0000284ConversionResult<uint32_t> aidl2legacy_Parameter_aec_uint32_mobileMode(const Parameter& aidl) {
285 bool mobileMode = VALUE_OR_RETURN(GET_PARAMETER_SPECIFIC_FIELD(
286 aidl, AcousticEchoCanceler, acousticEchoCanceler, mobileMode, bool));
Shunkai Yao284bb0d2023-01-10 00:42:36 +0000287 return VALUE_OR_RETURN(convertIntegral<uint32_t>(mobileMode));
288}
289
Shunkai Yao44bdbad2023-01-14 05:11:58 +0000290ConversionResult<Parameter> legacy2aidl_uint32_mobileMode_Parameter_aec(uint32_t legacy) {
Shunkai Yao284bb0d2023-01-10 00:42:36 +0000291 bool mode = VALUE_OR_RETURN(convertIntegral<bool>(legacy));
Shunkai Yao44bdbad2023-01-14 05:11:58 +0000292 return MAKE_SPECIFIC_PARAMETER(AcousticEchoCanceler, acousticEchoCanceler, mobileMode, mode);
293}
Shunkai Yao284bb0d2023-01-10 00:42:36 +0000294
Shunkai Yao44bdbad2023-01-14 05:11:58 +0000295ConversionResult<uint32_t> aidl2legacy_Parameter_agc_uint32_fixedDigitalGain(
296 const Parameter& aidl) {
297 int gain = VALUE_OR_RETURN(GET_PARAMETER_SPECIFIC_FIELD(
298 aidl, AutomaticGainControl, automaticGainControl, fixedDigitalGainMb, int));
299 return VALUE_OR_RETURN(convertReinterpret<uint32_t>(gain));
300}
301
302ConversionResult<Parameter> legacy2aidl_uint32_fixedDigitalGain_Parameter_agc(uint32_t legacy) {
303 int gain = VALUE_OR_RETURN(convertReinterpret<int>(legacy));
304 return MAKE_SPECIFIC_PARAMETER(AutomaticGainControl, automaticGainControl, fixedDigitalGainMb,
305 gain);
306}
307
308ConversionResult<uint32_t> aidl2legacy_Parameter_agc_uint32_levelEstimator(
309 const Parameter& aidl) {
310 const auto& le = VALUE_OR_RETURN(
311 GET_PARAMETER_SPECIFIC_FIELD(aidl, AutomaticGainControl, automaticGainControl,
312 levelEstimator, AutomaticGainControl::LevelEstimator));
313 return static_cast<uint32_t>(le);
314}
315
316ConversionResult<Parameter> legacy2aidl_uint32_levelEstimator_Parameter_agc(uint32_t legacy) {
317 if (legacy > (uint32_t) AutomaticGainControl::LevelEstimator::PEAK) {
318 return unexpected(BAD_VALUE);
319 }
320 AutomaticGainControl::LevelEstimator le =
321 static_cast<AutomaticGainControl::LevelEstimator>(legacy);
322 return MAKE_SPECIFIC_PARAMETER(AutomaticGainControl, automaticGainControl, levelEstimator, le);
323}
324
325ConversionResult<uint32_t> aidl2legacy_Parameter_agc_uint32_saturationMargin(
326 const Parameter& aidl) {
327 int saturationMargin = VALUE_OR_RETURN(GET_PARAMETER_SPECIFIC_FIELD(
328 aidl, AutomaticGainControl, automaticGainControl, saturationMarginMb, int));
329 return VALUE_OR_RETURN(convertIntegral<uint32_t>(saturationMargin));
330}
331
332ConversionResult<Parameter> legacy2aidl_uint32_saturationMargin_Parameter_agc(uint32_t legacy) {
333 int saturationMargin = VALUE_OR_RETURN(convertIntegral<int>(legacy));
334 return MAKE_SPECIFIC_PARAMETER(AutomaticGainControl, automaticGainControl, saturationMarginMb,
335 saturationMargin);
336}
337
338ConversionResult<uint16_t> aidl2legacy_Parameter_BassBoost_uint16_strengthPm(
339 const Parameter& aidl) {
340 int strength = VALUE_OR_RETURN(
341 GET_PARAMETER_SPECIFIC_FIELD(aidl, BassBoost, bassBoost, strengthPm, int));
342 return VALUE_OR_RETURN(convertIntegral<uint16_t>(strength));
343}
344
345ConversionResult<Parameter> legacy2aidl_uint16_strengthPm_Parameter_BassBoost(uint16_t legacy) {
346 int strength = VALUE_OR_RETURN(convertIntegral<int>(legacy));
347 return MAKE_SPECIFIC_PARAMETER(BassBoost, bassBoost, strengthPm, strength);
348}
349
350ConversionResult<int16_t> aidl2legacy_Parameter_Downmix_int16_type(const Parameter& aidl) {
351 Downmix::Type aidlType = VALUE_OR_RETURN(
352 GET_PARAMETER_SPECIFIC_FIELD(aidl, Downmix, downmix, type, Downmix::Type));
353 return VALUE_OR_RETURN(convertIntegral<int16_t>(static_cast<uint32_t>(aidlType)));
354}
355
356ConversionResult<Parameter> legacy2aidl_int16_type_Parameter_Downmix(int16_t legacy) {
357 if (legacy > (uint32_t) Downmix::Type::FOLD) {
358 return unexpected(BAD_VALUE);
359 }
360 Downmix::Type aidlType = static_cast<Downmix::Type>(legacy);
361 return MAKE_SPECIFIC_PARAMETER(Downmix, downmix, type, aidlType);
Shunkai Yao284bb0d2023-01-10 00:42:36 +0000362}
363
Shunkai Yao51202502022-12-12 06:11:46 +0000364} // namespace android
365} // aidl