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 | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 17 | #include <iterator> |
| 18 | #include <memory> |
| 19 | #include <tuple> |
| 20 | #include "include/effect-impl/EffectTypes.h" |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 21 | #define LOG_TAG "AHAL_EffectFactory" |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 22 | #include <dlfcn.h> |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 23 | #include <unordered_set> |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 24 | |
Shunkai Yao | 39bf2c3 | 2022-12-06 03:25:59 +0000 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | #include <android/binder_ibinder_platform.h> |
| 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 | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 30 | #include "effect-impl/EffectUUID.h" |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 31 | #include "effectFactory-impl/EffectFactory.h" |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 32 | |
| 33 | using aidl::android::media::audio::common::AudioUuid; |
| 34 | |
| 35 | namespace aidl::android::hardware::audio::effect { |
| 36 | |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 37 | Factory::Factory(const std::string& file) : mConfig(EffectConfig(file)) { |
| 38 | LOG(DEBUG) << __func__ << " with config file: " << file; |
| 39 | loadEffectLibs(); |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 42 | Factory::~Factory() { |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 43 | if (auto count = mEffectMap.size()) { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 44 | LOG(ERROR) << __func__ << " remaining " << count |
| 45 | << " effect instances not destroyed indicating resource leak!"; |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 46 | for (const auto& it : mEffectMap) { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 47 | if (auto spEffect = it.first.lock()) { |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 48 | LOG(ERROR) << __func__ << " erase remaining instance UUID " |
| 49 | << it.second.first.toString(); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 50 | destroyEffectImpl(spEffect); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | ndk::ScopedAStatus Factory::queryEffects(const std::optional<AudioUuid>& in_type_uuid, |
| 57 | const std::optional<AudioUuid>& in_impl_uuid, |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 58 | const std::optional<AudioUuid>& in_proxy_uuid, |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 59 | std::vector<Descriptor>* _aidl_return) { |
| 60 | // get the matching list |
| 61 | std::vector<Descriptor::Identity> idList; |
| 62 | std::copy_if(mIdentitySet.begin(), mIdentitySet.end(), std::back_inserter(idList), |
| 63 | [&](auto& id) { |
| 64 | return (!in_type_uuid.has_value() || in_type_uuid.value() == id.type) && |
| 65 | (!in_impl_uuid.has_value() || in_impl_uuid.value() == id.uuid) && |
| 66 | (!in_proxy_uuid.has_value() || |
| 67 | (id.proxy.has_value() && in_proxy_uuid.value() == id.proxy.value())); |
| 68 | }); |
| 69 | // query through the matching list |
| 70 | for (const auto& id : idList) { |
| 71 | if (mEffectLibMap.count(id.uuid)) { |
| 72 | Descriptor desc; |
| 73 | auto& entry = mEffectLibMap[id.uuid]; |
| 74 | getDlSyms(entry); |
| 75 | auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry); |
| 76 | RETURN_IF(!libInterface || !libInterface->queryEffectFunc, EX_NULL_POINTER, |
| 77 | "dlNullQueryEffectFunc"); |
| 78 | RETURN_IF_BINDER_EXCEPTION(libInterface->queryEffectFunc(&id.uuid, &desc)); |
| 79 | _aidl_return->emplace_back(std::move(desc)); |
| 80 | } |
| 81 | } |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 82 | return ndk::ScopedAStatus::ok(); |
| 83 | } |
| 84 | |
Shunkai Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 85 | ndk::ScopedAStatus Factory::queryProcessing(const std::optional<Processing::Type>& in_type, |
| 86 | std::vector<Processing>* _aidl_return) { |
| 87 | // TODO: implement this with audio_effect.xml. |
| 88 | if (in_type.has_value()) { |
| 89 | // return all matching process filter |
| 90 | LOG(DEBUG) << __func__ << " process type: " << in_type.value().toString(); |
| 91 | } |
| 92 | LOG(DEBUG) << __func__ << " return " << _aidl_return->size(); |
| 93 | return ndk::ScopedAStatus::ok(); |
| 94 | } |
| 95 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 96 | ndk::ScopedAStatus Factory::createEffect(const AudioUuid& in_impl_uuid, |
| 97 | std::shared_ptr<IEffect>* _aidl_return) { |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 98 | LOG(DEBUG) << __func__ << ": UUID " << in_impl_uuid.toString(); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 99 | if (mEffectLibMap.count(in_impl_uuid)) { |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 100 | auto& entry = mEffectLibMap[in_impl_uuid]; |
| 101 | getDlSyms(entry); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 102 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 103 | auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry); |
| 104 | RETURN_IF(!libInterface || !libInterface->createEffectFunc, EX_NULL_POINTER, |
| 105 | "dlNullcreateEffectFunc"); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 106 | std::shared_ptr<IEffect> effectSp; |
| 107 | RETURN_IF_BINDER_EXCEPTION(libInterface->createEffectFunc(&in_impl_uuid, &effectSp)); |
| 108 | if (!effectSp) { |
| 109 | LOG(ERROR) << __func__ << ": library created null instance without return error!"; |
| 110 | return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED); |
| 111 | } |
| 112 | *_aidl_return = effectSp; |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 113 | ndk::SpAIBinder effectBinder = effectSp->asBinder(); |
| 114 | AIBinder_setMinSchedulerPolicy(effectBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO); |
| 115 | mEffectMap[std::weak_ptr<IEffect>(effectSp)] = |
| 116 | std::make_pair(in_impl_uuid, std::move(effectBinder)); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 117 | LOG(DEBUG) << __func__ << ": instance " << effectSp.get() << " created successfully"; |
| 118 | return ndk::ScopedAStatus::ok(); |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 119 | } else { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 120 | LOG(ERROR) << __func__ << ": library doesn't exist"; |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 121 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 122 | } |
| 123 | return ndk::ScopedAStatus::ok(); |
| 124 | } |
| 125 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 126 | ndk::ScopedAStatus Factory::destroyEffectImpl(const std::shared_ptr<IEffect>& in_handle) { |
| 127 | std::weak_ptr<IEffect> wpHandle(in_handle); |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 128 | // find the effect entry with key (std::weak_ptr<IEffect>) |
| 129 | if (auto effectIt = mEffectMap.find(wpHandle); effectIt != mEffectMap.end()) { |
| 130 | auto& uuid = effectIt->second.first; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 131 | // find implementation library with UUID |
| 132 | if (auto libIt = mEffectLibMap.find(uuid); libIt != mEffectLibMap.end()) { |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 133 | auto& interface = std::get<kMapEntryInterfaceIndex>(libIt->second); |
| 134 | RETURN_IF(!interface || !interface->destroyEffectFunc, EX_NULL_POINTER, |
| 135 | "dlNulldestroyEffectFunc"); |
| 136 | RETURN_IF_BINDER_EXCEPTION(interface->destroyEffectFunc(in_handle)); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 137 | } else { |
| 138 | LOG(ERROR) << __func__ << ": UUID " << uuid.toString() << " does not exist in libMap!"; |
| 139 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 140 | } |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 141 | mEffectMap.erase(effectIt); |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 142 | return ndk::ScopedAStatus::ok(); |
| 143 | } else { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 144 | LOG(ERROR) << __func__ << ": instance " << in_handle << " does not exist!"; |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 145 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 146 | } |
| 147 | } |
| 148 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 149 | // go over the map and cleanup all expired weak_ptrs. |
| 150 | void Factory::cleanupEffectMap() { |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 151 | for (auto it = mEffectMap.begin(); it != mEffectMap.end();) { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 152 | if (nullptr == it->first.lock()) { |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 153 | it = mEffectMap.erase(it); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 154 | } else { |
| 155 | ++it; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | ndk::ScopedAStatus Factory::destroyEffect(const std::shared_ptr<IEffect>& in_handle) { |
| 161 | LOG(DEBUG) << __func__ << ": instance " << in_handle.get(); |
| 162 | ndk::ScopedAStatus status = destroyEffectImpl(in_handle); |
| 163 | // always do the cleanup |
| 164 | cleanupEffectMap(); |
| 165 | return status; |
| 166 | } |
| 167 | |
Shunkai Yao | 52ba4dc | 2023-02-01 21:57:20 +0000 | [diff] [blame] | 168 | bool Factory::openEffectLibrary(const AudioUuid& impl, const std::string& path) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 169 | std::function<void(void*)> dlClose = [](void* handle) -> void { |
| 170 | if (handle && dlclose(handle)) { |
| 171 | LOG(ERROR) << "dlclose failed " << dlerror(); |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | auto libHandle = |
Shunkai Yao | 52ba4dc | 2023-02-01 21:57:20 +0000 | [diff] [blame] | 176 | 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] | 177 | if (!libHandle) { |
| 178 | LOG(ERROR) << __func__ << ": dlopen failed, err: " << dlerror(); |
Shunkai Yao | e221e71 | 2023-01-10 00:58:03 +0000 | [diff] [blame] | 179 | return false; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Shunkai Yao | 52ba4dc | 2023-02-01 21:57:20 +0000 | [diff] [blame] | 182 | LOG(INFO) << __func__ << " dlopen lib:" << path << "\nimpl:" << impl.toString() |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 183 | << "\nhandle:" << libHandle; |
| 184 | auto interface = new effect_dl_interface_s{nullptr, nullptr, nullptr}; |
| 185 | mEffectLibMap.insert( |
| 186 | {impl, |
| 187 | std::make_tuple(std::move(libHandle), |
Shunkai Yao | 52ba4dc | 2023-02-01 21:57:20 +0000 | [diff] [blame] | 188 | std::unique_ptr<struct effect_dl_interface_s>(interface), path)}); |
Shunkai Yao | e221e71 | 2023-01-10 00:58:03 +0000 | [diff] [blame] | 189 | return true; |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 190 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 191 | |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 192 | void Factory::createIdentityWithConfig(const EffectConfig::LibraryUuid& configLib, |
| 193 | const AudioUuid& typeUuid, |
| 194 | const std::optional<AudioUuid> proxyUuid) { |
| 195 | static const auto& libMap = mConfig.getLibraryMap(); |
| 196 | const std::string& libName = configLib.name; |
| 197 | if (auto path = libMap.find(libName); path != libMap.end()) { |
| 198 | Descriptor::Identity id; |
| 199 | id.type = typeUuid; |
| 200 | id.uuid = configLib.uuid; |
| 201 | id.proxy = proxyUuid; |
Shunkai Yao | 52ba4dc | 2023-02-01 21:57:20 +0000 | [diff] [blame] | 202 | LOG(DEBUG) << __func__ << " loading lib " << path->second << ": typeUuid " |
| 203 | << id.type.toString() << "\nimplUuid " << id.uuid.toString() << " proxyUuid " |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 204 | << (proxyUuid.has_value() ? proxyUuid->toString() : "null"); |
Shunkai Yao | e221e71 | 2023-01-10 00:58:03 +0000 | [diff] [blame] | 205 | if (openEffectLibrary(id.uuid, path->second)) { |
| 206 | mIdentitySet.insert(std::move(id)); |
| 207 | } |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 208 | } else { |
| 209 | LOG(ERROR) << __func__ << ": library " << libName << " not exist!"; |
| 210 | return; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 211 | } |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void Factory::loadEffectLibs() { |
| 215 | const auto& configEffectsMap = mConfig.getEffectsMap(); |
| 216 | for (const auto& configEffects : configEffectsMap) { |
| 217 | if (auto typeUuid = kUuidNameTypeMap.find(configEffects.first /* effect name */); |
| 218 | typeUuid != kUuidNameTypeMap.end()) { |
| 219 | const auto& configLibs = configEffects.second; |
| 220 | std::optional<AudioUuid> proxyUuid; |
| 221 | if (configLibs.proxyLibrary.has_value()) { |
| 222 | const auto& proxyLib = configLibs.proxyLibrary.value(); |
| 223 | proxyUuid = proxyLib.uuid; |
| 224 | } |
| 225 | for (const auto& configLib : configLibs.libraries) { |
| 226 | createIdentityWithConfig(configLib, typeUuid->second, proxyUuid); |
| 227 | } |
| 228 | } else { |
| 229 | LOG(ERROR) << __func__ << ": can not find type UUID for effect " << configEffects.first |
| 230 | << " skipping!"; |
| 231 | } |
| 232 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 235 | void Factory::getDlSyms(DlEntry& entry) { |
| 236 | auto& dlHandle = std::get<kMapEntryHandleIndex>(entry); |
| 237 | RETURN_VALUE_IF(!dlHandle, void(), "dlNullHandle"); |
| 238 | // Get the reference of the DL interfaces in library map tuple. |
| 239 | auto& dlInterface = std::get<kMapEntryInterfaceIndex>(entry); |
| 240 | // return if interface already exist |
| 241 | if (!dlInterface->createEffectFunc) { |
| 242 | dlInterface->createEffectFunc = (EffectCreateFunctor)dlsym(dlHandle.get(), "createEffect"); |
| 243 | } |
| 244 | if (!dlInterface->queryEffectFunc) { |
| 245 | dlInterface->queryEffectFunc = (EffectQueryFunctor)dlsym(dlHandle.get(), "queryEffect"); |
| 246 | } |
| 247 | if (!dlInterface->destroyEffectFunc) { |
| 248 | dlInterface->destroyEffectFunc = |
| 249 | (EffectDestroyFunctor)dlsym(dlHandle.get(), "destroyEffect"); |
| 250 | } |
| 251 | |
| 252 | if (!dlInterface->createEffectFunc || !dlInterface->destroyEffectFunc || |
| 253 | !dlInterface->queryEffectFunc) { |
| 254 | LOG(ERROR) << __func__ << ": create (" << dlInterface->createEffectFunc << "), query (" |
| 255 | << dlInterface->queryEffectFunc << "), or destroy (" |
| 256 | << dlInterface->destroyEffectFunc |
| 257 | << ") not exist in library: " << std::get<kMapEntryLibNameIndex>(entry) |
| 258 | << " handle: " << dlHandle << " with dlerror: " << dlerror(); |
| 259 | return; |
| 260 | } |
| 261 | } |
| 262 | |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 263 | } // namespace aidl::android::hardware::audio::effect |