2022-07-13 06:59:37 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 08:13:58 +01:00
|
|
|
#include <iterator>
|
|
|
|
#include <memory>
|
|
|
|
#include <tuple>
|
|
|
|
#include "include/effect-impl/EffectTypes.h"
|
2022-07-13 06:59:37 +02:00
|
|
|
#define LOG_TAG "AHAL_EffectFactory"
|
2022-09-28 19:39:23 +02:00
|
|
|
#include <dlfcn.h>
|
2022-11-10 18:16:50 +01:00
|
|
|
#include <unordered_set>
|
2022-07-13 06:59:37 +02:00
|
|
|
|
2022-12-06 04:25:59 +01:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android/binder_ibinder_platform.h>
|
|
|
|
#include <system/thread_defs.h>
|
|
|
|
|
2022-10-27 00:47:20 +02:00
|
|
|
#include "effect-impl/EffectTypes.h"
|
2022-09-28 19:39:23 +02:00
|
|
|
#include "effect-impl/EffectUUID.h"
|
2022-07-13 06:59:37 +02:00
|
|
|
#include "effectFactory-impl/EffectFactory.h"
|
|
|
|
|
|
|
|
using aidl::android::media::audio::common::AudioUuid;
|
|
|
|
|
|
|
|
namespace aidl::android::hardware::audio::effect {
|
|
|
|
|
2022-11-10 18:16:50 +01:00
|
|
|
Factory::Factory(const std::string& file) : mConfig(EffectConfig(file)) {
|
|
|
|
LOG(DEBUG) << __func__ << " with config file: " << file;
|
|
|
|
loadEffectLibs();
|
2022-07-13 06:59:37 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 19:39:23 +02:00
|
|
|
Factory::~Factory() {
|
2022-12-15 01:11:14 +01:00
|
|
|
if (auto count = mEffectMap.size()) {
|
2022-09-28 19:39:23 +02:00
|
|
|
LOG(ERROR) << __func__ << " remaining " << count
|
|
|
|
<< " effect instances not destroyed indicating resource leak!";
|
2022-12-15 01:11:14 +01:00
|
|
|
for (const auto& it : mEffectMap) {
|
2022-09-28 19:39:23 +02:00
|
|
|
if (auto spEffect = it.first.lock()) {
|
2022-12-15 01:11:14 +01:00
|
|
|
LOG(ERROR) << __func__ << " erase remaining instance UUID "
|
|
|
|
<< it.second.first.toString();
|
2022-09-28 19:39:23 +02:00
|
|
|
destroyEffectImpl(spEffect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ndk::ScopedAStatus Factory::queryEffects(const std::optional<AudioUuid>& in_type_uuid,
|
|
|
|
const std::optional<AudioUuid>& in_impl_uuid,
|
2022-10-27 00:47:20 +02:00
|
|
|
const std::optional<AudioUuid>& in_proxy_uuid,
|
2022-12-12 08:13:58 +01:00
|
|
|
std::vector<Descriptor>* _aidl_return) {
|
|
|
|
// get the matching list
|
|
|
|
std::vector<Descriptor::Identity> idList;
|
|
|
|
std::copy_if(mIdentitySet.begin(), mIdentitySet.end(), std::back_inserter(idList),
|
|
|
|
[&](auto& id) {
|
|
|
|
return (!in_type_uuid.has_value() || in_type_uuid.value() == id.type) &&
|
|
|
|
(!in_impl_uuid.has_value() || in_impl_uuid.value() == id.uuid) &&
|
|
|
|
(!in_proxy_uuid.has_value() ||
|
|
|
|
(id.proxy.has_value() && in_proxy_uuid.value() == id.proxy.value()));
|
|
|
|
});
|
|
|
|
// query through the matching list
|
|
|
|
for (const auto& id : idList) {
|
|
|
|
if (mEffectLibMap.count(id.uuid)) {
|
|
|
|
Descriptor desc;
|
|
|
|
auto& entry = mEffectLibMap[id.uuid];
|
|
|
|
getDlSyms(entry);
|
|
|
|
auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry);
|
|
|
|
RETURN_IF(!libInterface || !libInterface->queryEffectFunc, EX_NULL_POINTER,
|
|
|
|
"dlNullQueryEffectFunc");
|
|
|
|
RETURN_IF_BINDER_EXCEPTION(libInterface->queryEffectFunc(&id.uuid, &desc));
|
|
|
|
_aidl_return->emplace_back(std::move(desc));
|
|
|
|
}
|
|
|
|
}
|
2022-07-13 06:59:37 +02:00
|
|
|
return ndk::ScopedAStatus::ok();
|
|
|
|
}
|
|
|
|
|
2022-10-13 23:11:11 +02:00
|
|
|
ndk::ScopedAStatus Factory::queryProcessing(const std::optional<Processing::Type>& in_type,
|
|
|
|
std::vector<Processing>* _aidl_return) {
|
|
|
|
// TODO: implement this with audio_effect.xml.
|
|
|
|
if (in_type.has_value()) {
|
|
|
|
// return all matching process filter
|
|
|
|
LOG(DEBUG) << __func__ << " process type: " << in_type.value().toString();
|
|
|
|
}
|
|
|
|
LOG(DEBUG) << __func__ << " return " << _aidl_return->size();
|
|
|
|
return ndk::ScopedAStatus::ok();
|
|
|
|
}
|
|
|
|
|
2022-09-28 19:39:23 +02:00
|
|
|
ndk::ScopedAStatus Factory::createEffect(const AudioUuid& in_impl_uuid,
|
|
|
|
std::shared_ptr<IEffect>* _aidl_return) {
|
2022-08-24 20:14:02 +02:00
|
|
|
LOG(DEBUG) << __func__ << ": UUID " << in_impl_uuid.toString();
|
2022-10-27 00:47:20 +02:00
|
|
|
if (mEffectLibMap.count(in_impl_uuid)) {
|
2022-12-12 08:13:58 +01:00
|
|
|
auto& entry = mEffectLibMap[in_impl_uuid];
|
|
|
|
getDlSyms(entry);
|
2022-10-27 00:47:20 +02:00
|
|
|
|
2022-12-12 08:13:58 +01:00
|
|
|
auto& libInterface = std::get<kMapEntryInterfaceIndex>(entry);
|
|
|
|
RETURN_IF(!libInterface || !libInterface->createEffectFunc, EX_NULL_POINTER,
|
|
|
|
"dlNullcreateEffectFunc");
|
2022-10-27 00:47:20 +02:00
|
|
|
std::shared_ptr<IEffect> effectSp;
|
|
|
|
RETURN_IF_BINDER_EXCEPTION(libInterface->createEffectFunc(&in_impl_uuid, &effectSp));
|
|
|
|
if (!effectSp) {
|
|
|
|
LOG(ERROR) << __func__ << ": library created null instance without return error!";
|
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
|
2022-09-28 19:39:23 +02:00
|
|
|
}
|
2022-10-27 00:47:20 +02:00
|
|
|
*_aidl_return = effectSp;
|
2022-12-15 01:11:14 +01:00
|
|
|
ndk::SpAIBinder effectBinder = effectSp->asBinder();
|
|
|
|
AIBinder_setMinSchedulerPolicy(effectBinder.get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
|
|
|
|
mEffectMap[std::weak_ptr<IEffect>(effectSp)] =
|
|
|
|
std::make_pair(in_impl_uuid, std::move(effectBinder));
|
2022-10-27 00:47:20 +02:00
|
|
|
LOG(DEBUG) << __func__ << ": instance " << effectSp.get() << " created successfully";
|
|
|
|
return ndk::ScopedAStatus::ok();
|
2022-08-24 20:14:02 +02:00
|
|
|
} else {
|
2022-10-27 00:47:20 +02:00
|
|
|
LOG(ERROR) << __func__ << ": library doesn't exist";
|
2022-08-24 20:14:02 +02:00
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
|
|
|
|
}
|
|
|
|
return ndk::ScopedAStatus::ok();
|
|
|
|
}
|
|
|
|
|
2022-09-28 19:39:23 +02:00
|
|
|
ndk::ScopedAStatus Factory::destroyEffectImpl(const std::shared_ptr<IEffect>& in_handle) {
|
|
|
|
std::weak_ptr<IEffect> wpHandle(in_handle);
|
2022-12-15 01:11:14 +01:00
|
|
|
// find the effect entry with key (std::weak_ptr<IEffect>)
|
|
|
|
if (auto effectIt = mEffectMap.find(wpHandle); effectIt != mEffectMap.end()) {
|
|
|
|
auto& uuid = effectIt->second.first;
|
2022-09-28 19:39:23 +02:00
|
|
|
// find implementation library with UUID
|
|
|
|
if (auto libIt = mEffectLibMap.find(uuid); libIt != mEffectLibMap.end()) {
|
2022-12-12 08:13:58 +01:00
|
|
|
auto& interface = std::get<kMapEntryInterfaceIndex>(libIt->second);
|
|
|
|
RETURN_IF(!interface || !interface->destroyEffectFunc, EX_NULL_POINTER,
|
|
|
|
"dlNulldestroyEffectFunc");
|
|
|
|
RETURN_IF_BINDER_EXCEPTION(interface->destroyEffectFunc(in_handle));
|
2022-09-28 19:39:23 +02:00
|
|
|
} else {
|
|
|
|
LOG(ERROR) << __func__ << ": UUID " << uuid.toString() << " does not exist in libMap!";
|
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
|
|
|
|
}
|
2022-12-15 01:11:14 +01:00
|
|
|
mEffectMap.erase(effectIt);
|
2022-08-24 20:14:02 +02:00
|
|
|
return ndk::ScopedAStatus::ok();
|
|
|
|
} else {
|
2022-09-28 19:39:23 +02:00
|
|
|
LOG(ERROR) << __func__ << ": instance " << in_handle << " does not exist!";
|
2022-08-24 20:14:02 +02:00
|
|
|
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-28 19:39:23 +02:00
|
|
|
// go over the map and cleanup all expired weak_ptrs.
|
|
|
|
void Factory::cleanupEffectMap() {
|
2022-12-15 01:11:14 +01:00
|
|
|
for (auto it = mEffectMap.begin(); it != mEffectMap.end();) {
|
2022-09-28 19:39:23 +02:00
|
|
|
if (nullptr == it->first.lock()) {
|
2022-12-15 01:11:14 +01:00
|
|
|
it = mEffectMap.erase(it);
|
2022-09-28 19:39:23 +02:00
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ndk::ScopedAStatus Factory::destroyEffect(const std::shared_ptr<IEffect>& in_handle) {
|
|
|
|
LOG(DEBUG) << __func__ << ": instance " << in_handle.get();
|
|
|
|
ndk::ScopedAStatus status = destroyEffectImpl(in_handle);
|
|
|
|
// always do the cleanup
|
|
|
|
cleanupEffectMap();
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2023-02-01 22:57:20 +01:00
|
|
|
bool Factory::openEffectLibrary(const AudioUuid& impl, const std::string& path) {
|
2022-10-27 00:47:20 +02:00
|
|
|
std::function<void(void*)> dlClose = [](void* handle) -> void {
|
|
|
|
if (handle && dlclose(handle)) {
|
|
|
|
LOG(ERROR) << "dlclose failed " << dlerror();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto libHandle =
|
2023-02-01 22:57:20 +01:00
|
|
|
std::unique_ptr<void, decltype(dlClose)>{dlopen(path.c_str(), RTLD_LAZY), dlClose};
|
2022-10-27 00:47:20 +02:00
|
|
|
if (!libHandle) {
|
|
|
|
LOG(ERROR) << __func__ << ": dlopen failed, err: " << dlerror();
|
2023-01-10 01:58:03 +01:00
|
|
|
return false;
|
2022-10-27 00:47:20 +02:00
|
|
|
}
|
|
|
|
|
2023-02-01 22:57:20 +01:00
|
|
|
LOG(INFO) << __func__ << " dlopen lib:" << path << "\nimpl:" << impl.toString()
|
2022-12-12 08:13:58 +01:00
|
|
|
<< "\nhandle:" << libHandle;
|
|
|
|
auto interface = new effect_dl_interface_s{nullptr, nullptr, nullptr};
|
|
|
|
mEffectLibMap.insert(
|
|
|
|
{impl,
|
|
|
|
std::make_tuple(std::move(libHandle),
|
2023-02-01 22:57:20 +01:00
|
|
|
std::unique_ptr<struct effect_dl_interface_s>(interface), path)});
|
2023-01-10 01:58:03 +01:00
|
|
|
return true;
|
2022-11-10 18:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Factory::createIdentityWithConfig(const EffectConfig::LibraryUuid& configLib,
|
|
|
|
const AudioUuid& typeUuid,
|
|
|
|
const std::optional<AudioUuid> proxyUuid) {
|
|
|
|
static const auto& libMap = mConfig.getLibraryMap();
|
|
|
|
const std::string& libName = configLib.name;
|
|
|
|
if (auto path = libMap.find(libName); path != libMap.end()) {
|
|
|
|
Descriptor::Identity id;
|
|
|
|
id.type = typeUuid;
|
|
|
|
id.uuid = configLib.uuid;
|
|
|
|
id.proxy = proxyUuid;
|
2023-02-01 22:57:20 +01:00
|
|
|
LOG(DEBUG) << __func__ << " loading lib " << path->second << ": typeUuid "
|
|
|
|
<< id.type.toString() << "\nimplUuid " << id.uuid.toString() << " proxyUuid "
|
2022-11-10 18:16:50 +01:00
|
|
|
<< (proxyUuid.has_value() ? proxyUuid->toString() : "null");
|
2023-01-10 01:58:03 +01:00
|
|
|
if (openEffectLibrary(id.uuid, path->second)) {
|
|
|
|
mIdentitySet.insert(std::move(id));
|
|
|
|
}
|
2022-11-10 18:16:50 +01:00
|
|
|
} else {
|
|
|
|
LOG(ERROR) << __func__ << ": library " << libName << " not exist!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-10-27 00:47:20 +02:00
|
|
|
|
2022-11-10 18:16:50 +01:00
|
|
|
void Factory::loadEffectLibs() {
|
|
|
|
const auto& configEffectsMap = mConfig.getEffectsMap();
|
|
|
|
for (const auto& configEffects : configEffectsMap) {
|
|
|
|
if (auto typeUuid = kUuidNameTypeMap.find(configEffects.first /* effect name */);
|
|
|
|
typeUuid != kUuidNameTypeMap.end()) {
|
|
|
|
const auto& configLibs = configEffects.second;
|
|
|
|
std::optional<AudioUuid> proxyUuid;
|
|
|
|
if (configLibs.proxyLibrary.has_value()) {
|
|
|
|
const auto& proxyLib = configLibs.proxyLibrary.value();
|
|
|
|
proxyUuid = proxyLib.uuid;
|
|
|
|
}
|
|
|
|
for (const auto& configLib : configLibs.libraries) {
|
|
|
|
createIdentityWithConfig(configLib, typeUuid->second, proxyUuid);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << __func__ << ": can not find type UUID for effect " << configEffects.first
|
|
|
|
<< " skipping!";
|
|
|
|
}
|
2022-10-27 00:47:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 08:13:58 +01:00
|
|
|
void Factory::getDlSyms(DlEntry& entry) {
|
|
|
|
auto& dlHandle = std::get<kMapEntryHandleIndex>(entry);
|
|
|
|
RETURN_VALUE_IF(!dlHandle, void(), "dlNullHandle");
|
|
|
|
// Get the reference of the DL interfaces in library map tuple.
|
|
|
|
auto& dlInterface = std::get<kMapEntryInterfaceIndex>(entry);
|
|
|
|
// return if interface already exist
|
|
|
|
if (!dlInterface->createEffectFunc) {
|
|
|
|
dlInterface->createEffectFunc = (EffectCreateFunctor)dlsym(dlHandle.get(), "createEffect");
|
|
|
|
}
|
|
|
|
if (!dlInterface->queryEffectFunc) {
|
|
|
|
dlInterface->queryEffectFunc = (EffectQueryFunctor)dlsym(dlHandle.get(), "queryEffect");
|
|
|
|
}
|
|
|
|
if (!dlInterface->destroyEffectFunc) {
|
|
|
|
dlInterface->destroyEffectFunc =
|
|
|
|
(EffectDestroyFunctor)dlsym(dlHandle.get(), "destroyEffect");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dlInterface->createEffectFunc || !dlInterface->destroyEffectFunc ||
|
|
|
|
!dlInterface->queryEffectFunc) {
|
|
|
|
LOG(ERROR) << __func__ << ": create (" << dlInterface->createEffectFunc << "), query ("
|
|
|
|
<< dlInterface->queryEffectFunc << "), or destroy ("
|
|
|
|
<< dlInterface->destroyEffectFunc
|
|
|
|
<< ") not exist in library: " << std::get<kMapEntryLibNameIndex>(entry)
|
|
|
|
<< " handle: " << dlHandle << " with dlerror: " << dlerror();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:59:37 +02:00
|
|
|
} // namespace aidl::android::hardware::audio::effect
|