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 | |
Shunkai Yao | 1283685 | 2023-12-13 01:07:52 +0000 | [diff] [blame] | 24 | #include <aidl/Vintf.h> |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 25 | #include <android/binder_auto_utils.h> |
Shunkai Yao | 9f9fe92 | 2024-03-04 21:23:04 +0000 | [diff] [blame^] | 26 | #include <system/audio_effects/aidl_effects_utils.h> |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 27 | |
Bart Van Assche | 5e7fcb4 | 2023-11-08 07:00:22 -0800 | [diff] [blame] | 28 | #include "AudioHalBinderServiceUtil.h" |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 29 | #include "TestUtils.h" |
| 30 | |
| 31 | using namespace android; |
| 32 | |
| 33 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 34 | using aidl::android::hardware::audio::effect::IFactory; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 35 | using aidl::android::media::audio::common::AudioUuid; |
| 36 | |
| 37 | class 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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 52 | std::shared_ptr<IFactory> GetFactory() const { return mEffectFactory; } |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 53 | |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 54 | static std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> getAllEffectDescriptors( |
| 55 | std::string serviceName, std::optional<AudioUuid> type = std::nullopt) { |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 56 | AudioHalBinderServiceUtil util; |
| 57 | auto names = android::getAidlHalInstanceNames(serviceName); |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 58 | std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> result; |
Shunkai Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 59 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 60 | for (const auto& name : names) { |
| 61 | auto factory = IFactory::fromBinder(util.connectToService(name)); |
| 62 | if (factory) { |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 63 | if (std::vector<Descriptor> descs; |
| 64 | factory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &descs) |
| 65 | .isOk()) { |
| 66 | for (const auto& desc : descs) { |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 67 | if (type.has_value() && desc.common.id.type != type.value()) { |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 68 | continue; |
| 69 | } |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 70 | result.emplace_back(factory, desc); |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 75 | return result; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Shunkai Yao | 9f9fe92 | 2024-03-04 21:23:04 +0000 | [diff] [blame^] | 78 | 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 Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 88 | private: |
| 89 | std::shared_ptr<IFactory> mEffectFactory; |
| 90 | std::string mServiceName; |
| 91 | AudioHalBinderServiceUtil binderUtil; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 92 | }; |