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