1 /*
2 * Copyright (c) 2021-2023 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_info_napi.h"
17
18 namespace OHOS {
19 namespace CameraStandard {
20 using namespace std;
21
22 thread_local napi_ref CameraDeviceNapi::sConstructor_ = nullptr;
23 thread_local sptr<CameraDevice> CameraDeviceNapi::sCameraDevice_ = nullptr;
24
CameraDeviceNapi()25 CameraDeviceNapi::CameraDeviceNapi() : env_(nullptr), wrapper_(nullptr)
26 {
27 }
28
~CameraDeviceNapi()29 CameraDeviceNapi::~CameraDeviceNapi()
30 {
31 MEDIA_DEBUG_LOG("~CameraDeviceNapi is called");
32 if (wrapper_ != nullptr) {
33 napi_delete_reference(env_, wrapper_);
34 }
35 if (cameraDevice_) {
36 cameraDevice_ = nullptr;
37 }
38 }
39
CameraDeviceNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)40 void CameraDeviceNapi::CameraDeviceNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
41 {
42 MEDIA_DEBUG_LOG("CameraDeviceNapiDestructor is called");
43 CameraDeviceNapi* cameraObj = reinterpret_cast<CameraDeviceNapi*>(nativeObject);
44 if (cameraObj != nullptr) {
45 delete cameraObj;
46 }
47 }
48
Init(napi_env env,napi_value exports)49 napi_value CameraDeviceNapi::Init(napi_env env, napi_value exports)
50 {
51 MEDIA_DEBUG_LOG("Init is called");
52 napi_status status;
53 napi_value ctorObj;
54 int32_t refCount = 1;
55
56 napi_property_descriptor camera_object_props[] = {
57 DECLARE_NAPI_GETTER("cameraId", GetCameraId),
58 DECLARE_NAPI_GETTER("cameraPosition", GetCameraPosition),
59 DECLARE_NAPI_GETTER("cameraType", GetCameraType),
60 DECLARE_NAPI_GETTER("connectionType", GetConnectionType),
61 DECLARE_NAPI_GETTER("hostDeviceName", GetHostDeviceName),
62 DECLARE_NAPI_GETTER("hostDeviceType", GetHostDeviceType)
63 };
64
65 status = napi_define_class(env, CAMERA_OBJECT_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
66 CameraDeviceNapiConstructor, nullptr,
67 sizeof(camera_object_props) / sizeof(camera_object_props[PARAM0]),
68 camera_object_props, &ctorObj);
69 if (status == napi_ok) {
70 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
71 if (status == napi_ok) {
72 status = napi_set_named_property(env, exports, CAMERA_OBJECT_NAPI_CLASS_NAME, ctorObj);
73 if (status == napi_ok) {
74 return exports;
75 }
76 }
77 }
78 MEDIA_ERR_LOG("Init call Failed!");
79 return nullptr;
80 }
81
82 // Constructor callback
CameraDeviceNapiConstructor(napi_env env,napi_callback_info info)83 napi_value CameraDeviceNapi::CameraDeviceNapiConstructor(napi_env env, napi_callback_info info)
84 {
85 MEDIA_DEBUG_LOG("CameraDeviceNapiConstructor is called");
86 napi_status status;
87 napi_value result = nullptr;
88 napi_value thisVar = nullptr;
89
90 napi_get_undefined(env, &result);
91 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
92
93 if (status == napi_ok && thisVar != nullptr) {
94 std::unique_ptr<CameraDeviceNapi> obj = std::make_unique<CameraDeviceNapi>();
95 obj->env_ = env;
96 obj->cameraDevice_ = sCameraDevice_;
97 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
98 CameraDeviceNapi::CameraDeviceNapiDestructor, nullptr, nullptr);
99 if (status == napi_ok) {
100 obj.release();
101 return thisVar;
102 } else {
103 MEDIA_ERR_LOG("Failure wrapping js to native napi");
104 }
105 }
106 MEDIA_ERR_LOG("CameraDeviceNapiConstructor call Failed!");
107 return result;
108 }
109
CreateCameraObj(napi_env env,sptr<CameraDevice> cameraDevice)110 napi_value CameraDeviceNapi::CreateCameraObj(napi_env env, sptr<CameraDevice> cameraDevice)
111 {
112 MEDIA_INFO_LOG("CreateCameraObj is called");
113 napi_status status;
114 napi_value result = nullptr;
115 napi_value constructor;
116
117 status = napi_get_reference_value(env, sConstructor_, &constructor);
118 if (status == napi_ok) {
119 sCameraDevice_ = cameraDevice;
120 status = napi_new_instance(env, constructor, 0, nullptr, &result);
121 sCameraDevice_ = nullptr;
122 if (status == napi_ok && result != nullptr) {
123 return result;
124 } else {
125 MEDIA_ERR_LOG("Failed to create Camera obj instance");
126 }
127 }
128 napi_get_undefined(env, &result);
129 MEDIA_ERR_LOG("CreateCameraObj call Failed!");
130 return result;
131 }
132
GetCameraId(napi_env env,napi_callback_info info)133 napi_value CameraDeviceNapi::GetCameraId(napi_env env, napi_callback_info info)
134 {
135 MEDIA_DEBUG_LOG("GetCameraId is called");
136 napi_status status;
137 napi_value jsResult = nullptr;
138 napi_value undefinedResult = nullptr;
139 CameraDeviceNapi* obj = nullptr;
140 napi_value thisVar = nullptr;
141
142 MEDIA_DEBUG_LOG("For get camera id");
143 napi_get_undefined(env, &undefinedResult);
144 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
145
146 if (status != napi_ok || thisVar == nullptr) {
147 MEDIA_ERR_LOG("Invalid arguments!");
148 return undefinedResult;
149 }
150 MEDIA_DEBUG_LOG("To get camera id");
151 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
152 if (status == napi_ok && obj != nullptr && obj->cameraDevice_ != nullptr) {
153 std::string cameraId = obj->cameraDevice_->GetID();
154 status = napi_create_string_utf8(env, cameraId.c_str(), NAPI_AUTO_LENGTH, &jsResult);
155 if (status == napi_ok) {
156 return jsResult;
157 } else {
158 MEDIA_ERR_LOG("Failed to get camera id!, errorCode : %{public}d", status);
159 }
160 }
161 MEDIA_ERR_LOG("GetCameraId call Failed!");
162 return undefinedResult;
163 }
164
GetCameraPosition(napi_env env,napi_callback_info info)165 napi_value CameraDeviceNapi::GetCameraPosition(napi_env env, napi_callback_info info)
166 {
167 MEDIA_DEBUG_LOG("GetCameraPosition is called");
168 napi_status status;
169 napi_value jsResult = nullptr;
170 napi_value undefinedResult = nullptr;
171 CameraDeviceNapi* obj = nullptr;
172 CameraPosition jsCameraPosition;
173 napi_value thisVar = nullptr;
174
175 MEDIA_DEBUG_LOG("For get camera position");
176 napi_get_undefined(env, &undefinedResult);
177 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
178
179 if (status != napi_ok || thisVar == nullptr) {
180 MEDIA_ERR_LOG("Invalid arguments!");
181 return undefinedResult;
182 }
183 MEDIA_DEBUG_LOG("To get camera position");
184 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
185 if (status == napi_ok && obj != nullptr && obj->cameraDevice_ != nullptr) {
186 jsCameraPosition = obj->cameraDevice_->GetPosition();
187 status = napi_create_int32(env, jsCameraPosition, &jsResult);
188 if (status == napi_ok) {
189 return jsResult;
190 } else {
191 MEDIA_ERR_LOG("Failed to get cameraPosition!, errorCode : %{public}d", status);
192 }
193 }
194 MEDIA_ERR_LOG("GetCameraPosition call Failed!");
195 return undefinedResult;
196 }
197
GetCameraType(napi_env env,napi_callback_info info)198 napi_value CameraDeviceNapi::GetCameraType(napi_env env, napi_callback_info info)
199 {
200 MEDIA_DEBUG_LOG("GetCameraType is called");
201 napi_status status;
202 napi_value jsResult = nullptr;
203 napi_value undefinedResult = nullptr;
204 CameraDeviceNapi* obj = nullptr;
205 CameraType jsCameraType;
206 napi_value thisVar = nullptr;
207
208 MEDIA_DEBUG_LOG("For get camera type");
209 napi_get_undefined(env, &undefinedResult);
210 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
211
212 if (status != napi_ok || thisVar == nullptr) {
213 MEDIA_ERR_LOG("Invalid arguments!");
214 return undefinedResult;
215 }
216 MEDIA_DEBUG_LOG("To get camera type");
217 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
218 if (status == napi_ok && obj != nullptr && obj->cameraDevice_ != nullptr) {
219 jsCameraType = obj->cameraDevice_->GetCameraType();
220 if (jsCameraType == CAMERA_TYPE_UNSUPPORTED) {
221 MEDIA_ERR_LOG("Camera type is not a recognized camera type in JS");
222 return undefinedResult;
223 }
224 status = napi_create_int32(env, jsCameraType, &jsResult);
225 if (status == napi_ok) {
226 return jsResult;
227 } else {
228 MEDIA_ERR_LOG("Failed to get cameraType!, errorCode : %{public}d", status);
229 }
230 }
231 MEDIA_ERR_LOG("GetCameraType call Failed!");
232 return undefinedResult;
233 }
234
GetConnectionType(napi_env env,napi_callback_info info)235 napi_value CameraDeviceNapi::GetConnectionType(napi_env env, napi_callback_info info)
236 {
237 MEDIA_DEBUG_LOG("GetConnectionType is called");
238 napi_status status;
239 napi_value jsResult = nullptr;
240 napi_value undefinedResult = nullptr;
241 CameraDeviceNapi* obj = nullptr;
242 ConnectionType jsConnectionType;
243 napi_value thisVar = nullptr;
244
245 MEDIA_DEBUG_LOG("For get connection type");
246 napi_get_undefined(env, &undefinedResult);
247 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
248
249 if (status != napi_ok || thisVar == nullptr) {
250 MEDIA_ERR_LOG("Invalid arguments!");
251 return undefinedResult;
252 }
253 MEDIA_DEBUG_LOG("To get connection type");
254 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
255 if (status == napi_ok && obj != nullptr && obj->cameraDevice_ != nullptr) {
256 jsConnectionType = obj->cameraDevice_->GetConnectionType();
257
258 status = napi_create_int32(env, jsConnectionType, &jsResult);
259 if (status == napi_ok) {
260 return jsResult;
261 } else {
262 MEDIA_ERR_LOG("Failed to get connectionType!, errorCode : %{public}d", status);
263 }
264 }
265 MEDIA_ERR_LOG("GetConnectionType call Failed!");
266 return undefinedResult;
267 }
268
GetHostDeviceName(napi_env env,napi_callback_info info)269 napi_value CameraDeviceNapi::GetHostDeviceName(napi_env env, napi_callback_info info)
270 {
271 if (!CameraNapiUtils::CheckSystemApp(env)) {
272 MEDIA_ERR_LOG("SystemApi GetHostDeviceName is called!");
273 return nullptr;
274 }
275 napi_status status;
276 napi_value jsResult = nullptr;
277 napi_value undefinedResult = nullptr;
278 CameraDeviceNapi* obj = nullptr;
279 napi_value thisVar = nullptr;
280
281 MEDIA_DEBUG_LOG("For get host device name");
282 napi_get_undefined(env, &undefinedResult);
283 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
284
285 if (status != napi_ok || thisVar == nullptr) {
286 MEDIA_ERR_LOG("Invalid arguments!");
287 return undefinedResult;
288 }
289 MEDIA_DEBUG_LOG("to get host device name");
290 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
291 if (status == napi_ok && obj != nullptr && obj->cameraDevice_ != nullptr) {
292 std::string hostDeviceName = obj->cameraDevice_->GetHostName();
293 status = napi_create_string_utf8(env, hostDeviceName.c_str(), NAPI_AUTO_LENGTH, &jsResult);
294 if (status == napi_ok) {
295 return jsResult;
296 } else {
297 MEDIA_ERR_LOG("Failed to get camera host Device Name!, errorCode : %{public}d", status);
298 }
299 }
300 return undefinedResult;
301 }
GetHostDeviceType(napi_env env,napi_callback_info info)302 napi_value CameraDeviceNapi::GetHostDeviceType(napi_env env, napi_callback_info info)
303 {
304 if (!CameraNapiUtils::CheckSystemApp(env)) {
305 MEDIA_ERR_LOG("SystemApi GetHostDeviceType is called!");
306 return nullptr;
307 }
308 napi_status status;
309 napi_value jsResult = nullptr;
310 napi_value undefinedResult = nullptr;
311 CameraDeviceNapi* obj = nullptr;
312 ConnectionType jsConnectionType;
313 napi_value thisVar = nullptr;
314
315 MEDIA_DEBUG_LOG("For get host device type");
316 napi_get_undefined(env, &undefinedResult);
317 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
318
319 if (status != napi_ok || thisVar == nullptr) {
320 MEDIA_ERR_LOG("Invalid arguments!");
321 return undefinedResult;
322 }
323 MEDIA_DEBUG_LOG("To get host device type");
324 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
325 if (status == napi_ok && obj != nullptr && obj->cameraDevice_ != nullptr) {
326 jsConnectionType = obj->cameraDevice_->GetConnectionType();
327 if (jsConnectionType == CAMERA_CONNECTION_BUILT_IN) {
328 MEDIA_DEBUG_LOG("local device cameraHostType is undefine");
329 return undefinedResult;
330 } else {
331 uint32_t hostDeviceType = obj->cameraDevice_->GetDeviceType();
332 status = napi_create_uint32(env, hostDeviceType, &jsResult);
333 }
334 if (status == napi_ok) {
335 return jsResult;
336 } else {
337 MEDIA_ERR_LOG("Failed to get camera host Device Type!, errorCode : %{public}d", status);
338 }
339 }
340 return undefinedResult;
341 }
342 } // namespace CameraStandard
343 } // namespace OHOS
344