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" 27 28 using namespace android; 29 30 using aidl::android::hardware::audio::effect::Descriptor; 31 using aidl::android::hardware::audio::effect::IFactory; 32 using aidl::android::media::audio::common::AudioUuid; 33 34 class EffectFactoryHelper { 35 public: EffectFactoryHelper(const std::string & name)36 explicit EffectFactoryHelper(const std::string& name) : mServiceName(name) {} 37 ConnectToFactoryService()38 void ConnectToFactoryService() { 39 mEffectFactory = IFactory::fromBinder(binderUtil.connectToService(mServiceName)); 40 ASSERT_NE(mEffectFactory, nullptr); 41 } 42 RestartFactoryService()43 void RestartFactoryService() { 44 ASSERT_NE(mEffectFactory, nullptr); 45 mEffectFactory = IFactory::fromBinder(binderUtil.restartService()); 46 ASSERT_NE(mEffectFactory, nullptr); 47 } 48 GetFactory()49 std::shared_ptr<IFactory> GetFactory() const { return mEffectFactory; } 50 51 static std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> getAllEffectDescriptors( 52 std::string serviceName, std::optional<AudioUuid> type = std::nullopt) { 53 AudioHalBinderServiceUtil util; 54 auto names = android::getAidlHalInstanceNames(serviceName); 55 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> result; 56 57 for (const auto& name : names) { 58 auto factory = IFactory::fromBinder(util.connectToService(name)); 59 if (factory) { 60 if (std::vector<Descriptor> descs; 61 factory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &descs) 62 .isOk()) { 63 for (const auto& desc : descs) { 64 if (type.has_value() && desc.common.id.type != type.value()) { 65 continue; 66 } 67 result.emplace_back(factory, desc); 68 } 69 } 70 } 71 } 72 return result; 73 } 74 75 private: 76 std::shared_ptr<IFactory> mEffectFactory; 77 std::string mServiceName; 78 AudioHalBinderServiceUtil binderUtil; 79 }; 80