blob: a2499fd3c193238edcb76b792bbc47b2839e893a [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"
27
28using namespace android;
29
30using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000031using aidl::android::hardware::audio::effect::IFactory;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000032using aidl::android::media::audio::common::AudioUuid;
33
34class EffectFactoryHelper {
35 public:
36 explicit EffectFactoryHelper(const std::string& name) : mServiceName(name) {}
37
38 void ConnectToFactoryService() {
39 mEffectFactory = IFactory::fromBinder(binderUtil.connectToService(mServiceName));
40 ASSERT_NE(mEffectFactory, nullptr);
41 }
42
43 void RestartFactoryService() {
44 ASSERT_NE(mEffectFactory, nullptr);
45 mEffectFactory = IFactory::fromBinder(binderUtil.restartService());
46 ASSERT_NE(mEffectFactory, nullptr);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000047 }
48
Shunkai Yao812d5b42022-11-16 18:08:50 +000049 std::shared_ptr<IFactory> GetFactory() const { return mEffectFactory; }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000050
Shunkai Yaocb0fc412022-12-15 20:34:32 +000051 static std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> getAllEffectDescriptors(
52 std::string serviceName, std::optional<AudioUuid> type = std::nullopt) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000053 AudioHalBinderServiceUtil util;
54 auto names = android::getAidlHalInstanceNames(serviceName);
Shunkai Yaocb0fc412022-12-15 20:34:32 +000055 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> result;
Shunkai Yao08b687d2022-10-13 21:11:11 +000056
Shunkai Yao812d5b42022-11-16 18:08:50 +000057 for (const auto& name : names) {
58 auto factory = IFactory::fromBinder(util.connectToService(name));
59 if (factory) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +000060 if (std::vector<Descriptor> descs;
61 factory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &descs)
62 .isOk()) {
63 for (const auto& desc : descs) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +000064 if (type.has_value() && desc.common.id.type != type.value()) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000065 continue;
66 }
Shunkai Yaocb0fc412022-12-15 20:34:32 +000067 result.emplace_back(factory, desc);
Shunkai Yao812d5b42022-11-16 18:08:50 +000068 }
69 }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000070 }
71 }
Shunkai Yao812d5b42022-11-16 18:08:50 +000072 return result;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000073 }
74
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000075 private:
76 std::shared_ptr<IFactory> mEffectFactory;
77 std::string mServiceName;
78 AudioHalBinderServiceUtil binderUtil;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000079};