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.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
5
6#include <QtMultimedia/private/qcameradevice_p.h>
7#include <QtMultimedia/private/qmultimedia_ranges_p.h>
8#include <QtMultimedia/private/qvideoframe_p.h>
9
11
12namespace ranges = QtMultimediaPrivate::ranges;
13
14QPlatformCamera::QPlatformCamera(QObject *parent)
15 : QPlatformVideoSource(parent), m_camera(qobject_cast<QCamera *>(parent))
16{
17 // TODO: create QPlatformGStreamerElement and use it instead of QPlatformCamera
18 // Then, uncomment the assert
19 Q_ASSERT(parent);
20 qRegisterMetaType<QVideoFrame>();
21}
22
23QCameraFormat QPlatformCamera::findBestCameraFormat(const QCameraDevice &camera) const
24{
25 // check if fmt is better. We try to find the highest resolution that offers
26 // at least 30 FPS
27 // we use 29 FPS to compare against as some cameras report 29.97 FPS...
28
29 auto makeCriteria = [this](const QCameraFormat &fmt) {
30 constexpr float MinSufficientFrameRate = 29.f;
31
32 const auto isValid = fmt.pixelFormat() != QVideoFrameFormat::Format_Invalid;
33 const auto resolution = fmt.resolution();
34 const auto sufficientFrameRate = std::min(fmt.maxFrameRate(), MinSufficientFrameRate);
35 const auto pixelFormatScore =
36 cameraPixelFormatScore(fmt.pixelFormat(), QCameraFormatPrivate::getColorRange(fmt));
37
38 return std::make_tuple(
39 isValid, // 1st: ensure valid formats
40 sufficientFrameRate, // 2nd: ensure the highest frame rate in the range [0; 29]*/
41 resolution.width() * resolution.height(), // 3rd: ensure the highest resolution
42 pixelFormatScore, // 4th: eshure the best pixel format
43 fmt.maxFrameRate()); // 5th: ensure the highest framerate in the whole range
44 };
45
46 const auto formats = camera.videoFormats();
47 if (formats.empty())
48 return QCameraFormat{};
49
50 const auto found =
51 ranges::max(formats, [&](const QCameraFormat &fmtA, const QCameraFormat &fmtB) {
52 return makeCriteria(fmtA) < makeCriteria(fmtB);
53 });
54
55 return found;
56}
57
58QVideoFrameFormat QPlatformCamera::frameFormat() const
59{
60 QVideoFrameFormat result(m_cameraFormat.resolution(),
61 m_framePixelFormat == QVideoFrameFormat::Format_Invalid
62 ? m_cameraFormat.pixelFormat()
63 : m_framePixelFormat);
64 result.setStreamFrameRate(m_cameraFormat.maxFrameRate());
65 return result;
66}
67
68void QPlatformCamera::supportedFeaturesChanged(QCamera::Features f)
69{
70 if (m_supportedFeatures == f)
71 return;
72 m_supportedFeatures = f;
73 Q_EMIT m_camera->supportedFeaturesChanged();
74}
75
76void QPlatformCamera::minimumZoomFactorChanged(float factor)
77{
78 if (m_minZoom == factor)
79 return;
80 m_minZoom = factor;
81 Q_EMIT m_camera->minimumZoomFactorChanged(factor);
82}
83
84void QPlatformCamera::maximumZoomFactorChanged(float factor)
85{
86 if (m_maxZoom == factor)
87 return;
88 m_maxZoom = factor;
89 Q_EMIT m_camera->maximumZoomFactorChanged(factor);
90}
91
92void QPlatformCamera::focusModeChanged(QCamera::FocusMode mode)
93{
94 if (m_focusMode == mode)
95 return;
96 m_focusMode = mode;
97 Q_EMIT m_camera->focusModeChanged();
98}
99
100void QPlatformCamera::customFocusPointChanged(const QPointF &point)
101{
102 if (m_customFocusPoint == point)
103 return;
104 m_customFocusPoint = point;
105 Q_EMIT m_camera->customFocusPointChanged();
106}
107
108
109void QPlatformCamera::zoomFactorChanged(float zoom)
110{
111 if (m_zoomFactor == zoom)
112 return;
113 m_zoomFactor = zoom;
114 Q_EMIT m_camera->zoomFactorChanged(zoom);
115}
116
117
118void QPlatformCamera::focusDistanceChanged(float d)
119{
120 if (m_focusDistance == d)
121 return;
122 m_focusDistance = d;
123 Q_EMIT m_camera->focusDistanceChanged(m_focusDistance);
124}
125
126
127void QPlatformCamera::flashReadyChanged(bool ready)
128{
129 if (m_flashReady == ready)
130 return;
131 m_flashReady = ready;
132 Q_EMIT m_camera->flashReady(m_flashReady);
133}
134
135void QPlatformCamera::flashModeChanged(QCamera::FlashMode mode)
136{
137 if (m_flashMode == mode)
138 return;
139 m_flashMode = mode;
140 Q_EMIT m_camera->flashModeChanged();
141}
142
143void QPlatformCamera::torchModeChanged(QCamera::TorchMode mode)
144{
145 if (m_torchMode == mode)
146 return;
147 m_torchMode = mode;
148 Q_EMIT m_camera->torchModeChanged();
149}
150
151void QPlatformCamera::exposureModeChanged(QCamera::ExposureMode mode)
152{
153 if (m_exposureMode == mode)
154 return;
155 m_exposureMode = mode;
156 Q_EMIT m_camera->exposureModeChanged();
157}
158
159void QPlatformCamera::exposureCompensationChanged(float compensation)
160{
161 if (m_exposureCompensation == compensation)
162 return;
163 m_exposureCompensation = compensation;
164 Q_EMIT m_camera->exposureCompensationChanged(compensation);
165}
166
167void QPlatformCamera::exposureCompensationRangeChanged(float min, float max)
168{
169 if (m_minExposureCompensation == min && m_maxExposureCompensation == max)
170 return;
171 m_minExposureCompensation = min;
172 m_maxExposureCompensation = max;
173 // tell frontend
174}
175
176void QPlatformCamera::isoSensitivityChanged(int iso)
177{
178 if (m_iso == iso)
179 return;
180 m_iso = iso;
181 Q_EMIT m_camera->isoSensitivityChanged(iso);
182}
183
184void QPlatformCamera::exposureTimeChanged(float speed)
185{
186 if (m_exposureTime == speed)
187 return;
188 m_exposureTime = speed;
189 Q_EMIT m_camera->exposureTimeChanged(speed);
191
192void QPlatformCamera::whiteBalanceModeChanged(QCamera::WhiteBalanceMode mode)
193{
194 if (m_whiteBalance == mode)
195 return;
196 m_whiteBalance = mode;
197 Q_EMIT m_camera->whiteBalanceModeChanged();
198}
199
200void QPlatformCamera::colorTemperatureChanged(int temperature)
201{
202 Q_ASSERT(temperature >= 0);
203 Q_ASSERT((temperature > 0 && whiteBalanceMode() == QCamera::WhiteBalanceManual) ||
204 (temperature == 0 && whiteBalanceMode() == QCamera::WhiteBalanceAuto));
205 if (m_colorTemperature == temperature)
206 return;
207 m_colorTemperature = temperature;
208 Q_EMIT m_camera->colorTemperatureChanged();
209}
210
211int QPlatformCamera::colorTemperatureForWhiteBalance(QCamera::WhiteBalanceMode mode)
212{
213 switch (mode) {
214 case QCamera::WhiteBalanceAuto:
215 break;
216 case QCamera::WhiteBalanceManual:
217 case QCamera::WhiteBalanceSunlight:
218 return 5600;
219 case QCamera::WhiteBalanceCloudy:
220 return 6000;
221 case QCamera::WhiteBalanceShade:
222 return 7000;
223 case QCamera::WhiteBalanceTungsten:
224 return 3200;
225 case QCamera::WhiteBalanceFluorescent:
226 return 4000;
227 case QCamera::WhiteBalanceFlash:
228 return 5500;
229 case QCamera::WhiteBalanceSunset:
230 return 3000;
231 }
232 return 0;
233}
234
235void QPlatformCamera::updateError(QCamera::Error error, const QString &errorString)
236{
237 QMetaObject::invokeMethod(this, [this, error, errorString]() {
238 m_error.setAndNotify(error, errorString, *this);
239 });
240}
241
242void QPlatformCamera::emitEndOfStreamFrame(std::chrono::microseconds lastStart,
243 std::optional<std::chrono::microseconds> lastEnd)
244{
245 using namespace std::chrono_literals;
246
247 std::chrono::microseconds endTime = lastEnd.value_or(lastStart);
248 if (endTime <= lastStart)
249 endTime = lastStart + 1us;
250 Q_EMIT newVideoFrame(QVideoFramePrivate::createEndOfStreamFrame(endTime));
251}
252
253QT_END_NAMESPACE
254
255#include "moc_qplatformcamera_p.cpp"
Combined button and popup list for selecting options.