1 /*
2 * Copyright (c) 2021-2022 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 #ifndef OHOS_CAMERA_UTIL_H
17 #define OHOS_CAMERA_UTIL_H
18 #define EXPORT_API __attribute__((visibility("default")))
19
20 #include <array>
21 #include <map>
22 #include <cstdint>
23 #include <iostream>
24 #include <iterator>
25 #include <limits.h>
26 #include <malloc.h>
27 #include <sstream>
28 #include <vector>
29
30 #include "camera_metadata_info.h"
31 #include "safe_map.h"
32 #include "surface_type.h"
33 #include "v1_0/types.h"
34 #include "v1_1/types.h"
35 #include "v1_2/types.h"
36 #include "v1_3/types.h"
37 #include "camera_hdi_const.h"
38
39 namespace OHOS {
40 namespace CameraStandard {
41 class HStreamCommon;
42 static const int32_t STREAM_ROTATE_0 = 0;
43 static const int32_t STREAM_ROTATE_90 = 90;
44 static const int32_t STREAM_ROTATE_180 = 180;
45 static const int32_t STREAM_ROTATE_270 = 270;
46 static const int32_t STREAM_ROTATE_360 = 360;
47 static const int32_t DISPALY_ROTATE_0 = 0;
48 static const int32_t DISPALY_ROTATE_1 = 1;
49 static const int32_t DISPALY_ROTATE_2 = 2;
50 static const int32_t DISPALY_ROTATE_3 = 3;
51 static const std::string OHOS_PERMISSION_CAMERA = "ohos.permission.CAMERA";
52 static const std::string OHOS_PERMISSION_MICROPHONE = "ohos.permission.MICROPHONE";
53 static const std::string OHOS_PERMISSION_MANAGE_CAMERA_CONFIG = "ohos.permission.MANAGE_CAMERA_CONFIG";
54 static const std::string OHOS_PERMISSION_CAMERA_CONTROL = "ohos.permission.CAMERA_CONTROL";
55 static const std::string SYSTEM_CAMERA = "com.huawei.hmos.camera";
56 static const std::string CLIENT_USER_ID = "clientUserId";
57 static const std::string CAMERA_ID = "cameraId";
58 static const std::string CAMERA_STATE = "cameraState";
59 static const std::string IS_SYSTEM_CAMERA = "isSystemCamera";
60 static const std::string CAMERA_MSG = "cameraMsg";
61 static const std::string CLIENT_NAME = "clientName";
62 // event
63 static const std::string COMMON_EVENT_CAMERA_STATUS = "usual.event.CAMERA_STATUS";
64 static const std::string COMMON_EVENT_DATA_SHARE_READY = "usual.event.DATA_SHARE_READY";
65 static const std::string COMMON_EVENT_SCREEN_LOCKED = "usual.event.SCREEN_LOCKED";
66 static const std::string COMMON_EVENT_SCREEN_UNLOCKED = "usual.event.SCREEN_UNLOCKED";
67 static const std::string COMMON_EVENT_RSS_MULTI_WINDOW_TYPE = "common.event.ressched.window.state";
68 #ifdef NOTIFICATION_ENABLE
69 // beauty notification
70 static const std::string EVENT_CAMERA_BEAUTY_NOTIFICATION = "CAMERA_BEAUTY_NOTIFICATION";
71 static const std::string BEAUTY_NOTIFICATION_ACTION_PARAM = "currentFlag";
72 static const int32_t BEAUTY_STATUS_OFF = 0;
73 static const int32_t BEAUTY_STATUS_ON = 1;
74 static const int32_t BEAUTY_LEVEL = 100;
75 #endif
76 // camera control center
77 static const int32_t CONTROL_CENTER_DATA_SIZE = 3;
78 static const int32_t CONTROL_CENTER_STATUS_INDEX = 0;
79 static const int32_t CONTROL_CENTER_BEAUTY_INDEX = 1;
80 static const int32_t CONTROL_CENTER_APERTURE_INDEX = 2;
81 static const int32_t CONTROL_CENTER_DATA_PRECISION = 9;
82
83 enum CamType {
84 SYSTEM = 0,
85 OTHER
86 };
87
88 enum CamStatus {
89 CAMERA_OPEN = 0,
90 CAMERA_CLOSE
91 };
92
93 enum CamServiceError {
94 CAMERA_OK = 0,
95 CAMERA_ALLOC_ERROR,
96 CAMERA_INVALID_ARG,
97 CAMERA_UNSUPPORTED,
98 CAMERA_DEVICE_BUSY,
99 CAMERA_DEVICE_CLOSED,
100 CAMERA_DEVICE_REQUEST_TIMEOUT,
101 CAMERA_STREAM_BUFFER_LOST,
102 CAMERA_INVALID_SESSION_CFG,
103 CAMERA_CAPTURE_LIMIT_EXCEED,
104 CAMERA_INVALID_STATE,
105 CAMERA_UNKNOWN_ERROR,
106 CAMERA_DEVICE_PREEMPTED,
107 CAMERA_OPERATION_NOT_ALLOWED,
108 CAMERA_DEVICE_ERROR,
109 CAMERA_NO_PERMISSION,
110 CAMERA_DEVICE_CONFLICT,
111 CAMERA_DEVICE_FATAL_ERROR,
112 CAMERA_DEVICE_DRIVER_ERROR,
113 CAMERA_DEVICE_DISCONNECT,
114 CAMERA_DEVICE_SENSOR_DATA_ERROR,
115 CAMERA_DCAMERA_ERROR_BEGIN,
116 CAMERA_DCAMERA_ERROR_DEVICE_IN_USE,
117 CAMERA_DCAMERA_ERROR_NO_PERMISSION,
118 CAMERA_SESSION_MAX_INSTANCE_NUMBER_REACHED,
119 CAMERA_DEVICE_SWITCH_FREQUENT,
120 CAMERA_DEVICE_LENS_RETRACTED,
121 };
122
123 enum ClientPriorityLevels {
124 PRIORITY_LEVEL_SAME = 0,
125 PRIORITY_LEVEL_LOWER,
126 PRIORITY_LEVEL_HIGHER
127 };
128
129 enum DeviceType {
130 FALLING_TYPE = 1,
131 FOLD_TYPE,
132 RSS_MULTI_WINDOW_TYPE
133 };
134
135 enum FallingState {
136 FALLING_STATE = 1008,
137 };
138
139 enum VideoCodecType : int32_t {
140 VIDEO_ENCODE_TYPE_AVC = 0,
141 VIDEO_ENCODE_TYPE_HEVC,
142 };
143
144 extern std::unordered_map<int32_t, int32_t> g_cameraToPixelFormat;
145 extern std::map<int, std::string> g_cameraPos;
146 extern std::map<int, std::string> g_cameraType;
147 extern std::map<int, std::string> g_cameraConType;
148 extern std::map<int, std::string> g_cameraFormat;
149 extern std::map<int, std::string> g_cameraFocusMode;
150 extern std::map<int, std::string> g_cameraQualityPrioritization;
151 extern std::map<int, std::string> g_cameraExposureMode;
152 extern std::map<int, std::string> g_cameraFlashMode;
153 extern std::map<int, std::string> g_cameraVideoStabilizationMode;
154 extern std::map<int, std::string> g_cameraPrelaunchAvailable;
155 extern std::map<int, std::string> g_cameraQuickThumbnailAvailable;
156 extern bool g_cameraDebugOn;
157
DisableJeMalloc()158 inline void DisableJeMalloc()
159 {
160 #ifdef CONFIG_USE_JEMALLOC_DFX_INTF
161 mallopt(M_SET_THREAD_CACHE, M_THREAD_CACHE_DISABLE);
162 mallopt(M_DELAYED_FREE, M_DELAYED_FREE_DISABLE);
163 mallopt(M_FLUSH_THREAD_CACHE, 0);
164 #endif
165 }
166
167 int32_t HdiToCameraErrorType(OHOS::HDI::Camera::V1_3::ErrorType type);
168
169 EXPORT_API int32_t HdiToServiceError(OHOS::HDI::Camera::V1_0::CamRetCode ret);
170
171 int32_t HdiToServiceErrorV1_2(HDI::Camera::V1_2::CamRetCode ret);
172
173 EXPORT_API std::string CreateMsg(const char* format, ...);
174
175 bool IsHapTokenId(uint32_t tokenId);
176
177 bool IsValidMode(int32_t opMode, std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility);
178
179 void DumpMetadata(std::shared_ptr<OHOS::Camera::CameraMetadata> cameraSettings);
180
181 std::string GetClientBundle(int uid);
182
183 std::string GetClientNameByToken(int tokenIdNum);
184
185 int32_t JudgmentPriority(const pid_t& pid, const pid_t& pidCompared);
186
187 bool IsSameClient(const pid_t& pid, const pid_t& pidCompared);
188
189 bool IsInForeGround(const uint32_t callerToken);
190
191 EXPORT_API bool IsCameraNeedClose(const uint32_t callerToken, const pid_t& pid, const pid_t& pidCompared);
192
193 EXPORT_API int32_t CheckPermission(std::string permissionName, uint32_t callerToken);
194
195 void AddCameraPermissionUsedRecord(const uint32_t callingTokenId, const std::string permissionName);
196
197 int32_t GetStreamRotation(int32_t& sensorOrientation, camera_position_enum_t& cameraPosition, int& disPlayRotation,
198 std::string& deviceClass);
199
200 bool CheckSystemApp();
201
202 std::vector<std::string> SplitString(const std::string& input, char delimiter);
203
204 int64_t GetTimestamp();
205
IsCameraDebugOn()206 inline bool IsCameraDebugOn()
207 {
208 return g_cameraDebugOn;
209 }
210
SetCameraDebugValue(bool value)211 inline void SetCameraDebugValue(bool value)
212 {
213 g_cameraDebugOn = value;
214 }
215
216 template<typename T>
CastStream(sptr<HStreamCommon> streamCommon)217 sptr<T> CastStream(sptr<HStreamCommon> streamCommon)
218 {
219 if (streamCommon == nullptr) {
220 return nullptr;
221 }
222 return static_cast<T*>(streamCommon.GetRefPtr());
223 }
224
225 template<typename Iter>
226 using return_container_iter_string_value =
227 typename std::enable_if<std::is_convertible<typename std::iterator_traits<Iter>::value_type,
228 typename std::iterator_traits<Iter>::value_type>::value,
229 std::string>::type;
230
231 template<typename Iter>
Container2String(Iter first,Iter last)232 return_container_iter_string_value<Iter> Container2String(Iter first, Iter last)
233 {
234 std::stringstream stringStream;
235 stringStream << "[";
236 bool isFirstElement = true;
237 while (first != last) {
238 if (isFirstElement) {
239 stringStream << *first;
240 isFirstElement = false;
241 } else {
242 stringStream << "," << *first;
243 }
244 first++;
245 }
246 stringStream << "]";
247 return stringStream.str();
248 }
249 bool IsVerticalDevice();
250 std::string GetFileStream(const std::string &filepath);
251 std::vector<std::string> SplitStringWithPattern(const std::string &str, const char& pattern);
252 void TrimString(std::string &inputStr);
253 bool RemoveFile(const std::string& path);
254 bool CheckPathExist(const char *path);
255
256 bool isIntegerRegex(const std::string& input);
257 std::string GetValidCameraId(std::string& cameraId);
258 std::string ControlCenterMapToString(const std::map<std::string, std::array<float, CONTROL_CENTER_DATA_SIZE>> &data);
259 std::map<std::string, std::array<float, CONTROL_CENTER_DATA_SIZE>> StringToControlCenterMap(const std::string& str);
260 } // namespace CameraStandard
261 } // namespace OHOS
262 #endif // OHOS_CAMERA_UTIL_H
263