blob: c1ac3f2d418308a628deec2b10802f953d8001f6 [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 Yaof8be1ac2023-03-06 18:41:27 +000017#include <dlfcn.h>
Shunkai Yaoc12e0822022-12-12 07:13:58 +000018#include <iterator>
19#include <memory>
20#include <tuple>
Shunkai Yao60b34b72022-11-10 17:16:50 +000021#include <unordered_set>
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000022#define LOG_TAG "AHAL_EffectFactory"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000023
Shunkai Yao39bf2c32022-12-06 03:25:59 +000024#include <android-base/logging.h>
25#include <android/binder_ibinder_platform.h>
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000026#include <system/audio_effects/effect_uuid.h>
Shunkai Yao39bf2c32022-12-06 03:25:59 +000027#include <system/thread_defs.h>
28
Shunkai Yao6afc8552022-10-26 22:47:20 +000029#include "effect-impl/EffectTypes.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000030#include "effectFactory-impl/EffectFactory.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000031
32using aidl::android::media::audio::common::AudioUuid;
33
34namespace aidl::android::hardware::audio::effect {
35
Shunkai Yao60b34b72022-11-10 17:16:50 +000036Factory::Factory(const std::string& file) : mConfig(EffectConfig(file)) {
37 LOG(DEBUG) << __func__ << " with config file: " << file;
38 loadEffectLibs();
Shunkai Yaoc23916b2022-07-13 04:59:37 +000039}
40
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000041Factory::~Factory() {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +000042 if (auto count = mEffectMap.size()) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000043 LOG(ERROR) << __func__ << " remaining " << count
44 << " effect instances not destroyed indicating resource leak!";
Mikhail Naganovdf5feba2022-12-15 00:11:14 +000045 for (const auto& it : mEffectMap) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000046 if (auto spEffect = it.first.lock()) {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +000047 LOG(ERROR) << __func__ << " erase remaining instance UUID "
48 << it.second.first.toString();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000049 destroyEffectImpl(spEffect);
50 }
51 }
52 }
53}
54
55ndk::ScopedAStatus Factory::queryEffects(const std::optional<AudioUuid>& in_type_uuid,
56 const std::optional<AudioUuid>& in_impl_uuid,
Shunkai Yao6afc8552022-10-26 22:47:20 +000057 const std::optional<AudioUuid>& in_proxy_uuid,
Shunkai Yaoc12e0822022-12-12 07:13:58 +000058 std::vector<Descriptor>* _aidl_return) {
59 // get the matching list
60 std::vector<Descriptor::Identity> idList;
61 std::copy_if(mIdentitySet.begin(), mIdentitySet.end(), std::back_inserter(idList),
62 [&](auto& id) {
63 return (!in_type_uuid.has_value() || in_type_uuid.value() == id.type) &&
64 (!in_impl_uuid.has_value() || in_impl_uuid.value() == id.uuid) &&
65 (!in_proxy_uuid.has_value() ||
66 (id.proxy.has_value() && in_proxy_uuid.value() == id.proxy.value()));
67 });
68 // query through the matching list
69 for (const auto& id : idList) {
70 if (mEffectLibMap.count(id.uuid)) {
71 Descriptor desc;
72 auto& entry = mEffectLibMap[id.uuid];
73 getDlSyms(entry);
74 auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry);
75 RETURN_IF(!libInterface || !libInterface->queryEffectFunc, EX_NULL_POINTER,
76 "dlNullQueryEffectFunc");
77 RETURN_IF_BINDER_EXCEPTION(libInterface->queryEffectFunc(&id.uuid, &desc));
78 _aidl_return->emplace_back(std::move(desc));
79 }
80 }
Shunkai Yaoc23916b2022-07-13 04:59:37 +000081 return ndk::ScopedAStatus::ok();
82}
83
Shunkai Yao08b687d2022-10-13 21:11:11 +000084ndk::ScopedAStatus Factory::queryProcessing(const std::optional<Processing::Type>& in_type,
85 std::vector<Processing>* _aidl_return) {
86 // TODO: implement this with audio_effect.xml.
87 if (in_type.has_value()) {
88 // return all matching process filter
89 LOG(DEBUG) << __func__ << " process type: " << in_type.value().toString();
90 }
91 LOG(DEBUG) << __func__ << " return " << _aidl_return->size();
92 return ndk::ScopedAStatus::ok();
93}
94
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000095ndk::ScopedAStatus Factory::createEffect(const AudioUuid& in_impl_uuid,
96 std::shared_ptr<IEffect>* _aidl_return) {
Shunkai Yao45905172022-08-24 18:14:02 +000097 LOG(DEBUG) << __func__ << ": UUID " << in_impl_uuid.toString();
Shunkai Yao6afc8552022-10-26 22:47:20 +000098 if (mEffectLibMap.count(in_impl_uuid)) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +000099 auto& entry = mEffectLibMap[in_impl_uuid];
100 getDlSyms(entry);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000101
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000102 auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry);
103 RETURN_IF(!libInterface || !libInterface->createEffectFunc, EX_NULL_POINTER,
104 "dlNullcreateEffectFunc");
Shunkai Yao6afc8552022-10-26 22:47:20 +0000105 std::shared_ptr<IEffect> effectSp;
106 RETURN_IF_BINDER_EXCEPTION(libInterface->createEffectFunc(&in_impl_uuid, &effectSp));
107 if (!effectSp) {
108 LOG(ERROR) << __func__ << ": library created null instance without return error!";
109 return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
110 }
111 *_aidl_return = effectSp;
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000112 ndk::SpAIBinder effectBinder = effectSp->asBinder();
113 AIBinder_setMinSchedulerPolicy(effectBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
114 mEffectMap[std::weak_ptr<IEffect>(effectSp)] =
115 std::make_pair(in_impl_uuid, std::move(effectBinder));
Shunkai Yao6afc8552022-10-26 22:47:20 +0000116 LOG(DEBUG) << __func__ << ": instance " << effectSp.get() << " created successfully";
117 return ndk::ScopedAStatus::ok();
Shunkai Yao45905172022-08-24 18:14:02 +0000118 } else {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000119 LOG(ERROR) << __func__ << ": library doesn't exist";
Shunkai Yao45905172022-08-24 18:14:02 +0000120 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
121 }
122 return ndk::ScopedAStatus::ok();
123}
124
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000125ndk::ScopedAStatus Factory::destroyEffectImpl(const std::shared_ptr<IEffect>& in_handle) {
126 std::weak_ptr<IEffect> wpHandle(in_handle);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000127 // find the effect entry with key (std::weak_ptr<IEffect>)
128 if (auto effectIt = mEffectMap.find(wpHandle); effectIt != mEffectMap.end()) {
129 auto& uuid = effectIt->second.first;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000130 // find implementation library with UUID
131 if (auto libIt = mEffectLibMap.find(uuid); libIt != mEffectLibMap.end()) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000132 auto& interface = std::get<kMapEntryInterfaceIndex>(libIt->second);
133 RETURN_IF(!interface || !interface->destroyEffectFunc, EX_NULL_POINTER,
134 "dlNulldestroyEffectFunc");
135 RETURN_IF_BINDER_EXCEPTION(interface->destroyEffectFunc(in_handle));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000136 } else {
137 LOG(ERROR) << __func__ << ": UUID " << uuid.toString() << " does not exist in libMap!";
138 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
139 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000140 mEffectMap.erase(effectIt);
Shunkai Yao45905172022-08-24 18:14:02 +0000141 return ndk::ScopedAStatus::ok();
142 } else {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000143 LOG(ERROR) << __func__ << ": instance " << in_handle << " does not exist!";
Shunkai Yao45905172022-08-24 18:14:02 +0000144 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
145 }
146}
147
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000148// go over the map and cleanup all expired weak_ptrs.
149void Factory::cleanupEffectMap() {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000150 for (auto it = mEffectMap.begin(); it != mEffectMap.end();) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000151 if (nullptr == it->first.lock()) {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000152 it = mEffectMap.erase(it);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000153 } else {
154 ++it;
155 }
156 }
157}
158
159ndk::ScopedAStatus Factory::destroyEffect(const std::shared_ptr<IEffect>& in_handle) {
160 LOG(DEBUG) << __func__ << ": instance " << in_handle.get();
161 ndk::ScopedAStatus status = destroyEffectImpl(in_handle);
162 // always do the cleanup
163 cleanupEffectMap();
164 return status;
165}
166
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000167bool Factory::openEffectLibrary(const AudioUuid& impl, const std::string& path) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000168 std::function<void(void*)> dlClose = [](void* handle) -> void {
169 if (handle && dlclose(handle)) {
170 LOG(ERROR) << "dlclose failed " << dlerror();
171 }
172 };
173
174 auto libHandle =
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000175 std::unique_ptr<void, decltype(dlClose)>{dlopen(path.c_str(), RTLD_LAZY), dlClose};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000176 if (!libHandle) {
177 LOG(ERROR) << __func__ << ": dlopen failed, err: " << dlerror();
Shunkai Yaoe221e712023-01-10 00:58:03 +0000178 return false;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000179 }
180
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000181 LOG(INFO) << __func__ << " dlopen lib:" << path << "\nimpl:" << impl.toString()
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000182 << "\nhandle:" << libHandle;
183 auto interface = new effect_dl_interface_s{nullptr, nullptr, nullptr};
184 mEffectLibMap.insert(
185 {impl,
186 std::make_tuple(std::move(libHandle),
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000187 std::unique_ptr<struct effect_dl_interface_s>(interface), path)});
Shunkai Yaoe221e712023-01-10 00:58:03 +0000188 return true;
Shunkai Yao60b34b72022-11-10 17:16:50 +0000189}
Shunkai Yao6afc8552022-10-26 22:47:20 +0000190
Shunkai Yao60b34b72022-11-10 17:16:50 +0000191void Factory::createIdentityWithConfig(const EffectConfig::LibraryUuid& configLib,
192 const AudioUuid& typeUuid,
193 const std::optional<AudioUuid> proxyUuid) {
194 static const auto& libMap = mConfig.getLibraryMap();
195 const std::string& libName = configLib.name;
196 if (auto path = libMap.find(libName); path != libMap.end()) {
197 Descriptor::Identity id;
198 id.type = typeUuid;
199 id.uuid = configLib.uuid;
200 id.proxy = proxyUuid;
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000201 LOG(DEBUG) << __func__ << " loading lib " << path->second << ": typeUuid "
202 << id.type.toString() << "\nimplUuid " << id.uuid.toString() << " proxyUuid "
Shunkai Yao60b34b72022-11-10 17:16:50 +0000203 << (proxyUuid.has_value() ? proxyUuid->toString() : "null");
Shunkai Yaoe221e712023-01-10 00:58:03 +0000204 if (openEffectLibrary(id.uuid, path->second)) {
205 mIdentitySet.insert(std::move(id));
206 }
Shunkai Yao60b34b72022-11-10 17:16:50 +0000207 } else {
208 LOG(ERROR) << __func__ << ": library " << libName << " not exist!";
209 return;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000210 }
Shunkai Yao60b34b72022-11-10 17:16:50 +0000211}
212
213void Factory::loadEffectLibs() {
214 const auto& configEffectsMap = mConfig.getEffectsMap();
215 for (const auto& configEffects : configEffectsMap) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000216 if (AudioUuid uuid;
217 EffectConfig::findUuid(configEffects.first /* xml effect name */, &uuid)) {
Shunkai Yao60b34b72022-11-10 17:16:50 +0000218 const auto& configLibs = configEffects.second;
219 std::optional<AudioUuid> proxyUuid;
220 if (configLibs.proxyLibrary.has_value()) {
221 const auto& proxyLib = configLibs.proxyLibrary.value();
222 proxyUuid = proxyLib.uuid;
223 }
224 for (const auto& configLib : configLibs.libraries) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000225 createIdentityWithConfig(configLib, uuid, proxyUuid);
Shunkai Yao60b34b72022-11-10 17:16:50 +0000226 }
227 } else {
228 LOG(ERROR) << __func__ << ": can not find type UUID for effect " << configEffects.first
229 << " skipping!";
230 }
231 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000232}
233
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000234void Factory::getDlSyms(DlEntry& entry) {
235 auto& dlHandle = std::get<kMapEntryHandleIndex>(entry);
236 RETURN_VALUE_IF(!dlHandle, void(), "dlNullHandle");
237 // Get the reference of the DL interfaces in library map tuple.
238 auto& dlInterface = std::get<kMapEntryInterfaceIndex>(entry);
239 // return if interface already exist
240 if (!dlInterface->createEffectFunc) {
241 dlInterface->createEffectFunc = (EffectCreateFunctor)dlsym(dlHandle.get(), "createEffect");
242 }
243 if (!dlInterface->queryEffectFunc) {
244 dlInterface->queryEffectFunc = (EffectQueryFunctor)dlsym(dlHandle.get(), "queryEffect");
245 }
246 if (!dlInterface->destroyEffectFunc) {
247 dlInterface->destroyEffectFunc =
248 (EffectDestroyFunctor)dlsym(dlHandle.get(), "destroyEffect");
249 }
250
251 if (!dlInterface->createEffectFunc || !dlInterface->destroyEffectFunc ||
252 !dlInterface->queryEffectFunc) {
253 LOG(ERROR) << __func__ << ": create (" << dlInterface->createEffectFunc << "), query ("
254 << dlInterface->queryEffectFunc << "), or destroy ("
255 << dlInterface->destroyEffectFunc
256 << ") not exist in library: " << std::get<kMapEntryLibNameIndex>(entry)
257 << " handle: " << dlHandle << " with dlerror: " << dlerror();
258 return;
259 }
260}
261
Shunkai Yaoc23916b2022-07-13 04:59:37 +0000262} // namespace aidl::android::hardware::audio::effect