• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::string GetCameraDisplayName(const std::string& cameraId, const CameraPosition& position);
146     sptr<CameraManager> cameraManager_;
147     sptr<CaptureSession> captureSession_;
148     sptr<CaptureInput> cameraInput_;
149     sptr<IConsumerSurface> previewSurface_;
150     sptr<CameraSurfaceListener> previewSurfaceListener_;
151     sptr<CaptureOutput> previewOutput_;
152     std::string deviceId_;
153     std::shared_ptr<VideoCaptureParamsAdapter> captureParams_;
154     std::shared_ptr<CameraBufferListenerAdapter> listener_;
155     const int32_t DEFAULT_FRAME_RATE = 30;
156     const uint32_t RANGE_MAX_SIZE = 2;
157     const uint32_t RANGE_MIN_INDEX = 0;
158     const uint32_t RANGE_MAX_INDEX = 1;
159     CameraStatusAdapter status_ = CameraStatusAdapter::AVAILABLE;
160     std::mutex mutex_;
161     bool inputInitedFlag_ = false;
162     bool isCapturing_ = false;
163     bool isForegound_ = false;
164     std::shared_ptr<CameraManagerAdapterCallback> cameraMngrCallback_;
165     std::string wantedDeviceId_;
166 #endif
167 };
168 
169 #if defined(NWEB_CAMERA_ENABLE)
170 class CameraSurfaceListener : public IBufferConsumerListener {
171 public:
172     CameraSurfaceListener(
173         SurfaceType surfaceType, sptr<IConsumerSurface> surface, std::shared_ptr<CameraBufferListenerAdapter> listener);
174     virtual ~CameraSurfaceListener() = default;
175     void OnBufferAvailable() override;
176 
177 private:
178     std::shared_ptr<CameraRotationInfoAdapter> GetRotationInfo(GraphicTransformType transform);
179     std::shared_ptr<CameraRotationInfoAdapter> FillRotationInfo(int32_t rotation, bool isFlipX, bool isFlipY);
180     int32_t GetScreenRotation();
181     int32_t GetPictureRotation();
182     bool IsNeedCorrectRotation();
183     SurfaceType surfaceType_;
184     sptr<IConsumerSurface> surface_;
185     std::shared_ptr<CameraBufferListenerAdapter> listener_ = nullptr;
186     const int32_t ROTATION_0 = 0;
187     const int32_t ROTATION_90 = 90;
188     const int32_t ROTATION_180 = 180;
189     const int32_t ROTATION_270 = 270;
190     const int32_t ROTATION_MAX = 360;
191 };
192 
193 class CameraSurfaceAdapterImpl : public CameraSurfaceAdapter {
194 public:
195     CameraSurfaceAdapterImpl();
196 
197     CameraSurfaceAdapterImpl(sptr<IConsumerSurface> surface);
198 
199     ~CameraSurfaceAdapterImpl() = default;
200 
201     int32_t ReleaseBuffer(std::shared_ptr<CameraSurfaceBufferAdapter> bufferAdapter, int32_t fence) override;
202 
203 private:
204     sptr<IConsumerSurface> cSurface_ = nullptr;
205 };
206 #endif
207 } // namespace OHOS::NWeb
208 #endif // CAMERA_MANAGER_ADAPTER_IMPL_H
209