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 <any>
20 #include <map>
21 #include <optional>
22 #include <set>
23 #include <tuple>
24 #include <vector>
25 
26 #include <aidl/android/hardware/audio/effect/BnFactory.h>
27 #include <android-base/thread_annotations.h>
28 #include "EffectConfig.h"
29 
30 namespace aidl::android::hardware::audio::effect {
31 
32 class Factory : public BnFactory {
33   public:
34     explicit Factory(const std::string& file);
35     /**
36      * @brief Get identity of all effects supported by the device, with the optional filter by type
37      * and/or by instance UUID.
38      *
39      * @param in_type Type UUID.
40      * @param in_instance Instance UUID.
41      * @param in_proxy Proxy UUID.
42      * @param out_descriptor List of Descriptors.
43      * @return ndk::ScopedAStatus
44      */
45     ndk::ScopedAStatus queryEffects(
46             const std::optional<::aidl::android::media::audio::common::AudioUuid>& in_type,
47             const std::optional<::aidl::android::media::audio::common::AudioUuid>& in_instance,
48             const std::optional<::aidl::android::media::audio::common::AudioUuid>& in_proxy,
49             std::vector<Descriptor>* out_descriptor) override;
50 
51     /**
52      * @brief Query list of defined processing, with the optional filter by AudioStreamType
53      *
54      * @param in_type Type of processing, could be AudioStreamType or AudioSource. Optional.
55      * @param _aidl_return List of processing filtered by in_type.
56      * @return ndk::ScopedAStatus
57      */
58     ndk::ScopedAStatus queryProcessing(const std::optional<Processing::Type>& in_type,
59                                        std::vector<Processing>* _aidl_return) override;
60 
61     /**
62      * @brief Create an effect instance for a certain implementation (identified by UUID).
63      *
64      * @param in_impl_uuid Effect implementation UUID.
65      * @param _aidl_return A pointer to created effect instance.
66      * @return ndk::ScopedAStatus
67      */
68     ndk::ScopedAStatus createEffect(
69             const ::aidl::android::media::audio::common::AudioUuid& in_impl_uuid,
70             std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>* _aidl_return)
71             override;
72 
73     /**
74      * @brief Destroy an effect instance.
75      *
76      * @param in_handle Effect instance handle.
77      * @return ndk::ScopedAStatus
78      */
79     ndk::ScopedAStatus destroyEffect(
80             const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_handle)
81             override;
82 
83   private:
84     const EffectConfig mConfig;
85     ~Factory();
86 
87     std::mutex mMutex;
88     // Set of effect descriptors supported by the devices.
89     std::set<Descriptor> mDescSet GUARDED_BY(mMutex);
90     std::set<Descriptor::Identity> mIdentitySet GUARDED_BY(mMutex);
91 
92     static constexpr int kMapEntryHandleIndex = 0;
93     static constexpr int kMapEntryInterfaceIndex = 1;
94     static constexpr int kMapEntryLibNameIndex = 2;
95     typedef std::tuple<std::unique_ptr<void, std::function<void(void*)>> /* dlHandle */,
96                        std::unique_ptr<struct effect_dl_interface_s> /* interfaces */,
97                        std::string /* library name */>
98             DlEntry;
99 
100     std::map<aidl::android::media::audio::common::AudioUuid /* implUUID */, DlEntry> mEffectLibMap
101             GUARDED_BY(mMutex);
102 
103     typedef std::pair<aidl::android::media::audio::common::AudioUuid, ndk::SpAIBinder> EffectEntry;
104     std::map<std::weak_ptr<IEffect>, EffectEntry, std::owner_less<>> mEffectMap GUARDED_BY(mMutex);
105 
106     ndk::ScopedAStatus destroyEffectImpl_l(const std::shared_ptr<IEffect>& in_handle)
107             REQUIRES(mMutex);
108     void cleanupEffectMap_l() REQUIRES(mMutex);
109     bool openEffectLibrary(const ::aidl::android::media::audio::common::AudioUuid& impl,
110                            const std::string& path);
111     void createIdentityWithConfig(
112             const EffectConfig::Library& configLib,
113             const ::aidl::android::media::audio::common::AudioUuid& typeUuidStr,
114             const std::optional<::aidl::android::media::audio::common::AudioUuid> proxyUuid);
115 
116     ndk::ScopedAStatus getDescriptorWithUuid_l(
117             const aidl::android::media::audio::common::AudioUuid& uuid, Descriptor* desc)
118             REQUIRES(mMutex);
119 
120     void loadEffectLibs();
121     /* Get effect_dl_interface_s from library handle */
122     void getDlSyms_l(DlEntry& entry) REQUIRES(mMutex);
123 };
124 
125 }  // namespace aidl::android::hardware::audio::effect
126