blob: fac03b7ccf112ba4a7ff8f0d67dc2c61d8dc06df [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 Yao79f98742023-05-03 23:54:43 +000018#include <iterator>
Shunkai Yao5c718342023-02-23 23:49:51 +000019#include <memory>
20#define LOG_TAG "EffectProxy"
Shunkai Yao79f98742023-05-03 23:54:43 +000021// #define LOG_NDEBUG 0
Shunkai Yao5c718342023-02-23 23:49:51 +000022
23#include <fmq/AidlMessageQueue.h>
Shunkai Yao007caf02023-05-17 00:52:54 +000024#include <system/audio_aidl_utils.h>
Shunkai Yao5c718342023-02-23 23:49:51 +000025#include <utils/Log.h>
26
27#include "EffectProxy.h"
28
Shunkai Yao79f98742023-05-03 23:54:43 +000029using ::aidl::android::hardware::audio::effect::Capability;
Shunkai Yao5c718342023-02-23 23:49:51 +000030using ::aidl::android::hardware::audio::effect::CommandId;
31using ::aidl::android::hardware::audio::effect::Descriptor;
32using ::aidl::android::hardware::audio::effect::Flags;
33using ::aidl::android::hardware::audio::effect::IEffect;
34using ::aidl::android::hardware::audio::effect::IFactory;
35using ::aidl::android::hardware::audio::effect::Parameter;
36using ::aidl::android::hardware::audio::effect::State;
37using ::aidl::android::media::audio::common::AudioUuid;
38
Shunkai Yao79f98742023-05-03 23:54:43 +000039namespace android::effect {
Shunkai Yao5c718342023-02-23 23:49:51 +000040
Shunkai Yao79f98742023-05-03 23:54:43 +000041EffectProxy::EffectProxy(const AudioUuid& uuid, const std::vector<Descriptor>& descriptors,
42 const std::shared_ptr<IFactory>& factory)
43 : mDescriptorCommon(buildDescriptorCommon(uuid, descriptors)),
44 mSubEffects(
45 [](const std::vector<Descriptor>& descs, const std::shared_ptr<IFactory>& factory) {
46 std::vector<SubEffect> subEffects;
47 ALOG_ASSERT(factory, "invalid EffectFactory handle");
48 ndk::ScopedAStatus status = ndk::ScopedAStatus::ok();
49 for (const auto& desc : descs) {
50 SubEffect sub({.descriptor = desc});
51 status = factory->createEffect(desc.common.id.uuid, &sub.handle);
52 if (!status.isOk() || !sub.handle) {
53 sub.handle = nullptr;
54 ALOGW("%s create sub-effect %s failed", __func__,
55 ::android::audio::utils::toString(desc.common.id.uuid).c_str());
56 }
57 subEffects.emplace_back(sub);
58 }
59 return subEffects;
60 }(descriptors, factory)),
Shunkai Yao5c718342023-02-23 23:49:51 +000061 mFactory(factory) {}
62
63EffectProxy::~EffectProxy() {
64 close();
65 destroy();
66 mSubEffects.clear();
67}
68
Shunkai Yao5c718342023-02-23 23:49:51 +000069ndk::ScopedAStatus EffectProxy::destroy() {
Shunkai Yao79f98742023-05-03 23:54:43 +000070 ALOGV("%s: %s", __func__,
71 ::android::audio::utils::toString(mDescriptorCommon.id.type).c_str());
Shunkai Yao5c718342023-02-23 23:49:51 +000072 return runWithAllSubEffects([&](std::shared_ptr<IEffect>& effect) {
73 ndk::ScopedAStatus status = mFactory->destroyEffect(effect);
74 if (status.isOk()) {
75 effect.reset();
76 }
77 return status;
78 });
79}
80
Shunkai Yao5c718342023-02-23 23:49:51 +000081ndk::ScopedAStatus EffectProxy::setOffloadParam(const effect_offload_param_t* offload) {
82 const auto& itor = std::find_if(mSubEffects.begin(), mSubEffects.end(), [&](const auto& sub) {
Shunkai Yao79f98742023-05-03 23:54:43 +000083 const auto& desc = sub.descriptor;
Shunkai Yao5c718342023-02-23 23:49:51 +000084 return offload->isOffload ==
85 (desc.common.flags.hwAcceleratorMode == Flags::HardwareAccelerator::TUNNEL);
86 });
87 if (itor == mSubEffects.end()) {
88 ALOGE("%s no %soffload sub-effect found", __func__, offload->isOffload ? "" : "non-");
Shunkai Yao79f98742023-05-03 23:54:43 +000089 mActiveSubIdx = 0;
Shunkai Yao5c718342023-02-23 23:49:51 +000090 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_NULL_POINTER,
91 "noActiveEffctFound");
92 }
93
Shunkai Yao79f98742023-05-03 23:54:43 +000094 mActiveSubIdx = std::distance(mSubEffects.begin(), itor);
95 ALOGV("%s: active %soffload sub-effect %zu descriptor: %s", __func__,
96 offload->isOffload ? "" : "non-", mActiveSubIdx,
97 ::android::audio::utils::toString(mSubEffects[mActiveSubIdx].descriptor.common.id.uuid)
98 .c_str());
Shunkai Yao5c718342023-02-23 23:49:51 +000099 return ndk::ScopedAStatus::ok();
100}
101
102// EffectProxy go over sub-effects and call IEffect interfaces
103ndk::ScopedAStatus EffectProxy::open(const Parameter::Common& common,
104 const std::optional<Parameter::Specific>& specific,
105 IEffect::OpenEffectReturn* ret __unused) {
Shunkai Yao5c718342023-02-23 23:49:51 +0000106 ndk::ScopedAStatus status = ndk::ScopedAStatus::fromExceptionCodeWithMessage(
107 EX_ILLEGAL_ARGUMENT, "nullEffectHandle");
108 for (auto& sub : mSubEffects) {
Shunkai Yao79f98742023-05-03 23:54:43 +0000109 IEffect::OpenEffectReturn openReturn;
110 if (!sub.handle || !(status = sub.handle->open(common, specific, &openReturn)).isOk()) {
111 ALOGE("%s: failed to open %p UUID %s", __func__, sub.handle.get(),
112 ::android::audio::utils::toString(sub.descriptor.common.id.uuid).c_str());
Shunkai Yao5c718342023-02-23 23:49:51 +0000113 break;
114 }
Shunkai Yao79f98742023-05-03 23:54:43 +0000115 sub.effectMq.statusQ = std::make_shared<StatusMQ>(openReturn.statusMQ);
116 sub.effectMq.inputQ = std::make_shared<DataMQ>(openReturn.inputDataMQ);
117 sub.effectMq.outputQ = std::make_shared<DataMQ>(openReturn.outputDataMQ);
Shunkai Yao5c718342023-02-23 23:49:51 +0000118 }
119
120 // close all opened effects if failure
121 if (!status.isOk()) {
Shunkai Yao79f98742023-05-03 23:54:43 +0000122 ALOGE("%s: closing all sub-effects with error %s", __func__,
123 status.getDescription().c_str());
Shunkai Yao5c718342023-02-23 23:49:51 +0000124 close();
125 }
126
127 return status;
128}
129
130ndk::ScopedAStatus EffectProxy::close() {
Shunkai Yao5c718342023-02-23 23:49:51 +0000131 return runWithAllSubEffects([&](std::shared_ptr<IEffect>& effect) {
132 return effect->close();
133 });
134}
135
136ndk::ScopedAStatus EffectProxy::getDescriptor(Descriptor* desc) {
Shunkai Yao79f98742023-05-03 23:54:43 +0000137 desc->common = mDescriptorCommon;
138 desc->capability = mSubEffects[mActiveSubIdx].descriptor.capability;
139 return ndk::ScopedAStatus::ok();
140}
141
142ndk::ScopedAStatus EffectProxy::buildDescriptor(const AudioUuid& uuid,
143 const std::vector<Descriptor>& subEffectDescs,
144 Descriptor* desc) {
Shunkai Yao5c718342023-02-23 23:49:51 +0000145 if (!desc) {
Shunkai Yao79f98742023-05-03 23:54:43 +0000146 ALOGE("%s: null descriptor pointer", __func__);
Shunkai Yao5c718342023-02-23 23:49:51 +0000147 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_NULL_POINTER, "nullptr");
148 }
149
Shunkai Yao79f98742023-05-03 23:54:43 +0000150 if (subEffectDescs.size() < 2) {
151 ALOGE("%s: proxy need at least 2 sub-effects, got %zu", __func__, subEffectDescs.size());
152 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
153 "needMoreSubEffects");
Shunkai Yao5c718342023-02-23 23:49:51 +0000154 }
155
Shunkai Yao79f98742023-05-03 23:54:43 +0000156 desc->common = buildDescriptorCommon(uuid, subEffectDescs);
Shunkai Yao5c718342023-02-23 23:49:51 +0000157 return ndk::ScopedAStatus::ok();
158}
159
Shunkai Yao79f98742023-05-03 23:54:43 +0000160Descriptor::Common EffectProxy::buildDescriptorCommon(
161 const AudioUuid& uuid, const std::vector<Descriptor>& subEffectDescs) {
162 Descriptor::Common common;
163
164 for (const auto& desc : subEffectDescs) {
165 if (desc.common.flags.hwAcceleratorMode == Flags::HardwareAccelerator::TUNNEL) {
166 common.flags.hwAcceleratorMode = Flags::HardwareAccelerator::TUNNEL;
167 }
168
169 // set indication if any sub-effect indication was set
170 common.flags.offloadIndication |= desc.common.flags.offloadIndication;
171 common.flags.deviceIndication |= desc.common.flags.deviceIndication;
172 common.flags.audioModeIndication |= desc.common.flags.audioModeIndication;
173 common.flags.audioSourceIndication |= desc.common.flags.audioSourceIndication;
174 }
175
176 // initial flag values before we know which sub-effect to active (with setOffloadParam)
177 // same as HIDL EffectProxy flags
178 common.flags.type = Flags::Type::INSERT;
179 common.flags.insert = Flags::Insert::LAST;
180 common.flags.volume = Flags::Volume::CTRL;
181
182 // copy type UUID from any of sub-effects, all sub-effects should have same type
183 common.id.type = subEffectDescs[0].common.id.type;
184 // replace implementation UUID with proxy UUID.
185 common.id.uuid = uuid;
186 common.id.proxy = std::nullopt;
187 common.name = "Proxy";
188 common.implementor = "AOSP";
189 return common;
190}
191
Shunkai Yao5c718342023-02-23 23:49:51 +0000192// Handle with active sub-effect first, only send to other sub-effects when success
193ndk::ScopedAStatus EffectProxy::command(CommandId id) {
Shunkai Yao5c718342023-02-23 23:49:51 +0000194 return runWithActiveSubEffectThenOthers(
195 [&](const std::shared_ptr<IEffect>& effect) -> ndk::ScopedAStatus {
196 return effect->command(id);
197 });
198}
199
200// Return the active sub-effect state
201ndk::ScopedAStatus EffectProxy::getState(State* state) {
202 return runWithActiveSubEffect(
203 [&](const std::shared_ptr<IEffect>& effect) -> ndk::ScopedAStatus {
204 return effect->getState(state);
205 });
206}
207
208// Handle with active sub-effect first, only send to other sub-effects when success
209ndk::ScopedAStatus EffectProxy::setParameter(const Parameter& param) {
210 return runWithActiveSubEffectThenOthers(
211 [&](const std::shared_ptr<IEffect>& effect) -> ndk::ScopedAStatus {
212 return effect->setParameter(param);
213 });
214}
215
216// Return the active sub-effect parameter
217ndk::ScopedAStatus EffectProxy::getParameter(const Parameter::Id& id, Parameter* param) {
218 return runWithActiveSubEffect(
219 [&](const std::shared_ptr<IEffect>& effect) -> ndk::ScopedAStatus {
220 return effect->getParameter(id, param);
221 });
222}
223
224ndk::ScopedAStatus EffectProxy::runWithActiveSubEffectThenOthers(
225 std::function<ndk::ScopedAStatus(const std::shared_ptr<IEffect>&)> const& func) {
226 ndk::ScopedAStatus status = runWithActiveSubEffect(func);
227 if (!status.isOk()) {
Shunkai Yao79f98742023-05-03 23:54:43 +0000228 ALOGE("%s active sub-effect return error %s", __func__, status.getDescription().c_str());
Shunkai Yao5c718342023-02-23 23:49:51 +0000229 }
230
Shunkai Yao79f98742023-05-03 23:54:43 +0000231 // proceed with others
232 for (size_t i = 0; i < mSubEffects.size() && i != mActiveSubIdx; i++) {
233 if (!mSubEffects[i].handle) {
234 ALOGE("%s null sub-effect interface for %s", __func__,
235 mSubEffects[i].descriptor.common.id.uuid.toString().c_str());
236 continue;
Shunkai Yao5c718342023-02-23 23:49:51 +0000237 }
Shunkai Yao79f98742023-05-03 23:54:43 +0000238 func(mSubEffects[i].handle);
Shunkai Yao5c718342023-02-23 23:49:51 +0000239 }
240 return status;
241}
242
243ndk::ScopedAStatus EffectProxy::runWithActiveSubEffect(
244 std::function<ndk::ScopedAStatus(const std::shared_ptr<IEffect>&)> const& func) {
Shunkai Yao79f98742023-05-03 23:54:43 +0000245 if (!mSubEffects[mActiveSubIdx].handle) {
Shunkai Yao5c718342023-02-23 23:49:51 +0000246 ALOGE("%s null active sub-effect interface, active %s", __func__,
Shunkai Yao79f98742023-05-03 23:54:43 +0000247 mSubEffects[mActiveSubIdx].descriptor.toString().c_str());
Shunkai Yao5c718342023-02-23 23:49:51 +0000248 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_NULL_POINTER,
249 "activeSubEffectNull");
250 }
Shunkai Yao79f98742023-05-03 23:54:43 +0000251 return func(mSubEffects[mActiveSubIdx].handle);
Shunkai Yao5c718342023-02-23 23:49:51 +0000252}
253
254ndk::ScopedAStatus EffectProxy::runWithAllSubEffects(
255 std::function<ndk::ScopedAStatus(std::shared_ptr<IEffect>&)> const& func) {
256 ndk::ScopedAStatus status = ndk::ScopedAStatus::ok();
257 // proceed with others if active sub-effect success
258 for (auto& sub : mSubEffects) {
Shunkai Yao79f98742023-05-03 23:54:43 +0000259 if (!sub.handle) {
260 ALOGW("%s null sub-effect interface %s", __func__, sub.descriptor.toString().c_str());
Shunkai Yao5c718342023-02-23 23:49:51 +0000261 continue;
262 }
Shunkai Yao79f98742023-05-03 23:54:43 +0000263 ndk::ScopedAStatus temp = func(sub.handle);
Shunkai Yao5c718342023-02-23 23:49:51 +0000264 if (!temp.isOk()) {
265 status = ndk::ScopedAStatus::fromStatus(temp.getStatus());
266 }
267 }
268 return status;
269}
270
Shunkai Yao79f98742023-05-03 23:54:43 +0000271bool EffectProxy::isBypassing() const {
272 return mSubEffects[mActiveSubIdx].descriptor.common.flags.bypass;
273}
274
275binder_status_t EffectProxy::dump(int fd, const char** args, uint32_t numArgs) {
276 const std::string dumpString = toString();
277 write(fd, dumpString.c_str(), dumpString.size());
278
279 return runWithAllSubEffects([&](std::shared_ptr<IEffect>& effect) {
280 return ndk::ScopedAStatus::fromStatus(effect->dump(fd, args, numArgs));
281 })
282 .getStatus();
283}
284
285std::string EffectProxy::toString(size_t level) const {
286 std::string prefixSpace(level, ' ');
287 std::string ss = prefixSpace + "EffectProxy:\n";
288 prefixSpace += " ";
289 base::StringAppendF(&ss, "%sDescriptorCommon: %s\n", prefixSpace.c_str(),
290 mDescriptorCommon.toString().c_str());
291 base::StringAppendF(&ss, "%sActiveSubIdx: %zu\n", prefixSpace.c_str(), mActiveSubIdx);
292 base::StringAppendF(&ss, "%sAllSubEffects:\n", prefixSpace.c_str());
293 for (size_t i = 0; i < mSubEffects.size(); i++) {
294 base::StringAppendF(&ss, "%s[%zu] - Handle: %p, %s\n", prefixSpace.c_str(), i,
295 mSubEffects[i].handle.get(),
296 mSubEffects[i].descriptor.toString().c_str());
297 }
298 return ss;
299}
300
301} // namespace android::effect