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
17 #define LOG_TAG "VtsHalAGC1ParamTest"
18 #include <android-base/logging.h>
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;
26 using aidl::android::hardware::audio::effect::getEffectTypeUuidAutomaticGainControlV1;
27 using aidl::android::hardware::audio::effect::IEffect;
28 using aidl::android::hardware::audio::effect::IFactory;
29 using aidl::android::hardware::audio::effect::Parameter;
30 using android::hardware::audio::common::testing::detail::TestExecutionTracer;
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:
AGC1ParamTest()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
SetUp()51 void SetUp() override {
52 ASSERT_NE(nullptr, mFactory);
53 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
54
55 Parameter::Specific specific = getDefaultParamSpecific();
56 Parameter::Common common = createParamCommon(
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
TearDown()64 void TearDown() override {
65 ASSERT_NO_FATAL_FAILURE(close(mEffect));
66 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
67 }
68
getDefaultParamSpecific()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
SetAndGetParameters()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
addTargetPeakLevelParam(int targetPeakLevel)120 void addTargetPeakLevelParam(int targetPeakLevel) {
121 AutomaticGainControlV1 AGC1;
122 AGC1.set<AutomaticGainControlV1::targetPeakLevelDbFs>(targetPeakLevel);
123 mTags.push_back({AutomaticGainControlV1::targetPeakLevelDbFs, AGC1});
124 }
addMaxCompressionGainParam(int maxCompressionGainDb)125 void addMaxCompressionGainParam(int maxCompressionGainDb) {
126 AutomaticGainControlV1 AGC1;
127 AGC1.set<AutomaticGainControlV1::maxCompressionGainDb>(maxCompressionGainDb);
128 mTags.push_back({AutomaticGainControlV1::maxCompressionGainDb, AGC1});
129 }
addEnableLimiterParam(bool enableLimiter)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;
CleanUp()138 void CleanUp() { mTags.clear(); }
139 };
140
TEST_P(AGC1ParamTest,SetAndGetTargetPeakLevelParam)141 TEST_P(AGC1ParamTest, SetAndGetTargetPeakLevelParam) {
142 EXPECT_NO_FATAL_FAILURE(addTargetPeakLevelParam(mTargetPeakLevel));
143 SetAndGetParameters();
144 }
145
TEST_P(AGC1ParamTest,SetAndGetMaxCompressionGain)146 TEST_P(AGC1ParamTest, SetAndGetMaxCompressionGain) {
147 EXPECT_NO_FATAL_FAILURE(addMaxCompressionGainParam(mMaxCompressionGain));
148 SetAndGetParameters();
149 }
150
TEST_P(AGC1ParamTest,SetAndGetEnableLimiter)151 TEST_P(AGC1ParamTest, SetAndGetEnableLimiter) {
152 EXPECT_NO_FATAL_FAILURE(addEnableLimiterParam(mEnableLimiter));
153 SetAndGetParameters();
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(
161 IFactory::descriptor,
162 getEffectTypeUuidAutomaticGainControlV1())),
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()),
__anon98369a5f0102(const testing::TestParamInfo<AGC1ParamTest::ParamType>& info) 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
180 std::string name = getPrefix(descriptor) + "_target_peak_level_" + targetPeakLevel +
181 "_max_compression_gain_" + maxCompressionGain + "_enable_limiter_" +
182 enableLimiter;
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
main(int argc,char ** argv)190 int main(int argc, char** argv) {
191 ::testing::InitGoogleTest(&argc, argv);
192 ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
193 ABinderProcess_setThreadPoolMaxThreadCount(1);
194 ABinderProcess_startThreadPool();
195 return RUN_ALL_TESTS();
196 }
197