Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [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 | #define LOG_TAG "VtsHalVirtualizerTest" |
| 18 | |
| 19 | #include <Utils.h> |
| 20 | #include <aidl/Vintf.h> |
| 21 | #include "EffectHelper.h" |
| 22 | |
| 23 | using namespace android; |
| 24 | |
| 25 | using aidl::android::hardware::audio::effect::Capability; |
| 26 | using aidl::android::hardware::audio::effect::Descriptor; |
| 27 | using aidl::android::hardware::audio::effect::IEffect; |
| 28 | using aidl::android::hardware::audio::effect::IFactory; |
| 29 | using aidl::android::hardware::audio::effect::kVirtualizerTypeUUID; |
| 30 | using aidl::android::hardware::audio::effect::Parameter; |
| 31 | using aidl::android::hardware::audio::effect::Virtualizer; |
| 32 | |
| 33 | /** |
| 34 | * Here we focus on specific parameter checking, general IEffect interfaces testing performed in |
| 35 | * VtsAudioEffectTargetTest. |
| 36 | */ |
| 37 | enum ParamName { PARAM_INSTANCE_NAME, PARAM_STRENGTH }; |
| 38 | using VirtualizerParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int>; |
| 39 | |
| 40 | /* |
| 41 | * Testing parameter range, assuming the parameter supported by effect is in this range. |
| 42 | * Parameter should be within the valid range defined in the documentation, |
| 43 | * for any supported value test expects EX_NONE from IEffect.setParameter(), |
| 44 | * otherwise expect EX_ILLEGAL_ARGUMENT. |
| 45 | */ |
| 46 | |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 47 | class VirtualizerParamTest : public ::testing::TestWithParam<VirtualizerParamTestParam>, |
| 48 | public EffectHelper { |
| 49 | public: |
| 50 | VirtualizerParamTest() : mParamStrength(std::get<PARAM_STRENGTH>(GetParam())) { |
| 51 | std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam()); |
| 52 | } |
| 53 | |
| 54 | void SetUp() override { |
| 55 | ASSERT_NE(nullptr, mFactory); |
| 56 | ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor)); |
| 57 | |
| 58 | Parameter::Specific specific = getDefaultParamSpecific(); |
| 59 | Parameter::Common common = EffectHelper::createParamCommon( |
| 60 | 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */, |
| 61 | kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */); |
| 62 | IEffect::OpenEffectReturn ret; |
| 63 | ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE)); |
| 64 | ASSERT_NE(nullptr, mEffect); |
| 65 | } |
| 66 | |
| 67 | void TearDown() override { |
| 68 | ASSERT_NO_FATAL_FAILURE(close(mEffect)); |
| 69 | ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect)); |
| 70 | } |
| 71 | |
| 72 | Parameter::Specific getDefaultParamSpecific() { |
Sham Rathod | e808b07 | 2022-12-27 11:56:30 +0530 | [diff] [blame] | 73 | Virtualizer vr = Virtualizer::make<Virtualizer::strengthPm>(0); |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 74 | Parameter::Specific specific = |
| 75 | Parameter::Specific::make<Parameter::Specific::virtualizer>(vr); |
| 76 | return specific; |
| 77 | } |
| 78 | |
| 79 | static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100; |
| 80 | std::shared_ptr<IFactory> mFactory; |
| 81 | std::shared_ptr<IEffect> mEffect; |
| 82 | Descriptor mDescriptor; |
Sham Rathod | e808b07 | 2022-12-27 11:56:30 +0530 | [diff] [blame] | 83 | int mParamStrength = 0; |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 84 | |
| 85 | void SetAndGetVirtualizerParameters() { |
| 86 | for (auto& it : mTags) { |
| 87 | auto& tag = it.first; |
| 88 | auto& vr = it.second; |
| 89 | |
| 90 | // validate parameter |
| 91 | Descriptor desc; |
| 92 | ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc)); |
| 93 | const bool valid = isTagInRange(it.first, it.second, desc); |
| 94 | const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT; |
| 95 | |
| 96 | // set parameter |
| 97 | Parameter expectParam; |
| 98 | Parameter::Specific specific; |
| 99 | specific.set<Parameter::Specific::virtualizer>(vr); |
| 100 | expectParam.set<Parameter::specific>(specific); |
| 101 | EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString(); |
| 102 | |
| 103 | // only get if parameter in range and set success |
| 104 | if (expected == EX_NONE) { |
| 105 | Parameter getParam; |
| 106 | Parameter::Id id; |
| 107 | Virtualizer::Id vrId; |
| 108 | vrId.set<Virtualizer::Id::commonTag>(tag); |
| 109 | id.set<Parameter::Id::virtualizerTag>(vrId); |
| 110 | // if set success, then get should match |
| 111 | EXPECT_STATUS(expected, mEffect->getParameter(id, &getParam)); |
| 112 | EXPECT_EQ(expectParam, getParam); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void addStrengthParam(int strength) { |
| 118 | Virtualizer vr; |
| 119 | vr.set<Virtualizer::strengthPm>(strength); |
| 120 | mTags.push_back({Virtualizer::strengthPm, vr}); |
| 121 | } |
| 122 | |
| 123 | bool isTagInRange(const Virtualizer::Tag& tag, const Virtualizer& vr, |
| 124 | const Descriptor& desc) const { |
| 125 | const Virtualizer::Capability& vrCap = desc.capability.get<Capability::virtualizer>(); |
| 126 | switch (tag) { |
| 127 | case Virtualizer::strengthPm: { |
| 128 | int strength = vr.get<Virtualizer::strengthPm>(); |
| 129 | return isStrengthInRange(vrCap, strength); |
| 130 | } |
| 131 | default: |
| 132 | return false; |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | bool isStrengthInRange(const Virtualizer::Capability& cap, int strength) const { |
Sham Rathod | e808b07 | 2022-12-27 11:56:30 +0530 | [diff] [blame] | 138 | return cap.strengthSupported && strength >= 0 && strength <= cap.maxStrengthPm; |
| 139 | } |
| 140 | |
| 141 | static std::vector<int> getStrengthTestValues( |
| 142 | std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kFactoryDescList) { |
| 143 | const auto max = std::max_element( |
| 144 | kFactoryDescList.begin(), kFactoryDescList.end(), |
| 145 | [](const std::pair<std::shared_ptr<IFactory>, Descriptor>& a, |
| 146 | const std::pair<std::shared_ptr<IFactory>, Descriptor>& b) { |
| 147 | return a.second.capability.get<Capability::virtualizer>().maxStrengthPm < |
| 148 | b.second.capability.get<Capability::virtualizer>().maxStrengthPm; |
| 149 | }); |
| 150 | if (max == kFactoryDescList.end()) { |
| 151 | return {0}; |
| 152 | } |
| 153 | int maxStrength = max->second.capability.get<Capability::virtualizer>().maxStrengthPm; |
| 154 | return {std::numeric_limits<int>::min(), |
| 155 | -1, |
| 156 | 0, |
| 157 | maxStrength >> 1, |
| 158 | maxStrength, |
| 159 | maxStrength + 1, |
| 160 | std::numeric_limits<int>::max()}; |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | private: |
| 164 | std::vector<std::pair<Virtualizer::Tag, Virtualizer>> mTags; |
| 165 | void CleanUp() { mTags.clear(); } |
| 166 | }; |
| 167 | |
| 168 | TEST_P(VirtualizerParamTest, SetAndGetStrength) { |
| 169 | EXPECT_NO_FATAL_FAILURE(addStrengthParam(mParamStrength)); |
| 170 | SetAndGetVirtualizerParameters(); |
| 171 | } |
| 172 | |
| 173 | INSTANTIATE_TEST_SUITE_P( |
| 174 | VirtualizerTest, VirtualizerParamTest, |
| 175 | ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors( |
| 176 | IFactory::descriptor, kVirtualizerTypeUUID)), |
Sham Rathod | e808b07 | 2022-12-27 11:56:30 +0530 | [diff] [blame] | 177 | testing::ValuesIn(VirtualizerParamTest::getStrengthTestValues( |
| 178 | EffectFactoryHelper::getAllEffectDescriptors( |
| 179 | IFactory::descriptor, kVirtualizerTypeUUID)))), |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 180 | [](const testing::TestParamInfo<VirtualizerParamTest::ParamType>& info) { |
| 181 | auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second; |
| 182 | std::string strength = std::to_string(std::get<PARAM_STRENGTH>(info.param)); |
| 183 | std::string name = "Implementor_" + descriptor.common.implementor + "_name_" + |
| 184 | descriptor.common.name + "_UUID_" + |
| 185 | descriptor.common.id.uuid.toString() + "_strength" + strength; |
| 186 | std::replace_if( |
| 187 | name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_'); |
| 188 | return name; |
| 189 | }); |
| 190 | |
| 191 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VirtualizerParamTest); |
| 192 | |
| 193 | int main(int argc, char** argv) { |
| 194 | ::testing::InitGoogleTest(&argc, argv); |
| 195 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 196 | ABinderProcess_startThreadPool(); |
| 197 | return RUN_ALL_TESTS(); |
| 198 | } |