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 "input/camera_profile_napi.h"
17 #include "input/camera_size_napi.h"
18
19 namespace OHOS {
20 namespace CameraStandard {
21 using OHOS::HiviewDFX::HiLog;
22 using OHOS::HiviewDFX::HiLogLabel;
23
24 thread_local napi_ref CameraProfileNapi::sConstructor_ = nullptr;
25 thread_local Profile* CameraProfileNapi::sCameraProfile_ = nullptr;
26
CameraProfileNapi()27 CameraProfileNapi::CameraProfileNapi() : env_(nullptr), wrapper_(nullptr)
28 {
29 }
30
~CameraProfileNapi()31 CameraProfileNapi::~CameraProfileNapi()
32 {
33 if (wrapper_ != nullptr) {
34 napi_delete_reference(env_, wrapper_);
35 }
36 if (cameraProfile_) {
37 cameraProfile_ = nullptr;
38 }
39 }
40
CameraProfileNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)41 void CameraProfileNapi::CameraProfileNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
42 {
43 CameraProfileNapi* cameraProfileNapi = reinterpret_cast<CameraProfileNapi*>(nativeObject);
44 if (cameraProfileNapi != nullptr) {
45 MEDIA_INFO_LOG("CameraProfileNapi::CameraProfileNapiDestructor ~");
46 cameraProfileNapi->~CameraProfileNapi();
47 }
48 }
49
Init(napi_env env,napi_value exports)50 napi_value CameraProfileNapi::Init(napi_env env, napi_value exports)
51 {
52 napi_status status;
53 napi_value ctorObj;
54 napi_property_descriptor camera_profile_props[] = {
55 DECLARE_NAPI_GETTER("format", GetCameraProfileFormat),
56 DECLARE_NAPI_GETTER("size", GetCameraProfileSize)
57 };
58 status = napi_define_class(env, CAMERA_PROFILE_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
59 CameraProfileNapiConstructor, nullptr,
60 sizeof(camera_profile_props) / sizeof(camera_profile_props[PARAM0]),
61 camera_profile_props, &ctorObj);
62 if (status == napi_ok) {
63 int32_t refCount = 1;
64 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
65 if (status == napi_ok) {
66 status = napi_set_named_property(env, exports, CAMERA_PROFILE_NAPI_CLASS_NAME, ctorObj);
67 if (status == napi_ok) {
68 return exports;
69 }
70 }
71 }
72 return nullptr;
73 }
74
75 // Constructor callback
CameraProfileNapiConstructor(napi_env env,napi_callback_info info)76 napi_value CameraProfileNapi::CameraProfileNapiConstructor(napi_env env, napi_callback_info info)
77 {
78 napi_status status;
79 napi_value result = nullptr;
80 napi_value thisVar = nullptr;
81
82 napi_get_undefined(env, &result);
83 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
84
85 if (status == napi_ok && thisVar != nullptr) {
86 std::unique_ptr<CameraProfileNapi> obj = std::make_unique<CameraProfileNapi>();
87 obj->env_ = env;
88 obj->cameraProfile_ = sCameraProfile_;
89 MEDIA_INFO_LOG("CameraProfileNapi::CameraProfileNapiConstructor "
90 "size.width = %{public}d, size.height = %{public}d, "
91 "format = %{public}d",
92 obj->cameraProfile_->GetSize().width,
93 obj->cameraProfile_->GetSize().height,
94 obj->cameraProfile_->format_);
95 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
96 CameraProfileNapi::CameraProfileNapiDestructor, nullptr, &(obj->wrapper_));
97 if (status == napi_ok) {
98 obj.release();
99 return thisVar;
100 } else {
101 MEDIA_ERR_LOG("Failure wrapping js to native napi");
102 }
103 }
104 return result;
105 }
106
CreateCameraProfile(napi_env env,Profile & profile)107 napi_value CameraProfileNapi::CreateCameraProfile(napi_env env, Profile &profile)
108 {
109 napi_status status;
110 napi_value result = nullptr;
111 napi_value constructor;
112
113 status = napi_get_reference_value(env, sConstructor_, &constructor);
114 if (status == napi_ok) {
115 CameraFormat format = profile.GetCameraFormat();
116 Size size = profile.GetSize();
117 MEDIA_INFO_LOG("CameraProfileNapi::CreateCameraProfile size.width = %{public}d, size.height = %{public}d",
118 size.width, size.height);
119 std::unique_ptr<Profile> profilePtr = std::make_unique<Profile>(format, size);
120 sCameraProfile_ = reinterpret_cast<Profile*>(profilePtr.get());
121 MEDIA_INFO_LOG("CameraProfileNapi::CreateCameraProfile sCameraProfile_ "
122 "size.width = %{public}d, size.height = %{public}d, format = %{public}d",
123 sCameraProfile_->GetSize().width, sCameraProfile_->GetSize().height, sCameraProfile_->format_);
124 status = napi_new_instance(env, constructor, 0, nullptr, &result);
125 if (status == napi_ok && result != nullptr) {
126 MEDIA_INFO_LOG("GetCameraProfileSize CreateCameraProfile napi_new_instance success");
127 return result;
128 } else {
129 MEDIA_ERR_LOG("Failed to create Camera obj instance");
130 }
131 }
132
133 napi_get_undefined(env, &result);
134 return result;
135 }
136
GetCameraProfileSize(napi_env env,napi_callback_info info)137 napi_value CameraProfileNapi::GetCameraProfileSize(napi_env env, napi_callback_info info)
138 {
139 napi_status status;
140 napi_value jsResult = nullptr;
141 napi_value undefinedResult = nullptr;
142 CameraProfileNapi* obj = nullptr;
143 Size cameraProfileSize;
144 napi_value thisVar = nullptr;
145
146 napi_get_undefined(env, &undefinedResult);
147 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
148
149 MEDIA_INFO_LOG("GetCameraProfileSize cameraProfileSize thisVar");
150 if (status != napi_ok || thisVar == nullptr) {
151 MEDIA_ERR_LOG("Invalid arguments!");
152 return undefinedResult;
153 }
154
155 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
156 if ((status == napi_ok) && (obj != nullptr)) {
157 cameraProfileSize.height = obj->cameraProfile_->GetSize().height;
158 cameraProfileSize.width = obj->cameraProfile_->GetSize().width;
159 MEDIA_INFO_LOG("GetCameraProfileSize cameraProfileSize "
160 "size.width = %{public}d, size.height = %{public}d",
161 cameraProfileSize.width, cameraProfileSize.height);
162 jsResult = CameraSizeNapi::CreateCameraSize(env, cameraProfileSize);
163 return jsResult;
164 }
165
166 return undefinedResult;
167 }
168
GetCameraProfileFormat(napi_env env,napi_callback_info info)169 napi_value CameraProfileNapi::GetCameraProfileFormat(napi_env env, napi_callback_info info)
170 {
171 napi_status status;
172 napi_value jsResult = nullptr;
173 napi_value undefinedResult = nullptr;
174 CameraProfileNapi* obj = nullptr;
175 CameraFormat cameraProfileFormat;
176 napi_value thisVar = nullptr;
177
178 napi_get_undefined(env, &undefinedResult);
179 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
180 MEDIA_INFO_LOG("GetCameraProfileSize cameraProfileSize thisVar");
181 if (status != napi_ok || thisVar == nullptr) {
182 MEDIA_ERR_LOG("Invalid arguments!");
183 return undefinedResult;
184 }
185
186 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
187 if ((status == napi_ok) && (obj != nullptr)) {
188 cameraProfileFormat = obj->cameraProfile_->GetCameraFormat();
189 MEDIA_INFO_LOG("GetCameraProfileFormat cameraProfileSize "
190 "size.width = %{public}d, size.height = %{public}d, format = %{public}d",
191 obj->cameraProfile_->size_.width,
192 obj->cameraProfile_->size_.height,
193 static_cast<int>(cameraProfileFormat));
194 status = napi_create_int32(env, static_cast<int>(cameraProfileFormat), &jsResult);
195 if (status == napi_ok) {
196 return jsResult;
197 } else {
198 MEDIA_ERR_LOG("Failed to get CameraProfileFormat!, errorCode : %{public}d", status);
199 }
200 }
201
202 return undefinedResult;
203 }
204
205 thread_local napi_ref CameraVideoProfileNapi::sVideoConstructor_ = nullptr;
206 thread_local VideoProfile CameraVideoProfileNapi::sVideoProfile_;
207
CameraVideoProfileNapi()208 CameraVideoProfileNapi::CameraVideoProfileNapi() : env_(nullptr), wrapper_(nullptr) {}
209
~CameraVideoProfileNapi()210 CameraVideoProfileNapi::~CameraVideoProfileNapi()
211 {
212 if (wrapper_ != nullptr) {
213 napi_delete_reference(env_, wrapper_);
214 }
215 }
216
CameraVideoProfileNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)217 void CameraVideoProfileNapi::CameraVideoProfileNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
218 {
219 CameraVideoProfileNapi* cameraVideoProfileNapi = reinterpret_cast<CameraVideoProfileNapi*>(nativeObject);
220 if (cameraVideoProfileNapi != nullptr) {
221 cameraVideoProfileNapi->~CameraVideoProfileNapi();
222 }
223 }
224
Init(napi_env env,napi_value exports)225 napi_value CameraVideoProfileNapi::Init(napi_env env, napi_value exports)
226 {
227 napi_status status;
228 napi_value ctorObj;
229
230 napi_property_descriptor camera_video_profile_props[] = {
231 DECLARE_NAPI_GETTER("format", CameraVideoProfileNapi::GetCameraProfileFormat),
232 DECLARE_NAPI_GETTER("size", CameraVideoProfileNapi::GetCameraProfileSize),
233 DECLARE_NAPI_GETTER("frameRateRange", GetFrameRateRange)
234 };
235
236 status = napi_define_class(env, CAMERA_VIDEO_PROFILE_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
237 CameraVideoProfileNapiConstructor, nullptr,
238 sizeof(camera_video_profile_props) / sizeof(camera_video_profile_props[PARAM0]),
239 camera_video_profile_props, &ctorObj);
240 if (status == napi_ok) {
241 int32_t refCount = 1;
242 status = napi_create_reference(env, ctorObj, refCount, &sVideoConstructor_);
243 if (status == napi_ok) {
244 status = napi_set_named_property(env, exports, CAMERA_VIDEO_PROFILE_NAPI_CLASS_NAME, ctorObj);
245 if (status == napi_ok) {
246 return exports;
247 }
248 }
249 }
250
251 return nullptr;
252 }
253
254 // Constructor callback
CameraVideoProfileNapiConstructor(napi_env env,napi_callback_info info)255 napi_value CameraVideoProfileNapi::CameraVideoProfileNapiConstructor(napi_env env, napi_callback_info info)
256 {
257 napi_status status;
258 napi_value result = nullptr;
259 napi_value thisVar = nullptr;
260
261 napi_get_undefined(env, &result);
262 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
263
264 if (status == napi_ok && thisVar != nullptr) {
265 std::unique_ptr<CameraVideoProfileNapi> obj = std::make_unique<CameraVideoProfileNapi>();
266 obj->env_ = env;
267 obj->videoProfile_ = sVideoProfile_;
268 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
269 CameraVideoProfileNapi::CameraVideoProfileNapiDestructor, nullptr, &(obj->wrapper_));
270 if (status == napi_ok) {
271 obj.release();
272 return thisVar;
273 } else {
274 MEDIA_ERR_LOG("Failure wrapping js to native napi");
275 }
276 }
277
278 return result;
279 }
280
CreateCameraVideoProfile(napi_env env,VideoProfile & profile)281 napi_value CameraVideoProfileNapi::CreateCameraVideoProfile(napi_env env, VideoProfile &profile)
282 {
283 napi_status status;
284 napi_value result = nullptr;
285 napi_value constructor;
286
287 status = napi_get_reference_value(env, sVideoConstructor_, &constructor);
288 if (status == napi_ok) {
289 sVideoProfile_ = profile;
290 status = napi_new_instance(env, constructor, 0, nullptr, &result);
291 if (status == napi_ok && result != nullptr) {
292 return result;
293 } else {
294 MEDIA_ERR_LOG("Failed to create Camera obj instance");
295 }
296 }
297
298 napi_get_undefined(env, &result);
299 return result;
300 }
301
GetCameraProfileSize(napi_env env,napi_callback_info info)302 napi_value CameraVideoProfileNapi::GetCameraProfileSize(napi_env env, napi_callback_info info)
303 {
304 napi_status status;
305 napi_value jsResult = nullptr;
306 napi_value undefinedResult = nullptr;
307 CameraVideoProfileNapi* obj = nullptr;
308 Size cameraProfileSize;
309 napi_value thisVar = nullptr;
310
311 napi_get_undefined(env, &undefinedResult);
312 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
313
314 if (status != napi_ok || thisVar == nullptr) {
315 MEDIA_ERR_LOG("Invalid arguments!");
316 return undefinedResult;
317 }
318
319 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
320 if ((status == napi_ok) && (obj != nullptr)) {
321 cameraProfileSize.width = obj->videoProfile_.GetSize().width;
322 cameraProfileSize.height = obj->videoProfile_.GetSize().height;
323 jsResult = CameraSizeNapi::CreateCameraSize(env, cameraProfileSize);
324 return jsResult;
325 }
326
327 return undefinedResult;
328 }
329
GetCameraProfileFormat(napi_env env,napi_callback_info info)330 napi_value CameraVideoProfileNapi::GetCameraProfileFormat(napi_env env, napi_callback_info info)
331 {
332 napi_status status;
333 napi_value jsResult = nullptr;
334 napi_value undefinedResult = nullptr;
335 CameraVideoProfileNapi* obj = nullptr;
336 CameraFormat CameraProfileFormat;
337 napi_value thisVar = nullptr;
338
339 napi_get_undefined(env, &undefinedResult);
340 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
341
342 if (status != napi_ok || thisVar == nullptr) {
343 MEDIA_ERR_LOG("Invalid arguments!");
344 return undefinedResult;
345 }
346
347 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
348 if ((status == napi_ok) && (obj != nullptr)) {
349 CameraProfileFormat = obj->videoProfile_.GetCameraFormat();
350 status = napi_create_int32(env, CameraProfileFormat, &jsResult);
351 if (status == napi_ok) {
352 return jsResult;
353 } else {
354 MEDIA_ERR_LOG("Failed to get CameraProfileHeight!, errorCode : %{public}d", status);
355 }
356 }
357
358 return undefinedResult;
359 }
360
CreateJSArray(napi_env env,napi_status status,std::vector<int32_t> nativeArray)361 static napi_value CreateJSArray(napi_env env, napi_status status,
362 std::vector<int32_t> nativeArray)
363 {
364 napi_value jsArray = nullptr;
365 napi_value item = nullptr;
366
367 if (nativeArray.empty()) {
368 MEDIA_ERR_LOG("nativeArray is empty");
369 return jsArray;
370 }
371
372 status = napi_create_array(env, &jsArray);
373 if (status == napi_ok) {
374 for (size_t i = 0; i < nativeArray.size(); i++) {
375 napi_create_int32(env, nativeArray[i], &item);
376 MEDIA_INFO_LOG("CreateJSArray CreateCameraProfile success");
377 if (napi_set_element(env, jsArray, i, item) != napi_ok) {
378 MEDIA_ERR_LOG("Failed to create profile napi wrapper object");
379 return nullptr;
380 }
381 }
382 }
383 return jsArray;
384 }
385
386 // todo: should to using CreateFrameRateRange in CreateJSArray
GetFrameRateRange(napi_env env,napi_callback_info info)387 napi_value CameraVideoProfileNapi::GetFrameRateRange(napi_env env, napi_callback_info info)
388 {
389 napi_status status;
390 napi_value jsResult = nullptr;
391 napi_value undefinedResult = nullptr;
392 CameraVideoProfileNapi* obj = nullptr;
393 napi_value thisVar = nullptr;
394
395 napi_get_undefined(env, &undefinedResult);
396 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
397
398 if (status != napi_ok || thisVar == nullptr) {
399 MEDIA_ERR_LOG("Invalid arguments!");
400 return undefinedResult;
401 }
402
403 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
404 if ((status == napi_ok) && (obj != nullptr)) {
405 std::vector<int32_t> frameRanges = obj->videoProfile_.GetFrameRates();
406 jsResult = CreateJSArray(env, status, frameRanges);
407 if (status == napi_ok) {
408 MEDIA_ERR_LOG("GetFrameRateRange success ");
409 return jsResult;
410 } else {
411 MEDIA_ERR_LOG("Failed to get FrameRateRanges!, errorCode : %{public}d", status);
412 }
413 }
414 return undefinedResult;
415 }
416 } // namespace CameraStandard
417 } // namespace OHOS
418