• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "session/camera_session_napi.h"
17 
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_napi_const.h"
27 #include "camera_napi_object_types.h"
28 #include "camera_napi_param_parser.h"
29 #include "camera_napi_security_utils.h"
30 #include "camera_napi_template_utils.h"
31 #include "camera_napi_utils.h"
32 #include "camera_output_capability.h"
33 #include "capture_scene_const.h"
34 #include "capture_session.h"
35 #include "js_native_api.h"
36 #include "js_native_api_types.h"
37 #include "listener_base.h"
38 #include "napi/native_api.h"
39 #include "napi/native_common.h"
40 #include "output/photo_output_napi.h"
41 
42 namespace OHOS {
43 namespace CameraStandard {
44 namespace {
AsyncCompleteCallback(napi_env env,napi_status status,void * data)45 void AsyncCompleteCallback(napi_env env, napi_status status, void* data)
46 {
47     auto context = static_cast<CameraSessionAsyncContext*>(data);
48     CHECK_ERROR_RETURN_LOG(context == nullptr, "CameraSessionNapi AsyncCompleteCallback context is null");
49     MEDIA_INFO_LOG("CameraSessionNapi AsyncCompleteCallback %{public}s, status = %{public}d", context->funcName.c_str(),
50         context->status);
51     std::unique_ptr<JSAsyncContextOutput> jsContext = std::make_unique<JSAsyncContextOutput>();
52     jsContext->status = context->status;
53     if (!context->status) {
54         CameraNapiUtils::CreateNapiErrorObject(env, context->errorCode, context->errorMsg.c_str(), jsContext);
55     } else {
56         napi_get_undefined(env, &jsContext->data);
57     }
58     if (!context->funcName.empty() && context->taskId > 0) {
59         // Finish async trace
60         CAMERA_FINISH_ASYNC_TRACE(context->funcName, context->taskId);
61         jsContext->funcName = context->funcName;
62     }
63     if (context->work != nullptr) {
64         CameraNapiUtils::InvokeJSAsyncMethod(env, context->deferred, context->callbackRef, context->work, *jsContext);
65     }
66     context->FreeHeldNapiValue(env);
67     delete context;
68 }
69 } // namespace
70 
71 using namespace std;
72 thread_local napi_ref CameraSessionNapi::sConstructor_ = nullptr;
73 thread_local sptr<CaptureSession> CameraSessionNapi::sCameraSession_ = nullptr;
74 thread_local uint32_t CameraSessionNapi::cameraSessionTaskId = CAMERA_SESSION_TASKID;
75 
76 const std::map<SceneMode, FunctionsType> CameraSessionNapi::modeToFunctionTypeMap_ = {
77     {SceneMode::CAPTURE, FunctionsType::PHOTO_FUNCTIONS},
78     {SceneMode::VIDEO, FunctionsType::VIDEO_FUNCTIONS},
79     {SceneMode::PORTRAIT, FunctionsType::PORTRAIT_PHOTO_FUNCTIONS}
80 };
81 
82 const std::map<SceneMode, FunctionsType> CameraSessionNapi::modeToConflictFunctionTypeMap_ = {
83     {SceneMode::CAPTURE, FunctionsType::PHOTO_CONFLICT_FUNCTIONS},
84     {SceneMode::VIDEO, FunctionsType::VIDEO_CONFLICT_FUNCTIONS},
85     {SceneMode::PORTRAIT, FunctionsType::PORTRAIT_PHOTO_CONFLICT_FUNCTIONS}
86 };
87 
88 const std::vector<napi_property_descriptor> CameraSessionNapi::camera_process_props = {
89     DECLARE_NAPI_FUNCTION("beginConfig", CameraSessionNapi::BeginConfig),
90     DECLARE_NAPI_FUNCTION("commitConfig", CameraSessionNapi::CommitConfig),
91 
92     DECLARE_NAPI_FUNCTION("canAddInput", CameraSessionNapi::CanAddInput),
93     DECLARE_NAPI_FUNCTION("addInput", CameraSessionNapi::AddInput),
94     DECLARE_NAPI_FUNCTION("removeInput", CameraSessionNapi::RemoveInput),
95 
96     DECLARE_NAPI_FUNCTION("canAddOutput", CameraSessionNapi::CanAddOutput),
97     DECLARE_NAPI_FUNCTION("addOutput", CameraSessionNapi::AddOutput),
98     DECLARE_NAPI_FUNCTION("removeOutput", CameraSessionNapi::RemoveOutput),
99 
100     DECLARE_NAPI_FUNCTION("start", CameraSessionNapi::Start),
101     DECLARE_NAPI_FUNCTION("stop", CameraSessionNapi::Stop),
102     DECLARE_NAPI_FUNCTION("release", CameraSessionNapi::Release),
103 
104     DECLARE_NAPI_FUNCTION("lockForControl", CameraSessionNapi::LockForControl),
105     DECLARE_NAPI_FUNCTION("unlockForControl", CameraSessionNapi::UnlockForControl),
106 
107     DECLARE_NAPI_FUNCTION("on", CameraSessionNapi::On),
108     DECLARE_NAPI_FUNCTION("once", CameraSessionNapi::Once),
109     DECLARE_NAPI_FUNCTION("off", CameraSessionNapi::Off),
110     DECLARE_NAPI_FUNCTION("setUsage", CameraSessionNapi::SetUsage)
111 };
112 
113 const std::vector<napi_property_descriptor> CameraSessionNapi::stabilization_props = {
114     DECLARE_NAPI_FUNCTION("isVideoStabilizationModeSupported", CameraSessionNapi::IsVideoStabilizationModeSupported),
115     DECLARE_NAPI_FUNCTION("getActiveVideoStabilizationMode", CameraSessionNapi::GetActiveVideoStabilizationMode),
116     DECLARE_NAPI_FUNCTION("setVideoStabilizationMode", CameraSessionNapi::SetVideoStabilizationMode)
117 };
118 
119 const std::vector<napi_property_descriptor> CameraSessionNapi::flash_props = {
120     DECLARE_NAPI_FUNCTION("hasFlash", CameraSessionNapi::HasFlash),
121     DECLARE_NAPI_FUNCTION("isFlashModeSupported", CameraSessionNapi::IsFlashModeSupported),
122     DECLARE_NAPI_FUNCTION("getFlashMode", CameraSessionNapi::GetFlashMode),
123     DECLARE_NAPI_FUNCTION("setFlashMode", CameraSessionNapi::SetFlashMode),
124     DECLARE_NAPI_FUNCTION("isLcdFlashSupported", CameraSessionNapi::IsLcdFlashSupported),
125     DECLARE_NAPI_FUNCTION("enableLcdFlash", CameraSessionNapi::EnableLcdFlash)
126 };
127 
128 const std::vector<napi_property_descriptor> CameraSessionNapi::auto_exposure_props = {
129     DECLARE_NAPI_FUNCTION("isExposureModeSupported", CameraSessionNapi::IsExposureModeSupported),
130     DECLARE_NAPI_FUNCTION("getExposureMode", CameraSessionNapi::GetExposureMode),
131     DECLARE_NAPI_FUNCTION("setExposureMode", CameraSessionNapi::SetExposureMode),
132     DECLARE_NAPI_FUNCTION("getExposureBiasRange", CameraSessionNapi::GetExposureBiasRange),
133     DECLARE_NAPI_FUNCTION("setExposureBias", CameraSessionNapi::SetExposureBias),
134     DECLARE_NAPI_FUNCTION("getExposureValue", CameraSessionNapi::GetExposureValue),
135     DECLARE_NAPI_FUNCTION("getMeteringPoint", CameraSessionNapi::GetMeteringPoint),
136     DECLARE_NAPI_FUNCTION("setMeteringPoint", CameraSessionNapi::SetMeteringPoint)
137 };
138 
139 const std::vector<napi_property_descriptor> CameraSessionNapi::focus_props = {
140     DECLARE_NAPI_FUNCTION("isFocusModeSupported", CameraSessionNapi::IsFocusModeSupported),
141     DECLARE_NAPI_FUNCTION("getFocusMode", CameraSessionNapi::GetFocusMode),
142     DECLARE_NAPI_FUNCTION("setFocusMode", CameraSessionNapi::SetFocusMode),
143     DECLARE_NAPI_FUNCTION("getFocusPoint", CameraSessionNapi::GetFocusPoint),
144     DECLARE_NAPI_FUNCTION("setFocusPoint", CameraSessionNapi::SetFocusPoint),
145     DECLARE_NAPI_FUNCTION("getFocalLength", CameraSessionNapi::GetFocalLength)
146 };
147 
148 const std::vector<napi_property_descriptor> CameraSessionNapi::manual_focus_props = {
149     DECLARE_NAPI_FUNCTION("getFocusDistance", CameraSessionNapi::GetFocusDistance),
150     DECLARE_NAPI_FUNCTION("setFocusDistance", CameraSessionNapi::SetFocusDistance),
151 };
152 
153 const std::vector<napi_property_descriptor> CameraSessionNapi::zoom_props = {
154     DECLARE_NAPI_FUNCTION("getZoomRatioRange", CameraSessionNapi::GetZoomRatioRange),
155     DECLARE_NAPI_FUNCTION("getZoomRatio", CameraSessionNapi::GetZoomRatio),
156     DECLARE_NAPI_FUNCTION("setZoomRatio", CameraSessionNapi::SetZoomRatio),
157     DECLARE_NAPI_FUNCTION("prepareZoom", PrepareZoom),
158     DECLARE_NAPI_FUNCTION("unprepareZoom", UnPrepareZoom),
159     DECLARE_NAPI_FUNCTION("setSmoothZoom", SetSmoothZoom),
160     DECLARE_NAPI_FUNCTION("getZoomPointInfos", CameraSessionNapi::GetZoomPointInfos)
161 };
162 
163 const std::vector<napi_property_descriptor> CameraSessionNapi::filter_props = {
164     DECLARE_NAPI_FUNCTION("getSupportedFilters", CameraSessionNapi::GetSupportedFilters),
165     DECLARE_NAPI_FUNCTION("getFilter", CameraSessionNapi::GetFilter),
166     DECLARE_NAPI_FUNCTION("setFilter", CameraSessionNapi::SetFilter)
167 };
168 
169 const std::vector<napi_property_descriptor> CameraSessionNapi::beauty_props = {
170     DECLARE_NAPI_FUNCTION("getSupportedBeautyTypes", CameraSessionNapi::GetSupportedBeautyTypes),
171     DECLARE_NAPI_FUNCTION("getSupportedBeautyRange", CameraSessionNapi::GetSupportedBeautyRange),
172     DECLARE_NAPI_FUNCTION("getBeauty", CameraSessionNapi::GetBeauty),
173     DECLARE_NAPI_FUNCTION("setBeauty", CameraSessionNapi::SetBeauty)
174 };
175 
176 const std::vector<napi_property_descriptor> CameraSessionNapi::color_effect_props = {
177     DECLARE_NAPI_FUNCTION("getSupportedColorEffects", CameraSessionNapi::GetSupportedColorEffects),
178     DECLARE_NAPI_FUNCTION("getColorEffect", CameraSessionNapi::GetColorEffect),
179     DECLARE_NAPI_FUNCTION("setColorEffect", CameraSessionNapi::SetColorEffect)
180 };
181 
182 const std::vector<napi_property_descriptor> CameraSessionNapi::macro_props = {
183     DECLARE_NAPI_FUNCTION("isMacroSupported", CameraSessionNapi::IsMacroSupported),
184     DECLARE_NAPI_FUNCTION("enableMacro", CameraSessionNapi::EnableMacro)
185 };
186 
187 const std::vector<napi_property_descriptor> CameraSessionNapi::depth_fusion_props = {
188     DECLARE_NAPI_FUNCTION("isDepthFusionSupported", CameraSessionNapi::IsDepthFusionSupported),
189     DECLARE_NAPI_FUNCTION("getDepthFusionThreshold", CameraSessionNapi::GetDepthFusionThreshold),
190     DECLARE_NAPI_FUNCTION("isDepthFusionEnabled", CameraSessionNapi::IsDepthFusionEnabled),
191     DECLARE_NAPI_FUNCTION("enableDepthFusion", CameraSessionNapi::EnableDepthFusion)
192 };
193 
194 const std::vector<napi_property_descriptor> CameraSessionNapi::moon_capture_boost_props = {
195     DECLARE_NAPI_FUNCTION("isMoonCaptureBoostSupported", CameraSessionNapi::IsMoonCaptureBoostSupported),
196     DECLARE_NAPI_FUNCTION("enableMoonCaptureBoost", CameraSessionNapi::EnableMoonCaptureBoost)
197 };
198 
199 const std::vector<napi_property_descriptor> CameraSessionNapi::features_props = {
200     DECLARE_NAPI_FUNCTION("isSceneFeatureSupported", CameraSessionNapi::IsFeatureSupported),
201     DECLARE_NAPI_FUNCTION("enableSceneFeature", CameraSessionNapi::EnableFeature)
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::preconfig_props = {
211     DECLARE_NAPI_FUNCTION("canPreconfig", CameraSessionNapi::CanPreconfig),
212     DECLARE_NAPI_FUNCTION("preconfig", CameraSessionNapi::Preconfig)
213 };
214 
215 const std::vector<napi_property_descriptor> CameraSessionNapi::camera_output_capability_props = {
216     DECLARE_NAPI_FUNCTION("getCameraOutputCapabilities", CameraSessionNapi::GetCameraOutputCapabilities)
217 };
218 
219 const std::vector<napi_property_descriptor> CameraSessionNapi::camera_ability_props = {
220     DECLARE_NAPI_FUNCTION("getSessionFunctions", CameraSessionNapi::GetSessionFunctions),
221     DECLARE_NAPI_FUNCTION("getSessionConflictFunctions", CameraSessionNapi::GetSessionConflictFunctions)
222 };
223 
224 const std::vector<napi_property_descriptor> CameraSessionNapi::effect_suggestion_props = {
225     DECLARE_NAPI_FUNCTION("isEffectSuggestionSupported", CameraSessionNapi::IsEffectSuggestionSupported),
226     DECLARE_NAPI_FUNCTION("enableEffectSuggestion", CameraSessionNapi::EnableEffectSuggestion),
227     DECLARE_NAPI_FUNCTION("getSupportedEffectSuggestionType", CameraSessionNapi::GetSupportedEffectSuggestionType),
228     DECLARE_NAPI_FUNCTION("getSupportedEffectSuggestionTypes", CameraSessionNapi::GetSupportedEffectSuggestionType),
229     DECLARE_NAPI_FUNCTION("setEffectSuggestionStatus", CameraSessionNapi::SetEffectSuggestionStatus),
230     DECLARE_NAPI_FUNCTION("updateEffectSuggestion", CameraSessionNapi::UpdateEffectSuggestion)
231 };
232 
233 const std::vector<napi_property_descriptor> CameraSessionNapi::auto_wb_props = {
234     DECLARE_NAPI_FUNCTION("getSupportedWhiteBalanceModes", CameraSessionNapi::GetSupportedWhiteBalanceModes),
235     DECLARE_NAPI_FUNCTION("isWhiteBalanceModeSupported", CameraSessionNapi::IsWhiteBalanceModeSupported),
236     DECLARE_NAPI_FUNCTION("getWhiteBalanceMode", CameraSessionNapi::GetWhiteBalanceMode),
237     DECLARE_NAPI_FUNCTION("setWhiteBalanceMode", CameraSessionNapi::SetWhiteBalanceMode),
238 };
239 
240 const std::vector<napi_property_descriptor> CameraSessionNapi::manual_wb_props = {
241     DECLARE_NAPI_FUNCTION("getWhiteBalanceRange", CameraSessionNapi::GetManualWhiteBalanceRange),
242     DECLARE_NAPI_FUNCTION("isManualWhiteBalanceSupported", CameraSessionNapi::IsManualWhiteBalanceSupported),
243     DECLARE_NAPI_FUNCTION("getWhiteBalance", CameraSessionNapi::GetManualWhiteBalance),
244     DECLARE_NAPI_FUNCTION("setWhiteBalance", CameraSessionNapi::SetManualWhiteBalance),
245 };
246 
247 const std::vector<napi_property_descriptor> CameraSessionNapi::aperture_props = {
248     DECLARE_NAPI_FUNCTION("getSupportedVirtualApertures", CameraSessionNapi::GetSupportedVirtualApertures),
249     DECLARE_NAPI_FUNCTION("getVirtualAperture", CameraSessionNapi::GetVirtualAperture),
250     DECLARE_NAPI_FUNCTION("setVirtualAperture", CameraSessionNapi::SetVirtualAperture),
251 
252     DECLARE_NAPI_FUNCTION("getSupportedPhysicalApertures", CameraSessionNapi::GetSupportedPhysicalApertures),
253     DECLARE_NAPI_FUNCTION("getPhysicalAperture", CameraSessionNapi::GetPhysicalAperture),
254     DECLARE_NAPI_FUNCTION("setPhysicalAperture", CameraSessionNapi::SetPhysicalAperture)
255 };
256 
257 const std::vector<napi_property_descriptor> CameraSessionNapi::auto_switch_props = {
258     DECLARE_NAPI_FUNCTION("isAutoDeviceSwitchSupported", CameraSessionNapi::IsAutoDeviceSwitchSupported),
259     DECLARE_NAPI_FUNCTION("enableAutoDeviceSwitch", CameraSessionNapi::EnableAutoDeviceSwitch)
260 };
261 
OnExposureStateCallbackAsync(ExposureState state) const262 void ExposureCallbackListener::OnExposureStateCallbackAsync(ExposureState state) const
263 {
264     MEDIA_DEBUG_LOG("OnExposureStateCallbackAsync is called");
265     uv_loop_s* loop = nullptr;
266     napi_get_uv_event_loop(env_, &loop);
267     if (!loop) {
268         MEDIA_ERR_LOG("failed to get event loop");
269         return;
270     }
271     uv_work_t* work = new(std::nothrow) uv_work_t;
272     if (!work) {
273         MEDIA_ERR_LOG("failed to allocate work");
274         return;
275     }
276     std::unique_ptr<ExposureCallbackInfo> callbackInfo = std::make_unique<ExposureCallbackInfo>(state, this);
277     work->data = callbackInfo.get();
278     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t* work) {}, [] (uv_work_t* work, int status) {
279         ExposureCallbackInfo* callbackInfo = reinterpret_cast<ExposureCallbackInfo *>(work->data);
280         if (callbackInfo) {
281             callbackInfo->listener_->OnExposureStateCallback(callbackInfo->state_);
282             delete callbackInfo;
283         }
284         delete work;
285     }, uv_qos_user_initiated);
286     if (ret) {
287         MEDIA_ERR_LOG("failed to execute work");
288         delete work;
289     } else {
290         callbackInfo.release();
291     }
292 }
293 
OnExposureStateCallback(ExposureState state) const294 void ExposureCallbackListener::OnExposureStateCallback(ExposureState state) const
295 {
296     MEDIA_DEBUG_LOG("OnExposureStateCallback is called");
297     napi_value result[ARGS_TWO] = {nullptr, nullptr};
298     napi_value retVal;
299 
300     napi_get_undefined(env_, &result[PARAM0]);
301     napi_create_int32(env_, state, &result[PARAM1]);
302     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
303     ExecuteCallback("exposureStateChange", callbackNapiPara);
304 }
305 
OnExposureState(const ExposureState state)306 void ExposureCallbackListener::OnExposureState(const ExposureState state)
307 {
308     MEDIA_DEBUG_LOG("OnExposureState is called, state: %{public}d", state);
309     OnExposureStateCallbackAsync(state);
310 }
311 
OnFocusStateCallbackAsync(FocusState state) const312 void FocusCallbackListener::OnFocusStateCallbackAsync(FocusState state) const
313 {
314     MEDIA_DEBUG_LOG("OnFocusStateCallbackAsync is called");
315     uv_loop_s* loop = nullptr;
316     napi_get_uv_event_loop(env_, &loop);
317     if (!loop) {
318         MEDIA_ERR_LOG("failed to get event loop");
319         return;
320     }
321     uv_work_t* work = new(std::nothrow) uv_work_t;
322     if (!work) {
323         MEDIA_ERR_LOG("failed to allocate work");
324         return;
325     }
326     std::unique_ptr<FocusCallbackInfo> callbackInfo = std::make_unique<FocusCallbackInfo>(state, this);
327     work->data = callbackInfo.get();
328     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t* work) {}, [] (uv_work_t* work, int status) {
329         FocusCallbackInfo* callbackInfo = reinterpret_cast<FocusCallbackInfo *>(work->data);
330         if (callbackInfo) {
331             callbackInfo->listener_->OnFocusStateCallback(callbackInfo->state_);
332             delete callbackInfo;
333         }
334         delete work;
335     }, uv_qos_user_initiated);
336     if (ret) {
337         MEDIA_ERR_LOG("failed to execute work");
338         delete work;
339     } else {
340         callbackInfo.release();
341     }
342 }
343 
OnFocusStateCallback(FocusState state) const344 void FocusCallbackListener::OnFocusStateCallback(FocusState state) const
345 {
346     MEDIA_DEBUG_LOG("OnFocusStateCallback is called");
347     napi_value result[ARGS_TWO] = {nullptr, nullptr};
348     napi_value retVal;
349     napi_get_undefined(env_, &result[PARAM0]);
350     napi_create_int32(env_, state, &result[PARAM1]);
351     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
352     ExecuteCallback("focusStateChange", callbackNapiPara);
353 }
354 
OnFocusState(FocusState state)355 void FocusCallbackListener::OnFocusState(FocusState state)
356 {
357     MEDIA_DEBUG_LOG("OnFocusState is called, state: %{public}d", state);
358     OnFocusStateCallbackAsync(state);
359 }
360 
OnMacroStatusCallbackAsync(MacroStatus status) const361 void MacroStatusCallbackListener::OnMacroStatusCallbackAsync(MacroStatus status) const
362 {
363     MEDIA_DEBUG_LOG("OnMacroStatusCallbackAsync is called");
364     uv_loop_s* loop = nullptr;
365     napi_get_uv_event_loop(env_, &loop);
366     if (!loop) {
367         MEDIA_ERR_LOG("failed to get event loop");
368         return;
369     }
370     uv_work_t* work = new (std::nothrow) uv_work_t;
371     if (!work) {
372         MEDIA_ERR_LOG("failed to allocate work");
373         return;
374     }
375     auto callbackInfo = std::make_unique<MacroStatusCallbackInfo>(status, this);
376     work->data = callbackInfo.get();
377     int ret = uv_queue_work_with_qos(
378         loop, work, [](uv_work_t* work) {},
379         [](uv_work_t* work, int status) {
380             auto callbackInfo = reinterpret_cast<MacroStatusCallbackInfo*>(work->data);
381             if (callbackInfo) {
382                 callbackInfo->listener_->OnMacroStatusCallback(callbackInfo->status_);
383                 delete callbackInfo;
384             }
385             delete work;
386         },
387         uv_qos_user_initiated);
388     if (ret) {
389         MEDIA_ERR_LOG("failed to execute work");
390         delete work;
391     } else {
392         callbackInfo.release();
393     }
394 }
395 
OnMacroStatusCallback(MacroStatus status) const396 void MacroStatusCallbackListener::OnMacroStatusCallback(MacroStatus status) const
397 {
398     MEDIA_DEBUG_LOG("OnMacroStatusCallback is called");
399     napi_value result[ARGS_TWO] = { nullptr, nullptr };
400     napi_value retVal;
401     napi_get_undefined(env_, &result[PARAM0]);
402     napi_get_boolean(env_, status == MacroStatus::ACTIVE, &result[PARAM1]);
403     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
404     ExecuteCallback("macroStatusChanged", callbackNapiPara);
405 }
406 
OnMacroStatusChanged(MacroStatus status)407 void MacroStatusCallbackListener::OnMacroStatusChanged(MacroStatus status)
408 {
409     MEDIA_DEBUG_LOG("OnMacroStatusChanged is called, status: %{public}d", status);
410     OnMacroStatusCallbackAsync(status);
411 }
412 
OnMoonCaptureBoostStatusCallbackAsync(MoonCaptureBoostStatus status) const413 void MoonCaptureBoostCallbackListener::OnMoonCaptureBoostStatusCallbackAsync(MoonCaptureBoostStatus status) const
414 {
415     MEDIA_DEBUG_LOG("OnMoonCaptureBoostStatusCallbackAsync is called");
416     uv_loop_s* loop = nullptr;
417     napi_get_uv_event_loop(env_, &loop);
418     if (!loop) {
419         MEDIA_ERR_LOG("failed to get event loop");
420         return;
421     }
422     uv_work_t* work = new (std::nothrow) uv_work_t;
423     if (!work) {
424         MEDIA_ERR_LOG("failed to allocate work");
425         return;
426     }
427     auto callbackInfo = std::make_unique<MoonCaptureBoostStatusCallbackInfo>(status, this);
428     work->data = callbackInfo.get();
429     int ret = uv_queue_work_with_qos(
430         loop, work, [](uv_work_t* work) {},
431         [](uv_work_t* work, int status) {
432             auto callbackInfo = reinterpret_cast<MoonCaptureBoostStatusCallbackInfo*>(work->data);
433             if (callbackInfo) {
434                 callbackInfo->listener_->OnMoonCaptureBoostStatusCallback(callbackInfo->status_);
435                 delete callbackInfo;
436             }
437             delete work;
438         },
439         uv_qos_user_initiated);
440     if (ret) {
441         MEDIA_ERR_LOG("failed to execute work");
442         delete work;
443     } else {
444         callbackInfo.release();
445     }
446 }
447 
OnMoonCaptureBoostStatusCallback(MoonCaptureBoostStatus status) const448 void MoonCaptureBoostCallbackListener::OnMoonCaptureBoostStatusCallback(MoonCaptureBoostStatus status) const
449 {
450     MEDIA_DEBUG_LOG("OnMoonCaptureBoostStatusCallback is called");
451     napi_value result[ARGS_TWO] = { nullptr, nullptr };
452     napi_value retVal;
453     napi_get_undefined(env_, &result[PARAM0]);
454     napi_get_boolean(env_, status == MoonCaptureBoostStatus::ACTIVE, &result[PARAM1]);
455     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
456     ExecuteCallback("moonCaptureBoostStatus", callbackNapiPara);
457 }
458 
OnMoonCaptureBoostStatusChanged(MoonCaptureBoostStatus status)459 void MoonCaptureBoostCallbackListener::OnMoonCaptureBoostStatusChanged(MoonCaptureBoostStatus status)
460 {
461     MEDIA_DEBUG_LOG("OnMoonCaptureBoostStatusChanged is called, status: %{public}d", status);
462     OnMoonCaptureBoostStatusCallbackAsync(status);
463 }
464 
OnFeatureDetectionStatusChangedCallbackAsync(SceneFeature feature,FeatureDetectionStatus status) const465 void FeatureDetectionStatusCallbackListener::OnFeatureDetectionStatusChangedCallbackAsync(
466     SceneFeature feature, FeatureDetectionStatus status) const
467 {
468     MEDIA_DEBUG_LOG("OnFeatureDetectionStatusChangedCallbackAsync is called");
469     uv_loop_s* loop = nullptr;
470     napi_get_uv_event_loop(env_, &loop);
471     if (!loop) {
472         MEDIA_ERR_LOG("failed to get event loop");
473         return;
474     }
475     uv_work_t* work = new (std::nothrow) uv_work_t;
476     if (!work) {
477         MEDIA_ERR_LOG("failed to allocate work");
478         return;
479     }
480     auto callbackInfo = std::make_unique<FeatureDetectionStatusCallbackInfo>(feature, status, this);
481     work->data = callbackInfo.get();
482     int ret = uv_queue_work_with_qos(
483         loop, work, [](uv_work_t* work) {},
484         [](uv_work_t* work, int status) {
485             auto callbackInfo = reinterpret_cast<FeatureDetectionStatusCallbackInfo*>(work->data);
486             if (callbackInfo) {
487                 callbackInfo->listener_->OnFeatureDetectionStatusChangedCallback(
488                     callbackInfo->feature_, callbackInfo->status_);
489                 delete callbackInfo;
490             }
491             delete work;
492         },
493         uv_qos_user_initiated);
494     if (ret) {
495         MEDIA_ERR_LOG("failed to execute work");
496         delete work;
497     } else {
498         callbackInfo.release();
499     }
500 }
501 
OnFeatureDetectionStatusChangedCallback(SceneFeature feature,FeatureDetectionStatus status) const502 void FeatureDetectionStatusCallbackListener::OnFeatureDetectionStatusChangedCallback(
503     SceneFeature feature, FeatureDetectionStatus status) const
504 {
505     MEDIA_DEBUG_LOG("OnFeatureDetectionStatusChangedCallback is called");
506     std::string eventName = "featureDetection" + std::to_string(static_cast<int32_t>(feature));
507     std::string eventNameOld = "featureDetectionStatus" + std::to_string(static_cast<int32_t>(feature));
508 
509     napi_value result[ARGS_TWO] = { nullptr, nullptr };
510     napi_value retVal;
511     napi_get_undefined(env_, &result[PARAM0]);
512     napi_create_object(env_, &result[PARAM1]);
513 
514     napi_value featureNapiValue;
515     napi_create_int32(env_, feature, &featureNapiValue);
516     napi_set_named_property(env_, result[PARAM1], "featureType", featureNapiValue);
517 
518     napi_value statusValue;
519     napi_get_boolean(env_, status == FeatureDetectionStatus::ACTIVE, &statusValue);
520     napi_set_named_property(env_, result[PARAM1], "detected", statusValue);
521 
522     if (feature == SceneFeature::FEATURE_TRIPOD_DETECTION) {
523         napi_value tripodStatusValue;
524         auto fwkTripodStatus = GetFeatureStatus();
525         napi_create_int32(env_, fwkTripodStatus, &tripodStatusValue);
526         napi_set_named_property(env_, result[PARAM1], "tripodStatus", tripodStatusValue);
527     }
528     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
529     ExecuteCallback(eventName, callbackNapiPara);
530     ExecuteCallback(eventNameOld, callbackNapiPara);
531 }
532 
OnFeatureDetectionStatusChanged(SceneFeature feature,FeatureDetectionStatus status)533 void FeatureDetectionStatusCallbackListener::OnFeatureDetectionStatusChanged(
534     SceneFeature feature, FeatureDetectionStatus status)
535 {
536     MEDIA_DEBUG_LOG(
537         "OnFeatureDetectionStatusChanged is called,feature:%{public}d, status: %{public}d", feature, status);
538     OnFeatureDetectionStatusChangedCallbackAsync(feature, status);
539 }
540 
IsFeatureSubscribed(SceneFeature feature)541 bool FeatureDetectionStatusCallbackListener::IsFeatureSubscribed(SceneFeature feature)
542 {
543     std::string eventName = "featureDetection" + std::to_string(static_cast<int32_t>(feature));
544     std::string eventNameOld = "featureDetectionStatus" + std::to_string(static_cast<int32_t>(feature));
545 
546     return !IsEmpty(eventName) || !IsEmpty(eventNameOld);
547 }
548 
OnErrorCallbackAsync(int32_t errorCode) const549 void SessionCallbackListener::OnErrorCallbackAsync(int32_t errorCode) const
550 {
551     MEDIA_DEBUG_LOG("OnErrorCallbackAsync is called");
552     uv_loop_s* loop = nullptr;
553     napi_get_uv_event_loop(env_, &loop);
554     if (!loop) {
555         MEDIA_ERR_LOG("failed to get event loop");
556         return;
557     }
558     uv_work_t* work = new(std::nothrow) uv_work_t;
559     if (!work) {
560         MEDIA_ERR_LOG("failed to allocate work");
561         return;
562     }
563     std::unique_ptr<SessionCallbackInfo> callbackInfo = std::make_unique<SessionCallbackInfo>(errorCode, this);
564     work->data = callbackInfo.get();
565     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t* work) {}, [] (uv_work_t* work, int status) {
566         SessionCallbackInfo* callbackInfo = reinterpret_cast<SessionCallbackInfo *>(work->data);
567         if (callbackInfo) {
568             callbackInfo->listener_->OnErrorCallback(callbackInfo->errorCode_);
569             delete callbackInfo;
570         }
571         delete work;
572     }, uv_qos_user_initiated);
573     if (ret) {
574         MEDIA_ERR_LOG("failed to execute work");
575         delete work;
576     } else {
577         callbackInfo.release();
578     }
579 }
580 
OnErrorCallback(int32_t errorCode) const581 void SessionCallbackListener::OnErrorCallback(int32_t errorCode) const
582 {
583     MEDIA_DEBUG_LOG("OnErrorCallback is called");
584     napi_value result[ARGS_ONE] = {nullptr};
585     napi_value retVal;
586     napi_value propValue;
587 
588     napi_create_object(env_, &result[PARAM0]);
589     napi_create_int32(env_, errorCode, &propValue);
590     napi_set_named_property(env_, result[PARAM0], "code", propValue);
591     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_ONE, .argv = result, .result = &retVal };
592     ExecuteCallback("error", callbackNapiPara);
593 }
594 
OnError(int32_t errorCode)595 void SessionCallbackListener::OnError(int32_t errorCode)
596 {
597     MEDIA_DEBUG_LOG("OnError is called, errorCode: %{public}d", errorCode);
598     OnErrorCallbackAsync(errorCode);
599 }
600 
OnSmoothZoomCallbackAsync(int32_t duration) const601 void SmoothZoomCallbackListener::OnSmoothZoomCallbackAsync(int32_t duration) const
602 {
603     MEDIA_DEBUG_LOG("OnSmoothZoomCallbackAsync is called");
604     uv_loop_s* loop = nullptr;
605     napi_get_uv_event_loop(env_, &loop);
606     if (!loop) {
607         MEDIA_ERR_LOG("failed to get event loop");
608         return;
609     }
610     uv_work_t* work = new(std::nothrow) uv_work_t;
611     if (!work) {
612         MEDIA_ERR_LOG("failed to allocate work");
613         return;
614     }
615     std::unique_ptr<SmoothZoomCallbackInfo> callbackInfo = std::make_unique<SmoothZoomCallbackInfo>(duration, this);
616     work->data = callbackInfo.get();
617     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t* work) {}, [] (uv_work_t* work, int status) {
618         SmoothZoomCallbackInfo* callbackInfo = reinterpret_cast<SmoothZoomCallbackInfo *>(work->data);
619         if (callbackInfo) {
620             callbackInfo->listener_->OnSmoothZoomCallback(callbackInfo->duration_);
621             delete callbackInfo;
622         }
623         delete work;
624     }, uv_qos_user_initiated);
625     if (ret) {
626         MEDIA_ERR_LOG("failed to execute work");
627         delete work;
628     } else {
629         callbackInfo.release();
630     }
631 }
632 
OnSmoothZoomCallback(int32_t duration) const633 void SmoothZoomCallbackListener::OnSmoothZoomCallback(int32_t duration) const
634 {
635     MEDIA_DEBUG_LOG("OnSmoothZoomCallback is called");
636     napi_value result[ARGS_TWO];
637     napi_value retVal;
638     napi_value propValue;
639 
640     napi_get_undefined(env_, &result[PARAM0]);
641     napi_create_object(env_, &result[PARAM1]);
642     napi_create_int32(env_, duration, &propValue);
643     napi_set_named_property(env_, result[PARAM1], "duration", propValue);
644 
645     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
646     ExecuteCallback("smoothZoomInfoAvailable", callbackNapiPara);
647 }
648 
OnSmoothZoom(int32_t duration)649 void SmoothZoomCallbackListener::OnSmoothZoom(int32_t duration)
650 {
651     MEDIA_DEBUG_LOG("OnSmoothZoom is called, duration: %{public}d", duration);
652     OnSmoothZoomCallbackAsync(duration);
653 }
654 
OnAbilityChangeCallbackAsync() const655 void AbilityCallbackListener::OnAbilityChangeCallbackAsync() const
656 {
657     MEDIA_DEBUG_LOG("OnAbilityChangeCallbackAsync is called");
658     uv_loop_s* loop = nullptr;
659     napi_get_uv_event_loop(env_, &loop);
660     if (!loop) {
661         MEDIA_ERR_LOG("failed to get event loop");
662         return;
663     }
664     uv_work_t* work = new(std::nothrow) uv_work_t;
665     if (!work) {
666         MEDIA_ERR_LOG("failed to allocate work");
667         return;
668     }
669     std::unique_ptr<AbilityCallbackInfo> callbackInfo = std::make_unique<AbilityCallbackInfo>(this);
670     work->data = callbackInfo.get();
671     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t* work) {}, [] (uv_work_t* work, int status) {
672         AbilityCallbackInfo* callbackInfo = reinterpret_cast<AbilityCallbackInfo *>(work->data);
673         if (callbackInfo) {
674             callbackInfo->listener_->OnAbilityChangeCallback();
675             delete callbackInfo;
676         }
677         delete work;
678     }, uv_qos_user_initiated);
679     if (ret) {
680         MEDIA_ERR_LOG("failed to execute work");
681         delete work;
682     } else {
683         callbackInfo.release();
684     }
685 }
686 
OnAbilityChangeCallback() const687 void AbilityCallbackListener::OnAbilityChangeCallback() const
688 {
689     MEDIA_DEBUG_LOG("OnAbilityChangeCallback is called");
690     napi_value result[ARGS_TWO];
691     napi_value retVal;
692     napi_get_undefined(env_, &result[PARAM0]);
693     napi_get_undefined(env_, &result[PARAM1]);
694 
695     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
696     ExecuteCallback("abilityChange", callbackNapiPara);
697 }
698 
OnAbilityChange()699 void AbilityCallbackListener::OnAbilityChange()
700 {
701     MEDIA_DEBUG_LOG("OnAbilityChange is called");
702     OnAbilityChangeCallbackAsync();
703 }
704 
OnEffectSuggestionCallbackAsync(EffectSuggestionType effectSuggestionType) const705 void EffectSuggestionCallbackListener::OnEffectSuggestionCallbackAsync(EffectSuggestionType effectSuggestionType) const
706 {
707     MEDIA_DEBUG_LOG("OnEffectSuggestionCallbackAsync is called");
708     uv_loop_s* loop = nullptr;
709     napi_get_uv_event_loop(env_, &loop);
710     if (!loop) {
711         MEDIA_ERR_LOG("failed to get event loop");
712         return;
713     }
714     uv_work_t* work = new(std::nothrow) uv_work_t;
715     if (!work) {
716         MEDIA_ERR_LOG("failed to allocate work");
717         return;
718     }
719     std::unique_ptr<EffectSuggestionCallbackInfo> callbackInfo =
720         std::make_unique<EffectSuggestionCallbackInfo>(effectSuggestionType, this);
721     work->data = callbackInfo.get();
722     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t* work) {}, [] (uv_work_t* work, int status) {
723         EffectSuggestionCallbackInfo* callbackInfo = reinterpret_cast<EffectSuggestionCallbackInfo *>(work->data);
724         if (callbackInfo) {
725             callbackInfo->listener_->OnEffectSuggestionCallback(callbackInfo->effectSuggestionType_);
726             delete callbackInfo;
727         }
728         delete work;
729     }, uv_qos_user_initiated);
730     if (ret) {
731         MEDIA_ERR_LOG("failed to execute work");
732         delete work;
733     } else {
734         callbackInfo.release();
735     }
736 }
737 
OnEffectSuggestionCallback(EffectSuggestionType effectSuggestionType) const738 void EffectSuggestionCallbackListener::OnEffectSuggestionCallback(EffectSuggestionType effectSuggestionType) const
739 {
740     MEDIA_DEBUG_LOG("OnEffectSuggestionCallback is called");
741     napi_value result[ARGS_TWO] = {nullptr, nullptr};
742     napi_value retVal;
743     napi_get_undefined(env_, &result[PARAM0]);
744     napi_create_int32(env_, effectSuggestionType, &result[PARAM1]);
745     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
746     ExecuteCallback("effectSuggestionChange", callbackNapiPara);
747 }
748 
OnEffectSuggestionChange(EffectSuggestionType effectSuggestionType)749 void EffectSuggestionCallbackListener::OnEffectSuggestionChange(EffectSuggestionType effectSuggestionType)
750 {
751     MEDIA_DEBUG_LOG("OnEffectSuggestionChange is called, effectSuggestionType: %{public}d", effectSuggestionType);
752     OnEffectSuggestionCallbackAsync(effectSuggestionType);
753 }
754 
OnLcdFlashStatusCallbackAsync(LcdFlashStatusInfo lcdFlashStatusInfo) const755 void LcdFlashStatusCallbackListener::OnLcdFlashStatusCallbackAsync(LcdFlashStatusInfo lcdFlashStatusInfo) const
756 {
757     MEDIA_DEBUG_LOG("OnLcdFlashStatusCallbackAsync is called");
758     uv_loop_s* loop = nullptr;
759     napi_get_uv_event_loop(env_, &loop);
760     if (!loop) {
761         MEDIA_ERR_LOG("failed to get event loop");
762         return;
763     }
764     uv_work_t* work = new (std::nothrow) uv_work_t;
765     if (!work) {
766         MEDIA_ERR_LOG("failed to allocate work");
767         return;
768     }
769     auto callbackInfo = std::make_unique<LcdFlashStatusStatusCallbackInfo>(lcdFlashStatusInfo, this);
770     work->data = callbackInfo.get();
771     int ret = uv_queue_work_with_qos(
772         loop, work, [](uv_work_t* work) {},
773         [](uv_work_t* work, int status) {
774             auto callbackInfo = reinterpret_cast<LcdFlashStatusStatusCallbackInfo*>(work->data);
775             if (callbackInfo) {
776                 callbackInfo->listener_->OnLcdFlashStatusCallback(callbackInfo->lcdFlashStatusInfo_);
777                 delete callbackInfo;
778             }
779             delete work;
780         },
781         uv_qos_user_initiated);
782     if (ret) {
783         MEDIA_ERR_LOG("failed to execute work");
784         delete work;
785     } else {
786         callbackInfo.release();
787     }
788 }
789 
OnLcdFlashStatusCallback(LcdFlashStatusInfo lcdFlashStatusInfo) const790 void LcdFlashStatusCallbackListener::OnLcdFlashStatusCallback(LcdFlashStatusInfo lcdFlashStatusInfo) const
791 {
792     MEDIA_DEBUG_LOG("OnLcdFlashStatusCallback is called");
793     napi_value result[ARGS_TWO] = { nullptr, nullptr };
794     napi_get_undefined(env_, &result[PARAM0]);
795     napi_value retVal;
796     napi_value propValue;
797     napi_create_object(env_, &result[PARAM1]);
798     napi_get_boolean(env_, lcdFlashStatusInfo.isLcdFlashNeeded, &propValue);
799     napi_set_named_property(env_, result[PARAM1], "isLcdFlashNeeded", propValue);
800     napi_create_int32(env_, lcdFlashStatusInfo.lcdCompensation, &propValue);
801     napi_set_named_property(env_, result[PARAM1], "lcdCompensation", propValue);
802     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
803     ExecuteCallback("lcdFlashStatus", callbackNapiPara);
804 }
805 
OnLcdFlashStatusChanged(LcdFlashStatusInfo lcdFlashStatusInfo)806 void LcdFlashStatusCallbackListener::OnLcdFlashStatusChanged(LcdFlashStatusInfo lcdFlashStatusInfo)
807 {
808     MEDIA_DEBUG_LOG("OnLcdFlashStatusChanged is called, isLcdFlashNeeded: %{public}d, lcdCompensation: %{public}d",
809         lcdFlashStatusInfo.isLcdFlashNeeded, lcdFlashStatusInfo.lcdCompensation);
810     OnLcdFlashStatusCallbackAsync(lcdFlashStatusInfo);
811 }
812 
OnAutoDeviceSwitchCallbackAsync(bool isDeviceSwitched,bool isDeviceCapabilityChanged) const813 void AutoDeviceSwitchCallbackListener::OnAutoDeviceSwitchCallbackAsync(
814     bool isDeviceSwitched, bool isDeviceCapabilityChanged) const
815 {
816     MEDIA_DEBUG_LOG("OnAutoDeviceSwitchCallbackAsync is called");
817     uv_loop_s* loop = nullptr;
818     napi_get_uv_event_loop(env_, &loop);
819     if (!loop) {
820         MEDIA_ERR_LOG("failed to get event loop");
821         return;
822     }
823     uv_work_t* work = new (std::nothrow) uv_work_t;
824     if (!work) {
825         MEDIA_ERR_LOG("failed to allocate work");
826         return;
827     }
828     auto callbackInfo = std::make_unique<AutoDeviceSwitchCallbackListenerInfo>(
829         isDeviceSwitched, isDeviceCapabilityChanged, this);
830     work->data = callbackInfo.get();
831     int ret = uv_queue_work_with_qos(
832         loop, work, [](uv_work_t* work) {},
833         [](uv_work_t* work, int status) {
834             auto callbackInfo = reinterpret_cast<AutoDeviceSwitchCallbackListenerInfo*>(work->data);
835             if (callbackInfo) {
836                 callbackInfo->listener_->OnAutoDeviceSwitchCallback(
837                     callbackInfo->isDeviceSwitched_, callbackInfo->isDeviceCapabilityChanged_);
838                 delete callbackInfo;
839             }
840             delete work;
841         },
842         uv_qos_user_initiated);
843     if (ret) {
844         MEDIA_ERR_LOG("failed to execute work");
845         delete work;
846     } else {
847         callbackInfo.release();
848     }
849 }
850 
OnAutoDeviceSwitchCallback(bool isDeviceSwitched,bool isDeviceCapabilityChanged) const851 void AutoDeviceSwitchCallbackListener::OnAutoDeviceSwitchCallback(
852     bool isDeviceSwitched, bool isDeviceCapabilityChanged) const
853 {
854     MEDIA_INFO_LOG("OnAutoDeviceSwitchCallback is called");
855     napi_value result[ARGS_TWO] = { nullptr, nullptr };
856     napi_get_undefined(env_, &result[PARAM0]);
857     napi_value retVal;
858     napi_value propValue;
859     napi_create_object(env_, &result[PARAM1]);
860     napi_get_boolean(env_, isDeviceSwitched, &propValue);
861     napi_set_named_property(env_, result[PARAM1], "isDeviceSwitched", propValue);
862     napi_get_boolean(env_, isDeviceCapabilityChanged, &propValue);
863     napi_set_named_property(env_, result[PARAM1], "isDeviceCapabilityChanged", propValue);
864     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
865     ExecuteCallback("autoDeviceSwitchStatusChange", callbackNapiPara);
866 }
867 
OnAutoDeviceSwitchStatusChange(bool isDeviceSwitched,bool isDeviceCapabilityChanged) const868 void AutoDeviceSwitchCallbackListener::OnAutoDeviceSwitchStatusChange(
869     bool isDeviceSwitched, bool isDeviceCapabilityChanged) const
870 {
871     MEDIA_INFO_LOG("isDeviceSwitched: %{public}d, isDeviceCapabilityChanged: %{public}d",
872         isDeviceSwitched, isDeviceCapabilityChanged);
873     OnAutoDeviceSwitchCallbackAsync(isDeviceSwitched, isDeviceCapabilityChanged);
874 }
875 
CameraSessionNapi()876 CameraSessionNapi::CameraSessionNapi() : env_(nullptr) {}
877 
~CameraSessionNapi()878 CameraSessionNapi::~CameraSessionNapi()
879 {
880     MEDIA_DEBUG_LOG("~CameraSessionNapi is called");
881 }
882 
CameraSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)883 void CameraSessionNapi::CameraSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
884 {
885     MEDIA_DEBUG_LOG("CameraSessionNapiDestructor is called");
886 }
887 
Init(napi_env env,napi_value exports)888 napi_value CameraSessionNapi::Init(napi_env env, napi_value exports)
889 {
890     MEDIA_DEBUG_LOG("Init is called");
891     napi_status status;
892     napi_value ctorObj;
893     int32_t refCount = 1;
894     std::vector<std::vector<napi_property_descriptor>> descriptors = { camera_process_props, stabilization_props,
895         flash_props, auto_exposure_props, focus_props, zoom_props, filter_props, beauty_props, color_effect_props,
896         macro_props, depth_fusion_props, moon_capture_boost_props, features_props, color_management_props,
897         manual_focus_props, preconfig_props, camera_output_capability_props };
898     std::vector<napi_property_descriptor> camera_session_props = CameraNapiUtils::GetPropertyDescriptor(descriptors);
899     status = napi_define_class(env, CAMERA_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
900                                CameraSessionNapiConstructor, nullptr,
901                                camera_session_props.size(),
902                                camera_session_props.data(), &ctorObj);
903     if (status == napi_ok) {
904         status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
905         if (status == napi_ok) {
906             status = napi_set_named_property(env, exports, CAMERA_SESSION_NAPI_CLASS_NAME, ctorObj);
907             if (status == napi_ok) {
908                 return exports;
909             }
910         }
911     }
912     MEDIA_ERR_LOG("Init call Failed!");
913     return nullptr;
914 }
915 
916 // Constructor callback
CameraSessionNapiConstructor(napi_env env,napi_callback_info info)917 napi_value CameraSessionNapi::CameraSessionNapiConstructor(napi_env env, napi_callback_info info)
918 {
919     MEDIA_DEBUG_LOG("CameraSessionNapiConstructor is called");
920     napi_status status;
921     napi_value result = nullptr;
922     napi_value thisVar = nullptr;
923 
924     napi_get_undefined(env, &result);
925     CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
926 
927     if (status == napi_ok && thisVar != nullptr) {
928         std::unique_ptr<CameraSessionNapi> obj = std::make_unique<CameraSessionNapi>();
929         if (obj != nullptr) {
930             obj->env_ = env;
931             if (sCameraSession_ == nullptr) {
932                 MEDIA_ERR_LOG("sCameraSession_ is null");
933                 return result;
934             }
935             obj->cameraSession_ = sCameraSession_;
936             status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
937                                CameraSessionNapi::CameraSessionNapiDestructor, nullptr, nullptr);
938             if (status == napi_ok) {
939                 obj.release();
940                 return thisVar;
941             } else {
942                 MEDIA_ERR_LOG("CameraSessionNapi Failure wrapping js to native napi");
943             }
944         }
945     }
946     MEDIA_ERR_LOG("CameraSessionNapiConstructor call Failed!");
947     return result;
948 }
949 
QueryAndGetInputProperty(napi_env env,napi_value arg,const string & propertyName,napi_value & property)950 int32_t QueryAndGetInputProperty(napi_env env, napi_value arg, const string &propertyName, napi_value &property)
951 {
952     MEDIA_DEBUG_LOG("QueryAndGetInputProperty is called");
953     bool present = false;
954     int32_t retval = 0;
955     if ((napi_has_named_property(env, arg, propertyName.c_str(), &present) != napi_ok)
956         || (!present) || (napi_get_named_property(env, arg, propertyName.c_str(), &property) != napi_ok)) {
957             MEDIA_ERR_LOG("Failed to obtain property: %{public}s", propertyName.c_str());
958             retval = -1;
959     }
960 
961     return retval;
962 }
963 
GetPointProperties(napi_env env,napi_value pointObj,Point & point)964 int32_t GetPointProperties(napi_env env, napi_value pointObj, Point &point)
965 {
966     MEDIA_DEBUG_LOG("GetPointProperties is called");
967     napi_value propertyX = nullptr;
968     napi_value propertyY = nullptr;
969     double pointX = -1.0;
970     double pointY = -1.0;
971 
972     if ((QueryAndGetInputProperty(env, pointObj, "x", propertyX) == 0) &&
973         (QueryAndGetInputProperty(env, pointObj, "y", propertyY) == 0)) {
974         if ((napi_get_value_double(env, propertyX, &pointX) != napi_ok) ||
975             (napi_get_value_double(env, propertyY, &pointY) != napi_ok)) {
976             MEDIA_ERR_LOG("GetPointProperties: get propery for x & y failed");
977             return -1;
978         } else {
979             point.x = pointX;
980             point.y = pointY;
981         }
982     } else {
983         return -1;
984     }
985 
986     // Return 0 after focus point properties are successfully obtained
987     return 0;
988 }
989 
GetPointNapiValue(napi_env env,Point & point)990 napi_value GetPointNapiValue(napi_env env, Point &point)
991 {
992     MEDIA_DEBUG_LOG("GetPointNapiValue is called");
993     napi_value result;
994     napi_value propValue;
995     napi_create_object(env, &result);
996     napi_create_double(env, CameraNapiUtils::FloatToDouble(point.x), &propValue);
997     napi_set_named_property(env, result, "x", propValue);
998     napi_create_double(env, CameraNapiUtils::FloatToDouble(point.y), &propValue);
999     napi_set_named_property(env, result, "y", propValue);
1000     return result;
1001 }
1002 
CreateCameraSession(napi_env env)1003 napi_value CameraSessionNapi::CreateCameraSession(napi_env env)
1004 {
1005     MEDIA_DEBUG_LOG("CreateCameraSession is called");
1006     CAMERA_SYNC_TRACE;
1007     napi_status status;
1008     napi_value result = nullptr;
1009     napi_value constructor;
1010 
1011     status = napi_get_reference_value(env, sConstructor_, &constructor);
1012     if (status == napi_ok) {
1013         int retCode = CameraManager::GetInstance()->CreateCaptureSession(&sCameraSession_);
1014         if (!CameraNapiUtils::CheckError(env, retCode)) {
1015             return nullptr;
1016         }
1017         if (sCameraSession_ == nullptr) {
1018             MEDIA_ERR_LOG("Failed to create Camera session instance");
1019             napi_get_undefined(env, &result);
1020             return result;
1021         }
1022         status = napi_new_instance(env, constructor, 0, nullptr, &result);
1023         sCameraSession_ = nullptr;
1024         if (status == napi_ok && result != nullptr) {
1025             MEDIA_DEBUG_LOG("success to create Camera session napi instance");
1026             return result;
1027         } else {
1028             MEDIA_ERR_LOG("Failed to create Camera session napi instance");
1029         }
1030     }
1031     MEDIA_ERR_LOG("Failed to create Camera session napi instance last");
1032     napi_get_undefined(env, &result);
1033     return result;
1034 }
1035 
BeginConfig(napi_env env,napi_callback_info info)1036 napi_value CameraSessionNapi::BeginConfig(napi_env env, napi_callback_info info)
1037 {
1038     MEDIA_INFO_LOG("BeginConfig is called");
1039     napi_status status;
1040     napi_value result = nullptr;
1041     size_t argc = ARGS_ZERO;
1042     napi_value argv[ARGS_ZERO];
1043     napi_value thisVar = nullptr;
1044     napi_get_undefined(env, &result);
1045     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1046 
1047     CameraSessionNapi* cameraSessionNapi = nullptr;
1048     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1049     if (status == napi_ok && cameraSessionNapi != nullptr) {
1050         int32_t ret = cameraSessionNapi->cameraSession_->BeginConfig();
1051         if (!CameraNapiUtils::CheckError(env, ret)) {
1052             return nullptr;
1053         }
1054     } else {
1055         MEDIA_ERR_LOG("BeginConfig call Failed!");
1056     }
1057     return result;
1058 }
1059 
CommitConfig(napi_env env,napi_callback_info info)1060 napi_value CameraSessionNapi::CommitConfig(napi_env env, napi_callback_info info)
1061 {
1062     MEDIA_INFO_LOG("CommitConfig is called");
1063     std::unique_ptr<CameraSessionAsyncContext> asyncContext = std::make_unique<CameraSessionAsyncContext>(
1064         "CameraSessionNapi::CommitConfig", CameraNapiUtils::IncrementAndGet(cameraSessionTaskId));
1065     auto asyncFunction = std::make_shared<CameraNapiAsyncFunction>(
1066         env, "CommitConfig", asyncContext->callbackRef, asyncContext->deferred);
1067     CameraNapiParamParser jsParamParser(env, info, asyncContext->objectInfo, asyncFunction);
1068     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "invalid argument")) {
1069         MEDIA_ERR_LOG("CameraSessionNapi::CommitConfig invalid argument");
1070         return nullptr;
1071     }
1072     asyncContext->HoldNapiValue(env, jsParamParser.GetThisVar());
1073     napi_status status = napi_create_async_work(
1074         env, nullptr, asyncFunction->GetResourceName(),
1075         [](napi_env env, void* data) {
1076             MEDIA_INFO_LOG("CameraSessionNapi::CommitConfig running on worker");
1077             auto context = static_cast<CameraSessionAsyncContext*>(data);
1078             CHECK_ERROR_RETURN_LOG(
1079                 context->objectInfo == nullptr, "CameraSessionNapi::CommitConfig async info is nullptr");
1080             CAMERA_START_ASYNC_TRACE(context->funcName, context->taskId);
1081             CameraNapiWorkerQueueKeeper::GetInstance()->ConsumeWorkerQueueTask(context->queueTask, [&context]() {
1082                 context->errorCode = context->objectInfo->cameraSession_->CommitConfig();
1083                 context->status = context->errorCode == CameraErrorCode::SUCCESS;
1084                 MEDIA_INFO_LOG("CameraSessionNapi::CommitConfig errorCode:%{public}d", context->errorCode);
1085             });
1086         },
1087         AsyncCompleteCallback, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1088     if (status != napi_ok) {
1089         MEDIA_ERR_LOG("Failed to create napi_create_async_work for CameraSessionNapi::CommitConfig");
1090         asyncFunction->Reset();
1091     } else {
1092         asyncContext->queueTask =
1093             CameraNapiWorkerQueueKeeper::GetInstance()->AcquireWorkerQueueTask("CameraSessionNapi::CommitConfig");
1094         napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_user_initiated);
1095         asyncContext.release();
1096     }
1097     if (asyncFunction->GetAsyncFunctionType() == ASYNC_FUN_TYPE_PROMISE) {
1098         return asyncFunction->GetPromise();
1099     }
1100     return CameraNapiUtils::GetUndefinedValue(env);
1101 }
1102 
LockForControl(napi_env env,napi_callback_info info)1103 napi_value CameraSessionNapi::LockForControl(napi_env env, napi_callback_info info)
1104 {
1105     MEDIA_DEBUG_LOG("LockForControl is called");
1106     napi_status status;
1107     napi_value result = nullptr;
1108     size_t argc = ARGS_ZERO;
1109     napi_value argv[ARGS_ZERO];
1110     napi_value thisVar = nullptr;
1111 
1112     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1113 
1114     napi_get_undefined(env, &result);
1115     CameraSessionNapi* cameraSessionNapi = nullptr;
1116     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1117     if (status == napi_ok && cameraSessionNapi != nullptr) {
1118         cameraSessionNapi->cameraSession_->LockForControl();
1119     } else {
1120         MEDIA_ERR_LOG("LockForControl call Failed!");
1121     }
1122     return result;
1123 }
1124 
UnlockForControl(napi_env env,napi_callback_info info)1125 napi_value CameraSessionNapi::UnlockForControl(napi_env env, napi_callback_info info)
1126 {
1127     MEDIA_DEBUG_LOG("UnlockForControl is called");
1128     napi_status status;
1129     napi_value result = nullptr;
1130     size_t argc = ARGS_ZERO;
1131     napi_value argv[ARGS_ZERO];
1132     napi_value thisVar = nullptr;
1133 
1134     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1135 
1136     napi_get_undefined(env, &result);
1137     CameraSessionNapi* cameraSessionNapi = nullptr;
1138     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1139     if (status == napi_ok && cameraSessionNapi != nullptr) {
1140         cameraSessionNapi->cameraSession_->UnlockForControl();
1141     } else {
1142         MEDIA_ERR_LOG("UnlockForControl call Failed!");
1143     }
1144     return result;
1145 }
1146 
GetJSArgsForCameraInput(napi_env env,size_t argc,const napi_value argv[],sptr<CaptureInput> & cameraInput)1147 napi_value GetJSArgsForCameraInput(napi_env env, size_t argc, const napi_value argv[],
1148     sptr<CaptureInput> &cameraInput)
1149 {
1150     MEDIA_DEBUG_LOG("GetJSArgsForCameraInput is called");
1151     napi_value result = nullptr;
1152     CameraInputNapi* cameraInputNapiObj = nullptr;
1153 
1154     NAPI_ASSERT(env, argv != nullptr, "Argument list is empty");
1155 
1156     for (size_t i = PARAM0; i < argc; i++) {
1157         napi_valuetype valueType = napi_undefined;
1158         napi_typeof(env, argv[i], &valueType);
1159         if (i == PARAM0 && valueType == napi_object) {
1160             napi_unwrap(env, argv[i], reinterpret_cast<void**>(&cameraInputNapiObj));
1161             if (cameraInputNapiObj != nullptr) {
1162                 cameraInput = cameraInputNapiObj->GetCameraInput();
1163             } else {
1164                 NAPI_ASSERT(env, false, "type mismatch");
1165             }
1166         } else {
1167             NAPI_ASSERT(env, false, "type mismatch");
1168         }
1169     }
1170     napi_get_boolean(env, true, &result);
1171     return result;
1172 }
1173 
AddInput(napi_env env,napi_callback_info info)1174 napi_value CameraSessionNapi::AddInput(napi_env env, napi_callback_info info)
1175 {
1176     MEDIA_INFO_LOG("AddInput is called");
1177     napi_status status;
1178     napi_value result = nullptr;
1179     size_t argc = ARGS_ONE;
1180     napi_value argv[ARGS_ONE] = {0};
1181     napi_value thisVar = nullptr;
1182 
1183     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1184     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, ADD_INPUT)) {
1185         return result;
1186     }
1187 
1188     napi_get_undefined(env, &result);
1189     CameraSessionNapi* cameraSessionNapi = nullptr;
1190     sptr<CaptureInput> cameraInput = nullptr;
1191     GetJSArgsForCameraInput(env, argc, argv, cameraInput);
1192     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1193     if (status == napi_ok && cameraSessionNapi != nullptr) {
1194         int32_t ret = cameraSessionNapi->cameraSession_->AddInput(cameraInput);
1195         if (!CameraNapiUtils::CheckError(env, ret)) {
1196             return nullptr;
1197         }
1198     } else {
1199         MEDIA_ERR_LOG("AddInput call Failed!");
1200     }
1201     return result;
1202 }
1203 
CanAddInput(napi_env env,napi_callback_info info)1204 napi_value CameraSessionNapi::CanAddInput(napi_env env, napi_callback_info info)
1205 {
1206     MEDIA_DEBUG_LOG("CanAddInput is called");
1207     napi_status status;
1208     napi_value result = nullptr;
1209     size_t argc = ARGS_ONE;
1210     napi_value argv[ARGS_ONE] = {0};
1211     napi_value thisVar = nullptr;
1212 
1213     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1214 
1215     napi_get_undefined(env, &result);
1216     CameraSessionNapi* cameraSessionNapi = nullptr;
1217     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1218     if (status == napi_ok && cameraSessionNapi != nullptr) {
1219         sptr<CaptureInput> cameraInput = nullptr;
1220         GetJSArgsForCameraInput(env, argc, argv, cameraInput);
1221         bool isSupported = cameraSessionNapi->cameraSession_->CanAddInput(cameraInput);
1222         napi_get_boolean(env, isSupported, &result);
1223     } else {
1224         MEDIA_ERR_LOG("CanAddInput call Failed!");
1225     }
1226     return result;
1227 }
1228 
RemoveInput(napi_env env,napi_callback_info info)1229 napi_value CameraSessionNapi::RemoveInput(napi_env env, napi_callback_info info)
1230 {
1231     MEDIA_DEBUG_LOG("RemoveInput is called");
1232     napi_status status;
1233     napi_value result = nullptr;
1234     size_t argc = ARGS_ONE;
1235     napi_value argv[ARGS_ONE] = {0};
1236     napi_value thisVar = nullptr;
1237 
1238     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1239     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, REMOVE_INPUT)) {
1240         return result;
1241     }
1242 
1243     napi_get_undefined(env, &result);
1244     CameraSessionNapi* cameraSessionNapi = nullptr;
1245     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1246     if (status == napi_ok && cameraSessionNapi != nullptr) {
1247         sptr<CaptureInput> cameraInput = nullptr;
1248         GetJSArgsForCameraInput(env, argc, argv, cameraInput);
1249         int32_t ret = cameraSessionNapi->cameraSession_->RemoveInput(cameraInput);
1250         if (!CameraNapiUtils::CheckError(env, ret)) {
1251             return nullptr;
1252         }
1253         return result;
1254     } else {
1255         MEDIA_ERR_LOG("RemoveInput call Failed!");
1256     }
1257     return result;
1258 }
1259 
GetJSArgsForCameraOutput(napi_env env,size_t argc,const napi_value argv[],sptr<CaptureOutput> & cameraOutput)1260 napi_value CameraSessionNapi::GetJSArgsForCameraOutput(napi_env env, size_t argc, const napi_value argv[],
1261     sptr<CaptureOutput> &cameraOutput)
1262 {
1263     MEDIA_DEBUG_LOG("GetJSArgsForCameraOutput is called");
1264     napi_value result = nullptr;
1265     PreviewOutputNapi* previewOutputNapiObj = nullptr;
1266     PhotoOutputNapi* photoOutputNapiObj = nullptr;
1267     VideoOutputNapi* videoOutputNapiObj = nullptr;
1268     MetadataOutputNapi* metadataOutputNapiObj = nullptr;
1269     DepthDataOutputNapi* depthDataOutputNapiObj = nullptr;
1270 
1271     NAPI_ASSERT(env, argv != nullptr, "Argument list is empty");
1272 
1273     for (size_t i = PARAM0; i < argc; i++) {
1274         napi_valuetype valueType = napi_undefined;
1275         napi_typeof(env, argv[i], &valueType);
1276 
1277         if (i == PARAM0 && valueType == napi_object) {
1278             if (PreviewOutputNapi::IsPreviewOutput(env, argv[i])) {
1279                 MEDIA_INFO_LOG("preview output adding..");
1280                 napi_unwrap(env, argv[i], reinterpret_cast<void**>(&previewOutputNapiObj));
1281                 cameraOutput = previewOutputNapiObj->GetPreviewOutput();
1282             } else if (PhotoOutputNapi::IsPhotoOutput(env, argv[i])) {
1283                 MEDIA_INFO_LOG("photo output adding..");
1284                 napi_unwrap(env, argv[i], reinterpret_cast<void**>(&photoOutputNapiObj));
1285                 cameraOutput = photoOutputNapiObj->GetPhotoOutput();
1286             } else if (VideoOutputNapi::IsVideoOutput(env, argv[i])) {
1287                 MEDIA_INFO_LOG("video output adding..");
1288                 napi_unwrap(env, argv[i], reinterpret_cast<void**>(&videoOutputNapiObj));
1289                 cameraOutput = videoOutputNapiObj->GetVideoOutput();
1290             } else if (MetadataOutputNapi::IsMetadataOutput(env, argv[i])) {
1291                 MEDIA_INFO_LOG("metadata output adding..");
1292                 napi_unwrap(env, argv[i], reinterpret_cast<void**>(&metadataOutputNapiObj));
1293                 cameraOutput = metadataOutputNapiObj->GetMetadataOutput();
1294             } else if (DepthDataOutputNapi::IsDepthDataOutput(env, argv[i])) {
1295                 MEDIA_INFO_LOG("depth data output adding..");
1296                 napi_unwrap(env, argv[i], reinterpret_cast<void**>(&depthDataOutputNapiObj));
1297                 cameraOutput = depthDataOutputNapiObj->GetDepthDataOutput();
1298             } else {
1299                 MEDIA_INFO_LOG("invalid output ..");
1300                 NAPI_ASSERT(env, false, "type mismatch");
1301             }
1302         } else {
1303             NAPI_ASSERT(env, false, "type mismatch");
1304         }
1305     }
1306     // Return true napi_value if params are successfully obtained
1307     napi_get_boolean(env, true, &result);
1308     return result;
1309 }
1310 
AddOutput(napi_env env,napi_callback_info info)1311 napi_value CameraSessionNapi::AddOutput(napi_env env, napi_callback_info info)
1312 {
1313     MEDIA_INFO_LOG("AddOutput is called");
1314     napi_status status;
1315     napi_value result = nullptr;
1316     size_t argc = ARGS_ONE;
1317     napi_value argv[ARGS_ONE] = {0};
1318     napi_value thisVar = nullptr;
1319 
1320     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1321     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, ADD_OUTPUT)) {
1322         return result;
1323     }
1324 
1325     napi_get_undefined(env, &result);
1326     CameraSessionNapi* cameraSessionNapi = nullptr;
1327     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1328     if (status == napi_ok && cameraSessionNapi != nullptr) {
1329         sptr<CaptureOutput> cameraOutput = nullptr;
1330         result = GetJSArgsForCameraOutput(env, argc, argv, cameraOutput);
1331         int32_t ret = cameraSessionNapi->cameraSession_->AddOutput(cameraOutput);
1332         if (!CameraNapiUtils::CheckError(env, ret)) {
1333             return nullptr;
1334         }
1335     } else {
1336         MEDIA_ERR_LOG("AddOutput call Failed!");
1337     }
1338     return result;
1339 }
1340 
CanAddOutput(napi_env env,napi_callback_info info)1341 napi_value CameraSessionNapi::CanAddOutput(napi_env env, napi_callback_info info)
1342 {
1343     MEDIA_DEBUG_LOG("CanAddOutput is called");
1344     napi_status status;
1345     napi_value result = nullptr;
1346     size_t argc = ARGS_ONE;
1347     napi_value argv[ARGS_ONE] = {0};
1348     napi_value thisVar = nullptr;
1349 
1350     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1351 
1352     napi_get_undefined(env, &result);
1353     CameraSessionNapi* cameraSessionNapi = nullptr;
1354     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1355     if (status == napi_ok && cameraSessionNapi != nullptr) {
1356         sptr<CaptureOutput> cameraOutput = nullptr;
1357         result = GetJSArgsForCameraOutput(env, argc, argv, cameraOutput);
1358         bool isSupported = cameraSessionNapi->cameraSession_->CanAddOutput(cameraOutput);
1359         napi_get_boolean(env, isSupported, &result);
1360     } else {
1361         MEDIA_ERR_LOG("CanAddOutput call Failed!");
1362     }
1363     return result;
1364 }
1365 
RemoveOutput(napi_env env,napi_callback_info info)1366 napi_value CameraSessionNapi::RemoveOutput(napi_env env, napi_callback_info info)
1367 {
1368     MEDIA_INFO_LOG("RemoveOutput is called");
1369     napi_status status;
1370     napi_value result = nullptr;
1371     size_t argc = ARGS_ONE;
1372     napi_value argv[ARGS_ONE] = {0};
1373     napi_value thisVar = nullptr;
1374 
1375     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1376     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, REMOVE_OUTPUT)) {
1377         return result;
1378     }
1379 
1380     napi_get_undefined(env, &result);
1381     CameraSessionNapi* cameraSessionNapi = nullptr;
1382     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1383     if (status == napi_ok && cameraSessionNapi != nullptr) {
1384         sptr<CaptureOutput> cameraOutput = nullptr;
1385         result = GetJSArgsForCameraOutput(env, argc, argv, cameraOutput);
1386         int32_t ret = cameraSessionNapi->cameraSession_->RemoveOutput(cameraOutput);
1387         if (!CameraNapiUtils::CheckError(env, ret)) {
1388             return nullptr;
1389         }
1390     } else {
1391         MEDIA_ERR_LOG("RemoveOutput call Failed!");
1392     }
1393     return result;
1394 }
1395 
Start(napi_env env,napi_callback_info info)1396 napi_value CameraSessionNapi::Start(napi_env env, napi_callback_info info)
1397 {
1398     MEDIA_INFO_LOG("Start is called");
1399     std::unique_ptr<CameraSessionAsyncContext> asyncContext = std::make_unique<CameraSessionAsyncContext>(
1400         "CameraSessionNapi::Start", CameraNapiUtils::IncrementAndGet(cameraSessionTaskId));
1401     auto asyncFunction =
1402         std::make_shared<CameraNapiAsyncFunction>(env, "Start", asyncContext->callbackRef, asyncContext->deferred);
1403     CameraNapiParamParser jsParamParser(env, info, asyncContext->objectInfo, asyncFunction);
1404     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "invalid argument")) {
1405         MEDIA_ERR_LOG("CameraSessionNapi::Start invalid argument");
1406         return nullptr;
1407     }
1408     asyncContext->HoldNapiValue(env, jsParamParser.GetThisVar());
1409     napi_status status = napi_create_async_work(
1410         env, nullptr, asyncFunction->GetResourceName(),
1411         [](napi_env env, void* data) {
1412             MEDIA_INFO_LOG("CameraSessionNapi::Start running on worker");
1413             auto context = static_cast<CameraSessionAsyncContext*>(data);
1414             CHECK_ERROR_RETURN_LOG(context->objectInfo == nullptr, "CameraSessionNapi::Start async info is nullptr");
1415             CAMERA_START_ASYNC_TRACE(context->funcName, context->taskId);
1416             CameraNapiWorkerQueueKeeper::GetInstance()->ConsumeWorkerQueueTask(context->queueTask, [&context]() {
1417                 context->errorCode = context->objectInfo->cameraSession_->Start();
1418                 context->status = context->errorCode == CameraErrorCode::SUCCESS;
1419                 MEDIA_INFO_LOG("CameraSessionNapi::Start errorCode:%{public}d", context->errorCode);
1420             });
1421         },
1422         AsyncCompleteCallback, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1423     if (status != napi_ok) {
1424         MEDIA_ERR_LOG("Failed to create napi_create_async_work for CameraSessionNapi::Start");
1425         asyncFunction->Reset();
1426     } else {
1427         asyncContext->queueTask =
1428             CameraNapiWorkerQueueKeeper::GetInstance()->AcquireWorkerQueueTask("CameraSessionNapi::Start");
1429         napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_user_initiated);
1430         asyncContext.release();
1431     }
1432     if (asyncFunction->GetAsyncFunctionType() == ASYNC_FUN_TYPE_PROMISE) {
1433         return asyncFunction->GetPromise();
1434     }
1435     return CameraNapiUtils::GetUndefinedValue(env);
1436 }
1437 
Stop(napi_env env,napi_callback_info info)1438 napi_value CameraSessionNapi::Stop(napi_env env, napi_callback_info info)
1439 {
1440     MEDIA_INFO_LOG("Stop is called");
1441     std::unique_ptr<CameraSessionAsyncContext> asyncContext = std::make_unique<CameraSessionAsyncContext>(
1442         "CameraSessionNapi::Stop", CameraNapiUtils::IncrementAndGet(cameraSessionTaskId));
1443     auto asyncFunction =
1444         std::make_shared<CameraNapiAsyncFunction>(env, "Stop", asyncContext->callbackRef, asyncContext->deferred);
1445     CameraNapiParamParser jsParamParser(env, info, asyncContext->objectInfo, asyncFunction);
1446     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "invalid argument")) {
1447         MEDIA_ERR_LOG("CameraSessionNapi::Stop invalid argument");
1448         return nullptr;
1449     }
1450     asyncContext->HoldNapiValue(env, jsParamParser.GetThisVar());
1451     napi_status status = napi_create_async_work(
1452         env, nullptr, asyncFunction->GetResourceName(),
1453         [](napi_env env, void* data) {
1454             MEDIA_INFO_LOG("CameraSessionNapi::Stop running on worker");
1455             auto context = static_cast<CameraSessionAsyncContext*>(data);
1456             CHECK_ERROR_RETURN_LOG(context->objectInfo == nullptr, "CameraSessionNapi::Stop async info is nullptr");
1457             CAMERA_START_ASYNC_TRACE(context->funcName, context->taskId);
1458             CameraNapiWorkerQueueKeeper::GetInstance()->ConsumeWorkerQueueTask(context->queueTask, [&context]() {
1459                 context->errorCode = context->objectInfo->cameraSession_->Stop();
1460                 context->status = context->errorCode == CameraErrorCode::SUCCESS;
1461                 MEDIA_INFO_LOG("CameraSessionNapi::Stop errorCode:%{public}d", context->errorCode);
1462             });
1463         },
1464         AsyncCompleteCallback, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1465     if (status != napi_ok) {
1466         MEDIA_ERR_LOG("Failed to create napi_create_async_work for CameraSessionNapi::Stop");
1467         asyncFunction->Reset();
1468     } else {
1469         asyncContext->queueTask =
1470             CameraNapiWorkerQueueKeeper::GetInstance()->AcquireWorkerQueueTask("CameraSessionNapi::Stop");
1471         napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_user_initiated);
1472         asyncContext.release();
1473     }
1474     if (asyncFunction->GetAsyncFunctionType() == ASYNC_FUN_TYPE_PROMISE) {
1475         return asyncFunction->GetPromise();
1476     }
1477     return CameraNapiUtils::GetUndefinedValue(env);
1478 }
1479 
Release(napi_env env,napi_callback_info info)1480 napi_value CameraSessionNapi::Release(napi_env env, napi_callback_info info)
1481 {
1482     MEDIA_INFO_LOG("Release is called");
1483     std::unique_ptr<CameraSessionAsyncContext> asyncContext = std::make_unique<CameraSessionAsyncContext>(
1484         "CameraSessionNapi::Release", CameraNapiUtils::IncrementAndGet(cameraSessionTaskId));
1485     auto asyncFunction =
1486         std::make_shared<CameraNapiAsyncFunction>(env, "Release", asyncContext->callbackRef, asyncContext->deferred);
1487     CameraNapiParamParser jsParamParser(env, info, asyncContext->objectInfo, asyncFunction);
1488     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "invalid argument")) {
1489         MEDIA_ERR_LOG("CameraSessionNapi::Release invalid argument");
1490         return nullptr;
1491     }
1492     asyncContext->HoldNapiValue(env, jsParamParser.GetThisVar());
1493     napi_status status = napi_create_async_work(
1494         env, nullptr, asyncFunction->GetResourceName(),
1495         [](napi_env env, void* data) {
1496             MEDIA_INFO_LOG("CameraSessionNapi::Release running on worker");
1497             auto context = static_cast<CameraSessionAsyncContext*>(data);
1498             CHECK_ERROR_RETURN_LOG(context->objectInfo == nullptr, "CameraSessionNapi::Release async info is nullptr");
1499             CAMERA_START_ASYNC_TRACE(context->funcName, context->taskId);
1500             CameraNapiWorkerQueueKeeper::GetInstance()->ConsumeWorkerQueueTask(context->queueTask, [&context]() {
1501                 context->errorCode = context->objectInfo->cameraSession_->Release();
1502                 context->status = context->errorCode == CameraErrorCode::SUCCESS;
1503                 MEDIA_INFO_LOG("CameraSessionNapi::Release errorCode:%{public}d", context->errorCode);
1504             });
1505         },
1506         AsyncCompleteCallback, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1507     if (status != napi_ok) {
1508         MEDIA_ERR_LOG("Failed to create napi_create_async_work for CameraSessionNapi::Release");
1509         asyncFunction->Reset();
1510     } else {
1511         asyncContext->queueTask =
1512             CameraNapiWorkerQueueKeeper::GetInstance()->AcquireWorkerQueueTask("CameraSessionNapi::Release");
1513         napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_user_initiated);
1514         asyncContext.release();
1515     }
1516     if (asyncFunction->GetAsyncFunctionType() == ASYNC_FUN_TYPE_PROMISE) {
1517         return asyncFunction->GetPromise();
1518     }
1519     return CameraNapiUtils::GetUndefinedValue(env);
1520 }
1521 
IsVideoStabilizationModeSupported(napi_env env,napi_callback_info info)1522 napi_value CameraSessionNapi::IsVideoStabilizationModeSupported(napi_env env, napi_callback_info info)
1523 {
1524     MEDIA_DEBUG_LOG("IsVideoStabilizationModeSupported is called");
1525     napi_status status;
1526     napi_value result = nullptr;
1527     size_t argc = ARGS_ONE;
1528     napi_value argv[ARGS_ONE] = {0};
1529     napi_value thisVar = nullptr;
1530 
1531     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1532 
1533     napi_get_undefined(env, &result);
1534     CameraSessionNapi* cameraSessionNapi = nullptr;
1535     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1536     if (status == napi_ok && cameraSessionNapi != nullptr) {
1537         int32_t value;
1538         napi_get_value_int32(env, argv[PARAM0], &value);
1539         VideoStabilizationMode videoStabilizationMode = (VideoStabilizationMode)value;
1540         bool isSupported;
1541         int32_t retCode = cameraSessionNapi->cameraSession_->
1542                           IsVideoStabilizationModeSupported(videoStabilizationMode, isSupported);
1543         if (!CameraNapiUtils::CheckError(env, retCode)) {
1544             return nullptr;
1545         }
1546         napi_get_boolean(env, isSupported, &result);
1547     } else {
1548         MEDIA_ERR_LOG("IsVideoStabilizationModeSupported call Failed!");
1549     }
1550     return result;
1551 }
1552 
GetActiveVideoStabilizationMode(napi_env env,napi_callback_info info)1553 napi_value CameraSessionNapi::GetActiveVideoStabilizationMode(napi_env env, napi_callback_info info)
1554 {
1555     MEDIA_DEBUG_LOG("GetActiveVideoStabilizationMode is called");
1556     napi_status status;
1557     napi_value result = nullptr;
1558     size_t argc = ARGS_ZERO;
1559     napi_value argv[ARGS_ZERO];
1560     napi_value thisVar = nullptr;
1561 
1562     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1563 
1564     napi_get_undefined(env, &result);
1565     CameraSessionNapi* cameraSessionNapi = nullptr;
1566     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1567     if (status == napi_ok && cameraSessionNapi != nullptr) {
1568         VideoStabilizationMode videoStabilizationMode;
1569         int32_t retCode = cameraSessionNapi->cameraSession_->
1570                           GetActiveVideoStabilizationMode(videoStabilizationMode);
1571         if (!CameraNapiUtils::CheckError(env, retCode)) {
1572             return nullptr;
1573         }
1574         napi_create_int32(env, videoStabilizationMode, &result);
1575     } else {
1576         MEDIA_ERR_LOG("GetActiveVideoStabilizationMode call Failed!");
1577     }
1578     return result;
1579 }
1580 
SetVideoStabilizationMode(napi_env env,napi_callback_info info)1581 napi_value CameraSessionNapi::SetVideoStabilizationMode(napi_env env, napi_callback_info info)
1582 {
1583     MEDIA_DEBUG_LOG("SetVideoStabilizationMode is called");
1584     napi_status status;
1585     napi_value result = nullptr;
1586     size_t argc = ARGS_ONE;
1587     napi_value argv[ARGS_ONE] = {0};
1588     napi_value thisVar = nullptr;
1589 
1590     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1591 
1592     napi_get_undefined(env, &result);
1593     CameraSessionNapi* cameraSessionNapi = nullptr;
1594     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1595     if (status == napi_ok && cameraSessionNapi != nullptr) {
1596         int32_t value;
1597         napi_get_value_int32(env, argv[PARAM0], &value);
1598         VideoStabilizationMode videoStabilizationMode = (VideoStabilizationMode)value;
1599         int retCode = cameraSessionNapi->cameraSession_->SetVideoStabilizationMode(videoStabilizationMode);
1600         if (!CameraNapiUtils::CheckError(env, retCode)) {
1601             return nullptr;
1602         }
1603     } else {
1604         MEDIA_ERR_LOG("SetVideoStabilizationMode call Failed!");
1605     }
1606     return result;
1607 }
1608 
HasFlash(napi_env env,napi_callback_info info)1609 napi_value CameraSessionNapi::HasFlash(napi_env env, napi_callback_info info)
1610 {
1611     MEDIA_DEBUG_LOG("HasFlash is called");
1612     napi_status status;
1613     napi_value result = nullptr;
1614     size_t argc = ARGS_ZERO;
1615     napi_value argv[ARGS_ZERO];
1616     napi_value thisVar = nullptr;
1617 
1618     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1619 
1620     napi_get_undefined(env, &result);
1621     CameraSessionNapi* cameraSessionNapi = nullptr;
1622     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1623     if (status == napi_ok && cameraSessionNapi != nullptr) {
1624         bool isSupported = false;
1625         int retCode = cameraSessionNapi->cameraSession_->HasFlash(isSupported);
1626         if (!CameraNapiUtils::CheckError(env, retCode)) {
1627             return nullptr;
1628         }
1629         napi_get_boolean(env, isSupported, &result);
1630     } else {
1631         MEDIA_ERR_LOG("HasFlash call Failed!");
1632     }
1633     return result;
1634 }
1635 
IsFlashModeSupported(napi_env env,napi_callback_info info)1636 napi_value CameraSessionNapi::IsFlashModeSupported(napi_env env, napi_callback_info info)
1637 {
1638     MEDIA_DEBUG_LOG("IsFlashModeSupported is called");
1639     napi_status status;
1640     napi_value result = nullptr;
1641     size_t argc = ARGS_ONE;
1642     napi_value argv[ARGS_ONE] = {0};
1643     napi_value thisVar = nullptr;
1644 
1645     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1646 
1647     napi_get_undefined(env, &result);
1648     CameraSessionNapi* cameraSessionNapi = nullptr;
1649     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1650     if (status == napi_ok && cameraSessionNapi != nullptr) {
1651         int32_t value;
1652         napi_get_value_int32(env, argv[PARAM0], &value);
1653         FlashMode flashMode = (FlashMode)value;
1654         bool isSupported;
1655         int32_t retCode = cameraSessionNapi->cameraSession_->IsFlashModeSupported(flashMode, isSupported);
1656         if (!CameraNapiUtils::CheckError(env, retCode)) {
1657             return nullptr;
1658         }
1659         napi_get_boolean(env, isSupported, &result);
1660     } else {
1661         MEDIA_ERR_LOG("IsFlashModeSupported call Failed!");
1662     }
1663     return result;
1664 }
1665 
SetFlashMode(napi_env env,napi_callback_info info)1666 napi_value CameraSessionNapi::SetFlashMode(napi_env env, napi_callback_info info)
1667 {
1668     MEDIA_DEBUG_LOG("SetFlashMode is called");
1669     CAMERA_SYNC_TRACE;
1670     napi_status status;
1671     napi_value result = nullptr;
1672     size_t argc = ARGS_ONE;
1673     napi_value argv[ARGS_ONE] = {0};
1674     napi_value thisVar = nullptr;
1675 
1676     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1677 
1678     napi_get_undefined(env, &result);
1679     CameraSessionNapi* cameraSessionNapi = nullptr;
1680     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1681     if (status == napi_ok && cameraSessionNapi != nullptr) {
1682         int32_t value;
1683         napi_get_value_int32(env, argv[PARAM0], &value);
1684         MEDIA_INFO_LOG("CameraSessionNapi::SetFlashMode mode:%{public}d", value);
1685         FlashMode flashMode = (FlashMode)value;
1686         cameraSessionNapi->cameraSession_->LockForControl();
1687         int retCode = cameraSessionNapi->cameraSession_->SetFlashMode(flashMode);
1688         cameraSessionNapi->cameraSession_->UnlockForControl();
1689         if (!CameraNapiUtils::CheckError(env, retCode)) {
1690             return nullptr;
1691         }
1692     } else {
1693         MEDIA_ERR_LOG("SetFlashMode call Failed!");
1694     }
1695     return result;
1696 }
1697 
GetFlashMode(napi_env env,napi_callback_info info)1698 napi_value CameraSessionNapi::GetFlashMode(napi_env env, napi_callback_info info)
1699 {
1700     MEDIA_DEBUG_LOG("GetFlashMode is called");
1701     napi_status status;
1702     napi_value result = nullptr;
1703     size_t argc = ARGS_ZERO;
1704     napi_value argv[ARGS_ZERO];
1705     napi_value thisVar = nullptr;
1706 
1707     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1708     napi_get_undefined(env, &result);
1709     CameraSessionNapi* cameraSessionNapi = nullptr;
1710     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1711     if (status == napi_ok && cameraSessionNapi != nullptr) {
1712         FlashMode flashMode;
1713         int32_t retCode = cameraSessionNapi->cameraSession_->GetFlashMode(flashMode);
1714         if (!CameraNapiUtils::CheckError(env, retCode)) {
1715             return nullptr;
1716         }
1717         napi_create_int32(env, flashMode, &result);
1718     } else {
1719         MEDIA_ERR_LOG("GetFlashMode call Failed!");
1720     }
1721     return result;
1722 }
1723 
IsLcdFlashSupported(napi_env env,napi_callback_info info)1724 napi_value CameraSessionNapi::IsLcdFlashSupported(napi_env env, napi_callback_info info)
1725 {
1726     MEDIA_DEBUG_LOG("IsLcdFlashSupported is called");
1727     CAMERA_SYNC_TRACE;
1728     napi_value result = CameraNapiUtils::GetUndefinedValue(env);
1729     if (!CameraNapiSecurity::CheckSystemApp(env)) {
1730         MEDIA_ERR_LOG("SystemApi isLcdFlashSupported is called!");
1731         return result;
1732     }
1733     CameraSessionNapi* cameraSessionNapi = nullptr;
1734     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
1735     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
1736         MEDIA_ERR_LOG("IsLcdFlashSupported parse parameter occur error");
1737         return result;
1738     }
1739     if (cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
1740         bool isSupported = cameraSessionNapi->cameraSession_->IsLcdFlashSupported();
1741         napi_get_boolean(env, isSupported, &result);
1742     } else {
1743         MEDIA_ERR_LOG("IsLcdFlashSupported call Failed!");
1744     }
1745     return result;
1746 }
1747 
EnableLcdFlash(napi_env env,napi_callback_info info)1748 napi_value CameraSessionNapi::EnableLcdFlash(napi_env env, napi_callback_info info)
1749 {
1750     MEDIA_DEBUG_LOG("EnableLcdFlash is called");
1751     napi_value result = CameraNapiUtils::GetUndefinedValue(env);
1752     if (!CameraNapiSecurity::CheckSystemApp(env)) {
1753         MEDIA_ERR_LOG("SystemApi enableLcdFlash is called!");
1754         return result;
1755     }
1756     bool isEnable;
1757     CameraSessionNapi* cameraSessionNapi = nullptr;
1758     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, isEnable);
1759     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
1760         MEDIA_ERR_LOG("EnableLcdFlash parse parameter occur error");
1761         return result;
1762     }
1763 
1764     if (cameraSessionNapi->cameraSession_ != nullptr) {
1765         MEDIA_INFO_LOG("EnableLcdFlash:%{public}d", isEnable);
1766         cameraSessionNapi->cameraSession_->LockForControl();
1767         int32_t retCode = cameraSessionNapi->cameraSession_->EnableLcdFlash(isEnable);
1768         cameraSessionNapi->cameraSession_->UnlockForControl();
1769         if (!CameraNapiUtils::CheckError(env, retCode)) {
1770             MEDIA_ERR_LOG("EnableLcdFlash fail %{public}d", retCode);
1771             return result;
1772         }
1773     } else {
1774         MEDIA_ERR_LOG("EnableLcdFlash get native object fail");
1775         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
1776         return result;
1777     }
1778     return result;
1779 }
1780 
IsExposureModeSupported(napi_env env,napi_callback_info info)1781 napi_value CameraSessionNapi::IsExposureModeSupported(napi_env env, napi_callback_info info)
1782 {
1783     MEDIA_DEBUG_LOG("IsExposureModeSupported is called");
1784     napi_status status;
1785     napi_value result = nullptr;
1786     size_t argc = ARGS_ONE;
1787     napi_value argv[ARGS_ONE] = {0};
1788     napi_value thisVar = nullptr;
1789 
1790     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1791 
1792     napi_get_undefined(env, &result);
1793     CameraSessionNapi* cameraSessionNapi = nullptr;
1794     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1795     if (status == napi_ok && cameraSessionNapi != nullptr) {
1796         int32_t value;
1797         napi_get_value_int32(env, argv[PARAM0], &value);
1798         ExposureMode exposureMode = (ExposureMode)value;
1799         bool isSupported;
1800         int32_t retCode = cameraSessionNapi->cameraSession_->
1801                     IsExposureModeSupported(static_cast<ExposureMode>(exposureMode), isSupported);
1802         if (!CameraNapiUtils::CheckError(env, retCode)) {
1803             return nullptr;
1804         }
1805         napi_get_boolean(env, isSupported, &result);
1806     } else {
1807         MEDIA_ERR_LOG("IsExposureModeSupported call Failed!");
1808     }
1809     return result;
1810 }
1811 
GetExposureMode(napi_env env,napi_callback_info info)1812 napi_value CameraSessionNapi::GetExposureMode(napi_env env, napi_callback_info info)
1813 {
1814     MEDIA_DEBUG_LOG("GetExposureMode is called");
1815     napi_status status;
1816     napi_value result = nullptr;
1817     size_t argc = ARGS_ZERO;
1818     napi_value argv[ARGS_ZERO];
1819     napi_value thisVar = nullptr;
1820 
1821     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1822 
1823     napi_get_undefined(env, &result);
1824     CameraSessionNapi* cameraSessionNapi = nullptr;
1825     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1826     if (status == napi_ok && cameraSessionNapi != nullptr) {
1827         ExposureMode exposureMode;
1828         int32_t retCode = cameraSessionNapi->cameraSession_->GetExposureMode(exposureMode);
1829         if (!CameraNapiUtils::CheckError(env, retCode)) {
1830             return nullptr;
1831         }
1832         napi_create_int32(env, exposureMode, &result);
1833     } else {
1834         MEDIA_ERR_LOG("GetExposureMode call Failed!");
1835     }
1836     return result;
1837 }
1838 
SetExposureMode(napi_env env,napi_callback_info info)1839 napi_value CameraSessionNapi::SetExposureMode(napi_env env, napi_callback_info info)
1840 {
1841     MEDIA_DEBUG_LOG("SetExposureMode is called");
1842     CAMERA_SYNC_TRACE;
1843     napi_status status;
1844     napi_value result = nullptr;
1845     size_t argc = ARGS_ONE;
1846     napi_value argv[ARGS_ONE] = {0};
1847     napi_value thisVar = nullptr;
1848 
1849     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1850 
1851     napi_get_undefined(env, &result);
1852     CameraSessionNapi* cameraSessionNapi = nullptr;
1853     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1854     if (status == napi_ok && cameraSessionNapi != nullptr) {
1855         int32_t value;
1856         napi_get_value_int32(env, argv[PARAM0], &value);
1857         ExposureMode exposureMode = (ExposureMode)value;
1858         cameraSessionNapi->cameraSession_->LockForControl();
1859         int retCode = cameraSessionNapi->cameraSession_->SetExposureMode(exposureMode);
1860         cameraSessionNapi->cameraSession_->UnlockForControl();
1861         if (!CameraNapiUtils::CheckError(env, retCode)) {
1862             return nullptr;
1863         }
1864     } else {
1865         MEDIA_ERR_LOG("SetExposureMode call Failed!");
1866     }
1867     return result;
1868 }
1869 
SetMeteringPoint(napi_env env,napi_callback_info info)1870 napi_value CameraSessionNapi::SetMeteringPoint(napi_env env, napi_callback_info info)
1871 {
1872     MEDIA_DEBUG_LOG("SetMeteringPoint is called");
1873     CAMERA_SYNC_TRACE;
1874     napi_status status;
1875     napi_value result = nullptr;
1876     size_t argc = ARGS_ONE;
1877     napi_value argv[ARGS_ONE] = {0};
1878     napi_value thisVar = nullptr;
1879 
1880     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1881 
1882     napi_get_undefined(env, &result);
1883     CameraSessionNapi* cameraSessionNapi = nullptr;
1884     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1885     if (status == napi_ok && cameraSessionNapi != nullptr) {
1886         Point exposurePoint;
1887         if (GetPointProperties(env, argv[PARAM0], exposurePoint) == 0) {
1888             cameraSessionNapi->cameraSession_->LockForControl();
1889             int32_t retCode = cameraSessionNapi->cameraSession_->SetMeteringPoint(exposurePoint);
1890             cameraSessionNapi->cameraSession_->UnlockForControl();
1891             if (!CameraNapiUtils::CheckError(env, retCode)) {
1892                 return nullptr;
1893             }
1894         } else {
1895             MEDIA_ERR_LOG("get point failed");
1896         }
1897     } else {
1898         MEDIA_ERR_LOG("SetMeteringPoint call Failed!");
1899     }
1900     return result;
1901 }
1902 
GetMeteringPoint(napi_env env,napi_callback_info info)1903 napi_value CameraSessionNapi::GetMeteringPoint(napi_env env, napi_callback_info info)
1904 {
1905     MEDIA_DEBUG_LOG("GetMeteringPoint is called");
1906     napi_status status;
1907     napi_value result = nullptr;
1908     size_t argc = ARGS_ZERO;
1909     napi_value argv[ARGS_ZERO];
1910     napi_value thisVar = nullptr;
1911 
1912     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1913     napi_get_undefined(env, &result);
1914     CameraSessionNapi* cameraSessionNapi = nullptr;
1915     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1916     if (status == napi_ok && cameraSessionNapi != nullptr) {
1917         Point exposurePoint;
1918         int32_t retCode = cameraSessionNapi->cameraSession_->GetMeteringPoint(exposurePoint);
1919         if (!CameraNapiUtils::CheckError(env, retCode)) {
1920             return nullptr;
1921         }
1922         return GetPointNapiValue(env, exposurePoint);
1923     } else {
1924         MEDIA_ERR_LOG("GetMeteringPoint call Failed!");
1925     }
1926     return result;
1927 }
1928 
GetExposureBiasRange(napi_env env,napi_callback_info info)1929 napi_value CameraSessionNapi::GetExposureBiasRange(napi_env env, napi_callback_info info)
1930 {
1931     MEDIA_DEBUG_LOG("GetExposureBiasRange is called");
1932     napi_status status;
1933     napi_value result = nullptr;
1934     size_t argc = ARGS_ZERO;
1935     napi_value argv[ARGS_ZERO];
1936     napi_value thisVar = nullptr;
1937 
1938     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1939 
1940     napi_get_undefined(env, &result);
1941     CameraSessionNapi* cameraSessionNapi = nullptr;
1942     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1943     if (status == napi_ok && cameraSessionNapi != nullptr) {
1944         std::vector<float> vecExposureBiasList;
1945         int32_t retCode = cameraSessionNapi->cameraSession_->GetExposureBiasRange(vecExposureBiasList);
1946         if (!CameraNapiUtils::CheckError(env, retCode)) {
1947             return nullptr;
1948         }
1949         if (vecExposureBiasList.empty() || napi_create_array(env, &result) != napi_ok) {
1950             return result;
1951         }
1952         size_t len = vecExposureBiasList.size();
1953         for (size_t i = 0; i < len; i++) {
1954             float exposureBias = vecExposureBiasList[i];
1955             MEDIA_DEBUG_LOG("EXPOSURE_BIAS_RANGE : exposureBias = %{public}f", vecExposureBiasList[i]);
1956             napi_value value;
1957             napi_create_double(env, CameraNapiUtils::FloatToDouble(exposureBias), &value);
1958             napi_set_element(env, result, i, value);
1959         }
1960         MEDIA_DEBUG_LOG("EXPOSURE_BIAS_RANGE ExposureBiasList size : %{public}zu", vecExposureBiasList.size());
1961     } else {
1962         MEDIA_ERR_LOG("GetExposureBiasRange call Failed!");
1963     }
1964     return result;
1965 }
1966 
GetExposureValue(napi_env env,napi_callback_info info)1967 napi_value CameraSessionNapi::GetExposureValue(napi_env env, napi_callback_info info)
1968 {
1969     MEDIA_DEBUG_LOG("GetExposureValue is called");
1970     napi_status status;
1971     napi_value result = nullptr;
1972     size_t argc = ARGS_ZERO;
1973     napi_value argv[ARGS_ZERO];
1974     napi_value thisVar = nullptr;
1975 
1976     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
1977 
1978     napi_get_undefined(env, &result);
1979     CameraSessionNapi* cameraSessionNapi = nullptr;
1980     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
1981     if (status == napi_ok && cameraSessionNapi!= nullptr) {
1982         float exposureValue;
1983         int32_t retCode = cameraSessionNapi->cameraSession_->GetExposureValue(exposureValue);
1984         if (!CameraNapiUtils::CheckError(env, retCode)) {
1985             return nullptr;
1986         }
1987         napi_create_double(env, CameraNapiUtils::FloatToDouble(exposureValue), &result);
1988     } else {
1989         MEDIA_ERR_LOG("GetExposureValue call Failed!");
1990     }
1991     return result;
1992 }
1993 
SetExposureBias(napi_env env,napi_callback_info info)1994 napi_value CameraSessionNapi::SetExposureBias(napi_env env, napi_callback_info info)
1995 {
1996     MEDIA_DEBUG_LOG("SetExposureBias is called");
1997     CAMERA_SYNC_TRACE;
1998     napi_status status;
1999     napi_value result = nullptr;
2000     size_t argc = ARGS_ONE;
2001     napi_value argv[ARGS_ONE] = {0};
2002     napi_value thisVar = nullptr;
2003 
2004     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2005 
2006     napi_get_undefined(env, &result);
2007     CameraSessionNapi* cameraSessionNapi = nullptr;
2008     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2009     if (status == napi_ok && cameraSessionNapi != nullptr) {
2010         double exposureValue;
2011         napi_get_value_double(env, argv[PARAM0], &exposureValue);
2012         cameraSessionNapi->cameraSession_->LockForControl();
2013         int32_t retCode = cameraSessionNapi->cameraSession_->SetExposureBias((float)exposureValue);
2014         cameraSessionNapi->cameraSession_->UnlockForControl();
2015         if (!CameraNapiUtils::CheckError(env, retCode)) {
2016             return nullptr;
2017         }
2018     } else {
2019         MEDIA_ERR_LOG("SetExposureBias call Failed!");
2020     }
2021     return result;
2022 }
2023 
IsFocusModeSupported(napi_env env,napi_callback_info info)2024 napi_value CameraSessionNapi::IsFocusModeSupported(napi_env env, napi_callback_info info)
2025 {
2026     MEDIA_DEBUG_LOG("IsFocusModeSupported is called");
2027     napi_status status;
2028     napi_value result = nullptr;
2029     size_t argc = ARGS_ONE;
2030     napi_value argv[ARGS_ONE] = {0};
2031     napi_value thisVar = nullptr;
2032 
2033     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2034 
2035     napi_get_undefined(env, &result);
2036     CameraSessionNapi* cameraSessionNapi = nullptr;
2037     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2038     if (status == napi_ok && cameraSessionNapi != nullptr) {
2039         int32_t value;
2040         napi_get_value_int32(env, argv[PARAM0], &value);
2041         FocusMode focusMode = (FocusMode)value;
2042         bool isSupported;
2043         int32_t retCode = cameraSessionNapi->cameraSession_->IsFocusModeSupported(focusMode,
2044                                                                                   isSupported);
2045         if (!CameraNapiUtils::CheckError(env, retCode)) {
2046             return nullptr;
2047         }
2048         napi_get_boolean(env, isSupported, &result);
2049     } else {
2050         MEDIA_ERR_LOG("IsFocusModeSupported call Failed!");
2051     }
2052     return result;
2053 }
2054 
GetFocalLength(napi_env env,napi_callback_info info)2055 napi_value CameraSessionNapi::GetFocalLength(napi_env env, napi_callback_info info)
2056 {
2057     MEDIA_DEBUG_LOG("GetFocalLength is called");
2058     napi_status status;
2059     napi_value result = nullptr;
2060     size_t argc = ARGS_ZERO;
2061     napi_value argv[ARGS_ZERO];
2062     napi_value thisVar = nullptr;
2063 
2064     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2065 
2066     napi_get_undefined(env, &result);
2067     CameraSessionNapi* cameraSessionNapi = nullptr;
2068     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2069     if (status == napi_ok && cameraSessionNapi != nullptr) {
2070         float focalLength;
2071         int32_t retCode = cameraSessionNapi->cameraSession_->GetFocalLength(focalLength);
2072         if (!CameraNapiUtils::CheckError(env, retCode)) {
2073             return nullptr;
2074         }
2075         napi_create_double(env, CameraNapiUtils::FloatToDouble(focalLength), &result);
2076     } else {
2077         MEDIA_ERR_LOG("GetFocalLength call Failed!");
2078     }
2079     return result;
2080 }
2081 
SetFocusPoint(napi_env env,napi_callback_info info)2082 napi_value CameraSessionNapi::SetFocusPoint(napi_env env, napi_callback_info info)
2083 {
2084     MEDIA_DEBUG_LOG("SetFocusPoint is called");
2085     CAMERA_SYNC_TRACE;
2086     napi_status status;
2087     napi_value result = nullptr;
2088     size_t argc = ARGS_ONE;
2089     napi_value argv[ARGS_ONE] = {0};
2090     napi_value thisVar = nullptr;
2091 
2092     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2093 
2094     napi_get_undefined(env, &result);
2095     CameraSessionNapi* cameraSessionNapi = nullptr;
2096     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2097     if (status == napi_ok && cameraSessionNapi != nullptr) {
2098         Point focusPoint;
2099         if (GetPointProperties(env, argv[PARAM0], focusPoint) == 0) {
2100             cameraSessionNapi->cameraSession_->LockForControl();
2101             int32_t retCode = cameraSessionNapi->cameraSession_->SetFocusPoint(focusPoint);
2102             cameraSessionNapi->cameraSession_->UnlockForControl();
2103             if (!CameraNapiUtils::CheckError(env, retCode)) {
2104                 return nullptr;
2105             }
2106         } else {
2107             MEDIA_ERR_LOG("get point failed");
2108         }
2109     } else {
2110         MEDIA_ERR_LOG("SetFocusPoint call Failed!");
2111     }
2112     return result;
2113 }
2114 
GetFocusPoint(napi_env env,napi_callback_info info)2115 napi_value CameraSessionNapi::GetFocusPoint(napi_env env, napi_callback_info info)
2116 {
2117     MEDIA_DEBUG_LOG("GetFocusPoint is called");
2118     napi_status status;
2119     napi_value result = nullptr;
2120     size_t argc = ARGS_ZERO;
2121     napi_value argv[ARGS_ZERO];
2122     napi_value thisVar = nullptr;
2123 
2124     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2125 
2126     napi_get_undefined(env, &result);
2127     CameraSessionNapi* cameraSessionNapi = nullptr;
2128     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2129     if (status == napi_ok && cameraSessionNapi != nullptr) {
2130         Point focusPoint;
2131         int32_t retCode = cameraSessionNapi->cameraSession_->GetFocusPoint(focusPoint);
2132         if (!CameraNapiUtils::CheckError(env, retCode)) {
2133             return nullptr;
2134         }
2135         return GetPointNapiValue(env, focusPoint);
2136     } else {
2137         MEDIA_ERR_LOG("GetFocusPoint call Failed!");
2138     }
2139     return result;
2140 }
2141 
GetFocusMode(napi_env env,napi_callback_info info)2142 napi_value CameraSessionNapi::GetFocusMode(napi_env env, napi_callback_info info)
2143 {
2144     MEDIA_DEBUG_LOG("GetFocusMode is called");
2145     napi_status status;
2146     napi_value result = nullptr;
2147     size_t argc = ARGS_ZERO;
2148     napi_value argv[ARGS_ZERO];
2149     napi_value thisVar = nullptr;
2150 
2151     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2152 
2153     napi_get_undefined(env, &result);
2154     CameraSessionNapi* cameraSessionNapi = nullptr;
2155     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2156     if (status == napi_ok && cameraSessionNapi != nullptr) {
2157         FocusMode focusMode;
2158         int32_t retCode = cameraSessionNapi->cameraSession_->GetFocusMode(focusMode);
2159         if (!CameraNapiUtils::CheckError(env, retCode)) {
2160             return nullptr;
2161         }
2162         napi_create_int32(env, focusMode, &result);
2163     } else {
2164         MEDIA_ERR_LOG("GetFocusMode call Failed!");
2165     }
2166     return result;
2167 }
2168 
SetFocusMode(napi_env env,napi_callback_info info)2169 napi_value CameraSessionNapi::SetFocusMode(napi_env env, napi_callback_info info)
2170 {
2171     MEDIA_DEBUG_LOG("SetFocusMode is called");
2172     CAMERA_SYNC_TRACE;
2173     napi_status status;
2174     napi_value result = nullptr;
2175     size_t argc = ARGS_ONE;
2176     napi_value argv[ARGS_ONE] = {0};
2177     napi_value thisVar = nullptr;
2178 
2179     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2180 
2181     napi_get_undefined(env, &result);
2182     CameraSessionNapi* cameraSessionNapi = nullptr;
2183     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2184     if (status == napi_ok && cameraSessionNapi != nullptr) {
2185         int32_t value;
2186         napi_get_value_int32(env, argv[PARAM0], &value);
2187         FocusMode focusMode = (FocusMode)value;
2188         cameraSessionNapi->cameraSession_->LockForControl();
2189         int retCode = cameraSessionNapi->cameraSession_->
2190                 SetFocusMode(static_cast<FocusMode>(focusMode));
2191         cameraSessionNapi->cameraSession_->UnlockForControl();
2192         if (!CameraNapiUtils::CheckError(env, retCode)) {
2193             return nullptr;
2194         }
2195     } else {
2196         MEDIA_ERR_LOG("SetFocusMode call Failed!");
2197     }
2198     return result;
2199 }
2200 
GetZoomRatioRange(napi_env env,napi_callback_info info)2201 napi_value CameraSessionNapi::GetZoomRatioRange(napi_env env, napi_callback_info info)
2202 {
2203     MEDIA_DEBUG_LOG("GetZoomRatioRange is called");
2204     napi_status status;
2205     napi_value result = nullptr;
2206     size_t argc = ARGS_ZERO;
2207     napi_value argv[ARGS_ZERO];
2208     napi_value thisVar = nullptr;
2209 
2210     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2211 
2212     napi_get_undefined(env, &result);
2213 
2214     CameraSessionNapi* cameraSessionNapi = nullptr;
2215     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2216     if (status == napi_ok && cameraSessionNapi != nullptr) {
2217         std::vector<float> vecZoomRatioList;
2218         int32_t retCode = cameraSessionNapi->cameraSession_->GetZoomRatioRange(vecZoomRatioList);
2219         if (!CameraNapiUtils::CheckError(env, retCode)) {
2220             return nullptr;
2221         }
2222         MEDIA_INFO_LOG("CameraSessionNapi::GetZoomRatioRange len = %{public}zu",
2223             vecZoomRatioList.size());
2224 
2225         if (!vecZoomRatioList.empty() && napi_create_array(env, &result) == napi_ok) {
2226             for (size_t i = 0; i < vecZoomRatioList.size(); i++) {
2227                 float zoomRatio = vecZoomRatioList[i];
2228                 napi_value value;
2229                 napi_create_double(env, CameraNapiUtils::FloatToDouble(zoomRatio), &value);
2230                 napi_set_element(env, result, i, value);
2231             }
2232         } else {
2233             MEDIA_ERR_LOG("vecSupportedZoomRatioList is empty or failed to create array!");
2234         }
2235     } else {
2236         MEDIA_ERR_LOG("GetZoomRatioRange call Failed!");
2237     }
2238     return result;
2239 }
2240 
GetZoomRatio(napi_env env,napi_callback_info info)2241 napi_value CameraSessionNapi::GetZoomRatio(napi_env env, napi_callback_info info)
2242 {
2243     MEDIA_DEBUG_LOG("GetZoomRatio is called");
2244     napi_status status;
2245     napi_value result = nullptr;
2246     size_t argc = ARGS_ZERO;
2247     napi_value argv[ARGS_ZERO];
2248     napi_value thisVar = nullptr;
2249 
2250     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2251 
2252     napi_get_undefined(env, &result);
2253     CameraSessionNapi* cameraSessionNapi = nullptr;
2254     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2255     if (status == napi_ok && cameraSessionNapi != nullptr) {
2256         float zoomRatio;
2257         int32_t retCode = cameraSessionNapi->cameraSession_->GetZoomRatio(zoomRatio);
2258         if (!CameraNapiUtils::CheckError(env, retCode)) {
2259             return nullptr;
2260         }
2261         napi_create_double(env, CameraNapiUtils::FloatToDouble(zoomRatio), &result);
2262     } else {
2263         MEDIA_ERR_LOG("GetZoomRatio call Failed!");
2264     }
2265     return result;
2266 }
2267 
SetZoomRatio(napi_env env,napi_callback_info info)2268 napi_value CameraSessionNapi::SetZoomRatio(napi_env env, napi_callback_info info)
2269 {
2270     MEDIA_DEBUG_LOG("SetZoomRatio is called");
2271     CAMERA_SYNC_TRACE;
2272     napi_status status;
2273     napi_value result = nullptr;
2274 
2275     size_t argc = ARGS_ONE;
2276     napi_value argv[ARGS_ONE];
2277     napi_value thisVar = nullptr;
2278 
2279     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2280 
2281     napi_get_undefined(env, &result);
2282     CameraSessionNapi* cameraSessionNapi = nullptr;
2283     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2284     if (status == napi_ok && cameraSessionNapi != nullptr) {
2285         double zoomRatio;
2286         napi_get_value_double(env, argv[PARAM0], &zoomRatio);
2287         cameraSessionNapi->cameraSession_->LockForControl();
2288         int32_t retCode = cameraSessionNapi->cameraSession_->SetZoomRatio((float)zoomRatio);
2289         cameraSessionNapi->cameraSession_->UnlockForControl();
2290         if (!CameraNapiUtils::CheckError(env, retCode)) {
2291             return nullptr;
2292         }
2293     } else {
2294         MEDIA_ERR_LOG("SetZoomRatio call Failed!");
2295     }
2296     return result;
2297 }
2298 
PrepareZoom(napi_env env,napi_callback_info info)2299 napi_value CameraSessionNapi::PrepareZoom(napi_env env, napi_callback_info info)
2300 {
2301     MEDIA_DEBUG_LOG("PrepareZoom is called");
2302     CAMERA_SYNC_TRACE;
2303     napi_status status;
2304     napi_value result = nullptr;
2305 
2306     size_t argc = ARGS_ZERO;
2307     napi_value argv[ARGS_ZERO];
2308     napi_value thisVar = nullptr;
2309 
2310     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2311 
2312     napi_get_undefined(env, &result);
2313     CameraSessionNapi* cameraSessionNapi = nullptr;
2314     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2315     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2316         cameraSessionNapi->cameraSession_->LockForControl();
2317         int32_t retCode = cameraSessionNapi->cameraSession_->PrepareZoom();
2318         cameraSessionNapi->cameraSession_->UnlockForControl();
2319         if (!CameraNapiUtils::CheckError(env, retCode)) {
2320             return nullptr;
2321         }
2322     } else {
2323         MEDIA_ERR_LOG("PrepareZoom call Failed!");
2324     }
2325     return result;
2326 }
2327 
UnPrepareZoom(napi_env env,napi_callback_info info)2328 napi_value CameraSessionNapi::UnPrepareZoom(napi_env env, napi_callback_info info)
2329 {
2330     MEDIA_DEBUG_LOG("PrepareZoom is called");
2331     CAMERA_SYNC_TRACE;
2332     napi_status status;
2333     napi_value result = nullptr;
2334 
2335     size_t argc = ARGS_ZERO;
2336     napi_value argv[ARGS_ZERO];
2337     napi_value thisVar = nullptr;
2338 
2339     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2340 
2341     napi_get_undefined(env, &result);
2342     CameraSessionNapi* cameraSessionNapi = nullptr;
2343     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2344     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2345         cameraSessionNapi->cameraSession_->LockForControl();
2346         int32_t retCode = cameraSessionNapi->cameraSession_->UnPrepareZoom();
2347         cameraSessionNapi->cameraSession_->UnlockForControl();
2348         if (!CameraNapiUtils::CheckError(env, retCode)) {
2349             return nullptr;
2350         }
2351     } else {
2352         MEDIA_ERR_LOG("PrepareZoom call Failed!");
2353     }
2354     return result;
2355 }
2356 
SetSmoothZoom(napi_env env,napi_callback_info info)2357 napi_value CameraSessionNapi::SetSmoothZoom(napi_env env, napi_callback_info info)
2358 {
2359     MEDIA_DEBUG_LOG("SetSmoothZoom is called");
2360     CAMERA_SYNC_TRACE;
2361     napi_status status;
2362     napi_value result = nullptr;
2363 
2364     size_t argc = ARGS_TWO;
2365     napi_value argv[ARGS_TWO];
2366     napi_value thisVar = nullptr;
2367 
2368     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2369 
2370     napi_get_undefined(env, &result);
2371     CameraSessionNapi* cameraSessionNapi = nullptr;
2372     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2373     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2374         double targetZoomRatio;
2375         int32_t smoothZoomType;
2376         napi_get_value_double(env, argv[PARAM0], &targetZoomRatio);
2377         napi_get_value_int32(env, argv[PARAM1], &smoothZoomType);
2378         cameraSessionNapi->cameraSession_->SetSmoothZoom((float)targetZoomRatio, smoothZoomType);
2379     } else {
2380         MEDIA_ERR_LOG("SetSmoothZoom call Failed!");
2381     }
2382     return result;
2383 }
2384 
GetZoomPointInfos(napi_env env,napi_callback_info info)2385 napi_value CameraSessionNapi::GetZoomPointInfos(napi_env env, napi_callback_info info)
2386 {
2387     if (!CameraNapiSecurity::CheckSystemApp(env)) {
2388         MEDIA_ERR_LOG("SystemApi GetZoomPointInfos is called!");
2389         return nullptr;
2390     }
2391     MEDIA_DEBUG_LOG("GetZoomPointInfos is called");
2392     napi_status status;
2393     napi_value result = nullptr;
2394     size_t argc = ARGS_ZERO;
2395     napi_value argv[ARGS_ZERO];
2396     napi_value thisVar = nullptr;
2397 
2398     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2399 
2400     napi_get_undefined(env, &result);
2401 
2402     CameraSessionNapi* cameraSessionNapi = nullptr;
2403     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2404     if (status == napi_ok && cameraSessionNapi != nullptr) {
2405         std::vector<ZoomPointInfo> vecZoomPointInfoList;
2406         int32_t retCode = cameraSessionNapi->cameraSession_->GetZoomPointInfos(vecZoomPointInfoList);
2407         if (!CameraNapiUtils::CheckError(env, retCode)) {
2408             return nullptr;
2409         }
2410         MEDIA_INFO_LOG("CameraSessionNapi::GetZoomPointInfos len = %{public}zu",
2411             vecZoomPointInfoList.size());
2412 
2413         if (!vecZoomPointInfoList.empty() && napi_create_array(env, &result) == napi_ok) {
2414             for (size_t i = 0; i < vecZoomPointInfoList.size(); i++) {
2415                 ZoomPointInfo zoomPointInfo = vecZoomPointInfoList[i];
2416                 napi_value value;
2417                 napi_value zoomRatio;
2418                 napi_value equivalentFocus;
2419                 napi_create_object(env, &value);
2420                 napi_create_double(env, CameraNapiUtils::FloatToDouble(zoomPointInfo.zoomRatio), &zoomRatio);
2421                 napi_set_named_property(env, value, "zoomRatio", zoomRatio);
2422                 napi_create_double(env, zoomPointInfo.equivalentFocalLength, &equivalentFocus);
2423                 napi_set_named_property(env, value, "equivalentFocalLength", equivalentFocus);
2424                 napi_set_element(env, result, i, value);
2425             }
2426         } else {
2427             MEDIA_ERR_LOG("vecSupportedZoomRatioList is empty or failed to create array!");
2428         }
2429     } else {
2430         MEDIA_ERR_LOG("GetZoomPointInfos call Failed!");
2431     }
2432     return result;
2433 }
2434 
GetSupportedFilters(napi_env env,napi_callback_info info)2435 napi_value CameraSessionNapi::GetSupportedFilters(napi_env env, napi_callback_info info)
2436 {
2437     MEDIA_DEBUG_LOG("getSupportedFilters is called");
2438     napi_status status;
2439     napi_value result = nullptr;
2440     size_t argc = ARGS_ZERO;
2441     napi_value argv[ARGS_ZERO];
2442     napi_value thisVar = nullptr;
2443 
2444     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2445 
2446     napi_get_undefined(env, &result);
2447     status = napi_create_array(env, &result);
2448     if (status != napi_ok) {
2449         MEDIA_ERR_LOG("napi_create_array call Failed!");
2450         return result;
2451     }
2452     CameraSessionNapi* cameraSessionNapi = nullptr;
2453     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2454     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2455         std::vector<FilterType> filterTypes = cameraSessionNapi->cameraSession_->GetSupportedFilters();
2456         MEDIA_INFO_LOG("CameraSessionNapi::GetSupportedFilters len = %{public}zu",
2457             filterTypes.size());
2458         if (!filterTypes.empty()) {
2459             for (size_t i = 0; i < filterTypes.size(); i++) {
2460                 FilterType filterType = filterTypes[i];
2461                 napi_value value;
2462                 napi_create_int32(env, filterType, &value);
2463                 napi_set_element(env, result, i, value);
2464             }
2465         }
2466     } else {
2467         MEDIA_ERR_LOG("GetSupportedFilters call Failed!");
2468     }
2469     return result;
2470 }
GetFilter(napi_env env,napi_callback_info info)2471 napi_value CameraSessionNapi::GetFilter(napi_env env, napi_callback_info info)
2472 {
2473     MEDIA_DEBUG_LOG("GetFilter is called");
2474     napi_status status;
2475     napi_value result = nullptr;
2476     size_t argc = ARGS_ZERO;
2477     napi_value argv[ARGS_ZERO];
2478     napi_value thisVar = nullptr;
2479 
2480     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2481 
2482     napi_get_undefined(env, &result);
2483     CameraSessionNapi* cameraSessionNapi = nullptr;
2484     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2485     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2486         FilterType filterType = cameraSessionNapi->cameraSession_->GetFilter();
2487         napi_create_int32(env, filterType, &result);
2488     } else {
2489         MEDIA_ERR_LOG("GetFilter call Failed!");
2490     }
2491     return result;
2492 }
SetFilter(napi_env env,napi_callback_info info)2493 napi_value CameraSessionNapi::SetFilter(napi_env env, napi_callback_info info)
2494 {
2495     MEDIA_DEBUG_LOG("setFilter is called");
2496     CAMERA_SYNC_TRACE;
2497     napi_status status;
2498     napi_value result = nullptr;
2499     size_t argc = ARGS_ONE;
2500     napi_value argv[ARGS_ONE] = {0};
2501     napi_value thisVar = nullptr;
2502 
2503     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2504 
2505     napi_get_undefined(env, &result);
2506     CameraSessionNapi* cameraSessionNapi = nullptr;
2507     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2508     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2509         int32_t value;
2510         napi_get_value_int32(env, argv[PARAM0], &value);
2511         FilterType filterType = (FilterType)value;
2512         cameraSessionNapi->cameraSession_->LockForControl();
2513         cameraSessionNapi->cameraSession_->
2514                 SetFilter(static_cast<FilterType>(filterType));
2515         cameraSessionNapi->cameraSession_->UnlockForControl();
2516     } else {
2517         MEDIA_ERR_LOG("SetFilter call Failed!");
2518     }
2519     return result;
2520 }
2521 
GetSupportedBeautyTypes(napi_env env,napi_callback_info info)2522 napi_value CameraSessionNapi::GetSupportedBeautyTypes(napi_env env, napi_callback_info info)
2523 {
2524     if (!CameraNapiSecurity::CheckSystemApp(env)) {
2525         MEDIA_ERR_LOG("SystemApi GetSupportedBeautyTypes is called!");
2526         return nullptr;
2527     }
2528     MEDIA_DEBUG_LOG("GetSupportedBeautyTypes is called");
2529     napi_status status;
2530     napi_value result = nullptr;
2531     size_t argc = ARGS_ZERO;
2532     napi_value argv[ARGS_ZERO];
2533     napi_value thisVar = nullptr;
2534 
2535     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2536 
2537     napi_get_undefined(env, &result);
2538     status = napi_create_array(env, &result);
2539     if (status != napi_ok) {
2540         MEDIA_ERR_LOG("napi_create_array call Failed!");
2541         return result;
2542     }
2543     CameraSessionNapi* cameraSessionNapi = nullptr;
2544     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2545     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2546         std::vector<BeautyType> beautyTypes = cameraSessionNapi->cameraSession_->GetSupportedBeautyTypes();
2547         MEDIA_INFO_LOG("CameraSessionNapi::GetSupportedBeautyTypes len = %{public}zu",
2548             beautyTypes.size());
2549         if (!beautyTypes.empty() && status == napi_ok) {
2550             for (size_t i = 0; i < beautyTypes.size(); i++) {
2551                 BeautyType beautyType = beautyTypes[i];
2552                 napi_value value;
2553                 napi_create_int32(env, beautyType, &value);
2554                 napi_set_element(env, result, i, value);
2555             }
2556         }
2557     } else {
2558         MEDIA_ERR_LOG("GetSupportedBeautyTypes call Failed!");
2559     }
2560     return result;
2561 }
2562 
GetSupportedBeautyRange(napi_env env,napi_callback_info info)2563 napi_value CameraSessionNapi::GetSupportedBeautyRange(napi_env env, napi_callback_info info)
2564 {
2565     if (!CameraNapiSecurity::CheckSystemApp(env)) {
2566         MEDIA_ERR_LOG("SystemApi GetSupportedBeautyRange is called!");
2567         return nullptr;
2568     }
2569     MEDIA_DEBUG_LOG("GetSupportedBeautyRange is called");
2570     napi_status status;
2571     napi_value result = nullptr;
2572     size_t argc = ARGS_ONE;
2573     napi_value argv[ARGS_ONE];
2574     napi_value thisVar = nullptr;
2575 
2576     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2577 
2578     napi_get_undefined(env, &result);
2579     status = napi_create_array(env, &result);
2580     if (status != napi_ok) {
2581         MEDIA_ERR_LOG("napi_create_array call Failed!");
2582         return result;
2583     }
2584     CameraSessionNapi* cameraSessionNapi = nullptr;
2585     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2586     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2587         int32_t beautyType;
2588         napi_get_value_int32(env, argv[PARAM0], &beautyType);
2589         std::vector<int32_t> beautyRanges =
2590             cameraSessionNapi->cameraSession_->GetSupportedBeautyRange(static_cast<BeautyType>(beautyType));
2591         MEDIA_INFO_LOG("CameraSessionNapi::GetSupportedBeautyRange beautyType = %{public}d, len = %{public}zu",
2592                        beautyType, beautyRanges.size());
2593         if (!beautyRanges.empty()) {
2594             for (size_t i = 0; i < beautyRanges.size(); i++) {
2595                 int beautyRange = beautyRanges[i];
2596                 napi_value value;
2597                 napi_create_int32(env, beautyRange, &value);
2598                 napi_set_element(env, result, i, value);
2599             }
2600         }
2601     } else {
2602         MEDIA_ERR_LOG("GetSupportedBeautyRange call Failed!");
2603     }
2604     return result;
2605 }
2606 
GetBeauty(napi_env env,napi_callback_info info)2607 napi_value CameraSessionNapi::GetBeauty(napi_env env, napi_callback_info info)
2608 {
2609     if (!CameraNapiSecurity::CheckSystemApp(env)) {
2610         MEDIA_ERR_LOG("SystemApi GetBeauty is called!");
2611         return nullptr;
2612     }
2613     MEDIA_DEBUG_LOG("GetBeauty is called");
2614     napi_status status;
2615     napi_value result = nullptr;
2616     size_t argc = ARGS_ONE;
2617     napi_value argv[ARGS_ONE];
2618     napi_value thisVar = nullptr;
2619 
2620     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2621 
2622     napi_get_undefined(env, &result);
2623     CameraSessionNapi* cameraSessionNapi = nullptr;
2624     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2625     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2626         int32_t beautyType;
2627         napi_get_value_int32(env, argv[PARAM0], &beautyType);
2628         int32_t beautyStrength = cameraSessionNapi->cameraSession_->GetBeauty(static_cast<BeautyType>(beautyType));
2629         napi_create_int32(env, beautyStrength, &result);
2630     } else {
2631         MEDIA_ERR_LOG("GetBeauty call Failed!");
2632     }
2633     return result;
2634 }
2635 
SetBeauty(napi_env env,napi_callback_info info)2636 napi_value CameraSessionNapi::SetBeauty(napi_env env, napi_callback_info info)
2637 {
2638     if (!CameraNapiSecurity::CheckSystemApp(env)) {
2639         MEDIA_ERR_LOG("SystemApi SetBeauty is called!");
2640         return nullptr;
2641     }
2642     MEDIA_DEBUG_LOG("SetBeauty is called");
2643     napi_status status;
2644     napi_value result = nullptr;
2645     size_t argc = ARGS_TWO;
2646     napi_value argv[ARGS_TWO];
2647     napi_value thisVar = nullptr;
2648 
2649     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2650 
2651     napi_get_undefined(env, &result);
2652     CameraSessionNapi* cameraSessionNapi = nullptr;
2653     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2654     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2655         int32_t beautyType;
2656         napi_get_value_int32(env, argv[PARAM0], &beautyType);
2657         int32_t beautyStrength;
2658         napi_get_value_int32(env, argv[PARAM1], &beautyStrength);
2659         cameraSessionNapi->cameraSession_->LockForControl();
2660         cameraSessionNapi->cameraSession_->SetBeauty(static_cast<BeautyType>(beautyType), beautyStrength);
2661         cameraSessionNapi->cameraSession_->UnlockForControl();
2662     } else {
2663         MEDIA_ERR_LOG("SetBeauty call Failed!");
2664     }
2665     return result;
2666 }
2667 
GetSupportedColorSpaces(napi_env env,napi_callback_info info)2668 napi_value CameraSessionNapi::GetSupportedColorSpaces(napi_env env, napi_callback_info info)
2669 {
2670     MEDIA_DEBUG_LOG("GetSupportedColorSpaces is called.");
2671     napi_status status;
2672     napi_value result = nullptr;
2673     size_t argc = ARGS_ZERO;
2674     napi_value argv[ARGS_ZERO];
2675     napi_value thisVar = nullptr;
2676 
2677     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2678 
2679     napi_get_undefined(env, &result);
2680     status = napi_create_array(env, &result);
2681     if (status != napi_ok) {
2682         MEDIA_ERR_LOG("napi_create_array call Failed!");
2683         return result;
2684     }
2685     CameraSessionNapi* cameraSessionNapi = nullptr;
2686     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2687     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2688         std::vector<ColorSpace> colorSpaces = cameraSessionNapi->cameraSession_->GetSupportedColorSpaces();
2689         if (!colorSpaces.empty()) {
2690             for (size_t i = 0; i < colorSpaces.size(); i++) {
2691                 int colorSpace = colorSpaces[i];
2692                 napi_value value;
2693                 napi_create_int32(env, colorSpace, &value);
2694                 napi_set_element(env, result, i, value);
2695             }
2696         }
2697     } else {
2698         MEDIA_ERR_LOG("GetSupportedColorSpaces call Failed!");
2699     }
2700     return result;
2701 }
2702 
GetActiveColorSpace(napi_env env,napi_callback_info info)2703 napi_value CameraSessionNapi::GetActiveColorSpace(napi_env env, napi_callback_info info)
2704 {
2705     MEDIA_DEBUG_LOG("GetActiveColorSpace is called");
2706     napi_status status;
2707     napi_value result = nullptr;
2708     size_t argc = ARGS_ZERO;
2709     napi_value argv[ARGS_ZERO];
2710     napi_value thisVar = nullptr;
2711 
2712     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2713 
2714     napi_get_undefined(env, &result);
2715     CameraSessionNapi* cameraSessionNapi = nullptr;
2716     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2717     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2718         ColorSpace colorSpace;
2719         int32_t retCode = cameraSessionNapi->cameraSession_->GetActiveColorSpace(colorSpace);
2720         if (!CameraNapiUtils::CheckError(env, retCode)) {
2721             return result;
2722         }
2723         napi_create_int32(env, colorSpace, &result);
2724     } else {
2725         MEDIA_ERR_LOG("GetActiveColorSpace call Failed!");
2726     }
2727     return result;
2728 }
2729 
SetColorSpace(napi_env env,napi_callback_info info)2730 napi_value CameraSessionNapi::SetColorSpace(napi_env env, napi_callback_info info)
2731 {
2732     MEDIA_DEBUG_LOG("SetColorSpace is called");
2733     CAMERA_SYNC_TRACE;
2734     napi_status status;
2735     napi_value result = nullptr;
2736     size_t argc = ARGS_ONE;
2737     napi_value argv[ARGS_ONE] = {0};
2738     napi_value thisVar = nullptr;
2739 
2740     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2741 
2742     napi_get_undefined(env, &result);
2743     CameraSessionNapi* cameraSessionNapi = nullptr;
2744     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2745     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2746         int32_t colorSpaceNumber;
2747         napi_get_value_int32(env, argv[PARAM0], &colorSpaceNumber);
2748         ColorSpace colorSpace = (ColorSpace)colorSpaceNumber;
2749         int32_t retCode = cameraSessionNapi->cameraSession_->SetColorSpace(colorSpace);
2750         if (!CameraNapiUtils::CheckError(env, retCode)) {
2751             return result;
2752         }
2753     } else {
2754         MEDIA_ERR_LOG("SetColorSpace call Failed!");
2755     }
2756     return result;
2757 }
2758 
GetSupportedColorEffects(napi_env env,napi_callback_info info)2759 napi_value CameraSessionNapi::GetSupportedColorEffects(napi_env env, napi_callback_info info)
2760 {
2761     MEDIA_DEBUG_LOG("GetSupportedColorEffects is called");
2762     napi_status status;
2763     napi_value result = nullptr;
2764     size_t argc = ARGS_ZERO;
2765     napi_value argv[ARGS_ZERO];
2766     napi_value thisVar = nullptr;
2767 
2768     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2769 
2770     napi_get_undefined(env, &result);
2771     status = napi_create_array(env, &result);
2772     if (status != napi_ok) {
2773         MEDIA_ERR_LOG("napi_create_array call Failed!");
2774         return result;
2775     }
2776     CameraSessionNapi* cameraSessionNapi = nullptr;
2777     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2778     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2779         std::vector<ColorEffect> colorEffects = cameraSessionNapi->cameraSession_->GetSupportedColorEffects();
2780         if (!colorEffects.empty()) {
2781             for (size_t i = 0; i < colorEffects.size(); i++) {
2782                 int colorEffect = colorEffects[i];
2783                 napi_value value;
2784                 napi_create_int32(env, colorEffect, &value);
2785                 napi_set_element(env, result, i, value);
2786             }
2787         }
2788     } else {
2789         MEDIA_ERR_LOG("GetSupportedColorEffects call Failed!");
2790     }
2791     return result;
2792 }
2793 
GetColorEffect(napi_env env,napi_callback_info info)2794 napi_value CameraSessionNapi::GetColorEffect(napi_env env, napi_callback_info info)
2795 {
2796     MEDIA_DEBUG_LOG("GetColorEffect is called");
2797     napi_status status;
2798     napi_value result = nullptr;
2799     size_t argc = ARGS_ZERO;
2800     napi_value argv[ARGS_ZERO];
2801     napi_value thisVar = nullptr;
2802 
2803     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2804 
2805     napi_get_undefined(env, &result);
2806     CameraSessionNapi* cameraSessionNapi = nullptr;
2807     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2808     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2809         ColorEffect colorEffect = cameraSessionNapi->cameraSession_->GetColorEffect();
2810         napi_create_int32(env, colorEffect, &result);
2811     } else {
2812         MEDIA_ERR_LOG("GetColorEffect call Failed!");
2813     }
2814     return result;
2815 }
2816 
SetColorEffect(napi_env env,napi_callback_info info)2817 napi_value CameraSessionNapi::SetColorEffect(napi_env env, napi_callback_info info)
2818 {
2819     MEDIA_DEBUG_LOG("SetColorEffect is called");
2820     CAMERA_SYNC_TRACE;
2821     napi_status status;
2822     napi_value result = nullptr;
2823     size_t argc = ARGS_ONE;
2824     napi_value argv[ARGS_ONE] = {0};
2825     napi_value thisVar = nullptr;
2826 
2827     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2828 
2829     napi_get_undefined(env, &result);
2830     CameraSessionNapi* cameraSessionNapi = nullptr;
2831     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2832     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2833         int32_t colorEffectNumber;
2834         napi_get_value_int32(env, argv[PARAM0], &colorEffectNumber);
2835         ColorEffect colorEffect = (ColorEffect)colorEffectNumber;
2836         cameraSessionNapi->cameraSession_->LockForControl();
2837         cameraSessionNapi->cameraSession_->SetColorEffect(static_cast<ColorEffect>(colorEffect));
2838         cameraSessionNapi->cameraSession_->UnlockForControl();
2839     } else {
2840         MEDIA_ERR_LOG("SetColorEffect call Failed!");
2841     }
2842     return result;
2843 }
2844 
GetFocusDistance(napi_env env,napi_callback_info info)2845 napi_value CameraSessionNapi::GetFocusDistance(napi_env env, napi_callback_info info)
2846 {
2847     MEDIA_DEBUG_LOG("GetFocusDistance is called");
2848     napi_status status;
2849     napi_value result = nullptr;
2850     size_t argc = ARGS_ZERO;
2851     napi_value argv[ARGS_ZERO];
2852     napi_value thisVar = nullptr;
2853 
2854     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2855 
2856     napi_get_undefined(env, &result);
2857     CameraSessionNapi* cameraSessionNapi = nullptr;
2858     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2859     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2860         float distance;
2861         int32_t retCode = cameraSessionNapi->cameraSession_->GetFocusDistance(distance);
2862         if (!CameraNapiUtils::CheckError(env, retCode)) {
2863             return nullptr;
2864         }
2865         napi_create_double(env, distance, &result);
2866     } else {
2867         MEDIA_ERR_LOG("GetFocusDistance call Failed!");
2868     }
2869     return result;
2870 }
2871 
SetFocusDistance(napi_env env,napi_callback_info info)2872 napi_value CameraSessionNapi::SetFocusDistance(napi_env env, napi_callback_info info)
2873 {
2874     MEDIA_DEBUG_LOG("SetFocusDistance is called");
2875     CAMERA_SYNC_TRACE;
2876     napi_status status;
2877     napi_value result = nullptr;
2878     size_t argc = ARGS_ONE;
2879     napi_value argv[ARGS_ONE] = {0};
2880     napi_value thisVar = nullptr;
2881 
2882     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
2883 
2884     napi_get_undefined(env, &result);
2885     CameraSessionNapi* cameraSessionNapi = nullptr;
2886     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
2887     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
2888         double value;
2889         napi_get_value_double(env, argv[PARAM0], &value);
2890         float distance = static_cast<float>(value);
2891         cameraSessionNapi->cameraSession_->LockForControl();
2892         cameraSessionNapi->cameraSession_->SetFocusDistance(distance);
2893         MEDIA_INFO_LOG("CameraSessionNapi::SetFocusDistance set focusDistance:%{public}f!", distance);
2894         cameraSessionNapi->cameraSession_->UnlockForControl();
2895     } else {
2896         MEDIA_ERR_LOG("SetFocusDistance call Failed!");
2897     }
2898     return result;
2899 }
2900 
IsMacroSupported(napi_env env,napi_callback_info info)2901 napi_value CameraSessionNapi::IsMacroSupported(napi_env env, napi_callback_info info)
2902 {
2903     if (!CameraNapiSecurity::CheckSystemApp(env)) {
2904         MEDIA_ERR_LOG("SystemApi IsMacroSupported is called!");
2905         return nullptr;
2906     }
2907     MEDIA_DEBUG_LOG("CameraSessionNapi::IsMacroSupported is called");
2908     CameraSessionNapi* cameraSessionNapi = nullptr;
2909     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
2910     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
2911         MEDIA_ERR_LOG("CameraSessionNapi::IsMacroSupported parse parameter occur error");
2912         return nullptr;
2913     }
2914     auto result = CameraNapiUtils::GetUndefinedValue(env);
2915     if (cameraSessionNapi->cameraSession_ != nullptr) {
2916         bool isSupported = cameraSessionNapi->cameraSession_->IsMacroSupported();
2917         napi_get_boolean(env, isSupported, &result);
2918     } else {
2919         MEDIA_ERR_LOG("CameraSessionNapi::IsMacroSupported get native object fail");
2920         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
2921         return nullptr;
2922     }
2923     return result;
2924 }
2925 
EnableMacro(napi_env env,napi_callback_info info)2926 napi_value CameraSessionNapi::EnableMacro(napi_env env, napi_callback_info info)
2927 {
2928     if (!CameraNapiSecurity::CheckSystemApp(env)) {
2929         MEDIA_ERR_LOG("SystemApi EnableMacro is called!");
2930         return nullptr;
2931     }
2932     MEDIA_DEBUG_LOG("CameraSessionNapi::EnableMacro is called");
2933     bool isEnableMacro;
2934     CameraSessionNapi* cameraSessionNapi = nullptr;
2935     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, isEnableMacro);
2936     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
2937         MEDIA_ERR_LOG("CameraSessionNapi::EnableMacro parse parameter occur error");
2938         return nullptr;
2939     }
2940 
2941     if (cameraSessionNapi->cameraSession_ != nullptr) {
2942         MEDIA_INFO_LOG("CameraSessionNapi::EnableMacro:%{public}d", isEnableMacro);
2943         cameraSessionNapi->cameraSession_->LockForControl();
2944         int32_t retCode = cameraSessionNapi->cameraSession_->EnableMacro(isEnableMacro);
2945         cameraSessionNapi->cameraSession_->UnlockForControl();
2946         if (!CameraNapiUtils::CheckError(env, retCode)) {
2947             MEDIA_ERR_LOG("CameraSessionNapi::EnableMacro fail %{public}d", retCode);
2948             return nullptr;
2949         }
2950     } else {
2951         MEDIA_ERR_LOG("CameraSessionNapi::EnableMacro get native object fail");
2952         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
2953         return nullptr;
2954     }
2955     return CameraNapiUtils::GetUndefinedValue(env);
2956 }
2957 
IsDepthFusionSupported(napi_env env,napi_callback_info info)2958 napi_value CameraSessionNapi::IsDepthFusionSupported(napi_env env, napi_callback_info info)
2959 {
2960     if (!CameraNapiSecurity::CheckSystemApp(env)) {
2961         MEDIA_ERR_LOG("SystemApi GetDepthFusionThreshold is called!");
2962         return nullptr;
2963     }
2964     MEDIA_DEBUG_LOG("CameraSessionNapi::IsDepthFusionSupported is called");
2965     CameraSessionNapi* cameraSessionNapi = nullptr;
2966     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
2967     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
2968         MEDIA_ERR_LOG("CameraSessionNapi::IsDepthFusionSupported parse parameter occur error");
2969         return nullptr;
2970     }
2971     auto result = CameraNapiUtils::GetUndefinedValue(env);
2972     if (cameraSessionNapi->cameraSession_ != nullptr) {
2973         bool isSupported = cameraSessionNapi->cameraSession_->IsDepthFusionSupported();
2974         napi_get_boolean(env, isSupported, &result);
2975         return result;
2976     } else {
2977         MEDIA_ERR_LOG("CameraSessionNapi::IsDepthFusionSupported call Failed!");
2978         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
2979         return nullptr;
2980     }
2981     return result;
2982 }
2983 
GetDepthFusionThreshold(napi_env env,napi_callback_info info)2984 napi_value CameraSessionNapi::GetDepthFusionThreshold(napi_env env, napi_callback_info info)
2985 {
2986     if (!CameraNapiSecurity::CheckSystemApp(env)) {
2987         MEDIA_ERR_LOG("SystemApi GetDepthFusionThreshold is called!");
2988         return nullptr;
2989     }
2990     MEDIA_DEBUG_LOG("CameraSessionNapi::GetDepthFusionThreshold is called");
2991     CameraSessionNapi* cameraSessionNapi = nullptr;
2992     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
2993     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
2994         MEDIA_ERR_LOG("CameraSessionNapi::IsDepthFusionSupported parse parameter occur error");
2995         return nullptr;
2996     }
2997     napi_value result = nullptr;
2998     if (cameraSessionNapi->cameraSession_ != nullptr) {
2999         std::vector<float> vecDepthFusionThreshold;
3000         int32_t retCode = cameraSessionNapi->cameraSession_->GetDepthFusionThreshold(vecDepthFusionThreshold);
3001         if (!CameraNapiUtils::CheckError(env, retCode)) {
3002             return nullptr;
3003         }
3004         MEDIA_INFO_LOG("CameraSessionNapi::GetDepthFusionThreshold len = %{public}zu",
3005             vecDepthFusionThreshold.size());
3006 
3007         if (!vecDepthFusionThreshold.empty() && napi_create_array(env, &result) == napi_ok) {
3008             for (size_t i = 0; i < vecDepthFusionThreshold.size(); i++) {
3009                 float depthFusion = vecDepthFusionThreshold[i];
3010                 napi_value value;
3011                 napi_create_double(env, CameraNapiUtils::FloatToDouble(depthFusion), &value);
3012                 napi_set_element(env, result, i, value);
3013             }
3014         } else {
3015             MEDIA_ERR_LOG("vecDepthFusionThreshold is empty or failed to create array!");
3016         }
3017     } else {
3018         MEDIA_ERR_LOG("CameraSessionNapi::GetDepthFusionThreshold call Failed!");
3019         return nullptr;
3020     }
3021     return result;
3022 }
3023 
IsDepthFusionEnabled(napi_env env,napi_callback_info info)3024 napi_value CameraSessionNapi::IsDepthFusionEnabled(napi_env env, napi_callback_info info)
3025 {
3026     if (!CameraNapiSecurity::CheckSystemApp(env)) {
3027         MEDIA_ERR_LOG("SystemApi IsDepthFusionEnabled is called!");
3028         return nullptr;
3029     }
3030     MEDIA_DEBUG_LOG("CameraSessionNapi::IsDepthFusionEnabled is called");
3031     CameraSessionNapi* cameraSessionNapi = nullptr;
3032     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
3033     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
3034         MEDIA_ERR_LOG("CameraSessionNapi::IsDepthFusionEnabled parse parameter occur error");
3035         return nullptr;
3036     }
3037     auto result = CameraNapiUtils::GetUndefinedValue(env);
3038     if (cameraSessionNapi->cameraSession_ != nullptr) {
3039         bool isEnabled = cameraSessionNapi->cameraSession_->IsDepthFusionEnabled();
3040         napi_get_boolean(env, isEnabled, &result);
3041         MEDIA_INFO_LOG("CameraSessionNapi::IsDepthFusionEnabled:%{public}d", isEnabled);
3042     } else {
3043         MEDIA_ERR_LOG("CameraSessionNapi::IsDepthFusionEnabled get native object fail");
3044         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
3045         return nullptr;
3046     }
3047     return result;
3048 }
3049 
EnableDepthFusion(napi_env env,napi_callback_info info)3050 napi_value CameraSessionNapi::EnableDepthFusion(napi_env env, napi_callback_info info)
3051 {
3052     if (!CameraNapiSecurity::CheckSystemApp(env)) {
3053         MEDIA_ERR_LOG("SystemApi EnableDepthFusion is called!");
3054         return nullptr;
3055     }
3056     MEDIA_DEBUG_LOG("CameraSessionNapi::EnableDepthFusion is called");
3057     bool isEnabledDepthFusion;
3058     CameraSessionNapi* cameraSessionNapi = nullptr;
3059     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, isEnabledDepthFusion);
3060     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
3061         MEDIA_ERR_LOG("CameraSessionNapi::EnabledDepthFusion parse parameter occur error");
3062         return nullptr;
3063     }
3064 
3065     if (cameraSessionNapi->cameraSession_ != nullptr) {
3066         MEDIA_INFO_LOG("CameraSessionNapi::EnableDepthFusion:%{public}d", isEnabledDepthFusion);
3067         cameraSessionNapi->cameraSession_->LockForControl();
3068         int32_t retCode = cameraSessionNapi->cameraSession_->EnableDepthFusion(isEnabledDepthFusion);
3069         cameraSessionNapi->cameraSession_->UnlockForControl();
3070         if (!CameraNapiUtils::CheckError(env, retCode)) {
3071             MEDIA_ERR_LOG("CameraSessionNapi::EnableDepthFusion fail %{public}d", retCode);
3072             return nullptr;
3073         }
3074         MEDIA_INFO_LOG("CameraSessionNapi::EnableDepthFusion success");
3075     } else {
3076         MEDIA_ERR_LOG("CameraSessionNapi::EnableDepthFusion get native object fail");
3077         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
3078         return nullptr;
3079     }
3080     return CameraNapiUtils::GetUndefinedValue(env);
3081 }
3082 
IsMoonCaptureBoostSupported(napi_env env,napi_callback_info info)3083 napi_value CameraSessionNapi::IsMoonCaptureBoostSupported(napi_env env, napi_callback_info info)
3084 {
3085     if (!CameraNapiSecurity::CheckSystemApp(env)) {
3086         MEDIA_ERR_LOG("SystemApi IsMoonCaptureBoostSupported is called!");
3087         return nullptr;
3088     }
3089     MEDIA_DEBUG_LOG("IsMoonCaptureBoostSupported is called");
3090     napi_status status;
3091     napi_value result = nullptr;
3092     size_t argc = ARGS_ZERO;
3093     napi_value argv[ARGS_ZERO];
3094     napi_value thisVar = nullptr;
3095 
3096     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3097 
3098     napi_get_undefined(env, &result);
3099     CameraSessionNapi* cameraSessionNapi = nullptr;
3100     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3101     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3102         bool isSupported = cameraSessionNapi->cameraSession_->IsMoonCaptureBoostSupported();
3103         napi_get_boolean(env, isSupported, &result);
3104     } else {
3105         MEDIA_ERR_LOG("IsMoonCaptureBoostSupported call Failed!");
3106     }
3107     return result;
3108 }
3109 
EnableMoonCaptureBoost(napi_env env,napi_callback_info info)3110 napi_value CameraSessionNapi::EnableMoonCaptureBoost(napi_env env, napi_callback_info info)
3111 {
3112     if (!CameraNapiSecurity::CheckSystemApp(env)) {
3113         MEDIA_ERR_LOG("SystemApi EnableMoonCaptureBoost is called!");
3114         return nullptr;
3115     }
3116     MEDIA_DEBUG_LOG("EnableMoonCaptureBoost is called");
3117     napi_status status;
3118     napi_value result = nullptr;
3119     size_t argc = ARGS_ONE;
3120     napi_value argv[ARGS_ONE] = { 0 };
3121     napi_value thisVar = nullptr;
3122     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3123     NAPI_ASSERT(env, argc == ARGS_ONE, "requires one parameter");
3124     napi_valuetype valueType = napi_undefined;
3125     napi_typeof(env, argv[0], &valueType);
3126     if (valueType != napi_boolean && !CameraNapiUtils::CheckError(env, INVALID_ARGUMENT)) {
3127         return result;
3128     }
3129     napi_get_undefined(env, &result);
3130     CameraSessionNapi* cameraSessionNapi = nullptr;
3131     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3132     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3133         bool isEnableMoonCaptureBoost;
3134         napi_get_value_bool(env, argv[PARAM0], &isEnableMoonCaptureBoost);
3135         MEDIA_INFO_LOG("CameraSessionNapi::EnableMoonCaptureBoost:%{public}d", isEnableMoonCaptureBoost);
3136         cameraSessionNapi->cameraSession_->LockForControl();
3137         int32_t retCode = cameraSessionNapi->cameraSession_->EnableMoonCaptureBoost(isEnableMoonCaptureBoost);
3138         cameraSessionNapi->cameraSession_->UnlockForControl();
3139         if (retCode != 0 && !CameraNapiUtils::CheckError(env, retCode)) {
3140             return result;
3141         }
3142     }
3143     return result;
3144 }
3145 
IsFeatureSupported(napi_env env,napi_callback_info info)3146 napi_value CameraSessionNapi::IsFeatureSupported(napi_env env, napi_callback_info info)
3147 {
3148     if (!CameraNapiSecurity::CheckSystemApp(env)) {
3149         MEDIA_ERR_LOG("SystemApi IsFeatureSupported is called!");
3150         return nullptr;
3151     }
3152     MEDIA_DEBUG_LOG("IsFeatureSupported is called");
3153     int32_t sceneFeature;
3154     CameraSessionNapi* cameraSessionNapi = nullptr;
3155     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, sceneFeature);
3156     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
3157         MEDIA_ERR_LOG("CameraSessionNapi::IsFeatureSupported parse parameter occur error");
3158         return nullptr;
3159     }
3160 
3161     napi_value result = nullptr;
3162     napi_get_boolean(
3163         env, cameraSessionNapi->cameraSession_->IsFeatureSupported(static_cast<SceneFeature>(sceneFeature)), &result);
3164     return result;
3165 }
3166 
EnableFeature(napi_env env,napi_callback_info info)3167 napi_value CameraSessionNapi::EnableFeature(napi_env env, napi_callback_info info)
3168 {
3169     if (!CameraNapiSecurity::CheckSystemApp(env)) {
3170         MEDIA_ERR_LOG("SystemApi EnableFeature is called!");
3171         return nullptr;
3172     }
3173     MEDIA_DEBUG_LOG("EnableFeature is called");
3174     int32_t sceneFeature;
3175     bool isEnable;
3176     CameraSessionNapi* cameraSessionNapi = nullptr;
3177     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, sceneFeature, isEnable);
3178     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
3179         MEDIA_ERR_LOG("CameraSessionNapi::EnableFeature parse parameter occur error");
3180         return nullptr;
3181     }
3182 
3183     MEDIA_INFO_LOG("CameraSessionNapi::EnableFeature:%{public}d", isEnable);
3184     int32_t retCode =
3185         cameraSessionNapi->cameraSession_->EnableFeature(static_cast<SceneFeature>(sceneFeature), isEnable);
3186     if (!CameraNapiUtils::CheckError(env, retCode)) {
3187         return nullptr;
3188     }
3189 
3190     return CameraNapiUtils::GetUndefinedValue(env);
3191 }
3192 
CanPreconfig(napi_env env,napi_callback_info info)3193 napi_value CameraSessionNapi::CanPreconfig(napi_env env, napi_callback_info info)
3194 {
3195     MEDIA_DEBUG_LOG("CanPreconfig is called");
3196     size_t argSize = CameraNapiUtils::GetNapiArgs(env, info);
3197     int32_t configType;
3198     int32_t profileSizeRatio = ProfileSizeRatio::UNSPECIFIED;
3199     CameraSessionNapi* cameraSessionNapi = nullptr;
3200     if (argSize == ARGS_ONE) {
3201         CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, configType);
3202         if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
3203             MEDIA_ERR_LOG("CameraSessionNapi::CanPreconfig parse parameter occur error");
3204             return nullptr;
3205         }
3206     } else {
3207         CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, configType, profileSizeRatio);
3208         if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
3209             MEDIA_ERR_LOG("CameraSessionNapi::CanPreconfig parse 2 parameter occur error");
3210             return nullptr;
3211         }
3212     }
3213 
3214     MEDIA_INFO_LOG("CameraSessionNapi::CanPreconfig: %{public}d, ratioType:%{public}d", configType, profileSizeRatio);
3215     bool result = cameraSessionNapi->cameraSession_->CanPreconfig(
3216         static_cast<PreconfigType>(configType), static_cast<ProfileSizeRatio>(profileSizeRatio));
3217     return CameraNapiUtils::GetBooleanValue(env, result);
3218 }
3219 
Preconfig(napi_env env,napi_callback_info info)3220 napi_value CameraSessionNapi::Preconfig(napi_env env, napi_callback_info info)
3221 {
3222     MEDIA_DEBUG_LOG("Preconfig is called");
3223     size_t argSize = CameraNapiUtils::GetNapiArgs(env, info);
3224     int32_t configType;
3225     int32_t profileSizeRatio = ProfileSizeRatio::UNSPECIFIED;
3226     CameraSessionNapi* cameraSessionNapi = nullptr;
3227     if (argSize == ARGS_ONE) {
3228         CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, configType);
3229         if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
3230             MEDIA_ERR_LOG("CameraSessionNapi::Preconfig parse parameter occur error");
3231             return nullptr;
3232         }
3233     } else {
3234         CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, configType, profileSizeRatio);
3235         if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
3236             MEDIA_ERR_LOG("CameraSessionNapi::Preconfig parse 2 parameter occur error");
3237             return nullptr;
3238         }
3239     }
3240     int32_t retCode = cameraSessionNapi->cameraSession_->Preconfig(
3241         static_cast<PreconfigType>(configType), static_cast<ProfileSizeRatio>(profileSizeRatio));
3242     if (!CameraNapiUtils::CheckError(env, retCode)) {
3243         return nullptr;
3244     }
3245     return CameraNapiUtils::GetUndefinedValue(env);
3246 }
3247 
GetCameraOutputCapabilities(napi_env env,napi_callback_info info)3248 napi_value CameraSessionNapi::GetCameraOutputCapabilities(napi_env env, napi_callback_info info)
3249 {
3250     MEDIA_INFO_LOG("GetCameraOutputCapabilities is called");
3251 
3252     size_t argSize = CameraNapiUtils::GetNapiArgs(env, info);
3253     if (argSize != ARGS_ONE) {
3254         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "Invalid argument.");
3255         return nullptr;
3256     }
3257 
3258     std::string cameraId;
3259     CameraNapiObject cameraInfoObj { { { "cameraId", &cameraId } } };
3260     CameraSessionNapi* cameraSessionNapi = nullptr;
3261     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, cameraInfoObj);
3262 
3263     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "Create cameraInput invalid argument!")) {
3264         MEDIA_ERR_LOG("CameraSessionNapi::GetCameraOutputCapabilities invalid argument");
3265         return nullptr;
3266     }
3267 
3268     sptr<CameraDevice> cameraInfo = CameraManager::GetInstance()->GetCameraDeviceFromId(cameraId);
3269     if (cameraInfo == nullptr) {
3270         MEDIA_ERR_LOG("cameraInfo is null");
3271         CameraNapiUtils::ThrowError(env, SERVICE_FATL_ERROR, "cameraInfo is null.");
3272         return nullptr;
3273     }
3274 
3275     std::vector<sptr<CameraOutputCapability>> caplist =
3276         cameraSessionNapi->cameraSession_->GetCameraOutputCapabilities(cameraInfo);
3277     if (caplist.empty()) {
3278         MEDIA_ERR_LOG("caplist is empty");
3279         return nullptr;
3280     }
3281 
3282     napi_value capArray;
3283     napi_status status = napi_create_array(env, &capArray);
3284     if (status != napi_ok) {
3285         MEDIA_ERR_LOG("Failed to create napi array");
3286         return nullptr;
3287     }
3288 
3289     for (size_t i = 0; i < caplist.size(); i++) {
3290         if (caplist[i] == nullptr) {
3291             continue;
3292         }
3293         caplist[i]->RemoveDuplicatesProfiles();
3294         napi_value cap = CameraNapiObjCameraOutputCapability(*caplist[i]).GenerateNapiValue(env);
3295         if (cap == nullptr || napi_set_element(env, capArray, i, cap) != napi_ok) {
3296             MEDIA_ERR_LOG("Failed to create camera napi wrapper object");
3297             return nullptr;
3298         }
3299     }
3300 
3301     return capArray;
3302 }
3303 
ParseSize(napi_env env,napi_value root,Size & size)3304 void ParseSize(napi_env env, napi_value root, Size& size)
3305 {
3306     MEDIA_DEBUG_LOG("ParseSize is called");
3307     napi_value res = nullptr;
3308     if (napi_get_named_property(env, root, "width", &res) == napi_ok) {
3309         napi_get_value_uint32(env, res, &size.width);
3310     }
3311 
3312     if (napi_get_named_property(env, root, "height", &res) == napi_ok) {
3313         napi_get_value_uint32(env, res, &size.height);
3314     }
3315 }
3316 
ParseProfile(napi_env env,napi_value root,Profile & profile)3317 void ParseProfile(napi_env env, napi_value root, Profile& profile)
3318 {
3319     MEDIA_DEBUG_LOG("ParseProfile is called");
3320     napi_value res = nullptr;
3321 
3322     if (napi_get_named_property(env, root, "size", &res) == napi_ok) {
3323         ParseSize(env, res, profile.size_);
3324     }
3325 
3326     int32_t intValue = 0;
3327     if (napi_get_named_property(env, root, "format", &res) == napi_ok) {
3328         napi_get_value_int32(env, res, &intValue);
3329         profile.format_ = static_cast<CameraFormat>(intValue);
3330     }
3331 }
3332 
ParseVideoProfile(napi_env env,napi_value root,VideoProfile & profile)3333 void ParseVideoProfile(napi_env env, napi_value root, VideoProfile& profile)
3334 {
3335     MEDIA_DEBUG_LOG("ParseVideoProfile is called");
3336     napi_value res = nullptr;
3337 
3338     if (napi_get_named_property(env, root, "size", &res) == napi_ok) {
3339         ParseSize(env, res, profile.size_);
3340     }
3341 
3342     int32_t intValue = 0;
3343     if (napi_get_named_property(env, root, "format", &res) == napi_ok) {
3344         napi_get_value_int32(env, res, &intValue);
3345         profile.format_ = static_cast<CameraFormat>(intValue);
3346     }
3347 
3348     if (napi_get_named_property(env, root, "frameRateRange", &res) == napi_ok) {
3349         const int32_t LENGTH = 2;
3350         std::vector<int32_t> rateRanges(LENGTH);
3351         napi_value value;
3352 
3353         if (napi_get_named_property(env, res, "min", &value) == napi_ok) {
3354             napi_get_value_int32(env, value, &rateRanges[0]);
3355         }
3356         if (napi_get_named_property(env, res, "max", &value) == napi_ok) {
3357             napi_get_value_int32(env, value, &rateRanges[1]);
3358         }
3359         profile.framerates_ = rateRanges;
3360     }
3361 }
3362 
3363 
ParseProfileList(napi_env env,napi_value arrayParam,std::vector<Profile> & profiles)3364 void ParseProfileList(napi_env env, napi_value arrayParam, std::vector<Profile> &profiles)
3365 {
3366     uint32_t length = 0;
3367     napi_get_array_length(env, arrayParam, &length);
3368     for (uint32_t i = 0; i < length; ++i) {
3369         napi_value value;
3370         napi_get_element(env, arrayParam, i, &value);
3371         Profile profile; // 在栈上创建 Profile 对象
3372         ParseProfile(env, value, profile);
3373         profiles.push_back(profile);
3374     }
3375 }
3376 
ParseVideoProfileList(napi_env env,napi_value arrayParam,std::vector<VideoProfile> & profiles)3377 void ParseVideoProfileList(napi_env env, napi_value arrayParam, std::vector<VideoProfile> &profiles)
3378 {
3379     uint32_t length = 0;
3380     napi_get_array_length(env, arrayParam, &length);
3381     for (uint32_t i = 0; i < length; ++i) {
3382         napi_value value;
3383         napi_get_element(env, arrayParam, i, &value);
3384         VideoProfile profile;
3385         ParseVideoProfile(env, value, profile);
3386         profiles.push_back(profile);
3387     }
3388 }
3389 
ParseCameraOutputCapability(napi_env env,napi_value root,std::vector<Profile> & previewProfiles,std::vector<Profile> & photoProfiles,std::vector<VideoProfile> & videoProfiles)3390 void ParseCameraOutputCapability(napi_env env, napi_value root,
3391                                  std::vector<Profile>& previewProfiles,
3392                                  std::vector<Profile>& photoProfiles,
3393                                  std::vector<VideoProfile>& videoProfiles)
3394 {
3395     previewProfiles.clear();
3396     photoProfiles.clear();
3397     videoProfiles.clear();
3398     napi_value res = nullptr;
3399 
3400     if (napi_get_named_property(env, root, "previewProfiles", &res) == napi_ok) {
3401         ParseProfileList(env, res, previewProfiles);
3402     }
3403     if (napi_get_named_property(env, root, "photoProfiles", &res) == napi_ok) {
3404         ParseProfileList(env, res, photoProfiles);
3405     }
3406     if (napi_get_named_property(env, root, "videoProfiles", &res) == napi_ok) {
3407         ParseVideoProfileList(env, res, videoProfiles);
3408     }
3409 }
3410 
GetSessionFunctions(napi_env env,napi_callback_info info)3411 napi_value CameraSessionNapi::GetSessionFunctions(napi_env env, napi_callback_info info)
3412 {
3413     MEDIA_INFO_LOG("GetSessionFunctions is called");
3414     napi_status status;
3415     napi_value result = nullptr;
3416     size_t argc = ARGS_ONE;
3417     napi_value argv[ARGS_ONE] = {0};
3418     napi_value thisVar = nullptr;
3419 
3420     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3421 
3422     std::vector<Profile> previewProfiles;
3423     std::vector<Profile> photoProfiles;
3424     std::vector<VideoProfile> videoProfiles;
3425     ParseCameraOutputCapability(env, argv[PARAM0], previewProfiles, photoProfiles, videoProfiles);
3426     CameraSessionNapi* cameraSessionNapi = nullptr;
3427     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3428     if (status != napi_ok || cameraSessionNapi == nullptr) {
3429         MEDIA_ERR_LOG("napi_unwrap failure!");
3430         return nullptr;
3431     }
3432 
3433     auto session = cameraSessionNapi->cameraSession_;
3434     SceneMode mode = session->GetMode();
3435     auto cameraFunctionsList = session->GetSessionFunctions(previewProfiles, photoProfiles, videoProfiles);
3436     auto it = modeToFunctionTypeMap_.find(mode);
3437     if (it != modeToFunctionTypeMap_.end()) {
3438         result = CreateFunctionsJSArray(env, cameraFunctionsList, it->second);
3439     } else {
3440         MEDIA_ERR_LOG("GetSessionFunctions failed due to unsupported mode: %{public}d", mode);
3441     }
3442     return result;
3443 }
3444 
GetSessionConflictFunctions(napi_env env,napi_callback_info info)3445 napi_value CameraSessionNapi::GetSessionConflictFunctions(napi_env env, napi_callback_info info)
3446 {
3447     MEDIA_INFO_LOG("GetSessionConflictFunctions is called");
3448     napi_status status;
3449     napi_value result = nullptr;
3450     size_t argc = ARGS_ONE;
3451     napi_value argv[ARGS_ONE];
3452     napi_value thisVar = nullptr;
3453 
3454     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3455 
3456     napi_get_undefined(env, &result);
3457 
3458     CameraSessionNapi* cameraSessionNapi = nullptr;
3459     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3460     if (status != napi_ok || cameraSessionNapi == nullptr) {
3461         MEDIA_ERR_LOG("napi_unwrap failure!");
3462         return nullptr;
3463     }
3464 
3465     auto session = cameraSessionNapi->cameraSession_;
3466     SceneMode mode = session->GetMode();
3467     auto conflictFunctionsList = session->GetSessionConflictFunctions();
3468     auto it = modeToConflictFunctionTypeMap_.find(mode);
3469     if (it != modeToConflictFunctionTypeMap_.end()) {
3470         result = CreateFunctionsJSArray(env, conflictFunctionsList, it->second);
3471     } else {
3472         MEDIA_ERR_LOG("GetSessionConflictFunctions failed due to unsupported mode: %{public}d", mode);
3473     }
3474     return result;
3475 }
3476 
CreateFunctionsJSArray(napi_env env,std::vector<sptr<CameraAbility>> functionsList,FunctionsType type)3477 napi_value CameraSessionNapi::CreateFunctionsJSArray(
3478     napi_env env, std::vector<sptr<CameraAbility>> functionsList, FunctionsType type)
3479 {
3480     MEDIA_DEBUG_LOG("CreateFunctionsJSArray is called");
3481     napi_value functionsArray = nullptr;
3482     napi_value functions = nullptr;
3483     napi_status status;
3484 
3485     if (functionsList.empty()) {
3486         MEDIA_ERR_LOG("functionsList is empty");
3487     }
3488 
3489     status = napi_create_array(env, &functionsArray);
3490     if (status != napi_ok) {
3491         MEDIA_ERR_LOG("napi_create_array failed");
3492         return functionsArray;
3493     }
3494 
3495     size_t j = 0;
3496     for (size_t i = 0; i < functionsList.size(); i++) {
3497         functions = CameraFunctionsNapi::CreateCameraFunctions(env, functionsList[i], type);
3498         if ((functions == nullptr) || napi_set_element(env, functionsArray, j++, functions) != napi_ok) {
3499             MEDIA_ERR_LOG("failed to create functions object napi wrapper object");
3500             return nullptr;
3501         }
3502     }
3503     MEDIA_INFO_LOG("create functions count = %{public}zu", j);
3504     return functionsArray;
3505 }
3506 
IsEffectSuggestionSupported(napi_env env,napi_callback_info info)3507 napi_value CameraSessionNapi::IsEffectSuggestionSupported(napi_env env, napi_callback_info info)
3508 {
3509     if (!CameraNapiSecurity::CheckSystemApp(env, false)) {
3510         MEDIA_ERR_LOG("SystemApi IsEffectSuggestionSupported is called!");
3511         return nullptr;
3512     }
3513     MEDIA_DEBUG_LOG("IsEffectSuggestionSupported is called");
3514     napi_status status;
3515     napi_value result = nullptr;
3516     size_t argc = ARGS_ZERO;
3517     napi_value argv[ARGS_ZERO];
3518     napi_value thisVar = nullptr;
3519 
3520     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3521 
3522     napi_get_undefined(env, &result);
3523     CameraSessionNapi* cameraSessionNapi = nullptr;
3524     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3525     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3526         bool isEffectSuggestionSupported = cameraSessionNapi->cameraSession_->IsEffectSuggestionSupported();
3527         napi_get_boolean(env, isEffectSuggestionSupported, &result);
3528     } else {
3529         MEDIA_ERR_LOG("IsEffectSuggestionSupported call Failed!");
3530     }
3531     return result;
3532 }
3533 
EnableEffectSuggestion(napi_env env,napi_callback_info info)3534 napi_value CameraSessionNapi::EnableEffectSuggestion(napi_env env, napi_callback_info info)
3535 {
3536     if (!CameraNapiSecurity::CheckSystemApp(env, false)) {
3537         MEDIA_ERR_LOG("SystemApi EnableEffectSuggestion is called!");
3538         return nullptr;
3539     }
3540     MEDIA_DEBUG_LOG("EnableEffectSuggestion is called");
3541     napi_status status;
3542     napi_value result = nullptr;
3543     size_t argc = ARGS_ONE;
3544     napi_value argv[ARGS_ONE] = { 0 };
3545     napi_value thisVar = nullptr;
3546     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3547     NAPI_ASSERT(env, argc == ARGS_ONE, "requires one parameter");
3548     napi_valuetype valueType = napi_undefined;
3549     napi_typeof(env, argv[0], &valueType);
3550     if (valueType != napi_boolean && !CameraNapiUtils::CheckError(env, INVALID_ARGUMENT)) {
3551         return result;
3552     }
3553     napi_get_undefined(env, &result);
3554     CameraSessionNapi* cameraSessionNapi = nullptr;
3555     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3556     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3557         bool enabled;
3558         napi_get_value_bool(env, argv[PARAM0], &enabled);
3559         MEDIA_INFO_LOG("CameraSessionNapi::EnableEffectSuggestion:%{public}d", enabled);
3560         cameraSessionNapi->cameraSession_->LockForControl();
3561         int32_t retCode = cameraSessionNapi->cameraSession_->EnableEffectSuggestion(enabled);
3562         cameraSessionNapi->cameraSession_->UnlockForControl();
3563         if (retCode != 0 && !CameraNapiUtils::CheckError(env, retCode)) {
3564             return result;
3565         }
3566     }
3567     return result;
3568 }
3569 
GetSupportedEffectSuggestionType(napi_env env,napi_callback_info info)3570 napi_value CameraSessionNapi::GetSupportedEffectSuggestionType(napi_env env, napi_callback_info info)
3571 {
3572     if (!CameraNapiSecurity::CheckSystemApp(env, false)) {
3573         MEDIA_ERR_LOG("SystemApi GetSupportedEffectSuggestionType is called!");
3574         return nullptr;
3575     }
3576     MEDIA_DEBUG_LOG("GetSupportedEffectSuggestionType is called");
3577     napi_status status;
3578     napi_value result = nullptr;
3579     size_t argc = ARGS_ZERO;
3580     napi_value argv[ARGS_ZERO];
3581     napi_value thisVar = nullptr;
3582 
3583     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3584 
3585     napi_get_undefined(env, &result);
3586     status = napi_create_array(env, &result);
3587     if (status != napi_ok) {
3588         MEDIA_ERR_LOG("napi_create_array call Failed!");
3589         return result;
3590     }
3591     CameraSessionNapi* cameraSessionNapi = nullptr;
3592     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3593     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3594         std::vector<EffectSuggestionType> effectSuggestionTypeList =
3595             cameraSessionNapi->cameraSession_->GetSupportedEffectSuggestionType();
3596         if (!effectSuggestionTypeList.empty()) {
3597             for (size_t i = 0; i < effectSuggestionTypeList.size(); i++) {
3598                 int type = effectSuggestionTypeList[i];
3599                 napi_value value;
3600                 napi_create_int32(env, type, &value);
3601                 napi_set_element(env, result, i, value);
3602             }
3603         }
3604     } else {
3605         MEDIA_ERR_LOG("GetSupportedEffectSuggestionType call Failed!");
3606     }
3607     return result;
3608 }
3609 
ParseEffectSuggestionStatus(napi_env env,napi_value arrayParam,std::vector<EffectSuggestionStatus> & effectSuggestionStatusList)3610 static void ParseEffectSuggestionStatus(napi_env env, napi_value arrayParam,
3611     std::vector<EffectSuggestionStatus> &effectSuggestionStatusList)
3612 {
3613     MEDIA_DEBUG_LOG("ParseEffectSuggestionStatus is called");
3614     uint32_t length = 0;
3615     napi_value value;
3616     napi_get_array_length(env, arrayParam, &length);
3617     for (uint32_t i = 0; i < length; i++) {
3618         napi_get_element(env, arrayParam, i, &value);
3619         napi_value res = nullptr;
3620         EffectSuggestionStatus effectSuggestionStatus;
3621         int32_t intValue = 0;
3622         if (napi_get_named_property(env, value, "type", &res) == napi_ok) {
3623             napi_get_value_int32(env, res, &intValue);
3624             effectSuggestionStatus.type = static_cast<EffectSuggestionType>(intValue);
3625         }
3626         bool enabled = false;
3627         if (napi_get_named_property(env, value, "status", &res) == napi_ok) {
3628             napi_get_value_bool(env, res, &enabled);
3629             effectSuggestionStatus.status = enabled;
3630         }
3631         effectSuggestionStatusList.push_back(effectSuggestionStatus);
3632     }
3633 }
3634 
SetEffectSuggestionStatus(napi_env env,napi_callback_info info)3635 napi_value CameraSessionNapi::SetEffectSuggestionStatus(napi_env env, napi_callback_info info)
3636 {
3637     if (!CameraNapiSecurity::CheckSystemApp(env, false)) {
3638         MEDIA_ERR_LOG("SystemApi SetEffectSuggestionStatus is called!");
3639         return nullptr;
3640     }
3641     MEDIA_INFO_LOG("SetEffectSuggestionStatus is called");
3642     napi_status status;
3643     napi_value result = nullptr;
3644     size_t argc = ARGS_ONE;
3645     napi_value argv[ARGS_ONE] = {0};
3646     napi_value thisVar = nullptr;
3647 
3648     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3649 
3650     std::vector<EffectSuggestionStatus> effectSuggestionStatusList;
3651     ParseEffectSuggestionStatus(env, argv[PARAM0], effectSuggestionStatusList);
3652 
3653     napi_get_undefined(env, &result);
3654     CameraSessionNapi* cameraSessionNapi = nullptr;
3655     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3656     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3657         cameraSessionNapi->cameraSession_->LockForControl();
3658         int32_t retCode = cameraSessionNapi->cameraSession_->SetEffectSuggestionStatus(effectSuggestionStatusList);
3659         cameraSessionNapi->cameraSession_->UnlockForControl();
3660         if (retCode != 0 && !CameraNapiUtils::CheckError(env, retCode)) {
3661             return result;
3662         }
3663     }
3664     return result;
3665 }
3666 
UpdateEffectSuggestion(napi_env env,napi_callback_info info)3667 napi_value CameraSessionNapi::UpdateEffectSuggestion(napi_env env, napi_callback_info info)
3668 {
3669     if (!CameraNapiSecurity::CheckSystemApp(env, false)) {
3670         MEDIA_ERR_LOG("SystemApi UpdateEffectSuggestion is called!");
3671         return nullptr;
3672     }
3673     MEDIA_DEBUG_LOG("UpdateEffectSuggestion is called");
3674     napi_status status;
3675     napi_value result = nullptr;
3676     size_t argc = ARGS_TWO;
3677     napi_value argv[ARGS_TWO] = { 0, 0 };
3678     napi_value thisVar = nullptr;
3679     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3680     NAPI_ASSERT(env, argc == ARGS_TWO, "requires two parameter");
3681     napi_valuetype valueType = napi_undefined;
3682     napi_typeof(env, argv[PARAM0], &valueType);
3683     if (valueType != napi_number && !CameraNapiUtils::CheckError(env, INVALID_ARGUMENT)) {
3684         return result;
3685     }
3686     napi_typeof(env, argv[PARAM1], &valueType);
3687     if (valueType != napi_boolean && !CameraNapiUtils::CheckError(env, INVALID_ARGUMENT)) {
3688         return result;
3689     }
3690 
3691     napi_get_undefined(env, &result);
3692     CameraSessionNapi* cameraSessionNapi = nullptr;
3693     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3694     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3695         int32_t value;
3696         napi_get_value_int32(env, argv[PARAM0], &value);
3697         auto effectSuggestionType = (EffectSuggestionType)value;
3698         bool enabled;
3699         napi_get_value_bool(env, argv[PARAM1], &enabled);
3700         MEDIA_INFO_LOG("CameraSessionNapi::UpdateEffectSuggestion:%{public}d enabled:%{public}d",
3701             effectSuggestionType, enabled);
3702         cameraSessionNapi->cameraSession_->LockForControl();
3703         int32_t retCode = cameraSessionNapi->cameraSession_->UpdateEffectSuggestion(effectSuggestionType, enabled);
3704         cameraSessionNapi->cameraSession_->UnlockForControl();
3705         if (retCode != 0 && !CameraNapiUtils::CheckError(env, retCode)) {
3706             return result;
3707         }
3708     }
3709     return result;
3710 }
3711 
3712 // ------------------------------------------------auto_awb_props-------------------------------------------------------
GetSupportedWhiteBalanceModes(napi_env env,napi_callback_info info)3713 napi_value CameraSessionNapi::GetSupportedWhiteBalanceModes(napi_env env, napi_callback_info info)
3714 {
3715     MEDIA_DEBUG_LOG("GetSupportedWhiteBalanceModes is called");
3716     napi_status status;
3717     napi_value result = nullptr;
3718     size_t argc = ARGS_ZERO;
3719     napi_value argv[ARGS_ZERO];
3720     napi_value thisVar = nullptr;
3721 
3722     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3723 
3724     napi_get_undefined(env, &result);
3725     status = napi_create_array(env, &result);
3726     if (status != napi_ok) {
3727         MEDIA_ERR_LOG("napi_create_array call Failed!");
3728         return result;
3729     }
3730     CameraSessionNapi* cameraSessionNapi = nullptr;
3731     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3732     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3733         std::vector<WhiteBalanceMode> whiteBalanceModes;
3734         int32_t retCode = cameraSessionNapi->cameraSession_->GetSupportedWhiteBalanceModes(whiteBalanceModes);
3735         if (!CameraNapiUtils::CheckError(env, retCode)) {
3736             return nullptr;
3737         }
3738 
3739         MEDIA_INFO_LOG("ProfessionSessionNapi::GetSupportedWhiteBalanceModes len = %{public}zu",
3740             whiteBalanceModes.size());
3741         if (!whiteBalanceModes.empty()) {
3742             for (size_t i = 0; i < whiteBalanceModes.size(); i++) {
3743                 WhiteBalanceMode whiteBalanceMode = whiteBalanceModes[i];
3744                 napi_value value;
3745                 napi_create_int32(env, whiteBalanceMode, &value);
3746                 napi_set_element(env, result, i, value);
3747             }
3748         }
3749     } else {
3750         MEDIA_ERR_LOG("GetSupportedWhiteBalanceModes call Failed!");
3751     }
3752     return result;
3753 }
3754 
IsWhiteBalanceModeSupported(napi_env env,napi_callback_info info)3755 napi_value CameraSessionNapi::IsWhiteBalanceModeSupported(napi_env env, napi_callback_info info)
3756 {
3757     MEDIA_DEBUG_LOG("IsWhiteBalanceModeSupported is called");
3758     napi_status status;
3759     napi_value result = nullptr;
3760     size_t argc = ARGS_ONE;
3761     napi_value argv[ARGS_ONE] = {0};
3762     napi_value thisVar = nullptr;
3763 
3764     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3765 
3766     napi_get_undefined(env, &result);
3767     CameraSessionNapi* cameraSessionNapi = nullptr;
3768     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3769     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3770         int32_t value;
3771         napi_get_value_int32(env, argv[PARAM0], &value);
3772         WhiteBalanceMode mode = (WhiteBalanceMode)value;
3773         bool isSupported;
3774         int32_t retCode = cameraSessionNapi->cameraSession_->IsWhiteBalanceModeSupported(mode, isSupported);
3775         if (!CameraNapiUtils::CheckError(env, retCode)) {
3776             return nullptr;
3777         }
3778         napi_get_boolean(env, isSupported, &result);
3779     } else {
3780         MEDIA_ERR_LOG("IsWhiteBalanceModeSupported call Failed!");
3781     }
3782     return result;
3783 }
3784 
GetWhiteBalanceMode(napi_env env,napi_callback_info info)3785 napi_value CameraSessionNapi::GetWhiteBalanceMode(napi_env env, napi_callback_info info)
3786 {
3787     MEDIA_DEBUG_LOG("GetWhiteBalanceMode is called");
3788     napi_status status;
3789     napi_value result = nullptr;
3790     size_t argc = ARGS_ZERO;
3791     napi_value argv[ARGS_ZERO];
3792     napi_value thisVar = nullptr;
3793 
3794     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3795 
3796     napi_get_undefined(env, &result);
3797     CameraSessionNapi* cameraSessionNapi = nullptr;
3798     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3799     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3800         WhiteBalanceMode whiteBalanceMode;
3801         int32_t retCode = cameraSessionNapi->cameraSession_->GetWhiteBalanceMode(whiteBalanceMode);
3802         if (!CameraNapiUtils::CheckError(env, retCode)) {
3803             return nullptr;
3804         }
3805         napi_create_int32(env, whiteBalanceMode, &result);
3806     } else {
3807         MEDIA_ERR_LOG("GetWhiteBalanceMode call Failed!");
3808     }
3809     return result;
3810 }
3811 
SetWhiteBalanceMode(napi_env env,napi_callback_info info)3812 napi_value CameraSessionNapi::SetWhiteBalanceMode(napi_env env, napi_callback_info info)
3813 {
3814     MEDIA_DEBUG_LOG("SetWhiteBalanceMode is called");
3815     CAMERA_SYNC_TRACE;
3816     napi_status status;
3817     napi_value result = nullptr;
3818     size_t argc = ARGS_ONE;
3819     napi_value argv[ARGS_ONE] = {0};
3820     napi_value thisVar = nullptr;
3821 
3822     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3823 
3824     napi_get_undefined(env, &result);
3825     CameraSessionNapi* cameraSessionNapi = nullptr;
3826     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3827     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3828         int32_t value;
3829         napi_get_value_int32(env, argv[PARAM0], &value);
3830         WhiteBalanceMode mode = (WhiteBalanceMode)value;
3831         cameraSessionNapi->cameraSession_->LockForControl();
3832         cameraSessionNapi->cameraSession_->SetWhiteBalanceMode(mode);
3833         MEDIA_INFO_LOG("ProfessionSessionNapi::SetWhiteBalanceMode set mode:%{public}d", value);
3834         cameraSessionNapi->cameraSession_->UnlockForControl();
3835     } else {
3836         MEDIA_ERR_LOG("SetWhiteBalanceMode call Failed!");
3837     }
3838     return result;
3839 }
3840 
3841 // -----------------------------------------------manual_awb_props------------------------------------------------------
GetManualWhiteBalanceRange(napi_env env,napi_callback_info info)3842 napi_value CameraSessionNapi::GetManualWhiteBalanceRange(napi_env env, napi_callback_info info)
3843 {
3844     MEDIA_DEBUG_LOG("GetManualWhiteBalanceRange is called");
3845     napi_status status;
3846     napi_value result = nullptr;
3847     size_t argc = ARGS_ZERO;
3848     napi_value argv[ARGS_ZERO];
3849     napi_value thisVar = nullptr;
3850 
3851     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3852 
3853     napi_get_undefined(env, &result);
3854 
3855     CameraSessionNapi* cameraSessionNapi = nullptr;
3856     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3857     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3858         std::vector<int32_t> whiteBalanceRange = {};
3859         int32_t retCode = cameraSessionNapi->cameraSession_->GetManualWhiteBalanceRange(whiteBalanceRange);
3860         if (!CameraNapiUtils::CheckError(env, retCode)) {
3861             return nullptr;
3862         }
3863         MEDIA_INFO_LOG("ProfessionSessionNapi::GetManualWhiteBalanceRange len = %{public}zu", whiteBalanceRange.size());
3864 
3865         if (!whiteBalanceRange.empty() && napi_create_array(env, &result) == napi_ok) {
3866             for (size_t i = 0; i < whiteBalanceRange.size(); i++) {
3867                 int32_t iso = whiteBalanceRange[i];
3868                 napi_value value;
3869                 napi_create_int32(env, iso, &value);
3870                 napi_set_element(env, result, i, value);
3871             }
3872         } else {
3873             MEDIA_ERR_LOG("whiteBalanceRange is empty or failed to create array!");
3874         }
3875     } else {
3876         MEDIA_ERR_LOG("GetManualWhiteBalanceRange call Failed!");
3877     }
3878     return result;
3879 }
3880 
IsManualWhiteBalanceSupported(napi_env env,napi_callback_info info)3881 napi_value CameraSessionNapi::IsManualWhiteBalanceSupported(napi_env env, napi_callback_info info)
3882 {
3883     if (!CameraNapiSecurity::CheckSystemApp(env)) {
3884         MEDIA_ERR_LOG("SystemApi IsManualIsoSupported is called!");
3885         return nullptr;
3886     }
3887     MEDIA_DEBUG_LOG("IsManualIsoSupported is called");
3888     napi_status status;
3889     napi_value result = nullptr;
3890     size_t argc = ARGS_ZERO;
3891     napi_value argv[ARGS_ZERO];
3892     napi_value thisVar = nullptr;
3893 
3894     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3895 
3896     napi_get_undefined(env, &result);
3897     CameraSessionNapi* cameraSessionNapi = nullptr;
3898     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3899     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3900         bool isSupported;
3901         int32_t retCode = cameraSessionNapi->cameraSession_->IsManualWhiteBalanceSupported(isSupported);
3902         if (!CameraNapiUtils::CheckError(env, retCode)) {
3903             return nullptr;
3904         }
3905         napi_get_boolean(env, isSupported, &result);
3906     } else {
3907         MEDIA_ERR_LOG("IsManualIsoSupported call Failed!");
3908     }
3909     return result;
3910 }
3911 
GetManualWhiteBalance(napi_env env,napi_callback_info info)3912 napi_value CameraSessionNapi::GetManualWhiteBalance(napi_env env, napi_callback_info info)
3913 {
3914     MEDIA_DEBUG_LOG("GetISO is called");
3915     napi_status status;
3916     napi_value result = nullptr;
3917     size_t argc = ARGS_ZERO;
3918     napi_value argv[ARGS_ZERO];
3919     napi_value thisVar = nullptr;
3920 
3921     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3922 
3923     napi_get_undefined(env, &result);
3924     CameraSessionNapi* cameraSessionNapi = nullptr;
3925     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3926     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3927         int32_t wbValue;
3928         int32_t retCode = cameraSessionNapi->cameraSession_->GetManualWhiteBalance(wbValue);
3929         if (!CameraNapiUtils::CheckError(env, retCode)) {
3930             return nullptr;
3931         }
3932         napi_create_int32(env, wbValue, &result);
3933     } else {
3934         MEDIA_ERR_LOG("GetISO call Failed!");
3935     }
3936     return result;
3937 }
3938 
SetManualWhiteBalance(napi_env env,napi_callback_info info)3939 napi_value CameraSessionNapi::SetManualWhiteBalance(napi_env env, napi_callback_info info)
3940 {
3941     MEDIA_DEBUG_LOG("SetManualWhiteBalance is called");
3942     CAMERA_SYNC_TRACE;
3943     napi_status status;
3944     napi_value result = nullptr;
3945     size_t argc = ARGS_ONE;
3946     napi_value argv[ARGS_ONE] = {0};
3947     napi_value thisVar = nullptr;
3948 
3949     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
3950 
3951     napi_get_undefined(env, &result);
3952     CameraSessionNapi* cameraSessionNapi = nullptr;
3953     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraSessionNapi));
3954     if (status == napi_ok && cameraSessionNapi != nullptr && cameraSessionNapi->cameraSession_ != nullptr) {
3955         int32_t wbValue;
3956         napi_get_value_int32(env, argv[PARAM0], &wbValue);
3957         cameraSessionNapi->cameraSession_->LockForControl();
3958         cameraSessionNapi->cameraSession_->SetManualWhiteBalance(wbValue);
3959         MEDIA_INFO_LOG("ProfessionSessionNapi::SetManualWhiteBalance set wbValue:%{public}d", wbValue);
3960         cameraSessionNapi->cameraSession_->UnlockForControl();
3961     } else {
3962         MEDIA_ERR_LOG("SetManualWhiteBalance call Failed!");
3963     }
3964     return result;
3965 }
3966 
GetSupportedVirtualApertures(napi_env env,napi_callback_info info)3967 napi_value CameraSessionNapi::GetSupportedVirtualApertures(napi_env env, napi_callback_info info)
3968 {
3969     if (!CameraNapiSecurity::CheckSystemApp(env)) {
3970         MEDIA_ERR_LOG("SystemApi GetSupportedVirtualApertures is called!");
3971         return nullptr;
3972     }
3973     MEDIA_DEBUG_LOG("GetSupportedVirtualApertures is called");
3974     CameraSessionNapi* cameraSessionNapi = nullptr;
3975     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
3976     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
3977         MEDIA_ERR_LOG("CameraSessionNapi::GetSupportedVirtualApertures parse parameter occur error");
3978         return nullptr;
3979     }
3980 
3981     napi_status status;
3982     napi_value result = nullptr;
3983     status = napi_create_array(env, &result);
3984     if (status != napi_ok) {
3985         MEDIA_ERR_LOG("napi_create_array call Failed!");
3986         return nullptr;
3987     }
3988 
3989     if (cameraSessionNapi->cameraSession_ != nullptr) {
3990         std::vector<float> virtualApertures = {};
3991         int32_t retCode = cameraSessionNapi->cameraSession_->GetSupportedVirtualApertures(virtualApertures);
3992         MEDIA_INFO_LOG("GetSupportedVirtualApertures virtualApertures len = %{public}zu", virtualApertures.size());
3993         if (!CameraNapiUtils::CheckError(env, retCode)) {
3994             return nullptr;
3995         }
3996         if (!virtualApertures.empty()) {
3997             for (size_t i = 0; i < virtualApertures.size(); i++) {
3998                 float virtualAperture = virtualApertures[i];
3999                 napi_value value;
4000                 napi_create_double(env, CameraNapiUtils::FloatToDouble(virtualAperture), &value);
4001                 napi_set_element(env, result, i, value);
4002             }
4003         }
4004     } else {
4005         MEDIA_ERR_LOG("GetSupportedVirtualApertures call Failed!");
4006     }
4007     return result;
4008 }
4009 
GetVirtualAperture(napi_env env,napi_callback_info info)4010 napi_value CameraSessionNapi::GetVirtualAperture(napi_env env, napi_callback_info info)
4011 {
4012     if (!CameraNapiSecurity::CheckSystemApp(env)) {
4013         MEDIA_ERR_LOG("SystemApi GetVirtualAperture is called!");
4014         return nullptr;
4015     }
4016     MEDIA_DEBUG_LOG("GetVirtualAperture is called");
4017     CameraSessionNapi* cameraSessionNapi = nullptr;
4018     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
4019     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
4020         MEDIA_ERR_LOG("CameraSessionNapi::GetVirtualAperture parse parameter occur error");
4021         return nullptr;
4022     }
4023     if (cameraSessionNapi->cameraSession_ != nullptr) {
4024         float virtualAperture;
4025         int32_t retCode = cameraSessionNapi->cameraSession_->GetVirtualAperture(virtualAperture);
4026         if (!CameraNapiUtils::CheckError(env, retCode)) {
4027             return nullptr;
4028         }
4029         napi_value result;
4030         napi_create_double(env, CameraNapiUtils::FloatToDouble(virtualAperture), &result);
4031         return result;
4032     } else {
4033         MEDIA_ERR_LOG("GetVirtualAperture call Failed!");
4034     }
4035     return CameraNapiUtils::GetUndefinedValue(env);
4036 }
4037 
SetVirtualAperture(napi_env env,napi_callback_info info)4038 napi_value CameraSessionNapi::SetVirtualAperture(napi_env env, napi_callback_info info)
4039 {
4040     if (!CameraNapiSecurity::CheckSystemApp(env)) {
4041         MEDIA_ERR_LOG("SystemApi SetVirtualAperture is called!");
4042         return nullptr;
4043     }
4044     MEDIA_DEBUG_LOG("SetVirtualAperture is called");
4045     double virtualAperture;
4046     CameraSessionNapi* cameraSessionNapi = nullptr;
4047     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, virtualAperture);
4048     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
4049         MEDIA_ERR_LOG("CameraSessionNapi::SetVirtualAperture parse parameter occur error");
4050         return nullptr;
4051     }
4052     if (cameraSessionNapi->cameraSession_ != nullptr) {
4053         cameraSessionNapi->cameraSession_->LockForControl();
4054         cameraSessionNapi->cameraSession_->SetVirtualAperture((float)virtualAperture);
4055         MEDIA_INFO_LOG("SetVirtualAperture set virtualAperture %{public}f!", virtualAperture);
4056         cameraSessionNapi->cameraSession_->UnlockForControl();
4057     } else {
4058         MEDIA_ERR_LOG("SetVirtualAperture call Failed!");
4059     }
4060     return CameraNapiUtils::GetUndefinedValue(env);
4061 }
4062 
GetSupportedPhysicalApertures(napi_env env,napi_callback_info info)4063 napi_value CameraSessionNapi::GetSupportedPhysicalApertures(napi_env env, napi_callback_info info)
4064 {
4065     if (!CameraNapiSecurity::CheckSystemApp(env)) {
4066         MEDIA_ERR_LOG("SystemApi GetSupportedPhysicalApertures is called!");
4067         return nullptr;
4068     }
4069     MEDIA_DEBUG_LOG("GetSupportedPhysicalApertures is called");
4070     CameraSessionNapi* cameraSessionNapi = nullptr;
4071     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
4072     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
4073         MEDIA_ERR_LOG("CameraSessionNapi::GetSupportedPhysicalApertures parse parameter occur error");
4074         return nullptr;
4075     }
4076 
4077     napi_status status;
4078     napi_value result = nullptr;
4079     status = napi_create_array(env, &result);
4080     if (status != napi_ok) {
4081         MEDIA_ERR_LOG("napi_create_array call Failed!");
4082         return nullptr;
4083     }
4084 
4085     if (status == napi_ok && cameraSessionNapi->cameraSession_ != nullptr) {
4086         std::vector<std::vector<float>> physicalApertures = {};
4087         int32_t retCode = cameraSessionNapi->cameraSession_->GetSupportedPhysicalApertures(physicalApertures);
4088         MEDIA_INFO_LOG("GetSupportedPhysicalApertures len = %{public}zu", physicalApertures.size());
4089         if (!CameraNapiUtils::CheckError(env, retCode)) {
4090             return nullptr;
4091         }
4092         if (!physicalApertures.empty()) {
4093             result = CameraNapiUtils::ProcessingPhysicalApertures(env, physicalApertures);
4094         }
4095     } else {
4096         MEDIA_ERR_LOG("GetSupportedPhysicalApertures call Failed!");
4097     }
4098     return result;
4099 }
4100 
GetPhysicalAperture(napi_env env,napi_callback_info info)4101 napi_value CameraSessionNapi::GetPhysicalAperture(napi_env env, napi_callback_info info)
4102 {
4103     if (!CameraNapiSecurity::CheckSystemApp(env)) {
4104         MEDIA_ERR_LOG("SystemApi GetPhysicalAperture is called!");
4105         return nullptr;
4106     }
4107     MEDIA_DEBUG_LOG("GetPhysicalAperture is called");
4108     CameraSessionNapi* cameraSessionNapi = nullptr;
4109     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
4110     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
4111         MEDIA_ERR_LOG("CameraSessionNapi::GetPhysicalAperture parse parameter occur error");
4112         return nullptr;
4113     }
4114 
4115     if (cameraSessionNapi->cameraSession_ != nullptr) {
4116         float physicalAperture = 0.0;
4117         int32_t retCode = cameraSessionNapi->cameraSession_->GetPhysicalAperture(physicalAperture);
4118         if (!CameraNapiUtils::CheckError(env, retCode)) {
4119             return nullptr;
4120         }
4121         napi_value result = nullptr;
4122         napi_create_double(env, CameraNapiUtils::FloatToDouble(physicalAperture), &result);
4123         return result;
4124     } else {
4125         MEDIA_ERR_LOG("GetPhysicalAperture call Failed!");
4126     }
4127     return CameraNapiUtils::GetUndefinedValue(env);
4128 }
4129 
SetPhysicalAperture(napi_env env,napi_callback_info info)4130 napi_value CameraSessionNapi::SetPhysicalAperture(napi_env env, napi_callback_info info)
4131 {
4132     if (!CameraNapiSecurity::CheckSystemApp(env)) {
4133         MEDIA_ERR_LOG("SystemApi SetPhysicalAperture is called!");
4134         return nullptr;
4135     }
4136     MEDIA_DEBUG_LOG("SetPhysicalAperture is called");
4137     double physicalAperture;
4138     CameraSessionNapi* cameraSessionNapi = nullptr;
4139     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, physicalAperture);
4140     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
4141         MEDIA_ERR_LOG("CameraSessionNapi::SetPhysicalAperture parse parameter occur error");
4142         return nullptr;
4143     }
4144 
4145     if (cameraSessionNapi->cameraSession_ != nullptr) {
4146         cameraSessionNapi->cameraSession_->LockForControl();
4147         int32_t retCode = cameraSessionNapi->cameraSession_->SetPhysicalAperture((float)physicalAperture);
4148         MEDIA_INFO_LOG("SetPhysicalAperture set physicalAperture %{public}f!", ConfusingNumber(physicalAperture));
4149         cameraSessionNapi->cameraSession_->UnlockForControl();
4150         if (!CameraNapiUtils::CheckError(env, retCode)) {
4151             return nullptr;
4152         }
4153     } else {
4154         MEDIA_ERR_LOG("SetPhysicalAperture call Failed!");
4155     }
4156     return CameraNapiUtils::GetUndefinedValue(env);
4157 }
4158 
SetUsage(napi_env env,napi_callback_info info)4159 napi_value CameraSessionNapi::SetUsage(napi_env env, napi_callback_info info)
4160 {
4161     MEDIA_DEBUG_LOG("SetUsage is called");
4162     if (!CameraNapiSecurity::CheckSystemApp(env)) {
4163         MEDIA_ERR_LOG("SystemApi SetUsage is called!");
4164         return nullptr;
4165     }
4166 
4167     uint32_t usageType;
4168     bool enabled;
4169     CameraSessionNapi* cameraSessionNapi = nullptr;
4170     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, usageType, enabled);
4171     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
4172         MEDIA_ERR_LOG("CameraSessionNapi::SetUsage parse parameter occur error");
4173         return nullptr;
4174     }
4175 
4176     cameraSessionNapi->cameraSession_->LockForControl();
4177     cameraSessionNapi->cameraSession_->SetUsage(static_cast<UsageType>(usageType), enabled);
4178     cameraSessionNapi->cameraSession_->UnlockForControl();
4179 
4180     MEDIA_DEBUG_LOG("CameraSessionNapi::SetUsage success");
4181 
4182     return CameraNapiUtils::GetUndefinedValue(env);
4183 }
4184 
RegisterExposureCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4185 void CameraSessionNapi::RegisterExposureCallbackListener(
4186     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4187 {
4188     if (exposureCallback_ == nullptr) {
4189         exposureCallback_ = std::make_shared<ExposureCallbackListener>(env);
4190         cameraSession_->SetExposureCallback(exposureCallback_);
4191     }
4192     exposureCallback_->SaveCallbackReference(eventName, callback, isOnce);
4193 }
4194 
UnregisterExposureCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4195 void CameraSessionNapi::UnregisterExposureCallbackListener(
4196     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4197 {
4198     if (exposureCallback_ == nullptr) {
4199         MEDIA_ERR_LOG("exposureCallback is null");
4200         return;
4201     }
4202     exposureCallback_->RemoveCallbackRef(eventName, callback);
4203 }
4204 
RegisterFocusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4205 void CameraSessionNapi::RegisterFocusCallbackListener(
4206     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4207 {
4208     if (focusCallback_ == nullptr) {
4209         focusCallback_ = make_shared<FocusCallbackListener>(env);
4210         cameraSession_->SetFocusCallback(focusCallback_);
4211     }
4212     focusCallback_->SaveCallbackReference(eventName, callback, isOnce);
4213 }
4214 
UnregisterFocusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4215 void CameraSessionNapi::UnregisterFocusCallbackListener(
4216     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4217 {
4218     if (focusCallback_ == nullptr) {
4219         MEDIA_ERR_LOG("focusCallback is null");
4220         return;
4221     }
4222     focusCallback_->RemoveCallbackRef(eventName, callback);
4223 }
4224 
RegisterMacroStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4225 void CameraSessionNapi::RegisterMacroStatusCallbackListener(
4226     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4227 {
4228     if (!CameraNapiSecurity::CheckSystemApp(env)) {
4229         MEDIA_ERR_LOG("SystemApi on macroStatusChanged is called!");
4230         return;
4231     }
4232     if (macroStatusCallback_ == nullptr) {
4233         macroStatusCallback_ = std::make_shared<MacroStatusCallbackListener>(env);
4234         cameraSession_->SetMacroStatusCallback(macroStatusCallback_);
4235     }
4236     macroStatusCallback_->SaveCallbackReference(eventName, callback, isOnce);
4237 }
4238 
UnregisterMacroStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4239 void CameraSessionNapi::UnregisterMacroStatusCallbackListener(
4240     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4241 {
4242     if (!CameraNapiSecurity::CheckSystemApp(env)) {
4243         MEDIA_ERR_LOG("SystemApi off macroStatusChanged is called!");
4244         return;
4245     }
4246     if (macroStatusCallback_ == nullptr) {
4247         MEDIA_ERR_LOG("macroStatusCallback is null");
4248         return;
4249     }
4250     macroStatusCallback_->RemoveCallbackRef(eventName, callback);
4251 }
4252 
RegisterMoonCaptureBoostCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4253 void CameraSessionNapi::RegisterMoonCaptureBoostCallbackListener(
4254     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4255 {
4256     if (!CameraNapiSecurity::CheckSystemApp(env)) {
4257         MEDIA_ERR_LOG("SystemApi on moonCaptureBoostStatus is called!");
4258         return;
4259     }
4260     if (moonCaptureBoostCallback_ == nullptr) {
4261         moonCaptureBoostCallback_ = std::make_shared<MoonCaptureBoostCallbackListener>(env);
4262         cameraSession_->SetMoonCaptureBoostStatusCallback(moonCaptureBoostCallback_);
4263     }
4264     moonCaptureBoostCallback_->SaveCallbackReference(eventName, callback, isOnce);
4265 }
4266 
UnregisterMoonCaptureBoostCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4267 void CameraSessionNapi::UnregisterMoonCaptureBoostCallbackListener(
4268     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4269 {
4270     if (!CameraNapiSecurity::CheckSystemApp(env)) {
4271         MEDIA_ERR_LOG("SystemApi off moonCaptureBoostStatus is called!");
4272         return;
4273     }
4274     if (moonCaptureBoostCallback_ == nullptr) {
4275         MEDIA_ERR_LOG("macroStatusCallback is null");
4276         return;
4277     }
4278     moonCaptureBoostCallback_->RemoveCallbackRef(eventName, callback);
4279 }
4280 
RegisterFeatureDetectionStatusListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4281 void CameraSessionNapi::RegisterFeatureDetectionStatusListener(
4282     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4283 {
4284     if (!CameraNapiSecurity::CheckSystemApp(env)) {
4285         MEDIA_ERR_LOG("SystemApi on featureDetectionStatus is called!");
4286         return;
4287     }
4288     int32_t featureType = SceneFeature::FEATURE_ENUM_MAX;
4289     CameraNapiParamParser jsParamParser(env, args, featureType);
4290     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "Invalid feature type")) {
4291         MEDIA_ERR_LOG("CameraSessionNapi::RegisterFeatureDetectionStatusListener Invalid feature type");
4292         return;
4293     }
4294     if (featureType < SceneFeature::FEATURE_ENUM_MIN || featureType >= SceneFeature::FEATURE_ENUM_MAX) {
4295         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "scene feature invalid");
4296         MEDIA_ERR_LOG("CameraSessionNapi::RegisterFeatureDetectionStatusListener scene feature invalid");
4297         return;
4298     }
4299 
4300     if (featureDetectionStatusCallback_ == nullptr) {
4301         featureDetectionStatusCallback_ = std::make_shared<FeatureDetectionStatusCallbackListener>(env);
4302         cameraSession_->SetFeatureDetectionStatusCallback(featureDetectionStatusCallback_);
4303     }
4304 
4305     if (featureType == SceneFeature::FEATURE_LOW_LIGHT_BOOST) {
4306         cameraSession_->LockForControl();
4307         cameraSession_->EnableLowLightDetection(true);
4308         cameraSession_->UnlockForControl();
4309     }
4310     if (featureType == SceneFeature::FEATURE_TRIPOD_DETECTION) {
4311         cameraSession_->LockForControl();
4312         cameraSession_->EnableTripodDetection(true);
4313         cameraSession_->UnlockForControl();
4314     }
4315     featureDetectionStatusCallback_->SaveCallbackReference(eventName + std::to_string(featureType), callback, isOnce);
4316 }
4317 
UnregisterFeatureDetectionStatusListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4318 void CameraSessionNapi::UnregisterFeatureDetectionStatusListener(
4319     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4320 {
4321     if (!CameraNapiSecurity::CheckSystemApp(env)) {
4322         MEDIA_ERR_LOG("SystemApi off featureDetectionStatus is called!");
4323         return;
4324     }
4325     if (featureDetectionStatusCallback_ == nullptr) {
4326         MEDIA_WARNING_LOG("featureDetectionStatusCallback_ is null");
4327         return;
4328     }
4329     int32_t featureType = SceneFeature::FEATURE_ENUM_MAX;
4330     CameraNapiParamParser jsParamParser(env, args, featureType);
4331     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "Invalid feature type")) {
4332         MEDIA_ERR_LOG("CameraSessionNapi::RegisterFeatureDetectionStatusListener Invalid feature type");
4333         return;
4334     }
4335     if (featureType < SceneFeature::FEATURE_ENUM_MIN || featureType >= SceneFeature::FEATURE_ENUM_MAX) {
4336         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "scene feature invalid");
4337         MEDIA_ERR_LOG("CameraSessionNapi::RegisterFeatureDetectionStatusListener scene feature invalid");
4338         return;
4339     }
4340 
4341     featureDetectionStatusCallback_->RemoveCallbackRef(eventName + std::to_string(featureType), callback);
4342 
4343     if (featureType == SceneFeature::FEATURE_LOW_LIGHT_BOOST &&
4344         !featureDetectionStatusCallback_->IsFeatureSubscribed(SceneFeature::FEATURE_LOW_LIGHT_BOOST)) {
4345         cameraSession_->LockForControl();
4346         cameraSession_->EnableLowLightDetection(false);
4347         cameraSession_->UnlockForControl();
4348     }
4349     if (featureType == SceneFeature::FEATURE_TRIPOD_DETECTION &&
4350         !featureDetectionStatusCallback_->IsFeatureSubscribed(SceneFeature::FEATURE_TRIPOD_DETECTION)) {
4351         cameraSession_->LockForControl();
4352         cameraSession_->EnableTripodDetection(false);
4353         cameraSession_->UnlockForControl();
4354     }
4355 }
4356 
RegisterSessionErrorCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4357 void CameraSessionNapi::RegisterSessionErrorCallbackListener(
4358     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4359 {
4360     if (sessionCallback_ == nullptr) {
4361         sessionCallback_ = std::make_shared<SessionCallbackListener>(env);
4362         cameraSession_->SetCallback(sessionCallback_);
4363     }
4364     sessionCallback_->SaveCallbackReference(eventName, callback, isOnce);
4365 }
4366 
UnregisterSessionErrorCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4367 void CameraSessionNapi::UnregisterSessionErrorCallbackListener(
4368     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4369 {
4370     if (sessionCallback_ == nullptr) {
4371         MEDIA_ERR_LOG("sessionCallback is null");
4372         return;
4373     }
4374     sessionCallback_->RemoveCallbackRef(eventName, callback);
4375 }
4376 
RegisterEffectSuggestionCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4377 void CameraSessionNapi::RegisterEffectSuggestionCallbackListener(
4378     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4379 {
4380     if (effectSuggestionCallback_ == nullptr) {
4381         auto effectSuggestionCallback = std::make_shared<EffectSuggestionCallbackListener>(env);
4382         effectSuggestionCallback_ = effectSuggestionCallback;
4383         cameraSession_->SetEffectSuggestionCallback(effectSuggestionCallback);
4384     }
4385     effectSuggestionCallback_->SaveCallbackReference(eventName, callback, isOnce);
4386 }
4387 
UnregisterEffectSuggestionCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4388 void CameraSessionNapi::UnregisterEffectSuggestionCallbackListener(
4389     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4390 {
4391     if (effectSuggestionCallback_ == nullptr) {
4392         MEDIA_ERR_LOG("effectSuggestionCallback is null");
4393     } else {
4394         effectSuggestionCallback_->RemoveCallbackRef(eventName, callback);
4395     }
4396 }
4397 
RegisterAbilityChangeCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4398 void CameraSessionNapi::RegisterAbilityChangeCallbackListener(
4399     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4400 {
4401     if (abilityCallback_ == nullptr) {
4402         auto abilityCallback = std::make_shared<AbilityCallbackListener>(env);
4403         abilityCallback_ = abilityCallback;
4404         cameraSession_->SetAbilityCallback(abilityCallback);
4405     }
4406     abilityCallback_->SaveCallbackReference(eventName, callback, isOnce);
4407 }
4408 
UnregisterAbilityChangeCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4409 void CameraSessionNapi::UnregisterAbilityChangeCallbackListener(
4410     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4411 {
4412     if (abilityCallback_ == nullptr) {
4413         MEDIA_ERR_LOG("abilityCallback is null");
4414     } else {
4415         abilityCallback_->RemoveCallbackRef(eventName, callback);
4416     }
4417 }
4418 
RegisterSmoothZoomCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4419 void CameraSessionNapi::RegisterSmoothZoomCallbackListener(
4420     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4421 {
4422     if (smoothZoomCallback_ == nullptr) {
4423         smoothZoomCallback_ = std::make_shared<SmoothZoomCallbackListener>(env);
4424         cameraSession_->SetSmoothZoomCallback(smoothZoomCallback_);
4425     }
4426     smoothZoomCallback_->SaveCallbackReference(eventName, callback, isOnce);
4427 }
4428 
UnregisterSmoothZoomCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4429 void CameraSessionNapi::UnregisterSmoothZoomCallbackListener(
4430     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4431 {
4432     if (smoothZoomCallback_ == nullptr) {
4433         MEDIA_ERR_LOG("smoothZoomCallback is null");
4434         return;
4435     }
4436     smoothZoomCallback_->RemoveCallbackRef(eventName, callback);
4437 }
4438 
RegisterExposureInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4439 void CameraSessionNapi::RegisterExposureInfoCallbackListener(
4440     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4441 {
4442     CameraNapiUtils::ThrowError(
4443         env, CameraErrorCode::OPERATION_NOT_ALLOWED, "this type callback can not be registered in current session!");
4444 }
4445 
UnregisterExposureInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4446 void CameraSessionNapi::UnregisterExposureInfoCallbackListener(
4447     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4448 {
4449     CameraNapiUtils::ThrowError(
4450         env, CameraErrorCode::OPERATION_NOT_ALLOWED, "this type callback can not be unregistered in current session!");
4451 }
4452 
RegisterIsoInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4453 void CameraSessionNapi::RegisterIsoInfoCallbackListener(
4454     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4455 {
4456     CameraNapiUtils::ThrowError(
4457         env, CameraErrorCode::OPERATION_NOT_ALLOWED, "this type callback can not be registered in current session!");
4458 }
4459 
UnregisterIsoInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4460 void CameraSessionNapi::UnregisterIsoInfoCallbackListener(
4461     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4462 {
4463     CameraNapiUtils::ThrowError(
4464         env, CameraErrorCode::OPERATION_NOT_ALLOWED, "this type callback can not be unregistered in current session!");
4465 }
4466 
RegisterApertureInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4467 void CameraSessionNapi::RegisterApertureInfoCallbackListener(
4468     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4469 {
4470     CameraNapiUtils::ThrowError(
4471         env, CameraErrorCode::OPERATION_NOT_ALLOWED, "this type callback can not be registered in current session!");
4472 }
4473 
UnregisterApertureInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4474 void CameraSessionNapi::UnregisterApertureInfoCallbackListener(
4475     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4476 {
4477     CameraNapiUtils::ThrowError(
4478         env, CameraErrorCode::OPERATION_NOT_ALLOWED, "this type callback can not be unregistered in current session!");
4479 }
4480 
RegisterLuminationInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4481 void CameraSessionNapi::RegisterLuminationInfoCallbackListener(
4482     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4483 {
4484     CameraNapiUtils::ThrowError(
4485         env, CameraErrorCode::OPERATION_NOT_ALLOWED, "this type callback can not be registered in current session!");
4486 }
4487 
UnregisterLuminationInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4488 void CameraSessionNapi::UnregisterLuminationInfoCallbackListener(
4489     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4490 {
4491     CameraNapiUtils::ThrowError(
4492         env, CameraErrorCode::OPERATION_NOT_ALLOWED, "this type callback can not be unregistered in current session!");
4493 }
4494 
RegisterSlowMotionStateCb(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4495 void CameraSessionNapi::RegisterSlowMotionStateCb(
4496     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4497 {
4498     CameraNapiUtils::ThrowError(
4499         env, CameraErrorCode::OPERATION_NOT_ALLOWED, "this type callback can not be unregistered in current session!");
4500 }
4501 
UnregisterSlowMotionStateCb(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4502 void CameraSessionNapi::UnregisterSlowMotionStateCb(
4503     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4504 {
4505     CameraNapiUtils::ThrowError(
4506         env, CameraErrorCode::OPERATION_NOT_ALLOWED, "this type callback can not be unregistered in current session!");
4507 }
4508 
RegisterTryAEInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4509 void CameraSessionNapi::RegisterTryAEInfoCallbackListener(
4510     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4511 {
4512     CameraNapiUtils::ThrowError(env, CameraErrorCode::OPERATION_NOT_ALLOWED,
4513         "this type callback can not be registered in current session!");
4514 }
4515 
UnregisterTryAEInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4516 void CameraSessionNapi::UnregisterTryAEInfoCallbackListener(
4517     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4518 {
4519     CameraNapiUtils::ThrowError(env, CameraErrorCode::OPERATION_NOT_ALLOWED,
4520         "this type callback can not be unregistered in current session!");
4521 }
4522 
4523 const CameraSessionNapi::EmitterFunctions CameraSessionNapi::fun_map_ = {
4524     { "exposureStateChange", {
4525         &CameraSessionNapi::RegisterExposureCallbackListener,
4526         &CameraSessionNapi::UnregisterExposureCallbackListener} },
4527     { "focusStateChange", {
4528         &CameraSessionNapi::RegisterFocusCallbackListener,
4529         &CameraSessionNapi::UnregisterFocusCallbackListener } },
4530     { "macroStatusChanged", {
4531         &CameraSessionNapi::RegisterMacroStatusCallbackListener,
4532         &CameraSessionNapi::UnregisterMacroStatusCallbackListener } },
4533     { "moonCaptureBoostStatus", {
4534         &CameraSessionNapi::RegisterMoonCaptureBoostCallbackListener,
4535         &CameraSessionNapi::UnregisterMoonCaptureBoostCallbackListener } },
4536     { "featureDetection", {
4537         &CameraSessionNapi::RegisterFeatureDetectionStatusListener,
4538         &CameraSessionNapi::UnregisterFeatureDetectionStatusListener } },
4539     { "featureDetectionStatus", {
4540         &CameraSessionNapi::RegisterFeatureDetectionStatusListener,
4541         &CameraSessionNapi::UnregisterFeatureDetectionStatusListener } },
4542     { "error", {
4543         &CameraSessionNapi::RegisterSessionErrorCallbackListener,
4544         &CameraSessionNapi::UnregisterSessionErrorCallbackListener } },
4545     { "smoothZoomInfoAvailable", {
4546         &CameraSessionNapi::RegisterSmoothZoomCallbackListener,
4547         &CameraSessionNapi::UnregisterSmoothZoomCallbackListener } },
4548     { "slowMotionStatus", {
4549         &CameraSessionNapi::RegisterSlowMotionStateCb,
4550         &CameraSessionNapi::UnregisterSlowMotionStateCb } },
4551     { "exposureInfoChange", {
4552         &CameraSessionNapi::RegisterExposureInfoCallbackListener,
4553         &CameraSessionNapi::UnregisterExposureInfoCallbackListener} },
4554     { "isoInfoChange", {
4555         &CameraSessionNapi::RegisterIsoInfoCallbackListener,
4556         &CameraSessionNapi::UnregisterIsoInfoCallbackListener } },
4557     { "apertureInfoChange", {
4558         &CameraSessionNapi::RegisterApertureInfoCallbackListener,
4559         &CameraSessionNapi::UnregisterApertureInfoCallbackListener } },
4560     { "luminationInfoChange", {
4561         &CameraSessionNapi::RegisterLuminationInfoCallbackListener,
4562         &CameraSessionNapi::UnregisterLuminationInfoCallbackListener } },
4563     { "abilityChange", {
4564         &CameraSessionNapi::RegisterAbilityChangeCallbackListener,
4565         &CameraSessionNapi::UnregisterAbilityChangeCallbackListener } },
4566     { "effectSuggestionChange", {
4567         &CameraSessionNapi::RegisterEffectSuggestionCallbackListener,
4568         &CameraSessionNapi::UnregisterEffectSuggestionCallbackListener } },
4569     { "tryAEInfoChange", {
4570         &CameraSessionNapi::RegisterTryAEInfoCallbackListener,
4571         &CameraSessionNapi::UnregisterTryAEInfoCallbackListener } },
4572     { "lcdFlashStatus", {
4573         &CameraSessionNapi::RegisterLcdFlashStatusCallbackListener,
4574         &CameraSessionNapi::UnregisterLcdFlashStatusCallbackListener } },
4575     { "autoDeviceSwitchStatusChange", {
4576         &CameraSessionNapi::RegisterAutoDeviceSwitchCallbackListener,
4577         &CameraSessionNapi::UnregisterAutoDeviceSwitchCallbackListener } },
4578 };
4579 
GetEmitterFunctions()4580 const CameraSessionNapi::EmitterFunctions& CameraSessionNapi::GetEmitterFunctions()
4581 {
4582     return fun_map_;
4583 }
4584 
On(napi_env env,napi_callback_info info)4585 napi_value CameraSessionNapi::On(napi_env env, napi_callback_info info)
4586 {
4587     return ListenerTemplate<CameraSessionNapi>::On(env, info);
4588 }
4589 
Once(napi_env env,napi_callback_info info)4590 napi_value CameraSessionNapi::Once(napi_env env, napi_callback_info info)
4591 {
4592     return ListenerTemplate<CameraSessionNapi>::Once(env, info);
4593 }
4594 
Off(napi_env env,napi_callback_info info)4595 napi_value CameraSessionNapi::Off(napi_env env, napi_callback_info info)
4596 {
4597     return ListenerTemplate<CameraSessionNapi>::Off(env, info);
4598 }
4599 
RegisterLcdFlashStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4600 void CameraSessionNapi::RegisterLcdFlashStatusCallbackListener(
4601     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4602 {
4603     if (!CameraNapiSecurity::CheckSystemApp(env)) {
4604         MEDIA_ERR_LOG("SystemApi on LcdFlashStatus is called!");
4605         return;
4606     }
4607     if (cameraSession_ == nullptr) {
4608         MEDIA_ERR_LOG("cameraSession is null!");
4609         return;
4610     }
4611     if (lcdFlashStatusCallback_ == nullptr) {
4612         lcdFlashStatusCallback_ = std::make_shared<LcdFlashStatusCallbackListener>(env);
4613         cameraSession_->SetLcdFlashStatusCallback(lcdFlashStatusCallback_);
4614     }
4615     lcdFlashStatusCallback_->SaveCallbackReference(eventName, callback, isOnce);
4616     cameraSession_->LockForControl();
4617     cameraSession_->EnableLcdFlashDetection(true);
4618     cameraSession_->UnlockForControl();
4619 }
4620 
UnregisterLcdFlashStatusCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4621 void CameraSessionNapi::UnregisterLcdFlashStatusCallbackListener(
4622     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4623 {
4624     if (lcdFlashStatusCallback_ == nullptr) {
4625         MEDIA_ERR_LOG("lcdFlashStatusCallback is null");
4626         return;
4627     }
4628     lcdFlashStatusCallback_->RemoveCallbackRef(eventName, callback);
4629     if (lcdFlashStatusCallback_->IsEmpty("lcdFlashStatus")) {
4630         cameraSession_->LockForControl();
4631         cameraSession_->EnableLcdFlashDetection(false);
4632         cameraSession_->UnlockForControl();
4633     }
4634 }
4635 
IsAutoDeviceSwitchSupported(napi_env env,napi_callback_info info)4636 napi_value CameraSessionNapi::IsAutoDeviceSwitchSupported(napi_env env, napi_callback_info info)
4637 {
4638     MEDIA_INFO_LOG("IsAutoDeviceSwitchSupported is called");
4639     CameraSessionNapi* cameraSessionNapi = nullptr;
4640     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi);
4641     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
4642         MEDIA_ERR_LOG("CameraSessionNapi::IsAutoDeviceSwitchSupported parse parameter occur error");
4643         return nullptr;
4644     }
4645     auto result = CameraNapiUtils::GetUndefinedValue(env);
4646     if (cameraSessionNapi->cameraSession_ != nullptr) {
4647         bool isSupported = cameraSessionNapi->cameraSession_->IsAutoDeviceSwitchSupported();
4648         napi_get_boolean(env, isSupported, &result);
4649     } else {
4650         MEDIA_ERR_LOG("CameraSessionNapi::IsAutoDeviceSwitchSupported get native object fail");
4651         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
4652         return nullptr;
4653     }
4654     return result;
4655 }
4656 
EnableAutoDeviceSwitch(napi_env env,napi_callback_info info)4657 napi_value CameraSessionNapi::EnableAutoDeviceSwitch(napi_env env, napi_callback_info info)
4658 {
4659     MEDIA_DEBUG_LOG("CameraSessionNapi::EnableAutoDeviceSwitch is called");
4660     bool isEnable;
4661     CameraSessionNapi* cameraSessionNapi = nullptr;
4662     CameraNapiParamParser jsParamParser(env, info, cameraSessionNapi, isEnable);
4663     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
4664         MEDIA_ERR_LOG("CameraSessionNapi::EnableAutoDeviceSwitch parse parameter occur error");
4665         return nullptr;
4666     }
4667 
4668     if (cameraSessionNapi->cameraSession_ != nullptr) {
4669         MEDIA_INFO_LOG("CameraSessionNapi::EnableAutoDeviceSwitch:%{public}d", isEnable);
4670         cameraSessionNapi->cameraSession_->LockForControl();
4671         int32_t retCode = cameraSessionNapi->cameraSession_->EnableAutoDeviceSwitch(isEnable);
4672         cameraSessionNapi->cameraSession_->UnlockForControl();
4673         if (!CameraNapiUtils::CheckError(env, retCode)) {
4674             MEDIA_ERR_LOG("CameraSessionNapi::EnableAutoSwitchDevice fail %{public}d", retCode);
4675             return nullptr;
4676         }
4677     } else {
4678         MEDIA_ERR_LOG("CameraSessionNapi::EnableAutoDeviceSwitch get native object fail");
4679         CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
4680         return nullptr;
4681     }
4682     return CameraNapiUtils::GetUndefinedValue(env);
4683 }
4684 
RegisterAutoDeviceSwitchCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)4685 void CameraSessionNapi::RegisterAutoDeviceSwitchCallbackListener(
4686     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
4687 {
4688     if (cameraSession_ == nullptr) {
4689         MEDIA_ERR_LOG("cameraSession is null!");
4690         return;
4691     }
4692     if (autoDeviceSwitchCallback_ == nullptr) {
4693         autoDeviceSwitchCallback_ = std::make_shared<AutoDeviceSwitchCallbackListener>(env);
4694         cameraSession_->SetAutoDeviceSwitchCallback(autoDeviceSwitchCallback_);
4695     }
4696     autoDeviceSwitchCallback_->SaveCallbackReference(eventName, callback, isOnce);
4697 }
4698 
UnregisterAutoDeviceSwitchCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)4699 void CameraSessionNapi::UnregisterAutoDeviceSwitchCallbackListener(
4700     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
4701 {
4702     if (autoDeviceSwitchCallback_ == nullptr) {
4703         MEDIA_ERR_LOG("autoDeviceSwitchCallback is nullptr.");
4704         return;
4705     }
4706     autoDeviceSwitchCallback_->RemoveCallbackRef(eventName, callback);
4707 }
4708 } // namespace CameraStandard
4709 } // namespace OHOS
4710