blob: 4f658f227bceabebe1396eadf11cb1a00b355537 [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;
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
54 Parameter::Specific specific = getDefaultParamSpecific();
55 Parameter::Common common = EffectHelper::createParamCommon(
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
68 Parameter::Specific getDefaultParamSpecific() {
69 AcousticEchoCanceler aec = AcousticEchoCanceler::make<AcousticEchoCanceler::echoDelayUs>(0);
70 Parameter::Specific specific =
71 Parameter::Specific::make<Parameter::Specific::acousticEchoCanceler>(aec);
72 return specific;
73 }
74
Shunkai Yao5bd4a302022-12-20 15:46:24 +000075 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
76 std::shared_ptr<IFactory> mFactory;
77 std::shared_ptr<IEffect> mEffect;
78 Descriptor mDescriptor;
79
80 int mEchoDelay;
81 bool mMobileMode;
82
83 void SetAndGetParameters() {
84 for (auto& it : mTags) {
85 auto& tag = it.first;
86 auto& aec = it.second;
87
88 // validate parameter
89 Descriptor desc;
90 ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
Shunkai Yao0a0c45e2023-02-13 17:41:11 +000091 const bool valid =
92 isParameterValid<AcousticEchoCanceler, Range::acousticEchoCanceler>(aec, desc);
Shunkai Yao5bd4a302022-12-20 15:46:24 +000093 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
94
95 // set parameter
96 Parameter expectParam;
97 Parameter::Specific specific;
98 specific.set<Parameter::Specific::acousticEchoCanceler>(aec);
99 expectParam.set<Parameter::specific>(specific);
100 EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
101
102 // only get if parameter in range and set success
103 if (expected == EX_NONE) {
104 Parameter getParam;
105 Parameter::Id id;
106 AcousticEchoCanceler::Id specificId;
107 specificId.set<AcousticEchoCanceler::Id::commonTag>(tag);
108 id.set<Parameter::Id::acousticEchoCancelerTag>(specificId);
109 EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam));
110
111 EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString()
112 << "\ngetParam:" << getParam.toString();
113 }
114 }
115 }
116
117 void addEchoDelayParam(int delay) {
118 AcousticEchoCanceler aec;
119 aec.set<AcousticEchoCanceler::echoDelayUs>(delay);
120 mTags.push_back({AcousticEchoCanceler::echoDelayUs, aec});
121 }
122
123 void addMobileModeParam(bool mode) {
124 AcousticEchoCanceler aec;
125 aec.set<AcousticEchoCanceler::mobileMode>(mode);
126 mTags.push_back({AcousticEchoCanceler::mobileMode, aec});
127 }
128
Shunkai Yao883d75b2022-12-24 05:15:15 +0000129 static std::unordered_set<bool> getMobileModeValues() { return {true, false}; }
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000130
131 private:
132 std::vector<std::pair<AcousticEchoCanceler::Tag, AcousticEchoCanceler>> mTags;
133 void CleanUp() { mTags.clear(); }
134};
135
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000136TEST_P(AECParamTest, SetAndGetEchoDelay) {
137 EXPECT_NO_FATAL_FAILURE(addEchoDelayParam(mEchoDelay));
138 SetAndGetParameters();
139}
140
141TEST_P(AECParamTest, SetAndGetMobileMode) {
142 EXPECT_NO_FATAL_FAILURE(addMobileModeParam(mMobileMode));
143 SetAndGetParameters();
144}
145
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000146std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
Shunkai Yao883d75b2022-12-24 05:15:15 +0000147INSTANTIATE_TEST_SUITE_P(
148 AECParamTest, AECParamTest,
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000149 ::testing::Combine(
150 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000151 IFactory::descriptor,
152 getEffectTypeUuidAcousticEchoCanceler())),
Shunkai Yao0a0c45e2023-02-13 17:41:11 +0000153 testing::ValuesIn(EffectHelper::getTestValueSet<AcousticEchoCanceler, int,
154 Range::acousticEchoCanceler,
155 AcousticEchoCanceler::echoDelayUs>(
156 kDescPair, EffectHelper::expandTestValueBasic<int>)),
157 testing::ValuesIn(EffectHelper::getTestValueSet<AcousticEchoCanceler, bool,
158 Range::acousticEchoCanceler,
159 AcousticEchoCanceler::mobileMode>(
160 kDescPair, EffectHelper::expandTestValueBasic<bool>))),
Shunkai Yao883d75b2022-12-24 05:15:15 +0000161 [](const testing::TestParamInfo<AECParamTest::ParamType>& info) {
162 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
163 std::string echoDelay = std::to_string(std::get<PARAM_ECHO_DELAY>(info.param));
164 std::string mobileMode = std::get<PARAM_MOBILE_MODE>(info.param) ? "true" : "false";
Jaideep Sharmae4c7a962023-06-14 19:14:44 +0530165 std::string name = getPrefix(descriptor) + "_EchoDelay_" + echoDelay +
Shunkai Yao883d75b2022-12-24 05:15:15 +0000166 "_MobileMode_" + mobileMode;
167 std::replace_if(
168 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
169 return name;
170 });
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000171
172GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AECParamTest);
173
174int main(int argc, char** argv) {
175 ::testing::InitGoogleTest(&argc, argv);
176 ABinderProcess_setThreadPoolMaxThreadCount(1);
177 ABinderProcess_startThreadPool();
178 return RUN_ALL_TESTS();
Mikhail Naganov872d4a62023-03-09 18:19:01 -0800179}