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