• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "input/camera_napi.h"
17 
18 namespace OHOS {
19 namespace CameraStandard {
20 using namespace std;
21 
22 thread_local napi_ref CameraNapi::sConstructor_ = nullptr;
23 
24 thread_local napi_ref CameraNapi::exposureModeRef_ = nullptr;
25 thread_local napi_ref CameraNapi::focusModeRef_ = nullptr;
26 thread_local napi_ref CameraNapi::flashModeRef_ = nullptr;
27 thread_local napi_ref CameraNapi::cameraFormatRef_ = nullptr;
28 thread_local napi_ref CameraNapi::cameraStatusRef_ = nullptr;
29 thread_local napi_ref CameraNapi::connectionTypeRef_ = nullptr;
30 thread_local napi_ref CameraNapi::cameraPositionRef_ = nullptr;
31 thread_local napi_ref CameraNapi::cameraTypeRef_ = nullptr;
32 thread_local napi_ref CameraNapi::imageRotationRef_ = nullptr;
33 
34 thread_local napi_ref CameraNapi::errorCameraInputRef_ = nullptr;
35 thread_local napi_ref CameraNapi::errorCaptureSessionRef_ = nullptr;
36 thread_local napi_ref CameraNapi::errorPreviewOutputRef_ = nullptr;
37 thread_local napi_ref CameraNapi::errorPhotoOutputRef_ = nullptr;
38 thread_local napi_ref CameraNapi::errorVideoOutputRef_ = nullptr;
39 thread_local napi_ref CameraNapi::errorMetaOutputRef_ = nullptr;
40 thread_local napi_ref CameraNapi::metadataObjectTypeRef_ = nullptr;
41 thread_local napi_ref CameraNapi::exposureStateRef_ = nullptr;
42 thread_local napi_ref CameraNapi::focusStateRef_ = nullptr;
43 thread_local napi_ref CameraNapi::qualityLevelRef_ = nullptr;
44 thread_local napi_ref CameraNapi::videoStabilizationModeRef_ = nullptr;
45 thread_local napi_ref CameraNapi::hostNameTypeRef_ = nullptr;
46 
CameraNapi()47 CameraNapi::CameraNapi() : env_(nullptr), wrapper_(nullptr)
48 {
49 }
50 
~CameraNapi()51 CameraNapi::~CameraNapi()
52 {
53     if (wrapper_ != nullptr) {
54         napi_delete_reference(env_, wrapper_);
55     }
56 }
57 
58 // Constructor callback
CameraNapiConstructor(napi_env env,napi_callback_info info)59 napi_value CameraNapi::CameraNapiConstructor(napi_env env, napi_callback_info info)
60 {
61     MEDIA_DEBUG_LOG("CameraNapiConstructor is called");
62     napi_status status;
63     napi_value result = nullptr;
64     napi_value thisVar = nullptr;
65 
66     napi_get_undefined(env, &result);
67     CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
68 
69     if (status == napi_ok && thisVar != nullptr) {
70         std::unique_ptr<CameraNapi> obj = std::make_unique<CameraNapi>();
71         if (obj != nullptr) {
72             obj->env_ = env;
73             status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
74                                CameraNapi::CameraNapiDestructor, nullptr, nullptr);
75             if (status == napi_ok) {
76                 obj.release();
77                 return thisVar;
78             } else {
79                 MEDIA_ERR_LOG("CameraNapiConstructor Failure wrapping js to native napi");
80             }
81         }
82     }
83     MEDIA_ERR_LOG("CameraNapiConstructor call Failed!");
84     return result;
85 }
86 
CameraNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)87 void CameraNapi::CameraNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
88 {
89     MEDIA_DEBUG_LOG("CameraNapiDestructor is called");
90     CameraNapi* camera = reinterpret_cast<CameraNapi*>(nativeObject);
91     if (camera != nullptr) {
92         delete camera;
93     }
94 }
95 
Init(napi_env env,napi_value exports)96 napi_value CameraNapi::Init(napi_env env, napi_value exports)
97 {
98     MEDIA_DEBUG_LOG("Init is called");
99     napi_status status;
100     napi_value ctorObj;
101     int32_t refCount = 1;
102     napi_property_descriptor camera_properties[] = {
103         DECLARE_NAPI_FUNCTION("getCameraManagerTest", CreateCameraManagerInstance)
104     };
105 
106     napi_property_descriptor camera_static_prop[] = {
107         DECLARE_NAPI_STATIC_FUNCTION("getCameraManager", CreateCameraManagerInstance),
108         DECLARE_NAPI_STATIC_FUNCTION("getModeManager", CreateModeManagerInstance),
109         DECLARE_NAPI_PROPERTY("FlashMode", CreateFlashModeObject(env)),
110         DECLARE_NAPI_PROPERTY("ExposureMode", CreateExposureModeObject(env)),
111         DECLARE_NAPI_PROPERTY("ExposureState", CreateExposureStateEnum(env)),
112         DECLARE_NAPI_PROPERTY("FocusMode", CreateFocusModeObject(env)),
113         DECLARE_NAPI_PROPERTY("FocusState", CreateFocusStateEnum(env)),
114         DECLARE_NAPI_PROPERTY("CameraPosition", CreateCameraPositionEnum(env)),
115         DECLARE_NAPI_PROPERTY("CameraType", CreateCameraTypeEnum(env)),
116         DECLARE_NAPI_PROPERTY("ConnectionType", CreateConnectionTypeEnum(env)),
117         DECLARE_NAPI_PROPERTY("CameraFormat", CreateCameraFormatObject(env)),
118         DECLARE_NAPI_PROPERTY("CameraStatus", CreateCameraStatusObject(env)),
119         DECLARE_NAPI_PROPERTY("ImageRotation", CreateImageRotationEnum(env)),
120         DECLARE_NAPI_PROPERTY("QualityLevel", CreateQualityLevelEnum(env)),
121         DECLARE_NAPI_PROPERTY("CameraErrorCode", CreateCameraErrorCode(env)),
122         DECLARE_NAPI_PROPERTY("CameraInputErrorCode", CreateCameraInputErrorCode(env)),
123         DECLARE_NAPI_PROPERTY("CaptureSessionErrorCode", CreateCaptureSessionErrorCode(env)),
124         DECLARE_NAPI_PROPERTY("PreviewOutputErrorCode", CreatePreviewOutputErrorCode(env)),
125         DECLARE_NAPI_PROPERTY("PhotoOutputErrorCode", CreatePhotoOutputErrorCode(env)),
126         DECLARE_NAPI_PROPERTY("VideoOutputErrorCode", CreateVideoOutputErrorCode(env)),
127         DECLARE_NAPI_PROPERTY("MetadataOutputErrorCode", CreateMetaOutputErrorCode(env)),
128         DECLARE_NAPI_PROPERTY("VideoStabilizationMode", CreateVideoStabilizationModeObject(env)),
129         DECLARE_NAPI_PROPERTY("MetadataObjectType", CreateMetadataObjectType(env)),
130         DECLARE_NAPI_PROPERTY("HostNameType", CreateHostNameType(env)),
131     };
132 
133     status = napi_define_class(env, CAMERA_LIB_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH, CameraNapiConstructor,
134                                nullptr, sizeof(camera_properties) / sizeof(camera_properties[PARAM0]),
135                                camera_properties, &ctorObj);
136     if (status == napi_ok) {
137         if (napi_create_reference(env, ctorObj, refCount, &sConstructor_) == napi_ok) {
138             status = napi_set_named_property(env, exports, CAMERA_LIB_NAPI_CLASS_NAME, ctorObj);
139             if (status == napi_ok && napi_define_properties(env, exports,
140                 sizeof(camera_static_prop) / sizeof(camera_static_prop[PARAM0]), camera_static_prop) == napi_ok) {
141                 return exports;
142             }
143         }
144     }
145     MEDIA_ERR_LOG("Init call Failed!");
146     return nullptr;
147 }
148 
AddNamedProperty(napi_env env,napi_value object,const std::string name,int32_t enumValue)149 napi_status CameraNapi::AddNamedProperty(napi_env env, napi_value object,
150                                          const std::string name, int32_t enumValue)
151 {
152     napi_status status;
153     napi_value enumNapiValue;
154 
155     status = napi_create_int32(env, enumValue, &enumNapiValue);
156     if (status == napi_ok) {
157         status = napi_set_named_property(env, object, name.c_str(), enumNapiValue);
158     }
159 
160     return status;
161 }
162 
CreateCameraManagerInstance(napi_env env,napi_callback_info info)163 napi_value CameraNapi::CreateCameraManagerInstance(napi_env env, napi_callback_info info)
164 {
165     MEDIA_INFO_LOG("CreateCameraManagerInstance is called");
166     napi_value result = nullptr;
167     size_t argc = ARGS_ONE;
168     napi_value argv[ARGS_ONE] = {0};
169     napi_value thisVar = nullptr;
170 
171     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
172 
173     napi_get_undefined(env, &result);
174     result = CameraManagerNapi::CreateCameraManager(env);
175     return result;
176 }
177 
CreateModeManagerInstance(napi_env env,napi_callback_info info)178 napi_value CameraNapi::CreateModeManagerInstance(napi_env env, napi_callback_info info)
179 {
180     MEDIA_INFO_LOG("CreateModeManagerInstance is called");
181     napi_value result = nullptr;
182     size_t argc = ARGS_ONE;
183     napi_value argv[ARGS_ONE] = {0};
184     napi_value thisVar = nullptr;
185 
186     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
187 
188     napi_get_undefined(env, &result);
189     result = ModeManagerNapi::CreateModeManager(env);
190     return result;
191 }
192 
CreateFlashModeObject(napi_env env)193 napi_value CameraNapi::CreateFlashModeObject(napi_env env)
194 {
195     MEDIA_DEBUG_LOG("CreateFlashModeObject is called");
196     napi_value result = nullptr;
197     napi_status status;
198 
199     status = napi_create_object(env, &result);
200     if (status == napi_ok) {
201         std::string propName;
202         for (unsigned int i = 0; i < vecFlashMode.size(); i++) {
203             propName = vecFlashMode[i];
204             status = AddNamedProperty(env, result, propName, i);
205             if (status != napi_ok) {
206                 MEDIA_ERR_LOG("Failed to add named prop for FlashMode!");
207                 break;
208             }
209             propName.clear();
210         }
211     }
212     if (status == napi_ok) {
213         status = napi_create_reference(env, result, 1, &flashModeRef_);
214         if (status == napi_ok) {
215             return result;
216         }
217     }
218     MEDIA_ERR_LOG("CreateFlashModeObject call Failed!");
219     napi_get_undefined(env, &result);
220 
221     return result;
222 }
223 
CreateExposureModeObject(napi_env env)224 napi_value CameraNapi::CreateExposureModeObject(napi_env env)
225 {
226     MEDIA_DEBUG_LOG("CreateExposureModeObject is called");
227     napi_value result = nullptr;
228     napi_status status;
229 
230     status = napi_create_object(env, &result);
231     if (status == napi_ok) {
232         std::string propName;
233         for (unsigned int i = 0; i < vecExposureMode.size(); i++) {
234             propName = vecExposureMode[i];
235             status = AddNamedProperty(env, result, propName, i);
236             if (status != napi_ok) {
237                 MEDIA_ERR_LOG("Failed to add named prop for ExposureMode!");
238                 break;
239             }
240             propName.clear();
241         }
242     }
243     if (status == napi_ok) {
244         status = napi_create_reference(env, result, 1, &exposureModeRef_);
245         if (status == napi_ok) {
246             return result;
247         }
248     }
249     MEDIA_ERR_LOG("CreateExposureModeObject call Failed!");
250     napi_get_undefined(env, &result);
251 
252     return result;
253 }
254 
CreateExposureStateEnum(napi_env env)255 napi_value CameraNapi::CreateExposureStateEnum(napi_env env)
256 {
257     MEDIA_DEBUG_LOG("CreateExposureStateEnum is called");
258     napi_value result = nullptr;
259     napi_status status;
260 
261     status = napi_create_object(env, &result);
262     if (status == napi_ok) {
263         std::string propName;
264         for (auto itr = mapExposureState.begin(); itr != mapExposureState.end(); ++itr) {
265             propName = itr->first;
266             status = AddNamedProperty(env, result, propName, itr->second);
267             if (status != napi_ok) {
268                 MEDIA_ERR_LOG("Failed to add FocusState prop!");
269                 break;
270             }
271             propName.clear();
272         }
273     }
274     if (status == napi_ok) {
275         status = napi_create_reference(env, result, 1, &exposureStateRef_);
276         if (status == napi_ok) {
277             return result;
278         }
279     }
280     MEDIA_ERR_LOG("CreateExposureStateEnum call Failed!");
281     napi_get_undefined(env, &result);
282 
283     return result;
284 }
285 
CreateFocusModeObject(napi_env env)286 napi_value CameraNapi::CreateFocusModeObject(napi_env env)
287 {
288     MEDIA_DEBUG_LOG("CreateFocusModeObject is called");
289     napi_value result = nullptr;
290     napi_status status;
291 
292     status = napi_create_object(env, &result);
293     if (status == napi_ok) {
294         std::string propName;
295         for (unsigned int i = 0; i < vecFocusMode.size(); i++) {
296             propName = vecFocusMode[i];
297             status = AddNamedProperty(env, result, propName, i);
298             if (status != napi_ok) {
299                 MEDIA_ERR_LOG("Failed to add named prop for FocusMode!");
300                 break;
301             }
302             propName.clear();
303         }
304     }
305     if (status == napi_ok) {
306         status = napi_create_reference(env, result, 1, &focusModeRef_);
307         if (status == napi_ok) {
308             return result;
309         }
310     }
311     MEDIA_ERR_LOG("CreateFocusModeObject call Failed!");
312     napi_get_undefined(env, &result);
313 
314     return result;
315 }
316 
CreateFocusStateEnum(napi_env env)317 napi_value CameraNapi::CreateFocusStateEnum(napi_env env)
318 {
319     MEDIA_DEBUG_LOG("CreateFocusStateEnum is called");
320     napi_value result = nullptr;
321     napi_status status;
322 
323     status = napi_create_object(env, &result);
324     if (status == napi_ok) {
325         std::string propName;
326         for (auto itr = mapFocusState.begin(); itr != mapFocusState.end(); ++itr) {
327             propName = itr->first;
328             status = AddNamedProperty(env, result, propName, itr->second);
329             if (status != napi_ok) {
330                 MEDIA_ERR_LOG("Failed to add FocusState prop!");
331                 break;
332             }
333             propName.clear();
334         }
335     }
336     if (status == napi_ok) {
337         status = napi_create_reference(env, result, 1, &focusStateRef_);
338         if (status == napi_ok) {
339             return result;
340         }
341     }
342     MEDIA_ERR_LOG("CreateFocusStateEnum call Failed!");
343     napi_get_undefined(env, &result);
344 
345     return result;
346 }
347 
CreateCameraPositionEnum(napi_env env)348 napi_value CameraNapi::CreateCameraPositionEnum(napi_env env)
349 {
350     MEDIA_DEBUG_LOG("CreateCameraPositionEnum is called");
351     napi_value result = nullptr;
352     napi_status status;
353 
354     status = napi_create_object(env, &result);
355     if (status == napi_ok) {
356         std::string propName;
357         for (unsigned int i = 0; i < vecCameraPositionMode.size(); i++) {
358             propName = vecCameraPositionMode[i];
359             status = AddNamedProperty(env, result, propName, i);
360             if (status != napi_ok) {
361                 MEDIA_ERR_LOG("Failed to add named prop for CameraPosition!");
362                 break;
363             }
364             propName.clear();
365         }
366     }
367     if (status == napi_ok) {
368         status = napi_create_reference(env, result, 1, &cameraPositionRef_);
369         if (status == napi_ok) {
370             return result;
371         }
372     }
373     MEDIA_ERR_LOG("CreateCameraPositionEnum call Failed!");
374     napi_get_undefined(env, &result);
375 
376     return result;
377 }
378 
CreateCameraTypeEnum(napi_env env)379 napi_value CameraNapi::CreateCameraTypeEnum(napi_env env)
380 {
381     MEDIA_DEBUG_LOG("CreateCameraTypeEnum is called");
382     napi_value result = nullptr;
383     napi_status status;
384 
385     status = napi_create_object(env, &result);
386     if (status == napi_ok) {
387         std::string propName;
388         for (unsigned int i = 0; i < vecCameraTypeMode.size(); i++) {
389             propName = vecCameraTypeMode[i];
390             status = AddNamedProperty(env, result, propName, i);
391             if (status != napi_ok) {
392                 MEDIA_ERR_LOG("Failed to add named prop for CameraType!");
393                 break;
394             }
395             propName.clear();
396         }
397     }
398     if (status == napi_ok) {
399         status = napi_create_reference(env, result, 1, &cameraTypeRef_);
400         if (status == napi_ok) {
401             return result;
402         }
403     }
404     MEDIA_ERR_LOG("CreateCameraTypeEnum call Failed!");
405     napi_get_undefined(env, &result);
406 
407     return result;
408 }
409 
CreateConnectionTypeEnum(napi_env env)410 napi_value CameraNapi::CreateConnectionTypeEnum(napi_env env)
411 {
412     MEDIA_DEBUG_LOG("CreateConnectionTypeEnum is called");
413     napi_value result = nullptr;
414     napi_status status;
415 
416     status = napi_create_object(env, &result);
417     if (status == napi_ok) {
418         std::string propName;
419         for (unsigned int i = 0; i < vecConnectionTypeMode.size(); i++) {
420             propName =  vecConnectionTypeMode[i];
421             status = AddNamedProperty(env, result, propName, i);
422             if (status != napi_ok) {
423                 MEDIA_ERR_LOG("Failed to add named prop for CameraPosition!");
424                 break;
425             }
426             propName.clear();
427         }
428     }
429     if (status == napi_ok) {
430         status = napi_create_reference(env, result, 1, &connectionTypeRef_);
431         if (status == napi_ok) {
432             return result;
433         }
434     }
435     MEDIA_ERR_LOG("CreateConnectionTypeEnum call Failed!");
436     napi_get_undefined(env, &result);
437 
438     return result;
439 }
440 
CreateCameraFormatObject(napi_env env)441 napi_value CameraNapi::CreateCameraFormatObject(napi_env env)
442 {
443     MEDIA_DEBUG_LOG("CreateCameraFormatObject is called");
444     napi_value result = nullptr;
445     napi_status status;
446 
447     status = napi_create_object(env, &result);
448     if (status == napi_ok) {
449         std::string propName;
450         for (unsigned int i = 0; i < vecCameraFormat.size(); i++) {
451             propName = vecCameraFormat[i];
452             int32_t value;
453             if (propName.compare("CAMERA_FORMAT_JPEG") == 0) {
454                 value = CAM_FORMAT_JPEG;
455             } else if (propName.compare("CAMERA_FORMAT_YUV_420_SP") == 0) {
456                 value = CAM_FORMAT_YUV_420_SP;
457             } else {
458                 value = CAM_FORMAT_RGBA_8888;
459             }
460             status = AddNamedProperty(env, result, propName, value);
461             if (status != napi_ok) {
462                 MEDIA_ERR_LOG("Failed to add named prop!");
463                 break;
464             }
465             propName.clear();
466         }
467     }
468     if (status == napi_ok) {
469         status = napi_create_reference(env, result, 1, &cameraFormatRef_);
470         if (status == napi_ok) {
471             return result;
472         }
473     }
474     MEDIA_ERR_LOG("CreateCameraFormatObject call Failed!");
475     napi_get_undefined(env, &result);
476 
477     return result;
478 }
479 
CreateCameraStatusObject(napi_env env)480 napi_value CameraNapi::CreateCameraStatusObject(napi_env env)
481 {
482     MEDIA_DEBUG_LOG("CreateCameraStatusObject is called");
483     napi_value result = nullptr;
484     napi_status status;
485 
486     status = napi_create_object(env, &result);
487     if (status == napi_ok) {
488         std::string propName;
489         for (unsigned int i = 0; i < vecCameraStatus.size(); i++) {
490             propName = vecCameraStatus[i];
491             status = AddNamedProperty(env, result, propName, i);
492             if (status != napi_ok) {
493                 MEDIA_ERR_LOG("Failed to add named prop!");
494                 break;
495             }
496             propName.clear();
497         }
498     }
499     if (status == napi_ok) {
500         status = napi_create_reference(env, result, 1, &cameraStatusRef_);
501         if (status == napi_ok) {
502             return result;
503         }
504     }
505     MEDIA_ERR_LOG("CreateCameraStatusObject call Failed!");
506     napi_get_undefined(env, &result);
507 
508     return result;
509 }
510 
CreateImageRotationEnum(napi_env env)511 napi_value CameraNapi::CreateImageRotationEnum(napi_env env)
512 {
513     MEDIA_DEBUG_LOG("CreateImageRotationEnum is called");
514     napi_value result = nullptr;
515     napi_status status;
516 
517     status = napi_create_object(env, &result);
518     if (status == napi_ok) {
519         std::string propName;
520         for (auto itr = mapImageRotation.begin(); itr != mapImageRotation.end(); ++itr) {
521             propName = itr->first;
522             status = AddNamedProperty(env, result, propName, itr->second);
523             if (status != napi_ok) {
524                 MEDIA_ERR_LOG("Failed to add ImageRotation prop!");
525                 break;
526             }
527             propName.clear();
528         }
529     }
530     if (status == napi_ok) {
531         status = napi_create_reference(env, result, 1, &imageRotationRef_);
532         if (status == napi_ok) {
533             return result;
534         }
535     }
536     MEDIA_ERR_LOG("CreateImageRotationEnum call Failed!");
537     napi_get_undefined(env, &result);
538 
539     return result;
540 }
541 
CreateHostNameType(napi_env env)542 napi_value CameraNapi::CreateHostNameType(napi_env env)
543 {
544     MEDIA_DEBUG_LOG("CreateHostNameType is called");
545     napi_value result = nullptr;
546     napi_status status;
547 
548     status = napi_create_object(env, &result);
549     if (status == napi_ok) {
550         std::string propName;
551         for (auto itr = mapHostNameType.begin(); itr != mapHostNameType.end(); ++itr) {
552             propName = itr->first;
553             status = AddNamedProperty(env, result, propName, itr->second);
554             if (status != napi_ok) {
555                 MEDIA_ERR_LOG("Failed to add HostNameType prop!");
556                 break;
557             }
558             propName.clear();
559         }
560     }
561     if (status == napi_ok) {
562         status = napi_create_reference(env, result, 1, &hostNameTypeRef_);
563         if (status == napi_ok) {
564             return result;
565         }
566     }
567     MEDIA_ERR_LOG("CreateHostNameType call Failed!");
568     napi_get_undefined(env, &result);
569 
570     return result;
571 }
572 
CreateQualityLevelEnum(napi_env env)573 napi_value CameraNapi::CreateQualityLevelEnum(napi_env env)
574 {
575     MEDIA_DEBUG_LOG("CreateQualityLevelEnum is called");
576     napi_value result = nullptr;
577     napi_status status;
578 
579     status = napi_create_object(env, &result);
580     if (status == napi_ok) {
581         std::string propName;
582         for (auto itr = mapQualityLevel.begin(); itr != mapQualityLevel.end(); ++itr) {
583             propName = itr->first;
584             status = AddNamedProperty(env, result, propName, itr->second);
585             if (status != napi_ok) {
586                 MEDIA_ERR_LOG("Failed to add QualityLevel prop!");
587                 break;
588             }
589             propName.clear();
590         }
591     }
592     if (status == napi_ok) {
593         status = napi_create_reference(env, result, 1, &qualityLevelRef_);
594         if (status == napi_ok) {
595             return result;
596         }
597     }
598     MEDIA_ERR_LOG("CreateQualityLevelEnum call Failed!");
599     napi_get_undefined(env, &result);
600 
601     return result;
602 }
603 
CreateCameraInputErrorCode(napi_env env)604 napi_value CameraNapi::CreateCameraInputErrorCode(napi_env env)
605 {
606     MEDIA_DEBUG_LOG("CreateCameraInputErrorCode is called");
607     napi_value result = nullptr;
608     napi_status status;
609 
610     status = napi_create_object(env, &result);
611     if (status == napi_ok) {
612         std::string propName = "ERROR_UNKNOWN";
613         for (auto itr = mapCameraInputErrorCode.begin(); itr != mapCameraInputErrorCode.end(); ++itr) {
614             propName = itr->first;
615             status = CameraNapi::AddNamedProperty(env, result, propName, itr->second);
616             if (status != napi_ok) {
617                 MEDIA_ERR_LOG("Failed to add QualityLevel prop!");
618                 break;
619             }
620             propName.clear();
621         }
622     }
623     if (status == napi_ok) {
624         status = napi_create_reference(env, result, 1, &errorCameraInputRef_);
625         if (status == napi_ok) {
626             return result;
627         }
628     }
629     MEDIA_ERR_LOG("CreateCameraInputErrorCode call Failed!");
630     napi_get_undefined(env, &result);
631 
632     return result;
633 }
634 
CreateCameraErrorCode(napi_env env)635 napi_value CameraNapi::CreateCameraErrorCode(napi_env env)
636 {
637     MEDIA_DEBUG_LOG("CreateCameraErrorCode is called");
638     napi_value result = nullptr;
639     napi_status status;
640 
641     status = napi_create_object(env, &result);
642     if (status == napi_ok) {
643         std::string propName = "ERROR_UNKNOWN";
644         for (auto itr = mapCameraErrorCode.begin(); itr != mapCameraErrorCode.end(); ++itr) {
645             propName = itr->first;
646             status = CameraNapi::AddNamedProperty(env, result, propName, itr->second);
647             if (status != napi_ok) {
648                 MEDIA_ERR_LOG("Failed to add QualityLevel prop!");
649                 break;
650             }
651             propName.clear();
652         }
653     }
654     if (status == napi_ok) {
655         status = napi_create_reference(env, result, 1, &errorCameraInputRef_);
656         if (status == napi_ok) {
657             return result;
658         }
659     }
660     MEDIA_ERR_LOG("CreateCameraErrorCode call Failed!");
661     napi_get_undefined(env, &result);
662 
663     return result;
664 }
665 
CreateCaptureSessionErrorCode(napi_env env)666 napi_value CameraNapi::CreateCaptureSessionErrorCode(napi_env env)
667 {
668     MEDIA_DEBUG_LOG("CreateCaptureSessionErrorCode is called");
669     napi_value result = nullptr;
670     napi_status status;
671 
672     status = napi_create_object(env, &result);
673     if (status == napi_ok) {
674         std::string propName = "ERROR_UNKNOWN";
675         for (auto itr = mapCaptureSessionErrorCode.begin(); itr != mapCaptureSessionErrorCode.end(); ++itr) {
676             propName = itr->first;
677             status = CameraNapi::AddNamedProperty(env, result, propName, itr->second);
678             if (status != napi_ok) {
679                 MEDIA_ERR_LOG("Failed to add CaptureSessionErrorCode prop!");
680                 break;
681             }
682             propName.clear();
683         }
684     }
685     if (status == napi_ok) {
686         status = napi_create_reference(env, result, 1, &errorCaptureSessionRef_);
687         if (status == napi_ok) {
688             return result;
689         }
690     }
691     MEDIA_ERR_LOG("CreateCaptureSessionErrorCode call Failed!");
692     napi_get_undefined(env, &result);
693 
694     return result;
695 }
696 
CreatePreviewOutputErrorCode(napi_env env)697 napi_value CameraNapi::CreatePreviewOutputErrorCode(napi_env env)
698 {
699     MEDIA_DEBUG_LOG("CreatePreviewOutputErrorCode is called");
700     napi_value result = nullptr;
701     napi_status status;
702 
703     status = napi_create_object(env, &result);
704     if (status == napi_ok) {
705         std::string propName = "ERROR_UNKNOWN";
706         for (auto itr = mapPreviewOutputErrorCode.begin(); itr != mapPreviewOutputErrorCode.end(); ++itr) {
707             propName = itr->first;
708             status = CameraNapi::AddNamedProperty(env, result, propName, itr->second);
709             if (status != napi_ok) {
710                 MEDIA_ERR_LOG("Failed to add PreviewOutputErrorCode prop!");
711                 break;
712             }
713             propName.clear();
714         }
715     }
716     if (status == napi_ok) {
717         status = napi_create_reference(env, result, 1, &errorPreviewOutputRef_);
718         if (status == napi_ok) {
719             return result;
720         }
721     }
722     MEDIA_ERR_LOG("CreatePreviewOutputErrorCode call Failed!");
723     napi_get_undefined(env, &result);
724 
725     return result;
726 }
727 
CreatePhotoOutputErrorCode(napi_env env)728 napi_value CameraNapi::CreatePhotoOutputErrorCode(napi_env env)
729 {
730     MEDIA_DEBUG_LOG("CreatePhotoOutputErrorCode is called");
731     napi_value result = nullptr;
732     napi_status status;
733 
734     status = napi_create_object(env, &result);
735     if (status == napi_ok) {
736         std::string propName = "ERROR_UNKNOWN";
737         for (auto itr = mapPhotoOutputErrorCode.begin(); itr != mapPhotoOutputErrorCode.end(); ++itr) {
738             propName = itr->first;
739             status = CameraNapi::AddNamedProperty(env, result, propName, itr->second);
740             if (status != napi_ok) {
741                 MEDIA_ERR_LOG("Failed to add PhotoOutputErrorCode prop!");
742                 break;
743             }
744             propName.clear();
745         }
746     }
747     if (status == napi_ok) {
748         status = napi_create_reference(env, result, 1, &errorPhotoOutputRef_);
749         if (status == napi_ok) {
750             return result;
751         }
752     }
753     MEDIA_ERR_LOG("CreatePhotoOutputErrorCode call Failed!");
754     napi_get_undefined(env, &result);
755 
756     return result;
757 }
758 
CreateVideoOutputErrorCode(napi_env env)759 napi_value CameraNapi::CreateVideoOutputErrorCode(napi_env env)
760 {
761     MEDIA_DEBUG_LOG("CreateVideoOutputErrorCode is called");
762     napi_value result = nullptr;
763     napi_status status;
764 
765     status = napi_create_object(env, &result);
766     if (status == napi_ok) {
767         std::string propName = "ERROR_UNKNOWN";
768         for (auto itr = mapVideoOutputErrorCode.begin(); itr != mapVideoOutputErrorCode.end(); ++itr) {
769             propName = itr->first;
770             status = CameraNapi::AddNamedProperty(env, result, propName, itr->second);
771             if (status != napi_ok) {
772                 MEDIA_ERR_LOG("Failed to add VideoOutputErrorCode prop!");
773                 break;
774             }
775             propName.clear();
776         }
777     }
778     if (status == napi_ok) {
779         status = napi_create_reference(env, result, 1, &errorVideoOutputRef_);
780         if (status == napi_ok) {
781             return result;
782         }
783     }
784     MEDIA_ERR_LOG("CreateCameraInputErrorCode call Failed!");
785     napi_get_undefined(env, &result);
786 
787     return result;
788 }
789 
CreateMetadataObjectType(napi_env env)790 napi_value CameraNapi::CreateMetadataObjectType(napi_env env)
791 {
792     MEDIA_DEBUG_LOG("CreateMetadataObjectType is called");
793     napi_value result = nullptr;
794     napi_status status;
795 
796     status = napi_create_object(env, &result);
797     if (status == napi_ok) {
798         std::string propName = "FACE_DETECTION";
799         for (auto itr = mapMetadataObjectType.begin(); itr != mapMetadataObjectType.end(); ++itr) {
800             propName = itr->first;
801             status = CameraNapi::AddNamedProperty(env, result, propName, itr->second);
802             if (status != napi_ok) {
803                 MEDIA_ERR_LOG("Failed to add MetadataObjectType prop!");
804                 break;
805             }
806             propName.clear();
807         }
808     }
809     if (status == napi_ok) {
810         status = napi_create_reference(env, result, 1, &metadataObjectTypeRef_);
811         if (status == napi_ok) {
812             return result;
813         }
814     }
815     MEDIA_ERR_LOG("CreateMetadataObjectType call Failed!");
816     napi_get_undefined(env, &result);
817     return result;
818 }
819 
CreateMetaOutputErrorCode(napi_env env)820 napi_value CameraNapi::CreateMetaOutputErrorCode(napi_env env)
821 {
822     MEDIA_DEBUG_LOG("CreateMetaOutputErrorCode is called");
823     napi_value result = nullptr;
824     napi_status status;
825 
826     status = napi_create_object(env, &result);
827     if (status == napi_ok) {
828         std::string propName = "ERROR_UNKNOWN";
829         for (auto itr = mapMetaOutputErrorCode.begin(); itr != mapMetaOutputErrorCode.end(); ++itr) {
830             propName = itr->first;
831             status = CameraNapi::AddNamedProperty(env, result, propName, itr->second);
832             if (status != napi_ok) {
833                 MEDIA_ERR_LOG("Failed to add MetaOutputErrorCode prop!");
834                 break;
835             }
836             propName.clear();
837         }
838     }
839     if (status == napi_ok) {
840         status = napi_create_reference(env, result, 1, &errorMetaOutputRef_);
841         if (status == napi_ok) {
842             return result;
843         }
844     }
845     MEDIA_ERR_LOG("CreateMetaOutputErrorCode call Failed!");
846     napi_get_undefined(env, &result);
847 
848     return result;
849 }
850 
CreateVideoStabilizationModeObject(napi_env env)851 napi_value CameraNapi::CreateVideoStabilizationModeObject(napi_env env)
852 {
853     MEDIA_DEBUG_LOG("CreateVideoStabilizationModeObject is called");
854     napi_value result = nullptr;
855     napi_status status;
856 
857     status = napi_create_object(env, &result);
858     if (status == napi_ok) {
859         std::string propName;
860         for (unsigned int i = 0; i < vecVideoStabilizationMode.size(); i++) {
861             propName = vecVideoStabilizationMode[i];
862             status = AddNamedProperty(env, result, propName, i);
863             if (status != napi_ok) {
864                 MEDIA_ERR_LOG("Failed to add named prop for VideoStabilizationMode!");
865                 break;
866             }
867             propName.clear();
868         }
869     }
870     if (status == napi_ok) {
871         status = napi_create_reference(env, result, 1, &videoStabilizationModeRef_);
872         if (status == napi_ok) {
873             return result;
874         }
875     }
876     MEDIA_ERR_LOG("CreateVideoStabilizationModeObject call Failed!");
877     napi_get_undefined(env, &result);
878 
879     return result;
880 }
881 } // namespace CameraStandard
882 } // namespace OHOS
883