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