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