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 "hilog/log_c.h"
19 #include "hilog/log_cpp.h"
20 #include "log_tags.h"
21 #include "map"
22 #include "new"
23 #include "platform_adp.h"
24 #include "plugin_common_type.h"
25 #include "plugin_errors.h"
26 #include "plugin_fw.h"
27 #include "singleton.h"
28 #include "string"
29 #include "type_traits"
30 #include "vector"
31
32 namespace OHOS {
33 namespace MultimediaPlugin {
34 using std::map;
35 using std::string;
36 using std::vector;
37 using namespace OHOS::HiviewDFX;
38
39 static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_TAG_DOMAIN_ID_PLUGIN, "PluginServer" };
40
Register(vector<string> && pluginPaths)41 uint32_t PluginServer::Register(vector<string> &&pluginPaths)
42 {
43 if (pluginPaths.empty()) {
44 return pluginFw_.Register(pluginPaths);
45 }
46 vector<string> canonicalPaths;
47 vector<string> gstCanonicalPaths;
48 for (string &path : pluginPaths) {
49 if (platformAdp_.CheckAndNormalizePath(path) != SUCCESS) {
50 HiLog::Error(LABEL, "failed to check and normalize path: %{public}s.", path.c_str());
51 continue;
52 }
53
54 PluginFWType fwType = AnalyzeFWType(path);
55 switch (fwType) {
56 case PluginFWType::PLUGIN_FW_GENERAL: {
57 // directory path parameters, usually not too much, will not cause massive logs.
58 HiLog::Debug(LABEL, "PluginFW path: %{public}s.", path.c_str());
59 canonicalPaths.push_back(std::move(path));
60 break;
61 }
62 case PluginFWType::PLUGIN_FW_GSTREAMER: {
63 // directory path parameters, usually not too much, will not cause massive logs.
64 HiLog::Debug(LABEL, "GstPluginFW path: %{public}s.", path.c_str());
65 gstCanonicalPaths.push_back(std::move(path));
66 break;
67 }
68 default: {
69 HiLog::Error(LABEL, "unknown FWType: %{public}d.", fwType);
70 }
71 }
72 }
73
74 if (canonicalPaths.empty() && gstCanonicalPaths.empty()) {
75 HiLog::Error(LABEL, "failed to find any valid plugin path.");
76 return ERR_INVALID_PARAMETER;
77 }
78
79 if (!gstCanonicalPaths.empty()) {
80 uint32_t result = gstPluginFw_.Register(gstCanonicalPaths);
81 if (result != SUCCESS) {
82 HiLog::Error(LABEL, "failed to register gst plugin path, ERRNO: %{public}u.", result);
83 return result;
84 }
85 }
86
87 if (!canonicalPaths.empty()) {
88 uint32_t result = pluginFw_.Register(canonicalPaths);
89 if (result != SUCCESS) {
90 HiLog::Error(LABEL, "failed to register plugin path, ERRNO: %{public}u.", result);
91 return result;
92 }
93 }
94
95 return SUCCESS;
96 }
97
98 // ------------------------------- private method -------------------------------
PluginServer()99 PluginServer::PluginServer()
100 : platformAdp_(DelayedRefSingleton<PlatformAdp>::GetInstance()),
101 pluginFw_(DelayedRefSingleton<PluginFw>::GetInstance()),
102 gstPluginFw_(DelayedRefSingleton<GstPluginFw>::GetInstance()) {}
103
~PluginServer()104 PluginServer::~PluginServer() {}
105
CreateObject(uint16_t interfaceID,const string & className,uint32_t & errorCode)106 PluginClassBase *PluginServer::CreateObject(uint16_t interfaceID, const string &className, uint32_t &errorCode)
107 {
108 HiLog::Debug(LABEL, "create object iid: %{public}u, className: %{public}s.", interfaceID, className.c_str());
109 PluginClassBase *obj = nullptr;
110 // if it is a pipeline service, use the gstreamer framework first.
111 if (GetInterfaceIDType(interfaceID) == IID_TYPE_PIPELINE) {
112 HiLog::Debug(LABEL, "it is a pipeline interface type.");
113 obj = gstPluginFw_.CreateObject(interfaceID, className, errorCode);
114 if (obj != nullptr) {
115 return obj;
116 }
117 }
118
119 obj = pluginFw_.CreateObject(interfaceID, className, errorCode);
120 return obj;
121 }
122
CreateObject(uint16_t interfaceID,uint16_t serviceType,const map<string,AttrData> & capabilities,const PriorityScheme & priorityScheme,uint32_t & errorCode)123 PluginClassBase *PluginServer::CreateObject(uint16_t interfaceID, uint16_t serviceType,
124 const map<string, AttrData> &capabilities,
125 const PriorityScheme &priorityScheme, uint32_t &errorCode)
126 {
127 HiLog::Debug(LABEL, "create object iid: %{public}hu, service Type: %{public}u.", interfaceID, serviceType);
128 PluginClassBase *obj = nullptr;
129 // if it is a pipeline service, use the gstreamer framework first.
130 if (GetInterfaceIDType(interfaceID) == IID_TYPE_PIPELINE) {
131 HiLog::Debug(LABEL, "it is a pipeline interface type.");
132 obj = gstPluginFw_.CreateObject(interfaceID, serviceType, capabilities, priorityScheme, errorCode);
133 if (obj != nullptr) {
134 return obj;
135 }
136 }
137
138 obj = pluginFw_.CreateObject(interfaceID, serviceType, capabilities, priorityScheme, errorCode);
139 return obj;
140 }
141
PluginServerGetClassInfo(uint16_t interfaceID,uint16_t serviceType,const map<std::string,AttrData> & capabilities,vector<ClassInfo> & classesInfo)142 uint32_t PluginServer::PluginServerGetClassInfo(uint16_t interfaceID, uint16_t serviceType,
143 const map<std::string, AttrData> &capabilities,
144 vector<ClassInfo> &classesInfo)
145 {
146 if (!classesInfo.empty()) {
147 classesInfo.clear();
148 }
149
150 uint32_t resultGst = ERR_MATCHING_PLUGIN;
151 // if it is a pipeline service, use the gstreamer framework first.
152 if (GetInterfaceIDType(interfaceID) == IID_TYPE_PIPELINE) {
153 resultGst = gstPluginFw_.GstPluginFwGetClassInfo(interfaceID, serviceType, capabilities, classesInfo);
154 }
155
156 // if the previous process has added classesInfo, the effect here is to append some other classesInfo.
157 uint32_t resultFw = pluginFw_.PluginFwGetClassInfo(interfaceID, serviceType, capabilities, classesInfo);
158
159 // if both gstreamer and self-developing plugin can not get class information, then considered fail.
160 if ((resultGst != SUCCESS) && (resultFw != SUCCESS)) {
161 HiLog::Error(LABEL, "failed to get class by serviceType, resultGst: %{public}u, resultFw: %{public}u.",
162 resultGst, resultFw);
163 return resultFw;
164 }
165
166 return SUCCESS;
167 }
168
AnalyzeFWType(const string & canonicalPath)169 PluginFWType PluginServer::AnalyzeFWType(const string &canonicalPath)
170 {
171 // for the current rule, contains the word "/gstreamer" is considered to be the gstreamer plugin directory.
172 if (canonicalPath.find(platformAdp_.DIR_SEPARATOR + "gstreamer") != string::npos) {
173 return PluginFWType::PLUGIN_FW_GSTREAMER;
174 }
175
176 return PluginFWType::PLUGIN_FW_GENERAL;
177 }
178 } // namespace MultimediaPlugin
179 } // namespace OHOS
180