1 /*
2 * Copyright (c) 2022-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 "devicestatus_napi_manager.h"
17 #include <dlfcn.h>
18 #include <cstdlib>
19 #include "fi_log.h"
20
21 #undef LOG_TAG
22 #define LOG_TAG "BoomerangAlgoManager"
23
24 namespace OHOS {
25 namespace Msdp {
26 namespace DeviceStatus {
27
28 void* BoomerangAlgoManager::boomerangAlgoHandle_ = nullptr;
29 EncodeImageFunc BoomerangAlgoManager::boomerangAlgoEncodeImageHandle_ = nullptr;
30 DecodeImageFunc BoomerangAlgoManager::boomerangAlgoDecodeImageHandle_ = nullptr;
31
32 const std::string BOOMERANG_ALGO_SO_PATH = "system/lib64/libmsdp_boomerang_algo.z.so";
33
EncodeImage(std::shared_ptr<Media::PixelMap> & pixelMap,std::string & content,std::shared_ptr<Media::PixelMap> & resultPixelMap)34 bool BoomerangAlgoManager::EncodeImage(std::shared_ptr<Media::PixelMap> &pixelMap, std::string &content,
35 std::shared_ptr<Media::PixelMap> &resultPixelMap)
36 {
37 FI_HILOGI("Boomerang Algo Encode Load");
38 char realPath[PATH_MAX] = {};
39 if (realpath(BOOMERANG_ALGO_SO_PATH.c_str(), realPath) == nullptr) {
40 FI_HILOGE("Path is error, path is %{private}s", BOOMERANG_ALGO_SO_PATH.c_str());
41 return false;
42 }
43 if (boomerangAlgoHandle_ == nullptr) {
44 boomerangAlgoHandle_ = dlopen(realPath, RTLD_LAZY);
45 char *error = nullptr;
46 if (((error = dlerror()) != nullptr) || (boomerangAlgoHandle_ == nullptr)) {
47 FI_HILOGE("Boomerang Algo Encode Load failed, error: %{public}s", error);
48 return false;
49 }
50 }
51 if (boomerangAlgoEncodeImageHandle_ == nullptr) {
52 boomerangAlgoEncodeImageHandle_ = reinterpret_cast<EncodeImageFunc>(dlsym(boomerangAlgoHandle_, "EncodeImage"));
53 char *error = nullptr;
54 if ((error = dlerror()) != nullptr) {
55 FI_HILOGE("Boomerang Algo Encode find symbol failed, error: %{public}s", error);
56 return false;
57 }
58 }
59 boomerangAlgoEncodeImageHandle_(pixelMap, content, resultPixelMap);
60 FI_HILOGI("Boomerang Algo Encode success");
61 return true;
62 }
63
DecodeImage(std::shared_ptr<Media::PixelMap> & pixelMap,std::string & content)64 bool BoomerangAlgoManager::DecodeImage(std::shared_ptr<Media::PixelMap> &pixelMap, std::string &content)
65 {
66 FI_HILOGI("Boomerang Algo Decode Load");
67 char realPath[PATH_MAX] = {};
68 if (realpath(BOOMERANG_ALGO_SO_PATH.c_str(), realPath) == nullptr) {
69 FI_HILOGE("Path is error, path is %{private}s", BOOMERANG_ALGO_SO_PATH.c_str());
70 return false;
71 }
72 if (boomerangAlgoHandle_ == nullptr) {
73 boomerangAlgoHandle_ = dlopen(realPath, RTLD_LAZY);
74 char *error = nullptr;
75 if (((error = dlerror()) != nullptr) || (boomerangAlgoHandle_ == nullptr)) {
76 FI_HILOGE("Boomerang Algo Decode Load failed, error: %{public}s", error);
77 return false;
78 }
79 }
80 if (boomerangAlgoDecodeImageHandle_ == nullptr) {
81 boomerangAlgoDecodeImageHandle_ = reinterpret_cast<DecodeImageFunc>(dlsym(boomerangAlgoHandle_, "DecodeImage"));
82 char *error = nullptr;
83 if (((error = dlerror()) != nullptr) || (boomerangAlgoDecodeImageHandle_ == nullptr)) {
84 FI_HILOGE("Boomerang Algo Decode find symbol failed, error: %{public}s", error);
85 return false;
86 }
87 }
88 boomerangAlgoDecodeImageHandle_(pixelMap, content);
89 FI_HILOGI("Boomerang Algo Decode success");
90 return true;
91 }
92
~BoomerangAlgoManager()93 BoomerangAlgoManager::~BoomerangAlgoManager()
94 {
95 FI_HILOGI("Boomerang Algo Unload");
96 if (boomerangAlgoHandle_ != nullptr) {
97 dlclose(boomerangAlgoHandle_);
98 boomerangAlgoHandle_ = nullptr;
99 FI_HILOGI("Remove boomerangAlgoHandle_");
100 }
101 boomerangAlgoEncodeImageHandle_ = nullptr;
102 boomerangAlgoDecodeImageHandle_ = nullptr;
103 }
104
EncodeImage(std::shared_ptr<Media::PixelMap> & pixelMap,std::string & content,std::shared_ptr<Media::PixelMap> & resultPixelMap)105 void BoomerangAlgoImpl::EncodeImage(std::shared_ptr<Media::PixelMap> &pixelMap, std::string &content,
106 std::shared_ptr<Media::PixelMap> &resultPixelMap)
107 {
108 FI_HILOGI("Boomerang Enter BoomerangAlgoImpl Encode Image");
109 if (BoomerangAlgoManager::EncodeImage(pixelMap, content, resultPixelMap) != true) {
110 resultPixelMap = pixelMap;
111 FI_HILOGE("BoomerangAlgoImpl Encode fail");
112 }
113 if (resultPixelMap == nullptr) {
114 resultPixelMap = pixelMap;
115 FI_HILOGE("BoomerangAlgoImpl Encode image is empty");
116 }
117 FI_HILOGI("BoomerangAlgoImpl Encode Success");
118 }
119
DecodeImage(std::shared_ptr<Media::PixelMap> & pixelMap,std::string & content)120 void BoomerangAlgoImpl::DecodeImage(std::shared_ptr<Media::PixelMap> &pixelMap, std::string &content)
121 {
122 BoomerangAlgoManager::DecodeImage(pixelMap, content);
123 return;
124 }
125 } // namespace DeviceStatus
126 } // namespace Msdp
127 } // namespace OHOS