blob: 974c2e7d0557e5a0ef56df800ca302992e51b4f3 [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;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000029using aidl::android::hardware::audio::effect::Descriptor;
30using aidl::android::hardware::audio::effect::IEffect;
31using aidl::android::hardware::audio::effect::IFactory;
32using aidl::android::hardware::audio::effect::kAutomaticGainControlTypeUUID;
33using aidl::android::hardware::audio::effect::Parameter;
34
35enum ParamName {
36 PARAM_INSTANCE_NAME,
37 PARAM_DIGITAL_GAIN,
38 PARAM_SATURATION_MARGIN,
39 PARAM_LEVEL_ESTIMATOR
40};
41using AGCParamTestParam =
42 std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int /* gain */,
43 int /* margin */, AutomaticGainControl::LevelEstimator>;
44
45class AGCParamTest : public ::testing::TestWithParam<AGCParamTestParam>, public EffectHelper {
46 public:
47 AGCParamTest()
48 : mGain(std::get<PARAM_DIGITAL_GAIN>(GetParam())),
49 mMargin(std::get<PARAM_SATURATION_MARGIN>(GetParam())),
50 mLevelEstimator(std::get<PARAM_LEVEL_ESTIMATOR>(GetParam())) {
51 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
52 }
53
54 void SetUp() override {
55 ASSERT_NE(nullptr, mFactory);
56 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
57
58 Parameter::Specific specific = getDefaultParamSpecific();
59 Parameter::Common common = EffectHelper::createParamCommon(
60 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
61 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
62 IEffect::OpenEffectReturn ret;
63 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE));
64 ASSERT_NE(nullptr, mEffect);
65 }
66
67 void TearDown() override {
68 ASSERT_NO_FATAL_FAILURE(close(mEffect));
69 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
70 }
71
72 Parameter::Specific getDefaultParamSpecific() {
73 AutomaticGainControl AGC =
74 AutomaticGainControl::make<AutomaticGainControl::fixedDigitalGainMb>(0);
75 Parameter::Specific specific =
76 Parameter::Specific::make<Parameter::Specific::automaticGainControl>(AGC);
77 return specific;
78 }
79
80 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000081 std::shared_ptr<IFactory> mFactory;
82 std::shared_ptr<IEffect> mEffect;
83 Descriptor mDescriptor;
84 int mGain;
85 int mMargin;
86 AutomaticGainControl::LevelEstimator mLevelEstimator;
87
88 void SetAndGetParameters() {
89 for (auto& it : mTags) {
90 auto& tag = it.first;
91 auto& AGC = it.second;
92
93 // validate parameter
94 Descriptor desc;
95 ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
Shunkai Yao0a0c45e2023-02-13 17:41:11 +000096 const bool valid =
97 isParameterValid<AutomaticGainControl, Range::automaticGainControl>(AGC, desc);
Shunkai Yao5bd4a302022-12-20 15:46:24 +000098 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
99
100 // set parameter
101 Parameter expectParam;
102 Parameter::Specific specific;
103 specific.set<Parameter::Specific::automaticGainControl>(AGC);
104 expectParam.set<Parameter::specific>(specific);
105 EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
106
107 // only get if parameter in range and set success
108 if (expected == EX_NONE) {
109 Parameter getParam;
110 Parameter::Id id;
111 AutomaticGainControl::Id specificId;
112 specificId.set<AutomaticGainControl::Id::commonTag>(tag);
113 id.set<Parameter::Id::automaticGainControlTag>(specificId);
114 EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam));
115
116 EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString()
117 << "\ngetParam:" << getParam.toString();
118 }
119 }
120 }
121
122 void addDigitalGainParam(int gain) {
123 AutomaticGainControl AGC;
124 AGC.set<AutomaticGainControl::fixedDigitalGainMb>(gain);
125 mTags.push_back({AutomaticGainControl::fixedDigitalGainMb, AGC});
126 }
127 void addSaturationMarginParam(int margin) {
128 AutomaticGainControl AGC;
129 AGC.set<AutomaticGainControl::saturationMarginMb>(margin);
130 mTags.push_back({AutomaticGainControl::saturationMarginMb, AGC});
131 }
132 void addLevelEstimatorParam(AutomaticGainControl::LevelEstimator levelEstimator) {
133 AutomaticGainControl AGC;
134 AGC.set<AutomaticGainControl::levelEstimator>(levelEstimator);
135 mTags.push_back({AutomaticGainControl::levelEstimator, AGC});
136 }
137
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000138 static std::set<AutomaticGainControl::LevelEstimator> getLevelEstimatorValues() {
Shunkai Yao883d75b2022-12-24 05:15:15 +0000139 return {ndk::enum_range<AutomaticGainControl::LevelEstimator>().begin(),
140 ndk::enum_range<AutomaticGainControl::LevelEstimator>().end()};
141 }
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000142
143 private:
144 std::vector<std::pair<AutomaticGainControl::Tag, AutomaticGainControl>> mTags;
145 void CleanUp() { mTags.clear(); }
146};
147
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000148TEST_P(AGCParamTest, SetAndGetDigitalGainParam) {
149 EXPECT_NO_FATAL_FAILURE(addDigitalGainParam(mGain));
150 SetAndGetParameters();
151}
152
153TEST_P(AGCParamTest, SetAndGetSaturationMargin) {
154 EXPECT_NO_FATAL_FAILURE(addSaturationMarginParam(mMargin));
155 SetAndGetParameters();
156}
157
158TEST_P(AGCParamTest, SetAndGetLevelEstimator) {
159 EXPECT_NO_FATAL_FAILURE(addLevelEstimatorParam(mLevelEstimator));
160 SetAndGetParameters();
161}
162
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000163std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000164INSTANTIATE_TEST_SUITE_P(
165 AGCParamTest, AGCParamTest,
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000166 ::testing::Combine(
167 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
168 IFactory::descriptor, kAutomaticGainControlTypeUUID)),
169 testing::ValuesIn(EffectHelper::getTestValueSet<
170 AutomaticGainControl, int, Range::automaticGainControl,
171 AutomaticGainControl::fixedDigitalGainMb>(
172 kDescPair, EffectHelper::expandTestValueBasic<int>)),
173 testing::ValuesIn(EffectHelper::getTestValueSet<
174 AutomaticGainControl, int, Range::automaticGainControl,
175 AutomaticGainControl::saturationMarginMb>(
176 kDescPair, EffectHelper::expandTestValueBasic<int>)),
177 testing::ValuesIn(AGCParamTest::getLevelEstimatorValues())),
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000178 [](const testing::TestParamInfo<AGCParamTest::ParamType>& info) {
179 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
180 std::string gain = std::to_string(std::get<PARAM_DIGITAL_GAIN>(info.param));
181 std::string estimator = aidl::android::hardware::audio::effect::toString(
182 std::get<PARAM_LEVEL_ESTIMATOR>(info.param));
183 std::string margin =
184 std::to_string(static_cast<int>(std::get<PARAM_SATURATION_MARGIN>(info.param)));
185
186 std::string name = "Implementor_" + descriptor.common.implementor + "_name_" +
187 descriptor.common.name + "_UUID_" +
188 descriptor.common.id.uuid.toString() + "_digital_gain_" + gain +
189 "_level_estimator_" + estimator + "_margin_" + margin;
190 std::replace_if(
191 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
192 return name;
193 });
194
195GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AGCParamTest);
196
197int main(int argc, char** argv) {
198 ::testing::InitGoogleTest(&argc, argv);
199 ABinderProcess_setThreadPoolMaxThreadCount(1);
200 ABinderProcess_startThreadPool();
201 return RUN_ALL_TESTS();
202}