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