blob: 638fa7fbb83901949762d444f6cb286e72fa67bb [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
Shunkai Yaoc12e0822022-12-12 07:13:58 +000017#include <iterator>
18#include <memory>
19#include <tuple>
20#include "include/effect-impl/EffectTypes.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000021#define LOG_TAG "AHAL_EffectFactory"
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000022#include <dlfcn.h>
Shunkai Yao60b34b72022-11-10 17:16:50 +000023#include <unordered_set>
Shunkai Yaoc23916b2022-07-13 04:59:37 +000024
Shunkai Yao39bf2c32022-12-06 03:25:59 +000025#include <android-base/logging.h>
26#include <android/binder_ibinder_platform.h>
27#include <system/thread_defs.h>
28
Shunkai Yao6afc8552022-10-26 22:47:20 +000029#include "effect-impl/EffectTypes.h"
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000030#include "effect-impl/EffectUUID.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000031#include "effectFactory-impl/EffectFactory.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000032
33using aidl::android::media::audio::common::AudioUuid;
34
35namespace aidl::android::hardware::audio::effect {
36
Shunkai Yao60b34b72022-11-10 17:16:50 +000037Factory::Factory(const std::string& file) : mConfig(EffectConfig(file)) {
38 LOG(DEBUG) << __func__ << " with config file: " << file;
39 loadEffectLibs();
Shunkai Yaoc23916b2022-07-13 04:59:37 +000040}
41
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000042Factory::~Factory() {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +000043 if (auto count = mEffectMap.size()) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000044 LOG(ERROR) << __func__ << " remaining " << count
45 << " effect instances not destroyed indicating resource leak!";
Mikhail Naganovdf5feba2022-12-15 00:11:14 +000046 for (const auto& it : mEffectMap) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000047 if (auto spEffect = it.first.lock()) {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +000048 LOG(ERROR) << __func__ << " erase remaining instance UUID "
49 << it.second.first.toString();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000050 destroyEffectImpl(spEffect);
51 }
52 }
53 }
54}
55
56ndk::ScopedAStatus Factory::queryEffects(const std::optional<AudioUuid>& in_type_uuid,
57 const std::optional<AudioUuid>& in_impl_uuid,
Shunkai Yao6afc8552022-10-26 22:47:20 +000058 const std::optional<AudioUuid>& in_proxy_uuid,
Shunkai Yaoc12e0822022-12-12 07:13:58 +000059 std::vector<Descriptor>* _aidl_return) {
60 // get the matching list
61 std::vector<Descriptor::Identity> idList;
62 std::copy_if(mIdentitySet.begin(), mIdentitySet.end(), std::back_inserter(idList),
63 [&](auto& id) {
64 return (!in_type_uuid.has_value() || in_type_uuid.value() == id.type) &&
65 (!in_impl_uuid.has_value() || in_impl_uuid.value() == id.uuid) &&
66 (!in_proxy_uuid.has_value() ||
67 (id.proxy.has_value() && in_proxy_uuid.value() == id.proxy.value()));
68 });
69 // query through the matching list
70 for (const auto& id : idList) {
71 if (mEffectLibMap.count(id.uuid)) {
72 Descriptor desc;
73 auto& entry = mEffectLibMap[id.uuid];
74 getDlSyms(entry);
75 auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry);
76 RETURN_IF(!libInterface || !libInterface->queryEffectFunc, EX_NULL_POINTER,
77 "dlNullQueryEffectFunc");
78 RETURN_IF_BINDER_EXCEPTION(libInterface->queryEffectFunc(&id.uuid, &desc));
79 _aidl_return->emplace_back(std::move(desc));
80 }
81 }
Shunkai Yaoc23916b2022-07-13 04:59:37 +000082 return ndk::ScopedAStatus::ok();
83}
84
Shunkai Yao08b687d2022-10-13 21:11:11 +000085ndk::ScopedAStatus Factory::queryProcessing(const std::optional<Processing::Type>& in_type,
86 std::vector<Processing>* _aidl_return) {
87 // TODO: implement this with audio_effect.xml.
88 if (in_type.has_value()) {
89 // return all matching process filter
90 LOG(DEBUG) << __func__ << " process type: " << in_type.value().toString();
91 }
92 LOG(DEBUG) << __func__ << " return " << _aidl_return->size();
93 return ndk::ScopedAStatus::ok();
94}
95
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000096ndk::ScopedAStatus Factory::createEffect(const AudioUuid& in_impl_uuid,
97 std::shared_ptr<IEffect>* _aidl_return) {
Shunkai Yao45905172022-08-24 18:14:02 +000098 LOG(DEBUG) << __func__ << ": UUID " << in_impl_uuid.toString();
Shunkai Yao6afc8552022-10-26 22:47:20 +000099 if (mEffectLibMap.count(in_impl_uuid)) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000100 auto& entry = mEffectLibMap[in_impl_uuid];
101 getDlSyms(entry);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000102
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000103 auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry);
104 RETURN_IF(!libInterface || !libInterface->createEffectFunc, EX_NULL_POINTER,
105 "dlNullcreateEffectFunc");
Shunkai Yao6afc8552022-10-26 22:47:20 +0000106 std::shared_ptr<IEffect> effectSp;
107 RETURN_IF_BINDER_EXCEPTION(libInterface->createEffectFunc(&in_impl_uuid, &effectSp));
108 if (!effectSp) {
109 LOG(ERROR) << __func__ << ": library created null instance without return error!";
110 return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
111 }
112 *_aidl_return = effectSp;
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000113 ndk::SpAIBinder effectBinder = effectSp->asBinder();
114 AIBinder_setMinSchedulerPolicy(effectBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
115 mEffectMap[std::weak_ptr<IEffect>(effectSp)] =
116 std::make_pair(in_impl_uuid, std::move(effectBinder));
Shunkai Yao6afc8552022-10-26 22:47:20 +0000117 LOG(DEBUG) << __func__ << ": instance " << effectSp.get() << " created successfully";
118 return ndk::ScopedAStatus::ok();
Shunkai Yao45905172022-08-24 18:14:02 +0000119 } else {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000120 LOG(ERROR) << __func__ << ": library doesn't exist";
Shunkai Yao45905172022-08-24 18:14:02 +0000121 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
122 }
123 return ndk::ScopedAStatus::ok();
124}
125
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000126ndk::ScopedAStatus Factory::destroyEffectImpl(const std::shared_ptr<IEffect>& in_handle) {
127 std::weak_ptr<IEffect> wpHandle(in_handle);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000128 // find the effect entry with key (std::weak_ptr<IEffect>)
129 if (auto effectIt = mEffectMap.find(wpHandle); effectIt != mEffectMap.end()) {
130 auto& uuid = effectIt->second.first;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000131 // find implementation library with UUID
132 if (auto libIt = mEffectLibMap.find(uuid); libIt != mEffectLibMap.end()) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000133 auto& interface = std::get<kMapEntryInterfaceIndex>(libIt->second);
134 RETURN_IF(!interface || !interface->destroyEffectFunc, EX_NULL_POINTER,
135 "dlNulldestroyEffectFunc");
136 RETURN_IF_BINDER_EXCEPTION(interface->destroyEffectFunc(in_handle));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000137 } else {
138 LOG(ERROR) << __func__ << ": UUID " << uuid.toString() << " does not exist in libMap!";
139 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
140 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000141 mEffectMap.erase(effectIt);
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() {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000151 for (auto it = mEffectMap.begin(); it != mEffectMap.end();) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000152 if (nullptr == it->first.lock()) {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000153 it = mEffectMap.erase(it);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000154 } 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 Yao52ba4dc2023-02-01 21:57:20 +0000168bool Factory::openEffectLibrary(const AudioUuid& impl, const std::string& path) {
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 =
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000176 std::unique_ptr<void, decltype(dlClose)>{dlopen(path.c_str(), RTLD_LAZY), dlClose};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000177 if (!libHandle) {
178 LOG(ERROR) << __func__ << ": dlopen failed, err: " << dlerror();
Shunkai Yaoe221e712023-01-10 00:58:03 +0000179 return false;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000180 }
181
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000182 LOG(INFO) << __func__ << " dlopen lib:" << path << "\nimpl:" << impl.toString()
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000183 << "\nhandle:" << libHandle;
184 auto interface = new effect_dl_interface_s{nullptr, nullptr, nullptr};
185 mEffectLibMap.insert(
186 {impl,
187 std::make_tuple(std::move(libHandle),
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000188 std::unique_ptr<struct effect_dl_interface_s>(interface), path)});
Shunkai Yaoe221e712023-01-10 00:58:03 +0000189 return true;
Shunkai Yao60b34b72022-11-10 17:16:50 +0000190}
Shunkai Yao6afc8552022-10-26 22:47:20 +0000191
Shunkai Yao60b34b72022-11-10 17:16:50 +0000192void Factory::createIdentityWithConfig(const EffectConfig::LibraryUuid& configLib,
193 const AudioUuid& typeUuid,
194 const std::optional<AudioUuid> proxyUuid) {
195 static const auto& libMap = mConfig.getLibraryMap();
196 const std::string& libName = configLib.name;
197 if (auto path = libMap.find(libName); path != libMap.end()) {
198 Descriptor::Identity id;
199 id.type = typeUuid;
200 id.uuid = configLib.uuid;
201 id.proxy = proxyUuid;
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000202 LOG(DEBUG) << __func__ << " loading lib " << path->second << ": typeUuid "
203 << id.type.toString() << "\nimplUuid " << id.uuid.toString() << " proxyUuid "
Shunkai Yao60b34b72022-11-10 17:16:50 +0000204 << (proxyUuid.has_value() ? proxyUuid->toString() : "null");
Shunkai Yaoe221e712023-01-10 00:58:03 +0000205 if (openEffectLibrary(id.uuid, path->second)) {
206 mIdentitySet.insert(std::move(id));
207 }
Shunkai Yao60b34b72022-11-10 17:16:50 +0000208 } else {
209 LOG(ERROR) << __func__ << ": library " << libName << " not exist!";
210 return;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000211 }
Shunkai Yao60b34b72022-11-10 17:16:50 +0000212}
213
214void Factory::loadEffectLibs() {
215 const auto& configEffectsMap = mConfig.getEffectsMap();
216 for (const auto& configEffects : configEffectsMap) {
217 if (auto typeUuid = kUuidNameTypeMap.find(configEffects.first /* effect name */);
218 typeUuid != kUuidNameTypeMap.end()) {
219 const auto& configLibs = configEffects.second;
220 std::optional<AudioUuid> proxyUuid;
221 if (configLibs.proxyLibrary.has_value()) {
222 const auto& proxyLib = configLibs.proxyLibrary.value();
223 proxyUuid = proxyLib.uuid;
224 }
225 for (const auto& configLib : configLibs.libraries) {
226 createIdentityWithConfig(configLib, typeUuid->second, proxyUuid);
227 }
228 } else {
229 LOG(ERROR) << __func__ << ": can not find type UUID for effect " << configEffects.first
230 << " skipping!";
231 }
232 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000233}
234
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000235void Factory::getDlSyms(DlEntry& entry) {
236 auto& dlHandle = std::get<kMapEntryHandleIndex>(entry);
237 RETURN_VALUE_IF(!dlHandle, void(), "dlNullHandle");
238 // Get the reference of the DL interfaces in library map tuple.
239 auto& dlInterface = std::get<kMapEntryInterfaceIndex>(entry);
240 // return if interface already exist
241 if (!dlInterface->createEffectFunc) {
242 dlInterface->createEffectFunc = (EffectCreateFunctor)dlsym(dlHandle.get(), "createEffect");
243 }
244 if (!dlInterface->queryEffectFunc) {
245 dlInterface->queryEffectFunc = (EffectQueryFunctor)dlsym(dlHandle.get(), "queryEffect");
246 }
247 if (!dlInterface->destroyEffectFunc) {
248 dlInterface->destroyEffectFunc =
249 (EffectDestroyFunctor)dlsym(dlHandle.get(), "destroyEffect");
250 }
251
252 if (!dlInterface->createEffectFunc || !dlInterface->destroyEffectFunc ||
253 !dlInterface->queryEffectFunc) {
254 LOG(ERROR) << __func__ << ": create (" << dlInterface->createEffectFunc << "), query ("
255 << dlInterface->queryEffectFunc << "), or destroy ("
256 << dlInterface->destroyEffectFunc
257 << ") not exist in library: " << std::get<kMapEntryLibNameIndex>(entry)
258 << " handle: " << dlHandle << " with dlerror: " << dlerror();
259 return;
260 }
261}
262
Shunkai Yaoc23916b2022-07-13 04:59:37 +0000263} // namespace aidl::android::hardware::audio::effect