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     {"PARAMETER_ERROR", 401},
230     {"INVALID_ARGUMENT", 7400101},
231     {"OPERATION_NOT_ALLOWED", 7400102},
232     {"SESSION_NOT_CONFIG", 7400103},
233     {"SESSION_NOT_RUNNING", 7400104},
234     {"SESSION_CONFIG_LOCKED", 7400105},
235     {"DEVICE_SETTING_LOCKED", 7400106},
236     {"CONFLICT_CAMERA", 7400107},
237     {"DEVICE_DISABLED", 7400108},
238     {"DEVICE_PREEMPTED", 7400109},
239     {"UNRESOLVED_CONFLICTS_BETWEEN_STREAMS", 7400110},
240     {"SERVICE_FATAL_ERROR", 7400201}
241 };
242 
243 static const std::unordered_map<std::string, int32_t> mapCameraInputErrorCode = {
244     {"ERROR_UNKNOWN", -1},
245     {"ERROR_NO_PERMISSION", 0},
246     {"ERROR_DEVICE_PREEMPTED", 1},
247     {"ERROR_DEVICE_DISCONNECTED", 2},
248     {"ERROR_DEVICE_IN_USE", 3},
249     {"ERROR_DRIVER_ERROR", 4}
250 };
251 
252 static const std::unordered_map<std::string, int32_t> mapCaptureSessionErrorCode = {
253     {"ERROR_UNKNOWN", -1},
254     {"ERROR_INSUFFICIENT_RESOURCES", 0},
255     {"ERROR_TIMEOUT", 1}
256 };
257 
258 static const std::unordered_map<std::string, int32_t> mapPreviewOutputErrorCode = {
259     {"ERROR_UNKNOWN", -1}
260 };
261 
262 static const std::unordered_map<std::string, int32_t> mapPhotoOutputErrorCode = {
263     {"ERROR_UNKNOWN", -1},
264     {"ERROR_DRIVER_ERROR", 0},
265     {"ERROR_INSUFFICIENT_RESOURCES", 1},
266     {"ERROR_TIMEOUT", 2}
267 };
268 
269 static const std::unordered_map<std::string, int32_t> mapVideoOutputErrorCode = {
270     {"ERROR_UNKNOWN", -1},
271     {"ERROR_DRIVER_ERROR", 0}
272 };
273 
274 static const std::unordered_map<std::string, int32_t> mapMetadataObjectType = {
275     {"FACE_DETECTION", 0},
276     {"HUMAN_BODY", 1},
277     {"CAT_FACE", 2},
278     {"CAT_BODY", 3},
279     {"DOG_FACE", 4},
280     {"DOG_BODY", 5},
281     {"SALIENT_DETECTION", 6},
282     {"BAR_CODE_DETECTION", 7}
283 };
284 
285 static const std::unordered_map<std::string, int32_t> mapMetaFaceEmotion = {
286     {"NEUTRAL", 0},
287     {"SADNESS", 1},
288     {"SMILE", 2},
289     {"SURPRISE", 3}
290 };
291 
292 static const std::unordered_map<std::string, int32_t> mapMetadataOutputErrorCode = {
293     {"ERROR_UNKNOWN", -1},
294     {"ERROR_INSUFFICIENT_RESOURCES", 0}
295 };
296 
297 static const std::unordered_map<std::string, int32_t> mapDeferredDeliveryImageType = {
298     {"NONE", 0},
299     {"PHOTO", 1},
300     {"VIDEO", 2},
301 };
302 
303 static const std::unordered_map<std::string, int32_t> mapSmoothZoomMode = {
304     {"NORMAL", 0},
305 };
306 
307 static const std::unordered_map<std::string, int32_t> mapColorEffectType = {
308     {"NORMAL", 0},
309     {"BRIGHT", 1},
310     {"SOFT", 2},
311     {"BLACK_WHITE", 3},
312 };
313 
314 static const std::unordered_map<std::string, int32_t> mapRestoreParamType = {
315     {"NO_NEED_RESTORE_PARAM", 0},
316     {"PERSISTENT_DEFAULT_PARAM", 1},
317     {"TRANSIENT_ACTIVE_PARAM", 2},
318 };
319 
320 static const std::unordered_map<std::string, int32_t> mapExposureMeteringMode = {
321     {"MATRIX", 0},
322     {"CENTER", 1},
323     {"SPOT", 2},
324 };
325 
326 static const std::unordered_map<std::string, int32_t> mapEffectSuggestionType = {
327     {"EFFECT_SUGGESTION_NONE", 0},
328     {"EFFECT_SUGGESTION_PORTRAIT", 1},
329     {"EFFECT_SUGGESTION_FOOD", 2},
330     {"EFFECT_SUGGESTION_SKY", 3},
331     {"EFFECT_SUGGESTION_SUNRISE_SUNSET", 4},
332 };
333 
334 static const std::unordered_map<std::string, int32_t> mapPolicyType = {
335     {"EDM", 0},
336     {"PRIVACY", 1},
337 };
338 
339 static const std::unordered_map<std::string, int32_t> mapSceneFeatureType = {
340     { "MOON_CAPTURE_BOOST", FEATURE_MOON_CAPTURE_BOOST },
341     { "TRIPOD_DETECTION", FEATURE_TRIPOD_DETECTION },
342     { "LOW_LIGHT_BOOST", FEATURE_LOW_LIGHT_BOOST },
343     { "MACRO", FEATURE_MACRO },
344 };
345 
346 static const std::unordered_map<std::string, int32_t> mapFoldStatus = {
347     {"NON_FOLDABLE", 0},
348     {"EXPANDED", 1},
349     {"FOLDED", 2}
350 };
351 
352 static const std::unordered_map<std::string, int32_t> mapLightPaintingType = {
353     {"TRAFFIC_TRAILS", 0},
354     {"STAR_TRAILS", 1},
355     {"SILKY_WATER", 2},
356     {"LIGHT_GRAFFITI", 3},
357 };
358 
359 static const std::unordered_map<std::string, int32_t> mapTimeLapseRecordState = {
360     {"IDLE", 0},
361     {"RECORDING", 1},
362 };
363 
364 static const std::unordered_map<std::string, int32_t> mapTimeLapsePreviewType = {
365     {"DARK", 1},
366     {"LIGHT", 2},
367 };
368 
369 static const std::unordered_map<std::string, int32_t> mapVideoCodecType = {
370     {"AVC", VideoCodecType::VIDEO_ENCODE_TYPE_AVC},
371     {"HEVC", VideoCodecType::VIDEO_ENCODE_TYPE_HEVC},
372 };
373 
374 static const std::unordered_map<std::string, int32_t> mapVideoMetaType = {
375     {"VIDEO_META_DEBUG_INFO", 0},
376 };
377 
378 static const std::unordered_map<std::string, int32_t> mapTripodStatus = {
379     { "INVALID", 0 },
380     { "ACTIVE", 1 },
381     { "ENTER", 2 },
382     { "EXITING", 3 },
383 };
384 
385 static const std::unordered_map<std::string, int32_t> mapUsageType = {
386     {"BOKEH", 0},
387 };
388 
389 static const std::unordered_map<std::string, int32_t> mapPortraitThemeType = {
390     {"NATURAL", 0},
391     {"DELICATE", 1},
392     {"STYLISH", 2},
393 };
394 
395 static const std::unordered_map<std::string, int32_t> mapQualityPrioritization = {
396     {"HIGH_QUALITY", 0},
397     {"POWER_BALANCE", 1},
398 };
399 
400 enum CreateAsyncCallbackModes {
401     CREATE_CAMERA_MANAGER_ASYNC_CALLBACK = 10,
402 };
403 
404 class CameraNapi {
405 public:
406     static napi_value Init(napi_env env, napi_value exports);
407     napi_ref GetErrorCallbackRef();
408 
409     CameraNapi();
410     ~CameraNapi();
411 
412     static void CameraNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint);
413     static napi_status AddNamedProperty(napi_env env, napi_value object,
414                                         const std::string name, int32_t enumValue);
415     static napi_value Construct(napi_env env, napi_callback_info info);
416     static napi_value CameraNapiConstructor(napi_env env, napi_callback_info info);
417 
418     static napi_value CreateCameraManagerInstance(napi_env env, napi_callback_info info);
419     static napi_value CreateModeManagerInstance(napi_env env, napi_callback_info info);
420 
421     static napi_value CreateObjectWithMap(napi_env env,
422                                           const std::string objectName,
423                                           const std::unordered_map<std::string, int32_t>& inputMap,
424                                           napi_ref& outputRef);
425 
426 private:
427     static thread_local napi_ref sConstructor_;
428     napi_env env_;
429 };
430 } // namespace CameraStandard
431 } // namespace OHOS
432 #endif /* CAMERA_NAPI_H_ */
433