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 | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Shunkai Yao | 883d75b | 2022-12-24 05:15:15 +0000 | [diff] [blame] | 18 | #include <string> |
Shunkai Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame] | 19 | #include <unordered_set> |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 20 | |
| 21 | #define LOG_TAG "VtsHalAECParamTest" |
Mikhail Naganov | 872d4a6 | 2023-03-09 18:19:01 -0800 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 23 | |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 24 | #include "EffectHelper.h" |
Shunkai Yao | 0a0c45e | 2023-02-13 17:41:11 +0000 | [diff] [blame] | 25 | #include "effect-impl/EffectTypes.h" |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace android; |
| 28 | |
| 29 | using aidl::android::hardware::audio::effect::AcousticEchoCanceler; |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 30 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 31 | using aidl::android::hardware::audio::effect::getEffectTypeUuidAcousticEchoCanceler; |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 32 | using aidl::android::hardware::audio::effect::IEffect; |
| 33 | using aidl::android::hardware::audio::effect::IFactory; |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 34 | using aidl::android::hardware::audio::effect::Parameter; |
Shunkai Yao | 0a0c45e | 2023-02-13 17:41:11 +0000 | [diff] [blame] | 35 | using aidl::android::hardware::audio::effect::Range; |
Jaideep Sharma | 7449841 | 2023-09-13 15:25:25 +0530 | [diff] [blame] | 36 | using android::hardware::audio::common::testing::detail::TestExecutionTracer; |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 37 | |
| 38 | enum ParamName { PARAM_INSTANCE_NAME, PARAM_ECHO_DELAY, PARAM_MOBILE_MODE }; |
| 39 | using AECParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, |
| 40 | int /* echoDelayUs */, bool /* mobileMode */>; |
| 41 | |
| 42 | class AECParamTest : public ::testing::TestWithParam<AECParamTestParam>, public EffectHelper { |
| 43 | public: |
| 44 | AECParamTest() |
| 45 | : mEchoDelay(std::get<PARAM_ECHO_DELAY>(GetParam())), |
| 46 | mMobileMode(std::get<PARAM_MOBILE_MODE>(GetParam())) { |
| 47 | std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam()); |
| 48 | } |
| 49 | |
| 50 | void SetUp() override { |
| 51 | ASSERT_NE(nullptr, mFactory); |
| 52 | ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor)); |
| 53 | |
Jaideep Sharma | b985903 | 2023-07-07 14:35:48 +0530 | [diff] [blame] | 54 | auto specific = getDefaultParamSpecific(); |
Shunkai Yao | 61f9dd2 | 2024-05-08 22:34:36 +0000 | [diff] [blame] | 55 | Parameter::Common common = createParamCommon( |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 56 | 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */, |
| 57 | kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */); |
| 58 | IEffect::OpenEffectReturn ret; |
| 59 | ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE)); |
| 60 | ASSERT_NE(nullptr, mEffect); |
| 61 | } |
| 62 | |
| 63 | void TearDown() override { |
| 64 | ASSERT_NO_FATAL_FAILURE(close(mEffect)); |
| 65 | ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect)); |
| 66 | } |
| 67 | |
Jaideep Sharma | b985903 | 2023-07-07 14:35:48 +0530 | [diff] [blame] | 68 | std::optional<Parameter::Specific> getDefaultParamSpecific() { |
| 69 | auto aec = AcousticEchoCanceler::make<AcousticEchoCanceler::echoDelayUs>(0); |
| 70 | if (!isParameterValid<AcousticEchoCanceler, Range::acousticEchoCanceler>(aec, |
| 71 | mDescriptor)) { |
| 72 | return std::nullopt; |
| 73 | } |
| 74 | |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 75 | Parameter::Specific specific = |
| 76 | Parameter::Specific::make<Parameter::Specific::acousticEchoCanceler>(aec); |
| 77 | return specific; |
| 78 | } |
| 79 | |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 80 | static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100; |
| 81 | std::shared_ptr<IFactory> mFactory; |
| 82 | std::shared_ptr<IEffect> mEffect; |
| 83 | Descriptor mDescriptor; |
| 84 | |
| 85 | int mEchoDelay; |
| 86 | bool mMobileMode; |
| 87 | |
| 88 | void SetAndGetParameters() { |
| 89 | for (auto& it : mTags) { |
| 90 | auto& tag = it.first; |
| 91 | auto& aec = it.second; |
| 92 | |
| 93 | // validate parameter |
| 94 | Descriptor desc; |
| 95 | ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc)); |
Shunkai Yao | 0a0c45e | 2023-02-13 17:41:11 +0000 | [diff] [blame] | 96 | const bool valid = |
| 97 | isParameterValid<AcousticEchoCanceler, Range::acousticEchoCanceler>(aec, desc); |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 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::acousticEchoCanceler>(aec); |
| 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 | AcousticEchoCanceler::Id specificId; |
| 112 | specificId.set<AcousticEchoCanceler::Id::commonTag>(tag); |
| 113 | id.set<Parameter::Id::acousticEchoCancelerTag>(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 addEchoDelayParam(int delay) { |
| 123 | AcousticEchoCanceler aec; |
| 124 | aec.set<AcousticEchoCanceler::echoDelayUs>(delay); |
| 125 | mTags.push_back({AcousticEchoCanceler::echoDelayUs, aec}); |
| 126 | } |
| 127 | |
| 128 | void addMobileModeParam(bool mode) { |
| 129 | AcousticEchoCanceler aec; |
| 130 | aec.set<AcousticEchoCanceler::mobileMode>(mode); |
| 131 | mTags.push_back({AcousticEchoCanceler::mobileMode, aec}); |
| 132 | } |
| 133 | |
Shunkai Yao | 883d75b | 2022-12-24 05:15:15 +0000 | [diff] [blame] | 134 | static std::unordered_set<bool> getMobileModeValues() { return {true, false}; } |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 135 | |
| 136 | private: |
| 137 | std::vector<std::pair<AcousticEchoCanceler::Tag, AcousticEchoCanceler>> mTags; |
| 138 | void CleanUp() { mTags.clear(); } |
| 139 | }; |
| 140 | |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 141 | TEST_P(AECParamTest, SetAndGetEchoDelay) { |
| 142 | EXPECT_NO_FATAL_FAILURE(addEchoDelayParam(mEchoDelay)); |
| 143 | SetAndGetParameters(); |
| 144 | } |
| 145 | |
| 146 | TEST_P(AECParamTest, SetAndGetMobileMode) { |
| 147 | EXPECT_NO_FATAL_FAILURE(addMobileModeParam(mMobileMode)); |
| 148 | SetAndGetParameters(); |
| 149 | } |
| 150 | |
Shunkai Yao | 0a0c45e | 2023-02-13 17:41:11 +0000 | [diff] [blame] | 151 | std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair; |
Shunkai Yao | 883d75b | 2022-12-24 05:15:15 +0000 | [diff] [blame] | 152 | INSTANTIATE_TEST_SUITE_P( |
| 153 | AECParamTest, AECParamTest, |
Shunkai Yao | 0a0c45e | 2023-02-13 17:41:11 +0000 | [diff] [blame] | 154 | ::testing::Combine( |
| 155 | testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors( |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 156 | IFactory::descriptor, |
| 157 | getEffectTypeUuidAcousticEchoCanceler())), |
Shunkai Yao | 0a0c45e | 2023-02-13 17:41:11 +0000 | [diff] [blame] | 158 | testing::ValuesIn(EffectHelper::getTestValueSet<AcousticEchoCanceler, int, |
| 159 | Range::acousticEchoCanceler, |
| 160 | AcousticEchoCanceler::echoDelayUs>( |
| 161 | kDescPair, EffectHelper::expandTestValueBasic<int>)), |
| 162 | testing::ValuesIn(EffectHelper::getTestValueSet<AcousticEchoCanceler, bool, |
| 163 | Range::acousticEchoCanceler, |
| 164 | AcousticEchoCanceler::mobileMode>( |
| 165 | kDescPair, EffectHelper::expandTestValueBasic<bool>))), |
Shunkai Yao | 883d75b | 2022-12-24 05:15:15 +0000 | [diff] [blame] | 166 | [](const testing::TestParamInfo<AECParamTest::ParamType>& info) { |
| 167 | auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second; |
| 168 | std::string echoDelay = std::to_string(std::get<PARAM_ECHO_DELAY>(info.param)); |
| 169 | std::string mobileMode = std::get<PARAM_MOBILE_MODE>(info.param) ? "true" : "false"; |
Shunkai Yao | 9e60e63 | 2023-06-23 04:34:50 +0000 | [diff] [blame] | 170 | std::string name = |
| 171 | getPrefix(descriptor) + "_EchoDelay_" + echoDelay + "_MobileMode_" + mobileMode; |
Shunkai Yao | 883d75b | 2022-12-24 05:15:15 +0000 | [diff] [blame] | 172 | std::replace_if( |
| 173 | name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_'); |
| 174 | return name; |
| 175 | }); |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 176 | |
| 177 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AECParamTest); |
| 178 | |
| 179 | int main(int argc, char** argv) { |
| 180 | ::testing::InitGoogleTest(&argc, argv); |
Jaideep Sharma | 7449841 | 2023-09-13 15:25:25 +0530 | [diff] [blame] | 181 | ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer()); |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 182 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 183 | ABinderProcess_startThreadPool(); |
| 184 | return RUN_ALL_TESTS(); |
Mikhail Naganov | 872d4a6 | 2023-03-09 18:19:01 -0800 | [diff] [blame] | 185 | } |