blob: 2e1bb12a43af1fc1df6fb25da99b21493d3fd988 [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));
Shunkai Yao4c4f3cd2023-02-28 01:50:40 +000079 // update proxy UUID with information from config xml
80 desc.common.id.proxy = id.proxy;
Shunkai Yaoc12e0822022-12-12 07:13:58 +000081 _aidl_return->emplace_back(std::move(desc));
82 }
83 }
Shunkai Yaoc23916b2022-07-13 04:59:37 +000084 return ndk::ScopedAStatus::ok();
85}
86
Shunkai Yao08b687d2022-10-13 21:11:11 +000087ndk::ScopedAStatus Factory::queryProcessing(const std::optional<Processing::Type>& in_type,
88 std::vector<Processing>* _aidl_return) {
89 // TODO: implement this with audio_effect.xml.
90 if (in_type.has_value()) {
91 // return all matching process filter
92 LOG(DEBUG) << __func__ << " process type: " << in_type.value().toString();
93 }
94 LOG(DEBUG) << __func__ << " return " << _aidl_return->size();
95 return ndk::ScopedAStatus::ok();
96}
97
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000098ndk::ScopedAStatus Factory::createEffect(const AudioUuid& in_impl_uuid,
99 std::shared_ptr<IEffect>* _aidl_return) {
Shunkai Yao45905172022-08-24 18:14:02 +0000100 LOG(DEBUG) << __func__ << ": UUID " << in_impl_uuid.toString();
Shunkai Yao6afc8552022-10-26 22:47:20 +0000101 if (mEffectLibMap.count(in_impl_uuid)) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000102 auto& entry = mEffectLibMap[in_impl_uuid];
103 getDlSyms(entry);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000104
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000105 auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry);
106 RETURN_IF(!libInterface || !libInterface->createEffectFunc, EX_NULL_POINTER,
107 "dlNullcreateEffectFunc");
Shunkai Yao6afc8552022-10-26 22:47:20 +0000108 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;
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000115 ndk::SpAIBinder effectBinder = effectSp->asBinder();
116 AIBinder_setMinSchedulerPolicy(effectBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
117 mEffectMap[std::weak_ptr<IEffect>(effectSp)] =
118 std::make_pair(in_impl_uuid, std::move(effectBinder));
Shunkai Yao6afc8552022-10-26 22:47:20 +0000119 LOG(DEBUG) << __func__ << ": instance " << effectSp.get() << " created successfully";
120 return ndk::ScopedAStatus::ok();
Shunkai Yao45905172022-08-24 18:14:02 +0000121 } else {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000122 LOG(ERROR) << __func__ << ": library doesn't exist";
Shunkai Yao45905172022-08-24 18:14:02 +0000123 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
124 }
125 return ndk::ScopedAStatus::ok();
126}
127
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000128ndk::ScopedAStatus Factory::destroyEffectImpl(const std::shared_ptr<IEffect>& in_handle) {
129 std::weak_ptr<IEffect> wpHandle(in_handle);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000130 // find the effect entry with key (std::weak_ptr<IEffect>)
131 if (auto effectIt = mEffectMap.find(wpHandle); effectIt != mEffectMap.end()) {
132 auto& uuid = effectIt->second.first;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000133 // find implementation library with UUID
134 if (auto libIt = mEffectLibMap.find(uuid); libIt != mEffectLibMap.end()) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000135 auto& interface = std::get<kMapEntryInterfaceIndex>(libIt->second);
136 RETURN_IF(!interface || !interface->destroyEffectFunc, EX_NULL_POINTER,
137 "dlNulldestroyEffectFunc");
138 RETURN_IF_BINDER_EXCEPTION(interface->destroyEffectFunc(in_handle));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000139 } else {
140 LOG(ERROR) << __func__ << ": UUID " << uuid.toString() << " does not exist in libMap!";
141 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
142 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000143 mEffectMap.erase(effectIt);
Shunkai Yao45905172022-08-24 18:14:02 +0000144 return ndk::ScopedAStatus::ok();
145 } else {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000146 LOG(ERROR) << __func__ << ": instance " << in_handle << " does not exist!";
Shunkai Yao45905172022-08-24 18:14:02 +0000147 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
148 }
149}
150
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000151// go over the map and cleanup all expired weak_ptrs.
152void Factory::cleanupEffectMap() {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000153 for (auto it = mEffectMap.begin(); it != mEffectMap.end();) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000154 if (nullptr == it->first.lock()) {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000155 it = mEffectMap.erase(it);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000156 } else {
157 ++it;
158 }
159 }
160}
161
162ndk::ScopedAStatus Factory::destroyEffect(const std::shared_ptr<IEffect>& in_handle) {
163 LOG(DEBUG) << __func__ << ": instance " << in_handle.get();
164 ndk::ScopedAStatus status = destroyEffectImpl(in_handle);
165 // always do the cleanup
166 cleanupEffectMap();
167 return status;
168}
169
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000170bool Factory::openEffectLibrary(const AudioUuid& impl, const std::string& path) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000171 std::function<void(void*)> dlClose = [](void* handle) -> void {
172 if (handle && dlclose(handle)) {
173 LOG(ERROR) << "dlclose failed " << dlerror();
174 }
175 };
176
177 auto libHandle =
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000178 std::unique_ptr<void, decltype(dlClose)>{dlopen(path.c_str(), RTLD_LAZY), dlClose};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000179 if (!libHandle) {
180 LOG(ERROR) << __func__ << ": dlopen failed, err: " << dlerror();
Shunkai Yaoe221e712023-01-10 00:58:03 +0000181 return false;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000182 }
183
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000184 LOG(INFO) << __func__ << " dlopen lib:" << path << "\nimpl:" << impl.toString()
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000185 << "\nhandle:" << libHandle;
186 auto interface = new effect_dl_interface_s{nullptr, nullptr, nullptr};
187 mEffectLibMap.insert(
188 {impl,
189 std::make_tuple(std::move(libHandle),
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000190 std::unique_ptr<struct effect_dl_interface_s>(interface), path)});
Shunkai Yaoe221e712023-01-10 00:58:03 +0000191 return true;
Shunkai Yao60b34b72022-11-10 17:16:50 +0000192}
Shunkai Yao6afc8552022-10-26 22:47:20 +0000193
Shunkai Yao60b34b72022-11-10 17:16:50 +0000194void Factory::createIdentityWithConfig(const EffectConfig::LibraryUuid& configLib,
195 const AudioUuid& typeUuid,
196 const std::optional<AudioUuid> proxyUuid) {
197 static const auto& libMap = mConfig.getLibraryMap();
198 const std::string& libName = configLib.name;
199 if (auto path = libMap.find(libName); path != libMap.end()) {
200 Descriptor::Identity id;
201 id.type = typeUuid;
202 id.uuid = configLib.uuid;
203 id.proxy = proxyUuid;
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000204 LOG(DEBUG) << __func__ << " loading lib " << path->second << ": typeUuid "
205 << id.type.toString() << "\nimplUuid " << id.uuid.toString() << " proxyUuid "
Shunkai Yao60b34b72022-11-10 17:16:50 +0000206 << (proxyUuid.has_value() ? proxyUuid->toString() : "null");
Shunkai Yaoe221e712023-01-10 00:58:03 +0000207 if (openEffectLibrary(id.uuid, path->second)) {
208 mIdentitySet.insert(std::move(id));
209 }
Shunkai Yao60b34b72022-11-10 17:16:50 +0000210 } else {
211 LOG(ERROR) << __func__ << ": library " << libName << " not exist!";
212 return;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000213 }
Shunkai Yao60b34b72022-11-10 17:16:50 +0000214}
215
216void Factory::loadEffectLibs() {
217 const auto& configEffectsMap = mConfig.getEffectsMap();
218 for (const auto& configEffects : configEffectsMap) {
219 if (auto typeUuid = kUuidNameTypeMap.find(configEffects.first /* effect name */);
220 typeUuid != kUuidNameTypeMap.end()) {
221 const auto& configLibs = configEffects.second;
222 std::optional<AudioUuid> proxyUuid;
223 if (configLibs.proxyLibrary.has_value()) {
224 const auto& proxyLib = configLibs.proxyLibrary.value();
225 proxyUuid = proxyLib.uuid;
226 }
227 for (const auto& configLib : configLibs.libraries) {
228 createIdentityWithConfig(configLib, typeUuid->second, proxyUuid);
229 }
230 } else {
231 LOG(ERROR) << __func__ << ": can not find type UUID for effect " << configEffects.first
232 << " skipping!";
233 }
234 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000235}
236
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000237void Factory::getDlSyms(DlEntry& entry) {
238 auto& dlHandle = std::get<kMapEntryHandleIndex>(entry);
239 RETURN_VALUE_IF(!dlHandle, void(), "dlNullHandle");
240 // Get the reference of the DL interfaces in library map tuple.
241 auto& dlInterface = std::get<kMapEntryInterfaceIndex>(entry);
242 // return if interface already exist
243 if (!dlInterface->createEffectFunc) {
244 dlInterface->createEffectFunc = (EffectCreateFunctor)dlsym(dlHandle.get(), "createEffect");
245 }
246 if (!dlInterface->queryEffectFunc) {
247 dlInterface->queryEffectFunc = (EffectQueryFunctor)dlsym(dlHandle.get(), "queryEffect");
248 }
249 if (!dlInterface->destroyEffectFunc) {
250 dlInterface->destroyEffectFunc =
251 (EffectDestroyFunctor)dlsym(dlHandle.get(), "destroyEffect");
252 }
253
254 if (!dlInterface->createEffectFunc || !dlInterface->destroyEffectFunc ||
255 !dlInterface->queryEffectFunc) {
256 LOG(ERROR) << __func__ << ": create (" << dlInterface->createEffectFunc << "), query ("
257 << dlInterface->queryEffectFunc << "), or destroy ("
258 << dlInterface->destroyEffectFunc
259 << ") not exist in library: " << std::get<kMapEntryLibNameIndex>(entry)
260 << " handle: " << dlHandle << " with dlerror: " << dlerror();
261 return;
262 }
263}
264
Shunkai Yaoc23916b2022-07-13 04:59:37 +0000265} // namespace aidl::android::hardware::audio::effect