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 <vector>
20
21 #define LOG_TAG "VtsHalEqualizerTest"
22 #include <aidl/Gtest.h>
23 #include <aidl/android/hardware/audio/effect/IEffect.h>
24 #include <aidl/android/hardware/audio/effect/IFactory.h>
25 #include <android-base/logging.h>
26 #include <android-base/properties.h>
27 #include <android/binder_interface_utils.h>
28 #include <android/binder_manager.h>
29 #include <android/binder_process.h>
30 #include <gtest/gtest.h>
31
32 #include "EffectHelper.h"
33 #include "TestUtils.h"
34
35 using namespace android;
36
37 using aidl::android::hardware::audio::effect::Descriptor;
38 using aidl::android::hardware::audio::effect::Equalizer;
39 using aidl::android::hardware::audio::effect::getEffectTypeUuidEqualizer;
40 using aidl::android::hardware::audio::effect::IEffect;
41 using aidl::android::hardware::audio::effect::IFactory;
42 using aidl::android::hardware::audio::effect::Parameter;
43 using android::hardware::audio::common::testing::detail::TestExecutionTracer;
44
45 /**
46 * Here we focus on specific effect (equalizer) parameter checking, general IEffect interfaces
47 * testing performed in VtsAudioEfectTargetTest.
48 */
49
50 enum ParamName { PARAM_INSTANCE_NAME, PARAM_PRESET, PARAM_BAND_LEVEL };
51 using EqualizerTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int,
52 std::vector<Equalizer::BandLevel>>;
53
54 /*
55 Testing parameter range, assuming the parameter supported by effect is in this range.
56 This range is verified with IEffect.getDescriptor(), for any index supported vts expect EX_NONE
57 from IEffect.setParameter(), otherwise expect EX_ILLEGAL_ARGUMENT.
58 */
59 const std::vector<int> kBandLevels = {0, -10, 10}; // needs update with implementation
60
61 class EqualizerTestHelper : public EffectHelper {
62 public:
EqualizerTestHelper(std::pair<std::shared_ptr<IFactory>,Descriptor> descPair,int presetIndex=0,std::vector<Equalizer::BandLevel> bandLevel=std::vector<Equalizer::BandLevel>{ Equalizer::BandLevel({.index = 0, .levelMb = 0})})63 EqualizerTestHelper(std::pair<std::shared_ptr<IFactory>, Descriptor> descPair,
64 int presetIndex = 0,
65 std::vector<Equalizer::BandLevel> bandLevel =
66 std::vector<Equalizer::BandLevel>{
67 Equalizer::BandLevel({.index = 0, .levelMb = 0})})
68 : mFactory(descPair.first), mPresetIndex(presetIndex), mBandLevel(bandLevel) {
69 mDescriptor = descPair.second;
70 }
71
SetUpEqualizer()72 void SetUpEqualizer() {
73 ASSERT_NE(nullptr, mFactory);
74 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
75
76 Parameter::Common common = createParamCommon(
77 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
78 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
79 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt, &mOpenEffectReturn, EX_NONE));
80 ASSERT_NE(nullptr, mEffect);
81 }
82
TearDownEqualizer()83 void TearDownEqualizer() {
84 ASSERT_NO_FATAL_FAILURE(close(mEffect));
85 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
86 mOpenEffectReturn = IEffect::OpenEffectReturn{};
87 }
88
SetAndGetEqualizerParameters()89 void SetAndGetEqualizerParameters() {
90 ASSERT_NE(nullptr, mEffect);
91 for (auto& it : mTags) {
92 auto& tag = it.first;
93 auto& eq = it.second;
94
95 // validate parameter
96 const bool valid = isParameterValid<Equalizer, Range::equalizer>(eq, mDescriptor);
97 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
98
99 // set
100 Parameter::Specific specific =
101 Parameter::Specific::make<Parameter::Specific::equalizer>(eq);
102 Parameter expectParam = Parameter::make<Parameter::specific>(specific);
103 EXPECT_STATUS(expected, mEffect->setParameter(expectParam))
104 << expectParam.toString() << "\n"
105 << mDescriptor.toString();
106
107 // only get if parameter in range and set success
108 if (expected == EX_NONE) {
109 Parameter getParam;
110 Equalizer::Id eqId = Equalizer::Id::make<Equalizer::Id::commonTag>(tag);
111 Parameter::Id id = Parameter::Id::make<Parameter::Id::equalizerTag>(eqId);
112 // if set success, then get should match
113 EXPECT_STATUS(expected, mEffect->getParameter(id, &getParam));
114 EXPECT_TRUE(isEqParameterExpected(expectParam, getParam))
115 << "\nexpect:" << expectParam.toString()
116 << "\ngetParam:" << getParam.toString();
117 }
118 }
119 }
120
isEqParameterExpected(const Parameter & expect,const Parameter & target)121 bool isEqParameterExpected(const Parameter& expect, const Parameter& target) {
122 // if parameter same, then for sure they are matched
123 if (expect == target) return true;
124
125 // if not, see if target include the expect parameter, and others all default (0).
126 /*
127 * This is to verify the case of client setParameter to a single bandLevel ({3, -1} for
128 * example), and return of getParameter must be [{0, 0}, {1, 0}, {2, 0}, {3, -1}, {4, 0}]
129 */
130 EXPECT_EQ(expect.getTag(), Parameter::specific);
131 EXPECT_EQ(target.getTag(), Parameter::specific);
132
133 Parameter::Specific expectSpec = expect.get<Parameter::specific>(),
134 targetSpec = target.get<Parameter::specific>();
135 EXPECT_EQ(expectSpec.getTag(), Parameter::Specific::equalizer);
136 EXPECT_EQ(targetSpec.getTag(), Parameter::Specific::equalizer);
137
138 Equalizer expectEq = expectSpec.get<Parameter::Specific::equalizer>(),
139 targetEq = targetSpec.get<Parameter::Specific::equalizer>();
140 EXPECT_EQ(expectEq.getTag(), targetEq.getTag());
141
142 auto eqTag = targetEq.getTag();
143 switch (eqTag) {
144 case Equalizer::bandLevels: {
145 auto expectBl = expectEq.get<Equalizer::bandLevels>();
146 std::sort(expectBl.begin(), expectBl.end(),
147 [](const auto& a, const auto& b) { return a.index < b.index; });
148 expectBl.erase(std::unique(expectBl.begin(), expectBl.end()), expectBl.end());
149 auto targetBl = targetEq.get<Equalizer::bandLevels>();
150 return std::includes(targetBl.begin(), targetBl.end(), expectBl.begin(),
151 expectBl.end());
152 }
153 case Equalizer::preset: {
154 return expectEq.get<Equalizer::preset>() == targetEq.get<Equalizer::preset>();
155 }
156 default:
157 return false;
158 }
159 return false;
160 }
161
addPresetParam(int preset)162 void addPresetParam(int preset) {
163 mTags.push_back({Equalizer::preset, Equalizer::make<Equalizer::preset>(preset)});
164 }
165
addBandLevelsParam(const std::vector<Equalizer::BandLevel> & bandLevels)166 void addBandLevelsParam(const std::vector<Equalizer::BandLevel>& bandLevels) {
167 mTags.push_back(
168 {Equalizer::bandLevels, Equalizer::make<Equalizer::bandLevels>(bandLevels)});
169 }
170
171 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
172 const std::shared_ptr<IFactory> mFactory;
173 const int mPresetIndex;
174 const std::vector<Equalizer::BandLevel> mBandLevel;
175 std::shared_ptr<IEffect> mEffect;
176 IEffect::OpenEffectReturn mOpenEffectReturn;
177
178 private:
179 std::vector<std::pair<Equalizer::Tag, Equalizer>> mTags;
CleanUp()180 void CleanUp() { mTags.clear(); }
181 };
182
183 class EqualizerParamTest : public ::testing::TestWithParam<EqualizerTestParam>,
184 public EqualizerTestHelper {
185 public:
EqualizerParamTest()186 EqualizerParamTest()
187 : EqualizerTestHelper(std::get<PARAM_INSTANCE_NAME>(GetParam()),
188 std::get<PARAM_PRESET>(GetParam()),
189 std::get<PARAM_BAND_LEVEL>(GetParam())) {}
190
SetUp()191 void SetUp() override { ASSERT_NO_FATAL_FAILURE(SetUpEqualizer()); }
192
TearDown()193 void TearDown() override { ASSERT_NO_FATAL_FAILURE(TearDownEqualizer()); }
194 };
195
TEST_P(EqualizerParamTest,SetAndGetParams)196 TEST_P(EqualizerParamTest, SetAndGetParams) {
197 addBandLevelsParam(mBandLevel);
198 addPresetParam(mPresetIndex);
199 ASSERT_NO_FATAL_FAILURE(SetAndGetEqualizerParameters());
200 }
201
202 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
203 INSTANTIATE_TEST_SUITE_P(
204 EqualizerParamTest, EqualizerParamTest,
205 ::testing::Combine(
206 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
207 IFactory::descriptor, getEffectTypeUuidEqualizer())),
208 testing::ValuesIn(EffectHelper::getTestValueSet<Equalizer, int, Range::equalizer,
209 Equalizer::preset>(
210 kDescPair, EffectHelper::expandTestValueBasic<int>)),
211 testing::ValuesIn(
212 EffectHelper::getTestValueSet<Equalizer, std::vector<Equalizer::BandLevel>,
213 Range::equalizer, Equalizer::bandLevels>(
214 kDescPair,
__anon0f8b1dd70202(std::set<std::vector<Equalizer::BandLevel>>& bandLevels) 215 [](std::set<std::vector<Equalizer::BandLevel>>& bandLevels) {
216 return bandLevels;
217 }))),
__anon0f8b1dd70302(const testing::TestParamInfo<EqualizerParamTest::ParamType>& info) 218 [](const testing::TestParamInfo<EqualizerParamTest::ParamType>& info) {
219 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
220 std::string bandLevel =
221 ::android::internal::ToString(std::get<PARAM_BAND_LEVEL>(info.param));
222 std::string name = getPrefix(descriptor) + "_preset_" +
223 std::to_string(std::get<PARAM_PRESET>(info.param)) + "_bandLevel_" +
224 bandLevel;
225 std::replace_if(
226 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
227 return name;
228 });
229 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EqualizerParamTest);
230
main(int argc,char ** argv)231 int main(int argc, char** argv) {
232 ::testing::InitGoogleTest(&argc, argv);
233 ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
234 ABinderProcess_setThreadPoolMaxThreadCount(1);
235 ABinderProcess_startThreadPool();
236 return RUN_ALL_TESTS();
237 }
238