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#include "qv4l2camera_p.h"
5#include "qv4l2filedescriptor_p.h"
6#include "qv4l2memorytransfer_p.h"
7
8#include <private/qcameradevice_p.h>
9#include <private/qmultimediautils_p.h>
10#include <private/qmemoryvideobuffer_p.h>
11#include <private/qvideoframe_p.h>
12#include <private/qcore_unix_p.h>
13
14#include <qsocketnotifier.h>
15#include <qloggingcategory.h>
16
17QT_BEGIN_NAMESPACE
18
19static Q_LOGGING_CATEGORY(qLcV4L2Camera, "qt.multimedia.ffmpeg.v4l2camera");
20
21static const struct {
22 QVideoFrameFormat::PixelFormat fmt;
23 uint32_t v4l2Format;
24} formatMap[] = {
25 // ### How do we handle V4L2_PIX_FMT_H264 and V4L2_PIX_FMT_MPEG4?
26 { .fmt: QVideoFrameFormat::Format_YUV420P, V4L2_PIX_FMT_YUV420 },
27 { .fmt: QVideoFrameFormat::Format_YUV422P, V4L2_PIX_FMT_YUV422P },
28 { .fmt: QVideoFrameFormat::Format_YUYV, V4L2_PIX_FMT_YUYV },
29 { .fmt: QVideoFrameFormat::Format_UYVY, V4L2_PIX_FMT_UYVY },
30 { .fmt: QVideoFrameFormat::Format_XBGR8888, V4L2_PIX_FMT_XBGR32 },
31 { .fmt: QVideoFrameFormat::Format_XRGB8888, V4L2_PIX_FMT_XRGB32 },
32 { .fmt: QVideoFrameFormat::Format_ABGR8888, V4L2_PIX_FMT_ABGR32 },
33 { .fmt: QVideoFrameFormat::Format_ARGB8888, V4L2_PIX_FMT_ARGB32 },
34 { .fmt: QVideoFrameFormat::Format_BGRX8888, V4L2_PIX_FMT_BGR32 },
35 { .fmt: QVideoFrameFormat::Format_RGBX8888, V4L2_PIX_FMT_RGB32 },
36 { .fmt: QVideoFrameFormat::Format_BGRA8888, V4L2_PIX_FMT_BGRA32 },
37 { .fmt: QVideoFrameFormat::Format_RGBA8888, V4L2_PIX_FMT_RGBA32 },
38 { .fmt: QVideoFrameFormat::Format_Y8, V4L2_PIX_FMT_GREY },
39 { .fmt: QVideoFrameFormat::Format_Y16, V4L2_PIX_FMT_Y16 },
40 { .fmt: QVideoFrameFormat::Format_NV12, V4L2_PIX_FMT_NV12 },
41 { .fmt: QVideoFrameFormat::Format_NV21, V4L2_PIX_FMT_NV21 },
42 { .fmt: QVideoFrameFormat::Format_Jpeg, V4L2_PIX_FMT_MJPEG },
43 { .fmt: QVideoFrameFormat::Format_Jpeg, V4L2_PIX_FMT_JPEG },
44 { .fmt: QVideoFrameFormat::Format_Invalid, .v4l2Format: 0 },
45};
46
47QVideoFrameFormat::PixelFormat formatForV4L2Format(uint32_t v4l2Format)
48{
49 auto *f = formatMap;
50 while (f->v4l2Format) {
51 if (f->v4l2Format == v4l2Format)
52 return f->fmt;
53 ++f;
54 }
55 return QVideoFrameFormat::Format_Invalid;
56}
57
58uint32_t v4l2FormatForPixelFormat(QVideoFrameFormat::PixelFormat format)
59{
60 auto *f = formatMap;
61 while (f->v4l2Format) {
62 if (f->fmt == format)
63 return f->v4l2Format;
64 ++f;
65 }
66 return 0;
67}
68
69QV4L2Camera::QV4L2Camera(QCamera *camera)
70 : QPlatformCamera(camera)
71{
72}
73
74QV4L2Camera::~QV4L2Camera()
75{
76 stopCapturing();
77 closeV4L2Fd();
78}
79
80bool QV4L2Camera::isActive() const
81{
82 return m_active;
83}
84
85void QV4L2Camera::setActive(bool active)
86{
87 if (m_active == active)
88 return;
89 if (m_cameraDevice.isNull() && active)
90 return;
91
92 if (m_cameraFormat.isNull())
93 resolveCameraFormat(format: {});
94
95 m_active = active;
96 if (m_active)
97 startCapturing();
98 else
99 stopCapturing();
100
101 emit newVideoFrame({});
102
103 emit activeChanged(active);
104}
105
106void QV4L2Camera::setCamera(const QCameraDevice &camera)
107{
108 if (m_cameraDevice == camera)
109 return;
110
111 stopCapturing();
112 closeV4L2Fd();
113
114 m_cameraDevice = camera;
115 resolveCameraFormat(format: {});
116
117 initV4L2Controls();
118
119 if (m_active)
120 startCapturing();
121}
122
123bool QV4L2Camera::setCameraFormat(const QCameraFormat &format)
124{
125 if (!format.isNull() && !m_cameraDevice.videoFormats().contains(t: format))
126 return false;
127
128 if (!resolveCameraFormat(format))
129 return true;
130
131 if (m_active) {
132 stopCapturing();
133 closeV4L2Fd();
134
135 initV4L2Controls();
136 startCapturing();
137 }
138
139 return true;
140}
141
142bool QV4L2Camera::resolveCameraFormat(const QCameraFormat &format)
143{
144 auto fmt = format;
145 if (fmt.isNull())
146 fmt = findBestCameraFormat(camera: m_cameraDevice);
147
148 if (fmt == m_cameraFormat)
149 return false;
150
151 m_cameraFormat = fmt;
152 return true;
153}
154
155void QV4L2Camera::setFocusMode(QCamera::FocusMode mode)
156{
157 if (mode == focusMode())
158 return;
159
160 bool focusDist = supportedFeatures() & QCamera::Feature::FocusDistance;
161 if (!focusDist && !m_v4l2Info.rangedFocus)
162 return;
163
164 switch (mode) {
165 default:
166 case QCamera::FocusModeAuto:
167 setV4L2Parameter(V4L2_CID_FOCUS_AUTO, value: 1);
168 if (m_v4l2Info.rangedFocus)
169 setV4L2Parameter(V4L2_CID_AUTO_FOCUS_RANGE, value: V4L2_AUTO_FOCUS_RANGE_AUTO);
170 break;
171 case QCamera::FocusModeAutoNear:
172 setV4L2Parameter(V4L2_CID_FOCUS_AUTO, value: 1);
173 if (m_v4l2Info.rangedFocus)
174 setV4L2Parameter(V4L2_CID_AUTO_FOCUS_RANGE, value: V4L2_AUTO_FOCUS_RANGE_MACRO);
175 else if (focusDist)
176 setV4L2Parameter(V4L2_CID_FOCUS_ABSOLUTE, value: m_v4l2Info.minFocus);
177 break;
178 case QCamera::FocusModeAutoFar:
179 setV4L2Parameter(V4L2_CID_FOCUS_AUTO, value: 1);
180 if (m_v4l2Info.rangedFocus)
181 setV4L2Parameter(V4L2_CID_AUTO_FOCUS_RANGE, value: V4L2_AUTO_FOCUS_RANGE_INFINITY);
182 break;
183 case QCamera::FocusModeInfinity:
184 setV4L2Parameter(V4L2_CID_FOCUS_AUTO, value: 0);
185 setV4L2Parameter(V4L2_CID_FOCUS_ABSOLUTE, value: m_v4l2Info.maxFocus);
186 break;
187 case QCamera::FocusModeManual:
188 setV4L2Parameter(V4L2_CID_FOCUS_AUTO, value: 0);
189 setFocusDistance(focusDistance());
190 break;
191 }
192 focusModeChanged(mode);
193}
194
195void QV4L2Camera::setFocusDistance(float d)
196{
197 int distance = m_v4l2Info.minFocus + int((m_v4l2Info.maxFocus - m_v4l2Info.minFocus) * d);
198 setV4L2Parameter(V4L2_CID_FOCUS_ABSOLUTE, value: distance);
199 focusDistanceChanged(d);
200}
201
202void QV4L2Camera::zoomTo(float factor, float)
203{
204 if (m_v4l2Info.maxZoom == m_v4l2Info.minZoom)
205 return;
206 factor = qBound(min: 1., val: factor, max: 2.);
207 int zoom = m_v4l2Info.minZoom + (factor - 1.) * (m_v4l2Info.maxZoom - m_v4l2Info.minZoom);
208 setV4L2Parameter(V4L2_CID_ZOOM_ABSOLUTE, value: zoom);
209 zoomFactorChanged(zoom: factor);
210}
211
212bool QV4L2Camera::isFocusModeSupported(QCamera::FocusMode mode) const
213{
214 if (supportedFeatures() & QCamera::Feature::FocusDistance &&
215 (mode == QCamera::FocusModeManual || mode == QCamera::FocusModeAutoNear || mode == QCamera::FocusModeInfinity))
216 return true;
217
218 return mode == QCamera::FocusModeAuto;
219}
220
221void QV4L2Camera::setFlashMode(QCamera::FlashMode mode)
222{
223 if (!m_v4l2Info.flashSupported || mode == QCamera::FlashOn)
224 return;
225 setV4L2Parameter(V4L2_CID_FLASH_LED_MODE, value: mode == QCamera::FlashAuto ? V4L2_FLASH_LED_MODE_FLASH : V4L2_FLASH_LED_MODE_NONE);
226 flashModeChanged(mode);
227}
228
229bool QV4L2Camera::isFlashModeSupported(QCamera::FlashMode mode) const
230{
231 if (m_v4l2Info.flashSupported && mode == QCamera::FlashAuto)
232 return true;
233 return mode == QCamera::FlashOff;
234}
235
236bool QV4L2Camera::isFlashReady() const
237{
238 struct v4l2_queryctrl queryControl;
239 ::memset(s: &queryControl, c: 0, n: sizeof(queryControl));
240 queryControl.id = V4L2_CID_AUTO_WHITE_BALANCE;
241
242 return m_v4l2FileDescriptor && m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, arg: &queryControl);
243}
244
245void QV4L2Camera::setTorchMode(QCamera::TorchMode mode)
246{
247 if (!m_v4l2Info.torchSupported || mode == QCamera::TorchOn)
248 return;
249 setV4L2Parameter(V4L2_CID_FLASH_LED_MODE, value: mode == QCamera::TorchOn ? V4L2_FLASH_LED_MODE_TORCH : V4L2_FLASH_LED_MODE_NONE);
250 torchModeChanged(mode);
251}
252
253bool QV4L2Camera::isTorchModeSupported(QCamera::TorchMode mode) const
254{
255 if (mode == QCamera::TorchOn)
256 return m_v4l2Info.torchSupported;
257 return mode == QCamera::TorchOff;
258}
259
260void QV4L2Camera::setExposureMode(QCamera::ExposureMode mode)
261{
262 if (m_v4l2Info.autoExposureSupported && m_v4l2Info.manualExposureSupported) {
263 if (mode != QCamera::ExposureAuto && mode != QCamera::ExposureManual)
264 return;
265 int value = QCamera::ExposureAuto ? V4L2_EXPOSURE_AUTO : V4L2_EXPOSURE_MANUAL;
266 setV4L2Parameter(V4L2_CID_EXPOSURE_AUTO, value);
267 exposureModeChanged(mode);
268 return;
269 }
270}
271
272bool QV4L2Camera::isExposureModeSupported(QCamera::ExposureMode mode) const
273{
274 if (mode == QCamera::ExposureAuto)
275 return true;
276 if (m_v4l2Info.manualExposureSupported && m_v4l2Info.autoExposureSupported)
277 return mode == QCamera::ExposureManual;
278 return false;
279}
280
281void QV4L2Camera::setExposureCompensation(float compensation)
282{
283 if ((m_v4l2Info.minExposureAdjustment != 0 || m_v4l2Info.maxExposureAdjustment != 0)) {
284 int value = qBound(min: m_v4l2Info.minExposureAdjustment, val: (int)(compensation * 1000),
285 max: m_v4l2Info.maxExposureAdjustment);
286 setV4L2Parameter(V4L2_CID_AUTO_EXPOSURE_BIAS, value);
287 exposureCompensationChanged(compensation: value/1000.);
288 return;
289 }
290}
291
292void QV4L2Camera::setManualIsoSensitivity(int iso)
293{
294 if (!(supportedFeatures() & QCamera::Feature::IsoSensitivity))
295 return;
296 setV4L2Parameter(V4L2_CID_ISO_SENSITIVITY_AUTO, value: iso <= 0 ? V4L2_ISO_SENSITIVITY_AUTO : V4L2_ISO_SENSITIVITY_MANUAL);
297 if (iso > 0) {
298 iso = qBound(min: minIso(), val: iso, max: maxIso());
299 setV4L2Parameter(V4L2_CID_ISO_SENSITIVITY, value: iso);
300 }
301 return;
302}
303
304int QV4L2Camera::isoSensitivity() const
305{
306 if (!(supportedFeatures() & QCamera::Feature::IsoSensitivity))
307 return -1;
308 return getV4L2Parameter(V4L2_CID_ISO_SENSITIVITY);
309}
310
311void QV4L2Camera::setManualExposureTime(float secs)
312{
313 if (m_v4l2Info.manualExposureSupported && m_v4l2Info.autoExposureSupported) {
314 int exposure =
315 qBound(min: m_v4l2Info.minExposure, val: qRound(d: secs * 10000.), max: m_v4l2Info.maxExposure);
316 setV4L2Parameter(V4L2_CID_EXPOSURE_ABSOLUTE, value: exposure);
317 exposureTimeChanged(speed: exposure/10000.);
318 return;
319 }
320}
321
322float QV4L2Camera::exposureTime() const
323{
324 return getV4L2Parameter(V4L2_CID_EXPOSURE_ABSOLUTE)/10000.;
325}
326
327bool QV4L2Camera::isWhiteBalanceModeSupported(QCamera::WhiteBalanceMode mode) const
328{
329 if (m_v4l2Info.autoWhiteBalanceSupported && m_v4l2Info.colorTemperatureSupported)
330 return true;
331
332 return mode == QCamera::WhiteBalanceAuto;
333}
334
335void QV4L2Camera::setWhiteBalanceMode(QCamera::WhiteBalanceMode mode)
336{
337 Q_ASSERT(isWhiteBalanceModeSupported(mode));
338
339 int temperature = colorTemperatureForWhiteBalance(mode);
340 int t = setV4L2ColorTemperature(temperature);
341 if (t == 0)
342 mode = QCamera::WhiteBalanceAuto;
343 whiteBalanceModeChanged(mode);
344}
345
346void QV4L2Camera::setColorTemperature(int temperature)
347{
348 if (temperature == 0) {
349 setWhiteBalanceMode(QCamera::WhiteBalanceAuto);
350 return;
351 }
352
353 Q_ASSERT(isWhiteBalanceModeSupported(QCamera::WhiteBalanceManual));
354
355 int t = setV4L2ColorTemperature(temperature);
356 if (t)
357 colorTemperatureChanged(temperature: t);
358}
359
360void QV4L2Camera::readFrame()
361{
362 Q_ASSERT(m_memoryTransfer);
363
364 auto buffer = m_memoryTransfer->dequeueBuffer();
365 if (!buffer) {
366 qCWarning(qLcV4L2Camera) << "Cannot take buffer";
367
368 if (errno == ENODEV) {
369 // camera got removed while being active
370 stopCapturing();
371 closeV4L2Fd();
372 }
373
374 return;
375 }
376
377 auto videoBuffer = std::make_unique<QMemoryVideoBuffer>(args&: buffer->data, args&: m_bytesPerLine);
378 QVideoFrame frame = QVideoFramePrivate::createFrame(buffer: std::move(videoBuffer), format: frameFormat());
379
380 auto &v4l2Buffer = buffer->v4l2Buffer;
381
382 if (m_firstFrameTime.tv_sec == -1)
383 m_firstFrameTime = v4l2Buffer.timestamp;
384 qint64 secs = v4l2Buffer.timestamp.tv_sec - m_firstFrameTime.tv_sec;
385 qint64 usecs = v4l2Buffer.timestamp.tv_usec - m_firstFrameTime.tv_usec;
386 frame.setStartTime(secs*1000000 + usecs);
387 frame.setEndTime(frame.startTime() + m_frameDuration);
388
389 emit newVideoFrame(frame);
390
391 if (!m_memoryTransfer->enqueueBuffer(index: v4l2Buffer.index))
392 qCWarning(qLcV4L2Camera) << "Cannot add buffer";
393}
394
395void QV4L2Camera::setCameraBusy()
396{
397 m_cameraBusy = true;
398 updateError(error: QCamera::CameraError, errorString: QLatin1String("Camera is in use"));
399}
400
401void QV4L2Camera::initV4L2Controls()
402{
403 m_v4l2Info = {};
404 QCamera::Features features;
405
406 const QByteArray deviceName = m_cameraDevice.id();
407 Q_ASSERT(!deviceName.isEmpty());
408
409 closeV4L2Fd();
410
411 const int descriptor = qt_safe_open(pathname: deviceName.constData(), O_RDWR);
412 if (descriptor == -1) {
413 qCWarning(qLcV4L2Camera) << "Unable to open the camera" << deviceName
414 << "for read to query the parameter info:"
415 << qt_error_string(errno);
416 updateError(error: QCamera::CameraError, errorString: QLatin1String("Cannot open camera"));
417 return;
418 }
419
420 m_v4l2FileDescriptor = std::make_shared<QV4L2FileDescriptor>(args: descriptor);
421
422 qCDebug(qLcV4L2Camera) << "FD=" << descriptor;
423
424 struct v4l2_queryctrl queryControl;
425 ::memset(s: &queryControl, c: 0, n: sizeof(queryControl));
426 queryControl.id = V4L2_CID_AUTO_WHITE_BALANCE;
427
428 if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, arg: &queryControl)) {
429 m_v4l2Info.autoWhiteBalanceSupported = true;
430 setV4L2Parameter(V4L2_CID_AUTO_WHITE_BALANCE, value: true);
431 }
432
433 ::memset(s: &queryControl, c: 0, n: sizeof(queryControl));
434 queryControl.id = V4L2_CID_WHITE_BALANCE_TEMPERATURE;
435 if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, arg: &queryControl)) {
436 m_v4l2Info.minColorTemp = queryControl.minimum;
437 m_v4l2Info.maxColorTemp = queryControl.maximum;
438 m_v4l2Info.colorTemperatureSupported = true;
439 features |= QCamera::Feature::ColorTemperature;
440 }
441
442 ::memset(s: &queryControl, c: 0, n: sizeof(queryControl));
443 queryControl.id = V4L2_CID_EXPOSURE_AUTO;
444 if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, arg: &queryControl)) {
445 m_v4l2Info.autoExposureSupported = true;
446 }
447
448 ::memset(s: &queryControl, c: 0, n: sizeof(queryControl));
449 queryControl.id = V4L2_CID_EXPOSURE_ABSOLUTE;
450 if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, arg: &queryControl)) {
451 m_v4l2Info.manualExposureSupported = true;
452 m_v4l2Info.minExposure = queryControl.minimum;
453 m_v4l2Info.maxExposure = queryControl.maximum;
454 features |= QCamera::Feature::ManualExposureTime;
455 }
456
457 ::memset(s: &queryControl, c: 0, n: sizeof(queryControl));
458 queryControl.id = V4L2_CID_AUTO_EXPOSURE_BIAS;
459 if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, arg: &queryControl)) {
460 m_v4l2Info.minExposureAdjustment = queryControl.minimum;
461 m_v4l2Info.maxExposureAdjustment = queryControl.maximum;
462 features |= QCamera::Feature::ExposureCompensation;
463 }
464
465 ::memset(s: &queryControl, c: 0, n: sizeof(queryControl));
466 queryControl.id = V4L2_CID_ISO_SENSITIVITY_AUTO;
467 if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, arg: &queryControl)) {
468 queryControl.id = V4L2_CID_ISO_SENSITIVITY;
469 if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, arg: &queryControl)) {
470 features |= QCamera::Feature::IsoSensitivity;
471 minIsoChanged(iso: queryControl.minimum);
472 maxIsoChanged(iso: queryControl.minimum);
473 }
474 }
475
476 ::memset(s: &queryControl, c: 0, n: sizeof(queryControl));
477 queryControl.id = V4L2_CID_FOCUS_ABSOLUTE;
478 if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, arg: &queryControl)) {
479 m_v4l2Info.minExposureAdjustment = queryControl.minimum;
480 m_v4l2Info.maxExposureAdjustment = queryControl.maximum;
481 features |= QCamera::Feature::FocusDistance;
482 }
483
484 ::memset(s: &queryControl, c: 0, n: sizeof(queryControl));
485 queryControl.id = V4L2_CID_AUTO_FOCUS_RANGE;
486 if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, arg: &queryControl)) {
487 m_v4l2Info.rangedFocus = true;
488 }
489
490 ::memset(s: &queryControl, c: 0, n: sizeof(queryControl));
491 queryControl.id = V4L2_CID_FLASH_LED_MODE;
492 if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, arg: &queryControl)) {
493 m_v4l2Info.flashSupported = queryControl.minimum <= V4L2_FLASH_LED_MODE_FLASH
494 && queryControl.maximum >= V4L2_FLASH_LED_MODE_FLASH;
495 m_v4l2Info.torchSupported = queryControl.minimum <= V4L2_FLASH_LED_MODE_TORCH
496 && queryControl.maximum >= V4L2_FLASH_LED_MODE_TORCH;
497 }
498
499 ::memset(s: &queryControl, c: 0, n: sizeof(queryControl));
500 queryControl.id = V4L2_CID_ZOOM_ABSOLUTE;
501 if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, arg: &queryControl)) {
502 m_v4l2Info.minZoom = queryControl.minimum;
503 m_v4l2Info.maxZoom = queryControl.maximum;
504 }
505 // zoom factors are in arbitrary units, so we simply normalize them to go from 1 to 2
506 // if they are different
507 minimumZoomFactorChanged(factor: 1);
508 maximumZoomFactorChanged(m_v4l2Info.minZoom != m_v4l2Info.maxZoom ? 2 : 1);
509
510 supportedFeaturesChanged(features);
511}
512
513void QV4L2Camera::closeV4L2Fd()
514{
515 Q_ASSERT(!m_memoryTransfer);
516
517 m_v4l2Info = {};
518 m_cameraBusy = false;
519 m_v4l2FileDescriptor = nullptr;
520}
521
522int QV4L2Camera::setV4L2ColorTemperature(int temperature)
523{
524 struct v4l2_control control;
525 ::memset(s: &control, c: 0, n: sizeof(control));
526
527 if (m_v4l2Info.autoWhiteBalanceSupported) {
528 setV4L2Parameter(V4L2_CID_AUTO_WHITE_BALANCE, value: temperature == 0 ? true : false);
529 } else if (temperature == 0) {
530 temperature = 5600;
531 }
532
533 if (temperature != 0 && m_v4l2Info.colorTemperatureSupported) {
534 temperature = qBound(min: m_v4l2Info.minColorTemp, val: temperature, max: m_v4l2Info.maxColorTemp);
535 if (!setV4L2Parameter(
536 V4L2_CID_WHITE_BALANCE_TEMPERATURE,
537 value: qBound(min: m_v4l2Info.minColorTemp, val: temperature, max: m_v4l2Info.maxColorTemp)))
538 temperature = 0;
539 } else {
540 temperature = 0;
541 }
542
543 return temperature;
544}
545
546bool QV4L2Camera::setV4L2Parameter(quint32 id, qint32 value)
547{
548 v4l2_control control{ .id: id, .value: value };
549 if (!m_v4l2FileDescriptor->call(VIDIOC_S_CTRL, arg: &control)) {
550 qWarning() << "Unable to set the V4L2 Parameter" << Qt::hex << id << "to" << value << qt_error_string(errno);
551 return false;
552 }
553 return true;
554}
555
556int QV4L2Camera::getV4L2Parameter(quint32 id) const
557{
558 struct v4l2_control control{.id: id, .value: 0};
559 if (!m_v4l2FileDescriptor->call(VIDIOC_G_CTRL, arg: &control)) {
560 qWarning() << "Unable to get the V4L2 Parameter" << Qt::hex << id << qt_error_string(errno);
561 return 0;
562 }
563 return control.value;
564}
565
566void QV4L2Camera::setV4L2CameraFormat()
567{
568 if (m_v4l2Info.formatInitialized || !m_v4l2FileDescriptor)
569 return;
570
571 Q_ASSERT(!m_cameraFormat.isNull());
572 qCDebug(qLcV4L2Camera) << "XXXXX" << this << m_cameraDevice.id() << m_cameraFormat.pixelFormat()
573 << m_cameraFormat.resolution();
574
575 v4l2_format fmt = {};
576 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
577
578 auto size = m_cameraFormat.resolution();
579 fmt.fmt.pix.width = size.width();
580 fmt.fmt.pix.height = size.height();
581 fmt.fmt.pix.pixelformat = v4l2FormatForPixelFormat(format: m_cameraFormat.pixelFormat());
582 fmt.fmt.pix.field = V4L2_FIELD_ANY;
583
584 qCDebug(qLcV4L2Camera) << "setting camera format to" << size << fmt.fmt.pix.pixelformat;
585
586 if (!m_v4l2FileDescriptor->call(VIDIOC_S_FMT, arg: &fmt)) {
587 if (errno == EBUSY) {
588 setCameraBusy();
589 return;
590 }
591 qWarning() << "Couldn't set video format on v4l2 camera" << strerror(errno);
592 }
593
594 m_v4l2Info.formatInitialized = true;
595 m_cameraBusy = false;
596
597 m_bytesPerLine = fmt.fmt.pix.bytesperline;
598 m_imageSize = std::max(a: fmt.fmt.pix.sizeimage, b: m_bytesPerLine * fmt.fmt.pix.height);
599
600 switch (v4l2_colorspace(fmt.fmt.pix.colorspace)) {
601 default:
602 case V4L2_COLORSPACE_DCI_P3:
603 m_colorSpace = QVideoFrameFormat::ColorSpace_Undefined;
604 break;
605 case V4L2_COLORSPACE_REC709:
606 m_colorSpace = QVideoFrameFormat::ColorSpace_BT709;
607 break;
608 case V4L2_COLORSPACE_JPEG:
609 m_colorSpace = QVideoFrameFormat::ColorSpace_AdobeRgb;
610 break;
611 case V4L2_COLORSPACE_SRGB:
612 // ##### is this correct???
613 m_colorSpace = QVideoFrameFormat::ColorSpace_BT601;
614 break;
615 case V4L2_COLORSPACE_BT2020:
616 m_colorSpace = QVideoFrameFormat::ColorSpace_BT2020;
617 break;
618 }
619
620 v4l2_streamparm streamParam = {};
621 streamParam.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
622
623 streamParam.parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
624 auto [num, den] = qRealToFraction(value: 1./m_cameraFormat.maxFrameRate());
625 streamParam.parm.capture.timeperframe = { .numerator: (uint)num, .denominator: (uint)den };
626 m_v4l2FileDescriptor->call(VIDIOC_S_PARM, arg: &streamParam);
627
628 m_frameDuration = 1000000 * streamParam.parm.capture.timeperframe.numerator
629 / streamParam.parm.capture.timeperframe.denominator;
630}
631
632void QV4L2Camera::initV4L2MemoryTransfer()
633{
634 if (m_cameraBusy)
635 return;
636
637 Q_ASSERT(!m_memoryTransfer);
638
639 m_memoryTransfer = makeUserPtrMemoryTransfer(fileDescriptor: m_v4l2FileDescriptor, imageSize: m_imageSize);
640
641 if (m_memoryTransfer)
642 return;
643
644 if (errno == EBUSY) {
645 setCameraBusy();
646 return;
647 }
648
649 qCDebug(qLcV4L2Camera) << "Cannot init V4L2_MEMORY_USERPTR; trying V4L2_MEMORY_MMAP";
650
651 m_memoryTransfer = makeMMapMemoryTransfer(fileDescriptor: m_v4l2FileDescriptor);
652
653 if (!m_memoryTransfer) {
654 qCWarning(qLcV4L2Camera) << "Cannot init v4l2 memory transfer," << qt_error_string(errno);
655 updateError(error: QCamera::CameraError, errorString: QLatin1String("Cannot init V4L2 memory transfer"));
656 }
657}
658
659void QV4L2Camera::stopCapturing()
660{
661 if (!m_memoryTransfer || !m_v4l2FileDescriptor)
662 return;
663
664 m_notifier = nullptr;
665
666 if (!m_v4l2FileDescriptor->stopStream()) {
667 // TODO: handle the case carefully to avoid possible memory corruption
668 if (errno != ENODEV)
669 qWarning() << "failed to stop capture";
670 }
671
672 m_memoryTransfer = nullptr;
673 m_cameraBusy = false;
674}
675
676void QV4L2Camera::startCapturing()
677{
678 if (!m_v4l2FileDescriptor)
679 return;
680
681 setV4L2CameraFormat();
682 initV4L2MemoryTransfer();
683
684 if (m_cameraBusy || !m_memoryTransfer)
685 return;
686
687 if (!m_v4l2FileDescriptor->startStream()) {
688 qWarning() << "Couldn't start v4l2 camera stream";
689 return;
690 }
691
692 m_notifier =
693 std::make_unique<QSocketNotifier>(args: m_v4l2FileDescriptor->get(), args: QSocketNotifier::Read);
694 connect(sender: m_notifier.get(), signal: &QSocketNotifier::activated, context: this, slot: &QV4L2Camera::readFrame);
695
696 m_firstFrameTime = { .tv_sec: -1, .tv_usec: -1 };
697}
698
699QVideoFrameFormat QV4L2Camera::frameFormat() const
700{
701 auto result = QPlatformCamera::frameFormat();
702 result.setColorSpace(m_colorSpace);
703 return result;
704}
705
706QT_END_NAMESPACE
707
708#include "moc_qv4l2camera_p.cpp"
709

source code of qtmultimedia/src/plugins/multimedia/ffmpeg/qv4l2camera.cpp