Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 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 <algorithm> |
| 18 | #include <limits> |
| 19 | #include <map> |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #define LOG_TAG "VtsHalEqualizerTest" |
| 25 | |
| 26 | #include <aidl/Gtest.h> |
| 27 | #include <aidl/Vintf.h> |
| 28 | #include <android-base/logging.h> |
| 29 | #include <android-base/properties.h> |
| 30 | #include <android/binder_interface_utils.h> |
| 31 | #include <android/binder_manager.h> |
| 32 | #include <android/binder_process.h> |
| 33 | #include <gtest/gtest.h> |
| 34 | |
| 35 | #include <Utils.h> |
| 36 | #include <aidl/android/hardware/audio/effect/IEffect.h> |
| 37 | #include <aidl/android/hardware/audio/effect/IFactory.h> |
| 38 | #include <aidl/android/media/audio/common/AudioChannelLayout.h> |
| 39 | #include <aidl/android/media/audio/common/AudioDeviceType.h> |
| 40 | |
| 41 | #include "AudioHalBinderServiceUtil.h" |
| 42 | #include "EffectHelper.h" |
| 43 | #include "TestUtils.h" |
| 44 | #include "effect-impl/EffectUUID.h" |
| 45 | |
| 46 | using namespace android; |
| 47 | |
| 48 | using aidl::android::hardware::audio::effect::Capability; |
| 49 | using aidl::android::hardware::audio::effect::Descriptor; |
| 50 | using aidl::android::hardware::audio::effect::EffectNullUuid; |
| 51 | using aidl::android::hardware::audio::effect::Equalizer; |
| 52 | using aidl::android::hardware::audio::effect::EqualizerTypeUUID; |
| 53 | using aidl::android::hardware::audio::effect::IEffect; |
| 54 | using aidl::android::hardware::audio::effect::IFactory; |
| 55 | using aidl::android::hardware::audio::effect::Parameter; |
| 56 | |
| 57 | /** |
| 58 | * Here we focus on specific parameter checking, general IEffect interfaces testing performed in |
| 59 | * VtsAudioEfectTargetTest. |
| 60 | */ |
Shunkai Yao | 464775e | 2022-10-28 21:42:25 +0000 | [diff] [blame] | 61 | enum ParamName { PARAM_INSTANCE_NAME, PARAM_PRESET_INDEX, PARAM_BAND_INDEX, PARAM_BAND_LEVEL }; |
| 62 | using EqualizerParamTestParam = std::tuple<std::string, int, int, int>; |
| 63 | |
| 64 | /* |
| 65 | Testing parameter range, assuming the parameter supported by effect is in this range. |
| 66 | This range is verified with IEffect.getDescriptor(), for any index supported vts expect EX_NONE |
| 67 | from IEffect.setParameter(), otherwise expect EX_ILLEGAL_ARGUMENT. |
| 68 | */ |
| 69 | constexpr std::pair<int, int> kPresetIndexRange = {-1, 10}; // valid range [0, 9] |
| 70 | constexpr std::pair<int, int> kBandIndexRange = {-1, 5}; // valid range [0, 4] |
| 71 | constexpr std::pair<int, int> kBandLevelRange = {-5, 5}; // needs update with implementation |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 72 | |
| 73 | class EqualizerParamTest : public ::testing::TestWithParam<EqualizerParamTestParam>, |
| 74 | public EffectHelper { |
| 75 | public: |
| 76 | EqualizerParamTest() |
Shunkai Yao | 464775e | 2022-10-28 21:42:25 +0000 | [diff] [blame] | 77 | : EffectHelper(std::get<PARAM_INSTANCE_NAME>(GetParam())), |
| 78 | mParamPresetIndex(std::get<PARAM_PRESET_INDEX>(GetParam())), |
| 79 | mParamBandIndex(std::get<PARAM_BAND_INDEX>(GetParam())), |
| 80 | mParamBandLevel(std::get<PARAM_BAND_LEVEL>(GetParam())) {} |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 81 | |
| 82 | void SetUp() override { |
| 83 | CreateEffectsWithUUID(EqualizerTypeUUID); |
| 84 | initParamCommonFormat(); |
| 85 | initParamCommon(); |
| 86 | initParamSpecific(); |
| 87 | OpenEffects(EqualizerTypeUUID); |
| 88 | SCOPED_TRACE(testing::Message() << "preset: " << mParamPresetIndex << " bandIdx " |
| 89 | << mParamBandIndex << " level " << mParamBandLevel); |
| 90 | } |
| 91 | |
| 92 | void TearDown() override { |
| 93 | CloseEffects(); |
| 94 | DestroyEffects(); |
| 95 | CleanUp(); |
| 96 | } |
| 97 | |
| 98 | const int mParamPresetIndex; |
| 99 | const int mParamBandIndex; |
| 100 | const int mParamBandLevel; |
| 101 | |
| 102 | void SetAndGetEqualizerParameters() { |
| 103 | auto functor = [&](const std::shared_ptr<IEffect>& effect) { |
| 104 | for (auto& it : mTags) { |
| 105 | auto& tag = it.first; |
| 106 | auto& eq = it.second; |
| 107 | |
| 108 | // validate parameter |
| 109 | Descriptor desc; |
| 110 | ASSERT_STATUS(EX_NONE, effect->getDescriptor(&desc)); |
| 111 | const bool valid = isTagInRange(it.first, it.second, desc); |
| 112 | const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT; |
| 113 | |
| 114 | // set |
| 115 | Parameter expectParam; |
| 116 | Parameter::Specific specific; |
| 117 | specific.set<Parameter::Specific::equalizer>(*eq.get()); |
| 118 | expectParam.set<Parameter::specific>(specific); |
| 119 | EXPECT_STATUS(expected, effect->setParameter(expectParam)) |
| 120 | << expectParam.toString(); |
| 121 | |
Shunkai Yao | 464775e | 2022-10-28 21:42:25 +0000 | [diff] [blame] | 122 | // only get if parameter in range and set success |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 123 | if (expected == EX_NONE) { |
| 124 | Parameter getParam; |
| 125 | Parameter::Specific::Id id; |
| 126 | id.set<Parameter::Specific::Id::equalizerTag>(tag); |
| 127 | // if set success, then get should match |
| 128 | EXPECT_STATUS(expected, effect->getParameter(id, &getParam)); |
| 129 | EXPECT_EQ(expectParam, getParam) << "\n" |
| 130 | << expectParam.toString() << "\n" |
| 131 | << getParam.toString(); |
| 132 | } |
| 133 | } |
| 134 | }; |
| 135 | EXPECT_NO_FATAL_FAILURE(ForEachEffect(functor)); |
| 136 | } |
| 137 | |
| 138 | void addPresetParam(int preset) { |
| 139 | Equalizer eq; |
| 140 | eq.set<Equalizer::preset>(preset); |
| 141 | mTags.push_back({Equalizer::preset, std::make_unique<Equalizer>(std::move(eq))}); |
| 142 | } |
| 143 | |
| 144 | void addBandLevelsParam(std::vector<Equalizer::BandLevel>& bandLevels) { |
| 145 | Equalizer eq; |
| 146 | eq.set<Equalizer::bandLevels>(bandLevels); |
| 147 | mTags.push_back({Equalizer::bandLevels, std::make_unique<Equalizer>(std::move(eq))}); |
| 148 | } |
| 149 | |
| 150 | bool isTagInRange(const Equalizer::Tag& tag, const std::unique_ptr<Equalizer>& eq, |
| 151 | const Descriptor& desc) const { |
| 152 | std::cout << "xxx" << toString(tag) << " " << desc.toString(); |
| 153 | const Equalizer::Capability& eqCap = desc.capability.get<Capability::equalizer>(); |
| 154 | switch (tag) { |
| 155 | case Equalizer::preset: { |
| 156 | int index = eq->get<Equalizer::preset>(); |
| 157 | return isPresetIndexInRange(eqCap, index); |
| 158 | } |
| 159 | case Equalizer::bandLevels: { |
| 160 | auto& bandLevel = eq->get<Equalizer::bandLevels>(); |
| 161 | return isBandIndexInRange(eqCap, bandLevel); |
| 162 | } |
| 163 | default: |
| 164 | return false; |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | bool isPresetIndexInRange(const Equalizer::Capability& cap, int idx) const { |
| 170 | const auto [min, max] = |
| 171 | std::minmax_element(cap.presets.begin(), cap.presets.end(), |
| 172 | [](const auto& a, const auto& b) { return a.index < b.index; }); |
| 173 | return idx >= min->index && idx <= max->index; |
| 174 | } |
| 175 | |
| 176 | bool isBandIndexInRange(const Equalizer::Capability& cap, |
| 177 | const std::vector<Equalizer::BandLevel>& bandLevel) const { |
| 178 | for (auto& it : bandLevel) { |
| 179 | if (!isBandIndexInRange(cap, it.index)) return false; |
| 180 | } |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | bool isBandIndexInRange(const Equalizer::Capability& cap, int idx) const { |
| 185 | const auto [min, max] = |
| 186 | std::minmax_element(cap.bandFrequencies.begin(), cap.bandFrequencies.end(), |
| 187 | [](const auto& a, const auto& b) { return a.index < b.index; }); |
| 188 | return idx >= min->index && idx <= max->index; |
| 189 | } |
| 190 | |
| 191 | private: |
| 192 | Equalizer::VendorExtension mVendorExtension; |
| 193 | std::vector<std::pair<Equalizer::Tag, std::unique_ptr<Equalizer>>> mTags; |
| 194 | |
| 195 | bool validCapabilityTag(Capability& cap) { return cap.getTag() == Capability::equalizer; } |
| 196 | |
| 197 | void initParamSpecific() { |
| 198 | Equalizer eq; |
| 199 | eq.set<Equalizer::preset>(0); |
| 200 | Parameter::Specific specific; |
| 201 | specific.set<Parameter::Specific::equalizer>(eq); |
| 202 | setSpecific(specific); |
| 203 | } |
| 204 | |
| 205 | void CleanUp() { mTags.clear(); } |
| 206 | }; |
| 207 | |
| 208 | TEST_P(EqualizerParamTest, SetAndGetPreset) { |
| 209 | EXPECT_NO_FATAL_FAILURE(addPresetParam(mParamPresetIndex)); |
| 210 | SetAndGetEqualizerParameters(); |
| 211 | } |
| 212 | |
| 213 | TEST_P(EqualizerParamTest, SetAndGetSingleBand) { |
| 214 | Equalizer::BandLevel bandLevel = {mParamBandIndex, mParamBandLevel}; |
| 215 | std::vector<Equalizer::BandLevel> bandLevels; |
| 216 | bandLevels.push_back(bandLevel); |
| 217 | EXPECT_NO_FATAL_FAILURE(addBandLevelsParam(bandLevels)); |
| 218 | SetAndGetEqualizerParameters(); |
| 219 | } |
| 220 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 221 | INSTANTIATE_TEST_SUITE_P( |
| 222 | EqualizerTest, EqualizerParamTest, |
Shunkai Yao | 464775e | 2022-10-28 21:42:25 +0000 | [diff] [blame] | 223 | ::testing::Combine( |
| 224 | testing::ValuesIn(android::getAidlHalInstanceNames(IFactory::descriptor)), |
| 225 | testing::Range(kPresetIndexRange.first, kPresetIndexRange.second), |
| 226 | testing::Range(kBandIndexRange.first, kBandIndexRange.second), |
| 227 | testing::Range(kBandLevelRange.first, kBandLevelRange.second)), |
| 228 | [](const testing::TestParamInfo<EqualizerParamTest::ParamType>& info) { |
| 229 | std::string instance = std::get<PARAM_INSTANCE_NAME>(info.param); |
| 230 | std::string presetIdx = std::to_string(std::get<PARAM_PRESET_INDEX>(info.param)); |
| 231 | std::string bandIdx = std::to_string(std::get<PARAM_BAND_INDEX>(info.param)); |
| 232 | std::string bandLevel = std::to_string(std::get<PARAM_BAND_LEVEL>(info.param)); |
| 233 | |
| 234 | std::string name = instance + "_presetIndex" + presetIdx + "_bandIndex" + bandIdx + |
| 235 | "_bandLevel" + bandLevel; |
| 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(EqualizerParamTest); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 242 | |
| 243 | int main(int argc, char** argv) { |
| 244 | ::testing::InitGoogleTest(&argc, argv); |
| 245 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 246 | ABinderProcess_startThreadPool(); |
| 247 | return RUN_ALL_TESTS(); |
| 248 | } |