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