Shunkai Yao | 21f6ac6 | 2024-02-08 01:41:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2024 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 <cstddef> |
| 18 | #include <unordered_map> |
| 19 | #define LOG_TAG "EffectHalVersionCompatibilityTest" |
| 20 | |
| 21 | #include <EffectHalAidl.h> |
| 22 | #include <aidl/android/hardware/audio/effect/IEffect.h> |
| 23 | #include <aidl/android/hardware/audio/effect/IFactory.h> |
| 24 | #include <android-base/logging.h> |
| 25 | #include <android/binder_manager.h> |
| 26 | #include <android/binder_process.h> |
| 27 | #include <gmock/gmock.h> |
| 28 | #include <gtest/gtest.h> |
| 29 | #include <media/audiohal/EffectsFactoryHalInterface.h> |
| 30 | #include <system/audio_aidl_utils.h> |
| 31 | #include <system/audio_config.h> |
| 32 | #include <system/audio_effects/audio_effects_utils.h> |
| 33 | #include <system/audio_effects/effect_uuid.h> |
| 34 | #include <utils/Log.h> |
| 35 | |
| 36 | using aidl::android::hardware::audio::effect::CommandId; |
| 37 | using aidl::android::hardware::audio::effect::Descriptor; |
| 38 | using aidl::android::hardware::audio::effect::IEffect; |
| 39 | using aidl::android::hardware::audio::effect::IFactory; |
| 40 | using aidl::android::hardware::audio::effect::kReopenSupportedVersion; |
| 41 | using aidl::android::hardware::audio::effect::Parameter; |
| 42 | using aidl::android::hardware::audio::effect::Processing; |
| 43 | using aidl::android::hardware::audio::effect::State; |
| 44 | using aidl::android::media::audio::common::AudioUuid; |
| 45 | using android::OK; |
| 46 | using android::sp; |
| 47 | using android::effect::EffectHalAidl; |
| 48 | using testing::_; |
| 49 | using testing::Eq; |
| 50 | |
| 51 | namespace { |
| 52 | |
| 53 | /** |
| 54 | * Maps of parameter and the version it was introduced. |
| 55 | */ |
| 56 | // parameters defined directly in the Parameter union, except Parameter::specific (defined in |
| 57 | // kParamIdEffectVersionMap). |
| 58 | static const std::unordered_map<Parameter::Tag, int /* version */> kParamTagVersionMap = { |
| 59 | {Parameter::common, 1}, {Parameter::deviceDescription, 1}, |
| 60 | {Parameter::mode, 1}, {Parameter::source, 1}, |
| 61 | {Parameter::offload, 1}, {Parameter::volumeStereo, 1}, |
| 62 | {Parameter::sourceMetadata, 2}, {Parameter::sinkMetadata, 2}, |
| 63 | }; |
| 64 | |
| 65 | // Map of the version a specific effect type introduction |
| 66 | // Id tags defined Parameter::Id union, except Parameter::Id::commonTag (defined in |
| 67 | // kParamTagVersionMap). |
| 68 | static const std::unordered_map<Parameter::Id::Tag, int /* version */> kParamIdEffectVersionMap = { |
| 69 | {Parameter::Id::vendorEffectTag, 1}, |
| 70 | {Parameter::Id::acousticEchoCancelerTag, 1}, |
| 71 | {Parameter::Id::automaticGainControlV1Tag, 1}, |
| 72 | {Parameter::Id::automaticGainControlV2Tag, 1}, |
| 73 | {Parameter::Id::bassBoostTag, 1}, |
| 74 | {Parameter::Id::downmixTag, 1}, |
| 75 | {Parameter::Id::dynamicsProcessingTag, 1}, |
| 76 | {Parameter::Id::environmentalReverbTag, 1}, |
| 77 | {Parameter::Id::equalizerTag, 1}, |
| 78 | {Parameter::Id::hapticGeneratorTag, 1}, |
| 79 | {Parameter::Id::loudnessEnhancerTag, 1}, |
| 80 | {Parameter::Id::noiseSuppressionTag, 1}, |
| 81 | {Parameter::Id::presetReverbTag, 1}, |
| 82 | {Parameter::Id::virtualizerTag, 1}, |
| 83 | {Parameter::Id::visualizerTag, 1}, |
| 84 | {Parameter::Id::volumeTag, 1}, |
| 85 | {Parameter::Id::spatializerTag, 2}, |
Shunkai Yao | f8b9906 | 2024-10-17 02:32:53 +0000 | [diff] [blame] | 86 | {Parameter::Id::eraserTag, 3}, |
Shunkai Yao | 21f6ac6 | 2024-02-08 01:41:10 +0000 | [diff] [blame] | 87 | }; |
| 88 | // Tags defined Parameter::Specific union. |
| 89 | static const std::unordered_map<Parameter::Specific::Tag, int /* version */> |
| 90 | kParamEffectVersionMap = { |
| 91 | {Parameter::Specific::vendorEffect, 1}, |
| 92 | {Parameter::Specific::acousticEchoCanceler, 1}, |
| 93 | {Parameter::Specific::automaticGainControlV1, 1}, |
| 94 | {Parameter::Specific::automaticGainControlV2, 1}, |
| 95 | {Parameter::Specific::bassBoost, 1}, |
| 96 | {Parameter::Specific::downmix, 1}, |
| 97 | {Parameter::Specific::dynamicsProcessing, 1}, |
| 98 | {Parameter::Specific::environmentalReverb, 1}, |
| 99 | {Parameter::Specific::equalizer, 1}, |
| 100 | {Parameter::Specific::hapticGenerator, 1}, |
| 101 | {Parameter::Specific::loudnessEnhancer, 1}, |
| 102 | {Parameter::Specific::noiseSuppression, 1}, |
| 103 | {Parameter::Specific::presetReverb, 1}, |
| 104 | {Parameter::Specific::virtualizer, 1}, |
| 105 | {Parameter::Specific::visualizer, 1}, |
| 106 | {Parameter::Specific::volume, 1}, |
| 107 | {Parameter::Specific::spatializer, 2}, |
Shunkai Yao | f8b9906 | 2024-10-17 02:32:53 +0000 | [diff] [blame] | 108 | {Parameter::Specific::eraser, 3}, |
Shunkai Yao | 21f6ac6 | 2024-02-08 01:41:10 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | class MockFactory : public IFactory { |
| 112 | public: |
| 113 | explicit MockFactory(int version) : IFactory(), mVersion(version) {} |
| 114 | MOCK_METHOD(ndk::ScopedAStatus, queryEffects, |
| 115 | (const std::optional<AudioUuid>& in_type_uuid, |
| 116 | const std::optional<AudioUuid>& in_impl_uuid, |
| 117 | const std::optional<AudioUuid>& in_proxy_uuid, |
| 118 | std::vector<Descriptor>* _aidl_return), |
| 119 | (override)); |
| 120 | |
| 121 | MOCK_METHOD(ndk::ScopedAStatus, queryProcessing, |
| 122 | (const std::optional<Processing::Type>& in_type, |
| 123 | std::vector<Processing>* _aidl_return), |
| 124 | (override)); |
| 125 | |
| 126 | MOCK_METHOD(ndk::ScopedAStatus, createEffect, |
| 127 | (const AudioUuid& in_impl_uuid, std::shared_ptr<IEffect>* _aidl_return), |
| 128 | (override)); |
| 129 | |
| 130 | MOCK_METHOD(ndk::ScopedAStatus, destroyEffect, (const std::shared_ptr<IEffect>& in_handle), |
| 131 | (override)); |
| 132 | |
| 133 | ndk::ScopedAStatus getInterfaceVersion(int32_t* _aidl_return) { |
| 134 | *_aidl_return = mVersion; |
| 135 | return ndk::ScopedAStatus::ok(); |
| 136 | } |
| 137 | |
| 138 | // these must be implemented but won't be used in this testing |
| 139 | ::ndk::SpAIBinder asBinder() { return ::ndk::SpAIBinder(); } |
| 140 | bool isRemote() { return false; } |
| 141 | ::ndk::ScopedAStatus getInterfaceHash(std::string*) { return ndk::ScopedAStatus::ok(); } |
| 142 | |
| 143 | private: |
| 144 | const int mVersion; |
| 145 | }; |
| 146 | |
| 147 | class MockEffect : public IEffect { |
| 148 | public: |
| 149 | explicit MockEffect(int version) : IEffect(), mVersion(version) {} |
| 150 | MOCK_METHOD(ndk::ScopedAStatus, open, |
| 151 | (const Parameter::Common& common, |
| 152 | const std::optional<Parameter::Specific>& specific, |
| 153 | IEffect::OpenEffectReturn* ret), |
| 154 | (override)); |
| 155 | MOCK_METHOD(ndk::ScopedAStatus, close, (), (override)); |
| 156 | MOCK_METHOD(binder_status_t, dump, (int fd, const char** args, uint32_t numArgs), (override)); |
| 157 | MOCK_METHOD(ndk::ScopedAStatus, command, (CommandId id), (override)); |
| 158 | MOCK_METHOD(ndk::ScopedAStatus, getState, (State * state), (override)); |
| 159 | MOCK_METHOD(ndk::ScopedAStatus, getDescriptor, (Descriptor * desc), (override)); |
| 160 | MOCK_METHOD(ndk::ScopedAStatus, destroy, (), ()); |
| 161 | |
| 162 | // reopen introduced in version kReopenSupportedVersion |
| 163 | ndk::ScopedAStatus reopen(IEffect::OpenEffectReturn*) override { |
| 164 | return mVersion < kReopenSupportedVersion |
| 165 | ? ndk::ScopedAStatus::fromStatus(STATUS_UNKNOWN_TRANSACTION) |
| 166 | : ndk::ScopedAStatus::ok(); |
| 167 | } |
| 168 | |
| 169 | // for all parameters introduced |
| 170 | ndk::ScopedAStatus setParameter(const Parameter& param) override { |
| 171 | const auto paramTag = param.getTag(); |
| 172 | switch (paramTag) { |
| 173 | case Parameter::common: |
| 174 | case Parameter::deviceDescription: |
| 175 | case Parameter::mode: |
| 176 | case Parameter::source: |
| 177 | case Parameter::offload: |
| 178 | case Parameter::volumeStereo: |
| 179 | case Parameter::sinkMetadata: |
| 180 | FALLTHROUGH_INTENDED; |
| 181 | case Parameter::sourceMetadata: { |
| 182 | if (kParamTagVersionMap.find(paramTag) != kParamTagVersionMap.end() && |
| 183 | kParamTagVersionMap.at(paramTag) >= mVersion) { |
| 184 | return ndk::ScopedAStatus::ok(); |
| 185 | } |
| 186 | break; |
| 187 | } |
| 188 | case Parameter::specific: { |
| 189 | // TODO |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | return ndk::ScopedAStatus::fromStatus(STATUS_BAD_VALUE); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Only care about version compatibility here: |
| 198 | * @return BAD_VALUE if a tag is not supported by current AIDL version. |
| 199 | * @return OK if a tag is supported by current AIDL version. |
| 200 | */ |
| 201 | ndk::ScopedAStatus getParameter(const Parameter::Id& id, Parameter*) override { |
| 202 | const auto idTag = id.getTag(); |
| 203 | switch (idTag) { |
| 204 | case Parameter::Id::commonTag: { |
| 205 | const auto paramTag = id.get<Parameter::Id::commonTag>(); |
| 206 | if (kParamTagVersionMap.find(paramTag) != kParamTagVersionMap.end() && |
| 207 | kParamTagVersionMap.at(paramTag) >= mVersion) { |
| 208 | return ndk::ScopedAStatus::ok(); |
| 209 | } |
| 210 | break; |
| 211 | } |
| 212 | case Parameter::Id::vendorEffectTag: |
| 213 | case Parameter::Id::acousticEchoCancelerTag: |
| 214 | case Parameter::Id::automaticGainControlV1Tag: |
| 215 | case Parameter::Id::automaticGainControlV2Tag: |
| 216 | case Parameter::Id::bassBoostTag: |
| 217 | case Parameter::Id::downmixTag: |
| 218 | case Parameter::Id::dynamicsProcessingTag: |
| 219 | case Parameter::Id::environmentalReverbTag: |
| 220 | case Parameter::Id::equalizerTag: |
| 221 | case Parameter::Id::hapticGeneratorTag: |
| 222 | case Parameter::Id::loudnessEnhancerTag: |
| 223 | case Parameter::Id::noiseSuppressionTag: |
| 224 | case Parameter::Id::presetReverbTag: |
| 225 | case Parameter::Id::virtualizerTag: |
| 226 | case Parameter::Id::visualizerTag: |
| 227 | case Parameter::Id::volumeTag: |
Shunkai Yao | f8b9906 | 2024-10-17 02:32:53 +0000 | [diff] [blame] | 228 | case Parameter::Id::eraserTag: |
Shunkai Yao | 21f6ac6 | 2024-02-08 01:41:10 +0000 | [diff] [blame] | 229 | FALLTHROUGH_INTENDED; |
| 230 | case Parameter::Id::spatializerTag: { |
| 231 | if (kParamIdEffectVersionMap.find(idTag) != kParamIdEffectVersionMap.end() && |
| 232 | kParamIdEffectVersionMap.at(idTag) >= mVersion) { |
| 233 | return ndk::ScopedAStatus::ok(); |
| 234 | } |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | return ndk::ScopedAStatus::fromStatus(STATUS_BAD_VALUE); |
| 239 | } |
| 240 | |
| 241 | ndk::ScopedAStatus getInterfaceVersion(int32_t* _aidl_return) { |
| 242 | *_aidl_return = mVersion; |
| 243 | return ndk::ScopedAStatus::ok(); |
| 244 | } |
| 245 | |
| 246 | // these must be implemented but won't be used in this testing |
| 247 | ::ndk::SpAIBinder asBinder() { return ::ndk::SpAIBinder(); } |
| 248 | bool isRemote() { return false; } |
| 249 | ::ndk::ScopedAStatus getInterfaceHash(std::string*) { return ndk::ScopedAStatus::ok(); } |
| 250 | |
| 251 | private: |
| 252 | const int mVersion; |
| 253 | }; |
| 254 | |
| 255 | static const std::vector<AudioUuid> kTestParamUUIDs = { |
| 256 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidAcousticEchoCanceler(), |
| 257 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidAutomaticGainControlV1(), |
| 258 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidAutomaticGainControlV2(), |
| 259 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidBassBoost(), |
| 260 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidDownmix(), |
| 261 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidDynamicsProcessing(), |
| 262 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidEnvReverb(), |
| 263 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidEqualizer(), |
| 264 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidHapticGenerator(), |
| 265 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidLoudnessEnhancer(), |
| 266 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidNoiseSuppression(), |
| 267 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidPresetReverb(), |
| 268 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidSpatializer(), |
| 269 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidVirtualizer(), |
| 270 | ::aidl::android::hardware::audio::effect::getEffectTypeUuidVisualizer(), |
| 271 | ::aidl::android::hardware::audio::effect::getEffectUuidNull(), |
| 272 | }; |
| 273 | static const std::vector<int> kTestParamVersion = {1, 2}; // Effect AIDL HAL versions to test |
| 274 | |
| 275 | enum ParamName { UUID, VERSION }; |
| 276 | using TestParam = std::tuple<AudioUuid, int /* version */>; |
| 277 | |
| 278 | class EffectHalVersionCompatibilityTest : public ::testing::TestWithParam<TestParam> { |
| 279 | public: |
| 280 | void SetUp() override { |
| 281 | mMockFactory = ndk::SharedRefBase::make<MockFactory>(mVersion); |
| 282 | ASSERT_NE(mMockFactory, nullptr); |
| 283 | mMockEffect = ndk::SharedRefBase::make<MockEffect>(mVersion); |
| 284 | ASSERT_NE(mMockEffect, nullptr); |
| 285 | mEffectHalAidl = sp<EffectHalAidl>::make(mMockFactory, mMockEffect, 0, 0, mDesc, false); |
| 286 | ASSERT_NE(mEffectHalAidl, nullptr); |
| 287 | } |
| 288 | |
| 289 | void TearDown() override { |
| 290 | EXPECT_CALL(*mMockFactory, destroyEffect(_)); |
| 291 | mEffectHalAidl.clear(); |
| 292 | mMockEffect.reset(); |
| 293 | mMockFactory.reset(); |
| 294 | } |
| 295 | |
| 296 | protected: |
| 297 | const int mVersion = std::get<VERSION>(GetParam()); |
| 298 | const AudioUuid mTypeUuid = std::get<UUID>(GetParam()); |
| 299 | const Descriptor mDesc = {.common.id.type = mTypeUuid}; |
| 300 | std::shared_ptr<MockFactory> mMockFactory = nullptr; |
| 301 | std::shared_ptr<MockEffect> mMockEffect = nullptr; |
| 302 | sp<EffectHalAidl> mEffectHalAidl = nullptr; |
| 303 | }; |
| 304 | |
| 305 | TEST_P(EffectHalVersionCompatibilityTest, testEffectAidlHalCreateDestroy) { |
| 306 | // do nothing |
| 307 | } |
| 308 | |
| 309 | INSTANTIATE_TEST_SUITE_P( |
| 310 | EffectHalVersionCompatibilityTestWithVersion, EffectHalVersionCompatibilityTest, |
| 311 | ::testing::Combine(testing::ValuesIn(kTestParamUUIDs), |
| 312 | testing::ValuesIn(kTestParamVersion)), |
| 313 | [](const testing::TestParamInfo<EffectHalVersionCompatibilityTest::ParamType>& info) { |
| 314 | auto version = std::to_string(std::get<VERSION>(info.param)); |
| 315 | auto uuid = android::audio::utils::toString(std::get<UUID>(info.param)); |
| 316 | std::string name = "EffectHalVersionCompatibilityTest_V" + version + "_" + uuid; |
| 317 | std::replace_if( |
| 318 | name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_'); |
| 319 | return name; |
| 320 | }); |
| 321 | |
| 322 | } // namespace |