Shraddha Basantwani | cac2e68 | 2023-02-15 18:03:58 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | |
Shraddha Basantwani | cac2e68 | 2023-02-15 18:03:58 +0530 | [diff] [blame] | 17 | #define LOG_TAG "VtsHalAGC1ParamTest" |
Mikhail Naganov | 872d4a6 | 2023-03-09 18:19:01 -0800 | [diff] [blame] | 18 | #include <android-base/logging.h> |
Shraddha Basantwani | cac2e68 | 2023-02-15 18:03:58 +0530 | [diff] [blame] | 19 | |
| 20 | #include "EffectHelper.h" |
| 21 | |
| 22 | using namespace android; |
| 23 | |
| 24 | using aidl::android::hardware::audio::effect::AutomaticGainControlV1; |
| 25 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 26 | using aidl::android::hardware::audio::effect::getEffectTypeUuidAutomaticGainControlV1; |
Shraddha Basantwani | cac2e68 | 2023-02-15 18:03:58 +0530 | [diff] [blame] | 27 | using aidl::android::hardware::audio::effect::IEffect; |
| 28 | using aidl::android::hardware::audio::effect::IFactory; |
Shraddha Basantwani | cac2e68 | 2023-02-15 18:03:58 +0530 | [diff] [blame] | 29 | using aidl::android::hardware::audio::effect::Parameter; |
Jaideep Sharma | 7449841 | 2023-09-13 15:25:25 +0530 | [diff] [blame] | 30 | using android::hardware::audio::common::testing::detail::TestExecutionTracer; |
Shraddha Basantwani | cac2e68 | 2023-02-15 18:03:58 +0530 | [diff] [blame] | 31 | |
| 32 | enum ParamName { |
| 33 | PARAM_INSTANCE_NAME, |
| 34 | PARAM_TARGET_PEAK_LEVEL, |
| 35 | PARAM_MAX_COMPRESSION_GAIN, |
| 36 | PARAM_ENABLE_LIMITER |
| 37 | }; |
| 38 | using AGC1ParamTestParam = |
| 39 | std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int /* targetPeakLevel */, |
| 40 | int /* maxCompressionGain */, bool /* enableLimiter */>; |
| 41 | |
| 42 | class AGC1ParamTest : public ::testing::TestWithParam<AGC1ParamTestParam>, public EffectHelper { |
| 43 | public: |
| 44 | AGC1ParamTest() |
| 45 | : mTargetPeakLevel(std::get<PARAM_TARGET_PEAK_LEVEL>(GetParam())), |
| 46 | mMaxCompressionGain(std::get<PARAM_MAX_COMPRESSION_GAIN>(GetParam())), |
| 47 | mEnableLimiter(std::get<PARAM_ENABLE_LIMITER>(GetParam())) { |
| 48 | std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam()); |
| 49 | } |
| 50 | |
| 51 | void SetUp() override { |
| 52 | ASSERT_NE(nullptr, mFactory); |
| 53 | ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor)); |
| 54 | |
| 55 | Parameter::Specific specific = getDefaultParamSpecific(); |
Shunkai Yao | 61f9dd2 | 2024-05-08 22:34:36 +0000 | [diff] [blame] | 56 | Parameter::Common common = createParamCommon( |
Shraddha Basantwani | cac2e68 | 2023-02-15 18:03:58 +0530 | [diff] [blame] | 57 | 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */, |
| 58 | kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */); |
| 59 | IEffect::OpenEffectReturn ret; |
| 60 | ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE)); |
| 61 | ASSERT_NE(nullptr, mEffect); |
| 62 | } |
| 63 | |
| 64 | void TearDown() override { |
| 65 | ASSERT_NO_FATAL_FAILURE(close(mEffect)); |
| 66 | ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect)); |
| 67 | } |
| 68 | |
| 69 | Parameter::Specific getDefaultParamSpecific() { |
| 70 | AutomaticGainControlV1 AGC1 = |
| 71 | AutomaticGainControlV1::make<AutomaticGainControlV1::targetPeakLevelDbFs>(0); |
| 72 | Parameter::Specific specific = |
| 73 | Parameter::Specific::make<Parameter::Specific::automaticGainControlV1>(AGC1); |
| 74 | return specific; |
| 75 | } |
| 76 | |
| 77 | static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100; |
| 78 | std::shared_ptr<IFactory> mFactory; |
| 79 | std::shared_ptr<IEffect> mEffect; |
| 80 | Descriptor mDescriptor; |
| 81 | int mTargetPeakLevel; |
| 82 | int mMaxCompressionGain; |
| 83 | bool mEnableLimiter; |
| 84 | |
| 85 | void SetAndGetParameters() { |
| 86 | for (auto& it : mTags) { |
| 87 | auto& tag = it.first; |
| 88 | auto& AGC1 = it.second; |
| 89 | |
| 90 | // validate parameter |
| 91 | Descriptor desc; |
| 92 | ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc)); |
| 93 | const bool valid = |
| 94 | isParameterValid<AutomaticGainControlV1, Range::automaticGainControlV1>(AGC1, |
| 95 | desc); |
| 96 | const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT; |
| 97 | |
| 98 | // set parameter |
| 99 | Parameter expectParam; |
| 100 | Parameter::Specific specific; |
| 101 | specific.set<Parameter::Specific::automaticGainControlV1>(AGC1); |
| 102 | expectParam.set<Parameter::specific>(specific); |
| 103 | EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString(); |
| 104 | |
| 105 | // only get if parameter in range and set success |
| 106 | if (expected == EX_NONE) { |
| 107 | Parameter getParam; |
| 108 | Parameter::Id id; |
| 109 | AutomaticGainControlV1::Id specificId; |
| 110 | specificId.set<AutomaticGainControlV1::Id::commonTag>(tag); |
| 111 | id.set<Parameter::Id::automaticGainControlV1Tag>(specificId); |
| 112 | EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam)); |
| 113 | |
| 114 | EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString() |
| 115 | << "\ngetParam:" << getParam.toString(); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void addTargetPeakLevelParam(int targetPeakLevel) { |
| 121 | AutomaticGainControlV1 AGC1; |
| 122 | AGC1.set<AutomaticGainControlV1::targetPeakLevelDbFs>(targetPeakLevel); |
| 123 | mTags.push_back({AutomaticGainControlV1::targetPeakLevelDbFs, AGC1}); |
| 124 | } |
| 125 | void addMaxCompressionGainParam(int maxCompressionGainDb) { |
| 126 | AutomaticGainControlV1 AGC1; |
| 127 | AGC1.set<AutomaticGainControlV1::maxCompressionGainDb>(maxCompressionGainDb); |
| 128 | mTags.push_back({AutomaticGainControlV1::maxCompressionGainDb, AGC1}); |
| 129 | } |
| 130 | void addEnableLimiterParam(bool enableLimiter) { |
| 131 | AutomaticGainControlV1 AGC1; |
| 132 | AGC1.set<AutomaticGainControlV1::enableLimiter>(enableLimiter); |
| 133 | mTags.push_back({AutomaticGainControlV1::enableLimiter, AGC1}); |
| 134 | } |
| 135 | |
| 136 | private: |
| 137 | std::vector<std::pair<AutomaticGainControlV1::Tag, AutomaticGainControlV1>> mTags; |
| 138 | void CleanUp() { mTags.clear(); } |
| 139 | }; |
| 140 | |
| 141 | TEST_P(AGC1ParamTest, SetAndGetTargetPeakLevelParam) { |
Sneha Patil | 3587ae5 | 2025-01-06 05:59:08 +0000 | [diff] [blame] | 142 | addTargetPeakLevelParam(mTargetPeakLevel); |
Aayush Soni | 63d6e2a | 2025-03-04 13:37:28 +0000 | [diff] [blame] | 143 | ASSERT_NO_FATAL_FAILURE(SetAndGetParameters()); |
Shraddha Basantwani | cac2e68 | 2023-02-15 18:03:58 +0530 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | TEST_P(AGC1ParamTest, SetAndGetMaxCompressionGain) { |
Sneha Patil | 3587ae5 | 2025-01-06 05:59:08 +0000 | [diff] [blame] | 147 | addMaxCompressionGainParam(mMaxCompressionGain); |
Aayush Soni | 63d6e2a | 2025-03-04 13:37:28 +0000 | [diff] [blame] | 148 | ASSERT_NO_FATAL_FAILURE(SetAndGetParameters()); |
Shraddha Basantwani | cac2e68 | 2023-02-15 18:03:58 +0530 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | TEST_P(AGC1ParamTest, SetAndGetEnableLimiter) { |
Sneha Patil | 3587ae5 | 2025-01-06 05:59:08 +0000 | [diff] [blame] | 152 | addEnableLimiterParam(mEnableLimiter); |
Aayush Soni | 63d6e2a | 2025-03-04 13:37:28 +0000 | [diff] [blame] | 153 | ASSERT_NO_FATAL_FAILURE(SetAndGetParameters()); |
Shraddha Basantwani | cac2e68 | 2023-02-15 18:03:58 +0530 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair; |
| 157 | INSTANTIATE_TEST_SUITE_P( |
| 158 | AGC1ParamTest, AGC1ParamTest, |
| 159 | ::testing::Combine( |
| 160 | testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors( |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 161 | IFactory::descriptor, |
| 162 | getEffectTypeUuidAutomaticGainControlV1())), |
Shraddha Basantwani | cac2e68 | 2023-02-15 18:03:58 +0530 | [diff] [blame] | 163 | testing::ValuesIn(EffectHelper::getTestValueSet< |
| 164 | AutomaticGainControlV1, int, Range::automaticGainControlV1, |
| 165 | AutomaticGainControlV1::targetPeakLevelDbFs>( |
| 166 | kDescPair, EffectHelper::expandTestValueBasic<int>)), |
| 167 | testing::ValuesIn(EffectHelper::getTestValueSet< |
| 168 | AutomaticGainControlV1, int, Range::automaticGainControlV1, |
| 169 | AutomaticGainControlV1::maxCompressionGainDb>( |
| 170 | kDescPair, EffectHelper::expandTestValueBasic<int>)), |
| 171 | testing::Bool()), |
| 172 | [](const testing::TestParamInfo<AGC1ParamTest::ParamType>& info) { |
| 173 | auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second; |
| 174 | std::string targetPeakLevel = |
| 175 | std::to_string(std::get<PARAM_TARGET_PEAK_LEVEL>(info.param)); |
| 176 | std::string maxCompressionGain = |
| 177 | std::to_string(std::get<PARAM_MAX_COMPRESSION_GAIN>(info.param)); |
| 178 | std::string enableLimiter = std::to_string(std::get<PARAM_ENABLE_LIMITER>(info.param)); |
| 179 | |
Shunkai Yao | 9e60e63 | 2023-06-23 04:34:50 +0000 | [diff] [blame] | 180 | std::string name = getPrefix(descriptor) + "_target_peak_level_" + targetPeakLevel + |
| 181 | "_max_compression_gain_" + maxCompressionGain + "_enable_limiter_" + |
| 182 | enableLimiter; |
Shraddha Basantwani | cac2e68 | 2023-02-15 18:03:58 +0530 | [diff] [blame] | 183 | std::replace_if( |
| 184 | name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_'); |
| 185 | return name; |
| 186 | }); |
| 187 | |
| 188 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AGC1ParamTest); |
| 189 | |
| 190 | int main(int argc, char** argv) { |
| 191 | ::testing::InitGoogleTest(&argc, argv); |
Jaideep Sharma | 7449841 | 2023-09-13 15:25:25 +0530 | [diff] [blame] | 192 | ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer()); |
Shraddha Basantwani | cac2e68 | 2023-02-15 18:03:58 +0530 | [diff] [blame] | 193 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 194 | ABinderProcess_startThreadPool(); |
| 195 | return RUN_ALL_TESTS(); |
| 196 | } |