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

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

source code of qtmultimedia/src/multimedia/platform/qplatformcamera_p.h