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
qv4l2camera.cpp
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
5
6#include <QtMultimedia/private/qcameradevice_p.h>
7#include <QtMultimedia/private/qmultimediautils_p.h>
8#include <QtMultimedia/private/qv4l2filedescriptor_p.h>
9#include <QtMultimedia/private/qv4l2formats_p.h>
10#include <QtMultimedia/private/qv4l2memorytransfer_p.h>
11#include <QtMultimedia/private/qvideoframe_p.h>
12#include <QtCore/qloggingcategory.h>
13#include <QtCore/qsocketnotifier.h>
14#include <QtCore/private/qcore_unix_p.h>
15
16#include <sys/time.h>
17
18#include <chrono>
19#include <system_error>
20
22
23namespace QtV4L2 {
24
25using namespace Qt::Literals;
26
27Q_STATIC_LOGGING_CATEGORY(qLcV4L2Camera, "qt.multimedia.v4l2.camera");
28
29static std::chrono::microseconds toMicroseconds(const timeval &tv)
30{
31 return std::chrono::seconds{ tv.tv_sec } + std::chrono::microseconds{ tv.tv_usec };
32}
33
34static QString v4l2ErrorMessage(std::error_code error)
35{
36 if (error == std::errc::device_or_resource_busy)
37 return u"Camera is in use"_s;
38 if (error == std::errc::no_such_device)
39 return u"Camera has been removed"_s;
40 if (error == std::errc::not_supported)
41 return u"Unsupported V4L2 camera format"_s;
42 return u"V4L2 camera error: " + qt_error_string(error.value());
43}
44
46
52
53bool QV4L2Camera::isActive() const
54{
55 return m_active;
56}
57
59{
60 if (m_active == active)
61 return;
63 return;
64
67
69 if (m_active) {
70 if (auto result = startCapturing(); !result)
72
74 } else {
78 }
79
81}
82
84{
86 return;
87
90
93
94 if (auto result = initV4L2Controls(); !result) {
96 return;
97 }
98
99 if (m_active) {
100 if (auto result = startCapturing(); !result)
102 }
103}
104
106{
108 return false;
109
111 return true;
112
113 if (m_active) {
115 closeV4L2Fd();
116
117 if (auto result = initV4L2Controls(); !result) {
119 return true;
120 }
121
122 if (auto result = startCapturing(); !result)
124 }
125
126 return true;
127}
128
130{
131 auto fmt = format;
132 if (fmt.isNull())
134
135 if (fmt == m_cameraFormat)
136 return false;
137
139 // The device must be reconfigured with the new format before the next stream start.
141 return true;
142}
143
145{
146 if (mode == focusMode())
147 return;
148
151 return;
152
153 switch (mode) {
154 default:
156 setV4L2Parameter(V4L2_CID_FOCUS_AUTO, 1);
158 setV4L2Parameter(V4L2_CID_AUTO_FOCUS_RANGE, V4L2_AUTO_FOCUS_RANGE_AUTO);
159 break;
161 setV4L2Parameter(V4L2_CID_FOCUS_AUTO, 1);
163 setV4L2Parameter(V4L2_CID_AUTO_FOCUS_RANGE, V4L2_AUTO_FOCUS_RANGE_MACRO);
164 else if (focusDist)
165 setV4L2Parameter(V4L2_CID_FOCUS_ABSOLUTE, m_v4l2Info.minFocus);
166 break;
168 setV4L2Parameter(V4L2_CID_FOCUS_AUTO, 1);
170 setV4L2Parameter(V4L2_CID_AUTO_FOCUS_RANGE, V4L2_AUTO_FOCUS_RANGE_INFINITY);
171 break;
173 setV4L2Parameter(V4L2_CID_FOCUS_AUTO, 0);
174 setV4L2Parameter(V4L2_CID_FOCUS_ABSOLUTE, m_v4l2Info.maxFocus);
175 break;
177 setV4L2Parameter(V4L2_CID_FOCUS_AUTO, 0);
179 break;
180 }
182}
183
185{
187 setV4L2Parameter(V4L2_CID_FOCUS_ABSOLUTE, distance);
189}
190
191void QV4L2Camera::zoomTo(float factor, float)
192{
194 return;
195 factor = qBound(1., factor, 2.);
197 setV4L2Parameter(V4L2_CID_ZOOM_ABSOLUTE, zoom);
199}
200
210
220
222{
224 return true;
225 return mode == QCamera::FlashOff;
226}
227
229{
230 return getV4L2Parameter(V4L2_CID_FLASH_READY).value_or(0) != 0;
231}
232
242
244{
245 if (mode == QCamera::TorchOn)
247 return mode == QCamera::TorchOff;
248}
249
261
270
281
283{
285 return;
286 setV4L2Parameter(V4L2_CID_ISO_SENSITIVITY_AUTO,
288 if (iso > 0) {
289 iso = qBound(minIso(), iso, maxIso());
290 setV4L2Parameter(V4L2_CID_ISO_SENSITIVITY, iso);
291 }
292}
293
295{
297 return -1;
298 return getV4L2Parameter(V4L2_CID_ISO_SENSITIVITY).value_or(-1);
299}
300
302{
304 int exposure =
306 setV4L2Parameter(V4L2_CID_EXPOSURE_ABSOLUTE, exposure);
308 return;
309 }
310}
311
313{
314 return getV4L2Parameter(V4L2_CID_EXPOSURE_ABSOLUTE).value_or(-10000) / 10000.;
315}
316
324
335
349
351{
353
355 if (!dequeued) {
356 if (dequeued.error() == std::errc::no_such_device) {
357 // camera got removed while being active
359 closeV4L2Fd();
362 << "Cannot take buffer:" << qt_error_string(dequeued.error().value());
363 }
364
365 return;
366 }
367
369
372
374 if (!m_firstFrameTime)
376 const auto frameTime = timestamp - *m_firstFrameTime;
381
383}
384
386{
387 qCWarning(qLcV4L2Camera) << "V4L2 camera error:" << qt_error_string(error.value());
389}
390
392{
394 if (!result) {
395 qCWarning(qLcV4L2Camera) << "Unable to query the V4L2 parameter" << Qt::hex << id
396 << result.error();
397 return std::nullopt;
398 }
399 return *result;
400}
401
403{
404 m_v4l2Info = {};
406
408
411
412 closeV4L2Fd();
413
415 if (descriptor == -1) {
416 const auto error = std::error_code{ errno, std::system_category() };
417 qCWarning(qLcV4L2Camera) << "Unable to open the camera" << deviceName
418 << "for read to query the parameter info:"
420 return q23::unexpected(error);
421 }
422
425
426 qCDebug(qLcV4L2Camera) << "FD=" << descriptor;
427
428 if (query(V4L2_CID_AUTO_WHITE_BALANCE)) {
430 setV4L2Parameter(V4L2_CID_AUTO_WHITE_BALANCE, true);
431 }
432
433 if (const auto control = query(V4L2_CID_WHITE_BALANCE_TEMPERATURE)) {
438 }
439
440 if (query(V4L2_CID_EXPOSURE_AUTO))
442
443 if (const auto control = query(V4L2_CID_EXPOSURE_ABSOLUTE)) {
448 }
449
450 if (const auto control = query(V4L2_CID_AUTO_EXPOSURE_BIAS)) {
454 }
455
456 if (query(V4L2_CID_ISO_SENSITIVITY_AUTO)) {
457 if (const auto control = query(V4L2_CID_ISO_SENSITIVITY)) {
461 }
462 }
463
464 if (const auto control = query(V4L2_CID_FOCUS_ABSOLUTE)) {
468 }
469
470 if (query(V4L2_CID_AUTO_FOCUS_RANGE))
471 m_v4l2Info.rangedFocus = true;
472
473 if (const auto control = query(V4L2_CID_FLASH_LED_MODE)) {
478 }
479
480 if (const auto control = query(V4L2_CID_ZOOM_ABSOLUTE)) {
483 }
484 // zoom factors are in arbitrary units, so we simply normalize them to go from 1 to 2
485 // if they are different
488
490 return {};
491}
492
494{
496
497 m_v4l2Info = {};
499 m_v4l2FileDescriptor = nullptr;
500}
501
503{
505 setV4L2Parameter(V4L2_CID_AUTO_WHITE_BALANCE, temperature == 0 ? true : false);
506 } else if (temperature == 0) {
507 temperature = 5600;
508 }
509
512 if (!setV4L2Parameter(
513 V4L2_CID_WHITE_BALANCE_TEMPERATURE,
515 temperature = 0;
516 } else {
517 temperature = 0;
518 }
519
520 return temperature;
521}
522
524{
526 auto result = m_v4l2FileDescriptor->call(VIDIOC_S_CTRL, &control);
527 if (!result) {
528 qCWarning(qLcV4L2Camera) << "Unable to set the V4L2 parameter" << Qt::hex << id << "to"
529 << value << result.error();
530 return q23::unexpected(result.error());
531 }
532 return {};
533}
534
536{
537 v4l2_control control{ id, 0 };
538 auto result = m_v4l2FileDescriptor->call(VIDIOC_G_CTRL, &control);
539 if (!result) {
540 qCWarning(qLcV4L2Camera) << "Unable to get the V4L2 parameter" << Qt::hex << id
541 << result.error();
542 return std::nullopt;
543 }
544 return control.value;
545}
546
548{
550 return {};
551
553
557
559 if (!v4l2Format) {
560 qCWarning(qLcV4L2Camera) << "Unsupported camera pixel format"
563 }
564
565 v4l2_format fmt = {};
567
573
574 qCDebug(qLcV4L2Camera) << "setting camera format to" << size << fmt.fmt.pix.pixelformat;
575
576 auto setFormatResult = m_v4l2FileDescriptor->call(VIDIOC_S_FMT, &fmt);
577 if (!setFormatResult) {
578 qCWarning(qLcV4L2Camera) << "Couldn't set video format on v4l2 camera"
581 }
582
583 // The driver may have adjusted the requested format, so keep what it negotiated.
584 const auto pixFormat = fmt.fmt.pix;
585
588
590 || (pixFormat.bytesperline == 0 && !isCompressed)) {
591 qCWarning(qLcV4L2Camera) << "Driver negotiated an unusable camera format"
594 }
595
598
599 streamParam.parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
603 if (auto result = m_v4l2FileDescriptor->call(VIDIOC_S_PARM, &streamParam); !result)
604 qCWarning(qLcV4L2Camera) << "Couldn't set camera frame rate:" << result.error();
605
606 using namespace std::chrono;
608 pixFormat,
610 };
611 return {};
612}
613
614q23::expected<void, std::error_code>
616{
618
619 // QT_V4L2_CAMERA_MEMORY=dmabuf|userptr|mmap selects one strategy, with no fallback.
620 const QByteArray forcedStrategy = qgetenv("QT_V4L2_CAMERA_MEMORY");
621 const auto isAllowed = [&](QByteArrayView strategy) {
623 };
624
626
627#if QT_CONFIG(linux_dmabuf)
628 if (isAllowed("dmabuf")) {
630 if (transfer) {
632 return {};
633 }
635 }
636#endif
637
638 if (isAllowed("userptr")) {
640 if (transfer) {
642 return {};
643 }
646 return q23::unexpected(lastError);
647 }
648
649 if (isAllowed("mmap")) {
650 qCDebug(qLcV4L2Camera) << "Trying V4L2_MEMORY_MMAP";
651
653 if (transfer) {
655 return {};
656 }
658 }
659
660 qCWarning(qLcV4L2Camera) << "Cannot init V4L2 memory transfer:"
662 return q23::unexpected(lastError);
663}
664
666{
668 return;
669
670 m_notifier = nullptr;
671
673 // TODO: handle the case carefully to avoid possible memory corruption
675 qCWarning(qLcV4L2Camera) << "failed to stop capture:" << result.error();
676 }
677
678 m_memoryTransfer = nullptr;
679}
680
682{
683 m_notifier = nullptr;
684 m_memoryTransfer = nullptr;
689}
690
692{
695
696 if (auto result = setV4L2CameraFormat(); !result) {
698 return q23::unexpected(result.error());
699 }
700
702
705 return q23::unexpected(result.error());
706 }
707
709 qCWarning(qLcV4L2Camera) << "Couldn't start v4l2 camera stream:" << result.error();
711 return q23::unexpected(result.error());
712 }
713
714 m_notifier =
717 this->readFrame();
718 });
719
723 return {};
724}
725
741
742} // namespace QtV4L2
743
744QT_END_NAMESPACE
745
746#include "moc_qv4l2camera_p.cpp"
Combined button and popup list for selecting options.
static QString v4l2ErrorMessage(std::error_code error)
static std::chrono::microseconds toMicroseconds(const timeval &tv)
Q_STATIC_LOGGING_CATEGORY(qLcV4L2Camera, "qt.multimedia.v4l2.camera")