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 | |
| 32 | using ::aidl::android::hardware::audio::effect::Descriptor; |
| 33 | using ::aidl::android::hardware::audio::effect::Flags; |
| 34 | |
| 35 | using ::android::BAD_VALUE; |
| 36 | using ::android::base::unexpected; |
| 37 | |
| 38 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 39 | // Converters |
| 40 | |
| 41 | ConversionResult<uint32_t> aidl2legacy_Flags_Type_uint32(Flags::Type type) { |
| 42 | switch (type) { |
| 43 | case Flags::Type::INSERT: |
| 44 | return EFFECT_FLAG_TYPE_INSERT; |
| 45 | case Flags::Type::AUXILIARY: |
| 46 | return EFFECT_FLAG_TYPE_AUXILIARY; |
| 47 | case Flags::Type::REPLACE: |
| 48 | return EFFECT_FLAG_TYPE_REPLACE; |
| 49 | case Flags::Type::PRE_PROC: |
| 50 | return EFFECT_FLAG_TYPE_PRE_PROC; |
| 51 | case Flags::Type::POST_PROC: |
| 52 | return EFFECT_FLAG_TYPE_POST_PROC; |
| 53 | } |
| 54 | return unexpected(BAD_VALUE); |
| 55 | } |
| 56 | |
| 57 | ConversionResult<uint32_t> aidl2legacy_Flags_Insert_uint32(Flags::Insert insert) { |
| 58 | switch (insert) { |
| 59 | case Flags::Insert::ANY: |
| 60 | return EFFECT_FLAG_INSERT_ANY; |
| 61 | case Flags::Insert::FIRST: |
| 62 | return EFFECT_FLAG_INSERT_FIRST; |
| 63 | case Flags::Insert::LAST: |
| 64 | return EFFECT_FLAG_INSERT_LAST; |
| 65 | case Flags::Insert::EXCLUSIVE: |
| 66 | return EFFECT_FLAG_INSERT_EXCLUSIVE; |
| 67 | } |
| 68 | return unexpected(BAD_VALUE); |
| 69 | } |
| 70 | |
| 71 | ConversionResult<uint32_t> aidl2legacy_Flags_Volume_uint32(Flags::Volume volume) { |
| 72 | switch (volume) { |
| 73 | case Flags::Volume::NONE: |
| 74 | return 0; |
| 75 | case Flags::Volume::CTRL: |
| 76 | return EFFECT_FLAG_VOLUME_CTRL; |
| 77 | case Flags::Volume::IND: |
| 78 | return EFFECT_FLAG_VOLUME_IND; |
| 79 | case Flags::Volume::MONITOR: |
| 80 | return EFFECT_FLAG_VOLUME_MONITOR; |
| 81 | } |
| 82 | return unexpected(BAD_VALUE); |
| 83 | } |
| 84 | ConversionResult<uint32_t> aidl2legacy_Flags_HardwareAccelerator_uint32( |
| 85 | Flags::HardwareAccelerator hwAcceleratorMode) { |
| 86 | switch (hwAcceleratorMode) { |
| 87 | case Flags::HardwareAccelerator::NONE: |
| 88 | return 0; |
| 89 | case Flags::HardwareAccelerator::SIMPLE: |
| 90 | return EFFECT_FLAG_HW_ACC_SIMPLE; |
| 91 | case Flags::HardwareAccelerator::TUNNEL: |
| 92 | return EFFECT_FLAG_HW_ACC_TUNNEL; |
| 93 | } |
| 94 | return unexpected(BAD_VALUE); |
| 95 | } |
| 96 | |
| 97 | ConversionResult<uint32_t> aidl2legacy_Flags_uint32(Flags aidl) { |
| 98 | uint32_t legacy = 0; |
| 99 | legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_Type_uint32(aidl.type)); |
| 100 | legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_Insert_uint32(aidl.insert)); |
| 101 | legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_Volume_uint32(aidl.volume)); |
| 102 | legacy |= VALUE_OR_RETURN(aidl2legacy_Flags_HardwareAccelerator_uint32(aidl.hwAcceleratorMode)); |
| 103 | |
| 104 | if (aidl.offloadIndication) { |
| 105 | legacy |= EFFECT_FLAG_OFFLOAD_SUPPORTED; |
| 106 | } |
| 107 | if (aidl.deviceIndication) { |
| 108 | legacy |= EFFECT_FLAG_DEVICE_IND; |
| 109 | } |
| 110 | if (aidl.audioModeIndication) { |
| 111 | legacy |= EFFECT_FLAG_AUDIO_MODE_IND; |
| 112 | } |
| 113 | if (aidl.audioSourceIndication) { |
| 114 | legacy |= EFFECT_FLAG_AUDIO_SOURCE_IND; |
| 115 | } |
| 116 | if (aidl.noProcessing) { |
| 117 | legacy |= EFFECT_FLAG_NO_PROCESS; |
| 118 | } |
| 119 | return legacy; |
| 120 | } |
| 121 | |
| 122 | ConversionResult<Flags::Type> legacy2aidl_uint32_Flags_Type(uint32_t legacy) { |
| 123 | switch (legacy & EFFECT_FLAG_TYPE_MASK) { |
| 124 | case EFFECT_FLAG_TYPE_INSERT: |
| 125 | return Flags::Type::INSERT; |
| 126 | case EFFECT_FLAG_TYPE_AUXILIARY: |
| 127 | return Flags::Type::AUXILIARY; |
| 128 | case EFFECT_FLAG_TYPE_REPLACE: |
| 129 | return Flags::Type::REPLACE; |
| 130 | case EFFECT_FLAG_TYPE_PRE_PROC: |
| 131 | return Flags::Type::PRE_PROC; |
| 132 | case EFFECT_FLAG_TYPE_POST_PROC: |
| 133 | return Flags::Type::POST_PROC; |
| 134 | } |
| 135 | return unexpected(BAD_VALUE); |
| 136 | } |
| 137 | |
| 138 | ConversionResult<Flags::Insert> legacy2aidl_uint32_Flags_Insert(uint32_t legacy) { |
| 139 | switch (legacy & EFFECT_FLAG_INSERT_MASK) { |
| 140 | case EFFECT_FLAG_INSERT_ANY: |
| 141 | return Flags::Insert::ANY; |
| 142 | case EFFECT_FLAG_INSERT_FIRST: |
| 143 | return Flags::Insert::FIRST; |
| 144 | case EFFECT_FLAG_INSERT_LAST: |
| 145 | return Flags::Insert::LAST; |
| 146 | case EFFECT_FLAG_INSERT_EXCLUSIVE: |
| 147 | return Flags::Insert::EXCLUSIVE; |
| 148 | } |
| 149 | return unexpected(BAD_VALUE); |
| 150 | } |
| 151 | |
| 152 | ConversionResult<Flags::Volume> legacy2aidl_uint32_Flags_Volume(uint32_t legacy) { |
| 153 | switch (legacy & EFFECT_FLAG_VOLUME_MASK) { |
| 154 | case EFFECT_FLAG_VOLUME_IND: |
| 155 | return Flags::Volume::IND; |
| 156 | case EFFECT_FLAG_VOLUME_MONITOR: |
| 157 | return Flags::Volume::MONITOR; |
| 158 | case EFFECT_FLAG_VOLUME_NONE: |
| 159 | return Flags::Volume::NONE; |
| 160 | } |
| 161 | return unexpected(BAD_VALUE); |
| 162 | } |
| 163 | |
| 164 | ConversionResult<Flags::HardwareAccelerator> legacy2aidl_uint32_Flags_HardwareAccelerator( |
| 165 | uint32_t legacy) { |
| 166 | switch (legacy & EFFECT_FLAG_HW_ACC_MASK) { |
| 167 | case EFFECT_FLAG_HW_ACC_SIMPLE: |
| 168 | return Flags::HardwareAccelerator::SIMPLE; |
| 169 | case EFFECT_FLAG_HW_ACC_TUNNEL: |
| 170 | return Flags::HardwareAccelerator::TUNNEL; |
| 171 | } |
| 172 | return unexpected(BAD_VALUE); |
| 173 | } |
| 174 | |
| 175 | ConversionResult<Flags> legacy2aidl_uint32_Flags(uint32_t legacy) { |
| 176 | Flags aidl; |
| 177 | |
| 178 | aidl.type = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_Type(legacy)); |
| 179 | aidl.insert = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_Insert(legacy)); |
| 180 | aidl.volume = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_Volume(legacy)); |
| 181 | aidl.hwAcceleratorMode = VALUE_OR_RETURN(legacy2aidl_uint32_Flags_HardwareAccelerator(legacy)); |
| 182 | aidl.offloadIndication = (legacy & EFFECT_FLAG_OFFLOAD_SUPPORTED); |
| 183 | aidl.deviceIndication = (legacy & EFFECT_FLAG_DEVICE_IND); |
| 184 | aidl.audioModeIndication = (legacy & EFFECT_FLAG_AUDIO_MODE_IND); |
| 185 | aidl.audioSourceIndication = (legacy & EFFECT_FLAG_AUDIO_SOURCE_IND); |
| 186 | aidl.noProcessing = (legacy & EFFECT_FLAG_NO_PROCESS); |
| 187 | return aidl; |
| 188 | } |
| 189 | |
| 190 | ConversionResult<effect_descriptor_t> |
| 191 | aidl2legacy_Descriptor_effect_descriptor(const Descriptor& aidl) { |
| 192 | effect_descriptor_t legacy; |
| 193 | legacy.type = VALUE_OR_RETURN(aidl2legacy_AudioUuid_audio_uuid_t(aidl.common.id.type)); |
| 194 | legacy.uuid = VALUE_OR_RETURN(aidl2legacy_AudioUuid_audio_uuid_t(aidl.common.id.uuid)); |
| 195 | // legacy descriptor doesn't have proxy information |
| 196 | // proxy = VALUE_OR_RETURN(aidl2legacy_AudioUuid_audio_uuid_t(aidl.proxy)); |
| 197 | legacy.apiVersion = EFFECT_CONTROL_API_VERSION; |
| 198 | legacy.flags = VALUE_OR_RETURN(aidl2legacy_Flags_uint32(aidl.common.flags)); |
| 199 | legacy.cpuLoad = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.common.cpuLoad)); |
| 200 | legacy.memoryUsage = VALUE_OR_RETURN(convertIntegral<uint16_t>(aidl.common.memoryUsage)); |
| 201 | RETURN_IF_ERROR(aidl2legacy_string(aidl.common.name, legacy.name, sizeof(legacy.name))); |
| 202 | RETURN_IF_ERROR(aidl2legacy_string(aidl.common.implementor, legacy.implementor, |
| 203 | sizeof(legacy.implementor))); |
| 204 | return legacy; |
| 205 | } |
| 206 | |
| 207 | ConversionResult<Descriptor> |
| 208 | legacy2aidl_effect_descriptor_Descriptor(const effect_descriptor_t& legacy) { |
| 209 | Descriptor aidl; |
| 210 | aidl.common.id.type = VALUE_OR_RETURN(legacy2aidl_audio_uuid_t_AudioUuid(legacy.type)); |
| 211 | aidl.common.id.uuid = VALUE_OR_RETURN(legacy2aidl_audio_uuid_t_AudioUuid(legacy.uuid)); |
| 212 | // legacy descriptor doesn't have proxy information |
| 213 | // aidl.common.id.proxy |
| 214 | aidl.common.flags = VALUE_OR_RETURN(legacy2aidl_uint32_Flags(legacy.flags)); |
| 215 | aidl.common.cpuLoad = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.cpuLoad)); |
| 216 | aidl.common.memoryUsage = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.memoryUsage)); |
| 217 | aidl.common.name = VALUE_OR_RETURN(legacy2aidl_string(legacy.name, sizeof(legacy.name))); |
| 218 | aidl.common.implementor = |
| 219 | VALUE_OR_RETURN(legacy2aidl_string(legacy.implementor, sizeof(legacy.implementor))); |
| 220 | return aidl; |
| 221 | } |
| 222 | |
| 223 | ConversionResult<buffer_config_t> aidl2legacy_AudioConfigBase_buffer_config_t( |
| 224 | const media::audio::common::AudioConfigBase& aidl, bool isInput) { |
| 225 | buffer_config_t legacy; |
| 226 | |
| 227 | legacy.samplingRate = VALUE_OR_RETURN(convertIntegral<uint32_t>(aidl.sampleRate)); |
| 228 | legacy.mask |= EFFECT_CONFIG_SMP_RATE; |
| 229 | |
| 230 | legacy.channels = VALUE_OR_RETURN( |
| 231 | aidl2legacy_AudioChannelLayout_audio_channel_mask_t(aidl.channelMask, isInput)); |
| 232 | legacy.mask |= EFFECT_CONFIG_CHANNELS; |
| 233 | |
| 234 | legacy.format = VALUE_OR_RETURN(aidl2legacy_AudioFormatDescription_audio_format_t(aidl.format)); |
| 235 | legacy.mask |= EFFECT_CONFIG_FORMAT; |
| 236 | |
| 237 | return legacy; |
| 238 | } |
| 239 | |
| 240 | ConversionResult<media::audio::common::AudioConfigBase> |
| 241 | legacy2aidl_AudioConfigBase_buffer_config_t(const buffer_config_t& legacy, bool isInput) { |
| 242 | media::audio::common::AudioConfigBase aidl; |
| 243 | |
| 244 | if (legacy.mask & EFFECT_CONFIG_SMP_RATE) { |
| 245 | aidl.sampleRate = VALUE_OR_RETURN(convertIntegral<int32_t>(legacy.samplingRate)); |
| 246 | } |
| 247 | if (legacy.mask & EFFECT_CONFIG_CHANNELS) { |
| 248 | aidl.channelMask = VALUE_OR_RETURN(legacy2aidl_audio_channel_mask_t_AudioChannelLayout( |
| 249 | static_cast<audio_channel_mask_t>(legacy.channels), isInput)); |
| 250 | } |
| 251 | if (legacy.mask & EFFECT_CONFIG_FORMAT) { |
| 252 | aidl.format = VALUE_OR_RETURN(legacy2aidl_audio_format_t_AudioFormatDescription( |
| 253 | static_cast<audio_format_t>(legacy.format))); |
| 254 | } |
| 255 | return aidl; |
| 256 | } |
| 257 | |
| 258 | } // namespace android |
| 259 | } // aidl |