Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 29 | namespace aidl { |
| 30 | namespace android { |
| 31 | |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame^] | 32 | using ::aidl::android::hardware::audio::effect::AcousticEchoCanceler; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 33 | using ::aidl::android::hardware::audio::effect::Descriptor; |
| 34 | using ::aidl::android::hardware::audio::effect::Flags; |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame^] | 35 | using ::aidl::android::hardware::audio::effect::Parameter; |
| 36 | using ::aidl::android::media::audio::common::AudioDeviceDescription; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 37 | |
| 38 | using ::android::BAD_VALUE; |
| 39 | using ::android::base::unexpected; |
| 40 | |
| 41 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame^] | 42 | // Utils |
| 43 | |
| 44 | ConversionResult<AcousticEchoCanceler> getParameterSpecificAec(const Parameter& aidl) { |
| 45 | const auto& specific = VALUE_OR_RETURN(UNION_GET(aidl, specific)); |
| 46 | return VALUE_OR_RETURN(UNION_GET(specific, acousticEchoCanceler)); |
| 47 | } |
| 48 | |
| 49 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 50 | // Converters |
| 51 | |
| 52 | ConversionResult<uint32_t> aidl2legacy_Flags_Type_uint32(Flags::Type type) { |
| 53 | switch (type) { |
| 54 | case Flags::Type::INSERT: |
| 55 | return EFFECT_FLAG_TYPE_INSERT; |
| 56 | case Flags::Type::AUXILIARY: |
| 57 | return EFFECT_FLAG_TYPE_AUXILIARY; |
| 58 | case Flags::Type::REPLACE: |
| 59 | return EFFECT_FLAG_TYPE_REPLACE; |
| 60 | case Flags::Type::PRE_PROC: |
| 61 | return EFFECT_FLAG_TYPE_PRE_PROC; |
| 62 | case Flags::Type::POST_PROC: |
| 63 | return EFFECT_FLAG_TYPE_POST_PROC; |
| 64 | } |
| 65 | return unexpected(BAD_VALUE); |
| 66 | } |
| 67 | |
| 68 | ConversionResult<uint32_t> aidl2legacy_Flags_Insert_uint32(Flags::Insert insert) { |
| 69 | switch (insert) { |
| 70 | case Flags::Insert::ANY: |
| 71 | return EFFECT_FLAG_INSERT_ANY; |
| 72 | case Flags::Insert::FIRST: |
| 73 | return EFFECT_FLAG_INSERT_FIRST; |
| 74 | case Flags::Insert::LAST: |
| 75 | return EFFECT_FLAG_INSERT_LAST; |
| 76 | case Flags::Insert::EXCLUSIVE: |
| 77 | return EFFECT_FLAG_INSERT_EXCLUSIVE; |
| 78 | } |
| 79 | return unexpected(BAD_VALUE); |
| 80 | } |
| 81 | |
| 82 | ConversionResult<uint32_t> aidl2legacy_Flags_Volume_uint32(Flags::Volume volume) { |
| 83 | switch (volume) { |
| 84 | case Flags::Volume::NONE: |
| 85 | return 0; |
| 86 | case Flags::Volume::CTRL: |
| 87 | return EFFECT_FLAG_VOLUME_CTRL; |
| 88 | case Flags::Volume::IND: |
| 89 | return EFFECT_FLAG_VOLUME_IND; |
| 90 | case Flags::Volume::MONITOR: |
| 91 | return EFFECT_FLAG_VOLUME_MONITOR; |
| 92 | } |
| 93 | return unexpected(BAD_VALUE); |
| 94 | } |
| 95 | ConversionResult<uint32_t> aidl2legacy_Flags_HardwareAccelerator_uint32( |
| 96 | Flags::HardwareAccelerator hwAcceleratorMode) { |
| 97 | switch (hwAcceleratorMode) { |
| 98 | case Flags::HardwareAccelerator::NONE: |
| 99 | return 0; |
| 100 | case Flags::HardwareAccelerator::SIMPLE: |
| 101 | return EFFECT_FLAG_HW_ACC_SIMPLE; |
| 102 | case Flags::HardwareAccelerator::TUNNEL: |
| 103 | return EFFECT_FLAG_HW_ACC_TUNNEL; |
| 104 | } |
| 105 | return unexpected(BAD_VALUE); |
| 106 | } |
| 107 | |
| 108 | ConversionResult<uint32_t> aidl2legacy_Flags_uint32(Flags aidl) { |
| 109 | uint32_t legacy = 0; |
| 110 | legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_Type_uint32(aidl.type)); |
| 111 | legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_Insert_uint32(aidl.insert)); |
| 112 | legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_Volume_uint32(aidl.volume)); |
| 113 | legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_HardwareAccelerator_uint32(aidl.hwAcceleratorMode)); |
| 114 | |
| 115 | if (aidl.offloadIndication) { |
| 116 | legacy |= EFFECT_FLAG_OFFLOAD_SUPPORTED; |
| 117 | } |
| 118 | if (aidl.deviceIndication) { |
| 119 | legacy |= EFFECT_FLAG_DEVICE_IND; |
| 120 | } |
| 121 | if (aidl.audioModeIndication) { |
| 122 | legacy |= EFFECT_FLAG_AUDIO_MODE_IND; |
| 123 | } |
| 124 | if (aidl.audioSourceIndication) { |
| 125 | legacy |= EFFECT_FLAG_AUDIO_SOURCE_IND; |
| 126 | } |
| 127 | if (aidl.noProcessing) { |
| 128 | legacy |= EFFECT_FLAG_NO_PROCESS; |
| 129 | } |
| 130 | return legacy; |
| 131 | } |
| 132 | |
| 133 | ConversionResult<Flags::Type> legacy2aidl_uint32_Flags_Type(uint32_t legacy) { |
| 134 | switch (legacy & EFFECT_FLAG_TYPE_MASK) { |
| 135 | case EFFECT_FLAG_TYPE_INSERT: |
| 136 | return Flags::Type::INSERT; |
| 137 | case EFFECT_FLAG_TYPE_AUXILIARY: |
| 138 | return Flags::Type::AUXILIARY; |
| 139 | case EFFECT_FLAG_TYPE_REPLACE: |
| 140 | return Flags::Type::REPLACE; |
| 141 | case EFFECT_FLAG_TYPE_PRE_PROC: |
| 142 | return Flags::Type::PRE_PROC; |
| 143 | case EFFECT_FLAG_TYPE_POST_PROC: |
| 144 | return Flags::Type::POST_PROC; |
| 145 | } |
| 146 | return unexpected(BAD_VALUE); |
| 147 | } |
| 148 | |
| 149 | ConversionResult<Flags::Insert> legacy2aidl_uint32_Flags_Insert(uint32_t legacy) { |
| 150 | switch (legacy & EFFECT_FLAG_INSERT_MASK) { |
| 151 | case EFFECT_FLAG_INSERT_ANY: |
| 152 | return Flags::Insert::ANY; |
| 153 | case EFFECT_FLAG_INSERT_FIRST: |
| 154 | return Flags::Insert::FIRST; |
| 155 | case EFFECT_FLAG_INSERT_LAST: |
| 156 | return Flags::Insert::LAST; |
| 157 | case EFFECT_FLAG_INSERT_EXCLUSIVE: |
| 158 | return Flags::Insert::EXCLUSIVE; |
| 159 | } |
| 160 | return unexpected(BAD_VALUE); |
| 161 | } |
| 162 | |
| 163 | ConversionResult<Flags::Volume> legacy2aidl_uint32_Flags_Volume(uint32_t legacy) { |
| 164 | switch (legacy & EFFECT_FLAG_VOLUME_MASK) { |
| 165 | case EFFECT_FLAG_VOLUME_IND: |
| 166 | return Flags::Volume::IND; |
| 167 | case EFFECT_FLAG_VOLUME_MONITOR: |
| 168 | return Flags::Volume::MONITOR; |
| 169 | case EFFECT_FLAG_VOLUME_NONE: |
| 170 | return Flags::Volume::NONE; |
| 171 | } |
| 172 | return unexpected(BAD_VALUE); |
| 173 | } |
| 174 | |
| 175 | ConversionResult<Flags::HardwareAccelerator> legacy2aidl_uint32_Flags_HardwareAccelerator( |
| 176 | uint32_t legacy) { |
| 177 | switch (legacy & EFFECT_FLAG_HW_ACC_MASK) { |
| 178 | case EFFECT_FLAG_HW_ACC_SIMPLE: |
| 179 | return Flags::HardwareAccelerator::SIMPLE; |
| 180 | case EFFECT_FLAG_HW_ACC_TUNNEL: |
| 181 | return Flags::HardwareAccelerator::TUNNEL; |
| 182 | } |
| 183 | return unexpected(BAD_VALUE); |
| 184 | } |
| 185 | |
| 186 | ConversionResult<Flags> legacy2aidl_uint32_Flags(uint32_t legacy) { |
| 187 | Flags aidl; |
| 188 | |
| 189 | aidl.type = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_Type(legacy)); |
| 190 | aidl.insert = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_Insert(legacy)); |
| 191 | aidl.volume = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_Volume(legacy)); |
| 192 | aidl.hwAcceleratorMode = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_HardwareAccelerator(legacy)); |
| 193 | aidl.offloadIndication = (legacy & EFFECT_FLAG_OFFLOAD_SUPPORTED); |
| 194 | aidl.deviceIndication = (legacy & EFFECT_FLAG_DEVICE_IND); |
| 195 | aidl.audioModeIndication = (legacy & EFFECT_FLAG_AUDIO_MODE_IND); |
| 196 | aidl.audioSourceIndication = (legacy & EFFECT_FLAG_AUDIO_SOURCE_IND); |
| 197 | aidl.noProcessing = (legacy & EFFECT_FLAG_NO_PROCESS); |
| 198 | return aidl; |
| 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<buffer_config_t> aidl2legacy_AudioConfigBase_buffer_config_t( |
| 235 | const media::audio::common::AudioConfigBase& aidl, bool isInput) { |
| 236 | buffer_config_t legacy; |
| 237 | |
| 238 | legacy.samplingRate = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.sampleRate)); |
| 239 | legacy.mask |= EFFECT_CONFIG_SMP_RATE; |
| 240 | |
| 241 | legacy.channels = VALUE_OR_RETURN( |
| 242 | aidl2legacy_AudioChannelLayout_audio_channel_mask_t(aidl.channelMask, isInput)); |
| 243 | legacy.mask |= EFFECT_CONFIG_CHANNELS; |
| 244 | |
| 245 | legacy.format = VALUE_OR_RETURN(aidl2legacy_AudioFormatDescription_audio_format_t(aidl.format)); |
| 246 | legacy.mask |= EFFECT_CONFIG_FORMAT; |
| 247 | |
| 248 | return legacy; |
| 249 | } |
| 250 | |
| 251 | ConversionResult<media::audio::common::AudioConfigBase> |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame^] | 252 | legacy2aidl_buffer_config_t_AudioConfigBase(const buffer_config_t& legacy, bool isInput) { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 253 | media::audio::common::AudioConfigBase aidl; |
| 254 | |
| 255 | if (legacy.mask & EFFECT_CONFIG_SMP_RATE) { |
| 256 | aidl.sampleRate = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.samplingRate)); |
| 257 | } |
| 258 | if (legacy.mask & EFFECT_CONFIG_CHANNELS) { |
| 259 | aidl.channelMask = VALUE_OR_RETURN(legacy2aidl_audio_channel_mask_t_AudioChannelLayout( |
| 260 | static_cast<audio_channel_mask_t>(legacy.channels), isInput)); |
| 261 | } |
| 262 | if (legacy.mask & EFFECT_CONFIG_FORMAT) { |
| 263 | aidl.format = VALUE_OR_RETURN(legacy2aidl_audio_format_t_AudioFormatDescription( |
| 264 | static_cast<audio_format_t>(legacy.format))); |
| 265 | } |
| 266 | return aidl; |
| 267 | } |
| 268 | |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame^] | 269 | ConversionResult<uint32_t> aidl2legacy_Parameter_uint32_echoDelay(const Parameter& aidl) { |
| 270 | const auto& aec = VALUE_OR_RETURN(getParameterSpecificAec(aidl)); |
| 271 | const auto& echoDelay = VALUE_OR_RETURN(UNION_GET(aec, echoDelayUs)); |
| 272 | return VALUE_OR_RETURN(convertIntegral<uint32_t>(echoDelay)); |
| 273 | } |
| 274 | |
| 275 | ConversionResult<Parameter> legacy2aidl_uint32_echoDelay_Parameter(const uint32_t& legacy) { |
| 276 | int delay = VALUE_OR_RETURN(convertReinterpret<int32_t>(legacy)); |
| 277 | AcousticEchoCanceler aec = AcousticEchoCanceler::make<AcousticEchoCanceler::echoDelayUs>(delay); |
| 278 | Parameter::Specific specific = |
| 279 | Parameter::Specific::make<Parameter::Specific::acousticEchoCanceler>(aec); |
| 280 | |
| 281 | return Parameter::make<Parameter::specific>(specific); |
| 282 | } |
| 283 | |
| 284 | ConversionResult<uint32_t> aidl2legacy_Parameter_uint32_mobileMode(const Parameter& aidl) { |
| 285 | const auto& aec = VALUE_OR_RETURN(getParameterSpecificAec(aidl)); |
| 286 | const auto& mobileMode = VALUE_OR_RETURN(UNION_GET(aec, mobileMode)); |
| 287 | return VALUE_OR_RETURN(convertIntegral<uint32_t>(mobileMode)); |
| 288 | } |
| 289 | |
| 290 | ConversionResult<Parameter> legacy2aidl_uint32_mobileMode_Parameter(const uint32_t& legacy) { |
| 291 | bool mode = VALUE_OR_RETURN(convertIntegral<bool>(legacy)); |
| 292 | AcousticEchoCanceler aec = AcousticEchoCanceler::make<AcousticEchoCanceler::mobileMode>(mode); |
| 293 | Parameter::Specific specific = |
| 294 | Parameter::Specific::make<Parameter::Specific::acousticEchoCanceler>(aec); |
| 295 | |
| 296 | return Parameter::make<Parameter::specific>(specific); |
| 297 | } |
| 298 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 299 | } // namespace android |
| 300 | } // aidl |