Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 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 | |
Shunkai Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame] | 17 | #include <Utils.h> |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 18 | #include <aidl/Vintf.h> |
Shunkai Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame] | 19 | #include <android/binder_enums.h> |
| 20 | #include <unordered_set> |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 21 | |
| 22 | #define LOG_TAG "VtsHalAGCParamTest" |
| 23 | |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 24 | #include "EffectHelper.h" |
| 25 | |
| 26 | using namespace android; |
| 27 | |
| 28 | using aidl::android::hardware::audio::effect::AutomaticGainControl; |
| 29 | using aidl::android::hardware::audio::effect::Capability; |
| 30 | using aidl::android::hardware::audio::effect::Descriptor; |
| 31 | using aidl::android::hardware::audio::effect::IEffect; |
| 32 | using aidl::android::hardware::audio::effect::IFactory; |
| 33 | using aidl::android::hardware::audio::effect::kAutomaticGainControlTypeUUID; |
| 34 | using aidl::android::hardware::audio::effect::Parameter; |
| 35 | |
| 36 | enum ParamName { |
| 37 | PARAM_INSTANCE_NAME, |
| 38 | PARAM_DIGITAL_GAIN, |
| 39 | PARAM_SATURATION_MARGIN, |
| 40 | PARAM_LEVEL_ESTIMATOR |
| 41 | }; |
| 42 | using AGCParamTestParam = |
| 43 | std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int /* gain */, |
| 44 | int /* margin */, AutomaticGainControl::LevelEstimator>; |
| 45 | |
| 46 | class 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; |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 82 | std::shared_ptr<IFactory> mFactory; |
| 83 | std::shared_ptr<IEffect> mEffect; |
| 84 | Descriptor mDescriptor; |
| 85 | int mGain; |
| 86 | int mMargin; |
| 87 | AutomaticGainControl::LevelEstimator mLevelEstimator; |
| 88 | |
| 89 | void SetAndGetParameters() { |
| 90 | for (auto& it : mTags) { |
| 91 | auto& tag = it.first; |
| 92 | auto& AGC = it.second; |
| 93 | |
| 94 | // validate parameter |
| 95 | Descriptor desc; |
| 96 | ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc)); |
| 97 | const bool valid = isTagInRange(tag, AGC, desc); |
| 98 | 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 | |
| 138 | bool isTagInRange(const AutomaticGainControl::Tag& tag, const AutomaticGainControl& AGC, |
| 139 | const Descriptor& desc) const { |
| 140 | const AutomaticGainControl::Capability& AGCCap = |
| 141 | desc.capability.get<Capability::automaticGainControl>(); |
| 142 | switch (tag) { |
| 143 | case AutomaticGainControl::fixedDigitalGainMb: { |
| 144 | auto gain = AGC.get<AutomaticGainControl::fixedDigitalGainMb>(); |
| 145 | return gain >= 0 && gain <= AGCCap.maxFixedDigitalGainMb; |
| 146 | } |
| 147 | case AutomaticGainControl::levelEstimator: { |
| 148 | return true; |
| 149 | } |
| 150 | case AutomaticGainControl::saturationMarginMb: { |
| 151 | auto margin = AGC.get<AutomaticGainControl::saturationMarginMb>(); |
| 152 | return margin >= 0 && margin <= AGCCap.maxSaturationMarginMb; |
| 153 | } |
| 154 | default: |
| 155 | return false; |
| 156 | } |
| 157 | } |
Shunkai Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame] | 158 | static std::unordered_set<int> getDigitalGainValues() { |
Shunkai Yao | 883d75b | 2022-12-24 05:15:15 +0000 | [diff] [blame] | 159 | auto descList = EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor, |
| 160 | kAutomaticGainControlTypeUUID); |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 161 | const auto max = std::max_element( |
Shunkai Yao | 883d75b | 2022-12-24 05:15:15 +0000 | [diff] [blame] | 162 | descList.begin(), descList.end(), |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 163 | [](const std::pair<std::shared_ptr<IFactory>, Descriptor>& a, |
| 164 | const std::pair<std::shared_ptr<IFactory>, Descriptor>& b) { |
| 165 | return a.second.capability.get<Capability::automaticGainControl>() |
| 166 | .maxFixedDigitalGainMb < |
| 167 | b.second.capability.get<Capability::automaticGainControl>() |
| 168 | .maxFixedDigitalGainMb; |
| 169 | }); |
Shunkai Yao | 883d75b | 2022-12-24 05:15:15 +0000 | [diff] [blame] | 170 | if (max == descList.end()) { |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 171 | return {0}; |
| 172 | } |
| 173 | int maxGain = max->second.capability.get<Capability::automaticGainControl>() |
| 174 | .maxFixedDigitalGainMb; |
| 175 | return {-1, 0, maxGain - 1, maxGain, maxGain + 1}; |
| 176 | } |
Shunkai Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame] | 177 | static std::unordered_set<int> getSaturationMarginValues() { |
Shunkai Yao | 883d75b | 2022-12-24 05:15:15 +0000 | [diff] [blame] | 178 | auto descList = EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor, |
| 179 | kAutomaticGainControlTypeUUID); |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 180 | const auto max = std::max_element( |
Shunkai Yao | 883d75b | 2022-12-24 05:15:15 +0000 | [diff] [blame] | 181 | descList.begin(), descList.end(), |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 182 | [](const std::pair<std::shared_ptr<IFactory>, Descriptor>& a, |
| 183 | const std::pair<std::shared_ptr<IFactory>, Descriptor>& b) { |
| 184 | return a.second.capability.get<Capability::automaticGainControl>() |
| 185 | .maxSaturationMarginMb < |
| 186 | b.second.capability.get<Capability::automaticGainControl>() |
| 187 | .maxSaturationMarginMb; |
| 188 | }); |
Shunkai Yao | 883d75b | 2022-12-24 05:15:15 +0000 | [diff] [blame] | 189 | if (max == descList.end()) { |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 190 | return {0}; |
| 191 | } |
| 192 | int maxMargin = max->second.capability.get<Capability::automaticGainControl>() |
| 193 | .maxSaturationMarginMb; |
| 194 | return {-1, 0, maxMargin - 1, maxMargin, maxMargin + 1}; |
| 195 | } |
Shunkai Yao | 883d75b | 2022-12-24 05:15:15 +0000 | [diff] [blame] | 196 | static std::unordered_set<AutomaticGainControl::LevelEstimator> getLevelEstimatorValues() { |
| 197 | return {ndk::enum_range<AutomaticGainControl::LevelEstimator>().begin(), |
| 198 | ndk::enum_range<AutomaticGainControl::LevelEstimator>().end()}; |
| 199 | } |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 200 | |
| 201 | private: |
| 202 | std::vector<std::pair<AutomaticGainControl::Tag, AutomaticGainControl>> mTags; |
| 203 | void CleanUp() { mTags.clear(); } |
| 204 | }; |
| 205 | |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 206 | TEST_P(AGCParamTest, SetAndGetDigitalGainParam) { |
| 207 | EXPECT_NO_FATAL_FAILURE(addDigitalGainParam(mGain)); |
| 208 | SetAndGetParameters(); |
| 209 | } |
| 210 | |
| 211 | TEST_P(AGCParamTest, SetAndGetSaturationMargin) { |
| 212 | EXPECT_NO_FATAL_FAILURE(addSaturationMarginParam(mMargin)); |
| 213 | SetAndGetParameters(); |
| 214 | } |
| 215 | |
| 216 | TEST_P(AGCParamTest, SetAndGetLevelEstimator) { |
| 217 | EXPECT_NO_FATAL_FAILURE(addLevelEstimatorParam(mLevelEstimator)); |
| 218 | SetAndGetParameters(); |
| 219 | } |
| 220 | |
| 221 | INSTANTIATE_TEST_SUITE_P( |
| 222 | AGCParamTest, AGCParamTest, |
| 223 | ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors( |
| 224 | IFactory::descriptor, kAutomaticGainControlTypeUUID)), |
Shunkai Yao | 883d75b | 2022-12-24 05:15:15 +0000 | [diff] [blame] | 225 | testing::ValuesIn(AGCParamTest::getDigitalGainValues()), |
| 226 | testing::ValuesIn(AGCParamTest::getSaturationMarginValues()), |
| 227 | testing::ValuesIn(AGCParamTest::getLevelEstimatorValues())), |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 228 | [](const testing::TestParamInfo<AGCParamTest::ParamType>& info) { |
| 229 | auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second; |
| 230 | std::string gain = std::to_string(std::get<PARAM_DIGITAL_GAIN>(info.param)); |
| 231 | std::string estimator = aidl::android::hardware::audio::effect::toString( |
| 232 | std::get<PARAM_LEVEL_ESTIMATOR>(info.param)); |
| 233 | std::string margin = |
| 234 | std::to_string(static_cast<int>(std::get<PARAM_SATURATION_MARGIN>(info.param))); |
| 235 | |
| 236 | std::string name = "Implementor_" + descriptor.common.implementor + "_name_" + |
| 237 | descriptor.common.name + "_UUID_" + |
| 238 | descriptor.common.id.uuid.toString() + "_digital_gain_" + gain + |
| 239 | "_level_estimator_" + estimator + "_margin_" + margin; |
| 240 | std::replace_if( |
| 241 | name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_'); |
| 242 | return name; |
| 243 | }); |
| 244 | |
| 245 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AGCParamTest); |
| 246 | |
| 247 | int main(int argc, char** argv) { |
| 248 | ::testing::InitGoogleTest(&argc, argv); |
| 249 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 250 | ABinderProcess_startThreadPool(); |
| 251 | return RUN_ALL_TESTS(); |
| 252 | } |