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