blob: 96f13ba05861f183b273e0c22a6008bf71e0f732 [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 Yao074cf232023-07-14 21:08:31 +000052 destroyEffectImpl_l(spEffect);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000053 }
54 }
55 }
56}
57
Shunkai Yao074cf232023-07-14 21:08:31 +000058ndk::ScopedAStatus Factory::getDescriptorWithUuid_l(const AudioUuid& uuid, Descriptor* desc) {
Shunkai Yao5824efb2023-05-08 21:16:13 +000059 RETURN_IF(!desc, EX_NULL_POINTER, "nullDescriptor");
60
61 if (mEffectLibMap.count(uuid)) {
62 auto& entry = mEffectLibMap[uuid];
Shunkai Yao074cf232023-07-14 21:08:31 +000063 getDlSyms_l(entry);
Shunkai Yao5824efb2023-05-08 21:16:13 +000064 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) {
Shunkai Yao074cf232023-07-14 21:08:31 +000078 std::lock_guard lg(mMutex);
Shunkai Yaoc12e0822022-12-12 07:13:58 +000079 // get the matching list
80 std::vector<Descriptor::Identity> idList;
81 std::copy_if(mIdentitySet.begin(), mIdentitySet.end(), std::back_inserter(idList),
82 [&](auto& id) {
83 return (!in_type_uuid.has_value() || in_type_uuid.value() == id.type) &&
84 (!in_impl_uuid.has_value() || in_impl_uuid.value() == id.uuid) &&
85 (!in_proxy_uuid.has_value() ||
86 (id.proxy.has_value() && in_proxy_uuid.value() == id.proxy.value()));
87 });
88 // query through the matching list
89 for (const auto& id : idList) {
90 if (mEffectLibMap.count(id.uuid)) {
91 Descriptor desc;
Shunkai Yao074cf232023-07-14 21:08:31 +000092 RETURN_IF_ASTATUS_NOT_OK(getDescriptorWithUuid_l(id.uuid, &desc),
93 "getDescriptorFailed");
Shunkai Yao4c4f3cd2023-02-28 01:50:40 +000094 // update proxy UUID with information from config xml
95 desc.common.id.proxy = id.proxy;
Shunkai Yaoc12e0822022-12-12 07:13:58 +000096 _aidl_return->emplace_back(std::move(desc));
97 }
98 }
Shunkai Yaoc23916b2022-07-13 04:59:37 +000099 return ndk::ScopedAStatus::ok();
100}
101
Shunkai Yao08b687d2022-10-13 21:11:11 +0000102ndk::ScopedAStatus Factory::queryProcessing(const std::optional<Processing::Type>& in_type,
103 std::vector<Processing>* _aidl_return) {
Shunkai Yao074cf232023-07-14 21:08:31 +0000104 std::lock_guard lg(mMutex);
Shunkai Yao5824efb2023-05-08 21:16:13 +0000105 const auto& processings = mConfig.getProcessingMap();
106 // Processing stream type
107 for (const auto& procIter : processings) {
108 if (!in_type.has_value() || in_type.value() == procIter.first) {
109 Processing process = {.type = procIter.first /* Processing::Type */};
110 for (const auto& libs : procIter.second /* std::vector<struct EffectLibraries> */) {
Shunkai Yao80e58502023-07-14 22:10:46 +0000111 for (const auto& lib : libs.libraries /* std::vector<struct Library> */) {
Shunkai Yao5824efb2023-05-08 21:16:13 +0000112 Descriptor desc;
113 if (libs.proxyLibrary.has_value()) {
114 desc.common.id.proxy = libs.proxyLibrary.value().uuid;
115 }
Shunkai Yao074cf232023-07-14 21:08:31 +0000116 RETURN_IF_ASTATUS_NOT_OK(getDescriptorWithUuid_l(lib.uuid, &desc),
Shunkai Yao5824efb2023-05-08 21:16:13 +0000117 "getDescriptorFailed");
118 process.ids.emplace_back(desc);
119 }
120 }
121 _aidl_return->emplace_back(process);
122 }
Shunkai Yao08b687d2022-10-13 21:11:11 +0000123 }
Shunkai Yao5824efb2023-05-08 21:16:13 +0000124
Shunkai Yao08b687d2022-10-13 21:11:11 +0000125 return ndk::ScopedAStatus::ok();
126}
127
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000128ndk::ScopedAStatus Factory::createEffect(const AudioUuid& in_impl_uuid,
129 std::shared_ptr<IEffect>* _aidl_return) {
Shunkai Yaobeef9092023-06-29 18:18:20 +0000130 LOG(DEBUG) << __func__ << ": UUID " << ::android::audio::utils::toString(in_impl_uuid);
Shunkai Yao074cf232023-07-14 21:08:31 +0000131 std::lock_guard lg(mMutex);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000132 if (mEffectLibMap.count(in_impl_uuid)) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000133 auto& entry = mEffectLibMap[in_impl_uuid];
Shunkai Yao074cf232023-07-14 21:08:31 +0000134 getDlSyms_l(entry);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000135
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000136 auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry);
137 RETURN_IF(!libInterface || !libInterface->createEffectFunc, EX_NULL_POINTER,
138 "dlNullcreateEffectFunc");
Shunkai Yao6afc8552022-10-26 22:47:20 +0000139 std::shared_ptr<IEffect> effectSp;
140 RETURN_IF_BINDER_EXCEPTION(libInterface->createEffectFunc(&in_impl_uuid, &effectSp));
141 if (!effectSp) {
142 LOG(ERROR) << __func__ << ": library created null instance without return error!";
143 return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
144 }
145 *_aidl_return = effectSp;
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000146 ndk::SpAIBinder effectBinder = effectSp->asBinder();
147 AIBinder_setMinSchedulerPolicy(effectBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
148 mEffectMap[std::weak_ptr<IEffect>(effectSp)] =
149 std::make_pair(in_impl_uuid, std::move(effectBinder));
Shunkai Yao6afc8552022-10-26 22:47:20 +0000150 LOG(DEBUG) << __func__ << ": instance " << effectSp.get() << " created successfully";
151 return ndk::ScopedAStatus::ok();
Shunkai Yao45905172022-08-24 18:14:02 +0000152 } else {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000153 LOG(ERROR) << __func__ << ": library doesn't exist";
Shunkai Yao45905172022-08-24 18:14:02 +0000154 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
155 }
156 return ndk::ScopedAStatus::ok();
157}
158
Shunkai Yao074cf232023-07-14 21:08:31 +0000159ndk::ScopedAStatus Factory::destroyEffectImpl_l(const std::shared_ptr<IEffect>& in_handle) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000160 std::weak_ptr<IEffect> wpHandle(in_handle);
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000161 // find the effect entry with key (std::weak_ptr<IEffect>)
162 if (auto effectIt = mEffectMap.find(wpHandle); effectIt != mEffectMap.end()) {
163 auto& uuid = effectIt->second.first;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000164 // find implementation library with UUID
165 if (auto libIt = mEffectLibMap.find(uuid); libIt != mEffectLibMap.end()) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000166 auto& interface = std::get<kMapEntryInterfaceIndex>(libIt->second);
167 RETURN_IF(!interface || !interface->destroyEffectFunc, EX_NULL_POINTER,
168 "dlNulldestroyEffectFunc");
169 RETURN_IF_BINDER_EXCEPTION(interface->destroyEffectFunc(in_handle));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000170 } else {
Shunkai Yaobeef9092023-06-29 18:18:20 +0000171 LOG(ERROR) << __func__ << ": UUID " << ::android::audio::utils::toString(uuid)
172 << " does not exist in libMap!";
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000173 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
174 }
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000175 mEffectMap.erase(effectIt);
Shunkai Yao45905172022-08-24 18:14:02 +0000176 return ndk::ScopedAStatus::ok();
177 } else {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000178 LOG(ERROR) << __func__ << ": instance " << in_handle << " does not exist!";
Shunkai Yao45905172022-08-24 18:14:02 +0000179 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
180 }
181}
182
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000183// go over the map and cleanup all expired weak_ptrs.
Shunkai Yao074cf232023-07-14 21:08:31 +0000184void Factory::cleanupEffectMap_l() {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000185 for (auto it = mEffectMap.begin(); it != mEffectMap.end();) {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000186 if (nullptr == it->first.lock()) {
Mikhail Naganovdf5feba2022-12-15 00:11:14 +0000187 it = mEffectMap.erase(it);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000188 } else {
189 ++it;
190 }
191 }
192}
193
194ndk::ScopedAStatus Factory::destroyEffect(const std::shared_ptr<IEffect>& in_handle) {
195 LOG(DEBUG) << __func__ << ": instance " << in_handle.get();
Shunkai Yao074cf232023-07-14 21:08:31 +0000196 std::lock_guard lg(mMutex);
197 ndk::ScopedAStatus status = destroyEffectImpl_l(in_handle);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000198 // always do the cleanup
Shunkai Yao074cf232023-07-14 21:08:31 +0000199 cleanupEffectMap_l();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000200 return status;
201}
202
Shunkai Yao074cf232023-07-14 21:08:31 +0000203bool Factory::openEffectLibrary(const AudioUuid& impl,
204 const std::string& path) NO_THREAD_SAFETY_ANALYSIS {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000205 std::function<void(void*)> dlClose = [](void* handle) -> void {
206 if (handle && dlclose(handle)) {
207 LOG(ERROR) << "dlclose failed " << dlerror();
208 }
209 };
210
211 auto libHandle =
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000212 std::unique_ptr<void, decltype(dlClose)>{dlopen(path.c_str(), RTLD_LAZY), dlClose};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000213 if (!libHandle) {
214 LOG(ERROR) << __func__ << ": dlopen failed, err: " << dlerror();
Shunkai Yaoe221e712023-01-10 00:58:03 +0000215 return false;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000216 }
217
Shunkai Yaobeef9092023-06-29 18:18:20 +0000218 LOG(INFO) << __func__ << " dlopen lib:" << path
219 << "\nimpl:" << ::android::audio::utils::toString(impl) << "\nhandle:" << libHandle;
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000220 auto interface = new effect_dl_interface_s{nullptr, nullptr, nullptr};
221 mEffectLibMap.insert(
222 {impl,
223 std::make_tuple(std::move(libHandle),
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000224 std::unique_ptr<struct effect_dl_interface_s>(interface), path)});
Shunkai Yaoe221e712023-01-10 00:58:03 +0000225 return true;
Shunkai Yao60b34b72022-11-10 17:16:50 +0000226}
Shunkai Yao6afc8552022-10-26 22:47:20 +0000227
Shunkai Yao074cf232023-07-14 21:08:31 +0000228void Factory::createIdentityWithConfig(
229 const EffectConfig::Library& configLib, const AudioUuid& typeUuid,
230 const std::optional<AudioUuid> proxyUuid) NO_THREAD_SAFETY_ANALYSIS {
Shunkai Yao60b34b72022-11-10 17:16:50 +0000231 static const auto& libMap = mConfig.getLibraryMap();
232 const std::string& libName = configLib.name;
233 if (auto path = libMap.find(libName); path != libMap.end()) {
234 Descriptor::Identity id;
235 id.type = typeUuid;
236 id.uuid = configLib.uuid;
237 id.proxy = proxyUuid;
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000238 LOG(DEBUG) << __func__ << " loading lib " << path->second << ": typeUuid "
Shunkai Yaobeef9092023-06-29 18:18:20 +0000239 << ::android::audio::utils::toString(id.type) << "\nimplUuid "
240 << ::android::audio::utils::toString(id.uuid) << " proxyUuid "
241 << (proxyUuid.has_value() ? ::android::audio::utils::toString(proxyUuid.value())
242 : "null");
Shunkai Yaoe221e712023-01-10 00:58:03 +0000243 if (openEffectLibrary(id.uuid, path->second)) {
244 mIdentitySet.insert(std::move(id));
245 }
Shunkai Yao60b34b72022-11-10 17:16:50 +0000246 } else {
247 LOG(ERROR) << __func__ << ": library " << libName << " not exist!";
248 return;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000249 }
Shunkai Yao60b34b72022-11-10 17:16:50 +0000250}
251
252void Factory::loadEffectLibs() {
253 const auto& configEffectsMap = mConfig.getEffectsMap();
254 for (const auto& configEffects : configEffectsMap) {
Shunkai Yao80e58502023-07-14 22:10:46 +0000255 if (AudioUuid type; EffectConfig::findUuid(configEffects /* xml effect */, &type)) {
Shunkai Yao60b34b72022-11-10 17:16:50 +0000256 const auto& configLibs = configEffects.second;
257 std::optional<AudioUuid> proxyUuid;
258 if (configLibs.proxyLibrary.has_value()) {
259 const auto& proxyLib = configLibs.proxyLibrary.value();
260 proxyUuid = proxyLib.uuid;
261 }
262 for (const auto& configLib : configLibs.libraries) {
Shunkai Yao80e58502023-07-14 22:10:46 +0000263 createIdentityWithConfig(configLib, type, proxyUuid);
Shunkai Yao60b34b72022-11-10 17:16:50 +0000264 }
265 } else {
266 LOG(ERROR) << __func__ << ": can not find type UUID for effect " << configEffects.first
267 << " skipping!";
268 }
269 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000270}
271
Shunkai Yao074cf232023-07-14 21:08:31 +0000272void Factory::getDlSyms_l(DlEntry& entry) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000273 auto& dlHandle = std::get<kMapEntryHandleIndex>(entry);
274 RETURN_VALUE_IF(!dlHandle, void(), "dlNullHandle");
275 // Get the reference of the DL interfaces in library map tuple.
276 auto& dlInterface = std::get<kMapEntryInterfaceIndex>(entry);
277 // return if interface already exist
278 if (!dlInterface->createEffectFunc) {
279 dlInterface->createEffectFunc = (EffectCreateFunctor)dlsym(dlHandle.get(), "createEffect");
280 }
281 if (!dlInterface->queryEffectFunc) {
282 dlInterface->queryEffectFunc = (EffectQueryFunctor)dlsym(dlHandle.get(), "queryEffect");
283 }
284 if (!dlInterface->destroyEffectFunc) {
285 dlInterface->destroyEffectFunc =
286 (EffectDestroyFunctor)dlsym(dlHandle.get(), "destroyEffect");
287 }
288
289 if (!dlInterface->createEffectFunc || !dlInterface->destroyEffectFunc ||
290 !dlInterface->queryEffectFunc) {
291 LOG(ERROR) << __func__ << ": create (" << dlInterface->createEffectFunc << "), query ("
292 << dlInterface->queryEffectFunc << "), or destroy ("
293 << dlInterface->destroyEffectFunc
294 << ") not exist in library: " << std::get<kMapEntryLibNameIndex>(entry)
295 << " handle: " << dlHandle << " with dlerror: " << dlerror();
296 return;
297 }
298}
299
Shunkai Yaoc23916b2022-07-13 04:59:37 +0000300} // namespace aidl::android::hardware::audio::effect