blob: 8ed62c9c7aa1e8afd4ae7ea3e65ebd2299e1548e [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 Yaobeef9092023-06-29 18:18:20 +000028#include <system/audio_aidl_utils.h>
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000029#include <system/audio_effects/effect_uuid.h>
Shunkai Yao39bf2c32022-12-06 03:25:59 +000030#include <system/thread_defs.h>
31
Shunkai Yao6afc8552022-10-26 22:47:20 +000032#include "effect-impl/EffectTypes.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000033#include "effectFactory-impl/EffectFactory.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000034
35using aidl::android::media::audio::common::AudioUuid;
36
37namespace aidl::android::hardware::audio::effect {
38
Shunkai Yao60b34b72022-11-10 17:16:50 +000039Factory::Factory(const std::string& file) : mConfig(EffectConfig(file)) {
40 LOG(DEBUG) << __func__ << " with config file: " << file;
41 loadEffectLibs();
Shunkai Yaoc23916b2022-07-13 04:59:37 +000042}
43
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000044Factory::~Factory() {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +000045 if (auto count = mEffectMap.size()) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000046 LOG(ERROR) << __func__ << " remaining " << count
47 << " effect instances not destroyed indicating resource leak!";
Mikhail Naganovdf5feba2022-12-15 00:11:14 +000048 for (const auto& it : mEffectMap) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000049 if (auto spEffect = it.first.lock()) {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +000050 LOG(ERROR) << __func__ << " erase remaining instance UUID "
Shunkai Yaobeef9092023-06-29 18:18:20 +000051 << ::android::audio::utils::toString(it.second.first);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000052 destroyEffectImpl(spEffect);
53 }
54 }
55 }
56}
57
Shunkai Yao5824efb2023-05-08 21:16:13 +000058ndk::ScopedAStatus Factory::getDescriptorWithUuid(const AudioUuid& uuid, Descriptor* desc) {
59 RETURN_IF(!desc, EX_NULL_POINTER, "nullDescriptor");
60
61 if (mEffectLibMap.count(uuid)) {
62 auto& entry = mEffectLibMap[uuid];
63 getDlSyms(entry);
64 auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry);
65 RETURN_IF(!libInterface || !libInterface->queryEffectFunc, EX_NULL_POINTER,
66 "dlNullQueryEffectFunc");
67 RETURN_IF_BINDER_EXCEPTION(libInterface->queryEffectFunc(&uuid, desc));
68 return ndk::ScopedAStatus::ok();
69 }
70
71 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
72}
73
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000074ndk::ScopedAStatus Factory::queryEffects(const std::optional<AudioUuid>& in_type_uuid,
75 const std::optional<AudioUuid>& in_impl_uuid,
Shunkai Yao6afc8552022-10-26 22:47:20 +000076 const std::optional<AudioUuid>& in_proxy_uuid,
Shunkai Yaoc12e0822022-12-12 07:13:58 +000077 std::vector<Descriptor>* _aidl_return) {
78 // get the matching list
79 std::vector<Descriptor::Identity> idList;
80 std::copy_if(mIdentitySet.begin(), mIdentitySet.end(), std::back_inserter(idList),
81 [&](auto& id) {
82 return (!in_type_uuid.has_value() || in_type_uuid.value() == id.type) &&
83 (!in_impl_uuid.has_value() || in_impl_uuid.value() == id.uuid) &&
84 (!in_proxy_uuid.has_value() ||
85 (id.proxy.has_value() && in_proxy_uuid.value() == id.proxy.value()));
86 });
87 // query through the matching list
88 for (const auto& id : idList) {
89 if (mEffectLibMap.count(id.uuid)) {
90 Descriptor desc;
Shunkai Yao5824efb2023-05-08 21:16:13 +000091 RETURN_IF_ASTATUS_NOT_OK(getDescriptorWithUuid(id.uuid, &desc), "getDescriptorFailed");
Shunkai Yao4c4f3cd2023-02-28 01:50:40 +000092 // update proxy UUID with information from config xml
93 desc.common.id.proxy = id.proxy;
Shunkai Yaoc12e0822022-12-12 07:13:58 +000094 _aidl_return->emplace_back(std::move(desc));
95 }
96 }
Shunkai Yaoc23916b2022-07-13 04:59:37 +000097 return ndk::ScopedAStatus::ok();
98}
99
Shunkai Yao08b687d2022-10-13 21:11:11 +0000100ndk::ScopedAStatus Factory::queryProcessing(const std::optional<Processing::Type>& in_type,
101 std::vector<Processing>* _aidl_return) {
Shunkai Yao5824efb2023-05-08 21:16:13 +0000102 const auto& processings = mConfig.getProcessingMap();
103 // Processing stream type
104 for (const auto& procIter : processings) {
105 if (!in_type.has_value() || in_type.value() == procIter.first) {
106 Processing process = {.type = procIter.first /* Processing::Type */};
107 for (const auto& libs : procIter.second /* std::vector<struct EffectLibraries> */) {
108 for (const auto& lib : libs.libraries /* std::vector<struct LibraryUuid> */) {
109 Descriptor desc;
110 if (libs.proxyLibrary.has_value()) {
111 desc.common.id.proxy = libs.proxyLibrary.value().uuid;
112 }
113 RETURN_IF_ASTATUS_NOT_OK(getDescriptorWithUuid(lib.uuid, &desc),
114 "getDescriptorFailed");
115 process.ids.emplace_back(desc);
116 }
117 }
118 _aidl_return->emplace_back(process);
119 }
Shunkai Yao08b687d2022-10-13 21:11:11 +0000120 }
Shunkai Yao5824efb2023-05-08 21:16:13 +0000121
Shunkai Yao08b687d2022-10-13 21:11:11 +0000122 return ndk::ScopedAStatus::ok();
123}
124
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000125ndk::ScopedAStatus Factory::createEffect(const AudioUuid& in_impl_uuid,
126 std::shared_ptr<IEffect>* _aidl_return) {
Shunkai Yaobeef9092023-06-29 18:18:20 +0000127 LOG(DEBUG) << __func__ << ": UUID " << ::android::audio::utils::toString(in_impl_uuid);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000128 if (mEffectLibMap.count(in_impl_uuid)) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000129 auto& entry = mEffectLibMap[in_impl_uuid];
130 getDlSyms(entry);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000131
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000132 auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry);
133 RETURN_IF(!libInterface || !libInterface->createEffectFunc, EX_NULL_POINTER,
134 "dlNullcreateEffectFunc");
Shunkai Yao6afc8552022-10-26 22:47:20 +0000135 std::shared_ptr<IEffect> effectSp;
136 RETURN_IF_BINDER_EXCEPTION(libInterface->createEffectFunc(&in_impl_uuid, &effectSp));
137 if (!effectSp) {
138 LOG(ERROR) << __func__ << ": library created null instance without return error!";
139 return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
140 }
141 *_aidl_return = effectSp;
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000142 ndk::SpAIBinder effectBinder = effectSp->asBinder();
143 AIBinder_setMinSchedulerPolicy(effectBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
144 mEffectMap[std::weak_ptr<IEffect>(effectSp)] =
145 std::make_pair(in_impl_uuid, std::move(effectBinder));
Shunkai Yao6afc8552022-10-26 22:47:20 +0000146 LOG(DEBUG) << __func__ << ": instance " << effectSp.get() << " created successfully";
147 return ndk::ScopedAStatus::ok();
Shunkai Yao45905172022-08-24 18:14:02 +0000148 } else {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000149 LOG(ERROR) << __func__ << ": library doesn't exist";
Shunkai Yao45905172022-08-24 18:14:02 +0000150 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
151 }
152 return ndk::ScopedAStatus::ok();
153}
154
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000155ndk::ScopedAStatus Factory::destroyEffectImpl(const std::shared_ptr<IEffect>& in_handle) {
156 std::weak_ptr<IEffect> wpHandle(in_handle);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000157 // find the effect entry with key (std::weak_ptr<IEffect>)
158 if (auto effectIt = mEffectMap.find(wpHandle); effectIt != mEffectMap.end()) {
159 auto& uuid = effectIt->second.first;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000160 // find implementation library with UUID
161 if (auto libIt = mEffectLibMap.find(uuid); libIt != mEffectLibMap.end()) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000162 auto& interface = std::get<kMapEntryInterfaceIndex>(libIt->second);
163 RETURN_IF(!interface || !interface->destroyEffectFunc, EX_NULL_POINTER,
164 "dlNulldestroyEffectFunc");
165 RETURN_IF_BINDER_EXCEPTION(interface->destroyEffectFunc(in_handle));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000166 } else {
Shunkai Yaobeef9092023-06-29 18:18:20 +0000167 LOG(ERROR) << __func__ << ": UUID " << ::android::audio::utils::toString(uuid)
168 << " does not exist in libMap!";
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000169 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
170 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000171 mEffectMap.erase(effectIt);
Shunkai Yao45905172022-08-24 18:14:02 +0000172 return ndk::ScopedAStatus::ok();
173 } else {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000174 LOG(ERROR) << __func__ << ": instance " << in_handle << " does not exist!";
Shunkai Yao45905172022-08-24 18:14:02 +0000175 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
176 }
177}
178
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000179// go over the map and cleanup all expired weak_ptrs.
180void Factory::cleanupEffectMap() {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000181 for (auto it = mEffectMap.begin(); it != mEffectMap.end();) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000182 if (nullptr == it->first.lock()) {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000183 it = mEffectMap.erase(it);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000184 } else {
185 ++it;
186 }
187 }
188}
189
190ndk::ScopedAStatus Factory::destroyEffect(const std::shared_ptr<IEffect>& in_handle) {
191 LOG(DEBUG) << __func__ << ": instance " << in_handle.get();
192 ndk::ScopedAStatus status = destroyEffectImpl(in_handle);
193 // always do the cleanup
194 cleanupEffectMap();
195 return status;
196}
197
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000198bool Factory::openEffectLibrary(const AudioUuid& impl, const std::string& path) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000199 std::function<void(void*)> dlClose = [](void* handle) -> void {
200 if (handle && dlclose(handle)) {
201 LOG(ERROR) << "dlclose failed " << dlerror();
202 }
203 };
204
205 auto libHandle =
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000206 std::unique_ptr<void, decltype(dlClose)>{dlopen(path.c_str(), RTLD_LAZY), dlClose};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000207 if (!libHandle) {
208 LOG(ERROR) << __func__ << ": dlopen failed, err: " << dlerror();
Shunkai Yaoe221e712023-01-10 00:58:03 +0000209 return false;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000210 }
211
Shunkai Yaobeef9092023-06-29 18:18:20 +0000212 LOG(INFO) << __func__ << " dlopen lib:" << path
213 << "\nimpl:" << ::android::audio::utils::toString(impl) << "\nhandle:" << libHandle;
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000214 auto interface = new effect_dl_interface_s{nullptr, nullptr, nullptr};
215 mEffectLibMap.insert(
216 {impl,
217 std::make_tuple(std::move(libHandle),
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000218 std::unique_ptr<struct effect_dl_interface_s>(interface), path)});
Shunkai Yaoe221e712023-01-10 00:58:03 +0000219 return true;
Shunkai Yao60b34b72022-11-10 17:16:50 +0000220}
Shunkai Yao6afc8552022-10-26 22:47:20 +0000221
Shunkai Yao60b34b72022-11-10 17:16:50 +0000222void Factory::createIdentityWithConfig(const EffectConfig::LibraryUuid& configLib,
223 const AudioUuid& typeUuid,
224 const std::optional<AudioUuid> proxyUuid) {
225 static const auto& libMap = mConfig.getLibraryMap();
226 const std::string& libName = configLib.name;
227 if (auto path = libMap.find(libName); path != libMap.end()) {
228 Descriptor::Identity id;
229 id.type = typeUuid;
230 id.uuid = configLib.uuid;
231 id.proxy = proxyUuid;
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000232 LOG(DEBUG) << __func__ << " loading lib " << path->second << ": typeUuid "
Shunkai Yaobeef9092023-06-29 18:18:20 +0000233 << ::android::audio::utils::toString(id.type) << "\nimplUuid "
234 << ::android::audio::utils::toString(id.uuid) << " proxyUuid "
235 << (proxyUuid.has_value() ? ::android::audio::utils::toString(proxyUuid.value())
236 : "null");
Shunkai Yaoe221e712023-01-10 00:58:03 +0000237 if (openEffectLibrary(id.uuid, path->second)) {
238 mIdentitySet.insert(std::move(id));
239 }
Shunkai Yao60b34b72022-11-10 17:16:50 +0000240 } else {
241 LOG(ERROR) << __func__ << ": library " << libName << " not exist!";
242 return;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000243 }
Shunkai Yao60b34b72022-11-10 17:16:50 +0000244}
245
246void Factory::loadEffectLibs() {
247 const auto& configEffectsMap = mConfig.getEffectsMap();
248 for (const auto& configEffects : configEffectsMap) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000249 if (AudioUuid uuid;
250 EffectConfig::findUuid(configEffects.first /* xml effect name */, &uuid)) {
Shunkai Yao60b34b72022-11-10 17:16:50 +0000251 const auto& configLibs = configEffects.second;
252 std::optional<AudioUuid> proxyUuid;
253 if (configLibs.proxyLibrary.has_value()) {
254 const auto& proxyLib = configLibs.proxyLibrary.value();
255 proxyUuid = proxyLib.uuid;
256 }
257 for (const auto& configLib : configLibs.libraries) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000258 createIdentityWithConfig(configLib, uuid, proxyUuid);
Shunkai Yao60b34b72022-11-10 17:16:50 +0000259 }
260 } else {
261 LOG(ERROR) << __func__ << ": can not find type UUID for effect " << configEffects.first
262 << " skipping!";
263 }
264 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000265}
266
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000267void Factory::getDlSyms(DlEntry& entry) {
268 auto& dlHandle = std::get<kMapEntryHandleIndex>(entry);
269 RETURN_VALUE_IF(!dlHandle, void(), "dlNullHandle");
270 // Get the reference of the DL interfaces in library map tuple.
271 auto& dlInterface = std::get<kMapEntryInterfaceIndex>(entry);
272 // return if interface already exist
273 if (!dlInterface->createEffectFunc) {
274 dlInterface->createEffectFunc = (EffectCreateFunctor)dlsym(dlHandle.get(), "createEffect");
275 }
276 if (!dlInterface->queryEffectFunc) {
277 dlInterface->queryEffectFunc = (EffectQueryFunctor)dlsym(dlHandle.get(), "queryEffect");
278 }
279 if (!dlInterface->destroyEffectFunc) {
280 dlInterface->destroyEffectFunc =
281 (EffectDestroyFunctor)dlsym(dlHandle.get(), "destroyEffect");
282 }
283
284 if (!dlInterface->createEffectFunc || !dlInterface->destroyEffectFunc ||
285 !dlInterface->queryEffectFunc) {
286 LOG(ERROR) << __func__ << ": create (" << dlInterface->createEffectFunc << "), query ("
287 << dlInterface->queryEffectFunc << "), or destroy ("
288 << dlInterface->destroyEffectFunc
289 << ") not exist in library: " << std::get<kMapEntryLibNameIndex>(entry)
290 << " handle: " << dlHandle << " with dlerror: " << dlerror();
291 return;
292 }
293}
294
Shunkai Yaoc23916b2022-07-13 04:59:37 +0000295} // namespace aidl::android::hardware::audio::effect