• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 
18 #include <cstdint>
19 #include <mutex>
20 #include <string>
21 #include <unordered_map>
22 #include <uv.h>
23 #include <vector>
24 
25 #include "camera_error_code.h"
26 #include "camera_log.h"
27 #include "camera_napi_const.h"
28 #include "camera_napi_object.h"
29 #include "camera_napi_object_types.h"
30 #include "camera_napi_param_parser.h"
31 #include "camera_napi_security_utils.h"
32 #include "camera_napi_template_utils.h"
33 #include "camera_napi_utils.h"
34 #include "camera_output_capability.h"
35 #include "capture_scene_const.h"
36 #include "capture_session.h"
37 #include "dynamic_loader/camera_napi_ex_manager.h"
38 #include "icapture_session_callback.h"
39 #include "input/camera_device.h"
40 #include "input/camera_input_napi.h"
41 #include "js_native_api.h"
42 #include "js_native_api_types.h"
43 #include "listener_base.h"
44 #include "napi/native_api.h"
45 #include "napi/native_common.h"
46 #include "output/metadata_output_napi.h"
47 #include "output/photo_output_napi.h"
48 #include "output/preview_output_napi.h"
49 #include "output/video_output_napi.h"
50 #include "napi/native_node_api.h"
51 #include "common/qos_utils.h"
52 
53 namespace OHOS {
54 namespace CameraStandard {
55 namespace {
AsyncCompleteCallback(napi_env env,napi_status status,void * data)56 void AsyncCompleteCallback(napi_env env, napi_status status, void* data)
57 {
58     auto context = static_cast<CameraSessionAsyncContext*>(data);
59     CHECK_RETURN_ELOG(context == nullptr, "CameraSessionNapi AsyncCompleteCallback context is null");
60     MEDIA_INFO_LOG("CameraSessionNapi AsyncCompleteCallback %{public}s, status = %{public}d", context->funcName.c_str(),
61         context->status);
62     std::unique_ptr<JSAsyncContextOutput> jsContext = std::make_unique<JSAsyncContextOutput>();
63     jsContext->status = context->status;
64     if (!context->status) {
65         CameraNapiUtils::CreateNapiErrorObject(env, context->errorCode, context->errorMsg.c_str(), jsContext);
66     } else {
67         napi_get_undefined(env, &jsContext->data);
68     }
69     if (!context->funcName.empty() && context->taskId > 0) {
70         // Finish async trace
71         CAMERA_FINISH_ASYNC_TRACE(context->funcName, context->taskId);
72         jsContext->funcName = context->funcName;
73     }
74     CHECK_EXECUTE(context->work != nullptr,
75         CameraNapiUtils::InvokeJSAsyncMethod(env, context->deferred, context->callbackRef, context->work, *jsContext));
76     context->FreeHeldNapiValue(env);
77     delete context;
78 }
79 } // namespace
80 
81 using namespace std;
82 thread_local napi_ref CameraSessionNapi::sConstructor_ = nullptr;
83 thread_local sptr<CaptureSession> CameraSessionNapi::sCameraSession_ = nullptr;
84 thread_local uint32_t CameraSessionNapi::cameraSessionTaskId = CAMERA_SESSION_TASKID;
85 
86 const std::vector<napi_property_descriptor> CameraSessionNapi::camera_process_props = {
87     DECLARE_NAPI_FUNCTION("beginConfig", CameraSessionNapi::BeginConfig),
88     DECLARE_NAPI_FUNCTION("commitConfig", CameraSessionNapi::CommitConfig),
89 
90     DECLARE_NAPI_FUNCTION("canAddInput", CameraSessionNapi::CanAddInput),
91     DECLARE_NAPI_FUNCTION("addInput", CameraSessionNapi::AddInput),
92     DECLARE_NAPI_FUNCTION("removeInput", CameraSessionNapi::RemoveInput),
93 
94     DECLARE_NAPI_FUNCTION("canAddOutput", CameraSessionNapi::CanAddOutput),
95     DECLARE_NAPI_FUNCTION("addOutput", CameraSessionNapi::AddOutput),
96     DECLARE_NAPI_FUNCTION("removeOutput", CameraSessionNapi::RemoveOutput),
97 
98     DECLARE_NAPI_FUNCTION("start", CameraSessionNapi::Start),
99     DECLARE_NAPI_FUNCTION("stop", CameraSessionNapi::Stop),
100     DECLARE_NAPI_FUNCTION("release", CameraSessionNapi::Release),
101 
102     DECLARE_NAPI_FUNCTION("lockForControl", CameraSessionNapi::LockForControl),
103     DECLARE_NAPI_FUNCTION("unlockForControl", CameraSessionNapi::UnlockForControl),
104 
105     DECLARE_NAPI_FUNCTION("on", CameraSessionNapi::On),
106     DECLARE_NAPI_FUNCTION("once", CameraSessionNapi::Once),
107     DECLARE_NAPI_FUNCTION("off", CameraSessionNapi::Off)
108 };
109 
110 const std::vector<napi_property_descriptor> CameraSessionNapi::camera_process_sys_props = {
111     DECLARE_NAPI_FUNCTION("setUsage", CameraSessionNapi::SetUsage)
112 };
113 
114 const std::vector<napi_property_descriptor> CameraSessionNapi::stabilization_props = {
115     DECLARE_NAPI_FUNCTION("isVideoStabilizationModeSupported", CameraSessionNapi::IsVideoStabilizationModeSupported),
116     DECLARE_NAPI_FUNCTION("getActiveVideoStabilizationMode", CameraSessionNapi::GetActiveVideoStabilizationMode),
117     DECLARE_NAPI_FUNCTION("setVideoStabilizationMode", CameraSessionNapi::SetVideoStabilizationMode)
118 };
119 
120 const std::vector<napi_property_descriptor> CameraSessionNapi::flash_props = {
121     DECLARE_NAPI_FUNCTION("hasFlash", CameraSessionNapi::HasFlash),
122     DECLARE_NAPI_FUNCTION("isFlashModeSupported", CameraSessionNapi::IsFlashModeSupported),
123     DECLARE_NAPI_FUNCTION("getFlashMode", CameraSessionNapi::GetFlashMode),
124     DECLARE_NAPI_FUNCTION("setFlashMode", CameraSessionNapi::SetFlashMode)
125 };
126 
127 
128 const std::vector<napi_property_descriptor> CameraSessionNapi::flash_sys_props = {
129     DECLARE_NAPI_FUNCTION("isLcdFlashSupported", CameraSessionNapi::IsLcdFlashSupported),
130     DECLARE_NAPI_FUNCTION("enableLcdFlash", CameraSessionNapi::EnableLcdFlash)
131 };
132 
133 const std::vector<napi_property_descriptor> CameraSessionNapi::auto_exposure_props = {
134     DECLARE_NAPI_FUNCTION("isExposureModeSupported", CameraSessionNapi::IsExposureModeSupported),
135     DECLARE_NAPI_FUNCTION("getExposureMode", CameraSessionNapi::GetExposureMode),
136     DECLARE_NAPI_FUNCTION("setExposureMode", CameraSessionNapi::SetExposureMode),
137     DECLARE_NAPI_FUNCTION("getExposureBiasRange", CameraSessionNapi::GetExposureBiasRange),
138     DECLARE_NAPI_FUNCTION("setExposureBias", CameraSessionNapi::SetExposureBias),
139     DECLARE_NAPI_FUNCTION("getExposureValue", CameraSessionNapi::GetExposureValue),
140     DECLARE_NAPI_FUNCTION("getMeteringPoint", CameraSessionNapi::GetMeteringPoint),
141     DECLARE_NAPI_FUNCTION("setMeteringPoint", CameraSessionNapi::SetMeteringPoint)
142 };
143 
144 const std::vector<napi_property_descriptor> CameraSessionNapi::focus_props = {
145     DECLARE_NAPI_FUNCTION("isFocusModeSupported", CameraSessionNapi::IsFocusModeSupported),
146     DECLARE_NAPI_FUNCTION("getFocusMode", CameraSessionNapi::GetFocusMode),
147     DECLARE_NAPI_FUNCTION("setFocusMode", CameraSessionNapi::SetFocusMode),
148     DECLARE_NAPI_FUNCTION("getFocusPoint", CameraSessionNapi::GetFocusPoint),
149     DECLARE_NAPI_FUNCTION("setFocusPoint", CameraSessionNapi::SetFocusPoint),
150     DECLARE_NAPI_FUNCTION("getFocalLength", CameraSessionNapi::GetFocalLength)
151 };
152 
153 const std::vector<napi_property_descriptor> CameraSessionNapi::focus_sys_props = {
154     DECLARE_NAPI_FUNCTION("isFocusRangeTypeSupported", CameraSessionNapi::IsFocusRangeTypeSupported),
155     DECLARE_NAPI_FUNCTION("getFocusRange", CameraSessionNapi::GetFocusRange),
156     DECLARE_NAPI_FUNCTION("setFocusRange", CameraSessionNapi::SetFocusRange),
157     DECLARE_NAPI_FUNCTION("isFocusDrivenTypeSupported", CameraSessionNapi::IsFocusDrivenTypeSupported),
158     DECLARE_NAPI_FUNCTION("getFocusDriven", CameraSessionNapi::GetFocusDriven),
159     DECLARE_NAPI_FUNCTION("setFocusDriven", CameraSessionNapi::SetFocusDriven)
160 };
161 
162 const std::vector<napi_property_descriptor> CameraSessionNapi::quality_prioritization_props = {
163     DECLARE_NAPI_FUNCTION("setQualityPrioritization", CameraSessionNapi::SetQualityPrioritization),
164 };
165 
166 const std::vector<napi_property_descriptor> CameraSessionNapi::zoom_props = {
167     DECLARE_NAPI_FUNCTION("getZoomRatioRange", CameraSessionNapi::GetZoomRatioRange),
168     DECLARE_NAPI_FUNCTION("getZoomRatio", CameraSessionNapi::GetZoomRatio),
169     DECLARE_NAPI_FUNCTION("setZoomRatio", CameraSessionNapi::SetZoomRatio),
170     DECLARE_NAPI_FUNCTION("setSmoothZoom", CameraSessionNapi::SetSmoothZoom),
171     DECLARE_NAPI_FUNCTION("isZoomCenterPointSupported", CameraSessionNapi::IsZoomCenterPointSupported),
172     DECLARE_NAPI_FUNCTION("getZoomCenterPoint", CameraSessionNapi::GetZoomCenterPoint),
173     DECLARE_NAPI_FUNCTION("setZoomCenterPoint", CameraSessionNapi::SetZoomCenterPoint)
174 };
175 
176 const std::vector<napi_property_descriptor> CameraSessionNapi::zoom_sys_props = {
177     DECLARE_NAPI_FUNCTION("prepareZoom", CameraSessionNapi::PrepareZoom),
178     DECLARE_NAPI_FUNCTION("unprepareZoom", CameraSessionNapi::UnPrepareZoom),
179     DECLARE_NAPI_FUNCTION("getZoomPointInfos", CameraSessionNapi::GetZoomPointInfos)
180 };
181 
182 const std::vector<napi_property_descriptor> CameraSessionNapi::filter_props = {
183     DECLARE_NAPI_FUNCTION("getSupportedFilters", CameraSessionNapi::GetSupportedFilters),
184     DECLARE_NAPI_FUNCTION("getFilter", CameraSessionNapi::GetFilter),
185     DECLARE_NAPI_FUNCTION("setFilter", CameraSessionNapi::SetFilter)
186 };
187 
188 const std::vector<napi_property_descriptor> CameraSessionNapi::beauty_props = {
189     DECLARE_NAPI_FUNCTION("getSupportedBeautyTypes", CameraSessionNapi::GetSupportedBeautyTypes),
190     DECLARE_NAPI_FUNCTION("getSupportedBeautyRange", CameraSessionNapi::GetSupportedBeautyRange),
191     DECLARE_NAPI_FUNCTION("getBeauty", CameraSessionNapi::GetBeauty),
192     DECLARE_NAPI_FUNCTION("setBeauty", CameraSessionNapi::SetBeauty)
193 };
194 const std::vector<napi_property_descriptor> CameraSessionNapi::macro_props = {
195     DECLARE_NAPI_FUNCTION("isMacroSupported", CameraSessionNapi::IsMacroSupported),
196     DECLARE_NAPI_FUNCTION("enableMacro", CameraSessionNapi::EnableMacro)
197 };
198 
199 const std::vector<napi_property_descriptor> CameraSessionNapi::moon_capture_boost_props = {
200     DECLARE_NAPI_FUNCTION("isMoonCaptureBoostSupported", CameraSessionNapi::IsMoonCaptureBoostSupported),
201     DECLARE_NAPI_FUNCTION("enableMoonCaptureBoost", CameraSessionNapi::EnableMoonCaptureBoost)
202 };
203 
204 const std::vector<napi_property_descriptor> CameraSessionNapi::color_management_props = {
205     DECLARE_NAPI_FUNCTION("getSupportedColorSpaces", CameraSessionNapi::GetSupportedColorSpaces),
206     DECLARE_NAPI_FUNCTION("getActiveColorSpace", CameraSessionNapi::GetActiveColorSpace),
207     DECLARE_NAPI_FUNCTION("setColorSpace", CameraSessionNapi::SetColorSpace)
208 };
209 
210 const std::vector<napi_property_descriptor> CameraSessionNapi::control_center_props = {
211     DECLARE_NAPI_FUNCTION("isControlCenterSupported", CameraSessionNapi::IsControlCenterSupported),
212     DECLARE_NAPI_FUNCTION("getSupportedEffectTypes", CameraSessionNapi::GetSupportedEffectTypes),
213     DECLARE_NAPI_FUNCTION("enableControlCenter", CameraSessionNapi::EnableControlCenter)
214 };
215 
216 const std::vector<napi_property_descriptor> CameraSessionNapi::preconfig_props = {
217     DECLARE_NAPI_FUNCTION("canPreconfig", CameraSessionNapi::CanPreconfig),
218     DECLARE_NAPI_FUNCTION("preconfig", CameraSessionNapi::Preconfig)
219 };
220 
221 const std::vector<napi_property_descriptor> CameraSessionNapi::camera_output_capability_sys_props = {
222     DECLARE_NAPI_FUNCTION("getCameraOutputCapabilities", CameraSessionNapi::GetCameraOutputCapabilities)
223 };
224 
225 const std::vector<napi_property_descriptor> CameraSessionNapi::camera_ability_sys_props = {
226     DECLARE_NAPI_FUNCTION("getSessionFunctions", CameraSessionNapi::GetSessionFunctions),
227     DECLARE_NAPI_FUNCTION("getSessionConflictFunctions", CameraSessionNapi::GetSessionConflictFunctions)
228 };
229 
230 const std::vector<napi_property_descriptor> CameraSessionNapi::white_balance_props = {
231     DECLARE_NAPI_FUNCTION("getSupportedWhiteBalanceModes", CameraSessionNapi::GetSupportedWhiteBalanceModes),
232     DECLARE_NAPI_FUNCTION("isManualWhiteBalanceSupported", CameraSessionNapi::IsManualWhiteBalanceSupported),
233     DECLARE_NAPI_FUNCTION("isWhiteBalanceModeSupported", CameraSessionNapi::IsWhiteBalanceModeSupported),
234     DECLARE_NAPI_FUNCTION("getWhiteBalanceRange", CameraSessionNapi::GetWhiteBalanceRange),
235     DECLARE_NAPI_FUNCTION("getWhiteBalanceMode", CameraSessionNapi::GetWhiteBalanceMode),
236     DECLARE_NAPI_FUNCTION("setWhiteBalanceMode", CameraSessionNapi::SetWhiteBalanceMode),
237     DECLARE_NAPI_FUNCTION("getWhiteBalance", CameraSessionNapi::GetWhiteBalance),
238     DECLARE_NAPI_FUNCTION("setWhiteBalance", CameraSessionNapi::SetWhiteBalance),
239 };
240 
241 const std::vector<napi_property_descriptor> CameraSessionNapi::auto_switch_props = {
242     DECLARE_NAPI_FUNCTION("isAutoDeviceSwitchSupported", CameraSessionNapi::IsAutoDeviceSwitchSupported),
243     DECLARE_NAPI_FUNCTION("enableAutoDeviceSwitch", CameraSessionNapi::EnableAutoDeviceSwitch)
244 };
245 
OnExposureStateCallbackAsync(ExposureState state) const246 void ExposureCallbackListener::OnExposureStateCallbackAsync(ExposureState state) const
247 {
248     MEDIA_DEBUG_LOG("OnExposureStateCallbackAsync is called");
249     std::unique_ptr<ExposureCallbackInfo> callbackInfo =
250         std::make_unique<ExposureCallbackInfo>(state, shared_from_this());
251     ExposureCallbackInfo *event = callbackInfo.get();
252     auto task = [event]() {
253         ExposureCallbackInfo* callbackInfo = reinterpret_cast<ExposureCallbackInfo *>(event);
254         if (callbackInfo) {
255             auto listener = callbackInfo->listener_.lock();
256             CHECK_EXECUTE(listener != nullptr, listener->OnExposureStateCallback(callbackInfo->state_));
257             delete callbackInfo;
258         }
259     };
260     if (napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
261         MEDIA_ERR_LOG("failed to execute work");
262     } else {
263         callbackInfo.release();
264     }
265 }
266 
OnExposureStateCallback(ExposureState state) const267 void ExposureCallbackListener::OnExposureStateCallback(ExposureState state) const
268 {
269     MEDIA_DEBUG_LOG("OnExposureStateCallback is called");
270     napi_value result[ARGS_TWO] = {nullptr, nullptr};
271     napi_value retVal;
272 
273     napi_get_undefined(env_, &result[PARAM0]);
274     napi_create_int32(env_, state, &result[PARAM1]);
275     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
276     ExecuteCallback("exposureStateChange", callbackNapiPara);
277 }
278 
OnExposureState(const ExposureState state)279 void ExposureCallbackListener::OnExposureState(const ExposureState state)
280 {
281     MEDIA_DEBUG_LOG("OnExposureState is called, state: %{public}d", state);
282     OnExposureStateCallbackAsync(state);
283 }
284 
OnFocusStateCallbackAsync(FocusState state) const285 void FocusCallbackListener::OnFocusStateCallbackAsync(FocusState state) const
286 {
287     MEDIA_DEBUG_LOG("OnFocusStateCallbackAsync is called");
288     std::unique_ptr<FocusCallbackInfo> callbackInfo = std::make_unique<FocusCallbackInfo>(state, shared_from_this());
289     FocusCallbackInfo *event = callbackInfo.get();
290     auto task = [event]() {
291         FocusCallbackInfo* callbackInfo = reinterpret_cast<FocusCallbackInfo *>(event);
292         if (callbackInfo) {
293             auto listener = callbackInfo->listener_.lock();
294             CHECK_EXECUTE(listener != nullptr, listener->OnFocusStateCallback(callbackInfo->state_));
295             delete callbackInfo;
296         }
297     };
298     if (napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
299         MEDIA_ERR_LOG("failed to execute work");
300     } else {
301         callbackInfo.release();
302     }
303 }
304 
OnFocusStateCallback(FocusState state) const305 void FocusCallbackListener::OnFocusStateCallback(FocusState state) const
306 {
307     MEDIA_DEBUG_LOG("OnFocusStateCallback is called");
308     napi_value result[ARGS_TWO] = {nullptr, nullptr};
309     napi_value retVal;
310     napi_get_undefined(env_, &result[PARAM0]);
311     napi_create_int32(env_, state, &result[PARAM1]);
312     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
313     ExecuteCallback("focusStateChange", callbackNapiPara);
314 }
315 
OnFocusState(FocusState state)316 void FocusCallbackListener::OnFocusState(FocusState state)
317 {
318     MEDIA_DEBUG_LOG("OnFocusState is called, state: %{public}d", state);
319     OnFocusStateCallbackAsync(state);
320 }
321 
OnMoonCaptureBoostStatusCallbackAsync(MoonCaptureBoostStatus status) const322 void MoonCaptureBoostCallbackListener::OnMoonCaptureBoostStatusCallbackAsync(MoonCaptureBoostStatus status) const
323 {
324     MEDIA_DEBUG_LOG("OnMoonCaptureBoostStatusCallbackAsync is called");
325     auto callbackInfo = std::make_unique<MoonCaptureBoostStatusCallbackInfo>(status, shared_from_this());
326     MoonCaptureBoostStatusCallbackInfo *event = callbackInfo.get();
327     auto task = [event]() {
328         auto callbackInfo = reinterpret_cast<MoonCaptureBoostStatusCallbackInfo*>(event);
329         if (callbackInfo) {
330             auto listener = callbackInfo->listener_.lock();
331             CHECK_EXECUTE(listener != nullptr, listener->OnMoonCaptureBoostStatusCallback(callbackInfo->status_));
332             delete callbackInfo;
333         }
334     };
335     if (napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
336         MEDIA_ERR_LOG("failed to execute work");
337     } else {
338         callbackInfo.release();
339     }
340 }
341 
OnMoonCaptureBoostStatusCallback(MoonCaptureBoostStatus status) const342 void MoonCaptureBoostCallbackListener::OnMoonCaptureBoostStatusCallback(MoonCaptureBoostStatus status) const
343 {
344     MEDIA_DEBUG_LOG("OnMoonCaptureBoostStatusCallback is called");
345     napi_value result[ARGS_TWO] = { nullptr, nullptr };
346     napi_value retVal;
347     napi_get_undefined(env_, &result[PARAM0]);
348     napi_get_boolean(env_, status == MoonCaptureBoostStatus::ACTIVE, &result[PARAM1]);
349     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
350     ExecuteCallback("moonCaptureBoostStatus", callbackNapiPara);
351 }
352 
OnMoonCaptureBoostStatusChanged(MoonCaptureBoostStatus status)353 void MoonCaptureBoostCallbackListener::OnMoonCaptureBoostStatusChanged(MoonCaptureBoostStatus status)
354 {
355     MEDIA_DEBUG_LOG("OnMoonCaptureBoostStatusChanged is called, status: %{public}d", status);
356     OnMoonCaptureBoostStatusCallbackAsync(status);
357 }
358 
OnErrorCallbackAsync(int32_t errorCode) const359 void SessionCallbackListener::OnErrorCallbackAsync(int32_t errorCode) const
360 {
361     MEDIA_DEBUG_LOG("OnErrorCallbackAsync is called");
362     std::unique_ptr<SessionCallbackInfo> callbackInfo =
363         std::make_unique<SessionCallbackInfo>(errorCode, shared_from_this());
364     SessionCallbackInfo *event = callbackInfo.get();
365     auto task = [event]() {
366         SessionCallbackInfo* callbackInfo = reinterpret_cast<SessionCallbackInfo *>(event);
367         if (callbackInfo) {
368             auto listener = callbackInfo->listener_.lock();
369             CHECK_EXECUTE(listener != nullptr, listener->OnErrorCallback(callbackInfo->errorCode_));
370             delete callbackInfo;
371         }
372     };
373     if (napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
374         MEDIA_ERR_LOG("failed to execute work");
375     } else {
376         callbackInfo.release();
377     }
378 }
379 
OnErrorCallback(int32_t errorCode) const380 void SessionCallbackListener::OnErrorCallback(int32_t errorCode) const
381 {
382     MEDIA_DEBUG_LOG("OnErrorCallback is called");
383     napi_value result[ARGS_ONE] = {nullptr};
384     napi_value retVal;
385     napi_value propValue;
386 
387     napi_create_object(env_, &result[PARAM0]);
388     napi_create_int32(env_, errorCode, &propValue);
389     napi_set_named_property(env_, result[PARAM0], "code", propValue);
390     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_ONE, .argv = result, .result = &retVal };
391     ExecuteCallback("error", callbackNapiPara);
392 }
393 
OnError(int32_t errorCode)394 void SessionCallbackListener::OnError(int32_t errorCode)
395 {
396     MEDIA_DEBUG_LOG("OnError is called, errorCode: %{public}d", errorCode);
397     OnErrorCallbackAsync(errorCode);
398 }
399 
OnPressureCallbackAsync(PressureStatus status) const400 void PressureCallbackListener::OnPressureCallbackAsync(PressureStatus status) const
401 {
402     MEDIA_INFO_LOG("OnPressureCallbackAsync is called");
403     std::unique_ptr<PressureCallbackInfo> callbackInfo =
404         std::make_unique<PressureCallbackInfo>(status, shared_from_this());
405     PressureCallbackInfo *event = callbackInfo.get();
406     auto task = [event]() {
407         PressureCallbackInfo* callbackInfo = reinterpret_cast<PressureCallbackInfo *>(event);
408         if (callbackInfo) {
409             auto listener = callbackInfo->listener_.lock();
410             if (listener != nullptr) {
411                 listener->OnPressureCallback(callbackInfo->status_);
412             }
413             delete callbackInfo;
414         }
415     };
416     if (napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
417         MEDIA_ERR_LOG("failed to execute work");
418     } else {
419         callbackInfo.release();
420     }
421 }
422 
OnPressureCallback(PressureStatus status) const423 void PressureCallbackListener::OnPressureCallback(PressureStatus status) const
424 {
425     MEDIA_INFO_LOG("OnPressureCallback is called   %{public}d ", status);
426     napi_value result[ARGS_TWO] = {nullptr, nullptr};
427     napi_value retVal;
428     napi_get_undefined(env_, &result[PARAM0]);
429     napi_create_int32(env_, static_cast<int32_t>(status), &result[PARAM1]);
430     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
431     ExecuteCallback("systemPressureLevelChange", callbackNapiPara);
432 }
433 
OnPressureStatusChanged(PressureStatus status)434 void PressureCallbackListener::OnPressureStatusChanged(PressureStatus status)
435 {
436     MEDIA_INFO_LOG("OnPressureStatusChanged is called, status: %{public}d", status);
437     OnPressureCallbackAsync(status);
438 }
439 
OnControlCenterEffectStatusCallbackAsync(ControlCenterStatusInfo controlCenterStatusInfo) const440 void ControlCenterEffectStatusCallbackListener::OnControlCenterEffectStatusCallbackAsync(
441     ControlCenterStatusInfo controlCenterStatusInfo) const
442 {
443     MEDIA_INFO_LOG("OnControlCenterEffectStatusCallbackAsync is called");
444     std::unique_ptr<ControlCenterEffectCallbackInfo> callbackInfo =
445         std::make_unique<ControlCenterEffectCallbackInfo>(controlCenterStatusInfo, shared_from_this());
446     ControlCenterEffectCallbackInfo *event = callbackInfo.get();
447     auto task = [event]() {
448         ControlCenterEffectCallbackInfo* callbackInfo = reinterpret_cast<ControlCenterEffectCallbackInfo *>(event);
449         if (callbackInfo) {
450             auto listener = callbackInfo->listener_.lock();
451             if (listener != nullptr) {
452                 listener->OnControlCenterEffectStatusCallback(callbackInfo->statusInfo_);
453             }
454             delete callbackInfo;
455         }
456     };
457     if (napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
458         MEDIA_ERR_LOG("failed to execute work");
459     } else {
460         callbackInfo.release();
461     }
462 }
463 
OnControlCenterEffectStatusCallback(ControlCenterStatusInfo controlCenterStatusInfo) const464 void ControlCenterEffectStatusCallbackListener::OnControlCenterEffectStatusCallback(
465     ControlCenterStatusInfo controlCenterStatusInfo) const
466 {
467     MEDIA_INFO_LOG("OnControlCenterEffectStatusCallback is called");
468     ExecuteCallbackScopeSafe("controlCenterEffectStatusChange", [&]() {
469         napi_value errCode = CameraNapiUtils::GetUndefinedValue(env_);
470         napi_value callbackObj;
471         ControlCenterStatusInfo info = controlCenterStatusInfo;
472         CameraNapiObject controlCenterStatusObj {{
473             { "effectType", reinterpret_cast<int32_t*>(&info.effectType)},
474             { "isActive", &info.isActive }
475         }};
476         callbackObj = controlCenterStatusObj.CreateNapiObjFromMap(env_);
477         return ExecuteCallbackData(env_, errCode, callbackObj);
478     });
479 }
480 
OnControlCenterEffectStatusChanged(ControlCenterStatusInfo controlCenterStatusInfo)481 void ControlCenterEffectStatusCallbackListener::OnControlCenterEffectStatusChanged(
482     ControlCenterStatusInfo controlCenterStatusInfo)
483 {
484     MEDIA_INFO_LOG("ControlCenterEffectStatusCallbackListener::OnControlCenterEffectStatusChanged");
485     OnControlCenterEffectStatusCallbackAsync(controlCenterStatusInfo);
486 }
487 
OnSmoothZoomCallbackAsync(int32_t duration) const488 void SmoothZoomCallbackListener::OnSmoothZoomCallbackAsync(int32_t duration) const
489 {
490     MEDIA_DEBUG_LOG("OnSmoothZoomCallbackAsync is called");
491     std::unique_ptr<SmoothZoomCallbackInfo> callbackInfo =
492         std::make_unique<SmoothZoomCallbackInfo>(duration, shared_from_this());
493     SmoothZoomCallbackInfo *event = callbackInfo.get();
494     auto task = [event]() {
495         SmoothZoomCallbackInfo* callbackInfo = reinterpret_cast<SmoothZoomCallbackInfo *>(event);
496         if (callbackInfo) {
497             auto listener = callbackInfo->listener_.lock();
498             CHECK_EXECUTE(listener != nullptr, listener->OnSmoothZoomCallback(callbackInfo->duration_));
499             delete callbackInfo;
500         }
501     };
502     if (napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
503         MEDIA_ERR_LOG("failed to execute work");
504     } else {
505         callbackInfo.release();
506     }
507 }
508 
OnSmoothZoomCallback(int32_t duration) const509 void SmoothZoomCallbackListener::OnSmoothZoomCallback(int32_t duration) const
510 {
511     MEDIA_DEBUG_LOG("OnSmoothZoomCallback is called");
512     napi_value result[ARGS_TWO];
513     napi_value retVal;
514     napi_value propValue;
515 
516     napi_get_undefined(env_, &result[PARAM0]);
517     napi_create_object(env_, &result[PARAM1]);
518     napi_create_int32(env_, duration, &propValue);
519     napi_set_named_property(env_, result[PARAM1], "duration", propValue);
520 
521     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
522     ExecuteCallback("smoothZoomInfoAvailable", callbackNapiPara);
523 }
524 
OnSmoothZoom(int32_t duration)525 void SmoothZoomCallbackListener::OnSmoothZoom(int32_t duration)
526 {
527     MEDIA_DEBUG_LOG("OnSmoothZoom is called, duration: %{public}d", duration);
528     OnSmoothZoomCallbackAsync(duration);
529 }
530 
OnAbilityChangeCallbackAsync() const531 void AbilityCallbackListener::OnAbilityChangeCallbackAsync() const
532 {
533     MEDIA_DEBUG_LOG("OnAbilityChangeCallbackAsync is called");
534     std::unique_ptr<AbilityCallbackInfo> callbackInfo = std::make_unique<AbilityCallbackInfo>(shared_from_this());
535     AbilityCallbackInfo *event = callbackInfo.get();
536     auto task = [event]() {
537         AbilityCallbackInfo* callbackInfo = reinterpret_cast<AbilityCallbackInfo *>(event);
538         if (callbackInfo) {
539             auto listener = callbackInfo->listener_.lock();
540             CHECK_EXECUTE(listener != nullptr, listener->OnAbilityChangeCallback());
541             delete callbackInfo;
542         }
543     };
544     if (napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
545         MEDIA_ERR_LOG("failed to execute work");
546     } else {
547         callbackInfo.release();
548     }
549 }
550 
OnAbilityChangeCallback() const551 void AbilityCallbackListener::OnAbilityChangeCallback() const
552 {
553     MEDIA_DEBUG_LOG("OnAbilityChangeCallback is called");
554     napi_value result[ARGS_TWO];
555     napi_value retVal;
556     napi_get_undefined(env_, &result[PARAM0]);
557     napi_get_undefined(env_, &result[PARAM1]);
558 
559     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
560     ExecuteCallback("abilityChange", callbackNapiPara);
561 }
562 
OnAbilityChange()563 void AbilityCallbackListener::OnAbilityChange()
564 {
565     MEDIA_DEBUG_LOG("OnAbilityChange is called");
566     OnAbilityChangeCallbackAsync();
567 }
568 
OnAutoDeviceSwitchCallbackAsync(bool isDeviceSwitched,bool isDeviceCapabilityChanged) const569 void AutoDeviceSwitchCallbackListener::OnAutoDeviceSwitchCallbackAsync(
570     bool isDeviceSwitched, bool isDeviceCapabilityChanged) const
571 {
572     MEDIA_DEBUG_LOG("OnAutoDeviceSwitchCallbackAsync is called");
573     auto callbackInfo = std::make_unique<AutoDeviceSwitchCallbackListenerInfo>(
574         isDeviceSwitched, isDeviceCapabilityChanged, shared_from_this());
575     AutoDeviceSwitchCallbackListenerInfo *event = callbackInfo.get();
576     auto task = [event]() {
577         auto callbackInfo = reinterpret_cast<AutoDeviceSwitchCallbackListenerInfo*>(event);
578         if (callbackInfo) {
579             auto listener = callbackInfo->listener_.lock();
580             CHECK_EXECUTE(listener != nullptr, listener->OnAutoDeviceSwitchCallback(callbackInfo->isDeviceSwitched_,
581                 callbackInfo->isDeviceCapabilityChanged_));
582             delete callbackInfo;
583         }
584     };
585     if (napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
586         MEDIA_ERR_LOG("failed to execute work");
587     } else {
588         callbackInfo.release();
589     }
590 }
591 
OnAutoDeviceSwitchCallback(bool isDeviceSwitched,bool isDeviceCapabilityChanged) const592 void AutoDeviceSwitchCallbackListener::OnAutoDeviceSwitchCallback(
593     bool isDeviceSwitched, bool isDeviceCapabilityChanged) const
594 {
595     MEDIA_INFO_LOG("OnAutoDeviceSwitchCallback is called");
596     napi_value result[ARGS_TWO] = { nullptr, nullptr };
597     napi_get_undefined(env_, &result[PARAM0]);
598     napi_value retVal;
599     napi_value propValue;
600     napi_create_object(env_, &result[PARAM1]);
601     napi_get_boolean(env_, isDeviceSwitched, &propValue);
602     napi_set_named_property(env_, result[PARAM1], "isDeviceSwitched", propValue);
603     napi_get_boolean(env_, isDeviceCapabilityChanged, &propValue);
604     napi_set_named_property(env_, result[PARAM1], "isDeviceCapabilityChanged", propValue);
605     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
606     ExecuteCallback("autoDeviceSwitchStatusChange", callbackNapiPara);
607 }
608 
OnAutoDeviceSwitchStatusChange(bool isDeviceSwitched,bool isDeviceCapabilityChanged) const609 void AutoDeviceSwitchCallbackListener::OnAutoDeviceSwitchStatusChange(
610     bool isDeviceSwitched, bool isDeviceCapabilityChanged) const
611 {
612     MEDIA_INFO_LOG("isDeviceSwitched: %{public}d, isDeviceCapabilityChanged: %{public}d",
613         isDeviceSwitched, isDeviceCapabilityChanged);
614     OnAutoDeviceSwitchCallbackAsync(isDeviceSwitched, isDeviceCapabilityChanged);
615 }
616 
CameraSessionNapi()617 CameraSessionNapi::CameraSessionNapi() : env_(nullptr) {}
618 
~CameraSessionNapi()619 CameraSessionNapi::~CameraSessionNapi()
620 {
621     MEDIA_DEBUG_LOG("~CameraSessionNapi is called");
622 }
623 
CameraSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)624 void CameraSessionNapi::CameraSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
625 {
626     MEDIA_DEBUG_LOG("CameraSessionNapiDestructor is called");
627 }
628 
Init(napi_env env)629 void CameraSessionNapi::Init(napi_env env)
630 {
631     MEDIA_DEBUG_LOG("Init is called");
632     napi_status status;
633     napi_value ctorObj;
634     std::vector<std::vector<napi_property_descriptor>> descriptors = { camera_process_props, camera_process_sys_props,
635         stabilization_props, flash_props, flash_sys_props, auto_exposure_props, focus_props, focus_sys_props,
636         zoom_props, zoom_sys_props, filter_props, macro_props,  moon_capture_boost_props, color_management_props,
637         preconfig_props, camera_output_capability_sys_props, beauty_props };
638     std::vector<napi_property_descriptor> camera_session_props = CameraNapiUtils::GetPropertyDescriptor(descriptors);
639     status = napi_define_class(env, CAMERA_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
640                                CameraSessionNapiConstructor, nullptr,
641                                camera_session_props.size(),
642                                camera_session_props.data(), &ctorObj);
643     CHECK_RETURN_ELOG(status != napi_ok, "CameraSessionNapi defined class failed");
644     status = NapiRefManager::CreateMemSafetyRef(env, ctorObj, &sConstructor_);
645     CHECK_RETURN_ELOG(status != napi_ok, "CameraSessionNapi Init failed");
646     MEDIA_DEBUG_LOG("CameraSessionNapi Init success");
647 }
648 
649 // Constructor callback
CameraSessionNapiConstructor(napi_env env,napi_callback_info info)650 napi_value CameraSessionNapi::CameraSessionNapiConstructor(napi_env env, napi_callback_info info)
651 {
652     MEDIA_DEBUG_LOG("CameraSessionNapiConstructor is called");
653     napi_status status;
654     napi_value result = nullptr;
655     napi_value thisVar = nullptr;
656 
657     napi_get_undefined(env, &result);
658     CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
659 
660     if (status == napi_ok && thisVar != nullptr) {
661         std::unique_ptr<CameraSessionNapi> obj = std::make_unique<CameraSessionNapi>();
662         if (obj != nullptr) {
663             obj->env_ = env;
664             CHECK_RETURN_RET_ELOG(sCameraSession_ == nullptr, result, "sCameraSession_ is null");
665             obj->cameraSession_ = sCameraSession_;
666             status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
667                                CameraSessionNapi::CameraSessionNapiDestructor, nullptr, nullptr);
668             if (status == napi_ok) {
669                 obj.release();
670                 return thisVar;
671             } else {
672                 MEDIA_ERR_LOG("CameraSessionNapi Failure wrapping js to native napi");
673             }
674         }
675     }
676     MEDIA_ERR_LOG("CameraSessionNapiConstructor call Failed!");
677     return result;
678 }
679 
QueryAndGetInputProperty(napi_env env,napi_value arg,const string & propertyName,napi_value & property)680 int32_t QueryAndGetInputProperty(napi_env env, napi_value arg, const string &propertyName, napi_value &property)
681 {
682     MEDIA_DEBUG_LOG("QueryAndGetInputProperty is called");
683     bool present = false;
684     int32_t retval = 0;
685     if ((napi_has_named_property(env, arg, propertyName.c_str(), &present) != napi_ok)
686         || (!present) || (napi_get_named_property(env, arg, propertyName.c_str(), &property) != napi_ok)) {
687             MEDIA_ERR_LOG("Failed to obtain property: %{public}s", propertyName.c_str());
688             retval = -1;
689     }
690 
691     return retval;
692 }
693 
GetPointProperties(napi_env env,napi_value pointObj,Point & point)694 int32_t GetPointProperties(napi_env env, napi_value pointObj, Point &point)
695 {
696     MEDIA_DEBUG_LOG("GetPointProperties is called");
697     napi_value propertyX = nullptr;
698     napi_value propertyY = nullptr;
699     double pointX = -1.0;
700     double pointY = -1.0;
701 
702     if ((QueryAndGetInputProperty(env, pointObj, "x", propertyX) == 0) &&
703         (QueryAndGetInputProperty(env, pointObj, "y", propertyY) == 0)) {
704         if ((napi_get_value_double(env, propertyX, &pointX) != napi_ok) ||
705             (napi_get_value_double(env, propertyY, &pointY) != napi_ok)) {
706             MEDIA_ERR_LOG("GetPointProperties: get propery for x & y failed");
707             return -1;
708         } else {
709             point.x = pointX;
710             point.y = pointY;
711         }
712     } else {
713         return -1;
714     }
715 
716     // Return 0 after focus point properties are successfully obtained
717     return 0;
718 }
719 
GetPointNapiValue(napi_env env,Point & point)720 napi_value GetPointNapiValue(napi_env env, Point &point)
721 {
722     MEDIA_DEBUG_LOG("GetPointNapiValue is called");
723     napi_value result;
724     napi_value propValue;
725     napi_create_object(env, &result);
726     napi_create_double(env, CameraNapiUtils::FloatToDouble(point.x), &propValue);
727     napi_set_named_property(env, result, "x", propValue);
728     napi_create_double(env, CameraNapiUtils::FloatToDouble(point.y), &propValue);
729     napi_set_named_property(env, result, "y", propValue);
730     return result;
731 }
732 
CreateCameraSession(napi_env env)733 napi_value CameraSessionNapi::CreateCameraSession(napi_env env)
734 {
735     MEDIA_DEBUG_LOG("CreateCameraSession is called");
736     CAMERA_SYNC_TRACE;
737     napi_status status;
738     napi_value result = nullptr;
739     napi_value constructor;
740 
741     if (sConstructor_ == nullptr) {
742         CameraSessionNapi::Init(env);
743         CHECK_RETURN_RET_ELOG(sConstructor_ == nullptr, result, "sConstructor_ is null");
744     }
745     status = napi_get_reference_value(env, sConstructor_, &constructor);
746     if (status == napi_ok) {
747         int retCode = CameraManager::GetInstance()->CreateCaptureSession(sCameraSession_, SceneMode::NORMAL);
748         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
749         if (sCameraSession_ == nullptr) {
750             MEDIA_ERR_LOG("Failed to create Camera session instance");
751             napi_get_undefined(env, &result);
752             return result;
753         }
754         status = napi_new_instance(env, constructor, 0, nullptr, &result);
755         sCameraSession_ = nullptr;
756         if (status == napi_ok && result != nullptr) {
757             MEDIA_DEBUG_LOG("success to create Camera session napi instance");
758             return result;
759         } else {
760             MEDIA_ERR_LOG("Failed to create Camera session napi instance");
761         }
762     }
763     MEDIA_ERR_LOG("Failed to create Camera session napi instance last");
764     napi_get_undefined(env, &result);
765     return result;
766 }
767 
BeginConfig(napi_env env,napi_callback_info info)768 napi_value CameraSessionNapi::BeginConfig(napi_env env, napi_callback_info info)
769 {
770     MEDIA_INFO_LOG("BeginConfig is called");
771     napi_status status;
772     napi_value result = nullptr;
773     size_t argc = ARGS_ZERO;
774     napi_value argv[ARGS_ZERO];
775     napi_value thisVar = nullptr;
776     napi_get_undefined(env, &result);
777     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
778 
779     CameraSessionNapi* cameraSessionNapi = nullptr;
780     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
781     if (status == napi_ok && cameraSessionNapi != nullptr) {
782         int32_t ret = cameraSessionNapi->cameraSession_->BeginConfig();
783         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, ret), nullptr);
784     } else {
785         MEDIA_ERR_LOG("BeginConfig call Failed!");
786     }
787     return result;
788 }
789 
CommitConfigAsync(uv_work_t * work)790 void CameraSessionNapi::CommitConfigAsync(uv_work_t* work)
791 {
792     CHECK_RETURN_ELOG(work == nullptr, "CommitConfigAsync null work");
793     MEDIA_INFO_LOG("CommitConfigAsync running on worker");
794     auto context = static_cast<CameraSessionAsyncContext*>(work->data);
795     CHECK_RETURN_ELOG(context == nullptr, "CommitConfigAsync context is null");
796     CHECK_RETURN_ELOG(
797         context->objectInfo == nullptr, "CommitConfigAsync async info is nullptr");
798     CAMERA_START_ASYNC_TRACE(context->funcName, context->taskId);
799     CameraNapiWorkerQueueKeeper::GetInstance()->ConsumeWorkerQueueTask(context->queueTask, [&context]() {
800         context->errorCode = context->objectInfo->cameraSession_->CommitConfig();
801         context->status = context->errorCode == CameraErrorCode::SUCCESS;
802         MEDIA_INFO_LOG("CommitConfigAsync errorCode:%{public}d", context->errorCode);
803     });
804 }
805 
UvWorkAsyncCompleted(uv_work_t * work,int status)806 void CameraSessionNapi::UvWorkAsyncCompleted(uv_work_t* work, int status)
807 {
808     CHECK_RETURN_ELOG(work == nullptr, "UvWorkAsyncCompleted null work");
809     auto context = static_cast<CameraSessionAsyncContext*>(work->data);
810     CHECK_RETURN_ELOG(context == nullptr, "UvWorkAsyncCompleted context is null");
811     MEDIA_INFO_LOG("UvWorkAsyncCompleted %{public}s, status = %{public}d", context->funcName.c_str(),
812         context->status);
813     std::unique_ptr<JSAsyncContextOutput> jsContext = std::make_unique<JSAsyncContextOutput>();
814     jsContext->status = context->status;
815     if (!context->status) {
816         CameraNapiUtils::CreateNapiErrorObject(context->env, context->errorCode, context->errorMsg.c_str(), jsContext);
817     } else {
818         napi_get_undefined(context->env, &jsContext->data);
819     }
820     if (!context->funcName.empty() && context->taskId > 0) {
821         // Finish async trace
822         CAMERA_FINISH_ASYNC_TRACE(context->funcName, context->taskId);
823         jsContext->funcName = context->funcName.c_str();
824     }
825     CHECK_EXECUTE(work != nullptr,
826         CameraNapiUtils::InvokeJSAsyncMethodWithUvWork(context->env, context->deferred,
827             context->callbackRef, *jsContext));
828     context->FreeHeldNapiValue(context->env);
829     delete context;
830     context= nullptr;
831     delete work;
832     work = nullptr;
833 }
834 
CommitConfig(napi_env env,napi_callback_info info)835 napi_value CameraSessionNapi::CommitConfig(napi_env env, napi_callback_info info)
836 {
837     MEDIA_INFO_LOG("CommitConfig is called");
838     std::unique_ptr<CameraSessionAsyncContext> asyncContext = std::make_unique<CameraSessionAsyncContext>(
839         "CameraSessionNapi::CommitConfig", CameraNapiUtils::IncrementAndGet(cameraSessionTaskId));
840     auto asyncFunction = std::make_shared<CameraNapiAsyncFunction>(
841         env, "CommitConfig", asyncContext->callbackRef, asyncContext->deferred);
842     CameraNapiParamParser jsParamParser(env, info, asyncContext->objectInfo, asyncFunction);
843     CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "invalid argument"), nullptr,
844         "CameraSessionNapi::CommitConfig invalid argument");
845     asyncContext->HoldNapiValue(env, jsParamParser.GetThisVar());
846     asyncContext->env = env;
847     uv_qos_t uvQos = QosUtils::GetUvWorkQos();
848     MEDIA_DEBUG_LOG("CameraSessionNapi::CommitConfig Qos level: %{public}d", uvQos);
849     uv_loop_s *loop = CameraInputNapi::GetEventLoop(env);
850     if (!loop) {
851         return nullptr;
852     }
853     uv_work_t *work = new(std::nothrow) uv_work_t;
854     if (work == nullptr) {
855         return nullptr;
856     }
857     work->data = static_cast<void*>(asyncContext.get());
858     asyncContext->queueTask =
859         CameraNapiWorkerQueueKeeper::GetInstance()->AcquireWorkerQueueTask("CameraSessionNapi::CommitConfig");
860     int rev = uv_queue_work_with_qos(
861         loop, work, CameraSessionNapi::CommitConfigAsync,
862         CameraSessionNapi::UvWorkAsyncCompleted, uvQos);
863     if (rev != 0) {
864         MEDIA_ERR_LOG("Failed to call uv_queue_work_with_qos for CameraSessionNapi::CommitConfig");
865         asyncFunction->Reset();
866         if (work != nullptr) {
867             delete work;
868             work = nullptr;
869         }
870         CameraNapiWorkerQueueKeeper::GetInstance()->RemoveWorkerTask(asyncContext->queueTask);
871     } else {
872         asyncContext.release();
873     }
874     CHECK_RETURN_RET(asyncFunction->GetAsyncFunctionType() == ASYNC_FUN_TYPE_PROMISE,
875         asyncFunction->GetPromise());
876     return CameraNapiUtils::GetUndefinedValue(env);
877 }
878 
LockForControl(napi_env env,napi_callback_info info)879 napi_value CameraSessionNapi::LockForControl(napi_env env, napi_callback_info info)
880 {
881     MEDIA_DEBUG_LOG("LockForControl is called");
882     napi_status status;
883     napi_value result = nullptr;
884     size_t argc = ARGS_ZERO;
885     napi_value argv[ARGS_ZERO];
886     napi_value thisVar = nullptr;
887 
888     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
889 
890     napi_get_undefined(env, &result);
891     CameraSessionNapi* cameraSessionNapi = nullptr;
892     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
893     if (status == napi_ok && cameraSessionNapi != nullptr) {
894         cameraSessionNapi->cameraSession_->LockForControl();
895     } else {
896         MEDIA_ERR_LOG("LockForControl call Failed!");
897     }
898     return result;
899 }
900 
UnlockForControl(napi_env env,napi_callback_info info)901 napi_value CameraSessionNapi::UnlockForControl(napi_env env, napi_callback_info info)
902 {
903     MEDIA_DEBUG_LOG("UnlockForControl is called");
904     napi_status status;
905     napi_value result = nullptr;
906     size_t argc = ARGS_ZERO;
907     napi_value argv[ARGS_ZERO];
908     napi_value thisVar = nullptr;
909 
910     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
911 
912     napi_get_undefined(env, &result);
913     CameraSessionNapi* cameraSessionNapi = nullptr;
914     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
915     if (status == napi_ok && cameraSessionNapi != nullptr) {
916         cameraSessionNapi->cameraSession_->UnlockForControl();
917     } else {
918         MEDIA_ERR_LOG("UnlockForControl call Failed!");
919     }
920     return result;
921 }
922 
GetJSArgsForCameraInput(napi_env env,size_t argc,const napi_value argv[],sptr<CaptureInput> & cameraInput)923 napi_value GetJSArgsForCameraInput(napi_env env, size_t argc, const napi_value argv[],
924     sptr<CaptureInput> &cameraInput)
925 {
926     MEDIA_DEBUG_LOG("GetJSArgsForCameraInput is called");
927     napi_value result = nullptr;
928     CameraInputNapi* cameraInputNapiObj = nullptr;
929 
930     NAPI_ASSERT(env, argv != nullptr, "Argument list is empty");
931 
932     for (size_t i = PARAM0; i < argc; i++) {
933         napi_valuetype valueType = napi_undefined;
934         napi_typeof(env, argv[i], &valueType);
935         if (i == PARAM0 && valueType == napi_object) {
936             napi_unwrap(env, argv[i], reinterpret_cast<void**>(&cameraInputNapiObj));
937             if (cameraInputNapiObj != nullptr) {
938                 cameraInput = cameraInputNapiObj->GetCameraInput();
939             } else {
940                 NAPI_ASSERT(env, false, "type mismatch");
941             }
942         } else {
943             NAPI_ASSERT(env, false, "type mismatch");
944         }
945     }
946     napi_get_boolean(env, true, &result);
947     return result;
948 }
949 
AddInput(napi_env env,napi_callback_info info)950 napi_value CameraSessionNapi::AddInput(napi_env env, napi_callback_info info)
951 {
952     MEDIA_INFO_LOG("AddInput is called");
953     napi_status status;
954     napi_value result = nullptr;
955     size_t argc = ARGS_ONE;
956     napi_value argv[ARGS_ONE] = {0};
957     napi_value thisVar = nullptr;
958 
959     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
960     CHECK_RETURN_RET(!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, ADD_INPUT), result);
961 
962     napi_get_undefined(env, &result);
963     CameraSessionNapi* cameraSessionNapi = nullptr;
964     sptr<CaptureInput> cameraInput = nullptr;
965     GetJSArgsForCameraInput(env, argc, argv, cameraInput);
966     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
967     if (status == napi_ok && cameraSessionNapi != nullptr) {
968         int32_t ret = cameraSessionNapi->cameraSession_->AddInput(cameraInput);
969         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, ret), nullptr);
970     } else {
971         MEDIA_ERR_LOG("AddInput call Failed!");
972     }
973     return result;
974 }
975 
CanAddInput(napi_env env,napi_callback_info info)976 napi_value CameraSessionNapi::CanAddInput(napi_env env, napi_callback_info info)
977 {
978     MEDIA_DEBUG_LOG("CanAddInput is called");
979     napi_status status;
980     napi_value result = nullptr;
981     size_t argc = ARGS_ONE;
982     napi_value argv[ARGS_ONE] = {0};
983     napi_value thisVar = nullptr;
984 
985     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
986 
987     napi_get_undefined(env, &result);
988     CameraSessionNapi* cameraSessionNapi = nullptr;
989     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
990     if (status == napi_ok && cameraSessionNapi != nullptr) {
991         sptr<CaptureInput> cameraInput = nullptr;
992         GetJSArgsForCameraInput(env, argc, argv, cameraInput);
993         bool isSupported = cameraSessionNapi->cameraSession_->CanAddInput(cameraInput);
994         napi_get_boolean(env, isSupported, &result);
995     } else {
996         MEDIA_ERR_LOG("CanAddInput call Failed!");
997     }
998     return result;
999 }
1000 
RemoveInput(napi_env env,napi_callback_info info)1001 napi_value CameraSessionNapi::RemoveInput(napi_env env, napi_callback_info info)
1002 {
1003     MEDIA_DEBUG_LOG("RemoveInput is called");
1004     napi_status status;
1005     napi_value result = nullptr;
1006     size_t argc = ARGS_ONE;
1007     napi_value argv[ARGS_ONE] = {0};
1008     napi_value thisVar = nullptr;
1009 
1010     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1011     CHECK_RETURN_RET(!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, REMOVE_INPUT), result);
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         sptr<CaptureInput> cameraInput = nullptr;
1018         GetJSArgsForCameraInput(env, argc, argv, cameraInput);
1019         int32_t ret = cameraSessionNapi->cameraSession_->RemoveInput(cameraInput);
1020         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, ret), nullptr);
1021         return result;
1022     } else {
1023         MEDIA_ERR_LOG("RemoveInput call Failed!");
1024     }
1025     return result;
1026 }
1027 
CheckExtensionCameraOutput(napi_env env,napi_value obj,sptr<CaptureOutput> & output)1028 void CheckExtensionCameraOutput(napi_env env, napi_value obj, sptr<CaptureOutput> &output)
1029 {
1030     MEDIA_DEBUG_LOG("CheckExtensionCameraOutput is called");
1031     output = nullptr;
1032     auto cameraNapiExProxy = CameraNapiExManager::GetCameraNapiExProxy();
1033     CHECK_RETURN_ELOG(cameraNapiExProxy == nullptr, "cameraNapiExProxy is nullptr");
1034     if (cameraNapiExProxy->CheckAndGetOutput(env, obj, output)) {
1035         MEDIA_DEBUG_LOG("system output adding..");
1036     } else {
1037         MEDIA_ERR_LOG("invalid output ..");
1038     }
1039 }
1040 
GetJSArgsForCameraOutput(napi_env env,size_t argc,const napi_value argv[],sptr<CaptureOutput> & cameraOutput)1041 napi_value CameraSessionNapi::GetJSArgsForCameraOutput(napi_env env, size_t argc, const napi_value argv[],
1042     sptr<CaptureOutput> &cameraOutput)
1043 {
1044     MEDIA_DEBUG_LOG("GetJSArgsForCameraOutput is called");
1045     napi_value result = nullptr;
1046     PreviewOutputNapi* previewOutputNapiObj = nullptr;
1047     PhotoOutputNapi* photoOutputNapiObj = nullptr;
1048     VideoOutputNapi* videoOutputNapiObj = nullptr;
1049     MetadataOutputNapi* metadataOutputNapiObj = nullptr;
1050 
1051     NAPI_ASSERT(env, argv != nullptr, "Argument list is empty");
1052 
1053     for (size_t i = PARAM0; i < argc; i++) {
1054         napi_valuetype valueType = napi_undefined;
1055         napi_typeof(env, argv[i], &valueType);
1056         cameraOutput = nullptr;
1057         if (i == PARAM0 && valueType == napi_object) {
1058             if (PreviewOutputNapi::IsPreviewOutput(env, argv[i])) {
1059                 MEDIA_DEBUG_LOG("preview output adding..");
1060                 napi_unwrap(env, argv[i], reinterpret_cast<void**>(&previewOutputNapiObj));
1061                 cameraOutput = previewOutputNapiObj->GetPreviewOutput();
1062             } else if (PhotoOutputNapi::IsPhotoOutput(env, argv[i])) {
1063                 MEDIA_DEBUG_LOG("photo output adding..");
1064                 napi_unwrap(env, argv[i], reinterpret_cast<void**>(&photoOutputNapiObj));
1065                 cameraOutput = photoOutputNapiObj->GetPhotoOutput();
1066             } else if (VideoOutputNapi::IsVideoOutput(env, argv[i])) {
1067                 MEDIA_DEBUG_LOG("video output adding..");
1068                 napi_unwrap(env, argv[i], reinterpret_cast<void**>(&videoOutputNapiObj));
1069                 cameraOutput = videoOutputNapiObj->GetVideoOutput();
1070             } else if (MetadataOutputNapi::IsMetadataOutput(env, argv[i])) {
1071                 MEDIA_DEBUG_LOG("metadata output adding..");
1072                 napi_unwrap(env, argv[i], reinterpret_cast<void**>(&metadataOutputNapiObj));
1073                 cameraOutput = metadataOutputNapiObj->GetMetadataOutput();
1074             }
1075             if (CameraNapiSecurity::CheckSystemApp(env, false) && cameraOutput == nullptr) {
1076                 CheckExtensionCameraOutput(env, argv[i], cameraOutput);
1077             }
1078             CHECK_EXECUTE(cameraOutput == nullptr, NAPI_ASSERT(env, false, "type mismatch"));
1079         } else {
1080             NAPI_ASSERT(env, false, "type mismatch");
1081         }
1082     }
1083     // Return true napi_value if params are successfully obtained
1084     napi_get_boolean(env, true, &result);
1085     return result;
1086 }
1087 
AddOutput(napi_env env,napi_callback_info info)1088 napi_value CameraSessionNapi::AddOutput(napi_env env, napi_callback_info info)
1089 {
1090     MEDIA_INFO_LOG("AddOutput is called");
1091     napi_status status;
1092     napi_value result = nullptr;
1093     size_t argc = ARGS_ONE;
1094     napi_value argv[ARGS_ONE] = {0};
1095     napi_value thisVar = nullptr;
1096 
1097     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1098     CHECK_RETURN_RET(!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, ADD_OUTPUT), result);
1099 
1100     napi_get_undefined(env, &result);
1101     CameraSessionNapi* cameraSessionNapi = nullptr;
1102     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1103     if (status == napi_ok && cameraSessionNapi != nullptr) {
1104         sptr<CaptureOutput> cameraOutput = nullptr;
1105         result = GetJSArgsForCameraOutput(env, argc, argv, cameraOutput);
1106         int32_t ret = cameraSessionNapi->cameraSession_->AddOutput(cameraOutput);
1107         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, ret), nullptr);
1108     } else {
1109         MEDIA_ERR_LOG("AddOutput call Failed!");
1110     }
1111     return result;
1112 }
1113 
CanAddOutput(napi_env env,napi_callback_info info)1114 napi_value CameraSessionNapi::CanAddOutput(napi_env env, napi_callback_info info)
1115 {
1116     MEDIA_DEBUG_LOG("CanAddOutput is called");
1117     napi_status status;
1118     napi_value result = nullptr;
1119     size_t argc = ARGS_ONE;
1120     napi_value argv[ARGS_ONE] = {0};
1121     napi_value thisVar = nullptr;
1122 
1123     MEDIA_DEBUG_LOG("CameraSessionNapi::CanAddOutput get js args");
1124     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1125 
1126     napi_get_undefined(env, &result);
1127     CameraSessionNapi* cameraSessionNapi = nullptr;
1128     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1129     if (status == napi_ok && cameraSessionNapi != nullptr) {
1130         sptr<CaptureOutput> cameraOutput = nullptr;
1131         result = GetJSArgsForCameraOutput(env, argc, argv, cameraOutput);
1132         bool isSupported = cameraSessionNapi->cameraSession_->CanAddOutput(cameraOutput);
1133         napi_get_boolean(env, isSupported, &result);
1134     } else {
1135         MEDIA_ERR_LOG("CanAddOutput call Failed!");
1136     }
1137     return result;
1138 }
1139 
RemoveOutput(napi_env env,napi_callback_info info)1140 napi_value CameraSessionNapi::RemoveOutput(napi_env env, napi_callback_info info)
1141 {
1142     MEDIA_INFO_LOG("RemoveOutput is called");
1143     napi_status status;
1144     napi_value result = nullptr;
1145     size_t argc = ARGS_ONE;
1146     napi_value argv[ARGS_ONE] = {0};
1147     napi_value thisVar = nullptr;
1148 
1149     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1150     CHECK_RETURN_RET(!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, REMOVE_OUTPUT), result);
1151 
1152     napi_get_undefined(env, &result);
1153     CameraSessionNapi* cameraSessionNapi = nullptr;
1154     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1155     if (status == napi_ok && cameraSessionNapi != nullptr) {
1156         sptr<CaptureOutput> cameraOutput = nullptr;
1157         result = GetJSArgsForCameraOutput(env, argc, argv, cameraOutput);
1158         int32_t ret = cameraSessionNapi->cameraSession_->RemoveOutput(cameraOutput);
1159         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, ret), nullptr);
1160     } else {
1161         MEDIA_ERR_LOG("RemoveOutput call Failed!");
1162     }
1163     return result;
1164 }
1165 
StartAsync(uv_work_t * work)1166 void CameraSessionNapi::StartAsync(uv_work_t* work)
1167 {
1168     CHECK_RETURN_ELOG(work == nullptr, "StartAsync null work");
1169     MEDIA_INFO_LOG("StartAsync running on worker");
1170     auto context = static_cast<CameraSessionAsyncContext*>(work->data);
1171     CHECK_RETURN_ELOG(context == nullptr, "StartAsync context is null");
1172     CHECK_RETURN_ELOG(context->objectInfo == nullptr, "StartAsync async info is nullptr");
1173     CAMERA_START_ASYNC_TRACE(context->funcName, context->taskId);
1174     CameraNapiWorkerQueueKeeper::GetInstance()->ConsumeWorkerQueueTask(context->queueTask, [&context]() {
1175         context->errorCode = context->objectInfo->cameraSession_->Start();
1176         context->status = context->errorCode == CameraErrorCode::SUCCESS;
1177         MEDIA_INFO_LOG("StartAsync errorCode:%{public}d", context->errorCode);
1178     });
1179 }
1180 
Start(napi_env env,napi_callback_info info)1181 napi_value CameraSessionNapi::Start(napi_env env, napi_callback_info info)
1182 {
1183     MEDIA_INFO_LOG("Start is called");
1184     std::unique_ptr<CameraSessionAsyncContext> asyncContext = std::make_unique<CameraSessionAsyncContext>(
1185         "CameraSessionNapi::Start", CameraNapiUtils::IncrementAndGet(cameraSessionTaskId));
1186     auto asyncFunction =
1187         std::make_shared<CameraNapiAsyncFunction>(env, "Start", asyncContext->callbackRef, asyncContext->deferred);
1188     CameraNapiParamParser jsParamParser(env, info, asyncContext->objectInfo, asyncFunction);
1189     CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "invalid argument"),
1190         nullptr, "CameraSessionNapi::Start invalid argument");
1191     asyncContext->HoldNapiValue(env, jsParamParser.GetThisVar());
1192     asyncContext->env = env;
1193     uv_qos_t uvQos = QosUtils::GetUvWorkQos();
1194     MEDIA_DEBUG_LOG("CameraSessionNapi::Start Qos level: %{public}d", uvQos);
1195     uv_loop_s *loop = CameraInputNapi::GetEventLoop(env);
1196     if (!loop) {
1197         return nullptr;
1198     }
1199     uv_work_t *work = new(std::nothrow) uv_work_t;
1200     if (work == nullptr) {
1201         return nullptr;
1202     }
1203     work->data = static_cast<void*>(asyncContext.get());
1204     asyncContext->queueTask =
1205         CameraNapiWorkerQueueKeeper::GetInstance()->AcquireWorkerQueueTask("CameraSessionNapi::Start");
1206     int rev = uv_queue_work_with_qos(
1207         loop, work, CameraSessionNapi::StartAsync,
1208         CameraSessionNapi::UvWorkAsyncCompleted, uvQos);
1209     if (rev != 0) {
1210         MEDIA_ERR_LOG("Failed to call uv_queue_work_with_qos for CameraSessionNapi::Start");
1211         asyncFunction->Reset();
1212         if (work != nullptr) {
1213             delete work;
1214             work = nullptr;
1215         }
1216         CameraNapiWorkerQueueKeeper::GetInstance()->RemoveWorkerTask(asyncContext->queueTask);
1217     } else {
1218         asyncContext.release();
1219     }
1220     CHECK_RETURN_RET(asyncFunction->GetAsyncFunctionType() == ASYNC_FUN_TYPE_PROMISE,
1221         asyncFunction->GetPromise());
1222     return CameraNapiUtils::GetUndefinedValue(env);
1223 }
1224 
Stop(napi_env env,napi_callback_info info)1225 napi_value CameraSessionNapi::Stop(napi_env env, napi_callback_info info)
1226 {
1227     MEDIA_INFO_LOG("Stop is called");
1228     std::unique_ptr<CameraSessionAsyncContext> asyncContext = std::make_unique<CameraSessionAsyncContext>(
1229         "CameraSessionNapi::Stop", CameraNapiUtils::IncrementAndGet(cameraSessionTaskId));
1230     auto asyncFunction =
1231         std::make_shared<CameraNapiAsyncFunction>(env, "Stop", asyncContext->callbackRef, asyncContext->deferred);
1232     CameraNapiParamParser jsParamParser(env, info, asyncContext->objectInfo, asyncFunction);
1233     CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "invalid argument"), nullptr,
1234         "CameraSessionNapi::Stop invalid argument");
1235     asyncContext->HoldNapiValue(env, jsParamParser.GetThisVar());
1236     napi_status status = napi_create_async_work(
1237         env, nullptr, asyncFunction->GetResourceName(),
1238         [](napi_env env, void* data) {
1239             MEDIA_INFO_LOG("CameraSessionNapi::Stop running on worker");
1240             auto context = static_cast<CameraSessionAsyncContext*>(data);
1241             CHECK_RETURN_ELOG(context->objectInfo == nullptr, "CameraSessionNapi::Stop async info is nullptr");
1242             CAMERA_START_ASYNC_TRACE(context->funcName, context->taskId);
1243             CameraNapiWorkerQueueKeeper::GetInstance()->ConsumeWorkerQueueTask(context->queueTask, [&context]() {
1244                 context->errorCode = context->objectInfo->cameraSession_->Stop();
1245                 context->status = context->errorCode == CameraErrorCode::SUCCESS;
1246                 MEDIA_INFO_LOG("CameraSessionNapi::Stop errorCode:%{public}d", context->errorCode);
1247             });
1248         },
1249         AsyncCompleteCallback, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1250     if (status != napi_ok) {
1251         MEDIA_ERR_LOG("Failed to create napi_create_async_work for CameraSessionNapi::Stop");
1252         asyncFunction->Reset();
1253     } else {
1254         asyncContext->queueTask =
1255             CameraNapiWorkerQueueKeeper::GetInstance()->AcquireWorkerQueueTask("CameraSessionNapi::Stop");
1256         napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_user_initiated);
1257         asyncContext.release();
1258     }
1259     CHECK_RETURN_RET(asyncFunction->GetAsyncFunctionType() == ASYNC_FUN_TYPE_PROMISE,
1260         asyncFunction->GetPromise());
1261     return CameraNapiUtils::GetUndefinedValue(env);
1262 }
1263 
Release(napi_env env,napi_callback_info info)1264 napi_value CameraSessionNapi::Release(napi_env env, napi_callback_info info)
1265 {
1266     MEDIA_INFO_LOG("Release is called");
1267     std::unique_ptr<CameraSessionAsyncContext> asyncContext = std::make_unique<CameraSessionAsyncContext>(
1268         "CameraSessionNapi::Release", CameraNapiUtils::IncrementAndGet(cameraSessionTaskId));
1269     auto asyncFunction =
1270         std::make_shared<CameraNapiAsyncFunction>(env, "Release", asyncContext->callbackRef, asyncContext->deferred);
1271     CameraNapiParamParser jsParamParser(env, info, asyncContext->objectInfo, asyncFunction);
1272     CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "invalid argument"), nullptr,
1273         "CameraSessionNapi::Release invalid argument");
1274     asyncContext->HoldNapiValue(env, jsParamParser.GetThisVar());
1275     napi_status status = napi_create_async_work(
1276         env, nullptr, asyncFunction->GetResourceName(),
1277         [](napi_env env, void* data) {
1278             MEDIA_INFO_LOG("CameraSessionNapi::Release running on worker");
1279             auto context = static_cast<CameraSessionAsyncContext*>(data);
1280             CHECK_RETURN_ELOG(context->objectInfo == nullptr, "CameraSessionNapi::Release async info is nullptr");
1281             CAMERA_START_ASYNC_TRACE(context->funcName, context->taskId);
1282             CameraNapiWorkerQueueKeeper::GetInstance()->ConsumeWorkerQueueTask(context->queueTask, [&context]() {
1283                 context->errorCode = context->objectInfo->cameraSession_->Release();
1284                 context->status = context->errorCode == CameraErrorCode::SUCCESS;
1285                 MEDIA_INFO_LOG("CameraSessionNapi::Release errorCode:%{public}d", context->errorCode);
1286             });
1287         },
1288         AsyncCompleteCallback, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1289     if (status != napi_ok) {
1290         MEDIA_ERR_LOG("Failed to create napi_create_async_work for CameraSessionNapi::Release");
1291         asyncFunction->Reset();
1292     } else {
1293         asyncContext->queueTask =
1294             CameraNapiWorkerQueueKeeper::GetInstance()->AcquireWorkerQueueTask("CameraSessionNapi::Release");
1295         napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_user_initiated);
1296         asyncContext.release();
1297     }
1298     CHECK_RETURN_RET(asyncFunction->GetAsyncFunctionType() == ASYNC_FUN_TYPE_PROMISE,
1299         asyncFunction->GetPromise());
1300     return CameraNapiUtils::GetUndefinedValue(env);
1301 }
1302 
IsVideoStabilizationModeSupported(napi_env env,napi_callback_info info)1303 napi_value CameraSessionNapi::IsVideoStabilizationModeSupported(napi_env env, napi_callback_info info)
1304 {
1305     MEDIA_DEBUG_LOG("IsVideoStabilizationModeSupported is called");
1306     napi_status status;
1307     napi_value result = nullptr;
1308     size_t argc = ARGS_ONE;
1309     napi_value argv[ARGS_ONE] = {0};
1310     napi_value thisVar = nullptr;
1311 
1312     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1313 
1314     napi_get_undefined(env, &result);
1315     CameraSessionNapi* cameraSessionNapi = nullptr;
1316     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1317     if (status == napi_ok && cameraSessionNapi != nullptr) {
1318         int32_t value;
1319         napi_get_value_int32(env, argv[PARAM0], &value);
1320         VideoStabilizationMode videoStabilizationMode = (VideoStabilizationMode)value;
1321         bool isSupported;
1322         int32_t retCode = cameraSessionNapi->cameraSession_->
1323                           IsVideoStabilizationModeSupported(videoStabilizationMode, isSupported);
1324         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1325         napi_get_boolean(env, isSupported, &result);
1326     } else {
1327         MEDIA_ERR_LOG("IsVideoStabilizationModeSupported call Failed!");
1328     }
1329     return result;
1330 }
1331 
GetActiveVideoStabilizationMode(napi_env env,napi_callback_info info)1332 napi_value CameraSessionNapi::GetActiveVideoStabilizationMode(napi_env env, napi_callback_info info)
1333 {
1334     MEDIA_DEBUG_LOG("GetActiveVideoStabilizationMode is called");
1335     napi_status status;
1336     napi_value result = nullptr;
1337     size_t argc = ARGS_ZERO;
1338     napi_value argv[ARGS_ZERO];
1339     napi_value thisVar = nullptr;
1340 
1341     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1342 
1343     napi_get_undefined(env, &result);
1344     CameraSessionNapi* cameraSessionNapi = nullptr;
1345     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1346     if (status == napi_ok && cameraSessionNapi != nullptr) {
1347         VideoStabilizationMode videoStabilizationMode;
1348         int32_t retCode = cameraSessionNapi->cameraSession_->
1349                           GetActiveVideoStabilizationMode(videoStabilizationMode);
1350         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1351         napi_create_int32(env, videoStabilizationMode, &result);
1352     } else {
1353         MEDIA_ERR_LOG("GetActiveVideoStabilizationMode call Failed!");
1354     }
1355     return result;
1356 }
1357 
SetVideoStabilizationMode(napi_env env,napi_callback_info info)1358 napi_value CameraSessionNapi::SetVideoStabilizationMode(napi_env env, napi_callback_info info)
1359 {
1360     MEDIA_DEBUG_LOG("SetVideoStabilizationMode is called");
1361     napi_status status;
1362     napi_value result = nullptr;
1363     size_t argc = ARGS_ONE;
1364     napi_value argv[ARGS_ONE] = {0};
1365     napi_value thisVar = nullptr;
1366 
1367     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1368 
1369     napi_get_undefined(env, &result);
1370     CameraSessionNapi* cameraSessionNapi = nullptr;
1371     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1372     if (status == napi_ok && cameraSessionNapi != nullptr) {
1373         int32_t value;
1374         napi_get_value_int32(env, argv[PARAM0], &value);
1375         VideoStabilizationMode videoStabilizationMode = (VideoStabilizationMode)value;
1376         int retCode = cameraSessionNapi->cameraSession_->SetVideoStabilizationMode(videoStabilizationMode);
1377         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1378     } else {
1379         MEDIA_ERR_LOG("SetVideoStabilizationMode call Failed!");
1380     }
1381     return result;
1382 }
1383 
HasFlash(napi_env env,napi_callback_info info)1384 napi_value CameraSessionNapi::HasFlash(napi_env env, napi_callback_info info)
1385 {
1386     MEDIA_DEBUG_LOG("HasFlash is called");
1387     napi_status status;
1388     napi_value result = nullptr;
1389     size_t argc = ARGS_ZERO;
1390     napi_value argv[ARGS_ZERO];
1391     napi_value thisVar = nullptr;
1392 
1393     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1394 
1395     napi_get_undefined(env, &result);
1396     CameraSessionNapi* cameraSessionNapi = nullptr;
1397     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1398     if (status == napi_ok && cameraSessionNapi != nullptr) {
1399         bool isSupported = false;
1400         int retCode = cameraSessionNapi->cameraSession_->HasFlash(isSupported);
1401         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1402         napi_get_boolean(env, isSupported, &result);
1403     } else {
1404         MEDIA_ERR_LOG("HasFlash call Failed!");
1405     }
1406     return result;
1407 }
1408 
IsFlashModeSupported(napi_env env,napi_callback_info info)1409 napi_value CameraSessionNapi::IsFlashModeSupported(napi_env env, napi_callback_info info)
1410 {
1411     MEDIA_DEBUG_LOG("IsFlashModeSupported is called");
1412     napi_status status;
1413     napi_value result = nullptr;
1414     size_t argc = ARGS_ONE;
1415     napi_value argv[ARGS_ONE] = {0};
1416     napi_value thisVar = nullptr;
1417 
1418     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1419 
1420     napi_get_undefined(env, &result);
1421     CameraSessionNapi* cameraSessionNapi = nullptr;
1422     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1423     if (status == napi_ok && cameraSessionNapi != nullptr) {
1424         int32_t value;
1425         napi_get_value_int32(env, argv[PARAM0], &value);
1426         FlashMode flashMode = (FlashMode)value;
1427         bool isSupported;
1428         int32_t retCode = cameraSessionNapi->cameraSession_->IsFlashModeSupported(flashMode, isSupported);
1429         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1430         napi_get_boolean(env, isSupported, &result);
1431     } else {
1432         MEDIA_ERR_LOG("IsFlashModeSupported call Failed!");
1433     }
1434     return result;
1435 }
1436 
SetFlashMode(napi_env env,napi_callback_info info)1437 napi_value CameraSessionNapi::SetFlashMode(napi_env env, napi_callback_info info)
1438 {
1439     MEDIA_DEBUG_LOG("SetFlashMode is called");
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         int32_t value;
1454         napi_get_value_int32(env, argv[PARAM0], &value);
1455         MEDIA_INFO_LOG("CameraSessionNapi::SetFlashMode mode:%{public}d", value);
1456         FlashMode flashMode = (FlashMode)value;
1457         cameraSessionNapi->cameraSession_->LockForControl();
1458         int retCode = cameraSessionNapi->cameraSession_->SetFlashMode(flashMode);
1459         cameraSessionNapi->cameraSession_->UnlockForControl();
1460         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1461     } else {
1462         MEDIA_ERR_LOG("SetFlashMode call Failed!");
1463     }
1464     return result;
1465 }
1466 
GetFlashMode(napi_env env,napi_callback_info info)1467 napi_value CameraSessionNapi::GetFlashMode(napi_env env, napi_callback_info info)
1468 {
1469     MEDIA_DEBUG_LOG("GetFlashMode is called");
1470     napi_status status;
1471     napi_value result = nullptr;
1472     size_t argc = ARGS_ZERO;
1473     napi_value argv[ARGS_ZERO];
1474     napi_value thisVar = nullptr;
1475 
1476     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1477     napi_get_undefined(env, &result);
1478     CameraSessionNapi* cameraSessionNapi = nullptr;
1479     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1480     if (status == napi_ok && cameraSessionNapi != nullptr) {
1481         FlashMode flashMode;
1482         int32_t retCode = cameraSessionNapi->cameraSession_->GetFlashMode(flashMode);
1483         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1484         napi_create_int32(env, flashMode, &result);
1485     } else {
1486         MEDIA_ERR_LOG("GetFlashMode call Failed!");
1487     }
1488     return result;
1489 }
1490 
IsLcdFlashSupported(napi_env env,napi_callback_info info)1491 napi_value CameraSessionNapi::IsLcdFlashSupported(napi_env env, napi_callback_info info)
1492 {
1493     MEDIA_ERR_LOG("SystemApi IsLcdFlashSupported is called!");
1494     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
1495         "System api can be invoked only by system applications");
1496     return CameraNapiUtils::GetUndefinedValue(env);
1497 }
1498 
EnableLcdFlash(napi_env env,napi_callback_info info)1499 napi_value CameraSessionNapi::EnableLcdFlash(napi_env env, napi_callback_info info)
1500 {
1501     MEDIA_ERR_LOG("SystemApi EnableLcdFlash is called!");
1502     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
1503         "System api can be invoked only by system applications");
1504     return CameraNapiUtils::GetUndefinedValue(env);
1505 }
1506 
IsExposureModeSupported(napi_env env,napi_callback_info info)1507 napi_value CameraSessionNapi::IsExposureModeSupported(napi_env env, napi_callback_info info)
1508 {
1509     MEDIA_DEBUG_LOG("IsExposureModeSupported is called");
1510     napi_status status;
1511     napi_value result = nullptr;
1512     size_t argc = ARGS_ONE;
1513     napi_value argv[ARGS_ONE] = {0};
1514     napi_value thisVar = nullptr;
1515 
1516     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1517 
1518     napi_get_undefined(env, &result);
1519     CameraSessionNapi* cameraSessionNapi = nullptr;
1520     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1521     if (status == napi_ok && cameraSessionNapi != nullptr) {
1522         int32_t value;
1523         napi_get_value_int32(env, argv[PARAM0], &value);
1524         ExposureMode exposureMode = (ExposureMode)value;
1525         bool isSupported;
1526         int32_t retCode = cameraSessionNapi->cameraSession_->
1527                     IsExposureModeSupported(static_cast<ExposureMode>(exposureMode), isSupported);
1528         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1529         napi_get_boolean(env, isSupported, &result);
1530     } else {
1531         MEDIA_ERR_LOG("IsExposureModeSupported call Failed!");
1532     }
1533     return result;
1534 }
1535 
GetExposureMode(napi_env env,napi_callback_info info)1536 napi_value CameraSessionNapi::GetExposureMode(napi_env env, napi_callback_info info)
1537 {
1538     MEDIA_DEBUG_LOG("GetExposureMode is called");
1539     napi_status status;
1540     napi_value result = nullptr;
1541     size_t argc = ARGS_ZERO;
1542     napi_value argv[ARGS_ZERO];
1543     napi_value thisVar = nullptr;
1544 
1545     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1546 
1547     napi_get_undefined(env, &result);
1548     CameraSessionNapi* cameraSessionNapi = nullptr;
1549     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1550     if (status == napi_ok && cameraSessionNapi != nullptr) {
1551         ExposureMode exposureMode;
1552         int32_t retCode = cameraSessionNapi->cameraSession_->GetExposureMode(exposureMode);
1553         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1554         napi_create_int32(env, exposureMode, &result);
1555     } else {
1556         MEDIA_ERR_LOG("GetExposureMode call Failed!");
1557     }
1558     return result;
1559 }
1560 
SetExposureMode(napi_env env,napi_callback_info info)1561 napi_value CameraSessionNapi::SetExposureMode(napi_env env, napi_callback_info info)
1562 {
1563     MEDIA_DEBUG_LOG("SetExposureMode is called");
1564     CAMERA_SYNC_TRACE;
1565     napi_status status;
1566     napi_value result = nullptr;
1567     size_t argc = ARGS_ONE;
1568     napi_value argv[ARGS_ONE] = {0};
1569     napi_value thisVar = nullptr;
1570 
1571     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1572 
1573     napi_get_undefined(env, &result);
1574     CameraSessionNapi* cameraSessionNapi = nullptr;
1575     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1576     if (status == napi_ok && cameraSessionNapi != nullptr) {
1577         int32_t value;
1578         napi_get_value_int32(env, argv[PARAM0], &value);
1579         ExposureMode exposureMode = (ExposureMode)value;
1580         cameraSessionNapi->cameraSession_->LockForControl();
1581         int retCode = cameraSessionNapi->cameraSession_->SetExposureMode(exposureMode);
1582         cameraSessionNapi->cameraSession_->UnlockForControl();
1583         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1584     } else {
1585         MEDIA_ERR_LOG("SetExposureMode call Failed!");
1586     }
1587     return result;
1588 }
1589 
SetMeteringPoint(napi_env env,napi_callback_info info)1590 napi_value CameraSessionNapi::SetMeteringPoint(napi_env env, napi_callback_info info)
1591 {
1592     MEDIA_DEBUG_LOG("SetMeteringPoint is called");
1593     CAMERA_SYNC_TRACE;
1594     napi_status status;
1595     napi_value result = nullptr;
1596     size_t argc = ARGS_ONE;
1597     napi_value argv[ARGS_ONE] = {0};
1598     napi_value thisVar = nullptr;
1599 
1600     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1601 
1602     napi_get_undefined(env, &result);
1603     CameraSessionNapi* cameraSessionNapi = nullptr;
1604     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1605     if (status == napi_ok && cameraSessionNapi != nullptr) {
1606         Point exposurePoint;
1607         if (GetPointProperties(env, argv[PARAM0], exposurePoint) == 0) {
1608             cameraSessionNapi->cameraSession_->LockForControl();
1609             int32_t retCode = cameraSessionNapi->cameraSession_->SetMeteringPoint(exposurePoint);
1610             cameraSessionNapi->cameraSession_->UnlockForControl();
1611             CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1612         } else {
1613             MEDIA_ERR_LOG("get point failed");
1614         }
1615     } else {
1616         MEDIA_ERR_LOG("SetMeteringPoint call Failed!");
1617     }
1618     return result;
1619 }
1620 
GetMeteringPoint(napi_env env,napi_callback_info info)1621 napi_value CameraSessionNapi::GetMeteringPoint(napi_env env, napi_callback_info info)
1622 {
1623     MEDIA_DEBUG_LOG("GetMeteringPoint is called");
1624     napi_status status;
1625     napi_value result = nullptr;
1626     size_t argc = ARGS_ZERO;
1627     napi_value argv[ARGS_ZERO];
1628     napi_value thisVar = nullptr;
1629 
1630     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1631     napi_get_undefined(env, &result);
1632     CameraSessionNapi* cameraSessionNapi = nullptr;
1633     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1634     if (status == napi_ok && cameraSessionNapi != nullptr) {
1635         Point exposurePoint;
1636         int32_t retCode = cameraSessionNapi->cameraSession_->GetMeteringPoint(exposurePoint);
1637         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1638         return GetPointNapiValue(env, exposurePoint);
1639     } else {
1640         MEDIA_ERR_LOG("GetMeteringPoint call Failed!");
1641     }
1642     return result;
1643 }
1644 
GetExposureBiasRange(napi_env env,napi_callback_info info)1645 napi_value CameraSessionNapi::GetExposureBiasRange(napi_env env, napi_callback_info info)
1646 {
1647     MEDIA_DEBUG_LOG("GetExposureBiasRange is called");
1648     napi_status status;
1649     napi_value result = nullptr;
1650     size_t argc = ARGS_ZERO;
1651     napi_value argv[ARGS_ZERO];
1652     napi_value thisVar = nullptr;
1653 
1654     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1655 
1656     napi_get_undefined(env, &result);
1657     CameraSessionNapi* cameraSessionNapi = nullptr;
1658     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1659     if (status == napi_ok && cameraSessionNapi != nullptr) {
1660         std::vector<float> vecExposureBiasList;
1661         int32_t retCode = cameraSessionNapi->cameraSession_->GetExposureBiasRange(vecExposureBiasList);
1662         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1663         CHECK_RETURN_RET(vecExposureBiasList.empty() || napi_create_array(env, &result) != napi_ok, result);
1664         size_t len = vecExposureBiasList.size();
1665         for (size_t i = 0; i < len; i++) {
1666             float exposureBias = vecExposureBiasList[i];
1667             MEDIA_DEBUG_LOG("EXPOSURE_BIAS_RANGE : exposureBias = %{public}f", vecExposureBiasList[i]);
1668             napi_value value;
1669             napi_create_double(env, CameraNapiUtils::FloatToDouble(exposureBias), &value);
1670             napi_set_element(env, result, i, value);
1671         }
1672         MEDIA_DEBUG_LOG("EXPOSURE_BIAS_RANGE ExposureBiasList size : %{public}zu", vecExposureBiasList.size());
1673     } else {
1674         MEDIA_ERR_LOG("GetExposureBiasRange call Failed!");
1675     }
1676     return result;
1677 }
1678 
GetExposureValue(napi_env env,napi_callback_info info)1679 napi_value CameraSessionNapi::GetExposureValue(napi_env env, napi_callback_info info)
1680 {
1681     MEDIA_DEBUG_LOG("GetExposureValue is called");
1682     napi_status status;
1683     napi_value result = nullptr;
1684     size_t argc = ARGS_ZERO;
1685     napi_value argv[ARGS_ZERO];
1686     napi_value thisVar = nullptr;
1687 
1688     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1689 
1690     napi_get_undefined(env, &result);
1691     CameraSessionNapi* cameraSessionNapi = nullptr;
1692     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1693     if (status == napi_ok && cameraSessionNapi!= nullptr) {
1694         float exposureValue;
1695         int32_t retCode = cameraSessionNapi->cameraSession_->GetExposureValue(exposureValue);
1696         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1697         napi_create_double(env, CameraNapiUtils::FloatToDouble(exposureValue), &result);
1698     } else {
1699         MEDIA_ERR_LOG("GetExposureValue call Failed!");
1700     }
1701     return result;
1702 }
1703 
SetExposureBias(napi_env env,napi_callback_info info)1704 napi_value CameraSessionNapi::SetExposureBias(napi_env env, napi_callback_info info)
1705 {
1706     MEDIA_DEBUG_LOG("SetExposureBias is called");
1707     CAMERA_SYNC_TRACE;
1708     napi_status status;
1709     napi_value result = nullptr;
1710     size_t argc = ARGS_ONE;
1711     napi_value argv[ARGS_ONE] = {0};
1712     napi_value thisVar = nullptr;
1713 
1714     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1715 
1716     napi_get_undefined(env, &result);
1717     CameraSessionNapi* cameraSessionNapi = nullptr;
1718     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1719     if (status == napi_ok && cameraSessionNapi != nullptr) {
1720         double exposureValue;
1721         napi_get_value_double(env, argv[PARAM0], &exposureValue);
1722         cameraSessionNapi->cameraSession_->LockForControl();
1723         int32_t retCode = cameraSessionNapi->cameraSession_->SetExposureBias((float)exposureValue);
1724         cameraSessionNapi->cameraSession_->UnlockForControl();
1725         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1726     } else {
1727         MEDIA_ERR_LOG("SetExposureBias call Failed!");
1728     }
1729     return result;
1730 }
1731 
IsFocusModeSupported(napi_env env,napi_callback_info info)1732 napi_value CameraSessionNapi::IsFocusModeSupported(napi_env env, napi_callback_info info)
1733 {
1734     MEDIA_DEBUG_LOG("IsFocusModeSupported is called");
1735     napi_status status;
1736     napi_value result = nullptr;
1737     size_t argc = ARGS_ONE;
1738     napi_value argv[ARGS_ONE] = {0};
1739     napi_value thisVar = nullptr;
1740 
1741     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1742 
1743     napi_get_undefined(env, &result);
1744     CameraSessionNapi* cameraSessionNapi = nullptr;
1745     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1746     if (status == napi_ok && cameraSessionNapi != nullptr) {
1747         int32_t value;
1748         napi_get_value_int32(env, argv[PARAM0], &value);
1749         FocusMode focusMode = (FocusMode)value;
1750         bool isSupported;
1751         int32_t retCode = cameraSessionNapi->cameraSession_->IsFocusModeSupported(focusMode,
1752                                                                                   isSupported);
1753         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1754         napi_get_boolean(env, isSupported, &result);
1755     } else {
1756         MEDIA_ERR_LOG("IsFocusModeSupported call Failed!");
1757     }
1758     return result;
1759 }
1760 
GetFocalLength(napi_env env,napi_callback_info info)1761 napi_value CameraSessionNapi::GetFocalLength(napi_env env, napi_callback_info info)
1762 {
1763     MEDIA_DEBUG_LOG("GetFocalLength is called");
1764     napi_status status;
1765     napi_value result = nullptr;
1766     size_t argc = ARGS_ZERO;
1767     napi_value argv[ARGS_ZERO];
1768     napi_value thisVar = nullptr;
1769 
1770     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1771 
1772     napi_get_undefined(env, &result);
1773     CameraSessionNapi* cameraSessionNapi = nullptr;
1774     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1775     if (status == napi_ok && cameraSessionNapi != nullptr) {
1776         float focalLength;
1777         int32_t retCode = cameraSessionNapi->cameraSession_->GetFocalLength(focalLength);
1778         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1779         napi_create_double(env, CameraNapiUtils::FloatToDouble(focalLength), &result);
1780     } else {
1781         MEDIA_ERR_LOG("GetFocalLength call Failed!");
1782     }
1783     return result;
1784 }
1785 
SetFocusPoint(napi_env env,napi_callback_info info)1786 napi_value CameraSessionNapi::SetFocusPoint(napi_env env, napi_callback_info info)
1787 {
1788     MEDIA_DEBUG_LOG("SetFocusPoint is called");
1789     CAMERA_SYNC_TRACE;
1790     napi_status status;
1791     napi_value result = nullptr;
1792     size_t argc = ARGS_ONE;
1793     napi_value argv[ARGS_ONE] = {0};
1794     napi_value thisVar = nullptr;
1795 
1796     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1797 
1798     napi_get_undefined(env, &result);
1799     CameraSessionNapi* cameraSessionNapi = nullptr;
1800     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1801     if (status == napi_ok && cameraSessionNapi != nullptr) {
1802         Point focusPoint;
1803         if (GetPointProperties(env, argv[PARAM0], focusPoint) == 0) {
1804             cameraSessionNapi->cameraSession_->LockForControl();
1805             int32_t retCode = cameraSessionNapi->cameraSession_->SetFocusPoint(focusPoint);
1806             cameraSessionNapi->cameraSession_->UnlockForControl();
1807             CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1808         } else {
1809             MEDIA_ERR_LOG("get point failed");
1810         }
1811     } else {
1812         MEDIA_ERR_LOG("SetFocusPoint call Failed!");
1813     }
1814     return result;
1815 }
1816 
GetFocusPoint(napi_env env,napi_callback_info info)1817 napi_value CameraSessionNapi::GetFocusPoint(napi_env env, napi_callback_info info)
1818 {
1819     MEDIA_DEBUG_LOG("GetFocusPoint is called");
1820     napi_status status;
1821     napi_value result = nullptr;
1822     size_t argc = ARGS_ZERO;
1823     napi_value argv[ARGS_ZERO];
1824     napi_value thisVar = nullptr;
1825 
1826     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1827 
1828     napi_get_undefined(env, &result);
1829     CameraSessionNapi* cameraSessionNapi = nullptr;
1830     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1831     if (status == napi_ok && cameraSessionNapi != nullptr) {
1832         Point focusPoint;
1833         int32_t retCode = cameraSessionNapi->cameraSession_->GetFocusPoint(focusPoint);
1834         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1835         return GetPointNapiValue(env, focusPoint);
1836     } else {
1837         MEDIA_ERR_LOG("GetFocusPoint call Failed!");
1838     }
1839     return result;
1840 }
1841 
GetFocusMode(napi_env env,napi_callback_info info)1842 napi_value CameraSessionNapi::GetFocusMode(napi_env env, napi_callback_info info)
1843 {
1844     MEDIA_DEBUG_LOG("GetFocusMode is called");
1845     napi_status status;
1846     napi_value result = nullptr;
1847     size_t argc = ARGS_ZERO;
1848     napi_value argv[ARGS_ZERO];
1849     napi_value thisVar = nullptr;
1850 
1851     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1852 
1853     napi_get_undefined(env, &result);
1854     CameraSessionNapi* cameraSessionNapi = nullptr;
1855     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1856     if (status == napi_ok && cameraSessionNapi != nullptr) {
1857         FocusMode focusMode;
1858         int32_t retCode = cameraSessionNapi->cameraSession_->GetFocusMode(focusMode);
1859         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1860         napi_create_int32(env, focusMode, &result);
1861     } else {
1862         MEDIA_ERR_LOG("GetFocusMode call Failed!");
1863     }
1864     return result;
1865 }
1866 
SetFocusMode(napi_env env,napi_callback_info info)1867 napi_value CameraSessionNapi::SetFocusMode(napi_env env, napi_callback_info info)
1868 {
1869     MEDIA_DEBUG_LOG("SetFocusMode is called");
1870     CAMERA_SYNC_TRACE;
1871     napi_status status;
1872     napi_value result = nullptr;
1873     size_t argc = ARGS_ONE;
1874     napi_value argv[ARGS_ONE] = {0};
1875     napi_value thisVar = nullptr;
1876 
1877     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1878 
1879     napi_get_undefined(env, &result);
1880     CameraSessionNapi* cameraSessionNapi = nullptr;
1881     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1882     if (status == napi_ok && cameraSessionNapi != nullptr) {
1883         int32_t value;
1884         napi_get_value_int32(env, argv[PARAM0], &value);
1885         FocusMode focusMode = (FocusMode)value;
1886         cameraSessionNapi->cameraSession_->LockForControl();
1887         int retCode = cameraSessionNapi->cameraSession_->
1888                 SetFocusMode(static_cast<FocusMode>(focusMode));
1889         cameraSessionNapi->cameraSession_->UnlockForControl();
1890         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1891     } else {
1892         MEDIA_ERR_LOG("SetFocusMode call Failed!");
1893     }
1894     return result;
1895 }
1896 
IsFocusRangeTypeSupported(napi_env env,napi_callback_info info)1897 napi_value CameraSessionNapi::IsFocusRangeTypeSupported(napi_env env, napi_callback_info info)
1898 {
1899     MEDIA_ERR_LOG("SystemApi IsFocusRangeTypeSupported is called!");
1900     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
1901         "System api can be invoked only by system applications");
1902     return nullptr;
1903 }
1904 
GetFocusRange(napi_env env,napi_callback_info info)1905 napi_value CameraSessionNapi::GetFocusRange(napi_env env, napi_callback_info info)
1906 {
1907     MEDIA_ERR_LOG("SystemApi IsFocusRangeTypeSupported is called!");
1908     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
1909         "System api can be invoked only by system applications");
1910     return nullptr;
1911 }
1912 
SetFocusRange(napi_env env,napi_callback_info info)1913 napi_value CameraSessionNapi::SetFocusRange(napi_env env, napi_callback_info info)
1914 {
1915     MEDIA_ERR_LOG("SystemApi IsFocusRangeTypeSupported is called!");
1916     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
1917         "System api can be invoked only by system applications");
1918     return nullptr;
1919 }
1920 
IsFocusDrivenTypeSupported(napi_env env,napi_callback_info info)1921 napi_value CameraSessionNapi::IsFocusDrivenTypeSupported(napi_env env, napi_callback_info info)
1922 {
1923     MEDIA_ERR_LOG("SystemApi IsFocusRangeTypeSupported is called!");
1924     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
1925         "System api can be invoked only by system applications");
1926     return nullptr;
1927 }
1928 
GetFocusDriven(napi_env env,napi_callback_info info)1929 napi_value CameraSessionNapi::GetFocusDriven(napi_env env, napi_callback_info info)
1930 {
1931     MEDIA_ERR_LOG("SystemApi GetFocusDriven is called!");
1932     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
1933         "System api can be invoked only by system applications");
1934     return nullptr;
1935 }
1936 
SetFocusDriven(napi_env env,napi_callback_info info)1937 napi_value CameraSessionNapi::SetFocusDriven(napi_env env, napi_callback_info info)
1938 {
1939     MEDIA_ERR_LOG("SystemApi SetFocusDriven is called!");
1940     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
1941         "System api can be invoked only by system applications");
1942     return nullptr;
1943 }
1944 
SetQualityPrioritization(napi_env env,napi_callback_info info)1945 napi_value CameraSessionNapi::SetQualityPrioritization(napi_env env, napi_callback_info info)
1946 {
1947     MEDIA_DEBUG_LOG("SetQualityPrioritization is called");
1948     CAMERA_SYNC_TRACE;
1949     napi_status status;
1950     napi_value result = nullptr;
1951     size_t argc = ARGS_ONE;
1952     napi_value argv[ARGS_ONE] = { 0 };
1953     napi_value thisVar = nullptr;
1954 
1955     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1956 
1957     napi_get_undefined(env, &result);
1958     CameraSessionNapi* cameraSessionNapi = nullptr;
1959     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1960     if (status == napi_ok && cameraSessionNapi != nullptr) {
1961         int32_t value;
1962         napi_get_value_int32(env, argv[PARAM0], &value);
1963         QualityPrioritization qualityPrioritization = (QualityPrioritization)value;
1964         cameraSessionNapi->cameraSession_->LockForControl();
1965         int retCode = cameraSessionNapi->cameraSession_->SetQualityPrioritization(
1966             static_cast<QualityPrioritization>(qualityPrioritization));
1967         cameraSessionNapi->cameraSession_->UnlockForControl();
1968         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1969     } else {
1970         MEDIA_ERR_LOG("SetQualityPrioritization call Failed!");
1971     }
1972     return result;
1973 }
1974 
GetZoomRatioRange(napi_env env,napi_callback_info info)1975 napi_value CameraSessionNapi::GetZoomRatioRange(napi_env env, napi_callback_info info)
1976 {
1977     MEDIA_DEBUG_LOG("GetZoomRatioRange is called");
1978     napi_status status;
1979     napi_value result = nullptr;
1980     size_t argc = ARGS_ZERO;
1981     napi_value argv[ARGS_ZERO];
1982     napi_value thisVar = nullptr;
1983 
1984     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1985 
1986     napi_get_undefined(env, &result);
1987 
1988     CameraSessionNapi* cameraSessionNapi = nullptr;
1989     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1990     if (status == napi_ok && cameraSessionNapi != nullptr) {
1991         std::vector<float> vecZoomRatioList;
1992         int32_t retCode = cameraSessionNapi->cameraSession_->GetZoomRatioRange(vecZoomRatioList);
1993         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
1994         MEDIA_INFO_LOG("CameraSessionNapi::GetZoomRatioRange len = %{public}zu",
1995             vecZoomRatioList.size());
1996 
1997         if (!vecZoomRatioList.empty() && napi_create_array(env, &result) == napi_ok) {
1998             for (size_t i = 0; i < vecZoomRatioList.size(); i++) {
1999                 float zoomRatio = vecZoomRatioList[i];
2000                 napi_value value;
2001                 napi_create_double(env, CameraNapiUtils::FloatToDouble(zoomRatio), &value);
2002                 napi_set_element(env, result, i, value);
2003             }
2004         } else {
2005             MEDIA_ERR_LOG("vecSupportedZoomRatioList is empty or failed to create array!");
2006         }
2007     } else {
2008         MEDIA_ERR_LOG("GetZoomRatioRange call Failed!");
2009     }
2010     return result;
2011 }
2012 
GetZoomRatio(napi_env env,napi_callback_info info)2013 napi_value CameraSessionNapi::GetZoomRatio(napi_env env, napi_callback_info info)
2014 {
2015     MEDIA_DEBUG_LOG("GetZoomRatio is called");
2016     napi_status status;
2017     napi_value result = nullptr;
2018     size_t argc = ARGS_ZERO;
2019     napi_value argv[ARGS_ZERO];
2020     napi_value thisVar = nullptr;
2021 
2022     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2023 
2024     napi_get_undefined(env, &result);
2025     CameraSessionNapi* cameraSessionNapi = nullptr;
2026     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2027     if (status == napi_ok && cameraSessionNapi != nullptr) {
2028         float zoomRatio;
2029         int32_t retCode = cameraSessionNapi->cameraSession_->GetZoomRatio(zoomRatio);
2030         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
2031         napi_create_double(env, CameraNapiUtils::FloatToDouble(zoomRatio), &result);
2032     } else {
2033         MEDIA_ERR_LOG("GetZoomRatio call Failed!");
2034     }
2035     return result;
2036 }
2037 
SetZoomRatio(napi_env env,napi_callback_info info)2038 napi_value CameraSessionNapi::SetZoomRatio(napi_env env, napi_callback_info info)
2039 {
2040     MEDIA_DEBUG_LOG("SetZoomRatio is called");
2041     CAMERA_SYNC_TRACE;
2042     napi_status status;
2043     napi_value result = nullptr;
2044 
2045     size_t argc = ARGS_ONE;
2046     napi_value argv[ARGS_ONE];
2047     napi_value thisVar = nullptr;
2048 
2049     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2050 
2051     napi_get_undefined(env, &result);
2052     CameraSessionNapi* cameraSessionNapi = nullptr;
2053     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2054     if (status == napi_ok && cameraSessionNapi != nullptr) {
2055         double zoomRatio;
2056         napi_get_value_double(env, argv[PARAM0], &zoomRatio);
2057         cameraSessionNapi->cameraSession_->LockForControl();
2058         int32_t retCode = cameraSessionNapi->cameraSession_->SetZoomRatio((float)zoomRatio);
2059         cameraSessionNapi->cameraSession_->UnlockForControl();
2060         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
2061     } else {
2062         MEDIA_ERR_LOG("SetZoomRatio call Failed!");
2063     }
2064     return result;
2065 }
2066 
PrepareZoom(napi_env env,napi_callback_info info)2067 napi_value CameraSessionNapi::PrepareZoom(napi_env env, napi_callback_info info)
2068 {
2069     MEDIA_ERR_LOG("SystemApi PrepareZoom is called!");
2070     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2071         "System api can be invoked only by system applications");
2072     return CameraNapiUtils::GetUndefinedValue(env);
2073 }
2074 
UnPrepareZoom(napi_env env,napi_callback_info info)2075 napi_value CameraSessionNapi::UnPrepareZoom(napi_env env, napi_callback_info info)
2076 {
2077     MEDIA_ERR_LOG("SystemApi UnPrepareZoom is called!");
2078     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2079         "System api can be invoked only by system applications");
2080     return CameraNapiUtils::GetUndefinedValue(env);
2081 }
2082 
SetSmoothZoom(napi_env env,napi_callback_info info)2083 napi_value CameraSessionNapi::SetSmoothZoom(napi_env env, napi_callback_info info)
2084 {
2085     MEDIA_DEBUG_LOG("SetSmoothZoom is called");
2086     CAMERA_SYNC_TRACE;
2087     napi_status status;
2088     napi_value result = nullptr;
2089 
2090     size_t argc = ARGS_TWO;
2091     napi_value argv[ARGS_TWO];
2092     napi_value thisVar = nullptr;
2093 
2094     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2095 
2096     napi_get_undefined(env, &result);
2097     CameraSessionNapi* cameraSessionNapi = nullptr;
2098     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2099     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2100         double targetZoomRatio;
2101         int32_t smoothZoomType;
2102         napi_get_value_double(env, argv[PARAM0], &targetZoomRatio);
2103         napi_get_value_int32(env, argv[PARAM1], &smoothZoomType);
2104         cameraSessionNapi->cameraSession_->SetSmoothZoom((float)targetZoomRatio, smoothZoomType);
2105     } else {
2106         MEDIA_ERR_LOG("SetSmoothZoom call Failed!");
2107     }
2108     return result;
2109 }
2110 
GetZoomPointInfos(napi_env env,napi_callback_info info)2111 napi_value CameraSessionNapi::GetZoomPointInfos(napi_env env, napi_callback_info info)
2112 {
2113     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2114         "SystemApi GetZoomPointInfos is called!");
2115     return CameraNapiUtils::GetUndefinedValue(env);
2116 }
2117 
IsZoomCenterPointSupported(napi_env env,napi_callback_info info)2118 napi_value CameraSessionNapi::IsZoomCenterPointSupported(napi_env env, napi_callback_info info)
2119 {
2120     CHECK_RETURN_RET_ELOG(!CameraNapiSecurity::CheckSystemApp(env), nullptr,
2121         "SystemApi IsZoomCenterPointSupported is called!");
2122     MEDIA_DEBUG_LOG("CameraSessionNapi::IsZoomCenterPointSupported is called");
2123     CameraSessionNapi* cameraSessionNapi = nullptr;
2124     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
2125     CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error"), nullptr,
2126         "CameraSessionNapi::IsZoomCenterPointSupported parse parameter occur error");
2127 
2128     auto result = CameraNapiUtils::GetUndefinedValue(env);
2129     if (cameraSessionNapi->cameraSession_ != nullptr) {
2130         bool isSupported = false;
2131         napi_get_boolean(env, isSupported, &result);
2132     } else {
2133         MEDIA_ERR_LOG("CameraSessionNapi::IsZoomCenterPointSupported get native object fail");
2134         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
2135         return nullptr;
2136     }
2137     return result;
2138 }
2139 
GetZoomCenterPoint(napi_env env,napi_callback_info info)2140 napi_value CameraSessionNapi::GetZoomCenterPoint(napi_env env, napi_callback_info info)
2141 {
2142     CHECK_RETURN_RET_ELOG(!CameraNapiSecurity::CheckSystemApp(env), nullptr,
2143         "SystemApi GetZoomCenterPoint is called!");
2144     MEDIA_DEBUG_LOG("GetZoomCenterPoint is called");
2145     CameraSessionNapi* cameraSessionNapi = nullptr;
2146     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
2147     CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error"), nullptr,
2148         "CameraSessionNapi::GetZoomCenterPoint parse parameter occur error");
2149 
2150     napi_value result = nullptr;
2151     napi_get_undefined(env, &result);
2152     return result;
2153 }
2154 
SetZoomCenterPoint(napi_env env,napi_callback_info info)2155 napi_value CameraSessionNapi::SetZoomCenterPoint(napi_env env, napi_callback_info info)
2156 {
2157     CHECK_RETURN_RET_ELOG(!CameraNapiSecurity::CheckSystemApp(env), nullptr,
2158         "SystemApi SetZoomCenterPoint is called!");
2159     MEDIA_DEBUG_LOG("CameraSessionNapi::SetZoomCenterPoint is called");
2160     double pointX = -1.0;
2161     double pointY = -1.0;
2162     CameraNapiObject zoomCenterPoint = {{{ "x", &pointX }, { "y", &pointY }}};
2163     CameraSessionNapi* cameraSessionNapi = nullptr;
2164     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, zoomCenterPoint);
2165     CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error"), nullptr,
2166         "CameraSessionNapi::SetZoomCenterPoint parse parameter occur error");
2167 
2168     if (cameraSessionNapi->cameraSession_ == nullptr) {
2169         MEDIA_ERR_LOG("CameraSessionNapi::SetZoomCenterPoint get native object fail");
2170         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
2171         return nullptr;
2172     }
2173     return CameraNapiUtils::GetUndefinedValue(env);
2174 }
2175 
GetSupportedFilters(napi_env env,napi_callback_info info)2176 napi_value CameraSessionNapi::GetSupportedFilters(napi_env env, napi_callback_info info)
2177 {
2178     MEDIA_DEBUG_LOG("getSupportedFilters is called");
2179     napi_status status;
2180     napi_value result = nullptr;
2181     size_t argc = ARGS_ZERO;
2182     napi_value argv[ARGS_ZERO];
2183     napi_value thisVar = nullptr;
2184 
2185     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2186 
2187     napi_get_undefined(env, &result);
2188     status = napi_create_array(env, &result);
2189     CHECK_RETURN_RET_ELOG(status != napi_ok, result, "napi_create_array call Failed!");
2190     CameraSessionNapi* cameraSessionNapi = nullptr;
2191     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2192     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2193         std::vector<FilterType> filterTypes = cameraSessionNapi->cameraSession_->GetSupportedFilters();
2194         MEDIA_INFO_LOG("CameraSessionNapi::GetSupportedFilters len = %{public}zu",
2195             filterTypes.size());
2196         if (!filterTypes.empty()) {
2197             for (size_t i = 0; i < filterTypes.size(); i++) {
2198                 FilterType filterType = filterTypes[i];
2199                 napi_value value;
2200                 napi_create_int32(env, filterType, &value);
2201                 napi_set_element(env, result, i, value);
2202             }
2203         }
2204     } else {
2205         MEDIA_ERR_LOG("GetSupportedFilters call Failed!");
2206     }
2207     return result;
2208 }
GetFilter(napi_env env,napi_callback_info info)2209 napi_value CameraSessionNapi::GetFilter(napi_env env, napi_callback_info info)
2210 {
2211     MEDIA_DEBUG_LOG("GetFilter is called");
2212     napi_status status;
2213     napi_value result = nullptr;
2214     size_t argc = ARGS_ZERO;
2215     napi_value argv[ARGS_ZERO];
2216     napi_value thisVar = nullptr;
2217 
2218     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2219 
2220     napi_get_undefined(env, &result);
2221     CameraSessionNapi* cameraSessionNapi = nullptr;
2222     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2223     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2224         FilterType filterType = cameraSessionNapi->cameraSession_->GetFilter();
2225         napi_create_int32(env, filterType, &result);
2226     } else {
2227         MEDIA_ERR_LOG("GetFilter call Failed!");
2228     }
2229     return result;
2230 }
SetFilter(napi_env env,napi_callback_info info)2231 napi_value CameraSessionNapi::SetFilter(napi_env env, napi_callback_info info)
2232 {
2233     MEDIA_DEBUG_LOG("setFilter is called");
2234     CAMERA_SYNC_TRACE;
2235     napi_value result = nullptr;
2236     napi_status status;
2237     size_t argc = ARGS_ONE;
2238     napi_value argv[ARGS_ONE] = {0};
2239     napi_value thisVar = nullptr;
2240 
2241     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2242 
2243     napi_get_undefined(env, &result);
2244     CameraSessionNapi* cameraSessionNapi = nullptr;
2245     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2246     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2247         int32_t value;
2248         napi_get_value_int32(env, argv[PARAM0], &value);
2249         FilterType filterType = (FilterType)value;
2250         cameraSessionNapi->cameraSession_->LockForControl();
2251         cameraSessionNapi->cameraSession_->
2252                 SetFilter(static_cast<FilterType>(filterType));
2253         cameraSessionNapi->cameraSession_->UnlockForControl();
2254     } else {
2255         MEDIA_ERR_LOG("SetFilter call Failed!");
2256     }
2257     return result;
2258 }
2259 
GetSupportedColorSpaces(napi_env env,napi_callback_info info)2260 napi_value CameraSessionNapi::GetSupportedColorSpaces(napi_env env, napi_callback_info info)
2261 {
2262     MEDIA_DEBUG_LOG("GetSupportedColorSpaces is called.");
2263     napi_status status;
2264     napi_value result = nullptr;
2265     size_t argc = ARGS_ZERO;
2266     napi_value argv[ARGS_ZERO];
2267     napi_value thisVar = nullptr;
2268 
2269     MEDIA_DEBUG_LOG("CameraSessionNapi::GetSupportedColorSpaces get js args");
2270     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2271 
2272     napi_get_undefined(env, &result);
2273     status = napi_create_array(env, &result);
2274     CHECK_RETURN_RET_ELOG(status != napi_ok, result, "napi_create_array call Failed!");
2275     CameraSessionNapi* cameraSessionNapi = nullptr;
2276     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2277     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2278         std::vector<ColorSpace> colorSpaces = cameraSessionNapi->cameraSession_->GetSupportedColorSpaces();
2279         if (!colorSpaces.empty()) {
2280             for (size_t i = 0; i < colorSpaces.size(); i++) {
2281                 int colorSpace = colorSpaces[i];
2282                 napi_value value;
2283                 napi_create_int32(env, colorSpace, &value);
2284                 napi_set_element(env, result, i, value);
2285             }
2286         }
2287     } else {
2288         MEDIA_ERR_LOG("GetSupportedColorSpaces call Failed!");
2289     }
2290     return result;
2291 }
2292 
IsControlCenterSupported(napi_env env,napi_callback_info info)2293 napi_value CameraSessionNapi::IsControlCenterSupported(napi_env env, napi_callback_info info)
2294 {
2295     MEDIA_INFO_LOG("CameraSessionNapi::IsControlCenterSupported is called.");
2296     CameraSessionNapi* cameraSessionNapi = nullptr;
2297     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
2298     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
2299         MEDIA_ERR_LOG("CameraSessionNapi::IsControlCenterSupported parse parameter occur error");
2300         return nullptr;
2301     }
2302     auto result = CameraNapiUtils::GetUndefinedValue(env);
2303     if (cameraSessionNapi->cameraSession_ != nullptr) {
2304         bool isSupported = cameraSessionNapi->cameraSession_->IsControlCenterSupported();
2305         napi_get_boolean(env, isSupported, &result);
2306     } else {
2307         MEDIA_ERR_LOG("CameraSessionNapi::IsControlCenterSupported get native object fail");
2308         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
2309         return nullptr;
2310     }
2311     return result;
2312 }
2313 
GetSupportedEffectTypes(napi_env env,napi_callback_info info)2314 napi_value CameraSessionNapi::GetSupportedEffectTypes(napi_env env, napi_callback_info info)
2315 {
2316     MEDIA_INFO_LOG("CameraSessionNapi::GetSupportedEffectTypes is called.");
2317     napi_status status;
2318     napi_value result = nullptr;
2319     size_t argc = ARGS_ZERO;
2320     napi_value argv[ARGS_ZERO];
2321     napi_value thisVar = nullptr;
2322 
2323     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2324     napi_get_undefined(env, &result);
2325     status = napi_create_array(env, &result);
2326     CHECK_RETURN_RET_ELOG(status != napi_ok, result, "napi_create_array call Failed!");
2327     CameraSessionNapi* cameraSessionNapi = nullptr;
2328     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2329     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2330         std::vector<ControlCenterEffectType> effectTypes
2331             = cameraSessionNapi->cameraSession_->GetSupportedEffectTypes();
2332         if (!effectTypes.empty()) {
2333             for (size_t i = 0; i < effectTypes.size(); i++) {
2334                 ControlCenterEffectType effectType = effectTypes[i];
2335                 napi_value value;
2336                 napi_create_int32(env, static_cast<int32_t>(effectType), &value);
2337                 napi_set_element(env, result, i, value);
2338             }
2339         }
2340     } else {
2341         MEDIA_ERR_LOG("GetSupportedEffectTypes call Failed!");
2342     }
2343     return result;
2344 }
2345 
EnableControlCenter(napi_env env,napi_callback_info info)2346 napi_value CameraSessionNapi::EnableControlCenter(napi_env env, napi_callback_info info)
2347 {
2348     MEDIA_INFO_LOG("CameraSessionNapi::EnableControlCenter");
2349     napi_value result = CameraNapiUtils::GetUndefinedValue(env);
2350     bool isEnableControlCenter;
2351     CameraSessionNapi* cameraSessionNapi = nullptr;
2352     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, isEnableControlCenter);
2353     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
2354         MEDIA_ERR_LOG("CameraSessionNapi::EnableControlCenter parse parameter occur error");
2355         return result;
2356     }
2357     if (cameraSessionNapi->cameraSession_ != nullptr) {
2358         MEDIA_INFO_LOG("CameraSessionNapi::EnableMacro:%{public}d", isEnableControlCenter);
2359         cameraSessionNapi->cameraSession_->LockForControl();
2360         cameraSessionNapi->cameraSession_->EnableControlCenter(isEnableControlCenter);
2361         cameraSessionNapi->cameraSession_->UnlockForControl();
2362     } else {
2363         MEDIA_ERR_LOG("CameraSessionNapi::EnableControlCenter get native object fail");
2364         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
2365         return result;
2366     }
2367     return result;
2368 }
2369 
GetActiveColorSpace(napi_env env,napi_callback_info info)2370 napi_value CameraSessionNapi::GetActiveColorSpace(napi_env env, napi_callback_info info)
2371 {
2372     MEDIA_DEBUG_LOG("GetActiveColorSpace is called");
2373     napi_value result = nullptr;
2374     size_t argc = ARGS_ZERO;
2375     napi_value argv[ARGS_ZERO];
2376     napi_status status;
2377     napi_value thisVar = nullptr;
2378 
2379     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2380 
2381     napi_get_undefined(env, &result);
2382     CameraSessionNapi* cameraSessionNapi = nullptr;
2383     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2384     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2385         ColorSpace colorSpace;
2386         int32_t retCode = cameraSessionNapi->cameraSession_->GetActiveColorSpace(colorSpace);
2387         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), result);
2388         napi_create_int32(env, colorSpace, &result);
2389     } else {
2390         MEDIA_ERR_LOG("GetActiveColorSpace call Failed!");
2391     }
2392     return result;
2393 }
2394 
SetColorSpace(napi_env env,napi_callback_info info)2395 napi_value CameraSessionNapi::SetColorSpace(napi_env env, napi_callback_info info)
2396 {
2397     MEDIA_DEBUG_LOG("SetColorSpace is called");
2398     CAMERA_SYNC_TRACE;
2399     napi_status status;
2400     napi_value result = nullptr;
2401     size_t argc = ARGS_ONE;
2402     napi_value argv[ARGS_ONE] = {0};
2403     napi_value thisVar = nullptr;
2404 
2405     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2406 
2407     napi_get_undefined(env, &result);
2408     CameraSessionNapi* cameraSessionNapi = nullptr;
2409     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2410     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2411         int32_t colorSpaceNumber;
2412         napi_get_value_int32(env, argv[PARAM0], &colorSpaceNumber);
2413         ColorSpace colorSpace = (ColorSpace)colorSpaceNumber;
2414         int32_t retCode = cameraSessionNapi->cameraSession_->SetColorSpace(colorSpace);
2415         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), result);
2416     } else {
2417         MEDIA_ERR_LOG("SetColorSpace call Failed!");
2418     }
2419     return result;
2420 }
2421 
IsMacroSupported(napi_env env,napi_callback_info info)2422 napi_value CameraSessionNapi::IsMacroSupported(napi_env env, napi_callback_info info)
2423 {
2424     MEDIA_DEBUG_LOG("CameraSessionNapi::IsMacroSupported is called");
2425     CameraSessionNapi* cameraSessionNapi = nullptr;
2426     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
2427     CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error"), nullptr,
2428         "CameraSessionNapi::IsMacroSupported parse parameter occur error");
2429     auto result = CameraNapiUtils::GetUndefinedValue(env);
2430     if (cameraSessionNapi->cameraSession_ != nullptr) {
2431         bool isSupported = cameraSessionNapi->cameraSession_->IsMacroSupported();
2432         napi_get_boolean(env, isSupported, &result);
2433     } else {
2434         MEDIA_ERR_LOG("CameraSessionNapi::IsMacroSupported get native object fail");
2435         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
2436         return nullptr;
2437     }
2438     return result;
2439 }
2440 
EnableMacro(napi_env env,napi_callback_info info)2441 napi_value CameraSessionNapi::EnableMacro(napi_env env, napi_callback_info info)
2442 {
2443     MEDIA_DEBUG_LOG("CameraSessionNapi::EnableMacro is called");
2444     bool isEnableMacro;
2445     CameraSessionNapi* cameraSessionNapi = nullptr;
2446     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, isEnableMacro);
2447     CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error"), nullptr,
2448         "CameraSessionNapi::EnableMacro parse parameter occur error");
2449 
2450     if (cameraSessionNapi->cameraSession_ != nullptr) {
2451         MEDIA_INFO_LOG("CameraSessionNapi::EnableMacro:%{public}d", isEnableMacro);
2452         cameraSessionNapi->cameraSession_->LockForControl();
2453         int32_t retCode = cameraSessionNapi->cameraSession_->EnableMacro(isEnableMacro);
2454         cameraSessionNapi->cameraSession_->UnlockForControl();
2455         CHECK_RETURN_RET_ELOG(!CameraNapiUtils::CheckError(env, retCode), nullptr,
2456             "CameraSessionNapi::EnableMacro fail %{public}d", retCode);
2457     } else {
2458         MEDIA_ERR_LOG("CameraSessionNapi::EnableMacro get native object fail");
2459         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
2460         return nullptr;
2461     }
2462     return CameraNapiUtils::GetUndefinedValue(env);
2463 }
2464 
GetSupportedBeautyTypes(napi_env env,napi_callback_info info)2465 napi_value CameraSessionNapi::GetSupportedBeautyTypes(napi_env env, napi_callback_info info)
2466 {
2467     MEDIA_DEBUG_LOG("GetSupportedBeautyTypes is called");
2468     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2469         "SystemApi GetSupportedBeautyTypes is called!");
2470     return CameraNapiUtils::GetUndefinedValue(env);
2471 }
2472 
GetSupportedBeautyRange(napi_env env,napi_callback_info info)2473 napi_value CameraSessionNapi::GetSupportedBeautyRange(napi_env env, napi_callback_info info)
2474 {
2475     MEDIA_DEBUG_LOG("GetSupportedBeautyRange is called");
2476     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2477         "SystemApi GetSupportedBeautyRange is called!");
2478     return CameraNapiUtils::GetUndefinedValue(env);
2479 }
2480 
GetBeauty(napi_env env,napi_callback_info info)2481 napi_value CameraSessionNapi::GetBeauty(napi_env env, napi_callback_info info)
2482 {
2483     MEDIA_DEBUG_LOG("GetBeauty is called");
2484     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2485         "SystemApi GetBeauty is called!");
2486     return CameraNapiUtils::GetUndefinedValue(env);
2487 }
2488 
SetBeauty(napi_env env,napi_callback_info info)2489 napi_value CameraSessionNapi::SetBeauty(napi_env env, napi_callback_info info)
2490 {
2491     MEDIA_DEBUG_LOG("SetBeauty is called");
2492     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2493         "SystemApi SetBeauty is called!");
2494     return CameraNapiUtils::GetUndefinedValue(env);
2495 }
2496 
IsMoonCaptureBoostSupported(napi_env env,napi_callback_info info)2497 napi_value CameraSessionNapi::IsMoonCaptureBoostSupported(napi_env env, napi_callback_info info)
2498 {
2499     CHECK_RETURN_RET_ELOG(!CameraNapiSecurity::CheckSystemApp(env), nullptr,
2500         "SystemApi IsMoonCaptureBoostSupported is called!");
2501     MEDIA_DEBUG_LOG("IsMoonCaptureBoostSupported is called");
2502     napi_status status;
2503     napi_value result = nullptr;
2504     size_t argc = ARGS_ZERO;
2505     napi_value argv[ARGS_ZERO];
2506     napi_value thisVar = nullptr;
2507 
2508     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2509 
2510     napi_get_undefined(env, &result);
2511     CameraSessionNapi* cameraSessionNapi = nullptr;
2512     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2513     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2514         bool isSupported = cameraSessionNapi->cameraSession_->IsMoonCaptureBoostSupported();
2515         napi_get_boolean(env, isSupported, &result);
2516     } else {
2517         MEDIA_ERR_LOG("IsMoonCaptureBoostSupported call Failed!");
2518     }
2519     return result;
2520 }
2521 
EnableMoonCaptureBoost(napi_env env,napi_callback_info info)2522 napi_value CameraSessionNapi::EnableMoonCaptureBoost(napi_env env, napi_callback_info info)
2523 {
2524     CHECK_RETURN_RET_ELOG(!CameraNapiSecurity::CheckSystemApp(env), nullptr,
2525         "SystemApi EnableMoonCaptureBoost is called!");
2526     MEDIA_DEBUG_LOG("EnableMoonCaptureBoost is called");
2527     napi_status status;
2528     napi_value result = nullptr;
2529     size_t argc = ARGS_ONE;
2530     napi_value argv[ARGS_ONE] = { 0 };
2531     napi_value thisVar = nullptr;
2532     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2533     NAPI_ASSERT(env, argc == ARGS_ONE, "requires one parameter");
2534     napi_valuetype valueType = napi_undefined;
2535     napi_typeof(env, argv[0], &valueType);
2536     CHECK_RETURN_RET(valueType != napi_boolean && !CameraNapiUtils::CheckError(env, INVALID_ARGUMENT), result);
2537     napi_get_undefined(env, &result);
2538     CameraSessionNapi* cameraSessionNapi = nullptr;
2539     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2540     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2541         bool isEnableMoonCaptureBoost;
2542         napi_get_value_bool(env, argv[PARAM0], &isEnableMoonCaptureBoost);
2543         MEDIA_INFO_LOG("CameraSessionNapi::EnableMoonCaptureBoost:%{public}d", isEnableMoonCaptureBoost);
2544         cameraSessionNapi->cameraSession_->LockForControl();
2545         int32_t retCode = cameraSessionNapi->cameraSession_->EnableMoonCaptureBoost(isEnableMoonCaptureBoost);
2546         cameraSessionNapi->cameraSession_->UnlockForControl();
2547         CHECK_RETURN_RET(retCode != 0 && !CameraNapiUtils::CheckError(env, retCode), result);
2548     }
2549     return result;
2550 }
2551 
CanPreconfig(napi_env env,napi_callback_info info)2552 napi_value CameraSessionNapi::CanPreconfig(napi_env env, napi_callback_info info)
2553 {
2554     MEDIA_DEBUG_LOG("CanPreconfig is called");
2555     size_t argSize = CameraNapiUtils::GetNapiArgs(env, info);
2556     int32_t configType;
2557     int32_t profileSizeRatio = ProfileSizeRatio::UNSPECIFIED;
2558     CameraSessionNapi* cameraSessionNapi = nullptr;
2559     if (argSize == ARGS_ONE) {
2560         CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, configType);
2561         CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error"),
2562             nullptr, "CameraSessionNapi::CanPreconfig parse parameter occur error");
2563     } else {
2564         CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, configType, profileSizeRatio);
2565         CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error"),
2566             nullptr, "CameraSessionNapi::CanPreconfig parse 2 parameter occur error");
2567     }
2568 
2569     MEDIA_INFO_LOG("CameraSessionNapi::CanPreconfig: %{public}d, ratioType:%{public}d", configType, profileSizeRatio);
2570     bool result = cameraSessionNapi->cameraSession_->CanPreconfig(
2571         static_cast<PreconfigType>(configType), static_cast<ProfileSizeRatio>(profileSizeRatio));
2572     return CameraNapiUtils::GetBooleanValue(env, result);
2573 }
2574 
Preconfig(napi_env env,napi_callback_info info)2575 napi_value CameraSessionNapi::Preconfig(napi_env env, napi_callback_info info)
2576 {
2577     MEDIA_DEBUG_LOG("Preconfig is called");
2578     size_t argSize = CameraNapiUtils::GetNapiArgs(env, info);
2579     int32_t configType;
2580     int32_t profileSizeRatio = ProfileSizeRatio::UNSPECIFIED;
2581     CameraSessionNapi* cameraSessionNapi = nullptr;
2582     if (argSize == ARGS_ONE) {
2583         CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, configType);
2584         CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error"),
2585             nullptr, "CameraSessionNapi::Preconfig parse parameter occur error");
2586     } else {
2587         CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, configType, profileSizeRatio);
2588         CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error"),
2589             nullptr, "CameraSessionNapi::Preconfig parse 2 parameter occur error");
2590     }
2591     int32_t retCode = cameraSessionNapi->cameraSession_->Preconfig(
2592         static_cast<PreconfigType>(configType), static_cast<ProfileSizeRatio>(profileSizeRatio));
2593     CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
2594     return CameraNapiUtils::GetUndefinedValue(env);
2595 }
2596 
GetCameraOutputCapabilities(napi_env env,napi_callback_info info)2597 napi_value CameraSessionNapi::GetCameraOutputCapabilities(napi_env env, napi_callback_info info)
2598 {
2599     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2600         "SystemApi GetCameraOutputCapabilities is called!");
2601     return CameraNapiUtils::GetUndefinedValue(env);
2602 }
2603 
GetSessionFunctions(napi_env env,napi_callback_info info)2604 napi_value CameraSessionNapi::GetSessionFunctions(napi_env env, napi_callback_info info)
2605 {
2606     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2607         "SystemApi GetSessionFunctions is called!");
2608     return CameraNapiUtils::GetUndefinedValue(env);
2609 }
2610 
GetSessionConflictFunctions(napi_env env,napi_callback_info info)2611 napi_value CameraSessionNapi::GetSessionConflictFunctions(napi_env env, napi_callback_info info)
2612 {
2613     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2614         "SystemApi GetSessionConflictFunctions is called!");
2615     return CameraNapiUtils::GetUndefinedValue(env);
2616 }
2617 
2618 // ------------------------------------------------auto_awb_props-------------------------------------------------------
GetSupportedWhiteBalanceModes(napi_env env,napi_callback_info info)2619 napi_value CameraSessionNapi::GetSupportedWhiteBalanceModes(napi_env env, napi_callback_info info)
2620 {
2621     MEDIA_DEBUG_LOG("CameraSessionNapi::GetSupportedWhiteBalanceModes is called");
2622     napi_status status;
2623     napi_value result = nullptr;
2624     size_t argc = ARGS_ZERO;
2625     napi_value argv[ARGS_ZERO];
2626     napi_value thisVar = nullptr;
2627 
2628     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2629 
2630     napi_get_undefined(env, &result);
2631     status = napi_create_array(env, &result);
2632     CHECK_RETURN_RET_ELOG(status != napi_ok, result,
2633         "CameraSessionNapi::GetSupportedWhiteBalanceModes napi_create_array call Failed!");
2634     CameraSessionNapi* cameraSessionNapi = nullptr;
2635     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2636     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2637         std::vector<WhiteBalanceMode> whiteBalanceModes;
2638         int32_t retCode = cameraSessionNapi->cameraSession_->GetSupportedWhiteBalanceModes(whiteBalanceModes);
2639         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
2640 
2641         MEDIA_INFO_LOG("CameraSessionNapi::GetSupportedWhiteBalanceModes len = %{public}zu",
2642             whiteBalanceModes.size());
2643         if (!whiteBalanceModes.empty()) {
2644             for (size_t i = 0; i < whiteBalanceModes.size(); i++) {
2645                 WhiteBalanceMode whiteBalanceMode = whiteBalanceModes[i];
2646                 napi_value value;
2647                 napi_create_int32(env, whiteBalanceMode, &value);
2648                 napi_set_element(env, result, i, value);
2649             }
2650         }
2651     } else {
2652         MEDIA_ERR_LOG("CameraSessionNapi::GetSupportedWhiteBalanceModes call Failed!");
2653     }
2654     return result;
2655 }
2656 
IsWhiteBalanceModeSupported(napi_env env,napi_callback_info info)2657 napi_value CameraSessionNapi::IsWhiteBalanceModeSupported(napi_env env, napi_callback_info info)
2658 {
2659     MEDIA_DEBUG_LOG("IsWhiteBalanceModeSupported is called");
2660     napi_status status;
2661     size_t argc = ARGS_ONE;
2662     napi_value result = nullptr;
2663     napi_value argv[ARGS_ONE] = {0};
2664     napi_value thisVar = nullptr;
2665 
2666     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2667 
2668     napi_get_undefined(env, &result);
2669     CameraSessionNapi* cameraSessionNapi = nullptr;
2670     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2671     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2672         int32_t value;
2673         napi_get_value_int32(env, argv[PARAM0], &value);
2674         WhiteBalanceMode mode = (WhiteBalanceMode)value;
2675         bool isSupported;
2676         int32_t retCode = cameraSessionNapi->cameraSession_->IsWhiteBalanceModeSupported(mode, isSupported);
2677         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
2678         napi_get_boolean(env, isSupported, &result);
2679     } else {
2680         MEDIA_ERR_LOG("IsWhiteBalanceModeSupported call Failed!");
2681     }
2682     return result;
2683 }
2684 
GetWhiteBalanceMode(napi_env env,napi_callback_info info)2685 napi_value CameraSessionNapi::GetWhiteBalanceMode(napi_env env, napi_callback_info info)
2686 {
2687     MEDIA_DEBUG_LOG("GetWhiteBalanceMode is called");
2688     napi_status status;
2689     napi_value whiteBalanceResult = nullptr;
2690     size_t argc = ARGS_ZERO;
2691     napi_value argv[ARGS_ZERO];
2692     napi_value thisVar = nullptr;
2693 
2694     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2695 
2696     napi_get_undefined(env, &whiteBalanceResult);
2697     CameraSessionNapi* cameraSessionNapi = nullptr;
2698     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2699     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2700         WhiteBalanceMode whiteBalanceMode;
2701         int32_t retCode = cameraSessionNapi->cameraSession_->GetWhiteBalanceMode(whiteBalanceMode);
2702         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
2703         napi_create_int32(env, whiteBalanceMode, &whiteBalanceResult);
2704     } else {
2705         MEDIA_ERR_LOG("GetWhiteBalanceMode call Failed!");
2706     }
2707     return whiteBalanceResult;
2708 }
2709 
SetWhiteBalanceMode(napi_env env,napi_callback_info info)2710 napi_value CameraSessionNapi::SetWhiteBalanceMode(napi_env env, napi_callback_info info)
2711 {
2712     MEDIA_DEBUG_LOG("SetWhiteBalanceMode is called");
2713     CAMERA_SYNC_TRACE;
2714     napi_status status;
2715     napi_value result = nullptr;
2716     size_t argc = ARGS_ONE;
2717     napi_value argv[ARGS_ONE] = {0};
2718     napi_value thisVar = nullptr;
2719 
2720     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2721 
2722     napi_get_undefined(env, &result);
2723     CameraSessionNapi* cameraSessionNapi = nullptr;
2724     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2725     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2726         int32_t value;
2727         napi_get_value_int32(env, argv[PARAM0], &value);
2728         WhiteBalanceMode mode = (WhiteBalanceMode)value;
2729         cameraSessionNapi->cameraSession_->LockForControl();
2730         cameraSessionNapi->cameraSession_->SetWhiteBalanceMode(mode);
2731         MEDIA_INFO_LOG("CameraSessionNapi::SetWhiteBalanceMode set mode:%{public}d", value);
2732         cameraSessionNapi->cameraSession_->UnlockForControl();
2733     } else {
2734         MEDIA_ERR_LOG("SetWhiteBalanceMode call Failed!");
2735     }
2736     return result;
2737 }
2738 
2739 // -----------------------------------------------manual_awb_props------------------------------------------------------
GetWhiteBalanceRange(napi_env env,napi_callback_info info)2740 napi_value CameraSessionNapi::GetWhiteBalanceRange(napi_env env, napi_callback_info info)
2741 {
2742     MEDIA_DEBUG_LOG("GetWhiteBalanceRange is called");
2743     napi_status status;
2744     napi_value whiteBalanceRangeResult = nullptr;
2745     size_t argc = ARGS_ZERO;
2746     napi_value argv[ARGS_ZERO];
2747     napi_value thisVar = nullptr;
2748 
2749     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2750 
2751     napi_get_undefined(env, &whiteBalanceRangeResult);
2752 
2753     CameraSessionNapi* cameraSessionNapi = nullptr;
2754     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2755     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2756         std::vector<int32_t> whiteBalanceRange = {};
2757         int32_t retCode = cameraSessionNapi->cameraSession_->GetManualWhiteBalanceRange(whiteBalanceRange);
2758         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
2759         MEDIA_INFO_LOG("CameraSessionNapi::GetWhiteBalanceRange len = %{public}zu", whiteBalanceRange.size());
2760 
2761         if (!whiteBalanceRange.empty() && napi_create_array(env, &whiteBalanceRangeResult) == napi_ok) {
2762             for (size_t i = 0; i < whiteBalanceRange.size(); i++) {
2763                 int32_t iso = whiteBalanceRange[i];
2764                 napi_value value;
2765                 napi_create_int32(env, iso, &value);
2766                 napi_set_element(env, whiteBalanceRangeResult, i, value);
2767             }
2768         } else {
2769             MEDIA_ERR_LOG("whiteBalanceRange is empty or failed to create array!");
2770         }
2771     } else {
2772         MEDIA_ERR_LOG("GetWhiteBalanceRange call Failed!");
2773     }
2774     return whiteBalanceRangeResult;
2775 }
2776 
IsManualWhiteBalanceSupported(napi_env env,napi_callback_info info)2777 napi_value CameraSessionNapi::IsManualWhiteBalanceSupported(napi_env env, napi_callback_info info)
2778 {
2779     CHECK_RETURN_RET_ELOG(!CameraNapiSecurity::CheckSystemApp(env), nullptr,
2780         "SystemApi IsManualWhiteBalanceSupported is called!");
2781     MEDIA_DEBUG_LOG("IsManualWhiteBalanceSupported is called");
2782     napi_status status;
2783     napi_value manualWhiteBalanceSupportedResult = nullptr;
2784     size_t argc = ARGS_ZERO;
2785     napi_value argv[ARGS_ZERO];
2786     napi_value thisVar = nullptr;
2787 
2788     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2789 
2790     napi_get_undefined(env, &manualWhiteBalanceSupportedResult);
2791     CameraSessionNapi* cameraSessionNapi = nullptr;
2792     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2793     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2794         bool isSupported;
2795         int32_t retCode = cameraSessionNapi->cameraSession_->IsManualWhiteBalanceSupported(isSupported);
2796         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
2797         napi_get_boolean(env, isSupported, &manualWhiteBalanceSupportedResult);
2798     } else {
2799         MEDIA_ERR_LOG("IsManualIsoSupported call Failed!");
2800     }
2801     return manualWhiteBalanceSupportedResult;
2802 }
2803 
GetWhiteBalance(napi_env env,napi_callback_info info)2804 napi_value CameraSessionNapi::GetWhiteBalance(napi_env env, napi_callback_info info)
2805 {
2806     MEDIA_DEBUG_LOG("GetWhiteBalance is called");
2807     napi_status status;
2808     napi_value result = nullptr;
2809     size_t argc = ARGS_ZERO;
2810     napi_value argv[ARGS_ZERO];
2811     napi_value thisVar = nullptr;
2812 
2813     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2814 
2815     napi_get_undefined(env, &result);
2816     CameraSessionNapi* cameraSessionNapi = nullptr;
2817     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2818     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2819         int32_t wbValue;
2820         int32_t retCode = cameraSessionNapi->cameraSession_->GetManualWhiteBalance(wbValue);
2821         CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
2822         napi_create_int32(env, wbValue, &result);
2823     } else {
2824         MEDIA_ERR_LOG("GetWhiteBalance call Failed!");
2825     }
2826     return result;
2827 }
2828 
SetWhiteBalance(napi_env env,napi_callback_info info)2829 napi_value CameraSessionNapi::SetWhiteBalance(napi_env env, napi_callback_info info)
2830 {
2831     MEDIA_DEBUG_LOG("SetWhiteBalance is called");
2832     CAMERA_SYNC_TRACE;
2833     napi_status status;
2834     napi_value result = nullptr;
2835     size_t argc = ARGS_ONE;
2836     napi_value argv[ARGS_ONE] = {0};
2837     napi_value thisVar = nullptr;
2838 
2839     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2840 
2841     napi_get_undefined(env, &result);
2842     CameraSessionNapi* cameraSessionNapi = nullptr;
2843     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2844     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2845         int32_t wbValue;
2846         napi_get_value_int32(env, argv[PARAM0], &wbValue);
2847         cameraSessionNapi->cameraSession_->LockForControl();
2848         cameraSessionNapi->cameraSession_->SetManualWhiteBalance(wbValue);
2849         MEDIA_INFO_LOG("CameraSessionNapi::SetWhiteBalance set wbValue:%{public}d", wbValue);
2850         cameraSessionNapi->cameraSession_->UnlockForControl();
2851     } else {
2852         MEDIA_ERR_LOG("SetWhiteBalance call Failed!");
2853     }
2854     return result;
2855 }
2856 
SetUsage(napi_env env,napi_callback_info info)2857 napi_value CameraSessionNapi::SetUsage(napi_env env, napi_callback_info info)
2858 {
2859     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2860         "SystemApi SetUsage is called!");
2861     return CameraNapiUtils::GetUndefinedValue(env);
2862 }
2863 
RegisterExposureCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)2864 void CameraSessionNapi::RegisterExposureCallbackListener(
2865     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
2866 {
2867     if (exposureCallback_ == nullptr) {
2868         exposureCallback_ = std::make_shared<ExposureCallbackListener>(env);
2869         cameraSession_->SetExposureCallback(exposureCallback_);
2870     }
2871     exposureCallback_->SaveCallbackReference(eventName, callback, isOnce);
2872 }
2873 
UnregisterExposureCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)2874 void CameraSessionNapi::UnregisterExposureCallbackListener(
2875     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
2876 {
2877     CHECK_RETURN_ELOG(exposureCallback_ == nullptr, "exposureCallback is null");
2878     exposureCallback_->RemoveCallbackRef(eventName, callback);
2879 }
2880 
RegisterFocusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)2881 void CameraSessionNapi::RegisterFocusCallbackListener(
2882     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
2883 {
2884     if (focusCallback_ == nullptr) {
2885         focusCallback_ = make_shared<FocusCallbackListener>(env);
2886         cameraSession_->SetFocusCallback(focusCallback_);
2887     }
2888     focusCallback_->SaveCallbackReference(eventName, callback, isOnce);
2889 }
2890 
UnregisterFocusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)2891 void CameraSessionNapi::UnregisterFocusCallbackListener(
2892     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
2893 {
2894     CHECK_RETURN_ELOG(focusCallback_ == nullptr, "focusCallback is null");
2895     focusCallback_->RemoveCallbackRef(eventName, callback);
2896 }
2897 
RegisterMacroStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)2898 void CameraSessionNapi::RegisterMacroStatusCallbackListener(
2899     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
2900 {
2901     if (macroStatusCallback_ == nullptr) {
2902         MEDIA_DEBUG_LOG("CameraSessionNapi::RegisterMacroStatusCallbackListener SET CALLBACK");
2903         macroStatusCallback_ = std::make_shared<MacroStatusCallbackListener>(env);
2904         CHECK_RETURN_ELOG(macroStatusCallback_ == nullptr,
2905             "CameraSessionNapi::RegisterMacroStatusCallbackListener THE CALLBACK IS NULL");
2906         cameraSession_->SetMacroStatusCallback(macroStatusCallback_);
2907     }
2908     macroStatusCallback_->SaveCallbackReference(eventName, callback, isOnce);
2909 }
2910 
UnregisterMacroStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)2911 void CameraSessionNapi::UnregisterMacroStatusCallbackListener(
2912     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
2913 {
2914     CHECK_RETURN_ELOG(macroStatusCallback_ == nullptr, "macroStatusCallback is null");
2915     macroStatusCallback_->RemoveCallbackRef(eventName, callback);
2916 }
2917 
RegisterMoonCaptureBoostCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)2918 void CameraSessionNapi::RegisterMoonCaptureBoostCallbackListener(
2919     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
2920 {
2921     CHECK_RETURN_ELOG(!CameraNapiSecurity::CheckSystemApp(env), "SystemApi on moonCaptureBoostStatus is called!");
2922     if (moonCaptureBoostCallback_ == nullptr) {
2923         moonCaptureBoostCallback_ = std::make_shared<MoonCaptureBoostCallbackListener>(env);
2924         cameraSession_->SetMoonCaptureBoostStatusCallback(moonCaptureBoostCallback_);
2925     }
2926     moonCaptureBoostCallback_->SaveCallbackReference(eventName, callback, isOnce);
2927 }
2928 
UnregisterMoonCaptureBoostCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)2929 void CameraSessionNapi::UnregisterMoonCaptureBoostCallbackListener(
2930     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
2931 {
2932     CHECK_RETURN_ELOG(!CameraNapiSecurity::CheckSystemApp(env), "SystemApi off moonCaptureBoostStatus is called!");
2933     CHECK_RETURN_ELOG(moonCaptureBoostCallback_ == nullptr, "macroStatusCallback is null");
2934     moonCaptureBoostCallback_->RemoveCallbackRef(eventName, callback);
2935 }
2936 
RegisterFeatureDetectionStatusListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)2937 void CameraSessionNapi::RegisterFeatureDetectionStatusListener(
2938     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
2939 {
2940     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2941         "SystemApi on featureDetection is called");
2942 }
2943 
UnregisterFeatureDetectionStatusListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)2944 void CameraSessionNapi::UnregisterFeatureDetectionStatusListener(
2945     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
2946 {
2947     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2948         "SystemApi off featureDetection is called");
2949 }
2950 
RegisterSessionErrorCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)2951 void CameraSessionNapi::RegisterSessionErrorCallbackListener(
2952     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
2953 {
2954     if (sessionCallback_ == nullptr) {
2955         sessionCallback_ = std::make_shared<SessionCallbackListener>(env);
2956         cameraSession_->SetCallback(sessionCallback_);
2957     }
2958     sessionCallback_->SaveCallbackReference(eventName, callback, isOnce);
2959 }
2960 
UnregisterSessionErrorCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)2961 void CameraSessionNapi::UnregisterSessionErrorCallbackListener(
2962     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
2963 {
2964     if (sessionCallback_ == nullptr) {
2965         MEDIA_DEBUG_LOG("sessionCallback is null");
2966         return;
2967     }
2968     sessionCallback_->RemoveCallbackRef(eventName, callback);
2969 }
2970 
RegisterEffectSuggestionCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)2971 void CameraSessionNapi::RegisterEffectSuggestionCallbackListener(
2972     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
2973 {
2974     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2975         "SystemApi on effectSuggestionChange is called");
2976 }
2977 
UnregisterEffectSuggestionCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)2978 void CameraSessionNapi::UnregisterEffectSuggestionCallbackListener(
2979     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
2980 {
2981     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
2982         "SystemApi off effectSuggestionChange is called");
2983 }
2984 
RegisterAbilityChangeCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)2985 void CameraSessionNapi::RegisterAbilityChangeCallbackListener(
2986     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
2987 {
2988     if (abilityCallback_ == nullptr) {
2989         auto abilityCallback = std::make_shared<AbilityCallbackListener>(env);
2990         abilityCallback_ = abilityCallback;
2991         cameraSession_->SetAbilityCallback(abilityCallback);
2992     }
2993     abilityCallback_->SaveCallbackReference(eventName, callback, isOnce);
2994 }
2995 
UnregisterAbilityChangeCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)2996 void CameraSessionNapi::UnregisterAbilityChangeCallbackListener(
2997     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
2998 {
2999     if (abilityCallback_ == nullptr) {
3000         MEDIA_ERR_LOG("abilityCallback is null");
3001     } else {
3002         abilityCallback_->RemoveCallbackRef(eventName, callback);
3003     }
3004 }
3005 
RegisterSmoothZoomCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)3006 void CameraSessionNapi::RegisterSmoothZoomCallbackListener(
3007     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
3008 {
3009     if (smoothZoomCallback_ == nullptr) {
3010         smoothZoomCallback_ = std::make_shared<SmoothZoomCallbackListener>(env);
3011         cameraSession_->SetSmoothZoomCallback(smoothZoomCallback_);
3012     }
3013     smoothZoomCallback_->SaveCallbackReference(eventName, callback, isOnce);
3014 }
3015 
UnregisterSmoothZoomCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)3016 void CameraSessionNapi::UnregisterSmoothZoomCallbackListener(
3017     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
3018 {
3019     CHECK_RETURN_ELOG(smoothZoomCallback_ == nullptr, "smoothZoomCallback is null");
3020     smoothZoomCallback_->RemoveCallbackRef(eventName, callback);
3021 }
3022 
RegisterFocusTrackingInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)3023 void CameraSessionNapi::RegisterFocusTrackingInfoCallbackListener(const std::string& eventName,
3024     napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
3025 {
3026     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
3027         "SystemApi on focusTrackingInfoAvailable is called");
3028 }
3029 
UnregisterFocusTrackingInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)3030 void CameraSessionNapi::UnregisterFocusTrackingInfoCallbackListener(const std::string& eventName,
3031     napi_env env, napi_value callback, const std::vector<napi_value>& args)
3032 {
3033     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
3034         "SystemApi off focusTrackingInfoAvailable is called");
3035 }
3036 
3037 const CameraSessionNapi::EmitterFunctions CameraSessionNapi::fun_map_ = {
3038     { "exposureStateChange", {
3039         &CameraSessionNapi::RegisterExposureCallbackListener,
3040         &CameraSessionNapi::UnregisterExposureCallbackListener} },
3041     { "focusStateChange", {
3042         &CameraSessionNapi::RegisterFocusCallbackListener,
3043         &CameraSessionNapi::UnregisterFocusCallbackListener } },
3044     { "macroStatusChanged", {
3045         &CameraSessionNapi::RegisterMacroStatusCallbackListener,
3046         &CameraSessionNapi::UnregisterMacroStatusCallbackListener } },
3047     { "systemPressureLevelChange", {
3048         &CameraSessionNapi::RegisterPressureStatusCallbackListener,
3049         &CameraSessionNapi::UnregisterPressureStatusCallbackListener } },
3050     { "controlCenterEffectStatusChange", {
3051         &CameraSessionNapi::RegisterControlCenterEffectStatusCallbackListener,
3052         &CameraSessionNapi::UnregisterControlCenterEffectStatusCallbackListener } },
3053     { "moonCaptureBoostStatus", {
3054         &CameraSessionNapi::RegisterMoonCaptureBoostCallbackListener,
3055         &CameraSessionNapi::UnregisterMoonCaptureBoostCallbackListener } },
3056     { "featureDetection", {
3057         &CameraSessionNapi::RegisterFeatureDetectionStatusListener,
3058         &CameraSessionNapi::UnregisterFeatureDetectionStatusListener } },
3059     { "featureDetectionStatus", {
3060         &CameraSessionNapi::RegisterFeatureDetectionStatusListener,
3061         &CameraSessionNapi::UnregisterFeatureDetectionStatusListener } },
3062     { "error", {
3063         &CameraSessionNapi::RegisterSessionErrorCallbackListener,
3064         &CameraSessionNapi::UnregisterSessionErrorCallbackListener } },
3065     { "smoothZoomInfoAvailable", {
3066         &CameraSessionNapi::RegisterSmoothZoomCallbackListener,
3067         &CameraSessionNapi::UnregisterSmoothZoomCallbackListener } },
3068     { "slowMotionStatus", {
3069         &CameraSessionNapi::RegisterSlowMotionStateCb,
3070         &CameraSessionNapi::UnregisterSlowMotionStateCb } },
3071     { "exposureInfoChange", {
3072         &CameraSessionNapi::RegisterExposureInfoCallbackListener,
3073         &CameraSessionNapi::UnregisterExposureInfoCallbackListener} },
3074     { "isoInfoChange", {
3075         &CameraSessionNapi::RegisterIsoInfoCallbackListener,
3076         &CameraSessionNapi::UnregisterIsoInfoCallbackListener } },
3077     { "apertureInfoChange", {
3078         &CameraSessionNapi::RegisterApertureInfoCallbackListener,
3079         &CameraSessionNapi::UnregisterApertureInfoCallbackListener } },
3080     { "luminationInfoChange", {
3081         &CameraSessionNapi::RegisterLuminationInfoCallbackListener,
3082         &CameraSessionNapi::UnregisterLuminationInfoCallbackListener } },
3083     { "abilityChange", {
3084         &CameraSessionNapi::RegisterAbilityChangeCallbackListener,
3085         &CameraSessionNapi::UnregisterAbilityChangeCallbackListener } },
3086     { "effectSuggestionChange", {
3087         &CameraSessionNapi::RegisterEffectSuggestionCallbackListener,
3088         &CameraSessionNapi::UnregisterEffectSuggestionCallbackListener } },
3089     { "tryAEInfoChange", {
3090         &CameraSessionNapi::RegisterTryAEInfoCallbackListener,
3091         &CameraSessionNapi::UnregisterTryAEInfoCallbackListener } },
3092     { "lcdFlashStatus", {
3093         &CameraSessionNapi::RegisterLcdFlashStatusCallbackListener,
3094         &CameraSessionNapi::UnregisterLcdFlashStatusCallbackListener } },
3095     { "autoDeviceSwitchStatusChange", {
3096         &CameraSessionNapi::RegisterAutoDeviceSwitchCallbackListener,
3097         &CameraSessionNapi::UnregisterAutoDeviceSwitchCallbackListener } },
3098     { "focusTrackingInfoAvailable", {
3099         &CameraSessionNapi::RegisterFocusTrackingInfoCallbackListener,
3100         &CameraSessionNapi::UnregisterFocusTrackingInfoCallbackListener } },
3101     { "lightStatusChange", {
3102         &CameraSessionNapi::RegisterLightStatusCallbackListener,
3103         &CameraSessionNapi::UnregisterLightStatusCallbackListener } },
3104 };
3105 
GetEmitterFunctions()3106 const CameraSessionNapi::EmitterFunctions& CameraSessionNapi::GetEmitterFunctions()
3107 {
3108     return fun_map_;
3109 }
3110 
On(napi_env env,napi_callback_info info)3111 napi_value CameraSessionNapi::On(napi_env env, napi_callback_info info)
3112 {
3113     return ListenerTemplate<CameraSessionNapi>::On(env, info);
3114 }
3115 
Once(napi_env env,napi_callback_info info)3116 napi_value CameraSessionNapi::Once(napi_env env, napi_callback_info info)
3117 {
3118     return ListenerTemplate<CameraSessionNapi>::Once(env, info);
3119 }
3120 
Off(napi_env env,napi_callback_info info)3121 napi_value CameraSessionNapi::Off(napi_env env, napi_callback_info info)
3122 {
3123     return ListenerTemplate<CameraSessionNapi>::Off(env, info);
3124 }
3125 
RegisterLcdFlashStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)3126 void CameraSessionNapi::RegisterLcdFlashStatusCallbackListener(
3127     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
3128 {
3129     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
3130         "SystemApi on lcdFlashStatus is called");
3131 }
3132 
UnregisterLcdFlashStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)3133 void CameraSessionNapi::UnregisterLcdFlashStatusCallbackListener(
3134     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
3135 {
3136     CameraNapiUtils::ThrowError(env, CameraErrorCode::NO_SYSTEM_APP_PERMISSION,
3137         "SystemApi off lcdFlashStatus is called");
3138 }
3139 
IsAutoDeviceSwitchSupported(napi_env env,napi_callback_info info)3140 napi_value CameraSessionNapi::IsAutoDeviceSwitchSupported(napi_env env, napi_callback_info info)
3141 {
3142     MEDIA_INFO_LOG("IsAutoDeviceSwitchSupported is called");
3143     CameraSessionNapi* cameraSessionNapi = nullptr;
3144     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
3145     CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error"), nullptr,
3146         "CameraSessionNapi::IsAutoDeviceSwitchSupported parse parameter occur error");
3147     auto result = CameraNapiUtils::GetUndefinedValue(env);
3148     if (cameraSessionNapi->cameraSession_ != nullptr) {
3149         bool isSupported = cameraSessionNapi->cameraSession_->IsAutoDeviceSwitchSupported();
3150         napi_get_boolean(env, isSupported, &result);
3151     } else {
3152         MEDIA_ERR_LOG("CameraSessionNapi::IsAutoDeviceSwitchSupported get native object fail");
3153         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
3154         return nullptr;
3155     }
3156     return result;
3157 }
3158 
EnableAutoDeviceSwitch(napi_env env,napi_callback_info info)3159 napi_value CameraSessionNapi::EnableAutoDeviceSwitch(napi_env env, napi_callback_info info)
3160 {
3161     MEDIA_DEBUG_LOG("CameraSessionNapi::EnableAutoDeviceSwitch is called");
3162     bool isEnable;
3163     CameraSessionNapi* cameraSessionNapi = nullptr;
3164     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, isEnable);
3165     CHECK_RETURN_RET_ELOG(!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error"), nullptr,
3166         "CameraSessionNapi::EnableAutoDeviceSwitch parse parameter occur error");
3167 
3168     if (cameraSessionNapi->cameraSession_ != nullptr) {
3169         MEDIA_INFO_LOG("CameraSessionNapi::EnableAutoDeviceSwitch:%{public}d", isEnable);
3170         cameraSessionNapi->cameraSession_->LockForControl();
3171         int32_t retCode = cameraSessionNapi->cameraSession_->EnableAutoDeviceSwitch(isEnable);
3172         cameraSessionNapi->cameraSession_->UnlockForControl();
3173         CHECK_RETURN_RET_ELOG(!CameraNapiUtils::CheckError(env, retCode), nullptr,
3174             "CameraSessionNapi::EnableAutoSwitchDevice fail %{public}d", retCode);
3175     } else {
3176         MEDIA_ERR_LOG("CameraSessionNapi::EnableAutoDeviceSwitch get native object fail");
3177         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
3178         return nullptr;
3179     }
3180     return CameraNapiUtils::GetUndefinedValue(env);
3181 }
3182 
RegisterAutoDeviceSwitchCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)3183 void CameraSessionNapi::RegisterAutoDeviceSwitchCallbackListener(
3184     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
3185 {
3186     CHECK_RETURN_ELOG(cameraSession_ == nullptr, "cameraSession is null!");
3187     if (autoDeviceSwitchCallback_ == nullptr) {
3188         autoDeviceSwitchCallback_ = std::make_shared<AutoDeviceSwitchCallbackListener>(env);
3189         cameraSession_->SetAutoDeviceSwitchCallback(autoDeviceSwitchCallback_);
3190     }
3191     autoDeviceSwitchCallback_->SaveCallbackReference(eventName, callback, isOnce);
3192 }
3193 
UnregisterAutoDeviceSwitchCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)3194 void CameraSessionNapi::UnregisterAutoDeviceSwitchCallbackListener(
3195     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
3196 {
3197     CHECK_RETURN_ELOG(autoDeviceSwitchCallback_ == nullptr, "autoDeviceSwitchCallback is nullptr.");
3198     autoDeviceSwitchCallback_->RemoveCallbackRef(eventName, callback);
3199 }
3200 
RegisterPressureStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)3201 void CameraSessionNapi::RegisterPressureStatusCallbackListener(
3202     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
3203 {
3204     CameraNapiUtils::ThrowError(env, CameraErrorCode::OPERATION_NOT_ALLOWED,
3205         "this type callback can not be registered in current session!");
3206 }
3207 
UnregisterPressureStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)3208 void CameraSessionNapi::UnregisterPressureStatusCallbackListener(
3209     const std::string &eventName, napi_env env, napi_value callback, const std::vector<napi_value> &args)
3210 {
3211     CameraNapiUtils::ThrowError(env, CameraErrorCode::OPERATION_NOT_ALLOWED,
3212         "this type callback can not be registered in current session!");
3213 }
3214 
RegisterControlCenterEffectStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)3215 void CameraSessionNapi::RegisterControlCenterEffectStatusCallbackListener(
3216     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
3217 {
3218     CameraNapiUtils::ThrowError(env, CameraErrorCode::OPERATION_NOT_ALLOWED,
3219         "this type callback can not be registered in current session!");
3220 }
3221 
UnregisterControlCenterEffectStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)3222 void CameraSessionNapi::UnregisterControlCenterEffectStatusCallbackListener(
3223     const std::string &eventName, napi_env env, napi_value callback, const std::vector<napi_value> &args)
3224 {
3225     CameraNapiUtils::ThrowError(env, CameraErrorCode::OPERATION_NOT_ALLOWED,
3226         "this type callback can not be unregistered in current session!");
3227 }
3228 
OnMacroStatusCallbackAsync(MacroStatus status) const3229 void MacroStatusCallbackListener::OnMacroStatusCallbackAsync(MacroStatus status) const
3230 {
3231     MEDIA_DEBUG_LOG("OnMacroStatusCallbackAsync is called");
3232     auto callbackInfo = std::make_unique<MacroStatusCallbackInfo>(status, shared_from_this());
3233     MacroStatusCallbackInfo *event = callbackInfo.get();
3234     auto task = [event]() {
3235         auto callbackInfo = reinterpret_cast<MacroStatusCallbackInfo*>(event);
3236         if (callbackInfo) {
3237             auto listener = callbackInfo->listener_.lock();
3238             CHECK_EXECUTE(listener != nullptr, listener->OnMacroStatusCallback(callbackInfo->status_));
3239             delete callbackInfo;
3240         }
3241     };
3242     if (napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
3243         MEDIA_ERR_LOG("failed to execute work");
3244     } else {
3245         callbackInfo.release();
3246     }
3247 }
3248 
OnMacroStatusCallback(MacroStatus status) const3249 void MacroStatusCallbackListener::OnMacroStatusCallback(MacroStatus status) const
3250 {
3251     MEDIA_DEBUG_LOG("OnMacroStatusCallback is called");
3252     ExecuteCallbackScopeSafe("macroStatusChanged", [&]() {
3253         napi_value result;
3254         napi_value errCode = CameraNapiUtils::GetUndefinedValue(env_);
3255         napi_get_boolean(env_, status == MacroStatus::ACTIVE, &result);
3256         return ExecuteCallbackData(env_, errCode, result);
3257     });
3258 }
3259 
OnMacroStatusChanged(MacroStatus status)3260 void MacroStatusCallbackListener::OnMacroStatusChanged(MacroStatus status)
3261 {
3262     MEDIA_DEBUG_LOG("OnMacroStatusChanged is called, status: %{public}d", status);
3263     OnMacroStatusCallbackAsync(status);
3264 }
3265 
3266 } // namespace CameraStandard
3267 } // namespace OHOS