blob: a6fc1aa0e87685f3e9e92dec233b4aeb918fcd15 [file] [log] [blame]
Shraddha Basantwanicac2e682023-02-15 18:03:58 +05301/*
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#include <Utils.h>
18#include <aidl/Vintf.h>
19
20#define LOG_TAG "VtsHalAGC1ParamTest"
21
22#include "EffectHelper.h"
23
24using namespace android;
25
26using aidl::android::hardware::audio::effect::AutomaticGainControlV1;
27using aidl::android::hardware::audio::effect::Descriptor;
28using aidl::android::hardware::audio::effect::IEffect;
29using aidl::android::hardware::audio::effect::IFactory;
30using aidl::android::hardware::audio::effect::kAutomaticGainControlV1TypeUUID;
31using aidl::android::hardware::audio::effect::Parameter;
32
33enum ParamName {
34 PARAM_INSTANCE_NAME,
35 PARAM_TARGET_PEAK_LEVEL,
36 PARAM_MAX_COMPRESSION_GAIN,
37 PARAM_ENABLE_LIMITER
38};
39using AGC1ParamTestParam =
40 std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int /* targetPeakLevel */,
41 int /* maxCompressionGain */, bool /* enableLimiter */>;
42
43class AGC1ParamTest : public ::testing::TestWithParam<AGC1ParamTestParam>, public EffectHelper {
44 public:
45 AGC1ParamTest()
46 : mTargetPeakLevel(std::get<PARAM_TARGET_PEAK_LEVEL>(GetParam())),
47 mMaxCompressionGain(std::get<PARAM_MAX_COMPRESSION_GAIN>(GetParam())),
48 mEnableLimiter(std::get<PARAM_ENABLE_LIMITER>(GetParam())) {
49 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
50 }
51
52 void SetUp() override {
53 ASSERT_NE(nullptr, mFactory);
54 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
55
56 Parameter::Specific specific = getDefaultParamSpecific();
57 Parameter::Common common = EffectHelper::createParamCommon(
58 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
59 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
60 IEffect::OpenEffectReturn ret;
61 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE));
62 ASSERT_NE(nullptr, mEffect);
63 }
64
65 void TearDown() override {
66 ASSERT_NO_FATAL_FAILURE(close(mEffect));
67 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
68 }
69
70 Parameter::Specific getDefaultParamSpecific() {
71 AutomaticGainControlV1 AGC1 =
72 AutomaticGainControlV1::make<AutomaticGainControlV1::targetPeakLevelDbFs>(0);
73 Parameter::Specific specific =
74 Parameter::Specific::make<Parameter::Specific::automaticGainControlV1>(AGC1);
75 return specific;
76 }
77
78 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
79 std::shared_ptr<IFactory> mFactory;
80 std::shared_ptr<IEffect> mEffect;
81 Descriptor mDescriptor;
82 int mTargetPeakLevel;
83 int mMaxCompressionGain;
84 bool mEnableLimiter;
85
86 void SetAndGetParameters() {
87 for (auto& it : mTags) {
88 auto& tag = it.first;
89 auto& AGC1 = it.second;
90
91 // validate parameter
92 Descriptor desc;
93 ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
94 const bool valid =
95 isParameterValid<AutomaticGainControlV1, Range::automaticGainControlV1>(AGC1,
96 desc);
97 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
98
99 // set parameter
100 Parameter expectParam;
101 Parameter::Specific specific;
102 specific.set<Parameter::Specific::automaticGainControlV1>(AGC1);
103 expectParam.set<Parameter::specific>(specific);
104 EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
105
106 // only get if parameter in range and set success
107 if (expected == EX_NONE) {
108 Parameter getParam;
109 Parameter::Id id;
110 AutomaticGainControlV1::Id specificId;
111 specificId.set<AutomaticGainControlV1::Id::commonTag>(tag);
112 id.set<Parameter::Id::automaticGainControlV1Tag>(specificId);
113 EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam));
114
115 EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString()
116 << "\ngetParam:" << getParam.toString();
117 }
118 }
119 }
120
121 void addTargetPeakLevelParam(int targetPeakLevel) {
122 AutomaticGainControlV1 AGC1;
123 AGC1.set<AutomaticGainControlV1::targetPeakLevelDbFs>(targetPeakLevel);
124 mTags.push_back({AutomaticGainControlV1::targetPeakLevelDbFs, AGC1});
125 }
126 void addMaxCompressionGainParam(int maxCompressionGainDb) {
127 AutomaticGainControlV1 AGC1;
128 AGC1.set<AutomaticGainControlV1::maxCompressionGainDb>(maxCompressionGainDb);
129 mTags.push_back({AutomaticGainControlV1::maxCompressionGainDb, AGC1});
130 }
131 void addEnableLimiterParam(bool enableLimiter) {
132 AutomaticGainControlV1 AGC1;
133 AGC1.set<AutomaticGainControlV1::enableLimiter>(enableLimiter);
134 mTags.push_back({AutomaticGainControlV1::enableLimiter, AGC1});
135 }
136
137 private:
138 std::vector<std::pair<AutomaticGainControlV1::Tag, AutomaticGainControlV1>> mTags;
139 void CleanUp() { mTags.clear(); }
140};
141
142TEST_P(AGC1ParamTest, SetAndGetTargetPeakLevelParam) {
143 EXPECT_NO_FATAL_FAILURE(addTargetPeakLevelParam(mTargetPeakLevel));
144 SetAndGetParameters();
145}
146
147TEST_P(AGC1ParamTest, SetAndGetMaxCompressionGain) {
148 EXPECT_NO_FATAL_FAILURE(addMaxCompressionGainParam(mMaxCompressionGain));
149 SetAndGetParameters();
150}
151
152TEST_P(AGC1ParamTest, SetAndGetEnableLimiter) {
153 EXPECT_NO_FATAL_FAILURE(addEnableLimiterParam(mEnableLimiter));
154 SetAndGetParameters();
155}
156
157std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
158INSTANTIATE_TEST_SUITE_P(
159 AGC1ParamTest, AGC1ParamTest,
160 ::testing::Combine(
161 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
162 IFactory::descriptor, kAutomaticGainControlV1TypeUUID)),
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
180 std::string name = "Implementor_" + descriptor.common.implementor + "_name_" +
181 descriptor.common.name + "_UUID_" +
182 descriptor.common.id.uuid.toString() + "_target_peak_level_" +
183 targetPeakLevel + "_max_compression_gain_" + maxCompressionGain +
184 "_enable_limiter_" + enableLimiter;
185 std::replace_if(
186 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
187 return name;
188 });
189
190GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AGC1ParamTest);
191
192int main(int argc, char** argv) {
193 ::testing::InitGoogleTest(&argc, argv);
194 ABinderProcess_setThreadPoolMaxThreadCount(1);
195 ABinderProcess_startThreadPool();
196 return RUN_ALL_TESTS();
197}