Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qplatformcamera_p.h
Go to the documentation of this file.
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 QPLATFORMCAMERA_H
5#define QPLATFORMCAMERA_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 <QtMultimedia/private/qerrorinfo_p.h>
19#include <QtMultimedia/private/qplatformvideosource_p.h>
20#include <QtMultimedia/qcamera.h>
21
22#include <chrono>
23#include <optional>
24
25// NOLINTBEGIN (bugprone-reserved-identifier)
26typedef struct _GstElement GstElement;
27// NOLINTEND (bugprone-reserved-identifier)
28
30
31class Q_MULTIMEDIA_EXPORT QPlatformCamera : public QPlatformVideoSource
32{
33 Q_OBJECT
34
35public:
36 // If the new camera-device is the same as the old camera-device,
37 // do nothing.
38 //
39 // The implementation needs to handle other camera properties during a successful
40 // camera-device-change. This should be done for every property, i.e focusMode,
41 // flashMode, focusDistance, zoom etc.
42 //
43 // The properties should be handled in the following way:
44 // - If property is supported on new device, apply the property to the device immediately.
45 // - If property is supported on new device, but range of valid values has changed,
46 // clamp the property and apply it to the camera-device.
47 // - If property is NOT supported on new device, reset the value to default and do
48 // nothing to the camera-device.
49 //
50 // TODO: There is currently no rules on the order of how each signal should be triggered.
51 // In the future we might want to add a rule that requires the implementation
52 // to update all properties at once, then trigger all relevant signals.
53 //
54 // TODO: There are currently no rules in the public API on how we should handle
55 // devices being disconnected or the user passing in an invalid device-id.
56 virtual void setCamera(const QCameraDevice &camera) = 0;
57 virtual bool setCameraFormat(const QCameraFormat &/*format*/) { return false; }
58 QCameraFormat cameraFormat() const { return m_cameraFormat; }
59
60 // FocusModeManual should only be reported as supported if the camera
61 // backend is also able to apply the focusDistance setting.
62 // This effectively means the backend should also report Feature::FocusDistance
63 // as supported if this is the case.
64 virtual bool isFocusModeSupported(QCamera::FocusMode mode) const { return mode == QCamera::FocusModeAuto; }
65
66 // If the focusMode is the same as the current, ignore the function call.
67 //
68 // If the new focusMode is reported as unsupported, send a warning
69 // and do nothing.
70 //
71 // FocusModeAuto should map to continuous autofocus mode in the backend.
72 // FocusModeManual should be treated as fixed lens position
73 // in the backend.
74 //
75 // If the new mode is FocusModeManual, apply the focusDistance setting.
76 //
77 // This function should call QPlatformCamera::focusModeChanged if
78 // successful.
79 virtual void setFocusMode(QCamera::FocusMode /*mode*/) {}
80
81 virtual void setCustomFocusPoint(const QPointF &/*point*/) {}
82
83 // If the new distance is the same as previous, ignore the function call.
84 //
85 // If supportedFeatures does not include the FocusDistance flag,
86 // send a warning and do nothing.
87 //
88 // If the incoming value is out of bounds (outside [0,1]),
89 // send a warning and do nothing.
90 //
91 // If focusMode is set to Manual, apply this focusDistance to the camera.
92 // If not, accept the value but don't apply it to the camera.
93 //
94 // The value 0 maps to the distance closest to the camera.
95 // The value 1 maps to the distance furthest away from the camera.
96 //
97 // This function should call QPlatformCamera::focusDistanceChanged()
98 // if successful.
99 virtual void setFocusDistance(float) {}
100
101 // smaller 0: zoom instantly, rate in power-of-two/sec
102 virtual void zoomTo(float /*newZoomFactor*/, float /*rate*/ = -1.) {}
103
104 virtual void setFlashMode(QCamera::FlashMode /*mode*/) {}
105 virtual bool isFlashModeSupported(QCamera::FlashMode mode) const { return mode == QCamera::FlashOff; }
106 virtual bool isFlashReady() const { return false; }
107
108 virtual void setTorchMode(QCamera::TorchMode /*mode*/) {}
109 virtual bool isTorchModeSupported(QCamera::TorchMode mode) const { return mode == QCamera::TorchOff; }
110
111 virtual void setExposureMode(QCamera::ExposureMode) {}
112 virtual bool isExposureModeSupported(QCamera::ExposureMode mode) const { return mode == QCamera::ExposureAuto; }
113 virtual void setExposureCompensation(float) {}
114 virtual int isoSensitivity() const { return 100; }
115 virtual void setManualIsoSensitivity(int) {}
116 virtual void setManualExposureTime(float) {}
117 virtual float exposureTime() const { return -1.; }
118
119 virtual bool isWhiteBalanceModeSupported(QCamera::WhiteBalanceMode mode) const { return mode == QCamera::WhiteBalanceAuto; }
120 virtual void setWhiteBalanceMode(QCamera::WhiteBalanceMode /*mode*/) {}
121 virtual void setColorTemperature(int /*temperature*/) {}
122
123#if QT_CONFIG(gstreamer_qt_api)
124 virtual GstElement *rawGstElement() const { return nullptr; }
125#endif
126
127 QVideoFrameFormat frameFormat() const override;
128
129 // Note: Because FocusModeManual effectively cannot function without
130 // being able to set FocusDistance, this feature flag is redundant.
131 // Should be considered for deprecation in the future.
132 QCamera::Features supportedFeatures() const { return m_supportedFeatures; }
133
134 QCamera::FocusMode focusMode() const { return m_focusMode; }
135 QPointF focusPoint() const { return m_customFocusPoint; }
136
137 float minZoomFactor() const { return m_minZoom; }
138 float maxZoomFactor() const { return m_maxZoom; }
139 float zoomFactor() const { return m_zoomFactor; }
140 QPointF customFocusPoint() const { return m_customFocusPoint; }
141 float focusDistance() const { return m_focusDistance; }
142
143 QCamera::FlashMode flashMode() const { return m_flashMode; }
144 QCamera::TorchMode torchMode() const { return m_torchMode; }
145
146 QCamera::ExposureMode exposureMode() const { return m_exposureMode; }
147 float exposureCompensation() const { return m_exposureCompensation; }
148 int manualIsoSensitivity() const { return m_iso; }
149 int minIso() const { return m_minIso; }
150 int maxIso() const { return m_maxIso; }
151 float manualExposureTime() const { return m_exposureTime; }
152 float minExposureTime() const { return m_minExposureTime; }
153 float maxExposureTime() const { return m_maxExposureTime; }
154 QCamera::WhiteBalanceMode whiteBalanceMode() const { return m_whiteBalance; }
155 int colorTemperature() const { return m_colorTemperature; }
156
157 void supportedFeaturesChanged(QCamera::Features);
158 void minimumZoomFactorChanged(float factor);
159 void maximumZoomFactorChanged(float);
160 void focusModeChanged(QCamera::FocusMode mode);
161 void customFocusPointChanged(const QPointF &point);
162 void focusDistanceChanged(float d);
163 void zoomFactorChanged(float zoom);
164 void flashReadyChanged(bool);
165 void flashModeChanged(QCamera::FlashMode mode);
166 void torchModeChanged(QCamera::TorchMode mode);
167 void exposureModeChanged(QCamera::ExposureMode mode);
168 void exposureCompensationChanged(float compensation);
169 void exposureCompensationRangeChanged(float min, float max);
170 void isoSensitivityChanged(int iso);
171 void minIsoChanged(int iso) { m_minIso = iso; }
172 void maxIsoChanged(int iso) { m_maxIso = iso; }
173 void exposureTimeChanged(float speed);
174 void minExposureTimeChanged(float secs) { m_minExposureTime = secs; }
175 void maxExposureTimeChanged(float secs) { m_maxExposureTime = secs; }
176 void whiteBalanceModeChanged(QCamera::WhiteBalanceMode mode);
177 void colorTemperatureChanged(int temperature);
178
179 static int colorTemperatureForWhiteBalance(QCamera::WhiteBalanceMode mode);
180
181 QCamera::Error error() const { return m_error.code(); }
182 QString errorString() const final { return m_error.description(); }
183
184 void updateError(QCamera::Error error, const QString &errorString);
185
186Q_SIGNALS:
187 void errorOccurred(QCamera::Error error, const QString &errorString);
188
189protected:
190 explicit QPlatformCamera(QObject *parent);
191
192 void emitEndOfStreamFrame(std::chrono::microseconds lastStart,
193 std::optional<std::chrono::microseconds> lastEnd = std::nullopt);
194
195 virtual int cameraPixelFormatScore(QVideoFrameFormat::PixelFormat /*format*/,
196 QVideoFrameFormat::ColorRange /*colorRange*/) const
197 {
198 return 0;
199 }
200
201 QCameraFormat findBestCameraFormat(const QCameraDevice &camera) const;
202 QCameraFormat m_cameraFormat;
203 QVideoFrameFormat::PixelFormat m_framePixelFormat = QVideoFrameFormat::Format_Invalid;
204
205 // Helper functions to allow backends to reset properties to default values.
206 // i.e using focusModeChanged(defaultFocusMode());
207 static constexpr int defaultColorTemperature() { return 0; }
208 static constexpr QPointF defaultCustomFocusPoint() { return { -1, -1 }; }
209 static constexpr float defaultExposureCompensation() { return 0.f; }
210 static constexpr QCamera::ExposureMode defaultExposureMode() { return QCamera::ExposureAuto; }
211 static constexpr float defaultExposureTime() { return -1.f; }
212 static constexpr QCamera::FlashMode defaultFlashMode() { return QCamera::FlashOff; }
213 static constexpr bool defaultFlashReady() { return false; }
214 static constexpr float defaultFocusDistance() { return 1.f; }
215 static constexpr QCamera::FocusMode defaultFocusMode() { return QCamera::FocusModeAuto; }
216 static constexpr int defaultIso() { return -1; }
217 static constexpr QCamera::TorchMode defaultTorchMode() { return QCamera::TorchOff; }
218 static constexpr QCamera::WhiteBalanceMode defaultWhiteBalanceMode() { return QCamera::WhiteBalanceAuto; }
219 static constexpr float defaultZoomFactor() { return 1.f; }
220
221private:
222 QCamera *m_camera = nullptr;
223 QCamera::Features m_supportedFeatures = {};
224 QCamera::FocusMode m_focusMode = defaultFocusMode();
225 float m_minZoom = 1.;
226 float m_maxZoom = 1.;
227 float m_zoomFactor = defaultZoomFactor();
228 float m_focusDistance = defaultFocusDistance();
229 QPointF m_customFocusPoint = defaultCustomFocusPoint();
230 bool m_flashReady = defaultFlashReady();
231 QCamera::FlashMode m_flashMode = defaultFlashMode();
232 QCamera::TorchMode m_torchMode = defaultTorchMode();
233 QCamera::ExposureMode m_exposureMode = defaultExposureMode();
234 float m_exposureCompensation = defaultExposureCompensation();
235 float m_minExposureCompensation = 0.;
236 float m_maxExposureCompensation = 0.;
237 int m_iso = defaultIso();
238 int m_minIso = -1;
239 int m_maxIso = -1;
240 float m_exposureTime = defaultExposureTime();
241 float m_minExposureTime = -1.;
242 float m_maxExposureTime = -1.;
243 QCamera::WhiteBalanceMode m_whiteBalance = defaultWhiteBalanceMode();
244 int m_colorTemperature = defaultColorTemperature();
245 QErrorInfo<QCamera::Error> m_error;
246};
247
248QT_END_NAMESPACE
249
250
251#endif // QPLATFORMCAMERA_H
Combined button and popup list for selecting options.
struct _GstElement GstElement