Shunkai Yao | 242521c | 2023-01-29 18:08:09 +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 <cstdint> |
| 18 | #include <cstring> |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 19 | #define LOG_TAG "AidlConversionSpatializer" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
Shunkai Yao | da4a640 | 2023-03-03 19:38:17 +0000 | [diff] [blame] | 22 | #include <aidl/android/hardware/audio/effect/DefaultExtension.h> |
| 23 | #include <aidl/android/hardware/audio/effect/VendorExtension.h> |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 24 | #include <error/expected_utils.h> |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 25 | #include <media/AidlConversionCppNdk.h> |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 26 | #include <media/AidlConversionEffect.h> |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 27 | #include <media/AidlConversionNdk.h> |
| 28 | #include <system/audio_effects/aidl_effects_utils.h> |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 29 | #include <system/audio_effects/effect_spatializer.h> |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 30 | #include <utils/Log.h> |
| 31 | |
| 32 | #include "AidlConversionSpatializer.h" |
| 33 | |
| 34 | namespace android { |
| 35 | namespace effect { |
| 36 | |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 37 | using aidl::android::getParameterSpecificField; |
| 38 | using aidl::android::aidl_utils::statusTFromBinderStatus; |
| 39 | using aidl::android::hardware::audio::common::SourceMetadata; |
| 40 | using aidl::android::hardware::audio::effect::DefaultExtension; |
| 41 | using aidl::android::hardware::audio::effect::Parameter; |
| 42 | using aidl::android::hardware::audio::effect::Range; |
| 43 | using aidl::android::hardware::audio::effect::Spatializer; |
| 44 | using aidl::android::hardware::audio::effect::VendorExtension; |
| 45 | using aidl::android::media::audio::common::AudioChannelLayout; |
| 46 | using aidl::android::media::audio::common::HeadTracking; |
| 47 | using aidl::android::media::audio::common::Spatialization; |
| 48 | using aidl::android::media::audio::common::toString; |
| 49 | using android::status_t; |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 50 | using utils::EffectParamReader; |
| 51 | using utils::EffectParamWriter; |
| 52 | |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 53 | bool AidlConversionSpatializer::isSpatializerParameterSupported() { |
| 54 | return mIsSpatializerAidlParamSupported.value_or( |
| 55 | (mIsSpatializerAidlParamSupported = |
| 56 | [&]() { |
| 57 | ::aidl::android::hardware::audio::effect::Parameter aidlParam; |
| 58 | auto id = MAKE_SPECIFIC_PARAMETER_ID(Spatializer, spatializerTag, |
| 59 | Spatializer::vendor); |
| 60 | // No range defined in descriptor capability means no Spatializer AIDL |
| 61 | // implementation BAD_VALUE return from getParameter indicates the |
| 62 | // parameter is not supported by HAL |
| 63 | return mDesc.capability.range.getTag() == Range::spatializer && |
| 64 | mEffect->getParameter(id, &aidlParam).getStatus() != |
| 65 | android::BAD_VALUE; |
| 66 | }()) |
| 67 | .value()); |
| 68 | } |
| 69 | |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 70 | status_t AidlConversionSpatializer::setParameter(EffectParamReader& param) { |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 71 | Parameter aidlParam; |
| 72 | if (isSpatializerParameterSupported()) { |
| 73 | uint32_t command = 0; |
| 74 | if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(int8_t)) || |
| 75 | OK != param.readFromParameter(&command)) { |
| 76 | ALOGE("%s %d invalid param %s", __func__, __LINE__, param.toString().c_str()); |
| 77 | return BAD_VALUE; |
| 78 | } |
| 79 | |
| 80 | switch (command) { |
| 81 | case SPATIALIZER_PARAM_LEVEL: { |
| 82 | Spatialization::Level level = Spatialization::Level::NONE; |
| 83 | if (OK != param.readFromValue(&level)) { |
| 84 | ALOGE("%s invalid level value %s", __func__, param.toString().c_str()); |
| 85 | return BAD_VALUE; |
| 86 | } |
| 87 | aidlParam = MAKE_SPECIFIC_PARAMETER(Spatializer, spatializer, |
| 88 | spatializationLevel, level); |
| 89 | break; |
| 90 | } |
| 91 | case SPATIALIZER_PARAM_HEADTRACKING_MODE: { |
| 92 | HeadTracking::Mode mode = HeadTracking::Mode::DISABLED; |
| 93 | if (OK != param.readFromValue(&mode)) { |
| 94 | ALOGE("%s invalid mode value %s", __func__, param.toString().c_str()); |
| 95 | return BAD_VALUE; |
| 96 | } |
| 97 | aidlParam = MAKE_SPECIFIC_PARAMETER(Spatializer, spatializer, headTrackingMode, |
| 98 | mode); |
| 99 | break; |
| 100 | } |
| 101 | case SPATIALIZER_PARAM_HEAD_TO_STAGE: { |
| 102 | const size_t valueSize = param.getValueSize(); |
| 103 | if (valueSize / sizeof(float) > 6 || valueSize % sizeof(float) != 0) { |
| 104 | ALOGE("%s invalid parameter value size %zu", __func__, valueSize); |
| 105 | return BAD_VALUE; |
| 106 | } |
| 107 | std::array<float, 6> headToStage = {}; |
| 108 | for (size_t i = 0; i < valueSize / sizeof(float); i++) { |
| 109 | if (OK != param.readFromValue(&headToStage[i])) { |
| 110 | ALOGE("%s failed to read headToStage from %s", __func__, |
| 111 | param.toString().c_str()); |
| 112 | return BAD_VALUE; |
| 113 | } |
| 114 | } |
| 115 | HeadTracking::SensorData sensorData = |
| 116 | HeadTracking::SensorData::make<HeadTracking::SensorData::headToStage>( |
| 117 | headToStage); |
| 118 | aidlParam = MAKE_SPECIFIC_PARAMETER(Spatializer, spatializer, |
| 119 | headTrackingSensorData, sensorData); |
| 120 | break; |
| 121 | } |
| 122 | case SPATIALIZER_PARAM_HEADTRACKING_CONNECTION: { |
| 123 | int32_t modeInt32 = 0; |
| 124 | int32_t sensorId = -1; |
| 125 | if (OK != param.readFromValue(&modeInt32) || OK != param.readFromValue(&sensorId)) { |
| 126 | ALOGE("%s %d invalid parameter value %s", __func__, __LINE__, |
| 127 | param.toString().c_str()); |
| 128 | return BAD_VALUE; |
| 129 | } |
| 130 | |
| 131 | const auto mode = static_cast<HeadTracking::ConnectionMode>(modeInt32); |
| 132 | if (mode < *ndk::enum_range<HeadTracking::ConnectionMode>().begin() || |
| 133 | mode > *ndk::enum_range<HeadTracking::ConnectionMode>().end()) { |
| 134 | ALOGE("%s %d invalid mode %d", __func__, __LINE__, modeInt32); |
| 135 | return BAD_VALUE; |
| 136 | } |
| 137 | aidlParam = MAKE_SPECIFIC_PARAMETER(Spatializer, spatializer, |
| 138 | headTrackingConnectionMode, mode); |
| 139 | if (status_t status = statusTFromBinderStatus(mEffect->setParameter(aidlParam)); |
| 140 | status != OK) { |
| 141 | ALOGE("%s failed to set headTrackingConnectionMode %s", __func__, |
| 142 | toString(mode).c_str()); |
| 143 | return status; |
| 144 | } |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 145 | aidlParam = MAKE_SPECIFIC_PARAMETER(Spatializer, spatializer, headTrackingSensorId, |
| 146 | sensorId); |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 147 | return statusTFromBinderStatus(mEffect->setParameter(aidlParam)); |
| 148 | } |
| 149 | default: { |
| 150 | ALOGE("%s %d invalid command %u", __func__, __LINE__, command); |
| 151 | return BAD_VALUE; |
| 152 | } |
| 153 | } |
| 154 | } else { |
| 155 | aidlParam = VALUE_OR_RETURN_STATUS( |
| 156 | ::aidl::android::legacy2aidl_EffectParameterReader_Parameter(param)); |
| 157 | } |
| 158 | |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 159 | return statusTFromBinderStatus(mEffect->setParameter(aidlParam)); |
| 160 | } |
| 161 | |
| 162 | status_t AidlConversionSpatializer::getParameter(EffectParamWriter& param) { |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 163 | if (isSpatializerParameterSupported()) { |
| 164 | uint32_t command = 0; |
| 165 | if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(int8_t)) || |
| 166 | OK != param.readFromParameter(&command)) { |
| 167 | ALOGE("%s %d invalid param %s", __func__, __LINE__, param.toString().c_str()); |
| 168 | return BAD_VALUE; |
| 169 | } |
Shunkai Yao | da4a640 | 2023-03-03 19:38:17 +0000 | [diff] [blame] | 170 | |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 171 | switch (command) { |
| 172 | case SPATIALIZER_PARAM_SUPPORTED_LEVELS: { |
| 173 | const auto& range = getRange<Range::spatializer, Range::SpatializerRange>( |
| 174 | mDesc.capability, Spatializer::spatializationLevel); |
| 175 | if (!range) { |
| 176 | return BAD_VALUE; |
| 177 | } |
| 178 | for (const auto level : ::ndk::enum_range<Spatialization::Level>()) { |
| 179 | const auto spatializer = |
| 180 | Spatializer::make<Spatializer::spatializationLevel>(level); |
| 181 | if (spatializer >= range->min && spatializer <= range->max) { |
| 182 | if (status_t status = param.writeToValue(&level); status != OK) { |
Shunkai Yao | ac61ee9 | 2024-03-14 21:57:46 +0000 | [diff] [blame^] | 183 | ALOGW("%s %d: write level %s to value failed %d", __func__, __LINE__, |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 184 | toString(level).c_str(), status); |
| 185 | return status; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | return OK; |
| 190 | } |
| 191 | case SPATIALIZER_PARAM_LEVEL: { |
| 192 | Parameter aidlParam; |
| 193 | Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(Spatializer, spatializerTag, |
| 194 | Spatializer::spatializationLevel); |
| 195 | RETURN_STATUS_IF_ERROR( |
| 196 | statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam))); |
| 197 | const auto level = VALUE_OR_RETURN_STATUS(GET_PARAMETER_SPECIFIC_FIELD( |
| 198 | aidlParam, Spatializer, spatializer, Spatializer::spatializationLevel, |
| 199 | Spatialization::Level)); |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 200 | return param.writeToValue(&level); |
| 201 | } |
| 202 | case SPATIALIZER_PARAM_HEADTRACKING_SUPPORTED: { |
| 203 | const auto& range = getRange<Range::spatializer, Range::SpatializerRange>( |
| 204 | mDesc.capability, Spatializer::spatializationLevel); |
| 205 | if (!range) { |
| 206 | ALOGE("%s %d: range not defined for spatializationLevel", __func__, __LINE__); |
| 207 | return BAD_VALUE; |
| 208 | } |
| 209 | const auto& nonSupport = Spatializer::make<Spatializer::spatializationLevel>( |
| 210 | Spatialization::Level::NONE); |
| 211 | const bool support = (range->min > range->max || |
| 212 | (range->min == nonSupport && range->max == nonSupport)) |
| 213 | ? false |
| 214 | : true; |
| 215 | return param.writeToValue(&support); |
| 216 | } |
| 217 | case SPATIALIZER_PARAM_HEADTRACKING_MODE: { |
| 218 | Parameter aidlParam; |
| 219 | Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(Spatializer, spatializerTag, |
| 220 | Spatializer::headTrackingMode); |
| 221 | RETURN_STATUS_IF_ERROR( |
| 222 | statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam))); |
| 223 | const auto mode = VALUE_OR_RETURN_STATUS(GET_PARAMETER_SPECIFIC_FIELD( |
| 224 | aidlParam, Spatializer, spatializer, Spatializer::headTrackingMode, |
| 225 | HeadTracking::Mode)); |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 226 | return param.writeToValue(&mode); |
| 227 | } |
| 228 | case SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS: { |
| 229 | Parameter aidlParam; |
| 230 | Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(Spatializer, spatializerTag, |
| 231 | Spatializer::supportedChannelLayout); |
| 232 | RETURN_STATUS_IF_ERROR( |
| 233 | statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam))); |
| 234 | const auto& supportedLayouts = VALUE_OR_RETURN_STATUS(GET_PARAMETER_SPECIFIC_FIELD( |
| 235 | aidlParam, Spatializer, spatializer, Spatializer::supportedChannelLayout, |
| 236 | std::vector<AudioChannelLayout>)); |
| 237 | for (const auto& layout : supportedLayouts) { |
| 238 | audio_channel_mask_t mask = VALUE_OR_RETURN_STATUS( |
| 239 | ::aidl::android::aidl2legacy_AudioChannelLayout_audio_channel_mask_t( |
| 240 | layout, false /* isInput */)); |
| 241 | if (status_t status = param.writeToValue(&mask); status != OK) { |
Shunkai Yao | ac61ee9 | 2024-03-14 21:57:46 +0000 | [diff] [blame^] | 242 | ALOGW("%s %d: write mask %s to value failed %d", __func__, __LINE__, |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 243 | layout.toString().c_str(), status); |
| 244 | return status; |
| 245 | } |
| 246 | } |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 247 | return OK; |
| 248 | } |
| 249 | case SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES: { |
| 250 | const auto& range = getRange<Range::spatializer, Range::SpatializerRange>( |
| 251 | mDesc.capability, Spatializer::spatializationMode); |
| 252 | if (!range) { |
| 253 | return BAD_VALUE; |
| 254 | } |
| 255 | for (const auto mode : ::ndk::enum_range<Spatialization::Mode>()) { |
| 256 | if (const auto spatializer = |
| 257 | Spatializer::make<Spatializer::spatializationMode>(mode); |
| 258 | spatializer >= range->min && spatializer <= range->max) { |
| 259 | if (status_t status = param.writeToValue(&mode); status != OK) { |
Shunkai Yao | ac61ee9 | 2024-03-14 21:57:46 +0000 | [diff] [blame^] | 260 | ALOGW("%s %d: write mode %s to value failed %d", __func__, __LINE__, |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 261 | toString(mode).c_str(), status); |
| 262 | return status; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | return OK; |
| 267 | } |
| 268 | case SPATIALIZER_PARAM_SUPPORTED_HEADTRACKING_CONNECTION: { |
| 269 | const auto& range = getRange<Range::spatializer, Range::SpatializerRange>( |
| 270 | mDesc.capability, Spatializer::headTrackingConnectionMode); |
| 271 | if (!range) { |
| 272 | return BAD_VALUE; |
| 273 | } |
| 274 | for (const auto mode : ::ndk::enum_range<HeadTracking::ConnectionMode>()) { |
| 275 | if (const auto spatializer = |
| 276 | Spatializer::make<Spatializer::headTrackingConnectionMode>(mode); |
| 277 | spatializer < range->min || spatializer > range->max) { |
| 278 | continue; |
| 279 | } |
| 280 | if (status_t status = param.writeToValue(&mode); status != OK) { |
Shunkai Yao | ac61ee9 | 2024-03-14 21:57:46 +0000 | [diff] [blame^] | 281 | ALOGW("%s %d: write mode %s to value failed %d", __func__, __LINE__, |
| 282 | toString(mode).c_str(), status); |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 283 | return status; |
| 284 | } |
| 285 | } |
| 286 | return OK; |
| 287 | } |
| 288 | case SPATIALIZER_PARAM_HEADTRACKING_CONNECTION: { |
| 289 | status_t status = OK; |
| 290 | Parameter aidlParam; |
| 291 | Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID( |
| 292 | Spatializer, spatializerTag, Spatializer::headTrackingConnectionMode); |
| 293 | RETURN_STATUS_IF_ERROR( |
| 294 | statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam))); |
| 295 | const auto mode = VALUE_OR_RETURN_STATUS(GET_PARAMETER_SPECIFIC_FIELD( |
| 296 | aidlParam, Spatializer, spatializer, |
| 297 | Spatializer::headTrackingConnectionMode, HeadTracking::ConnectionMode)); |
| 298 | |
| 299 | id = MAKE_SPECIFIC_PARAMETER_ID(Spatializer, spatializerTag, |
| 300 | Spatializer::headTrackingSensorId); |
| 301 | RETURN_STATUS_IF_ERROR( |
| 302 | statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam))); |
| 303 | const auto sensorId = VALUE_OR_RETURN_STATUS( |
| 304 | GET_PARAMETER_SPECIFIC_FIELD(aidlParam, Spatializer, spatializer, |
| 305 | Spatializer::headTrackingSensorId, int32_t)); |
| 306 | uint32_t modeInt32 = static_cast<int32_t>(mode); |
| 307 | if (status = param.writeToValue(&modeInt32); status != OK) { |
Shunkai Yao | ac61ee9 | 2024-03-14 21:57:46 +0000 | [diff] [blame^] | 308 | ALOGW("%s %d: write mode %s to value failed %d", __func__, __LINE__, |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 309 | toString(mode).c_str(), status); |
| 310 | return status; |
| 311 | } |
| 312 | if (status = param.writeToValue(&sensorId); status != OK) { |
Shunkai Yao | ac61ee9 | 2024-03-14 21:57:46 +0000 | [diff] [blame^] | 313 | ALOGW("%s %d: write sensorId %d to value failed %d", __func__, __LINE__, |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 314 | sensorId, status); |
| 315 | return status; |
| 316 | } |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 317 | return OK; |
| 318 | } |
| 319 | default: { |
| 320 | ALOGE("%s %d invalid command %u", __func__, __LINE__, command); |
| 321 | return BAD_VALUE; |
| 322 | } |
| 323 | } |
| 324 | } else { |
| 325 | Parameter aidlParam; |
| 326 | DefaultExtension defaultExt; |
| 327 | // read parameters into DefaultExtension vector<uint8_t> |
| 328 | defaultExt.bytes.resize(param.getParameterSize()); |
| 329 | if (OK != param.readFromParameter(defaultExt.bytes.data(), param.getParameterSize())) { |
| 330 | ALOGE("%s %d invalid param %s", __func__, __LINE__, param.toString().c_str()); |
| 331 | param.setStatus(BAD_VALUE); |
| 332 | return BAD_VALUE; |
| 333 | } |
| 334 | |
| 335 | VendorExtension idTag; |
| 336 | idTag.extension.setParcelable(defaultExt); |
| 337 | Parameter::Id id = UNION_MAKE(Parameter::Id, vendorEffectTag, idTag); |
| 338 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam))); |
Shunkai Yao | ec116bf | 2024-02-13 01:57:36 +0000 | [diff] [blame] | 339 | // copy the AIDL extension data back to effect_param_t |
| 340 | return VALUE_OR_RETURN_STATUS( |
| 341 | ::aidl::android::aidl2legacy_Parameter_EffectParameterWriter(aidlParam, param)); |
| 342 | } |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | } // namespace effect |
| 346 | } // namespace android |