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" |
| 18 | #include <android-base/logging.h> |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 19 | #include <dlfcn.h> |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame^] | 20 | #include <unordered_set> |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 21 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 22 | #include "effect-impl/EffectTypes.h" |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 23 | #include "effect-impl/EffectUUID.h" |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 24 | #include "effectFactory-impl/EffectFactory.h" |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 25 | |
| 26 | using aidl::android::media::audio::common::AudioUuid; |
| 27 | |
| 28 | namespace aidl::android::hardware::audio::effect { |
| 29 | |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame^] | 30 | Factory::Factory(const std::string& file) : mConfig(EffectConfig(file)) { |
| 31 | LOG(DEBUG) << __func__ << " with config file: " << file; |
| 32 | loadEffectLibs(); |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 35 | Factory::~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 | |
| 48 | ndk::ScopedAStatus Factory::queryEffects(const std::optional<AudioUuid>& in_type_uuid, |
| 49 | const std::optional<AudioUuid>& in_impl_uuid, |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 50 | const std::optional<AudioUuid>& in_proxy_uuid, |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 51 | std::vector<Descriptor::Identity>* _aidl_return) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 52 | std::copy_if( |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame^] | 53 | mIdentitySet.begin(), mIdentitySet.end(), std::back_inserter(*_aidl_return), |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 54 | [&](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 Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 60 | return ndk::ScopedAStatus::ok(); |
| 61 | } |
| 62 | |
Shunkai Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 63 | ndk::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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 74 | #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 | |
| 83 | ndk::ScopedAStatus Factory::createEffect(const AudioUuid& in_impl_uuid, |
| 84 | std::shared_ptr<IEffect>* _aidl_return) { |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 85 | LOG(DEBUG) << __func__ << ": UUID " << in_impl_uuid.toString(); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 86 | 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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 99 | return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED); |
| 100 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 101 | lib.second = std::move(dlInterface); |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 102 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 103 | |
| 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 Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 115 | } else { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 116 | LOG(ERROR) << __func__ << ": library doesn't exist"; |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 117 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 118 | } |
| 119 | return ndk::ScopedAStatus::ok(); |
| 120 | } |
| 121 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 122 | ndk::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 Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 137 | return ndk::ScopedAStatus::ok(); |
| 138 | } else { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 139 | LOG(ERROR) << __func__ << ": instance " << in_handle << " does not exist!"; |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 140 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 141 | } |
| 142 | } |
| 143 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 144 | // go over the map and cleanup all expired weak_ptrs. |
| 145 | void 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 | |
| 155 | ndk::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 Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame^] | 163 | void Factory::openEffectLibrary(const AudioUuid& impl, const std::string& libName) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 164 | 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 Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame^] | 177 | LOG(DEBUG) << __func__ << " dlopen lib:" << libName << "\nimpl:" << impl.toString() |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 178 | << "\nhandle:" << libHandle; |
| 179 | mEffectLibMap.insert({impl, std::make_pair(std::move(libHandle), nullptr)}); |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame^] | 180 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 181 | |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame^] | 182 | void 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 Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 200 | } |
Shunkai Yao | 60b34b7 | 2022-11-10 17:16:50 +0000 | [diff] [blame^] | 201 | } |
| 202 | |
| 203 | void 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 Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 224 | } // namespace aidl::android::hardware::audio::effect |