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 | #include <algorithm> |
| 19 | |
| 20 | #define LOG_TAG "VtsHalAECParamTest" |
| 21 | |
| 22 | #include <Utils.h> |
| 23 | #include "EffectHelper.h" |
| 24 | |
| 25 | using namespace android; |
| 26 | |
| 27 | using aidl::android::hardware::audio::effect::AcousticEchoCanceler; |
| 28 | using aidl::android::hardware::audio::effect::Capability; |
| 29 | using aidl::android::hardware::audio::effect::Descriptor; |
| 30 | using aidl::android::hardware::audio::effect::IEffect; |
| 31 | using aidl::android::hardware::audio::effect::IFactory; |
| 32 | using aidl::android::hardware::audio::effect::kAcousticEchoCancelerTypeUUID; |
| 33 | using aidl::android::hardware::audio::effect::Parameter; |
| 34 | |
| 35 | enum ParamName { PARAM_INSTANCE_NAME, PARAM_ECHO_DELAY, PARAM_MOBILE_MODE }; |
| 36 | using AECParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, |
| 37 | int /* echoDelayUs */, bool /* mobileMode */>; |
| 38 | |
| 39 | class AECParamTest : public ::testing::TestWithParam<AECParamTestParam>, public EffectHelper { |
| 40 | public: |
| 41 | AECParamTest() |
| 42 | : mEchoDelay(std::get<PARAM_ECHO_DELAY>(GetParam())), |
| 43 | mMobileMode(std::get<PARAM_MOBILE_MODE>(GetParam())) { |
| 44 | std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam()); |
| 45 | } |
| 46 | |
| 47 | void SetUp() override { |
| 48 | ASSERT_NE(nullptr, mFactory); |
| 49 | ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor)); |
| 50 | |
| 51 | Parameter::Specific specific = getDefaultParamSpecific(); |
| 52 | Parameter::Common common = EffectHelper::createParamCommon( |
| 53 | 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */, |
| 54 | kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */); |
| 55 | IEffect::OpenEffectReturn ret; |
| 56 | ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE)); |
| 57 | ASSERT_NE(nullptr, mEffect); |
| 58 | } |
| 59 | |
| 60 | void TearDown() override { |
| 61 | ASSERT_NO_FATAL_FAILURE(close(mEffect)); |
| 62 | ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect)); |
| 63 | } |
| 64 | |
| 65 | Parameter::Specific getDefaultParamSpecific() { |
| 66 | AcousticEchoCanceler aec = AcousticEchoCanceler::make<AcousticEchoCanceler::echoDelayUs>(0); |
| 67 | Parameter::Specific specific = |
| 68 | Parameter::Specific::make<Parameter::Specific::acousticEchoCanceler>(aec); |
| 69 | return specific; |
| 70 | } |
| 71 | |
| 72 | static const std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kFactoryDescList; |
| 73 | static const std::vector<int> kEchoDelayValues; |
| 74 | static const std::vector<bool> kMobileModeValues; |
| 75 | |
| 76 | static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100; |
| 77 | std::shared_ptr<IFactory> mFactory; |
| 78 | std::shared_ptr<IEffect> mEffect; |
| 79 | Descriptor mDescriptor; |
| 80 | |
| 81 | int mEchoDelay; |
| 82 | bool mMobileMode; |
| 83 | |
| 84 | void SetAndGetParameters() { |
| 85 | for (auto& it : mTags) { |
| 86 | auto& tag = it.first; |
| 87 | auto& aec = it.second; |
| 88 | |
| 89 | // validate parameter |
| 90 | Descriptor desc; |
| 91 | ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc)); |
| 92 | const bool valid = isTagInRange(tag, aec, desc); |
| 93 | const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT; |
| 94 | |
| 95 | // set parameter |
| 96 | Parameter expectParam; |
| 97 | Parameter::Specific specific; |
| 98 | specific.set<Parameter::Specific::acousticEchoCanceler>(aec); |
| 99 | expectParam.set<Parameter::specific>(specific); |
| 100 | EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString(); |
| 101 | |
| 102 | // only get if parameter in range and set success |
| 103 | if (expected == EX_NONE) { |
| 104 | Parameter getParam; |
| 105 | Parameter::Id id; |
| 106 | AcousticEchoCanceler::Id specificId; |
| 107 | specificId.set<AcousticEchoCanceler::Id::commonTag>(tag); |
| 108 | id.set<Parameter::Id::acousticEchoCancelerTag>(specificId); |
| 109 | EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam)); |
| 110 | |
| 111 | EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString() |
| 112 | << "\ngetParam:" << getParam.toString(); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void addEchoDelayParam(int delay) { |
| 118 | AcousticEchoCanceler aec; |
| 119 | aec.set<AcousticEchoCanceler::echoDelayUs>(delay); |
| 120 | mTags.push_back({AcousticEchoCanceler::echoDelayUs, aec}); |
| 121 | } |
| 122 | |
| 123 | void addMobileModeParam(bool mode) { |
| 124 | AcousticEchoCanceler aec; |
| 125 | aec.set<AcousticEchoCanceler::mobileMode>(mode); |
| 126 | mTags.push_back({AcousticEchoCanceler::mobileMode, aec}); |
| 127 | } |
| 128 | |
| 129 | bool isTagInRange(const AcousticEchoCanceler::Tag& tag, const AcousticEchoCanceler& aec, |
| 130 | const Descriptor& desc) const { |
| 131 | const AcousticEchoCanceler::Capability& aecCap = |
| 132 | desc.capability.get<Capability::acousticEchoCanceler>(); |
| 133 | switch (tag) { |
| 134 | case AcousticEchoCanceler::echoDelayUs: { |
| 135 | return isEchoDelayInRange(aecCap, aec.get<AcousticEchoCanceler::echoDelayUs>()); |
| 136 | } |
| 137 | case AcousticEchoCanceler::mobileMode: { |
| 138 | bool mode = aec.get<AcousticEchoCanceler::mobileMode>(); |
| 139 | return isMobileModeValid(aecCap, mode); |
| 140 | } |
| 141 | default: |
| 142 | return false; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | bool isEchoDelayInRange(const AcousticEchoCanceler::Capability& cap, int delay) const { |
| 147 | return (delay >= 0 && delay <= cap.maxEchoDelayUs); |
| 148 | } |
| 149 | |
| 150 | bool isMobileModeValid(const AcousticEchoCanceler::Capability& cap, bool mode) const { |
| 151 | if (cap.supportMobileMode) { |
| 152 | return true; |
| 153 | } else { |
| 154 | return mode == false; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static std::vector<int> getEchoDelayTestValues() { |
| 159 | const auto max = std::max_element( |
| 160 | kFactoryDescList.begin(), kFactoryDescList.end(), |
| 161 | [](const std::pair<std::shared_ptr<IFactory>, Descriptor>& a, |
| 162 | const std::pair<std::shared_ptr<IFactory>, Descriptor>& b) { |
| 163 | return a.second.capability.get<Capability::acousticEchoCanceler>() |
| 164 | .maxEchoDelayUs < |
| 165 | b.second.capability.get<Capability::acousticEchoCanceler>() |
| 166 | .maxEchoDelayUs; |
| 167 | }); |
| 168 | if (max == kFactoryDescList.end()) { |
| 169 | return {0}; |
| 170 | } |
| 171 | int maxDelay = |
| 172 | max->second.capability.get<Capability::acousticEchoCanceler>().maxEchoDelayUs; |
| 173 | return {-1, 0, maxDelay - 1, maxDelay, maxDelay + 1}; |
| 174 | } |
| 175 | |
| 176 | private: |
| 177 | std::vector<std::pair<AcousticEchoCanceler::Tag, AcousticEchoCanceler>> mTags; |
| 178 | void CleanUp() { mTags.clear(); } |
| 179 | }; |
| 180 | |
| 181 | const std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> AECParamTest::kFactoryDescList = |
| 182 | EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor, |
| 183 | kAcousticEchoCancelerTypeUUID); |
| 184 | const std::vector<int> AECParamTest::kEchoDelayValues = AECParamTest::getEchoDelayTestValues(); |
| 185 | const std::vector<bool> AECParamTest::kMobileModeValues = {true, false}; |
| 186 | |
| 187 | TEST_P(AECParamTest, SetAndGetEchoDelay) { |
| 188 | EXPECT_NO_FATAL_FAILURE(addEchoDelayParam(mEchoDelay)); |
| 189 | SetAndGetParameters(); |
| 190 | } |
| 191 | |
| 192 | TEST_P(AECParamTest, SetAndGetMobileMode) { |
| 193 | EXPECT_NO_FATAL_FAILURE(addMobileModeParam(mMobileMode)); |
| 194 | SetAndGetParameters(); |
| 195 | } |
| 196 | |
| 197 | INSTANTIATE_TEST_SUITE_P(AECParamTest, AECParamTest, |
| 198 | ::testing::Combine(testing::ValuesIn(AECParamTest::kFactoryDescList), |
| 199 | testing::ValuesIn(AECParamTest::kEchoDelayValues), |
| 200 | testing::ValuesIn(AECParamTest::kMobileModeValues)), |
| 201 | [](const testing::TestParamInfo<AECParamTest::ParamType>& info) { |
| 202 | auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second; |
| 203 | std::string name = "Implementor_" + descriptor.common.implementor + |
| 204 | "_name_" + descriptor.common.name + "_UUID_" + |
| 205 | descriptor.common.id.uuid.toString(); |
| 206 | std::replace_if( |
| 207 | name.begin(), name.end(), |
| 208 | [](const char c) { return !std::isalnum(c); }, '_'); |
| 209 | return name; |
| 210 | }); |
| 211 | |
| 212 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AECParamTest); |
| 213 | |
| 214 | int main(int argc, char** argv) { |
| 215 | ::testing::InitGoogleTest(&argc, argv); |
| 216 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 217 | ABinderProcess_startThreadPool(); |
| 218 | return RUN_ALL_TESTS(); |
| 219 | } |