• 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/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 "ohcamera/camera.h"
30 #include "ohcamera/camera_manager.h"
31 #include "ohcamera/camera_input.h"
32 #include "ohcamera/capture_session.h"
33 #include "ohcamera/preview_output.h"
34 #include "ohcamera/photo_output.h"
35 #include "ohcamera/video_output.h"
36 #include <map>
37 #include <string>
38 #include <vector>
39 #include <ohcamera/camera_device.h>
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 IsVideoMirror_;
91     bool HasFlash_;
92     bool IsFlashMode_;
93     bool IsExposureMode_;
94     float minExposureBias_;
95     float maxExposureBias_;
96     float exposureBias_;
97     bool isFocusSupported_;
98     bool isContolCenterSupported_;
99     float minZoom_;
100     float maxZoom_;
101     float zoom_;
102     bool isVideoSupported_;
103     Camera_FlashMode flashMode_;
104     Camera_ExposureMode exposureMode_;
105     Camera_Point point_;
106     Camera_FocusMode focusMode_;
107     Camera_Point focusPoint_;
108     Camera_VideoStabilizationMode videoMode_;
109     Camera_Device* cameras_;
110     Camera_OutputCapability* cameraOutputCapability_;
111     bool isAddInput_;
112     Camera_Device *camera_;
113     Camera_SceneMode *sceneModes_;
114     Camera_SceneMode sceneMode_;
115     uint64_t secureSeqId_;
116     bool canPreconfig_;
117     uint32_t sceneModesSize_;
118     bool isNormalPhoto_;
119     bool isNormalVideo_;
120     bool isSecurePhoto_;
121     bool isMovingPhotoSupported_;
122     OH_PhotoNative *photoNative_;
123     OH_ImageNative *mainImage_;
124     bool isTorchSupported_;
125     bool isTorchSupportedByTorchMode_;
126     Camera_TorchMode torchMode_;
127     uint32_t frameRatesSize_;
128     uint32_t videoFrameRatesSize_;
129     uint32_t colorSpacesSize_;
130     OH_NativeBuffer_ColorSpace* colorSpace_;
131     OH_NativeBuffer_ColorSpace activeColorSpace_;
132     OH_NativeBuffer_ColorSpace setcolorSpace_;
133     Camera_FrameRateRange* frameRateRange_;
134     Camera_FrameRateRange activeFrameRateRange_;
135     Camera_FrameRateRange* videoFrameRateRange_;
136     Camera_FrameRateRange videoActiveFrameRateRange_;
137     Camera_ImageRotation imageRotation_;
138     Camera_ImageRotation previewRotation_;
139     uint32_t effectTypeSize_;
140     Camera_ControlCenterEffectType* controlCenterEffectType_;
141     bool isCreatePhotoOutputWithoutSurface_;
142     bool isMacroSupported_;
143     bool isEnableMacro_;
144 
145     //callback
146     static CameraCallbackCode cameraCallbackCode_;
147 
148     Camera_ErrorCode CreateCameraInput(void);
149     Camera_ErrorCode CreateCameraInputWithPositionAndType(Camera_Position position, Camera_Type type);
150     Camera_ErrorCode CreateSession(void);
151     Camera_ErrorCode CameraInputOpen(void);
152     Camera_ErrorCode CameraInputClose(void);
153     Camera_ErrorCode CameraInputRelease(void);
154     Camera_ErrorCode GetSupportedCameras(void);
155     Camera_ErrorCode GetCameraOrientation(void);
156     Camera_ErrorCode GetCameraHostName(void);
157     Camera_ErrorCode GetCameraHostNameErrorParameter(void);
158     Camera_ErrorCode GetCameraHostTypeErrorParameter(void);
159     Camera_ErrorCode GetCameraHostNameInspection(void);
160     Camera_ErrorCode GetCameraHostTypeInspection(void);
161     Camera_ErrorCode GetCameraHostType(void);
162     Camera_ErrorCode GetSupportedOutputCapability(void);
163     Camera_ErrorCode CreatePreviewOutput(void);
164     Camera_ErrorCode CreatePhotoOutput(char* photoSurfaceId);
165     Camera_ErrorCode CreateVideoOutput(char* videoId);
166     Camera_ErrorCode CreateMetadataOutput(void);
167     Camera_ErrorCode IsCameraMuted(void);
168     Camera_ErrorCode PreviewOutputStop(void);
169     Camera_ErrorCode PreviewOutputRelease(void);
170     Camera_ErrorCode PhotoOutputRelease(void);
171     Camera_ErrorCode HasFlashFn(uint32_t mode);
172     Camera_ErrorCode setZoomRatioFn(uint32_t zoomRatio);
173 
174     Camera_ErrorCode SessionBegin(void);
175     Camera_ErrorCode SessionCommitConfig(void);
176     Camera_ErrorCode SessionStart(void);
177     Camera_ErrorCode SessionStop(void);
178     Camera_ErrorCode StartVideo(char* videoId);
179     Camera_ErrorCode AddVideoOutput(void);
180     Camera_ErrorCode VideoOutputStart(void);
181 
182     Camera_ErrorCode PreviewOutputStart(void);
183     Camera_ErrorCode PhotoOutputCapture(void);
184     Camera_ErrorCode TakePictureWithPhotoSettings(Camera_PhotoCaptureSetting photoSetting);
185     Camera_ErrorCode IsMirrorSupported(void);
186     Camera_ErrorCode VideoOutputStop(void);
187     Camera_ErrorCode VideoOutputRelease(void);
188     Camera_ErrorCode IsVideoMirrorSupported(void);
189     Camera_ErrorCode EnableVideoMirror(bool isEnable);
190 
191     Camera_ErrorCode MetadataOutputStart(void);
192     Camera_ErrorCode MetadataOutputStop(void);
193     Camera_ErrorCode MetadataOutputRelease(void);
194 
195     Camera_ErrorCode SessionAddInput(void);
196     Camera_ErrorCode SessionRemoveInput(void);
197     Camera_ErrorCode SessionAddPreviewOutput(void);
198     Camera_ErrorCode SessionAddPhotoOutput(void);
199     Camera_ErrorCode SessionAddVideoOutput(void);
200     Camera_ErrorCode SessionAddMetadataOutput(void);
201     Camera_ErrorCode SessionRemovePreviewOutput(void);
202     Camera_ErrorCode SessionRemovePhotoOutput(void);
203     Camera_ErrorCode SessionRemoveVideoOutput(void);
204     Camera_ErrorCode SessionRemoveMetadataOutput(void);
205     Camera_ErrorCode SessionRelease(void);
206 
207     Camera_ErrorCode SessionHasFlash(void);
208     Camera_ErrorCode SessionIsFlashModeSupported(uint32_t mode);
209     Camera_ErrorCode SessionGetFlashMode(void);
210     Camera_ErrorCode SessionSetFlashMode(uint32_t mode);
211     Camera_ErrorCode GetCameraDevice(int useCaseCode);     // 基于位置和类型获取相机实例。
212     Camera_ErrorCode GetCameraConcurrentInfos(int useCaseCode);   // 获取相机并发信息。
213 
214     Camera_ErrorCode SessionIsExposureModeSupported(uint32_t mode);
215     Camera_ErrorCode SessionGetExposureMode(void);
216     Camera_ErrorCode SessionSetExposureMode(uint32_t mode);
217     Camera_ErrorCode SessionGetMeteringPoint(void);
218     Camera_ErrorCode SessionSetMeteringPoint(double point_x, double point_y);
219     Camera_ErrorCode SessionGetExposureBiasRange(void);
220     Camera_ErrorCode SessionSetExposureBias(float exposureBias);
221     Camera_ErrorCode SessionGetExposureBias(void);
222     Camera_ErrorCode SessionGetPhotoRotation(int rotation);
223     Camera_ErrorCode SessionGetVideoRotation(int rotation);
224     Camera_ErrorCode SessionGetPreviewRotation(int rotation);
225     Camera_ErrorCode SessionSetPreviewRotation(int rotation, bool isDisplayLocked);
226 
227     Camera_ErrorCode SessionIsFocusModeSupported(uint32_t mode);
228     Camera_ErrorCode SessionGetFocusMode(void);
229     Camera_ErrorCode SessionSetFocusMode(uint32_t mode);
230     Camera_ErrorCode SessionSetFocusPoint(double point_x, double point_y);
231     Camera_ErrorCode SessionGetFocusPoint(void);
232 
233     Camera_ErrorCode SessionIsControlCenterSupported(void);
234     Camera_ErrorCode SessionGetSupportedEffectTypes(int useCaseCode);
235     Camera_ErrorCode SessionDeleteSupportedEffectTypes(int useCaseCode);
236     Camera_ErrorCode SessionEnableControlCenter(bool enabled);
237     Camera_ErrorCode RegisterControlCenterEffectStatusChange(int useCaseCode);
238     Camera_ErrorCode UnregisterControlCenterEffectStatusChange(int useCaseCode);
239 
240     Camera_ErrorCode SessionGetZoomRatioRange(void);
241     Camera_ErrorCode SessionGetZoomRatio(void);
242     Camera_ErrorCode SessionSetZoomRatio(float zoom);
243 
244     Camera_ErrorCode SessionIsVideoStabilizationModeSupported(uint32_t mode);
245     Camera_ErrorCode SessionGetVideoStabilizationMode(void);
246     Camera_ErrorCode SessionSetVideoStabilizationMode(uint32_t mode);
247     Camera_ErrorCode SessionSetQualityPrioritization(uint32_t quality);
248 
249     // RegisterCallback
250     Camera_ErrorCode CameraManagerRegisterCallback(void);
251     Camera_ErrorCode CameraInputRegisterCallback(void);
252     Camera_ErrorCode PreviewOutputRegisterCallback(void);
253     Camera_ErrorCode PhotoOutputRegisterCallback(void);
254     Camera_ErrorCode VideoOutputRegisterCallback(void);
255     Camera_ErrorCode MetadataOutputRegisterCallback(void);
256     Camera_ErrorCode CaptureSessionRegisterCallback(void);
257     Camera_ErrorCode CaptureSessionRegisterCallback(int useCaseCode);
258 
259     // UnRegisterCallback
260     Camera_ErrorCode CameraManagerUnRegisterCallback(void);
261     Camera_ErrorCode CameraInputUnRegisterCallback(void);
262     Camera_ErrorCode PreviewOutputUnRegisterCallback(void);
263     Camera_ErrorCode PhotoOutputUnRegisterCallback(void);
264     Camera_ErrorCode VideoOutputUnRegisterCallback(void);
265     Camera_ErrorCode MetadataOutputUnRegisterCallback(void);
266     Camera_ErrorCode CaptureSessionUnRegisterCallback(void);
267     Camera_ErrorCode CaptureSessionUnRegisterCallback(int useCaseCode);
268 
269     // Get callback
270     CameraManager_Callbacks* GetCameraManagerListener(void);
271     CameraInput_Callbacks* GetCameraInputListener(void);
272     PreviewOutput_Callbacks* GetPreviewOutputListener(void);
273     PhotoOutput_Callbacks* GetPhotoOutputListener(void);
274     VideoOutput_Callbacks* GetVideoOutputListener(void);
275     MetadataOutput_Callbacks* GetMetadataOutputListener(void);
276     CaptureSession_Callbacks* GetCaptureSessionRegister(void);
277 
278     // RegisterCallback_On
279     Camera_ErrorCode CaptureSessionRegisterCallbackOn(int useCaseCode);
280 
281     // UnregisterCallback_Off
282     Camera_ErrorCode CaptureSessionUnregisterCallbackOff(int useCaseCode);
283 
284     // Get callback
285     CaptureSession_Callbacks* GetCaptureSessionRegister(int useCaseCode);
286 
287     Camera_ErrorCode GetSupportedSceneModes(int useCaseCode);
288     Camera_ErrorCode DeleteSceneModes(int useCaseCode);
289     Camera_ErrorCode GetSupportedCameraOutputCapabilityWithSceneMode(int useCaseCode);
290     Camera_ErrorCode SetSessionMode(int useCaseCode);
291     Camera_ErrorCode CanAddInput(int useCaseCode);
292     Camera_ErrorCode CanAddPreviewOutput(int useCaseCode);
293     Camera_ErrorCode CanAddPhotoOutput(int useCaseCode);
294     Camera_ErrorCode CanAddVideoOutput(int useCaseCode);
295     Camera_ErrorCode AddSecureOutput(int useCaseCode);
296     Camera_ErrorCode OpenSecureCamera(int useCaseCode);
297 
298     Camera_ErrorCode CreatePreviewOutputUsedInPreconfig(int useCaseCode);
299     Camera_ErrorCode CreatePhotoOutputUsedInPreconfig(char *photoSurfaceId, int useCaseCode);
300     Camera_ErrorCode CreateVideoOutputUsedInPreconfig(char *videoId, int useCaseCode);
301     Camera_ErrorCode VideoOutputGetActiveProfile(int useCaseCode);
302     Camera_ErrorCode VideoOutputDeleteProfile(int useCaseCode);
303     Camera_ErrorCode SessionCanPreconfig(uint32_t mode, int useCaseCode);
304     Camera_ErrorCode SessionCanPreconfigWithRatio(uint32_t mode, uint32_t mode2, int useCaseCode);
305     Camera_ErrorCode SessionPreconfig(uint32_t mode, int useCaseCode);
306     Camera_ErrorCode SessionPreconfigWithRatio(uint32_t mode, uint32_t mode2, int useCaseCode);
307     Camera_ErrorCode PreviewOutputGetActiveProfile(int useCaseCode);
308     Camera_ErrorCode PreviewOutputDeleteProfile(int useCaseCode);
309     Camera_ErrorCode PhotoOutputGetActiveProfile(int useCaseCode);
310     Camera_ErrorCode PhotoOutputDeleteProfile(int useCaseCode);
311 
312     Camera_ErrorCode RegisterPhotoAvailableCallback(int useCaseCode);
313     Camera_ErrorCode UnregisterPhotoAvailableCallback(int useCaseCode);
314     Camera_ErrorCode RegisterPhotoAssetAvailableCallback(int useCaseCode);
315     Camera_ErrorCode UnregisterPhotoAssetAvailableCallback(int useCaseCode);
316     Camera_ErrorCode IsMovingPhotoSupported(int useCaseCode);
317     Camera_ErrorCode EnableMovingPhoto(int useCaseCode, bool enable);
318     Camera_ErrorCode GetMainImage(int useCaseCode);
319     Camera_ErrorCode PhotoNativeRelease(int useCaseCode);
320     Camera_ErrorCode CreatePhotoOutputWithoutSurface(int useCaseCode);
321 
322     Camera_ErrorCode TorchMode(int useCaseCode);
323     Camera_ErrorCode IsTorchSupported(int useCaseCode);
324     Camera_ErrorCode IsTorchSupportedByTorchMode(int useCaseCode);
325     Camera_ErrorCode SetTorchMode(int useCaseCode);
326     Camera_ErrorCode GetExposureValue(int useCaseCode);
327     Camera_ErrorCode GetFocalLength(int useCaseCode);
328     Camera_ErrorCode SetSmoothZoom(int useCaseCode);
329     Camera_ErrorCode GetSupportedColorSpaces(int useCaseCode);
330     Camera_ErrorCode DeleteColorSpaces(int useCaseCode);
331     Camera_ErrorCode GetActiveColorSpace(int useCaseCode);
332     Camera_ErrorCode SetColorSpace(int useCaseCode);
333     Camera_ErrorCode GetSupportedFrameRates(int useCaseCode);
334     Camera_ErrorCode DeleteFrameRates(int useCaseCode);
335     Camera_ErrorCode SetFrameRate(int useCaseCode);
336     Camera_ErrorCode GetActiveFrameRate(int useCaseCode);
337     Camera_ErrorCode VideoOutputGetSupportedFrameRates(int useCaseCode);
338     Camera_ErrorCode VideoOutputSetFrameRate(int useCaseCode);
339     Camera_ErrorCode VideoOutputGetActiveFrameRate(int useCaseCode);
340     Camera_ErrorCode VideoOutputDeleteFrameRates(int useCaseCode);
341     Camera_ErrorCode CameraManagerRegisterTorchStatusCallback(int useCaseCode);
342     Camera_ErrorCode CameraManagerUnregisterTorchStatusCallback(int useCaseCode);
343     Camera_ErrorCode CaptureSessionRegisterSmoothZoomInfoCallback(int useCaseCode);
344     Camera_ErrorCode CaptureSessionUnregisterSmoothZoomInfoCallback(int useCaseCode);
345     Camera_ErrorCode PhotoOutputRegisterCaptureStartWithInfoCallback(int useCaseCode);
346     Camera_ErrorCode PhotoOutputUnregisterCaptureStartWithInfoCallback(int useCaseCode);
347     Camera_ErrorCode PhotoOutputRegisterCaptureEndCallback(int useCaseCode);
348     Camera_ErrorCode PhotoOutputUnregisterCaptureEndCallback(int useCaseCode);
349     Camera_ErrorCode PhotoOutputRegisterFrameShutterEndCallback(int useCaseCode);
350     Camera_ErrorCode PhotoOutputUnregisterFrameShutterEndCallback(int useCaseCode);
351     Camera_ErrorCode PhotoOutputRegisterCaptureReadyCallback(int useCaseCode);
352     Camera_ErrorCode PhotoOutputUnregisterCaptureReadyCallback(int useCaseCode);
353     Camera_ErrorCode PhotoOutputRegisterEstimatedCaptureDurationCallback(int useCaseCode);
354     Camera_ErrorCode PhotoOutputUnregisterEstimatedCaptureDurationCallback(int useCaseCode);
355 
356     Camera_ErrorCode IsAutoDeviceSwitchSupported(bool* isSupported);
357     Camera_ErrorCode EnableAutoDeviceSwitch(bool isEnable);
358     Camera_ErrorCode RegisterAutoDeviceSwitchStatusCallback(int useCaseCode);
359     Camera_ErrorCode UnegisterAutoDeviceSwitchStatusCallback(int useCaseCode);
360     Camera_ErrorCode RegisterSystemPressureLevel(int useCaseCode);
361     Camera_ErrorCode UnregisterSystemPressureLevel(int useCaseCode);
362     Camera_ErrorCode CameraManagerRegisterFoldStatusCallback(int useCaseCode);
363     Camera_ErrorCode CameraManagerUnregisterFoldStatusCallback(int useCaseCode);
364     int32_t ColorSpace(void);
365     Camera_ErrorCode SessionIsMacroSupported(int useCaseCode);
366     Camera_ErrorCode SessionEnableMacro(int useCaseCode, bool enable);
367     Camera_ErrorCode WhiteBalanceTest(void);
368     Camera_ErrorCode MacroStatusChangeTest(void);
369 
370     // test aid
371     Camera_ErrorCode SetSceneMode(int useCaseCode);
372     Camera_ErrorCode GetCameraFromCameras(Camera_Device* cameras, Camera_Device** camera,
373         size_t cameraIndex = BACK_CAMERA);
374     Camera_ErrorCode ReleaseCamera(void);
375     Camera_ErrorCode ReadyCreatePhotoOutputWithoutSurface();
GetCameraDeviceSize()376     inline uint32_t GetCameraDeviceSize()
377     {
378         return size_;
379     }
380 
381 private:
382     NDKCamera(const NDKCamera&) = delete;
383     NDKCamera& operator = (const NDKCamera&) = delete;
384 
385     Camera_Manager* cameraManager_;
386     Camera_CaptureSession* captureSession_;
387     uint32_t size_; // 记录支持的Camera_Device列表的大小。
388     const Camera_Profile* profile_;
389     const Camera_Profile* photoProfile_;
390     const Camera_VideoProfile* videoProfile_;
391     Camera_PreviewOutput* previewOutput_;
392     Camera_PhotoOutput* photoOutput_;
393     Camera_VideoOutput* videoOutput_;
394     const Camera_MetadataObjectType* metaDataObjectType_;
395     Camera_MetadataOutput* metadataOutput_;
396     Camera_Input* cameraInput_;
397     bool* isCameraMuted_;
398     char* previewSurfaceId_;
399     Camera_Profile *cameraProfile_;
400     Camera_VideoProfile *videoActiveProfile_;
401     Camera_ErrorCode ret_;
402     float step_;
403 
404     static NDKCamera* ndkCamera_;
405     static std::mutex mtx_;
406     volatile bool valid_;
407     uint32_t orientation_;
408     char* hostName_;
409     Camera_HostDeviceType hostType_;
410     uint32_t cameraDeviceIndex_;
411 };
412 #endif  // CAMERA_NATIVE_CAMERA_H