• 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 "session/camera_session_napi.h"
17 #include <uv.h>
18 
19 namespace OHOS {
20 namespace CameraStandard {
21 using namespace std;
22 using OHOS::HiviewDFX::HiLog;
23 using OHOS::HiviewDFX::HiLogLabel;
24 
25 namespace {
26     constexpr HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "CameraNapi"};
27 }
28 
29 thread_local napi_ref CameraSessionNapi::sConstructor_ = nullptr;
30 thread_local sptr<CaptureSession> CameraSessionNapi::sCameraSession_ = nullptr;
31 thread_local uint32_t CameraSessionNapi::cameraSessionTaskId = CAMERA_SESSION_TASKID;
32 
OnExposureStateCallbackAsync(ExposureState state) const33 void ExposureCallbackListener::OnExposureStateCallbackAsync(ExposureState state) const
34 {
35     uv_loop_s* loop = nullptr;
36     napi_get_uv_event_loop(env_, &loop);
37     if (!loop) {
38         MEDIA_ERR_LOG("ExposureCallbackListener:OnExposureStateCallbackAsync() failed to get event loop");
39         return;
40     }
41     uv_work_t* work = new(std::nothrow) uv_work_t;
42     if (!work) {
43         MEDIA_ERR_LOG("ExposureCallbackListener:OnExposureStateCallbackAsync() failed to allocate work");
44         return;
45     }
46     std::unique_ptr<ExposureCallbackInfo> callbackInfo = std::make_unique<ExposureCallbackInfo>(state, this);
47     work->data = callbackInfo.get();
48     int ret = uv_queue_work(loop, work, [] (uv_work_t* work) {}, [] (uv_work_t* work, int status) {
49         ExposureCallbackInfo* callbackInfo = reinterpret_cast<ExposureCallbackInfo *>(work->data);
50         if (callbackInfo) {
51             callbackInfo->listener_->OnExposureStateCallback(callbackInfo->state_);
52             delete callbackInfo;
53         }
54         delete work;
55     });
56     if (ret) {
57         MEDIA_ERR_LOG("ExposureCallbackListener:OnExposureStateCallbackAsync() failed to execute work");
58         delete work;
59     } else {
60         callbackInfo.release();
61     }
62 }
63 
OnExposureStateCallback(ExposureState state) const64 void ExposureCallbackListener::OnExposureStateCallback(ExposureState state) const
65 {
66     napi_value result[ARGS_ONE];
67     napi_value callback = nullptr;
68     napi_value retVal;
69 
70     napi_create_int32(env_, state, &result[PARAM0]);
71 
72     napi_get_reference_value(env_, callbackRef_, &callback);
73     napi_call_function(env_, nullptr, callback, ARGS_ONE, result, &retVal);
74 }
75 
OnExposureState(const ExposureState state)76 void ExposureCallbackListener::OnExposureState(const ExposureState state)
77 {
78     MEDIA_INFO_LOG("ExposureCallbackListener:OnExposureState() is called!, state: %{public}d", state);
79     OnExposureStateCallbackAsync(state);
80 }
81 
OnFocusStateCallbackAsync(FocusState state) const82 void FocusCallbackListener::OnFocusStateCallbackAsync(FocusState state) const
83 {
84     uv_loop_s* loop = nullptr;
85     napi_get_uv_event_loop(env_, &loop);
86     if (!loop) {
87         MEDIA_ERR_LOG("FocusCallbackListener:OnFocusStateCallbackAsync() failed to get event loop");
88         return;
89     }
90     uv_work_t* work = new(std::nothrow) uv_work_t;
91     if (!work) {
92         MEDIA_ERR_LOG("FocusCallbackListener:OnFocusStateCallbackAsync() failed to allocate work");
93         return;
94     }
95     std::unique_ptr<FocusCallbackInfo> callbackInfo = std::make_unique<FocusCallbackInfo>(state, this);
96     work->data = callbackInfo.get();
97     int ret = uv_queue_work(loop, work, [] (uv_work_t* work) {}, [] (uv_work_t* work, int status) {
98         FocusCallbackInfo* callbackInfo = reinterpret_cast<FocusCallbackInfo *>(work->data);
99         if (callbackInfo) {
100             callbackInfo->listener_->OnFocusStateCallback(callbackInfo->state_);
101             delete callbackInfo;
102         }
103         delete work;
104     });
105     if (ret) {
106         MEDIA_ERR_LOG("FocusCallbackListener:OnFocusStateCallbackAsync() failed to execute work");
107         delete work;
108     } else {
109         callbackInfo.release();
110     }
111 }
112 
OnFocusStateCallback(FocusState state) const113 void FocusCallbackListener::OnFocusStateCallback(FocusState state) const
114 {
115     napi_value result[ARGS_ONE];
116     napi_value callback = nullptr;
117     napi_value retVal;
118 
119     napi_create_int32(env_, state, &result[PARAM0]);
120 
121     napi_get_reference_value(env_, callbackRef_, &callback);
122     napi_call_function(env_, nullptr, callback, ARGS_ONE, result, &retVal);
123 }
124 
OnFocusState(FocusState state)125 void FocusCallbackListener::OnFocusState(FocusState state)
126 {
127     MEDIA_INFO_LOG("FocusCallbackListener:OnFocusState() is called!, state: %{public}d", state);
128     OnFocusStateCallbackAsync(state);
129 }
130 
OnErrorCallbackAsync(int32_t errorCode) const131 void SessionCallbackListener::OnErrorCallbackAsync(int32_t errorCode) const
132 {
133     uv_loop_s* loop = nullptr;
134     napi_get_uv_event_loop(env_, &loop);
135     if (!loop) {
136         MEDIA_ERR_LOG("SessionCallbackListener:OnErrorCallbackAsync() failed to get event loop");
137         return;
138     }
139     uv_work_t* work = new(std::nothrow) uv_work_t;
140     if (!work) {
141         MEDIA_ERR_LOG("SessionCallbackListener:OnErrorCallbackAsync() failed to allocate work");
142         return;
143     }
144     std::unique_ptr<SessionCallbackInfo> callbackInfo = std::make_unique<SessionCallbackInfo>(errorCode, this);
145     work->data = callbackInfo.get();
146     int ret = uv_queue_work(loop, work, [] (uv_work_t* work) {}, [] (uv_work_t* work, int status) {
147         SessionCallbackInfo* callbackInfo = reinterpret_cast<SessionCallbackInfo *>(work->data);
148         if (callbackInfo) {
149             callbackInfo->listener_->OnErrorCallback(callbackInfo->errorCode_);
150             delete callbackInfo;
151         }
152         delete work;
153     });
154     if (ret) {
155         MEDIA_ERR_LOG("SessionCallbackListener:OnErrorCallbackAsync() failed to execute work");
156         delete work;
157     } else {
158         callbackInfo.release();
159     }
160 }
161 
OnErrorCallback(int32_t errorCode) const162 void SessionCallbackListener::OnErrorCallback(int32_t errorCode) const
163 {
164     int32_t jsErrorCodeUnknown = -1;
165     napi_value result[ARGS_ONE];
166     napi_value callback = nullptr;
167     napi_value retVal;
168     napi_value propValue;
169     napi_create_object(env_, &result[PARAM0]);
170 
171     napi_create_int32(env_, jsErrorCodeUnknown, &propValue);
172 
173     napi_set_named_property(env_, result[PARAM0], "code", propValue);
174     napi_get_reference_value(env_, callbackRef_, &callback);
175     napi_call_function(env_, nullptr, callback, ARGS_ONE, result, &retVal);
176 }
177 
OnError(int32_t errorCode)178 void SessionCallbackListener::OnError(int32_t errorCode)
179 {
180     MEDIA_INFO_LOG("SessionCallbackListener:OnError() is called!, errorCode: %{public}d", errorCode);
181     OnErrorCallbackAsync(errorCode);
182 }
183 
CameraSessionNapi()184 CameraSessionNapi::CameraSessionNapi() : env_(nullptr), wrapper_(nullptr)
185 {
186 }
187 
~CameraSessionNapi()188 CameraSessionNapi::~CameraSessionNapi()
189 {
190     if (wrapper_ != nullptr) {
191         napi_delete_reference(env_, wrapper_);
192     }
193     if (cameraSession_) {
194         cameraSession_ = nullptr;
195     }
196 }
197 
CameraSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)198 void CameraSessionNapi::CameraSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
199 {
200     CameraSessionNapi* cameraObj = reinterpret_cast<CameraSessionNapi*>(nativeObject);
201     if (cameraObj != nullptr) {
202         cameraObj->~CameraSessionNapi();
203     }
204 }
205 
Init(napi_env env,napi_value exports)206 napi_value CameraSessionNapi::Init(napi_env env, napi_value exports)
207 {
208     napi_status status;
209     napi_value ctorObj;
210     int32_t refCount = 1;
211 
212     napi_property_descriptor camera_session_props[] = {
213         DECLARE_NAPI_FUNCTION("beginConfig", BeginConfig),
214         DECLARE_NAPI_FUNCTION("commitConfig", CommitConfig),
215 
216         DECLARE_NAPI_FUNCTION("canAddInput", CanAddInput),
217         DECLARE_NAPI_FUNCTION("addInput", AddInput),
218         DECLARE_NAPI_FUNCTION("removeInput", RemoveInput),
219 
220         DECLARE_NAPI_FUNCTION("canAddOutput", CanAddOutput),
221         DECLARE_NAPI_FUNCTION("addOutput", AddOutput),
222         DECLARE_NAPI_FUNCTION("removeOutput", RemoveOutput),
223 
224         DECLARE_NAPI_FUNCTION("start", Start),
225         DECLARE_NAPI_FUNCTION("stop", Stop),
226         DECLARE_NAPI_FUNCTION("release", Release),
227 
228         DECLARE_NAPI_FUNCTION("lockForControl", LockForControl),
229         DECLARE_NAPI_FUNCTION("unlockForControl", UnlockForControl),
230 
231         DECLARE_NAPI_FUNCTION("isVideoStabilizationModeSupported", IsVideoStabilizationModeSupported),
232         DECLARE_NAPI_FUNCTION("getActiveVideoStabilizationMode", GetActiveVideoStabilizationMode),
233         DECLARE_NAPI_FUNCTION("setVideoStabilizationMode", SetVideoStabilizationMode),
234 
235         DECLARE_NAPI_FUNCTION("hasFlash", HasFlash),
236         DECLARE_NAPI_FUNCTION("isFlashModeSupported", IsFlashModeSupported),
237         DECLARE_NAPI_FUNCTION("getFlashMode", GetFlashMode),
238         DECLARE_NAPI_FUNCTION("setFlashMode", SetFlashMode),
239 
240         DECLARE_NAPI_FUNCTION("isExposureModeSupported", IsExposureModeSupported),
241         DECLARE_NAPI_FUNCTION("getExposureMode", GetExposureMode),
242         DECLARE_NAPI_FUNCTION("setExposureMode", SetExposureMode),
243         DECLARE_NAPI_FUNCTION("getExposureBiasRange", GetExposureBiasRange),
244         DECLARE_NAPI_FUNCTION("setExposureBias", SetExposureBias),
245         DECLARE_NAPI_FUNCTION("getExposureValue", GetExposureValue),
246 
247         DECLARE_NAPI_FUNCTION("getMeteringPoint", GetMeteringPoint),
248         DECLARE_NAPI_FUNCTION("setMeteringPoint", SetMeteringPoint),
249 
250         DECLARE_NAPI_FUNCTION("isFocusModeSupported", IsFocusModeSupported),
251         DECLARE_NAPI_FUNCTION("getFocusMode", GetFocusMode),
252         DECLARE_NAPI_FUNCTION("setFocusMode", SetFocusMode),
253 
254         DECLARE_NAPI_FUNCTION("getFocusPoint", GetFocusPoint),
255         DECLARE_NAPI_FUNCTION("setFocusPoint", SetFocusPoint),
256         DECLARE_NAPI_FUNCTION("getFocalLength", GetFocalLength),
257 
258         DECLARE_NAPI_FUNCTION("getZoomRatioRange", GetZoomRatioRange),
259         DECLARE_NAPI_FUNCTION("getZoomRatio", GetZoomRatio),
260         DECLARE_NAPI_FUNCTION("setZoomRatio", SetZoomRatio),
261 
262         DECLARE_NAPI_FUNCTION("on", On)
263     };
264 
265     status = napi_define_class(env, CAMERA_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
266                                CameraSessionNapiConstructor, nullptr,
267                                sizeof(camera_session_props) / sizeof(camera_session_props[PARAM0]),
268                                camera_session_props, &ctorObj);
269     if (status == napi_ok) {
270         status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
271         if (status == napi_ok) {
272             status = napi_set_named_property(env, exports, CAMERA_SESSION_NAPI_CLASS_NAME, ctorObj);
273             if (status == napi_ok) {
274                 return exports;
275             }
276         }
277     }
278 
279     return nullptr;
280 }
281 
282 // Constructor callback
CameraSessionNapiConstructor(napi_env env,napi_callback_info info)283 napi_value CameraSessionNapi::CameraSessionNapiConstructor(napi_env env, napi_callback_info info)
284 {
285     napi_status status;
286     napi_value result = nullptr;
287     napi_value thisVar = nullptr;
288 
289     napi_get_undefined(env, &result);
290     CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
291 
292     if (status == napi_ok && thisVar != nullptr) {
293         std::unique_ptr<CameraSessionNapi> obj = std::make_unique<CameraSessionNapi>();
294         if (obj != nullptr) {
295             obj->env_ = env;
296             if (sCameraSession_ == nullptr) {
297                 MEDIA_ERR_LOG("sCameraSession_ is null");
298                 return result;
299             }
300             obj->cameraSession_ = sCameraSession_;
301             status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
302                                CameraSessionNapi::CameraSessionNapiDestructor, nullptr, &(obj->wrapper_));
303             if (status == napi_ok) {
304                 obj.release();
305                 return thisVar;
306             } else {
307                 MEDIA_ERR_LOG("CameraSessionNapi Failure wrapping js to native napi");
308             }
309         }
310     }
311 
312     return result;
313 }
314 
QueryAndGetInputProperty(napi_env env,napi_value arg,const string & propertyName,napi_value & property)315 int32_t QueryAndGetInputProperty(napi_env env, napi_value arg, const string &propertyName, napi_value &property)
316 {
317     bool present = false;
318     int32_t retval = 0;
319     if ((napi_has_named_property(env, arg, propertyName.c_str(), &present) != napi_ok)
320         || (!present) || (napi_get_named_property(env, arg, propertyName.c_str(), &property) != napi_ok)) {
321             HiLog::Error(LABEL, "Failed to obtain property: %{public}s", propertyName.c_str());
322             retval = -1;
323     }
324 
325     return retval;
326 }
327 
GetPointProperties(napi_env env,napi_value pointObj,Point & point)328 int32_t GetPointProperties(napi_env env, napi_value pointObj, Point &point)
329 {
330     napi_value propertyX = nullptr;
331     napi_value propertyY = nullptr;
332     double pointX = -1.0;
333     double pointY = -1.0;
334 
335     if ((QueryAndGetInputProperty(env, pointObj, "x", propertyX) == 0) &&
336         (QueryAndGetInputProperty(env, pointObj, "y", propertyY) == 0)) {
337         if ((napi_get_value_double(env, propertyX, &pointX) != napi_ok) ||
338             (napi_get_value_double(env, propertyY, &pointY) != napi_ok)) {
339             MEDIA_ERR_LOG("GetPointProperties: get propery for x & y failed");
340             return -1;
341         } else {
342             point.x = pointX;
343             point.y = pointY;
344         }
345     } else {
346         return -1;
347     }
348 
349     // Return 0 after focus point properties are successfully obtained
350     return 0;
351 }
352 
GetPointNapiValue(napi_env env,Point & point)353 napi_value GetPointNapiValue(napi_env env, Point &point)
354 {
355     napi_value result;
356     napi_value propValue;
357     napi_create_object(env, &result);
358     napi_create_double(env, point.x, &propValue);
359     napi_set_named_property(env, result, "x", propValue);
360     napi_create_double(env, point.y, &propValue);
361     napi_set_named_property(env, result, "y", propValue);
362     return result;
363 }
364 
CreateCameraSession(napi_env env)365 napi_value CameraSessionNapi::CreateCameraSession(napi_env env)
366 {
367     CAMERA_SYNC_TRACE;
368     napi_status status;
369     napi_value result = nullptr;
370     napi_value constructor;
371 
372     status = napi_get_reference_value(env, sConstructor_, &constructor);
373     if (status == napi_ok) {
374         int retCode = CameraManager::GetInstance()->CreateCaptureSession(&sCameraSession_);
375         if (!CameraNapiUtils::CheckError(env, retCode)) {
376             return nullptr;
377         }
378         if (sCameraSession_ == nullptr) {
379             MEDIA_ERR_LOG("Failed to create Camera session instance");
380             napi_get_undefined(env, &result);
381             return result;
382         } else {
383             MEDIA_INFO_LOG("Camera session instance create success");
384         }
385         MEDIA_INFO_LOG("before new Camera session napi instance");
386         status = napi_new_instance(env, constructor, 0, nullptr, &result);
387         MEDIA_INFO_LOG("after new Camera session napi instance");
388         sCameraSession_ = nullptr;
389         if (status == napi_ok && result != nullptr) {
390             MEDIA_INFO_LOG("success to create Camera session napi instance");
391             return result;
392         } else {
393             MEDIA_ERR_LOG("Failed to create Camera session napi instance");
394         }
395     }
396     MEDIA_ERR_LOG("Failed to create Camera session napi instance last");
397     napi_get_undefined(env, &result);
398     return result;
399 }
400 
PopulateRetVal(napi_env env,SessionAsyncCallbackModes mode,CameraSessionAsyncContext * context,std::unique_ptr<JSAsyncContextOutput> & jsContext)401 void PopulateRetVal(napi_env env, SessionAsyncCallbackModes mode,
402     CameraSessionAsyncContext* context, std::unique_ptr<JSAsyncContextOutput> &jsContext)
403 {
404     jsContext->status = true;
405     napi_get_undefined(env, &jsContext->error);
406     switch (mode) {
407         case COMMIT_CONFIG_ASYNC_CALLBACK:
408             context->errorCode = context->objectInfo->cameraSession_->CommitConfig();
409             MEDIA_INFO_LOG("commit config return : %{public}d", context->errorCode);
410             break;
411         case SESSION_START_ASYNC_CALLBACK:
412             context->errorCode = context->objectInfo->cameraSession_->Start();
413             MEDIA_INFO_LOG("Start return : %{public}d", context->errorCode);
414             break;
415         case SESSION_STOP_ASYNC_CALLBACK:
416             context->errorCode = context->objectInfo->cameraSession_->Stop();
417             MEDIA_INFO_LOG("Stop return : %{public}d", context->errorCode);
418             break;
419         case SESSION_RELEASE_ASYNC_CALLBACK:
420             context->errorCode = context->objectInfo->cameraSession_->Release();
421             MEDIA_INFO_LOG("Release return : %{public}d", context->errorCode);
422             break;
423         default:
424             MEDIA_DEBUG_LOG("mode is not support");
425             break;
426     }
427     if (context->errorCode != 0) {
428         context->status = false;
429         CameraNapiUtils::CreateNapiErrorObject(env, context->errorCode, context->errorMsg.c_str(), jsContext);
430     }
431     napi_get_undefined(env, &jsContext->data);
432 }
433 
CommonCompleteCallback(napi_env env,napi_status status,void * data)434 static void CommonCompleteCallback(napi_env env, napi_status status, void* data)
435 {
436     auto context = static_cast<CameraSessionAsyncContext*>(data);
437     if (context == nullptr) {
438         MEDIA_ERR_LOG("Async context is null");
439         return;
440     }
441 
442     std::unique_ptr<JSAsyncContextOutput> jsContext = std::make_unique<JSAsyncContextOutput>();
443 
444     if (!context->status) {
445         CameraNapiUtils::CreateNapiErrorObject(env, context->errorCode, context->errorMsg.c_str(), jsContext);
446     } else {
447         PopulateRetVal(env, context->modeForAsync, context, jsContext);
448     }
449 
450     if (!context->funcName.empty() && context->taskId > 0) {
451         // Finish async trace
452         CAMERA_FINISH_ASYNC_TRACE(context->funcName, context->taskId);
453         jsContext->funcName = context->funcName;
454     }
455 
456     if (context->work != nullptr) {
457         CameraNapiUtils::InvokeJSAsyncMethod(env, context->deferred, context->callbackRef,
458                                              context->work, *jsContext);
459     }
460     delete context;
461 }
462 
BeginConfig(napi_env env,napi_callback_info info)463 napi_value CameraSessionNapi::BeginConfig(napi_env env, napi_callback_info info)
464 {
465     MEDIA_INFO_LOG("BeginConfig called");
466     napi_status status;
467     napi_value result = nullptr;
468     size_t argc = ARGS_ZERO;
469     napi_value argv[ARGS_ZERO];
470     napi_value thisVar = nullptr;
471     napi_get_undefined(env, &result);
472     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
473 
474     CameraSessionNapi* cameraSessionNapi = nullptr;
475     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
476     if (status == napi_ok && cameraSessionNapi != nullptr) {
477         int32_t ret = cameraSessionNapi->cameraSession_->BeginConfig();
478         if (!CameraNapiUtils::CheckError(env, ret)) {
479             return nullptr;
480         }
481     }
482     return result;
483 }
484 
CommitConfig(napi_env env,napi_callback_info info)485 napi_value CameraSessionNapi::CommitConfig(napi_env env, napi_callback_info info)
486 {
487     MEDIA_INFO_LOG("CommitConfig called");
488     napi_status status;
489     napi_value result = nullptr;
490     const int32_t refCount = 1;
491     napi_value resource = nullptr;
492     size_t argc = ARGS_ONE;
493     napi_value argv[ARGS_ONE] = {0};
494     napi_value thisVar = nullptr;
495 
496     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
497     NAPI_ASSERT(env, argc <= 1, "requires 1 parameter maximum");
498 
499     napi_get_undefined(env, &result);
500     std::unique_ptr<CameraSessionAsyncContext> asyncContext = std::make_unique<CameraSessionAsyncContext>();
501     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
502     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
503         if (argc == ARGS_ONE) {
504             CAMERA_NAPI_GET_JS_ASYNC_CB_REF(env, argv[PARAM0], refCount, asyncContext->callbackRef);
505         }
506 
507         CAMERA_NAPI_CREATE_PROMISE(env, asyncContext->callbackRef, asyncContext->deferred, result);
508         CAMERA_NAPI_CREATE_RESOURCE_NAME(env, resource, "CommitConfig");
509 
510         status = napi_create_async_work(
511             env, nullptr, resource, [](napi_env env, void* data) {
512                 auto context = static_cast<CameraSessionAsyncContext*>(data);
513                 context->status = false;
514                 // Start async trace
515                 context->funcName = "CameraSessionNapi::CommitConfig";
516                 context->taskId = CameraNapiUtils::IncreamentAndGet(cameraSessionTaskId);
517                 CAMERA_START_ASYNC_TRACE(context->funcName, context->taskId);
518                 if (context->objectInfo != nullptr) {
519                     context->bRetBool = false;
520                     context->status = true;
521                     context->modeForAsync = COMMIT_CONFIG_ASYNC_CALLBACK;
522                 }
523             },
524             CommonCompleteCallback, static_cast<void*>(asyncContext.get()), &asyncContext->work);
525         if (status != napi_ok) {
526             MEDIA_ERR_LOG("Failed to create napi_create_async_work for CommitConfig");
527             napi_get_undefined(env, &result);
528         } else {
529             napi_queue_async_work(env, asyncContext->work);
530             asyncContext.release();
531         }
532     }
533 
534     return result;
535 }
536 
LockForControl(napi_env env,napi_callback_info info)537 napi_value CameraSessionNapi::LockForControl(napi_env env, napi_callback_info info)
538 {
539     MEDIA_INFO_LOG("BeginConfig called");
540     napi_status status;
541     napi_value result = nullptr;
542     size_t argc = ARGS_ZERO;
543     napi_value argv[ARGS_ZERO];
544     napi_value thisVar = nullptr;
545 
546     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
547 
548     napi_get_undefined(env, &result);
549     CameraSessionNapi* cameraSessionNapi = nullptr;
550     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
551     if (status == napi_ok && cameraSessionNapi != nullptr) {
552         cameraSessionNapi->cameraSession_->LockForControl();
553     }
554 
555     return result;
556 }
557 
UnlockForControl(napi_env env,napi_callback_info info)558 napi_value CameraSessionNapi::UnlockForControl(napi_env env, napi_callback_info info)
559 {
560     MEDIA_INFO_LOG("UnlockForControl called");
561     napi_status status;
562     napi_value result = nullptr;
563     size_t argc = ARGS_ZERO;
564     napi_value argv[ARGS_ZERO];
565     napi_value thisVar = nullptr;
566 
567     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
568 
569     napi_get_undefined(env, &result);
570     CameraSessionNapi* cameraSessionNapi = nullptr;
571     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
572     if (status == napi_ok && cameraSessionNapi != nullptr) {
573         cameraSessionNapi->cameraSession_->UnlockForControl();
574     }
575 
576     return result;
577 }
578 
GetJSArgsForCameraInput(napi_env env,size_t argc,const napi_value argv[],sptr<CaptureInput> & cameraInput)579 napi_value GetJSArgsForCameraInput(napi_env env, size_t argc, const napi_value argv[],
580     sptr<CaptureInput> &cameraInput)
581 {
582     napi_value result = nullptr;
583     CameraInputNapi* cameraInputNapiObj = nullptr;
584 
585     NAPI_ASSERT(env, argv != nullptr, "Argument list is empty");
586 
587     for (size_t i = PARAM0; i < argc; i++) {
588         napi_valuetype valueType = napi_undefined;
589         napi_typeof(env, argv[i], &valueType);
590         if (i == PARAM0 && valueType == napi_object) {
591             napi_unwrap(env, argv[i], reinterpret_cast<void**>(&cameraInputNapiObj));
592             if (cameraInputNapiObj != nullptr) {
593                 MEDIA_ERR_LOG("cameraInputNapiObj->GetCameraInput()");
594                 cameraInput = cameraInputNapiObj->GetCameraInput();
595             } else {
596                 NAPI_ASSERT(env, false, "type mismatch");
597             }
598         } else {
599             NAPI_ASSERT(env, false, "type mismatch");
600         }
601     }
602     napi_get_boolean(env, true, &result);
603     return result;
604 }
605 
AddInput(napi_env env,napi_callback_info info)606 napi_value CameraSessionNapi::AddInput(napi_env env, napi_callback_info info)
607 {
608     MEDIA_INFO_LOG("AddInput called");
609     napi_status status;
610     napi_value result = nullptr;
611     size_t argc = ARGS_ONE;
612     napi_value argv[ARGS_ONE] = {0};
613     napi_value thisVar = nullptr;
614 
615     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
616     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, ADD_INPUT)) {
617         return result;
618     }
619 
620     napi_get_undefined(env, &result);
621     CameraSessionNapi* cameraSessionNapi = nullptr;
622     sptr<CaptureInput> cameraInput = nullptr;
623     GetJSArgsForCameraInput(env, argc, argv, cameraInput);
624     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
625     if (status == napi_ok && cameraSessionNapi != nullptr) {
626         int32_t ret = cameraSessionNapi->cameraSession_->AddInput(cameraInput);
627         if (!CameraNapiUtils::CheckError(env, ret)) {
628             return nullptr;
629         }
630     }
631     return result;
632 }
633 
CanAddInput(napi_env env,napi_callback_info info)634 napi_value CameraSessionNapi::CanAddInput(napi_env env, napi_callback_info info)
635 {
636     MEDIA_INFO_LOG("CanAddInput called");
637     napi_status status;
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 
645     napi_get_undefined(env, &result);
646     CameraSessionNapi* cameraSessionNapi = nullptr;
647     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
648     if (status == napi_ok && cameraSessionNapi != nullptr) {
649         sptr<CaptureInput> cameraInput = nullptr;
650         GetJSArgsForCameraInput(env, argc, argv, cameraInput);
651         bool isSupported = cameraSessionNapi->cameraSession_->CanAddInput(cameraInput);
652         napi_get_boolean(env, isSupported, &result);
653     }
654 
655     return result;
656 }
657 
RemoveInput(napi_env env,napi_callback_info info)658 napi_value CameraSessionNapi::RemoveInput(napi_env env, napi_callback_info info)
659 {
660     napi_status status;
661     napi_value result = nullptr;
662     size_t argc = ARGS_ONE;
663     napi_value argv[ARGS_ONE] = {0};
664     napi_value thisVar = nullptr;
665 
666     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
667     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, REMOVE_INPUT)) {
668         return result;
669     }
670 
671     napi_get_undefined(env, &result);
672     CameraSessionNapi* cameraSessionNapi = nullptr;
673     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
674     if (status == napi_ok && cameraSessionNapi != nullptr) {
675         sptr<CaptureInput> cameraInput = nullptr;
676         GetJSArgsForCameraInput(env, argc, argv, cameraInput);
677         int32_t ret = cameraSessionNapi->cameraSession_->RemoveInput(cameraInput);
678         if (!CameraNapiUtils::CheckError(env, ret)) {
679             return nullptr;
680         }
681         return result;
682     }
683 
684     return result;
685 }
686 
GetJSArgsForCameraOutput(napi_env env,size_t argc,const napi_value argv[],sptr<CaptureOutput> & cameraOutput)687 napi_value GetJSArgsForCameraOutput(napi_env env, size_t argc, const napi_value argv[],
688     sptr<CaptureOutput> &cameraOutput)
689 {
690     napi_value result = nullptr;
691     PreviewOutputNapi* previewOutputNapiObj = nullptr;
692     PhotoOutputNapi* photoOutputNapiObj = nullptr;
693     VideoOutputNapi* videoOutputNapiObj = nullptr;
694     MetadataOutputNapi* metadataOutputNapiObj = nullptr;
695 
696     NAPI_ASSERT(env, argv != nullptr, "Argument list is empty");
697 
698     for (size_t i = PARAM0; i < argc; i++) {
699         napi_valuetype valueType = napi_undefined;
700         napi_typeof(env, argv[i], &valueType);
701 
702         if (i == PARAM0 && valueType == napi_object) {
703             if (PreviewOutputNapi::IsPreviewOutput(env, argv[i])) {
704                 MEDIA_INFO_LOG("preview output adding..");
705                 napi_unwrap(env, argv[i], reinterpret_cast<void**>(&previewOutputNapiObj));
706                 cameraOutput = previewOutputNapiObj->GetPreviewOutput();
707             } else if (PhotoOutputNapi::IsPhotoOutput(env, argv[i])) {
708                 MEDIA_INFO_LOG("photo output adding..");
709                 napi_unwrap(env, argv[i], reinterpret_cast<void**>(&photoOutputNapiObj));
710                 cameraOutput = photoOutputNapiObj->GetPhotoOutput();
711             } else if (VideoOutputNapi::IsVideoOutput(env, argv[i])) {
712                 MEDIA_INFO_LOG("video output adding..");
713                 napi_unwrap(env, argv[i], reinterpret_cast<void**>(&videoOutputNapiObj));
714                 cameraOutput = videoOutputNapiObj->GetVideoOutput();
715             } else if (MetadataOutputNapi::IsMetadataOutput(env, argv[i])) {
716                 MEDIA_INFO_LOG("metadata output adding..");
717                 napi_unwrap(env, argv[i], reinterpret_cast<void**>(&metadataOutputNapiObj));
718                 cameraOutput = metadataOutputNapiObj->GetMetadataOutput();
719             } else {
720                 MEDIA_INFO_LOG("invalid output ..");
721                 NAPI_ASSERT(env, false, "type mismatch");
722             }
723         } else {
724             NAPI_ASSERT(env, false, "type mismatch");
725         }
726     }
727     // Return true napi_value if params are successfully obtained
728     napi_get_boolean(env, true, &result);
729     return result;
730 }
731 
AddOutput(napi_env env,napi_callback_info info)732 napi_value CameraSessionNapi::AddOutput(napi_env env, napi_callback_info info)
733 {
734     MEDIA_INFO_LOG("AddOutput called");
735     napi_status status;
736     napi_value result = nullptr;
737     size_t argc = ARGS_ONE;
738     napi_value argv[ARGS_ONE] = {0};
739     napi_value thisVar = nullptr;
740 
741     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
742     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, ADD_OUTPUT)) {
743         return result;
744     }
745 
746     napi_get_undefined(env, &result);
747     CameraSessionNapi* cameraSessionNapi = nullptr;
748     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
749     if (status == napi_ok && cameraSessionNapi != nullptr) {
750         sptr<CaptureOutput> cameraOutput = nullptr;
751         result = GetJSArgsForCameraOutput(env, argc, argv, cameraOutput);
752         int32_t ret = cameraSessionNapi->cameraSession_->AddOutput(cameraOutput);
753         if (!CameraNapiUtils::CheckError(env, ret)) {
754             return nullptr;
755         }
756     }
757     return result;
758 }
759 
CanAddOutput(napi_env env,napi_callback_info info)760 napi_value CameraSessionNapi::CanAddOutput(napi_env env, napi_callback_info info)
761 {
762     MEDIA_INFO_LOG("CanAddOutput called");
763     napi_status status;
764     napi_value result = nullptr;
765     size_t argc = ARGS_ONE;
766     napi_value argv[ARGS_ONE] = {0};
767     napi_value thisVar = nullptr;
768 
769     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
770 
771     napi_get_undefined(env, &result);
772     CameraSessionNapi* cameraSessionNapi = nullptr;
773     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
774     if (status == napi_ok && cameraSessionNapi != nullptr) {
775         sptr<CaptureOutput> cameraOutput = nullptr;
776         result = GetJSArgsForCameraOutput(env, argc, argv, cameraOutput);
777         bool isSupported = cameraSessionNapi->cameraSession_->CanAddOutput(cameraOutput);
778         napi_get_boolean(env, isSupported, &result);
779     }
780 
781     return result;
782 }
783 
RemoveOutput(napi_env env,napi_callback_info info)784 napi_value CameraSessionNapi::RemoveOutput(napi_env env, napi_callback_info info)
785 {
786     MEDIA_INFO_LOG("RemoveOutput called");
787     napi_status status;
788     napi_value result = nullptr;
789     size_t argc = ARGS_ONE;
790     napi_value argv[ARGS_ONE] = {0};
791     napi_value thisVar = nullptr;
792 
793     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
794     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, REMOVE_OUTPUT)) {
795         return result;
796     }
797 
798     napi_get_undefined(env, &result);
799     CameraSessionNapi* cameraSessionNapi = nullptr;
800     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
801     if (status == napi_ok && cameraSessionNapi != nullptr) {
802         sptr<CaptureOutput> cameraOutput = nullptr;
803         result = GetJSArgsForCameraOutput(env, argc, argv, cameraOutput);
804         int32_t ret = cameraSessionNapi->cameraSession_->RemoveOutput(cameraOutput);
805         if (!CameraNapiUtils::CheckError(env, ret)) {
806             return nullptr;
807         }
808     }
809 
810     return result;
811 }
812 
Start(napi_env env,napi_callback_info info)813 napi_value CameraSessionNapi::Start(napi_env env, napi_callback_info info)
814 {
815     MEDIA_INFO_LOG("start called");
816     napi_status status;
817     napi_value result = nullptr;
818     const int32_t refCount = 1;
819     napi_value resource = nullptr;
820     size_t argc = ARGS_ONE;
821     napi_value argv[ARGS_ONE] = {0};
822     napi_value thisVar = nullptr;
823 
824     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
825     NAPI_ASSERT(env, argc <= 1, "requires 1 parameter maximum");
826 
827     napi_get_undefined(env, &result);
828     std::unique_ptr<CameraSessionAsyncContext> asyncContext = std::make_unique<CameraSessionAsyncContext>();
829     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
830     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
831         if (argc == ARGS_ONE) {
832             CAMERA_NAPI_GET_JS_ASYNC_CB_REF(env, argv[PARAM0], refCount, asyncContext->callbackRef);
833         }
834 
835         CAMERA_NAPI_CREATE_PROMISE(env, asyncContext->callbackRef, asyncContext->deferred, result);
836         CAMERA_NAPI_CREATE_RESOURCE_NAME(env, resource, "Start");
837 
838         status = napi_create_async_work(
839             env, nullptr, resource, [](napi_env env, void* data) {
840                 auto context = static_cast<CameraSessionAsyncContext*>(data);
841                 context->status = false;
842                 // Start async trace
843                 context->funcName = "CameraSessionNapi::Start";
844                 context->taskId = CameraNapiUtils::IncreamentAndGet(cameraSessionTaskId);
845                 CAMERA_START_ASYNC_TRACE(context->funcName, context->taskId);
846                 if (context->objectInfo != nullptr) {
847                     context->bRetBool = false;
848                     context->status = true;
849                     context->modeForAsync = SESSION_START_ASYNC_CALLBACK;
850                 }
851             },
852             CommonCompleteCallback, static_cast<void*>(asyncContext.get()), &asyncContext->work);
853         if (status != napi_ok) {
854             MEDIA_ERR_LOG("Failed to create napi_create_async_work for CameraSessionNapi::Start");
855             napi_get_undefined(env, &result);
856         } else {
857             napi_queue_async_work(env, asyncContext->work);
858             asyncContext.release();
859         }
860     }
861 
862     return result;
863 }
864 
Stop(napi_env env,napi_callback_info info)865 napi_value CameraSessionNapi::Stop(napi_env env, napi_callback_info info)
866 {
867     MEDIA_INFO_LOG("Stop called");
868     napi_status status;
869     napi_value result = nullptr;
870     const int32_t refCount = 1;
871     napi_value resource = nullptr;
872     size_t argc = ARGS_ONE;
873     napi_value argv[ARGS_ONE] = {0};
874     napi_value thisVar = nullptr;
875 
876     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
877     NAPI_ASSERT(env, argc <= 1, "requires 1 parameter maximum");
878 
879     napi_get_undefined(env, &result);
880     std::unique_ptr<CameraSessionAsyncContext> asyncContext = std::make_unique<CameraSessionAsyncContext>();
881     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
882     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
883         if (argc == ARGS_ONE) {
884             CAMERA_NAPI_GET_JS_ASYNC_CB_REF(env, argv[PARAM0], refCount, asyncContext->callbackRef);
885         }
886 
887         CAMERA_NAPI_CREATE_PROMISE(env, asyncContext->callbackRef, asyncContext->deferred, result);
888         CAMERA_NAPI_CREATE_RESOURCE_NAME(env, resource, "Stop");
889 
890         status = napi_create_async_work(
891             env, nullptr, resource, [](napi_env env, void* data) {
892                 auto context = static_cast<CameraSessionAsyncContext*>(data);
893                 context->status = false;
894                 // Start async trace
895                 context->funcName = "CameraSessionNapi::Stop";
896                 context->taskId = CameraNapiUtils::IncreamentAndGet(cameraSessionTaskId);
897                 CAMERA_START_ASYNC_TRACE(context->funcName, context->taskId);
898                 if (context->objectInfo != nullptr) {
899                     context->bRetBool = false;
900                     context->status = true;
901                     context->modeForAsync = SESSION_STOP_ASYNC_CALLBACK;
902                 }
903             },
904             CommonCompleteCallback, static_cast<void*>(asyncContext.get()), &asyncContext->work);
905         if (status != napi_ok) {
906             MEDIA_ERR_LOG("Failed to create napi_create_async_work for CameraSessionNapi::Stop");
907             napi_get_undefined(env, &result);
908         } else {
909             napi_queue_async_work(env, asyncContext->work);
910             asyncContext.release();
911         }
912     }
913 
914     return result;
915 }
916 
Release(napi_env env,napi_callback_info info)917 napi_value CameraSessionNapi::Release(napi_env env, napi_callback_info info)
918 {
919     MEDIA_INFO_LOG("Release called");
920     napi_status status;
921     napi_value result = nullptr;
922     const int32_t refCount = 1;
923     napi_value resource = nullptr;
924     size_t argc = ARGS_ONE;
925     napi_value argv[ARGS_ONE] = {0};
926     napi_value thisVar = nullptr;
927 
928     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
929     NAPI_ASSERT(env, argc <= 1, "requires 1 parameter maximum");
930 
931     napi_get_undefined(env, &result);
932     std::unique_ptr<CameraSessionAsyncContext> asyncContext = std::make_unique<CameraSessionAsyncContext>();
933     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
934     if (status == napi_ok && asyncContext->objectInfo != nullptr) {
935         if (argc == ARGS_ONE) {
936             CAMERA_NAPI_GET_JS_ASYNC_CB_REF(env, argv[PARAM0], refCount, asyncContext->callbackRef);
937         }
938 
939         CAMERA_NAPI_CREATE_PROMISE(env, asyncContext->callbackRef, asyncContext->deferred, result);
940         CAMERA_NAPI_CREATE_RESOURCE_NAME(env, resource, "Release");
941 
942         status = napi_create_async_work(
943             env, nullptr, resource, [](napi_env env, void* data) {
944                 auto context = static_cast<CameraSessionAsyncContext*>(data);
945                 context->status = false;
946                 // Start async trace
947                 context->funcName = "CameraSessionNapi::Release";
948                 context->taskId = CameraNapiUtils::IncreamentAndGet(cameraSessionTaskId);
949                 CAMERA_START_ASYNC_TRACE(context->funcName, context->taskId);
950                 if (context->objectInfo != nullptr) {
951                     context->bRetBool = false;
952                     context->status = true;
953                     context->modeForAsync = SESSION_RELEASE_ASYNC_CALLBACK;
954                 }
955             },
956             CommonCompleteCallback, static_cast<void*>(asyncContext.get()), &asyncContext->work);
957         if (status != napi_ok) {
958             MEDIA_ERR_LOG("Failed to create napi_create_async_work for CameraSessionNapi::Release");
959             napi_get_undefined(env, &result);
960         } else {
961             napi_queue_async_work(env, asyncContext->work);
962             asyncContext.release();
963         }
964     }
965 
966     return result;
967 }
968 
IsVideoStabilizationModeSupported(napi_env env,napi_callback_info info)969 napi_value CameraSessionNapi::IsVideoStabilizationModeSupported(napi_env env, napi_callback_info info)
970 {
971     napi_status status;
972     napi_value result = nullptr;
973     size_t argc = ARGS_ONE;
974     napi_value argv[ARGS_ONE] = {0};
975     napi_value thisVar = nullptr;
976 
977     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
978 
979     napi_get_undefined(env, &result);
980     CameraSessionNapi* cameraSessionNapi = nullptr;
981     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
982     if (status == napi_ok && cameraSessionNapi != nullptr) {
983         int32_t value;
984         napi_get_value_int32(env, argv[PARAM0], &value);
985         VideoStabilizationMode videoStabilizationMode = (VideoStabilizationMode)value;
986         bool isSupported;
987         int32_t retCode = cameraSessionNapi->cameraSession_->
988                           IsVideoStabilizationModeSupported(videoStabilizationMode, isSupported);
989         if (!CameraNapiUtils::CheckError(env, retCode)) {
990             return nullptr;
991         }
992         napi_get_boolean(env, isSupported, &result);
993     }
994 
995     return result;
996 }
997 
GetActiveVideoStabilizationMode(napi_env env,napi_callback_info info)998 napi_value CameraSessionNapi::GetActiveVideoStabilizationMode(napi_env env, napi_callback_info info)
999 {
1000     napi_status status;
1001     napi_value result = nullptr;
1002     size_t argc = ARGS_ZERO;
1003     napi_value argv[ARGS_ZERO];
1004     napi_value thisVar = nullptr;
1005 
1006     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1007 
1008     napi_get_undefined(env, &result);
1009     CameraSessionNapi* cameraSessionNapi = nullptr;
1010     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1011     if (status == napi_ok && cameraSessionNapi != nullptr) {
1012         VideoStabilizationMode videoStabilizationMode;
1013         int32_t retCode = cameraSessionNapi->cameraSession_->
1014                           GetActiveVideoStabilizationMode(videoStabilizationMode);
1015         if (!CameraNapiUtils::CheckError(env, retCode)) {
1016             return nullptr;
1017         }
1018         napi_create_int32(env, videoStabilizationMode, &result);
1019     }
1020 
1021     return result;
1022 }
1023 
SetVideoStabilizationMode(napi_env env,napi_callback_info info)1024 napi_value CameraSessionNapi::SetVideoStabilizationMode(napi_env env, napi_callback_info info)
1025 {
1026     napi_status status;
1027     napi_value result = nullptr;
1028     size_t argc = ARGS_ONE;
1029     napi_value argv[ARGS_ONE] = {0};
1030     napi_value thisVar = nullptr;
1031 
1032     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1033 
1034     napi_get_undefined(env, &result);
1035     CameraSessionNapi* cameraSessionNapi = nullptr;
1036     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1037     if (status == napi_ok && cameraSessionNapi != nullptr) {
1038         int32_t value;
1039         napi_get_value_int32(env, argv[PARAM0], &value);
1040         VideoStabilizationMode videoStabilizationMode = (VideoStabilizationMode)value;
1041         int retCode = cameraSessionNapi->cameraSession_->SetVideoStabilizationMode(videoStabilizationMode);
1042         if (!CameraNapiUtils::CheckError(env, retCode)) {
1043             return nullptr;
1044         }
1045     }
1046     return result;
1047 }
1048 
HasFlash(napi_env env,napi_callback_info info)1049 napi_value CameraSessionNapi::HasFlash(napi_env env, napi_callback_info info)
1050 {
1051     napi_status status;
1052     napi_value result = nullptr;
1053     size_t argc = ARGS_ZERO;
1054     napi_value argv[ARGS_ZERO];
1055     napi_value thisVar = nullptr;
1056 
1057     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1058 
1059     napi_get_undefined(env, &result);
1060     CameraSessionNapi* cameraSessionNapi = nullptr;
1061     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1062     if (status == napi_ok && cameraSessionNapi != nullptr) {
1063         std::vector<FlashMode> flashModes;
1064         int retCode = cameraSessionNapi->cameraSession_->GetSupportedFlashModes(flashModes);
1065         if (!CameraNapiUtils::CheckError(env, retCode)) {
1066             return nullptr;
1067         }
1068         bool isSupported = !(flashModes.empty());
1069         napi_get_boolean(env, isSupported, &result);
1070     }
1071     return result;
1072 }
1073 
IsFlashModeSupported(napi_env env,napi_callback_info info)1074 napi_value CameraSessionNapi::IsFlashModeSupported(napi_env env, napi_callback_info info)
1075 {
1076     napi_status status;
1077     napi_value result = nullptr;
1078     size_t argc = ARGS_ONE;
1079     napi_value argv[ARGS_ONE] = {0};
1080     napi_value thisVar = nullptr;
1081 
1082     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1083 
1084     napi_get_undefined(env, &result);
1085     CameraSessionNapi* cameraSessionNapi = nullptr;
1086     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1087     if (status == napi_ok && cameraSessionNapi != nullptr) {
1088         int32_t value;
1089         napi_get_value_int32(env, argv[PARAM0], &value);
1090         FlashMode flashMode = (FlashMode)value;
1091         bool isSupported;
1092         int32_t retCode = cameraSessionNapi->cameraSession_->IsFlashModeSupported(flashMode, isSupported);
1093         if (!CameraNapiUtils::CheckError(env, retCode)) {
1094             return nullptr;
1095         }
1096         napi_get_boolean(env, isSupported, &result);
1097     }
1098 
1099     return result;
1100 }
1101 
SetFlashMode(napi_env env,napi_callback_info info)1102 napi_value CameraSessionNapi::SetFlashMode(napi_env env, napi_callback_info info)
1103 {
1104     CAMERA_SYNC_TRACE;
1105     napi_status status;
1106     napi_value result = nullptr;
1107     size_t argc = ARGS_ONE;
1108     napi_value argv[ARGS_ONE] = {0};
1109     napi_value thisVar = nullptr;
1110 
1111     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1112 
1113     napi_get_undefined(env, &result);
1114     CameraSessionNapi* cameraSessionNapi = nullptr;
1115     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1116     if (status == napi_ok && cameraSessionNapi != nullptr) {
1117         int32_t value;
1118         napi_get_value_int32(env, argv[PARAM0], &value);
1119         FlashMode flashMode = (FlashMode)value;
1120         cameraSessionNapi->cameraSession_->LockForControl();
1121         int retCode = cameraSessionNapi->cameraSession_->SetFlashMode(flashMode);
1122         cameraSessionNapi->cameraSession_->UnlockForControl();
1123         if (!CameraNapiUtils::CheckError(env, retCode)) {
1124             return nullptr;
1125         }
1126     }
1127     return result;
1128 }
1129 
GetFlashMode(napi_env env,napi_callback_info info)1130 napi_value CameraSessionNapi::GetFlashMode(napi_env env, napi_callback_info info)
1131 {
1132     napi_status status;
1133     napi_value result = nullptr;
1134     size_t argc = ARGS_ZERO;
1135     napi_value argv[ARGS_ZERO];
1136     napi_value thisVar = nullptr;
1137 
1138     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1139     napi_get_undefined(env, &result);
1140     CameraSessionNapi* cameraSessionNapi = nullptr;
1141     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1142     if (status == napi_ok && cameraSessionNapi != nullptr) {
1143         FlashMode flashMode;
1144         int32_t retCode = cameraSessionNapi->cameraSession_->GetFlashMode(flashMode);
1145         if (!CameraNapiUtils::CheckError(env, retCode)) {
1146             return nullptr;
1147         }
1148         napi_create_int32(env, flashMode, &result);
1149     }
1150 
1151     return result;
1152 }
1153 
IsExposureModeSupported(napi_env env,napi_callback_info info)1154 napi_value CameraSessionNapi::IsExposureModeSupported(napi_env env, napi_callback_info info)
1155 {
1156     napi_status status;
1157     napi_value result = nullptr;
1158     size_t argc = ARGS_ONE;
1159     napi_value argv[ARGS_ONE] = {0};
1160     napi_value thisVar = nullptr;
1161 
1162     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1163 
1164     napi_get_undefined(env, &result);
1165     CameraSessionNapi* cameraSessionNapi = nullptr;
1166     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1167     if (status == napi_ok && cameraSessionNapi != nullptr) {
1168         int32_t value;
1169         napi_get_value_int32(env, argv[PARAM0], &value);
1170         ExposureMode exposureMode = (ExposureMode)value;
1171         bool isSupported;
1172         int32_t retCode = cameraSessionNapi->cameraSession_->
1173                     IsExposureModeSupported(static_cast<ExposureMode>(exposureMode), isSupported);
1174         if (!CameraNapiUtils::CheckError(env, retCode)) {
1175             return nullptr;
1176         }
1177         napi_get_boolean(env, isSupported, &result);
1178     }
1179     return result;
1180 }
1181 
GetExposureMode(napi_env env,napi_callback_info info)1182 napi_value CameraSessionNapi::GetExposureMode(napi_env env, napi_callback_info info)
1183 {
1184     napi_status status;
1185     napi_value result = nullptr;
1186     size_t argc = ARGS_ZERO;
1187     napi_value argv[ARGS_ZERO];
1188     napi_value thisVar = nullptr;
1189 
1190     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1191 
1192     napi_get_undefined(env, &result);
1193     CameraSessionNapi* cameraSessionNapi = nullptr;
1194     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1195     if (status == napi_ok && cameraSessionNapi != nullptr) {
1196         ExposureMode exposureMode;
1197         int32_t retCode = cameraSessionNapi->cameraSession_->GetExposureMode(exposureMode);
1198         if (!CameraNapiUtils::CheckError(env, retCode)) {
1199             return nullptr;
1200         }
1201         napi_create_int32(env, exposureMode, &result);
1202     }
1203 
1204     return result;
1205 }
1206 
SetExposureMode(napi_env env,napi_callback_info info)1207 napi_value CameraSessionNapi::SetExposureMode(napi_env env, napi_callback_info info)
1208 {
1209     CAMERA_SYNC_TRACE;
1210     napi_status status;
1211     napi_value result = nullptr;
1212     size_t argc = ARGS_ONE;
1213     napi_value argv[ARGS_ONE] = {0};
1214     napi_value thisVar = nullptr;
1215 
1216     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1217 
1218     napi_get_undefined(env, &result);
1219     CameraSessionNapi* cameraSessionNapi = nullptr;
1220     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1221     if (status == napi_ok && cameraSessionNapi != nullptr) {
1222         int32_t value;
1223         napi_get_value_int32(env, argv[PARAM0], &value);
1224         ExposureMode exposureMode = (ExposureMode)value;
1225         cameraSessionNapi->cameraSession_->LockForControl();
1226         int retCode = cameraSessionNapi->cameraSession_->SetExposureMode(exposureMode);
1227         cameraSessionNapi->cameraSession_->UnlockForControl();
1228         if (!CameraNapiUtils::CheckError(env, retCode)) {
1229             return nullptr;
1230         }
1231     }
1232 
1233     return result;
1234 }
1235 
SetMeteringPoint(napi_env env,napi_callback_info info)1236 napi_value CameraSessionNapi::SetMeteringPoint(napi_env env, napi_callback_info info)
1237 {
1238     CAMERA_SYNC_TRACE;
1239     napi_status status;
1240     napi_value result = nullptr;
1241     size_t argc = ARGS_ONE;
1242     napi_value argv[ARGS_ONE] = {0};
1243     napi_value thisVar = nullptr;
1244 
1245     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1246 
1247     napi_get_undefined(env, &result);
1248     CameraSessionNapi* cameraSessionNapi = nullptr;
1249     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1250     if (status == napi_ok && cameraSessionNapi != nullptr) {
1251         Point exposurePoint;
1252         if (GetPointProperties(env, argv[PARAM0], exposurePoint) == 0) {
1253             cameraSessionNapi->cameraSession_->LockForControl();
1254             int32_t retCode = cameraSessionNapi->cameraSession_->SetMeteringPoint(exposurePoint);
1255             cameraSessionNapi->cameraSession_->UnlockForControl();
1256             if (!CameraNapiUtils::CheckError(env, retCode)) {
1257                 return nullptr;
1258             }
1259         } else {
1260             MEDIA_ERR_LOG("get point failed");
1261         }
1262     }
1263 
1264     return result;
1265 }
1266 
GetMeteringPoint(napi_env env,napi_callback_info info)1267 napi_value CameraSessionNapi::GetMeteringPoint(napi_env env, napi_callback_info info)
1268 {
1269     napi_status status;
1270     napi_value result = nullptr;
1271     size_t argc = ARGS_ZERO;
1272     napi_value argv[ARGS_ZERO];
1273     napi_value thisVar = nullptr;
1274 
1275     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1276     napi_get_undefined(env, &result);
1277     CameraSessionNapi* cameraSessionNapi = nullptr;
1278     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1279     if (status == napi_ok && cameraSessionNapi != nullptr) {
1280         Point exposurePoint;
1281         int32_t retCode = cameraSessionNapi->cameraSession_->GetMeteringPoint(exposurePoint);
1282         if (!CameraNapiUtils::CheckError(env, retCode)) {
1283             return nullptr;
1284         }
1285         return GetPointNapiValue(env, exposurePoint);
1286     }
1287 
1288     return result;
1289 }
1290 
GetExposureBiasRange(napi_env env,napi_callback_info info)1291 napi_value CameraSessionNapi::GetExposureBiasRange(napi_env env, napi_callback_info info)
1292 {
1293     napi_status status;
1294     napi_value result = nullptr;
1295     size_t argc = ARGS_ZERO;
1296     napi_value argv[ARGS_ZERO];
1297     napi_value thisVar = nullptr;
1298 
1299     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1300 
1301     napi_get_undefined(env, &result);
1302     CameraSessionNapi* cameraSessionNapi = nullptr;
1303     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1304     if (status == napi_ok && cameraSessionNapi != nullptr) {
1305         std::vector<int32_t> vecExposureBiasList;
1306         int32_t retCode = cameraSessionNapi->cameraSession_->GetExposureBiasRange(vecExposureBiasList);
1307         if (!CameraNapiUtils::CheckError(env, retCode)) {
1308             return nullptr;
1309         }
1310         if (vecExposureBiasList.empty() || napi_create_array(env, &result) != napi_ok) {
1311             return result;
1312         }
1313         int32_t j = 0;
1314         size_t len = vecExposureBiasList.size();
1315         for (size_t i = 0; i < len; i++) {
1316             int32_t  exposureBias = vecExposureBiasList[i];
1317             MEDIA_DEBUG_LOG("EXPOSURE_BIAS_RANGE : exposureBias = %{public}d", vecExposureBiasList[i]);
1318             napi_value value;
1319             if (napi_create_int32(env, exposureBias, &value) == napi_ok) {
1320                 napi_set_element(env, result, j, value);
1321                 j++;
1322             }
1323         }
1324         MEDIA_DEBUG_LOG("EXPOSURE_BIAS_RANGE ExposureBiasList size : %{public}zu", vecExposureBiasList.size());
1325     }
1326 
1327     return result;
1328 }
1329 
GetExposureValue(napi_env env,napi_callback_info info)1330 napi_value CameraSessionNapi::GetExposureValue(napi_env env, napi_callback_info info)
1331 {
1332     napi_status status;
1333     napi_value result = nullptr;
1334     size_t argc = ARGS_ZERO;
1335     napi_value argv[ARGS_ZERO];
1336     napi_value thisVar = nullptr;
1337 
1338     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1339 
1340     napi_get_undefined(env, &result);
1341     CameraSessionNapi* cameraSessionNapi = nullptr;
1342     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1343     if (status == napi_ok && cameraSessionNapi!= nullptr) {
1344         int32_t exposureValue;
1345         int32_t retCode = cameraSessionNapi->cameraSession_->GetExposureValue(exposureValue);
1346         if (!CameraNapiUtils::CheckError(env, retCode)) {
1347             return nullptr;
1348         }
1349         napi_create_int32(env, exposureValue, &result);
1350     }
1351     return result;
1352 }
1353 
SetExposureBias(napi_env env,napi_callback_info info)1354 napi_value CameraSessionNapi::SetExposureBias(napi_env env, napi_callback_info info)
1355 {
1356     CAMERA_SYNC_TRACE;
1357     napi_status status;
1358     napi_value result = nullptr;
1359     size_t argc = ARGS_ONE;
1360     napi_value argv[ARGS_ONE] = {0};
1361     napi_value thisVar = nullptr;
1362 
1363     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1364 
1365     napi_get_undefined(env, &result);
1366     CameraSessionNapi* cameraSessionNapi = nullptr;
1367     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1368     if (status == napi_ok && cameraSessionNapi != nullptr) {
1369         int32_t exposureValue;
1370         napi_get_value_int32(env, argv[0], &exposureValue);
1371         cameraSessionNapi->cameraSession_->LockForControl();
1372         int32_t retCode = cameraSessionNapi->cameraSession_->SetExposureBias(exposureValue);
1373         cameraSessionNapi->cameraSession_->UnlockForControl();
1374         if (!CameraNapiUtils::CheckError(env, retCode)) {
1375             return nullptr;
1376         }
1377     }
1378 
1379     return result;
1380 }
1381 
IsFocusModeSupported(napi_env env,napi_callback_info info)1382 napi_value CameraSessionNapi::IsFocusModeSupported(napi_env env, napi_callback_info info)
1383 {
1384     MEDIA_INFO_LOG("IsFocusModeSupported called");
1385     napi_status status;
1386     napi_value result = nullptr;
1387     size_t argc = ARGS_ONE;
1388     napi_value argv[ARGS_ONE] = {0};
1389     napi_value thisVar = nullptr;
1390 
1391     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1392 
1393     napi_get_undefined(env, &result);
1394     CameraSessionNapi* cameraSessionNapi = nullptr;
1395     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1396     if (status == napi_ok && cameraSessionNapi != nullptr) {
1397         int32_t value;
1398         napi_get_value_int32(env, argv[PARAM0], &value);
1399         FocusMode focusMode = (FocusMode)value;
1400         bool isSupported;
1401         int32_t retCode = cameraSessionNapi->cameraSession_->IsFocusModeSupported(focusMode,
1402                                                                                   isSupported);
1403         if (!CameraNapiUtils::CheckError(env, retCode)) {
1404             return nullptr;
1405         }
1406         napi_get_boolean(env, isSupported, &result);
1407     }
1408 
1409     return result;
1410 }
1411 
GetFocalLength(napi_env env,napi_callback_info info)1412 napi_value CameraSessionNapi::GetFocalLength(napi_env env, napi_callback_info info)
1413 {
1414     napi_status status;
1415     napi_value result = nullptr;
1416     size_t argc = ARGS_ZERO;
1417     napi_value argv[ARGS_ZERO];
1418     napi_value thisVar = nullptr;
1419 
1420     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1421 
1422     napi_get_undefined(env, &result);
1423     CameraSessionNapi* cameraSessionNapi = nullptr;
1424     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1425     if (status == napi_ok && cameraSessionNapi != nullptr) {
1426         float focalLength;
1427         int32_t retCode = cameraSessionNapi->cameraSession_->GetFocalLength(focalLength);
1428         if (!CameraNapiUtils::CheckError(env, retCode)) {
1429             return nullptr;
1430         }
1431         napi_create_double(env, focalLength, &result);
1432     }
1433 
1434     return result;
1435 }
1436 
SetFocusPoint(napi_env env,napi_callback_info info)1437 napi_value CameraSessionNapi::SetFocusPoint(napi_env env, napi_callback_info info)
1438 {
1439     CAMERA_SYNC_TRACE;
1440     napi_status status;
1441     napi_value result = nullptr;
1442     size_t argc = ARGS_ONE;
1443     napi_value argv[ARGS_ONE] = {0};
1444     napi_value thisVar = nullptr;
1445 
1446     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1447 
1448     napi_get_undefined(env, &result);
1449     CameraSessionNapi* cameraSessionNapi = nullptr;
1450     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1451     if (status == napi_ok && cameraSessionNapi != nullptr) {
1452         Point focusPoint;
1453         if (GetPointProperties(env, argv[PARAM0], focusPoint) == 0) {
1454             cameraSessionNapi->cameraSession_->LockForControl();
1455             int32_t retCode = cameraSessionNapi->cameraSession_->SetFocusPoint(focusPoint);
1456             cameraSessionNapi->cameraSession_->UnlockForControl();
1457             if (!CameraNapiUtils::CheckError(env, retCode)) {
1458                 return nullptr;
1459             }
1460         } else {
1461             MEDIA_ERR_LOG("get point failed");
1462         }
1463     }
1464 
1465     return result;
1466 }
1467 
GetFocusPoint(napi_env env,napi_callback_info info)1468 napi_value CameraSessionNapi::GetFocusPoint(napi_env env, napi_callback_info info)
1469 {
1470     napi_status status;
1471     napi_value result = nullptr;
1472     size_t argc = ARGS_ZERO;
1473     napi_value argv[ARGS_ZERO];
1474     napi_value thisVar = nullptr;
1475 
1476     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1477 
1478     napi_get_undefined(env, &result);
1479     CameraSessionNapi* cameraSessionNapi = nullptr;
1480     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1481     if (status == napi_ok && cameraSessionNapi != nullptr) {
1482         Point focusPoint;
1483         int32_t retCode = cameraSessionNapi->cameraSession_->GetFocusPoint(focusPoint);
1484         if (!CameraNapiUtils::CheckError(env, retCode)) {
1485             return nullptr;
1486         }
1487         return GetPointNapiValue(env, focusPoint);
1488     }
1489 
1490     return result;
1491 }
1492 
GetFocusMode(napi_env env,napi_callback_info info)1493 napi_value CameraSessionNapi::GetFocusMode(napi_env env, napi_callback_info info)
1494 {
1495     napi_status status;
1496     napi_value result = nullptr;
1497     size_t argc = ARGS_ZERO;
1498     napi_value argv[ARGS_ZERO];
1499     napi_value thisVar = nullptr;
1500 
1501     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1502 
1503     napi_get_undefined(env, &result);
1504     CameraSessionNapi* cameraSessionNapi = nullptr;
1505     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1506     if (status == napi_ok && cameraSessionNapi != nullptr) {
1507         FocusMode focusMode;
1508         int32_t retCode = cameraSessionNapi->cameraSession_->GetFocusMode(focusMode);
1509         if (!CameraNapiUtils::CheckError(env, retCode)) {
1510             return nullptr;
1511         }
1512         napi_create_int32(env, focusMode, &result);
1513     }
1514 
1515     return result;
1516 }
1517 
SetFocusMode(napi_env env,napi_callback_info info)1518 napi_value CameraSessionNapi::SetFocusMode(napi_env env, napi_callback_info info)
1519 {
1520     CAMERA_SYNC_TRACE;
1521     napi_status status;
1522     napi_value result = nullptr;
1523     size_t argc = ARGS_ONE;
1524     napi_value argv[ARGS_ONE] = {0};
1525     napi_value thisVar = nullptr;
1526 
1527     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1528 
1529     napi_get_undefined(env, &result);
1530     CameraSessionNapi* cameraSessionNapi = nullptr;
1531     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1532     if (status == napi_ok && cameraSessionNapi != nullptr) {
1533         int32_t value;
1534         napi_get_value_int32(env, argv[PARAM0], &value);
1535         FocusMode focusMode = (FocusMode)value;
1536         cameraSessionNapi->cameraSession_->LockForControl();
1537         int retCode = cameraSessionNapi->cameraSession_->
1538                 SetFocusMode(static_cast<FocusMode>(focusMode));
1539         cameraSessionNapi->cameraSession_->UnlockForControl();
1540         if (!CameraNapiUtils::CheckError(env, retCode)) {
1541             return nullptr;
1542         }
1543     }
1544     return result;
1545 }
1546 
GetZoomRatioRange(napi_env env,napi_callback_info info)1547 napi_value CameraSessionNapi::GetZoomRatioRange(napi_env env, napi_callback_info info)
1548 {
1549     napi_status status;
1550     napi_value result = nullptr;
1551     size_t argc = ARGS_ZERO;
1552     napi_value argv[ARGS_ZERO];
1553     napi_value thisVar = nullptr;
1554 
1555     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1556 
1557     napi_get_undefined(env, &result);
1558     CameraSessionNapi* cameraSessionNapi = nullptr;
1559     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1560     if (status == napi_ok && cameraSessionNapi != nullptr) {
1561         std::vector<float> vecZoomRatioList;
1562         int32_t retCode = cameraSessionNapi->cameraSession_->GetZoomRatioRange(vecZoomRatioList);
1563         if (!CameraNapiUtils::CheckError(env, retCode)) {
1564             return nullptr;
1565         }
1566         MEDIA_INFO_LOG("CameraSessionNapi::GetZoomRatioRange len = %{public}zu",
1567             vecZoomRatioList.size());
1568 
1569         if (!vecZoomRatioList.empty() && napi_create_array(env, &result) == napi_ok) {
1570             for (size_t i = 0; i < vecZoomRatioList.size(); i++) {
1571                 float zoomRatio = vecZoomRatioList[i];
1572                 napi_value value;
1573                 napi_create_double(env, zoomRatio, &value);
1574                 napi_set_element(env, result, i, value);
1575             }
1576         } else {
1577             MEDIA_ERR_LOG("vecSupportedZoomRatioList is empty or failed to create array!");
1578         }
1579     }
1580 
1581     return result;
1582 }
1583 
GetZoomRatio(napi_env env,napi_callback_info info)1584 napi_value CameraSessionNapi::GetZoomRatio(napi_env env, napi_callback_info info)
1585 {
1586     napi_status status;
1587     napi_value result = nullptr;
1588     size_t argc = ARGS_ZERO;
1589     napi_value argv[ARGS_ZERO];
1590     napi_value thisVar = nullptr;
1591 
1592     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1593 
1594     napi_get_undefined(env, &result);
1595     CameraSessionNapi* cameraSessionNapi = nullptr;
1596     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1597     if (status == napi_ok && cameraSessionNapi != nullptr) {
1598         float zoomRatio;
1599         int32_t retCode = cameraSessionNapi->cameraSession_->GetZoomRatio(zoomRatio);
1600         if (!CameraNapiUtils::CheckError(env, retCode)) {
1601             return nullptr;
1602         }
1603         napi_create_double(env, zoomRatio, &result);
1604     }
1605 
1606     return result;
1607 }
1608 
SetZoomRatio(napi_env env,napi_callback_info info)1609 napi_value CameraSessionNapi::SetZoomRatio(napi_env env, napi_callback_info info)
1610 {
1611     CAMERA_SYNC_TRACE;
1612     napi_status status;
1613     napi_value result = nullptr;
1614 
1615     size_t argc = ARGS_ONE;
1616     napi_value argv[ARGS_ONE];
1617     napi_value thisVar = nullptr;
1618 
1619     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1620 
1621     napi_get_undefined(env, &result);
1622     CameraSessionNapi* cameraSessionNapi = nullptr;
1623     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1624     if (status == napi_ok && cameraSessionNapi != nullptr) {
1625         double zoomRatio;
1626         napi_get_value_double(env, argv[PARAM0], &zoomRatio);
1627         cameraSessionNapi->cameraSession_->LockForControl();
1628         int32_t retCode = cameraSessionNapi->cameraSession_->SetZoomRatio((float)zoomRatio);
1629         cameraSessionNapi->cameraSession_->UnlockForControl();
1630         if (!CameraNapiUtils::CheckError(env, retCode)) {
1631             return nullptr;
1632         }
1633     }
1634 
1635     return result;
1636 }
1637 
On(napi_env env,napi_callback_info info)1638 napi_value CameraSessionNapi::On(napi_env env, napi_callback_info info)
1639 {
1640     CAMERA_SYNC_TRACE;
1641     napi_value undefinedResult = nullptr;
1642     size_t argCount = ARGS_TWO;
1643     napi_value argv[ARGS_TWO] = {nullptr};
1644     napi_value thisVar = nullptr;
1645     size_t res = 0;
1646     char buffer[SIZE];
1647     const int32_t refCount = 1;
1648     CameraSessionNapi* obj = nullptr;
1649     napi_status status;
1650 
1651     napi_get_undefined(env, &undefinedResult);
1652 
1653     CAMERA_NAPI_GET_JS_ARGS(env, info, argCount, argv, thisVar);
1654     NAPI_ASSERT(env, argCount == ARGS_TWO, "requires 2 parameters");
1655 
1656     if (thisVar == nullptr || argv[PARAM0] == nullptr || argv[PARAM1] == nullptr) {
1657         MEDIA_ERR_LOG("Failed to retrieve details about the callback");
1658         return undefinedResult;
1659     }
1660 
1661     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&obj));
1662     if (status == napi_ok && obj != nullptr) {
1663         napi_valuetype valueType = napi_undefined;
1664         if (napi_typeof(env, argv[PARAM0], &valueType) != napi_ok || valueType != napi_string
1665             || napi_typeof(env, argv[PARAM1], &valueType) != napi_ok || valueType != napi_function) {
1666             return undefinedResult;
1667         }
1668 
1669         napi_get_value_string_utf8(env, argv[PARAM0], buffer, SIZE, &res);
1670         std::string eventType = std::string(buffer);
1671 
1672         napi_ref callbackRef;
1673         napi_create_reference(env, argv[PARAM1], refCount, &callbackRef);
1674 
1675         if (!eventType.empty() && eventType.compare("exposureStateChange") == 0) {
1676             // Set callback for exposureStateChange
1677             std::shared_ptr<ExposureCallbackListener> exposureCallback =
1678                 make_shared<ExposureCallbackListener>(env, callbackRef);
1679             obj->cameraSession_->SetExposureCallback(exposureCallback);
1680         } else if (!eventType.empty() && eventType.compare("focusStateChange") == 0) {
1681             // Set callback for focusStateChange
1682             std::shared_ptr<FocusCallbackListener> focusCallback =
1683                 make_shared<FocusCallbackListener>(env, callbackRef);
1684             obj->cameraSession_->SetFocusCallback(focusCallback);
1685         } else if (!eventType.empty() && eventType.compare("error") == 0) {
1686             std::shared_ptr<SessionCallbackListener> errorCallback =
1687                 std::make_shared<SessionCallbackListener>(SessionCallbackListener(env, callbackRef));
1688             obj->cameraSession_->SetCallback(errorCallback);
1689         } else  {
1690             MEDIA_ERR_LOG("Failed to Register Callback: event type is empty!");
1691             if (callbackRef != nullptr) {
1692                 napi_delete_reference(env, callbackRef);
1693             }
1694         }
1695     }
1696 
1697     return undefinedResult;
1698 }
1699 } // namespace CameraStandard
1700 } // namespace OHOS
1701