blob: cc29f5e2aff3ca26ab3bbf9e5ba357cf705ffa4f [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>
Shunkai Yao9f9fe922024-03-04 21:23:04 +000026#include <system/audio_effects/aidl_effects_utils.h>
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000027
Bart Van Assche5e7fcb42023-11-08 07:00:22 -080028#include "AudioHalBinderServiceUtil.h"
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000029#include "TestUtils.h"
30
31using namespace android;
32
33using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000034using aidl::android::hardware::audio::effect::IFactory;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000035using aidl::android::media::audio::common::AudioUuid;
36
37class EffectFactoryHelper {
38 public:
39 explicit EffectFactoryHelper(const std::string& name) : mServiceName(name) {}
40
41 void ConnectToFactoryService() {
42 mEffectFactory = IFactory::fromBinder(binderUtil.connectToService(mServiceName));
43 ASSERT_NE(mEffectFactory, nullptr);
44 }
45
46 void RestartFactoryService() {
47 ASSERT_NE(mEffectFactory, nullptr);
48 mEffectFactory = IFactory::fromBinder(binderUtil.restartService());
49 ASSERT_NE(mEffectFactory, nullptr);
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000050 }
51
Shunkai Yao812d5b42022-11-16 18:08:50 +000052 std::shared_ptr<IFactory> GetFactory() const { return mEffectFactory; }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000053
Shunkai Yaocb0fc412022-12-15 20:34:32 +000054 static std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> getAllEffectDescriptors(
55 std::string serviceName, std::optional<AudioUuid> type = std::nullopt) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000056 AudioHalBinderServiceUtil util;
57 auto names = android::getAidlHalInstanceNames(serviceName);
Shunkai Yaocb0fc412022-12-15 20:34:32 +000058 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> result;
Shunkai Yao08b687d2022-10-13 21:11:11 +000059
Shunkai Yao812d5b42022-11-16 18:08:50 +000060 for (const auto& name : names) {
61 auto factory = IFactory::fromBinder(util.connectToService(name));
62 if (factory) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +000063 if (std::vector<Descriptor> descs;
64 factory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &descs)
65 .isOk()) {
66 for (const auto& desc : descs) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +000067 if (type.has_value() && desc.common.id.type != type.value()) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000068 continue;
69 }
Shunkai Yaocb0fc412022-12-15 20:34:32 +000070 result.emplace_back(factory, desc);
Shunkai Yao812d5b42022-11-16 18:08:50 +000071 }
72 }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000073 }
74 }
Shunkai Yao812d5b42022-11-16 18:08:50 +000075 return result;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000076 }
77
Shunkai Yao9f9fe922024-03-04 21:23:04 +000078 static int getHalVersion(const std::shared_ptr<IFactory>& factory) {
79 int version = 0;
80 return (factory && factory->getInterfaceVersion(&version).isOk()) ? version : 0;
81 }
82
83 static bool isReopenSupported(const std::shared_ptr<IFactory>& factory) {
84 return EffectFactoryHelper::getHalVersion(factory) >=
85 aidl::android::hardware::audio::effect::kReopenSupportedVersion;
86 }
87
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000088 private:
89 std::shared_ptr<IFactory> mEffectFactory;
90 std::string mServiceName;
91 AudioHalBinderServiceUtil binderUtil;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000092};