Shunkai Yao | a03533e | 2023-01-25 06:38:10 +0000 | [diff] [blame] | 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 <utility> |
| 18 | |
| 19 | #define LOG_TAG "AidlConversionEffect" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | #include <media/AidlConversionCppNdk.h> |
| 24 | #include <media/AidlConversionEffect.h> |
| 25 | |
| 26 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 27 | // AIDL NDK backend to legacy audio data structure conversion utilities. |
| 28 | |
| 29 | namespace aidl { |
| 30 | namespace android { |
| 31 | |
| 32 | using ::aidl::android::hardware::audio::effect::AcousticEchoCanceler; |
| 33 | using ::aidl::android::hardware::audio::effect::AutomaticGainControl; |
| 34 | using ::aidl::android::hardware::audio::effect::BassBoost; |
| 35 | using ::aidl::android::hardware::audio::effect::Descriptor; |
| 36 | using ::aidl::android::hardware::audio::effect::Downmix; |
| 37 | using ::aidl::android::hardware::audio::effect::Flags; |
| 38 | using ::aidl::android::hardware::audio::effect::Parameter; |
| 39 | using ::aidl::android::media::audio::common::AudioDeviceDescription; |
| 40 | |
| 41 | using ::android::BAD_VALUE; |
| 42 | using ::android::base::unexpected; |
| 43 | |
| 44 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 45 | // Converters |
| 46 | |
| 47 | ConversionResult<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 | |
| 63 | ConversionResult<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 | |
| 79 | ConversionResult<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 | |
| 93 | ConversionResult<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 | |
| 107 | ConversionResult<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 | } |
| 120 | |
| 121 | ConversionResult<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; |
| 131 | } |
| 132 | return unexpected(BAD_VALUE); |
| 133 | } |
| 134 | |
| 135 | ConversionResult<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 | |
| 160 | ConversionResult<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 | |
| 175 | ConversionResult<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 | |
| 188 | ConversionResult<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 | |
| 201 | ConversionResult<effect_descriptor_t> |
| 202 | aidl2legacy_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 | |
| 218 | ConversionResult<Descriptor> |
| 219 | legacy2aidl_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 | |
| 234 | ConversionResult<uint32_t> aidl2legacy_Parameter_aec_uint32_echoDelay(const Parameter& aidl) { |
| 235 | int echoDelay = VALUE_OR_RETURN(GET_PARAMETER_SPECIFIC_FIELD( |
| 236 | aidl, AcousticEchoCanceler, acousticEchoCanceler, echoDelayUs, int)); |
| 237 | return VALUE_OR_RETURN(convertReinterpret<uint32_t>(echoDelay)); |
| 238 | } |
| 239 | |
| 240 | ConversionResult<Parameter> legacy2aidl_uint32_echoDelay_Parameter_aec(uint32_t legacy) { |
| 241 | int delay = VALUE_OR_RETURN(convertReinterpret<int32_t>(legacy)); |
| 242 | return MAKE_SPECIFIC_PARAMETER(AcousticEchoCanceler, acousticEchoCanceler, echoDelayUs, delay); |
| 243 | } |
| 244 | |
| 245 | ConversionResult<uint32_t> aidl2legacy_Parameter_aec_uint32_mobileMode(const Parameter& aidl) { |
| 246 | bool mobileMode = VALUE_OR_RETURN(GET_PARAMETER_SPECIFIC_FIELD( |
| 247 | aidl, AcousticEchoCanceler, acousticEchoCanceler, mobileMode, bool)); |
| 248 | return VALUE_OR_RETURN(convertIntegral<uint32_t>(mobileMode)); |
| 249 | } |
| 250 | |
| 251 | ConversionResult<Parameter> legacy2aidl_uint32_mobileMode_Parameter_aec(uint32_t legacy) { |
| 252 | bool mode = VALUE_OR_RETURN(convertIntegral<bool>(legacy)); |
| 253 | return MAKE_SPECIFIC_PARAMETER(AcousticEchoCanceler, acousticEchoCanceler, mobileMode, mode); |
| 254 | } |
| 255 | |
| 256 | ConversionResult<uint32_t> aidl2legacy_Parameter_agc_uint32_fixedDigitalGain( |
| 257 | const Parameter& aidl) { |
| 258 | int gain = VALUE_OR_RETURN(GET_PARAMETER_SPECIFIC_FIELD( |
| 259 | aidl, AutomaticGainControl, automaticGainControl, fixedDigitalGainMb, int)); |
| 260 | return VALUE_OR_RETURN(convertReinterpret<uint32_t>(gain)); |
| 261 | } |
| 262 | |
| 263 | ConversionResult<Parameter> legacy2aidl_uint32_fixedDigitalGain_Parameter_agc(uint32_t legacy) { |
| 264 | int gain = VALUE_OR_RETURN(convertReinterpret<int>(legacy)); |
| 265 | return MAKE_SPECIFIC_PARAMETER(AutomaticGainControl, automaticGainControl, fixedDigitalGainMb, |
| 266 | gain); |
| 267 | } |
| 268 | |
| 269 | ConversionResult<uint32_t> aidl2legacy_Parameter_agc_uint32_levelEstimator( |
| 270 | const Parameter& aidl) { |
| 271 | const auto& le = VALUE_OR_RETURN( |
| 272 | GET_PARAMETER_SPECIFIC_FIELD(aidl, AutomaticGainControl, automaticGainControl, |
| 273 | levelEstimator, AutomaticGainControl::LevelEstimator)); |
| 274 | return static_cast<uint32_t>(le); |
| 275 | } |
| 276 | |
| 277 | ConversionResult<Parameter> legacy2aidl_uint32_levelEstimator_Parameter_agc(uint32_t legacy) { |
| 278 | if (legacy > (uint32_t) AutomaticGainControl::LevelEstimator::PEAK) { |
| 279 | return unexpected(BAD_VALUE); |
| 280 | } |
| 281 | AutomaticGainControl::LevelEstimator le = |
| 282 | static_cast<AutomaticGainControl::LevelEstimator>(legacy); |
| 283 | return MAKE_SPECIFIC_PARAMETER(AutomaticGainControl, automaticGainControl, levelEstimator, le); |
| 284 | } |
| 285 | |
| 286 | ConversionResult<uint32_t> aidl2legacy_Parameter_agc_uint32_saturationMargin( |
| 287 | const Parameter& aidl) { |
| 288 | int saturationMargin = VALUE_OR_RETURN(GET_PARAMETER_SPECIFIC_FIELD( |
| 289 | aidl, AutomaticGainControl, automaticGainControl, saturationMarginMb, int)); |
| 290 | return VALUE_OR_RETURN(convertIntegral<uint32_t>(saturationMargin)); |
| 291 | } |
| 292 | |
| 293 | ConversionResult<Parameter> legacy2aidl_uint32_saturationMargin_Parameter_agc(uint32_t legacy) { |
| 294 | int saturationMargin = VALUE_OR_RETURN(convertIntegral<int>(legacy)); |
| 295 | return MAKE_SPECIFIC_PARAMETER(AutomaticGainControl, automaticGainControl, saturationMarginMb, |
| 296 | saturationMargin); |
| 297 | } |
| 298 | |
| 299 | ConversionResult<uint16_t> aidl2legacy_Parameter_BassBoost_uint16_strengthPm( |
| 300 | const Parameter& aidl) { |
| 301 | int strength = VALUE_OR_RETURN( |
| 302 | GET_PARAMETER_SPECIFIC_FIELD(aidl, BassBoost, bassBoost, strengthPm, int)); |
| 303 | return VALUE_OR_RETURN(convertIntegral<uint16_t>(strength)); |
| 304 | } |
| 305 | |
| 306 | ConversionResult<Parameter> legacy2aidl_uint16_strengthPm_Parameter_BassBoost(uint16_t legacy) { |
| 307 | int strength = VALUE_OR_RETURN(convertIntegral<int>(legacy)); |
| 308 | return MAKE_SPECIFIC_PARAMETER(BassBoost, bassBoost, strengthPm, strength); |
| 309 | } |
| 310 | |
| 311 | ConversionResult<int16_t> aidl2legacy_Parameter_Downmix_int16_type(const Parameter& aidl) { |
| 312 | Downmix::Type aidlType = VALUE_OR_RETURN( |
| 313 | GET_PARAMETER_SPECIFIC_FIELD(aidl, Downmix, downmix, type, Downmix::Type)); |
| 314 | return VALUE_OR_RETURN(convertIntegral<int16_t>(static_cast<uint32_t>(aidlType))); |
| 315 | } |
| 316 | |
| 317 | ConversionResult<Parameter> legacy2aidl_int16_type_Parameter_Downmix(int16_t legacy) { |
| 318 | if (legacy > (uint32_t) Downmix::Type::FOLD) { |
| 319 | return unexpected(BAD_VALUE); |
| 320 | } |
| 321 | Downmix::Type aidlType = static_cast<Downmix::Type>(legacy); |
| 322 | return MAKE_SPECIFIC_PARAMETER(Downmix, downmix, type, aidlType); |
| 323 | } |
| 324 | |
| 325 | } // namespace android |
| 326 | } // aidl |