1 /*
2  * Copyright (c) 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_MANAGER_ADAPTER_IMPL_H
17 #define CAMERA_MANAGER_ADAPTER_IMPL_H
18 
19 #include <cstdio>
20 #include <fcntl.h>
21 #include <securec.h>
22 #include <sys/time.h>
23 #include <unistd.h>
24 
25 #include "camera_manager_adapter.h"
26 
27 #if defined(NWEB_CAMERA_ENABLE)
28 #include "camera_manager.h"
29 #endif
30 
31 namespace OHOS::NWeb {
32 #if defined(NWEB_CAMERA_ENABLE)
33 using namespace OHOS::CameraStandard;
34 
35 enum class SurfaceType { INVALID = 0, PREVIEW, SECOND_PREVIEW, PHOTO, VIDEO };
36 
37 class CameraSurfaceListener;
38 
39 class CameraSurfaceBufferAdapterImpl : public CameraSurfaceBufferAdapter {
40 public:
41     explicit CameraSurfaceBufferAdapterImpl(sptr<SurfaceBuffer> buffer);
42 
43     ~CameraSurfaceBufferAdapterImpl() override = default;
44 
45     int32_t GetFileDescriptor() override;
46 
47     int32_t GetWidth() override;
48 
49     int32_t GetHeight() override;
50 
51     int32_t GetStride() override;
52 
53     int32_t GetFormat() override;
54 
55     uint32_t GetSize() override;
56 
57     uint8_t* GetBufferAddr() override;
58 
59     sptr<SurfaceBuffer>& GetBuffer();
60 
61 private:
62     sptr<SurfaceBuffer> buffer_ = nullptr;
63 };
64 
65 class CameraManagerAdapterCallback : public CameraManagerCallback {
66 public:
67     explicit CameraManagerAdapterCallback(std::shared_ptr<CameraStatusCallbackAdapter> callback);
68     ~CameraManagerAdapterCallback() = default;
69     void OnCameraStatusChanged(const CameraStatusInfo& cameraStatusInfo) const override;
70     void OnFlashlightStatusChanged(const std::string& cameraID, const FlashStatus flashStatus) const override;
71 
72 private:
73     CameraStatusAdapter GetAdapterCameraStatus(CameraStatus status) const;
74     std::shared_ptr<CameraStatusCallbackAdapter> statusCallback_;
75 };
76 #endif
77 
78 class CameraManagerAdapterImpl : public CameraManagerAdapter {
79 public:
80     static CameraManagerAdapterImpl& GetInstance();
81     CameraManagerAdapterImpl() = default;
82 
83     ~CameraManagerAdapterImpl() override = default;
84 
85     int32_t Create(std::shared_ptr<CameraStatusCallbackAdapter> cameraStatusCallback) override;
86 
87     std::vector<std::shared_ptr<VideoDeviceDescriptorAdapter>> GetDevicesInfo() override;
88 
89     int32_t ReleaseCameraManger() override;
90 
91     int32_t GetExposureModes(std::vector<ExposureModeAdapter>& exposureModesAdapter) override;
92 
93     int32_t GetCurrentExposureMode(ExposureModeAdapter& exposureModeAdapter) override;
94 
95     std::shared_ptr<VideoCaptureRangeAdapter> GetCaptionRangeById(RangeIDAdapter rangeId) override;
96 
97     bool IsFocusModeSupported(FocusModeAdapter focusMode) override;
98 
99     FocusModeAdapter GetCurrentFocusMode() override;
100 
101     bool IsFlashModeSupported(FlashModeAdapter flashMode) override;
102 
103     int32_t RestartSession() override;
104 
105     int32_t StopSession(CameraStopType stopType) override;
106 
107     CameraStatusAdapter GetCameraStatus() override;
108 
109     bool IsExistCaptureTask() override;
110 
111     int32_t StartStream(const std::string& deviceId, const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,
112         std::shared_ptr<CameraBufferListenerAdapter> listener) override;
113 
114     void SetForegroundFlag(bool isForeground) override;
115 
116     void SetCameraStatus(CameraStatusAdapter status) override;
117 
118     std::string GetCurrentDeviceId() override;
119 
120 #if defined(NWEB_CAMERA_ENABLE)
121 private:
122     VideoTransportType GetCameraTransportType(ConnectionType connectType);
123     VideoFacingModeAdapter GetCameraFacingMode(CameraPosition position);
124     std::vector<std::shared_ptr<FormatAdapter>> GetCameraSupportFormats(sptr<CameraOutputCapability> outputcapability);
125     VideoPixelFormatAdapter TransToAdapterCameraFormat(CameraFormat format);
126     ExposureModeAdapter GetAdapterExposureMode(ExposureMode exportMode);
127     CameraFormat TransToOriCameraFormat(VideoPixelFormatAdapter format);
128     int32_t TransToAdapterExposureModes(
129         std::vector<ExposureMode>& exposureModes, std::vector<ExposureModeAdapter>& exposureModesAdapter);
130     std::shared_ptr<VideoCaptureRangeAdapter> GetExposureCompensation();
131     FocusMode GetOriFocusMode(FocusModeAdapter focusMode);
132     FocusModeAdapter GetAdapterFocusMode(FocusMode focusMode);
133     FlashMode GetOriFlashMode(FlashModeAdapter flashMode);
134     int32_t ReleaseSession();
135     int32_t ReleaseSessionResource(const std::string& deviceId);
136     int32_t InitCameraInput(const std::string& deviceId);
137     int32_t InitPreviewOutput(const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,
138         std::shared_ptr<CameraBufferListenerAdapter> listener);
139     int32_t CreateAndStartSession();
140     int32_t ErrorTypeToString(CameraErrorType errorType, std::string& errnoTypeString);
141     void ReportErrorSysEvent(CameraErrorType errorType);
142     int32_t StartStreamInner(const std::string& deviceId,
143         const std::shared_ptr<VideoCaptureParamsAdapter> captureParams,
144         std::shared_ptr<CameraBufferListenerAdapter> listener);
145     sptr<CameraManager> cameraManager_;
146     sptr<CaptureSession> captureSession_;
147     sptr<CaptureInput> cameraInput_;
148     sptr<IConsumerSurface> previewSurface_;
149     sptr<CameraSurfaceListener> previewSurfaceListener_;
150     sptr<CaptureOutput> previewOutput_;
151     std::string deviceId_;
152     std::shared_ptr<VideoCaptureParamsAdapter> captureParams_;
153     std::shared_ptr<CameraBufferListenerAdapter> listener_;
154     const int32_t DEFAULT_FRAME_RATE = 30;
155     const uint32_t RANGE_MAX_SIZE = 2;
156     const uint32_t RANGE_MIN_INDEX = 0;
157     const uint32_t RANGE_MAX_INDEX = 1;
158     CameraStatusAdapter status_ = CameraStatusAdapter::AVAILABLE;
159     std::mutex mutex_;
160     bool inputInitedFlag_ = false;
161     bool isCapturing_ = false;
162     bool isForegound_ = false;
163     std::shared_ptr<CameraManagerAdapterCallback> cameraMngrCallback_;
164     std::string wantedDeviceId_;
165 #endif
166 };
167 
168 #if defined(NWEB_CAMERA_ENABLE)
169 class CameraSurfaceListener : public IBufferConsumerListener {
170 public:
171     CameraSurfaceListener(
172         SurfaceType surfaceType, sptr<IConsumerSurface> surface, std::shared_ptr<CameraBufferListenerAdapter> listener);
173     virtual ~CameraSurfaceListener() = default;
174     void OnBufferAvailable() override;
175 
176 private:
177     std::shared_ptr<CameraRotationInfoAdapter> GetRotationInfo(GraphicTransformType transform);
178     std::shared_ptr<CameraRotationInfoAdapter> FillRotationInfo(int32_t rotation, bool isFlipX, bool isFlipY);
179     int32_t GetScreenRotation();
180     int32_t GetPictureRotation();
181     bool IsNeedCorrectRotation();
182     SurfaceType surfaceType_;
183     sptr<IConsumerSurface> surface_;
184     std::shared_ptr<CameraBufferListenerAdapter> listener_ = nullptr;
185     const int32_t ROTATION_0 = 0;
186     const int32_t ROTATION_90 = 90;
187     const int32_t ROTATION_180 = 180;
188     const int32_t ROTATION_270 = 270;
189     const int32_t ROTATION_MAX = 360;
190 };
191 
192 class CameraSurfaceAdapterImpl : public CameraSurfaceAdapter {
193 public:
194     CameraSurfaceAdapterImpl();
195 
196     CameraSurfaceAdapterImpl(sptr<IConsumerSurface> surface);
197 
198     ~CameraSurfaceAdapterImpl() = default;
199 
200     int32_t ReleaseBuffer(std::shared_ptr<CameraSurfaceBufferAdapter> bufferAdapter, int32_t fence) override;
201 
202 private:
203     sptr<IConsumerSurface> cSurface_ = nullptr;
204 };
205 #endif
206 } // namespace OHOS::NWeb
207 #endif // CAMERA_MANAGER_ADAPTER_IMPL_H
208