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 "hilog/log.h"
17 #include "input/camera_input_napi.h"
18 #include "output/metadata_object_napi.h"
19
20 namespace OHOS {
21 namespace CameraStandard {
22 thread_local napi_ref MetadataObjectNapi::sConstructor_ = nullptr;
23 thread_local sptr<MetadataObject> g_metadataObject;
24
CreateMetaFaceObj(napi_env env,sptr<MetadataObject> metaObj)25 napi_value MetadataObjectNapi::CreateMetaFaceObj(napi_env env, sptr<MetadataObject> metaObj)
26 {
27 MEDIA_DEBUG_LOG("CreateMetaFaceObj is called");
28 napi_status status;
29 napi_value result = nullptr;
30 napi_value constructor;
31
32 status = napi_get_reference_value(env, sConstructor_, &constructor);
33 if (status == napi_ok) {
34 g_metadataObject = metaObj;
35 status = napi_new_instance(env, constructor, 0, nullptr, &result);
36 g_metadataObject = nullptr;
37 if (status == napi_ok && result != nullptr) {
38 return result;
39 } else {
40 MEDIA_ERR_LOG("Failed to create Camera obj instance");
41 }
42 }
43 MEDIA_ERR_LOG("CreateMetaFaceObj call Failed");
44 napi_get_undefined(env, &result);
45 return result;
46 }
47
MetadataObjectNapi()48 MetadataObjectNapi::MetadataObjectNapi() : env_(nullptr), wrapper_(nullptr)
49 {
50 }
51
~MetadataObjectNapi()52 MetadataObjectNapi::~MetadataObjectNapi()
53 {
54 MEDIA_DEBUG_LOG("~MetadataObjectNapi is called");
55 if (wrapper_ != nullptr) {
56 napi_delete_reference(env_, wrapper_);
57 }
58 if (metadataObject_) {
59 metadataObject_ = nullptr;
60 }
61 }
62
MetadataObjectNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)63 void MetadataObjectNapi::MetadataObjectNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
64 {
65 MEDIA_DEBUG_LOG("MetadataObjectNapiDestructor is called");
66 MetadataObjectNapi* metadataObject = reinterpret_cast<MetadataObjectNapi*>(nativeObject);
67 if (metadataObject != nullptr) {
68 delete metadataObject;
69 }
70 }
71
Init(napi_env env,napi_value exports)72 napi_value MetadataObjectNapi::Init(napi_env env, napi_value exports)
73 {
74 MEDIA_DEBUG_LOG("Init is called");
75 napi_status status;
76 napi_value ctorObj;
77 int32_t refCount = 1;
78
79 napi_property_descriptor metadata_object_props[] = {
80 DECLARE_NAPI_FUNCTION("getType", GetType),
81 DECLARE_NAPI_FUNCTION("getTimestamp", GetTimestamp),
82 DECLARE_NAPI_FUNCTION("getBoundingBox", GetBoundingBox),
83 };
84
85 status = napi_define_class(env, CAMERA_METADATA_OBJECT_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
86 MetadataObjectNapiConstructor, nullptr,
87 sizeof(metadata_object_props) / sizeof(metadata_object_props[PARAM0]),
88 metadata_object_props, &ctorObj);
89 if (status == napi_ok) {
90 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
91 if (status == napi_ok) {
92 status = napi_set_named_property(env, exports, CAMERA_METADATA_OBJECT_NAPI_CLASS_NAME, ctorObj);
93 if (status == napi_ok) {
94 return exports;
95 }
96 }
97 }
98 MEDIA_ERR_LOG("Init call Failed");
99 return nullptr;
100 }
101
MetadataObjectNapiConstructor(napi_env env,napi_callback_info info)102 napi_value MetadataObjectNapi::MetadataObjectNapiConstructor(napi_env env, napi_callback_info info)
103 {
104 MEDIA_DEBUG_LOG("MetadataObjectNapiConstructor is called");
105 napi_status status;
106 napi_value result = nullptr;
107 napi_value thisVar = nullptr;
108
109 napi_get_undefined(env, &result);
110 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
111
112 if (status == napi_ok && thisVar != nullptr) {
113 std::unique_ptr<MetadataObjectNapi> obj = std::make_unique<MetadataObjectNapi>();
114 obj->env_ = env;
115 obj->metadataObject_ = g_metadataObject;
116 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
117 MetadataObjectNapi::MetadataObjectNapiDestructor, nullptr, nullptr);
118 if (status == napi_ok) {
119 obj.release();
120 return thisVar;
121 } else {
122 MEDIA_ERR_LOG("Failure wrapping js to native napi");
123 }
124 }
125 MEDIA_ERR_LOG("MetadataObjectNapiConstructor call Failed");
126 return result;
127 }
128
GetMetadataObject()129 sptr<MetadataObject> MetadataObjectNapi::GetMetadataObject()
130 {
131 return metadataObject_;
132 }
133
GetType(napi_env env,napi_callback_info info)134 napi_value MetadataObjectNapi::GetType(napi_env env, napi_callback_info info)
135 {
136 MEDIA_DEBUG_LOG("GetType 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 MetadataObjectNapi* metadataObjectNapi = nullptr;
147 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&metadataObjectNapi));
148 if (status == napi_ok && metadataObjectNapi != nullptr) {
149 MetadataObjectType metadataObjType = metadataObjectNapi->metadataObject_->GetType();
150 int32_t iProp;
151 CameraNapiUtils::MapMetadataObjSupportedTypesEnum(metadataObjType, iProp);
152 napi_create_int32(env, iProp, &result);
153 }
154 MEDIA_ERR_LOG("GetType call Failed");
155 return result;
156 }
157
GetTimestamp(napi_env env,napi_callback_info info)158 napi_value MetadataObjectNapi::GetTimestamp(napi_env env, napi_callback_info info)
159 {
160 MEDIA_DEBUG_LOG("GetTimestamp is called");
161 napi_status status;
162 napi_value result = nullptr;
163 size_t argc = ARGS_ZERO;
164 napi_value argv[ARGS_ZERO];
165 napi_value thisVar = nullptr;
166
167 CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
168
169 napi_get_undefined(env, &result);
170 MetadataObjectNapi* metadataObjectNapi = nullptr;
171 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&metadataObjectNapi));
172 if (status == napi_ok && metadataObjectNapi != nullptr) {
173 double metaTimestamp = metadataObjectNapi->metadataObject_->GetTimestamp();
174 napi_create_double(env, metaTimestamp, &result);
175 }
176 MEDIA_ERR_LOG("GetTimestamp call Failed");
177 return result;
178 }
179
GetBoundingBox(napi_env env,napi_callback_info info)180 napi_value MetadataObjectNapi::GetBoundingBox(napi_env env, napi_callback_info info)
181 {
182 MEDIA_DEBUG_LOG("GetBoundingBox is called");
183 napi_status status;
184 napi_value result = nullptr;
185 size_t argc = ARGS_ZERO;
186 napi_value argv[ARGS_ZERO];
187 napi_value thisVar = nullptr;
188
189 CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
190
191 napi_get_undefined(env, &result);
192 MetadataObjectNapi* metadataObjectNapi = nullptr;
193 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&metadataObjectNapi));
194 if (status == napi_ok && metadataObjectNapi != nullptr) {
195 Rect metaFace = metadataObjectNapi->metadataObject_->GetBoundingBox();
196
197 napi_value propValue;
198
199 napi_create_object(env, &result);
200
201 napi_create_double(env, metaFace.topLeftX, &propValue);
202 napi_set_named_property(env, result, "topLeftX", propValue);
203
204 napi_create_double(env, metaFace.topLeftY, &propValue);
205 napi_set_named_property(env, result, "topLeftY", propValue);
206
207 napi_create_double(env, metaFace.width, &propValue);
208 napi_set_named_property(env, result, "width", propValue);
209
210 napi_create_double(env, metaFace.height, &propValue);
211 napi_set_named_property(env, result, "height", propValue);
212 }
213 MEDIA_ERR_LOG("GetBoundingBox call Failed");
214 return result;
215 }
216 } // namespace CameraStandard
217 } // namespace OHOS