1 /*
2 * Copyright (c) 2024 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 "dynamic_interface.h"
17 #include <dlfcn.h>
18 #include "post_processing_utils.h"
19
20 namespace {
21 static constexpr const char* LIBRARY_PATH{"libvideoprocessingengine.z.so"};
22 }
23
24 namespace OHOS {
25 namespace MediaAVCodec {
26 namespace PostProcessing {
27
~DynamicInterface()28 DynamicInterface::~DynamicInterface()
29 {
30 Unload();
31 }
32
Load()33 bool DynamicInterface::Load()
34 {
35 CHECK_AND_RETURN_RET_LOG(OpenLibrary(), false, "Load VPE library failed.");
36 AVCODEC_LOGD("Library opened.");
37 CHECK_AND_RETURN_RET_LOG(ReadSymbols(), false, "Read VPE Symbols failed.");
38 AVCODEC_LOGD("Symbols loaded.");
39 return true;
40 }
41
Unload()42 void DynamicInterface::Unload()
43 {
44 ClearSymbols();
45 AVCODEC_LOGD("Symbols cleared.");
46 CloseLibrary();
47 AVCODEC_LOGD("Library closed.");
48 }
49
OpenLibrary()50 bool DynamicInterface::OpenLibrary()
51 {
52 if (lib_ != nullptr) {
53 AVCODEC_LOGI("VPE lib is already loaded.");
54 return true;
55 }
56 lib_ = dlopen(LIBRARY_PATH, RTLD_LAZY);
57 CHECK_AND_RETURN_RET_LOG(lib_ != nullptr, false, "Load VPE lib failed.");
58 return true;
59 }
60
CloseLibrary()61 void DynamicInterface::CloseLibrary()
62 {
63 if (lib_ != nullptr) {
64 dlclose(lib_);
65 lib_ = nullptr;
66 }
67 }
68
ReadSymbols()69 bool DynamicInterface::ReadSymbols()
70 {
71 for (size_t i = 0; i < DYNAMIC_INTERFACE_NUM; ++i) {
72 auto fp = dlsym(lib_, DYNAMIC_INTERFACE_SYMBOLS[i]);
73 CHECK_AND_RETURN_RET_LOG(fp != nullptr, false, "Symbol not found");
74 interfaces_[i] = static_cast<void*>(fp);
75 }
76
77 return true;
78 }
79
ClearSymbols()80 void DynamicInterface::ClearSymbols()
81 {
82 interfaces_.fill(nullptr);
83 }
84
85 } // namespace PostProcessing
86 } // namespace MediaAVCodec
87 } // namespace OHOS