Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +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 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 18 | #include <cstddef> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 19 | #include <cstdint> |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 20 | #include <iterator> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 21 | #include <memory> |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 22 | #define LOG_TAG "EffectsFactoryHalAidl" |
| 23 | //#define LOG_NDEBUG 0 |
| 24 | |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame] | 25 | #include <error/expected_utils.h> |
Shunkai Yao | 8f6ad0f | 2023-04-18 23:14:25 +0000 | [diff] [blame] | 26 | #include <aidl/android/media/audio/common/AudioStreamType.h> |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 27 | #include <android/binder_manager.h> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 28 | #include <media/AidlConversionCppNdk.h> |
Shunkai Yao | a03533e | 2023-01-25 06:38:10 +0000 | [diff] [blame] | 29 | #include <media/AidlConversionEffect.h> |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame] | 30 | #include <system/audio.h> |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 31 | #include <utils/Log.h> |
| 32 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 33 | #include "EffectBufferHalAidl.h" |
| 34 | #include "EffectHalAidl.h" |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 35 | #include "EffectProxy.h" |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 36 | #include "EffectsFactoryHalAidl.h" |
| 37 | |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame] | 38 | using ::aidl::android::legacy2aidl_audio_uuid_t_AudioUuid; |
Shunkai Yao | 8f6ad0f | 2023-04-18 23:14:25 +0000 | [diff] [blame] | 39 | using ::aidl::android::aidl_utils::statusTFromBinderStatus; |
| 40 | using ::aidl::android::hardware::audio::effect::Descriptor; |
| 41 | using ::aidl::android::hardware::audio::effect::IFactory; |
| 42 | using ::aidl::android::hardware::audio::effect::Processing; |
Shunkai Yao | 0a51cf9 | 2023-05-10 22:42:56 +0000 | [diff] [blame] | 43 | using ::aidl::android::media::audio::common::AudioSource; |
| 44 | using ::aidl::android::media::audio::common::AudioStreamType; |
Shunkai Yao | 8f6ad0f | 2023-04-18 23:14:25 +0000 | [diff] [blame] | 45 | using ::aidl::android::media::audio::common::AudioUuid; |
| 46 | using ::android::base::unexpected; |
| 47 | using ::android::detail::AudioHalVersionInfo; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 48 | |
| 49 | namespace android { |
| 50 | namespace effect { |
| 51 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 52 | EffectsFactoryHalAidl::EffectsFactoryHalAidl(std::shared_ptr<IFactory> effectsFactory) |
| 53 | : mFactory(effectsFactory), |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 54 | mHalVersion(AudioHalVersionInfo( |
| 55 | AudioHalVersionInfo::Type::AIDL, |
| 56 | [this]() { |
| 57 | int32_t majorVersion = 0; |
| 58 | return (mFactory && mFactory->getInterfaceVersion(&majorVersion).isOk()) |
| 59 | ? majorVersion |
| 60 | : 0; |
| 61 | }())), |
| 62 | mHalDescList([this]() { |
| 63 | std::vector<Descriptor> list; |
| 64 | if (mFactory) { |
| 65 | mFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &list).isOk(); |
| 66 | } |
| 67 | return list; |
| 68 | }()), |
| 69 | mUuidProxyMap([this]() { |
| 70 | std::map<AudioUuid, std::shared_ptr<EffectProxy>> proxyMap; |
| 71 | for (const auto& desc : mHalDescList) { |
| 72 | // create EffectProxy |
| 73 | if (desc.common.id.proxy.has_value()) { |
| 74 | const auto& uuid = desc.common.id.proxy.value(); |
| 75 | if (0 == proxyMap.count(uuid)) { |
| 76 | proxyMap.insert({uuid, ndk::SharedRefBase::make<EffectProxy>(desc.common.id, |
| 77 | mFactory)}); |
| 78 | } |
| 79 | proxyMap[uuid]->addSubEffect(desc); |
| 80 | ALOGI("%s addSubEffect %s", __func__, desc.common.toString().c_str()); |
| 81 | } |
| 82 | } |
| 83 | return proxyMap; |
| 84 | }()), |
| 85 | mProxyDescList([this]() { |
| 86 | std::vector<Descriptor> list; |
| 87 | for (const auto& proxy : mUuidProxyMap) { |
| 88 | if (Descriptor desc; proxy.second && proxy.second->getDescriptor(&desc).isOk()) { |
| 89 | list.emplace_back(std::move(desc)); |
| 90 | } |
| 91 | } |
| 92 | return list; |
| 93 | }()), |
| 94 | mNonProxyDescList([this]() { |
| 95 | std::vector<Descriptor> list; |
| 96 | std::copy_if(mHalDescList.begin(), mHalDescList.end(), std::back_inserter(list), |
| 97 | [](const Descriptor& desc) { return !desc.common.id.proxy.has_value(); }); |
| 98 | return list; |
| 99 | }()), |
Shunkai Yao | 8f6ad0f | 2023-04-18 23:14:25 +0000 | [diff] [blame] | 100 | mEffectCount(mNonProxyDescList.size() + mProxyDescList.size()), |
Shunkai Yao | 0a51cf9 | 2023-05-10 22:42:56 +0000 | [diff] [blame] | 101 | mAidlProcessings([this]() -> std::vector<Processing> { |
| 102 | std::vector<Processing> processings; |
| 103 | if (!mFactory || !mFactory->queryProcessing(std::nullopt, &processings).isOk()) { |
| 104 | ALOGE("%s queryProcessing failed", __func__); |
| 105 | } |
| 106 | return processings; |
| 107 | }()) { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 108 | ALOG_ASSERT(mFactory != nullptr, "Provided IEffectsFactory service is NULL"); |
| 109 | ALOGI("%s with %zu nonProxyEffects and %zu proxyEffects", __func__, mNonProxyDescList.size(), |
| 110 | mProxyDescList.size()); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | status_t EffectsFactoryHalAidl::queryNumberEffects(uint32_t *pNumEffects) { |
| 114 | if (pNumEffects == nullptr) { |
| 115 | return BAD_VALUE; |
| 116 | } |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 117 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 118 | *pNumEffects = mEffectCount; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 119 | ALOGI("%s %d", __func__, *pNumEffects); |
| 120 | return OK; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | status_t EffectsFactoryHalAidl::getDescriptor(uint32_t index, effect_descriptor_t* pDescriptor) { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 124 | if (pDescriptor == nullptr) { |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 125 | return BAD_VALUE; |
| 126 | } |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 127 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 128 | if (index >= mEffectCount) { |
| 129 | ALOGE("%s index %d exceed max number %zu", __func__, index, mEffectCount); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 130 | return INVALID_OPERATION; |
| 131 | } |
| 132 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 133 | if (index >= mNonProxyDescList.size()) { |
| 134 | *pDescriptor = |
| 135 | VALUE_OR_RETURN_STATUS(::aidl::android::aidl2legacy_Descriptor_effect_descriptor( |
| 136 | mProxyDescList.at(index - mNonProxyDescList.size()))); |
| 137 | } else { |
| 138 | *pDescriptor = |
| 139 | VALUE_OR_RETURN_STATUS(::aidl::android::aidl2legacy_Descriptor_effect_descriptor( |
| 140 | mNonProxyDescList.at(index))); |
| 141 | } |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 142 | return OK; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 145 | status_t EffectsFactoryHalAidl::getDescriptor(const effect_uuid_t* halUuid, |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 146 | effect_descriptor_t* pDescriptor) { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 147 | if (halUuid == nullptr) { |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 148 | return BAD_VALUE; |
| 149 | } |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 150 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 151 | AudioUuid uuid = |
| 152 | VALUE_OR_RETURN_STATUS(::aidl::android::legacy2aidl_audio_uuid_t_AudioUuid(*halUuid)); |
| 153 | return getHalDescriptorWithImplUuid(uuid, pDescriptor); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 156 | status_t EffectsFactoryHalAidl::getDescriptors(const effect_uuid_t* halType, |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 157 | std::vector<effect_descriptor_t>* descriptors) { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 158 | if (halType == nullptr) { |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 159 | return BAD_VALUE; |
| 160 | } |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 161 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 162 | AudioUuid type = |
| 163 | VALUE_OR_RETURN_STATUS(::aidl::android::legacy2aidl_audio_uuid_t_AudioUuid(*halType)); |
| 164 | return getHalDescriptorWithTypeUuid(type, descriptors); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 167 | status_t EffectsFactoryHalAidl::createEffect(const effect_uuid_t* uuid, int32_t sessionId, |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 168 | int32_t ioId, int32_t deviceId __unused, |
| 169 | sp<EffectHalInterface>* effect) { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 170 | if (uuid == nullptr || effect == nullptr) { |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 171 | return BAD_VALUE; |
| 172 | } |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame] | 173 | if (sessionId == AUDIO_SESSION_DEVICE && ioId == AUDIO_IO_HANDLE_NONE) { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 174 | return INVALID_OPERATION; |
| 175 | } |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame] | 176 | ALOGI("%s session %d ioId %d", __func__, sessionId, ioId); |
| 177 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 178 | AudioUuid aidlUuid = |
| 179 | VALUE_OR_RETURN_STATUS(::aidl::android::legacy2aidl_audio_uuid_t_AudioUuid(*uuid)); |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame] | 180 | std::shared_ptr<IEffect> aidlEffect; |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 181 | // Use EffectProxy interface instead of IFactory to create |
| 182 | const bool isProxy = isProxyEffect(aidlUuid); |
| 183 | if (isProxy) { |
| 184 | aidlEffect = mUuidProxyMap.at(aidlUuid); |
| 185 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mUuidProxyMap.at(aidlUuid)->create())); |
| 186 | } else { |
| 187 | RETURN_STATUS_IF_ERROR( |
| 188 | statusTFromBinderStatus(mFactory->createEffect(aidlUuid, &aidlEffect))); |
| 189 | } |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame] | 190 | if (aidlEffect == nullptr) { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 191 | ALOGE("%s failed to create effect with UUID: %s", __func__, aidlUuid.toString().c_str()); |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame] | 192 | return NAME_NOT_FOUND; |
| 193 | } |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 194 | Descriptor desc; |
Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame] | 195 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aidlEffect->getDescriptor(&desc))); |
| 196 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 197 | uint64_t effectId; |
| 198 | { |
| 199 | std::lock_guard lg(mLock); |
| 200 | effectId = ++mEffectIdCounter; |
| 201 | } |
| 202 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 203 | *effect = |
| 204 | sp<EffectHalAidl>::make(mFactory, aidlEffect, effectId, sessionId, ioId, desc, isProxy); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 205 | return OK; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | status_t EffectsFactoryHalAidl::dumpEffects(int fd) { |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 209 | status_t ret = OK; |
| 210 | // record the error ret and continue dump as many effects as possible |
| 211 | for (const auto& proxy : mUuidProxyMap) { |
| 212 | if (proxy.second) { |
| 213 | if (status_t temp = proxy.second->dump(fd, nullptr, 0); temp != OK) { |
| 214 | ret = temp; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | RETURN_STATUS_IF_ERROR(mFactory->dump(fd, nullptr, 0)); |
| 219 | return ret; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | status_t EffectsFactoryHalAidl::allocateBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 223 | ALOGI("%s size %zu buffer %p", __func__, size, buffer); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 224 | return EffectBufferHalAidl::allocate(size, buffer); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | status_t EffectsFactoryHalAidl::mirrorBuffer(void* external, size_t size, |
| 228 | sp<EffectBufferHalInterface>* buffer) { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 229 | ALOGI("%s extern %p size %zu buffer %p", __func__, external, size, buffer); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 230 | return EffectBufferHalAidl::mirror(external, size, buffer); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | AudioHalVersionInfo EffectsFactoryHalAidl::getHalVersion() const { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 234 | return mHalVersion; |
| 235 | } |
| 236 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 237 | status_t EffectsFactoryHalAidl::getHalDescriptorWithImplUuid(const AudioUuid& uuid, |
| 238 | effect_descriptor_t* pDescriptor) { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 239 | if (pDescriptor == nullptr) { |
| 240 | return BAD_VALUE; |
| 241 | } |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 242 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 243 | const auto& list = isProxyEffect(uuid) ? mProxyDescList : mNonProxyDescList; |
| 244 | auto matchIt = std::find_if(list.begin(), list.end(), |
| 245 | [&](const auto& desc) { return desc.common.id.uuid == uuid; }); |
| 246 | if (matchIt == list.end()) { |
| 247 | ALOGE("%s UUID not found in HAL and proxy list %s", __func__, uuid.toString().c_str()); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 248 | return BAD_VALUE; |
| 249 | } |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 250 | ALOGI("%s UUID impl found %s", __func__, uuid.toString().c_str()); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 251 | |
| 252 | *pDescriptor = VALUE_OR_RETURN_STATUS( |
| 253 | ::aidl::android::aidl2legacy_Descriptor_effect_descriptor(*matchIt)); |
| 254 | return OK; |
| 255 | } |
| 256 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 257 | status_t EffectsFactoryHalAidl::getHalDescriptorWithTypeUuid( |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 258 | const AudioUuid& type, std::vector<effect_descriptor_t>* descriptors) { |
| 259 | if (descriptors == nullptr) { |
| 260 | return BAD_VALUE; |
| 261 | } |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 262 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 263 | std::vector<Descriptor> result; |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 264 | std::copy_if(mNonProxyDescList.begin(), mNonProxyDescList.end(), std::back_inserter(result), |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 265 | [&](auto& desc) { return desc.common.id.type == type; }); |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 266 | std::copy_if(mProxyDescList.begin(), mProxyDescList.end(), std::back_inserter(result), |
| 267 | [&](auto& desc) { return desc.common.id.type == type; }); |
| 268 | if (result.empty()) { |
| 269 | ALOGW("%s UUID type not found in HAL and proxy list %s", __func__, type.toString().c_str()); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 270 | return BAD_VALUE; |
| 271 | } |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 272 | ALOGI("%s UUID type found %zu \n %s", __func__, result.size(), type.toString().c_str()); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 273 | |
| 274 | *descriptors = VALUE_OR_RETURN_STATUS( |
| 275 | aidl::android::convertContainer<std::vector<effect_descriptor_t>>( |
| 276 | result, ::aidl::android::aidl2legacy_Descriptor_effect_descriptor)); |
| 277 | return OK; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Shunkai Yao | 5c71834 | 2023-02-23 23:49:51 +0000 | [diff] [blame] | 280 | bool EffectsFactoryHalAidl::isProxyEffect(const AudioUuid& uuid) const { |
| 281 | return 0 != mUuidProxyMap.count(uuid); |
| 282 | } |
| 283 | |
Shunkai Yao | 8f6ad0f | 2023-04-18 23:14:25 +0000 | [diff] [blame] | 284 | std::shared_ptr<const effectsConfig::Processings> EffectsFactoryHalAidl::getProcessings() const { |
Shunkai Yao | 0a51cf9 | 2023-05-10 22:42:56 +0000 | [diff] [blame] | 285 | |
| 286 | auto getConfigEffectWithDescriptor = |
| 287 | [](const auto& desc) -> std::shared_ptr<const effectsConfig::Effect> { |
| 288 | effectsConfig::Effect effect = {.name = desc.common.name, .isProxy = false}; |
| 289 | if (const auto uuid = |
| 290 | ::aidl::android::aidl2legacy_AudioUuid_audio_uuid_t(desc.common.id.uuid); |
| 291 | uuid.ok()) { |
| 292 | static_cast<effectsConfig::EffectImpl>(effect).uuid = uuid.value(); |
| 293 | return std::make_shared<const effectsConfig::Effect>(effect); |
| 294 | } else { |
| 295 | return nullptr; |
| 296 | } |
| 297 | }; |
| 298 | |
| 299 | auto getConfigProcessingWithAidlProcessing = |
| 300 | [&](const auto& aidlProcess, std::vector<effectsConfig::InputStream>& preprocess, |
| 301 | std::vector<effectsConfig::OutputStream>& postprocess) { |
| 302 | if (aidlProcess.type.getTag() == Processing::Type::streamType) { |
| 303 | AudioStreamType aidlType = |
| 304 | aidlProcess.type.template get<Processing::Type::streamType>(); |
| 305 | const auto type = |
| 306 | ::aidl::android::aidl2legacy_AudioStreamType_audio_stream_type_t( |
| 307 | aidlType); |
| 308 | if (!type.ok()) { |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | std::vector<std::shared_ptr<const effectsConfig::Effect>> effects; |
| 313 | std::transform(aidlProcess.ids.begin(), aidlProcess.ids.end(), |
| 314 | std::back_inserter(effects), getConfigEffectWithDescriptor); |
| 315 | effectsConfig::OutputStream stream = {.type = type.value(), |
| 316 | .effects = std::move(effects)}; |
| 317 | postprocess.emplace_back(stream); |
| 318 | } else if (aidlProcess.type.getTag() == Processing::Type::source) { |
| 319 | AudioSource aidlType = |
| 320 | aidlProcess.type.template get<Processing::Type::source>(); |
| 321 | const auto type = |
| 322 | ::aidl::android::aidl2legacy_AudioSource_audio_source_t(aidlType); |
| 323 | if (!type.ok()) { |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | std::vector<std::shared_ptr<const effectsConfig::Effect>> effects; |
| 328 | std::transform(aidlProcess.ids.begin(), aidlProcess.ids.end(), |
| 329 | std::back_inserter(effects), getConfigEffectWithDescriptor); |
| 330 | effectsConfig::InputStream stream = {.type = type.value(), |
| 331 | .effects = std::move(effects)}; |
| 332 | preprocess.emplace_back(stream); |
| 333 | } |
| 334 | }; |
| 335 | |
| 336 | static std::shared_ptr<const effectsConfig::Processings> processings( |
| 337 | [&]() -> std::shared_ptr<const effectsConfig::Processings> { |
| 338 | std::vector<effectsConfig::InputStream> preprocess; |
| 339 | std::vector<effectsConfig::OutputStream> postprocess; |
| 340 | for (const auto& processing : mAidlProcessings) { |
| 341 | getConfigProcessingWithAidlProcessing(processing, preprocess, postprocess); |
| 342 | } |
| 343 | |
| 344 | if (0 == preprocess.size() && 0 == postprocess.size()) { |
| 345 | return nullptr; |
| 346 | } |
| 347 | |
| 348 | return std::make_shared<const effectsConfig::Processings>( |
| 349 | effectsConfig::Processings({.preprocess = std::move(preprocess), |
| 350 | .postprocess = std::move(postprocess)})); |
| 351 | }()); |
| 352 | |
| 353 | return processings; |
Shunkai Yao | 8f6ad0f | 2023-04-18 23:14:25 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Shunkai Yao | 0a51cf9 | 2023-05-10 22:42:56 +0000 | [diff] [blame] | 356 | // Return 0 for AIDL, as the AIDL interface is not aware of the configuration file. |
Shunkai Yao | 8f6ad0f | 2023-04-18 23:14:25 +0000 | [diff] [blame] | 357 | ::android::error::Result<size_t> EffectsFactoryHalAidl::getSkippedElements() const { |
Shunkai Yao | 8f6ad0f | 2023-04-18 23:14:25 +0000 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 361 | } // namespace effect |
| 362 | |
| 363 | // When a shared library is built from a static library, even explicit |
| 364 | // exports from a static library are optimized out unless actually used by |
| 365 | // the shared library. See EffectsFactoryHalEntry.cpp. |
| 366 | extern "C" void* createIEffectsFactoryImpl() { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 367 | auto serviceName = std::string(IFactory::descriptor) + "/default"; |
| 368 | auto service = IFactory::fromBinder( |
| 369 | ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str()))); |
| 370 | if (!service) { |
| 371 | ALOGE("%s binder service %s not exist", __func__, serviceName.c_str()); |
| 372 | return nullptr; |
| 373 | } |
| 374 | return new effect::EffectsFactoryHalAidl(service); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | } // namespace android |