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