1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "mode/night_session_napi.h"
17
18 #include "napi/native_common.h"
19 #include "uv.h"
20
21 namespace OHOS {
22 namespace CameraStandard {
23 using namespace std;
24
25 thread_local napi_ref NightSessionNapi::sConstructor_ = nullptr;
26
NightSessionNapi()27 NightSessionNapi::NightSessionNapi() : env_(nullptr)
28 {
29 }
~NightSessionNapi()30 NightSessionNapi::~NightSessionNapi()
31 {
32 MEDIA_DEBUG_LOG("~NightSessionNapi is called");
33 }
NightSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)34 void NightSessionNapi::NightSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
35 {
36 MEDIA_DEBUG_LOG("NightSessionNapiDestructor is called");
37 NightSessionNapi* cameraObj = reinterpret_cast<NightSessionNapi*>(nativeObject);
38 if (cameraObj != nullptr) {
39 delete cameraObj;
40 }
41 }
Init(napi_env env,napi_value exports)42 napi_value NightSessionNapi::Init(napi_env env, napi_value exports)
43 {
44 MEDIA_DEBUG_LOG("Init is called");
45 napi_status status;
46 napi_value ctorObj;
47 std::vector<napi_property_descriptor> manual_exposure_props = {
48 DECLARE_NAPI_FUNCTION("getSupportedExposureRange", NightSessionNapi::GetSupportedExposureRange),
49 DECLARE_NAPI_FUNCTION("getExposure", NightSessionNapi::GetExposure),
50 DECLARE_NAPI_FUNCTION("setExposure", NightSessionNapi::SetExposure)
51 };
52 std::vector<std::vector<napi_property_descriptor>> descriptors = {camera_process_props, stabilization_props,
53 flash_props, auto_exposure_props, focus_props, zoom_props, filter_props, beauty_props,
54 color_effect_props, macro_props, color_management_props, manual_exposure_props};
55 std::vector<napi_property_descriptor> night_session_props = CameraNapiUtils::GetPropertyDescriptor(descriptors);
56 status = napi_define_class(env, NIGHT_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
57 NightSessionNapiConstructor, nullptr,
58 night_session_props.size(),
59 night_session_props.data(), &ctorObj);
60 if (status == napi_ok) {
61 int32_t refCount = 1;
62 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
63 if (status == napi_ok) {
64 status = napi_set_named_property(env, exports, NIGHT_SESSION_NAPI_CLASS_NAME, ctorObj);
65 CHECK_ERROR_RETURN_RET(status == napi_ok, exports);
66 }
67 }
68 MEDIA_ERR_LOG("Init call Failed!");
69 return nullptr;
70 }
71
CreateCameraSession(napi_env env)72 napi_value NightSessionNapi::CreateCameraSession(napi_env env)
73 {
74 MEDIA_DEBUG_LOG("CreateCameraSession is called");
75 CAMERA_SYNC_TRACE;
76 napi_status status;
77 napi_value result = nullptr;
78 napi_value constructor;
79 status = napi_get_reference_value(env, sConstructor_, &constructor);
80 if (status == napi_ok) {
81 sCameraSession_ = CameraManager::GetInstance()->CreateCaptureSession(SceneMode::NIGHT);
82 if (sCameraSession_ == nullptr) {
83 MEDIA_ERR_LOG("Failed to create Camera session instance");
84 napi_get_undefined(env, &result);
85 return result;
86 }
87 status = napi_new_instance(env, constructor, 0, nullptr, &result);
88 sCameraSession_ = nullptr;
89 if (status == napi_ok && result != nullptr) {
90 MEDIA_DEBUG_LOG("success to create Camera session napi instance");
91 return result;
92 } else {
93 MEDIA_ERR_LOG("Failed to create Camera session napi instance");
94 }
95 }
96 MEDIA_ERR_LOG("Failed to create Camera session napi instance last");
97 napi_get_undefined(env, &result);
98 return result;
99 }
100
GetSupportedExposureRange(napi_env env,napi_callback_info info)101 napi_value NightSessionNapi::GetSupportedExposureRange(napi_env env, napi_callback_info info)
102 {
103 MEDIA_DEBUG_LOG("GetSupportedExposureRange is called");
104 napi_status status;
105 napi_value result = nullptr;
106 size_t argc = ARGS_ZERO;
107 napi_value argv[ARGS_ZERO];
108 napi_value thisVar = nullptr;
109
110 CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
111
112 napi_get_undefined(env, &result);
113 NightSessionNapi* nightSessionNapi = nullptr;
114 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&nightSessionNapi));
115 if (status == napi_ok && nightSessionNapi != nullptr) {
116 std::vector<uint32_t> vecExposureList;
117 int32_t retCode = nightSessionNapi->nightSession_->GetExposureRange(vecExposureList);
118 CHECK_ERROR_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
119 CHECK_ERROR_RETURN_RET(vecExposureList.empty() || napi_create_array(env, &result) != napi_ok, result);
120 for (size_t i = 0; i < vecExposureList.size(); i++) {
121 uint32_t exposure = vecExposureList[i];
122 MEDIA_DEBUG_LOG("EXPOSURE_RANGE : exposure = %{public}d", vecExposureList[i]);
123 napi_value value;
124 napi_create_uint32(env, exposure, &value);
125 napi_set_element(env, result, i, value);
126 }
127 MEDIA_DEBUG_LOG("EXPOSURE_RANGE ExposureList size : %{public}zu", vecExposureList.size());
128 } else {
129 MEDIA_ERR_LOG("GetExposureBiasRange call Failed!");
130 }
131 return result;
132 }
133
GetExposure(napi_env env,napi_callback_info info)134 napi_value NightSessionNapi::GetExposure(napi_env env, napi_callback_info info)
135 {
136 MEDIA_DEBUG_LOG("GetExposure is called");
137 napi_status status;
138 napi_value result = nullptr;
139 size_t argc = ARGS_ZERO;
140 napi_value argv[ARGS_ZERO];
141 napi_value thisVar = nullptr;
142
143 CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
144
145 napi_get_undefined(env, &result);
146 NightSessionNapi* nightSessionNapi = nullptr;
147 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&nightSessionNapi));
148 if (status == napi_ok && nightSessionNapi!= nullptr) {
149 uint32_t exposureValue;
150 int32_t retCode = nightSessionNapi->nightSession_->GetExposure(exposureValue);
151 CHECK_ERROR_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
152 MEDIA_DEBUG_LOG("GetExposure : exposure = %{public}d", exposureValue);
153 napi_create_uint32(env, exposureValue, &result);
154 } else {
155 MEDIA_ERR_LOG("GetExposure call Failed!");
156 }
157 return result;
158 }
159
SetExposure(napi_env env,napi_callback_info info)160 napi_value NightSessionNapi::SetExposure(napi_env env, napi_callback_info info)
161 {
162 MEDIA_DEBUG_LOG("SetExposure is called");
163 CAMERA_SYNC_TRACE;
164 napi_status status;
165 napi_value result = nullptr;
166 size_t argc = ARGS_ONE;
167 napi_value argv[ARGS_ONE] = {0};
168 napi_value thisVar = nullptr;
169
170 CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
171
172 napi_get_undefined(env, &result);
173 NightSessionNapi* nightSessionNapi = nullptr;
174 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&nightSessionNapi));
175 if (status == napi_ok && nightSessionNapi != nullptr) {
176 uint32_t exposureValue;
177 napi_get_value_uint32(env, argv[PARAM0], &exposureValue);
178 MEDIA_DEBUG_LOG("SetExposure : exposure = %{public}d", exposureValue);
179 nightSessionNapi->nightSession_->LockForControl();
180 int32_t retCode = nightSessionNapi->nightSession_->SetExposure(exposureValue);
181 nightSessionNapi->nightSession_->UnlockForControl();
182 CHECK_ERROR_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), result);
183 } else {
184 MEDIA_ERR_LOG("SetExposure call Failed!");
185 }
186 return result;
187 }
188
NightSessionNapiConstructor(napi_env env,napi_callback_info info)189 napi_value NightSessionNapi::NightSessionNapiConstructor(napi_env env, napi_callback_info info)
190 {
191 MEDIA_DEBUG_LOG("NightSessionNapiConstructor is called");
192 napi_status status;
193 napi_value result = nullptr;
194 napi_value thisVar = nullptr;
195
196 napi_get_undefined(env, &result);
197 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
198
199 if (status == napi_ok && thisVar != nullptr) {
200 std::unique_ptr<NightSessionNapi> obj = std::make_unique<NightSessionNapi>();
201 obj->env_ = env;
202 CHECK_ERROR_RETURN_RET_LOG(sCameraSession_ == nullptr, result, "sCameraSession_ is null");
203 obj->nightSession_ = static_cast<NightSession*>(sCameraSession_.GetRefPtr());
204 obj->cameraSession_ = obj->nightSession_;
205 CHECK_ERROR_RETURN_RET_LOG(obj->nightSession_ == nullptr, result, "nightSession_ is null");
206 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
207 NightSessionNapi::NightSessionNapiDestructor, nullptr, nullptr);
208 if (status == napi_ok) {
209 obj.release();
210 return thisVar;
211 } else {
212 MEDIA_ERR_LOG("NightSessionNapi Failure wrapping js to native napi");
213 }
214 }
215 MEDIA_ERR_LOG("NightSessionNapi call Failed!");
216 return result;
217 }
218 } // namespace CameraStandard
219 } // namespace OHOS