1 /* 2 * Copyright (C) 2017 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 "AudioEffectHidlHalTest" 18 #include <android-base/logging.h> 19 #include <system/audio.h> 20 21 #include <android/hardware/audio/effect/2.0/IEffect.h> 22 #include <android/hardware/audio/effect/2.0/IEffectsFactory.h> 23 #include <android/hardware/audio/effect/2.0/IEqualizerEffect.h> 24 #include <android/hardware/audio/effect/2.0/ILoudnessEnhancerEffect.h> 25 #include <android/hardware/audio/effect/2.0/types.h> 26 #include <android/hidl/allocator/1.0/IAllocator.h> 27 #include <android/hidl/memory/1.0/IMemory.h> 28 29 #include <VtsHalHidlTargetTestBase.h> 30 31 using android::hardware::audio::common::V2_0::AudioDevice; 32 using android::hardware::audio::common::V2_0::AudioHandleConsts; 33 using android::hardware::audio::common::V2_0::AudioMode; 34 using android::hardware::audio::common::V2_0::AudioSource; 35 using android::hardware::audio::common::V2_0::Uuid; 36 using android::hardware::audio::effect::V2_0::AudioBuffer; 37 using android::hardware::audio::effect::V2_0::EffectAuxChannelsConfig; 38 using android::hardware::audio::effect::V2_0::EffectBufferConfig; 39 using android::hardware::audio::effect::V2_0::EffectConfig; 40 using android::hardware::audio::effect::V2_0::EffectDescriptor; 41 using android::hardware::audio::effect::V2_0::EffectOffloadParameter; 42 using android::hardware::audio::effect::V2_0::IEffect; 43 using android::hardware::audio::effect::V2_0::IEffectsFactory; 44 using android::hardware::audio::effect::V2_0::IEqualizerEffect; 45 using android::hardware::audio::effect::V2_0::ILoudnessEnhancerEffect; 46 using android::hardware::audio::effect::V2_0::Result; 47 using android::hardware::MQDescriptorSync; 48 using android::hardware::Return; 49 using android::hardware::Void; 50 using android::hardware::hidl_handle; 51 using android::hardware::hidl_memory; 52 using android::hardware::hidl_string; 53 using android::hardware::hidl_vec; 54 using android::hidl::allocator::V1_0::IAllocator; 55 using android::hidl::memory::V1_0::IMemory; 56 using android::sp; 57 58 #ifndef ARRAY_SIZE 59 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) 60 #endif 61 62 // The main test class for Audio Effects Factory HIDL HAL. 63 class AudioEffectsFactoryHidlTest : public ::testing::VtsHalHidlTargetTestBase { 64 public: 65 void SetUp() override { 66 effectsFactory = 67 ::testing::VtsHalHidlTargetTestBase::getService<IEffectsFactory>(); 68 ASSERT_NE(effectsFactory, nullptr); 69 } 70 71 void TearDown() override { effectsFactory.clear(); } 72 73 protected: 74 static void description(const std::string& description) { 75 RecordProperty("description", description); 76 } 77 78 sp<IEffectsFactory> effectsFactory; 79 }; 80 81 TEST_F(AudioEffectsFactoryHidlTest, EnumerateEffects) { 82 description("Verify that EnumerateEffects returns at least one effect"); 83 Result retval = Result::NOT_INITIALIZED; 84 size_t effectCount = 0; 85 Return<void> ret = effectsFactory->getAllDescriptors( 86 [&](Result r, const hidl_vec<EffectDescriptor>& result) { 87 retval = r; 88 effectCount = result.size(); 89 }); 90 EXPECT_TRUE(ret.isOk()); 91 EXPECT_EQ(Result::OK, retval); 92 EXPECT_GT(effectCount, 0u); 93 } 94 95 TEST_F(AudioEffectsFactoryHidlTest, CreateEffect) { 96 description("Verify that an effect can be created via CreateEffect"); 97 bool gotEffect = false; 98 Uuid effectUuid; 99 Return<void> ret = effectsFactory->getAllDescriptors( 100 [&](Result r, const hidl_vec<EffectDescriptor>& result) { 101 if (r == Result::OK && result.size() > 0) { 102 gotEffect = true; 103 effectUuid = result[0].uuid; 104 } 105 }); 106 ASSERT_TRUE(ret.isOk()); 107 ASSERT_TRUE(gotEffect); 108 Result retval = Result::NOT_INITIALIZED; 109 sp<IEffect> effect; 110 ret = effectsFactory->createEffect( 111 effectUuid, 1 /*session*/, 1 /*ioHandle*/, 112 [&](Result r, const sp<IEffect>& result, uint64_t /*effectId*/) { 113 retval = r; 114 if (r == Result::OK) { 115 effect = result; 116 } 117 }); 118 EXPECT_TRUE(ret.isOk()); 119 EXPECT_EQ(Result::OK, retval); 120 EXPECT_NE(nullptr, effect.get()); 121 } 122 123 TEST_F(AudioEffectsFactoryHidlTest, GetDescriptor) { 124 description( 125 "Verify that effects factory can provide an effect descriptor via " 126 "GetDescriptor"); 127 hidl_vec<EffectDescriptor> allDescriptors; 128 Return<void> ret = effectsFactory->getAllDescriptors( 129 [&](Result r, const hidl_vec<EffectDescriptor>& result) { 130 if (r == Result::OK) { 131 allDescriptors = result; 132 } 133 }); 134 ASSERT_TRUE(ret.isOk()); 135 ASSERT_GT(allDescriptors.size(), 0u); 136 for (size_t i = 0; i < allDescriptors.size(); ++i) { 137 ret = effectsFactory->getDescriptor( 138 allDescriptors[i].uuid, [&](Result r, const EffectDescriptor& result) { 139 EXPECT_EQ(r, Result::OK); 140 EXPECT_EQ(result, allDescriptors[i]); 141 }); 142 } 143 EXPECT_TRUE(ret.isOk()); 144 } 145 146 TEST_F(AudioEffectsFactoryHidlTest, DebugDumpInvalidArgument) { 147 description("Verify that debugDump doesn't crash on invalid arguments"); 148 Return<void> ret = effectsFactory->debugDump(hidl_handle()); 149 ASSERT_TRUE(ret.isOk()); 150 } 151 152 // Equalizer effect is required by CDD, but only the type is fixed. 153 // This is the same UUID as AudioEffect.EFFECT_TYPE_EQUALIZER in Java. 154 static const Uuid EQUALIZER_EFFECT_TYPE = { 155 0x0bed4300, 0xddd6, 0x11db, 0x8f34, 156 std::array<uint8_t, 6>{{0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}}; 157 // Loudness Enhancer effect is required by CDD, but only the type is fixed. 158 // This is the same UUID as AudioEffect.EFFECT_TYPE_LOUDNESS_ENHANCER in Java. 159 static const Uuid LOUDNESS_ENHANCER_EFFECT_TYPE = { 160 0xfe3199be, 0xaed0, 0x413f, 0x87bb, 161 std::array<uint8_t, 6>{{0x11, 0x26, 0x0e, 0xb6, 0x3c, 0xf1}}}; 162 163 // The main test class for Audio Effect HIDL HAL. 164 class AudioEffectHidlTest : public ::testing::VtsHalHidlTargetTestBase { 165 public: 166 void SetUp() override { 167 effectsFactory = 168 ::testing::VtsHalHidlTargetTestBase::getService<IEffectsFactory>(); 169 ASSERT_NE(nullptr, effectsFactory.get()); 170 171 findAndCreateEffect(getEffectType()); 172 ASSERT_NE(nullptr, effect.get()); 173 174 Return<Result> ret = effect->init(); 175 ASSERT_TRUE(ret.isOk()); 176 ASSERT_EQ(Result::OK, ret); 177 } 178 179 void TearDown() override { 180 effect.clear(); 181 effectsFactory.clear(); 182 } 183 184 protected: 185 static void description(const std::string& description) { 186 RecordProperty("description", description); 187 } 188 189 virtual Uuid getEffectType() { return EQUALIZER_EFFECT_TYPE; } 190 191 void findAndCreateEffect(const Uuid& type); 192 void findEffectInstance(const Uuid& type, Uuid* uuid); 193 void getChannelCount(uint32_t* channelCount); 194 195 sp<IEffectsFactory> effectsFactory; 196 sp<IEffect> effect; 197 }; 198 199 void AudioEffectHidlTest::findAndCreateEffect(const Uuid& type) { 200 Uuid effectUuid; 201 findEffectInstance(type, &effectUuid); 202 Return<void> ret = effectsFactory->createEffect( 203 effectUuid, 1 /*session*/, 1 /*ioHandle*/, 204 [&](Result r, const sp<IEffect>& result, uint64_t /*effectId*/) { 205 if (r == Result::OK) { 206 effect = result; 207 } 208 }); 209 ASSERT_TRUE(ret.isOk()); 210 } 211 212 void AudioEffectHidlTest::findEffectInstance(const Uuid& type, Uuid* uuid) { 213 bool effectFound = false; 214 Return<void> ret = effectsFactory->getAllDescriptors( 215 [&](Result r, const hidl_vec<EffectDescriptor>& result) { 216 if (r == Result::OK) { 217 for (const auto& desc : result) { 218 if (desc.type == type) { 219 effectFound = true; 220 *uuid = desc.uuid; 221 break; 222 } 223 } 224 } 225 }); 226 ASSERT_TRUE(ret.isOk()); 227 ASSERT_TRUE(effectFound); 228 } 229 230 void AudioEffectHidlTest::getChannelCount(uint32_t* channelCount) { 231 Result retval; 232 EffectConfig currentConfig; 233 Return<void> ret = effect->getConfig([&](Result r, const EffectConfig& conf) { 234 retval = r; 235 if (r == Result::OK) { 236 currentConfig = conf; 237 } 238 }); 239 ASSERT_TRUE(ret.isOk()); 240 ASSERT_EQ(Result::OK, retval); 241 ASSERT_TRUE(audio_channel_mask_is_valid( 242 static_cast<audio_channel_mask_t>(currentConfig.outputCfg.channels))); 243 *channelCount = audio_channel_count_from_out_mask( 244 static_cast<audio_channel_mask_t>(currentConfig.outputCfg.channels)); 245 } 246 247 TEST_F(AudioEffectHidlTest, Close) { 248 description("Verify that an effect can be closed"); 249 Return<Result> ret = effect->close(); 250 EXPECT_TRUE(ret.isOk()); 251 EXPECT_EQ(Result::OK, ret); 252 } 253 254 TEST_F(AudioEffectHidlTest, GetDescriptor) { 255 description( 256 "Verify that an effect can return its own descriptor via GetDescriptor"); 257 Result retval = Result::NOT_INITIALIZED; 258 Uuid actualType; 259 Return<void> ret = 260 effect->getDescriptor([&](Result r, const EffectDescriptor& desc) { 261 retval = r; 262 if (r == Result::OK) { 263 actualType = desc.type; 264 } 265 }); 266 EXPECT_TRUE(ret.isOk()); 267 EXPECT_EQ(Result::OK, retval); 268 EXPECT_EQ(getEffectType(), actualType); 269 } 270 271 TEST_F(AudioEffectHidlTest, GetSetConfig) { 272 description( 273 "Verify that it is possible to manipulate effect config via Get / " 274 "SetConfig"); 275 Result retval = Result::NOT_INITIALIZED; 276 EffectConfig currentConfig; 277 Return<void> ret = effect->getConfig([&](Result r, const EffectConfig& conf) { 278 retval = r; 279 if (r == Result::OK) { 280 currentConfig = conf; 281 } 282 }); 283 EXPECT_TRUE(ret.isOk()); 284 EXPECT_EQ(Result::OK, retval); 285 Return<Result> ret2 = effect->setConfig(currentConfig, nullptr, nullptr); 286 EXPECT_TRUE(ret2.isOk()); 287 EXPECT_EQ(Result::OK, ret2); 288 } 289 290 TEST_F(AudioEffectHidlTest, GetConfigReverse) { 291 description("Verify that GetConfigReverse does not crash"); 292 Return<void> ret = 293 effect->getConfigReverse([&](Result, const EffectConfig&) {}); 294 EXPECT_TRUE(ret.isOk()); 295 } 296 297 TEST_F(AudioEffectHidlTest, GetSupportedAuxChannelsConfigs) { 298 description("Verify that GetSupportedAuxChannelsConfigs does not crash"); 299 Return<void> ret = effect->getSupportedAuxChannelsConfigs( 300 0, [&](Result, const hidl_vec<EffectAuxChannelsConfig>&) {}); 301 EXPECT_TRUE(ret.isOk()); 302 } 303 304 TEST_F(AudioEffectHidlTest, GetAuxChannelsConfig) { 305 description("Verify that GetAuxChannelsConfig does not crash"); 306 Return<void> ret = effect->getAuxChannelsConfig( 307 [&](Result, const EffectAuxChannelsConfig&) {}); 308 EXPECT_TRUE(ret.isOk()); 309 } 310 311 TEST_F(AudioEffectHidlTest, SetAuxChannelsConfig) { 312 description("Verify that SetAuxChannelsConfig does not crash"); 313 Return<Result> ret = effect->setAuxChannelsConfig(EffectAuxChannelsConfig()); 314 EXPECT_TRUE(ret.isOk()); 315 } 316 317 // Not generated automatically because AudioBuffer contains 318 // instances of hidl_memory which can't be compared properly 319 // in general case due to presence of handles. 320 // 321 // However, in this particular case, handles must not present 322 // thus comparison is possible. 323 // 324 // operator== must be defined in the same namespace as the structures. 325 namespace android { 326 namespace hardware { 327 namespace audio { 328 namespace effect { 329 namespace V2_0 { 330 inline bool operator==(const AudioBuffer& lhs, const AudioBuffer& rhs) { 331 return lhs.id == rhs.id && lhs.frameCount == rhs.frameCount && 332 lhs.data.handle() == nullptr && rhs.data.handle() == nullptr; 333 } 334 335 inline bool operator==(const EffectBufferConfig& lhs, 336 const EffectBufferConfig& rhs) { 337 return lhs.buffer == rhs.buffer && lhs.samplingRateHz == rhs.samplingRateHz && 338 lhs.channels == rhs.channels && lhs.format == rhs.format && 339 lhs.accessMode == rhs.accessMode && lhs.mask == rhs.mask; 340 } 341 342 inline bool operator==(const EffectConfig& lhs, const EffectConfig& rhs) { 343 return lhs.inputCfg == rhs.inputCfg && lhs.outputCfg == rhs.outputCfg; 344 } 345 } // namespace V2_0 346 } // namespace effect 347 } // namespace audio 348 } // namespace hardware 349 } // namespace android 350 351 TEST_F(AudioEffectHidlTest, Reset) { 352 description("Verify that Reset preserves effect configuration"); 353 Result retval = Result::NOT_INITIALIZED; 354 EffectConfig originalConfig; 355 Return<void> ret = effect->getConfig([&](Result r, const EffectConfig& conf) { 356 retval = r; 357 if (r == Result::OK) { 358 originalConfig = conf; 359 } 360 }); 361 ASSERT_TRUE(ret.isOk()); 362 ASSERT_EQ(Result::OK, retval); 363 Return<Result> ret2 = effect->reset(); 364 EXPECT_TRUE(ret2.isOk()); 365 EXPECT_EQ(Result::OK, ret2); 366 EffectConfig configAfterReset; 367 ret = effect->getConfig([&](Result r, const EffectConfig& conf) { 368 retval = r; 369 if (r == Result::OK) { 370 configAfterReset = conf; 371 } 372 }); 373 EXPECT_EQ(originalConfig, configAfterReset); 374 } 375 376 TEST_F(AudioEffectHidlTest, DisableEnableDisable) { 377 description("Verify Disable -> Enable -> Disable sequence for an effect"); 378 Return<Result> ret = effect->disable(); 379 EXPECT_TRUE(ret.isOk()); 380 EXPECT_EQ(Result::INVALID_ARGUMENTS, ret); 381 ret = effect->enable(); 382 EXPECT_TRUE(ret.isOk()); 383 EXPECT_EQ(Result::OK, ret); 384 ret = effect->disable(); 385 EXPECT_TRUE(ret.isOk()); 386 EXPECT_EQ(Result::OK, ret); 387 } 388 389 TEST_F(AudioEffectHidlTest, SetDevice) { 390 description("Verify that SetDevice works for an output chain effect"); 391 Return<Result> ret = effect->setDevice(AudioDevice::OUT_SPEAKER); 392 EXPECT_TRUE(ret.isOk()); 393 EXPECT_EQ(Result::OK, ret); 394 } 395 396 TEST_F(AudioEffectHidlTest, SetAndGetVolume) { 397 description("Verify that SetAndGetVolume method works for an effect"); 398 uint32_t channelCount; 399 getChannelCount(&channelCount); 400 hidl_vec<uint32_t> volumes; 401 volumes.resize(channelCount); 402 for (uint32_t i = 0; i < channelCount; ++i) { 403 volumes[i] = 0; 404 } 405 Result retval = Result::NOT_INITIALIZED; 406 Return<void> ret = effect->setAndGetVolume( 407 volumes, [&](Result r, const hidl_vec<uint32_t>&) { retval = r; }); 408 EXPECT_TRUE(ret.isOk()); 409 EXPECT_EQ(Result::OK, retval); 410 } 411 412 TEST_F(AudioEffectHidlTest, VolumeChangeNotification) { 413 description("Verify that effect accepts VolumeChangeNotification"); 414 uint32_t channelCount; 415 getChannelCount(&channelCount); 416 hidl_vec<uint32_t> volumes; 417 volumes.resize(channelCount); 418 for (uint32_t i = 0; i < channelCount; ++i) { 419 volumes[i] = 0; 420 } 421 Return<Result> ret = effect->volumeChangeNotification(volumes); 422 EXPECT_TRUE(ret.isOk()); 423 EXPECT_EQ(Result::OK, ret); 424 } 425 426 TEST_F(AudioEffectHidlTest, SetAudioMode) { 427 description("Verify that SetAudioMode works for an effect"); 428 Return<Result> ret = effect->setAudioMode(AudioMode::NORMAL); 429 EXPECT_TRUE(ret.isOk()); 430 EXPECT_EQ(Result::OK, ret); 431 } 432 433 TEST_F(AudioEffectHidlTest, SetConfigReverse) { 434 description("Verify that SetConfigReverse does not crash"); 435 Return<Result> ret = 436 effect->setConfigReverse(EffectConfig(), nullptr, nullptr); 437 EXPECT_TRUE(ret.isOk()); 438 } 439 440 TEST_F(AudioEffectHidlTest, SetInputDevice) { 441 description("Verify that SetInputDevice does not crash"); 442 Return<Result> ret = effect->setInputDevice(AudioDevice::IN_BUILTIN_MIC); 443 EXPECT_TRUE(ret.isOk()); 444 } 445 446 TEST_F(AudioEffectHidlTest, SetAudioSource) { 447 description("Verify that SetAudioSource does not crash"); 448 Return<Result> ret = effect->setAudioSource(AudioSource::MIC); 449 EXPECT_TRUE(ret.isOk()); 450 } 451 452 TEST_F(AudioEffectHidlTest, Offload) { 453 description("Verify that calling Offload method does not crash"); 454 EffectOffloadParameter offloadParam; 455 offloadParam.isOffload = false; 456 offloadParam.ioHandle = 457 static_cast<int>(AudioHandleConsts::AUDIO_IO_HANDLE_NONE); 458 Return<Result> ret = effect->offload(offloadParam); 459 EXPECT_TRUE(ret.isOk()); 460 } 461 462 TEST_F(AudioEffectHidlTest, PrepareForProcessing) { 463 description("Verify that PrepareForProcessing method works for an effect"); 464 Result retval = Result::NOT_INITIALIZED; 465 Return<void> ret = effect->prepareForProcessing( 466 [&](Result r, const MQDescriptorSync<Result>&) { retval = r; }); 467 EXPECT_TRUE(ret.isOk()); 468 EXPECT_EQ(Result::OK, retval); 469 } 470 471 TEST_F(AudioEffectHidlTest, SetProcessBuffers) { 472 description("Verify that SetProcessBuffers works for an effect"); 473 sp<IAllocator> ashmem = IAllocator::getService("ashmem"); 474 ASSERT_NE(nullptr, ashmem.get()); 475 bool success = false; 476 AudioBuffer buffer; 477 Return<void> ret = 478 ashmem->allocate(1024, [&](bool s, const hidl_memory& memory) { 479 success = s; 480 if (s) { 481 buffer.data = memory; 482 } 483 }); 484 ASSERT_TRUE(ret.isOk()); 485 ASSERT_TRUE(success); 486 Return<Result> ret2 = effect->setProcessBuffers(buffer, buffer); 487 EXPECT_TRUE(ret2.isOk()); 488 EXPECT_EQ(Result::OK, ret2); 489 } 490 491 TEST_F(AudioEffectHidlTest, Command) { 492 description("Verify that Command does not crash"); 493 Return<void> ret = effect->command(0, hidl_vec<uint8_t>(), 0, 494 [&](int32_t, const hidl_vec<uint8_t>&) {}); 495 EXPECT_TRUE(ret.isOk()); 496 } 497 498 TEST_F(AudioEffectHidlTest, SetParameter) { 499 description("Verify that SetParameter does not crash"); 500 Return<Result> ret = 501 effect->setParameter(hidl_vec<uint8_t>(), hidl_vec<uint8_t>()); 502 EXPECT_TRUE(ret.isOk()); 503 } 504 505 TEST_F(AudioEffectHidlTest, GetParameter) { 506 description("Verify that GetParameter does not crash"); 507 Return<void> ret = effect->getParameter( 508 hidl_vec<uint8_t>(), 0, [&](Result, const hidl_vec<uint8_t>&) {}); 509 EXPECT_TRUE(ret.isOk()); 510 } 511 512 TEST_F(AudioEffectHidlTest, GetSupportedConfigsForFeature) { 513 description("Verify that GetSupportedConfigsForFeature does not crash"); 514 Return<void> ret = effect->getSupportedConfigsForFeature( 515 0, 0, 0, [&](Result, uint32_t, const hidl_vec<uint8_t>&) {}); 516 EXPECT_TRUE(ret.isOk()); 517 } 518 519 TEST_F(AudioEffectHidlTest, GetCurrentConfigForFeature) { 520 description("Verify that GetCurrentConfigForFeature does not crash"); 521 Return<void> ret = effect->getCurrentConfigForFeature( 522 0, 0, [&](Result, const hidl_vec<uint8_t>&) {}); 523 EXPECT_TRUE(ret.isOk()); 524 } 525 526 TEST_F(AudioEffectHidlTest, SetCurrentConfigForFeature) { 527 description("Verify that SetCurrentConfigForFeature does not crash"); 528 Return<Result> ret = 529 effect->setCurrentConfigForFeature(0, hidl_vec<uint8_t>()); 530 EXPECT_TRUE(ret.isOk()); 531 } 532 533 534 // The main test class for Equalizer Audio Effect HIDL HAL. 535 class EqualizerAudioEffectHidlTest : public AudioEffectHidlTest { 536 public: 537 void SetUp() override { 538 AudioEffectHidlTest::SetUp(); 539 equalizer = IEqualizerEffect::castFrom(effect); 540 ASSERT_NE(nullptr, equalizer.get()); 541 } 542 543 protected: 544 Uuid getEffectType() override { return EQUALIZER_EFFECT_TYPE; } 545 void getNumBands(uint16_t* numBands); 546 void getLevelRange(int16_t* minLevel, int16_t* maxLevel); 547 void getBandFrequencyRange(uint16_t band, uint32_t* minFreq, 548 uint32_t* centerFreq, uint32_t* maxFreq); 549 void getPresetCount(size_t* count); 550 551 sp<IEqualizerEffect> equalizer; 552 }; 553 554 void EqualizerAudioEffectHidlTest::getNumBands(uint16_t* numBands) { 555 Result retval = Result::NOT_INITIALIZED; 556 Return<void> ret = equalizer->getNumBands([&](Result r, uint16_t b) { 557 retval = r; 558 if (retval == Result::OK) { 559 *numBands = b; 560 } 561 }); 562 ASSERT_TRUE(ret.isOk()); 563 ASSERT_EQ(Result::OK, retval); 564 } 565 566 void EqualizerAudioEffectHidlTest::getLevelRange(int16_t* minLevel, 567 int16_t* maxLevel) { 568 Result retval = Result::NOT_INITIALIZED; 569 Return<void> ret = 570 equalizer->getLevelRange([&](Result r, int16_t min, int16_t max) { 571 retval = r; 572 if (retval == Result::OK) { 573 *minLevel = min; 574 *maxLevel = max; 575 } 576 }); 577 ASSERT_TRUE(ret.isOk()); 578 ASSERT_EQ(Result::OK, retval); 579 } 580 581 void EqualizerAudioEffectHidlTest::getBandFrequencyRange(uint16_t band, 582 uint32_t* minFreq, 583 uint32_t* centerFreq, 584 uint32_t* maxFreq) { 585 Result retval = Result::NOT_INITIALIZED; 586 Return<void> ret = equalizer->getBandFrequencyRange( 587 band, [&](Result r, uint32_t min, uint32_t max) { 588 retval = r; 589 if (retval == Result::OK) { 590 *minFreq = min; 591 *maxFreq = max; 592 } 593 }); 594 ASSERT_TRUE(ret.isOk()); 595 ASSERT_EQ(Result::OK, retval); 596 ret = equalizer->getBandCenterFrequency(band, [&](Result r, uint32_t center) { 597 retval = r; 598 if (retval == Result::OK) { 599 *centerFreq = center; 600 } 601 }); 602 ASSERT_TRUE(ret.isOk()); 603 ASSERT_EQ(Result::OK, retval); 604 } 605 606 void EqualizerAudioEffectHidlTest::getPresetCount(size_t* count) { 607 Result retval = Result::NOT_INITIALIZED; 608 Return<void> ret = equalizer->getPresetNames( 609 [&](Result r, const hidl_vec<hidl_string>& names) { 610 retval = r; 611 if (retval == Result::OK) { 612 *count = names.size(); 613 } 614 }); 615 ASSERT_TRUE(ret.isOk()); 616 ASSERT_EQ(Result::OK, retval); 617 } 618 619 TEST_F(EqualizerAudioEffectHidlTest, GetNumBands) { 620 description("Verify that Equalizer effect reports at least one band"); 621 uint16_t numBands = 0; 622 getNumBands(&numBands); 623 EXPECT_GT(numBands, 0); 624 } 625 626 TEST_F(EqualizerAudioEffectHidlTest, GetLevelRange) { 627 description("Verify that Equalizer effect reports adequate band level range"); 628 int16_t minLevel = 0x7fff, maxLevel = 0; 629 getLevelRange(&minLevel, &maxLevel); 630 EXPECT_GT(maxLevel, minLevel); 631 } 632 633 TEST_F(EqualizerAudioEffectHidlTest, GetSetBandLevel) { 634 description( 635 "Verify that manipulating band levels works for Equalizer effect"); 636 uint16_t numBands = 0; 637 getNumBands(&numBands); 638 ASSERT_GT(numBands, 0); 639 int16_t levels[3]{0x7fff, 0, 0}; 640 getLevelRange(&levels[0], &levels[2]); 641 ASSERT_GT(levels[2], levels[0]); 642 levels[1] = (levels[2] + levels[0]) / 2; 643 for (uint16_t i = 0; i < numBands; ++i) { 644 for (size_t j = 0; j < ARRAY_SIZE(levels); ++j) { 645 Return<Result> ret = equalizer->setBandLevel(i, levels[j]); 646 EXPECT_TRUE(ret.isOk()); 647 EXPECT_EQ(Result::OK, ret); 648 Result retval = Result::NOT_INITIALIZED; 649 int16_t actualLevel; 650 Return<void> ret2 = equalizer->getBandLevel(i, [&](Result r, int16_t l) { 651 retval = r; 652 if (retval == Result::OK) { 653 actualLevel = l; 654 } 655 }); 656 EXPECT_TRUE(ret2.isOk()); 657 EXPECT_EQ(Result::OK, retval); 658 EXPECT_EQ(levels[j], actualLevel); 659 } 660 } 661 } 662 663 TEST_F(EqualizerAudioEffectHidlTest, GetBandCenterFrequencyAndRange) { 664 description( 665 "Verify that Equalizer effect reports adequate band frequency range"); 666 uint16_t numBands = 0; 667 getNumBands(&numBands); 668 ASSERT_GT(numBands, 0); 669 for (uint16_t i = 0; i < numBands; ++i) { 670 uint32_t minFreq = 0xffffffff, centerFreq = 0xffffffff, 671 maxFreq = 0xffffffff; 672 getBandFrequencyRange(i, &minFreq, ¢erFreq, &maxFreq); 673 // Note: NXP legacy implementation reports "1" as upper bound for last band, 674 // so this check fails. 675 EXPECT_GE(maxFreq, centerFreq); 676 EXPECT_GE(centerFreq, minFreq); 677 } 678 } 679 680 TEST_F(EqualizerAudioEffectHidlTest, GetBandForFrequency) { 681 description( 682 "Verify that Equalizer effect supports GetBandForFrequency correctly"); 683 uint16_t numBands = 0; 684 getNumBands(&numBands); 685 ASSERT_GT(numBands, 0); 686 for (uint16_t i = 0; i < numBands; ++i) { 687 uint32_t freqs[3]{0, 0, 0}; 688 getBandFrequencyRange(i, &freqs[0], &freqs[1], &freqs[2]); 689 // NXP legacy implementation reports "1" as upper bound for last band, some 690 // of the checks fail. 691 for (size_t j = 0; j < ARRAY_SIZE(freqs); ++j) { 692 if (j == 0) { 693 freqs[j]++; 694 } // Min frequency is an open interval. 695 Result retval = Result::NOT_INITIALIZED; 696 uint16_t actualBand = numBands + 1; 697 Return<void> ret = 698 equalizer->getBandForFrequency(freqs[j], [&](Result r, uint16_t b) { 699 retval = r; 700 if (retval == Result::OK) { 701 actualBand = b; 702 } 703 }); 704 EXPECT_TRUE(ret.isOk()); 705 EXPECT_EQ(Result::OK, retval); 706 EXPECT_EQ(i, actualBand) << "Frequency: " << freqs[j]; 707 } 708 } 709 } 710 711 TEST_F(EqualizerAudioEffectHidlTest, GetPresetNames) { 712 description("Verify that Equalizer effect reports at least one preset"); 713 size_t presetCount; 714 getPresetCount(&presetCount); 715 EXPECT_GT(presetCount, 0u); 716 } 717 718 TEST_F(EqualizerAudioEffectHidlTest, GetSetCurrentPreset) { 719 description( 720 "Verify that manipulating the current preset for Equalizer effect"); 721 size_t presetCount; 722 getPresetCount(&presetCount); 723 ASSERT_GT(presetCount, 0u); 724 for (uint16_t i = 0; i < presetCount; ++i) { 725 Return<Result> ret = equalizer->setCurrentPreset(i); 726 EXPECT_TRUE(ret.isOk()); 727 EXPECT_EQ(Result::OK, ret); 728 Result retval = Result::NOT_INITIALIZED; 729 uint16_t actualPreset = 0xffff; 730 Return<void> ret2 = equalizer->getCurrentPreset([&](Result r, uint16_t p) { 731 retval = r; 732 if (retval == Result::OK) { 733 actualPreset = p; 734 } 735 }); 736 EXPECT_TRUE(ret2.isOk()); 737 EXPECT_EQ(Result::OK, retval); 738 EXPECT_EQ(i, actualPreset); 739 } 740 } 741 742 TEST_F(EqualizerAudioEffectHidlTest, GetSetAllProperties) { 743 description( 744 "Verify that setting band levels and presets works via Get / " 745 "SetAllProperties for Equalizer effect"); 746 using AllProperties = 747 android::hardware::audio::effect::V2_0::IEqualizerEffect::AllProperties; 748 uint16_t numBands = 0; 749 getNumBands(&numBands); 750 ASSERT_GT(numBands, 0); 751 AllProperties props; 752 props.bandLevels.resize(numBands); 753 for (size_t i = 0; i < numBands; ++i) { 754 props.bandLevels[i] = 0; 755 } 756 757 AllProperties actualProps; 758 Result retval = Result::NOT_INITIALIZED; 759 760 // Verify setting of the band levels via properties. 761 props.curPreset = -1; 762 Return<Result> ret = equalizer->setAllProperties(props); 763 EXPECT_TRUE(ret.isOk()); 764 EXPECT_EQ(Result::OK, ret); 765 Return<void> ret2 = 766 equalizer->getAllProperties([&](Result r, AllProperties p) { 767 retval = r; 768 if (retval == Result::OK) { 769 actualProps = p; 770 } 771 }); 772 EXPECT_TRUE(ret2.isOk()); 773 EXPECT_EQ(Result::OK, retval); 774 EXPECT_EQ(props.bandLevels, actualProps.bandLevels); 775 776 // Verify setting of the current preset via properties. 777 props.curPreset = 0; // Assuming there is at least one preset. 778 ret = equalizer->setAllProperties(props); 779 EXPECT_TRUE(ret.isOk()); 780 EXPECT_EQ(Result::OK, ret); 781 ret2 = equalizer->getAllProperties([&](Result r, AllProperties p) { 782 retval = r; 783 if (retval == Result::OK) { 784 actualProps = p; 785 } 786 }); 787 EXPECT_TRUE(ret2.isOk()); 788 EXPECT_EQ(Result::OK, retval); 789 EXPECT_EQ(props.curPreset, actualProps.curPreset); 790 } 791 792 // The main test class for Equalizer Audio Effect HIDL HAL. 793 class LoudnessEnhancerAudioEffectHidlTest : public AudioEffectHidlTest { 794 public: 795 void SetUp() override { 796 AudioEffectHidlTest::SetUp(); 797 enhancer = ILoudnessEnhancerEffect::castFrom(effect); 798 ASSERT_NE(nullptr, enhancer.get()); 799 } 800 801 protected: 802 Uuid getEffectType() override { return LOUDNESS_ENHANCER_EFFECT_TYPE; } 803 804 sp<ILoudnessEnhancerEffect> enhancer; 805 }; 806 807 TEST_F(LoudnessEnhancerAudioEffectHidlTest, GetSetTargetGain) { 808 description( 809 "Verify that manipulating the target gain works for Loudness Enhancer " 810 "effect"); 811 const int32_t gain = 100; 812 Return<Result> ret = enhancer->setTargetGain(gain); 813 EXPECT_TRUE(ret.isOk()); 814 EXPECT_EQ(Result::OK, ret); 815 int32_t actualGain = 0; 816 Result retval; 817 Return<void> ret2 = enhancer->getTargetGain([&](Result r, int32_t g) { 818 retval = r; 819 if (retval == Result::OK) { 820 actualGain = g; 821 } 822 }); 823 EXPECT_TRUE(ret2.isOk()); 824 EXPECT_EQ(Result::OK, retval); 825 EXPECT_EQ(gain, actualGain); 826 } 827