• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #include <algorithm>
18 #include <string>
19 #include <unordered_set>
20 
21 #define LOG_TAG "VtsHalAECParamTest"
22 #include <android-base/logging.h>
23 
24 #include "EffectHelper.h"
25 #include "effect-impl/EffectTypes.h"
26 
27 using namespace android;
28 
29 using aidl::android::hardware::audio::effect::AcousticEchoCanceler;
30 using aidl::android::hardware::audio::effect::Descriptor;
31 using aidl::android::hardware::audio::effect::getEffectTypeUuidAcousticEchoCanceler;
32 using aidl::android::hardware::audio::effect::IEffect;
33 using aidl::android::hardware::audio::effect::IFactory;
34 using aidl::android::hardware::audio::effect::Parameter;
35 using aidl::android::hardware::audio::effect::Range;
36 using android::hardware::audio::common::testing::detail::TestExecutionTracer;
37 
38 enum ParamName { PARAM_INSTANCE_NAME, PARAM_ECHO_DELAY, PARAM_MOBILE_MODE };
39 using AECParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>,
40                                      int /* echoDelayUs */, bool /* mobileMode */>;
41 
42 class AECParamTest : public ::testing::TestWithParam<AECParamTestParam>, public EffectHelper {
43   public:
AECParamTest()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 
SetUp()50     void SetUp() override {
51         ASSERT_NE(nullptr, mFactory);
52         ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
53 
54         auto specific = getDefaultParamSpecific();
55         Parameter::Common common = 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 
TearDown()63     void TearDown() override {
64         ASSERT_NO_FATAL_FAILURE(close(mEffect));
65         ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
66     }
67 
getDefaultParamSpecific()68     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 
75         Parameter::Specific specific =
76                 Parameter::Specific::make<Parameter::Specific::acousticEchoCanceler>(aec);
77         return specific;
78     }
79 
80     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 
SetAndGetParameters()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));
96             const bool valid =
97                     isParameterValid<AcousticEchoCanceler, Range::acousticEchoCanceler>(aec, desc);
98             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 
addEchoDelayParam(int delay)122     void addEchoDelayParam(int delay) {
123         AcousticEchoCanceler aec;
124         aec.set<AcousticEchoCanceler::echoDelayUs>(delay);
125         mTags.push_back({AcousticEchoCanceler::echoDelayUs, aec});
126     }
127 
addMobileModeParam(bool mode)128     void addMobileModeParam(bool mode) {
129         AcousticEchoCanceler aec;
130         aec.set<AcousticEchoCanceler::mobileMode>(mode);
131         mTags.push_back({AcousticEchoCanceler::mobileMode, aec});
132     }
133 
getMobileModeValues()134     static std::unordered_set<bool> getMobileModeValues() { return {true, false}; }
135 
136   private:
137     std::vector<std::pair<AcousticEchoCanceler::Tag, AcousticEchoCanceler>> mTags;
CleanUp()138     void CleanUp() { mTags.clear(); }
139 };
140 
TEST_P(AECParamTest,SetAndGetEchoDelay)141 TEST_P(AECParamTest, SetAndGetEchoDelay) {
142     EXPECT_NO_FATAL_FAILURE(addEchoDelayParam(mEchoDelay));
143     SetAndGetParameters();
144 }
145 
TEST_P(AECParamTest,SetAndGetMobileMode)146 TEST_P(AECParamTest, SetAndGetMobileMode) {
147     EXPECT_NO_FATAL_FAILURE(addMobileModeParam(mMobileMode));
148     SetAndGetParameters();
149 }
150 
151 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
152 INSTANTIATE_TEST_SUITE_P(
153         AECParamTest, AECParamTest,
154         ::testing::Combine(
155                 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
156                                           IFactory::descriptor,
157                                           getEffectTypeUuidAcousticEchoCanceler())),
158                 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>))),
__anonae4fcdec0102(const testing::TestParamInfo<AECParamTest::ParamType>& info) 166         [](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";
170             std::string name =
171                     getPrefix(descriptor) + "_EchoDelay_" + echoDelay + "_MobileMode_" + mobileMode;
172             std::replace_if(
173                     name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
174             return name;
175         });
176 
177 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AECParamTest);
178 
main(int argc,char ** argv)179 int main(int argc, char** argv) {
180     ::testing::InitGoogleTest(&argc, argv);
181     ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
182     ABinderProcess_setThreadPoolMaxThreadCount(1);
183     ABinderProcess_startThreadPool();
184     return RUN_ALL_TESTS();
185 }
186