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 | |
| 17 | #define LOG_TAG "AHAL_EffectFactory" |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 18 | #include <dlfcn.h> |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 19 | #include <unordered_set> |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 20 | |
Shunkai Yao | 39bf2c3 | 2022-12-06 03:25:59 +0000 | [diff] [blame^] | 21 | #include <android-base/logging.h> |
| 22 | #include <android/binder_ibinder_platform.h> |
| 23 | #include <system/thread_defs.h> |
| 24 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 25 | #include "effect-impl/EffectTypes.h" |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 26 | #include "effect-impl/EffectUUID.h" |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 27 | #include "effectFactory-impl/EffectFactory.h" |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 28 | |
| 29 | using aidl::android::media::audio::common::AudioUuid; |
| 30 | |
| 31 | namespace aidl::android::hardware::audio::effect { |
| 32 | |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 33 | Factory::Factory(const std::string& file) : mConfig(EffectConfig(file)) { |
| 34 | LOG(DEBUG) << __func__ << " with config file: " << file; |
| 35 | loadEffectLibs(); |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 38 | Factory::~Factory() { |
| 39 | if (auto count = mEffectUuidMap.size()) { |
| 40 | LOG(ERROR) << __func__ << " remaining " << count |
| 41 | << " effect instances not destroyed indicating resource leak!"; |
| 42 | for (const auto& it : mEffectUuidMap) { |
| 43 | if (auto spEffect = it.first.lock()) { |
| 44 | LOG(ERROR) << __func__ << " erase remaining instance UUID " << it.second.toString(); |
| 45 | destroyEffectImpl(spEffect); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | ndk::ScopedAStatus Factory::queryEffects(const std::optional<AudioUuid>& in_type_uuid, |
| 52 | const std::optional<AudioUuid>& in_impl_uuid, |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 53 | const std::optional<AudioUuid>& in_proxy_uuid, |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 54 | std::vector<Descriptor::Identity>* _aidl_return) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 55 | std::copy_if( |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 56 | mIdentitySet.begin(), mIdentitySet.end(), std::back_inserter(*_aidl_return), |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 57 | [&](auto& desc) { |
| 58 | return (!in_type_uuid.has_value() || in_type_uuid.value() == desc.type) && |
| 59 | (!in_impl_uuid.has_value() || in_impl_uuid.value() == desc.uuid) && |
| 60 | (!in_proxy_uuid.has_value() || |
| 61 | (desc.proxy.has_value() && in_proxy_uuid.value() == desc.proxy.value())); |
| 62 | }); |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 63 | return ndk::ScopedAStatus::ok(); |
| 64 | } |
| 65 | |
Shunkai Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 66 | ndk::ScopedAStatus Factory::queryProcessing(const std::optional<Processing::Type>& in_type, |
| 67 | std::vector<Processing>* _aidl_return) { |
| 68 | // TODO: implement this with audio_effect.xml. |
| 69 | if (in_type.has_value()) { |
| 70 | // return all matching process filter |
| 71 | LOG(DEBUG) << __func__ << " process type: " << in_type.value().toString(); |
| 72 | } |
| 73 | LOG(DEBUG) << __func__ << " return " << _aidl_return->size(); |
| 74 | return ndk::ScopedAStatus::ok(); |
| 75 | } |
| 76 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 77 | #define RETURN_IF_BINDER_EXCEPTION(functor) \ |
| 78 | { \ |
| 79 | binder_exception_t exception = functor; \ |
| 80 | if (EX_NONE != exception) { \ |
| 81 | LOG(ERROR) << #functor << ": failed with error " << exception; \ |
| 82 | return ndk::ScopedAStatus::fromExceptionCode(exception); \ |
| 83 | } \ |
| 84 | } |
| 85 | |
| 86 | ndk::ScopedAStatus Factory::createEffect(const AudioUuid& in_impl_uuid, |
| 87 | std::shared_ptr<IEffect>* _aidl_return) { |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 88 | LOG(DEBUG) << __func__ << ": UUID " << in_impl_uuid.toString(); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 89 | if (mEffectLibMap.count(in_impl_uuid)) { |
| 90 | auto& lib = mEffectLibMap[in_impl_uuid]; |
| 91 | // didn't do dlsym yet |
| 92 | if (nullptr == lib.second) { |
| 93 | void* libHandle = lib.first.get(); |
| 94 | auto dlInterface = std::make_unique<struct effect_dl_interface_s>(); |
| 95 | dlInterface->createEffectFunc = (EffectCreateFunctor)dlsym(libHandle, "createEffect"); |
| 96 | dlInterface->destroyEffectFunc = |
| 97 | (EffectDestroyFunctor)dlsym(libHandle, "destroyEffect"); |
| 98 | if (!dlInterface->createEffectFunc || !dlInterface->destroyEffectFunc) { |
| 99 | LOG(ERROR) << __func__ |
| 100 | << ": create or destroy symbol not exist in library: " << libHandle |
| 101 | << " with dlerror: " << dlerror(); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 102 | return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED); |
| 103 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 104 | lib.second = std::move(dlInterface); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 105 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 106 | |
| 107 | auto& libInterface = lib.second; |
| 108 | std::shared_ptr<IEffect> effectSp; |
| 109 | RETURN_IF_BINDER_EXCEPTION(libInterface->createEffectFunc(&in_impl_uuid, &effectSp)); |
| 110 | if (!effectSp) { |
| 111 | LOG(ERROR) << __func__ << ": library created null instance without return error!"; |
| 112 | return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED); |
| 113 | } |
| 114 | *_aidl_return = effectSp; |
Shunkai Yao | 39bf2c3 | 2022-12-06 03:25:59 +0000 | [diff] [blame^] | 115 | AIBinder_setMinSchedulerPolicy(effectSp->asBinder().get(), SCHED_NORMAL, |
| 116 | ANDROID_PRIORITY_AUDIO); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 117 | mEffectUuidMap[std::weak_ptr<IEffect>(effectSp)] = in_impl_uuid; |
| 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); |
| 129 | // find UUID with key (std::weak_ptr<IEffect>) |
| 130 | if (auto uuidIt = mEffectUuidMap.find(wpHandle); uuidIt != mEffectUuidMap.end()) { |
| 131 | auto& uuid = uuidIt->second; |
| 132 | // find implementation library with UUID |
| 133 | if (auto libIt = mEffectLibMap.find(uuid); libIt != mEffectLibMap.end()) { |
| 134 | if (libIt->second.second->destroyEffectFunc) { |
| 135 | RETURN_IF_BINDER_EXCEPTION(libIt->second.second->destroyEffectFunc(in_handle)); |
| 136 | } |
| 137 | } else { |
| 138 | LOG(ERROR) << __func__ << ": UUID " << uuid.toString() << " does not exist in libMap!"; |
| 139 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 140 | } |
| 141 | mEffectUuidMap.erase(uuidIt); |
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() { |
| 151 | for (auto it = mEffectUuidMap.begin(); it != mEffectUuidMap.end();) { |
| 152 | if (nullptr == it->first.lock()) { |
| 153 | it = mEffectUuidMap.erase(it); |
| 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 | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 168 | void Factory::openEffectLibrary(const AudioUuid& impl, const std::string& libName) { |
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 = |
| 176 | std::unique_ptr<void, decltype(dlClose)>{dlopen(libName.c_str(), RTLD_LAZY), dlClose}; |
| 177 | if (!libHandle) { |
| 178 | LOG(ERROR) << __func__ << ": dlopen failed, err: " << dlerror(); |
| 179 | return; |
| 180 | } |
| 181 | |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 182 | LOG(DEBUG) << __func__ << " dlopen lib:" << libName << "\nimpl:" << impl.toString() |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 183 | << "\nhandle:" << libHandle; |
| 184 | mEffectLibMap.insert({impl, std::make_pair(std::move(libHandle), nullptr)}); |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 185 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 186 | |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 187 | void Factory::createIdentityWithConfig(const EffectConfig::LibraryUuid& configLib, |
| 188 | const AudioUuid& typeUuid, |
| 189 | const std::optional<AudioUuid> proxyUuid) { |
| 190 | static const auto& libMap = mConfig.getLibraryMap(); |
| 191 | const std::string& libName = configLib.name; |
| 192 | if (auto path = libMap.find(libName); path != libMap.end()) { |
| 193 | Descriptor::Identity id; |
| 194 | id.type = typeUuid; |
| 195 | id.uuid = configLib.uuid; |
| 196 | id.proxy = proxyUuid; |
| 197 | LOG(DEBUG) << __func__ << ": typeUuid " << id.type.toString() << "\nimplUuid " |
| 198 | << id.uuid.toString() << " proxyUuid " |
| 199 | << (proxyUuid.has_value() ? proxyUuid->toString() : "null"); |
| 200 | openEffectLibrary(id.uuid, path->second); |
| 201 | mIdentitySet.insert(std::move(id)); |
| 202 | } else { |
| 203 | LOG(ERROR) << __func__ << ": library " << libName << " not exist!"; |
| 204 | return; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 205 | } |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void Factory::loadEffectLibs() { |
| 209 | const auto& configEffectsMap = mConfig.getEffectsMap(); |
| 210 | for (const auto& configEffects : configEffectsMap) { |
| 211 | if (auto typeUuid = kUuidNameTypeMap.find(configEffects.first /* effect name */); |
| 212 | typeUuid != kUuidNameTypeMap.end()) { |
| 213 | const auto& configLibs = configEffects.second; |
| 214 | std::optional<AudioUuid> proxyUuid; |
| 215 | if (configLibs.proxyLibrary.has_value()) { |
| 216 | const auto& proxyLib = configLibs.proxyLibrary.value(); |
| 217 | proxyUuid = proxyLib.uuid; |
| 218 | } |
| 219 | for (const auto& configLib : configLibs.libraries) { |
| 220 | createIdentityWithConfig(configLib, typeUuid->second, proxyUuid); |
| 221 | } |
| 222 | } else { |
| 223 | LOG(ERROR) << __func__ << ": can not find type UUID for effect " << configEffects.first |
| 224 | << " skipping!"; |
| 225 | } |
| 226 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 229 | } // namespace aidl::android::hardware::audio::effect |