Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement AIDL IEffect ViPER library #25

Draft
wants to merge 1 commit into
base: foss
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions Android.bp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
filegroup {
name: "v4aCommonFile",
srcs: [
// Main
"src/viper/ViPER.cpp",
"src/ViperContext.cpp",
// Effects
"src/viper/effects/AnalogX.cpp",
"src/viper/effects/ColorfulMusic.cpp",
"src/viper/effects/Convolver.cpp",
"src/viper/effects/Cure.cpp",
"src/viper/effects/DiffSurround.cpp",
"src/viper/effects/DynamicSystem.cpp",
"src/viper/effects/FETCompressor.cpp",
"src/viper/effects/IIRFilter.cpp",
"src/viper/effects/PlaybackGain.cpp",
"src/viper/effects/Reverberation.cpp",
"src/viper/effects/SoftwareLimiter.cpp",
"src/viper/effects/SpeakerCorrection.cpp",
"src/viper/effects/SpectrumExtend.cpp",
"src/viper/effects/TubeSimulator.cpp",
"src/viper/effects/VHE.cpp",
"src/viper/effects/ViPERBass.cpp",
"src/viper/effects/ViPERClarity.cpp",
"src/viper/effects/ViPERDDC.cpp",
// Utils
"src/viper/utils/AdaptiveBuffer.cpp",
"src/viper/utils/Biquad.cpp",
"src/viper/utils/CAllpassFilter.cpp",
"src/viper/utils/CCombFilter.cpp",
"src/viper/utils/CRevModel.cpp",
"src/viper/utils/Crossfeed.cpp",
"src/viper/utils/DepthSurround.cpp",
"src/viper/utils/DynamicBass.cpp",
"src/viper/utils/FIR.cpp",
"src/viper/utils/Harmonic.cpp",
"src/viper/utils/HiFi.cpp",
"src/viper/utils/HighShelf.cpp",
"src/viper/utils/IIR_1st.cpp",
"src/viper/utils/IIR_NOrder_BW_BP.cpp",
"src/viper/utils/IIR_NOrder_BW_LH.cpp",
"src/viper/utils/MinPhaseIIRCoeffs.cpp",
"src/viper/utils/MultiBiquad.cpp",
"src/viper/utils/NoiseSharpening.cpp",
"src/viper/utils/PassFilter.cpp",
"src/viper/utils/PConvSingle.cpp",
"src/viper/utils/PolesFilter.cpp",
"src/viper/utils/Polyphase.cpp",
"src/viper/utils/Stereo3DSurround.cpp",
"src/viper/utils/Subwoofer.cpp",
"src/viper/utils/TimeConstDelay.cpp",
"src/viper/utils/WaveBuffer.cpp",
],
}

cc_defaults {
name: "v4aDefaults",
srcs: [
":v4aCommonFile",
],
cflags: [
"-O3",
"-DNDEBUG",
"-DVIPER_VERSION=20240314",
"-DAOSP_SOONG_BUILD",
],
header_libs: [
"libhardware_headers",
],
lto: {
// TODO: full was removed in 0713e336d9898aad7c161e92b8d27096142b64b6 (build/soong)
thin: true,
},
}

cc_library_shared {
name: "libv4aidl_re",
vendor: true,
relative_install_path: "soundfx",
defaults: [
"aidlaudioeffectservice_defaults",
"v4aDefaults",
],
srcs: [
"src/ViPER4Aidl.cpp",
// AOSP
":effectCommonFile",
],
cflags: [
"-O3",
"-DNDEBUG",
"-DVIPER_VERSION=20240314",
"-DAOSP_SOONG_BUILD",
],
shared_libs: [
"libbinder",
],
whole_static_libs: [
"libaudio_aidl_conversion_common_ndk",
"libstagefright_foundation",
],
}

