1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "plugin_server.h"
17 #include "gst_plugin_fw.h"
18 #include "image_log.h"
19 #include "map"
20 #include "new"
21 #include "platform_adp.h"
22 #include "plugin_common_type.h"
23 #include "plugin_errors.h"
24 #include "plugin_fw.h"
25 #include "singleton.h"
26 #include "string"
27 #include "type_traits"
28 #include "vector"
29
30 #undef LOG_DOMAIN
31 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_PLUGIN
32
33 #undef LOG_TAG
34 #define LOG_TAG "PluginServer"
35
36 namespace OHOS {
37 namespace MultimediaPlugin {
38 using std::map;
39 using std::string;
40 using std::vector;
41
Register(vector<string> && pluginPaths)42 uint32_t PluginServer::Register(vector<string> &&pluginPaths)
43 {
44 if (pluginPaths.empty()) {
45 return pluginFw_.Register(pluginPaths);
46 }
47 vector<string> canonicalPaths;
48 vector<string> gstCanonicalPaths;
49 for (string &path : pluginPaths) {
50 if (platformAdp_.CheckAndNormalizePath(path) != SUCCESS) {
51 IMAGE_LOGE("failed to check and normalize path: %{public}s.", path.c_str());
52 continue;
53 }
54
55 PluginFWType fwType = AnalyzeFWType(path);
56 switch (fwType) {
57 case PluginFWType::PLUGIN_FW_GENERAL: {
58 // directory path parameters, usually not too much, will not cause massive logs.
59 IMAGE_LOGD("PluginFW path: %{public}s.", path.c_str());
60 canonicalPaths.push_back(std::move(path));
61 break;
62 }
63 case PluginFWType::PLUGIN_FW_GSTREAMER: {
64 // directory path parameters, usually not too much, will not cause massive logs.
65 IMAGE_LOGD("GstPluginFW path: %{public}s.", path.c_str());
66 gstCanonicalPaths.push_back(std::move(path));
67 break;
68 }
69 default: {
70 IMAGE_LOGE("unknown FWType: %{public}d.", fwType);
71 }
72 }
73 }
74
75 if (canonicalPaths.empty() && gstCanonicalPaths.empty()) {
76 IMAGE_LOGE("failed to find any valid plugin path.");
77 return ERR_INVALID_PARAMETER;
78 }
79
80 if (!gstCanonicalPaths.empty()) {
81 uint32_t result = gstPluginFw_.Register(gstCanonicalPaths);
82 CHECK_ERROR_RETURN_RET_LOG(result != SUCCESS, result,
83 "failed to register gst plugin path, ERRNO: %{public}u.", result);
84 }
85
86 if (!canonicalPaths.empty()) {
87 uint32_t result = pluginFw_.Register(canonicalPaths);
88 bool cond = result != SUCCESS;
89 CHECK_ERROR_RETURN_RET_LOG(cond, result, "failed to register plugin path, ERRNO: %{public}u.", result);
90 }
91
92 return SUCCESS;
93 }
94
95 // ------------------------------- private method -------------------------------
PluginServer()96 PluginServer::PluginServer()
97 : platformAdp_(DelayedRefSingleton<PlatformAdp>::GetInstance()),
98 pluginFw_(DelayedRefSingleton<PluginFw>::GetInstance()),
99 gstPluginFw_(DelayedRefSingleton<GstPluginFw>::GetInstance()) {}
100
~PluginServer()101 PluginServer::~PluginServer() {}
102
CreateObject(uint16_t interfaceID,const string & className,uint32_t & errorCode)103 PluginClassBase *PluginServer::CreateObject(uint16_t interfaceID, const string &className, uint32_t &errorCode)
104 {
105 IMAGE_LOGD("create object iid: %{public}u, className: %{public}s.", interfaceID, className.c_str());
106 PluginClassBase *obj = nullptr;
107 // if it is a pipeline service, use the gstreamer framework first.
108 if (GetInterfaceIDType(interfaceID) == IID_TYPE_PIPELINE) {
109 IMAGE_LOGD("it is a pipeline interface type.");
110 obj = gstPluginFw_.CreateObject(interfaceID, className, errorCode);
111 bool cond = obj != nullptr;
112 CHECK_ERROR_RETURN_RET(cond, obj);
113 }
114
115 obj = pluginFw_.CreateObject(interfaceID, className, errorCode);
116 return obj;
117 }
118
CreateObject(uint16_t interfaceID,uint16_t serviceType,const map<string,AttrData> & capabilities,const PriorityScheme & priorityScheme,uint32_t & errorCode)119 PluginClassBase *PluginServer::CreateObject(uint16_t interfaceID, uint16_t serviceType,
120 const map<string, AttrData> &capabilities,
121 const PriorityScheme &priorityScheme, uint32_t &errorCode)
122 {
123 IMAGE_LOGD("create object iid: %{public}hu, service Type: %{public}u.", interfaceID, serviceType);
124 PluginClassBase *obj = nullptr;
125 // if it is a pipeline service, use the gstreamer framework first.
126 if (GetInterfaceIDType(interfaceID) == IID_TYPE_PIPELINE) {
127 IMAGE_LOGD("it is a pipeline interface type.");
128 obj = gstPluginFw_.CreateObject(interfaceID, serviceType, capabilities, priorityScheme, errorCode);
129 CHECK_ERROR_RETURN_RET(obj != nullptr, obj);
130 }
131
132 obj = pluginFw_.CreateObject(interfaceID, serviceType, capabilities, priorityScheme, errorCode);
133 return obj;
134 }
135
PluginServerGetClassInfo(uint16_t interfaceID,uint16_t serviceType,const map<std::string,AttrData> & capabilities,vector<ClassInfo> & classesInfo)136 uint32_t PluginServer::PluginServerGetClassInfo(uint16_t interfaceID, uint16_t serviceType,
137 const map<std::string, AttrData> &capabilities,
138 vector<ClassInfo> &classesInfo)
139 {
140 if (!classesInfo.empty()) {
141 classesInfo.clear();
142 }
143
144 uint32_t resultGst = ERR_MATCHING_PLUGIN;
145 // if it is a pipeline service, use the gstreamer framework first.
146 if (GetInterfaceIDType(interfaceID) == IID_TYPE_PIPELINE) {
147 resultGst = gstPluginFw_.GstPluginFwGetClassInfo(interfaceID, serviceType, capabilities, classesInfo);
148 }
149
150 // if the previous process has added classesInfo, the effect here is to append some other classesInfo.
151 uint32_t resultFw = pluginFw_.PluginFwGetClassInfo(interfaceID, serviceType, capabilities, classesInfo);
152
153 // if both gstreamer and self-developing plugin can not get class information, then considered fail.
154 if ((resultGst != SUCCESS) && (resultFw != SUCCESS)) {
155 IMAGE_LOGE("failed to get class by serviceType, resultGst: %{public}u, resultFw: %{public}u.",
156 resultGst, resultFw);
157 return resultFw;
158 }
159
160 return SUCCESS;
161 }
162
AnalyzeFWType(const string & canonicalPath)163 PluginFWType PluginServer::AnalyzeFWType(const string &canonicalPath)
164 {
165 // for the current rule, contains the word "/gstreamer" is considered to be the gstreamer plugin directory.
166 if (canonicalPath.find(platformAdp_.DIR_SEPARATOR + "gstreamer") != string::npos) {
167 return PluginFWType::PLUGIN_FW_GSTREAMER;
168 }
169
170 return PluginFWType::PLUGIN_FW_GENERAL;
171 }
172 } // namespace MultimediaPlugin
173 } // namespace OHOS
174