1 /*
2 * Copyright (c) 2021-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 #define HST_LOG_TAG "PluginLoader"
17
18 #include "plugin/core/plugin_loader.h"
19
20 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
21 #include <Windows.h>
22 #include <iostream>
23 #include <utility>
24 #else
25 #include <dlfcn.h>
26 #endif
27
28 #include "foundation/log.h"
29
30 using namespace OHOS::Media::Plugin;
31
Create(const std::string & name,const std::string & path)32 std::shared_ptr<PluginLoader> PluginLoader::Create(const std::string& name, const std::string& path)
33 {
34 if (name.empty() || path.empty()) {
35 return {};
36 }
37 return CheckSymbol(LoadPluginFile(path), name);
38 }
39
FetchRegisterFunction()40 RegisterFunc PluginLoader::FetchRegisterFunction()
41 {
42 return registerFunc_;
43 }
44
FetchUnregisterFunction()45 UnregisterFunc PluginLoader::FetchUnregisterFunction()
46 {
47 return unregisterFunc_;
48 }
49
LoadPluginFile(const std::string & path)50 void* PluginLoader::LoadPluginFile(const std::string& path)
51 {
52 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
53 return ::LoadLibraryA(path.c_str());
54 #else
55 auto pathStr = path.c_str();
56 if (pathStr) {
57 auto ptr = ::dlopen(pathStr, RTLD_NOW);
58 if (ptr == nullptr) {
59 MEDIA_LOG_E("dlopen failed due to " PUBLIC_LOG_S, ::dlerror());
60 }
61 return ptr;
62 }
63 return nullptr;
64 #endif
65 }
66
CheckSymbol(void * handler,const std::string & name)67 std::shared_ptr<PluginLoader> PluginLoader::CheckSymbol(void* handler, const std::string& name)
68 {
69 if (handler) {
70 std::string registerFuncName = "register_" + name;
71 std::string unregisterFuncName = "unregister_" + name;
72 RegisterFunc registerFunc = nullptr;
73 UnregisterFunc unregisterFunc = nullptr;
74 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
75 registerFunc = (RegisterFunc)(::GetProcAddress((HMODULE)handler, registerFuncName.c_str()));
76 unregisterFunc = (UnregisterFunc)(::GetProcAddress((HMODULE)handler, unregisterFuncName.c_str()));
77 #else
78 registerFunc = (RegisterFunc)(::dlsym(handler, registerFuncName.c_str()));
79 unregisterFunc = (UnregisterFunc)(::dlsym(handler, unregisterFuncName.c_str()));
80 #endif
81 if (registerFunc && unregisterFunc) {
82 std::shared_ptr<PluginLoader> loader(new PluginLoader(), [&] (void*) {
83 if (handler) {
84 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
85 ::FreeLibrary((HMODULE)handler);
86 #endif
87 }
88 });
89 loader->registerFunc_ = registerFunc;
90 loader->unregisterFunc_ = unregisterFunc;
91 return loader;
92 } else {
93 MEDIA_LOG_W("register or unregister found is not found in " PUBLIC_LOG_S, name.c_str());
94 }
95 }
96 return {};
97 }