Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 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 | #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 Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 27 | #include "effect-impl/EffectUUID.h" |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace android; |
| 30 | |
| 31 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 32 | using aidl::android::hardware::audio::effect::IFactory; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 33 | using aidl::android::media::audio::common::AudioUuid; |
| 34 | |
| 35 | class 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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 50 | std::shared_ptr<IFactory> GetFactory() const { return mEffectFactory; } |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 51 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 52 | 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 Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 57 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 58 | for (const auto& name : names) { |
| 59 | auto factory = IFactory::fromBinder(util.connectToService(name)); |
| 60 | if (factory) { |
| 61 | if (std::vector<Descriptor::Identity> ids; |
| 62 | factory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &ids).isOk()) { |
| 63 | for (const auto& id : ids) { |
| 64 | if (type.has_value() && id.type != type.value()) { |
| 65 | continue; |
| 66 | } |
| 67 | result.emplace_back(factory, id); |
| 68 | } |
| 69 | } |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 72 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 73 | return result; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 76 | private: |
| 77 | std::shared_ptr<IFactory> mEffectFactory; |
| 78 | std::string mServiceName; |
| 79 | AudioHalBinderServiceUtil binderUtil; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 80 | }; |