• 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 #ifndef CAMERA_NAPI_UTILS_H_
17 #define CAMERA_NAPI_UTILS_H_
18 
19 #include <vector>
20 #include "camera_error_code.h"
21 #include "camera_device_ability_items.h"
22 #include "input/camera_input.h"
23 #include "camera_log.h"
24 #include "napi/native_api.h"
25 #include "napi/native_node_api.h"
26 #include "output/photo_output.h"
27 #include "input/camera_manager.h"
28 #include "mode/mode_manager.h"
29 #include "ipc_skeleton.h"
30 #include "tokenid_kit.h"
31 
32 #define CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar)                 \
33     do {                                                                        \
34         void* data;                                                             \
35         napi_get_cb_info(env, info, &(argc), argv, &(thisVar), &data);          \
36     } while (0)
37 
38 #define CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar)                       \
39     do {                                                                                        \
40         void* data;                                                                             \
41         status = napi_get_cb_info(env, info, nullptr, nullptr, &(thisVar), &data);              \
42     } while (0)
43 
44 #define CAMERA_NAPI_GET_JS_ASYNC_CB_REF(env, arg, count, cbRef)         \
45     do {                                                                \
46         napi_valuetype valueType = napi_undefined;                      \
47         napi_typeof(env, arg, &valueType);                              \
48         if (valueType == napi_function) {                               \
49             napi_create_reference(env, arg, count, &(cbRef));           \
50         } else {                                                        \
51             NAPI_ASSERT(env, false, "type mismatch");                   \
52         }                                                               \
53     } while (0);
54 
55 #define CAMERA_NAPI_ASSERT_NULLPTR_CHECK(env, result)       \
56     do {                                                    \
57         if ((result) == nullptr) {                          \
58             napi_get_undefined(env, &(result));             \
59             return result;                                  \
60         }                                                   \
61     } while (0);
62 
63 #define CAMERA_NAPI_CREATE_PROMISE(env, callbackRef, deferred, result)      \
64     do {                                                                    \
65         if ((callbackRef) == nullptr) {                                     \
66             napi_create_promise(env, &(deferred), &(result));               \
67         }                                                                   \
68     } while (0);
69 
70 #define CAMERA_NAPI_CREATE_RESOURCE_NAME(env, resource, resourceName)                       \
71     do {                                                                                    \
72         napi_create_string_utf8(env, resourceName, NAPI_AUTO_LENGTH, &(resource));          \
73     } while (0);
74 
75 #define CAMERA_NAPI_CHECK_NULL_PTR_RETURN_UNDEFINED(env, ptr, ret, message)     \
76     do {                                                            \
77         if ((ptr) == nullptr) {                                     \
78             HiLog::Error(LABEL, message);                           \
79             napi_get_undefined(env, &(ret));                        \
80             return ret;                                             \
81         }                                                           \
82     } while (0)
83 
84 #define CAMERA_NAPI_CHECK_NULL_PTR_RETURN_VOID(ptr, message)   \
85     do {                                           \
86         if ((ptr) == nullptr) {                    \
87             HiLog::Error(LABEL, message);          \
88             return;                                \
89         }                                          \
90     } while (0)
91 
92 #define CAMERA_NAPI_ASSERT_EQUAL(condition, errMsg)     \
93     do {                                    \
94         if (!(condition)) {                 \
95             HiLog::Error(LABEL, errMsg);    \
96             return;                         \
97         }                                   \
98     } while (0)
99 
100 #define CAMERA_NAPI_CHECK_AND_BREAK_LOG(cond, fmt, ...)            \
101     do {                                               \
102         if (!(cond)) {                                 \
103             MEDIA_ERR_LOG(fmt, ##__VA_ARGS__);         \
104             break;                                     \
105         }                                              \
106     } while (0)
107 
108 #define CAMERA_NAPI_CHECK_AND_RETURN_LOG(cond, fmt, ...)           \
109     do {                                               \
110         if (!(cond)) {                                 \
111             MEDIA_ERR_LOG(fmt, ##__VA_ARGS__);         \
112             return;                                    \
113         }                                              \
114     } while (0)
115 
116 namespace OHOS {
117 namespace CameraStandard {
118 /* Constants for array index */
119 const int32_t PARAM0 = 0;
120 const int32_t PARAM1 = 1;
121 const int32_t PARAM2 = 2;
122 
123 /* Constants for array size */
124 const int32_t ARGS_ZERO = 0;
125 const int32_t ARGS_ONE = 1;
126 const int32_t ARGS_TWO = 2;
127 const int32_t ARGS_THREE = 3;
128 const int32_t SIZE = 100;
129 
130 struct AsyncContext {
131     napi_env env;
132     napi_async_work work;
133     napi_deferred deferred;
134     napi_ref callbackRef;
135     bool status;
136     int32_t taskId;
137     int32_t errorCode;
138     std::string errorMsg;
139     std::string funcName;
140     bool isInvalidArgument;
141 };
142 
143 struct JSAsyncContextOutput {
144     napi_value error;
145     napi_value data;
146     bool status;
147     bool bRetBool;
148     std::string funcName;
149 };
150 
151 enum JSQualityLevel {
152     QUALITY_LEVEL_HIGH = 0,
153     QUALITY_LEVEL_MEDIUM,
154     QUALITY_LEVEL_LOW
155 };
156 
157 enum JSImageRotation {
158     ROTATION_0 = 0,
159     ROTATION_90 = 90,
160     ROTATION_180 = 180,
161     ROTATION_270 = 270
162 };
163 
164 enum JSCameraStatus {
165     JS_CAMERA_STATUS_APPEAR = 0,
166     JS_CAMERA_STATUS_DISAPPEAR = 1,
167     JS_CAMERA_STATUS_AVAILABLE = 2,
168     JS_CAMERA_STATUS_UNAVAILABLE = 3
169 };
170 
171 enum CameraTaskId {
172     CAMERA_MANAGER_TASKID = 0x01000000,
173     CAMERA_INPUT_TASKID = 0x02000000,
174     CAMERA_PHOTO_OUTPUT_TASKID = 0x03000000,
175     CAMERA_PREVIEW_OUTPUT_TASKID = 0x04000000,
176     CAMERA_VIDEO_OUTPUT_TASKID = 0x05000000,
177     CAMERA_SESSION_TASKID = 0x06000000,
178     MODE_MANAGER_TASKID = 0x07000000,
179 };
180 
181 enum JSMetadataObjectType {
182     FACE = 0
183 };
184 
185 enum CameraSteps {
186     CREATE_CAMERA_INPUT_INSTANCE,
187     CREATE_PREVIEW_OUTPUT_INSTANCE,
188     CREATE_PHOTO_OUTPUT_INSTANCE,
189     CREATE_VIDEO_OUTPUT_INSTANCE,
190     CREATE_METADATA_OUTPUT_INSTANCE,
191     ADD_INPUT,
192     REMOVE_INPUT,
193     ADD_OUTPUT,
194     REMOVE_OUTPUT,
195     PHOTO_OUT_CAPTURE,
196     ADD_DEFERRED_SURFACE,
197     CREATE_DEFERRED_PREVIEW_OUTPUT
198 };
199 
200 /* Util class used by napi asynchronous methods for making call to js callback function */
201 class CameraNapiUtils {
202 public:
203     static int32_t MapExposureModeEnumFromJs(int32_t jsExposureMode, camera_exposure_mode_enum_t &nativeExposureMode);
204 
205     static void MapExposureModeEnum(camera_exposure_mode_enum_t nativeExposureMode, int32_t &jsExposureMode);
206 
207     static void MapFocusStateEnum(FocusCallback::FocusState nativeFocusState, int32_t &jsFocusState);
208 
209     static void MapExposureStateEnum(ExposureCallback::ExposureState nativeExposureState, int32_t &jsExposureState);
210 
211     static void MapCameraPositionEnum(camera_position_enum_t nativeCamPos, int32_t &jsCameraPosition);
212 
213     static int32_t MapCameraPositionEnumFromJs(int32_t jsCameraPosition, camera_position_enum_t &nativeCamPos);
214 
215     static void MapCameraFormatEnum(camera_format_t nativeCamFormat, int32_t &jsCameraFormat);
216 
217     static void MapMetadataObjSupportedTypesEnum(MetadataObjectType nativeMetadataObjType, int32_t &jsMetadataObjType);
218 
219     static void MapMetadataObjSupportedTypesEnumFromJS(int32_t jsMetadataObjType,
220         MetadataObjectType &nativeMetadataObjType, bool &isValid);
221 
222     static int32_t MapCameraFormatEnumFromJs(int32_t jsCameraFormat, camera_format_t &nativeCamFormat);
223 
224     static void MapCameraTypeEnum(camera_type_enum_t nativeCamType, int32_t &jsCameraType);
225 
226     static int32_t MapCameraTypeEnumFromJs(int32_t jsCameraType, camera_type_enum_t &nativeCamType);
227 
228     static void MapCameraConnectionTypeEnum(camera_connection_type_t nativeCamConnType, int32_t &jsCameraConnType);
229 
230     static int32_t MapQualityLevelFromJs(int32_t jsQuality, PhotoCaptureSetting::QualityLevel &nativeQuality);
231 
232     static int32_t MapImageRotationFromJs(int32_t jsRotation, PhotoCaptureSetting::RotationConfig &nativeRotation);
233 
234     static void MapCameraStatusEnum(CameraStatus deviceStatus, int32_t &jsCameraStatus);
235 
236     static void VideoStabilizationModeEnum(
237         CameraVideoStabilizationMode nativeVideoStabilizationMode, int32_t &jsVideoStabilizationMode);
238 
239     static void CreateNapiErrorObject(napi_env env, int32_t errorCode, const char* errString,
240         std::unique_ptr<JSAsyncContextOutput> &jsContext);
241 
242     static void InvokeJSAsyncMethod(napi_env env, napi_deferred deferred,
243         napi_ref callbackRef, napi_async_work work, const JSAsyncContextOutput &asyncContext);
244 
245     static int32_t IncreamentAndGet(uint32_t &num);
246 
247     static bool CheckInvalidArgument(napi_env env, size_t argc, int32_t length,
248                                      napi_value *argv, CameraSteps step);
249 
250     static bool CheckError(napi_env env, int32_t retCode);
251 
252     static double FloatToDouble(float val);
253 
254     static bool CheckSystemApp(napi_env env);
255 };
256 } // namespace CameraStandard
257 } // namespace OHOS
258 #endif /* CAMERA_NAPI_UTILS_H_ */
259