• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 #ifndef CAMERA_NATIVE_CAMERA_H
17 #define CAMERA_NATIVE_CAMERA_H
18 
19 #include <native_buffer.h>
20 #include <unistd.h>
21 #include <string>
22 #include <thread>
23 #include <cstdio>
24 #include <fcntl.h>
25 #include "napi/native_api.h"
26 #include "iostream"
27 #include "mutex"
28 #include "hilog/log.h"
29 #include "camera.h"
30 #include "camera_manager.h"
31 #include "camera_input.h"
32 #include "capture_session.h"
33 #include "preview_output.h"
34 #include "photo_output.h"
35 #include "video_output.h"
36 #include <map>
37 #include <string>
38 #include <vector>
39 
40 #define COLOR_SPACE_NOT_SUPPORTED_MODE 3
41 #define TARGET_ZOOM 1.5
42 #define ALL_CALLBACK_IS_NULL 1
43 #define ONLY_ON_ERROR 2
44 #define ONLY_ON_FOCUS_STATE_CHANGE 3
45 #define INVALID_SURFACE_ID 4
46 #define SET_OH_COLORSPACE_SRGB_FULL 2
47 #define INVALID_MIN_FPS (-1)
48 #define INVALID_MAX_FPS 5
49 #define COLOR_SPACE_NOT_SUPPORTED 0
50 #define SET_CAMERA_FRONT_FOR_SECURE_PHOTO 5
51 #define BACK_CAMERA 0
52 #define FRONT_CAMERA 1
53 
54 typedef enum CameraCallbackCode {
55     CameraInput_Status = 1,
56     Preview_OnFrameStart = 2,
57     Preview_OnFrameEnd = 3,
58     Preview_OnError = 4,
59     Photo_OnFrameStart = 5,
60     Photo_OnFrameShutter = 6,
61     Photo_OnFrameEnd = 7,
62     Photo_OnError = 8,
63     Video_OnFrameStart = 9,
64     Video_OnFrameEnd = 10,
65     Video_OnError = 11,
66     Metadata_Object_Available = 12,
67     Metadata_Output_Error = 13,
68     Session_OnFocusState_Change = 14,
69     Session_OnError = 15,
70     CameraManager_Status = 16,
71     NoReceived = 10086,
72     PHOTO_ON_PHOTO_AVAILABLE = 17,
73     PHOTO_ON_PHOTO_ASSET_AVAILABLE = 18,
74 } CameraCallbackCode;
75 
76 typedef enum UseCaseCode {
77     PARAMETER_OK = 0,
78     PARAMETER1_ERROR = 1,
79     PARAMETER2_ERROR = 2,
80     PARAMETER3_ERROR = 3,
81     PARAMETER4_ERROR = 4,
82 } UseCaseCode;
83 
84 class NDKCamera {
85 public:
86     ~NDKCamera(void);
87     NDKCamera(char *str);
88 
89     bool IsMirror_;
90     bool HasFlash_;
91     bool IsFlashMode_;
92     bool IsExposureMode_;
93     float minExposureBias_;
94     float maxExposureBias_;
95     float exposureBias_;
96     bool isFocusSupported_;
97     float minZoom_;
98     float maxZoom_;
99     float zoom_;
100     bool isVideoSupported_;
101     Camera_FlashMode flashMode_;
102     Camera_ExposureMode exposureMode_;
103     Camera_Point point_;
104     Camera_FocusMode focusMode_;
105     Camera_Point focusPoint_;
106     Camera_VideoStabilizationMode videoMode_;
107     Camera_Device* cameras_;
108     Camera_OutputCapability* cameraOutputCapability_;
109     bool isAddInput_;
110     Camera_Device *camera_;
111     Camera_SceneMode *sceneModes_;
112     Camera_SceneMode sceneMode_;
113     uint64_t secureSeqId_;
114     bool canPreconfig_;
115     uint32_t sceneModesSize_;
116     bool isNormalPhoto_;
117     bool isNormalVideo_;
118     bool isSecurePhoto_;
119     bool isMovingPhotoSupported_;
120     OH_PhotoNative *photoNative_;
121     OH_ImageNative *mainImage_;
122     bool isTorchSupported_;
123     bool isTorchSupportedByTorchMode_;
124     Camera_TorchMode torchMode_;
125     uint32_t frameRatesSize_;
126     uint32_t videoFrameRatesSize_;
127     uint32_t colorSpacesSize_;
128     OH_NativeBuffer_ColorSpace* colorSpace_;
129     OH_NativeBuffer_ColorSpace activeColorSpace_;
130     OH_NativeBuffer_ColorSpace setcolorSpace_;
131     Camera_FrameRateRange* frameRateRange_;
132     Camera_FrameRateRange activeFrameRateRange_;
133     Camera_FrameRateRange* videoFrameRateRange_;
134     Camera_FrameRateRange videoActiveFrameRateRange_;
135     Camera_ImageRotation imageRotation_;
136     Camera_ImageRotation previewRotation_;
137     bool isCreatePhotoOutputWithoutSurface_;
138 
139     //callback
140     static CameraCallbackCode cameraCallbackCode_;
141 
142     Camera_ErrorCode CreateCameraInput(void);
143     Camera_ErrorCode CreateCameraInputWithPositionAndType(Camera_Position position, Camera_Type type);
144     Camera_ErrorCode CreateSession(void);
145     Camera_ErrorCode CameraInputOpen(void);
146     Camera_ErrorCode CameraInputClose(void);
147     Camera_ErrorCode CameraInputRelease(void);
148     Camera_ErrorCode GetSupportedCameras(void);
149     Camera_ErrorCode GetCameraOrientation(void);
150     Camera_ErrorCode GetSupportedOutputCapability(void);
151     Camera_ErrorCode CreatePreviewOutput(void);
152     Camera_ErrorCode CreatePhotoOutput(char* photoSurfaceId);
153     Camera_ErrorCode CreateVideoOutput(char* videoId);
154     Camera_ErrorCode CreateMetadataOutput(void);
155     Camera_ErrorCode IsCameraMuted(void);
156     Camera_ErrorCode PreviewOutputStop(void);
157     Camera_ErrorCode PreviewOutputRelease(void);
158     Camera_ErrorCode PhotoOutputRelease(void);
159     Camera_ErrorCode HasFlashFn(uint32_t mode);
160     Camera_ErrorCode setZoomRatioFn(uint32_t zoomRatio);
161 
162     Camera_ErrorCode SessionBegin(void);
163     Camera_ErrorCode SessionCommitConfig(void);
164     Camera_ErrorCode SessionStart(void);
165     Camera_ErrorCode SessionStop(void);
166     Camera_ErrorCode StartVideo(char* videoId);
167     Camera_ErrorCode AddVideoOutput(void);
168     Camera_ErrorCode VideoOutputStart(void);
169 
170     Camera_ErrorCode PreviewOutputStart(void);
171     Camera_ErrorCode PhotoOutputCapture(void);
172     Camera_ErrorCode TakePictureWithPhotoSettings(Camera_PhotoCaptureSetting photoSetting);
173     Camera_ErrorCode IsMirrorSupported(void);
174     Camera_ErrorCode VideoOutputStop(void);
175     Camera_ErrorCode VideoOutputRelease(void);
176     Camera_ErrorCode MetadataOutputStart(void);
177     Camera_ErrorCode MetadataOutputStop(void);
178     Camera_ErrorCode MetadataOutputRelease(void);
179 
180     Camera_ErrorCode SessionAddInput(void);
181     Camera_ErrorCode SessionRemoveInput(void);
182     Camera_ErrorCode SessionAddPreviewOutput(void);
183     Camera_ErrorCode SessionAddPhotoOutput(void);
184     Camera_ErrorCode SessionAddVideoOutput(void);
185     Camera_ErrorCode SessionAddMetadataOutput(void);
186     Camera_ErrorCode SessionRemovePreviewOutput(void);
187     Camera_ErrorCode SessionRemovePhotoOutput(void);
188     Camera_ErrorCode SessionRemoveVideoOutput(void);
189     Camera_ErrorCode SessionRemoveMetadataOutput(void);
190     Camera_ErrorCode SessionRelease(void);
191 
192     Camera_ErrorCode SessionHasFlash(void);
193     Camera_ErrorCode SessionIsFlashModeSupported(uint32_t mode);
194     Camera_ErrorCode SessionGetFlashMode(void);
195     Camera_ErrorCode SessionSetFlashMode(uint32_t mode);
196 
197     Camera_ErrorCode SessionIsExposureModeSupported(uint32_t mode);
198     Camera_ErrorCode SessionGetExposureMode(void);
199     Camera_ErrorCode SessionSetExposureMode(uint32_t mode);
200     Camera_ErrorCode SessionGetMeteringPoint(void);
201     Camera_ErrorCode SessionSetMeteringPoint(double point_x, double point_y);
202     Camera_ErrorCode SessionGetExposureBiasRange(void);
203     Camera_ErrorCode SessionSetExposureBias(float exposureBias);
204     Camera_ErrorCode SessionGetExposureBias(void);
205     Camera_ErrorCode SessionGetPhotoRotation(int rotation);
206     Camera_ErrorCode SessionGetVideoRotation(int rotation);
207     Camera_ErrorCode SessionGetPreviewRotation(int rotation);
208     Camera_ErrorCode SessionSetPreviewRotation(int rotation, bool isDisplayLocked);
209 
210     Camera_ErrorCode SessionIsFocusModeSupported(uint32_t mode);
211     Camera_ErrorCode SessionGetFocusMode(void);
212     Camera_ErrorCode SessionSetFocusMode(uint32_t mode);
213     Camera_ErrorCode SessionSetFocusPoint(double point_x, double point_y);
214     Camera_ErrorCode SessionGetFocusPoint(void);
215 
216     Camera_ErrorCode SessionGetZoomRatioRange(void);
217     Camera_ErrorCode SessionGetZoomRatio(void);
218     Camera_ErrorCode SessionSetZoomRatio(float zoom);
219 
220     Camera_ErrorCode SessionIsVideoStabilizationModeSupported(uint32_t mode);
221     Camera_ErrorCode SessionGetVideoStabilizationMode(void);
222     Camera_ErrorCode SessionSetVideoStabilizationMode(uint32_t mode);
223 
224     // RegisterCallback
225     Camera_ErrorCode CameraManagerRegisterCallback(void);
226     Camera_ErrorCode CameraInputRegisterCallback(void);
227     Camera_ErrorCode PreviewOutputRegisterCallback(void);
228     Camera_ErrorCode PhotoOutputRegisterCallback(void);
229     Camera_ErrorCode VideoOutputRegisterCallback(void);
230     Camera_ErrorCode MetadataOutputRegisterCallback(void);
231     Camera_ErrorCode CaptureSessionRegisterCallback(void);
232     Camera_ErrorCode CaptureSessionRegisterCallback(int useCaseCode);
233 
234     // UnRegisterCallback
235     Camera_ErrorCode CameraManagerUnRegisterCallback(void);
236     Camera_ErrorCode CameraInputUnRegisterCallback(void);
237     Camera_ErrorCode PreviewOutputUnRegisterCallback(void);
238     Camera_ErrorCode PhotoOutputUnRegisterCallback(void);
239     Camera_ErrorCode VideoOutputUnRegisterCallback(void);
240     Camera_ErrorCode MetadataOutputUnRegisterCallback(void);
241     Camera_ErrorCode CaptureSessionUnRegisterCallback(void);
242     Camera_ErrorCode CaptureSessionUnRegisterCallback(int useCaseCode);
243 
244     // Get callback
245     CameraManager_Callbacks* GetCameraManagerListener(void);
246     CameraInput_Callbacks* GetCameraInputListener(void);
247     PreviewOutput_Callbacks* GetPreviewOutputListener(void);
248     PhotoOutput_Callbacks* GetPhotoOutputListener(void);
249     VideoOutput_Callbacks* GetVideoOutputListener(void);
250     MetadataOutput_Callbacks* GetMetadataOutputListener(void);
251     CaptureSession_Callbacks* GetCaptureSessionRegister(void);
252 
253     // RegisterCallback_On
254     Camera_ErrorCode CaptureSessionRegisterCallbackOn(int useCaseCode);
255 
256     // UnregisterCallback_Off
257     Camera_ErrorCode CaptureSessionUnregisterCallbackOff(int useCaseCode);
258 
259     // Get callback
260     CaptureSession_Callbacks* GetCaptureSessionRegister(int useCaseCode);
261 
262     Camera_ErrorCode GetSupportedSceneModes(int useCaseCode);
263     Camera_ErrorCode DeleteSceneModes(int useCaseCode);
264     Camera_ErrorCode GetSupportedCameraOutputCapabilityWithSceneMode(int useCaseCode);
265     Camera_ErrorCode SetSessionMode(int useCaseCode);
266     Camera_ErrorCode CanAddInput(int useCaseCode);
267     Camera_ErrorCode CanAddPreviewOutput(int useCaseCode);
268     Camera_ErrorCode CanAddPhotoOutput(int useCaseCode);
269     Camera_ErrorCode CanAddVideoOutput(int useCaseCode);
270     Camera_ErrorCode AddSecureOutput(int useCaseCode);
271     Camera_ErrorCode OpenSecureCamera(int useCaseCode);
272 
273     Camera_ErrorCode CreatePreviewOutputUsedInPreconfig(int useCaseCode);
274     Camera_ErrorCode CreatePhotoOutputUsedInPreconfig(char *photoSurfaceId, int useCaseCode);
275     Camera_ErrorCode CreateVideoOutputUsedInPreconfig(char *videoId, int useCaseCode);
276     Camera_ErrorCode VideoOutputGetActiveProfile(int useCaseCode);
277     Camera_ErrorCode VideoOutputDeleteProfile(int useCaseCode);
278     Camera_ErrorCode SessionCanPreconfig(uint32_t mode, int useCaseCode);
279     Camera_ErrorCode SessionCanPreconfigWithRatio(uint32_t mode, uint32_t mode2, int useCaseCode);
280     Camera_ErrorCode SessionPreconfig(uint32_t mode, int useCaseCode);
281     Camera_ErrorCode SessionPreconfigWithRatio(uint32_t mode, uint32_t mode2, int useCaseCode);
282     Camera_ErrorCode PreviewOutputGetActiveProfile(int useCaseCode);
283     Camera_ErrorCode PreviewOutputDeleteProfile(int useCaseCode);
284     Camera_ErrorCode PhotoOutputGetActiveProfile(int useCaseCode);
285     Camera_ErrorCode PhotoOutputDeleteProfile(int useCaseCode);
286 
287     Camera_ErrorCode RegisterPhotoAvailableCallback(int useCaseCode);
288     Camera_ErrorCode UnregisterPhotoAvailableCallback(int useCaseCode);
289     Camera_ErrorCode RegisterPhotoAssetAvailableCallback(int useCaseCode);
290     Camera_ErrorCode UnregisterPhotoAssetAvailableCallback(int useCaseCode);
291     Camera_ErrorCode IsMovingPhotoSupported(int useCaseCode);
292     Camera_ErrorCode EnableMovingPhoto(int useCaseCode, bool enable);
293     Camera_ErrorCode GetMainImage(int useCaseCode);
294     Camera_ErrorCode PhotoNativeRelease(int useCaseCode);
295     Camera_ErrorCode CreatePhotoOutputWithoutSurface(int useCaseCode);
296 
297     Camera_ErrorCode TorchMode(int useCaseCode);
298     Camera_ErrorCode IsTorchSupported(int useCaseCode);
299     Camera_ErrorCode IsTorchSupportedByTorchMode(int useCaseCode);
300     Camera_ErrorCode SetTorchMode(int useCaseCode);
301     Camera_ErrorCode GetExposureValue(int useCaseCode);
302     Camera_ErrorCode GetFocalLength(int useCaseCode);
303     Camera_ErrorCode SetSmoothZoom(int useCaseCode);
304     Camera_ErrorCode GetSupportedColorSpaces(int useCaseCode);
305     Camera_ErrorCode DeleteColorSpaces(int useCaseCode);
306     Camera_ErrorCode GetActiveColorSpace(int useCaseCode);
307     Camera_ErrorCode SetColorSpace(int useCaseCode);
308     Camera_ErrorCode GetSupportedFrameRates(int useCaseCode);
309     Camera_ErrorCode DeleteFrameRates(int useCaseCode);
310     Camera_ErrorCode SetFrameRate(int useCaseCode);
311     Camera_ErrorCode GetActiveFrameRate(int useCaseCode);
312     Camera_ErrorCode VideoOutputGetSupportedFrameRates(int useCaseCode);
313     Camera_ErrorCode VideoOutputSetFrameRate(int useCaseCode);
314     Camera_ErrorCode VideoOutputGetActiveFrameRate(int useCaseCode);
315     Camera_ErrorCode VideoOutputDeleteFrameRates(int useCaseCode);
316     Camera_ErrorCode CameraManagerRegisterTorchStatusCallback(int useCaseCode);
317     Camera_ErrorCode CameraManagerUnregisterTorchStatusCallback(int useCaseCode);
318     Camera_ErrorCode CaptureSessionRegisterSmoothZoomInfoCallback(int useCaseCode);
319     Camera_ErrorCode CaptureSessionUnregisterSmoothZoomInfoCallback(int useCaseCode);
320     Camera_ErrorCode PhotoOutputRegisterCaptureStartWithInfoCallback(int useCaseCode);
321     Camera_ErrorCode PhotoOutputUnregisterCaptureStartWithInfoCallback(int useCaseCode);
322     Camera_ErrorCode PhotoOutputRegisterCaptureEndCallback(int useCaseCode);
323     Camera_ErrorCode PhotoOutputUnregisterCaptureEndCallback(int useCaseCode);
324     Camera_ErrorCode PhotoOutputRegisterFrameShutterEndCallback(int useCaseCode);
325     Camera_ErrorCode PhotoOutputUnregisterFrameShutterEndCallback(int useCaseCode);
326     Camera_ErrorCode PhotoOutputRegisterCaptureReadyCallback(int useCaseCode);
327     Camera_ErrorCode PhotoOutputUnregisterCaptureReadyCallback(int useCaseCode);
328     Camera_ErrorCode PhotoOutputRegisterEstimatedCaptureDurationCallback(int useCaseCode);
329     Camera_ErrorCode PhotoOutputUnregisterEstimatedCaptureDurationCallback(int useCaseCode);
330 
331     Camera_ErrorCode IsAutoDeviceSwitchSupported(bool* isSupported);
332     Camera_ErrorCode EnableAutoDeviceSwitch(bool isEnable);
333     Camera_ErrorCode RegisterAutoDeviceSwitchStatusCallback(int useCaseCode);
334     Camera_ErrorCode UnegisterAutoDeviceSwitchStatusCallback(int useCaseCode);
335     Camera_ErrorCode CameraManagerRegisterFoldStatusCallback(int useCaseCode);
336     Camera_ErrorCode CameraManagerUnregisterFoldStatusCallback(int useCaseCode);
337     int32_t ColorSpace(void);
338 
339     // test aid
340     Camera_ErrorCode SetSceneMode(int useCaseCode);
341     Camera_ErrorCode GetCameraFromCameras(Camera_Device* cameras, Camera_Device** camera,
342         size_t cameraIndex = BACK_CAMERA);
343     Camera_ErrorCode ReleaseCamera(void);
344     Camera_ErrorCode ReadyCreatePhotoOutputWithoutSurface();
345 
346 private:
347     NDKCamera(const NDKCamera&) = delete;
348     NDKCamera& operator = (const NDKCamera&) = delete;
349 
350     Camera_Manager* cameraManager_;
351     Camera_CaptureSession* captureSession_;
352     uint32_t size_;
353     const Camera_Profile* profile_;
354     const Camera_Profile* photoProfile_;
355     const Camera_VideoProfile* videoProfile_;
356     Camera_PreviewOutput* previewOutput_;
357     Camera_PhotoOutput* photoOutput_;
358     Camera_VideoOutput* videoOutput_;
359     const Camera_MetadataObjectType* metaDataObjectType_;
360     Camera_MetadataOutput* metadataOutput_;
361     Camera_Input* cameraInput_;
362     bool* isCameraMuted_;
363     char* previewSurfaceId_;
364     Camera_Profile *cameraProfile_;
365     Camera_VideoProfile *videoActiveProfile_;
366     Camera_ErrorCode ret_;
367     float step_;
368 
369     static NDKCamera* ndkCamera_;
370     static std::mutex mtx_;
371     volatile bool valid_;
372     uint32_t orientation_;
373     uint32_t cameraDeviceIndex_;
374 };
375 #endif  // CAMERA_NATIVE_CAMERA_H