blob: 39168b1bf1a917820c7ec80de27455e008ff99d7 [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
Mikhail Naganov872d4a62023-03-09 18:19:01 -080021#include <aidl/Vintf.h>
Shunkai Yao5bd4a302022-12-20 15:46:24 +000022#define LOG_TAG "VtsHalAECParamTest"
Mikhail Naganov872d4a62023-03-09 18:19:01 -080023#include <android-base/logging.h>
Shunkai Yao5bd4a302022-12-20 15:46:24 +000024
Shunkai Yao5bd4a302022-12-20 15:46:24 +000025#include "EffectHelper.h"
Shunkai Yao0a0c45e2023-02-13 17:41:11 +000026#include "effect-impl/EffectTypes.h"
Shunkai Yao5bd4a302022-12-20 15:46:24 +000027
28using namespace android;
29
30using aidl::android::hardware::audio::effect::AcousticEchoCanceler;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000031using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000032using aidl::android::hardware::audio::effect::getEffectTypeUuidAcousticEchoCanceler;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000033using aidl::android::hardware::audio::effect::IEffect;
34using aidl::android::hardware::audio::effect::IFactory;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000035using aidl::android::hardware::audio::effect::Parameter;
Shunkai Yao0a0c45e2023-02-13 17:41:11 +000036using aidl::android::hardware::audio::effect::Range;
Jaideep Sharma74498412023-09-13 15:25:25 +053037using android::hardware::audio::common::testing::detail::TestExecutionTracer;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000038
39enum ParamName { PARAM_INSTANCE_NAME, PARAM_ECHO_DELAY, PARAM_MOBILE_MODE };
40using AECParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>,
41 int /* echoDelayUs */, bool /* mobileMode */>;
42
43class AECParamTest : public ::testing::TestWithParam<AECParamTestParam>, public EffectHelper {
44 public:
45 AECParamTest()
46 : mEchoDelay(std::get<PARAM_ECHO_DELAY>(GetParam())),
47 mMobileMode(std::get<PARAM_MOBILE_MODE>(GetParam())) {
48 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
49 }
50
51 void SetUp() override {
52 ASSERT_NE(nullptr, mFactory);
53 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
54
Jaideep Sharmab9859032023-07-07 14:35:48 +053055 auto specific = getDefaultParamSpecific();
Shunkai Yao5bd4a302022-12-20 15:46:24 +000056 Parameter::Common common = EffectHelper::createParamCommon(
57 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
58 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
59 IEffect::OpenEffectReturn ret;
60 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE));
61 ASSERT_NE(nullptr, mEffect);
62 }
63
64 void TearDown() override {
65 ASSERT_NO_FATAL_FAILURE(close(mEffect));
66 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
67 }
68
Jaideep Sharmab9859032023-07-07 14:35:48 +053069 std::optional<Parameter::Specific> getDefaultParamSpecific() {
70 auto aec = AcousticEchoCanceler::make<AcousticEchoCanceler::echoDelayUs>(0);
71 if (!isParameterValid<AcousticEchoCanceler, Range::acousticEchoCanceler>(aec,
72 mDescriptor)) {
73 return std::nullopt;
74 }
75
Shunkai Yao5bd4a302022-12-20 15:46:24 +000076 Parameter::Specific specific =
77 Parameter::Specific::make<Parameter::Specific::acousticEchoCanceler>(aec);
78 return specific;
79 }
80
Shunkai Yao5bd4a302022-12-20 15:46:24 +000081 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
82 std::shared_ptr<IFactory> mFactory;
83 std::shared_ptr<IEffect> mEffect;
84 Descriptor mDescriptor;
85
86 int mEchoDelay;
87 bool mMobileMode;
88
89 void SetAndGetParameters() {
90 for (auto& it : mTags) {
91 auto& tag = it.first;
92 auto& aec = it.second;
93
94 // validate parameter
95 Descriptor desc;
96 ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
Shunkai Yao0a0c45e2023-02-13 17:41:11 +000097 const bool valid =
98 isParameterValid<AcousticEchoCanceler, Range::acousticEchoCanceler>(aec, desc);
Shunkai Yao5bd4a302022-12-20 15:46:24 +000099 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
100
101 // set parameter
102 Parameter expectParam;
103 Parameter::Specific specific;
104 specific.set<Parameter::Specific::acousticEchoCanceler>(aec);
105 expectParam.set<Parameter::specific>(specific);
106 EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
107
108 // only get if parameter in range and set success
109 if (expected == EX_NONE) {
110 Parameter getParam;
111 Parameter::Id id;
112 AcousticEchoCanceler::Id specificId;
113 specificId.set<AcousticEchoCanceler::Id::commonTag>(tag);
114 id.set<Parameter::Id::acousticEchoCancelerTag>(specificId);
115 EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam));
116
117 EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString()
118 << "\ngetParam:" << getParam.toString();
119 }
120 }
121 }
122
123 void addEchoDelayParam(int delay) {
124 AcousticEchoCanceler aec;
125 aec.set<AcousticEchoCanceler::echoDelayUs>(delay);
126 mTags.push_back({AcousticEchoCanceler::echoDelayUs, aec});
127 }
128
129 void addMobileModeParam(bool mode) {
130 AcousticEchoCanceler aec;
131 aec.set<AcousticEchoCanceler::mobileMode>(mode);
132 mTags.push_back({AcousticEchoCanceler::mobileMode, aec});
133 }
134
Shunkai Yao883d75b2022-12-24 05:15:15 +0000135 static std::unordered_set<bool> getMobileModeValues() { return {true, false}; }
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000136
137 private:
138 std::vector<std::pair<AcousticEchoCanceler::Tag, AcousticEchoCanceler>> mTags;
139 void CleanUp() { mTags.clear(); }
140};
141
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000142TEST_P(AECParamTest, SetAndGetEchoDelay) {
143 EXPECT_NO_FATAL_FAILURE(addEchoDelayParam(mEchoDelay));
144 SetAndGetParameters();
145}
146
147TEST_P(AECParamTest, SetAndGetMobileMode) {
148 EXPECT_NO_FATAL_FAILURE(addMobileModeParam(mMobileMode));
149 SetAndGetParameters();
150}
151
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000152std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
Shunkai Yao883d75b2022-12-24 05:15:15 +0000153INSTANTIATE_TEST_SUITE_P(
154 AECParamTest, AECParamTest,
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000155 ::testing::Combine(
156 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000157 IFactory::descriptor,
158 getEffectTypeUuidAcousticEchoCanceler())),
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000159 testing::ValuesIn(EffectHelper::getTestValueSet<AcousticEchoCanceler, int,
160 Range::acousticEchoCanceler,
161 AcousticEchoCanceler::echoDelayUs>(
162 kDescPair, EffectHelper::expandTestValueBasic<int>)),
163 testing::ValuesIn(EffectHelper::getTestValueSet<AcousticEchoCanceler, bool,
164 Range::acousticEchoCanceler,
165 AcousticEchoCanceler::mobileMode>(
166 kDescPair, EffectHelper::expandTestValueBasic<bool>))),
Shunkai Yao883d75b2022-12-24 05:15:15 +0000167 [](const testing::TestParamInfo<AECParamTest::ParamType>& info) {
168 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
169 std::string echoDelay = std::to_string(std::get<PARAM_ECHO_DELAY>(info.param));
170 std::string mobileMode = std::get<PARAM_MOBILE_MODE>(info.param) ? "true" : "false";
Shunkai Yao9e60e632023-06-23 04:34:50 +0000171 std::string name =
172 getPrefix(descriptor) + "_EchoDelay_" + echoDelay + "_MobileMode_" + mobileMode;
Shunkai Yao883d75b2022-12-24 05:15:15 +0000173 std::replace_if(
174 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
175 return name;
176 });
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000177
178GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AECParamTest);
179
180int main(int argc, char** argv) {
181 ::testing::InitGoogleTest(&argc, argv);
Jaideep Sharma74498412023-09-13 15:25:25 +0530182 ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000183 ABinderProcess_setThreadPoolMaxThreadCount(1);
184 ABinderProcess_startThreadPool();
185 return RUN_ALL_TESTS();
Mikhail Naganov872d4a62023-03-09 18:19:01 -0800186}