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