• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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_loader.h"
17 #include <dlfcn.h>
18 #include "avcodec_log.h"
19 
20 namespace {
21     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "PluginLoader"};
22 }
23 
24 using namespace OHOS::MediaAVCodec::Plugin;
25 
26 // NOLINTNEXTLINE: void*
PluginLoader(void * handler,std::string name)27 PluginLoader::PluginLoader(void* handler, std::string name) : handler_(handler), name_(std::move(name))
28 {
29 }
30 
~PluginLoader()31 PluginLoader::~PluginLoader()
32 {
33     UnLoadPluginFile();
34 }
35 
Create(const std::string & name,const std::string & path)36 std::shared_ptr<PluginLoader> PluginLoader::Create(const std::string& name, const std::string& path)
37 {
38     if (name.empty() || path.empty()) {
39         return {};
40     }
41     return CheckSymbol(LoadPluginFile(path), name);
42 }
43 
FetchRegisterFunction() const44 RegisterFunc PluginLoader::FetchRegisterFunction() const
45 {
46     return registerFunc_;
47 }
48 
FetchUnregisterFunction() const49 UnregisterFunc PluginLoader::FetchUnregisterFunction() const
50 {
51     return unregisterFunc_;
52 }
53 
LoadPluginFile(const std::string & path)54 void* PluginLoader::LoadPluginFile(const std::string& path)
55 {
56     auto ptr = ::dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
57     if (ptr == nullptr) {
58         AVCODEC_LOGE("dlopen failed due to %{public}s", ::dlerror());
59     }
60     return ptr;
61 }
62 
CheckSymbol(void * handler,const std::string & name)63 std::shared_ptr<PluginLoader> PluginLoader::CheckSymbol(void* handler, const std::string& name)
64 {
65     if (handler) {
66         std::string registerFuncName = "register_" + name;
67         std::string unregisterFuncName = "unregister_" + name;
68         RegisterFunc registerFunc = nullptr;
69         UnregisterFunc unregisterFunc = nullptr;
70         AVCODEC_LOGD("CheckSymbol:  registerFuncName %{public}s", registerFuncName.c_str());
71         AVCODEC_LOGD("CheckSymbol:  unregisterFuncName %{public}s", unregisterFuncName.c_str());
72         registerFunc = (RegisterFunc)(::dlsym(handler, registerFuncName.c_str()));
73         unregisterFunc = (UnregisterFunc)(::dlsym(handler, unregisterFuncName.c_str()));
74         if (registerFunc && unregisterFunc) {
75             std::shared_ptr<PluginLoader> loader = std::make_shared<PluginLoader>(handler, name);
76             loader->registerFunc_ = registerFunc;
77             loader->unregisterFunc_ = unregisterFunc;
78             return loader;
79         }
80     }
81     return {};
82 }
83 
UnLoadPluginFile()84 void PluginLoader::UnLoadPluginFile()
85 {
86     if (handler_) {
87         ::dlclose(const_cast<void*>(handler_)); // NOLINT: const_cast
88     }
89 }
90