blob: 820b447dd1a27425a406327cc0a407d6ea045b13 [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
17#define LOG_TAG "AHAL_EffectFactory"
18#include <android-base/logging.h>
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000019#include <dlfcn.h>
Shunkai Yao60b34b72022-11-10 17:16:50 +000020#include <unordered_set>
Shunkai Yaoc23916b2022-07-13 04:59:37 +000021
Shunkai Yao6afc8552022-10-26 22:47:20 +000022#include "effect-impl/EffectTypes.h"
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000023#include "effect-impl/EffectUUID.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000024#include "effectFactory-impl/EffectFactory.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000025
26using aidl::android::media::audio::common::AudioUuid;
27
28namespace aidl::android::hardware::audio::effect {
29
Shunkai Yao60b34b72022-11-10 17:16:50 +000030Factory::Factory(const std::string& file) : mConfig(EffectConfig(file)) {
31 LOG(DEBUG) << __func__ << " with config file: " << file;
32 loadEffectLibs();
Shunkai Yaoc23916b2022-07-13 04:59:37 +000033}
34
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000035Factory::~Factory() {
36 if (auto count = mEffectUuidMap.size()) {
37 LOG(ERROR) << __func__ << " remaining " << count
38 << " effect instances not destroyed indicating resource leak!";
39 for (const auto& it : mEffectUuidMap) {
40 if (auto spEffect = it.first.lock()) {
41 LOG(ERROR) << __func__ << " erase remaining instance UUID " << it.second.toString();
42 destroyEffectImpl(spEffect);
43 }
44 }
45 }
46}
47
48ndk::ScopedAStatus Factory::queryEffects(const std::optional<AudioUuid>& in_type_uuid,
49 const std::optional<AudioUuid>& in_impl_uuid,
Shunkai Yao6afc8552022-10-26 22:47:20 +000050 const std::optional<AudioUuid>& in_proxy_uuid,
Shunkai Yaoc23916b2022-07-13 04:59:37 +000051 std::vector<Descriptor::Identity>* _aidl_return) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000052 std::copy_if(
Shunkai Yao60b34b72022-11-10 17:16:50 +000053 mIdentitySet.begin(), mIdentitySet.end(), std::back_inserter(*_aidl_return),
Shunkai Yao6afc8552022-10-26 22:47:20 +000054 [&](auto& desc) {
55 return (!in_type_uuid.has_value() || in_type_uuid.value() == desc.type) &&
56 (!in_impl_uuid.has_value() || in_impl_uuid.value() == desc.uuid) &&
57 (!in_proxy_uuid.has_value() ||
58 (desc.proxy.has_value() && in_proxy_uuid.value() == desc.proxy.value()));
59 });
Shunkai Yaoc23916b2022-07-13 04:59:37 +000060 return ndk::ScopedAStatus::ok();
61}
62
Shunkai Yao08b687d2022-10-13 21:11:11 +000063ndk::ScopedAStatus Factory::queryProcessing(const std::optional<Processing::Type>& in_type,
64 std::vector<Processing>* _aidl_return) {
65 // TODO: implement this with audio_effect.xml.
66 if (in_type.has_value()) {
67 // return all matching process filter
68 LOG(DEBUG) << __func__ << " process type: " << in_type.value().toString();
69 }
70 LOG(DEBUG) << __func__ << " return " << _aidl_return->size();
71 return ndk::ScopedAStatus::ok();
72}
73
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000074#define RETURN_IF_BINDER_EXCEPTION(functor) \
75 { \
76 binder_exception_t exception = functor; \
77 if (EX_NONE != exception) { \
78 LOG(ERROR) << #functor << ": failed with error " << exception; \
79 return ndk::ScopedAStatus::fromExceptionCode(exception); \
80 } \
81 }
82
83ndk::ScopedAStatus Factory::createEffect(const AudioUuid& in_impl_uuid,
84 std::shared_ptr<IEffect>* _aidl_return) {
Shunkai Yao45905172022-08-24 18:14:02 +000085 LOG(DEBUG) << __func__ << ": UUID " << in_impl_uuid.toString();
Shunkai Yao6afc8552022-10-26 22:47:20 +000086 if (mEffectLibMap.count(in_impl_uuid)) {
87 auto& lib = mEffectLibMap[in_impl_uuid];
88 // didn't do dlsym yet
89 if (nullptr == lib.second) {
90 void* libHandle = lib.first.get();
91 auto dlInterface = std::make_unique<struct effect_dl_interface_s>();
92 dlInterface->createEffectFunc = (EffectCreateFunctor)dlsym(libHandle, "createEffect");
93 dlInterface->destroyEffectFunc =
94 (EffectDestroyFunctor)dlsym(libHandle, "destroyEffect");
95 if (!dlInterface->createEffectFunc || !dlInterface->destroyEffectFunc) {
96 LOG(ERROR) << __func__
97 << ": create or destroy symbol not exist in library: " << libHandle
98 << " with dlerror: " << dlerror();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000099 return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
100 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000101 lib.second = std::move(dlInterface);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000102 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000103
104 auto& libInterface = lib.second;
105 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;
112 mEffectUuidMap[std::weak_ptr<IEffect>(effectSp)] = in_impl_uuid;
113 LOG(DEBUG) << __func__ << ": instance " << effectSp.get() << " created successfully";
114 return ndk::ScopedAStatus::ok();
Shunkai Yao45905172022-08-24 18:14:02 +0000115 } else {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000116 LOG(ERROR) << __func__ << ": library doesn't exist";
Shunkai Yao45905172022-08-24 18:14:02 +0000117 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
118 }
119 return ndk::ScopedAStatus::ok();
120}
121
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000122ndk::ScopedAStatus Factory::destroyEffectImpl(const std::shared_ptr<IEffect>& in_handle) {
123 std::weak_ptr<IEffect> wpHandle(in_handle);
124 // find UUID with key (std::weak_ptr<IEffect>)
125 if (auto uuidIt = mEffectUuidMap.find(wpHandle); uuidIt != mEffectUuidMap.end()) {
126 auto& uuid = uuidIt->second;
127 // find implementation library with UUID
128 if (auto libIt = mEffectLibMap.find(uuid); libIt != mEffectLibMap.end()) {
129 if (libIt->second.second->destroyEffectFunc) {
130 RETURN_IF_BINDER_EXCEPTION(libIt->second.second->destroyEffectFunc(in_handle));
131 }
132 } else {
133 LOG(ERROR) << __func__ << ": UUID " << uuid.toString() << " does not exist in libMap!";
134 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
135 }
136 mEffectUuidMap.erase(uuidIt);
Shunkai Yao45905172022-08-24 18:14:02 +0000137 return ndk::ScopedAStatus::ok();
138 } else {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000139 LOG(ERROR) << __func__ << ": instance " << in_handle << " does not exist!";
Shunkai Yao45905172022-08-24 18:14:02 +0000140 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
141 }
142}
143
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000144// go over the map and cleanup all expired weak_ptrs.
145void Factory::cleanupEffectMap() {
146 for (auto it = mEffectUuidMap.begin(); it != mEffectUuidMap.end();) {
147 if (nullptr == it->first.lock()) {
148 it = mEffectUuidMap.erase(it);
149 } else {
150 ++it;
151 }
152 }
153}
154
155ndk::ScopedAStatus Factory::destroyEffect(const std::shared_ptr<IEffect>& in_handle) {
156 LOG(DEBUG) << __func__ << ": instance " << in_handle.get();
157 ndk::ScopedAStatus status = destroyEffectImpl(in_handle);
158 // always do the cleanup
159 cleanupEffectMap();
160 return status;
161}
162
Shunkai Yao60b34b72022-11-10 17:16:50 +0000163void Factory::openEffectLibrary(const AudioUuid& impl, const std::string& libName) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000164 std::function<void(void*)> dlClose = [](void* handle) -> void {
165 if (handle && dlclose(handle)) {
166 LOG(ERROR) << "dlclose failed " << dlerror();
167 }
168 };
169
170 auto libHandle =
171 std::unique_ptr<void, decltype(dlClose)>{dlopen(libName.c_str(), RTLD_LAZY), dlClose};
172 if (!libHandle) {
173 LOG(ERROR) << __func__ << ": dlopen failed, err: " << dlerror();
174 return;
175 }
176
Shunkai Yao60b34b72022-11-10 17:16:50 +0000177 LOG(DEBUG) << __func__ << " dlopen lib:" << libName << "\nimpl:" << impl.toString()
Shunkai Yao6afc8552022-10-26 22:47:20 +0000178 << "\nhandle:" << libHandle;
179 mEffectLibMap.insert({impl, std::make_pair(std::move(libHandle), nullptr)});
Shunkai Yao60b34b72022-11-10 17:16:50 +0000180}
Shunkai Yao6afc8552022-10-26 22:47:20 +0000181
Shunkai Yao60b34b72022-11-10 17:16:50 +0000182void Factory::createIdentityWithConfig(const EffectConfig::LibraryUuid& configLib,
183 const AudioUuid& typeUuid,
184 const std::optional<AudioUuid> proxyUuid) {
185 static const auto& libMap = mConfig.getLibraryMap();
186 const std::string& libName = configLib.name;
187 if (auto path = libMap.find(libName); path != libMap.end()) {
188 Descriptor::Identity id;
189 id.type = typeUuid;
190 id.uuid = configLib.uuid;
191 id.proxy = proxyUuid;
192 LOG(DEBUG) << __func__ << ": typeUuid " << id.type.toString() << "\nimplUuid "
193 << id.uuid.toString() << " proxyUuid "
194 << (proxyUuid.has_value() ? proxyUuid->toString() : "null");
195 openEffectLibrary(id.uuid, path->second);
196 mIdentitySet.insert(std::move(id));
197 } else {
198 LOG(ERROR) << __func__ << ": library " << libName << " not exist!";
199 return;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000200 }
Shunkai Yao60b34b72022-11-10 17:16:50 +0000201}
202
203void Factory::loadEffectLibs() {
204 const auto& configEffectsMap = mConfig.getEffectsMap();
205 for (const auto& configEffects : configEffectsMap) {
206 if (auto typeUuid = kUuidNameTypeMap.find(configEffects.first /* effect name */);
207 typeUuid != kUuidNameTypeMap.end()) {
208 const auto& configLibs = configEffects.second;
209 std::optional<AudioUuid> proxyUuid;
210 if (configLibs.proxyLibrary.has_value()) {
211 const auto& proxyLib = configLibs.proxyLibrary.value();
212 proxyUuid = proxyLib.uuid;
213 }
214 for (const auto& configLib : configLibs.libraries) {
215 createIdentityWithConfig(configLib, typeUuid->second, proxyUuid);
216 }
217 } else {
218 LOG(ERROR) << __func__ << ": can not find type UUID for effect " << configEffects.first
219 << " skipping!";
220 }
221 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000222}
223
Shunkai Yaoc23916b2022-07-13 04:59:37 +0000224} // namespace aidl::android::hardware::audio::effect