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