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 if (result != SUCCESS) {
83 IMAGE_LOGE("failed to register gst plugin path, ERRNO: %{public}u.", result);
84 return result;
85 }
86 }
87
88 if (!canonicalPaths.empty()) {
89 uint32_t result = pluginFw_.Register(canonicalPaths);
90 bool cond = result != SUCCESS;
91 CHECK_ERROR_RETURN_RET_LOG(cond, result, "failed to register plugin path, ERRNO: %{public}u.", result);
92 }
93
94 return SUCCESS;
95 }
96
97 // ------------------------------- private method -------------------------------
PluginServer()98 PluginServer::PluginServer()
99 : platformAdp_(DelayedRefSingleton<PlatformAdp>::GetInstance()),
100 pluginFw_(DelayedRefSingleton<PluginFw>::GetInstance()),
101 gstPluginFw_(DelayedRefSingleton<GstPluginFw>::GetInstance()) {}
102
~PluginServer()103 PluginServer::~PluginServer() {}
104
CreateObject(uint16_t interfaceID,const string & className,uint32_t & errorCode)105 PluginClassBase *PluginServer::CreateObject(uint16_t interfaceID, const string &className, uint32_t &errorCode)
106 {
107 IMAGE_LOGD("create object iid: %{public}u, className: %{public}s.", interfaceID, className.c_str());
108 PluginClassBase *obj = nullptr;
109 // if it is a pipeline service, use the gstreamer framework first.
110 if (GetInterfaceIDType(interfaceID) == IID_TYPE_PIPELINE) {
111 IMAGE_LOGD("it is a pipeline interface type.");
112 obj = gstPluginFw_.CreateObject(interfaceID, className, errorCode);
113 bool cond = obj != nullptr;
114 CHECK_ERROR_RETURN_RET(cond, obj);
115 }
116
117 obj = pluginFw_.CreateObject(interfaceID, className, errorCode);
118 return obj;
119 }
120
CreateObject(uint16_t interfaceID,uint16_t serviceType,const map<string,AttrData> & capabilities,const PriorityScheme & priorityScheme,uint32_t & errorCode)121 PluginClassBase *PluginServer::CreateObject(uint16_t interfaceID, uint16_t serviceType,
122 const map<string, AttrData> &capabilities,
123 const PriorityScheme &priorityScheme, uint32_t &errorCode)
124 {
125 IMAGE_LOGD("create object iid: %{public}hu, service Type: %{public}u.", interfaceID, serviceType);
126 PluginClassBase *obj = nullptr;
127 // if it is a pipeline service, use the gstreamer framework first.
128 if (GetInterfaceIDType(interfaceID) == IID_TYPE_PIPELINE) {
129 IMAGE_LOGD("it is a pipeline interface type.");
130 obj = gstPluginFw_.CreateObject(interfaceID, serviceType, capabilities, priorityScheme, errorCode);
131 if (obj != nullptr) {
132 return obj;
133 }
134 }
135
136 obj = pluginFw_.CreateObject(interfaceID, serviceType, capabilities, priorityScheme, errorCode);
137 return obj;
138 }
139
PluginServerGetClassInfo(uint16_t interfaceID,uint16_t serviceType,const map<std::string,AttrData> & capabilities,vector<ClassInfo> & classesInfo)140 uint32_t PluginServer::PluginServerGetClassInfo(uint16_t interfaceID, uint16_t serviceType,
141 const map<std::string, AttrData> &capabilities,
142 vector<ClassInfo> &classesInfo)
143 {
144 if (!classesInfo.empty()) {
145 classesInfo.clear();
146 }
147
148 uint32_t resultGst = ERR_MATCHING_PLUGIN;
149 // if it is a pipeline service, use the gstreamer framework first.
150 if (GetInterfaceIDType(interfaceID) == IID_TYPE_PIPELINE) {
151 resultGst = gstPluginFw_.GstPluginFwGetClassInfo(interfaceID, serviceType, capabilities, classesInfo);
152 }
153
154 // if the previous process has added classesInfo, the effect here is to append some other classesInfo.
155 uint32_t resultFw = pluginFw_.PluginFwGetClassInfo(interfaceID, serviceType, capabilities, classesInfo);
156
157 // if both gstreamer and self-developing plugin can not get class information, then considered fail.
158 if ((resultGst != SUCCESS) && (resultFw != SUCCESS)) {
159 IMAGE_LOGE("failed to get class by serviceType, resultGst: %{public}u, resultFw: %{public}u.",
160 resultGst, resultFw);
161 return resultFw;
162 }
163
164 return SUCCESS;
165 }
166
AnalyzeFWType(const string & canonicalPath)167 PluginFWType PluginServer::AnalyzeFWType(const string &canonicalPath)
168 {
169 // for the current rule, contains the word "/gstreamer" is considered to be the gstreamer plugin directory.
170 if (canonicalPath.find(platformAdp_.DIR_SEPARATOR + "gstreamer") != string::npos) {
171 return PluginFWType::PLUGIN_FW_GSTREAMER;
172 }
173
174 return PluginFWType::PLUGIN_FW_GENERAL;
175 }
176 } // namespace MultimediaPlugin
177 } // namespace OHOS
178