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> |
| 19 | #include <optional> |
| 20 | #define LOG_TAG "AidlConversionVirtualizer" |
| 21 | //#define LOG_NDEBUG 0 |
| 22 | |
| 23 | #include <error/expected_utils.h> |
Shunkai Yao | 9b4307f | 2023-02-21 17:49:26 +0000 | [diff] [blame] | 24 | #include <media/AidlConversionCppNdk.h> |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 25 | #include <media/AidlConversionNdk.h> |
| 26 | #include <media/AidlConversionEffect.h> |
| 27 | #include <media/audiohal/AudioEffectUuid.h> |
Shunkai Yao | 9b4307f | 2023-02-21 17:49:26 +0000 | [diff] [blame] | 28 | #include <system/audio_effects/aidl_effects_utils.h> |
| 29 | #include <system/audio_effects/effect_virtualizer.h> |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 30 | |
| 31 | #include <utils/Log.h> |
| 32 | |
| 33 | #include "AidlConversionVirtualizer.h" |
| 34 | |
| 35 | namespace android { |
| 36 | namespace effect { |
| 37 | |
| 38 | using ::aidl::android::aidl_utils::statusTFromBinderStatus; |
Shunkai Yao | 9b4307f | 2023-02-21 17:49:26 +0000 | [diff] [blame] | 39 | using ::aidl::android::getParameterSpecificField; |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 40 | using ::aidl::android::hardware::audio::effect::Parameter; |
Shunkai Yao | 9b4307f | 2023-02-21 17:49:26 +0000 | [diff] [blame] | 41 | using ::aidl::android::hardware::audio::effect::Range; |
| 42 | using ::aidl::android::hardware::audio::effect::Virtualizer; |
Shunkai Yao | da4a640 | 2023-03-03 19:38:17 +0000 | [diff] [blame^] | 43 | using ::aidl::android::hardware::audio::effect::VendorExtension; |
Shunkai Yao | 9b4307f | 2023-02-21 17:49:26 +0000 | [diff] [blame] | 44 | using ::aidl::android::media::audio::common::AudioDeviceDescription; |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 45 | using ::android::status_t; |
| 46 | using utils::EffectParamReader; |
| 47 | using utils::EffectParamWriter; |
| 48 | |
| 49 | status_t AidlConversionVirtualizer::setParameter(EffectParamReader& param) { |
| 50 | uint32_t type = 0; |
Shunkai Yao | 9b4307f | 2023-02-21 17:49:26 +0000 | [diff] [blame] | 51 | if (OK != param.readFromParameter(&type)) { |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 52 | ALOGE("%s invalid param %s", __func__, param.toString().c_str()); |
| 53 | return BAD_VALUE; |
| 54 | } |
| 55 | Parameter aidlParam; |
Shunkai Yao | 9b4307f | 2023-02-21 17:49:26 +0000 | [diff] [blame] | 56 | switch (type) { |
| 57 | case VIRTUALIZER_PARAM_STRENGTH: { |
| 58 | int16_t strength = 0; |
| 59 | if (OK != param.readFromValue(&strength)) { |
| 60 | ALOGE("%s invalid param %s for type %d", __func__, param.toString().c_str(), type); |
| 61 | return BAD_VALUE; |
| 62 | } |
| 63 | aidlParam = MAKE_SPECIFIC_PARAMETER(Virtualizer, virtualizer, strengthPm, strength); |
| 64 | break; |
| 65 | } |
| 66 | case VIRTUALIZER_PARAM_FORCE_VIRTUALIZATION_MODE: { |
| 67 | audio_devices_t deviceType; |
| 68 | if (OK != param.readFromValue(&deviceType)) { |
| 69 | ALOGE("%s invalid param %s for type %d", __func__, param.toString().c_str(), type); |
| 70 | return BAD_VALUE; |
| 71 | } |
| 72 | AudioDeviceDescription deviceDesc = VALUE_OR_RETURN_STATUS( |
| 73 | ::aidl::android::legacy2aidl_audio_devices_t_AudioDeviceDescription( |
| 74 | deviceType)); |
| 75 | aidlParam = MAKE_SPECIFIC_PARAMETER(Virtualizer, virtualizer, device, deviceDesc); |
| 76 | break; |
| 77 | } |
| 78 | default: { |
Shunkai Yao | da4a640 | 2023-03-03 19:38:17 +0000 | [diff] [blame^] | 79 | // for vendor extension, copy data area to the DefaultExtension, parameter ignored |
| 80 | VendorExtension ext = VALUE_OR_RETURN_STATUS( |
| 81 | aidl::android::legacy2aidl_EffectParameterReader_Data_VendorExtension(param)); |
| 82 | aidlParam = MAKE_SPECIFIC_PARAMETER(Virtualizer, virtualizer, vendor, ext); |
| 83 | break; |
Shunkai Yao | 9b4307f | 2023-02-21 17:49:26 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 86 | return statusTFromBinderStatus(mEffect->setParameter(aidlParam)); |
| 87 | } |
| 88 | |
| 89 | status_t AidlConversionVirtualizer::getParameter(EffectParamWriter& param) { |
Shunkai Yao | 9b4307f | 2023-02-21 17:49:26 +0000 | [diff] [blame] | 90 | uint32_t type = 0; |
| 91 | if (OK != param.readFromParameter(&type)) { |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 92 | ALOGE("%s invalid param %s", __func__, param.toString().c_str()); |
| 93 | param.setStatus(BAD_VALUE); |
| 94 | return BAD_VALUE; |
| 95 | } |
Shunkai Yao | 9b4307f | 2023-02-21 17:49:26 +0000 | [diff] [blame] | 96 | Parameter aidlParam; |
| 97 | switch (type) { |
| 98 | case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED: { |
| 99 | // an invalid range indicates not setting support for this parameter |
| 100 | uint32_t support = |
| 101 | ::aidl::android::hardware::audio::effect::isRangeValid<Range::Tag::virtualizer>( |
| 102 | Virtualizer::strengthPm, mDesc.capability); |
| 103 | return param.writeToValue(&support); |
| 104 | } |
| 105 | case VIRTUALIZER_PARAM_STRENGTH: { |
| 106 | Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(Virtualizer, virtualizerTag, |
| 107 | Virtualizer::strengthPm); |
| 108 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam))); |
| 109 | int16_t strength = VALUE_OR_RETURN_STATUS(GET_PARAMETER_SPECIFIC_FIELD( |
| 110 | aidlParam, Virtualizer, virtualizer, Virtualizer::strengthPm, int32_t)); |
| 111 | return param.writeToValue(&strength); |
| 112 | } |
| 113 | case VIRTUALIZER_PARAM_VIRTUAL_SPEAKER_ANGLES: { |
| 114 | audio_channel_mask_t mask; |
| 115 | audio_devices_t device; |
| 116 | if (OK != param.readFromParameter(&mask) || OK != param.readFromParameter(&device)) { |
| 117 | ALOGW("%s illegal param %s", __func__, param.toString().c_str()); |
| 118 | return BAD_VALUE; |
| 119 | } |
| 120 | Virtualizer::SpeakerAnglesPayload payload = { |
| 121 | .layout = VALUE_OR_RETURN_STATUS( |
| 122 | ::aidl::android::legacy2aidl_audio_channel_mask_t_AudioChannelLayout( |
| 123 | mask, false)), |
| 124 | .device = VALUE_OR_RETURN_STATUS( |
| 125 | ::aidl::android::legacy2aidl_audio_devices_t_AudioDeviceDescription( |
| 126 | device))}; |
| 127 | Virtualizer::Id vId = UNION_MAKE(Virtualizer::Id, speakerAnglesPayload, payload); |
| 128 | Parameter::Id id = UNION_MAKE(Parameter::Id, virtualizerTag, vId); |
| 129 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam))); |
| 130 | const auto& angles = VALUE_OR_RETURN_STATUS(GET_PARAMETER_SPECIFIC_FIELD( |
| 131 | aidlParam, Virtualizer, virtualizer, Virtualizer::speakerAngles, |
| 132 | std::vector<Virtualizer::ChannelAngle>)); |
| 133 | for (const auto& angle : angles) { |
| 134 | const audio_channel_mask_t chMask = ::aidl::android:: |
| 135 | aidl2legacy_AudioChannelLayout_layout_audio_channel_mask_t_bits( |
| 136 | angle.channel, false); |
| 137 | ALOGW("%s aidl %d ch %d", __func__, angle.channel, chMask); |
| 138 | if (OK != param.writeToValue(&chMask) || |
| 139 | OK != param.writeToValue(&angle.azimuthDegree) || |
| 140 | OK != param.writeToValue(&angle.elevationDegree)) { |
| 141 | ALOGW("%s can't write angles to param %s", __func__, param.toString().c_str()); |
| 142 | return BAD_VALUE; |
| 143 | } |
| 144 | } |
| 145 | return OK; |
| 146 | } |
| 147 | case VIRTUALIZER_PARAM_VIRTUALIZATION_MODE: { |
| 148 | Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(Virtualizer, virtualizerTag, |
| 149 | Virtualizer::device); |
| 150 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam))); |
| 151 | AudioDeviceDescription device = VALUE_OR_RETURN_STATUS( |
| 152 | GET_PARAMETER_SPECIFIC_FIELD(aidlParam, Virtualizer, virtualizer, |
| 153 | Virtualizer::device, AudioDeviceDescription)); |
| 154 | const audio_devices_t deviceType = VALUE_OR_RETURN_STATUS( |
| 155 | ::aidl::android::aidl2legacy_AudioDeviceDescription_audio_devices_t(device)); |
| 156 | return param.writeToValue(&deviceType); |
| 157 | } |
| 158 | default: { |
Shunkai Yao | da4a640 | 2023-03-03 19:38:17 +0000 | [diff] [blame^] | 159 | VENDOR_EXTENSION_GET_AND_RETURN(Virtualizer, virtualizer, param); |
Shunkai Yao | 9b4307f | 2023-02-21 17:49:26 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | } // namespace effect |
| 165 | } // namespace android |