blob: 4162551ba44023d8ab0f13f65be10369b8f200dc [file] [log] [blame]
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +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
17#include <algorithm>
18#include <limits>
19#include <map>
20#include <memory>
21#include <string>
22#include <vector>
23
24#define LOG_TAG "VtsHalEqualizerTest"
25
26#include <aidl/Gtest.h>
27#include <aidl/Vintf.h>
28#include <android-base/logging.h>
29#include <android-base/properties.h>
30#include <android/binder_interface_utils.h>
31#include <android/binder_manager.h>
32#include <android/binder_process.h>
33#include <gtest/gtest.h>
34
35#include <Utils.h>
36#include <aidl/android/hardware/audio/effect/IEffect.h>
37#include <aidl/android/hardware/audio/effect/IFactory.h>
38#include <aidl/android/media/audio/common/AudioChannelLayout.h>
39#include <aidl/android/media/audio/common/AudioDeviceType.h>
40
41#include "AudioHalBinderServiceUtil.h"
42#include "EffectHelper.h"
43#include "TestUtils.h"
44#include "effect-impl/EffectUUID.h"
45
46using namespace android;
47
48using aidl::android::hardware::audio::effect::Capability;
49using aidl::android::hardware::audio::effect::Descriptor;
50using aidl::android::hardware::audio::effect::EffectNullUuid;
51using aidl::android::hardware::audio::effect::Equalizer;
52using aidl::android::hardware::audio::effect::EqualizerTypeUUID;
53using aidl::android::hardware::audio::effect::IEffect;
54using aidl::android::hardware::audio::effect::IFactory;
55using aidl::android::hardware::audio::effect::Parameter;
56
57/**
58 * Here we focus on specific parameter checking, general IEffect interfaces testing performed in
59 * VtsAudioEfectTargetTest.
60 */
Shunkai Yao464775e2022-10-28 21:42:25 +000061enum ParamName { PARAM_INSTANCE_NAME, PARAM_PRESET_INDEX, PARAM_BAND_INDEX, PARAM_BAND_LEVEL };
62using EqualizerParamTestParam = std::tuple<std::string, int, int, int>;
63
64/*
65Testing parameter range, assuming the parameter supported by effect is in this range.
66This range is verified with IEffect.getDescriptor(), for any index supported vts expect EX_NONE
67from IEffect.setParameter(), otherwise expect EX_ILLEGAL_ARGUMENT.
68*/
69constexpr std::pair<int, int> kPresetIndexRange = {-1, 10}; // valid range [0, 9]
70constexpr std::pair<int, int> kBandIndexRange = {-1, 5}; // valid range [0, 4]
71constexpr std::pair<int, int> kBandLevelRange = {-5, 5}; // needs update with implementation
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000072
73class EqualizerParamTest : public ::testing::TestWithParam<EqualizerParamTestParam>,
74 public EffectHelper {
75 public:
76 EqualizerParamTest()
Shunkai Yao464775e2022-10-28 21:42:25 +000077 : EffectHelper(std::get<PARAM_INSTANCE_NAME>(GetParam())),
78 mParamPresetIndex(std::get<PARAM_PRESET_INDEX>(GetParam())),
79 mParamBandIndex(std::get<PARAM_BAND_INDEX>(GetParam())),
80 mParamBandLevel(std::get<PARAM_BAND_LEVEL>(GetParam())) {}
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000081
82 void SetUp() override {
83 CreateEffectsWithUUID(EqualizerTypeUUID);
84 initParamCommonFormat();
85 initParamCommon();
86 initParamSpecific();
87 OpenEffects(EqualizerTypeUUID);
88 SCOPED_TRACE(testing::Message() << "preset: " << mParamPresetIndex << " bandIdx "
89 << mParamBandIndex << " level " << mParamBandLevel);
90 }
91
92 void TearDown() override {
93 CloseEffects();
94 DestroyEffects();
95 CleanUp();
96 }
97
Shunkai Yao6afc8552022-10-26 22:47:20 +000098 int mParamPresetIndex = 0;
99 int mParamBandIndex = 0;
100 int mParamBandLevel = 0;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000101
102 void SetAndGetEqualizerParameters() {
103 auto functor = [&](const std::shared_ptr<IEffect>& effect) {
104 for (auto& it : mTags) {
105 auto& tag = it.first;
106 auto& eq = it.second;
107
108 // validate parameter
109 Descriptor desc;
110 ASSERT_STATUS(EX_NONE, effect->getDescriptor(&desc));
111 const bool valid = isTagInRange(it.first, it.second, desc);
112 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
113
114 // set
115 Parameter expectParam;
116 Parameter::Specific specific;
117 specific.set<Parameter::Specific::equalizer>(*eq.get());
118 expectParam.set<Parameter::specific>(specific);
119 EXPECT_STATUS(expected, effect->setParameter(expectParam))
120 << expectParam.toString();
121
Shunkai Yao464775e2022-10-28 21:42:25 +0000122 // only get if parameter in range and set success
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000123 if (expected == EX_NONE) {
124 Parameter getParam;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000125 Parameter::Id id;
126 Equalizer::Id eqId;
127 eqId.set<Equalizer::Id::commonTag>(tag);
128 id.set<Parameter::Id::equalizerTag>(eqId);
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000129 // if set success, then get should match
130 EXPECT_STATUS(expected, effect->getParameter(id, &getParam));
Shunkai Yao6afc8552022-10-26 22:47:20 +0000131 EXPECT_TRUE(isEqParameterExpected(expectParam, getParam));
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000132 }
133 }
134 };
135 EXPECT_NO_FATAL_FAILURE(ForEachEffect(functor));
136 }
137
Shunkai Yao6afc8552022-10-26 22:47:20 +0000138 bool isEqParameterExpected(const Parameter& expect, const Parameter& target) {
139 // if parameter same, then for sure matched
140 if (expect == target) return true;
141
142 // if not, see if target include the expect parameter, and others all default (0).
143 /*
144 This is verify the case of client setParameter to a single bandLevel ({3, -1} for
145 example), and return of getParameter must be [{0, 0}, {1, 0}, {2, 0}, {3, -1}, {4, 0}]
146 */
147 EXPECT_EQ(expect.getTag(), Parameter::specific);
148 EXPECT_EQ(target.getTag(), Parameter::specific);
149
150 Parameter::Specific expectSpec = expect.get<Parameter::specific>(),
151 targetSpec = target.get<Parameter::specific>();
152 EXPECT_EQ(expectSpec.getTag(), Parameter::Specific::equalizer);
153 EXPECT_EQ(targetSpec.getTag(), Parameter::Specific::equalizer);
154
155 Equalizer expectEq = expectSpec.get<Parameter::Specific::equalizer>(),
156 targetEq = targetSpec.get<Parameter::Specific::equalizer>();
157 EXPECT_EQ(expectEq.getTag(), targetEq.getTag());
158
159 auto eqTag = targetEq.getTag();
160 switch (eqTag) {
161 case Equalizer::bandLevels: {
162 auto expectBl = expectEq.get<Equalizer::bandLevels>();
163 auto targetBl = targetEq.get<Equalizer::bandLevels>();
164 return std::includes(targetBl.begin(), targetBl.end(), expectBl.begin(),
165 expectBl.end());
166 }
167 default:
168 return false;
169 }
170 return false;
171 }
172
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000173 void addPresetParam(int preset) {
174 Equalizer eq;
175 eq.set<Equalizer::preset>(preset);
176 mTags.push_back({Equalizer::preset, std::make_unique<Equalizer>(std::move(eq))});
177 }
178
179 void addBandLevelsParam(std::vector<Equalizer::BandLevel>& bandLevels) {
180 Equalizer eq;
181 eq.set<Equalizer::bandLevels>(bandLevels);
182 mTags.push_back({Equalizer::bandLevels, std::make_unique<Equalizer>(std::move(eq))});
183 }
184
185 bool isTagInRange(const Equalizer::Tag& tag, const std::unique_ptr<Equalizer>& eq,
186 const Descriptor& desc) const {
187 std::cout << "xxx" << toString(tag) << " " << desc.toString();
188 const Equalizer::Capability& eqCap = desc.capability.get<Capability::equalizer>();
189 switch (tag) {
190 case Equalizer::preset: {
191 int index = eq->get<Equalizer::preset>();
192 return isPresetIndexInRange(eqCap, index);
193 }
194 case Equalizer::bandLevels: {
195 auto& bandLevel = eq->get<Equalizer::bandLevels>();
196 return isBandIndexInRange(eqCap, bandLevel);
197 }
198 default:
199 return false;
200 }
201 return false;
202 }
203
204 bool isPresetIndexInRange(const Equalizer::Capability& cap, int idx) const {
205 const auto [min, max] =
206 std::minmax_element(cap.presets.begin(), cap.presets.end(),
207 [](const auto& a, const auto& b) { return a.index < b.index; });
208 return idx >= min->index && idx <= max->index;
209 }
210
211 bool isBandIndexInRange(const Equalizer::Capability& cap,
212 const std::vector<Equalizer::BandLevel>& bandLevel) const {
213 for (auto& it : bandLevel) {
214 if (!isBandIndexInRange(cap, it.index)) return false;
215 }
216 return true;
217 }
218
219 bool isBandIndexInRange(const Equalizer::Capability& cap, int idx) const {
220 const auto [min, max] =
221 std::minmax_element(cap.bandFrequencies.begin(), cap.bandFrequencies.end(),
222 [](const auto& a, const auto& b) { return a.index < b.index; });
223 return idx >= min->index && idx <= max->index;
224 }
225
226 private:
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000227 std::vector<std::pair<Equalizer::Tag, std::unique_ptr<Equalizer>>> mTags;
228
229 bool validCapabilityTag(Capability& cap) { return cap.getTag() == Capability::equalizer; }
230
231 void initParamSpecific() {
232 Equalizer eq;
233 eq.set<Equalizer::preset>(0);
234 Parameter::Specific specific;
235 specific.set<Parameter::Specific::equalizer>(eq);
236 setSpecific(specific);
237 }
238
239 void CleanUp() { mTags.clear(); }
240};
241
242TEST_P(EqualizerParamTest, SetAndGetPreset) {
243 EXPECT_NO_FATAL_FAILURE(addPresetParam(mParamPresetIndex));
244 SetAndGetEqualizerParameters();
245}
246
247TEST_P(EqualizerParamTest, SetAndGetSingleBand) {
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000248 std::vector<Equalizer::BandLevel> bandLevels;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000249 Equalizer::BandLevel bandLevel = {mParamBandIndex, mParamBandLevel};
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000250 bandLevels.push_back(bandLevel);
251 EXPECT_NO_FATAL_FAILURE(addBandLevelsParam(bandLevels));
252 SetAndGetEqualizerParameters();
253}
254
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000255INSTANTIATE_TEST_SUITE_P(
256 EqualizerTest, EqualizerParamTest,
Shunkai Yao464775e2022-10-28 21:42:25 +0000257 ::testing::Combine(
258 testing::ValuesIn(android::getAidlHalInstanceNames(IFactory::descriptor)),
259 testing::Range(kPresetIndexRange.first, kPresetIndexRange.second),
260 testing::Range(kBandIndexRange.first, kBandIndexRange.second),
261 testing::Range(kBandLevelRange.first, kBandLevelRange.second)),
262 [](const testing::TestParamInfo<EqualizerParamTest::ParamType>& info) {
263 std::string instance = std::get<PARAM_INSTANCE_NAME>(info.param);
264 std::string presetIdx = std::to_string(std::get<PARAM_PRESET_INDEX>(info.param));
265 std::string bandIdx = std::to_string(std::get<PARAM_BAND_INDEX>(info.param));
266 std::string bandLevel = std::to_string(std::get<PARAM_BAND_LEVEL>(info.param));
267
268 std::string name = instance + "_presetIndex" + presetIdx + "_bandIndex" + bandIdx +
269 "_bandLevel" + bandLevel;
270 std::replace_if(
271 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
272 return name;
273 });
274
275GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EqualizerParamTest);
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000276
277int main(int argc, char** argv) {
278 ::testing::InitGoogleTest(&argc, argv);
279 ABinderProcess_setThreadPoolMaxThreadCount(1);
280 ABinderProcess_startThreadPool();
281 return RUN_ALL_TESTS();
282}