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