blob: 15a93743522854cb00db98e4eaa91108b71c6a7b [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
Shraddha Basantwanicac2e682023-02-15 18:03:58 +053017#include <aidl/Vintf.h>
Shraddha Basantwanicac2e682023-02-15 18:03:58 +053018#define LOG_TAG "VtsHalAGC1ParamTest"
Mikhail Naganov872d4a62023-03-09 18:19:01 -080019#include <android-base/logging.h>
Shraddha Basantwanicac2e682023-02-15 18:03:58 +053020
21#include "EffectHelper.h"
22
23using namespace android;
24
25using aidl::android::hardware::audio::effect::AutomaticGainControlV1;
26using aidl::android::hardware::audio::effect::Descriptor;
27using aidl::android::hardware::audio::effect::IEffect;
28using aidl::android::hardware::audio::effect::IFactory;
29using aidl::android::hardware::audio::effect::kAutomaticGainControlV1TypeUUID;
30using aidl::android::hardware::audio::effect::Parameter;
31
32enum ParamName {
33 PARAM_INSTANCE_NAME,
34 PARAM_TARGET_PEAK_LEVEL,
35 PARAM_MAX_COMPRESSION_GAIN,
36 PARAM_ENABLE_LIMITER
37};
38using AGC1ParamTestParam =
39 std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int /* targetPeakLevel */,
40 int /* maxCompressionGain */, bool /* enableLimiter */>;
41
42class 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();
56 Parameter::Common common = EffectHelper::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
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
141TEST_P(AGC1ParamTest, SetAndGetTargetPeakLevelParam) {
142 EXPECT_NO_FATAL_FAILURE(addTargetPeakLevelParam(mTargetPeakLevel));
143 SetAndGetParameters();
144}
145
146TEST_P(AGC1ParamTest, SetAndGetMaxCompressionGain) {
147 EXPECT_NO_FATAL_FAILURE(addMaxCompressionGainParam(mMaxCompressionGain));
148 SetAndGetParameters();
149}
150
151TEST_P(AGC1ParamTest, SetAndGetEnableLimiter) {
152 EXPECT_NO_FATAL_FAILURE(addEnableLimiterParam(mEnableLimiter));
153 SetAndGetParameters();
154}
155
156std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
157INSTANTIATE_TEST_SUITE_P(
158 AGC1ParamTest, AGC1ParamTest,
159 ::testing::Combine(
160 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
161 IFactory::descriptor, kAutomaticGainControlV1TypeUUID)),
162 testing::ValuesIn(EffectHelper::getTestValueSet<
163 AutomaticGainControlV1, int, Range::automaticGainControlV1,
164 AutomaticGainControlV1::targetPeakLevelDbFs>(
165 kDescPair, EffectHelper::expandTestValueBasic<int>)),
166 testing::ValuesIn(EffectHelper::getTestValueSet<
167 AutomaticGainControlV1, int, Range::automaticGainControlV1,
168 AutomaticGainControlV1::maxCompressionGainDb>(
169 kDescPair, EffectHelper::expandTestValueBasic<int>)),
170 testing::Bool()),
171 [](const testing::TestParamInfo<AGC1ParamTest::ParamType>& info) {
172 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
173 std::string targetPeakLevel =
174 std::to_string(std::get<PARAM_TARGET_PEAK_LEVEL>(info.param));
175 std::string maxCompressionGain =
176 std::to_string(std::get<PARAM_MAX_COMPRESSION_GAIN>(info.param));
177 std::string enableLimiter = std::to_string(std::get<PARAM_ENABLE_LIMITER>(info.param));
178
179 std::string name = "Implementor_" + descriptor.common.implementor + "_name_" +
180 descriptor.common.name + "_UUID_" +
181 descriptor.common.id.uuid.toString() + "_target_peak_level_" +
182 targetPeakLevel + "_max_compression_gain_" + maxCompressionGain +
183 "_enable_limiter_" + enableLimiter;
184 std::replace_if(
185 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
186 return name;
187 });
188
189GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AGC1ParamTest);
190
191int main(int argc, char** argv) {
192 ::testing::InitGoogleTest(&argc, argv);
193 ABinderProcess_setThreadPoolMaxThreadCount(1);
194 ABinderProcess_startThreadPool();
195 return RUN_ALL_TESTS();
196}