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 | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame^] | 32 | using aidl::android::hardware::audio::effect::EffectNullUuid; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 33 | using aidl::android::hardware::audio::effect::IEffect; |
| 34 | using aidl::android::hardware::audio::effect::IFactory; |
Shunkai Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 35 | using aidl::android::hardware::audio::effect::Processing; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 36 | using aidl::android::media::audio::common::AudioUuid; |
| 37 | |
| 38 | class EffectFactoryHelper { |
| 39 | public: |
| 40 | explicit EffectFactoryHelper(const std::string& name) : mServiceName(name) {} |
| 41 | |
| 42 | void ConnectToFactoryService() { |
| 43 | mEffectFactory = IFactory::fromBinder(binderUtil.connectToService(mServiceName)); |
| 44 | ASSERT_NE(mEffectFactory, nullptr); |
| 45 | } |
| 46 | |
| 47 | void RestartFactoryService() { |
| 48 | ASSERT_NE(mEffectFactory, nullptr); |
| 49 | mEffectFactory = IFactory::fromBinder(binderUtil.restartService()); |
| 50 | ASSERT_NE(mEffectFactory, nullptr); |
| 51 | ClearEffectMap(); |
| 52 | } |
| 53 | |
| 54 | void QueryEffects(const std::optional<AudioUuid>& in_type, |
| 55 | const std::optional<AudioUuid>& in_instance, |
| 56 | std::vector<Descriptor::Identity>* _aidl_return) { |
| 57 | ASSERT_NE(mEffectFactory, nullptr); |
| 58 | EXPECT_IS_OK(mEffectFactory->queryEffects(in_type, in_instance, _aidl_return)); |
| 59 | mIds = *_aidl_return; |
| 60 | } |
| 61 | |
Shunkai Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 62 | void QueryProcessing(const std::optional<Processing::Type>& in_type, |
| 63 | std::vector<Processing>* _aidl_return) { |
| 64 | ASSERT_NE(mEffectFactory, nullptr); |
| 65 | EXPECT_IS_OK(mEffectFactory->queryProcessing(in_type, _aidl_return)); |
| 66 | // only update the whole list if no filter applied |
| 67 | if (!in_type.has_value()) { |
| 68 | mProcesses = *_aidl_return; |
| 69 | } |
| 70 | } |
| 71 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 72 | void CreateEffects() { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 73 | for (const auto& id : mIds) { |
| 74 | std::shared_ptr<IEffect> effect; |
| 75 | EXPECT_IS_OK(mEffectFactory->createEffect(id.uuid, &effect)); |
| 76 | EXPECT_NE(effect, nullptr) << id.toString(); |
| 77 | if (effect) { |
| 78 | mEffectIdMap[effect] = id; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame^] | 83 | void QueryAndCreateEffects(const AudioUuid& type = EffectNullUuid) { |
| 84 | std::vector<Descriptor::Identity> ids; |
| 85 | ASSERT_NE(mEffectFactory, nullptr); |
| 86 | |
| 87 | if (type == EffectNullUuid) { |
| 88 | EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, &ids)); |
| 89 | } else { |
| 90 | EXPECT_IS_OK(mEffectFactory->queryEffects(type, std::nullopt, &ids)); |
| 91 | } |
| 92 | for (const auto& id : ids) { |
| 93 | ASSERT_EQ(id.type, type); |
| 94 | std::shared_ptr<IEffect> effect; |
| 95 | EXPECT_IS_OK(mEffectFactory->createEffect(id.uuid, &effect)); |
| 96 | EXPECT_NE(effect, nullptr) << id.toString(); |
| 97 | if (effect) { |
| 98 | mEffectIdMap[effect] = id; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 103 | void CreateEffectsAndExpect( |
| 104 | const std::vector<std::pair<Descriptor::Identity, binder_exception_t>>& uuid_status) { |
| 105 | ASSERT_NE(mEffectFactory, nullptr); |
| 106 | for (const auto& it : uuid_status) { |
| 107 | std::shared_ptr<IEffect> effect; |
| 108 | auto status = mEffectFactory->createEffect(it.first.uuid, &effect); |
| 109 | EXPECT_STATUS(it.second, status); |
| 110 | if (effect) { |
| 111 | mEffectIdMap[effect] = it.first; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void DestroyEffectAndExpect(std::shared_ptr<IEffect>& instance, binder_exception_t exception) { |
| 117 | ASSERT_NE(mEffectFactory, nullptr); |
| 118 | auto status = mEffectFactory->destroyEffect(instance); |
| 119 | EXPECT_STATUS(exception, status); |
| 120 | } |
| 121 | |
| 122 | void QueryAndCreateAllEffects() { |
| 123 | ASSERT_NE(mEffectFactory, nullptr); |
| 124 | EXPECT_IS_OK(mEffectFactory->queryEffects(std::nullopt, std::nullopt, &mCompleteIds)); |
| 125 | for (const auto& id : mCompleteIds) { |
| 126 | std::shared_ptr<IEffect> effect; |
| 127 | EXPECT_IS_OK(mEffectFactory->createEffect(id.uuid, &effect)); |
| 128 | EXPECT_NE(effect, nullptr) << id.toString(); |
| 129 | mEffectIdMap[effect] = id; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void DestroyEffects(const binder_exception_t expected = EX_NONE, const int remaining = 0) { |
| 134 | ASSERT_NE(mEffectFactory, nullptr); |
| 135 | |
| 136 | for (auto it = mEffectIdMap.begin(); it != mEffectIdMap.end();) { |
| 137 | auto erased = it++; |
| 138 | auto status = mEffectFactory->destroyEffect(erased->first); |
| 139 | EXPECT_STATUS(expected, status); |
| 140 | if (status.isOk()) { |
| 141 | mEffectIdMap.erase(erased); |
| 142 | } |
| 143 | } |
| 144 | EXPECT_EQ((unsigned int)remaining, mEffectIdMap.size()); |
| 145 | } |
| 146 | |
| 147 | std::shared_ptr<IFactory> GetFactory() { return mEffectFactory; } |
| 148 | const std::vector<Descriptor::Identity>& GetEffectIds() { return mIds; } |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame^] | 149 | const std::vector<Descriptor::Identity>& GetCompleteEffectIdList() const { |
| 150 | return mCompleteIds; |
| 151 | } |
| 152 | const std::map<std::shared_ptr<IEffect>, Descriptor::Identity>& GetEffectMap() const { |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 153 | return mEffectIdMap; |
| 154 | } |
| 155 | void ClearEffectMap() { mEffectIdMap.clear(); } |
| 156 | |
| 157 | private: |
| 158 | std::shared_ptr<IFactory> mEffectFactory; |
| 159 | std::string mServiceName; |
| 160 | AudioHalBinderServiceUtil binderUtil; |
| 161 | std::vector<Descriptor::Identity> mIds; |
| 162 | std::vector<Descriptor::Identity> mCompleteIds; |
Shunkai Yao | 08b687d | 2022-10-13 21:11:11 +0000 | [diff] [blame] | 163 | std::vector<Processing> mProcesses; |
Shunkai Yao | ea24c1a | 2022-09-28 17:39:23 +0000 | [diff] [blame] | 164 | |
| 165 | std::map<std::shared_ptr<IEffect>, Descriptor::Identity> mEffectIdMap; |
| 166 | }; |