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 vector<string> canonicalPaths;
44 vector<string> gstCanonicalPaths;
45 for (string &path : pluginPaths) {
46 if (platformAdp_.CheckAndNormalizePath(path) != SUCCESS) {
47 HiLog::Error(LABEL, "failed to check and normalize path: %{public}s.", path.c_str());
48 continue;
49 }
50
51 PluginFWType fwType = AnalyzeFWType(path);
52 switch (fwType) {
53 case PluginFWType::PLUGIN_FW_GENERAL: {
54 // directory path parameters, usually not too much, will not cause massive logs.
55 HiLog::Debug(LABEL, "PluginFW path: %{public}s.", path.c_str());
56 canonicalPaths.push_back(std::move(path));
57 break;
58 }
59 case PluginFWType::PLUGIN_FW_GSTREAMER: {
60 // directory path parameters, usually not too much, will not cause massive logs.
61 HiLog::Debug(LABEL, "GstPluginFW path: %{public}s.", path.c_str());
62 gstCanonicalPaths.push_back(std::move(path));
63 break;
64 }
65 default: {
66 HiLog::Error(LABEL, "unknown FWType: %{public}d.", fwType);
67 }
68 }
69 }
70
71 if (canonicalPaths.empty() && gstCanonicalPaths.empty()) {
72 HiLog::Error(LABEL, "failed to find any valid plugin path.");
73 return ERR_INVALID_PARAMETER;
74 }
75
76 if (!gstCanonicalPaths.empty()) {
77 uint32_t result = gstPluginFw_.Register(gstCanonicalPaths);
78 if (result != SUCCESS) {
79 HiLog::Error(LABEL, "failed to register gst plugin path, ERRNO: %{public}u.", result);
80 return result;
81 }
82 }
83
84 if (!canonicalPaths.empty()) {
85 uint32_t result = pluginFw_.Register(canonicalPaths);
86 if (result != SUCCESS) {
87 HiLog::Error(LABEL, "failed to register plugin path, ERRNO: %{public}u.", result);
88 return result;
89 }
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 HiLog::Debug(LABEL, "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 HiLog::Debug(LABEL, "it is a pipeline interface type.");
110 obj = gstPluginFw_.CreateObject(interfaceID, className, errorCode);
111 if (obj != nullptr) {
112 return obj;
113 }
114 }
115
116 obj = pluginFw_.CreateObject(interfaceID, className, errorCode);
117 return obj;
118 }
119
CreateObject(uint16_t interfaceID,uint16_t serviceType,const map<string,AttrData> & capabilities,const PriorityScheme & priorityScheme,uint32_t & errorCode)120 PluginClassBase *PluginServer::CreateObject(uint16_t interfaceID, uint16_t serviceType,
121 const map<string, AttrData> &capabilities,
122 const PriorityScheme &priorityScheme, uint32_t &errorCode)
123 {
124 HiLog::Debug(LABEL, "create object iid: %{public}hu, service Type: %{public}u.", interfaceID, serviceType);
125 PluginClassBase *obj = nullptr;
126 // if it is a pipeline service, use the gstreamer framework first.
127 if (GetInterfaceIDType(interfaceID) == IID_TYPE_PIPELINE) {
128 HiLog::Debug(LABEL, "it is a pipeline interface type.");
129 obj = gstPluginFw_.CreateObject(interfaceID, serviceType, capabilities, priorityScheme, errorCode);
130 if (obj != nullptr) {
131 return obj;
132 }
133 }
134
135 obj = pluginFw_.CreateObject(interfaceID, serviceType, capabilities, priorityScheme, errorCode);
136 return obj;
137 }
138
PluginServerGetClassInfo(uint16_t interfaceID,uint16_t serviceType,const map<std::string,AttrData> & capabilities,vector<ClassInfo> & classesInfo)139 uint32_t PluginServer::PluginServerGetClassInfo(uint16_t interfaceID, uint16_t serviceType,
140 const map<std::string, AttrData> &capabilities,
141 vector<ClassInfo> &classesInfo)
142 {
143 if (!classesInfo.empty()) {
144 classesInfo.clear();
145 }
146
147 uint32_t resultGst = ERR_MATCHING_PLUGIN;
148 // if it is a pipeline service, use the gstreamer framework first.
149 if (GetInterfaceIDType(interfaceID) == IID_TYPE_PIPELINE) {
150 resultGst = gstPluginFw_.GstPluginFwGetClassInfo(interfaceID, serviceType, capabilities, classesInfo);
151 }
152
153 // if the previous process has added classesInfo, the effect here is to append some other classesInfo.
154 uint32_t resultFw = pluginFw_.PluginFwGetClassInfo(interfaceID, serviceType, capabilities, classesInfo);
155
156 // if both gstreamer and self-developing plugin can not get class information, then considered fail.
157 if ((resultGst != SUCCESS) && (resultFw != SUCCESS)) {
158 HiLog::Error(LABEL, "failed to get class by serviceType, resultGst: %{public}u, resultFw: %{public}u.",
159 resultGst, resultFw);
160 return resultFw;
161 }
162
163 return SUCCESS;
164 }
165
AnalyzeFWType(const string & canonicalPath)166 PluginFWType PluginServer::AnalyzeFWType(const string &canonicalPath)
167 {
168 // for the current rule, contains the word "/gstreamer" is considered to be the gstreamer plugin directory.
169 if (canonicalPath.find(platformAdp_.DIR_SEPARATOR + "gstreamer") != string::npos) {
170 return PluginFWType::PLUGIN_FW_GSTREAMER;
171 }
172
173 return PluginFWType::PLUGIN_FW_GENERAL;
174 }
175 } // namespace MultimediaPlugin
176 } // namespace OHOS
177