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