blob: 61776b2b6eb73839f3f17a3733138c9d1a5bad45 [file] [log] [blame]
Sham Rathod2d319dc2022-11-29 15:06:12 +05301/*
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#define LOG_TAG "VtsHalVirtualizerTest"
18
19#include <Utils.h>
20#include <aidl/Vintf.h>
21#include "EffectHelper.h"
22
23using namespace android;
24
25using aidl::android::hardware::audio::effect::Capability;
26using aidl::android::hardware::audio::effect::Descriptor;
27using aidl::android::hardware::audio::effect::IEffect;
28using aidl::android::hardware::audio::effect::IFactory;
29using aidl::android::hardware::audio::effect::kVirtualizerTypeUUID;
30using aidl::android::hardware::audio::effect::Parameter;
31using aidl::android::hardware::audio::effect::Virtualizer;
32
33/**
34 * Here we focus on specific parameter checking, general IEffect interfaces testing performed in
35 * VtsAudioEffectTargetTest.
36 */
37enum ParamName { PARAM_INSTANCE_NAME, PARAM_STRENGTH };
38using VirtualizerParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int>;
39
40/*
41 * Testing parameter range, assuming the parameter supported by effect is in this range.
42 * Parameter should be within the valid range defined in the documentation,
43 * for any supported value test expects EX_NONE from IEffect.setParameter(),
44 * otherwise expect EX_ILLEGAL_ARGUMENT.
45 */
46
47const std::vector<int> kStrengthValues = {
48 std::numeric_limits<int>::min(), Virtualizer::MIN_PER_MILLE_STRENGTH - 1,
49 Virtualizer::MIN_PER_MILLE_STRENGTH, Virtualizer::MAX_PER_MILLE_STRENGTH,
50 Virtualizer::MAX_PER_MILLE_STRENGTH + 1, std::numeric_limits<int>::max()};
51
52class VirtualizerParamTest : public ::testing::TestWithParam<VirtualizerParamTestParam>,
53 public EffectHelper {
54 public:
55 VirtualizerParamTest() : mParamStrength(std::get<PARAM_STRENGTH>(GetParam())) {
56 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
57 }
58
59 void SetUp() override {
60 ASSERT_NE(nullptr, mFactory);
61 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
62
63 Parameter::Specific specific = getDefaultParamSpecific();
64 Parameter::Common common = EffectHelper::createParamCommon(
65 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
66 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
67 IEffect::OpenEffectReturn ret;
68 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE));
69 ASSERT_NE(nullptr, mEffect);
70 }
71
72 void TearDown() override {
73 ASSERT_NO_FATAL_FAILURE(close(mEffect));
74 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
75 }
76
77 Parameter::Specific getDefaultParamSpecific() {
78 Virtualizer vr =
79 Virtualizer::make<Virtualizer::strengthPm>(Virtualizer::MIN_PER_MILLE_STRENGTH);
80 Parameter::Specific specific =
81 Parameter::Specific::make<Parameter::Specific::virtualizer>(vr);
82 return specific;
83 }
84
85 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
86 std::shared_ptr<IFactory> mFactory;
87 std::shared_ptr<IEffect> mEffect;
88 Descriptor mDescriptor;
89 int mParamStrength = Virtualizer::MIN_PER_MILLE_STRENGTH;
90
91 void SetAndGetVirtualizerParameters() {
92 for (auto& it : mTags) {
93 auto& tag = it.first;
94 auto& vr = it.second;
95
96 // validate parameter
97 Descriptor desc;
98 ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
99 const bool valid = isTagInRange(it.first, it.second, desc);
100 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
101
102 // set parameter
103 Parameter expectParam;
104 Parameter::Specific specific;
105 specific.set<Parameter::Specific::virtualizer>(vr);
106 expectParam.set<Parameter::specific>(specific);
107 EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
108
109 // only get if parameter in range and set success
110 if (expected == EX_NONE) {
111 Parameter getParam;
112 Parameter::Id id;
113 Virtualizer::Id vrId;
114 vrId.set<Virtualizer::Id::commonTag>(tag);
115 id.set<Parameter::Id::virtualizerTag>(vrId);
116 // if set success, then get should match
117 EXPECT_STATUS(expected, mEffect->getParameter(id, &getParam));
118 EXPECT_EQ(expectParam, getParam);
119 }
120 }
121 }
122
123 void addStrengthParam(int strength) {
124 Virtualizer vr;
125 vr.set<Virtualizer::strengthPm>(strength);
126 mTags.push_back({Virtualizer::strengthPm, vr});
127 }
128
129 bool isTagInRange(const Virtualizer::Tag& tag, const Virtualizer& vr,
130 const Descriptor& desc) const {
131 const Virtualizer::Capability& vrCap = desc.capability.get<Capability::virtualizer>();
132 switch (tag) {
133 case Virtualizer::strengthPm: {
134 int strength = vr.get<Virtualizer::strengthPm>();
135 return isStrengthInRange(vrCap, strength);
136 }
137 default:
138 return false;
139 }
140 return false;
141 }
142
143 bool isStrengthInRange(const Virtualizer::Capability& cap, int strength) const {
144 return cap.strengthSupported && strength >= Virtualizer::MIN_PER_MILLE_STRENGTH &&
145 strength <= Virtualizer::MAX_PER_MILLE_STRENGTH;
146 }
147
148 private:
149 std::vector<std::pair<Virtualizer::Tag, Virtualizer>> mTags;
150 void CleanUp() { mTags.clear(); }
151};
152
153TEST_P(VirtualizerParamTest, SetAndGetStrength) {
154 EXPECT_NO_FATAL_FAILURE(addStrengthParam(mParamStrength));
155 SetAndGetVirtualizerParameters();
156}
157
158INSTANTIATE_TEST_SUITE_P(
159 VirtualizerTest, VirtualizerParamTest,
160 ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
161 IFactory::descriptor, kVirtualizerTypeUUID)),
162 testing::ValuesIn(kStrengthValues)),
163 [](const testing::TestParamInfo<VirtualizerParamTest::ParamType>& info) {
164 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
165 std::string strength = std::to_string(std::get<PARAM_STRENGTH>(info.param));
166 std::string name = "Implementor_" + descriptor.common.implementor + "_name_" +
167 descriptor.common.name + "_UUID_" +
168 descriptor.common.id.uuid.toString() + "_strength" + strength;
169 std::replace_if(
170 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
171 return name;
172 });
173
174GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VirtualizerParamTest);
175
176int main(int argc, char** argv) {
177 ::testing::InitGoogleTest(&argc, argv);
178 ABinderProcess_setThreadPoolMaxThreadCount(1);
179 ABinderProcess_startThreadPool();
180 return RUN_ALL_TESTS();
181}