• 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_manager_napi.h"
17 #include "input/camera_napi.h"
18 #include "input/camera_pre_launch_config_napi.h"
19 
20 namespace OHOS {
21 namespace CameraStandard {
22 using namespace std;
23 using OHOS::HiviewDFX::HiLog;
24 using OHOS::HiviewDFX::HiLogLabel;
25 namespace {
26     constexpr HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "CameraManager"};
27 }
28 thread_local napi_ref CameraManagerNapi::sConstructor_ = nullptr;
29 thread_local uint32_t CameraManagerNapi::cameraManagerTaskId = CAMERA_MANAGER_TASKID;
30 
CameraManagerNapi()31 CameraManagerNapi::CameraManagerNapi() : env_(nullptr), wrapper_(nullptr)
32 {
33     CAMERA_SYNC_TRACE;
34 }
35 
~CameraManagerNapi()36 CameraManagerNapi::~CameraManagerNapi()
37 {
38     MEDIA_DEBUG_LOG("~CameraManagerNapi is called");
39     if (wrapper_ != nullptr) {
40         napi_delete_reference(env_, wrapper_);
41     }
42     if (cameraManager_) {
43         cameraManager_ = nullptr;
44     }
45 }
46 
47 // Constructor callback
CameraManagerNapiConstructor(napi_env env,napi_callback_info info)48 napi_value CameraManagerNapi::CameraManagerNapiConstructor(napi_env env, napi_callback_info info)
49 {
50     MEDIA_DEBUG_LOG("CameraManagerNapiConstructor is called");
51     napi_status status;
52     napi_value result = nullptr;
53     napi_value thisVar = nullptr;
54 
55     napi_get_undefined(env, &result);
56     CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
57 
58     if (status == napi_ok && thisVar != nullptr) {
59         std::unique_ptr<CameraManagerNapi> obj = std::make_unique<CameraManagerNapi>();
60         obj->env_ = env;
61         obj->cameraManager_ = CameraManager::GetInstance();
62         if (obj->cameraManager_ == nullptr) {
63             MEDIA_ERR_LOG("Failure wrapping js to native napi, obj->cameraManager_ null");
64             return result;
65         }
66         status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
67                            CameraManagerNapi::CameraManagerNapiDestructor, nullptr, nullptr);
68         if (status == napi_ok) {
69             obj.release();
70             return thisVar;
71         } else {
72             MEDIA_ERR_LOG("Failure wrapping js to native napi");
73         }
74     }
75     MEDIA_ERR_LOG("CameraManagerNapiConstructor call Failed!");
76     return result;
77 }
78 
CameraManagerNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)79 void CameraManagerNapi::CameraManagerNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
80 {
81     MEDIA_DEBUG_LOG("CameraManagerNapiDestructor is called");
82     CameraManagerNapi* camera = reinterpret_cast<CameraManagerNapi*>(nativeObject);
83     if (camera != nullptr) {
84         delete camera;
85     }
86 }
87 
Init(napi_env env,napi_value exports)88 napi_value CameraManagerNapi::Init(napi_env env, napi_value exports)
89 {
90     MEDIA_DEBUG_LOG("Init is called");
91     napi_status status;
92     napi_value ctorObj;
93     int32_t refCount = 1;
94 
95     napi_property_descriptor camera_mgr_properties[] = {
96         // CameraManager
97         DECLARE_NAPI_FUNCTION("getSupportedCameras", GetSupportedCameras),
98         DECLARE_NAPI_FUNCTION("getSupportedOutputCapability", GetSupportedOutputCapability),
99         DECLARE_NAPI_FUNCTION("isCameraMuted", IsCameraMuted),
100         DECLARE_NAPI_FUNCTION("isCameraMuteSupported", IsCameraMuteSupported),
101         DECLARE_NAPI_FUNCTION("muteCamera", MuteCamera),
102         DECLARE_NAPI_FUNCTION("prelaunch", PrelaunchCamera),
103         DECLARE_NAPI_FUNCTION("isPrelaunchSupported", IsPrelaunchSupported),
104         DECLARE_NAPI_FUNCTION("setPrelaunchConfig", SetPrelaunchConfig),
105         DECLARE_NAPI_FUNCTION("createCameraInput", CreateCameraInputInstance),
106         DECLARE_NAPI_FUNCTION("createCaptureSession", CreateCameraSessionInstance),
107         DECLARE_NAPI_FUNCTION("createPreviewOutput", CreatePreviewOutputInstance),
108         DECLARE_NAPI_FUNCTION("createDeferredPreviewOutput", CreateDeferredPreviewOutputInstance),
109         DECLARE_NAPI_FUNCTION("createPhotoOutput", CreatePhotoOutputInstance),
110         DECLARE_NAPI_FUNCTION("createVideoOutput", CreateVideoOutputInstance),
111         DECLARE_NAPI_FUNCTION("createMetadataOutput", CreateMetadataOutputInstance),
112         DECLARE_NAPI_FUNCTION("on", On)
113     };
114 
115     status = napi_define_class(env, CAMERA_MANAGER_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
116                                CameraManagerNapiConstructor, nullptr,
117                                sizeof(camera_mgr_properties) / sizeof(camera_mgr_properties[PARAM0]),
118                                camera_mgr_properties, &ctorObj);
119     if (status == napi_ok) {
120         if (napi_create_reference(env, ctorObj, refCount, &sConstructor_) == napi_ok) {
121             status = napi_set_named_property(env, exports, CAMERA_MANAGER_NAPI_CLASS_NAME, ctorObj);
122             if (status == napi_ok) {
123                 return exports;
124             }
125         }
126     }
127     MEDIA_ERR_LOG("Init call Failed!");
128     return nullptr;
129 }
130 
CreateCameraManager(napi_env env)131 napi_value CameraManagerNapi::CreateCameraManager(napi_env env)
132 {
133     MEDIA_INFO_LOG("CreateCameraManager is called");
134     napi_status status;
135     napi_value result = nullptr;
136     napi_value ctor;
137 
138     status = napi_get_reference_value(env, sConstructor_, &ctor);
139     if (status == napi_ok) {
140         status = napi_new_instance(env, ctor, 0, nullptr, &result);
141         if (status == napi_ok) {
142             return result;
143         } else {
144             MEDIA_ERR_LOG("New instance could not be obtained");
145         }
146     }
147     napi_get_undefined(env, &result);
148     MEDIA_ERR_LOG("CreateCameraManager call Failed!");
149     return result;
150 }
151 
CreateCameraJSArray(napi_env env,napi_status status,std::vector<sptr<CameraDevice>> cameraObjList)152 static napi_value CreateCameraJSArray(napi_env env, napi_status status,
153     std::vector<sptr<CameraDevice>> cameraObjList)
154 {
155     MEDIA_DEBUG_LOG("CreateCameraJSArray is called");
156     napi_value cameraArray = nullptr;
157     napi_value camera = nullptr;
158 
159     if (cameraObjList.empty()) {
160         MEDIA_ERR_LOG("cameraObjList is empty");
161         return cameraArray;
162     }
163 
164     status = napi_create_array(env, &cameraArray);
165     if (status == napi_ok) {
166         for (size_t i = 0; i < cameraObjList.size(); i++) {
167             camera = CameraDeviceNapi::CreateCameraObj(env, cameraObjList[i]);
168             if (camera == nullptr || napi_set_element(env, cameraArray, i, camera) != napi_ok) {
169                 MEDIA_ERR_LOG("Failed to create camera napi wrapper object");
170                 return nullptr;
171             }
172         }
173     }
174     return cameraArray;
175 }
176 
CameraManagerCommonCompleteCallback(napi_env env,napi_status status,void * data)177 void CameraManagerCommonCompleteCallback(napi_env env, napi_status status, void* data)
178 {
179     MEDIA_INFO_LOG("CameraManagerCommonCompleteCallback is called");
180     auto context = static_cast<CameraManagerContext*>(data);
181 
182     CAMERA_NAPI_CHECK_NULL_PTR_RETURN_VOID(context, "Async context is null");
183     std::unique_ptr<JSAsyncContextOutput> jsContext = std::make_unique<JSAsyncContextOutput>();
184     MEDIA_INFO_LOG("modeForAsync = %{public}d", context->modeForAsync);
185     napi_get_undefined(env, &jsContext->error);
186     if (context->modeForAsync == CREATE_DEFERRED_PREVIEW_OUTPUT_ASYNC_CALLBACK) {
187         MEDIA_INFO_LOG("createDeferredPreviewOutput");
188         jsContext->data = PreviewOutputNapi::CreateDeferredPreviewOutput(env, context->profile);
189     }
190 
191     if (jsContext->data == nullptr) {
192         context->status = false;
193         context->errString = context->funcName + " failed";
194         MEDIA_ERR_LOG("Failed to create napi, funcName = %{public}s", context->funcName.c_str());
195         CameraNapiUtils::CreateNapiErrorObject(env, context->errorCode, context->errString.c_str(), jsContext);
196     } else {
197         jsContext->status = true;
198         MEDIA_INFO_LOG("Success to create napi, funcName = %{public}s", context->funcName.c_str());
199     }
200 
201     // Finish async trace
202     if (!context->funcName.empty() && context->taskId > 0) {
203         CAMERA_FINISH_ASYNC_TRACE(context->funcName, context->taskId);
204         jsContext->funcName = context->funcName;
205     }
206     if (context->work != nullptr) {
207         CameraNapiUtils::InvokeJSAsyncMethod(env, context->deferred, context->callbackRef,
208                                              context->work, *jsContext);
209     }
210     delete context;
211 }
212 
CreateCameraSessionInstance(napi_env env,napi_callback_info info)213 napi_value CameraManagerNapi::CreateCameraSessionInstance(napi_env env, napi_callback_info info)
214 {
215     MEDIA_INFO_LOG("CreateCameraSessionInstance is called");
216     napi_status status;
217     napi_value result = nullptr;
218     size_t argc = ARGS_ZERO;
219     napi_value argv[ARGS_ZERO];
220     napi_value thisVar = nullptr;
221 
222     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
223 
224     napi_get_undefined(env, &result);
225 
226     CameraManagerNapi* cameraManagerNapi = nullptr;
227     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraManagerNapi));
228     if (status != napi_ok || cameraManagerNapi == nullptr) {
229         MEDIA_ERR_LOG("napi_unwrap failure!");
230         return nullptr;
231     }
232     result = CameraSessionNapi::CreateCameraSession(env);
233     return result;
234 }
235 
ParseSize(napi_env env,napi_value root,Size * size)236 bool ParseSize(napi_env env, napi_value root, Size* size)
237 {
238     MEDIA_DEBUG_LOG("ParseSize is called");
239     napi_value tempValue = nullptr;
240 
241     if (napi_get_named_property(env, root, "width", &tempValue) == napi_ok) {
242         napi_get_value_uint32(env, tempValue, &size->width);
243     }
244 
245     if (napi_get_named_property(env, root, "height", &tempValue) == napi_ok) {
246         napi_get_value_uint32(env, tempValue, &(size->height));
247     }
248 
249     return true;
250 }
251 
ParseProfile(napi_env env,napi_value root,Profile * profile)252 bool ParseProfile(napi_env env, napi_value root, Profile* profile)
253 {
254     MEDIA_DEBUG_LOG("ParseProfile is called");
255     napi_value res = nullptr;
256 
257     if (napi_get_named_property(env, root, "size", &res) == napi_ok) {
258         ParseSize(env, res, &(profile->size_));
259     }
260 
261     int32_t intValue = {0};
262     if (napi_get_named_property(env, root, "format", &res) == napi_ok) {
263         napi_get_value_int32(env, res, &intValue);
264         profile->format_ = static_cast<CameraFormat>(intValue);
265     }
266 
267     return true;
268 }
269 
ParseVideoProfile(napi_env env,napi_value root,VideoProfile * profile)270 bool ParseVideoProfile(napi_env env, napi_value root, VideoProfile* profile)
271 {
272     MEDIA_DEBUG_LOG("ParseVideoProfile is called");
273     napi_value res = nullptr;
274 
275     if (napi_get_named_property(env, root, "size", &res) == napi_ok) {
276         ParseSize(env, res, &(profile->size_));
277     }
278     int32_t intValue = {0};
279     if (napi_get_named_property(env, root, "format", &res) == napi_ok) {
280         napi_get_value_int32(env, res, &intValue);
281         profile->format_ = static_cast<CameraFormat>(intValue);
282     }
283 
284     if (napi_get_named_property(env, root, "frameRateRange", &res) == napi_ok) {
285         const static int32_t LENGTH = 2;
286         std::vector<int32_t> rateRanges(LENGTH);
287 
288         int32_t intValue = {0};
289         const static uint32_t MIN_INDEX = 0;
290         const static uint32_t MAX_INDEX = 1;
291 
292         napi_value value;
293         if (napi_get_named_property(env, res, "min", &value) == napi_ok) {
294             napi_get_value_int32(env, value, &intValue);
295             rateRanges[MIN_INDEX] = intValue;
296         }
297         if (napi_get_named_property(env, res, "max", &value) == napi_ok) {
298             napi_get_value_int32(env, value, &intValue);
299             rateRanges[MAX_INDEX] = intValue;
300         }
301         profile->framerates_ = rateRanges;
302     }
303 
304     return true;
305 }
306 
ConvertJSArgsToNative(napi_env env,size_t argc,const napi_value argv[],CameraManagerContext & asyncContext)307 static napi_value ConvertJSArgsToNative(napi_env env, size_t argc, const napi_value argv[],
308     CameraManagerContext &asyncContext)
309 {
310     MEDIA_DEBUG_LOG("ConvertJSArgsToNative is called");
311     char buffer[PATH_MAX];
312     const int32_t refCount = 1;
313     napi_value result;
314     size_t length = 0;
315     auto context = &asyncContext;
316     NAPI_ASSERT(env, argv != nullptr, "Argument list is empty");
317     for (size_t i = PARAM0; i < argc; i++) {
318         napi_valuetype valueType = napi_undefined;
319         napi_typeof(env, argv[i], &valueType);
320         if (i == PARAM0 && valueType == napi_object) {
321             bool isVideoMode = false;
322             (void)napi_has_named_property(env, argv[i], "frameRateRange", &isVideoMode);
323             if (!isVideoMode) {
324                 ParseProfile(env, argv[i], &(context->profile));
325                 MEDIA_INFO_LOG("ParseProfile "
326                     "size.width = %{public}d, size.height = %{public}d, format = %{public}d",
327                     context->profile.size_.width, context->profile.size_.height, context->profile.format_);
328             } else {
329                 ParseVideoProfile(env, argv[i], &(context->videoProfile));
330                 MEDIA_INFO_LOG("ParseVideoProfile "
331                     "size.width = %{public}d, size.height = %{public}d, format = %{public}d, "
332                     "frameRateRange : min = %{public}d, max = %{public}d",
333                     context->videoProfile.size_.width,
334                     context->videoProfile.size_.height,
335                     context->videoProfile.format_,
336                     context->videoProfile.framerates_[0],
337                     context->videoProfile.framerates_[1]);
338             }
339         } else if (i == PARAM1 && valueType == napi_function) {
340             napi_create_reference(env, argv[i], refCount, &context->callbackRef);
341             break;
342         } else if (i == PARAM1 && valueType == napi_string) {
343             if (napi_get_value_string_utf8(env, argv[i], buffer, PATH_MAX, &length) == napi_ok) {
344                 context->surfaceId = std::string(buffer);
345                 MEDIA_INFO_LOG("context->surfaceId after convert : %{public}s", context->surfaceId.c_str());
346             } else {
347                 MEDIA_ERR_LOG("Could not able to read surfaceId argument!");
348             }
349         } else if (i == PARAM2 && valueType == napi_function) {
350             napi_create_reference(env, argv[i], refCount, &context->callbackRef);
351             break;
352         } else {
353             NAPI_ASSERT(env, false, "type mismatch");
354         }
355     }
356     // Return true napi_value if params are successfully obtained
357     napi_get_boolean(env, true, &result);
358     return result;
359 }
360 
CreatePreviewOutputInstance(napi_env env,napi_callback_info info)361 napi_value CameraManagerNapi::CreatePreviewOutputInstance(napi_env env, napi_callback_info info)
362 {
363     MEDIA_INFO_LOG("CreatePreviewOutputInstance is called");
364     napi_status status;
365     napi_value result = nullptr;
366     size_t argc = ARGS_TWO;
367     napi_value argv[ARGS_TWO] = {0};
368     napi_value thisVar = nullptr;
369 
370     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
371     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_TWO, argv, CREATE_PREVIEW_OUTPUT_INSTANCE)) {
372         MEDIA_INFO_LOG("CheckInvalidArgument napi_throw_type_error ");
373         return result;
374     }
375     MEDIA_INFO_LOG("CheckInvalidArgument pass");
376     napi_get_undefined(env, &result);
377     CameraManagerNapi* cameraManagerNapi = nullptr;
378     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraManagerNapi));
379     if (status != napi_ok || cameraManagerNapi == nullptr) {
380         MEDIA_ERR_LOG("napi_unwrap failure!");
381         return nullptr;
382     }
383     Profile profile;
384     ParseProfile(env, argv[PARAM0], &profile);
385     MEDIA_INFO_LOG("ParseProfile "
386                    "size.width = %{public}d, size.height = %{public}d, format = %{public}d",
387                    profile.size_.width, profile.size_.height, profile.format_);
388 
389     char buffer[PATH_MAX];
390     size_t length = 0;
391     if (napi_get_value_string_utf8(env, argv[PARAM1], buffer, PATH_MAX, &length) == napi_ok) {
392         MEDIA_INFO_LOG("surfaceId buffer --1  : %{public}s", buffer);
393         std::string surfaceId = std::string(buffer);
394         result = PreviewOutputNapi::CreatePreviewOutput(env, profile, surfaceId);
395         MEDIA_INFO_LOG("surfaceId after convert : %{public}s", surfaceId.c_str());
396     } else {
397         MEDIA_ERR_LOG("Could not able to read surfaceId argument!");
398     }
399     return result;
400 }
401 
CreateDeferredPreviewOutputInstance(napi_env env,napi_callback_info info)402 napi_value CameraManagerNapi::CreateDeferredPreviewOutputInstance(napi_env env, napi_callback_info info)
403 {
404     MEDIA_INFO_LOG("CreateDeferredPreviewOutputInstance is called");
405     if (!CameraNapiUtils::CheckSystemApp(env)) {
406         MEDIA_ERR_LOG("SystemApi CreateDeferredPreviewOutputInstance is called!");
407         return nullptr;
408     }
409     napi_status status;
410     napi_value result = nullptr;
411     napi_value resource = nullptr;
412     size_t argc = ARGS_ONE;
413     napi_value argv[ARGS_ONE] = {0};
414     napi_value thisVar = nullptr;
415     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
416     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, CREATE_DEFERRED_PREVIEW_OUTPUT)) {
417         return result;
418     }
419     napi_get_undefined(env, &result);
420     std::unique_ptr<CameraManagerContext> asyncContext = std::make_unique<CameraManagerContext>();
421     result = ConvertJSArgsToNative(env, argc, argv, *asyncContext);
422     CAMERA_NAPI_CHECK_NULL_PTR_RETURN_UNDEFINED(env, result, result, "Failed to obtain arguments");
423     CAMERA_NAPI_CREATE_PROMISE(env, asyncContext->callbackRef, asyncContext->deferred, result);
424     CAMERA_NAPI_CREATE_RESOURCE_NAME(env, resource, "CreateDeferredPreviewOutput");
425     status = napi_create_async_work(
426         env, nullptr, resource,
427         [](napi_env env, void* data) {
428             auto context = static_cast<CameraManagerContext*>(data);
429             // Start async trace
430             context->funcName = "CameraManagerNapi::CreateDeferredPreviewOutputInstance";
431             context->taskId = CameraNapiUtils::IncreamentAndGet(cameraManagerTaskId);
432             CAMERA_START_ASYNC_TRACE(context->funcName, context->taskId);
433             context->status = true;
434             context->modeForAsync = CREATE_DEFERRED_PREVIEW_OUTPUT_ASYNC_CALLBACK;
435         },
436         CameraManagerCommonCompleteCallback, static_cast<void*>(asyncContext.get()), &asyncContext->work);
437     if (status != napi_ok) {
438         MEDIA_ERR_LOG("Failed to create napi_create_async_work for CreatePhotoOutputInstance");
439         napi_get_undefined(env, &result);
440     } else {
441         napi_queue_async_work(env, asyncContext->work);
442         asyncContext.release();
443     }
444 
445     return result;
446 }
447 
CreatePhotoOutputInstance(napi_env env,napi_callback_info info)448 napi_value CameraManagerNapi::CreatePhotoOutputInstance(napi_env env, napi_callback_info info)
449 {
450     MEDIA_INFO_LOG("CreatePhotoOutputInstance is called");
451     napi_status status;
452     napi_value result = nullptr;
453     size_t argc = ARGS_TWO;
454     napi_value argv[ARGS_TWO] = {0};
455     napi_value thisVar = nullptr;
456 
457     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
458     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_TWO, argv, CREATE_PHOTO_OUTPUT_INSTANCE)) {
459         return result;
460     }
461 
462     napi_get_undefined(env, &result);
463     CameraManagerNapi* cameraManagerNapi = nullptr;
464     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraManagerNapi));
465     if (status != napi_ok || cameraManagerNapi == nullptr) {
466         MEDIA_ERR_LOG("napi_unwrap failure!");
467         return nullptr;
468     }
469 
470     Profile profile;
471     ParseProfile(env, argv[PARAM0], &profile);
472     MEDIA_INFO_LOG("ParseProfile "
473                    "size.width = %{public}d, size.height = %{public}d, format = %{public}d",
474                    profile.size_.width, profile.size_.height, profile.format_);
475 
476     char buffer[PATH_MAX];
477     size_t length = 0;
478     if (napi_get_value_string_utf8(env, argv[PARAM1], buffer, PATH_MAX, &length) == napi_ok) {
479         MEDIA_INFO_LOG("surfaceId buffer --1  : %{public}s", buffer);
480         std::string surfaceId = std::string(buffer);
481         result = PhotoOutputNapi::CreatePhotoOutput(env, profile, surfaceId);
482         MEDIA_INFO_LOG("surfaceId after convert : %{public}s", surfaceId.c_str());
483     } else {
484         MEDIA_ERR_LOG("Could not able to read surfaceId argument!");
485     }
486     return result;
487 }
488 
CreateVideoOutputInstance(napi_env env,napi_callback_info info)489 napi_value CameraManagerNapi::CreateVideoOutputInstance(napi_env env, napi_callback_info info)
490 {
491     MEDIA_INFO_LOG("CreateVideoOutputInstance is called");
492     napi_status status;
493     napi_value result = nullptr;
494     size_t argc = ARGS_TWO;
495     napi_value argv[ARGS_TWO] = {0};
496     napi_value thisVar = nullptr;
497 
498     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
499     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_TWO, argv, CREATE_VIDEO_OUTPUT_INSTANCE)) {
500         return result;
501     }
502 
503     CameraManagerNapi* cameraManagerNapi = nullptr;
504     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraManagerNapi));
505     if (status != napi_ok || cameraManagerNapi == nullptr) {
506         MEDIA_ERR_LOG("napi_unwrap failure!");
507         return nullptr;
508     }
509     VideoProfile vidProfile;
510     ParseVideoProfile(env, argv[0], &(vidProfile));
511                 MEDIA_INFO_LOG("ParseVideoProfile "
512                     "size.width = %{public}d, size.height = %{public}d, format = %{public}d, "
513                     "frameRateRange : min = %{public}d, max = %{public}d",
514                     vidProfile.size_.width,
515                     vidProfile.size_.height,
516                     vidProfile.format_,
517                     vidProfile.framerates_[0],
518                     vidProfile.framerates_[1]);
519     char buffer[PATH_MAX];
520     size_t length = 0;
521     if (napi_get_value_string_utf8(env, argv[PARAM1], buffer, PATH_MAX, &length) == napi_ok) {
522         MEDIA_INFO_LOG("surfaceId buffer --1  : %{public}s", buffer);
523         std::string surfaceId = std::string(buffer);
524         result = VideoOutputNapi::CreateVideoOutput(env, vidProfile, surfaceId);
525         MEDIA_INFO_LOG("surfaceId after convert : %{public}s", surfaceId.c_str());
526     } else {
527         MEDIA_ERR_LOG("Could not able to read surfaceId argument!");
528     }
529     return result;
530 }
531 
ParseMetadataObjectTypes(napi_env env,napi_value arrayParam,std::vector<MetadataObjectType> & metadataObjectTypes)532 napi_value ParseMetadataObjectTypes(napi_env env, napi_value arrayParam,
533                                     std::vector<MetadataObjectType> &metadataObjectTypes)
534 {
535     MEDIA_DEBUG_LOG("ParseMetadataObjectTypes is called");
536     napi_value result;
537     uint32_t length = 0;
538     napi_value value;
539     int32_t metadataType;
540     napi_get_array_length(env, arrayParam, &length);
541     napi_valuetype type = napi_undefined;
542     for (uint32_t i = 0; i < length; i++) {
543         napi_get_element(env, arrayParam, i, &value);
544         napi_typeof(env, value, &type);
545         if (type != napi_number) {
546             return nullptr;
547         }
548         napi_get_value_int32(env, value, &metadataType);
549         metadataObjectTypes.push_back(static_cast<MetadataObjectType>(metadataType));
550     }
551     napi_get_boolean(env, true, &result);
552     return result;
553 }
554 
CreateMetadataOutputInstance(napi_env env,napi_callback_info info)555 napi_value CameraManagerNapi::CreateMetadataOutputInstance(napi_env env, napi_callback_info info)
556 {
557     MEDIA_INFO_LOG("CreateMetadataOutputInstance is called");
558     napi_status status;
559     napi_value result = nullptr;
560     size_t argc = ARGS_ONE;
561     napi_value argv[ARGS_ONE] = {0};
562     napi_value thisVar = nullptr;
563 
564     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
565     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, CREATE_METADATA_OUTPUT_INSTANCE)) {
566         return result;
567     }
568 
569     napi_get_undefined(env, &result);
570     CameraManagerNapi* cameraManagerNapi = nullptr;
571     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraManagerNapi));
572     if (status != napi_ok || cameraManagerNapi == nullptr) {
573         MEDIA_ERR_LOG("napi_unwrap failure!");
574         return nullptr;
575     }
576     std::vector<MetadataObjectType> metadataObjectTypes;
577     ParseMetadataObjectTypes(env, argv[PARAM0], metadataObjectTypes);
578     result = MetadataOutputNapi::CreateMetadataOutput(env);
579     return result;
580 }
581 
GetSupportedCameras(napi_env env,napi_callback_info info)582 napi_value CameraManagerNapi::GetSupportedCameras(napi_env env, napi_callback_info info)
583 {
584     MEDIA_INFO_LOG("GetSupportedCameras is called");
585     napi_status status;
586     napi_value result = nullptr;
587     size_t argc = ARGS_ZERO;
588     napi_value argv[ARGS_ZERO];
589     napi_value thisVar = nullptr;
590 
591     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
592 
593     napi_get_undefined(env, &result);
594     CameraManagerNapi* cameraManagerNapi = nullptr;
595     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraManagerNapi));
596     if (status == napi_ok && cameraManagerNapi != nullptr) {
597         std::vector<sptr<CameraDevice>> cameraObjList = cameraManagerNapi->cameraManager_->GetSupportedCameras();
598         result = CreateCameraJSArray(env, status, cameraObjList);
599     } else {
600         MEDIA_ERR_LOG("GetSupportedCameras call Failed!");
601     }
602     return result;
603 }
604 
GetSupportedOutputCapability(napi_env env,napi_callback_info info)605 napi_value CameraManagerNapi::GetSupportedOutputCapability(napi_env env, napi_callback_info info)
606 {
607     MEDIA_INFO_LOG("GetSupportedOutputCapability is called");
608     napi_status status;
609 
610     napi_value result = nullptr;
611     size_t argc = ARGS_ONE;
612     napi_value argv[ARGS_ONE] = {0};
613     napi_value thisVar = nullptr;
614     CameraDeviceNapi* cameraDeviceNapi = nullptr;
615     CameraManagerNapi* cameraManagerNapi = nullptr;
616 
617     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
618 
619     napi_get_undefined(env, &result);
620     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraManagerNapi));
621     if (status != napi_ok || cameraManagerNapi == nullptr) {
622         MEDIA_ERR_LOG("napi_unwrap( ) failure!");
623         return result;
624     }
625     status = napi_unwrap(env, argv[PARAM0], reinterpret_cast<void **>(&cameraDeviceNapi));
626     if (status != napi_ok || cameraDeviceNapi == nullptr) {
627         MEDIA_ERR_LOG("Could not able to read cameraId argument!");
628         return result;
629     }
630     sptr<CameraDevice> cameraInfo = cameraDeviceNapi->cameraDevice_;
631     result = CameraOutputCapabilityNapi::CreateCameraOutputCapability(env, cameraInfo);
632     return result;
633 }
634 
IsCameraMuted(napi_env env,napi_callback_info info)635 napi_value CameraManagerNapi::IsCameraMuted(napi_env env, napi_callback_info info)
636 {
637     MEDIA_INFO_LOG("IsCameraMuted is called");
638     napi_value result = nullptr;
639     size_t argc = ARGS_ONE;
640     napi_value argv[ARGS_ONE] = {0};
641     napi_value thisVar = nullptr;
642 
643     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
644     NAPI_ASSERT(env, argc <= ARGS_ONE, "requires 1 parameters maximum");
645     bool isMuted = CameraManager::GetInstance()->IsCameraMuted();
646     MEDIA_DEBUG_LOG("IsCameraMuted : %{public}d", isMuted);
647     napi_get_boolean(env, isMuted, &result);
648     return result;
649 }
650 
IsCameraMuteSupported(napi_env env,napi_callback_info info)651 napi_value CameraManagerNapi::IsCameraMuteSupported(napi_env env, napi_callback_info info)
652 {
653     if (!CameraNapiUtils::CheckSystemApp(env)) {
654         MEDIA_ERR_LOG("SystemApi IsCameraMuteSupported is called!");
655         return nullptr;
656     }
657     MEDIA_INFO_LOG("IsCameraMuteSupported is called");
658     napi_value result = nullptr;
659     size_t argc = ARGS_ONE;
660     napi_value argv[ARGS_ONE] = {0};
661     napi_value thisVar = nullptr;
662 
663     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
664     NAPI_ASSERT(env, argc <= ARGS_ONE, "requires 1 parameters maximum");
665 
666     bool isMuteSupported = CameraManager::GetInstance()->IsCameraMuteSupported();
667     MEDIA_DEBUG_LOG("isMuteSupported: %{public}d", isMuteSupported);
668     napi_get_boolean(env, isMuteSupported, &result);
669     return result;
670 }
671 
MuteCamera(napi_env env,napi_callback_info info)672 napi_value CameraManagerNapi::MuteCamera(napi_env env, napi_callback_info info)
673 {
674     if (!CameraNapiUtils::CheckSystemApp(env)) {
675         MEDIA_ERR_LOG("SystemApi MuteCamera is called!");
676         return nullptr;
677     }
678     MEDIA_INFO_LOG("MuteCamera is called");
679     napi_value result = nullptr;
680     size_t argc = ARGS_TWO;
681     napi_value argv[ARGS_TWO] = {0};
682     napi_value thisVar = nullptr;
683 
684     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
685     NAPI_ASSERT(env, argc <= ARGS_TWO, "requires 1 parameters maximum");
686     bool isSupported;
687     napi_get_value_bool(env, argv[PARAM0], &isSupported);
688     CameraManager::GetInstance()->MuteCamera(isSupported);
689     napi_get_undefined(env, &result);
690     return result;
691 }
692 
CreateCameraInputInstance(napi_env env,napi_callback_info info)693 napi_value CameraManagerNapi::CreateCameraInputInstance(napi_env env, napi_callback_info info)
694 {
695     MEDIA_INFO_LOG("CreateCameraInputInstance is called");
696     napi_status status;
697     napi_value result = nullptr;
698     size_t argc = ARGS_TWO;
699     napi_value argv[ARGS_TWO] = {0};
700     napi_value thisVar = nullptr;
701 
702     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
703     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_TWO, argv, CREATE_CAMERA_INPUT_INSTANCE)) {
704         return result;
705     }
706 
707     napi_get_undefined(env, &result);
708     CameraManagerNapi* cameraManagerNapi = nullptr;
709     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraManagerNapi));
710     if (status != napi_ok || cameraManagerNapi == nullptr) {
711         MEDIA_ERR_LOG("napi_unwrap( ) failure!");
712         return result;
713     }
714     sptr<CameraDevice> cameraInfo = nullptr;
715     if (argc == ARGS_ONE) {
716         CameraDeviceNapi* cameraDeviceNapi = nullptr;
717         status = napi_unwrap(env, argv[PARAM0], reinterpret_cast<void **>(&cameraDeviceNapi));
718         if (status != napi_ok || cameraDeviceNapi == nullptr) {
719             MEDIA_ERR_LOG("napi_unwrap( ) failure!");
720             return result;
721         }
722         cameraInfo = cameraDeviceNapi->cameraDevice_;
723     } else if (argc == ARGS_TWO) {
724         int32_t numValue;
725 
726         napi_get_value_int32(env, argv[PARAM0], &numValue);
727         CameraPosition cameraPosition = static_cast<CameraPosition>(numValue);
728 
729         napi_get_value_int32(env, argv[PARAM1], &numValue);
730         CameraType cameraType = static_cast<CameraType>(numValue);
731 
732         std::vector<sptr<CameraDevice>> cameraObjList = cameraManagerNapi->cameraManager_->GetSupportedCameras();
733         MEDIA_DEBUG_LOG("cameraInfo is null, the cameraObjList size is %{public}zu",
734                         cameraObjList.size());
735         for (size_t i = 0; i < cameraObjList.size(); i++) {
736             sptr<CameraDevice> cameraDevice = cameraObjList[i];
737             if (cameraDevice == nullptr) {
738                 continue;
739             }
740             if (cameraDevice->GetPosition() == cameraPosition &&
741                 cameraDevice->GetCameraType() == cameraType) {
742                 cameraInfo = cameraDevice;
743                 break;
744             }
745         }
746     }
747     if (cameraInfo != nullptr) {
748         sptr<CameraInput> cameraInput = nullptr;
749         int retCode = CameraManager::GetInstance()->CreateCameraInput(cameraInfo, &cameraInput);
750         if (!CameraNapiUtils::CheckError(env, retCode)) {
751             return nullptr;
752         }
753         result = CameraInputNapi::CreateCameraInput(env, cameraInput);
754     } else {
755         MEDIA_ERR_LOG("cameraInfo is null");
756     }
757     return result;
758 }
759 
On(napi_env env,napi_callback_info info)760 napi_value CameraManagerNapi::On(napi_env env, napi_callback_info info)
761 {
762     MEDIA_INFO_LOG("On is called");
763     napi_value undefinedResult = nullptr;
764     size_t argCount = ARGS_TWO;
765     napi_value argv[ARGS_TWO] = {nullptr};
766     napi_value thisVar = nullptr;
767     size_t res = 0;
768     char buffer[SIZE];
769     CameraManagerNapi* obj = nullptr;
770     napi_status status;
771 
772     napi_get_undefined(env, &undefinedResult);
773 
774     CAMERA_NAPI_GET_JS_ARGS(env, info, argCount, argv, thisVar);
775     NAPI_ASSERT(env, argCount == ARGS_TWO, "requires 2 parameters");
776 
777     if (thisVar == nullptr || argv[PARAM0] == nullptr || argv[PARAM1] == nullptr) {
778         MEDIA_ERR_LOG("Failed to retrieve details about the callback");
779         return undefinedResult;
780     }
781 
782     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&obj));
783     if (status == napi_ok && obj != nullptr) {
784         napi_valuetype valueType = napi_undefined;
785         if (napi_typeof(env, argv[PARAM0], &valueType) != napi_ok || valueType != napi_string
786             || napi_typeof(env, argv[PARAM1], &valueType) != napi_ok || valueType != napi_function) {
787             return undefinedResult;
788         }
789 
790         napi_get_value_string_utf8(env, argv[PARAM0], buffer, SIZE, &res);
791         std::string eventType = std::string(buffer);
792 
793         napi_ref callbackRef;
794         napi_create_reference(env, argv[PARAM1], 1, &callbackRef);
795 
796         if (!eventType.empty() && (eventType.compare("cameraStatus")==0)) {
797             shared_ptr<CameraManagerCallbackNapi> callback =
798                 make_shared<CameraManagerCallbackNapi>(env, callbackRef);
799             obj->cameraManager_->SetCallback(callback);
800         } else if (!eventType.empty() && (eventType.compare("cameraMute")==0)) {
801             if (!CameraNapiUtils::CheckSystemApp(env)) {
802                 MEDIA_ERR_LOG("SystemApi On cameraMute is called!");
803                 return undefinedResult;
804             }
805             shared_ptr<CameraMuteListenerNapi> listener =
806                 make_shared<CameraMuteListenerNapi>(env, callbackRef);
807             obj->cameraManager_->RegisterCameraMuteListener(listener);
808         } else {
809             MEDIA_ERR_LOG("Incorrect callback event type provided for camera manager!");
810             if (callbackRef != nullptr) {
811                 napi_delete_reference(env, callbackRef);
812             }
813         }
814     } else {
815         MEDIA_ERR_LOG("On call Failed!");
816     }
817     return undefinedResult;
818 }
819 
IsPrelaunchSupported(napi_env env,napi_callback_info info)820 napi_value CameraManagerNapi::IsPrelaunchSupported(napi_env env, napi_callback_info info)
821 {
822     MEDIA_INFO_LOG("IsPrelaunchSupported is called");
823     if (!CameraNapiUtils::CheckSystemApp(env)) {
824         MEDIA_ERR_LOG("SystemApi IsPrelaunchSupported is called!");
825         return nullptr;
826     }
827     napi_status status;
828 
829     napi_value result = nullptr;
830     size_t argc = ARGS_ONE;
831     napi_value argv[ARGS_ONE] = {0};
832     napi_value thisVar = nullptr;
833     CameraDeviceNapi* cameraDeviceNapi = nullptr;
834     CameraManagerNapi* cameraManagerNapi = nullptr;
835 
836     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
837 
838     napi_get_undefined(env, &result);
839     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraManagerNapi));
840     if (status != napi_ok || cameraManagerNapi == nullptr) {
841         MEDIA_ERR_LOG("napi_unwrap( ) failure!");
842         return result;
843     }
844     status = napi_unwrap(env, argv[PARAM0], reinterpret_cast<void **>(&cameraDeviceNapi));
845     if (status == napi_ok && cameraDeviceNapi != nullptr) {
846         sptr<CameraDevice> cameraInfo = cameraDeviceNapi->cameraDevice_;
847         bool isPrelaunchSupported = CameraManager::GetInstance()->IsPrelaunchSupported(cameraInfo);
848         MEDIA_DEBUG_LOG("isPrelaunchSupported: %{public}d", isPrelaunchSupported);
849         napi_get_boolean(env, isPrelaunchSupported, &result);
850     } else {
851         MEDIA_ERR_LOG("Could not able to read cameraDevice argument!");
852         if (!CameraNapiUtils::CheckError(env, INVALID_ARGUMENT)) {
853             MEDIA_DEBUG_LOG("Could not able to read cameraDevice argument throw error");
854         }
855     }
856     return result;
857 }
858 
PrelaunchCamera(napi_env env,napi_callback_info info)859 napi_value CameraManagerNapi::PrelaunchCamera(napi_env env, napi_callback_info info)
860 {
861     MEDIA_INFO_LOG("PrelaunchCamera is called");
862     if (!CameraNapiUtils::CheckSystemApp(env)) {
863         MEDIA_ERR_LOG("SystemApi PrelaunchCamera is called!");
864         return nullptr;
865     }
866     napi_value result = nullptr;
867     int32_t retCode = CameraManager::GetInstance()->PrelaunchCamera();
868     if (!CameraNapiUtils::CheckError(env, retCode)) {
869         return result;
870     }
871     MEDIA_INFO_LOG("PrelaunchCamera");
872     napi_get_undefined(env, &result);
873     return result;
874 }
875 
SetPrelaunchConfig(napi_env env,napi_callback_info info)876 napi_value CameraManagerNapi::SetPrelaunchConfig(napi_env env, napi_callback_info info)
877 {
878     MEDIA_INFO_LOG("SetPrelaunchConfig is called");
879     if (!CameraNapiUtils::CheckSystemApp(env)) {
880         MEDIA_ERR_LOG("SystemApi SetPrelaunchConfig is called!");
881         return nullptr;
882     }
883     napi_status status;
884     napi_value result = nullptr;
885     size_t argc = ARGS_ONE;
886     napi_value argv[ARGS_ONE] = {0};
887     napi_value thisVar = nullptr;
888     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
889     NAPI_ASSERT(env, argc < ARGS_TWO, "requires 1 parameters maximum");
890     napi_value res = nullptr;
891     PrelaunchConfig prelaunchConfig;
892     CameraDeviceNapi* cameraDeviceNapi = nullptr;
893     if (napi_get_named_property(env, argv[PARAM0], "cameraDevice", &res) == napi_ok) {
894         status = napi_unwrap(env, res, reinterpret_cast<void **>(&cameraDeviceNapi));
895         prelaunchConfig.cameraDevice_ = cameraDeviceNapi->cameraDevice_;
896         if (status != napi_ok || prelaunchConfig.cameraDevice_ == nullptr) {
897             MEDIA_ERR_LOG("napi_unwrap( ) failure!");
898             return result;
899         }
900     } else {
901         if (!CameraNapiUtils::CheckError(env, INVALID_ARGUMENT)) {
902             return result;
903         }
904     }
905     std::string cameraId = prelaunchConfig.GetCameraDevice()->GetID();
906     MEDIA_INFO_LOG("SetPrelaunchConfig cameraId = %{public}s", cameraId.c_str());
907     int32_t retCode = CameraManager::GetInstance()->SetPrelaunchConfig(cameraId);
908     if (!CameraNapiUtils::CheckError(env, retCode)) {
909         return result;
910     }
911     napi_get_undefined(env, &result);
912     return result;
913 }
914 } // namespace CameraStandard
915 } // namespace OHOS
916