1 /*
2  * Copyright 2020, 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 #ifndef CODEC2_HIDL_PLUGIN_FILTER_WRAPPER_H
18 
19 #define CODEC2_HIDL_PLUGIN_FILTER_WRAPPER_H
20 
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 
25 #include <C2Component.h>
26 #include <C2PlatformSupport.h>
27 
28 #include <codec2/hidl/plugin/FilterPlugin.h>
29 #include <utils/Errors.h>
30 
31 namespace android {
32 
33 // TODO: documentation
34 class FilterWrapper : public std::enable_shared_from_this<FilterWrapper> {
35 public:
36     using Descriptor = FilterPlugin_V1::Descriptor;
37 
38     class Plugin {
39     public:
40         Plugin() = default;
41         virtual ~Plugin() = default;
42         virtual status_t status() const = 0;
43         virtual std::shared_ptr<C2ComponentStore> getStore() = 0;
44         virtual bool describe(C2String name, Descriptor *desc) = 0;
45         virtual bool isFilteringEnabled(const std::shared_ptr<C2ComponentInterface> &intf) = 0;
46         virtual c2_status_t queryParamsForPreviousComponent(
47                 const std::shared_ptr<C2ComponentInterface> &intf,
48                 std::vector<std::unique_ptr<C2Param>> *params) = 0;
49         C2_DO_NOT_COPY(Plugin);
50     };
51 
52     struct Component {
53         const std::shared_ptr<C2Component> comp;
54         const std::shared_ptr<C2ComponentInterface> intf;
55         const C2Component::Traits traits;
56         const Descriptor desc;
57     };
58 
59 private:
60     explicit FilterWrapper(std::unique_ptr<Plugin> &&plugin);
61 public:
Create(std::unique_ptr<Plugin> && plugin)62     static std::shared_ptr<FilterWrapper> Create(std::unique_ptr<Plugin> &&plugin) {
63         return std::shared_ptr<FilterWrapper>(new FilterWrapper(std::move(plugin)));
64     }
65     ~FilterWrapper();
66 
67     /**
68      * Returns wrapped interface, or |intf| if wrapping is not possible / needed.
69      */
70     std::shared_ptr<C2ComponentInterface> maybeWrapInterface(
71             const std::shared_ptr<C2ComponentInterface> intf);
72 
73     /**
74      * Returns wrapped component, or |comp| if wrapping is not possible / needed.
75      */
76     std::shared_ptr<C2Component> maybeWrapComponent(
77             const std::shared_ptr<C2Component> comp);
78 
79     /**
80      * Returns ture iff the filtering will apply to the buffer in current configuration.
81      */
82     bool isFilteringEnabled(const std::shared_ptr<C2ComponentInterface> &intf);
83 
84     /**
85      * Create a C2BlockPool object with |allocatorId| for |component|.
86      */
87     c2_status_t createBlockPool(
88             C2PlatformAllocatorStore::id_t allocatorId,
89             std::shared_ptr<const C2Component> component,
90             std::shared_ptr<C2BlockPool> *pool);
91 
92     /**
93      * Query parameters that |intf| wants from the previous component.
94      */
95     c2_status_t queryParamsForPreviousComponent(
96             const std::shared_ptr<C2ComponentInterface> &intf,
97             std::vector<std::unique_ptr<C2Param>> *params);
98 
99 private:
100     status_t mInit;
101     std::unique_ptr<Plugin> mPlugin;
102     std::shared_ptr<C2ComponentStore> mStore;
103     std::list<FilterWrapper::Component> mComponents;
104 
105     std::mutex mCacheMutex;
106     std::map<std::string, C2Component::Traits> mCachedTraits;
107 
108     std::mutex mWrappedComponentsMutex;
109     std::list<std::vector<std::weak_ptr<const C2Component>>> mWrappedComponents;
110 
111     std::vector<FilterWrapper::Component> createFilters();
112     C2Component::Traits getTraits(const std::shared_ptr<C2ComponentInterface> &intf);
113 
114     C2_DO_NOT_COPY(FilterWrapper);
115 };
116 
117 }  // namespace android
118 
119 #endif  // CODEC2_HIDL_PLUGIN_FILTER_WRAPPER_H
120