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