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_info_napi.h"
17
18 namespace OHOS {
19 namespace CameraStandard {
20 using namespace std;
21 using OHOS::HiviewDFX::HiLog;
22 using OHOS::HiviewDFX::HiLogLabel;
23
24 thread_local napi_ref CameraDeviceNapi::sConstructor_ = nullptr;
25 thread_local sptr<CameraDevice> CameraDeviceNapi::sCameraDevice_ = nullptr;
26
CameraDeviceNapi()27 CameraDeviceNapi::CameraDeviceNapi() : env_(nullptr), wrapper_(nullptr)
28 {
29 }
30
~CameraDeviceNapi()31 CameraDeviceNapi::~CameraDeviceNapi()
32 {
33 if (wrapper_ != nullptr) {
34 napi_delete_reference(env_, wrapper_);
35 }
36 if (cameraDevice_) {
37 cameraDevice_ = nullptr;
38 }
39 }
40
CameraDeviceNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)41 void CameraDeviceNapi::CameraDeviceNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
42 {
43 CameraDeviceNapi* cameraObj = reinterpret_cast<CameraDeviceNapi*>(nativeObject);
44 if (cameraObj != nullptr) {
45 cameraObj->~CameraDeviceNapi();
46 }
47 }
48
Init(napi_env env,napi_value exports)49 napi_value CameraDeviceNapi::Init(napi_env env, napi_value exports)
50 {
51 napi_status status;
52 napi_value ctorObj;
53 int32_t refCount = 1;
54
55 napi_property_descriptor camera_object_props[] = {
56 DECLARE_NAPI_GETTER("cameraId", GetCameraId),
57 DECLARE_NAPI_GETTER("cameraPosition", GetCameraPosition),
58 DECLARE_NAPI_GETTER("cameraType", GetCameraType),
59 DECLARE_NAPI_GETTER("connectionType", GetConnectionType)
60 };
61
62 status = napi_define_class(env, CAMERA_OBJECT_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
63 CameraDeviceNapiConstructor, nullptr,
64 sizeof(camera_object_props) / sizeof(camera_object_props[PARAM0]),
65 camera_object_props, &ctorObj);
66 if (status == napi_ok) {
67 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
68 if (status == napi_ok) {
69 status = napi_set_named_property(env, exports, CAMERA_OBJECT_NAPI_CLASS_NAME, ctorObj);
70 if (status == napi_ok) {
71 return exports;
72 }
73 }
74 }
75
76 return nullptr;
77 }
78
79 // Constructor callback
CameraDeviceNapiConstructor(napi_env env,napi_callback_info info)80 napi_value CameraDeviceNapi::CameraDeviceNapiConstructor(napi_env env, napi_callback_info info)
81 {
82 napi_status status;
83 napi_value result = nullptr;
84 napi_value thisVar = nullptr;
85
86 napi_get_undefined(env, &result);
87 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
88
89 if (status == napi_ok && thisVar != nullptr) {
90 std::unique_ptr<CameraDeviceNapi> obj = std::make_unique<CameraDeviceNapi>();
91 obj->env_ = env;
92 obj->cameraDevice_ = sCameraDevice_;
93 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
94 CameraDeviceNapi::CameraDeviceNapiDestructor, nullptr, &(obj->wrapper_));
95 if (status == napi_ok) {
96 obj.release();
97 return thisVar;
98 } else {
99 MEDIA_ERR_LOG("Failure wrapping js to native napi");
100 }
101 }
102
103 return result;
104 }
105
CreateCameraObj(napi_env env,sptr<CameraDevice> cameraDevice)106 napi_value CameraDeviceNapi::CreateCameraObj(napi_env env, sptr<CameraDevice> cameraDevice)
107 {
108 napi_status status;
109 napi_value result = nullptr;
110 napi_value constructor;
111
112 status = napi_get_reference_value(env, sConstructor_, &constructor);
113 if (status == napi_ok) {
114 sCameraDevice_ = cameraDevice;
115 status = napi_new_instance(env, constructor, 0, nullptr, &result);
116 sCameraDevice_ = nullptr;
117 if (status == napi_ok && result != nullptr) {
118 return result;
119 } else {
120 MEDIA_ERR_LOG("Failed to create Camera obj instance");
121 }
122 }
123
124 napi_get_undefined(env, &result);
125
126 return result;
127 }
128
GetCameraId(napi_env env,napi_callback_info info)129 napi_value CameraDeviceNapi::GetCameraId(napi_env env, napi_callback_info info)
130 {
131 napi_status status;
132 napi_value jsResult = nullptr;
133 napi_value undefinedResult = nullptr;
134 CameraDeviceNapi* obj = nullptr;
135 napi_value thisVar = nullptr;
136
137 napi_get_undefined(env, &undefinedResult);
138 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
139
140 if (status != napi_ok || thisVar == nullptr) {
141 MEDIA_ERR_LOG("Invalid arguments!");
142 return undefinedResult;
143 }
144
145 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
146 if (status == napi_ok && obj != nullptr) {
147 std::string cameraId = obj->cameraDevice_->GetID();
148 status = napi_create_string_utf8(env, cameraId.c_str(), NAPI_AUTO_LENGTH, &jsResult);
149 if (status == napi_ok) {
150 return jsResult;
151 } else {
152 MEDIA_ERR_LOG("Failed to get camera id!, errorCode : %{public}d", status);
153 }
154 }
155
156 return undefinedResult;
157 }
158
GetCameraPosition(napi_env env,napi_callback_info info)159 napi_value CameraDeviceNapi::GetCameraPosition(napi_env env, napi_callback_info info)
160 {
161 napi_status status;
162 napi_value jsResult = nullptr;
163 napi_value undefinedResult = nullptr;
164 CameraDeviceNapi* obj = nullptr;
165 CameraPosition jsCameraPosition;
166 napi_value thisVar = nullptr;
167
168 napi_get_undefined(env, &undefinedResult);
169 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
170
171 if (status != napi_ok || thisVar == nullptr) {
172 MEDIA_ERR_LOG("Invalid arguments!");
173 return undefinedResult;
174 }
175
176 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
177 if (status == napi_ok && obj != nullptr) {
178 jsCameraPosition = obj->cameraDevice_->GetPosition();
179 status = napi_create_int32(env, jsCameraPosition, &jsResult);
180 if (status == napi_ok) {
181 return jsResult;
182 } else {
183 MEDIA_ERR_LOG("Failed to get cameraPosition!, errorCode : %{public}d", status);
184 }
185 }
186
187 return undefinedResult;
188 }
189
GetCameraType(napi_env env,napi_callback_info info)190 napi_value CameraDeviceNapi::GetCameraType(napi_env env, napi_callback_info info)
191 {
192 napi_status status;
193 napi_value jsResult = nullptr;
194 napi_value undefinedResult = nullptr;
195 CameraDeviceNapi* obj = nullptr;
196 CameraType jsCameraType;
197 napi_value thisVar = nullptr;
198
199 napi_get_undefined(env, &undefinedResult);
200 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
201
202 if (status != napi_ok || thisVar == nullptr) {
203 MEDIA_ERR_LOG("Invalid arguments!");
204 return undefinedResult;
205 }
206
207 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
208 if (status == napi_ok && obj != nullptr) {
209 jsCameraType = obj->cameraDevice_->GetCameraType();
210 if (jsCameraType == CAMERA_TYPE_UNSUPPORTED) {
211 MEDIA_ERR_LOG("Camera type is not a recognized camera type in JS");
212 return undefinedResult;
213 }
214 status = napi_create_int32(env, jsCameraType, &jsResult);
215 if (status == napi_ok) {
216 return jsResult;
217 } else {
218 MEDIA_ERR_LOG("Failed to get cameraType!, errorCode : %{public}d", status);
219 }
220 }
221
222 return undefinedResult;
223 }
224
GetConnectionType(napi_env env,napi_callback_info info)225 napi_value CameraDeviceNapi::GetConnectionType(napi_env env, napi_callback_info info)
226 {
227 napi_status status;
228 napi_value jsResult = nullptr;
229 napi_value undefinedResult = nullptr;
230 CameraDeviceNapi* obj = nullptr;
231 ConnectionType jsConnectionType;
232 napi_value thisVar = nullptr;
233
234 napi_get_undefined(env, &undefinedResult);
235 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
236
237 if (status != napi_ok || thisVar == nullptr) {
238 MEDIA_ERR_LOG("Invalid arguments!");
239 return undefinedResult;
240 }
241
242 status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
243 if (status == napi_ok && obj != nullptr) {
244 jsConnectionType = obj->cameraDevice_->GetConnectionType();
245
246 status = napi_create_int32(env, jsConnectionType, &jsResult);
247 if (status == napi_ok) {
248 return jsResult;
249 } else {
250 MEDIA_ERR_LOG("Failed to get connectionType!, errorCode : %{public}d", status);
251 }
252 }
253
254 return undefinedResult;
255 }
256 } // namespace CameraStandard
257 } // namespace OHOS
258