cc_library_shared {
name: "libv4a_re",
vendor: true,
relative_install_path: "soundfx",
defaults: [
"v4aDefaults",
],
srcs: [
"src/ViPER4Android.cpp",
],
shared_libs: [
"liblog",
],
}
234 changes: 234 additions & 0 deletions src/ViPER4Aidl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
/*
* Copyright (C) 2022 The Android Open Source Project
* 2024 anonymix007
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <algorithm>
#include <cstddef>

#define LOG_TAG "ViPER4Aidl"
#include <android-base/logging.h>
#include <fmq/AidlMessageQueue.h>
#include <system/audio_effects/effect_uuid.h>
#include <aidl/android/hardware/audio/effect/DefaultExtension.h>
#include <media/AidlConversionUtil.h>
template <typename T>
using ConversionResult = ::android::error::Result<T>;
#include <media/AidlConversionNdk.h>

#include "viper/constants.h"
#include "ViPER4Aidl.h"

using aidl::android::hardware::audio::effect::Descriptor;
using aidl::android::hardware::audio::effect::DefaultExtension;
using aidl::android::hardware::audio::effect::ViperAidl;
using aidl::android::hardware::audio::effect::getEffectImplUuidViper;
using aidl::android::hardware::audio::effect::getEffectUuidNull;
using aidl::android::hardware::audio::effect::IEffect;
using aidl::android::hardware::audio::effect::State;
using aidl::android::hardware::audio::effect::VendorExtension;
using aidl::android::media::audio::common::AudioUuid;

extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
std::shared_ptr<IEffect>* instanceSpp) {
if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidViper()) {
LOG(ERROR) << __func__ << "uuid not supported";
return EX_ILLEGAL_ARGUMENT;
}
if (instanceSpp) {
*instanceSpp = ndk::SharedRefBase::make<ViperAidl>();
LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
return EX_NONE;
} else {
LOG(ERROR) << __func__ << " invalid input parameter!";
return EX_ILLEGAL_ARGUMENT;
}
}

extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidViper()) {
LOG(ERROR) << __func__ << "uuid not supported";
return EX_ILLEGAL_ARGUMENT;
}
*_aidl_return = ViperAidl::kDesc;
return EX_NONE;
}

namespace aidl::android::hardware::audio::effect {

const std::string ViperAidl::kEffectName = VIPER_NAME;

const Descriptor ViperAidl::kDesc = {.common = {.id = {.type = getEffectUuidNull(),
.uuid = getEffectImplUuidViper()},
.flags = {.type = Flags::Type::INSERT,
.insert = Flags::Insert::LAST,
.volume = Flags::Volume::CTRL},
.name = ViperAidl::kEffectName,
.implementor = VIPER_AUTHORS}};

ndk::ScopedAStatus ViperAidl::getDescriptor(Descriptor* _aidl_return) {
LOG(DEBUG) << __func__ << kDesc.toString();
*_aidl_return = kDesc;
return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus ViperAidl::setParameterSpecific(const Parameter::Specific& specific) {
RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
// TODO: Handle Parameter::Common (EFFECT_CMD_SET_CONFIG)
RETURN_IF(Parameter::Specific::vendorEffect != specific.getTag(), EX_ILLEGAL_ARGUMENT, "EffectNotSupported");
auto& vendorEffect = specific.get<Parameter::Specific::vendorEffect>();
std::optional<DefaultExtension> defaultExt;
RETURN_IF(STATUS_OK != vendorEffect.extension.getParcelable(&defaultExt), EX_ILLEGAL_ARGUMENT, "getParcelableFailed");
RETURN_IF(!defaultExt.has_value(), EX_ILLEGAL_ARGUMENT, "parcelableNull");

int32_t ret = 0;
uint32_t ret_size = sizeof(ret);
RETURN_IF(mContext->viperContext->handleCommand(EFFECT_CMD_SET_PARAM, defaultExt->bytes.size(), defaultExt->bytes.data(), &ret_size, &ret) != 0, EX_ILLEGAL_ARGUMENT, "handleCommandFailed");
RETURN_IF(ret != 0, EX_ILLEGAL_ARGUMENT, "handleCommandInternalFailed");
return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus ViperAidl::getParameterSpecific(const Parameter::Id& id,
Parameter::Specific* specific) {
RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
// TODO: Handle Parameter::Common (EFFECT_CMD_GET_CONFIG)
RETURN_IF(Parameter::Id::vendorEffectTag != id.getTag(), EX_ILLEGAL_ARGUMENT, "wrongIdTag");
auto extensionId = id.get<Parameter::Id::vendorEffectTag>();
std::optional<DefaultExtension> defaultIdExt;
RETURN_IF(STATUS_OK != extensionId.extension.getParcelable(&defaultIdExt), EX_ILLEGAL_ARGUMENT, "getIdParcelableFailed");
RETURN_IF(!defaultIdExt.has_value(), EX_ILLEGAL_ARGUMENT, "parcelableIdNull");

VendorExtension extension;
DefaultExtension defaultExt;
defaultExt.bytes.resize(sizeof(effect_param_t));
uint32_t data_size = defaultExt.bytes.size();
RETURN_IF(mContext->viperContext->handleCommand(EFFECT_CMD_GET_PARAM, defaultIdExt->bytes.size(), defaultIdExt->bytes.data(), &data_size, defaultExt.bytes.data()) != 0, EX_ILLEGAL_ARGUMENT, "handleCommandFailed");
RETURN_IF(STATUS_OK != extension.extension.setParcelable(defaultExt), EX_ILLEGAL_ARGUMENT, "setParcelableFailed");
specific->set<Parameter::Specific::vendorEffect>(extension);
return ndk::ScopedAStatus::ok();
}

std::shared_ptr<EffectContext> ViperAidl::createContext(const Parameter::Common& common) {
if (mContext) {
LOG(DEBUG) << __func__ << " context already exist";
} else {
mContext = std::make_shared<ViperAidlContext>(1 /* statusFmqDepth */, common);
int32_t ret = 0;
uint32_t ret_size = sizeof(ret);
int32_t status = mContext->viperContext->handleCommand(EFFECT_CMD_INIT, 0, NULL, &ret_size, &ret);
if (status < 0) {
LOG(ERROR) << __func__ << ": ViperContext::handleCommand failed: " << status;
mContext = nullptr;
return mContext;
}
if (ret < 0) {
LOG(ERROR) << __func__ << ": ViperContext::handleCommand failed (internal): " << ret;
mContext = nullptr;
return mContext;
}
ret = 0;
ret_size = sizeof(ret);
effect_config_t conf = {};


