| 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 |  | 
|---|
| 22 | QT_BEGIN_NAMESPACE | 
|---|
| 23 |  | 
|---|
| 24 | class Q_MULTIMEDIA_EXPORT QPlatformCamera : public QPlatformVideoSource | 
|---|
| 25 | { | 
|---|
| 26 | Q_OBJECT | 
|---|
| 27 |  | 
|---|
| 28 | public: | 
|---|
| 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 | // | 
|---|
| 70 | // This function should call QPlatformCamera::focusModeChanged if | 
|---|
| 71 | // successful. | 
|---|
| 72 | virtual void setFocusMode(QCamera::FocusMode /*mode*/) {} | 
|---|
| 73 |  | 
|---|
| 74 | virtual void setCustomFocusPoint(const QPointF &/*point*/) {} | 
|---|
| 75 |  | 
|---|
| 76 | // If the new distance is the same as previous, ignore the function call. | 
|---|
| 77 | // | 
|---|
| 78 | // If supportedFeatures does not include the FocusDistance flag, | 
|---|
| 79 | // send a warning and do nothing. | 
|---|
| 80 | // | 
|---|
| 81 | // If the incoming value is out of bounds (outside [0,1]), | 
|---|
| 82 | // send a warning and do nothing. | 
|---|
| 83 | // | 
|---|
| 84 | // If focusMode is set to Manual, apply this focusDistance to the camera. | 
|---|
| 85 | // If not, accept the value but don't apply it to the camera. | 
|---|
| 86 | // | 
|---|
| 87 | // The value 0 maps to the distance closest to the camera. | 
|---|
| 88 | // The value 1 maps to the distance furthest away from the camera. | 
|---|
| 89 | // | 
|---|
| 90 | // This function should call QPlatformCamera::focusDistanceChanged() | 
|---|
| 91 | // if successful. | 
|---|
| 92 | virtual void setFocusDistance(float) {} | 
|---|
| 93 |  | 
|---|
| 94 | // smaller 0: zoom instantly, rate in power-of-two/sec | 
|---|
| 95 | virtual void zoomTo(float /*newZoomFactor*/, float /*rate*/ = -1.) {} | 
|---|
| 96 |  | 
|---|
| 97 | virtual void setFlashMode(QCamera::FlashMode /*mode*/) {} | 
|---|
| 98 | virtual bool isFlashModeSupported(QCamera::FlashMode mode) const { return mode == QCamera::FlashOff; } | 
|---|
| 99 | virtual bool isFlashReady() const { return false; } | 
|---|
| 100 |  | 
|---|
| 101 | virtual void setTorchMode(QCamera::TorchMode /*mode*/) {} | 
|---|
| 102 | virtual bool isTorchModeSupported(QCamera::TorchMode mode) const { return mode == QCamera::TorchOff; } | 
|---|
| 103 |  | 
|---|
| 104 | virtual void setExposureMode(QCamera::ExposureMode) {} | 
|---|
| 105 | virtual bool isExposureModeSupported(QCamera::ExposureMode mode) const { return mode == QCamera::ExposureAuto; } | 
|---|
| 106 | virtual void setExposureCompensation(float) {} | 
|---|
| 107 | virtual int isoSensitivity() const { return 100; } | 
|---|
| 108 | virtual void setManualIsoSensitivity(int) {} | 
|---|
| 109 | virtual void setManualExposureTime(float) {} | 
|---|
| 110 | virtual float exposureTime() const { return -1.; } | 
|---|
| 111 |  | 
|---|
| 112 | virtual bool isWhiteBalanceModeSupported(QCamera::WhiteBalanceMode mode) const { return mode == QCamera::WhiteBalanceAuto; } | 
|---|
| 113 | virtual void setWhiteBalanceMode(QCamera::WhiteBalanceMode /*mode*/) {} | 
|---|
| 114 | virtual void setColorTemperature(int /*temperature*/) {} | 
|---|
| 115 |  | 
|---|
| 116 | QVideoFrameFormat frameFormat() const override; | 
|---|
| 117 |  | 
|---|
| 118 | // Note: Because FocusModeManual effectively cannot function without | 
|---|
| 119 | // being able to set FocusDistance, this feature flag is redundant. | 
|---|
| 120 | // Should be considered for deprecation in the future. | 
|---|
| 121 | QCamera::Features supportedFeatures() const { return m_supportedFeatures; } | 
|---|
| 122 |  | 
|---|
| 123 | QCamera::FocusMode focusMode() const { return m_focusMode; } | 
|---|
| 124 | QPointF focusPoint() const { return m_customFocusPoint; } | 
|---|
| 125 |  | 
|---|
| 126 | float minZoomFactor() const { return m_minZoom; } | 
|---|
| 127 | float maxZoomFactor() const { return m_maxZoom; } | 
|---|
| 128 | float zoomFactor() const { return m_zoomFactor; } | 
|---|
| 129 | QPointF customFocusPoint() const { return m_customFocusPoint; } | 
|---|
| 130 | float focusDistance() const { return m_focusDistance; } | 
|---|
| 131 |  | 
|---|
| 132 | QCamera::FlashMode flashMode() const { return m_flashMode; } | 
|---|
| 133 | QCamera::TorchMode torchMode() const { return m_torchMode; } | 
|---|
| 134 |  | 
|---|
| 135 | QCamera::ExposureMode exposureMode() const { return m_exposureMode; } | 
|---|
| 136 | float exposureCompensation() const { return m_exposureCompensation; } | 
|---|
| 137 | int manualIsoSensitivity() const { return m_iso; } | 
|---|
| 138 | int minIso() const { return m_minIso; } | 
|---|
| 139 | int maxIso() const { return m_maxIso; } | 
|---|
| 140 | float manualExposureTime() const { return m_exposureTime; } | 
|---|
| 141 | float minExposureTime() const { return m_minExposureTime; } | 
|---|
| 142 | float maxExposureTime() const { return m_maxExposureTime; } | 
|---|
| 143 | QCamera::WhiteBalanceMode whiteBalanceMode() const { return m_whiteBalance; } | 
|---|
| 144 | int colorTemperature() const { return m_colorTemperature; } | 
|---|
| 145 |  | 
|---|
| 146 | void supportedFeaturesChanged(QCamera::Features); | 
|---|
| 147 | void minimumZoomFactorChanged(float factor); | 
|---|
| 148 | void maximumZoomFactorChanged(float); | 
|---|
| 149 | void focusModeChanged(QCamera::FocusMode mode); | 
|---|
| 150 | void customFocusPointChanged(const QPointF &point); | 
|---|
| 151 | void focusDistanceChanged(float d); | 
|---|
| 152 | void zoomFactorChanged(float zoom); | 
|---|
| 153 | void flashReadyChanged(bool); | 
|---|
| 154 | void flashModeChanged(QCamera::FlashMode mode); | 
|---|
| 155 | void torchModeChanged(QCamera::TorchMode mode); | 
|---|
| 156 | void exposureModeChanged(QCamera::ExposureMode mode); | 
|---|
| 157 | void exposureCompensationChanged(float compensation); | 
|---|
| 158 | void exposureCompensationRangeChanged(float min, float max); | 
|---|
| 159 | void isoSensitivityChanged(int iso); | 
|---|
| 160 | void minIsoChanged(int iso) { m_minIso = iso; } | 
|---|
| 161 | void maxIsoChanged(int iso) { m_maxIso = iso; } | 
|---|
| 162 | void exposureTimeChanged(float speed); | 
|---|
| 163 | void minExposureTimeChanged(float secs) { m_minExposureTime = secs; } | 
|---|
| 164 | void maxExposureTimeChanged(float secs) { m_maxExposureTime = secs; } | 
|---|
| 165 | void whiteBalanceModeChanged(QCamera::WhiteBalanceMode mode); | 
|---|
| 166 | void colorTemperatureChanged(int temperature); | 
|---|
| 167 |  | 
|---|
| 168 | static int colorTemperatureForWhiteBalance(QCamera::WhiteBalanceMode mode); | 
|---|
| 169 |  | 
|---|
| 170 | QCamera::Error error() const { return m_error.code(); } | 
|---|
| 171 | QString errorString() const final { return m_error.description(); } | 
|---|
| 172 |  | 
|---|
| 173 | void updateError(QCamera::Error error, const QString &errorString); | 
|---|
| 174 |  | 
|---|
| 175 | Q_SIGNALS: | 
|---|
| 176 | void errorOccurred(QCamera::Error error, const QString &errorString); | 
|---|
| 177 |  | 
|---|
| 178 | protected: | 
|---|
| 179 | explicit QPlatformCamera(QCamera *parent); | 
|---|
| 180 |  | 
|---|
| 181 | virtual int cameraPixelFormatScore(QVideoFrameFormat::PixelFormat /*format*/, | 
|---|
| 182 | QVideoFrameFormat::ColorRange /*colorRange*/) const | 
|---|
| 183 | { | 
|---|
| 184 | return 0; | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | QCameraFormat findBestCameraFormat(const QCameraDevice &camera) const; | 
|---|
| 188 | QCameraFormat m_cameraFormat; | 
|---|
| 189 | QVideoFrameFormat::PixelFormat m_framePixelFormat = QVideoFrameFormat::Format_Invalid; | 
|---|
| 190 |  | 
|---|
| 191 | // Helper functions to allow backends to reset properties to default values. | 
|---|
| 192 | // i.e using focusModeChanged(defaultFocusMode()); | 
|---|
| 193 | static constexpr int defaultColorTemperature() { return 0; } | 
|---|
| 194 | static constexpr QPointF defaultCustomFocusPoint() { return { -1, -1 }; } | 
|---|
| 195 | static constexpr float defaultExposureCompensation() { return 0.f; } | 
|---|
| 196 | static constexpr QCamera::ExposureMode defaultExposureMode() { return QCamera::ExposureAuto; } | 
|---|
| 197 | static constexpr float defaultExposureTime() { return -1.f; } | 
|---|
| 198 | static constexpr QCamera::FlashMode defaultFlashMode() { return QCamera::FlashOff; } | 
|---|
| 199 | static constexpr bool defaultFlashReady() { return false; } | 
|---|
| 200 | static constexpr float defaultFocusDistance() { return 1.f; } | 
|---|
| 201 | static constexpr QCamera::FocusMode defaultFocusMode() { return QCamera::FocusModeAuto; } | 
|---|
| 202 | static constexpr int defaultIso() { return -1; } | 
|---|
| 203 | static constexpr QCamera::TorchMode defaultTorchMode() { return QCamera::TorchOff; } | 
|---|
| 204 | static constexpr QCamera::WhiteBalanceMode defaultWhiteBalanceMode() { return QCamera::WhiteBalanceAuto; } | 
|---|
| 205 | static constexpr float defaultZoomFactor() { return 1.f; } | 
|---|
| 206 |  | 
|---|
| 207 | private: | 
|---|
| 208 | QCamera *m_camera = nullptr; | 
|---|
| 209 | QCamera::Features m_supportedFeatures = {}; | 
|---|
| 210 | QCamera::FocusMode m_focusMode = defaultFocusMode(); | 
|---|
| 211 | float m_minZoom = 1.; | 
|---|
| 212 | float m_maxZoom = 1.; | 
|---|
| 213 | float m_zoomFactor = defaultZoomFactor(); | 
|---|
| 214 | float m_focusDistance = defaultFocusDistance(); | 
|---|
| 215 | QPointF m_customFocusPoint = defaultCustomFocusPoint(); | 
|---|
| 216 | bool m_flashReady = defaultFlashReady(); | 
|---|
| 217 | QCamera::FlashMode m_flashMode = defaultFlashMode(); | 
|---|
| 218 | QCamera::TorchMode m_torchMode = defaultTorchMode(); | 
|---|
| 219 | QCamera::ExposureMode m_exposureMode = defaultExposureMode(); | 
|---|
| 220 | float m_exposureCompensation = defaultExposureCompensation(); | 
|---|
| 221 | float m_minExposureCompensation = 0.; | 
|---|
| 222 | float m_maxExposureCompensation = 0.; | 
|---|
| 223 | int m_iso = defaultIso(); | 
|---|
| 224 | int m_minIso = -1; | 
|---|
| 225 | int m_maxIso = -1; | 
|---|
| 226 | float m_exposureTime = defaultExposureTime(); | 
|---|
| 227 | float m_minExposureTime = -1.; | 
|---|
| 228 | float m_maxExposureTime = -1.; | 
|---|
| 229 | QCamera::WhiteBalanceMode m_whiteBalance = defaultWhiteBalanceMode(); | 
|---|
| 230 | int m_colorTemperature = defaultColorTemperature(); | 
|---|
| 231 | QErrorInfo<QCamera::Error> m_error; | 
|---|
| 232 | }; | 
|---|
| 233 |  | 
|---|
| 234 | QT_END_NAMESPACE | 
|---|
| 235 |  | 
|---|
| 236 |  | 
|---|
| 237 | #endif  // QPLATFORMCAMERA_H | 
|---|
| 238 |  | 
|---|
| 239 |  | 
|---|