• 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 #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 kDurationMilliSec = 720;
98     static constexpr int kBufferSize = kSamplingFrequency * kDurationMilliSec / 1000;
99     int kChannelCount = getChannelCount(
100             AudioChannelLayout::make<AudioChannelLayout::layoutMask>(kDefaultChannelLayout));
101     long mInputFrameCount;
102     long mOutputFrameCount;
103     std::shared_ptr<IFactory> mFactory;
104     std::shared_ptr<IEffect> mEffect;
105     IEffect::OpenEffectReturn mOpenEffectReturn;
106     Descriptor mDescriptor;
107 };
108 
109 /**
110  * Here we focus on specific parameter checking, general IEffect interfaces testing performed in
111  * VtsAudioEffectTargetTest.
112  */
113 enum ParamName { PARAM_INSTANCE_NAME, PARAM_STRENGTH };
114 using VirtualizerParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int>;
115 
116 /*
117  * Testing parameter range, assuming the parameter supported by effect is in this range.
118  * Parameter should be within the valid range defined in the documentation,
119  * for any supported value test expects EX_NONE from IEffect.setParameter(),
120  * otherwise expect EX_ILLEGAL_ARGUMENT.
121  */
122 
123 class VirtualizerParamTest : public ::testing::TestWithParam<VirtualizerParamTestParam>,
124                              public VirtualizerHelper {
125   public:
VirtualizerParamTest()126     VirtualizerParamTest() : mParamStrength(std::get<PARAM_STRENGTH>(GetParam())) {
127         std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
128     }
SetUp()129     void SetUp() override { ASSERT_NO_FATAL_FAILURE(SetUpVirtualizer()); }
TearDown()130     void TearDown() override { TearDownVirtualizer(); }
131 
132     int mParamStrength = 0;
133 };
134 
TEST_P(VirtualizerParamTest,SetAndGetStrength)135 TEST_P(VirtualizerParamTest, SetAndGetStrength) {
136     ASSERT_NO_FATAL_FAILURE(setAndVerifyStrength(
137             mParamStrength, isStrengthValid(mParamStrength) ? EX_NONE : EX_ILLEGAL_ARGUMENT));
138 }
139 
140 enum ProcessTestParam { PROCESS_INSTANCE_NAME, PROCESS_ZERO_INPUT };
141 using VirtualizerProcessTestParam =
142         std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, bool>;
143 
144 class VirtualizerProcessTest : public ::testing::TestWithParam<VirtualizerProcessTestParam>,
145                                public VirtualizerHelper {
146   public:
VirtualizerProcessTest()147     VirtualizerProcessTest() : mZeroInput(std::get<PROCESS_ZERO_INPUT>(GetParam())) {
148         std::tie(mFactory, mDescriptor) = std::get<PROCESS_INSTANCE_NAME>(GetParam());
149     }
150 
SetUp()151     void SetUp() override {
152         SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
153         ASSERT_NO_FATAL_FAILURE(SetUpVirtualizer());
154     }
155 
TearDown()156     void TearDown() override {
157         SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
158         ASSERT_NO_FATAL_FAILURE(TearDownVirtualizer());
159     }
160 
161     static constexpr float kAbsError = 0.00001;
162     bool mZeroInput;
163 };
164 
TEST_P(VirtualizerProcessTest,IncreasingStrength)165 TEST_P(VirtualizerProcessTest, IncreasingStrength) {
166     std::vector<float> input(kBufferSize);
167     std::vector<float> output(kBufferSize);
168     std::vector<int> strengths = {250, 500, 750, 1000};
169 
170     if (mZeroInput) {
171         std::fill(input.begin(), input.end(), 0);
172     } else {
173         ASSERT_NO_FATAL_FAILURE(generateSineWave(1000 /*Input Frequency*/, input));
174     }
175 
176     const float inputRmse =
177             audio_utils_compute_energy_mono(input.data(), AUDIO_FORMAT_PCM_FLOAT, input.size());
178 
179     for (int strength : strengths) {
180         // Skipping the further steps for unnsupported Strength values
181         if (!isStrengthValid(strength)) {
182             continue;
183         }
184         setAndVerifyStrength(strength, EX_NONE);
185         ASSERT_NO_FATAL_FAILURE(
186                 processAndWriteToOutput(input, output, mEffect, &mOpenEffectReturn));
187 
188         const float outputRmse = audio_utils_compute_energy_mono(
189                 output.data(), AUDIO_FORMAT_PCM_FLOAT, output.size());
190 
191         if (inputRmse != 0) {
192             EXPECT_NE(outputRmse, 0);
193             if (strength != 0) {
194                 EXPECT_GT(abs(outputRmse - inputRmse), kAbsError);
195             }
196         } else {
197             EXPECT_NEAR(outputRmse, inputRmse, kAbsError);
198         }
199     }
200 }
201 
202 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
203 INSTANTIATE_TEST_SUITE_P(
204         VirtualizerTest, VirtualizerParamTest,
205         ::testing::Combine(
206                 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
207                                           IFactory::descriptor, getEffectTypeUuidVirtualizer())),
208                 testing::ValuesIn(EffectHelper::getTestValueSet<
209                                   Virtualizer, int, Range::virtualizer, Virtualizer::strengthPm>(
210                         kDescPair, EffectHelper::expandTestValueBasic<int>))),
__anon864049860102(const testing::TestParamInfo<VirtualizerParamTest::ParamType>& info) 211         [](const testing::TestParamInfo<VirtualizerParamTest::ParamType>& info) {
212             auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
213             std::string strength = std::to_string(std::get<PARAM_STRENGTH>(info.param));
214             std::string name = getPrefix(descriptor) + "_strength" + strength;
215             std::replace_if(
216                     name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
217             return name;
218         });
219 
220 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VirtualizerParamTest);
221 
222 INSTANTIATE_TEST_SUITE_P(
223         VirtualizerTest, VirtualizerProcessTest,
224         ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
225                                    IFactory::descriptor, getEffectTypeUuidVirtualizer())),
226                            testing::Bool()),
__anon864049860302(const testing::TestParamInfo<VirtualizerProcessTest::ParamType>& info) 227         [](const testing::TestParamInfo<VirtualizerProcessTest::ParamType>& info) {
228             auto descriptor = std::get<PROCESS_INSTANCE_NAME>(info.param).second;
229             std::string isInputZero = std::to_string(std::get<PROCESS_ZERO_INPUT>(info.param));
230             std::string name = getPrefix(descriptor) + "_isInputZero_" + isInputZero;
231             std::replace_if(
232                     name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
233             return name;
234         });
235 
236 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VirtualizerProcessTest);
237 
main(int argc,char ** argv)238 int main(int argc, char** argv) {
239     ::testing::InitGoogleTest(&argc, argv);
240     ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
241     ABinderProcess_setThreadPoolMaxThreadCount(1);
242     ABinderProcess_startThreadPool();
243     return RUN_ALL_TESTS();
244 }
245