1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QV4L2CAMERA_H |
5 | #define QV4L2CAMERA_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <private/qplatformcamera_p.h> |
19 | #include <sys/time.h> |
20 | |
21 | QT_BEGIN_NAMESPACE |
22 | |
23 | class QV4L2FileDescriptor; |
24 | class QV4L2MemoryTransfer; |
25 | class QSocketNotifier; |
26 | |
27 | struct V4L2CameraInfo |
28 | { |
29 | bool formatInitialized = false; |
30 | |
31 | bool autoWhiteBalanceSupported = false; |
32 | bool colorTemperatureSupported = false; |
33 | bool autoExposureSupported = false; |
34 | bool manualExposureSupported = false; |
35 | bool flashSupported = false; |
36 | bool torchSupported = false; |
37 | qint32 minColorTemp = 5600; // Daylight... |
38 | qint32 maxColorTemp = 5600; |
39 | qint32 minExposure = 0; |
40 | qint32 maxExposure = 0; |
41 | qint32 minExposureAdjustment = 0; |
42 | qint32 maxExposureAdjustment = 0; |
43 | qint32 minFocus = 0; |
44 | qint32 maxFocus = 0; |
45 | qint32 rangedFocus = false; |
46 | |
47 | int minZoom = 0; |
48 | int maxZoom = 0; |
49 | }; |
50 | |
51 | QVideoFrameFormat::PixelFormat formatForV4L2Format(uint32_t v4l2Format); |
52 | uint32_t v4l2FormatForPixelFormat(QVideoFrameFormat::PixelFormat format); |
53 | |
54 | class QV4L2Camera : public QPlatformCamera |
55 | { |
56 | Q_OBJECT |
57 | |
58 | public: |
59 | explicit QV4L2Camera(QCamera *parent); |
60 | ~QV4L2Camera(); |
61 | |
62 | bool isActive() const override; |
63 | void setActive(bool active) override; |
64 | |
65 | void setCamera(const QCameraDevice &camera) override; |
66 | bool setCameraFormat(const QCameraFormat &format) override; |
67 | bool resolveCameraFormat(const QCameraFormat &format); |
68 | |
69 | bool isFocusModeSupported(QCamera::FocusMode mode) const override; |
70 | void setFocusMode(QCamera::FocusMode /*mode*/) override; |
71 | |
72 | // void setCustomFocusPoint(const QPointF &/*point*/) override; |
73 | void setFocusDistance(float) override; |
74 | void zoomTo(float /*newZoomFactor*/, float /*rate*/ = -1.) override; |
75 | |
76 | void setFlashMode(QCamera::FlashMode /*mode*/) override; |
77 | bool isFlashModeSupported(QCamera::FlashMode mode) const override; |
78 | bool isFlashReady() const override; |
79 | |
80 | void setTorchMode(QCamera::TorchMode /*mode*/) override; |
81 | bool isTorchModeSupported(QCamera::TorchMode mode) const override; |
82 | |
83 | void setExposureMode(QCamera::ExposureMode) override; |
84 | bool isExposureModeSupported(QCamera::ExposureMode mode) const override; |
85 | void setExposureCompensation(float) override; |
86 | int isoSensitivity() const override; |
87 | void setManualIsoSensitivity(int) override; |
88 | void setManualExposureTime(float) override; |
89 | float exposureTime() const override; |
90 | |
91 | bool isWhiteBalanceModeSupported(QCamera::WhiteBalanceMode mode) const override; |
92 | void setWhiteBalanceMode(QCamera::WhiteBalanceMode /*mode*/) override; |
93 | void setColorTemperature(int /*temperature*/) override; |
94 | |
95 | QVideoFrameFormat frameFormat() const override; |
96 | |
97 | private Q_SLOTS: |
98 | void readFrame(); |
99 | |
100 | private: |
101 | void setCameraBusy(); |
102 | void initV4L2Controls(); |
103 | void closeV4L2Fd(); |
104 | int setV4L2ColorTemperature(int temperature); |
105 | bool setV4L2Parameter(quint32 id, qint32 value); |
106 | int getV4L2Parameter(quint32 id) const; |
107 | |
108 | void setV4L2CameraFormat(); |
109 | void initV4L2MemoryTransfer(); |
110 | void startCapturing(); |
111 | void stopCapturing(); |
112 | |
113 | private: |
114 | bool m_active = false; |
115 | QCameraDevice m_cameraDevice; |
116 | |
117 | std::unique_ptr<QSocketNotifier> m_notifier; |
118 | std::unique_ptr<QV4L2MemoryTransfer> m_memoryTransfer; |
119 | std::shared_ptr<QV4L2FileDescriptor> m_v4l2FileDescriptor; |
120 | |
121 | V4L2CameraInfo m_v4l2Info; |
122 | |
123 | timeval m_firstFrameTime = { .tv_sec: -1, .tv_usec: -1 }; |
124 | quint32 m_bytesPerLine = 0; |
125 | quint32 m_imageSize = 0; |
126 | QVideoFrameFormat::ColorSpace m_colorSpace = QVideoFrameFormat::ColorSpace_Undefined; |
127 | qint64 m_frameDuration = -1; |
128 | bool m_cameraBusy = false; |
129 | }; |
130 | |
131 | QT_END_NAMESPACE |
132 | |
133 | #endif // QV4L2CAMERA_H |
134 | |