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 | |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 17 | #include <cstddef> |
Shunkai Yao | 57b9339 | 2024-04-26 04:12:21 +0000 | [diff] [blame^] | 18 | #include <cstring> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 19 | #define LOG_TAG "EffectHalAidl" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 22 | #include <memory> |
| 23 | |
Shunkai Yao | 919b73f | 2024-03-13 00:52:08 +0000 | [diff] [blame] | 24 | #include <audio_utils/primitives.h> |
Mikhail Naganov | 5e406ba | 2023-01-13 00:27:22 +0000 | [diff] [blame] | 25 | #include <error/expected_utils.h> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 26 | #include <media/AidlConversionCppNdk.h> |
Shunkai Yao | a03533e | 2023-01-25 06:38:10 +0000 | [diff] [blame] | 27 | #include <media/AidlConversionEffect.h> |
Mikhail Naganov | 5e406ba | 2023-01-13 00:27:22 +0000 | [diff] [blame] | 28 | #include <media/AidlConversionUtil.h> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 29 | #include <media/EffectsFactoryApi.h> |
| 30 | #include <mediautils/TimeCheck.h> |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 31 | #include <system/audio.h> |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 32 | #include <system/audio_effects/effect_uuid.h> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 33 | #include <utils/Log.h> |
| 34 | |
| 35 | #include "EffectHalAidl.h" |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 36 | #include "EffectProxy.h" |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 37 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 38 | #include <aidl/android/hardware/audio/effect/IEffect.h> |
| 39 | |
Shunkai Yao | dba8ba3 | 2023-01-27 17:02:21 +0000 | [diff] [blame] | 40 | #include "effectsAidlConversion/AidlConversionAec.h" |
Shunkai Yao | 61ce957 | 2023-02-28 23:55:33 +0000 | [diff] [blame] | 41 | #include "effectsAidlConversion/AidlConversionAgc1.h" |
Shunkai Yao | dba8ba3 | 2023-01-27 17:02:21 +0000 | [diff] [blame] | 42 | #include "effectsAidlConversion/AidlConversionAgc2.h" |
| 43 | #include "effectsAidlConversion/AidlConversionBassBoost.h" |
| 44 | #include "effectsAidlConversion/AidlConversionDownmix.h" |
| 45 | #include "effectsAidlConversion/AidlConversionDynamicsProcessing.h" |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 46 | #include "effectsAidlConversion/AidlConversionEnvReverb.h" |
| 47 | #include "effectsAidlConversion/AidlConversionEq.h" |
| 48 | #include "effectsAidlConversion/AidlConversionHapticGenerator.h" |
| 49 | #include "effectsAidlConversion/AidlConversionLoudnessEnhancer.h" |
| 50 | #include "effectsAidlConversion/AidlConversionNoiseSuppression.h" |
| 51 | #include "effectsAidlConversion/AidlConversionPresetReverb.h" |
| 52 | #include "effectsAidlConversion/AidlConversionSpatializer.h" |
| 53 | #include "effectsAidlConversion/AidlConversionVendorExtension.h" |
| 54 | #include "effectsAidlConversion/AidlConversionVirtualizer.h" |
| 55 | #include "effectsAidlConversion/AidlConversionVisualizer.h" |
Shunkai Yao | dba8ba3 | 2023-01-27 17:02:21 +0000 | [diff] [blame] | 56 | |
Mikhail Naganov | 5e406ba | 2023-01-13 00:27:22 +0000 | [diff] [blame] | 57 | using ::aidl::android::aidl_utils::statusTFromBinderStatus; |
David Li | a691343 | 2024-02-20 09:54:14 +0800 | [diff] [blame] | 58 | using ::aidl::android::hardware::audio::effect::CommandId; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 59 | using ::aidl::android::hardware::audio::effect::Descriptor; |
| 60 | using ::aidl::android::hardware::audio::effect::IEffect; |
Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 61 | using ::aidl::android::hardware::audio::effect::IFactory; |
Shunkai Yao | e910bfd | 2024-04-18 04:36:42 +0000 | [diff] [blame] | 62 | using ::aidl::android::hardware::audio::effect::kEventFlagDataMqNotEmpty; |
Shunkai Yao | 1afb46c | 2024-01-09 20:40:45 +0000 | [diff] [blame] | 63 | using ::aidl::android::hardware::audio::effect::kEventFlagDataMqUpdate; |
Shunkai Yao | e910bfd | 2024-04-18 04:36:42 +0000 | [diff] [blame] | 64 | using ::aidl::android::hardware::audio::effect::kEventFlagNotEmpty; |
Shunkai Yao | e317e33 | 2024-02-06 00:39:49 +0000 | [diff] [blame] | 65 | using ::aidl::android::hardware::audio::effect::kReopenSupportedVersion; |
Shunkai Yao | 26689d9 | 2023-08-01 23:06:13 +0000 | [diff] [blame] | 66 | using ::aidl::android::hardware::audio::effect::State; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 67 | |
| 68 | namespace android { |
| 69 | namespace effect { |
| 70 | |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 71 | EffectHalAidl::EffectHalAidl(const std::shared_ptr<IFactory>& factory, |
Shunkai Yao | e317e33 | 2024-02-06 00:39:49 +0000 | [diff] [blame] | 72 | const std::shared_ptr<IEffect>& effect, int32_t sessionId, |
| 73 | int32_t ioId, const Descriptor& desc, bool isProxyEffect) |
Shunkai Yao | dba8ba3 | 2023-01-27 17:02:21 +0000 | [diff] [blame] | 74 | : mFactory(factory), |
Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 75 | mEffect(effect), |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame] | 76 | mSessionId(sessionId), |
| 77 | mIoId(ioId), |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 78 | mIsProxyEffect(isProxyEffect) { |
Shunkai Yao | e317e33 | 2024-02-06 00:39:49 +0000 | [diff] [blame] | 79 | assert(mFactory != nullptr); |
| 80 | assert(mEffect != nullptr); |
Shunkai Yao | dba8ba3 | 2023-01-27 17:02:21 +0000 | [diff] [blame] | 81 | createAidlConversion(effect, sessionId, ioId, desc); |
| 82 | } |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 83 | |
Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 84 | EffectHalAidl::~EffectHalAidl() { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 85 | if (mEffect) { |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 86 | if (mIsProxyEffect) { |
| 87 | std::static_pointer_cast<EffectProxy>(mEffect)->destroy(); |
| 88 | } else if (mFactory) { |
| 89 | mFactory->destroyEffect(mEffect); |
| 90 | } |
Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 93 | |
Shunkai Yao | dba8ba3 | 2023-01-27 17:02:21 +0000 | [diff] [blame] | 94 | status_t EffectHalAidl::createAidlConversion( |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 95 | std::shared_ptr<IEffect> effect, |
Shunkai Yao | dba8ba3 | 2023-01-27 17:02:21 +0000 | [diff] [blame] | 96 | int32_t sessionId, int32_t ioId, |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 97 | const Descriptor& desc) { |
Shunkai Yao | dba8ba3 | 2023-01-27 17:02:21 +0000 | [diff] [blame] | 98 | const auto& typeUuid = desc.common.id.type; |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 99 | ALOGI("%s create UUID %s", __func__, typeUuid.toString().c_str()); |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 100 | if (typeUuid == |
| 101 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidAcousticEchoCanceler()) { |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 102 | mConversion = std::make_unique<android::effect::AidlConversionAec>(effect, sessionId, ioId, |
| 103 | desc, mIsProxyEffect); |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 104 | } else if (typeUuid == ::aidl::android::hardware::audio::effect:: |
| 105 | getEffectTypeUuidAutomaticGainControlV1()) { |
Shunkai Yao | 61ce957 | 2023-02-28 23:55:33 +0000 | [diff] [blame] | 106 | mConversion = std::make_unique<android::effect::AidlConversionAgc1>(effect, sessionId, ioId, |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 107 | desc, mIsProxyEffect); |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 108 | } else if (typeUuid == ::aidl::android::hardware::audio::effect:: |
| 109 | getEffectTypeUuidAutomaticGainControlV2()) { |
Shunkai Yao | dba8ba3 | 2023-01-27 17:02:21 +0000 | [diff] [blame] | 110 | mConversion = std::make_unique<android::effect::AidlConversionAgc2>(effect, sessionId, ioId, |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 111 | desc, mIsProxyEffect); |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 112 | } else if (typeUuid == ::aidl::android::hardware::audio::effect::getEffectTypeUuidBassBoost()) { |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 113 | mConversion = std::make_unique<android::effect::AidlConversionBassBoost>( |
| 114 | effect, sessionId, ioId, desc, mIsProxyEffect); |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 115 | } else if (typeUuid == ::aidl::android::hardware::audio::effect::getEffectTypeUuidDownmix()) { |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 116 | mConversion = std::make_unique<android::effect::AidlConversionDownmix>( |
| 117 | effect, sessionId, ioId, desc, mIsProxyEffect); |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 118 | } else if (typeUuid == |
| 119 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidDynamicsProcessing()) { |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 120 | mConversion = std::make_unique<android::effect::AidlConversionDp>(effect, sessionId, ioId, |
| 121 | desc, mIsProxyEffect); |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 122 | } else if (typeUuid == ::aidl::android::hardware::audio::effect::getEffectTypeUuidEnvReverb()) { |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 123 | mConversion = std::make_unique<android::effect::AidlConversionEnvReverb>( |
| 124 | effect, sessionId, ioId, desc, mIsProxyEffect); |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 125 | } else if (typeUuid == ::aidl::android::hardware::audio::effect::getEffectTypeUuidEqualizer()) { |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 126 | mConversion = std::make_unique<android::effect::AidlConversionEq>(effect, sessionId, ioId, |
| 127 | desc, mIsProxyEffect); |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 128 | } else if (typeUuid == |
| 129 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidHapticGenerator()) { |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 130 | mConversion = std::make_unique<android::effect::AidlConversionHapticGenerator>( |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 131 | effect, sessionId, ioId, desc, mIsProxyEffect); |
Shunkai Yao | 57b9339 | 2024-04-26 04:12:21 +0000 | [diff] [blame^] | 132 | mIsHapticGenerator = true; |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 133 | } else if (typeUuid == |
| 134 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidLoudnessEnhancer()) { |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 135 | mConversion = std::make_unique<android::effect::AidlConversionLoudnessEnhancer>( |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 136 | effect, sessionId, ioId, desc, mIsProxyEffect); |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 137 | } else if (typeUuid == |
| 138 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidNoiseSuppression()) { |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 139 | mConversion = std::make_unique<android::effect::AidlConversionNoiseSuppression>( |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 140 | effect, sessionId, ioId, desc, mIsProxyEffect); |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 141 | } else if (typeUuid == |
| 142 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidPresetReverb()) { |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 143 | mConversion = std::make_unique<android::effect::AidlConversionPresetReverb>( |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 144 | effect, sessionId, ioId, desc, mIsProxyEffect); |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 145 | } else if (typeUuid == |
| 146 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidSpatializer()) { |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 147 | mConversion = std::make_unique<android::effect::AidlConversionSpatializer>( |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 148 | effect, sessionId, ioId, desc, mIsProxyEffect); |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 149 | } else if (typeUuid == |
| 150 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidVirtualizer()) { |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 151 | mConversion = std::make_unique<android::effect::AidlConversionVirtualizer>( |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 152 | effect, sessionId, ioId, desc, mIsProxyEffect); |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame] | 153 | } else if (typeUuid == |
| 154 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidVisualizer()) { |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 155 | mConversion = std::make_unique<android::effect::AidlConversionVisualizer>( |
| 156 | effect, sessionId, ioId, desc, mIsProxyEffect); |
Shunkai Yao | dba8ba3 | 2023-01-27 17:02:21 +0000 | [diff] [blame] | 157 | } else { |
Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame] | 158 | // For unknown UUID, use vendor extension implementation |
| 159 | mConversion = std::make_unique<android::effect::AidlConversionVendorExtension>( |
Shunkai Yao | 323273d | 2023-05-24 00:45:43 +0000 | [diff] [blame] | 160 | effect, sessionId, ioId, desc, mIsProxyEffect); |
Shunkai Yao | dba8ba3 | 2023-01-27 17:02:21 +0000 | [diff] [blame] | 161 | } |
| 162 | return OK; |
| 163 | } |
| 164 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 165 | status_t EffectHalAidl::setInBuffer(const sp<EffectBufferHalInterface>& buffer) { |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 166 | mInBuffer = buffer; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 167 | return OK; |
| 168 | } |
| 169 | |
| 170 | status_t EffectHalAidl::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) { |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 171 | mOutBuffer = buffer; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 172 | return OK; |
| 173 | } |
| 174 | |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 175 | // write to input FMQ here, wait for statusMQ STATUS_OK, and read from output FMQ |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 176 | status_t EffectHalAidl::process() { |
Shunkai Yao | 1afb46c | 2024-01-09 20:40:45 +0000 | [diff] [blame] | 177 | const std::string effectName = mConversion->getDescriptor().common.name; |
Shunkai Yao | 26689d9 | 2023-08-01 23:06:13 +0000 | [diff] [blame] | 178 | State state = State::INIT; |
| 179 | if (mConversion->isBypassing() || !mEffect->getState(&state).isOk() || |
| 180 | state != State::PROCESSING) { |
Shunkai Yao | 1afb46c | 2024-01-09 20:40:45 +0000 | [diff] [blame] | 181 | ALOGI("%s skipping %s process because it's %s", __func__, effectName.c_str(), |
Shunkai Yao | 26689d9 | 2023-08-01 23:06:13 +0000 | [diff] [blame] | 182 | mConversion->isBypassing() |
| 183 | ? "bypassing" |
| 184 | : aidl::android::hardware::audio::effect::toString(state).c_str()); |
Jaideep Sharma | bd9b0d3 | 2023-09-20 11:50:25 +0530 | [diff] [blame] | 185 | return -ENODATA; |
Shunkai Yao | 26689d9 | 2023-08-01 23:06:13 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Shunkai Yao | 1afb46c | 2024-01-09 20:40:45 +0000 | [diff] [blame] | 188 | // check if the DataMq needs any update, timeout at 1ns to avoid being blocked |
| 189 | auto efGroup = mConversion->getEventFlagGroup(); |
| 190 | if (!efGroup) { |
| 191 | ALOGE("%s invalid efGroup", __func__); |
| 192 | return INVALID_OPERATION; |
| 193 | } |
| 194 | |
Shunkai Yao | e317e33 | 2024-02-06 00:39:49 +0000 | [diff] [blame] | 195 | // use IFactory HAL version because IEffect can be an EffectProxy instance |
| 196 | static const int halVersion = [&]() { |
| 197 | int version = 0; |
| 198 | return mFactory->getInterfaceVersion(&version).isOk() ? version : 0; |
| 199 | }(); |
| 200 | |
| 201 | if (uint32_t efState = 0; halVersion >= kReopenSupportedVersion && |
| 202 | ::android::OK == efGroup->wait(kEventFlagDataMqUpdate, &efState, |
| 203 | 1 /* ns */, true /* retry */) && |
| 204 | efState & kEventFlagDataMqUpdate) { |
Shunkai Yao | 57b9339 | 2024-04-26 04:12:21 +0000 | [diff] [blame^] | 205 | ALOGD("%s %s V%d receive dataMQUpdate eventFlag from HAL", __func__, effectName.c_str(), |
Shunkai Yao | e317e33 | 2024-02-06 00:39:49 +0000 | [diff] [blame] | 206 | halVersion); |
Shunkai Yao | e910bfd | 2024-04-18 04:36:42 +0000 | [diff] [blame] | 207 | |
Shunkai Yao | 1afb46c | 2024-01-09 20:40:45 +0000 | [diff] [blame] | 208 | mConversion->reopen(); |
| 209 | } |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 210 | auto statusQ = mConversion->getStatusMQ(); |
| 211 | auto inputQ = mConversion->getInputMQ(); |
| 212 | auto outputQ = mConversion->getOutputMQ(); |
| 213 | if (!statusQ || !statusQ->isValid() || !inputQ || !inputQ->isValid() || !outputQ || |
Shunkai Yao | 1afb46c | 2024-01-09 20:40:45 +0000 | [diff] [blame] | 214 | !outputQ->isValid()) { |
| 215 | ALOGE("%s invalid FMQ [Status %d I %d O %d]", __func__, statusQ ? statusQ->isValid() : 0, |
| 216 | inputQ ? inputQ->isValid() : 0, outputQ ? outputQ->isValid() : 0); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 217 | return INVALID_OPERATION; |
| 218 | } |
| 219 | |
| 220 | size_t available = inputQ->availableToWrite(); |
Shunkai Yao | 57b9339 | 2024-04-26 04:12:21 +0000 | [diff] [blame^] | 221 | const size_t floatsToWrite = std::min(available, mInBuffer->getSize() / sizeof(float)); |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 222 | if (floatsToWrite == 0) { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 223 | ALOGE("%s not able to write, floats in buffer %zu, space in FMQ %zu", __func__, |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 224 | mInBuffer->getSize() / sizeof(float), available); |
| 225 | return INVALID_OPERATION; |
| 226 | } |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 227 | if (!mInBuffer->audioBuffer() || |
| 228 | !inputQ->write((float*)mInBuffer->audioBuffer()->f32, floatsToWrite)) { |
| 229 | ALOGE("%s failed to write %zu floats from audiobuffer %p to inputQ [avail %zu]", __func__, |
| 230 | floatsToWrite, mInBuffer->audioBuffer(), inputQ->availableToWrite()); |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 231 | return INVALID_OPERATION; |
| 232 | } |
Shunkai Yao | e910bfd | 2024-04-18 04:36:42 +0000 | [diff] [blame] | 233 | |
| 234 | // for V2 audio effect HAL, expect different EventFlag to avoid bit conflict with FMQ_NOT_EMPTY |
| 235 | efGroup->wake(halVersion >= kReopenSupportedVersion ? kEventFlagDataMqNotEmpty |
| 236 | : kEventFlagNotEmpty); |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 237 | |
| 238 | IEffect::Status retStatus{}; |
Shunkai Yao | e910bfd | 2024-04-18 04:36:42 +0000 | [diff] [blame] | 239 | if (!statusQ->readBlocking(&retStatus, 1)) { |
| 240 | ALOGE("%s %s V%d read status from status FMQ failed", __func__, effectName.c_str(), |
| 241 | halVersion); |
| 242 | return INVALID_OPERATION; |
| 243 | } |
| 244 | if (retStatus.status != OK || (size_t)retStatus.fmqConsumed != floatsToWrite || |
| 245 | retStatus.fmqProduced == 0) { |
| 246 | ALOGE("%s read status failed: %s, consumed %d (of %zu) produced %d", __func__, |
| 247 | retStatus.toString().c_str(), retStatus.fmqConsumed, floatsToWrite, |
| 248 | retStatus.fmqProduced); |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 249 | return INVALID_OPERATION; |
| 250 | } |
| 251 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 252 | available = outputQ->availableToRead(); |
Shunkai Yao | 57b9339 | 2024-04-26 04:12:21 +0000 | [diff] [blame^] | 253 | const size_t floatsToRead = std::min(available, mOutBuffer->getSize() / sizeof(float)); |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 254 | if (floatsToRead == 0) { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 255 | ALOGE("%s not able to read, buffer space %zu, floats in FMQ %zu", __func__, |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 256 | mOutBuffer->getSize() / sizeof(float), available); |
| 257 | return INVALID_OPERATION; |
| 258 | } |
Shunkai Yao | 919b73f | 2024-03-13 00:52:08 +0000 | [diff] [blame] | 259 | |
| 260 | float *outputRawBuffer = mOutBuffer->audioBuffer()->f32; |
| 261 | std::vector<float> tempBuffer; |
Shunkai Yao | 57b9339 | 2024-04-26 04:12:21 +0000 | [diff] [blame^] | 262 | // keep original data in the output buffer for accumulate mode or HapticGenerator effect |
| 263 | if (mConversion->mOutputAccessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE || mIsHapticGenerator) { |
Shunkai Yao | 919b73f | 2024-03-13 00:52:08 +0000 | [diff] [blame] | 264 | tempBuffer.resize(floatsToRead); |
| 265 | outputRawBuffer = tempBuffer.data(); |
| 266 | } |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 267 | // always read floating point data for AIDL |
Shunkai Yao | 919b73f | 2024-03-13 00:52:08 +0000 | [diff] [blame] | 268 | if (!outputQ->read(outputRawBuffer, floatsToRead)) { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 269 | ALOGE("%s failed to read %zu from outputQ to audioBuffer %p", __func__, floatsToRead, |
| 270 | mOutBuffer->audioBuffer()); |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 271 | return INVALID_OPERATION; |
| 272 | } |
Shunkai Yao | 57b9339 | 2024-04-26 04:12:21 +0000 | [diff] [blame^] | 273 | |
| 274 | // HapticGenerator needs special handling because the generated haptic samples should append to |
| 275 | // the end of audio samples, the generated haptic data pass back from HAL in output FMQ at same |
| 276 | // offset as input buffer, here we skip the audio samples in output FMQ and append haptic |
| 277 | // samples to the end of input buffer |
| 278 | if (mIsHapticGenerator) { |
| 279 | static constexpr float kHalFloatSampleLimit = 2.0f; |
| 280 | assert(floatsToRead == floatsToWrite); |
| 281 | const auto audioChNum = mConversion->getAudioChannelCount(); |
| 282 | const auto audioSamples = |
| 283 | floatsToWrite * audioChNum / (audioChNum + mConversion->getHapticChannelCount()); |
| 284 | // accumulate or copy input to output, haptic samples remains all zero |
| 285 | if (mConversion->mOutputAccessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) { |
| 286 | accumulate_float(mOutBuffer->audioBuffer()->f32, mInBuffer->audioBuffer()->f32, |
| 287 | audioSamples); |
| 288 | } else { |
| 289 | memcpy_to_float_from_float_with_clamping(mOutBuffer->audioBuffer()->f32, |
| 290 | mInBuffer->audioBuffer()->f32, audioSamples, |
| 291 | kHalFloatSampleLimit); |
| 292 | } |
| 293 | // append the haptic sample at the end of input audio samples |
| 294 | memcpy_to_float_from_float_with_clamping(mInBuffer->audioBuffer()->f32 + audioSamples, |
| 295 | outputRawBuffer + audioSamples, |
| 296 | floatsToRead - audioSamples, kHalFloatSampleLimit); |
| 297 | } else if (mConversion->mOutputAccessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) { |
Shunkai Yao | 919b73f | 2024-03-13 00:52:08 +0000 | [diff] [blame] | 298 | accumulate_float(mOutBuffer->audioBuffer()->f32, outputRawBuffer, floatsToRead); |
| 299 | } |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 300 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 301 | return OK; |
| 302 | } |
| 303 | |
| 304 | // TODO: no one using, maybe deprecate this interface |
| 305 | status_t EffectHalAidl::processReverse() { |
| 306 | ALOGW("%s not implemented yet", __func__); |
| 307 | return OK; |
| 308 | } |
| 309 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 310 | status_t EffectHalAidl::command(uint32_t cmdCode, uint32_t cmdSize, void* pCmdData, |
| 311 | uint32_t* replySize, void* pReplyData) { |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 312 | TIME_CHECK(); |
| 313 | if (!mConversion) { |
| 314 | ALOGE("%s can not handle command %d when conversion not exist", __func__, cmdCode); |
| 315 | return INVALID_OPERATION; |
| 316 | } |
| 317 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 318 | return mConversion->handleCommand(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | status_t EffectHalAidl::getDescriptor(effect_descriptor_t* pDescriptor) { |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 322 | TIME_CHECK(); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 323 | if (pDescriptor == nullptr) { |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 324 | ALOGE("%s null descriptor pointer", __func__); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 325 | return BAD_VALUE; |
| 326 | } |
| 327 | Descriptor aidlDesc; |
Mikhail Naganov | 5e406ba | 2023-01-13 00:27:22 +0000 | [diff] [blame] | 328 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getDescriptor(&aidlDesc))); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 329 | |
| 330 | *pDescriptor = VALUE_OR_RETURN_STATUS( |
| 331 | ::aidl::android::aidl2legacy_Descriptor_effect_descriptor(aidlDesc)); |
| 332 | return OK; |
| 333 | } |
| 334 | |
| 335 | status_t EffectHalAidl::close() { |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 336 | TIME_CHECK(); |
David Li | a691343 | 2024-02-20 09:54:14 +0800 | [diff] [blame] | 337 | mEffect->command(CommandId::STOP); |
Shunkai Yao | 44bdbad | 2023-01-14 05:11:58 +0000 | [diff] [blame] | 338 | return statusTFromBinderStatus(mEffect->close()); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | status_t EffectHalAidl::dump(int fd) { |
Shunkai Yao | c630871 | 2023-02-22 17:53:04 +0000 | [diff] [blame] | 342 | TIME_CHECK(); |
| 343 | return mEffect->dump(fd, nullptr, 0); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | } // namespace effect |
| 347 | } // namespace android |