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