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