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 <memory>
18 #include <set>
19 #include <string>
20 #include <vector>
21
22 #define LOG_TAG "VtsHalAudioEffectFactory"
23
24 #include <aidl/Gtest.h>
25 #include <aidl/Vintf.h>
26 #include <android-base/logging.h>
27 #include <android-base/properties.h>
28 #include <android/binder_interface_utils.h>
29 #include <android/binder_manager.h>
30 #include <android/binder_process.h>
31 #include <system/audio_effects/effect_uuid.h>
32
33 #include <aidl/android/hardware/audio/effect/IFactory.h>
34
35 #include "EffectFactoryHelper.h"
36 #include "TestUtils.h"
37
38 #include <system/audio_aidl_utils.h>
39
40 using namespace android;
41 using ::android::audio::utils::toString;
42
43 using namespace android;
44
45 using aidl::android::hardware::audio::effect::Descriptor;
46 using aidl::android::hardware::audio::effect::getEffectUuidNull;
47 using aidl::android::hardware::audio::effect::getEffectUuidZero;
48 using aidl::android::hardware::audio::effect::IEffect;
49 using aidl::android::hardware::audio::effect::IFactory;
50 using aidl::android::hardware::audio::effect::Processing;
51 using aidl::android::media::audio::common::AudioSource;
52 using aidl::android::media::audio::common::AudioStreamType;
53 using aidl::android::media::audio::common::AudioUuid;
54 using android::hardware::audio::common::testing::detail::TestExecutionTracer;
55
56 /// Effect factory testing.
57 class EffectFactoryTest : public testing::TestWithParam<std::string> {
58 public:
SetUp()59 void SetUp() override { connectAndGetFactory(); }
60
TearDown()61 void TearDown() override {
62 for (auto& effect : mEffects) {
63 const auto status = mEffectFactory->destroyEffect(effect);
64 EXPECT_STATUS(EX_NONE, status);
65 }
66 }
67
68 const std::string kServiceName = GetParam();
69 std::shared_ptr<IFactory> mEffectFactory;
70 std::vector<std::shared_ptr<IEffect>> mEffects;
71 const Descriptor::Identity kNullId = {.uuid = getEffectUuidNull()};
72 const Descriptor::Identity kZeroId = {.uuid = getEffectUuidZero()};
73 const Descriptor kNullDesc = {.common.id = kNullId};
74 const Descriptor kZeroDesc = {.common.id = kZeroId};
75 AudioHalBinderServiceUtil mBinderUtil;
76
77 template <typename Functor>
ForEachId(const std::vector<Descriptor::Identity> ids,Functor functor)78 void ForEachId(const std::vector<Descriptor::Identity> ids, Functor functor) {
79 for (const auto& id : ids) {
80 SCOPED_TRACE(id.toString());
81 functor(id);
82 }
83 }
84 template <typename Functor>
ForEachEffect(std::vector<std::shared_ptr<IEffect>> effects,Functor functor)85 void ForEachEffect(std::vector<std::shared_ptr<IEffect>> effects, Functor functor) {
86 for (auto& effect : effects) {
87 functor(effect);
88 }
89 }
90
createWithDescs(const std::vector<Descriptor> descs,const binder_status_t expectStatus=EX_NONE)91 std::vector<std::shared_ptr<IEffect>> createWithDescs(
92 const std::vector<Descriptor> descs, const binder_status_t expectStatus = EX_NONE) {
93 std::vector<std::shared_ptr<IEffect>> effects;
94 for (const auto& desc : descs) {
95 const auto& uuid = desc.common.id.uuid;
96 std::shared_ptr<IEffect> effect;
97 EXPECT_STATUS(expectStatus, mEffectFactory->createEffect(uuid, &effect));
98 if (expectStatus == EX_NONE) {
99 EXPECT_NE(effect, nullptr) << " null effect with uuid: " << toString(uuid);
100 effects.push_back(std::move(effect));
101 }
102 }
103 return effects;
104 }
destroyEffects(std::vector<std::shared_ptr<IEffect>> effects,const binder_status_t expectStatus=EX_NONE)105 void destroyEffects(std::vector<std::shared_ptr<IEffect>> effects,
106 const binder_status_t expectStatus = EX_NONE) {
107 for (const auto& effect : effects) {
108 EXPECT_STATUS(expectStatus, mEffectFactory->destroyEffect(effect));
109 }
110 }
creatAndDestroyDescs(const std::vector<Descriptor> descs)111 void creatAndDestroyDescs(const std::vector<Descriptor> descs) {
112 for (const auto& desc : descs) {
113 auto effects = createWithDescs({desc});
114 ASSERT_NO_FATAL_FAILURE(destroyEffects(effects));
115 }
116 }
connectAndGetFactory()117 void connectAndGetFactory() {
118 mEffectFactory = IFactory::fromBinder(mBinderUtil.connectToService(kServiceName));
119 ASSERT_NE(mEffectFactory, nullptr);
120 }
restartAndGetFactory()121 void restartAndGetFactory() {
122 mEffectFactory = IFactory::fromBinder(mBinderUtil.restartService());
123 ASSERT_NE(mEffectFactory, nullptr);
124 }
125 };
126
TEST_P(EffectFactoryTest,SetupAndTearDown)127 TEST_P(EffectFactoryTest, SetupAndTearDown) {
128 // Intentionally empty test body.
129 }
130
TEST_P(EffectFactoryTest,CanBeRestarted)131 TEST_P(EffectFactoryTest, CanBeRestarted) {
132 ASSERT_NE(mEffectFactory, nullptr);
133 restartAndGetFactory();
134 }
135
136 /**
137 * @brief Check at least support list of effect must be supported by aosp:
138 * https://developer.android.com/reference/android/media/audiofx/AudioEffect
139 *
140 * For Android 13, they are: Equalizer, LoudnessEnhancer, Visualizer, and DynamicsProcessing.
141 * https://source.android.com/docs/compatibility/13/android-13-cdd#552_audio_effects
142 */
TEST_P(EffectFactoryTest,SupportMandatoryEffectTypes)143 TEST_P(EffectFactoryTest, SupportMandatoryEffectTypes) {
144 std::vector<Descriptor> descs;
145 std::set<AudioUuid> typeUuidSet({
146 aidl::android::hardware::audio::effect::getEffectTypeUuidEqualizer(),
147 aidl::android::hardware::audio::effect::getEffectTypeUuidDynamicsProcessing(),
148 aidl::android::hardware::audio::effect::getEffectTypeUuidLoudnessEnhancer(),
149 aidl::android::hardware::audio::effect::getEffectTypeUuidVisualizer(),
150 });
151
152 EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &descs));
153 EXPECT_TRUE(descs.size() >= typeUuidSet.size());
154 for (const auto& desc : descs) {
155 typeUuidSet.erase(desc.common.id.type);
156 }
157 std::string msg = " missing type UUID:\n";
158 for (const auto& uuid : typeUuidSet) {
159 msg += (toString(uuid) + "\n");
160 }
161 SCOPED_TRACE(msg);
162 EXPECT_EQ(0UL, typeUuidSet.size());
163 }
164
TEST_P(EffectFactoryTest,QueryNullTypeUuid)165 TEST_P(EffectFactoryTest, QueryNullTypeUuid) {
166 std::vector<Descriptor> descs;
167 EXPECT_IS_OK(
168 mEffectFactory->queryEffects(getEffectUuidNull(), std::nullopt, std::nullopt, &descs));
169 EXPECT_EQ(descs.size(), 0UL);
170 }
171
TEST_P(EffectFactoryTest,QueriedNullImplUuid)172 TEST_P(EffectFactoryTest, QueriedNullImplUuid) {
173 std::vector<Descriptor> descs;
174 EXPECT_IS_OK(
175 mEffectFactory->queryEffects(std::nullopt, getEffectUuidNull(), std::nullopt, &descs));
176 EXPECT_EQ(descs.size(), 0UL);
177 }
178
TEST_P(EffectFactoryTest,QueriedNullProxyUuid)179 TEST_P(EffectFactoryTest, QueriedNullProxyUuid) {
180 std::vector<Descriptor> descs;
181 EXPECT_IS_OK(
182 mEffectFactory->queryEffects(std::nullopt, std::nullopt, getEffectUuidNull(), &descs));
183 EXPECT_EQ(descs.size(), 0UL);
184 }
185
186 // create all effects, and then destroy them all together
TEST_P(EffectFactoryTest,CreateAndDestroyEffects)187 TEST_P(EffectFactoryTest, CreateAndDestroyEffects) {
188 std::vector<Descriptor> descs;
189 EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &descs));
190 EXPECT_NE(descs.size(), 0UL);
191
192 std::vector<std::shared_ptr<IEffect>> effects;
193 effects = createWithDescs(descs);
194 EXPECT_EQ(descs.size(), effects.size());
195 destroyEffects(effects);
196 }
197
TEST_P(EffectFactoryTest,CreateMultipleInstanceOfSameEffect)198 TEST_P(EffectFactoryTest, CreateMultipleInstanceOfSameEffect) {
199 std::vector<Descriptor> descs;
200 EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &descs));
201 EXPECT_NE(descs.size(), 0UL);
202
203 std::vector<std::shared_ptr<IEffect>> effects = createWithDescs(descs);
204 EXPECT_EQ(descs.size(), effects.size());
205 std::vector<std::shared_ptr<IEffect>> effects2 = createWithDescs(descs);
206 EXPECT_EQ(descs.size(), effects2.size());
207 std::vector<std::shared_ptr<IEffect>> effects3 = createWithDescs(descs);
208 EXPECT_EQ(descs.size(), effects3.size());
209
210 destroyEffects(effects);
211 destroyEffects(effects2);
212 destroyEffects(effects3);
213 }
214
215 // create and destroy each effect one by one
TEST_P(EffectFactoryTest,CreateAndDestroyEffectsOneByOne)216 TEST_P(EffectFactoryTest, CreateAndDestroyEffectsOneByOne) {
217 std::vector<Descriptor> descs;
218 EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &descs));
219 EXPECT_NE(descs.size(), 0UL);
220
221 creatAndDestroyDescs(descs);
222 }
223
224 // for each effect: repeat 3 times create and destroy
TEST_P(EffectFactoryTest,CreateAndDestroyRepeat)225 TEST_P(EffectFactoryTest, CreateAndDestroyRepeat) {
226 std::vector<Descriptor> descs;
227 EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &descs));
228 EXPECT_NE(descs.size(), 0UL);
229
230 creatAndDestroyDescs(descs);
231 creatAndDestroyDescs(descs);
232 creatAndDestroyDescs(descs);
233 }
234
235 // Expect EX_ILLEGAL_ARGUMENT when create with invalid UUID.
TEST_P(EffectFactoryTest,CreateWithInvalidUuid)236 TEST_P(EffectFactoryTest, CreateWithInvalidUuid) {
237 std::vector<Descriptor> descs = {kNullDesc, kZeroDesc};
238 auto effects = createWithDescs(descs, EX_ILLEGAL_ARGUMENT);
239 EXPECT_EQ(effects.size(), 0UL);
240 }
241
242 // Expect EX_ILLEGAL_ARGUMENT when destroy null interface.
TEST_P(EffectFactoryTest,DestroyWithInvalidInterface)243 TEST_P(EffectFactoryTest, DestroyWithInvalidInterface) {
244 std::shared_ptr<IEffect> spDummyEffect(nullptr);
245 destroyEffects({spDummyEffect}, EX_ILLEGAL_ARGUMENT);
246 }
247
248 // Same descriptor ID should work after service restart.
TEST_P(EffectFactoryTest,CreateDestroyWithRestart)249 TEST_P(EffectFactoryTest, CreateDestroyWithRestart) {
250 std::vector<Descriptor> descs;
251 EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &descs));
252 EXPECT_NE(descs.size(), 0UL);
253 creatAndDestroyDescs(descs);
254
255 restartAndGetFactory();
256 connectAndGetFactory();
257 creatAndDestroyDescs(descs);
258 }
259
260 // Effect handle invalid after restart.
TEST_P(EffectFactoryTest,EffectInvalidAfterRestart)261 TEST_P(EffectFactoryTest, EffectInvalidAfterRestart) {
262 std::vector<Descriptor> descs;
263 EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &descs));
264 EXPECT_NE(descs.size(), 0UL);
265 std::vector<std::shared_ptr<IEffect>> effects = createWithDescs(descs);
266
267 restartAndGetFactory();
268 connectAndGetFactory();
269 destroyEffects(effects, EX_ILLEGAL_ARGUMENT);
270 }
271
272 // expect no error with the queryProcessing interface, but don't check number of processing
TEST_P(EffectFactoryTest,QueryProcess)273 TEST_P(EffectFactoryTest, QueryProcess) {
274 std::vector<Processing> processing;
275 EXPECT_IS_OK(mEffectFactory->queryProcessing(std::nullopt, &processing));
276 std::set<Processing> processingSet(processing.begin(), processing.end());
277
278 Processing::Type streamType =
279 Processing::Type::make<Processing::Type::streamType>(AudioStreamType::SYSTEM);
280 std::vector<Processing> processingFilteredByStream;
281 EXPECT_IS_OK(mEffectFactory->queryProcessing(streamType, &processingFilteredByStream));
282
283 Processing::Type source =
284 Processing::Type::make<Processing::Type::source>(AudioSource::DEFAULT);
285 std::vector<Processing> processingFilteredBySource;
286 EXPECT_IS_OK(mEffectFactory->queryProcessing(source, &processingFilteredBySource));
287
288 EXPECT_TRUE(processing.size() >= processingFilteredByStream.size());
289 EXPECT_TRUE(std::all_of(
290 processingFilteredByStream.begin(), processingFilteredByStream.end(),
291 [&](const auto& proc) { return processingSet.find(proc) != processingSet.end(); }));
292
293 EXPECT_TRUE(processing.size() >= processingFilteredBySource.size());
294 EXPECT_TRUE(std::all_of(
295 processingFilteredBySource.begin(), processingFilteredBySource.end(),
296 [&](const auto& proc) { return processingSet.find(proc) != processingSet.end(); }));
297 }
298
299 // Make sure all effect instances have same HAL version number as IFactory.
TEST_P(EffectFactoryTest,VersionNumberForAllEffectsEqualsToIFactory)300 TEST_P(EffectFactoryTest, VersionNumberForAllEffectsEqualsToIFactory) {
301 std::vector<Descriptor> descs;
302 EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &descs));
303 EXPECT_NE(descs.size(), 0UL);
304
305 std::vector<std::shared_ptr<IEffect>> effects = createWithDescs(descs);
306 int factoryVersion = 0;
307 EXPECT_IS_OK(mEffectFactory->getInterfaceVersion(&factoryVersion));
308
309 for (const auto& effect : effects) {
310 int effectVersion = 0;
311 EXPECT_NE(nullptr, effect);
312 EXPECT_IS_OK(effect->getInterfaceVersion(&effectVersion));
313 EXPECT_EQ(factoryVersion, effectVersion);
314 }
315 ASSERT_NO_FATAL_FAILURE(destroyEffects(effects));
316 }
317
318 INSTANTIATE_TEST_SUITE_P(EffectFactoryTest, EffectFactoryTest,
319 testing::ValuesIn(android::getAidlHalInstanceNames(IFactory::descriptor)),
320 android::PrintInstanceNameToString);
321 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EffectFactoryTest);
322
main(int argc,char ** argv)323 int main(int argc, char** argv) {
324 ::testing::InitGoogleTest(&argc, argv);
325 ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
326 ABinderProcess_setThreadPoolMaxThreadCount(1);
327 ABinderProcess_startThreadPool();
328 return RUN_ALL_TESTS();
329 }
330