blob: 7073a10c96a26a376fec2320c2c9c65385ce722a [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 Yao5824efb2023-05-08 21:16:13 +000018#include <algorithm>
Shunkai Yaoc12e0822022-12-12 07:13:58 +000019#include <iterator>
20#include <memory>
Shunkai Yao5824efb2023-05-08 21:16:13 +000021#include <optional>
Shunkai Yaoc12e0822022-12-12 07:13:58 +000022#include <tuple>
Shunkai Yao60b34b72022-11-10 17:16:50 +000023#include <unordered_set>
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000024#define LOG_TAG "AHAL_EffectFactory"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000025
Shunkai Yao39bf2c32022-12-06 03:25:59 +000026#include <android-base/logging.h>
27#include <android/binder_ibinder_platform.h>
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000028#include <system/audio_effects/effect_uuid.h>
Shunkai Yao39bf2c32022-12-06 03:25:59 +000029#include <system/thread_defs.h>
30
Shunkai Yao6afc8552022-10-26 22:47:20 +000031#include "effect-impl/EffectTypes.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000032#include "effectFactory-impl/EffectFactory.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000033
34using aidl::android::media::audio::common::AudioUuid;
35
36namespace aidl::android::hardware::audio::effect {
37
Shunkai Yao60b34b72022-11-10 17:16:50 +000038Factory::Factory(const std::string& file) : mConfig(EffectConfig(file)) {
39 LOG(DEBUG) << __func__ << " with config file: " << file;
40 loadEffectLibs();
Shunkai Yaoc23916b2022-07-13 04:59:37 +000041}
42
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000043Factory::~Factory() {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +000044 if (auto count = mEffectMap.size()) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000045 LOG(ERROR) << __func__ << " remaining " << count
46 << " effect instances not destroyed indicating resource leak!";
Mikhail Naganovdf5feba2022-12-15 00:11:14 +000047 for (const auto& it : mEffectMap) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000048 if (auto spEffect = it.first.lock()) {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +000049 LOG(ERROR) << __func__ << " erase remaining instance UUID "
50 << it.second.first.toString();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000051 destroyEffectImpl(spEffect);
52 }
53 }
54 }
55}
56
Shunkai Yao5824efb2023-05-08 21:16:13 +000057ndk::ScopedAStatus Factory::getDescriptorWithUuid(const AudioUuid& uuid, Descriptor* desc) {
58 RETURN_IF(!desc, EX_NULL_POINTER, "nullDescriptor");
59
60 if (mEffectLibMap.count(uuid)) {
61 auto& entry = mEffectLibMap[uuid];
62 getDlSyms(entry);
63 auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry);
64 RETURN_IF(!libInterface || !libInterface->queryEffectFunc, EX_NULL_POINTER,
65 "dlNullQueryEffectFunc");
66 RETURN_IF_BINDER_EXCEPTION(libInterface->queryEffectFunc(&uuid, desc));
67 return ndk::ScopedAStatus::ok();
68 }
69
70 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
71}
72
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000073ndk::ScopedAStatus Factory::queryEffects(const std::optional<AudioUuid>& in_type_uuid,
74 const std::optional<AudioUuid>& in_impl_uuid,
Shunkai Yao6afc8552022-10-26 22:47:20 +000075 const std::optional<AudioUuid>& in_proxy_uuid,
Shunkai Yaoc12e0822022-12-12 07:13:58 +000076 std::vector<Descriptor>* _aidl_return) {
77 // get the matching list
78 std::vector<Descriptor::Identity> idList;
79 std::copy_if(mIdentitySet.begin(), mIdentitySet.end(), std::back_inserter(idList),
80 [&](auto& id) {
81 return (!in_type_uuid.has_value() || in_type_uuid.value() == id.type) &&
82 (!in_impl_uuid.has_value() || in_impl_uuid.value() == id.uuid) &&
83 (!in_proxy_uuid.has_value() ||
84 (id.proxy.has_value() && in_proxy_uuid.value() == id.proxy.value()));
85 });
86 // query through the matching list
87 for (const auto& id : idList) {
88 if (mEffectLibMap.count(id.uuid)) {
89 Descriptor desc;
Shunkai Yao5824efb2023-05-08 21:16:13 +000090 RETURN_IF_ASTATUS_NOT_OK(getDescriptorWithUuid(id.uuid, &desc), "getDescriptorFailed");
Shunkai Yao4c4f3cd2023-02-28 01:50:40 +000091 // update proxy UUID with information from config xml
92 desc.common.id.proxy = id.proxy;
Shunkai Yaoc12e0822022-12-12 07:13:58 +000093 _aidl_return->emplace_back(std::move(desc));
94 }
95 }
Shunkai Yaoc23916b2022-07-13 04:59:37 +000096 return ndk::ScopedAStatus::ok();
97}
98
Shunkai Yao08b687d2022-10-13 21:11:11 +000099ndk::ScopedAStatus Factory::queryProcessing(const std::optional<Processing::Type>& in_type,
100 std::vector<Processing>* _aidl_return) {
Shunkai Yao5824efb2023-05-08 21:16:13 +0000101 const auto& processings = mConfig.getProcessingMap();
102 // Processing stream type
103 for (const auto& procIter : processings) {
104 if (!in_type.has_value() || in_type.value() == procIter.first) {
105 Processing process = {.type = procIter.first /* Processing::Type */};
106 for (const auto& libs : procIter.second /* std::vector<struct EffectLibraries> */) {
107 for (const auto& lib : libs.libraries /* std::vector<struct LibraryUuid> */) {
108 Descriptor desc;
109 if (libs.proxyLibrary.has_value()) {
110 desc.common.id.proxy = libs.proxyLibrary.value().uuid;
111 }
112 RETURN_IF_ASTATUS_NOT_OK(getDescriptorWithUuid(lib.uuid, &desc),
113 "getDescriptorFailed");
114 process.ids.emplace_back(desc);
115 }
116 }
117 _aidl_return->emplace_back(process);
118 }
Shunkai Yao08b687d2022-10-13 21:11:11 +0000119 }
Shunkai Yao5824efb2023-05-08 21:16:13 +0000120
Shunkai Yao08b687d2022-10-13 21:11:11 +0000121 return ndk::ScopedAStatus::ok();
122}
123
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000124ndk::ScopedAStatus Factory::createEffect(const AudioUuid& in_impl_uuid,
125 std::shared_ptr<IEffect>* _aidl_return) {
Shunkai Yao45905172022-08-24 18:14:02 +0000126 LOG(DEBUG) << __func__ << ": UUID " << in_impl_uuid.toString();
Shunkai Yao6afc8552022-10-26 22:47:20 +0000127 if (mEffectLibMap.count(in_impl_uuid)) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000128 auto& entry = mEffectLibMap[in_impl_uuid];
129 getDlSyms(entry);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000130
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000131 auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry);
132 RETURN_IF(!libInterface || !libInterface->createEffectFunc, EX_NULL_POINTER,
133 "dlNullcreateEffectFunc");
Shunkai Yao6afc8552022-10-26 22:47:20 +0000134 std::shared_ptr<IEffect> effectSp;
135 RETURN_IF_BINDER_EXCEPTION(libInterface->createEffectFunc(&in_impl_uuid, &effectSp));
136 if (!effectSp) {
137 LOG(ERROR) << __func__ << ": library created null instance without return error!";
138 return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
139 }
140 *_aidl_return = effectSp;
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000141 ndk::SpAIBinder effectBinder = effectSp->asBinder();
142 AIBinder_setMinSchedulerPolicy(effectBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
143 mEffectMap[std::weak_ptr<IEffect>(effectSp)] =
144 std::make_pair(in_impl_uuid, std::move(effectBinder));
Shunkai Yao6afc8552022-10-26 22:47:20 +0000145 LOG(DEBUG) << __func__ << ": instance " << effectSp.get() << " created successfully";
146 return ndk::ScopedAStatus::ok();
Shunkai Yao45905172022-08-24 18:14:02 +0000147 } else {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000148 LOG(ERROR) << __func__ << ": library doesn't exist";
Shunkai Yao45905172022-08-24 18:14:02 +0000149 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
150 }
151 return ndk::ScopedAStatus::ok();
152}
153
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000154ndk::ScopedAStatus Factory::destroyEffectImpl(const std::shared_ptr<IEffect>& in_handle) {
155 std::weak_ptr<IEffect> wpHandle(in_handle);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000156 // find the effect entry with key (std::weak_ptr<IEffect>)
157 if (auto effectIt = mEffectMap.find(wpHandle); effectIt != mEffectMap.end()) {
158 auto& uuid = effectIt->second.first;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000159 // find implementation library with UUID
160 if (auto libIt = mEffectLibMap.find(uuid); libIt != mEffectLibMap.end()) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000161 auto& interface = std::get<kMapEntryInterfaceIndex>(libIt->second);
162 RETURN_IF(!interface || !interface->destroyEffectFunc, EX_NULL_POINTER,
163 "dlNulldestroyEffectFunc");
164 RETURN_IF_BINDER_EXCEPTION(interface->destroyEffectFunc(in_handle));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000165 } else {
166 LOG(ERROR) << __func__ << ": UUID " << uuid.toString() << " does not exist in libMap!";
167 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
168 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000169 mEffectMap.erase(effectIt);
Shunkai Yao45905172022-08-24 18:14:02 +0000170 return ndk::ScopedAStatus::ok();
171 } else {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000172 LOG(ERROR) << __func__ << ": instance " << in_handle << " does not exist!";
Shunkai Yao45905172022-08-24 18:14:02 +0000173 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
174 }
175}
176
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000177// go over the map and cleanup all expired weak_ptrs.
178void Factory::cleanupEffectMap() {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000179 for (auto it = mEffectMap.begin(); it != mEffectMap.end();) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000180 if (nullptr == it->first.lock()) {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000181 it = mEffectMap.erase(it);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000182 } else {
183 ++it;
184 }
185 }
186}
187
188ndk::ScopedAStatus Factory::destroyEffect(const std::shared_ptr<IEffect>& in_handle) {
189 LOG(DEBUG) << __func__ << ": instance " << in_handle.get();
190 ndk::ScopedAStatus status = destroyEffectImpl(in_handle);
191 // always do the cleanup
192 cleanupEffectMap();
193 return status;
194}
195
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000196bool Factory::openEffectLibrary(const AudioUuid& impl, const std::string& path) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000197 std::function<void(void*)> dlClose = [](void* handle) -> void {
198 if (handle && dlclose(handle)) {
199 LOG(ERROR) << "dlclose failed " << dlerror();
200 }
201 };
202
203 auto libHandle =
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000204 std::unique_ptr<void, decltype(dlClose)>{dlopen(path.c_str(), RTLD_LAZY), dlClose};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000205 if (!libHandle) {
206 LOG(ERROR) << __func__ << ": dlopen failed, err: " << dlerror();
Shunkai Yaoe221e712023-01-10 00:58:03 +0000207 return false;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000208 }
209
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000210 LOG(INFO) << __func__ << " dlopen lib:" << path << "\nimpl:" << impl.toString()
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000211 << "\nhandle:" << libHandle;
212 auto interface = new effect_dl_interface_s{nullptr, nullptr, nullptr};
213 mEffectLibMap.insert(
214 {impl,
215 std::make_tuple(std::move(libHandle),
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000216 std::unique_ptr<struct effect_dl_interface_s>(interface), path)});
Shunkai Yaoe221e712023-01-10 00:58:03 +0000217 return true;
Shunkai Yao60b34b72022-11-10 17:16:50 +0000218}
Shunkai Yao6afc8552022-10-26 22:47:20 +0000219
Shunkai Yao60b34b72022-11-10 17:16:50 +0000220void Factory::createIdentityWithConfig(const EffectConfig::LibraryUuid& configLib,
221 const AudioUuid& typeUuid,
222 const std::optional<AudioUuid> proxyUuid) {
223 static const auto& libMap = mConfig.getLibraryMap();
224 const std::string& libName = configLib.name;
225 if (auto path = libMap.find(libName); path != libMap.end()) {
226 Descriptor::Identity id;
227 id.type = typeUuid;
228 id.uuid = configLib.uuid;
229 id.proxy = proxyUuid;
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000230 LOG(DEBUG) << __func__ << " loading lib " << path->second << ": typeUuid "
231 << id.type.toString() << "\nimplUuid " << id.uuid.toString() << " proxyUuid "
Shunkai Yao60b34b72022-11-10 17:16:50 +0000232 << (proxyUuid.has_value() ? proxyUuid->toString() : "null");
Shunkai Yaoe221e712023-01-10 00:58:03 +0000233 if (openEffectLibrary(id.uuid, path->second)) {
234 mIdentitySet.insert(std::move(id));
235 }
Shunkai Yao60b34b72022-11-10 17:16:50 +0000236 } else {
237 LOG(ERROR) << __func__ << ": library " << libName << " not exist!";
238 return;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000239 }
Shunkai Yao60b34b72022-11-10 17:16:50 +0000240}
241
242void Factory::loadEffectLibs() {
243 const auto& configEffectsMap = mConfig.getEffectsMap();
244 for (const auto& configEffects : configEffectsMap) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000245 if (AudioUuid uuid;
246 EffectConfig::findUuid(configEffects.first /* xml effect name */, &uuid)) {
Shunkai Yao60b34b72022-11-10 17:16:50 +0000247 const auto& configLibs = configEffects.second;
248 std::optional<AudioUuid> proxyUuid;
249 if (configLibs.proxyLibrary.has_value()) {
250 const auto& proxyLib = configLibs.proxyLibrary.value();
251 proxyUuid = proxyLib.uuid;
252 }
253 for (const auto& configLib : configLibs.libraries) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000254 createIdentityWithConfig(configLib, uuid, proxyUuid);
Shunkai Yao60b34b72022-11-10 17:16:50 +0000255 }
256 } else {
257 LOG(ERROR) << __func__ << ": can not find type UUID for effect " << configEffects.first
258 << " skipping!";
259 }
260 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000261}
262
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000263void Factory::getDlSyms(DlEntry& entry) {
264 auto& dlHandle = std::get<kMapEntryHandleIndex>(entry);
265 RETURN_VALUE_IF(!dlHandle, void(), "dlNullHandle");
266 // Get the reference of the DL interfaces in library map tuple.
267 auto& dlInterface = std::get<kMapEntryInterfaceIndex>(entry);
268 // return if interface already exist
269 if (!dlInterface->createEffectFunc) {
270 dlInterface->createEffectFunc = (EffectCreateFunctor)dlsym(dlHandle.get(), "createEffect");
271 }
272 if (!dlInterface->queryEffectFunc) {
273 dlInterface->queryEffectFunc = (EffectQueryFunctor)dlsym(dlHandle.get(), "queryEffect");
274 }
275 if (!dlInterface->destroyEffectFunc) {
276 dlInterface->destroyEffectFunc =
277 (EffectDestroyFunctor)dlsym(dlHandle.get(), "destroyEffect");
278 }
279
280 if (!dlInterface->createEffectFunc || !dlInterface->destroyEffectFunc ||
281 !dlInterface->queryEffectFunc) {
282 LOG(ERROR) << __func__ << ": create (" << dlInterface->createEffectFunc << "), query ("
283 << dlInterface->queryEffectFunc << "), or destroy ("
284 << dlInterface->destroyEffectFunc
285 << ") not exist in library: " << std::get<kMapEntryLibNameIndex>(entry)
286 << " handle: " << dlHandle << " with dlerror: " << dlerror();
287 return;
288 }
289}
290
Shunkai Yaoc23916b2022-07-13 04:59:37 +0000291} // namespace aidl::android::hardware::audio::effect