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_napi_utils.h" 20 #include "camera_util.h" 21 #include "capture_scene_const.h" 22 #include "hilog/log.h" 23 #include "input/camera_input_napi.h" 24 #include "input/camera_manager.h" 25 #include "input/camera_manager_napi.h" 26 #include "input/capture_input.h" 27 #include "mode/mode_manager_napi.h" 28 #include "output/camera_output_capability.h" 29 #include "output/capture_output.h" 30 #include "output/metadata_output_napi.h" 31 #include "output/photo_output_napi.h" 32 #include "output/preview_output_napi.h" 33 #include "output/video_output_napi.h" 34 #include "session/camera_session_napi.h" 35 #include "session/capture_session.h" 36 #include <unordered_map> 37 38 namespace OHOS { 39 namespace CameraStandard { 40 struct CamRecorderCallback; 41 42 static const char CAMERA_LIB_NAPI_CLASS_NAME[] = "camera"; 43 // Photo default size 44 static const std::int32_t PHOTO_DEFAULT_WIDTH = 1280; 45 static const std::int32_t PHOTO_DEFAULT_HEIGHT = 960; 46 47 // Surface default size 48 static const std::int32_t SURFACE_DEFAULT_WIDTH = 640; 49 static const std::int32_t SURFACE_DEFAULT_HEIGHT = 480; 50 51 // Preview default size 52 static const std::int32_t PREVIEW_DEFAULT_WIDTH = 640; 53 static const std::int32_t PREVIEW_DEFAULT_HEIGHT = 480; 54 55 // Video default size 56 static const std::int32_t VIDEO_DEFAULT_WIDTH = 640; 57 static const std::int32_t VIDEO_DEFAULT_HEIGHT = 360; 58 59 static const std::int32_t SURFACE_QUEUE_SIZE = 10; 60 61 static const std::unordered_map<std::string, int32_t> mapFlashMode = { 62 {"FLASH_MODE_CLOSE", 0}, 63 {"FLASH_MODE_OPEN", 1}, 64 {"FLASH_MODE_AUTO", 2}, 65 {"FLASH_MODE_ALWAYS_OPEN", 3}, 66 }; 67 68 static const std::unordered_map<std::string, int32_t> mapExposureMode = { 69 {"EXPOSURE_MODE_LOCKED", 0}, 70 {"EXPOSURE_MODE_AUTO", 1}, 71 {"EXPOSURE_MODE_CONTINUOUS_AUTO", 2}, 72 }; 73 74 static const std::unordered_map<std::string, int32_t> mapFocusMode = { 75 {"FOCUS_MODE_MANUAL", 0}, 76 {"FOCUS_MODE_CONTINUOUS_AUTO", 1}, 77 {"FOCUS_MODE_AUTO", 2}, 78 {"FOCUS_MODE_LOCKED", 3}, 79 }; 80 81 static const std::unordered_map<std::string, int32_t> mapCameraPosition = { 82 {"CAMERA_POSITION_UNSPECIFIED", 0}, 83 {"CAMERA_POSITION_BACK", 1}, 84 {"CAMERA_POSITION_FRONT", 2}, 85 {"CAMERA_POSITION_FOLD_INNER", 3}, 86 }; 87 88 static const std::unordered_map<std::string, int32_t> mapCameraType = { 89 {"CAMERA_TYPE_DEFAULT", 0}, 90 {"CAMERA_TYPE_WIDE_ANGLE", 1}, 91 {"CAMERA_TYPE_ULTRA_WIDE", 2}, 92 {"CAMERA_TYPE_TELEPHOTO", 3}, 93 {"CAMERA_TYPE_TRUE_DEPTH", 4}, 94 }; 95 96 static const std::unordered_map<std::string, int32_t> mapConnectionType = { 97 {"CAMERA_CONNECTION_BUILT_IN", 0}, 98 {"CAMERA_CONNECTION_USB_PLUGIN", 1}, 99 {"CAMERA_CONNECTION_REMOTE", 2}, 100 }; 101 102 static const std::unordered_map<std::string, int32_t> mapCameraFormat = { 103 {"CAMERA_FORMAT_YUV_420_SP", CameraFormat::CAMERA_FORMAT_YUV_420_SP}, 104 {"CAMERA_FORMAT_JPEG", CameraFormat::CAMERA_FORMAT_JPEG}, 105 {"CAMERA_FORMAT_RGBA_8888", CameraFormat::CAMERA_FORMAT_RGBA_8888}, 106 {"CAMERA_FORMAT_DNG", CameraFormat::CAMERA_FORMAT_DNG}, 107 {"CAMERA_FORMAT_YCBCR_P010", CameraFormat::CAMERA_FORMAT_YCBCR_P010}, 108 {"CAMERA_FORMAT_YCRCB_P010", CameraFormat::CAMERA_FORMAT_YCRCB_P010}, 109 {"CAMERA_FORMAT_HEIC", CameraFormat::CAMERA_FORMAT_HEIC}, 110 }; 111 112 static const std::unordered_map<std::string, int32_t> mapCameraStatus = { 113 {"CAMERA_STATUS_APPEAR", 0}, 114 {"CAMERA_STATUS_DISAPPEAR", 1}, 115 {"CAMERA_STATUS_AVAILABLE", 2}, 116 {"CAMERA_STATUS_UNAVAILABLE", 3}, 117 }; 118 119 static const std::unordered_map<std::string, int32_t> mapVideoStabilizationMode = { 120 {"OFF", 0}, 121 {"LOW", 1}, 122 {"MIDDLE", 2}, 123 {"HIGH", 3}, 124 {"AUTO", 4}, 125 }; 126 127 static const std::unordered_map<std::string, int32_t> mapImageRotation = { 128 {"ROTATION_0", 0}, 129 {"ROTATION_90", 90}, 130 {"ROTATION_180", 180}, 131 {"ROTATION_270", 270}, 132 }; 133 134 static const std::unordered_map<std::string, int32_t> mapQualityLevel = { 135 {"QUALITY_LEVEL_HIGH", 0}, 136 {"QUALITY_LEVEL_MEDIUM", 1}, 137 {"QUALITY_LEVEL_LOW", 2}, 138 }; 139 140 static const std::unordered_map<std::string, int32_t> mapFocusState = { 141 {"FOCUS_STATE_SCAN", 0}, 142 {"FOCUS_STATE_FOCUSED", 1}, 143 {"FOCUS_STATE_UNFOCUSED", 2}, 144 }; 145 146 static const std::unordered_map<std::string, int32_t> mapHostNameType = { 147 {"UNKNOWN", 0x00}, 148 {"PHONE", 0x0E}, 149 {"TABLE", 0x11}, 150 }; 151 152 static const std::unordered_map<std::string, int32_t> mapExposureState = { 153 {"EXPOSURE_STATE_SCAN", 0}, 154 {"EXPOSURE_STATE_CONVERGED", 1}, 155 }; 156 157 static const std::unordered_map<std::string, int32_t> mapSceneMode = { 158 {"NORMAL", JS_NORMAL}, 159 {"NORMAL_PHOTO", JS_CAPTURE}, 160 {"NORMAL_VIDEO", JS_VIDEO}, 161 {"PORTRAIT", JS_PORTRAIT}, 162 {"PORTRAIT_PHOTO", JS_PORTRAIT}, 163 {"NIGHT", JS_NIGHT}, 164 {"NIGHT_PHOTO", JS_NIGHT}, 165 {"PROFESSIONAL_PHOTO", JS_PROFESSIONAL_PHOTO}, 166 {"PROFESSIONAL_VIDEO", JS_PROFESSIONAL_VIDEO}, 167 {"SLOW_MOTION_VIDEO", JS_SLOW_MOTION}, 168 {"MACRO_PHOTO", JS_CAPTURE_MARCO}, 169 {"MACRO_VIDEO", JS_VIDEO_MARCO}, 170 {"LIGHT_PAINTING_PHOTO", JS_LIGHT_PAINTING }, 171 {"HIGH_RES_PHOTO", JS_HIGH_RES_PHOTO}, 172 {"SECURE_PHOTO", JS_SECURE_CAMERA}, 173 {"QUICK_SHOT_PHOTO", JS_QUICK_SHOT_PHOTO}, 174 {"APERTURE_VIDEO", JS_APERTURE_VIDEO}, 175 {"PANORAMA_PHOTO", JS_PANORAMA_PHOTO}, 176 {"TIME_LAPSE_PHOTO", JS_TIMELAPSE_PHOTO}, 177 {"FLUORESCENCE_PHOTO", JS_FLUORESCENCE_PHOTO}, 178 }; 179 180 static const std::unordered_map<std::string, int32_t> mapPreconfigType = { 181 {"PRECONFIG_720P", PRECONFIG_720P}, 182 {"PRECONFIG_1080P", PRECONFIG_1080P}, 183 {"PRECONFIG_4K", PRECONFIG_4K}, 184 {"PRECONFIG_HIGH_QUALITY", PRECONFIG_HIGH_QUALITY}, 185 }; 186 187 static const std::unordered_map<std::string, int32_t> mapPreconfigRatio = { 188 { "PRECONFIG_RATIO_1_1", ProfileSizeRatio::RATIO_1_1 }, 189 { "PRECONFIG_RATIO_4_3", ProfileSizeRatio::RATIO_4_3 }, 190 { "PRECONFIG_RATIO_16_9", ProfileSizeRatio::RATIO_16_9 }, 191 }; 192 193 static const std::unordered_map<std::string, int32_t> mapFilterType = { 194 {"NONE", 0}, 195 {"CLASSIC", 1}, 196 {"DAWN", 2}, 197 {"PURE", 3}, 198 {"GREY", 4}, 199 {"NATURAL", 5}, 200 {"MORI", 6}, 201 {"FAIR", 7}, 202 {"PINK", 8}, 203 }; 204 205 static const std::unordered_map<std::string, int32_t> mapBeautyType = { 206 {"AUTO", 0}, 207 {"SKIN_SMOOTH", 1}, 208 {"FACE_SLENDER", 2}, 209 {"SKIN_TONE", 3}, 210 }; 211 212 static const std::unordered_map<std::string, int32_t> mapPortraitEffect = { 213 {"OFF", 0}, 214 {"CIRCLES", 1}, 215 {"HEART", 2}, 216 {"ROTATED", 3}, 217 {"STUDIO", 4}, 218 {"THEATER", 5}, 219 }; 220 221 static const std::unordered_map<std::string, int32_t> mapTorchMode = { 222 {"OFF", 0}, 223 {"ON", 1}, 224 {"AUTO", 2}, 225 }; 226 227 static const std::unordered_map<std::string, int32_t> mapCameraErrorCode = { 228 {"NO_SYSTEM_APP_PERMISSION", 202}, 229 {"INVALID_ARGUMENT", 7400101}, 230 {"OPERATION_NOT_ALLOWED", 7400102}, 231 {"SESSION_NOT_CONFIG", 7400103}, 232 {"SESSION_NOT_RUNNING", 7400104}, 233 {"SESSION_CONFIG_LOCKED", 7400105}, 234 {"DEVICE_SETTING_LOCKED", 7400106}, 235 {"CONFLICT_CAMERA", 7400107}, 236 {"DEVICE_DISABLED", 7400108}, 237 {"DEVICE_PREEMPTED", 7400109}, 238 {"UNRESOLVED_CONFLICTS_BETWEEN_STREAMS", 7400110}, 239 {"SERVICE_FATAL_ERROR", 7400201} 240 }; 241 242 static const std::unordered_map<std::string, int32_t> mapCameraInputErrorCode = { 243 {"ERROR_UNKNOWN", -1}, 244 {"ERROR_NO_PERMISSION", 0}, 245 {"ERROR_DEVICE_PREEMPTED", 1}, 246 {"ERROR_DEVICE_DISCONNECTED", 2}, 247 {"ERROR_DEVICE_IN_USE", 3}, 248 {"ERROR_DRIVER_ERROR", 4} 249 }; 250 251 static const std::unordered_map<std::string, int32_t> mapCaptureSessionErrorCode = { 252 {"ERROR_UNKNOWN", -1}, 253 {"ERROR_INSUFFICIENT_RESOURCES", 0}, 254 {"ERROR_TIMEOUT", 1} 255 }; 256 257 static const std::unordered_map<std::string, int32_t> mapPreviewOutputErrorCode = { 258 {"ERROR_UNKNOWN", -1} 259 }; 260 261 static const std::unordered_map<std::string, int32_t> mapPhotoOutputErrorCode = { 262 {"ERROR_UNKNOWN", -1}, 263 {"ERROR_DRIVER_ERROR", 0}, 264 {"ERROR_INSUFFICIENT_RESOURCES", 1}, 265 {"ERROR_TIMEOUT", 2} 266 }; 267 268 static const std::unordered_map<std::string, int32_t> mapVideoOutputErrorCode = { 269 {"ERROR_UNKNOWN", -1}, 270 {"ERROR_DRIVER_ERROR", 0} 271 }; 272 273 static const std::unordered_map<std::string, int32_t> mapMetadataObjectType = { 274 {"FACE_DETECTION", 0}, 275 {"HUMAN_BODY", 1}, 276 {"CAT_FACE", 2}, 277 {"CAT_BODY", 3}, 278 {"DOG_FACE", 4}, 279 {"DOG_BODY", 5}, 280 {"SALIENT_DETECTION", 6}, 281 {"BAR_CODE_DETECTION", 7} 282 }; 283 284 static const std::unordered_map<std::string, int32_t> mapMetaFaceEmotion = { 285 {"NEUTRAL", 0}, 286 {"SADNESS", 1}, 287 {"SMILE", 2}, 288 {"SURPRISE", 3} 289 }; 290 291 static const std::unordered_map<std::string, int32_t> mapMetadataOutputErrorCode = { 292 {"ERROR_UNKNOWN", -1}, 293 {"ERROR_INSUFFICIENT_RESOURCES", 0} 294 }; 295 296 static const std::unordered_map<std::string, int32_t> mapDeferredDeliveryImageType = { 297 {"NONE", 0}, 298 {"PHOTO", 1}, 299 {"VIDEO", 2}, 300 }; 301 302 static const std::unordered_map<std::string, int32_t> mapSmoothZoomMode = { 303 {"NORMAL", 0}, 304 }; 305 306 static const std::unordered_map<std::string, int32_t> mapColorEffectType = { 307 {"NORMAL", 0}, 308 {"BRIGHT", 1}, 309 {"SOFT", 2}, 310 {"BLACK_WHITE", 3}, 311 }; 312 313 static const std::unordered_map<std::string, int32_t> mapRestoreParamType = { 314 {"NO_NEED_RESTORE_PARAM", 0}, 315 {"PERSISTENT_DEFAULT_PARAM", 1}, 316 {"TRANSIENT_ACTIVE_PARAM", 2}, 317 }; 318 319 static const std::unordered_map<std::string, int32_t> mapExposureMeteringMode = { 320 {"MATRIX", 0}, 321 {"CENTER", 1}, 322 {"SPOT", 2}, 323 }; 324 325 static const std::unordered_map<std::string, int32_t> mapEffectSuggestionType = { 326 {"EFFECT_SUGGESTION_NONE", 0}, 327 {"EFFECT_SUGGESTION_PORTRAIT", 1}, 328 {"EFFECT_SUGGESTION_FOOD", 2}, 329 {"EFFECT_SUGGESTION_SKY", 3}, 330 {"EFFECT_SUGGESTION_SUNRISE_SUNSET", 4}, 331 }; 332 333 static const std::unordered_map<std::string, int32_t> mapPolicyType = { 334 {"EDM", 0}, 335 {"PRIVACY", 1}, 336 }; 337 338 static const std::unordered_map<std::string, int32_t> mapSceneFeatureType = { 339 { "MOON_CAPTURE_BOOST", FEATURE_MOON_CAPTURE_BOOST }, 340 { "TRIPOD_DETECTION", FEATURE_TRIPOD_DETECTION }, 341 { "LOW_LIGHT_BOOST", FEATURE_LOW_LIGHT_BOOST }, 342 { "MACRO", FEATURE_MACRO }, 343 }; 344 345 static const std::unordered_map<std::string, int32_t> mapFoldStatus = { 346 {"NON_FOLDABLE", 0}, 347 {"EXPANDED", 1}, 348 {"FOLDED", 2} 349 }; 350 351 static const std::unordered_map<std::string, int32_t> mapLightPaintingType = { 352 {"TRAFFIC_TRAILS", 0}, 353 {"STAR_TRAILS", 1}, 354 {"SILKY_WATER", 2}, 355 {"LIGHT_GRAFFITI", 3}, 356 }; 357 358 static const std::unordered_map<std::string, int32_t> mapTimeLapseRecordState = { 359 {"IDLE", 0}, 360 {"RECORDING", 1}, 361 }; 362 363 static const std::unordered_map<std::string, int32_t> mapTimeLapsePreviewType = { 364 {"DARK", 1}, 365 {"LIGHT", 2}, 366 }; 367 368 static const std::unordered_map<std::string, int32_t> mapVideoCodecType = { 369 {"AVC", VideoCodecType::VIDEO_ENCODE_TYPE_AVC}, 370 {"HEVC", VideoCodecType::VIDEO_ENCODE_TYPE_HEVC}, 371 }; 372 373 static const std::unordered_map<std::string, int32_t> mapVideoMetaType = { 374 {"VIDEO_META_DEBUG_INFO", 0}, 375 }; 376 377 static const std::unordered_map<std::string, int32_t> mapTripodStatus = { 378 { "INVALID", 0 }, 379 { "ACTIVE", 1 }, 380 { "ENTER", 2 }, 381 { "EXITING", 3 }, 382 }; 383 384 static const std::unordered_map<std::string, int32_t> mapUsageType = { 385 {"BOKEH", 0}, 386 }; 387 388 enum CreateAsyncCallbackModes { 389 CREATE_CAMERA_MANAGER_ASYNC_CALLBACK = 10, 390 }; 391 392 class CameraNapi { 393 public: 394 static napi_value Init(napi_env env, napi_value exports); 395 napi_ref GetErrorCallbackRef(); 396 397 CameraNapi(); 398 ~CameraNapi(); 399 400 static void CameraNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint); 401 static napi_status AddNamedProperty(napi_env env, napi_value object, 402 const std::string name, int32_t enumValue); 403 static napi_value Construct(napi_env env, napi_callback_info info); 404 static napi_value CameraNapiConstructor(napi_env env, napi_callback_info info); 405 406 static napi_value CreateCameraManagerInstance(napi_env env, napi_callback_info info); 407 static napi_value CreateModeManagerInstance(napi_env env, napi_callback_info info); 408 409 static napi_value CreateObjectWithMap(napi_env env, 410 const std::string objectName, 411 const std::unordered_map<std::string, int32_t>& inputMap, 412 napi_ref& outputRef); 413 414 private: 415 static thread_local napi_ref sConstructor_; 416 napi_env env_; 417 }; 418 } // namespace CameraStandard 419 } // namespace OHOS 420 #endif /* CAMERA_NAPI_H_ */ 421