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