blob: 0dcb8ee8ea550f1ba3c7a3738a8ca6c307aa169e [file] [log] [blame]
Shunkai Yaodca65ce2022-12-02 05:35:41 +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
Shunkai Yao51202502022-12-12 06:11:46 +000017#include <algorithm>
Shunkai Yao5c718342023-02-23 23:49:51 +000018#include <cstddef>
Shunkai Yao51202502022-12-12 06:11:46 +000019#include <cstdint>
Shunkai Yao5c718342023-02-23 23:49:51 +000020#include <iterator>
Shunkai Yao51202502022-12-12 06:11:46 +000021#include <memory>
Shunkai Yaodca65ce2022-12-02 05:35:41 +000022#define LOG_TAG "EffectsFactoryHalAidl"
23//#define LOG_NDEBUG 0
24
Shunkai Yao284bb0d2023-01-10 00:42:36 +000025#include <error/expected_utils.h>
Shunkai Yao8f6ad0f2023-04-18 23:14:25 +000026#include <aidl/android/media/audio/common/AudioStreamType.h>
Shunkai Yaodca65ce2022-12-02 05:35:41 +000027#include <android/binder_manager.h>
Shunkai Yao51202502022-12-12 06:11:46 +000028#include <media/AidlConversionCppNdk.h>
Shunkai Yaoa03533e2023-01-25 06:38:10 +000029#include <media/AidlConversionEffect.h>
Shunkai Yao284bb0d2023-01-10 00:42:36 +000030#include <system/audio.h>
Shunkai Yaodca65ce2022-12-02 05:35:41 +000031#include <utils/Log.h>
32
Shunkai Yao51202502022-12-12 06:11:46 +000033#include "EffectBufferHalAidl.h"
34#include "EffectHalAidl.h"
Shunkai Yao5c718342023-02-23 23:49:51 +000035#include "EffectProxy.h"
Shunkai Yaodca65ce2022-12-02 05:35:41 +000036#include "EffectsFactoryHalAidl.h"
37
Shunkai Yao284bb0d2023-01-10 00:42:36 +000038using ::aidl::android::legacy2aidl_audio_uuid_t_AudioUuid;
Shunkai Yao8f6ad0f2023-04-18 23:14:25 +000039using ::aidl::android::aidl_utils::statusTFromBinderStatus;
40using ::aidl::android::hardware::audio::effect::Descriptor;
41using ::aidl::android::hardware::audio::effect::IFactory;
42using ::aidl::android::hardware::audio::effect::Processing;
43using ::aidl::android::media::audio::common::AudioUuid;
44using ::android::base::unexpected;
45using ::android::detail::AudioHalVersionInfo;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000046
47namespace android {
48namespace effect {
49
Shunkai Yao51202502022-12-12 06:11:46 +000050EffectsFactoryHalAidl::EffectsFactoryHalAidl(std::shared_ptr<IFactory> effectsFactory)
51 : mFactory(effectsFactory),
Shunkai Yao5c718342023-02-23 23:49:51 +000052 mHalVersion(AudioHalVersionInfo(
53 AudioHalVersionInfo::Type::AIDL,
54 [this]() {
55 int32_t majorVersion = 0;
56 return (mFactory && mFactory->getInterfaceVersion(&majorVersion).isOk())
57 ? majorVersion
58 : 0;
59 }())),
60 mHalDescList([this]() {
61 std::vector<Descriptor> list;
62 if (mFactory) {
63 mFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &list).isOk();
64 }
65 return list;
66 }()),
67 mUuidProxyMap([this]() {
68 std::map<AudioUuid, std::shared_ptr<EffectProxy>> proxyMap;
69 for (const auto& desc : mHalDescList) {
70 // create EffectProxy
71 if (desc.common.id.proxy.has_value()) {
72 const auto& uuid = desc.common.id.proxy.value();
73 if (0 == proxyMap.count(uuid)) {
74 proxyMap.insert({uuid, ndk::SharedRefBase::make<EffectProxy>(desc.common.id,
75 mFactory)});
76 }
77 proxyMap[uuid]->addSubEffect(desc);
78 ALOGI("%s addSubEffect %s", __func__, desc.common.toString().c_str());
79 }
80 }
81 return proxyMap;
82 }()),
83 mProxyDescList([this]() {
84 std::vector<Descriptor> list;
85 for (const auto& proxy : mUuidProxyMap) {
86 if (Descriptor desc; proxy.second && proxy.second->getDescriptor(&desc).isOk()) {
87 list.emplace_back(std::move(desc));
88 }
89 }
90 return list;
91 }()),
92 mNonProxyDescList([this]() {
93 std::vector<Descriptor> list;
94 std::copy_if(mHalDescList.begin(), mHalDescList.end(), std::back_inserter(list),
95 [](const Descriptor& desc) { return !desc.common.id.proxy.has_value(); });
96 return list;
97 }()),
Shunkai Yao8f6ad0f2023-04-18 23:14:25 +000098 mEffectCount(mNonProxyDescList.size() + mProxyDescList.size()),
99 mEffectProcessings(nullptr /* TODO: add AIDL implementation */) {
Shunkai Yao5c718342023-02-23 23:49:51 +0000100 ALOG_ASSERT(mFactory != nullptr, "Provided IEffectsFactory service is NULL");
101 ALOGI("%s with %zu nonProxyEffects and %zu proxyEffects", __func__, mNonProxyDescList.size(),
102 mProxyDescList.size());
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000103}
104
105status_t EffectsFactoryHalAidl::queryNumberEffects(uint32_t *pNumEffects) {
106 if (pNumEffects == nullptr) {
107 return BAD_VALUE;
108 }
Shunkai Yao51202502022-12-12 06:11:46 +0000109
Shunkai Yao5c718342023-02-23 23:49:51 +0000110 *pNumEffects = mEffectCount;
Shunkai Yao51202502022-12-12 06:11:46 +0000111 ALOGI("%s %d", __func__, *pNumEffects);
112 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000113}
114
115status_t EffectsFactoryHalAidl::getDescriptor(uint32_t index, effect_descriptor_t* pDescriptor) {
Shunkai Yao51202502022-12-12 06:11:46 +0000116 if (pDescriptor == nullptr) {
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000117 return BAD_VALUE;
118 }
Shunkai Yao51202502022-12-12 06:11:46 +0000119
Shunkai Yao5c718342023-02-23 23:49:51 +0000120 if (index >= mEffectCount) {
121 ALOGE("%s index %d exceed max number %zu", __func__, index, mEffectCount);
Shunkai Yao51202502022-12-12 06:11:46 +0000122 return INVALID_OPERATION;
123 }
124
Shunkai Yao5c718342023-02-23 23:49:51 +0000125 if (index >= mNonProxyDescList.size()) {
126 *pDescriptor =
127 VALUE_OR_RETURN_STATUS(::aidl::android::aidl2legacy_Descriptor_effect_descriptor(
128 mProxyDescList.at(index - mNonProxyDescList.size())));
129 } else {
130 *pDescriptor =
131 VALUE_OR_RETURN_STATUS(::aidl::android::aidl2legacy_Descriptor_effect_descriptor(
132 mNonProxyDescList.at(index)));
133 }
Shunkai Yao51202502022-12-12 06:11:46 +0000134 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000135}
136
Shunkai Yao51202502022-12-12 06:11:46 +0000137status_t EffectsFactoryHalAidl::getDescriptor(const effect_uuid_t* halUuid,
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000138 effect_descriptor_t* pDescriptor) {
Shunkai Yao5c718342023-02-23 23:49:51 +0000139 if (halUuid == nullptr) {
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000140 return BAD_VALUE;
141 }
Shunkai Yao51202502022-12-12 06:11:46 +0000142
Shunkai Yao5c718342023-02-23 23:49:51 +0000143 AudioUuid uuid =
144 VALUE_OR_RETURN_STATUS(::aidl::android::legacy2aidl_audio_uuid_t_AudioUuid(*halUuid));
145 return getHalDescriptorWithImplUuid(uuid, pDescriptor);
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000146}
147
Shunkai Yao51202502022-12-12 06:11:46 +0000148status_t EffectsFactoryHalAidl::getDescriptors(const effect_uuid_t* halType,
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000149 std::vector<effect_descriptor_t>* descriptors) {
Shunkai Yao5c718342023-02-23 23:49:51 +0000150 if (halType == nullptr) {
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000151 return BAD_VALUE;
152 }
Shunkai Yao51202502022-12-12 06:11:46 +0000153
Shunkai Yao5c718342023-02-23 23:49:51 +0000154 AudioUuid type =
155 VALUE_OR_RETURN_STATUS(::aidl::android::legacy2aidl_audio_uuid_t_AudioUuid(*halType));
156 return getHalDescriptorWithTypeUuid(type, descriptors);
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000157}
158
Shunkai Yao51202502022-12-12 06:11:46 +0000159status_t EffectsFactoryHalAidl::createEffect(const effect_uuid_t* uuid, int32_t sessionId,
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000160 int32_t ioId, int32_t deviceId __unused,
161 sp<EffectHalInterface>* effect) {
Shunkai Yao51202502022-12-12 06:11:46 +0000162 if (uuid == nullptr || effect == nullptr) {
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000163 return BAD_VALUE;
164 }
Shunkai Yao284bb0d2023-01-10 00:42:36 +0000165 if (sessionId == AUDIO_SESSION_DEVICE && ioId == AUDIO_IO_HANDLE_NONE) {
Shunkai Yao51202502022-12-12 06:11:46 +0000166 return INVALID_OPERATION;
167 }
Shunkai Yao284bb0d2023-01-10 00:42:36 +0000168 ALOGI("%s session %d ioId %d", __func__, sessionId, ioId);
169
Shunkai Yao5c718342023-02-23 23:49:51 +0000170 AudioUuid aidlUuid =
171 VALUE_OR_RETURN_STATUS(::aidl::android::legacy2aidl_audio_uuid_t_AudioUuid(*uuid));
Shunkai Yao284bb0d2023-01-10 00:42:36 +0000172 std::shared_ptr<IEffect> aidlEffect;
Shunkai Yao5c718342023-02-23 23:49:51 +0000173 // Use EffectProxy interface instead of IFactory to create
174 const bool isProxy = isProxyEffect(aidlUuid);
175 if (isProxy) {
176 aidlEffect = mUuidProxyMap.at(aidlUuid);
177 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mUuidProxyMap.at(aidlUuid)->create()));
178 } else {
179 RETURN_STATUS_IF_ERROR(
180 statusTFromBinderStatus(mFactory->createEffect(aidlUuid, &aidlEffect)));
181 }
Shunkai Yao284bb0d2023-01-10 00:42:36 +0000182 if (aidlEffect == nullptr) {
Shunkai Yao5c718342023-02-23 23:49:51 +0000183 ALOGE("%s failed to create effect with UUID: %s", __func__, aidlUuid.toString().c_str());
Shunkai Yao284bb0d2023-01-10 00:42:36 +0000184 return NAME_NOT_FOUND;
185 }
Shunkai Yao5c718342023-02-23 23:49:51 +0000186 Descriptor desc;
Shunkai Yao284bb0d2023-01-10 00:42:36 +0000187 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aidlEffect->getDescriptor(&desc)));
188
Shunkai Yao51202502022-12-12 06:11:46 +0000189 uint64_t effectId;
190 {
191 std::lock_guard lg(mLock);
192 effectId = ++mEffectIdCounter;
193 }
194
Shunkai Yao5c718342023-02-23 23:49:51 +0000195 *effect =
196 sp<EffectHalAidl>::make(mFactory, aidlEffect, effectId, sessionId, ioId, desc, isProxy);
Shunkai Yao51202502022-12-12 06:11:46 +0000197 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000198}
199
200status_t EffectsFactoryHalAidl::dumpEffects(int fd) {
Shunkai Yao5c718342023-02-23 23:49:51 +0000201 status_t ret = OK;
202 // record the error ret and continue dump as many effects as possible
203 for (const auto& proxy : mUuidProxyMap) {
204 if (proxy.second) {
205 if (status_t temp = proxy.second->dump(fd, nullptr, 0); temp != OK) {
206 ret = temp;
207 }
208 }
209 }
210 RETURN_STATUS_IF_ERROR(mFactory->dump(fd, nullptr, 0));
211 return ret;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000212}
213
214status_t EffectsFactoryHalAidl::allocateBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) {
Shunkai Yao51202502022-12-12 06:11:46 +0000215 ALOGI("%s size %zu buffer %p", __func__, size, buffer);
Shunkai Yao51202502022-12-12 06:11:46 +0000216 return EffectBufferHalAidl::allocate(size, buffer);
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000217}
218
219status_t EffectsFactoryHalAidl::mirrorBuffer(void* external, size_t size,
220 sp<EffectBufferHalInterface>* buffer) {
Shunkai Yao51202502022-12-12 06:11:46 +0000221 ALOGI("%s extern %p size %zu buffer %p", __func__, external, size, buffer);
Shunkai Yao51202502022-12-12 06:11:46 +0000222 return EffectBufferHalAidl::mirror(external, size, buffer);
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000223}
224
225AudioHalVersionInfo EffectsFactoryHalAidl::getHalVersion() const {
Shunkai Yao51202502022-12-12 06:11:46 +0000226 return mHalVersion;
227}
228
Shunkai Yao5c718342023-02-23 23:49:51 +0000229status_t EffectsFactoryHalAidl::getHalDescriptorWithImplUuid(const AudioUuid& uuid,
230 effect_descriptor_t* pDescriptor) {
Shunkai Yao51202502022-12-12 06:11:46 +0000231 if (pDescriptor == nullptr) {
232 return BAD_VALUE;
233 }
Shunkai Yao51202502022-12-12 06:11:46 +0000234
Shunkai Yao5c718342023-02-23 23:49:51 +0000235 const auto& list = isProxyEffect(uuid) ? mProxyDescList : mNonProxyDescList;
236 auto matchIt = std::find_if(list.begin(), list.end(),
237 [&](const auto& desc) { return desc.common.id.uuid == uuid; });
238 if (matchIt == list.end()) {
239 ALOGE("%s UUID not found in HAL and proxy list %s", __func__, uuid.toString().c_str());
Shunkai Yao51202502022-12-12 06:11:46 +0000240 return BAD_VALUE;
241 }
Shunkai Yao5c718342023-02-23 23:49:51 +0000242 ALOGI("%s UUID impl found %s", __func__, uuid.toString().c_str());
Shunkai Yao51202502022-12-12 06:11:46 +0000243
244 *pDescriptor = VALUE_OR_RETURN_STATUS(
245 ::aidl::android::aidl2legacy_Descriptor_effect_descriptor(*matchIt));
246 return OK;
247}
248
Shunkai Yao5c718342023-02-23 23:49:51 +0000249status_t EffectsFactoryHalAidl::getHalDescriptorWithTypeUuid(
Shunkai Yao51202502022-12-12 06:11:46 +0000250 const AudioUuid& type, std::vector<effect_descriptor_t>* descriptors) {
251 if (descriptors == nullptr) {
252 return BAD_VALUE;
253 }
Shunkai Yao5c718342023-02-23 23:49:51 +0000254
Shunkai Yao51202502022-12-12 06:11:46 +0000255 std::vector<Descriptor> result;
Shunkai Yao5c718342023-02-23 23:49:51 +0000256 std::copy_if(mNonProxyDescList.begin(), mNonProxyDescList.end(), std::back_inserter(result),
Shunkai Yao51202502022-12-12 06:11:46 +0000257 [&](auto& desc) { return desc.common.id.type == type; });
Shunkai Yao5c718342023-02-23 23:49:51 +0000258 std::copy_if(mProxyDescList.begin(), mProxyDescList.end(), std::back_inserter(result),
259 [&](auto& desc) { return desc.common.id.type == type; });
260 if (result.empty()) {
261 ALOGW("%s UUID type not found in HAL and proxy list %s", __func__, type.toString().c_str());
Shunkai Yao51202502022-12-12 06:11:46 +0000262 return BAD_VALUE;
263 }
Shunkai Yao5c718342023-02-23 23:49:51 +0000264 ALOGI("%s UUID type found %zu \n %s", __func__, result.size(), type.toString().c_str());
Shunkai Yao51202502022-12-12 06:11:46 +0000265
266 *descriptors = VALUE_OR_RETURN_STATUS(
267 aidl::android::convertContainer<std::vector<effect_descriptor_t>>(
268 result, ::aidl::android::aidl2legacy_Descriptor_effect_descriptor));
269 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000270}
271
Shunkai Yao5c718342023-02-23 23:49:51 +0000272bool EffectsFactoryHalAidl::isProxyEffect(const AudioUuid& uuid) const {
273 return 0 != mUuidProxyMap.count(uuid);
274}
275
Shunkai Yao8f6ad0f2023-04-18 23:14:25 +0000276std::shared_ptr<const effectsConfig::Processings> EffectsFactoryHalAidl::getProcessings() const {
277 return mEffectProcessings;
278}
279
280::android::error::Result<size_t> EffectsFactoryHalAidl::getSkippedElements() const {
281 if (!mEffectProcessings) {
282 return ::android::base::unexpected(BAD_VALUE);
283 }
284
285 // Only return 0 for AIDL, because the AIDL interface doesn't aware of configuration file
286 return 0;
287}
288
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000289} // namespace effect
290
291// When a shared library is built from a static library, even explicit
292// exports from a static library are optimized out unless actually used by
293// the shared library. See EffectsFactoryHalEntry.cpp.
294extern "C" void* createIEffectsFactoryImpl() {
Shunkai Yao51202502022-12-12 06:11:46 +0000295 auto serviceName = std::string(IFactory::descriptor) + "/default";
296 auto service = IFactory::fromBinder(
297 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
298 if (!service) {
299 ALOGE("%s binder service %s not exist", __func__, serviceName.c_str());
300 return nullptr;
301 }
302 return new effect::EffectsFactoryHalAidl(service);
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000303}
304
305} // namespace android