• 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 <vector>
20 
21 #include <aidl/Gtest.h>
22 #include <aidl/android/hardware/audio/effect/IEffect.h>
23 #include <aidl/android/hardware/audio/effect/IFactory.h>
24 #define LOG_TAG "VtsHalEqualizerTest"
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 EqualizerParamTestParam = 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 EqualizerTest : public ::testing::TestWithParam<EqualizerParamTestParam>,
62                       public EffectHelper {
63   public:
EqualizerTest()64     EqualizerTest()
65         : mPresetIndex(std::get<PARAM_PRESET>(GetParam())),
66           mBandLevel(std::get<PARAM_BAND_LEVEL>(GetParam())) {
67         std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
68     }
69 
SetUp()70     void SetUp() override {
71         ASSERT_NE(nullptr, mFactory);
72         ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
73 
74         Parameter::Common common = createParamCommon(
75                 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
76                 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
77         IEffect::OpenEffectReturn ret;
78         ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt, &ret, EX_NONE));
79         ASSERT_NE(nullptr, mEffect);
80     }
TearDown()81     void TearDown() override {
82         ASSERT_NO_FATAL_FAILURE(close(mEffect));
83         ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
84     }
85 
86     static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
87     std::shared_ptr<IFactory> mFactory;
88     std::shared_ptr<IEffect> mEffect;
89     Descriptor mDescriptor;
90     int mPresetIndex;
91     std::vector<Equalizer::BandLevel> mBandLevel;
92 
SetAndGetEqualizerParameters()93     void SetAndGetEqualizerParameters() {
94         ASSERT_NE(nullptr, mEffect);
95         for (auto& it : mTags) {
96             auto& tag = it.first;
97             auto& eq = it.second;
98 
99             // validate parameter
100             const bool valid = isParameterValid<Equalizer, Range::equalizer>(eq, mDescriptor);
101             const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
102 
103             // set
104             Parameter::Specific specific =
105                     Parameter::Specific::make<Parameter::Specific::equalizer>(eq);
106             Parameter expectParam = Parameter::make<Parameter::specific>(specific);
107             EXPECT_STATUS(expected, mEffect->setParameter(expectParam))
108                     << expectParam.toString() << "\n"
109                     << mDescriptor.toString();
110 
111             // only get if parameter in range and set success
112             if (expected == EX_NONE) {
113                 Parameter getParam;
114                 Equalizer::Id eqId = Equalizer::Id::make<Equalizer::Id::commonTag>(tag);
115                 Parameter::Id id = Parameter::Id::make<Parameter::Id::equalizerTag>(eqId);
116                 // if set success, then get should match
117                 EXPECT_STATUS(expected, mEffect->getParameter(id, &getParam));
118                 EXPECT_TRUE(isEqParameterExpected(expectParam, getParam))
119                         << "\nexpect:" << expectParam.toString()
120                         << "\ngetParam:" << getParam.toString();
121             }
122         }
123     }
124 
isEqParameterExpected(const Parameter & expect,const Parameter & target)125     bool isEqParameterExpected(const Parameter& expect, const Parameter& target) {
126         // if parameter same, then for sure they are matched
127         if (expect == target) return true;
128 
129         // if not, see if target include the expect parameter, and others all default (0).
130         /*
131          * This is to verify the case of client setParameter to a single bandLevel ({3, -1} for
132          * example), and return of getParameter must be [{0, 0}, {1, 0}, {2, 0}, {3, -1}, {4, 0}]
133          */
134         EXPECT_EQ(expect.getTag(), Parameter::specific);
135         EXPECT_EQ(target.getTag(), Parameter::specific);
136 
137         Parameter::Specific expectSpec = expect.get<Parameter::specific>(),
138                             targetSpec = target.get<Parameter::specific>();
139         EXPECT_EQ(expectSpec.getTag(), Parameter::Specific::equalizer);
140         EXPECT_EQ(targetSpec.getTag(), Parameter::Specific::equalizer);
141 
142         Equalizer expectEq = expectSpec.get<Parameter::Specific::equalizer>(),
143                   targetEq = targetSpec.get<Parameter::Specific::equalizer>();
144         EXPECT_EQ(expectEq.getTag(), targetEq.getTag());
145 
146         auto eqTag = targetEq.getTag();
147         switch (eqTag) {
148             case Equalizer::bandLevels: {
149                 auto expectBl = expectEq.get<Equalizer::bandLevels>();
150                 std::sort(expectBl.begin(), expectBl.end(),
151                           [](const auto& a, const auto& b) { return a.index < b.index; });
152                 expectBl.erase(std::unique(expectBl.begin(), expectBl.end()), expectBl.end());
153                 auto targetBl = targetEq.get<Equalizer::bandLevels>();
154                 return std::includes(targetBl.begin(), targetBl.end(), expectBl.begin(),
155                                      expectBl.end());
156             }
157             case Equalizer::preset: {
158                 return expectEq.get<Equalizer::preset>() == targetEq.get<Equalizer::preset>();
159             }
160             default:
161                 return false;
162         }
163         return false;
164     }
165 
addPresetParam(int preset)166     void addPresetParam(int preset) {
167         mTags.push_back({Equalizer::preset, Equalizer::make<Equalizer::preset>(preset)});
168     }
169 
addBandLevelsParam(std::vector<Equalizer::BandLevel> & bandLevels)170     void addBandLevelsParam(std::vector<Equalizer::BandLevel>& bandLevels) {
171         mTags.push_back(
172                 {Equalizer::bandLevels, Equalizer::make<Equalizer::bandLevels>(bandLevels)});
173     }
174 
175   private:
176     std::vector<std::pair<Equalizer::Tag, Equalizer>> mTags;
177 
CleanUp()178     void CleanUp() { mTags.clear(); }
179 };
180 
TEST_P(EqualizerTest,SetAndGetParams)181 TEST_P(EqualizerTest, SetAndGetParams) {
182     addBandLevelsParam(mBandLevel);
183     addPresetParam(mPresetIndex);
184     EXPECT_NO_FATAL_FAILURE(SetAndGetEqualizerParameters());
185 }
186 
187 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
188 INSTANTIATE_TEST_SUITE_P(
189         EqualizerTest, EqualizerTest,
190         ::testing::Combine(
191                 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
192                                           IFactory::descriptor, getEffectTypeUuidEqualizer())),
193                 testing::ValuesIn(EffectHelper::getTestValueSet<Equalizer, int, Range::equalizer,
194                                                                 Equalizer::preset>(
195                         kDescPair, EffectHelper::expandTestValueBasic<int>)),
196                 testing::ValuesIn(
197                         EffectHelper::getTestValueSet<Equalizer, std::vector<Equalizer::BandLevel>,
198                                                       Range::equalizer, Equalizer::bandLevels>(
199                                 kDescPair,
__anon7f169cb50202(std::set<std::vector<Equalizer::BandLevel>>& bandLevels) 200                                 [](std::set<std::vector<Equalizer::BandLevel>>& bandLevels) {
201                                     return bandLevels;
202                                 }))),
__anon7f169cb50302(const testing::TestParamInfo<EqualizerTest::ParamType>& info) 203         [](const testing::TestParamInfo<EqualizerTest::ParamType>& info) {
204             auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
205             std::string bandLevel =
206                     ::android::internal::ToString(std::get<PARAM_BAND_LEVEL>(info.param));
207             std::string name = getPrefix(descriptor) + "_preset_" +
208                                std::to_string(std::get<PARAM_PRESET>(info.param)) + "_bandLevel_" +
209                                bandLevel;
210             std::replace_if(
211                     name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
212             return name;
213         });
214 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EqualizerTest);
215 
main(int argc,char ** argv)216 int main(int argc, char** argv) {
217     ::testing::InitGoogleTest(&argc, argv);
218     ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
219     ABinderProcess_setThreadPoolMaxThreadCount(1);
220     ABinderProcess_startThreadPool();
221     return RUN_ALL_TESTS();
222 }
223