Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +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 <algorithm> |
Shunkai Yao | 849ba4c | 2023-07-11 18:10:18 +0000 | [diff] [blame] | 18 | #include <cstddef> |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 19 | #include <iterator> |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 20 | #include <memory> |
| 21 | #define LOG_TAG "EffectProxy" |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 22 | // #define LOG_NDEBUG 0 |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 23 | |
| 24 | #include <fmq/AidlMessageQueue.h> |
Shunkai Yao | 007caf0 | 2023-05-17 00:52:54 +0000 | [diff] [blame] | 25 | #include <system/audio_aidl_utils.h> |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | |
| 28 | #include "EffectProxy.h" |
| 29 | |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 30 | using ::aidl::android::hardware::audio::effect::Capability; |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 31 | using ::aidl::android::hardware::audio::effect::CommandId; |
| 32 | using ::aidl::android::hardware::audio::effect::Descriptor; |
| 33 | using ::aidl::android::hardware::audio::effect::Flags; |
| 34 | using ::aidl::android::hardware::audio::effect::IEffect; |
| 35 | using ::aidl::android::hardware::audio::effect::IFactory; |
| 36 | using ::aidl::android::hardware::audio::effect::Parameter; |
| 37 | using ::aidl::android::hardware::audio::effect::State; |
| 38 | using ::aidl::android::media::audio::common::AudioUuid; |
| 39 | |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 40 | namespace android::effect { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 41 | |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 42 | EffectProxy::EffectProxy(const AudioUuid& uuid, const std::vector<Descriptor>& descriptors, |
| 43 | const std::shared_ptr<IFactory>& factory) |
| 44 | : mDescriptorCommon(buildDescriptorCommon(uuid, descriptors)), |
| 45 | mSubEffects( |
| 46 | [](const std::vector<Descriptor>& descs, const std::shared_ptr<IFactory>& factory) { |
| 47 | std::vector<SubEffect> subEffects; |
| 48 | ALOG_ASSERT(factory, "invalid EffectFactory handle"); |
| 49 | ndk::ScopedAStatus status = ndk::ScopedAStatus::ok(); |
| 50 | for (const auto& desc : descs) { |
| 51 | SubEffect sub({.descriptor = desc}); |
| 52 | status = factory->createEffect(desc.common.id.uuid, &sub.handle); |
| 53 | if (!status.isOk() || !sub.handle) { |
| 54 | sub.handle = nullptr; |
| 55 | ALOGW("%s create sub-effect %s failed", __func__, |
| 56 | ::android::audio::utils::toString(desc.common.id.uuid).c_str()); |
| 57 | } |
| 58 | subEffects.emplace_back(sub); |
| 59 | } |
| 60 | return subEffects; |
| 61 | }(descriptors, factory)), |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 62 | mFactory(factory) {} |
| 63 | |
| 64 | EffectProxy::~EffectProxy() { |
| 65 | close(); |
| 66 | destroy(); |
| 67 | mSubEffects.clear(); |
| 68 | } |
| 69 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 70 | ndk::ScopedAStatus EffectProxy::destroy() { |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 71 | ALOGV("%s: %s", __func__, |
| 72 | ::android::audio::utils::toString(mDescriptorCommon.id.type).c_str()); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 73 | return runWithAllSubEffects([&](std::shared_ptr<IEffect>& effect) { |
| 74 | ndk::ScopedAStatus status = mFactory->destroyEffect(effect); |
| 75 | if (status.isOk()) { |
| 76 | effect.reset(); |
| 77 | } |
| 78 | return status; |
| 79 | }); |
| 80 | } |
| 81 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 82 | ndk::ScopedAStatus EffectProxy::setOffloadParam(const effect_offload_param_t* offload) { |
| 83 | const auto& itor = std::find_if(mSubEffects.begin(), mSubEffects.end(), [&](const auto& sub) { |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 84 | const auto& desc = sub.descriptor; |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 85 | return offload->isOffload == |
| 86 | (desc.common.flags.hwAcceleratorMode == Flags::HardwareAccelerator::TUNNEL); |
| 87 | }); |
| 88 | if (itor == mSubEffects.end()) { |
| 89 | ALOGE("%s no %soffload sub-effect found", __func__, offload->isOffload ? "" : "non-"); |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 90 | mActiveSubIdx = 0; |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 91 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_NULL_POINTER, |
| 92 | "noActiveEffctFound"); |
| 93 | } |
| 94 | |
Shunkai Yao | 4fe6644 | 2023-07-25 21:51:24 +0000 | [diff] [blame] | 95 | mActiveSubIdx = std::distance(mSubEffects.begin(), itor); |
Shunkai Yao | 849ba4c | 2023-07-11 18:10:18 +0000 | [diff] [blame] | 96 | ALOGI("%s: active %soffload sub-effect %zu descriptor: %s", __func__, |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 97 | offload->isOffload ? "" : "non-", mActiveSubIdx, |
| 98 | ::android::audio::utils::toString(mSubEffects[mActiveSubIdx].descriptor.common.id.uuid) |
| 99 | .c_str()); |
Shunkai Yao | 849ba4c | 2023-07-11 18:10:18 +0000 | [diff] [blame] | 100 | return runWithAllSubEffects([&](std::shared_ptr<IEffect>& effect) { |
| 101 | return effect->setParameter(Parameter::make<Parameter::offload>(offload->isOffload)); |
| 102 | }); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // EffectProxy go over sub-effects and call IEffect interfaces |
| 106 | ndk::ScopedAStatus EffectProxy::open(const Parameter::Common& common, |
| 107 | const std::optional<Parameter::Specific>& specific, |
| 108 | IEffect::OpenEffectReturn* ret __unused) { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 109 | ndk::ScopedAStatus status = ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 110 | EX_ILLEGAL_ARGUMENT, "nullEffectHandle"); |
| 111 | for (auto& sub : mSubEffects) { |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 112 | IEffect::OpenEffectReturn openReturn; |
| 113 | if (!sub.handle || !(status = sub.handle->open(common, specific, &openReturn)).isOk()) { |
| 114 | ALOGE("%s: failed to open %p UUID %s", __func__, sub.handle.get(), |
| 115 | ::android::audio::utils::toString(sub.descriptor.common.id.uuid).c_str()); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 116 | break; |
| 117 | } |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 118 | sub.effectMq.statusQ = std::make_shared<StatusMQ>(openReturn.statusMQ); |
| 119 | sub.effectMq.inputQ = std::make_shared<DataMQ>(openReturn.inputDataMQ); |
| 120 | sub.effectMq.outputQ = std::make_shared<DataMQ>(openReturn.outputDataMQ); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // close all opened effects if failure |
| 124 | if (!status.isOk()) { |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 125 | ALOGE("%s: closing all sub-effects with error %s", __func__, |
| 126 | status.getDescription().c_str()); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 127 | close(); |
| 128 | } |
| 129 | |
| 130 | return status; |
| 131 | } |
| 132 | |
| 133 | ndk::ScopedAStatus EffectProxy::close() { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 134 | return runWithAllSubEffects([&](std::shared_ptr<IEffect>& effect) { |
| 135 | return effect->close(); |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | ndk::ScopedAStatus EffectProxy::getDescriptor(Descriptor* desc) { |
Shunkai Yao | df7bd8b | 2023-07-13 23:25:38 +0000 | [diff] [blame] | 140 | *desc = mSubEffects[mActiveSubIdx].descriptor; |
| 141 | desc->common.id.uuid = desc->common.id.proxy.value(); |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 142 | return ndk::ScopedAStatus::ok(); |
| 143 | } |
| 144 | |
| 145 | ndk::ScopedAStatus EffectProxy::buildDescriptor(const AudioUuid& uuid, |
| 146 | const std::vector<Descriptor>& subEffectDescs, |
| 147 | Descriptor* desc) { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 148 | if (!desc) { |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 149 | ALOGE("%s: null descriptor pointer", __func__); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 150 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_NULL_POINTER, "nullptr"); |
| 151 | } |
| 152 | |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 153 | if (subEffectDescs.size() < 2) { |
| 154 | ALOGE("%s: proxy need at least 2 sub-effects, got %zu", __func__, subEffectDescs.size()); |
| 155 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 156 | "needMoreSubEffects"); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 159 | desc->common = buildDescriptorCommon(uuid, subEffectDescs); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 160 | return ndk::ScopedAStatus::ok(); |
| 161 | } |
| 162 | |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 163 | Descriptor::Common EffectProxy::buildDescriptorCommon( |
| 164 | const AudioUuid& uuid, const std::vector<Descriptor>& subEffectDescs) { |
| 165 | Descriptor::Common common; |
| 166 | |
| 167 | for (const auto& desc : subEffectDescs) { |
| 168 | if (desc.common.flags.hwAcceleratorMode == Flags::HardwareAccelerator::TUNNEL) { |
| 169 | common.flags.hwAcceleratorMode = Flags::HardwareAccelerator::TUNNEL; |
| 170 | } |
| 171 | |
Shunkai Yao | df7bd8b | 2023-07-13 23:25:38 +0000 | [diff] [blame] | 172 | // initial flag values before we know which sub-effect to active (with setOffloadParam) |
| 173 | // same as HIDL EffectProxy flags |
| 174 | common.flags.type = Flags::Type::INSERT; |
| 175 | common.flags.insert = Flags::Insert::LAST; |
| 176 | common.flags.volume = Flags::Volume::NONE; |
| 177 | |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 178 | // set indication if any sub-effect indication was set |
| 179 | common.flags.offloadIndication |= desc.common.flags.offloadIndication; |
| 180 | common.flags.deviceIndication |= desc.common.flags.deviceIndication; |
| 181 | common.flags.audioModeIndication |= desc.common.flags.audioModeIndication; |
| 182 | common.flags.audioSourceIndication |= desc.common.flags.audioSourceIndication; |
| 183 | } |
| 184 | |
| 185 | // initial flag values before we know which sub-effect to active (with setOffloadParam) |
| 186 | // same as HIDL EffectProxy flags |
| 187 | common.flags.type = Flags::Type::INSERT; |
| 188 | common.flags.insert = Flags::Insert::LAST; |
| 189 | common.flags.volume = Flags::Volume::CTRL; |
| 190 | |
| 191 | // copy type UUID from any of sub-effects, all sub-effects should have same type |
| 192 | common.id.type = subEffectDescs[0].common.id.type; |
| 193 | // replace implementation UUID with proxy UUID. |
| 194 | common.id.uuid = uuid; |
| 195 | common.id.proxy = std::nullopt; |
| 196 | common.name = "Proxy"; |
| 197 | common.implementor = "AOSP"; |
| 198 | return common; |
| 199 | } |
| 200 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 201 | // Handle with active sub-effect first, only send to other sub-effects when success |
| 202 | ndk::ScopedAStatus EffectProxy::command(CommandId id) { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 203 | return runWithActiveSubEffectThenOthers( |
| 204 | [&](const std::shared_ptr<IEffect>& effect) -> ndk::ScopedAStatus { |
| 205 | return effect->command(id); |
| 206 | }); |
| 207 | } |
| 208 | |
| 209 | // Return the active sub-effect state |
| 210 | ndk::ScopedAStatus EffectProxy::getState(State* state) { |
| 211 | return runWithActiveSubEffect( |
| 212 | [&](const std::shared_ptr<IEffect>& effect) -> ndk::ScopedAStatus { |
| 213 | return effect->getState(state); |
| 214 | }); |
| 215 | } |
| 216 | |
| 217 | // Handle with active sub-effect first, only send to other sub-effects when success |
| 218 | ndk::ScopedAStatus EffectProxy::setParameter(const Parameter& param) { |
| 219 | return runWithActiveSubEffectThenOthers( |
| 220 | [&](const std::shared_ptr<IEffect>& effect) -> ndk::ScopedAStatus { |
| 221 | return effect->setParameter(param); |
| 222 | }); |
| 223 | } |
| 224 | |
| 225 | // Return the active sub-effect parameter |
| 226 | ndk::ScopedAStatus EffectProxy::getParameter(const Parameter::Id& id, Parameter* param) { |
| 227 | return runWithActiveSubEffect( |
| 228 | [&](const std::shared_ptr<IEffect>& effect) -> ndk::ScopedAStatus { |
| 229 | return effect->getParameter(id, param); |
| 230 | }); |
| 231 | } |
| 232 | |
| 233 | ndk::ScopedAStatus EffectProxy::runWithActiveSubEffectThenOthers( |
| 234 | std::function<ndk::ScopedAStatus(const std::shared_ptr<IEffect>&)> const& func) { |
| 235 | ndk::ScopedAStatus status = runWithActiveSubEffect(func); |
| 236 | if (!status.isOk()) { |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 237 | ALOGE("%s active sub-effect return error %s", __func__, status.getDescription().c_str()); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 240 | // proceed with others |
Jaideep Sharma | d823104 | 2023-08-14 13:03:18 +0530 | [diff] [blame^] | 241 | for (size_t i = 0; i < mSubEffects.size(); i++) { |
| 242 | if (i == mActiveSubIdx) { |
| 243 | continue; |
| 244 | } |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 245 | if (!mSubEffects[i].handle) { |
| 246 | ALOGE("%s null sub-effect interface for %s", __func__, |
| 247 | mSubEffects[i].descriptor.common.id.uuid.toString().c_str()); |
| 248 | continue; |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 249 | } |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 250 | func(mSubEffects[i].handle); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 251 | } |
| 252 | return status; |
| 253 | } |
| 254 | |
| 255 | ndk::ScopedAStatus EffectProxy::runWithActiveSubEffect( |
| 256 | std::function<ndk::ScopedAStatus(const std::shared_ptr<IEffect>&)> const& func) { |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 257 | if (!mSubEffects[mActiveSubIdx].handle) { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 258 | ALOGE("%s null active sub-effect interface, active %s", __func__, |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 259 | mSubEffects[mActiveSubIdx].descriptor.toString().c_str()); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 260 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_NULL_POINTER, |
| 261 | "activeSubEffectNull"); |
| 262 | } |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 263 | return func(mSubEffects[mActiveSubIdx].handle); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | ndk::ScopedAStatus EffectProxy::runWithAllSubEffects( |
| 267 | std::function<ndk::ScopedAStatus(std::shared_ptr<IEffect>&)> const& func) { |
| 268 | ndk::ScopedAStatus status = ndk::ScopedAStatus::ok(); |
| 269 | // proceed with others if active sub-effect success |
| 270 | for (auto& sub : mSubEffects) { |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 271 | if (!sub.handle) { |
| 272 | ALOGW("%s null sub-effect interface %s", __func__, sub.descriptor.toString().c_str()); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 273 | continue; |
| 274 | } |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 275 | ndk::ScopedAStatus temp = func(sub.handle); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 276 | if (!temp.isOk()) { |
| 277 | status = ndk::ScopedAStatus::fromStatus(temp.getStatus()); |
| 278 | } |
| 279 | } |
| 280 | return status; |
| 281 | } |
| 282 | |
Shunkai Yao | 79f9874 | 2023-05-03 23:54:43 +0000 | [diff] [blame] | 283 | bool EffectProxy::isBypassing() const { |
| 284 | return mSubEffects[mActiveSubIdx].descriptor.common.flags.bypass; |
| 285 | } |
| 286 | |
| 287 | binder_status_t EffectProxy::dump(int fd, const char** args, uint32_t numArgs) { |
| 288 | const std::string dumpString = toString(); |
| 289 | write(fd, dumpString.c_str(), dumpString.size()); |
| 290 | |
| 291 | return runWithAllSubEffects([&](std::shared_ptr<IEffect>& effect) { |
| 292 | return ndk::ScopedAStatus::fromStatus(effect->dump(fd, args, numArgs)); |
| 293 | }) |
| 294 | .getStatus(); |
| 295 | } |
| 296 | |
| 297 | std::string EffectProxy::toString(size_t level) const { |
| 298 | std::string prefixSpace(level, ' '); |
| 299 | std::string ss = prefixSpace + "EffectProxy:\n"; |
| 300 | prefixSpace += " "; |
| 301 | base::StringAppendF(&ss, "%sDescriptorCommon: %s\n", prefixSpace.c_str(), |
| 302 | mDescriptorCommon.toString().c_str()); |
| 303 | base::StringAppendF(&ss, "%sActiveSubIdx: %zu\n", prefixSpace.c_str(), mActiveSubIdx); |
| 304 | base::StringAppendF(&ss, "%sAllSubEffects:\n", prefixSpace.c_str()); |
| 305 | for (size_t i = 0; i < mSubEffects.size(); i++) { |
| 306 | base::StringAppendF(&ss, "%s[%zu] - Handle: %p, %s\n", prefixSpace.c_str(), i, |
| 307 | mSubEffects[i].handle.get(), |
| 308 | mSubEffects[i].descriptor.toString().c_str()); |
| 309 | } |
| 310 | return ss; |
| 311 | } |
| 312 | |
| 313 | } // namespace android::effect |