xref: /aosp_15_r20/hardware/interfaces/audio/aidl/vts/VtsHalVolumeTargetTest.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 #define LOG_TAG "VtsHalVolumeTest"
18 #include <android-base/logging.h>
19 
20 #include "EffectHelper.h"
21 
22 using namespace android;
23 
24 using aidl::android::hardware::audio::common::getChannelCount;
25 using aidl::android::hardware::audio::effect::Descriptor;
26 using aidl::android::hardware::audio::effect::getEffectTypeUuidVolume;
27 using aidl::android::hardware::audio::effect::IEffect;
28 using aidl::android::hardware::audio::effect::IFactory;
29 using aidl::android::hardware::audio::effect::Parameter;
30 using aidl::android::hardware::audio::effect::Volume;
31 using android::hardware::audio::common::testing::detail::TestExecutionTracer;
32 
33 class VolumeControlHelper : public EffectHelper {
34   public:
SetUpVolumeControl()35     void SetUpVolumeControl() {
36         ASSERT_NE(nullptr, mFactory);
37         ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
38         initFrameCount();
39         Parameter::Specific specific = getDefaultParamSpecific();
40         Parameter::Common common = createParamCommon(
41                 0 /* session */, 1 /* ioHandle */, kSamplingFrequency /* iSampleRate */,
42                 kSamplingFrequency /* oSampleRate */, mInputFrameCount /* iFrameCount */,
43                 mInputFrameCount /* oFrameCount */);
44         ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &mOpenEffectReturn, EX_NONE));
45         ASSERT_NE(nullptr, mEffect);
46     }
47 
TearDownVolumeControl()48     void TearDownVolumeControl() {
49         ASSERT_NO_FATAL_FAILURE(close(mEffect));
50         ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
51         mOpenEffectReturn = IEffect::OpenEffectReturn{};
52     }
53 
getDefaultParamSpecific()54     Parameter::Specific getDefaultParamSpecific() {
55         Volume vol = Volume::make<Volume::levelDb>(kMinLevel);
56         Parameter::Specific specific = Parameter::Specific::make<Parameter::Specific::volume>(vol);
57         return specific;
58     }
59 
createVolumeParam(int param,Volume::Tag volTag)60     Parameter createVolumeParam(int param, Volume::Tag volTag) {
61         return Parameter::make<Parameter::specific>(
62                 Parameter::Specific::make<Parameter::Specific::volume>(
63                         (volTag == Volume::mute) ? Volume::make<Volume::mute>(param)
64                                                  : Volume::make<Volume::levelDb>(param)));
65     }
66 
initFrameCount()67     void initFrameCount() {
68         int channelCount = getChannelCount(
69                 AudioChannelLayout::make<AudioChannelLayout::layoutMask>(kDefaultChannelLayout));
70         mInputFrameCount = kBufferSize / channelCount;
71         mOutputFrameCount = kBufferSize / channelCount;
72     }
73 
isLevelValid(int level)74     bool isLevelValid(int level) {
75         auto vol = Volume::make<Volume::levelDb>(level);
76         return isParameterValid<Volume, Range::volume>(vol, mDescriptor);
77     }
78 
setAndVerifyParameters(Volume::Tag volTag,int param,binder_exception_t expected)79     void setAndVerifyParameters(Volume::Tag volTag, int param, binder_exception_t expected) {
80         auto expectedParam = createVolumeParam(param, volTag);
81         EXPECT_STATUS(expected, mEffect->setParameter(expectedParam)) << expectedParam.toString();
82 
83         if (expected == EX_NONE) {
84             Volume::Id volId = Volume::Id::make<Volume::Id::commonTag>(volTag);
85 
86             auto id = Parameter::Id::make<Parameter::Id::volumeTag>(volId);
87             // get parameter
88             Parameter getParam;
89             // if set success, then get should match
90             EXPECT_STATUS(expected, mEffect->getParameter(id, &getParam));
91             EXPECT_EQ(expectedParam, getParam) << "\nexpectedParam:" << expectedParam.toString()
92                                                << "\ngetParam:" << getParam.toString();
93         }
94     }
95 
96     static constexpr int kDurationMilliSec = 720;
97     static constexpr int kBufferSize = kSamplingFrequency * kDurationMilliSec / 1000;
98     static constexpr int kMinLevel = -96;
99     static constexpr int kDefaultChannelLayout = AudioChannelLayout::LAYOUT_STEREO;
100     long mInputFrameCount, mOutputFrameCount;
101     std::shared_ptr<IFactory> mFactory;
102     std::shared_ptr<IEffect> mEffect;
103     IEffect::OpenEffectReturn mOpenEffectReturn;
104     Descriptor mDescriptor;
105 };
106 /**
107  * Here we focus on specific parameter checking, general IEffect interfaces testing performed in
108  * VtsAudioEffectTargetTest.
109  */
110 enum VolumeLevelParamName { PARAM_INSTANCE_NAME, PARAM_LEVEL };
111 using VolumeLevelTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int>;
112 
113 class VolumeLevelParamTest : public ::testing::TestWithParam<VolumeLevelTestParam>,
114                              public VolumeControlHelper {
115   public:
VolumeLevelParamTest()116     VolumeLevelParamTest() : mParamLevel(std::get<PARAM_LEVEL>(GetParam())) {
117         std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
118     }
119 
SetUp()120     void SetUp() override { ASSERT_NO_FATAL_FAILURE(SetUpVolumeControl()); }
TearDown()121     void TearDown() override { TearDownVolumeControl(); }
122 
123     int mParamLevel = 0;
124 };
125 
TEST_P(VolumeLevelParamTest,SetAndGetParams)126 TEST_P(VolumeLevelParamTest, SetAndGetParams) {
127     ASSERT_NO_FATAL_FAILURE(
128             setAndVerifyParameters(Volume::levelDb, mParamLevel,
129                                    isLevelValid(mParamLevel) ? EX_NONE : EX_ILLEGAL_ARGUMENT));
130 }
131 
132 enum VolumeMuteParamName { MUTE_PARAM_INSTANCE_NAME, PARAM_MUTE };
133 using VolumeMuteTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, bool>;
134 
135 class VolumeMuteParamTest : public ::testing::TestWithParam<VolumeMuteTestParam>,
136                             public VolumeControlHelper {
137   public:
VolumeMuteParamTest()138     VolumeMuteParamTest() : mParamMute(std::get<PARAM_MUTE>(GetParam())) {
139         std::tie(mFactory, mDescriptor) = std::get<MUTE_PARAM_INSTANCE_NAME>(GetParam());
140     }
141 
SetUp()142     void SetUp() override { ASSERT_NO_FATAL_FAILURE(SetUpVolumeControl()); }
TearDown()143     void TearDown() override { TearDownVolumeControl(); }
144 
145     bool mParamMute = false;
146 };
147 
TEST_P(VolumeMuteParamTest,SetAndGetParams)148 TEST_P(VolumeMuteParamTest, SetAndGetParams) {
149     ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(Volume::mute, mParamMute, EX_NONE));
150 }
151 
152 using VolumeDataTestParam = std::pair<std::shared_ptr<IFactory>, Descriptor>;
153 
154 class VolumeDataTest : public ::testing::TestWithParam<VolumeDataTestParam>,
155                        public VolumeControlHelper {
156   public:
VolumeDataTest()157     VolumeDataTest()
158         : kVsrApiLevel(
159                   android::base::GetIntProperty("ro.vendor.api_level", __ANDROID_API_FUTURE__)) {
160         std::tie(mFactory, mDescriptor) = GetParam();
161         mInput.resize(kBufferSize);
162         mInputMag.resize(mTestFrequencies.size());
163         mBinOffsets.resize(mTestFrequencies.size());
164         roundToFreqCenteredToFftBin(mTestFrequencies, mBinOffsets, kBinWidth);
165         generateSineWave(mTestFrequencies, mInput);
166         mInputMag = calculateMagnitude(mInput, mBinOffsets, kNPointFFT);
167     }
168 
calculatePercentageDiff(const std::vector<float> & outputMag)169     std::vector<int> calculatePercentageDiff(const std::vector<float>& outputMag) {
170         std::vector<int> percentages(mTestFrequencies.size());
171 
172         for (size_t i = 0; i < mInputMag.size(); i++) {
173             float diff = mInputMag[i] - outputMag[i];
174             percentages[i] = std::round(diff / mInputMag[i] * 100);
175         }
176         return percentages;
177     }
178 
179     // Convert Decibel value to Percentage
percentageDb(float level)180     int percentageDb(float level) { return std::round((1 - (pow(10, level / 20))) * 100); }
181 
SetUp()182     void SetUp() override {
183         SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
184         // Skips test fixture if api_level <= 34 (__ANDROID_API_U__).
185         if (kVsrApiLevel <= __ANDROID_API_U__) GTEST_SKIP();
186         ASSERT_NO_FATAL_FAILURE(SetUpVolumeControl());
187     }
TearDown()188     void TearDown() override {
189         SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
190         if (kVsrApiLevel <= __ANDROID_API_U__) GTEST_SKIP();
191         TearDownVolumeControl();
192     }
193 
194     const int kVsrApiLevel;
195     static constexpr int kMaxAudioSample = 1;
196     static constexpr int kTransitionDuration = 300;
197     static constexpr int kNPointFFT = 16384;
198     static constexpr float kBinWidth = (float)kSamplingFrequency / kNPointFFT;
199     static constexpr size_t offset = kSamplingFrequency * kTransitionDuration / 1000;
200     static constexpr float kBaseLevel = 0;
201     std::vector<int> mTestFrequencies = {100, 1000};
202     std::vector<float> mInput;
203     std::vector<float> mInputMag;
204     std::vector<int> mBinOffsets;
205 };
206 
TEST_P(VolumeDataTest,ApplyLevelMuteUnmute)207 TEST_P(VolumeDataTest, ApplyLevelMuteUnmute) {
208     std::vector<float> output(kBufferSize);
209     std::vector<int> diffs(mTestFrequencies.size());
210     std::vector<float> outputMag(mTestFrequencies.size());
211 
212     if (!isLevelValid(kBaseLevel)) {
213         GTEST_SKIP() << "Volume Level not supported, skipping the test\n";
214     }
215 
216     // Apply Volume Level
217 
218     ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(Volume::levelDb, kBaseLevel, EX_NONE));
219     ASSERT_NO_FATAL_FAILURE(processAndWriteToOutput(mInput, output, mEffect, &mOpenEffectReturn));
220 
221     outputMag = calculateMagnitude(output, mBinOffsets, kNPointFFT);
222     diffs = calculatePercentageDiff(outputMag);
223 
224     for (size_t i = 0; i < diffs.size(); i++) {
225         ASSERT_EQ(diffs[i], percentageDb(kBaseLevel));
226     }
227 
228     // Apply Mute
229 
230     ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(Volume::mute, true /*mute*/, EX_NONE));
231     ASSERT_NO_FATAL_FAILURE(processAndWriteToOutput(mInput, output, mEffect, &mOpenEffectReturn));
232 
233     std::vector<float> subOutputMute(output.begin() + offset, output.end());
234     outputMag = calculateMagnitude(subOutputMute, mBinOffsets, kNPointFFT);
235     diffs = calculatePercentageDiff(outputMag);
236 
237     for (size_t i = 0; i < diffs.size(); i++) {
238         ASSERT_EQ(diffs[i], percentageDb(kMinLevel /*Mute*/));
239     }
240 
241     // Verifying Fade out
242     outputMag = calculateMagnitude(output, mBinOffsets, kNPointFFT);
243     diffs = calculatePercentageDiff(outputMag);
244 
245     for (size_t i = 0; i < diffs.size(); i++) {
246         ASSERT_LT(diffs[i], percentageDb(kMinLevel /*Mute*/));
247     }
248 
249     // Apply Unmute
250 
251     ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(Volume::mute, false /*unmute*/, EX_NONE));
252     ASSERT_NO_FATAL_FAILURE(processAndWriteToOutput(mInput, output, mEffect, &mOpenEffectReturn));
253 
254     std::vector<float> subOutputUnmute(output.begin() + offset, output.end());
255 
256     outputMag = calculateMagnitude(subOutputUnmute, mBinOffsets, kNPointFFT);
257     diffs = calculatePercentageDiff(outputMag);
258 
259     for (size_t i = 0; i < diffs.size(); i++) {
260         ASSERT_EQ(diffs[i], percentageDb(kBaseLevel));
261     }
262 
263     // Verifying Fade in
264     outputMag = calculateMagnitude(output, mBinOffsets, kNPointFFT);
265     diffs = calculatePercentageDiff(outputMag);
266 
267     for (size_t i = 0; i < diffs.size(); i++) {
268         ASSERT_GT(diffs[i], percentageDb(kBaseLevel));
269     }
270 }
271 
TEST_P(VolumeDataTest,DecreasingLevels)272 TEST_P(VolumeDataTest, DecreasingLevels) {
273     std::vector<int> decreasingLevels = {-24, -48, -96};
274     std::vector<float> baseOutput(kBufferSize);
275     std::vector<int> baseDiffs(mTestFrequencies.size());
276     std::vector<float> outputMag(mTestFrequencies.size());
277 
278     if (!isLevelValid(kBaseLevel)) {
279         GTEST_SKIP() << "Volume Level not supported, skipping the test\n";
280     }
281 
282     ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(Volume::levelDb, kBaseLevel, EX_NONE));
283     ASSERT_NO_FATAL_FAILURE(
284             processAndWriteToOutput(mInput, baseOutput, mEffect, &mOpenEffectReturn));
285 
286     outputMag = calculateMagnitude(baseOutput, mBinOffsets, kNPointFFT);
287     baseDiffs = calculatePercentageDiff(outputMag);
288 
289     for (int level : decreasingLevels) {
290         std::vector<float> output(kBufferSize);
291         std::vector<int> diffs(mTestFrequencies.size());
292 
293         // Skipping the further steps for unnsupported level values
294         if (!isLevelValid(level)) {
295             continue;
296         }
297         ASSERT_NO_FATAL_FAILURE(setAndVerifyParameters(Volume::levelDb, level, EX_NONE));
298         ASSERT_NO_FATAL_FAILURE(
299                 processAndWriteToOutput(mInput, output, mEffect, &mOpenEffectReturn));
300 
301         outputMag = calculateMagnitude(output, mBinOffsets, kNPointFFT);
302         diffs = calculatePercentageDiff(outputMag);
303 
304         // Decrease in volume level results in greater magnitude difference
305         for (size_t i = 0; i < diffs.size(); i++) {
306             ASSERT_GT(diffs[i], baseDiffs[i]);
307         }
308 
309         baseDiffs = diffs;
310     }
311 }
312 
313 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair;
314 INSTANTIATE_TEST_SUITE_P(
315         VolumeTest, VolumeLevelParamTest,
316         ::testing::Combine(
317                 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
318                                           IFactory::descriptor, getEffectTypeUuidVolume())),
319                 testing::ValuesIn(
320                         EffectHelper::getTestValueSet<Volume, int, Range::volume, Volume::levelDb>(
321                                 kDescPair, EffectHelper::expandTestValueBasic<int>))),
__anon9b402f9d0102(const testing::TestParamInfo<VolumeLevelParamTest::ParamType>& info) 322         [](const testing::TestParamInfo<VolumeLevelParamTest::ParamType>& info) {
323             auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
324             std::string level = std::to_string(std::get<PARAM_LEVEL>(info.param));
325             std::string name = getPrefix(descriptor) + "_level" + level;
326             std::replace_if(
327                     name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
328             return name;
329         });
330 
331 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VolumeLevelParamTest);
332 
333 INSTANTIATE_TEST_SUITE_P(
334         VolumeTest, VolumeMuteParamTest,
335         ::testing::Combine(
336                 testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors(
337                                           IFactory::descriptor, getEffectTypeUuidVolume())),
338                 testing::Bool() /* mute */),
__anon9b402f9d0302(const testing::TestParamInfo<VolumeMuteParamTest::ParamType>& info) 339         [](const testing::TestParamInfo<VolumeMuteParamTest::ParamType>& info) {
340             auto descriptor = std::get<MUTE_PARAM_INSTANCE_NAME>(info.param).second;
341             std::string mute = std::to_string(std::get<PARAM_MUTE>(info.param));
342             std::string name = getPrefix(descriptor) + "_mute" + mute;
343             std::replace_if(
344                     name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
345             return name;
346         });
347 
348 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VolumeMuteParamTest);
349 
350 INSTANTIATE_TEST_SUITE_P(VolumeTest, VolumeDataTest,
351                          testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
352                                  IFactory::descriptor, getEffectTypeUuidVolume())),
__anon9b402f9d0502(const testing::TestParamInfo<VolumeDataTest::ParamType>& info) 353                          [](const testing::TestParamInfo<VolumeDataTest::ParamType>& info) {
354                              auto descriptor = info.param;
355                              std::string name = getPrefix(descriptor.second);
356                              std::replace_if(
357                                      name.begin(), name.end(),
358                                      [](const char c) { return !std::isalnum(c); }, '_');
359                              return name;
360                          });
361 
362 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VolumeDataTest);
363 
main(int argc,char ** argv)364 int main(int argc, char** argv) {
365     ::testing::InitGoogleTest(&argc, argv);
366     ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
367     ABinderProcess_setThreadPoolMaxThreadCount(1);
368     ABinderProcess_startThreadPool();
369     return RUN_ALL_TESTS();
370 }
371