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 #define LOG_TAG "VtsHalVirtualizerTest"
18 #include <android-base/logging.h>
19 #include <audio_utils/power.h>
20 #include <system/audio.h>
21
22 #include "EffectHelper.h"
23
24 using namespace android;
25
26 using aidl::android::hardware::audio::common::getChannelCount;
27 using aidl::android::hardware::audio::effect::Descriptor;
28 using aidl::android::hardware::audio::effect::getEffectTypeUuidVirtualizer;
29 using aidl::android::hardware::audio::effect::IEffect;
30 using aidl::android::hardware::audio::effect::IFactory;
31 using aidl::android::hardware::audio::effect::Parameter;
32 using aidl::android::hardware::audio::effect::Virtualizer;
33 using android::hardware::audio::common::testing::detail::TestExecutionTracer;
34
35 class VirtualizerHelper : public EffectHelper {
36 public:
SetUpVirtualizer()37 void SetUpVirtualizer() {
38 ASSERT_NE(nullptr, mFactory);
39 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
40 initFrameCount();
41 Parameter::Specific specific = getDefaultParamSpecific();
42 Parameter::Common common = createParamCommon(
43 0 /* session */, 1 /* ioHandle */, kSamplingFrequency /* iSampleRate */,
44 kSamplingFrequency /* oSampleRate */, mInputFrameCount /* iFrameCount */,
45 mInputFrameCount /* oFrameCount */);
46 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &mOpenEffectReturn, EX_NONE));
47 ASSERT_NE(nullptr, mEffect);
48 }
49
TearDownVirtualizer()50 void TearDownVirtualizer() {
51 ASSERT_NO_FATAL_FAILURE(close(mEffect));
52 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
53 mOpenEffectReturn = IEffect::OpenEffectReturn{};
54 }
55
getDefaultParamSpecific()56 Parameter::Specific getDefaultParamSpecific() {
57 Virtualizer vr = Virtualizer::make<Virtualizer::strengthPm>(0);
58 Parameter::Specific specific =
59 Parameter::Specific::make<Parameter::Specific::virtualizer>(vr);
60 return specific;
61 }
62
createVirtualizerStrengthParam(int param)63 Parameter createVirtualizerStrengthParam(int param) {
64 return Parameter::make<Parameter::specific>(
65 Parameter::Specific::make<Parameter::Specific::virtualizer>(
66 Virtualizer::make<Virtualizer::strengthPm>(param)));
67 }
68
initFrameCount()69 void initFrameCount() {
70 mInputFrameCount = kBufferSize / kChannelCount;
71 mOutputFrameCount = kBufferSize / kChannelCount;
72 }
73
isStrengthValid(int level)74 bool isStrengthValid(int level) {
75 auto vir = Virtualizer::make<Virtualizer::strengthPm>(level);
76 return isParameterValid<Virtualizer, Range::virtualizer>(vir, mDescriptor);
77 }
78
setAndVerifyStrength(int param,binder_exception_t expected)79 void setAndVerifyStrength(int param, binder_exception_t expected) {
80 auto expectedParam = createVirtualizerStrengthParam(param);
81 EXPECT_STATUS(expected, mEffect->setParameter(expectedParam)) << expectedParam.toString();
82
83 if (expected == EX_NONE) {
84 Virtualizer::Id vrlId =
85 Virtualizer::Id::make<Virtualizer::Id::commonTag>(Virtualizer::strengthPm);
86
87 auto id = Parameter::Id::make<Parameter::Id::virtualizerTag>(vrlId);
88 // get parameter
89 Parameter getParam;
90 // if set success, then get should match
91 EXPECT_STATUS(expected, mEffect->getParameter(id, &getParam));
92 EXPECT_EQ(expectedParam, getParam) << "\nexpectedParam:" << expectedParam.toString()
93 << "\ngetParam:" << getParam.toString();
94 }
95 }
96
97 static constexpr int kDefaultChannelLayout = AudioChannelLayout::LAYOUT_STEREO;
98 static constexpr int kDurationMilliSec = 720;
99 static constexpr int kBufferSize = kSamplingFrequency * kDurationMilliSec / 1000;
100 int kChannelCount = getChannelCount(
101 AudioChannelLayout::make<AudioChannelLayout::layoutMask>(kDefaultChannelLayout));
102 long mInputFrameCount;
103 long mOutputFrameCount;
104 std::shared_ptr<IFactory> mFactory;
105 std::shared_ptr<IEffect> mEffect;
106 IEffect::OpenEffectReturn mOpenEffectReturn;
107 Descriptor mDescriptor;
108 };
109
110 /**
111 * Here we focus on specific parameter checking, general IEffect interfaces testing performed in
112 * VtsAudioEffectTargetTest.
113 */
114 enum ParamName { PARAM_INSTANCE_NAME, PARAM_STRENGTH };
115 using VirtualizerParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int>;
116
117 /*
118 * Testing parameter range, assuming the parameter supported by effect is in this range.
119 * Parameter should be within the valid range defined in the documentation,
120 * for any supported value test expects EX_NONE from IEffect.setParameter(),
121 * otherwise expect EX_ILLEGAL_ARGUMENT.
122 */
123
124 class VirtualizerParamTest : public ::testing::TestWithParam<VirtualizerParamTestParam>,
125 public VirtualizerHelper {
126 public:
VirtualizerParamTest()127 VirtualizerParamTest() : mParamStrength(std::get<PARAM_STRENGTH>(GetParam())) {
128 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
129 }
SetUp()130 void SetUp() override { ASSERT_NO_FATAL_FAILURE(SetUpVirtualizer()); }
TearDown()131 void TearDown() override { TearDownVirtualizer(); }
132
133 int mParamStrength = 0;
134 };
135
TEST_P(VirtualizerParamTest,SetAndGetStrength)136 TEST_P(VirtualizerParamTest, SetAndGetStrength) {
137 ASSERT_NO_FATAL_FAILURE(setAndVerifyStrength(
138 mParamStrength, isStrengthValid(mParamStrength) ? EX_NONE : EX_ILLEGAL_ARGUMENT));
139 }
140
141 enum ProcessTestParam { PROCESS_INSTANCE_NAME, PROCESS_ZERO_INPUT };
142 using VirtualizerProcessTestParam =
143 std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, bool>;
144
145 class VirtualizerProcessTest : public ::testing::TestWithParam<VirtualizerProcessTestParam>,
146 public VirtualizerHelper {
147 public:
VirtualizerProcessTest()148 VirtualizerProcessTest() : mZeroInput(std::get<PROCESS_ZERO_INPUT>(GetParam())) {
149 std::tie(mFactory, mDescriptor) = std::get<PROCESS_INSTANCE_NAME>(GetParam());
150 }
151
SetUp()152 void SetUp() override {
153 SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
154 ASSERT_NO_FATAL_FAILURE(SetUpVirtualizer());
155 }
156
TearDown()157 void TearDown() override {
158 SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
159 ASSERT_NO_FATAL_FAILURE(TearDownVirtualizer());
160 }
161
generateInput(std::vector<float> & buffer)162 void generateInput(std::vector<float>& buffer) {
163 if (mZeroInput) {
164 std::fill(buffer.begin(), buffer.end(), 0);
165 } else {
166 generateSineWave(1000 /*Input Frequency*/, buffer);
167 }
168 }
169
170 static constexpr float kAbsError = 0.00001;
171 bool mZeroInput;
172 };
173
TEST_P(VirtualizerProcessTest,IncreasingStrength)174 TEST_P(VirtualizerProcessTest, IncreasingStrength) {
175 std::vector<float> input(kBufferSize);
176 std::vector<float> output(kBufferSize);
177 std::vector<int> strengths = {250, 500, 750, 1000};
178
179 generateInput(input);
180
181 const float inputRmse =
182 audio_utils_compute_energy_mono(input.data(), AUDIO_FORMAT_PCM_FLOAT, input.size());
183
184 for (int strength : strengths) {
185 // Skipping the further steps for unnsupported Strength values
186 if (!isStrengthValid(strength)) {
187 continue;
188 }
189 setAndVerifyStrength(strength, EX_NONE);
190 ASSERT_NO_FATAL_FAILURE(
191 processAndWriteToOutput(input, output, mEffect, &mOpenEffectReturn));
192
193 const float outputRmse = audio_utils_compute_energy_mono(
194 output.data(), AUDIO_FORMAT_PCM_FLOAT, output.size());
195
196 if (inputRmse != 0) {
197 EXPECT_NE(outputRmse, 0);
198 if (strength != 0) {
199 EXPECT_GT(abs(outputRmse - inputRmse), kAbsError);
200 }
201 } else {
202 EXPECT_NEAR(outputRmse, inputRmse, kAbsError);
203 }
204 }
205 }
206
207 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
208 INSTANTIATE_TEST_SUITE_P(
209 VirtualizerTest, VirtualizerParamTest,
210 ::testing::Combine(
211 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
212 IFactory::descriptor, getEffectTypeUuidVirtualizer())),
213 testing::ValuesIn(EffectHelper::getTestValueSet<
214 Virtualizer, int, Range::virtualizer, Virtualizer::strengthPm>(
215 kDescPair, EffectHelper::expandTestValueBasic<int>))),
__anona0f08f060102(const testing::TestParamInfo<VirtualizerParamTest::ParamType>& info) 216 [](const testing::TestParamInfo<VirtualizerParamTest::ParamType>& info) {
217 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
218 std::string strength = std::to_string(std::get<PARAM_STRENGTH>(info.param));
219 std::string name = getPrefix(descriptor) + "_strength" + strength;
220 std::replace_if(
221 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
222 return name;
223 });
224
225 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VirtualizerParamTest);
226
227 INSTANTIATE_TEST_SUITE_P(
228 VirtualizerTest, VirtualizerProcessTest,
229 ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
230 IFactory::descriptor, getEffectTypeUuidVirtualizer())),
231 testing::Bool()),
__anona0f08f060302(const testing::TestParamInfo<VirtualizerProcessTest::ParamType>& info) 232 [](const testing::TestParamInfo<VirtualizerProcessTest::ParamType>& info) {
233 auto descriptor = std::get<PROCESS_INSTANCE_NAME>(info.param).second;
234 std::string isInputZero = std::to_string(std::get<PROCESS_ZERO_INPUT>(info.param));
235 std::string name = getPrefix(descriptor) + "_isInputZero_" + isInputZero;
236 std::replace_if(
237 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
238 return name;
239 });
240
241 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VirtualizerProcessTest);
242
main(int argc,char ** argv)243 int main(int argc, char** argv) {
244 ::testing::InitGoogleTest(&argc, argv);
245 ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
246 ABinderProcess_setThreadPoolMaxThreadCount(1);
247 ABinderProcess_startThreadPool();
248 return RUN_ALL_TESTS();
249 }
250