1 /*
2 * Copyright (c) 2023-2023 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/plugin_package.h"
17
18 #include <dlfcn.h>
19 #include "common/log.h"
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "PluginPackage" };
23 }
24
25 namespace OHOS {
26 namespace Media {
27 namespace Plugins {
LoadPluginPackage(std::string packageName)28 bool PluginPackage::LoadPluginPackage(std::string packageName)
29 {
30 packageName_ = packageName;
31 static std::string fileSeparator = "/";
32 static std::string soPrefix = "libmedia_plugin_";
33 static std::string soSuffix = ".z.so";
34 std::string libPath = HST_PLUGIN_PATH + fileSeparator + soPrefix + packageName + soSuffix;
35 auto libPathStr = libPath.c_str();
36 soHandler_ = ::dlopen(libPathStr, RTLD_NOW);
37 if (soHandler_ == nullptr) {
38 MEDIA_LOG_E("dlopen failed due to " PUBLIC_LOG_S, ::dlerror());
39 return false;
40 }
41 std::string registerFuncName = "register_" + packageName;
42 std::string unregisterFuncName = "unregister_" + packageName;
43 RegisterFunc registerFunc = nullptr;
44 UnregisterFunc unregisterFunc = nullptr;
45 MEDIA_LOG_I("dlopen registerFuncName " PUBLIC_LOG_S
46 ", unregisterFuncName: " PUBLIC_LOG_S, registerFuncName.c_str(), unregisterFuncName.c_str());
47 registerFunc = (RegisterFunc)(::dlsym(soHandler_, registerFuncName.c_str()));
48 unregisterFunc = (UnregisterFunc)(::dlsym(soHandler_, unregisterFuncName.c_str()));
49 if (registerFunc && unregisterFunc) {
50 registerFunc(shared_from_this());
51 return true;
52 } else {
53 return false;
54 }
55 }
56
UnLoadPluginPackage()57 bool PluginPackage::UnLoadPluginPackage()
58 {
59 (void)soHandler_;
60 soHandler_ = nullptr;
61 return true;
62 }
63
AddPlugin(const PluginDefBase & def)64 Status PluginPackage::AddPlugin(const PluginDefBase& def)
65 {
66 MEDIA_LOG_I("AddPlugin " PUBLIC_LOG_S, def.name.c_str());
67 std::shared_ptr<PluginDefBase> pluginDef = std::make_shared<PluginDefBase>();
68 pluginDef->pluginType = def.pluginType;
69 pluginDef->name = def.name;
70 pluginDef->rank = def.rank;
71 pluginDef->SetCreator(def.GetCreator());
72 pluginDef->SetSniffer(def.GetSniffer());
73 pluginDefList_.push_back(pluginDef);
74 return Status::OK;
75 }
76
GetAllPlugins()77 std::vector<std::shared_ptr<PluginDefBase>> PluginPackage::GetAllPlugins()
78 {
79 return pluginDefList_;
80 }
81
82 } // namespace Plugins
83 } // namespace Media
84 } // namespace OHOS