blob: 74ed7802ee2e589c171ecb721e038eda8ae82fd3 [file] [log] [blame]
Shunkai Yaoc23916b2022-07-13 04:59:37 +00001/*
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
17#define LOG_TAG "AHAL_EffectFactory"
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000018#include <dlfcn.h>
Shunkai Yao60b34b72022-11-10 17:16:50 +000019#include <unordered_set>
Shunkai Yaoc23916b2022-07-13 04:59:37 +000020
Shunkai Yao39bf2c32022-12-06 03:25:59 +000021#include <android-base/logging.h>
22#include <android/binder_ibinder_platform.h>
23#include <system/thread_defs.h>
24
Shunkai Yao6afc8552022-10-26 22:47:20 +000025#include "effect-impl/EffectTypes.h"
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000026#include "effect-impl/EffectUUID.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000027#include "effectFactory-impl/EffectFactory.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000028
29using aidl::android::media::audio::common::AudioUuid;
30
31namespace aidl::android::hardware::audio::effect {
32
Shunkai Yao60b34b72022-11-10 17:16:50 +000033Factory::Factory(const std::string& file) : mConfig(EffectConfig(file)) {
34 LOG(DEBUG) << __func__ << " with config file: " << file;
35 loadEffectLibs();
Shunkai Yaoc23916b2022-07-13 04:59:37 +000036}
37
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000038Factory::~Factory() {
39 if (auto count = mEffectUuidMap.size()) {
40 LOG(ERROR) << __func__ << " remaining " << count
41 << " effect instances not destroyed indicating resource leak!";
42 for (const auto& it : mEffectUuidMap) {
43 if (auto spEffect = it.first.lock()) {
44 LOG(ERROR) << __func__ << " erase remaining instance UUID " << it.second.toString();
45 destroyEffectImpl(spEffect);
46 }
47 }
48 }
49}
50
51ndk::ScopedAStatus Factory::queryEffects(const std::optional<AudioUuid>& in_type_uuid,
52 const std::optional<AudioUuid>& in_impl_uuid,
Shunkai Yao6afc8552022-10-26 22:47:20 +000053 const std::optional<AudioUuid>& in_proxy_uuid,
Shunkai Yaoc23916b2022-07-13 04:59:37 +000054 std::vector<Descriptor::Identity>* _aidl_return) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000055 std::copy_if(
Shunkai Yao60b34b72022-11-10 17:16:50 +000056 mIdentitySet.begin(), mIdentitySet.end(), std::back_inserter(*_aidl_return),
Shunkai Yao6afc8552022-10-26 22:47:20 +000057 [&](auto& desc) {
58 return (!in_type_uuid.has_value() || in_type_uuid.value() == desc.type) &&
59 (!in_impl_uuid.has_value() || in_impl_uuid.value() == desc.uuid) &&
60 (!in_proxy_uuid.has_value() ||
61 (desc.proxy.has_value() && in_proxy_uuid.value() == desc.proxy.value()));
62 });
Shunkai Yaoc23916b2022-07-13 04:59:37 +000063 return ndk::ScopedAStatus::ok();
64}
65
Shunkai Yao08b687d2022-10-13 21:11:11 +000066ndk::ScopedAStatus Factory::queryProcessing(const std::optional<Processing::Type>& in_type,
67 std::vector<Processing>* _aidl_return) {
68 // TODO: implement this with audio_effect.xml.
69 if (in_type.has_value()) {
70 // return all matching process filter
71 LOG(DEBUG) << __func__ << " process type: " << in_type.value().toString();
72 }
73 LOG(DEBUG) << __func__ << " return " << _aidl_return->size();
74 return ndk::ScopedAStatus::ok();
75}
76
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000077#define RETURN_IF_BINDER_EXCEPTION(functor) \
78 { \
79 binder_exception_t exception = functor; \
80 if (EX_NONE != exception) { \
81 LOG(ERROR) << #functor << ": failed with error " << exception; \
82 return ndk::ScopedAStatus::fromExceptionCode(exception); \
83 } \
84 }
85
86ndk::ScopedAStatus Factory::createEffect(const AudioUuid& in_impl_uuid,
87 std::shared_ptr<IEffect>* _aidl_return) {
Shunkai Yao45905172022-08-24 18:14:02 +000088 LOG(DEBUG) << __func__ << ": UUID " << in_impl_uuid.toString();
Shunkai Yao6afc8552022-10-26 22:47:20 +000089 if (mEffectLibMap.count(in_impl_uuid)) {
90 auto& lib = mEffectLibMap[in_impl_uuid];
91 // didn't do dlsym yet
92 if (nullptr == lib.second) {
93 void* libHandle = lib.first.get();
94 auto dlInterface = std::make_unique<struct effect_dl_interface_s>();
95 dlInterface->createEffectFunc = (EffectCreateFunctor)dlsym(libHandle, "createEffect");
96 dlInterface->destroyEffectFunc =
97 (EffectDestroyFunctor)dlsym(libHandle, "destroyEffect");
98 if (!dlInterface->createEffectFunc || !dlInterface->destroyEffectFunc) {
99 LOG(ERROR) << __func__
100 << ": create or destroy symbol not exist in library: " << libHandle
101 << " with dlerror: " << dlerror();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000102 return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
103 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000104 lib.second = std::move(dlInterface);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000105 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000106
107 auto& libInterface = lib.second;
108 std::shared_ptr<IEffect> effectSp;
109 RETURN_IF_BINDER_EXCEPTION(libInterface->createEffectFunc(&in_impl_uuid, &effectSp));
110 if (!effectSp) {
111 LOG(ERROR) << __func__ << ": library created null instance without return error!";
112 return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
113 }
114 *_aidl_return = effectSp;
Shunkai Yao39bf2c32022-12-06 03:25:59 +0000115 AIBinder_setMinSchedulerPolicy(effectSp->asBinder().get(), SCHED_NORMAL,
116 ANDROID_PRIORITY_AUDIO);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000117 mEffectUuidMap[std::weak_ptr<IEffect>(effectSp)] = in_impl_uuid;
118 LOG(DEBUG) << __func__ << ": instance " << effectSp.get() << " created successfully";
119 return ndk::ScopedAStatus::ok();
Shunkai Yao45905172022-08-24 18:14:02 +0000120 } else {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000121 LOG(ERROR) << __func__ << ": library doesn't exist";
Shunkai Yao45905172022-08-24 18:14:02 +0000122 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
123 }
124 return ndk::ScopedAStatus::ok();
125}
126
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000127ndk::ScopedAStatus Factory::destroyEffectImpl(const std::shared_ptr<IEffect>& in_handle) {
128 std::weak_ptr<IEffect> wpHandle(in_handle);
129 // find UUID with key (std::weak_ptr<IEffect>)
130 if (auto uuidIt = mEffectUuidMap.find(wpHandle); uuidIt != mEffectUuidMap.end()) {
131 auto& uuid = uuidIt->second;
132 // find implementation library with UUID
133 if (auto libIt = mEffectLibMap.find(uuid); libIt != mEffectLibMap.end()) {
134 if (libIt->second.second->destroyEffectFunc) {
135 RETURN_IF_BINDER_EXCEPTION(libIt->second.second->destroyEffectFunc(in_handle));
136 }
137 } else {
138 LOG(ERROR) << __func__ << ": UUID " << uuid.toString() << " does not exist in libMap!";
139 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
140 }
141 mEffectUuidMap.erase(uuidIt);
Shunkai Yao45905172022-08-24 18:14:02 +0000142 return ndk::ScopedAStatus::ok();
143 } else {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000144 LOG(ERROR) << __func__ << ": instance " << in_handle << " does not exist!";
Shunkai Yao45905172022-08-24 18:14:02 +0000145 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
146 }
147}
148
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000149// go over the map and cleanup all expired weak_ptrs.
150void Factory::cleanupEffectMap() {
151 for (auto it = mEffectUuidMap.begin(); it != mEffectUuidMap.end();) {
152 if (nullptr == it->first.lock()) {
153 it = mEffectUuidMap.erase(it);
154 } else {
155 ++it;
156 }
157 }
158}
159
160ndk::ScopedAStatus Factory::destroyEffect(const std::shared_ptr<IEffect>& in_handle) {
161 LOG(DEBUG) << __func__ << ": instance " << in_handle.get();
162 ndk::ScopedAStatus status = destroyEffectImpl(in_handle);
163 // always do the cleanup
164 cleanupEffectMap();
165 return status;
166}
167
Shunkai Yao60b34b72022-11-10 17:16:50 +0000168void Factory::openEffectLibrary(const AudioUuid& impl, const std::string& libName) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000169 std::function<void(void*)> dlClose = [](void* handle) -> void {
170 if (handle && dlclose(handle)) {
171 LOG(ERROR) << "dlclose failed " << dlerror();
172 }
173 };
174
175 auto libHandle =
176 std::unique_ptr<void, decltype(dlClose)>{dlopen(libName.c_str(), RTLD_LAZY), dlClose};
177 if (!libHandle) {
178 LOG(ERROR) << __func__ << ": dlopen failed, err: " << dlerror();
179 return;
180 }
181
Shunkai Yao60b34b72022-11-10 17:16:50 +0000182 LOG(DEBUG) << __func__ << " dlopen lib:" << libName << "\nimpl:" << impl.toString()
Shunkai Yao6afc8552022-10-26 22:47:20 +0000183 << "\nhandle:" << libHandle;
184 mEffectLibMap.insert({impl, std::make_pair(std::move(libHandle), nullptr)});
Shunkai Yao60b34b72022-11-10 17:16:50 +0000185}
Shunkai Yao6afc8552022-10-26 22:47:20 +0000186
Shunkai Yao60b34b72022-11-10 17:16:50 +0000187void Factory::createIdentityWithConfig(const EffectConfig::LibraryUuid& configLib,
188 const AudioUuid& typeUuid,
189 const std::optional<AudioUuid> proxyUuid) {
190 static const auto& libMap = mConfig.getLibraryMap();
191 const std::string& libName = configLib.name;
192 if (auto path = libMap.find(libName); path != libMap.end()) {
193 Descriptor::Identity id;
194 id.type = typeUuid;
195 id.uuid = configLib.uuid;
196 id.proxy = proxyUuid;
197 LOG(DEBUG) << __func__ << ": typeUuid " << id.type.toString() << "\nimplUuid "
198 << id.uuid.toString() << " proxyUuid "
199 << (proxyUuid.has_value() ? proxyUuid->toString() : "null");
200 openEffectLibrary(id.uuid, path->second);
201 mIdentitySet.insert(std::move(id));
202 } else {
203 LOG(ERROR) << __func__ << ": library " << libName << " not exist!";
204 return;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000205 }
Shunkai Yao60b34b72022-11-10 17:16:50 +0000206}
207
208void Factory::loadEffectLibs() {
209 const auto& configEffectsMap = mConfig.getEffectsMap();
210 for (const auto& configEffects : configEffectsMap) {
211 if (auto typeUuid = kUuidNameTypeMap.find(configEffects.first /* effect name */);
212 typeUuid != kUuidNameTypeMap.end()) {
213 const auto& configLibs = configEffects.second;
214 std::optional<AudioUuid> proxyUuid;
215 if (configLibs.proxyLibrary.has_value()) {
216 const auto& proxyLib = configLibs.proxyLibrary.value();
217 proxyUuid = proxyLib.uuid;
218 }
219 for (const auto& configLib : configLibs.libraries) {
220 createIdentityWithConfig(configLib, typeUuid->second, proxyUuid);
221 }
222 } else {
223 LOG(ERROR) << __func__ << ": can not find type UUID for effect " << configEffects.first
224 << " skipping!";
225 }
226 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000227}
228
Shunkai Yaoc23916b2022-07-13 04:59:37 +0000229} // namespace aidl::android::hardware::audio::effect