Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 1 | /* |
| 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 Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 17 | #include <dlfcn.h> |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 18 | #include <iterator> |
| 19 | #include <memory> |
| 20 | #include <tuple> |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 21 | #include <unordered_set> |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 22 | #define LOG_TAG "AHAL_EffectFactory" |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 23 | |
Shunkai Yao | 39bf2c3 | 2022-12-06 03:25:59 +0000 | [diff] [blame] | 24 | #include <android-base/logging.h> |
| 25 | #include <android/binder_ibinder_platform.h> |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 26 | #include <system/audio_effects/effect_uuid.h> |
Shunkai Yao | 39bf2c3 | 2022-12-06 03:25:59 +0000 | [diff] [blame] | 27 | #include <system/thread_defs.h> |
| 28 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 29 | #include "effect-impl/EffectTypes.h" |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 30 | #include "effectFactory-impl/EffectFactory.h" |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 31 | |
| 32 | using aidl::android::media::audio::common::AudioUuid; |
| 33 | |
| 34 | namespace aidl::android::hardware::audio::effect { |
| 35 | |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 36 | Factory::Factory(const std::string& file) : mConfig(EffectConfig(file)) { |
| 37 | LOG(DEBUG) << __func__ << " with config file: " << file; |
| 38 | loadEffectLibs(); |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 41 | Factory::~Factory() { |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 42 | if (auto count = mEffectMap.size()) { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 43 | LOG(ERROR) << __func__ << " remaining " << count |
| 44 | << " effect instances not destroyed indicating resource leak!"; |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 45 | for (const auto& it : mEffectMap) { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 46 | if (auto spEffect = it.first.lock()) { |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 47 | LOG(ERROR) << __func__ << " erase remaining instance UUID " |
| 48 | << it.second.first.toString(); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 49 | destroyEffectImpl(spEffect); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | ndk::ScopedAStatus Factory::queryEffects(const std::optional<AudioUuid>& in_type_uuid, |
| 56 | const std::optional<AudioUuid>& in_impl_uuid, |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 57 | const std::optional<AudioUuid>& in_proxy_uuid, |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 58 | 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 Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 81 | return ndk::ScopedAStatus::ok(); |
| 82 | } |
| 83 | |
Shunkai Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 84 | ndk::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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 95 | ndk::ScopedAStatus Factory::createEffect(const AudioUuid& in_impl_uuid, |
| 96 | std::shared_ptr<IEffect>* _aidl_return) { |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 97 | LOG(DEBUG) << __func__ << ": UUID " << in_impl_uuid.toString(); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 98 | if (mEffectLibMap.count(in_impl_uuid)) { |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 99 | auto& entry = mEffectLibMap[in_impl_uuid]; |
| 100 | getDlSyms(entry); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 101 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 102 | auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry); |
| 103 | RETURN_IF(!libInterface || !libInterface->createEffectFunc, EX_NULL_POINTER, |
| 104 | "dlNullcreateEffectFunc"); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 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; |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 112 | 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 Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 116 | LOG(DEBUG) << __func__ << ": instance " << effectSp.get() << " created successfully"; |
| 117 | return ndk::ScopedAStatus::ok(); |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 118 | } else { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 119 | LOG(ERROR) << __func__ << ": library doesn't exist"; |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 120 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 121 | } |
| 122 | return ndk::ScopedAStatus::ok(); |
| 123 | } |
| 124 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 125 | ndk::ScopedAStatus Factory::destroyEffectImpl(const std::shared_ptr<IEffect>& in_handle) { |
| 126 | std::weak_ptr<IEffect> wpHandle(in_handle); |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 127 | // 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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 130 | // find implementation library with UUID |
| 131 | if (auto libIt = mEffectLibMap.find(uuid); libIt != mEffectLibMap.end()) { |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 132 | 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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 136 | } else { |
| 137 | LOG(ERROR) << __func__ << ": UUID " << uuid.toString() << " does not exist in libMap!"; |
| 138 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 139 | } |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 140 | mEffectMap.erase(effectIt); |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 141 | return ndk::ScopedAStatus::ok(); |
| 142 | } else { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 143 | LOG(ERROR) << __func__ << ": instance " << in_handle << " does not exist!"; |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 144 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 145 | } |
| 146 | } |
| 147 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 148 | // go over the map and cleanup all expired weak_ptrs. |
| 149 | void Factory::cleanupEffectMap() { |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 150 | for (auto it = mEffectMap.begin(); it != mEffectMap.end();) { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 151 | if (nullptr == it->first.lock()) { |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 152 | it = mEffectMap.erase(it); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 153 | } else { |
| 154 | ++it; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | ndk::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 Yao | 52ba4dc | 2023-02-01 21:57:20 +0000 | [diff] [blame] | 167 | bool Factory::openEffectLibrary(const AudioUuid& impl, const std::string& path) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 168 | 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 Yao | 52ba4dc | 2023-02-01 21:57:20 +0000 | [diff] [blame] | 175 | std::unique_ptr<void, decltype(dlClose)>{dlopen(path.c_str(), RTLD_LAZY), dlClose}; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 176 | if (!libHandle) { |
| 177 | LOG(ERROR) << __func__ << ": dlopen failed, err: " << dlerror(); |
Shunkai Yao | e221e71 | 2023-01-10 00:58:03 +0000 | [diff] [blame] | 178 | return false; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Shunkai Yao | 52ba4dc | 2023-02-01 21:57:20 +0000 | [diff] [blame] | 181 | LOG(INFO) << __func__ << " dlopen lib:" << path << "\nimpl:" << impl.toString() |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 182 | << "\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 Yao | 52ba4dc | 2023-02-01 21:57:20 +0000 | [diff] [blame] | 187 | std::unique_ptr<struct effect_dl_interface_s>(interface), path)}); |
Shunkai Yao | e221e71 | 2023-01-10 00:58:03 +0000 | [diff] [blame] | 188 | return true; |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 189 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 190 | |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 191 | void 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 Yao | 52ba4dc | 2023-02-01 21:57:20 +0000 | [diff] [blame] | 201 | LOG(DEBUG) << __func__ << " loading lib " << path->second << ": typeUuid " |
| 202 | << id.type.toString() << "\nimplUuid " << id.uuid.toString() << " proxyUuid " |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 203 | << (proxyUuid.has_value() ? proxyUuid->toString() : "null"); |
Shunkai Yao | e221e71 | 2023-01-10 00:58:03 +0000 | [diff] [blame] | 204 | if (openEffectLibrary(id.uuid, path->second)) { |
| 205 | mIdentitySet.insert(std::move(id)); |
| 206 | } |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 207 | } else { |
| 208 | LOG(ERROR) << __func__ << ": library " << libName << " not exist!"; |
| 209 | return; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 210 | } |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void Factory::loadEffectLibs() { |
| 214 | const auto& configEffectsMap = mConfig.getEffectsMap(); |
| 215 | for (const auto& configEffects : configEffectsMap) { |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 216 | if (AudioUuid uuid; |
| 217 | EffectConfig::findUuid(configEffects.first /* xml effect name */, &uuid)) { |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 218 | 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 Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 225 | createIdentityWithConfig(configLib, uuid, proxyUuid); |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 226 | } |
| 227 | } else { |
| 228 | LOG(ERROR) << __func__ << ": can not find type UUID for effect " << configEffects.first |
| 229 | << " skipping!"; |
| 230 | } |
| 231 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 234 | void 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 Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 262 | } // namespace aidl::android::hardware::audio::effect |