blob: e03dda378f0c06ceb6176b2e419730588cba55ce [file] [log] [blame]
Shunkai Yaoc23916b2022-07-13 04:59:37 +00001/*
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 Yaoea24c1a2022-09-28 17:39:23 +000019#include <dlfcn.h>
Shunkai Yaoc23916b2022-07-13 04:59:37 +000020
Shunkai Yao6afc8552022-10-26 22:47:20 +000021#include "effect-impl/EffectTypes.h"
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000022#include "effect-impl/EffectUUID.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000023#include "effectFactory-impl/EffectFactory.h"
Shunkai Yaoc23916b2022-07-13 04:59:37 +000024
25using aidl::android::media::audio::common::AudioUuid;
26
27namespace aidl::android::hardware::audio::effect {
28
29Factory::Factory() {
Shunkai Yao6afc8552022-10-26 22:47:20 +000030 // TODO: get list of library UUID and name from audio_effect.xml.
31 openEffectLibrary(EqualizerTypeUUID, EqualizerSwImplUUID, std::nullopt, "libequalizersw.so");
32 openEffectLibrary(EqualizerTypeUUID, EqualizerBundleImplUUID, std::nullopt, "libbundleaidl.so");
33 openEffectLibrary(BassBoostTypeUUID, BassBoostSwImplUUID, std::nullopt, "libbassboostsw.so");
34 openEffectLibrary(DynamicsProcessingTypeUUID, DynamicsProcessingSwImplUUID, std::nullopt,
35 "libdynamicsprocessingsw.so");
36 openEffectLibrary(HapticGeneratorTypeUUID, HapticGeneratorSwImplUUID, std::nullopt,
37 "libhapticgeneratorsw.so");
38 openEffectLibrary(LoudnessEnhancerTypeUUID, LoudnessEnhancerSwImplUUID, std::nullopt,
39 "libloudnessenhancersw.so");
40 openEffectLibrary(ReverbTypeUUID, ReverbSwImplUUID, std::nullopt, "libreverbsw.so");
41 openEffectLibrary(VirtualizerTypeUUID, VirtualizerSwImplUUID, std::nullopt,
42 "libvirtualizersw.so");
43 openEffectLibrary(VisualizerTypeUUID, VisualizerSwImplUUID, std::nullopt, "libvisualizersw.so");
44 openEffectLibrary(VolumeTypeUUID, VolumeSwImplUUID, std::nullopt, "libvolumesw.so");
Shunkai Yaoc23916b2022-07-13 04:59:37 +000045}
46
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000047Factory::~Factory() {
48 if (auto count = mEffectUuidMap.size()) {
49 LOG(ERROR) << __func__ << " remaining " << count
50 << " effect instances not destroyed indicating resource leak!";
51 for (const auto& it : mEffectUuidMap) {
52 if (auto spEffect = it.first.lock()) {
53 LOG(ERROR) << __func__ << " erase remaining instance UUID " << it.second.toString();
54 destroyEffectImpl(spEffect);
55 }
56 }
57 }
58}
59
60ndk::ScopedAStatus Factory::queryEffects(const std::optional<AudioUuid>& in_type_uuid,
61 const std::optional<AudioUuid>& in_impl_uuid,
Shunkai Yao6afc8552022-10-26 22:47:20 +000062 const std::optional<AudioUuid>& in_proxy_uuid,
Shunkai Yaoc23916b2022-07-13 04:59:37 +000063 std::vector<Descriptor::Identity>* _aidl_return) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000064 std::copy_if(
65 mIdentityList.begin(), mIdentityList.end(), std::back_inserter(*_aidl_return),
66 [&](auto& desc) {
67 return (!in_type_uuid.has_value() || in_type_uuid.value() == desc.type) &&
68 (!in_impl_uuid.has_value() || in_impl_uuid.value() == desc.uuid) &&
69 (!in_proxy_uuid.has_value() ||
70 (desc.proxy.has_value() && in_proxy_uuid.value() == desc.proxy.value()));
71 });
Shunkai Yaoc23916b2022-07-13 04:59:37 +000072 return ndk::ScopedAStatus::ok();
73}
74
Shunkai Yao08b687d2022-10-13 21:11:11 +000075ndk::ScopedAStatus Factory::queryProcessing(const std::optional<Processing::Type>& in_type,
76 std::vector<Processing>* _aidl_return) {
77 // TODO: implement this with audio_effect.xml.
78 if (in_type.has_value()) {
79 // return all matching process filter
80 LOG(DEBUG) << __func__ << " process type: " << in_type.value().toString();
81 }
82 LOG(DEBUG) << __func__ << " return " << _aidl_return->size();
83 return ndk::ScopedAStatus::ok();
84}
85
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000086#define RETURN_IF_BINDER_EXCEPTION(functor) \
87 { \
88 binder_exception_t exception = functor; \
89 if (EX_NONE != exception) { \
90 LOG(ERROR) << #functor << ": failed with error " << exception; \
91 return ndk::ScopedAStatus::fromExceptionCode(exception); \
92 } \
93 }
94
95ndk::ScopedAStatus Factory::createEffect(const AudioUuid& in_impl_uuid,
96 std::shared_ptr<IEffect>* _aidl_return) {
Shunkai Yao45905172022-08-24 18:14:02 +000097 LOG(DEBUG) << __func__ << ": UUID " << in_impl_uuid.toString();
Shunkai Yao6afc8552022-10-26 22:47:20 +000098 if (mEffectLibMap.count(in_impl_uuid)) {
99 auto& lib = mEffectLibMap[in_impl_uuid];
100 // didn't do dlsym yet
101 if (nullptr == lib.second) {
102 void* libHandle = lib.first.get();
103 auto dlInterface = std::make_unique<struct effect_dl_interface_s>();
104 dlInterface->createEffectFunc = (EffectCreateFunctor)dlsym(libHandle, "createEffect");
105 dlInterface->destroyEffectFunc =
106 (EffectDestroyFunctor)dlsym(libHandle, "destroyEffect");
107 if (!dlInterface->createEffectFunc || !dlInterface->destroyEffectFunc) {
108 LOG(ERROR) << __func__
109 << ": create or destroy symbol not exist in library: " << libHandle
110 << " with dlerror: " << dlerror();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000111 return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
112 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000113 lib.second = std::move(dlInterface);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000114 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000115
116 auto& libInterface = lib.second;
117 std::shared_ptr<IEffect> effectSp;
118 RETURN_IF_BINDER_EXCEPTION(libInterface->createEffectFunc(&in_impl_uuid, &effectSp));
119 if (!effectSp) {
120 LOG(ERROR) << __func__ << ": library created null instance without return error!";
121 return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
122 }
123 *_aidl_return = effectSp;
124 mEffectUuidMap[std::weak_ptr<IEffect>(effectSp)] = in_impl_uuid;
125 LOG(DEBUG) << __func__ << ": instance " << effectSp.get() << " created successfully";
126 return ndk::ScopedAStatus::ok();
Shunkai Yao45905172022-08-24 18:14:02 +0000127 } else {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000128 LOG(ERROR) << __func__ << ": library doesn't exist";
Shunkai Yao45905172022-08-24 18:14:02 +0000129 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
130 }
131 return ndk::ScopedAStatus::ok();
132}
133
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000134ndk::ScopedAStatus Factory::destroyEffectImpl(const std::shared_ptr<IEffect>& in_handle) {
135 std::weak_ptr<IEffect> wpHandle(in_handle);
136 // find UUID with key (std::weak_ptr<IEffect>)
137 if (auto uuidIt = mEffectUuidMap.find(wpHandle); uuidIt != mEffectUuidMap.end()) {
138 auto& uuid = uuidIt->second;
139 // find implementation library with UUID
140 if (auto libIt = mEffectLibMap.find(uuid); libIt != mEffectLibMap.end()) {
141 if (libIt->second.second->destroyEffectFunc) {
142 RETURN_IF_BINDER_EXCEPTION(libIt->second.second->destroyEffectFunc(in_handle));
143 }
144 } else {
145 LOG(ERROR) << __func__ << ": UUID " << uuid.toString() << " does not exist in libMap!";
146 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
147 }
148 mEffectUuidMap.erase(uuidIt);
Shunkai Yao45905172022-08-24 18:14:02 +0000149 return ndk::ScopedAStatus::ok();
150 } else {
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000151 LOG(ERROR) << __func__ << ": instance " << in_handle << " does not exist!";
Shunkai Yao45905172022-08-24 18:14:02 +0000152 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
153 }
154}
155
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000156// go over the map and cleanup all expired weak_ptrs.
157void Factory::cleanupEffectMap() {
158 for (auto it = mEffectUuidMap.begin(); it != mEffectUuidMap.end();) {
159 if (nullptr == it->first.lock()) {
160 it = mEffectUuidMap.erase(it);
161 } else {
162 ++it;
163 }
164 }
165}
166
167ndk::ScopedAStatus Factory::destroyEffect(const std::shared_ptr<IEffect>& in_handle) {
168 LOG(DEBUG) << __func__ << ": instance " << in_handle.get();
169 ndk::ScopedAStatus status = destroyEffectImpl(in_handle);
170 // always do the cleanup
171 cleanupEffectMap();
172 return status;
173}
174
Shunkai Yao6afc8552022-10-26 22:47:20 +0000175void Factory::openEffectLibrary(const AudioUuid& type, const AudioUuid& impl,
176 const std::optional<AudioUuid>& proxy, const std::string& libName) {
177 std::function<void(void*)> dlClose = [](void* handle) -> void {
178 if (handle && dlclose(handle)) {
179 LOG(ERROR) << "dlclose failed " << dlerror();
180 }
181 };
182
183 auto libHandle =
184 std::unique_ptr<void, decltype(dlClose)>{dlopen(libName.c_str(), RTLD_LAZY), dlClose};
185 if (!libHandle) {
186 LOG(ERROR) << __func__ << ": dlopen failed, err: " << dlerror();
187 return;
188 }
189
190 LOG(DEBUG) << __func__ << " dlopen lib:" << libName << " for uuid:\ntype:" << type.toString()
191 << "\nimpl:" << impl.toString()
192 << "\nproxy:" << (proxy.has_value() ? proxy.value().toString() : "null")
193 << "\nhandle:" << libHandle;
194 mEffectLibMap.insert({impl, std::make_pair(std::move(libHandle), nullptr)});
195
196 Descriptor::Identity id;
197 id.type = type;
198 id.uuid = impl;
199 if (proxy.has_value()) {
200 id.proxy = proxy.value();
201 }
202 mIdentityList.push_back(id);
203}
204
Shunkai Yaoc23916b2022-07-13 04:59:37 +0000205} // namespace aidl::android::hardware::audio::effect