xref: /aosp_15_r20/hardware/interfaces/audio/aidl/vts/EffectFactoryHelper.h (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
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 <aidl/Vintf.h>
25 #include <android/binder_auto_utils.h>
26 #include <system/audio_effects/aidl_effects_utils.h>
27 
28 #include "AudioHalBinderServiceUtil.h"
29 #include "TestUtils.h"
30 
31 using namespace android;
32 
33 using aidl::android::hardware::audio::effect::Descriptor;
34 using aidl::android::hardware::audio::effect::IFactory;
35 using aidl::android::media::audio::common::AudioUuid;
36 
37 class EffectFactoryHelper {
38   public:
39     static std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> getAllEffectDescriptors(
40             std::string serviceName, std::optional<AudioUuid> type = std::nullopt) {
41         AudioHalBinderServiceUtil util;
42         auto names = android::getAidlHalInstanceNames(serviceName);
43         std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> result;
44 
45         for (const auto& name : names) {
46             auto factory = IFactory::fromBinder(util.connectToService(name));
47             if (factory) {
48                 if (std::vector<Descriptor> descs;
49                     factory->queryEffects(std::nullopt, std::nullopt, std::nullopt, &descs)
50                             .isOk()) {
51                     for (const auto& desc : descs) {
52                         if (type.has_value() && desc.common.id.type != type.value()) {
53                             continue;
54                         }
55                         result.emplace_back(factory, desc);
56                     }
57                 }
58             }
59         }
60         return result;
61     }
62 
getHalVersion(const std::shared_ptr<IFactory> & factory)63     static int getHalVersion(const std::shared_ptr<IFactory>& factory) {
64         int version = 0;
65         return (factory && factory->getInterfaceVersion(&version).isOk()) ? version : 0;
66     }
67 
isReopenSupported(const std::shared_ptr<IFactory> & factory)68     static bool isReopenSupported(const std::shared_ptr<IFactory>& factory) {
69         return EffectFactoryHelper::getHalVersion(factory) >=
70                aidl::android::hardware::audio::effect::kReopenSupportedVersion;
71     }
72 };
73