1 /*
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 
17 #define LOG_TAG "VtsHalAGC2ParamTest"
18 #include <android-base/logging.h>
19 #include <android/binder_enums.h>
20 
21 #include "EffectHelper.h"
22 
23 using namespace android;
24 
25 using aidl::android::hardware::audio::effect::AutomaticGainControlV2;
26 using aidl::android::hardware::audio::effect::Descriptor;
27 using aidl::android::hardware::audio::effect::getEffectTypeUuidAutomaticGainControlV2;
28 using aidl::android::hardware::audio::effect::IEffect;
29 using aidl::android::hardware::audio::effect::IFactory;
30 using aidl::android::hardware::audio::effect::Parameter;
31 using android::hardware::audio::common::testing::detail::TestExecutionTracer;
32 
33 enum ParamName {
34     PARAM_INSTANCE_NAME,
35     PARAM_DIGITAL_GAIN,
36     PARAM_SATURATION_MARGIN,
37     PARAM_LEVEL_ESTIMATOR
38 };
39 using AGC2ParamTestParam =
40         std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int /* gain */,
41                    int /* margin */, AutomaticGainControlV2::LevelEstimator>;
42 
43 class AGC2ParamTest : public ::testing::TestWithParam<AGC2ParamTestParam>, public EffectHelper {
44   public:
AGC2ParamTest()45     AGC2ParamTest()
46         : mGain(std::get<PARAM_DIGITAL_GAIN>(GetParam())),
47           mMargin(std::get<PARAM_SATURATION_MARGIN>(GetParam())),
48           mLevelEstimator(std::get<PARAM_LEVEL_ESTIMATOR>(GetParam())) {
49         std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
50     }
51 
SetUp()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 = 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 
TearDown()65     void TearDown() override {
66         ASSERT_NO_FATAL_FAILURE(close(mEffect));
67         ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
68     }
69 
getDefaultParamSpecific()70     Parameter::Specific getDefaultParamSpecific() {
71         AutomaticGainControlV2 AGC2 =
72                 AutomaticGainControlV2::make<AutomaticGainControlV2::fixedDigitalGainMb>(0);
73         Parameter::Specific specific =
74                 Parameter::Specific::make<Parameter::Specific::automaticGainControlV2>(AGC2);
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 mGain;
83     int mMargin;
84     AutomaticGainControlV2::LevelEstimator mLevelEstimator;
85 
SetAndGetParameters()86     void SetAndGetParameters() {
87         for (auto& it : mTags) {
88             auto& tag = it.first;
89             auto& AGC2 = it.second;
90 
91             // validate parameter
92             Descriptor desc;
93             ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
94             const bool valid =
95                     isParameterValid<AutomaticGainControlV2, Range::automaticGainControlV2>(AGC2,
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::automaticGainControlV2>(AGC2);
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                 AutomaticGainControlV2::Id specificId;
111                 specificId.set<AutomaticGainControlV2::Id::commonTag>(tag);
112                 id.set<Parameter::Id::automaticGainControlV2Tag>(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 
addDigitalGainParam(int gain)121     void addDigitalGainParam(int gain) {
122         AutomaticGainControlV2 AGC2;
123         AGC2.set<AutomaticGainControlV2::fixedDigitalGainMb>(gain);
124         mTags.push_back({AutomaticGainControlV2::fixedDigitalGainMb, AGC2});
125     }
addSaturationMarginParam(int margin)126     void addSaturationMarginParam(int margin) {
127         AutomaticGainControlV2 AGC2;
128         AGC2.set<AutomaticGainControlV2::saturationMarginMb>(margin);
129         mTags.push_back({AutomaticGainControlV2::saturationMarginMb, AGC2});
130     }
addLevelEstimatorParam(AutomaticGainControlV2::LevelEstimator levelEstimator)131     void addLevelEstimatorParam(AutomaticGainControlV2::LevelEstimator levelEstimator) {
132         AutomaticGainControlV2 AGC2;
133         AGC2.set<AutomaticGainControlV2::levelEstimator>(levelEstimator);
134         mTags.push_back({AutomaticGainControlV2::levelEstimator, AGC2});
135     }
136 
getLevelEstimatorValues()137     static std::set<AutomaticGainControlV2::LevelEstimator> getLevelEstimatorValues() {
138         return {ndk::enum_range<AutomaticGainControlV2::LevelEstimator>().begin(),
139                 ndk::enum_range<AutomaticGainControlV2::LevelEstimator>().end()};
140     }
141 
142   private:
143     std::vector<std::pair<AutomaticGainControlV2::Tag, AutomaticGainControlV2>> mTags;
CleanUp()144     void CleanUp() { mTags.clear(); }
145 };
146 
TEST_P(AGC2ParamTest,SetAndGetDigitalGainParam)147 TEST_P(AGC2ParamTest, SetAndGetDigitalGainParam) {
148     EXPECT_NO_FATAL_FAILURE(addDigitalGainParam(mGain));
149     SetAndGetParameters();
150 }
151 
TEST_P(AGC2ParamTest,SetAndGetSaturationMargin)152 TEST_P(AGC2ParamTest, SetAndGetSaturationMargin) {
153     EXPECT_NO_FATAL_FAILURE(addSaturationMarginParam(mMargin));
154     SetAndGetParameters();
155 }
156 
TEST_P(AGC2ParamTest,SetAndGetLevelEstimator)157 TEST_P(AGC2ParamTest, SetAndGetLevelEstimator) {
158     EXPECT_NO_FATAL_FAILURE(addLevelEstimatorParam(mLevelEstimator));
159     SetAndGetParameters();
160 }
161 
162 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
163 INSTANTIATE_TEST_SUITE_P(
164         AGC2ParamTest, AGC2ParamTest,
165         ::testing::Combine(
166                 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
167                                           IFactory::descriptor,
168                                           getEffectTypeUuidAutomaticGainControlV2())),
169                 testing::ValuesIn(EffectHelper::getTestValueSet<
170                                   AutomaticGainControlV2, int, Range::automaticGainControlV2,
171                                   AutomaticGainControlV2::fixedDigitalGainMb>(
172                         kDescPair, EffectHelper::expandTestValueBasic<int>)),
173                 testing::ValuesIn(EffectHelper::getTestValueSet<
174                                   AutomaticGainControlV2, int, Range::automaticGainControlV2,
175                                   AutomaticGainControlV2::saturationMarginMb>(
176                         kDescPair, EffectHelper::expandTestValueBasic<int>)),
177                 testing::ValuesIn(AGC2ParamTest::getLevelEstimatorValues())),
__anoncf62100c0102(const testing::TestParamInfo<AGC2ParamTest::ParamType>& info) 178         [](const testing::TestParamInfo<AGC2ParamTest::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 = getPrefix(descriptor) + "_digital_gain_" + gain +
187                                "_level_estimator_" + estimator + "_margin_" + margin;
188             std::replace_if(
189                     name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
190             return name;
191         });
192 
193 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AGC2ParamTest);
194 
main(int argc,char ** argv)195 int main(int argc, char** argv) {
196     ::testing::InitGoogleTest(&argc, argv);
197     ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
198     ABinderProcess_setThreadPoolMaxThreadCount(1);
199     ABinderProcess_startThreadPool();
200     return RUN_ALL_TESTS();
201 }
202