blob: 3b9699b5aa0258306dcd8fac5d9c61cca058fb51 [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 */
61using EqualizerParamTestParam = std::tuple<int, int, int>;
62
63class EqualizerParamTest : public ::testing::TestWithParam<EqualizerParamTestParam>,
64 public EffectHelper {
65 public:
66 EqualizerParamTest()
67 : EffectHelper(android::getAidlHalInstanceNames(IFactory::descriptor)[0]),
68 mParamPresetIndex(std::get<0 /* kPresetIndexRange */>(GetParam())),
69 mParamBandIndex(std::get<1 /* kBandIndexRange */>(GetParam())),
70 mParamBandLevel(std::get<2 /* kBandLevelRange */>(GetParam())) {}
71
72 void SetUp() override {
73 CreateEffectsWithUUID(EqualizerTypeUUID);
74 initParamCommonFormat();
75 initParamCommon();
76 initParamSpecific();
77 OpenEffects(EqualizerTypeUUID);
78 SCOPED_TRACE(testing::Message() << "preset: " << mParamPresetIndex << " bandIdx "
79 << mParamBandIndex << " level " << mParamBandLevel);
80 }
81
82 void TearDown() override {
83 CloseEffects();
84 DestroyEffects();
85 CleanUp();
86 }
87
88 const int mParamPresetIndex;
89 const int mParamBandIndex;
90 const int mParamBandLevel;
91
92 void SetAndGetEqualizerParameters() {
93 auto functor = [&](const std::shared_ptr<IEffect>& effect) {
94 for (auto& it : mTags) {
95 auto& tag = it.first;
96 auto& eq = it.second;
97
98 // validate parameter
99 Descriptor desc;
100 ASSERT_STATUS(EX_NONE, effect->getDescriptor(&desc));
101 const bool valid = isTagInRange(it.first, it.second, desc);
102 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
103
104 // set
105 Parameter expectParam;
106 Parameter::Specific specific;
107 specific.set<Parameter::Specific::equalizer>(*eq.get());
108 expectParam.set<Parameter::specific>(specific);
109 EXPECT_STATUS(expected, effect->setParameter(expectParam))
110 << expectParam.toString();
111
112 // get
113 if (expected == EX_NONE) {
114 Parameter getParam;
115 Parameter::Specific::Id id;
116 id.set<Parameter::Specific::Id::equalizerTag>(tag);
117 // if set success, then get should match
118 EXPECT_STATUS(expected, effect->getParameter(id, &getParam));
119 EXPECT_EQ(expectParam, getParam) << "\n"
120 << expectParam.toString() << "\n"
121 << getParam.toString();
122 }
123 }
124 };
125 EXPECT_NO_FATAL_FAILURE(ForEachEffect(functor));
126 }
127
128 void addPresetParam(int preset) {
129 Equalizer eq;
130 eq.set<Equalizer::preset>(preset);
131 mTags.push_back({Equalizer::preset, std::make_unique<Equalizer>(std::move(eq))});
132 }
133
134 void addBandLevelsParam(std::vector<Equalizer::BandLevel>& bandLevels) {
135 Equalizer eq;
136 eq.set<Equalizer::bandLevels>(bandLevels);
137 mTags.push_back({Equalizer::bandLevels, std::make_unique<Equalizer>(std::move(eq))});
138 }
139
140 bool isTagInRange(const Equalizer::Tag& tag, const std::unique_ptr<Equalizer>& eq,
141 const Descriptor& desc) const {
142 std::cout << "xxx" << toString(tag) << " " << desc.toString();
143 const Equalizer::Capability& eqCap = desc.capability.get<Capability::equalizer>();
144 switch (tag) {
145 case Equalizer::preset: {
146 int index = eq->get<Equalizer::preset>();
147 return isPresetIndexInRange(eqCap, index);
148 }
149 case Equalizer::bandLevels: {
150 auto& bandLevel = eq->get<Equalizer::bandLevels>();
151 return isBandIndexInRange(eqCap, bandLevel);
152 }
153 default:
154 return false;
155 }
156 return false;
157 }
158
159 bool isPresetIndexInRange(const Equalizer::Capability& cap, int idx) const {
160 const auto [min, max] =
161 std::minmax_element(cap.presets.begin(), cap.presets.end(),
162 [](const auto& a, const auto& b) { return a.index < b.index; });
163 return idx >= min->index && idx <= max->index;
164 }
165
166 bool isBandIndexInRange(const Equalizer::Capability& cap,
167 const std::vector<Equalizer::BandLevel>& bandLevel) const {
168 for (auto& it : bandLevel) {
169 if (!isBandIndexInRange(cap, it.index)) return false;
170 }
171 return true;
172 }
173
174 bool isBandIndexInRange(const Equalizer::Capability& cap, int idx) const {
175 const auto [min, max] =
176 std::minmax_element(cap.bandFrequencies.begin(), cap.bandFrequencies.end(),
177 [](const auto& a, const auto& b) { return a.index < b.index; });
178 return idx >= min->index && idx <= max->index;
179 }
180
181 private:
182 Equalizer::VendorExtension mVendorExtension;
183 std::vector<std::pair<Equalizer::Tag, std::unique_ptr<Equalizer>>> mTags;
184
185 bool validCapabilityTag(Capability& cap) { return cap.getTag() == Capability::equalizer; }
186
187 void initParamSpecific() {
188 Equalizer eq;
189 eq.set<Equalizer::preset>(0);
190 Parameter::Specific specific;
191 specific.set<Parameter::Specific::equalizer>(eq);
192 setSpecific(specific);
193 }
194
195 void CleanUp() { mTags.clear(); }
196};
197
198TEST_P(EqualizerParamTest, SetAndGetPreset) {
199 EXPECT_NO_FATAL_FAILURE(addPresetParam(mParamPresetIndex));
200 SetAndGetEqualizerParameters();
201}
202
203TEST_P(EqualizerParamTest, SetAndGetSingleBand) {
204 Equalizer::BandLevel bandLevel = {mParamBandIndex, mParamBandLevel};
205 std::vector<Equalizer::BandLevel> bandLevels;
206 bandLevels.push_back(bandLevel);
207 EXPECT_NO_FATAL_FAILURE(addBandLevelsParam(bandLevels));
208 SetAndGetEqualizerParameters();
209}
210
211/**
212 Testing preset index range with [-10, 10], assuming the min/max preset index supported by
213effect is in this range.
214 This range is verified with IEffect.getDescriptor(): for any index supported vts expect EX_NONE
215from IEffect.setParameter(), otherwise expect EX_ILLEGAL_ARGUMENT.
216 */
217constexpr std::pair<int, int> kPresetIndexRange = {-1, 10}; // valid range [0, 9]
218constexpr std::pair<int, int> kBandIndexRange = {-1, 5}; // valid range [0, 4]
219constexpr std::pair<int, int> kBandLevelRange = {-5, 5}; // needs update with implementation
220
221INSTANTIATE_TEST_SUITE_P(
222 EqualizerTest, EqualizerParamTest,
223 ::testing::Combine(testing::Range(kPresetIndexRange.first, kPresetIndexRange.second),
224 testing::Range(kBandIndexRange.first, kBandIndexRange.second),
225 testing::Range(kBandLevelRange.first, kBandLevelRange.second)));
226GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EqualizerTest);
227
228int main(int argc, char** argv) {
229 ::testing::InitGoogleTest(&argc, argv);
230 ABinderProcess_setThreadPoolMaxThreadCount(1);
231 ABinderProcess_startThreadPool();
232 return RUN_ALL_TESTS();
233}