From 68041cab61ef0a58a1d976e92db64b3adcd1fbb0 Mon Sep 17 00:00:00 2001 From: Shraddha Basantwani Date: Fri, 4 Nov 2022 15:13:32 +0530 Subject: [PATCH] LoudnessEnhancer: Add AIDL implementation and its unit test Added AIDL implementation for LoudnessEnhancer audio effect parameters and its unit test. Bug: 258124419 Test: atest VtsHalLoudnessEnhancerTargetTest Change-Id: I2dfd304ca9f72383572fd1f762d41864dd73e39d --- .../loudnessEnhancer/LoudnessEnhancerSw.cpp | 52 ++++++- .../loudnessEnhancer/LoudnessEnhancerSw.h | 15 +- audio/aidl/vts/Android.bp | 6 + .../vts/VtsHalLoudnessEnhancerTargetTest.cpp | 145 ++++++++++++++++++ 4 files changed, 211 insertions(+), 7 deletions(-) create mode 100644 audio/aidl/vts/VtsHalLoudnessEnhancerTargetTest.cpp diff --git a/audio/aidl/default/loudnessEnhancer/LoudnessEnhancerSw.cpp b/audio/aidl/default/loudnessEnhancer/LoudnessEnhancerSw.cpp index 51645c761b..27b9169cef 100644 --- a/audio/aidl/default/loudnessEnhancer/LoudnessEnhancerSw.cpp +++ b/audio/aidl/default/loudnessEnhancer/LoudnessEnhancerSw.cpp @@ -76,16 +76,60 @@ ndk::ScopedAStatus LoudnessEnhancerSw::setParameterSpecific(const Parameter::Spe std::lock_guard lg(mMutex); RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); - mSpecificParam = specific.get(); - LOG(DEBUG) << __func__ << " success with: " << specific.toString(); - return ndk::ScopedAStatus::ok(); + auto& leParam = specific.get(); + auto tag = leParam.getTag(); + + switch (tag) { + case LoudnessEnhancer::gainMb: { + RETURN_IF(mContext->setLeGainMb(leParam.get()) != + RetCode::SUCCESS, + EX_ILLEGAL_ARGUMENT, "setGainMbFailed"); + return ndk::ScopedAStatus::ok(); + } + default: { + LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); + return ndk::ScopedAStatus::fromExceptionCodeWithMessage( + EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported"); + } + } } ndk::ScopedAStatus LoudnessEnhancerSw::getParameterSpecific(const Parameter::Id& id, Parameter::Specific* specific) { auto tag = id.getTag(); RETURN_IF(Parameter::Id::loudnessEnhancerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); - specific->set(mSpecificParam); + auto leId = id.get(); + auto leIdTag = leId.getTag(); + switch (leIdTag) { + case LoudnessEnhancer::Id::commonTag: + return getParameterLoudnessEnhancer(leId.get(), + specific); + default: + LOG(ERROR) << __func__ << " unsupported tag: " << toString(leIdTag); + return ndk::ScopedAStatus::fromExceptionCodeWithMessage( + EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported"); + } +} + +ndk::ScopedAStatus LoudnessEnhancerSw::getParameterLoudnessEnhancer( + const LoudnessEnhancer::Tag& tag, Parameter::Specific* specific) { + std::lock_guard lg(mMutex); + RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); + + LoudnessEnhancer leParam; + switch (tag) { + case LoudnessEnhancer::gainMb: { + leParam.set(mContext->getLeGainMb()); + break; + } + default: { + LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); + return ndk::ScopedAStatus::fromExceptionCodeWithMessage( + EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported"); + } + } + + specific->set(leParam); return ndk::ScopedAStatus::ok(); } diff --git a/audio/aidl/default/loudnessEnhancer/LoudnessEnhancerSw.h b/audio/aidl/default/loudnessEnhancer/LoudnessEnhancerSw.h index c0de9c12c9..478aebc1ae 100644 --- a/audio/aidl/default/loudnessEnhancer/LoudnessEnhancerSw.h +++ b/audio/aidl/default/loudnessEnhancer/LoudnessEnhancerSw.h @@ -32,7 +32,16 @@ class LoudnessEnhancerSwContext final : public EffectContext { : EffectContext(statusDepth, common) { LOG(DEBUG) << __func__; } - // TODO: add specific context here + + RetCode setLeGainMb(int gainMb) { + // TODO : Add implementation to apply new gain + mGainMb = gainMb; + return RetCode::SUCCESS; + } + int getLeGainMb() const { return mGainMb; } + + private: + int mGainMb = 0; // Default Gain }; class LoudnessEnhancerSw final : public EffectImpl { @@ -66,7 +75,7 @@ class LoudnessEnhancerSw final : public EffectImpl { .name = "LoudnessEnhancerSw"}, .capability = Capability::make(kCapability)}; - /* parameters */ - LoudnessEnhancer mSpecificParam; + ndk::ScopedAStatus getParameterLoudnessEnhancer(const LoudnessEnhancer::Tag& tag, + Parameter::Specific* specific); }; } // namespace aidl::android::hardware::audio::effect diff --git a/audio/aidl/vts/Android.bp b/audio/aidl/vts/Android.bp index b15432ee3a..03e9fcaeee 100644 --- a/audio/aidl/vts/Android.bp +++ b/audio/aidl/vts/Android.bp @@ -70,3 +70,9 @@ cc_test { defaults: ["VtsHalAudioTargetTestDefaults"], srcs: ["VtsHalEqualizerTargetTest.cpp"], } + +cc_test { + name: "VtsHalLoudnessEnhancerTargetTest", + defaults: ["VtsHalAudioTargetTestDefaults"], + srcs: ["VtsHalLoudnessEnhancerTargetTest.cpp"], +} diff --git a/audio/aidl/vts/VtsHalLoudnessEnhancerTargetTest.cpp b/audio/aidl/vts/VtsHalLoudnessEnhancerTargetTest.cpp new file mode 100644 index 0000000000..2ad016d7df --- /dev/null +++ b/audio/aidl/vts/VtsHalLoudnessEnhancerTargetTest.cpp @@ -0,0 +1,145 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * 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 + +#define LOG_TAG "VtsHalLoudnessEnhancerTest" + +#include +#include "EffectHelper.h" + +using namespace android; + +using aidl::android::hardware::audio::effect::Capability; +using aidl::android::hardware::audio::effect::Descriptor; +using aidl::android::hardware::audio::effect::IEffect; +using aidl::android::hardware::audio::effect::IFactory; +using aidl::android::hardware::audio::effect::LoudnessEnhancer; +using aidl::android::hardware::audio::effect::LoudnessEnhancerTypeUUID; +using aidl::android::hardware::audio::effect::Parameter; + +/** + * Here we focus on specific parameter checking, general IEffect interfaces testing performed in + * VtsAudioEffectTargetTest. + */ +enum ParamName { PARAM_INSTANCE_NAME, PARAM_GAIN_MB }; +using LoudnessEnhancerParamTestParam = std::tuple; + +// Every int 32 bit value is a valid gain, so testing the corner cases and one regular value. +// TODO : Update the test values once range/capability is updated by implementation. +const std::vector kGainMbValues = {std::numeric_limits::min(), 100, + std::numeric_limits::max()}; + +class LoudnessEnhancerParamTest : public ::testing::TestWithParam, + public EffectHelper { + public: + LoudnessEnhancerParamTest() + : EffectHelper(std::get(GetParam())), + mParamGainMb(std::get(GetParam())) {} + + void SetUp() override { + CreateEffectsWithUUID(LoudnessEnhancerTypeUUID); + initParamCommonFormat(); + initParamCommon(); + initParamSpecific(); + OpenEffects(LoudnessEnhancerTypeUUID); + SCOPED_TRACE(testing::Message() << "gainMb: " << mParamGainMb); + } + + void TearDown() override { + CloseEffects(); + DestroyEffects(); + CleanUp(); + } + + int mParamGainMb = 0; + + void SetAndGetLoudnessEnhancerParameters() { + auto functor = [&](const std::shared_ptr& effect) { + for (auto& it : mTags) { + auto& tag = it.first; + auto& le = it.second; + + // set parameter + Parameter expectParam; + Parameter::Specific specific; + specific.set(le); + expectParam.set(specific); + // All values are valid, set parameter should succeed + EXPECT_STATUS(EX_NONE, effect->setParameter(expectParam)) << expectParam.toString(); + + // get parameter + Parameter getParam; + Parameter::Id id; + LoudnessEnhancer::Id leId; + leId.set(tag); + id.set(leId); + EXPECT_STATUS(EX_NONE, effect->getParameter(id, &getParam)); + + EXPECT_EQ(expectParam, getParam); + } + }; + EXPECT_NO_FATAL_FAILURE(ForEachEffect(functor)); + } + + void addGainMbParam(int gainMb) { + LoudnessEnhancer le; + le.set(gainMb); + mTags.push_back({LoudnessEnhancer::gainMb, le}); + } + + private: + std::vector> mTags; + + void initParamSpecific() { + LoudnessEnhancer le; + le.set(0); + Parameter::Specific specific; + specific.set(le); + setSpecific(specific); + } + + void CleanUp() { mTags.clear(); } +}; + +TEST_P(LoudnessEnhancerParamTest, SetAndGetGainMb) { + EXPECT_NO_FATAL_FAILURE(addGainMbParam(mParamGainMb)); + SetAndGetLoudnessEnhancerParameters(); +} + +INSTANTIATE_TEST_SUITE_P( + LoudnessEnhancerTest, LoudnessEnhancerParamTest, + ::testing::Combine( + testing::ValuesIn(android::getAidlHalInstanceNames(IFactory::descriptor)), + testing::ValuesIn(kGainMbValues)), + [](const testing::TestParamInfo& info) { + std::string instance = std::get(info.param); + std::string gainMb = std::to_string(std::get(info.param)); + + std::string name = instance + "_gainMb" + gainMb; + std::replace_if( + name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_'); + return name; + }); + +GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(LoudnessEnhancerParamTest); + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + ABinderProcess_setThreadPoolMaxThreadCount(1); + ABinderProcess_startThreadPool(); + return RUN_ALL_TESTS(); +}