blob: aee42a94638721ea1ff38a9886196f4f345fd9d3 [file] [log] [blame]
Shunkai Yao5c718342023-02-23 23:49:51 +00001/*
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 Yao849ba4c2023-07-11 18:10:18 +000018#include <cstddef>
Shunkai Yao79f98742023-05-03 23:54:43 +000019#include <iterator>
Shunkai Yao5c718342023-02-23 23:49:51 +000020#include <memory>
21#define LOG_TAG "EffectProxy"
Shunkai Yao79f98742023-05-03 23:54:43 +000022// #define LOG_NDEBUG 0
Shunkai Yao5c718342023-02-23 23:49:51 +000023
24#include <fmq/AidlMessageQueue.h>
Shunkai Yao007caf02023-05-17 00:52:54 +000025#include <system/audio_aidl_utils.h>
Shunkai Yao5c718342023-02-23 23:49:51 +000026#include <utils/Log.h>
27
28#include "EffectProxy.h"
29
Shunkai Yao79f98742023-05-03 23:54:43 +000030using ::aidl::android::hardware::audio::effect::Capability;
Shunkai Yao5c718342023-02-23 23:49:51 +000031using ::aidl::android::hardware::audio::effect::CommandId;
32using ::aidl::android::hardware::audio::effect::Descriptor;
33using ::aidl::android::hardware::audio::effect::Flags;
34using ::aidl::android::hardware::audio::effect::IEffect;
35using ::aidl::android::hardware::audio::effect::IFactory;
36using ::aidl::android::hardware::audio::effect::Parameter;
37using ::aidl::android::hardware::audio::effect::State;
38using ::aidl::android::media::audio::common::AudioUuid;
39
Shunkai Yao79f98742023-05-03 23:54:43 +000040namespace android::effect {
Shunkai Yao5c718342023-02-23 23:49:51 +000041
Shunkai Yao79f98742023-05-03 23:54:43 +000042EffectProxy::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 Yao5c718342023-02-23 23:49:51 +000062 mFactory(factory) {}
63
64EffectProxy::~EffectProxy() {
65 close();
66 destroy();
67 mSubEffects.clear();
68}
69
Shunkai Yao5c718342023-02-23 23:49:51 +000070ndk::ScopedAStatus EffectProxy::destroy() {
Shunkai Yao79f98742023-05-03 23:54:43 +000071 ALOGV("%s: %s", __func__,
72 ::android::audio::utils::toString(mDescriptorCommon.id.type).c_str());
Shunkai Yao5c718342023-02-23 23:49:51 +000073 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 Yao5c718342023-02-23 23:49:51 +000082ndk::ScopedAStatus EffectProxy::setOffloadParam(const effect_offload_param_t* offload) {
83 const auto& itor = std::find_if(mSubEffects.begin(), mSubEffects.end(), [&](const auto& sub) {
Shunkai Yao79f98742023-05-03 23:54:43 +000084 const auto& desc = sub.descriptor;
Shunkai Yao5c718342023-02-23 23:49:51 +000085 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 Yao79f98742023-05-03 23:54:43 +000090 mActiveSubIdx = 0;
Shunkai Yao5c718342023-02-23 23:49:51 +000091 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_NULL_POINTER,
92 "noActiveEffctFound");
93 }
94
Shunkai Yao4fe66442023-07-25 21:51:24 +000095 mActiveSubIdx = std::distance(mSubEffects.begin(), itor);
Shunkai Yao849ba4c2023-07-11 18:10:18 +000096 ALOGI("%s: active %soffload sub-effect %zu descriptor: %s", __func__,
Shunkai Yao79f98742023-05-03 23:54:43 +000097 offload->isOffload ? "" : "non-", mActiveSubIdx,
98 ::android::audio::utils::toString(mSubEffects[mActiveSubIdx].descriptor.common.id.uuid)
99 .c_str());
Shunkai Yao849ba4c2023-07-11 18:10:18 +0000100 return runWithAllSubEffects([&](std::shared_ptr<IEffect>& effect) {
101 return effect->setParameter(Parameter::make<Parameter::offload>(offload->isOffload));
102 });
Shunkai Yao5c718342023-02-23 23:49:51 +0000103}
104
105// EffectProxy go over sub-effects and call IEffect interfaces
106ndk::ScopedAStatus EffectProxy::open(const Parameter::Common& common,
107 const std::optional<Parameter::Specific>& specific,
108 IEffect::OpenEffectReturn* ret __unused) {
Shunkai Yao5c718342023-02-23 23:49:51 +0000109 ndk::ScopedAStatus status = ndk::ScopedAStatus::fromExceptionCodeWithMessage(
110 EX_ILLEGAL_ARGUMENT, "nullEffectHandle");
111 for (auto& sub : mSubEffects) {
Shunkai Yao79f98742023-05-03 23:54:43 +0000112 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 Yao5c718342023-02-23 23:49:51 +0000116 break;
117 }
Shunkai Yao79f98742023-05-03 23:54:43 +0000118 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 Yao5c718342023-02-23 23:49:51 +0000121 }
122
123 // close all opened effects if failure
124 if (!status.isOk()) {
Shunkai Yao79f98742023-05-03 23:54:43 +0000125 ALOGE("%s: closing all sub-effects with error %s", __func__,
126 status.getDescription().c_str());
Shunkai Yao5c718342023-02-23 23:49:51 +0000127 close();
128 }
129
130 return status;
131}
132
133ndk::ScopedAStatus EffectProxy::close() {
Shunkai Yao5c718342023-02-23 23:49:51 +0000134 return runWithAllSubEffects([&](std::shared_ptr<IEffect>& effect) {
135 return effect->close();
136 });
137}
138
139ndk::ScopedAStatus EffectProxy::getDescriptor(Descriptor* desc) {
Shunkai Yaodf7bd8b2023-07-13 23:25:38 +0000140 *desc = mSubEffects[mActiveSubIdx].descriptor;
141 desc->common.id.uuid = desc->common.id.proxy.value();
Shunkai Yao79f98742023-05-03 23:54:43 +0000142 return ndk::ScopedAStatus::ok();
143}
144
145ndk::ScopedAStatus EffectProxy::buildDescriptor(const AudioUuid& uuid,
146 const std::vector<Descriptor>& subEffectDescs,
147 Descriptor* desc) {
Shunkai Yao5c718342023-02-23 23:49:51 +0000148 if (!desc) {
Shunkai Yao79f98742023-05-03 23:54:43 +0000149 ALOGE("%s: null descriptor pointer", __func__);
Shunkai Yao5c718342023-02-23 23:49:51 +0000150 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_NULL_POINTER, "nullptr");
151 }
152
Shunkai Yao79f98742023-05-03 23:54:43 +0000153 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 Yao5c718342023-02-23 23:49:51 +0000157 }
158
Shunkai Yao79f98742023-05-03 23:54:43 +0000159 desc->common = buildDescriptorCommon(uuid, subEffectDescs);
Shunkai Yao5c718342023-02-23 23:49:51 +0000160 return ndk::ScopedAStatus::ok();
161}
162
Shunkai Yao79f98742023-05-03 23:54:43 +0000163Descriptor::Common EffectProxy::buildDescriptorCommon(
164 const AudioUuid& uuid, const std::vector<Descriptor>& subEffectDescs) {
Shunkai Yaoff5eb3c2023-11-10 23:50:57 +0000165 // initial flag values before we know which sub-effect to active (with setOffloadParam)
166 // align to HIDL EffectProxy flags
167 Descriptor::Common common = {.flags = {.type = Flags::Type::INSERT,
168 .insert = Flags::Insert::LAST,
169 .volume = Flags::Volume::CTRL}};
Shunkai Yao79f98742023-05-03 23:54:43 +0000170
171 for (const auto& desc : subEffectDescs) {
172 if (desc.common.flags.hwAcceleratorMode == Flags::HardwareAccelerator::TUNNEL) {
173 common.flags.hwAcceleratorMode = Flags::HardwareAccelerator::TUNNEL;
174 }
175
176 // set indication if any sub-effect indication was set
177 common.flags.offloadIndication |= desc.common.flags.offloadIndication;
178 common.flags.deviceIndication |= desc.common.flags.deviceIndication;
179 common.flags.audioModeIndication |= desc.common.flags.audioModeIndication;
180 common.flags.audioSourceIndication |= desc.common.flags.audioSourceIndication;
Shunkai Yaoff5eb3c2023-11-10 23:50:57 +0000181 // Set to NONE if any sub-effect not supporting any Volume command
182 if (desc.common.flags.volume == Flags::Volume::NONE) {
183 common.flags.volume = Flags::Volume::NONE;
184 }
Shunkai Yao79f98742023-05-03 23:54:43 +0000185 }
186
Shunkai Yao79f98742023-05-03 23:54:43 +0000187 // copy type UUID from any of sub-effects, all sub-effects should have same type
188 common.id.type = subEffectDescs[0].common.id.type;
189 // replace implementation UUID with proxy UUID.
190 common.id.uuid = uuid;
191 common.id.proxy = std::nullopt;
192 common.name = "Proxy";
193 common.implementor = "AOSP";
194 return common;
195}
196
Shunkai Yao5c718342023-02-23 23:49:51 +0000197// Handle with active sub-effect first, only send to other sub-effects when success
198ndk::ScopedAStatus EffectProxy::command(CommandId id) {
Shunkai Yao5c718342023-02-23 23:49:51 +0000199 return runWithActiveSubEffectThenOthers(
200 [&](const std::shared_ptr<IEffect>& effect) -> ndk::ScopedAStatus {
201 return effect->command(id);
202 });
203}
204
205// Return the active sub-effect state
206ndk::ScopedAStatus EffectProxy::getState(State* state) {
207 return runWithActiveSubEffect(
208 [&](const std::shared_ptr<IEffect>& effect) -> ndk::ScopedAStatus {
209 return effect->getState(state);
210 });
211}
212
213// Handle with active sub-effect first, only send to other sub-effects when success
214ndk::ScopedAStatus EffectProxy::setParameter(const Parameter& param) {
215 return runWithActiveSubEffectThenOthers(
216 [&](const std::shared_ptr<IEffect>& effect) -> ndk::ScopedAStatus {
217 return effect->setParameter(param);
218 });
219}
220
221// Return the active sub-effect parameter
222ndk::ScopedAStatus EffectProxy::getParameter(const Parameter::Id& id, Parameter* param) {
223 return runWithActiveSubEffect(
224 [&](const std::shared_ptr<IEffect>& effect) -> ndk::ScopedAStatus {
225 return effect->getParameter(id, param);
226 });
227}
228
229ndk::ScopedAStatus EffectProxy::runWithActiveSubEffectThenOthers(
230 std::function<ndk::ScopedAStatus(const std::shared_ptr<IEffect>&)> const& func) {
231 ndk::ScopedAStatus status = runWithActiveSubEffect(func);
232 if (!status.isOk()) {
Shunkai Yao79f98742023-05-03 23:54:43 +0000233 ALOGE("%s active sub-effect return error %s", __func__, status.getDescription().c_str());
Shunkai Yao5c718342023-02-23 23:49:51 +0000234 }
235
Shunkai Yao79f98742023-05-03 23:54:43 +0000236 // proceed with others
Jaideep Sharmad8231042023-08-14 13:03:18 +0530237 for (size_t i = 0; i < mSubEffects.size(); i++) {
238 if (i == mActiveSubIdx) {
239 continue;
240 }
Shunkai Yao79f98742023-05-03 23:54:43 +0000241 if (!mSubEffects[i].handle) {
242 ALOGE("%s null sub-effect interface for %s", __func__,
243 mSubEffects[i].descriptor.common.id.uuid.toString().c_str());
244 continue;
Shunkai Yao5c718342023-02-23 23:49:51 +0000245 }
Shunkai Yao79f98742023-05-03 23:54:43 +0000246 func(mSubEffects[i].handle);
Shunkai Yao5c718342023-02-23 23:49:51 +0000247 }
248 return status;
249}
250
251ndk::ScopedAStatus EffectProxy::runWithActiveSubEffect(
252 std::function<ndk::ScopedAStatus(const std::shared_ptr<IEffect>&)> const& func) {
Shunkai Yao79f98742023-05-03 23:54:43 +0000253 if (!mSubEffects[mActiveSubIdx].handle) {
Shunkai Yao5c718342023-02-23 23:49:51 +0000254 ALOGE("%s null active sub-effect interface, active %s", __func__,
Shunkai Yao79f98742023-05-03 23:54:43 +0000255 mSubEffects[mActiveSubIdx].descriptor.toString().c_str());
Shunkai Yao5c718342023-02-23 23:49:51 +0000256 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_NULL_POINTER,
257 "activeSubEffectNull");
258 }
Shunkai Yao79f98742023-05-03 23:54:43 +0000259 return func(mSubEffects[mActiveSubIdx].handle);
Shunkai Yao5c718342023-02-23 23:49:51 +0000260}
261
262ndk::ScopedAStatus EffectProxy::runWithAllSubEffects(
263 std::function<ndk::ScopedAStatus(std::shared_ptr<IEffect>&)> const& func) {
264 ndk::ScopedAStatus status = ndk::ScopedAStatus::ok();
265 // proceed with others if active sub-effect success
266 for (auto& sub : mSubEffects) {
Shunkai Yao79f98742023-05-03 23:54:43 +0000267 if (!sub.handle) {
268 ALOGW("%s null sub-effect interface %s", __func__, sub.descriptor.toString().c_str());
Shunkai Yao5c718342023-02-23 23:49:51 +0000269 continue;
270 }
Shunkai Yao79f98742023-05-03 23:54:43 +0000271 ndk::ScopedAStatus temp = func(sub.handle);
Shunkai Yao5c718342023-02-23 23:49:51 +0000272 if (!temp.isOk()) {
273 status = ndk::ScopedAStatus::fromStatus(temp.getStatus());
274 }
275 }
276 return status;
277}
278
Shunkai Yao79f98742023-05-03 23:54:43 +0000279bool EffectProxy::isBypassing() const {
280 return mSubEffects[mActiveSubIdx].descriptor.common.flags.bypass;
281}
282
Shunkai Yao13993232023-09-14 22:49:41 +0000283bool EffectProxy::isTunnel() const {
284 return mSubEffects[mActiveSubIdx].descriptor.common.flags.hwAcceleratorMode ==
285 Flags::HardwareAccelerator::TUNNEL;
Shunkai Yao0a047982023-09-13 16:55:26 +0000286}
287
Shunkai Yao79f98742023-05-03 23:54:43 +0000288binder_status_t EffectProxy::dump(int fd, const char** args, uint32_t numArgs) {
289 const std::string dumpString = toString();
290 write(fd, dumpString.c_str(), dumpString.size());
291
292 return runWithAllSubEffects([&](std::shared_ptr<IEffect>& effect) {
293 return ndk::ScopedAStatus::fromStatus(effect->dump(fd, args, numArgs));
294 })
295 .getStatus();
296}
297
298std::string EffectProxy::toString(size_t level) const {
299 std::string prefixSpace(level, ' ');
300 std::string ss = prefixSpace + "EffectProxy:\n";
301 prefixSpace += " ";
302 base::StringAppendF(&ss, "%sDescriptorCommon: %s\n", prefixSpace.c_str(),
303 mDescriptorCommon.toString().c_str());
304 base::StringAppendF(&ss, "%sActiveSubIdx: %zu\n", prefixSpace.c_str(), mActiveSubIdx);
305 base::StringAppendF(&ss, "%sAllSubEffects:\n", prefixSpace.c_str());
306 for (size_t i = 0; i < mSubEffects.size(); i++) {
307 base::StringAppendF(&ss, "%s[%zu] - Handle: %p, %s\n", prefixSpace.c_str(), i,
308 mSubEffects[i].handle.get(),
309 mSubEffects[i].descriptor.toString().c_str());
310 }
311 return ss;
312}
313
314} // namespace android::effect