Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [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 <aidl/Vintf.h> |
| 18 | |
| 19 | #define LOG_TAG "VtsHalVisualizerTest" |
| 20 | |
| 21 | #include <Utils.h> |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 22 | #include <android/binder_enums.h> |
| 23 | #include <unordered_set> |
| 24 | |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 25 | #include "EffectHelper.h" |
| 26 | |
| 27 | using namespace android; |
| 28 | |
| 29 | using aidl::android::hardware::audio::effect::Capability; |
| 30 | using aidl::android::hardware::audio::effect::Descriptor; |
| 31 | using aidl::android::hardware::audio::effect::IEffect; |
| 32 | using aidl::android::hardware::audio::effect::IFactory; |
| 33 | using aidl::android::hardware::audio::effect::kVisualizerTypeUUID; |
| 34 | using aidl::android::hardware::audio::effect::Parameter; |
| 35 | using aidl::android::hardware::audio::effect::Visualizer; |
| 36 | |
| 37 | /** |
| 38 | * Here we focus on specific parameter checking, general IEffect interfaces testing performed in |
| 39 | * VtsAudioEffectTargetTest. |
| 40 | */ |
| 41 | enum ParamName { |
| 42 | PARAM_INSTANCE_NAME, |
| 43 | PARAM_CAPTURE_SIZE, |
| 44 | PARAM_SCALING_MODE, |
| 45 | PARAM_MEASUREMENT_MODE, |
| 46 | PARAM_LATENCY, |
| 47 | }; |
| 48 | using VisualizerParamTestParam = |
| 49 | std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int, Visualizer::ScalingMode, |
| 50 | Visualizer::MeasurementMode, int>; |
| 51 | |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 52 | class VisualizerParamTest : public ::testing::TestWithParam<VisualizerParamTestParam>, |
| 53 | public EffectHelper { |
| 54 | public: |
| 55 | VisualizerParamTest() |
| 56 | : mCaptureSize(std::get<PARAM_CAPTURE_SIZE>(GetParam())), |
| 57 | mScalingMode(std::get<PARAM_SCALING_MODE>(GetParam())), |
| 58 | mMeasurementMode(std::get<PARAM_MEASUREMENT_MODE>(GetParam())), |
| 59 | mLatency(std::get<PARAM_LATENCY>(GetParam())) { |
| 60 | std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam()); |
| 61 | } |
| 62 | |
| 63 | void SetUp() override { |
| 64 | ASSERT_NE(nullptr, mFactory); |
| 65 | ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor)); |
| 66 | |
| 67 | Parameter::Specific specific = getDefaultParamSpecific(); |
| 68 | Parameter::Common common = EffectHelper::createParamCommon( |
| 69 | 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */, |
| 70 | kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */); |
| 71 | IEffect::OpenEffectReturn ret; |
| 72 | ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE)); |
| 73 | ASSERT_NE(nullptr, mEffect); |
| 74 | } |
| 75 | |
| 76 | void TearDown() override { |
| 77 | ASSERT_NO_FATAL_FAILURE(close(mEffect)); |
| 78 | ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect)); |
| 79 | } |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 80 | Parameter::Specific getDefaultParamSpecific() { |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 81 | Visualizer vs = Visualizer::make<Visualizer::captureSamples>( |
| 82 | mDescriptor.capability.get<Capability::visualizer>().captureSampleRange.max); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 83 | Parameter::Specific specific = |
| 84 | Parameter::Specific::make<Parameter::Specific::visualizer>(vs); |
| 85 | return specific; |
| 86 | } |
| 87 | |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 88 | static const std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kFactoryDescList; |
| 89 | static const std::unordered_set<Visualizer::ScalingMode> kScalingModeValues; |
| 90 | static const std::unordered_set<Visualizer::MeasurementMode> kMeasurementModeValues; |
| 91 | static const std::unordered_set<int> kLatencyValues; |
| 92 | static const std::unordered_set<int> kCaptureSizeValues; |
| 93 | |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 94 | static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100; |
| 95 | std::shared_ptr<IFactory> mFactory; |
| 96 | std::shared_ptr<IEffect> mEffect; |
| 97 | Descriptor mDescriptor; |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 98 | int mCaptureSize; |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 99 | Visualizer::ScalingMode mScalingMode = Visualizer::ScalingMode::NORMALIZED; |
| 100 | Visualizer::MeasurementMode mMeasurementMode = Visualizer::MeasurementMode::NONE; |
| 101 | int mLatency = 0; |
| 102 | |
| 103 | void SetAndGetCommonParameters() { |
| 104 | for (auto& it : mCommonTags) { |
| 105 | auto& tag = it.first; |
| 106 | auto& vs = it.second; |
| 107 | |
| 108 | // validate parameter |
| 109 | Descriptor desc; |
| 110 | ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc)); |
| 111 | const bool valid = isTagInRange(tag, vs, desc); |
| 112 | const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT; |
| 113 | |
| 114 | // set parameter |
| 115 | Parameter expectParam; |
| 116 | Parameter::Specific specific; |
| 117 | specific.set<Parameter::Specific::visualizer>(vs); |
| 118 | expectParam.set<Parameter::specific>(specific); |
| 119 | EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString(); |
| 120 | |
| 121 | // only get if parameter in range and set success |
| 122 | if (expected == EX_NONE) { |
| 123 | Parameter getParam; |
| 124 | Parameter::Id id; |
| 125 | Visualizer::Id vsId; |
| 126 | vsId.set<Visualizer::Id::commonTag>(tag); |
| 127 | id.set<Parameter::Id::visualizerTag>(vsId); |
| 128 | EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam)); |
| 129 | |
| 130 | EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString() |
| 131 | << "\ngetParam:" << getParam.toString(); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void SetAndGetSetOnlyParameters() { |
| 137 | for (auto& it : mSetOnlyParamTags) { |
| 138 | auto& tag = it.first; |
| 139 | auto& vs = it.second; |
| 140 | |
| 141 | // validate parameter |
| 142 | Descriptor desc; |
| 143 | ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc)); |
| 144 | const bool valid = isSetOnlyParamTagInRange(tag, vs, desc); |
| 145 | const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT; |
| 146 | |
| 147 | // set parameter |
| 148 | Parameter expectParam; |
| 149 | Parameter::Specific specific; |
| 150 | specific.set<Parameter::Specific::visualizer>(vs); |
| 151 | expectParam.set<Parameter::specific>(specific); |
| 152 | ASSERT_STATUS(expected, mEffect->setParameter(expectParam)); |
| 153 | |
| 154 | // parameter defined in this setOnlyParameter union must be settable via |
| 155 | // setParameter(), but must not be gettable |
| 156 | Parameter getParam; |
| 157 | Parameter::Id id; |
| 158 | Visualizer::Id vsId; |
| 159 | vsId.set<Visualizer::Id::setOnlyParamTag>(tag); |
| 160 | id.set<Parameter::Id::visualizerTag>(vsId); |
| 161 | EXPECT_STATUS(EX_ILLEGAL_ARGUMENT, mEffect->getParameter(id, &getParam)); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void GetandSetGetOnlyParameters() { |
| 166 | for (auto& tag : mGetOnlyParamTags) { |
| 167 | // get parameter |
| 168 | Parameter getParam; |
| 169 | Parameter::Id id; |
| 170 | Visualizer::Id vsId; |
| 171 | vsId.set<Visualizer::Id::getOnlyParamTag>(tag); |
| 172 | id.set<Parameter::Id::visualizerTag>(vsId); |
| 173 | ASSERT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam)); |
| 174 | |
| 175 | // parameter defined in this getOnlyParameter union must be gettable via |
| 176 | // getParameter(), but must not be settable |
| 177 | // set parameter |
| 178 | ASSERT_STATUS(EX_ILLEGAL_ARGUMENT, mEffect->setParameter(getParam)); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void addCaptureSizeParam(int captureSize) { |
| 183 | Visualizer vs; |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 184 | vs.set<Visualizer::captureSamples>(captureSize); |
| 185 | mCommonTags.push_back({Visualizer::captureSamples, vs}); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void addScalingModeParam(Visualizer::ScalingMode scalingMode) { |
| 189 | Visualizer vs; |
| 190 | vs.set<Visualizer::scalingMode>(scalingMode); |
| 191 | mCommonTags.push_back({Visualizer::scalingMode, vs}); |
| 192 | } |
| 193 | |
| 194 | void addMeasurementModeParam(Visualizer::MeasurementMode measurementMode) { |
| 195 | Visualizer vs; |
| 196 | vs.set<Visualizer::measurementMode>(measurementMode); |
| 197 | mCommonTags.push_back({Visualizer::measurementMode, vs}); |
| 198 | } |
| 199 | |
| 200 | void addLatencyParam(int latency) { |
| 201 | Visualizer vs; |
| 202 | Visualizer::SetOnlyParameters setOnlyParam; |
| 203 | setOnlyParam.set<Visualizer::SetOnlyParameters::latencyMs>(latency); |
| 204 | vs.set<Visualizer::setOnlyParameters>(setOnlyParam); |
| 205 | mSetOnlyParamTags.push_back({Visualizer::SetOnlyParameters::latencyMs, vs}); |
| 206 | } |
| 207 | |
| 208 | void addMeasurementTag() { |
| 209 | mGetOnlyParamTags.push_back(Visualizer::GetOnlyParameters::measurement); |
| 210 | } |
| 211 | |
| 212 | void addCaptureBytesTag() { |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 213 | mGetOnlyParamTags.push_back(Visualizer::GetOnlyParameters::captureSampleBuffer); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | bool isTagInRange(const Visualizer::Tag& tag, const Visualizer& vs, |
| 217 | const Descriptor& desc) const { |
| 218 | const Visualizer::Capability& vsCap = desc.capability.get<Capability::visualizer>(); |
| 219 | switch (tag) { |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 220 | case Visualizer::captureSamples: { |
| 221 | int captureSize = vs.get<Visualizer::captureSamples>(); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 222 | return isCaptureSizeInRange(vsCap, captureSize); |
| 223 | } |
| 224 | case Visualizer::scalingMode: |
| 225 | case Visualizer::measurementMode: |
| 226 | return true; |
| 227 | default: |
| 228 | return false; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | bool isSetOnlyParamTagInRange(Visualizer::SetOnlyParameters::Tag, const Visualizer& vs, |
| 233 | const Descriptor& desc) const { |
| 234 | const Visualizer::Capability& vsCap = desc.capability.get<Capability::visualizer>(); |
| 235 | if (vs.getTag() != Visualizer::setOnlyParameters) return false; |
| 236 | Visualizer::SetOnlyParameters setOnlyParam = vs.get<Visualizer::setOnlyParameters>(); |
| 237 | if (setOnlyParam.getTag() != Visualizer::SetOnlyParameters::latencyMs) return false; |
| 238 | int latency = setOnlyParam.get<Visualizer::SetOnlyParameters::latencyMs>(); |
| 239 | return isLatencyInRange(vsCap, latency); |
| 240 | } |
| 241 | |
| 242 | bool isCaptureSizeInRange(const Visualizer::Capability& cap, int captureSize) const { |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 243 | return (captureSize >= cap.captureSampleRange.min && |
| 244 | captureSize <= cap.captureSampleRange.max); |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | bool isLatencyInRange(const Visualizer::Capability& cap, int latency) const { |
| 248 | return (latency >= 0 && latency <= cap.maxLatencyMs); |
| 249 | } |
| 250 | |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 251 | static std::unordered_set<int> getCaptureSizeValues() { |
| 252 | int minCaptureSize = std::numeric_limits<int>::max(); |
| 253 | int maxCaptureSize = std::numeric_limits<int>::min(); |
| 254 | for (const auto& it : kFactoryDescList) { |
| 255 | maxCaptureSize = std::max( |
| 256 | it.second.capability.get<Capability::visualizer>().captureSampleRange.max, |
| 257 | maxCaptureSize); |
| 258 | minCaptureSize = std::min( |
| 259 | it.second.capability.get<Capability ::visualizer>().captureSampleRange.min, |
| 260 | minCaptureSize); |
| 261 | } |
| 262 | return {std::numeric_limits<int>::min(), minCaptureSize - 1, minCaptureSize, |
| 263 | (minCaptureSize + maxCaptureSize) >> 1, maxCaptureSize, maxCaptureSize + 1, |
| 264 | std::numeric_limits<int>::max()}; |
| 265 | } |
| 266 | |
| 267 | static std::unordered_set<int> getLatencyValues() { |
| 268 | const auto max = std::max_element( |
| 269 | kFactoryDescList.begin(), kFactoryDescList.end(), |
| 270 | [](const std::pair<std::shared_ptr<IFactory>, Descriptor>& a, |
| 271 | const std::pair<std::shared_ptr<IFactory>, Descriptor>& b) { |
| 272 | return a.second.capability.get<Capability::visualizer>().maxLatencyMs < |
| 273 | b.second.capability.get<Capability::visualizer>().maxLatencyMs; |
| 274 | }); |
| 275 | if (max == kFactoryDescList.end()) { |
| 276 | return {0}; |
| 277 | } |
| 278 | int maxDelay = max->second.capability.get<Capability::visualizer>().maxLatencyMs; |
| 279 | return {-1, 0, maxDelay >> 1, maxDelay - 1, maxDelay, maxDelay + 1}; |
| 280 | } |
| 281 | |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 282 | private: |
| 283 | std::vector<std::pair<Visualizer::Tag, Visualizer>> mCommonTags; |
| 284 | std::vector<std::pair<Visualizer::SetOnlyParameters::Tag, Visualizer>> mSetOnlyParamTags; |
| 285 | std::vector<Visualizer::GetOnlyParameters::Tag> mGetOnlyParamTags; |
| 286 | void CleanUp() { |
| 287 | mCommonTags.clear(); |
| 288 | mSetOnlyParamTags.clear(); |
| 289 | mGetOnlyParamTags.clear(); |
| 290 | } |
| 291 | }; |
| 292 | |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 293 | const std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> |
| 294 | VisualizerParamTest::kFactoryDescList = EffectFactoryHelper::getAllEffectDescriptors( |
| 295 | IFactory::descriptor, kVisualizerTypeUUID); |
| 296 | const std::unordered_set<int> VisualizerParamTest::kCaptureSizeValues = |
| 297 | VisualizerParamTest::getCaptureSizeValues(); |
| 298 | const std::unordered_set<Visualizer::MeasurementMode> VisualizerParamTest::kMeasurementModeValues( |
| 299 | ndk::enum_range<Visualizer::MeasurementMode>().begin(), |
| 300 | ndk::enum_range<Visualizer::MeasurementMode>().end()); |
| 301 | const std::unordered_set<Visualizer::ScalingMode> VisualizerParamTest::kScalingModeValues( |
| 302 | ndk::enum_range<Visualizer::ScalingMode>().begin(), |
| 303 | ndk::enum_range<Visualizer::ScalingMode>().end()); |
| 304 | const std::unordered_set<int> VisualizerParamTest::kLatencyValues = |
| 305 | VisualizerParamTest::getLatencyValues(); |
| 306 | |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 307 | TEST_P(VisualizerParamTest, SetAndGetCaptureSize) { |
| 308 | EXPECT_NO_FATAL_FAILURE(addCaptureSizeParam(mCaptureSize)); |
| 309 | SetAndGetCommonParameters(); |
| 310 | } |
| 311 | |
| 312 | TEST_P(VisualizerParamTest, SetAndGetScalingMode) { |
| 313 | EXPECT_NO_FATAL_FAILURE(addScalingModeParam(mScalingMode)); |
| 314 | SetAndGetCommonParameters(); |
| 315 | } |
| 316 | |
| 317 | TEST_P(VisualizerParamTest, SetAndGetMeasurementMode) { |
| 318 | EXPECT_NO_FATAL_FAILURE(addMeasurementModeParam(mMeasurementMode)); |
| 319 | SetAndGetCommonParameters(); |
| 320 | } |
| 321 | |
| 322 | TEST_P(VisualizerParamTest, SetAndGetLatency) { |
| 323 | EXPECT_NO_FATAL_FAILURE(addLatencyParam(mLatency)); |
| 324 | SetAndGetSetOnlyParameters(); |
| 325 | } |
| 326 | |
| 327 | TEST_P(VisualizerParamTest, GetAndSetMeasurement) { |
| 328 | EXPECT_NO_FATAL_FAILURE(addMeasurementTag()); |
| 329 | GetandSetGetOnlyParameters(); |
| 330 | } |
| 331 | |
| 332 | TEST_P(VisualizerParamTest, GetAndSetCaptureBytes) { |
| 333 | EXPECT_NO_FATAL_FAILURE(addCaptureBytesTag()); |
| 334 | GetandSetGetOnlyParameters(); |
| 335 | } |
| 336 | |
| 337 | INSTANTIATE_TEST_SUITE_P( |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 338 | VisualizerParamTest, VisualizerParamTest, |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 339 | ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors( |
| 340 | IFactory::descriptor, kVisualizerTypeUUID)), |
Shunkai Yao | e39cd36 | 2022-12-22 00:23:34 +0000 | [diff] [blame] | 341 | testing::ValuesIn(VisualizerParamTest::kCaptureSizeValues), |
| 342 | testing::ValuesIn(VisualizerParamTest::kScalingModeValues), |
| 343 | testing::ValuesIn(VisualizerParamTest::kMeasurementModeValues), |
| 344 | testing::ValuesIn(VisualizerParamTest::kLatencyValues)), |
Sham Rathod | 94aae5e | 2022-11-23 12:22:32 +0530 | [diff] [blame] | 345 | [](const testing::TestParamInfo<VisualizerParamTest::ParamType>& info) { |
| 346 | auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second; |
| 347 | std::string captureSize = std::to_string(std::get<PARAM_CAPTURE_SIZE>(info.param)); |
| 348 | std::string scalingMode = |
| 349 | std::to_string(static_cast<int>(std::get<PARAM_SCALING_MODE>(info.param))); |
| 350 | std::string measurementMode = |
| 351 | std::to_string(static_cast<int>(std::get<PARAM_MEASUREMENT_MODE>(info.param))); |
| 352 | std::string latency = std::to_string(std::get<PARAM_LATENCY>(info.param)); |
| 353 | |
| 354 | std::string name = "Implementor_" + descriptor.common.implementor + "_name_" + |
| 355 | descriptor.common.name + "_UUID_" + |
| 356 | descriptor.common.id.uuid.toString() + "_captureSize" + captureSize + |
| 357 | "_scalingMode" + scalingMode + "_measurementMode" + measurementMode + |
| 358 | "_latency" + latency; |
| 359 | std::replace_if( |
| 360 | name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_'); |
| 361 | return name; |
| 362 | }); |
| 363 | |
| 364 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VisualizerParamTest); |
| 365 | |
| 366 | int main(int argc, char** argv) { |
| 367 | ::testing::InitGoogleTest(&argc, argv); |
| 368 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 369 | ABinderProcess_startThreadPool(); |
| 370 | return RUN_ALL_TESTS(); |
| 371 | } |