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