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 CAMERA_NAPI_H_ 17 #define CAMERA_NAPI_H_ 18 19 #include "camera_log.h" 20 #include "napi/native_api.h" 21 #include "napi/native_node_api.h" 22 #include "hilog/log.h" 23 #include "camera_napi_utils.h" 24 #include "output/camera_output_capability.h" 25 #include "output/metadata_output_napi.h" 26 27 #include "input/camera_manager.h" 28 #include "output/capture_output.h" 29 #include "session/capture_session.h" 30 #include "input/capture_input.h" 31 32 #include "input/camera_input_napi.h" 33 #include "input/camera_manager_napi.h" 34 #include "mode/mode_manager_napi.h" 35 #include "output/preview_output_napi.h" 36 #include "output/photo_output_napi.h" 37 #include "output/video_output_napi.h" 38 #include "session/camera_session_napi.h" 39 #include "output/metadata_output_napi.h" 40 41 #include <cinttypes> 42 #include <fstream> 43 #include <iostream> 44 #include <sstream> 45 #include <vector> 46 47 #include <fcntl.h> 48 #include <sys/stat.h> 49 #include <sys/time.h> 50 #include <sys/types.h> 51 52 namespace OHOS { 53 namespace CameraStandard { 54 struct CamRecorderCallback; 55 56 static const char CAMERA_LIB_NAPI_CLASS_NAME[] = "camera"; 57 // Photo default size 58 static const std::int32_t PHOTO_DEFAULT_WIDTH = 1280; 59 static const std::int32_t PHOTO_DEFAULT_HEIGHT = 960; 60 61 // Surface default size 62 static const std::int32_t SURFACE_DEFAULT_WIDTH = 640; 63 static const std::int32_t SURFACE_DEFAULT_HEIGHT = 480; 64 65 // Preview default size 66 static const std::int32_t PREVIEW_DEFAULT_WIDTH = 640; 67 static const std::int32_t PREVIEW_DEFAULT_HEIGHT = 480; 68 69 // Video default size 70 static const std::int32_t VIDEO_DEFAULT_WIDTH = 640; 71 static const std::int32_t VIDEO_DEFAULT_HEIGHT = 360; 72 73 static const std::int32_t SURFACE_QUEUE_SIZE = 10; 74 75 static const std::int32_t CAM_FORMAT_RGBA_8888 = 3; 76 static const std::int32_t CAM_FORMAT_JPEG = 2000; 77 static const std::int32_t CAM_FORMAT_YUV_420_SP = 1003; 78 79 static const std::vector<std::string> vecFlashMode { 80 "FLASH_MODE_CLOSE", "FLASH_MODE_OPEN", "FLASH_MODE_AUTO", "FLASH_MODE_ALWAYS_OPEN" 81 }; 82 83 static const std::vector<std::string> vecExposureMode { 84 "EXPOSURE_MODE_LOCKED", "EXPOSURE_MODE_AUTO", "EXPOSURE_MODE_CONTINUOUS_AUTO" 85 }; 86 87 static const std::vector<std::string> vecFocusMode { 88 "FOCUS_MODE_MANUAL", "FOCUS_MODE_CONTINUOUS_AUTO", "FOCUS_MODE_AUTO", "FOCUS_MODE_LOCKED" 89 }; 90 91 static const std::vector<std::string> vecCameraPositionMode { 92 "CAMERA_POSITION_UNSPECIFIED", "CAMERA_POSITION_BACK", "CAMERA_POSITION_FRONT" 93 }; 94 95 static const std::vector<std::string> vecCameraTypeMode { 96 "CAMERA_TYPE_DEFAULT", "CAMERA_TYPE_WIDE_ANGLE", "CAMERA_TYPE_ULTRA_WIDE", 97 "CAMERA_TYPE_TELEPHOTO", "CAMERA_TYPE_TRUE_DEPTH" 98 }; 99 100 static const std::vector<std::string> vecConnectionTypeMode { 101 "CAMERA_CONNECTION_BUILT_IN", "CAMERA_CONNECTION_USB_PLUGIN", "CAMERA_CONNECTION_REMOTE" 102 }; 103 104 static const std::vector<std::string> vecCameraFormat { 105 "CAMERA_FORMAT_YUV_420_SP", "CAMERA_FORMAT_JPEG", "CAMERA_FORMAT_RGBA_8888" 106 }; 107 108 static const std::vector<std::string> vecCameraStatus { 109 "CAMERA_STATUS_APPEAR", "CAMERA_STATUS_DISAPPEAR", "CAMERA_STATUS_AVAILABLE", "CAMERA_STATUS_UNAVAILABLE" 110 }; 111 112 static const std::vector<std::string> vecVideoStabilizationMode { 113 "OFF", "LOW", "MIDDLE", "HIGH", "AUTO" 114 }; 115 116 static const std::unordered_map<std::string, int32_t> mapImageRotation = { 117 {"ROTATION_0", 0}, 118 {"ROTATION_90", 90}, 119 {"ROTATION_180", 180}, 120 {"ROTATION_270", 270}, 121 }; 122 123 static const std::unordered_map<std::string, int32_t> mapQualityLevel = { 124 {"QUALITY_LEVEL_HIGH", 0}, 125 {"QUALITY_LEVEL_MEDIUM", 1}, 126 {"QUALITY_LEVEL_LOW", 2}, 127 }; 128 129 static const std::unordered_map<std::string, int32_t> mapFocusState = { 130 {"FOCUS_STATE_SCAN", 0}, 131 {"FOCUS_STATE_FOCUSED", 1}, 132 {"FOCUS_STATE_UNFOCUSED", 2}, 133 }; 134 135 static const std::unordered_map<std::string, int32_t> mapHostNameType = { 136 {"UNKNOWN", 0x00}, 137 {"PHONE", 0x0E}, 138 {"TABLE", 0x11}, 139 }; 140 141 static const std::unordered_map<std::string, int32_t> mapExposureState = { 142 {"EXPOSURE_STATE_SCAN", 0}, 143 {"EXPOSURE_STATE_CONVERGED", 1}, 144 }; 145 146 static const std::unordered_map<std::string, int32_t> mapCameraErrorCode = { 147 {"NO_SYSTEM_APP_PERMISSION", 202}, 148 {"INVALID_ARGUMENT", 7400101}, 149 {"OPERATION_NOT_ALLOWED", 7400102}, 150 {"SESSION_NOT_CONFIG", 7400103}, 151 {"SESSION_NOT_RUNNING", 7400104}, 152 {"SESSION_CONFIG_LOCKED", 7400105}, 153 {"DEVICE_SETTING_LOCKED", 7400106}, 154 {"CONFLICT_CAMERA", 7400107}, 155 {"DEVICE_DISABLED", 7400108}, 156 {"DEVICE_PREEMPTED", 7400109}, 157 {"SERVICE_FATAL_ERROR", 7400201} 158 }; 159 160 static const std::unordered_map<std::string, int32_t> mapCameraInputErrorCode = { 161 {"ERROR_UNKNOWN", -1}, 162 {"ERROR_NO_PERMISSION", 0}, 163 {"ERROR_DEVICE_PREEMPTED", 1}, 164 {"ERROR_DEVICE_DISCONNECTED", 2}, 165 {"ERROR_DEVICE_IN_USE", 3}, 166 {"ERROR_DRIVER_ERROR", 4} 167 }; 168 169 static const std::unordered_map<std::string, int32_t> mapCaptureSessionErrorCode = { 170 {"ERROR_UNKNOWN", -1}, 171 {"ERROR_INSUFFICIENT_RESOURCES", 0}, 172 {"ERROR_TIMEOUT", 1} 173 }; 174 175 static const std::unordered_map<std::string, int32_t> mapPreviewOutputErrorCode = { 176 {"ERROR_UNKNOWN", -1} 177 }; 178 179 static const std::unordered_map<std::string, int32_t> mapPhotoOutputErrorCode = { 180 {"ERROR_UNKNOWN", -1}, 181 {"ERROR_DRIVER_ERROR", 0}, 182 {"ERROR_INSUFFICIENT_RESOURCES", 1}, 183 {"ERROR_TIMEOUT", 2} 184 }; 185 186 static const std::unordered_map<std::string, int32_t> mapVideoOutputErrorCode = { 187 {"ERROR_UNKNOWN", -1}, 188 {"ERROR_DRIVER_ERROR", 0} 189 }; 190 191 static const std::unordered_map<std::string, int32_t> mapMetadataObjectType = { 192 {"FACE_DETECTION", 0} 193 }; 194 195 static const std::unordered_map<std::string, int32_t> mapMetaOutputErrorCode = { 196 {"ERROR_UNKNOWN", -1}, 197 {"ERROR_INSUFFICIENT_RESOURCES", 0} 198 }; 199 200 enum CreateAsyncCallbackModes { 201 CREATE_CAMERA_MANAGER_ASYNC_CALLBACK = 10, 202 }; 203 204 class CameraNapi { 205 public: 206 static napi_value Init(napi_env env, napi_value exports); 207 napi_ref GetErrorCallbackRef(); 208 209 CameraNapi(); 210 ~CameraNapi(); 211 212 static void CameraNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint); 213 static napi_status AddNamedProperty(napi_env env, napi_value object, 214 const std::string name, int32_t enumValue); 215 static napi_value Construct(napi_env env, napi_callback_info info); 216 static napi_value CameraNapiConstructor(napi_env env, napi_callback_info info); 217 218 static napi_value CreateCameraManagerInstance(napi_env env, napi_callback_info info); 219 static napi_value CreateModeManagerInstance(napi_env env, napi_callback_info info); 220 static napi_value CreateFlashModeObject(napi_env env); 221 static napi_value CreateExposureModeObject(napi_env env); 222 static napi_value CreateFocusModeObject(napi_env env); 223 static napi_value CreateCameraPositionEnum(napi_env env); 224 static napi_value CreateCameraTypeEnum(napi_env env); 225 static napi_value CreateConnectionTypeEnum(napi_env env); 226 static napi_value CreateCameraStatusObject(napi_env env); 227 static napi_value CreateCameraFormatObject(napi_env env); 228 static napi_value CreateImageRotationEnum(napi_env env); 229 static napi_value CreateExposureStateEnum(napi_env env); 230 static napi_value CreateFocusStateEnum(napi_env env); 231 static napi_value CreateQualityLevelEnum(napi_env env); 232 static napi_value CreateVideoStabilizationModeObject(napi_env env); 233 static napi_value CreateHostNameType(napi_env env); 234 235 static napi_value CreateCameraErrorCode(napi_env env); 236 static napi_value CreateCameraInputErrorCode(napi_env env); 237 static napi_value CreateCaptureSessionErrorCode(napi_env env); 238 static napi_value CreatePreviewOutputErrorCode(napi_env env); 239 static napi_value CreatePhotoOutputErrorCode(napi_env env); 240 static napi_value CreateVideoOutputErrorCode(napi_env env); 241 static napi_value CreateMetaOutputErrorCode(napi_env env); 242 static napi_value CreateMetadataObjectType(napi_env env); 243 244 private: 245 static thread_local napi_ref sConstructor_; 246 247 static thread_local napi_ref flashModeRef_; 248 static thread_local napi_ref exposureModeRef_; 249 static thread_local napi_ref exposureStateRef_; 250 static thread_local napi_ref focusModeRef_; 251 static thread_local napi_ref focusStateRef_; 252 static thread_local napi_ref cameraFormatRef_; 253 static thread_local napi_ref cameraStatusRef_; 254 static thread_local napi_ref connectionTypeRef_; 255 static thread_local napi_ref cameraPositionRef_; 256 static thread_local napi_ref cameraTypeRef_; 257 static thread_local napi_ref imageRotationRef_; 258 static thread_local napi_ref qualityLevelRef_; 259 static thread_local napi_ref videoStabilizationModeRef_; 260 static thread_local napi_ref hostNameTypeRef_; 261 262 static thread_local napi_ref errorCameraInputRef_; 263 static thread_local napi_ref errorCaptureSessionRef_; 264 static thread_local napi_ref errorPreviewOutputRef_; 265 static thread_local napi_ref errorPhotoOutputRef_; 266 static thread_local napi_ref errorVideoOutputRef_; 267 static thread_local napi_ref metadataObjectTypeRef_; 268 static thread_local napi_ref errorMetaOutputRef_; 269 napi_env env_; 270 napi_ref wrapper_; 271 }; 272 } // namespace CameraStandard 273 } // namespace OHOS 274 #endif /* CAMERA_NAPI_H_ */ 275