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; |
| 82 | static const std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kFactoryDescList; |
Shunkai Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame] | 83 | 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 Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 86 | |
| 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 Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame] | 163 | static std::unordered_set<int> getDigitalGainValues() { |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 164 | 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 Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame] | 180 | static std::unordered_set<int> getSaturationMarginValues() { |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 181 | 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 | |
| 203 | const std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> AGCParamTest::kFactoryDescList = |
| 204 | EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor, |
| 205 | kAutomaticGainControlTypeUUID); |
Shunkai Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame] | 206 | const std::unordered_set<int> AGCParamTest::kDigitalGainValues = |
| 207 | AGCParamTest::getDigitalGainValues(); |
| 208 | const std::unordered_set<int> AGCParamTest::kSaturationMarginValues = |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 209 | AGCParamTest::getSaturationMarginValues(); |
Shunkai Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame] | 210 | const std::unordered_set<AutomaticGainControl::LevelEstimator> AGCParamTest::kLevelEstimatorValues = |
| 211 | {ndk::enum_range<AutomaticGainControl::LevelEstimator>().begin(), |
| 212 | ndk::enum_range<AutomaticGainControl::LevelEstimator>().end()}; |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 213 | |
| 214 | TEST_P(AGCParamTest, SetAndGetDigitalGainParam) { |
| 215 | EXPECT_NO_FATAL_FAILURE(addDigitalGainParam(mGain)); |
| 216 | SetAndGetParameters(); |
| 217 | } |
| 218 | |
| 219 | TEST_P(AGCParamTest, SetAndGetSaturationMargin) { |
| 220 | EXPECT_NO_FATAL_FAILURE(addSaturationMarginParam(mMargin)); |
| 221 | SetAndGetParameters(); |
| 222 | } |
| 223 | |
| 224 | TEST_P(AGCParamTest, SetAndGetLevelEstimator) { |
| 225 | EXPECT_NO_FATAL_FAILURE(addLevelEstimatorParam(mLevelEstimator)); |
| 226 | SetAndGetParameters(); |
| 227 | } |
| 228 | |
| 229 | INSTANTIATE_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 | |
| 253 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AGCParamTest); |
| 254 | |
| 255 | int main(int argc, char** argv) { |
| 256 | ::testing::InitGoogleTest(&argc, argv); |
| 257 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 258 | ABinderProcess_startThreadPool(); |
| 259 | return RUN_ALL_TESTS(); |
| 260 | } |