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_cameraQualityPrioritization;
124 extern std::map<int, std::string> g_cameraExposureMode;
125 extern std::map<int, std::string> g_cameraFlashMode;
126 extern std::map<int, std::string> g_cameraVideoStabilizationMode;
127 extern std::map<int, std::string> g_cameraPrelaunchAvailable;
128 extern std::map<int, std::string> g_cameraQuickThumbnailAvailable;
129 extern bool g_cameraDebugOn;
130 
DisableJeMalloc()131 inline void DisableJeMalloc()
132 {
133 #ifdef CONFIG_USE_JEMALLOC_DFX_INTF
134     mallopt(M_SET_THREAD_CACHE, M_THREAD_CACHE_DISABLE);
135     mallopt(M_DELAYED_FREE, M_DELAYED_FREE_DISABLE);
136     mallopt(M_FLUSH_THREAD_CACHE, 0);
137 #endif
138 }
139 
140 int32_t HdiToCameraErrorType(OHOS::HDI::Camera::V1_3::ErrorType type);
141 
142 EXPORT_API int32_t HdiToServiceError(OHOS::HDI::Camera::V1_0::CamRetCode ret);
143 
144 int32_t HdiToServiceErrorV1_2(HDI::Camera::V1_2::CamRetCode ret);
145 
146 EXPORT_API std::string CreateMsg(const char* format, ...);
147 
148 bool IsHapTokenId(uint32_t tokenId);
149 
150 int32_t GetVersionId(uint32_t major, uint32_t minor);
151 
152 bool IsValidMode(int32_t opMode, std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility);
153 
154 void DumpMetadata(std::shared_ptr<OHOS::Camera::CameraMetadata> cameraSettings);
155 
156 std::string GetClientBundle(int uid);
157 
158 int32_t JudgmentPriority(const pid_t& pid, const pid_t& pidCompared);
159 
160 bool IsSameClient(const pid_t& pid, const pid_t& pidCompared);
161 
162 bool IsInForeGround(const uint32_t callerToken);
163 
164 EXPORT_API bool IsCameraNeedClose(const uint32_t callerToken, const pid_t& pid, const pid_t& pidCompared);
165 
166 EXPORT_API int32_t CheckPermission(std::string permissionName, uint32_t callerToken);
167 
168 void AddCameraPermissionUsedRecord(const uint32_t callingTokenId, const std::string permissionName);
169 
170 int32_t GetStreamRotation(int32_t& sensorOrientation, camera_position_enum_t& cameraPosition, int& disPlayRotation,
171     std::string& deviceClass);
172 
173 bool CheckSystemApp();
174 
IsCameraDebugOn()175 inline bool IsCameraDebugOn()
176 {
177     return g_cameraDebugOn;
178 }
179 
SetCameraDebugValue(bool value)180 inline void SetCameraDebugValue(bool value)
181 {
182     g_cameraDebugOn = value;
183 }
184 
185 template<typename T>
CastStream(sptr<HStreamCommon> streamCommon)186 sptr<T> CastStream(sptr<HStreamCommon> streamCommon)
187 {
188     if (streamCommon == nullptr) {
189         return nullptr;
190     }
191     return static_cast<T*>(streamCommon.GetRefPtr());
192 }
193 
194 template<typename Iter>
195 using return_container_iter_string_value =
196     typename std::enable_if<std::is_convertible<typename std::iterator_traits<Iter>::value_type,
197                                 typename std::iterator_traits<Iter>::value_type>::value,
198         std::string>::type;
199 
200 template<typename Iter>
Container2String(Iter first,Iter last)201 return_container_iter_string_value<Iter> Container2String(Iter first, Iter last)
202 {
203     std::stringstream stringStream;
204     stringStream << "[";
205     bool isFirstElement = true;
206     while (first != last) {
207         if (isFirstElement) {
208             stringStream << *first;
209             isFirstElement = false;
210         } else {
211             stringStream << "," << *first;
212         }
213         first++;
214     }
215     stringStream << "]";
216     return stringStream.str();
217 }
218 bool IsVerticalDevice();
219 } // namespace CameraStandard
220 } // namespace OHOS
221 #endif // OHOS_CAMERA_UTIL_H
222