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 | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 20 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 21 | #include "effect-impl/EffectUUID.h" |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 22 | #include "effectFactory-impl/EffectFactory.h" |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 23 | |
| 24 | using aidl::android::media::audio::common::AudioUuid; |
| 25 | |
| 26 | namespace aidl::android::hardware::audio::effect { |
| 27 | |
| 28 | Factory::Factory() { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 29 | std::function<void(void*)> dlClose = [](void* handle) -> void { |
| 30 | if (handle && dlclose(handle)) { |
| 31 | LOG(ERROR) << "dlclose failed " << dlerror(); |
| 32 | } |
| 33 | }; |
| 34 | // TODO: implement this with audio_effect.xml. |
| 35 | auto libHandle = |
| 36 | std::unique_ptr<void, decltype(dlClose)>{dlopen("libequalizer.so", RTLD_LAZY), dlClose}; |
| 37 | if (!libHandle) { |
| 38 | LOG(ERROR) << __func__ << ": dlopen failed, err: " << dlerror(); |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | LOG(DEBUG) << __func__ << " dlopen uuid: " << EqualizerSwImplUUID.toString() << " handle " |
| 43 | << libHandle; |
| 44 | mEffectLibMap.insert({EqualizerSwImplUUID, std::make_pair(std::move(libHandle), nullptr)}); |
| 45 | |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 46 | Descriptor::Identity id; |
Shunkai Yao | 121c6dd | 2022-09-21 23:42:08 +0000 | [diff] [blame] | 47 | id.type = EqualizerTypeUUID; |
| 48 | id.uuid = EqualizerSwImplUUID; |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 49 | mIdentityList.push_back(id); |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 52 | Factory::~Factory() { |
| 53 | if (auto count = mEffectUuidMap.size()) { |
| 54 | LOG(ERROR) << __func__ << " remaining " << count |
| 55 | << " effect instances not destroyed indicating resource leak!"; |
| 56 | for (const auto& it : mEffectUuidMap) { |
| 57 | if (auto spEffect = it.first.lock()) { |
| 58 | LOG(ERROR) << __func__ << " erase remaining instance UUID " << it.second.toString(); |
| 59 | destroyEffectImpl(spEffect); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | ndk::ScopedAStatus Factory::queryEffects(const std::optional<AudioUuid>& in_type_uuid, |
| 66 | const std::optional<AudioUuid>& in_impl_uuid, |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 67 | std::vector<Descriptor::Identity>* _aidl_return) { |
| 68 | std::copy_if(mIdentityList.begin(), mIdentityList.end(), std::back_inserter(*_aidl_return), |
| 69 | [&](auto& desc) { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 70 | return (!in_type_uuid.has_value() || in_type_uuid.value() == desc.type) && |
| 71 | (!in_impl_uuid.has_value() || in_impl_uuid.value() == desc.uuid); |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 72 | }); |
| 73 | return ndk::ScopedAStatus::ok(); |
| 74 | } |
| 75 | |
Shunkai Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 76 | ndk::ScopedAStatus Factory::queryProcessing(const std::optional<Processing::Type>& in_type, |
| 77 | std::vector<Processing>* _aidl_return) { |
| 78 | // TODO: implement this with audio_effect.xml. |
| 79 | if (in_type.has_value()) { |
| 80 | // return all matching process filter |
| 81 | LOG(DEBUG) << __func__ << " process type: " << in_type.value().toString(); |
| 82 | } |
| 83 | LOG(DEBUG) << __func__ << " return " << _aidl_return->size(); |
| 84 | return ndk::ScopedAStatus::ok(); |
| 85 | } |
| 86 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 87 | #define RETURN_IF_BINDER_EXCEPTION(functor) \ |
| 88 | { \ |
| 89 | binder_exception_t exception = functor; \ |
| 90 | if (EX_NONE != exception) { \ |
| 91 | LOG(ERROR) << #functor << ": failed with error " << exception; \ |
| 92 | return ndk::ScopedAStatus::fromExceptionCode(exception); \ |
| 93 | } \ |
| 94 | } |
| 95 | |
| 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 | 121c6dd | 2022-09-21 23:42:08 +0000 | [diff] [blame] | 99 | if (in_impl_uuid == EqualizerSwImplUUID) { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 100 | if (mEffectLibMap.count(in_impl_uuid)) { |
| 101 | auto& lib = mEffectLibMap[in_impl_uuid]; |
| 102 | // didn't do dlsym yet |
| 103 | if (nullptr == lib.second) { |
| 104 | void* libHandle = lib.first.get(); |
| 105 | struct effect_interface_s intf = { |
| 106 | .createEffectFunc = (EffectCreateFunctor)dlsym(libHandle, "createEffect"), |
| 107 | .destroyEffectFunc = |
| 108 | (EffectDestroyFunctor)dlsym(libHandle, "destroyEffect")}; |
| 109 | auto dlInterface = std::make_unique<struct effect_interface_s>(intf); |
| 110 | if (!dlInterface->createEffectFunc || !dlInterface->destroyEffectFunc) { |
| 111 | LOG(ERROR) << __func__ |
| 112 | << ": create or destroy symbol not exist in library: " << libHandle |
| 113 | << "!"; |
| 114 | return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED); |
| 115 | } |
| 116 | lib.second = std::move(dlInterface); |
| 117 | } |
| 118 | |
| 119 | auto& libInterface = lib.second; |
| 120 | std::shared_ptr<IEffect> effectSp; |
| 121 | RETURN_IF_BINDER_EXCEPTION(libInterface->createEffectFunc(&effectSp)); |
| 122 | if (!effectSp) { |
| 123 | LOG(ERROR) << __func__ << ": library created null instance without return error!"; |
| 124 | return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED); |
| 125 | } |
| 126 | *_aidl_return = effectSp; |
| 127 | mEffectUuidMap[std::weak_ptr<IEffect>(effectSp)] = in_impl_uuid; |
| 128 | LOG(DEBUG) << __func__ << ": instance " << effectSp.get() << " created successfully"; |
| 129 | return ndk::ScopedAStatus::ok(); |
| 130 | } else { |
| 131 | LOG(ERROR) << __func__ << ": library doesn't exist"; |
| 132 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 133 | } |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 134 | } else { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 135 | LOG(ERROR) << __func__ << ": UUID not supported"; |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 136 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 137 | } |
| 138 | return ndk::ScopedAStatus::ok(); |
| 139 | } |
| 140 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 141 | ndk::ScopedAStatus Factory::destroyEffectImpl(const std::shared_ptr<IEffect>& in_handle) { |
| 142 | std::weak_ptr<IEffect> wpHandle(in_handle); |
| 143 | // find UUID with key (std::weak_ptr<IEffect>) |
| 144 | if (auto uuidIt = mEffectUuidMap.find(wpHandle); uuidIt != mEffectUuidMap.end()) { |
| 145 | auto& uuid = uuidIt->second; |
| 146 | // find implementation library with UUID |
| 147 | if (auto libIt = mEffectLibMap.find(uuid); libIt != mEffectLibMap.end()) { |
| 148 | if (libIt->second.second->destroyEffectFunc) { |
| 149 | RETURN_IF_BINDER_EXCEPTION(libIt->second.second->destroyEffectFunc(in_handle)); |
| 150 | } |
| 151 | } else { |
| 152 | LOG(ERROR) << __func__ << ": UUID " << uuid.toString() << " does not exist in libMap!"; |
| 153 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 154 | } |
| 155 | mEffectUuidMap.erase(uuidIt); |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 156 | return ndk::ScopedAStatus::ok(); |
| 157 | } else { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 158 | LOG(ERROR) << __func__ << ": instance " << in_handle << " does not exist!"; |
Shunkai Yao | 4590517 | 2022-08-24 18:14:02 +0000 | [diff] [blame] | 159 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 160 | } |
| 161 | } |
| 162 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 163 | // go over the map and cleanup all expired weak_ptrs. |
| 164 | void Factory::cleanupEffectMap() { |
| 165 | for (auto it = mEffectUuidMap.begin(); it != mEffectUuidMap.end();) { |
| 166 | if (nullptr == it->first.lock()) { |
| 167 | it = mEffectUuidMap.erase(it); |
| 168 | } else { |
| 169 | ++it; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | ndk::ScopedAStatus Factory::destroyEffect(const std::shared_ptr<IEffect>& in_handle) { |
| 175 | LOG(DEBUG) << __func__ << ": instance " << in_handle.get(); |
| 176 | ndk::ScopedAStatus status = destroyEffectImpl(in_handle); |
| 177 | // always do the cleanup |
| 178 | cleanupEffectMap(); |
| 179 | return status; |
| 180 | } |
| 181 | |
Shunkai Yao | c23916b | 2022-07-13 04:59:37 +0000 | [diff] [blame] | 182 | } // namespace aidl::android::hardware::audio::effect |