xref: /aosp_15_r20/hardware/interfaces/audio/aidl/vts/VtsHalLoudnessEnhancerTargetTest.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
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 <string>
18 
19 #define LOG_TAG "VtsHalLoudnessEnhancerTest"
20 #include <android-base/logging.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::getEffectTypeUuidLoudnessEnhancer;
29 using aidl::android::hardware::audio::effect::IEffect;
30 using aidl::android::hardware::audio::effect::IFactory;
31 using aidl::android::hardware::audio::effect::LoudnessEnhancer;
32 using aidl::android::hardware::audio::effect::Parameter;
33 using android::hardware::audio::common::testing::detail::TestExecutionTracer;
34 
35 static constexpr int kZeroGain = 0;
36 static constexpr int kMaxGain = std::numeric_limits<int>::max();
37 static constexpr int kMinGain = std::numeric_limits<int>::min();
38 static constexpr float kAbsError = 0.0001;
39 
40 // Every int 32 bit value is a valid gain, so testing the corner cases and one regular value.
41 // TODO : Update the test values once range/capability is updated by implementation.
42 static const std::vector<int> kGainMbValues = {kMinGain, -100, -50, kZeroGain, 50, 100, kMaxGain};
43 
44 class LoudnessEnhancerEffectHelper : public EffectHelper {
45   public:
SetUpLoudnessEnhancer()46     void SetUpLoudnessEnhancer() {
47         ASSERT_NE(nullptr, mFactory);
48         ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
49 
50         Parameter::Specific specific = getDefaultParamSpecific();
51         Parameter::Common common = createParamCommon(
52                 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
53                 kFrameCount /* iFrameCount */, kFrameCount /* oFrameCount */);
54         ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &mOpenEffectReturn, EX_NONE));
55         ASSERT_NE(nullptr, mEffect);
56         mVersion = EffectFactoryHelper::getHalVersion(mFactory);
57     }
58 
TearDownLoudnessEnhancer()59     void TearDownLoudnessEnhancer() {
60         ASSERT_NO_FATAL_FAILURE(close(mEffect));
61         ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
62         mOpenEffectReturn = IEffect::OpenEffectReturn{};
63     }
64 
getDefaultParamSpecific()65     Parameter::Specific getDefaultParamSpecific() {
66         LoudnessEnhancer le = LoudnessEnhancer::make<LoudnessEnhancer::gainMb>(0);
67         Parameter::Specific specific =
68                 Parameter::Specific::make<Parameter::Specific::loudnessEnhancer>(le);
69         return specific;
70     }
71 
createLoudnessParam(int gainMb)72     Parameter createLoudnessParam(int gainMb) {
73         LoudnessEnhancer le;
74         le.set<LoudnessEnhancer::gainMb>(gainMb);
75         Parameter param;
76         Parameter::Specific specific;
77         specific.set<Parameter::Specific::loudnessEnhancer>(le);
78         param.set<Parameter::specific>(specific);
79         return param;
80     }
81 
isGainValid(int gainMb)82     binder_exception_t isGainValid(int gainMb) {
83         LoudnessEnhancer le;
84         le.set<LoudnessEnhancer::gainMb>(gainMb);
85         if (isParameterValid<LoudnessEnhancer, Range::loudnessEnhancer>(le, mDescriptor)) {
86             return EX_NONE;
87         } else {
88             return EX_ILLEGAL_ARGUMENT;
89         }
90     }
91 
setParameters(int gain,binder_exception_t expected)92     void setParameters(int gain, binder_exception_t expected) {
93         // set parameter
94         auto param = createLoudnessParam(gain);
95         EXPECT_STATUS(expected, mEffect->setParameter(param)) << param.toString();
96     }
97 
validateParameters(int gain)98     void validateParameters(int gain) {
99         // get parameter
100         LoudnessEnhancer::Id leId;
101         Parameter getParam;
102         Parameter::Id id;
103 
104         LoudnessEnhancer::Tag tag(LoudnessEnhancer::gainMb);
105         leId.set<LoudnessEnhancer::Id::commonTag>(tag);
106         id.set<Parameter::Id::loudnessEnhancerTag>(leId);
107         EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam));
108         auto expectedParam = createLoudnessParam(gain);
109         EXPECT_EQ(expectedParam, getParam) << "\nexpectedParam:" << expectedParam.toString()
110                                            << "\ngetParam:" << getParam.toString();
111     }
112 
113     static const long kFrameCount = 256;
114     IEffect::OpenEffectReturn mOpenEffectReturn;
115     std::shared_ptr<IFactory> mFactory;
116     std::shared_ptr<IEffect> mEffect;
117     Descriptor mDescriptor;
118     int mVersion = 0;
119 };
120 
121 /**
122  * Here we focus on specific parameter checking, general IEffect interfaces testing performed in
123  * VtsAudioEffectTargetTest.
124  */
125 enum ParamName { PARAM_INSTANCE_NAME, PARAM_GAIN_MB };
126 using LoudnessEnhancerParamTestParam =
127         std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int>;
128 
129 class LoudnessEnhancerParamTest : public ::testing::TestWithParam<LoudnessEnhancerParamTestParam>,
130                                   public LoudnessEnhancerEffectHelper {
131   public:
LoudnessEnhancerParamTest()132     LoudnessEnhancerParamTest() : mParamGainMb(std::get<PARAM_GAIN_MB>(GetParam())) {
133         std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
134     }
135 
SetUp()136     void SetUp() override { SetUpLoudnessEnhancer(); }
TearDown()137     void TearDown() override { TearDownLoudnessEnhancer(); }
138     int mParamGainMb = 0;
139 };
140 
TEST_P(LoudnessEnhancerParamTest,SetAndGetGainMb)141 TEST_P(LoudnessEnhancerParamTest, SetAndGetGainMb) {
142     binder_exception_t expected = isGainValid(mParamGainMb);
143     setParameters(mParamGainMb, expected);
144     if (expected == EX_NONE) {
145         validateParameters(mParamGainMb);
146     }
147 }
148 
149 using LoudnessEnhancerDataTestParam = std::pair<std::shared_ptr<IFactory>, Descriptor>;
150 
151 class LoudnessEnhancerDataTest : public ::testing::TestWithParam<LoudnessEnhancerDataTestParam>,
152                                  public LoudnessEnhancerEffectHelper {
153   public:
LoudnessEnhancerDataTest()154     LoudnessEnhancerDataTest() {
155         std::tie(mFactory, mDescriptor) = GetParam();
156         size_t channelCount =
157                 getChannelCount(AudioChannelLayout::make<AudioChannelLayout::layoutMask>(
158                         AudioChannelLayout::LAYOUT_STEREO));
159         mBufferSizeInFrames = kFrameCount * channelCount;
160         mInputBuffer.resize(mBufferSizeInFrames);
161         generateInputBuffer(mInputBuffer, 0, true, channelCount, kMaxAudioSampleValue);
162 
163         mOutputBuffer.resize(mBufferSizeInFrames);
164     }
165 
SetUp()166     void SetUp() override {
167         SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
168         SetUpLoudnessEnhancer();
169 
170         // Creating AidlMessageQueues
171         mStatusMQ = std::make_unique<EffectHelper::StatusMQ>(mOpenEffectReturn.statusMQ);
172         mInputMQ = std::make_unique<EffectHelper::DataMQ>(mOpenEffectReturn.inputDataMQ);
173         mOutputMQ = std::make_unique<EffectHelper::DataMQ>(mOpenEffectReturn.outputDataMQ);
174     }
175 
TearDown()176     void TearDown() override {
177         SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
178         TearDownLoudnessEnhancer();
179     }
180 
181     // Add gains to the mInputBuffer and store processed output to mOutputBuffer
processAndWriteToOutput()182     void processAndWriteToOutput() {
183         // Check AidlMessageQueues are not null
184         ASSERT_TRUE(mStatusMQ->isValid());
185         ASSERT_TRUE(mInputMQ->isValid());
186         ASSERT_TRUE(mOutputMQ->isValid());
187 
188         // Enabling the process
189         ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
190         ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
191 
192         // Write from buffer to message queues and calling process
193         EXPECT_NO_FATAL_FAILURE(
194                 EffectHelper::writeToFmq(mStatusMQ, mInputMQ, mInputBuffer, mVersion));
195 
196         // Read the updated message queues into buffer
197         EXPECT_NO_FATAL_FAILURE(EffectHelper::readFromFmq(mStatusMQ, 1, mOutputMQ,
198                                                           mOutputBuffer.size(), mOutputBuffer));
199 
200         // Disable the process
201         ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
202     }
203 
assertGreaterGain(const std::vector<float> & first,const std::vector<float> & second)204     void assertGreaterGain(const std::vector<float>& first, const std::vector<float>& second) {
205         for (size_t i = 0; i < first.size(); i++) {
206             if (first[i] != 0) {
207                 ASSERT_GT(abs(first[i]), abs(second[i]));
208 
209             } else {
210                 ASSERT_EQ(first[i], second[i]);
211             }
212         }
213     }
214 
assertSequentialGains(const std::vector<int> & gainValues,bool isIncreasing)215     void assertSequentialGains(const std::vector<int>& gainValues, bool isIncreasing) {
216         std::vector<float> baseOutput(mBufferSizeInFrames);
217 
218         // Process a reference output buffer with 0 gain which gives compressed input values
219         binder_exception_t expected;
220         expected = isGainValid(kZeroGain);
221         ASSERT_EQ(expected, EX_NONE);
222         setParameters(kZeroGain, expected);
223         ASSERT_NO_FATAL_FAILURE(processAndWriteToOutput());
224         baseOutput = mOutputBuffer;
225 
226         // Compare the outputs for increasing gain
227         for (int gain : gainValues) {
228             // Setting the parameters
229             binder_exception_t expected = isGainValid(gain);
230             if (expected != EX_NONE) {
231                 GTEST_SKIP() << "Gains not supported.";
232             }
233             setParameters(gain, expected);
234             ASSERT_NO_FATAL_FAILURE(processAndWriteToOutput());
235 
236             // Compare the mOutputBuffer values with baseOutput and update it
237             if (isIncreasing) {
238                 ASSERT_NO_FATAL_FAILURE(assertGreaterGain(mOutputBuffer, baseOutput));
239             } else {
240                 ASSERT_NO_FATAL_FAILURE(assertGreaterGain(baseOutput, mOutputBuffer));
241             }
242 
243             baseOutput = mOutputBuffer;
244         }
245     }
246 
247     std::unique_ptr<StatusMQ> mStatusMQ;
248     std::unique_ptr<DataMQ> mInputMQ;
249     std::unique_ptr<DataMQ> mOutputMQ;
250 
251     std::vector<float> mInputBuffer;
252     std::vector<float> mOutputBuffer;
253     size_t mBufferSizeInFrames;
254 };
255 
TEST_P(LoudnessEnhancerDataTest,IncreasingGains)256 TEST_P(LoudnessEnhancerDataTest, IncreasingGains) {
257     static const std::vector<int> kIncreasingGains = {50, 100};
258 
259     assertSequentialGains(kIncreasingGains, true /*isIncreasing*/);
260 }
261 
TEST_P(LoudnessEnhancerDataTest,DecreasingGains)262 TEST_P(LoudnessEnhancerDataTest, DecreasingGains) {
263     static const std::vector<int> kDecreasingGains = {-50, -100};
264 
265     assertSequentialGains(kDecreasingGains, false /*isIncreasing*/);
266 }
267 
TEST_P(LoudnessEnhancerDataTest,MinimumGain)268 TEST_P(LoudnessEnhancerDataTest, MinimumGain) {
269     // Setting the parameters
270     binder_exception_t expected = isGainValid(kMinGain);
271     if (expected != EX_NONE) {
272         GTEST_SKIP() << "Minimum integer value not supported";
273     }
274     setParameters(kMinGain, expected);
275     ASSERT_NO_FATAL_FAILURE(processAndWriteToOutput());
276 
277     // Validate that mOutputBuffer has 0 values for INT_MIN gain
278     for (size_t i = 0; i < mOutputBuffer.size(); i++) {
279         ASSERT_FLOAT_EQ(mOutputBuffer[i], 0);
280     }
281 }
282 
TEST_P(LoudnessEnhancerDataTest,MaximumGain)283 TEST_P(LoudnessEnhancerDataTest, MaximumGain) {
284     // Setting the parameters
285     binder_exception_t expected = isGainValid(kMaxGain);
286     if (expected != EX_NONE) {
287         GTEST_SKIP() << "Maximum integer value not supported";
288     }
289     setParameters(kMaxGain, expected);
290     ASSERT_NO_FATAL_FAILURE(processAndWriteToOutput());
291 
292     // Validate that mOutputBuffer reaches to kMaxAudioSampleValue for INT_MAX gain
293     for (size_t i = 0; i < mOutputBuffer.size(); i++) {
294         if (mInputBuffer[i] != 0) {
295             EXPECT_NEAR(kMaxAudioSampleValue, abs(mOutputBuffer[i]), kAbsError);
296         } else {
297             ASSERT_EQ(mOutputBuffer[i], mInputBuffer[i]);
298         }
299     }
300 }
301 
302 INSTANTIATE_TEST_SUITE_P(
303         LoudnessEnhancerTest, LoudnessEnhancerParamTest,
304         ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
305                                    IFactory::descriptor, getEffectTypeUuidLoudnessEnhancer())),
306                            testing::ValuesIn(kGainMbValues)),
__anonba4a10b60102(const testing::TestParamInfo<LoudnessEnhancerParamTest::ParamType>& info) 307         [](const testing::TestParamInfo<LoudnessEnhancerParamTest::ParamType>& info) {
308             auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
309             std::string gainMb = std::to_string(std::get<PARAM_GAIN_MB>(info.param));
310             std::string name = getPrefix(descriptor) + "_gainMb_" + gainMb;
311             std::replace_if(
312                     name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
313             return name;
314         });
315 
316 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(LoudnessEnhancerParamTest);
317 
318 INSTANTIATE_TEST_SUITE_P(
319         LoudnessEnhancerTest, LoudnessEnhancerDataTest,
320         testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
321                 IFactory::descriptor, getEffectTypeUuidLoudnessEnhancer())),
__anonba4a10b60302(const testing::TestParamInfo<LoudnessEnhancerDataTest::ParamType>& info) 322         [](const testing::TestParamInfo<LoudnessEnhancerDataTest::ParamType>& info) {
323             auto descriptor = info.param;
324             std::string name = getPrefix(descriptor.second);
325             std::replace_if(
326                     name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
327             return name;
328         });
329 
330 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(LoudnessEnhancerDataTest);
331 
main(int argc,char ** argv)332 int main(int argc, char** argv) {
333     ::testing::InitGoogleTest(&argc, argv);
334     ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
335     ABinderProcess_setThreadPoolMaxThreadCount(1);
336     ABinderProcess_startThreadPool();
337     return RUN_ALL_TESTS();
338 }
339