blob: ca3665565508c06e1ffe9683cf8c6a2e64b82121 [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
Shunkai Yao12836852023-12-13 01:07:52 +000024#include <aidl/Vintf.h>
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000025#include <android/binder_auto_utils.h>
26
27#include "TestUtils.h"
28
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 Yaocb0fc412022-12-15 20:34:32 +000052 static std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> getAllEffectDescriptors(
53 std::string serviceName, std::optional<AudioUuid> type = std::nullopt) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000054 AudioHalBinderServiceUtil util;
55 auto names = android::getAidlHalInstanceNames(serviceName);
Shunkai Yaocb0fc412022-12-15 20:34:32 +000056 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> 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) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +000065 if (type.has_value() && desc.common.id.type != type.value()) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000066 continue;
67 }
Shunkai Yaocb0fc412022-12-15 20:34:32 +000068 result.emplace_back(factory, desc);
Shunkai Yao812d5b42022-11-16 18:08:50 +000069 }
70 }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000071 }
72 }
Shunkai Yao812d5b42022-11-16 18:08:50 +000073 return result;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000074 }
75
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000076 private:
77 std::shared_ptr<IFactory> mEffectFactory;
78 std::string mServiceName;
79 AudioHalBinderServiceUtil binderUtil;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000080};