blob: 484cc8491b2e5c4c74ff3d282079771b12964492 [file] [log] [blame]
Shunkai Yao5bd4a302022-12-20 15:46:24 +00001/*
2 * Copyright (C) 2022 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
Shunkai Yaoab59e6d2022-12-22 00:45:23 +000017#include <Utils.h>
Shunkai Yao5bd4a302022-12-20 15:46:24 +000018#include <aidl/Vintf.h>
Shunkai Yaoab59e6d2022-12-22 00:45:23 +000019#include <android/binder_enums.h>
20#include <unordered_set>
Shunkai Yao5bd4a302022-12-20 15:46:24 +000021
22#define LOG_TAG "VtsHalAGCParamTest"
23
Shunkai Yao5bd4a302022-12-20 15:46:24 +000024#include "EffectHelper.h"
25
26using namespace android;
27
28using aidl::android::hardware::audio::effect::AutomaticGainControl;
29using aidl::android::hardware::audio::effect::Capability;
30using aidl::android::hardware::audio::effect::Descriptor;
31using aidl::android::hardware::audio::effect::IEffect;
32using aidl::android::hardware::audio::effect::IFactory;
33using aidl::android::hardware::audio::effect::kAutomaticGainControlTypeUUID;
34using aidl::android::hardware::audio::effect::Parameter;
35
36enum ParamName {
37 PARAM_INSTANCE_NAME,
38 PARAM_DIGITAL_GAIN,
39 PARAM_SATURATION_MARGIN,
40 PARAM_LEVEL_ESTIMATOR
41};
42using AGCParamTestParam =
43 std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int /* gain */,
44 int /* margin */, AutomaticGainControl::LevelEstimator>;
45
46class AGCParamTest : public ::testing::TestWithParam<AGCParamTestParam>, public EffectHelper {
47 public:
48 AGCParamTest()
49 : mGain(std::get<PARAM_DIGITAL_GAIN>(GetParam())),
50 mMargin(std::get<PARAM_SATURATION_MARGIN>(GetParam())),
51 mLevelEstimator(std::get<PARAM_LEVEL_ESTIMATOR>(GetParam())) {
52 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
53 }
54
55 void SetUp() override {
56 ASSERT_NE(nullptr, mFactory);
57 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
58
59 Parameter::Specific specific = getDefaultParamSpecific();
60 Parameter::Common common = EffectHelper::createParamCommon(
61 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
62 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
63 IEffect::OpenEffectReturn ret;
64 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE));
65 ASSERT_NE(nullptr, mEffect);
66 }
67
68 void TearDown() override {
69 ASSERT_NO_FATAL_FAILURE(close(mEffect));
70 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
71 }
72
73 Parameter::Specific getDefaultParamSpecific() {
74 AutomaticGainControl AGC =
75 AutomaticGainControl::make<AutomaticGainControl::fixedDigitalGainMb>(0);
76 Parameter::Specific specific =
77 Parameter::Specific::make<Parameter::Specific::automaticGainControl>(AGC);
78 return specific;
79 }
80
81 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
82 static const std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kFactoryDescList;
Shunkai Yaoab59e6d2022-12-22 00:45:23 +000083 static const std::unordered_set<int> kDigitalGainValues;
84 static const std::unordered_set<int> kSaturationMarginValues;
85 static const std::unordered_set<AutomaticGainControl::LevelEstimator> kLevelEstimatorValues;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000086
87 std::shared_ptr<IFactory> mFactory;
88 std::shared_ptr<IEffect> mEffect;
89 Descriptor mDescriptor;
90 int mGain;
91 int mMargin;
92 AutomaticGainControl::LevelEstimator mLevelEstimator;
93
94 void SetAndGetParameters() {
95 for (auto& it : mTags) {
96 auto& tag = it.first;
97 auto& AGC = it.second;
98
99 // validate parameter
100 Descriptor desc;
101 ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
102 const bool valid = isTagInRange(tag, AGC, desc);
103 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
104
105 // set parameter
106 Parameter expectParam;
107 Parameter::Specific specific;
108 specific.set<Parameter::Specific::automaticGainControl>(AGC);
109 expectParam.set<Parameter::specific>(specific);
110 EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
111
112 // only get if parameter in range and set success
113 if (expected == EX_NONE) {
114 Parameter getParam;
115 Parameter::Id id;
116 AutomaticGainControl::Id specificId;
117 specificId.set<AutomaticGainControl::Id::commonTag>(tag);
118 id.set<Parameter::Id::automaticGainControlTag>(specificId);
119 EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam));
120
121 EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString()
122 << "\ngetParam:" << getParam.toString();
123 }
124 }
125 }
126
127 void addDigitalGainParam(int gain) {
128 AutomaticGainControl AGC;
129 AGC.set<AutomaticGainControl::fixedDigitalGainMb>(gain);
130 mTags.push_back({AutomaticGainControl::fixedDigitalGainMb, AGC});
131 }
132 void addSaturationMarginParam(int margin) {
133 AutomaticGainControl AGC;
134 AGC.set<AutomaticGainControl::saturationMarginMb>(margin);
135 mTags.push_back({AutomaticGainControl::saturationMarginMb, AGC});
136 }
137 void addLevelEstimatorParam(AutomaticGainControl::LevelEstimator levelEstimator) {
138 AutomaticGainControl AGC;
139 AGC.set<AutomaticGainControl::levelEstimator>(levelEstimator);
140 mTags.push_back({AutomaticGainControl::levelEstimator, AGC});
141 }
142
143 bool isTagInRange(const AutomaticGainControl::Tag& tag, const AutomaticGainControl& AGC,
144 const Descriptor& desc) const {
145 const AutomaticGainControl::Capability& AGCCap =
146 desc.capability.get<Capability::automaticGainControl>();
147 switch (tag) {
148 case AutomaticGainControl::fixedDigitalGainMb: {
149 auto gain = AGC.get<AutomaticGainControl::fixedDigitalGainMb>();
150 return gain >= 0 && gain <= AGCCap.maxFixedDigitalGainMb;
151 }
152 case AutomaticGainControl::levelEstimator: {
153 return true;
154 }
155 case AutomaticGainControl::saturationMarginMb: {
156 auto margin = AGC.get<AutomaticGainControl::saturationMarginMb>();
157 return margin >= 0 && margin <= AGCCap.maxSaturationMarginMb;
158 }
159 default:
160 return false;
161 }
162 }
Shunkai Yaoab59e6d2022-12-22 00:45:23 +0000163 static std::unordered_set<int> getDigitalGainValues() {
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000164 const auto max = std::max_element(
165 kFactoryDescList.begin(), kFactoryDescList.end(),
166 [](const std::pair<std::shared_ptr<IFactory>, Descriptor>& a,
167 const std::pair<std::shared_ptr<IFactory>, Descriptor>& b) {
168 return a.second.capability.get<Capability::automaticGainControl>()
169 .maxFixedDigitalGainMb <
170 b.second.capability.get<Capability::automaticGainControl>()
171 .maxFixedDigitalGainMb;
172 });
173 if (max == kFactoryDescList.end()) {
174 return {0};
175 }
176 int maxGain = max->second.capability.get<Capability::automaticGainControl>()
177 .maxFixedDigitalGainMb;
178 return {-1, 0, maxGain - 1, maxGain, maxGain + 1};
179 }
Shunkai Yaoab59e6d2022-12-22 00:45:23 +0000180 static std::unordered_set<int> getSaturationMarginValues() {
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000181 const auto max = std::max_element(
182 kFactoryDescList.begin(), kFactoryDescList.end(),
183 [](const std::pair<std::shared_ptr<IFactory>, Descriptor>& a,
184 const std::pair<std::shared_ptr<IFactory>, Descriptor>& b) {
185 return a.second.capability.get<Capability::automaticGainControl>()
186 .maxSaturationMarginMb <
187 b.second.capability.get<Capability::automaticGainControl>()
188 .maxSaturationMarginMb;
189 });
190 if (max == kFactoryDescList.end()) {
191 return {0};
192 }
193 int maxMargin = max->second.capability.get<Capability::automaticGainControl>()
194 .maxSaturationMarginMb;
195 return {-1, 0, maxMargin - 1, maxMargin, maxMargin + 1};
196 }
197
198 private:
199 std::vector<std::pair<AutomaticGainControl::Tag, AutomaticGainControl>> mTags;
200 void CleanUp() { mTags.clear(); }
201};
202
203const std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> AGCParamTest::kFactoryDescList =
204 EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor,
205 kAutomaticGainControlTypeUUID);
Shunkai Yaoab59e6d2022-12-22 00:45:23 +0000206const std::unordered_set<int> AGCParamTest::kDigitalGainValues =
207 AGCParamTest::getDigitalGainValues();
208const std::unordered_set<int> AGCParamTest::kSaturationMarginValues =
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000209 AGCParamTest::getSaturationMarginValues();
Shunkai Yaoab59e6d2022-12-22 00:45:23 +0000210const std::unordered_set<AutomaticGainControl::LevelEstimator> AGCParamTest::kLevelEstimatorValues =
211 {ndk::enum_range<AutomaticGainControl::LevelEstimator>().begin(),
212 ndk::enum_range<AutomaticGainControl::LevelEstimator>().end()};
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000213
214TEST_P(AGCParamTest, SetAndGetDigitalGainParam) {
215 EXPECT_NO_FATAL_FAILURE(addDigitalGainParam(mGain));
216 SetAndGetParameters();
217}
218
219TEST_P(AGCParamTest, SetAndGetSaturationMargin) {
220 EXPECT_NO_FATAL_FAILURE(addSaturationMarginParam(mMargin));
221 SetAndGetParameters();
222}
223
224TEST_P(AGCParamTest, SetAndGetLevelEstimator) {
225 EXPECT_NO_FATAL_FAILURE(addLevelEstimatorParam(mLevelEstimator));
226 SetAndGetParameters();
227}
228
229INSTANTIATE_TEST_SUITE_P(
230 AGCParamTest, AGCParamTest,
231 ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
232 IFactory::descriptor, kAutomaticGainControlTypeUUID)),
233 testing::ValuesIn(AGCParamTest::kDigitalGainValues),
234 testing::ValuesIn(AGCParamTest::kSaturationMarginValues),
235 testing::ValuesIn(AGCParamTest::kLevelEstimatorValues)),
236 [](const testing::TestParamInfo<AGCParamTest::ParamType>& info) {
237 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
238 std::string gain = std::to_string(std::get<PARAM_DIGITAL_GAIN>(info.param));
239 std::string estimator = aidl::android::hardware::audio::effect::toString(
240 std::get<PARAM_LEVEL_ESTIMATOR>(info.param));
241 std::string margin =
242 std::to_string(static_cast<int>(std::get<PARAM_SATURATION_MARGIN>(info.param)));
243
244 std::string name = "Implementor_" + descriptor.common.implementor + "_name_" +
245 descriptor.common.name + "_UUID_" +
246 descriptor.common.id.uuid.toString() + "_digital_gain_" + gain +
247 "_level_estimator_" + estimator + "_margin_" + margin;
248 std::replace_if(
249 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
250 return name;
251 });
252
253GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AGCParamTest);
254
255int main(int argc, char** argv) {
256 ::testing::InitGoogleTest(&argc, argv);
257 ABinderProcess_setThreadPoolMaxThreadCount(1);
258 ABinderProcess_startThreadPool();
259 return RUN_ALL_TESTS();
260}