blob: 53b6757220764b9f4cb611737622d8844c584ca9 [file] [log] [blame]
Shunkai Yao5bd4a302022-12-20 15:46:24 +00001/*
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 Yao5bd4a302022-12-20 15:46:24 +000017#include <algorithm>
Shunkai Yao883d75b2022-12-24 05:15:15 +000018#include <string>
Shunkai Yaoab59e6d2022-12-22 00:45:23 +000019#include <unordered_set>
Shunkai Yao5bd4a302022-12-20 15:46:24 +000020
21#define LOG_TAG "VtsHalAECParamTest"
Mikhail Naganov872d4a62023-03-09 18:19:01 -080022#include <android-base/logging.h>
Shunkai Yao5bd4a302022-12-20 15:46:24 +000023
Shunkai Yao5bd4a302022-12-20 15:46:24 +000024#include "EffectHelper.h"
Shunkai Yao0a0c45e2023-02-13 17:41:11 +000025#include "effect-impl/EffectTypes.h"
Shunkai Yao5bd4a302022-12-20 15:46:24 +000026
27using namespace android;
28
29using aidl::android::hardware::audio::effect::AcousticEchoCanceler;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000030using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000031using aidl::android::hardware::audio::effect::getEffectTypeUuidAcousticEchoCanceler;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000032using aidl::android::hardware::audio::effect::IEffect;
33using aidl::android::hardware::audio::effect::IFactory;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000034using aidl::android::hardware::audio::effect::Parameter;
Shunkai Yao0a0c45e2023-02-13 17:41:11 +000035using aidl::android::hardware::audio::effect::Range;
Jaideep Sharma74498412023-09-13 15:25:25 +053036using android::hardware::audio::common::testing::detail::TestExecutionTracer;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000037
38enum ParamName { PARAM_INSTANCE_NAME, PARAM_ECHO_DELAY, PARAM_MOBILE_MODE };
39using AECParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>,
40 int /* echoDelayUs */, bool /* mobileMode */>;
41
42class 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 Sharmab9859032023-07-07 14:35:48 +053054 auto specific = getDefaultParamSpecific();
Shunkai Yao61f9dd22024-05-08 22:34:36 +000055 Parameter::Common common = createParamCommon(
Shunkai Yao5bd4a302022-12-20 15:46:24 +000056 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 Sharmab9859032023-07-07 14:35:48 +053068 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 Yao5bd4a302022-12-20 15:46:24 +000075 Parameter::Specific specific =
76 Parameter::Specific::make<Parameter::Specific::acousticEchoCanceler>(aec);
77 return specific;
78 }
79
Shunkai Yao5bd4a302022-12-20 15:46:24 +000080 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 Yao0a0c45e2023-02-13 17:41:11 +000096 const bool valid =
97 isParameterValid<AcousticEchoCanceler, Range::acousticEchoCanceler>(aec, desc);
Shunkai Yao5bd4a302022-12-20 15:46:24 +000098 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 Yao883d75b2022-12-24 05:15:15 +0000134 static std::unordered_set<bool> getMobileModeValues() { return {true, false}; }
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000135
136 private:
137 std::vector<std::pair<AcousticEchoCanceler::Tag, AcousticEchoCanceler>> mTags;
138 void CleanUp() { mTags.clear(); }
139};
140
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000141TEST_P(AECParamTest, SetAndGetEchoDelay) {
142 EXPECT_NO_FATAL_FAILURE(addEchoDelayParam(mEchoDelay));
143 SetAndGetParameters();
144}
145
146TEST_P(AECParamTest, SetAndGetMobileMode) {
147 EXPECT_NO_FATAL_FAILURE(addMobileModeParam(mMobileMode));
148 SetAndGetParameters();
149}
150
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000151std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
Shunkai Yao883d75b2022-12-24 05:15:15 +0000152INSTANTIATE_TEST_SUITE_P(
153 AECParamTest, AECParamTest,
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000154 ::testing::Combine(
155 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000156 IFactory::descriptor,
157 getEffectTypeUuidAcousticEchoCanceler())),
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000158 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 Yao883d75b2022-12-24 05:15:15 +0000166 [](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 Yao9e60e632023-06-23 04:34:50 +0000170 std::string name =
171 getPrefix(descriptor) + "_EchoDelay_" + echoDelay + "_MobileMode_" + mobileMode;
Shunkai Yao883d75b2022-12-24 05:15:15 +0000172 std::replace_if(
173 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
174 return name;
175 });
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000176
177GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AECParamTest);
178
179int main(int argc, char** argv) {
180 ::testing::InitGoogleTest(&argc, argv);
Jaideep Sharma74498412023-09-13 15:25:25 +0530181 ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000182 ABinderProcess_setThreadPoolMaxThreadCount(1);
183 ABinderProcess_startThreadPool();
184 return RUN_ALL_TESTS();
Mikhail Naganov872d4a62023-03-09 18:19:01 -0800185}