auto tmp = ::aidl::android::aidl2legacy_AudioConfig_buffer_config_t(common.input, true);
if (tmp.ok()) {
conf.inputCfg = tmp.value();
} else {
mContext = nullptr;
return mContext;
}

tmp = ::aidl::android::aidl2legacy_AudioConfig_buffer_config_t(common.output, false);
if (tmp.ok()) {
conf.outputCfg = tmp.value();
} else {
mContext = nullptr;
return mContext;
}

status = mContext->viperContext->handleCommand(EFFECT_CMD_SET_CONFIG, sizeof(conf), &conf, &ret_size, &ret);
if (status < 0) {
LOG(ERROR) << __func__ << ": ViperContext::handleCommand failed: " << status;
mContext = nullptr;
return mContext;
}
if (ret < 0) {
LOG(ERROR) << __func__ << ": ViperContext::handleCommand failed (internal): " << ret;
mContext = nullptr;
return mContext;
}
}

return mContext;
}

ndk::ScopedAStatus ViperAidl::commandImpl(CommandId command) {
RETURN_IF(!mImplContext, EX_NULL_POINTER, "nullContext");
int32_t ret = 0;
uint32_t ret_size = sizeof(ret);
switch (command) {
case CommandId::START:
RETURN_IF(mContext->viperContext->handleCommand(EFFECT_CMD_ENABLE, 0, NULL, &ret_size, &ret) != 0, EX_ILLEGAL_ARGUMENT, "handleCommandFailed");
RETURN_IF(ret != 0, EX_ILLEGAL_ARGUMENT, "handleCommandInternalFailed");
break;
case CommandId::STOP:
RETURN_IF(mContext->viperContext->handleCommand(EFFECT_CMD_DISABLE, 0, NULL, &ret_size, &ret) != 0, EX_ILLEGAL_ARGUMENT, "handleCommandFailed");
RETURN_IF(ret != 0, EX_ILLEGAL_ARGUMENT, "handleCommandInternalFailed");
break;
case CommandId::RESET:
mImplContext->resetBuffer();
break;
default:
break;
}
return ndk::ScopedAStatus::ok();
}


RetCode ViperAidl::releaseContext() {
if (mContext) {
int32_t ret = 0;
uint32_t ret_size = sizeof(ret);
int32_t status = mContext->viperContext->handleCommand(EFFECT_CMD_RESET, 0, NULL, &ret_size, &ret);
if (status < 0) {
LOG(ERROR) << __func__ << ": ViperContext::handleCommand failed: " << status;
return RetCode::ERROR_ILLEGAL_PARAMETER;
}
if (ret < 0) {
LOG(ERROR) << __func__ << ": ViperContext::handleCommand failed (internal): " << ret;
return RetCode::ERROR_ILLEGAL_PARAMETER;
}
mContext.reset();
}
return RetCode::SUCCESS;
}

// Processing method running in EffectWorker thread.
IEffect::Status ViperAidl::effectProcessImpl(float* in_, float* out_, int samples) {
audio_buffer_t in{static_cast<size_t>(samples), {in_}}, out{static_cast<size_t>(samples), {out_}};
LOG(DEBUG) << __func__ << " in " << in_ << " out " << out_ << " samples " << samples;

switch(mContext->viperContext->process(&in, &out)) {
case 0:
return {STATUS_OK, samples, samples};
case -ENODATA:
return {STATUS_NOT_ENOUGH_DATA, 0, 0};
default:
return {STATUS_INVALID_OPERATION, 0, 0};
}
}

} // namespace aidl::android::hardware::audio::effect
Loading