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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "Codec2-DefaultFilterPlugin"
19 #include <android-base/logging.h>
20 
21 #include <set>
22 
23 #include <dlfcn.h>
24 
25 #include <C2Config.h>
26 #include <C2Debug.h>
27 #include <C2ParamInternal.h>
28 
29 #include <codec2/hidl/plugin/FilterPlugin.h>
30 
31 #include <DefaultFilterPlugin.h>
32 #include <FilterWrapper.h>
33 
34 namespace android {
35 
DefaultFilterPlugin(const char * pluginPath)36 DefaultFilterPlugin::DefaultFilterPlugin(const char *pluginPath)
37     : mInit(NO_INIT),
38       mHandle(nullptr),
39       mDestroyPlugin(nullptr),
40       mPlugin(nullptr) {
41     mHandle = dlopen(pluginPath, RTLD_NOW | RTLD_NODELETE);
42     if (!mHandle) {
43         LOG(DEBUG) << "FilterPlugin: no plugin detected";
44         return;
45     }
46     GetFilterPluginVersionFunc getVersion =
47         (GetFilterPluginVersionFunc)dlsym(mHandle, "GetFilterPluginVersion");
48     if (!getVersion) {
49         LOG(WARNING) << "FilterPlugin: GetFilterPluginVersion undefined";
50         return;
51     }
52     int32_t version = getVersion();
53     if (version != FilterPlugin_V1::VERSION) {
54         LOG(WARNING) << "FilterPlugin: unrecognized version (" << version << ")";
55         return;
56     }
57     CreateFilterPluginFunc createPlugin =
58         (CreateFilterPluginFunc)dlsym(mHandle, "CreateFilterPlugin");
59     if (!createPlugin) {
60         LOG(WARNING) << "FilterPlugin: CreateFilterPlugin undefined";
61         return;
62     }
63     mDestroyPlugin =
64         (DestroyFilterPluginFunc)dlsym(mHandle, "DestroyFilterPlugin");
65     if (!mDestroyPlugin) {
66         LOG(WARNING) << "FilterPlugin: DestroyFilterPlugin undefined";
67         return;
68     }
69     mPlugin = (FilterPlugin_V1 *)createPlugin();
70     if (!mPlugin) {
71         LOG(WARNING) << "FilterPlugin: CreateFilterPlugin returned nullptr";
72         return;
73     }
74     mStore = mPlugin->getComponentStore();
75     if (!mStore) {
76         LOG(WARNING) << "FilterPlugin: FilterPlugin_V1::getComponentStore returned nullptr";
77         return;
78     }
79     mInit = OK;
80 }
81 
~DefaultFilterPlugin()82 DefaultFilterPlugin::~DefaultFilterPlugin() {
83     if (mHandle) {
84         if (mDestroyPlugin && mPlugin) {
85             mDestroyPlugin(mPlugin);
86             mPlugin = nullptr;
87         }
88         dlclose(mHandle);
89         mHandle = nullptr;
90         mDestroyPlugin = nullptr;
91     }
92 }
93 
describe(C2String name,FilterWrapper::Descriptor * desc)94 bool DefaultFilterPlugin::describe(C2String name, FilterWrapper::Descriptor *desc) {
95     if (mInit != OK) {
96         return false;
97     }
98     return mPlugin->describe(name, desc);
99 }
100 
isFilteringEnabled(const std::shared_ptr<C2ComponentInterface> & intf)101 bool DefaultFilterPlugin::isFilteringEnabled(const std::shared_ptr<C2ComponentInterface> &intf) {
102     if (mInit != OK) {
103         return false;
104     }
105     return mPlugin->isFilteringEnabled(intf);
106 }
107 
queryParamsForPreviousComponent(const std::shared_ptr<C2ComponentInterface> & intf,std::vector<std::unique_ptr<C2Param>> * params)108 c2_status_t DefaultFilterPlugin::queryParamsForPreviousComponent(
109         const std::shared_ptr<C2ComponentInterface> &intf,
110         std::vector<std::unique_ptr<C2Param>> *params) {
111     if (mInit != OK) {
112         return C2_NO_INIT;
113     }
114     return mPlugin->queryParamsForPreviousComponent(intf, params);
115 }
116 
117 }  // namespace android
118