|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 The Android Open Source Project |
| 3 | + * 2025 anonymix007 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <algorithm> |
| 19 | +#include <cstddef> |
| 20 | + |
| 21 | +#define LOG_TAG "ViPER4Aidl" |
| 22 | +#include <android-base/logging.h> |
| 23 | + |
| 24 | +#include <aidl/android/hardware/audio/effect/DefaultExtension.h> |
| 25 | +#include <fmq/AidlMessageQueue.h> |
| 26 | +#include <media/AidlConversionNdk.h> |
| 27 | +#include <system/audio_effects/effect_uuid.h> |
| 28 | + |
| 29 | +#include "viper/constants.h" |
| 30 | +#include "ViPER4Aidl.h" |
| 31 | + |
| 32 | +using aidl::android::hardware::audio::effect::Descriptor; |
| 33 | +using aidl::android::hardware::audio::effect::DefaultExtension; |
| 34 | +using aidl::android::hardware::audio::effect::ViperAidl; |
| 35 | +using aidl::android::hardware::audio::effect::getEffectImplUuidViper; |
| 36 | +using aidl::android::hardware::audio::effect::getEffectTypeUuidViper; |
| 37 | +using aidl::android::hardware::audio::effect::IEffect; |
| 38 | +using aidl::android::hardware::audio::effect::State; |
| 39 | +using aidl::android::hardware::audio::effect::VendorExtension; |
| 40 | +using aidl::android::media::audio::common::AudioUuid; |
| 41 | + |
| 42 | +extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 43 | + std::shared_ptr<IEffect>* instanceSpp) { |
| 44 | + if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidViper()) { |
| 45 | + LOG(ERROR) << __func__ << "uuid not supported"; |
| 46 | + return EX_ILLEGAL_ARGUMENT; |
| 47 | + } |
| 48 | + if (instanceSpp) { |
| 49 | + *instanceSpp = ndk::SharedRefBase::make<ViperAidl>(); |
| 50 | + LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created"; |
| 51 | + return EX_NONE; |
| 52 | + } else { |
| 53 | + LOG(ERROR) << __func__ << " invalid input parameter!"; |
| 54 | + return EX_ILLEGAL_ARGUMENT; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) { |
| 59 | + if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidViper()) { |
| 60 | + LOG(ERROR) << __func__ << "uuid not supported"; |
| 61 | + return EX_ILLEGAL_ARGUMENT; |
| 62 | + } |
| 63 | + *_aidl_return = ViperAidl::kDesc; |
| 64 | + return EX_NONE; |
| 65 | +} |
| 66 | + |
| 67 | +namespace aidl::android::hardware::audio::effect { |
| 68 | + |
| 69 | +const std::string ViperAidl::kEffectName = VIPER_NAME; |
| 70 | + |
| 71 | +const Descriptor ViperAidl::kDesc = {.common = {.id = {.type = getEffectTypeUuidViper(), |
| 72 | + .uuid = getEffectImplUuidViper()}, |
| 73 | + .flags = {.type = Flags::Type::INSERT, |
| 74 | + .insert = Flags::Insert::LAST, |
| 75 | + .volume = Flags::Volume::CTRL}, |
| 76 | + .name = ViperAidl::kEffectName, |
| 77 | + .implementor = VIPER_AUTHORS}}; |
| 78 | + |
| 79 | +ndk::ScopedAStatus ViperAidl::getDescriptor(Descriptor* _aidl_return) { |
| 80 | + LOG(DEBUG) << __func__ << kDesc.toString(); |
| 81 | + *_aidl_return = kDesc; |
| 82 | + return ndk::ScopedAStatus::ok(); |
| 83 | +} |
| 84 | + |
| 85 | +ndk::ScopedAStatus ViperAidl::setParameterSpecific(const Parameter::Specific& specific) { |
| 86 | + LOG(DEBUG) << __func__ << ": " << specific.toString(); |
| 87 | + RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 88 | + RETURN_IF(Parameter::Specific::vendorEffect != specific.getTag(), EX_ILLEGAL_ARGUMENT, "EffectNotSupported"); |
| 89 | + auto& vendorEffect = specific.get<Parameter::Specific::vendorEffect>(); |
| 90 | + std::optional<DefaultExtension> defaultExt; |
| 91 | + RETURN_IF(STATUS_OK != vendorEffect.extension.getParcelable(&defaultExt), EX_ILLEGAL_ARGUMENT, "getParcelableFailed"); |
| 92 | + RETURN_IF(!defaultExt.has_value(), EX_ILLEGAL_ARGUMENT, "parcelableNull"); |
| 93 | + |
| 94 | +#ifdef AIDL_DEBUG |
| 95 | + LOG(DEBUG) << __func__ << ": defaultExt: " << defaultExt->toString(); |
| 96 | +#endif |
| 97 | + |
| 98 | + int32_t ret = 0; |
| 99 | + uint32_t ret_size = sizeof(ret); |
| 100 | + RETURN_IF(mContext->handleCommand(EFFECT_CMD_SET_PARAM, defaultExt->bytes.size(), defaultExt->bytes.data(), &ret_size, &ret) != 0, EX_ILLEGAL_ARGUMENT, "handleCommandFailed"); |
| 101 | + RETURN_IF(ret != 0, EX_ILLEGAL_ARGUMENT, "handleCommandInternalFailed"); |
| 102 | + return ndk::ScopedAStatus::ok(); |
| 103 | +} |
| 104 | + |
| 105 | +ndk::ScopedAStatus ViperAidl::getParameterSpecific(const Parameter::Id& id, |
| 106 | + Parameter::Specific* specific) { |
| 107 | + LOG(DEBUG) << __func__ << ": " << specific->toString(); |
| 108 | + RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 109 | + RETURN_IF(Parameter::Id::vendorEffectTag != id.getTag(), EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
| 110 | + auto extensionId = id.get<Parameter::Id::vendorEffectTag>(); |
| 111 | + std::optional<DefaultExtension> defaultIdExt; |
| 112 | + RETURN_IF(STATUS_OK != extensionId.extension.getParcelable(&defaultIdExt), EX_ILLEGAL_ARGUMENT, "getIdParcelableFailed"); |
| 113 | + RETURN_IF(!defaultIdExt.has_value(), EX_ILLEGAL_ARGUMENT, "parcelableIdNull"); |
| 114 | + |
| 115 | +#ifdef AIDL_DEBUG |
| 116 | + LOG(DEBUG) << __func__ << ": defaultIdExt: " << defaultIdExt->toString(); |
| 117 | +#endif |
| 118 | + |
| 119 | + VendorExtension extension; |
| 120 | + DefaultExtension defaultExt; |
| 121 | + defaultExt.bytes.resize(sizeof(effect_param_t) + 2 * sizeof(int32_t)); |
| 122 | + uint32_t data_size = defaultExt.bytes.size(); |
| 123 | + RETURN_IF(mContext->handleCommand(EFFECT_CMD_GET_PARAM, defaultIdExt->bytes.size(), defaultIdExt->bytes.data(), &data_size, defaultExt.bytes.data()) != 0, EX_ILLEGAL_ARGUMENT, "handleCommandFailed"); |
| 124 | + assert(data_size <= defaultExt.bytes.size()); |
| 125 | + defaultExt.bytes.resize(data_size); |
| 126 | + |
| 127 | +#ifdef AIDL_DEBUG |
| 128 | + LOG(DEBUG) << __func__ << ": defaultExt: " << defaultExt.toString(); |
| 129 | +#endif |
| 130 | + |
| 131 | + RETURN_IF(STATUS_OK != extension.extension.setParcelable(defaultExt), EX_ILLEGAL_ARGUMENT, "setParcelableFailed"); |
| 132 | + specific->set<Parameter::Specific::vendorEffect>(extension); |
| 133 | + return ndk::ScopedAStatus::ok(); |
| 134 | +} |
| 135 | + |
| 136 | +std::shared_ptr<EffectContext> ViperAidl::createContext(const Parameter::Common& common) { |
| 137 | + if (mContext) { |
| 138 | + LOG(DEBUG) << __func__ << " context already exists"; |
| 139 | + } else { |
| 140 | + mContext = std::make_shared<ViperAidlContext>(1 /* statusFmqDepth */, common); |
| 141 | + int32_t ret = 0; |
| 142 | + uint32_t ret_size = sizeof(ret); |
| 143 | + int32_t status = mContext->handleCommand(EFFECT_CMD_INIT, 0, NULL, &ret_size, &ret); |
| 144 | + if (status < 0) { |
| 145 | + LOG(ERROR) << __func__ << ": ViperContext::handleCommand failed: " << status; |
| 146 | + mContext = nullptr; |
| 147 | + return mContext; |
| 148 | + } |
| 149 | + if (ret < 0) { |
| 150 | + LOG(ERROR) << __func__ << ": ViperContext::handleCommand failed (internal): " << ret; |
| 151 | + mContext = nullptr; |
| 152 | + return mContext; |
| 153 | + } |
| 154 | + ret = 0; |
| 155 | + ret_size = sizeof(ret); |
| 156 | + effect_config_t conf; |
| 157 | + |
| 158 | + if (!aidl2legacy_ParameterCommon_effect_config_t(common, conf)) { |
| 159 | + LOG(ERROR) << __func__ << ": JamesDSPAIDL::createContext: aidl2legacy_ParameterCommon_effect_config_t failed"; |
| 160 | + mContext = nullptr; |
| 161 | + return mContext; |
| 162 | + } |
| 163 | + |
| 164 | + status = mContext->handleCommand(EFFECT_CMD_SET_CONFIG, sizeof(conf), &conf, &ret_size, &ret); |
| 165 | + if (status < 0) { |
| 166 | + LOG(ERROR) << __func__ << ": ViperContext::handleCommand failed: " << status; |
| 167 | + mContext = nullptr; |
| 168 | + return mContext; |
| 169 | + } |
| 170 | + if (ret < 0) { |
| 171 | + LOG(ERROR) << __func__ << ": ViperContext::handleCommand failed (internal): " << ret; |
| 172 | + mContext = nullptr; |
| 173 | + return mContext; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return mContext; |
| 178 | +} |
| 179 | + |
| 180 | +ndk::ScopedAStatus ViperAidl::commandImpl(CommandId command) { |
| 181 | + RETURN_IF(!mImplContext, EX_NULL_POINTER, "nullContext"); |
| 182 | + int32_t ret = 0; |
| 183 | + uint32_t ret_size = sizeof(ret); |
| 184 | + switch (command) { |
| 185 | + case CommandId::START: |
| 186 | + RETURN_IF(mContext->handleCommand(EFFECT_CMD_ENABLE, 0, NULL, &ret_size, &ret) != 0, EX_ILLEGAL_ARGUMENT, "handleCommandFailed"); |
| 187 | + RETURN_IF(ret != 0, EX_ILLEGAL_ARGUMENT, "handleCommandInternalFailed"); |
| 188 | + break; |
| 189 | + case CommandId::STOP: |
| 190 | + RETURN_IF(mContext->handleCommand(EFFECT_CMD_DISABLE, 0, NULL, &ret_size, &ret) != 0, EX_ILLEGAL_ARGUMENT, "handleCommandFailed"); |
| 191 | + RETURN_IF(ret != 0, EX_ILLEGAL_ARGUMENT, "handleCommandInternalFailed"); |
| 192 | + break; |
| 193 | + case CommandId::RESET: |
| 194 | + mImplContext->resetBuffer(); |
| 195 | + break; |
| 196 | + default: |
| 197 | + break; |
| 198 | + } |
| 199 | + return ndk::ScopedAStatus::ok(); |
| 200 | +} |
| 201 | + |
| 202 | + |
| 203 | +RetCode ViperAidl::releaseContext() { |
| 204 | + if (mContext) { |
| 205 | + int32_t ret = 0; |
| 206 | + uint32_t ret_size = sizeof(ret); |
| 207 | + int32_t status = mContext->handleCommand(EFFECT_CMD_RESET, 0, NULL, &ret_size, &ret); |
| 208 | + if (status < 0) { |
| 209 | + LOG(ERROR) << __func__ << ": ViperContext::handleCommand failed: " << status; |
| 210 | + return RetCode::ERROR_ILLEGAL_PARAMETER; |
| 211 | + } |
| 212 | + if (ret < 0) { |
| 213 | + LOG(ERROR) << __func__ << ": ViperContext::handleCommand failed (internal): " << ret; |
| 214 | + return RetCode::ERROR_ILLEGAL_PARAMETER; |
| 215 | + } |
| 216 | + mContext.reset(); |
| 217 | + } |
| 218 | + return RetCode::SUCCESS; |
| 219 | +} |
| 220 | + |
| 221 | +// Processing method running in EffectWorker thread. |
| 222 | +IEffect::Status ViperAidl::effectProcessImpl(float* in_, float* out_, int samples) { |
| 223 | + size_t frames = static_cast<size_t>(samples) / 2; |
| 224 | + audio_buffer_t in{frames, {in_}}, out{frames, {out_}}; |
| 225 | + |
| 226 | +#ifdef AIDL_DEBUG |
| 227 | + LOG(DEBUG) << __func__ << " in " << in_ << " out " << out_ << " samples " << samples; |
| 228 | +#endif |
| 229 | + int32_t ret = mContext->process(&in, &out); |
| 230 | +#ifdef AIDL_DEBUG |
| 231 | + LOG(DEBUG) << __func__ << ": mContext->process: " << ret; |
| 232 | +#endif |
| 233 | + |
| 234 | + switch(ret) { |
| 235 | + case 0: |
| 236 | + return {STATUS_OK, samples, samples}; |
| 237 | + case -ENODATA: |
| 238 | + return {STATUS_NOT_ENOUGH_DATA, 0, 0}; |
| 239 | + default: |
| 240 | + return {STATUS_INVALID_OPERATION, 0, 0}; |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +} // namespace aidl::android::hardware::audio::effect |
0 commit comments