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