• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define HST_LOG_TAG "PluginLoader"
17 
18 #include "plugin/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 "common/log.h"
29 
30 using namespace OHOS::Media::Plugins;
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         MEDIA_LOG_I("check symbol, dlopen registerFuncName " PUBLIC_LOG_S
75             ", unregisterFuncName: " PUBLIC_LOG_S, registerFuncName.c_str(), unregisterFuncName.c_str());
76 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
77         registerFunc = (RegisterFunc)(::GetProcAddress((HMODULE)handler, registerFuncName.c_str()));
78         unregisterFunc = (UnregisterFunc)(::GetProcAddress((HMODULE)handler, unregisterFuncName.c_str()));
79 #else
80         registerFunc = (RegisterFunc)(::dlsym(handler, registerFuncName.c_str()));
81         unregisterFunc = (UnregisterFunc)(::dlsym(handler, unregisterFuncName.c_str()));
82 #endif
83         if (registerFunc && unregisterFunc) {
84             std::shared_ptr<PluginLoader> loader(new PluginLoader(), [&] (void*) {
85                 if (handler) {
86 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
87                     ::FreeLibrary((HMODULE)handler);
88 #endif
89                 }
90             });
91             loader->registerFunc_ = registerFunc;
92             loader->unregisterFunc_ = unregisterFunc;
93             return loader;
94         } else {
95             MEDIA_LOG_W("register or unregister found is not found in " PUBLIC_LOG_S, name.c_str());
96         }
97     }
98     return {};
99 }