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 | |
| 17 | #include <aidl/Vintf.h> |
| 18 | |
| 19 | #define LOG_TAG "VtsHalAGCParamTest" |
| 20 | |
| 21 | #include <Utils.h> |
| 22 | #include "EffectHelper.h" |
| 23 | |
| 24 | using namespace android; |
| 25 | |
| 26 | using aidl::android::hardware::audio::effect::AutomaticGainControl; |
| 27 | using aidl::android::hardware::audio::effect::Capability; |
| 28 | using aidl::android::hardware::audio::effect::Descriptor; |
| 29 | using aidl::android::hardware::audio::effect::IEffect; |
| 30 | using aidl::android::hardware::audio::effect::IFactory; |
| 31 | using aidl::android::hardware::audio::effect::kAutomaticGainControlTypeUUID; |
| 32 | using aidl::android::hardware::audio::effect::Parameter; |
| 33 | |
| 34 | enum ParamName { |
| 35 | PARAM_INSTANCE_NAME, |
| 36 | PARAM_DIGITAL_GAIN, |
| 37 | PARAM_SATURATION_MARGIN, |
| 38 | PARAM_LEVEL_ESTIMATOR |
| 39 | }; |
| 40 | using AGCParamTestParam = |
| 41 | std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int /* gain */, |
| 42 | int /* margin */, AutomaticGainControl::LevelEstimator>; |
| 43 | |
| 44 | class AGCParamTest : public ::testing::TestWithParam<AGCParamTestParam>, public EffectHelper { |
| 45 | public: |
| 46 | AGCParamTest() |
| 47 | : mGain(std::get<PARAM_DIGITAL_GAIN>(GetParam())), |
| 48 | mMargin(std::get<PARAM_SATURATION_MARGIN>(GetParam())), |
| 49 | mLevelEstimator(std::get<PARAM_LEVEL_ESTIMATOR>(GetParam())) { |
| 50 | std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam()); |
| 51 | } |
| 52 | |
| 53 | void SetUp() override { |
| 54 | ASSERT_NE(nullptr, mFactory); |
| 55 | ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor)); |
| 56 | |
| 57 | Parameter::Specific specific = getDefaultParamSpecific(); |
| 58 | Parameter::Common common = EffectHelper::createParamCommon( |
| 59 | 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */, |
| 60 | kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */); |
| 61 | IEffect::OpenEffectReturn ret; |
| 62 | ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE)); |
| 63 | ASSERT_NE(nullptr, mEffect); |
| 64 | } |
| 65 | |
| 66 | void TearDown() override { |
| 67 | ASSERT_NO_FATAL_FAILURE(close(mEffect)); |
| 68 | ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect)); |
| 69 | } |
| 70 | |
| 71 | Parameter::Specific getDefaultParamSpecific() { |
| 72 | AutomaticGainControl AGC = |
| 73 | AutomaticGainControl::make<AutomaticGainControl::fixedDigitalGainMb>(0); |
| 74 | Parameter::Specific specific = |
| 75 | Parameter::Specific::make<Parameter::Specific::automaticGainControl>(AGC); |
| 76 | return specific; |
| 77 | } |
| 78 | |
| 79 | static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100; |
| 80 | static const std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kFactoryDescList; |
| 81 | static const std::vector<int> kDigitalGainValues; |
| 82 | static const std::vector<int> kSaturationMarginValues; |
| 83 | static const std::vector<AutomaticGainControl::LevelEstimator> kLevelEstimatorValues; |
| 84 | |
| 85 | std::shared_ptr<IFactory> mFactory; |
| 86 | std::shared_ptr<IEffect> mEffect; |
| 87 | Descriptor mDescriptor; |
| 88 | int mGain; |
| 89 | int mMargin; |
| 90 | AutomaticGainControl::LevelEstimator mLevelEstimator; |
| 91 | |
| 92 | void SetAndGetParameters() { |
| 93 | for (auto& it : mTags) { |
| 94 | auto& tag = it.first; |
| 95 | auto& AGC = it.second; |
| 96 | |
| 97 | // validate parameter |
| 98 | Descriptor desc; |
| 99 | ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc)); |
| 100 | const bool valid = isTagInRange(tag, AGC, desc); |
| 101 | const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT; |
| 102 | |
| 103 | // set parameter |
| 104 | Parameter expectParam; |
| 105 | Parameter::Specific specific; |
| 106 | specific.set<Parameter::Specific::automaticGainControl>(AGC); |
| 107 | expectParam.set<Parameter::specific>(specific); |
| 108 | EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString(); |
| 109 | |
| 110 | // only get if parameter in range and set success |
| 111 | if (expected == EX_NONE) { |
| 112 | Parameter getParam; |
| 113 | Parameter::Id id; |
| 114 | AutomaticGainControl::Id specificId; |
| 115 | specificId.set<AutomaticGainControl::Id::commonTag>(tag); |
| 116 | id.set<Parameter::Id::automaticGainControlTag>(specificId); |
| 117 | EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam)); |
| 118 | |
| 119 | EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString() |
| 120 | << "\ngetParam:" << getParam.toString(); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void addDigitalGainParam(int gain) { |
| 126 | AutomaticGainControl AGC; |
| 127 | AGC.set<AutomaticGainControl::fixedDigitalGainMb>(gain); |
| 128 | mTags.push_back({AutomaticGainControl::fixedDigitalGainMb, AGC}); |
| 129 | } |
| 130 | void addSaturationMarginParam(int margin) { |
| 131 | AutomaticGainControl AGC; |
| 132 | AGC.set<AutomaticGainControl::saturationMarginMb>(margin); |
| 133 | mTags.push_back({AutomaticGainControl::saturationMarginMb, AGC}); |
| 134 | } |
| 135 | void addLevelEstimatorParam(AutomaticGainControl::LevelEstimator levelEstimator) { |
| 136 | AutomaticGainControl AGC; |
| 137 | AGC.set<AutomaticGainControl::levelEstimator>(levelEstimator); |
| 138 | mTags.push_back({AutomaticGainControl::levelEstimator, AGC}); |
| 139 | } |
| 140 | |
| 141 | bool isTagInRange(const AutomaticGainControl::Tag& tag, const AutomaticGainControl& AGC, |
| 142 | const Descriptor& desc) const { |
| 143 | const AutomaticGainControl::Capability& AGCCap = |
| 144 | desc.capability.get<Capability::automaticGainControl>(); |
| 145 | switch (tag) { |
| 146 | case AutomaticGainControl::fixedDigitalGainMb: { |
| 147 | auto gain = AGC.get<AutomaticGainControl::fixedDigitalGainMb>(); |
| 148 | return gain >= 0 && gain <= AGCCap.maxFixedDigitalGainMb; |
| 149 | } |
| 150 | case AutomaticGainControl::levelEstimator: { |
| 151 | return true; |
| 152 | } |
| 153 | case AutomaticGainControl::saturationMarginMb: { |
| 154 | auto margin = AGC.get<AutomaticGainControl::saturationMarginMb>(); |
| 155 | return margin >= 0 && margin <= AGCCap.maxSaturationMarginMb; |
| 156 | } |
| 157 | default: |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | static std::vector<int> getDigitalGainValues() { |
| 162 | const auto max = std::max_element( |
| 163 | kFactoryDescList.begin(), kFactoryDescList.end(), |
| 164 | [](const std::pair<std::shared_ptr<IFactory>, Descriptor>& a, |
| 165 | const std::pair<std::shared_ptr<IFactory>, Descriptor>& b) { |
| 166 | return a.second.capability.get<Capability::automaticGainControl>() |
| 167 | .maxFixedDigitalGainMb < |
| 168 | b.second.capability.get<Capability::automaticGainControl>() |
| 169 | .maxFixedDigitalGainMb; |
| 170 | }); |
| 171 | if (max == kFactoryDescList.end()) { |
| 172 | return {0}; |
| 173 | } |
| 174 | int maxGain = max->second.capability.get<Capability::automaticGainControl>() |
| 175 | .maxFixedDigitalGainMb; |
| 176 | return {-1, 0, maxGain - 1, maxGain, maxGain + 1}; |
| 177 | } |
| 178 | static std::vector<int> getSaturationMarginValues() { |
| 179 | const auto max = std::max_element( |
| 180 | kFactoryDescList.begin(), kFactoryDescList.end(), |
| 181 | [](const std::pair<std::shared_ptr<IFactory>, Descriptor>& a, |
| 182 | const std::pair<std::shared_ptr<IFactory>, Descriptor>& b) { |
| 183 | return a.second.capability.get<Capability::automaticGainControl>() |
| 184 | .maxSaturationMarginMb < |
| 185 | b.second.capability.get<Capability::automaticGainControl>() |
| 186 | .maxSaturationMarginMb; |
| 187 | }); |
| 188 | if (max == kFactoryDescList.end()) { |
| 189 | return {0}; |
| 190 | } |
| 191 | int maxMargin = max->second.capability.get<Capability::automaticGainControl>() |
| 192 | .maxSaturationMarginMb; |
| 193 | return {-1, 0, maxMargin - 1, maxMargin, maxMargin + 1}; |
| 194 | } |
| 195 | |
| 196 | private: |
| 197 | std::vector<std::pair<AutomaticGainControl::Tag, AutomaticGainControl>> mTags; |
| 198 | void CleanUp() { mTags.clear(); } |
| 199 | }; |
| 200 | |
| 201 | const std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> AGCParamTest::kFactoryDescList = |
| 202 | EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor, |
| 203 | kAutomaticGainControlTypeUUID); |
| 204 | const std::vector<int> AGCParamTest::kDigitalGainValues = AGCParamTest::getDigitalGainValues(); |
| 205 | const std::vector<int> AGCParamTest::kSaturationMarginValues = |
| 206 | AGCParamTest::getSaturationMarginValues(); |
| 207 | const std::vector<AutomaticGainControl::LevelEstimator> AGCParamTest::kLevelEstimatorValues = { |
| 208 | AutomaticGainControl::LevelEstimator::RMS, AutomaticGainControl::LevelEstimator::PEAK}; |
| 209 | |
| 210 | TEST_P(AGCParamTest, SetAndGetDigitalGainParam) { |
| 211 | EXPECT_NO_FATAL_FAILURE(addDigitalGainParam(mGain)); |
| 212 | SetAndGetParameters(); |
| 213 | } |
| 214 | |
| 215 | TEST_P(AGCParamTest, SetAndGetSaturationMargin) { |
| 216 | EXPECT_NO_FATAL_FAILURE(addSaturationMarginParam(mMargin)); |
| 217 | SetAndGetParameters(); |
| 218 | } |
| 219 | |
| 220 | TEST_P(AGCParamTest, SetAndGetLevelEstimator) { |
| 221 | EXPECT_NO_FATAL_FAILURE(addLevelEstimatorParam(mLevelEstimator)); |
| 222 | SetAndGetParameters(); |
| 223 | } |
| 224 | |
| 225 | INSTANTIATE_TEST_SUITE_P( |
| 226 | AGCParamTest, AGCParamTest, |
| 227 | ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors( |
| 228 | IFactory::descriptor, kAutomaticGainControlTypeUUID)), |
| 229 | testing::ValuesIn(AGCParamTest::kDigitalGainValues), |
| 230 | testing::ValuesIn(AGCParamTest::kSaturationMarginValues), |
| 231 | testing::ValuesIn(AGCParamTest::kLevelEstimatorValues)), |
| 232 | [](const testing::TestParamInfo<AGCParamTest::ParamType>& info) { |
| 233 | auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second; |
| 234 | std::string gain = std::to_string(std::get<PARAM_DIGITAL_GAIN>(info.param)); |
| 235 | std::string estimator = aidl::android::hardware::audio::effect::toString( |
| 236 | std::get<PARAM_LEVEL_ESTIMATOR>(info.param)); |
| 237 | std::string margin = |
| 238 | std::to_string(static_cast<int>(std::get<PARAM_SATURATION_MARGIN>(info.param))); |
| 239 | |
| 240 | std::string name = "Implementor_" + descriptor.common.implementor + "_name_" + |
| 241 | descriptor.common.name + "_UUID_" + |
| 242 | descriptor.common.id.uuid.toString() + "_digital_gain_" + gain + |
| 243 | "_level_estimator_" + estimator + "_margin_" + margin; |
| 244 | std::replace_if( |
| 245 | name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_'); |
| 246 | return name; |
| 247 | }); |
| 248 | |
| 249 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AGCParamTest); |
| 250 | |
| 251 | int main(int argc, char** argv) { |
| 252 | ::testing::InitGoogleTest(&argc, argv); |
| 253 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 254 | ABinderProcess_startThreadPool(); |
| 255 | return RUN_ALL_TESTS(); |
| 256 | } |