blob: b649d9ee978382f2c8a461468d75eb1cfbf0b48e [file] [log] [blame]
Shunkai Yaoea24c1a2022-09-28 17:39:23 +00001/*
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#pragma once
18
19#include <memory>
20#include <string>
21#include <unordered_map>
22#include <vector>
23
24#include <android/binder_auto_utils.h>
25
26#include "TestUtils.h"
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000027#include "effect-impl/EffectUUID.h"
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000028
29using namespace android;
30
31using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000032using aidl::android::hardware::audio::effect::IFactory;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000033using aidl::android::media::audio::common::AudioUuid;
34
35class EffectFactoryHelper {
36 public:
37 explicit EffectFactoryHelper(const std::string& name) : mServiceName(name) {}
38
39 void ConnectToFactoryService() {
40 mEffectFactory = IFactory::fromBinder(binderUtil.connectToService(mServiceName));
41 ASSERT_NE(mEffectFactory, nullptr);
42 }
43
44 void RestartFactoryService() {
45 ASSERT_NE(mEffectFactory, nullptr);
46 mEffectFactory = IFactory::fromBinder(binderUtil.restartService());
47 ASSERT_NE(mEffectFactory, nullptr);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000048 }
49
Shunkai Yao812d5b42022-11-16 18:08:50 +000050 std::shared_ptr<IFactory> GetFactory() const { return mEffectFactory; }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000051
Shunkai Yao812d5b42022-11-16 18:08:50 +000052 static std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor::Identity>>
53 getAllEffectDescriptors(std::string serviceName, std::optional<AudioUuid> type = std::nullopt) {
54 AudioHalBinderServiceUtil util;
55 auto names = android::getAidlHalInstanceNames(serviceName);
56 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor::Identity>> result;
Shunkai Yao08b687d2022-10-13 21:11:11 +000057
Shunkai Yao812d5b42022-11-16 18:08:50 +000058 for (const auto& name : names) {
59 auto factory = IFactory::fromBinder(util.connectToService(name));
60 if (factory) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +000061 if (std::vector<Descriptor> descs;
62 factory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &descs)
63 .isOk()) {
64 for (const auto& desc : descs) {
65 const auto& id = desc.common.id;
Shunkai Yao812d5b42022-11-16 18:08:50 +000066 if (type.has_value() && id.type != type.value()) {
67 continue;
68 }
69 result.emplace_back(factory, id);
70 }
71 }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000072 }
73 }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000074
Shunkai Yao812d5b42022-11-16 18:08:50 +000075 return result;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000076 }
77
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000078 private:
79 std::shared_ptr<IFactory> mEffectFactory;
80 std::string mServiceName;
81 AudioHalBinderServiceUtil binderUtil;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000082};