1 /*
2  * Copyright (c) 2024-2024 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 <cstddef>
17 #include <uv.h>
18 #include "camera_napi_const.h"
19 #include "js_native_api.h"
20 #include "mode/profession_session_napi.h"
21 #include "camera_napi_security_utils.h"
22 
23 namespace OHOS {
24 namespace CameraStandard {
25 using namespace std;
26 
27 thread_local napi_ref ProfessionSessionNapi::sConstructor_ = nullptr;
28 
ProfessionSessionNapi()29 ProfessionSessionNapi::ProfessionSessionNapi() : env_(nullptr) {}
~ProfessionSessionNapi()30 ProfessionSessionNapi::~ProfessionSessionNapi()
31 {
32     MEDIA_DEBUG_LOG("~ProfessionSessionNapi is called");
33 }
34 
ProfessionSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)35 void ProfessionSessionNapi::ProfessionSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
36 {
37     MEDIA_DEBUG_LOG("ProfessionSessionNapiDestructor is called");
38     ProfessionSessionNapi* cameraObj = reinterpret_cast<ProfessionSessionNapi*>(nativeObject);
39     if (cameraObj != nullptr) {
40         delete cameraObj;
41     }
42 }
43 
44 const std::vector<napi_property_descriptor> ProfessionSessionNapi::manual_exposure_funcs = {
45     DECLARE_NAPI_FUNCTION("getSupportedMeteringModes", ProfessionSessionNapi::GetSupportedMeteringModes),
46     DECLARE_NAPI_FUNCTION("isExposureMeteringModeSupported", ProfessionSessionNapi::IsMeteringModeSupported),
47     DECLARE_NAPI_FUNCTION("getExposureMeteringMode", ProfessionSessionNapi::GetMeteringMode),
48     DECLARE_NAPI_FUNCTION("setExposureMeteringMode", ProfessionSessionNapi::SetMeteringMode),
49 
50     DECLARE_NAPI_FUNCTION("getExposureDurationRange", ProfessionSessionNapi::GetExposureDurationRange),
51     DECLARE_NAPI_FUNCTION("getExposureDuration", ProfessionSessionNapi::GetExposureDuration),
52     DECLARE_NAPI_FUNCTION("setExposureDuration", ProfessionSessionNapi::SetExposureDuration),
53 };
54 
55 const std::vector<napi_property_descriptor> ProfessionSessionNapi::manual_focus_funcs = {
56     DECLARE_NAPI_FUNCTION("getSupportedFocusAssistFlashModes",
57         ProfessionSessionNapi::GetSupportedFocusAssistFlashModes),
58     DECLARE_NAPI_FUNCTION("isFocusAssistSupported", ProfessionSessionNapi::IsFocusAssistFlashModeSupported),
59     DECLARE_NAPI_FUNCTION("getFocusAssistFlashMode", ProfessionSessionNapi::GetFocusAssistFlashMode),
60     DECLARE_NAPI_FUNCTION("setFocusAssist", ProfessionSessionNapi::SetFocusAssistFlashMode),
61 };
62 
63 const std::vector<napi_property_descriptor> ProfessionSessionNapi::manual_iso_props = {
64     DECLARE_NAPI_FUNCTION("getISORange", ProfessionSessionNapi::GetIsoRange),
65     DECLARE_NAPI_FUNCTION("isManualISOSupported", ProfessionSessionNapi::IsManualIsoSupported),
66     DECLARE_NAPI_FUNCTION("getISO", ProfessionSessionNapi::GetISO),
67     DECLARE_NAPI_FUNCTION("setISO", ProfessionSessionNapi::SetISO),
68 };
69 
70 const std::vector<napi_property_descriptor> ProfessionSessionNapi::pro_session_props = {
71     DECLARE_NAPI_FUNCTION("getSupportedExposureHintModes", ProfessionSessionNapi::GetSupportedExposureHintModes),
72     DECLARE_NAPI_FUNCTION("getExposureHintMode", ProfessionSessionNapi::GetExposureHintMode),
73     DECLARE_NAPI_FUNCTION("setExposureHintMode", ProfessionSessionNapi::SetExposureHintMode),
74 
75     DECLARE_NAPI_FUNCTION("on", ProfessionSessionNapi::On),
76     DECLARE_NAPI_FUNCTION("once", ProfessionSessionNapi::Once),
77     DECLARE_NAPI_FUNCTION("off", ProfessionSessionNapi::Off),
78 };
79 
Init(napi_env env,napi_value exports)80 napi_value ProfessionSessionNapi::Init(napi_env env, napi_value exports)
81 {
82     MEDIA_DEBUG_LOG("Init is called");
83     napi_status status;
84     napi_value ctorObj;
85     std::vector<napi_property_descriptor> manual_exposure_props = CameraSessionNapi::auto_exposure_props;
86     manual_exposure_props.insert(manual_exposure_props.end(), ProfessionSessionNapi::manual_exposure_funcs.begin(),
87                                  ProfessionSessionNapi::manual_exposure_funcs.end());
88     std::vector<napi_property_descriptor> pro_manual_focus_props = CameraSessionNapi::manual_focus_props;
89     pro_manual_focus_props.insert(pro_manual_focus_props.end(), ProfessionSessionNapi::manual_focus_funcs.begin(),
90                                   ProfessionSessionNapi::manual_focus_funcs.end());
91     std::vector<std::vector<napi_property_descriptor>> descriptors = {
92         CameraSessionNapi::camera_process_props, CameraSessionNapi::zoom_props,
93         CameraSessionNapi::color_effect_props, CameraSessionNapi::flash_props,
94         CameraSessionNapi::auto_wb_props, CameraSessionNapi::manual_wb_props,
95         CameraSessionNapi::focus_props, ProfessionSessionNapi::manual_iso_props,
96         ProfessionSessionNapi::auto_wb_props, ProfessionSessionNapi::manual_wb_props,
97         ProfessionSessionNapi::pro_session_props, aperture_props,
98         manual_exposure_props, pro_manual_focus_props };
99     std::vector<napi_property_descriptor> professional_session_props =
100         CameraNapiUtils::GetPropertyDescriptor(descriptors);
101     status = napi_define_class(env, PROFESSIONAL_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
102                                ProfessionSessionNapiConstructor, nullptr,
103                                professional_session_props.size(),
104                                professional_session_props.data(), &ctorObj);
105     if (status == napi_ok) {
106         int32_t refCount = 1;
107         status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
108         if (status == napi_ok) {
109             status = napi_set_named_property(env, exports, PROFESSIONAL_SESSION_NAPI_CLASS_NAME, ctorObj);
110             if (status == napi_ok) {
111                 return exports;
112             }
113         }
114     }
115     MEDIA_ERR_LOG("Init call Failed!");
116     return nullptr;
117 }
118 
CreateCameraSession(napi_env env,SceneMode mode)119 napi_value ProfessionSessionNapi::CreateCameraSession(napi_env env, SceneMode mode)
120 {
121     MEDIA_DEBUG_LOG("CreateCameraSession is called");
122     CAMERA_SYNC_TRACE;
123     napi_status status;
124     napi_value result = nullptr;
125     napi_value constructor;
126     status = napi_get_reference_value(env, sConstructor_, &constructor);
127     if (status == napi_ok) {
128         sCameraSession_ = CameraManager::GetInstance()->CreateCaptureSession(mode);
129         if (sCameraSession_ == nullptr) {
130             MEDIA_ERR_LOG("Failed to create Profession session instance");
131             napi_get_undefined(env, &result);
132             return result;
133         }
134         status = napi_new_instance(env, constructor, 0, nullptr, &result);
135         sCameraSession_ = nullptr;
136         if (status == napi_ok && result != nullptr) {
137             MEDIA_DEBUG_LOG("success to create Profession session napi instance");
138             return result;
139         } else {
140             MEDIA_ERR_LOG("Failed to create Profession session napi instance");
141         }
142     }
143     MEDIA_ERR_LOG("Failed to create Profession session napi instance last");
144     napi_get_undefined(env, &result);
145     return result;
146 }
147 
ProfessionSessionNapiConstructor(napi_env env,napi_callback_info info)148 napi_value ProfessionSessionNapi::ProfessionSessionNapiConstructor(napi_env env, napi_callback_info info)
149 {
150     MEDIA_DEBUG_LOG("ProfessionSessionNapiConstructor is called");
151     napi_status status;
152     napi_value result = nullptr;
153     napi_value thisVar = nullptr;
154 
155     napi_get_undefined(env, &result);
156     CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
157 
158     if (status == napi_ok && thisVar != nullptr) {
159         std::unique_ptr<ProfessionSessionNapi> obj = std::make_unique<ProfessionSessionNapi>();
160         obj->env_ = env;
161         if (sCameraSession_ == nullptr) {
162             MEDIA_ERR_LOG("sCameraSession_ is null");
163             return result;
164         }
165         obj->professionSession_ = static_cast<ProfessionSession*>(sCameraSession_.GetRefPtr());
166         obj->cameraSession_ = obj->professionSession_;
167         if (obj->professionSession_ == nullptr) {
168             MEDIA_ERR_LOG("professionSession_ is null");
169             return result;
170         }
171         status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
172             ProfessionSessionNapi::ProfessionSessionNapiDestructor, nullptr, nullptr);
173         if (status == napi_ok) {
174             obj.release();
175             return thisVar;
176         } else {
177             MEDIA_ERR_LOG("ProfessionSessionNapi Failure wrapping js to native napi");
178         }
179     }
180     MEDIA_ERR_LOG("ProfessionSessionNapi call Failed!");
181     return result;
182 }
183 // MeteringMode
GetSupportedMeteringModes(napi_env env,napi_callback_info info)184 napi_value ProfessionSessionNapi::GetSupportedMeteringModes(napi_env env, napi_callback_info info)
185 {
186     MEDIA_DEBUG_LOG("GetSupportedMeteringModes is called");
187     napi_status status;
188     napi_value result = nullptr;
189     size_t argc = ARGS_ZERO;
190     napi_value argv[ARGS_ZERO];
191     napi_value thisVar = nullptr;
192 
193     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
194 
195     napi_get_undefined(env, &result);
196     status = napi_create_array(env, &result);
197     if (status != napi_ok) {
198         MEDIA_ERR_LOG("napi_create_array call Failed!");
199         return result;
200     }
201     ProfessionSessionNapi* professionSessionNapi = nullptr;
202     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
203     if (status == napi_ok && professionSessionNapi != nullptr && professionSessionNapi->professionSession_ != nullptr) {
204         std::vector<MeteringMode> meteringModes;
205         int32_t retCode = professionSessionNapi->professionSession_->GetSupportedMeteringModes(meteringModes);
206         if (!CameraNapiUtils::CheckError(env, retCode)) {
207             return nullptr;
208         }
209         MEDIA_INFO_LOG("ProfessionSessionNapi::GetSupportedMeteringModes len = %{public}zu",
210             meteringModes.size());
211         if (!meteringModes.empty()) {
212             for (size_t i = 0; i < meteringModes.size(); i++) {
213                 MeteringMode mode = meteringModes[i];
214                 napi_value value;
215                 napi_create_int32(env, mode, &value);
216                 napi_set_element(env, result, i, value);
217             }
218         }
219     } else {
220         MEDIA_ERR_LOG("GetSupportedMeteringModes call Failed!");
221     }
222     return result;
223 }
224 
IsMeteringModeSupported(napi_env env,napi_callback_info info)225 napi_value ProfessionSessionNapi::IsMeteringModeSupported(napi_env env, napi_callback_info info)
226 {
227     MEDIA_DEBUG_LOG("IsMeteringModeSupported is called");
228     napi_status status;
229     napi_value result = nullptr;
230     size_t argc = ARGS_ONE;
231     napi_value argv[ARGS_ONE] = {0};
232     napi_value thisVar = nullptr;
233 
234     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
235 
236     napi_get_undefined(env, &result);
237     ProfessionSessionNapi* professionSessionNapi = nullptr;
238     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
239     if (status == napi_ok && professionSessionNapi != nullptr) {
240         int32_t value;
241         napi_get_value_int32(env, argv[PARAM0], &value);
242         MeteringMode mode = (MeteringMode)value;
243         bool isSupported;
244         int32_t retCode = professionSessionNapi->professionSession_->IsMeteringModeSupported(mode, isSupported);
245         if (!CameraNapiUtils::CheckError(env, retCode)) {
246             return nullptr;
247         }
248         napi_get_boolean(env, isSupported, &result);
249     } else {
250         MEDIA_ERR_LOG("IsMeteringModeSupported call Failed!");
251     }
252     return result;
253 }
254 
GetMeteringMode(napi_env env,napi_callback_info info)255 napi_value ProfessionSessionNapi::GetMeteringMode(napi_env env, napi_callback_info info)
256 {
257     MEDIA_DEBUG_LOG("GetMeteringMode is called");
258     napi_status status;
259     napi_value result = nullptr;
260     size_t argc = ARGS_ZERO;
261     napi_value argv[ARGS_ZERO];
262     napi_value thisVar = nullptr;
263 
264     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
265 
266     napi_get_undefined(env, &result);
267     ProfessionSessionNapi* professionSessionNapi = nullptr;
268     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
269     if (status == napi_ok && professionSessionNapi != nullptr && professionSessionNapi->professionSession_ != nullptr) {
270         MeteringMode mode;
271         int32_t retCode = professionSessionNapi->professionSession_->GetMeteringMode(mode);
272         if (!CameraNapiUtils::CheckError(env, retCode)) {
273             return nullptr;
274         }
275         napi_create_int32(env, mode, &result);
276     } else {
277         MEDIA_ERR_LOG("GetMeteringMode call Failed!");
278     }
279     return result;
280 }
281 
SetMeteringMode(napi_env env,napi_callback_info info)282 napi_value ProfessionSessionNapi::SetMeteringMode(napi_env env, napi_callback_info info)
283 {
284     MEDIA_DEBUG_LOG("SetMeteringMode is called");
285     CAMERA_SYNC_TRACE;
286     napi_status status;
287     napi_value result = nullptr;
288     size_t argc = ARGS_ONE;
289     napi_value argv[ARGS_ONE] = {0};
290     napi_value thisVar = nullptr;
291 
292     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
293 
294     napi_get_undefined(env, &result);
295     ProfessionSessionNapi* professionSessionNapi = nullptr;
296     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
297     if (status == napi_ok && professionSessionNapi != nullptr && professionSessionNapi->professionSession_ != nullptr) {
298         int32_t value;
299         napi_get_value_int32(env, argv[PARAM0], &value);
300         MeteringMode mode = static_cast<MeteringMode>(value);
301         professionSessionNapi->professionSession_->LockForControl();
302         professionSessionNapi->professionSession_->
303                 SetMeteringMode(static_cast<MeteringMode>(mode));
304         MEDIA_INFO_LOG("ProfessionSessionNapi SetMeteringMode set meteringMode %{public}d!", mode);
305         professionSessionNapi->professionSession_->UnlockForControl();
306     } else {
307         MEDIA_ERR_LOG("SetMeteringMode call Failed!");
308     }
309     return result;
310 }
311 // ExposureDuration
GetExposureDurationRange(napi_env env,napi_callback_info info)312 napi_value ProfessionSessionNapi::GetExposureDurationRange(napi_env env, napi_callback_info info)
313 {
314     MEDIA_DEBUG_LOG("getExposureDurationRange is called");
315     napi_status status;
316     napi_value result = nullptr;
317     size_t argc = ARGS_ZERO;
318     napi_value argv[ARGS_ZERO];
319     napi_value thisVar = nullptr;
320 
321     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
322 
323     napi_get_undefined(env, &result);
324     ProfessionSessionNapi* professionSessionNapi = nullptr;
325     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
326     if (status == napi_ok && professionSessionNapi != nullptr) {
327         std::vector<uint32_t> vecExposureList;
328         int32_t retCode = professionSessionNapi->professionSession_->GetSensorExposureTimeRange(vecExposureList);
329         if (!CameraNapiUtils::CheckError(env, retCode)) {
330             return nullptr;
331         }
332         if (vecExposureList.empty() || napi_create_array(env, &result) != napi_ok) {
333             return result;
334         }
335         for (size_t i = 0; i < vecExposureList.size(); i++) {
336             uint32_t exposure = vecExposureList[i];
337             MEDIA_DEBUG_LOG("EXPOSURE_RANGE : exposureDuration = %{public}d", vecExposureList[i]);
338             napi_value value;
339             napi_create_uint32(env, exposure, &value);
340             napi_set_element(env, result, i, value);
341         }
342         MEDIA_DEBUG_LOG("EXPOSURE_RANGE ExposureList size : %{public}zu", vecExposureList.size());
343     } else {
344         MEDIA_ERR_LOG("getExposureDurationRange call Failed!");
345     }
346     return result;
347 }
348 
GetExposureDuration(napi_env env,napi_callback_info info)349 napi_value ProfessionSessionNapi::GetExposureDuration(napi_env env, napi_callback_info info)
350 {
351     MEDIA_DEBUG_LOG("GetExposureDuration is called");
352     napi_status status;
353     napi_value result = nullptr;
354     size_t argc = ARGS_ZERO;
355     napi_value argv[ARGS_ZERO];
356     napi_value thisVar = nullptr;
357 
358     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
359 
360     napi_get_undefined(env, &result);
361     ProfessionSessionNapi* professionSessionNapi = nullptr;
362     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
363     if (status == napi_ok && professionSessionNapi!= nullptr) {
364         uint32_t exposureDurationValue;
365         int32_t retCode = professionSessionNapi->professionSession_->GetSensorExposureTime(exposureDurationValue);
366         if (!CameraNapiUtils::CheckError(env, retCode)) {
367             return nullptr;
368         }
369         MEDIA_DEBUG_LOG("GetExposureDuration : exposureDuration = %{public}d", exposureDurationValue);
370         napi_create_uint32(env, exposureDurationValue, &result);
371     } else {
372         MEDIA_ERR_LOG("GetExposureDuration call Failed!");
373     }
374     return result;
375 }
376 
SetExposureDuration(napi_env env,napi_callback_info info)377 napi_value ProfessionSessionNapi::SetExposureDuration(napi_env env, napi_callback_info info)
378 {
379     MEDIA_DEBUG_LOG("SetExposureDuration is called");
380     CAMERA_SYNC_TRACE;
381     napi_status status;
382     napi_value result = nullptr;
383     size_t argc = ARGS_ONE;
384     napi_value argv[ARGS_ONE] = {0};
385     napi_value thisVar = nullptr;
386 
387     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
388 
389     napi_get_undefined(env, &result);
390     ProfessionSessionNapi* professionSessionNapi = nullptr;
391     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
392     if (status == napi_ok && professionSessionNapi != nullptr) {
393         uint32_t exposureDurationValue;
394         napi_get_value_uint32(env, argv[PARAM0], &exposureDurationValue);
395         MEDIA_DEBUG_LOG("SetExposureDuration : exposureDuration = %{public}d", exposureDurationValue);
396         professionSessionNapi->professionSession_->LockForControl();
397         int32_t retCode = professionSessionNapi->professionSession_->SetSensorExposureTime(exposureDurationValue);
398         professionSessionNapi->professionSession_->UnlockForControl();
399         if (!CameraNapiUtils::CheckError(env, retCode)) {
400             return result;
401         }
402     } else {
403         MEDIA_ERR_LOG("SetExposureDuration call Failed!");
404     }
405     return result;
406 }
407 
408 // FocusAssistFlashMode
GetSupportedFocusAssistFlashModes(napi_env env,napi_callback_info info)409 napi_value ProfessionSessionNapi::GetSupportedFocusAssistFlashModes(napi_env env, napi_callback_info info)
410 {
411     MEDIA_DEBUG_LOG("GetSupportedFocusAssistFlashModes is called");
412     napi_status status;
413     napi_value result = nullptr;
414     size_t argc = ARGS_ZERO;
415     napi_value argv[ARGS_ZERO];
416     napi_value thisVar = nullptr;
417 
418     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
419 
420     napi_get_undefined(env, &result);
421     status = napi_create_array(env, &result);
422     if (status != napi_ok) {
423         MEDIA_ERR_LOG("napi_create_array call Failed!");
424         return result;
425     }
426     ProfessionSessionNapi* professionSessionNapi = nullptr;
427     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
428     if (status == napi_ok && professionSessionNapi != nullptr && professionSessionNapi->professionSession_ != nullptr) {
429         std::vector<FocusAssistFlashMode> focusAssistFlashs;
430         int32_t retCode =
431             professionSessionNapi->professionSession_->GetSupportedFocusAssistFlashModes(focusAssistFlashs);
432         if (!CameraNapiUtils::CheckError(env, retCode)) {
433             return nullptr;
434         }
435         MEDIA_INFO_LOG("ProfessionSessionNapi::GetSupportedFocusAssistFlashModes len = %{public}zu",
436             focusAssistFlashs.size());
437         if (!focusAssistFlashs.empty()) {
438             for (size_t i = 0; i < focusAssistFlashs.size(); i++) {
439                 FocusAssistFlashMode mode = focusAssistFlashs[i];
440                 napi_value value;
441                 napi_create_int32(env, mode, &value);
442                 napi_set_element(env, result, i, value);
443             }
444         }
445     } else {
446         MEDIA_ERR_LOG("GetSupportedFocusAssistFlashModes call Failed!");
447     }
448     return result;
449 }
450 
IsFocusAssistFlashModeSupported(napi_env env,napi_callback_info info)451 napi_value ProfessionSessionNapi::IsFocusAssistFlashModeSupported(napi_env env, napi_callback_info info)
452 {
453     MEDIA_DEBUG_LOG("IsFocusAssistFlashModeSupported is called");
454     napi_status status;
455     napi_value result = nullptr;
456     size_t argc = ARGS_ONE;
457     napi_value argv[ARGS_ONE] = {0};
458     napi_value thisVar = nullptr;
459 
460     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
461 
462     napi_get_undefined(env, &result);
463     ProfessionSessionNapi* professionSessionNapi = nullptr;
464     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
465     if (status == napi_ok && professionSessionNapi != nullptr) {
466         int32_t value;
467         napi_get_value_int32(env, argv[PARAM0], &value);
468         FocusAssistFlashMode mode = static_cast<FocusAssistFlashMode>(value);
469         bool isSupported;
470         int32_t retCode = professionSessionNapi->professionSession_->IsFocusAssistFlashModeSupported(mode, isSupported);
471         if (!CameraNapiUtils::CheckError(env, retCode)) {
472             return nullptr;
473         }
474         napi_get_boolean(env, isSupported, &result);
475     } else {
476         MEDIA_ERR_LOG("IsFocusAssistFlashModeSupported call Failed!");
477     }
478     return result;
479 }
480 
GetFocusAssistFlashMode(napi_env env,napi_callback_info info)481 napi_value ProfessionSessionNapi::GetFocusAssistFlashMode(napi_env env, napi_callback_info info)
482 {
483     MEDIA_DEBUG_LOG("GetFocusAssistFlashMode is called");
484     napi_status status;
485     napi_value result = nullptr;
486     size_t argc = ARGS_ZERO;
487     napi_value argv[ARGS_ZERO];
488     napi_value thisVar = nullptr;
489 
490     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
491 
492     napi_get_undefined(env, &result);
493     ProfessionSessionNapi* professionSessionNapi = nullptr;
494     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
495     if (status == napi_ok && professionSessionNapi != nullptr && professionSessionNapi->professionSession_ != nullptr) {
496         FocusAssistFlashMode mode;
497         int32_t retCode = professionSessionNapi->professionSession_->GetFocusAssistFlashMode(mode);
498         if (!CameraNapiUtils::CheckError(env, retCode)) {
499             return nullptr;
500         }
501         napi_create_int32(env, mode, &result);
502     } else {
503         MEDIA_ERR_LOG("GetFocusAssistFlashMode call Failed!");
504     }
505     return result;
506 }
507 
SetFocusAssistFlashMode(napi_env env,napi_callback_info info)508 napi_value ProfessionSessionNapi::SetFocusAssistFlashMode(napi_env env, napi_callback_info info)
509 {
510     MEDIA_DEBUG_LOG("SetFocusAssistFlashMode is called");
511     CAMERA_SYNC_TRACE;
512     napi_status status;
513     napi_value result = nullptr;
514     size_t argc = ARGS_ONE;
515     napi_value argv[ARGS_ONE] = {0};
516     napi_value thisVar = nullptr;
517 
518     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
519 
520     napi_get_undefined(env, &result);
521     ProfessionSessionNapi* professionSessionNapi = nullptr;
522     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
523     if (status == napi_ok && professionSessionNapi != nullptr && professionSessionNapi->professionSession_ != nullptr) {
524         bool value;
525         napi_get_value_bool(env, argv[PARAM0], &value);
526         FocusAssistFlashMode mode = FOCUS_ASSIST_FLASH_MODE_OFF;
527         if (value == true) {
528             mode = FOCUS_ASSIST_FLASH_MODE_AUTO;
529         }
530         professionSessionNapi->professionSession_->LockForControl();
531         professionSessionNapi->professionSession_->
532                 SetFocusAssistFlashMode(mode);
533         MEDIA_INFO_LOG("ProfessionSessionNapi SetFocusAssistFlashMode set focusAssistFlash %{public}d!", mode);
534         professionSessionNapi->professionSession_->UnlockForControl();
535     } else {
536         MEDIA_ERR_LOG("SetFocusAssistFlashMode call Failed!");
537     }
538     return result;
539 }
540 
GetIsoRange(napi_env env,napi_callback_info info)541 napi_value ProfessionSessionNapi::GetIsoRange(napi_env env, napi_callback_info info)
542 {
543     MEDIA_DEBUG_LOG("GetIsoRange is called");
544     napi_status status;
545     napi_value result = nullptr;
546     size_t argc = ARGS_ZERO;
547     napi_value argv[ARGS_ZERO];
548     napi_value thisVar = nullptr;
549 
550     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
551 
552     napi_get_undefined(env, &result);
553 
554     ProfessionSessionNapi* professionSessionNapi = nullptr;
555     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
556     if (status == napi_ok && professionSessionNapi != nullptr) {
557         std::vector<int32_t> vecIsoList;
558         int32_t retCode = professionSessionNapi->professionSession_->GetIsoRange(vecIsoList);
559         if (!CameraNapiUtils::CheckError(env, retCode)) {
560             return nullptr;
561         }
562         MEDIA_INFO_LOG("ProfessionSessionNapi::GetIsoRange len = %{public}zu", vecIsoList.size());
563 
564         if (!vecIsoList.empty() && napi_create_array(env, &result) == napi_ok) {
565             for (size_t i = 0; i < vecIsoList.size(); i++) {
566                 int32_t iso = vecIsoList[i];
567                 napi_value value;
568                 napi_create_int32(env, iso, &value);
569                 napi_set_element(env, result, i, value);
570             }
571         } else {
572             MEDIA_ERR_LOG("vecIsoList is empty or failed to create array!");
573         }
574     } else {
575         MEDIA_ERR_LOG("GetIsoRange call Failed!");
576     }
577     return result;
578 }
579 
IsManualIsoSupported(napi_env env,napi_callback_info info)580 napi_value ProfessionSessionNapi::IsManualIsoSupported(napi_env env, napi_callback_info info)
581 {
582     if (!CameraNapiSecurity::CheckSystemApp(env)) {
583         MEDIA_ERR_LOG("SystemApi IsManualIsoSupported is called!");
584         return nullptr;
585     }
586     MEDIA_DEBUG_LOG("IsManualIsoSupported is called");
587     napi_status status;
588     napi_value result = nullptr;
589     size_t argc = ARGS_ZERO;
590     napi_value argv[ARGS_ZERO];
591     napi_value thisVar = nullptr;
592 
593     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
594 
595     napi_get_undefined(env, &result);
596     ProfessionSessionNapi* professionSessionNapi = nullptr;
597     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
598     if (status == napi_ok && professionSessionNapi != nullptr && professionSessionNapi->professionSession_ != nullptr) {
599         bool isSupported = professionSessionNapi->professionSession_->IsManualIsoSupported();
600         napi_get_boolean(env, isSupported, &result);
601     } else {
602         MEDIA_ERR_LOG("IsManualIsoSupported call Failed!");
603     }
604     return result;
605 }
606 
GetISO(napi_env env,napi_callback_info info)607 napi_value ProfessionSessionNapi::GetISO(napi_env env, napi_callback_info info)
608 {
609     MEDIA_DEBUG_LOG("GetISO is called");
610     napi_status status;
611     napi_value result = nullptr;
612     size_t argc = ARGS_ZERO;
613     napi_value argv[ARGS_ZERO];
614     napi_value thisVar = nullptr;
615 
616     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
617 
618     napi_get_undefined(env, &result);
619     ProfessionSessionNapi* professionSessionNapi = nullptr;
620     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
621     if (status == napi_ok && professionSessionNapi != nullptr && professionSessionNapi->professionSession_ != nullptr) {
622         int32_t iso;
623         int32_t retCode = professionSessionNapi->professionSession_->GetISO(iso);
624         if (!CameraNapiUtils::CheckError(env, retCode)) {
625             return nullptr;
626         }
627         napi_create_int32(env, iso, &result);
628     } else {
629         MEDIA_ERR_LOG("GetISO call Failed!");
630     }
631     return result;
632 }
633 
SetISO(napi_env env,napi_callback_info info)634 napi_value ProfessionSessionNapi::SetISO(napi_env env, napi_callback_info info)
635 {
636     MEDIA_DEBUG_LOG("SetISO is called");
637     CAMERA_SYNC_TRACE;
638     napi_status status;
639     napi_value result = nullptr;
640     size_t argc = ARGS_ONE;
641     napi_value argv[ARGS_ONE] = {0};
642     napi_value thisVar = nullptr;
643 
644     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
645 
646     napi_get_undefined(env, &result);
647     ProfessionSessionNapi* professionSessionNapi = nullptr;
648     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
649     if (status == napi_ok && professionSessionNapi != nullptr && professionSessionNapi->professionSession_ != nullptr) {
650         int32_t iso;
651         napi_get_value_int32(env, argv[PARAM0], &iso);
652         professionSessionNapi->professionSession_->LockForControl();
653         professionSessionNapi->professionSession_->SetISO(iso);
654         MEDIA_INFO_LOG("ProfessionSessionNapi::SetISO set iso:%{public}d", iso);
655         professionSessionNapi->professionSession_->UnlockForControl();
656     } else {
657         MEDIA_ERR_LOG("SetISO call Failed!");
658     }
659     return result;
660 }
661 
662 // ExposureHintMode
GetSupportedExposureHintModes(napi_env env,napi_callback_info info)663 napi_value ProfessionSessionNapi::GetSupportedExposureHintModes(napi_env env, napi_callback_info info)
664 {
665     MEDIA_DEBUG_LOG("GetSupportedExposureHintModes is called");
666     napi_status status;
667     napi_value result = nullptr;
668     size_t argc = ARGS_ZERO;
669     napi_value argv[ARGS_ZERO];
670     napi_value thisVar = nullptr;
671 
672     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
673 
674     napi_get_undefined(env, &result);
675     status = napi_create_array(env, &result);
676     if (status != napi_ok) {
677         MEDIA_ERR_LOG("napi_create_array call Failed!");
678         return result;
679     }
680     ProfessionSessionNapi* professionSessionNapi = nullptr;
681     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
682     if (status == napi_ok && professionSessionNapi != nullptr && professionSessionNapi->professionSession_ != nullptr) {
683         std::vector<ExposureHintMode> exposureHints;
684         int32_t retCode =
685             professionSessionNapi->professionSession_->GetSupportedExposureHintModes(exposureHints);
686         if (!CameraNapiUtils::CheckError(env, retCode)) {
687             return nullptr;
688         }
689         MEDIA_INFO_LOG("ProfessionSessionNapi::GetSupportedExposureHintModes len = %{public}zu",
690             exposureHints.size());
691         if (!exposureHints.empty()) {
692             for (size_t i = 0; i < exposureHints.size(); i++) {
693                 ExposureHintMode mode = exposureHints[i];
694                 napi_value value;
695                 napi_create_int32(env, mode, &value);
696                 napi_set_element(env, result, i, value);
697             }
698         }
699     } else {
700         MEDIA_ERR_LOG("GetSupportedExposureHintModes call Failed!");
701     }
702     return result;
703 }
704 
GetExposureHintMode(napi_env env,napi_callback_info info)705 napi_value ProfessionSessionNapi::GetExposureHintMode(napi_env env, napi_callback_info info)
706 {
707     MEDIA_DEBUG_LOG("GetExposureHintMode is called");
708     napi_status status;
709     napi_value result = nullptr;
710     size_t argc = ARGS_ZERO;
711     napi_value argv[ARGS_ZERO];
712     napi_value thisVar = nullptr;
713 
714     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
715 
716     napi_get_undefined(env, &result);
717     ProfessionSessionNapi* professionSessionNapi = nullptr;
718     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
719     if (status == napi_ok && professionSessionNapi != nullptr && professionSessionNapi->professionSession_ != nullptr) {
720         ExposureHintMode mode = EXPOSURE_HINT_UNSUPPORTED;
721         int32_t retCode = professionSessionNapi->professionSession_->GetExposureHintMode(mode);
722         if (!CameraNapiUtils::CheckError(env, retCode)) {
723             return nullptr;
724         }
725         napi_create_int32(env, mode, &result);
726     } else {
727         MEDIA_ERR_LOG("GetExposureHintMode call Failed!");
728     }
729     return result;
730 }
731 
SetExposureHintMode(napi_env env,napi_callback_info info)732 napi_value ProfessionSessionNapi::SetExposureHintMode(napi_env env, napi_callback_info info)
733 {
734     MEDIA_DEBUG_LOG("SetExposureHintMode is called");
735     CAMERA_SYNC_TRACE;
736     napi_status status;
737     napi_value result = nullptr;
738     size_t argc = ARGS_ONE;
739     napi_value argv[ARGS_ONE] = {0};
740     napi_value thisVar = nullptr;
741 
742     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
743 
744     napi_get_undefined(env, &result);
745     ProfessionSessionNapi* professionSessionNapi = nullptr;
746     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&professionSessionNapi));
747     if (status == napi_ok && professionSessionNapi != nullptr && professionSessionNapi->professionSession_ != nullptr) {
748         int32_t value;
749         napi_get_value_int32(env, argv[PARAM0], &value);
750         ExposureHintMode mode = static_cast<ExposureHintMode>(value);
751         professionSessionNapi->professionSession_->LockForControl();
752         professionSessionNapi->professionSession_->
753                 SetExposureHintMode(static_cast<ExposureHintMode>(mode));
754         MEDIA_INFO_LOG("ProfessionSessionNapi SetExposureHintMode set exposureHint %{public}d!", mode);
755         professionSessionNapi->professionSession_->UnlockForControl();
756     } else {
757         MEDIA_ERR_LOG("SetExposureHintMode call Failed!");
758     }
759     return result;
760 }
761 
RegisterAbilityChangeCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)762 void ProfessionSessionNapi::RegisterAbilityChangeCallbackListener(
763     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
764 {
765     if (abilityCallback_ == nullptr) {
766         abilityCallback_ = std::make_shared<AbilityCallbackListener>(env);
767         professionSession_->SetAbilityCallback(abilityCallback_);
768     }
769     abilityCallback_->SaveCallbackReference(eventName, callback, isOnce);
770 }
771 
UnregisterAbilityChangeCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)772 void ProfessionSessionNapi::UnregisterAbilityChangeCallbackListener(
773     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
774 {
775     if (abilityCallback_ == nullptr) {
776         MEDIA_ERR_LOG("abilityCallback is null");
777     } else {
778         abilityCallback_->RemoveCallbackRef(eventName, callback);
779     }
780 }
781 
RegisterExposureInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)782 void ProfessionSessionNapi::RegisterExposureInfoCallbackListener(
783     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
784 {
785     if (exposureInfoCallback_ == nullptr) {
786         exposureInfoCallback_ = std::make_shared<ExposureInfoCallbackListener>(env);
787         professionSession_->SetExposureInfoCallback(exposureInfoCallback_);
788     }
789     exposureInfoCallback_->SaveCallbackReference(eventName, callback, isOnce);
790 }
791 
UnregisterExposureInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)792 void ProfessionSessionNapi::UnregisterExposureInfoCallbackListener(
793     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
794 {
795     if (exposureInfoCallback_ == nullptr) {
796         MEDIA_ERR_LOG("abilityCallback is null");
797     } else {
798         exposureInfoCallback_->RemoveCallbackRef(eventName, callback);
799     }
800 }
801 
RegisterIsoInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)802 void ProfessionSessionNapi::RegisterIsoInfoCallbackListener(
803     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
804 {
805     if (isoInfoCallback_ == nullptr) {
806         isoInfoCallback_ = std::make_shared<IsoInfoCallbackListener>(env);
807         professionSession_->SetIsoInfoCallback(isoInfoCallback_);
808     }
809     isoInfoCallback_->SaveCallbackReference(eventName, callback, isOnce);
810 }
811 
UnregisterIsoInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)812 void ProfessionSessionNapi::UnregisterIsoInfoCallbackListener(
813     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
814 {
815     if (isoInfoCallback_ == nullptr) {
816         MEDIA_ERR_LOG("abilityCallback is null");
817     } else {
818         isoInfoCallback_->RemoveCallbackRef(eventName, callback);
819     }
820 }
821 
RegisterApertureInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)822 void ProfessionSessionNapi::RegisterApertureInfoCallbackListener(
823     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
824 {
825     if (apertureInfoCallback_ == nullptr) {
826         apertureInfoCallback_ = std::make_shared<ApertureInfoCallbackListener>(env);
827         professionSession_->SetApertureInfoCallback(apertureInfoCallback_);
828     }
829     apertureInfoCallback_->SaveCallbackReference(eventName, callback, isOnce);
830 }
831 
UnregisterApertureInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)832 void ProfessionSessionNapi::UnregisterApertureInfoCallbackListener(
833     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
834 {
835     if (apertureInfoCallback_ == nullptr) {
836         MEDIA_ERR_LOG("apertureInfoCallback is null");
837     } else {
838         apertureInfoCallback_->RemoveCallbackRef(eventName, callback);
839     }
840 }
841 
RegisterLuminationInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args,bool isOnce)842 void ProfessionSessionNapi::RegisterLuminationInfoCallbackListener(
843     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args, bool isOnce)
844 {
845     if (luminationInfoCallback_ == nullptr) {
846         ExposureHintMode mode = EXPOSURE_HINT_MODE_ON;
847         professionSession_->LockForControl();
848         professionSession_->SetExposureHintMode(mode);
849         professionSession_->UnlockForControl();
850         MEDIA_INFO_LOG("ProfessionSessionNapi SetExposureHintMode set exposureHint %{public}d!", mode);
851         luminationInfoCallback_ = std::make_shared<LuminationInfoCallbackListener>(env);
852         professionSession_->SetLuminationInfoCallback(luminationInfoCallback_);
853     }
854     luminationInfoCallback_->SaveCallbackReference(eventName, callback, isOnce);
855 }
856 
UnregisterLuminationInfoCallbackListener(const std::string & eventName,napi_env env,napi_value callback,const std::vector<napi_value> & args)857 void ProfessionSessionNapi::UnregisterLuminationInfoCallbackListener(
858     const std::string& eventName, napi_env env, napi_value callback, const std::vector<napi_value>& args)
859 {
860     if (luminationInfoCallback_ == nullptr) {
861         MEDIA_ERR_LOG("abilityCallback is null");
862     } else {
863         ExposureHintMode mode = EXPOSURE_HINT_MODE_OFF;
864         professionSession_->LockForControl();
865         professionSession_->SetExposureHintMode(mode);
866         professionSession_->UnlockForControl();
867         MEDIA_INFO_LOG("ProfessionSessionNapi SetExposureHintMode set exposureHint %{public}d!", mode);
868         luminationInfoCallback_->RemoveCallbackRef(eventName, callback);
869     }
870 }
871 
OnExposureInfoChangedCallbackAsync(ExposureInfo info) const872 void ExposureInfoCallbackListener::OnExposureInfoChangedCallbackAsync(ExposureInfo info) const
873 {
874     MEDIA_DEBUG_LOG("OnExposureInfoChangedCallbackAsync is called");
875     uv_loop_s* loop = nullptr;
876     napi_get_uv_event_loop(env_, &loop);
877     if (!loop) {
878         MEDIA_ERR_LOG("failed to get event loop");
879         return;
880     }
881     uv_work_t* work = new(std::nothrow) uv_work_t;
882     if (!work) {
883         MEDIA_ERR_LOG("failed to allocate work");
884         return;
885     }
886     std::unique_ptr<ExposureInfoChangedCallback> callback = std::make_unique<ExposureInfoChangedCallback>(info, this);
887     work->data = callback.get();
888     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t* work) {}, [] (uv_work_t* work, int status) {
889         ExposureInfoChangedCallback* callback = reinterpret_cast<ExposureInfoChangedCallback *>(work->data);
890         if (callback) {
891             callback->listener_->OnExposureInfoChangedCallback(callback->info_);
892             delete callback;
893         }
894         delete work;
895     }, uv_qos_user_initiated);
896     if (ret) {
897         MEDIA_ERR_LOG("failed to execute work");
898         delete work;
899     } else {
900         callback.release();
901     }
902 }
903 
OnExposureInfoChangedCallback(ExposureInfo info) const904 void ExposureInfoCallbackListener::OnExposureInfoChangedCallback(ExposureInfo info) const
905 {
906     MEDIA_DEBUG_LOG("OnExposureInfoChangedCallback is called");
907     napi_value result[ARGS_TWO] = { nullptr, nullptr };
908     napi_value retVal;
909 
910     napi_get_undefined(env_, &result[PARAM0]);
911     napi_create_object(env_, &result[PARAM1]);
912     napi_value value;
913     napi_create_uint32(env_, info.exposureDurationValue, &value);
914     napi_set_named_property(env_, result[PARAM1], "exposureTimeValue", value);
915 
916     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
917     ExecuteCallback("exposureInfoChange", callbackNapiPara);
918 }
919 
OnExposureInfoChanged(ExposureInfo info)920 void ExposureInfoCallbackListener::OnExposureInfoChanged(ExposureInfo info)
921 {
922     MEDIA_DEBUG_LOG("OnExposureInfoChanged is called, info: %{public}d", info.exposureDurationValue);
923     OnExposureInfoChangedCallbackAsync(info);
924 }
925 
OnIsoInfoChangedCallbackAsync(IsoInfo info) const926 void IsoInfoCallbackListener::OnIsoInfoChangedCallbackAsync(IsoInfo info) const
927 {
928     MEDIA_DEBUG_LOG("OnIsoInfoChangedCallbackAsync is called");
929     uv_loop_s* loop = nullptr;
930     napi_get_uv_event_loop(env_, &loop);
931     if (!loop) {
932         MEDIA_ERR_LOG("failed to get event loop");
933         return;
934     }
935     uv_work_t* work = new(std::nothrow) uv_work_t;
936     if (!work) {
937         MEDIA_ERR_LOG("failed to allocate work");
938         return;
939     }
940     std::unique_ptr<IsoInfoChangedCallback> callback = std::make_unique<IsoInfoChangedCallback>(info, this);
941     work->data = callback.get();
942     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t* work) {}, [] (uv_work_t* work, int status) {
943         IsoInfoChangedCallback* callback = reinterpret_cast<IsoInfoChangedCallback *>(work->data);
944         if (callback) {
945             callback->listener_->OnIsoInfoChangedCallback(callback->info_);
946             delete callback;
947         }
948         delete work;
949     }, uv_qos_user_initiated);
950     if (ret) {
951         MEDIA_ERR_LOG("failed to execute work");
952         delete work;
953     } else {
954         callback.release();
955     }
956 }
957 
OnIsoInfoChangedCallback(IsoInfo info) const958 void IsoInfoCallbackListener::OnIsoInfoChangedCallback(IsoInfo info) const
959 {
960     MEDIA_DEBUG_LOG("OnIsoInfoChangedCallback is called");
961     napi_value result[ARGS_TWO] = { nullptr, nullptr };
962     napi_value retVal;
963 
964     napi_get_undefined(env_, &result[PARAM0]);
965     napi_create_object(env_, &result[PARAM1]);
966     napi_value value;
967     napi_create_int32(env_, CameraNapiUtils::FloatToDouble(info.isoValue), &value);
968     napi_set_named_property(env_, result[PARAM1], "iso", value);
969     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
970     ExecuteCallback("isoInfoChange", callbackNapiPara);
971 }
972 
OnIsoInfoChanged(IsoInfo info)973 void IsoInfoCallbackListener::OnIsoInfoChanged(IsoInfo info)
974 {
975     MEDIA_DEBUG_LOG("OnIsoInfoChanged is called, info: %{public}d", info.isoValue);
976     OnIsoInfoChangedCallbackAsync(info);
977 }
978 
OnApertureInfoChangedCallbackAsync(ApertureInfo info) const979 void ApertureInfoCallbackListener::OnApertureInfoChangedCallbackAsync(ApertureInfo info) const
980 {
981     MEDIA_DEBUG_LOG("OnApertureInfoChangedCallbackAsync is called");
982     uv_loop_s* loop = nullptr;
983     napi_get_uv_event_loop(env_, &loop);
984     if (!loop) {
985         MEDIA_ERR_LOG("failed to get event loop");
986         return;
987     }
988     uv_work_t* work = new(std::nothrow) uv_work_t;
989     if (!work) {
990         MEDIA_ERR_LOG("failed to allocate work");
991         return;
992     }
993     std::unique_ptr<ApertureInfoChangedCallback> callback = std::make_unique<ApertureInfoChangedCallback>(info, this);
994     work->data = callback.get();
995     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t* work) {}, [] (uv_work_t* work, int status) {
996         ApertureInfoChangedCallback* callback = reinterpret_cast<ApertureInfoChangedCallback *>(work->data);
997         if (callback) {
998             callback->listener_->OnApertureInfoChangedCallback(callback->info_);
999             delete callback;
1000         }
1001         delete work;
1002     }, uv_qos_user_initiated);
1003     if (ret) {
1004         MEDIA_ERR_LOG("failed to execute work");
1005         delete work;
1006     } else {
1007         callback.release();
1008     }
1009 }
1010 
OnApertureInfoChangedCallback(ApertureInfo info) const1011 void ApertureInfoCallbackListener::OnApertureInfoChangedCallback(ApertureInfo info) const
1012 {
1013     MEDIA_DEBUG_LOG("OnApertureInfoChangedCallback is called");
1014     napi_value result[ARGS_TWO] = { nullptr, nullptr };
1015     napi_value retVal;
1016 
1017     napi_get_undefined(env_, &result[PARAM0]);
1018     napi_create_object(env_, &result[PARAM1]);
1019     napi_value value;
1020     napi_create_double(env_, info.apertureValue, &value);
1021     napi_set_named_property(env_, result[PARAM1], "aperture", value);
1022     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
1023     ExecuteCallback("apertureInfoChange", callbackNapiPara);
1024 }
1025 
OnApertureInfoChanged(ApertureInfo info)1026 void ApertureInfoCallbackListener::OnApertureInfoChanged(ApertureInfo info)
1027 {
1028     MEDIA_DEBUG_LOG("OnApertureInfoChanged is called, apertureValue: %{public}f", info.apertureValue);
1029     OnApertureInfoChangedCallbackAsync(info);
1030 }
1031 
OnLuminationInfoChangedCallbackAsync(LuminationInfo info) const1032 void LuminationInfoCallbackListener::OnLuminationInfoChangedCallbackAsync(LuminationInfo info) const
1033 {
1034     MEDIA_DEBUG_LOG("OnLuminationInfoChangedCallbackAsync is called");
1035     uv_loop_s* loop = nullptr;
1036     napi_get_uv_event_loop(env_, &loop);
1037     if (!loop) {
1038         MEDIA_ERR_LOG("failed to get event loop");
1039         return;
1040     }
1041     uv_work_t* work = new(std::nothrow) uv_work_t;
1042     if (!work) {
1043         MEDIA_ERR_LOG("failed to allocate work");
1044         return;
1045     }
1046     std::unique_ptr<LuminationInfoChangedCallback> callback =
1047         std::make_unique<LuminationInfoChangedCallback>(info, this);
1048     work->data = callback.get();
1049     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t* work) {}, [] (uv_work_t* work, int status) {
1050         LuminationInfoChangedCallback* callback = reinterpret_cast<LuminationInfoChangedCallback *>(work->data);
1051         if (callback) {
1052             callback->listener_->OnLuminationInfoChangedCallback(callback->info_);
1053             delete callback;
1054         }
1055         delete work;
1056     }, uv_qos_user_initiated);
1057     if (ret) {
1058         MEDIA_ERR_LOG("failed to execute work");
1059         delete work;
1060     } else {
1061         callback.release();
1062     }
1063 }
1064 
OnLuminationInfoChangedCallback(LuminationInfo info) const1065 void LuminationInfoCallbackListener::OnLuminationInfoChangedCallback(LuminationInfo info) const
1066 {
1067     MEDIA_DEBUG_LOG("OnLuminationInfoChangedCallback is called");
1068     napi_value result[ARGS_TWO] = {nullptr, nullptr};
1069     napi_value retVal;
1070 
1071     napi_get_undefined(env_, &result[PARAM0]);
1072     napi_create_object(env_, &result[PARAM1]);
1073     napi_value isoValue;
1074     napi_create_double(env_, info.luminationValue, &isoValue);
1075     napi_set_named_property(env_, result[PARAM1], "lumination", isoValue);
1076 
1077     ExecuteCallbackNapiPara callbackNapiPara { .recv = nullptr, .argc = ARGS_TWO, .argv = result, .result = &retVal };
1078     ExecuteCallback("luminationInfoChange", callbackNapiPara);
1079 }
1080 
OnLuminationInfoChanged(LuminationInfo info)1081 void LuminationInfoCallbackListener::OnLuminationInfoChanged(LuminationInfo info)
1082 {
1083     MEDIA_DEBUG_LOG("OnLuminationInfoChanged is called, luminationValue: %{public}f", info.luminationValue);
1084     OnLuminationInfoChangedCallbackAsync(info);
1085 }
1086 
On(napi_env env,napi_callback_info info)1087 napi_value ProfessionSessionNapi::On(napi_env env, napi_callback_info info)
1088 {
1089     return ListenerTemplate<CameraSessionNapi>::On(env, info);
1090 }
1091 
Once(napi_env env,napi_callback_info info)1092 napi_value ProfessionSessionNapi::Once(napi_env env, napi_callback_info info)
1093 {
1094     return ListenerTemplate<CameraSessionNapi>::Once(env, info);
1095 }
1096 
Off(napi_env env,napi_callback_info info)1097 napi_value ProfessionSessionNapi::Off(napi_env env, napi_callback_info info)
1098 {
1099     return ListenerTemplate<CameraSessionNapi>::Off(env, info);
1100 }
1101 } // namespace CameraStandard
1102 } // namespace OHOS