Debian Codegraph @

contrib/depends/patches/qt/v4l2.patch

raw
1 diff --git a/src/plugins/multimedia/CMakeLists.txt b/src/plugins/multimedia/CMakeLists.txt
2 index 5b163e98a..eac405570 100644
3 --- a/src/plugins/multimedia/CMakeLists.txt
4 +++ b/src/plugins/multimedia/CMakeLists.txt
5 @@ -2,7 +2,7 @@
6 # SPDX-License-Identifier: BSD-3-Clause
7
8 if(QT_FEATURE_ffmpeg)
9 - add_subdirectory(ffmpeg)
10 + add_subdirectory(v4l2)
11 endif()
12 if(QT_FEATURE_gstreamer)
13 add_subdirectory(gstreamer)
14 diff --git a/src/plugins/multimedia/v4l2/CMakeLists.txt b/src/plugins/multimedia/v4l2/CMakeLists.txt
15 new file mode 100644
16 index 000000000..f20612c29
17 --- /dev/null
18 +++ b/src/plugins/multimedia/v4l2/CMakeLists.txt
19 @@ -0,0 +1,24 @@
20 +qt_internal_add_plugin(QFFmpegMediaPlugin
21 + OUTPUT_NAME ffmpegmediaplugin
22 + PLUGIN_TYPE multimedia
23 + SOURCES
24 + qffmpegmediametadata.cpp qffmpegmediametadata_p.h
25 + qffmpegvideosink.cpp qffmpegvideosink_p.h
26 + qffmpegmediaformatinfo.cpp qffmpegmediaformatinfo_p.h
27 + qffmpegmediaintegration.cpp qffmpegmediaintegration_p.h
28 + qffmpegimagecapture.cpp qffmpegimagecapture_p.h
29 + qffmpegmediacapturesession.cpp qffmpegmediacapturesession_p.h
30 + DEFINES
31 + QT_COMPILING_FFMPEG
32 + LIBRARIES
33 + Qt::MultimediaPrivate
34 + Qt::CorePrivate
35 +)
36 +
37 +qt_internal_extend_target(QFFmpegMediaPlugin CONDITION QT_FEATURE_linux_v4l
38 + SOURCES
39 + qv4l2camera.cpp qv4l2camera_p.h
40 + qv4l2filedescriptor.cpp qv4l2filedescriptor_p.h
41 + qv4l2memorytransfer.cpp qv4l2memorytransfer_p.h
42 + qv4l2cameradevices.cpp qv4l2cameradevices_p.h
43 +)
44 diff --git a/src/plugins/multimedia/v4l2/ffmpeg.json b/src/plugins/multimedia/v4l2/ffmpeg.json
45 new file mode 100644
46 index 000000000..d8e7e4456
47 --- /dev/null
48 +++ b/src/plugins/multimedia/v4l2/ffmpeg.json
49 @@ -0,0 +1,3 @@
50 +{
51 + "Keys": [ "ffmpeg" ]
52 +}
53 diff --git a/src/plugins/multimedia/v4l2/qffmpegimagecapture.cpp b/src/plugins/multimedia/v4l2/qffmpegimagecapture.cpp
54 new file mode 100644
55 index 000000000..9ee4e1db8
56 --- /dev/null
57 +++ b/src/plugins/multimedia/v4l2/qffmpegimagecapture.cpp
58 @@ -0,0 +1,269 @@
59 +// Copyright (C) 2016 The Qt Company Ltd.
60 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
61 +
62 +#include "qffmpegimagecapture_p.h"
63 +#include <private/qplatformmediaformatinfo_p.h>
64 +#include <private/qplatformcamera_p.h>
65 +#include <private/qplatformimagecapture_p.h>
66 +#include <qvideoframeformat.h>
67 +#include <private/qmediastoragelocation_p.h>
68 +#include <qimagewriter.h>
69 +
70 +#include <QtCore/QDebug>
71 +#include <QtCore/QDir>
72 +#include <qstandardpaths.h>
73 +
74 +#include <qloggingcategory.h>
75 +
76 +QT_BEGIN_NAMESPACE
77 +
78 +Q_LOGGING_CATEGORY(qLcImageCapture, "qt.multimedia.imageCapture")
79 +
80 +QFFmpegImageCapture::QFFmpegImageCapture(QImageCapture *parent)
81 + : QPlatformImageCapture(parent)
82 +{
83 +}
84 +
85 +QFFmpegImageCapture::~QFFmpegImageCapture()
86 +{
87 +}
88 +
89 +bool QFFmpegImageCapture::isReadyForCapture() const
90 +{
91 + return m_isReadyForCapture;
92 +}
93 +
94 +static const char *extensionForFormat(QImageCapture::FileFormat format)
95 +{
96 + const char *fmt = "jpg";
97 + switch (format) {
98 + case QImageCapture::UnspecifiedFormat:
99 + case QImageCapture::JPEG:
100 + fmt = "jpg";
101 + break;
102 + case QImageCapture::PNG:
103 + fmt = "png";
104 + break;
105 + case QImageCapture::WebP:
106 + fmt = "webp";
107 + break;
108 + case QImageCapture::Tiff:
109 + fmt = "tiff";
110 + break;
111 + }
112 + return fmt;
113 +}
114 +
115 +int QFFmpegImageCapture::capture(const QString &fileName)
116 +{
117 + QString path = QMediaStorageLocation::generateFileName(fileName, QStandardPaths::PicturesLocation, QLatin1String(extensionForFormat(m_settings.format())));
118 + return doCapture(path);
119 +}
120 +
121 +int QFFmpegImageCapture::captureToBuffer()
122 +{
123 + return doCapture(QString());
124 +}
125 +
126 +int QFFmpegImageCapture::doCapture(const QString &fileName)
127 +{
128 + qCDebug(qLcImageCapture) << "do capture";
129 + if (!m_session) {
130 + //emit error in the next event loop,
131 + //so application can associate it with returned request id.
132 + QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
133 + Q_ARG(int, -1),
134 + Q_ARG(int, QImageCapture::ResourceError),
135 + Q_ARG(QString, QPlatformImageCapture::msgImageCaptureNotSet()));
136 +
137 + qCDebug(qLcImageCapture) << "error 1";
138 + return -1;
139 + }
140 + if (!m_camera) {
141 + //emit error in the next event loop,
142 + //so application can associate it with returned request id.
143 + QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
144 + Q_ARG(int, -1),
145 + Q_ARG(int, QImageCapture::ResourceError),
146 + Q_ARG(QString,tr("No camera available.")));
147 +
148 + qCDebug(qLcImageCapture) << "error 2";
149 + return -1;
150 + }
151 + if (passImage) {
152 + //emit error in the next event loop,
153 + //so application can associate it with returned request id.
154 + QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
155 + Q_ARG(int, -1),
156 + Q_ARG(int, QImageCapture::NotReadyError),
157 + Q_ARG(QString, QPlatformImageCapture::msgCameraNotReady()));
158 +
159 + qCDebug(qLcImageCapture) << "error 3";
160 + return -1;
161 + }
162 + m_lastId++;
163 +
164 + pendingImages.enqueue({m_lastId, fileName, QMediaMetaData{}});
165 + // let one image pass the pipeline
166 + passImage = true;
167 +
168 + updateReadyForCapture();
169 + return m_lastId;
170 +}
171 +
172 +void QFFmpegImageCapture::setCaptureSession(QPlatformMediaCaptureSession *session)
173 +{
174 + auto *captureSession = static_cast<QFFmpegMediaCaptureSession *>(session);
175 + if (m_session == captureSession)
176 + return;
177 +
178 + if (m_session) {
179 + disconnect(m_session, nullptr, this, nullptr);
180 + m_lastId = 0;
181 + pendingImages.clear();
182 + passImage = false;
183 + cameraActive = false;
184 + }
185 +
186 + m_session = captureSession;
187 + if (m_session)
188 + connect(m_session, &QPlatformMediaCaptureSession::cameraChanged, this, &QFFmpegImageCapture::onCameraChanged);
189 +
190 + onCameraChanged();
191 + updateReadyForCapture();
192 +}
193 +
194 +void QFFmpegImageCapture::updateReadyForCapture()
195 +{
196 + bool ready = m_session && !passImage && cameraActive;
197 + if (ready == m_isReadyForCapture)
198 + return;
199 + m_isReadyForCapture = ready;
200 + emit readyForCaptureChanged(m_isReadyForCapture);
201 +}
202 +
203 +void QFFmpegImageCapture::cameraActiveChanged(bool active)
204 +{
205 + qCDebug(qLcImageCapture) << "cameraActiveChanged" << cameraActive << active;
206 + if (cameraActive == active)
207 + return;
208 + cameraActive = active;
209 + qCDebug(qLcImageCapture) << "isReady" << isReadyForCapture();
210 + updateReadyForCapture();
211 +}
212 +
213 +void QFFmpegImageCapture::newVideoFrame(const QVideoFrame &frame)
214 +{
215 + if (!passImage)
216 + return;
217 +
218 + passImage = false;
219 + Q_ASSERT(!pendingImages.isEmpty());
220 + auto pending = pendingImages.dequeue();
221 +
222 + emit imageExposed(pending.id);
223 + // ### Add metadata from the AVFrame
224 + emit imageMetadataAvailable(pending.id, pending.metaData);
225 + emit imageAvailable(pending.id, frame);
226 + QImage image = frame.toImage();
227 + if (m_settings.resolution().isValid() && m_settings.resolution() != image.size())
228 + image = image.scaled(m_settings.resolution());
229 +
230 + emit imageCaptured(pending.id, image);
231 + if (!pending.filename.isEmpty()) {
232 + const char *fmt = nullptr;
233 + switch (m_settings.format()) {
234 + case QImageCapture::UnspecifiedFormat:
235 + case QImageCapture::JPEG:
236 + fmt = "jpeg";
237 + break;
238 + case QImageCapture::PNG:
239 + fmt = "png";
240 + break;
241 + case QImageCapture::WebP:
242 + fmt = "webp";
243 + break;
244 + case QImageCapture::Tiff:
245 + fmt = "tiff";
246 + break;
247 + }
248 + int quality = -1;
249 + switch (m_settings.quality()) {
250 + case QImageCapture::VeryLowQuality:
251 + quality = 25;
252 + break;
253 + case QImageCapture::LowQuality:
254 + quality = 50;
255 + break;
256 + case QImageCapture::NormalQuality:
257 + break;
258 + case QImageCapture::HighQuality:
259 + quality = 75;
260 + break;
261 + case QImageCapture::VeryHighQuality:
262 + quality = 99;
263 + break;
264 + }
265 +
266 + QImageWriter writer(pending.filename, fmt);
267 + writer.setQuality(quality);
268 +
269 + if (writer.write(image)) {
270 + emit imageSaved(pending.id, pending.filename);
271 + } else {
272 + QImageCapture::Error err = QImageCapture::ResourceError;
273 + if (writer.error() == QImageWriter::UnsupportedFormatError)
274 + err = QImageCapture::FormatError;
275 + emit error(pending.id, err, writer.errorString());
276 + }
277 + }
278 + updateReadyForCapture();
279 +}
280 +
281 +void QFFmpegImageCapture::onCameraChanged()
282 +{
283 + auto *camera = m_session ? m_session->camera() : nullptr;
284 + if (m_camera == camera)
285 + return;
286 +
287 + if (m_camera)
288 + disconnect(m_camera);
289 +
290 + m_camera = camera;
291 +
292 + if (camera) {
293 + cameraActiveChanged(camera->isActive());
294 + connect(camera, &QPlatformCamera::activeChanged, this, &QFFmpegImageCapture::cameraActiveChanged);
295 + connect(camera, &QPlatformCamera::newVideoFrame, this, &QFFmpegImageCapture::newVideoFrame);
296 + } else {
297 + cameraActiveChanged(false);
298 + }
299 +}
300 +
301 +QImageEncoderSettings QFFmpegImageCapture::imageSettings() const
302 +{
303 + return m_settings;
304 +}
305 +
306 +void QFFmpegImageCapture::setImageSettings(const QImageEncoderSettings &settings)
307 +{
308 + auto s = settings;
309 + const auto supportedFormats = QPlatformMediaIntegration::instance()->formatInfo()->imageFormats;
310 + if (supportedFormats.isEmpty()) {
311 + emit error(-1, QImageCapture::FormatError, "No image formats supported, can't capture.");
312 + return;
313 + }
314 + if (s.format() == QImageCapture::UnspecifiedFormat) {
315 + auto f = QImageCapture::JPEG;
316 + if (!supportedFormats.contains(f))
317 + f = supportedFormats.first();
318 + s.setFormat(f);
319 + } else if (!supportedFormats.contains(settings.format())) {
320 + emit error(-1, QImageCapture::FormatError, "Image format not supported.");
321 + return;
322 + }
323 +
324 + m_settings = settings;
325 +}
326 +
327 +QT_END_NAMESPACE
328 diff --git a/src/plugins/multimedia/v4l2/qffmpegimagecapture_p.h b/src/plugins/multimedia/v4l2/qffmpegimagecapture_p.h
329 new file mode 100644
330 index 000000000..de54fe7cb
331 --- /dev/null
332 +++ b/src/plugins/multimedia/v4l2/qffmpegimagecapture_p.h
333 @@ -0,0 +1,72 @@
334 +// Copyright (C) 2016 The Qt Company Ltd.
335 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
336 +
337 +
338 +#ifndef QFFMPEGIMAGECAPTURE_H
339 +#define QFFMPEGIMAGECAPTURE_H
340 +
341 +//
342 +// W A R N I N G
343 +// -------------
344 +//
345 +// This file is not part of the Qt API. It exists purely as an
346 +// implementation detail. This header file may change from version to
347 +// version without notice, or even be removed.
348 +//
349 +// We mean it.
350 +//
351 +
352 +#include <private/qplatformimagecapture_p.h>
353 +#include "qffmpegmediacapturesession_p.h"
354 +
355 +#include <qqueue.h>
356 +
357 +QT_BEGIN_NAMESPACE
358 +
359 +class QFFmpegImageCapture : public QPlatformImageCapture
360 +
361 +{
362 + Q_OBJECT
363 +public:
364 + QFFmpegImageCapture(QImageCapture *parent);
365 + virtual ~QFFmpegImageCapture();
366 +
367 + bool isReadyForCapture() const override;
368 + int capture(const QString &fileName) override;
369 + int captureToBuffer() override;
370 +
371 + QImageEncoderSettings imageSettings() const override;
372 + void setImageSettings(const QImageEncoderSettings &settings) override;
373 +
374 + void setCaptureSession(QPlatformMediaCaptureSession *session);
375 +
376 + void updateReadyForCapture();
377 +
378 +public Q_SLOTS:
379 + void cameraActiveChanged(bool active);
380 + void newVideoFrame(const QVideoFrame &frame);
381 + void onCameraChanged();
382 +
383 +private:
384 + int doCapture(const QString &fileName);
385 +
386 + QFFmpegMediaCaptureSession *m_session = nullptr;
387 + int m_lastId = 0;
388 + QImageEncoderSettings m_settings;
389 + QPlatformCamera *m_camera = nullptr;
390 +
391 + struct PendingImage {
392 + int id;
393 + QString filename;
394 + QMediaMetaData metaData;
395 + };
396 +
397 + QQueue<PendingImage> pendingImages;
398 + bool passImage = false;
399 + bool cameraActive = false;
400 + bool m_isReadyForCapture = false;
401 +};
402 +
403 +QT_END_NAMESPACE
404 +
405 +#endif // QGSTREAMERCAPTURECORNTROL_H
406 diff --git a/src/plugins/multimedia/v4l2/qffmpegmediacapturesession.cpp b/src/plugins/multimedia/v4l2/qffmpegmediacapturesession.cpp
407 new file mode 100644
408 index 000000000..b6865761c
409 --- /dev/null
410 +++ b/src/plugins/multimedia/v4l2/qffmpegmediacapturesession.cpp
411 @@ -0,0 +1,114 @@
412 +// Copyright (C) 2016 The Qt Company Ltd.
413 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
414 +
415 +#include "qffmpegmediacapturesession_p.h"
416 +
417 +#include "private/qplatformaudioinput_p.h"
418 +#include "private/qplatformaudiooutput_p.h"
419 +#include "qffmpegimagecapture_p.h"
420 +#include "private/qplatformcamera_p.h"
421 +#include "qvideosink.h"
422 +
423 +#include <qloggingcategory.h>
424 +
425 +QT_BEGIN_NAMESPACE
426 +
427 +Q_LOGGING_CATEGORY(qLcMediaCapture, "qt.multimedia.capture")
428 +
429 +
430 +
431 +QFFmpegMediaCaptureSession::QFFmpegMediaCaptureSession()
432 +{
433 +}
434 +
435 +QFFmpegMediaCaptureSession::~QFFmpegMediaCaptureSession()
436 +{
437 +}
438 +
439 +QPlatformCamera *QFFmpegMediaCaptureSession::camera()
440 +{
441 + return m_camera;
442 +}
443 +
444 +void QFFmpegMediaCaptureSession::setCamera(QPlatformCamera *camera)
445 +{
446 + if (m_camera == camera)
447 + return;
448 + if (m_camera) {
449 + m_camera->disconnect(this);
450 + m_camera->setCaptureSession(nullptr);
451 + }
452 +
453 + m_camera = camera;
454 +
455 + if (m_camera) {
456 + connect(m_camera, &QPlatformCamera::newVideoFrame, this, &QFFmpegMediaCaptureSession::newVideoFrame);
457 + m_camera->setCaptureSession(this);
458 + }
459 +
460 + emit cameraChanged();
461 +}
462 +
463 +QPlatformImageCapture *QFFmpegMediaCaptureSession::imageCapture()
464 +{
465 + return m_imageCapture;
466 +}
467 +
468 +void QFFmpegMediaCaptureSession::setImageCapture(QPlatformImageCapture *imageCapture)
469 +{
470 + if (m_imageCapture == imageCapture)
471 + return;
472 +
473 + if (m_imageCapture)
474 + m_imageCapture->setCaptureSession(nullptr);
475 +
476 + m_imageCapture = static_cast<QFFmpegImageCapture *>(imageCapture);
477 +
478 + if (m_imageCapture)
479 + m_imageCapture->setCaptureSession(this);
480 +
481 + emit imageCaptureChanged();
482 +}
483 +
484 +void QFFmpegMediaCaptureSession::setMediaRecorder(QPlatformMediaRecorder *recorder)
485 +{
486 + return;
487 +}
488 +
489 +QPlatformMediaRecorder *QFFmpegMediaCaptureSession::mediaRecorder()
490 +{
491 + return nullptr;
492 +}
493 +
494 +void QFFmpegMediaCaptureSession::setAudioInput(QPlatformAudioInput *input)
495 +{
496 + if (m_audioInput == input)
497 + return;
498 +
499 + m_audioInput = input;
500 +}
501 +
502 +void QFFmpegMediaCaptureSession::setVideoPreview(QVideoSink *sink)
503 +{
504 + if (m_videoSink == sink)
505 + return;
506 +
507 + m_videoSink = sink;
508 +}
509 +
510 +void QFFmpegMediaCaptureSession::setAudioOutput(QPlatformAudioOutput *output)
511 +{
512 + if (m_audioOutput == output)
513 + return;
514 +
515 + m_audioOutput = output;
516 +}
517 +
518 +void QFFmpegMediaCaptureSession::newVideoFrame(const QVideoFrame &frame)
519 +{
520 + if (m_videoSink)
521 + m_videoSink->setVideoFrame(frame);
522 +}
523 +
524 +
525 +QT_END_NAMESPACE
526 diff --git a/src/plugins/multimedia/v4l2/qffmpegmediacapturesession_p.h b/src/plugins/multimedia/v4l2/qffmpegmediacapturesession_p.h
527 new file mode 100644
528 index 000000000..858a537cc
529 --- /dev/null
530 +++ b/src/plugins/multimedia/v4l2/qffmpegmediacapturesession_p.h
531 @@ -0,0 +1,63 @@
532 +// Copyright (C) 2016 The Qt Company Ltd.
533 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
534 +
535 +#ifndef QFFMPEGMEDIACAPTURESESSION_H
536 +#define QFFMPEGMEDIACAPTURESESSION_H
537 +
538 +//
539 +// W A R N I N G
540 +// -------------
541 +//
542 +// This file is not part of the Qt API. It exists purely as an
543 +// implementation detail. This header file may change from version to
544 +// version without notice, or even be removed.
545 +//
546 +// We mean it.
547 +//
548 +
549 +#include <private/qplatformmediacapture_p.h>
550 +#include <private/qplatformmediaintegration_p.h>
551 +
552 +QT_BEGIN_NAMESPACE
553 +
554 +class QFFmpegMediaRecorder;
555 +class QFFmpegImageCapture;
556 +class QVideoFrame;
557 +
558 +class QFFmpegMediaCaptureSession : public QPlatformMediaCaptureSession
559 +{
560 + Q_OBJECT
561 +
562 +public:
563 + QFFmpegMediaCaptureSession();
564 + virtual ~QFFmpegMediaCaptureSession();
565 +
566 + QPlatformCamera *camera() override;
567 + void setCamera(QPlatformCamera *camera) override;
568 +
569 + QPlatformImageCapture *imageCapture() override;
570 + void setImageCapture(QPlatformImageCapture *imageCapture) override;
571 +
572 + QPlatformMediaRecorder *mediaRecorder() override;
573 + void setMediaRecorder(QPlatformMediaRecorder *recorder) override;
574 +
575 + void setAudioInput(QPlatformAudioInput *input) override;
576 + QPlatformAudioInput *audioInput() { return m_audioInput; }
577 +
578 + void setVideoPreview(QVideoSink *sink) override;
579 + void setAudioOutput(QPlatformAudioOutput *output) override;
580 +
581 +public Q_SLOTS:
582 + void newVideoFrame(const QVideoFrame &frame);
583 +
584 +private:
585 + QPlatformCamera *m_camera = nullptr;
586 + QPlatformAudioInput *m_audioInput = nullptr;
587 + QFFmpegImageCapture *m_imageCapture = nullptr;
588 + QPlatformAudioOutput *m_audioOutput = nullptr;
589 + QVideoSink *m_videoSink = nullptr;
590 +};
591 +
592 +QT_END_NAMESPACE
593 +
594 +#endif // QGSTREAMERCAPTURESERVICE_H
595 diff --git a/src/plugins/multimedia/v4l2/qffmpegmediaformatinfo.cpp b/src/plugins/multimedia/v4l2/qffmpegmediaformatinfo.cpp
596 new file mode 100644
597 index 000000000..00b838d50
598 --- /dev/null
599 +++ b/src/plugins/multimedia/v4l2/qffmpegmediaformatinfo.cpp
600 @@ -0,0 +1,32 @@
601 +// Copyright (C) 2021 The Qt Company Ltd.
602 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
603 +
604 +#include "qffmpegmediaformatinfo_p.h"
605 +#include "qaudioformat.h"
606 +#include "qimagewriter.h"
607 +
608 +QT_BEGIN_NAMESPACE
609 +
610 +QFFmpegMediaFormatInfo::QFFmpegMediaFormatInfo()
611 +{
612 + // Add image formats we support. We currently simply use Qt's built-in image write
613 + // to save images. That doesn't give us HDR support or support for larger bit depths,
614 + // but most cameras can currently not generate those anyway.
615 + const auto imgFormats = QImageWriter::supportedImageFormats();
616 + for (const auto &f : imgFormats) {
617 + if (f == "png")
618 + imageFormats.append(QImageCapture::PNG);
619 + else if (f == "jpeg")
620 + imageFormats.append(QImageCapture::JPEG);
621 + else if (f == "tiff")
622 + imageFormats.append(QImageCapture::Tiff);
623 + else if (f == "webp")
624 + imageFormats.append(QImageCapture::WebP);
625 + }
626 +
627 +}
628 +
629 +QFFmpegMediaFormatInfo::~QFFmpegMediaFormatInfo() = default;
630 +
631 +
632 +QT_END_NAMESPACE
633 diff --git a/src/plugins/multimedia/v4l2/qffmpegmediaformatinfo_p.h b/src/plugins/multimedia/v4l2/qffmpegmediaformatinfo_p.h
634 new file mode 100644
635 index 000000000..e34005bbf
636 --- /dev/null
637 +++ b/src/plugins/multimedia/v4l2/qffmpegmediaformatinfo_p.h
638 @@ -0,0 +1,34 @@
639 +// Copyright (C) 2021 The Qt Company Ltd.
640 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
641 +
642 +#ifndef QFFmpegMediaFormatInfo_H
643 +#define QFFmpegMediaFormatInfo_H
644 +
645 +//
646 +// W A R N I N G
647 +// -------------
648 +//
649 +// This file is not part of the Qt API. It exists purely as an
650 +// implementation detail. This header file may change from version to
651 +// version without notice, or even be removed.
652 +//
653 +// We mean it.
654 +//
655 +
656 +#include <private/qplatformmediaformatinfo_p.h>
657 +#include <qhash.h>
658 +#include <qlist.h>
659 +#include <qaudioformat.h>
660 +
661 +QT_BEGIN_NAMESPACE
662 +
663 +class QFFmpegMediaFormatInfo : public QPlatformMediaFormatInfo
664 +{
665 +public:
666 + QFFmpegMediaFormatInfo();
667 + ~QFFmpegMediaFormatInfo();
668 +};
669 +
670 +QT_END_NAMESPACE
671 +
672 +#endif
673 diff --git a/src/plugins/multimedia/v4l2/qffmpegmediaintegration.cpp b/src/plugins/multimedia/v4l2/qffmpegmediaintegration.cpp
674 new file mode 100644
675 index 000000000..63a8ff196
676 --- /dev/null
677 +++ b/src/plugins/multimedia/v4l2/qffmpegmediaintegration.cpp
678 @@ -0,0 +1,131 @@
679 +// Copyright (C) 2021 The Qt Company Ltd.
680 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
681 +
682 +#include <QtMultimedia/private/qplatformmediaplugin_p.h>
683 +#include <qcameradevice.h>
684 +#include "qffmpegmediaintegration_p.h"
685 +#include "qffmpegmediaformatinfo_p.h"
686 +#include "qffmpegvideosink_p.h"
687 +#include "qffmpegmediacapturesession_p.h"
688 +#include "qffmpegimagecapture_p.h"
689 +
690 +#ifdef Q_OS_MACOS
691 +#include <VideoToolbox/VideoToolbox.h>
692 +#endif
693 +
694 +#ifdef Q_OS_DARWIN
695 +#include "qavfcamera_p.h"
696 +#elif defined(Q_OS_WINDOWS)
697 +#include "../windows/mediacapture/qwindowscamera_p.h"
698 +#include "../windows/qwindowsvideodevices_p.h"
699 +#endif
700 +
701 +#if QT_CONFIG(linux_v4l)
702 +#include "qv4l2camera_p.h"
703 +#include "qv4l2cameradevices_p.h"
704 +#endif
705 +
706 +QT_BEGIN_NAMESPACE
707 +
708 +class QFFmpegMediaPlugin : public QPlatformMediaPlugin
709 +{
710 + Q_OBJECT
711 + Q_PLUGIN_METADATA(IID QPlatformMediaPlugin_iid FILE "ffmpeg.json")
712 +
713 +public:
714 + QFFmpegMediaPlugin()
715 + : QPlatformMediaPlugin()
716 + {}
717 +
718 + QPlatformMediaIntegration* create(const QString &name) override
719 + {
720 + if (name == QLatin1String("ffmpeg"))
721 + return new QFFmpegMediaIntegration;
722 + return nullptr;
723 + }
724 +};
725 +
726 +QFFmpegMediaIntegration::QFFmpegMediaIntegration()
727 + : QPlatformMediaIntegration(QLatin1String("ffmpeg"))
728 +{
729 +#ifndef QT_NO_DEBUG
730 + qDebug() << "Available HW decoding frameworks:";
731 + AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
732 + while ((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
733 + qDebug() << " " << av_hwdevice_get_type_name(type);
734 +#endif
735 +}
736 +
737 +QPlatformMediaFormatInfo *QFFmpegMediaIntegration::createFormatInfo()
738 +{
739 + return new QFFmpegMediaFormatInfo();
740 +}
741 +
742 +QPlatformVideoDevices *QFFmpegMediaIntegration::createVideoDevices()
743 +{
744 +#if defined(Q_OS_ANDROID)
745 + return new QAndroidVideoDevices(this);
746 +#elif QT_CONFIG(linux_v4l)
747 + return new QV4L2CameraDevices(this);
748 +#elif defined Q_OS_DARWIN
749 + return new QAVFVideoDevices(this);
750 +#elif defined(Q_OS_WINDOWS)
751 + return new QWindowsVideoDevices(this);
752 +#else
753 + return nullptr;
754 +#endif
755 +}
756 +
757 +QMaybe<QPlatformMediaCaptureSession *> QFFmpegMediaIntegration::createCaptureSession()
758 +{
759 + return new QFFmpegMediaCaptureSession();
760 +}
761 +
762 +QMaybe<QPlatformCamera *> QFFmpegMediaIntegration::createCamera(QCamera *camera)
763 +{
764 +#ifdef Q_OS_DARWIN
765 + return new QAVFCamera(camera);
766 +#elif QT_CONFIG(linux_v4l)
767 + return new QV4L2Camera(camera);
768 +#elif defined(Q_OS_WINDOWS)
769 + return new QWindowsCamera(camera);
770 +#else
771 + Q_UNUSED(camera);
772 + return nullptr;//new QFFmpegCamera(camera);
773 +#endif
774 +}
775 +
776 +QMaybe<QPlatformImageCapture *> QFFmpegMediaIntegration::createImageCapture(QImageCapture *imageCapture)
777 +{
778 + return new QFFmpegImageCapture(imageCapture);
779 +}
780 +
781 +QMaybe<QPlatformVideoSink *> QFFmpegMediaIntegration::createVideoSink(QVideoSink *sink)
782 +{
783 + return new QFFmpegVideoSink(sink);
784 +}
785 +
786 +#ifdef Q_OS_ANDROID
787 +Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void * /*reserved*/)
788 +{
789 + static bool initialized = false;
790 + if (initialized)
791 + return JNI_VERSION_1_6;
792 + initialized = true;
793 +
794 + QT_USE_NAMESPACE
795 + void *environment;
796 + if (vm->GetEnv(&environment, JNI_VERSION_1_6))
797 + return JNI_ERR;
798 +
799 + // setting our javavm into ffmpeg.
800 + if (av_jni_set_java_vm(vm, nullptr))
801 + return JNI_ERR;
802 +
803 + return JNI_VERSION_1_6;
804 +}
805 +#endif
806 +
807 +QT_END_NAMESPACE
808 +
809 +#include "qffmpegmediaintegration.moc"
810 diff --git a/src/plugins/multimedia/v4l2/qffmpegmediaintegration_p.h b/src/plugins/multimedia/v4l2/qffmpegmediaintegration_p.h
811 new file mode 100644
812 index 000000000..13b81ecc2
813 --- /dev/null
814 +++ b/src/plugins/multimedia/v4l2/qffmpegmediaintegration_p.h
815 @@ -0,0 +1,47 @@
816 +// Copyright (C) 2021 The Qt Company Ltd.
817 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
818 +
819 +#ifndef QGSTREAMERINTEGRATION_H
820 +#define QGSTREAMERINTEGRATION_H
821 +
822 +//
823 +// W A R N I N G
824 +// -------------
825 +//
826 +// This file is not part of the Qt API. It exists purely as an
827 +// implementation detail. This header file may change from version to
828 +// version without notice, or even be removed.
829 +//
830 +// We mean it.
831 +//
832 +
833 +#include <private/qplatformmediaintegration_p.h>
834 +
835 +QT_BEGIN_NAMESPACE
836 +
837 +class QFFmpegMediaFormatInfo;
838 +
839 +class QFFmpegMediaIntegration : public QPlatformMediaIntegration
840 +{
841 +public:
842 + QFFmpegMediaIntegration();
843 +
844 + static QFFmpegMediaIntegration *instance()
845 + {
846 + return static_cast<QFFmpegMediaIntegration *>(QPlatformMediaIntegration::instance());
847 + }
848 +
849 + QMaybe<QPlatformMediaCaptureSession *> createCaptureSession() override;
850 + QMaybe<QPlatformCamera *> createCamera(QCamera *) override;
851 + QMaybe<QPlatformImageCapture *> createImageCapture(QImageCapture *) override;
852 + QMaybe<QPlatformVideoSink *> createVideoSink(QVideoSink *sink) override;
853 +
854 +protected:
855 + QPlatformMediaFormatInfo *createFormatInfo() override;
856 +
857 + QPlatformVideoDevices *createVideoDevices() override;
858 +};
859 +
860 +QT_END_NAMESPACE
861 +
862 +#endif
863 diff --git a/src/plugins/multimedia/v4l2/qffmpegmediametadata.cpp b/src/plugins/multimedia/v4l2/qffmpegmediametadata.cpp
864 new file mode 100644
865 index 000000000..dda577d44
866 --- /dev/null
867 +++ b/src/plugins/multimedia/v4l2/qffmpegmediametadata.cpp
868 @@ -0,0 +1,72 @@
869 +// Copyright (C) 2022 The Qt Company Ltd.
870 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
871 +
872 +#include "qffmpegmediametadata_p.h"
873 +#include <QDebug>
874 +#include <QtCore/qdatetime.h>
875 +#include <qstringlist.h>
876 +#include <qurl.h>
877 +#include <qlocale.h>
878 +
879 +QT_BEGIN_NAMESPACE
880 +
881 +namespace {
882 +
883 +struct {
884 + const char *tag;
885 + QMediaMetaData::Key key;
886 +} ffmpegTagToMetaDataKey[] = {
887 + { "title", QMediaMetaData::Title },
888 + { "comment", QMediaMetaData::Comment },
889 + { "description", QMediaMetaData::Description },
890 + { "genre", QMediaMetaData::Genre },
891 + { "date", QMediaMetaData::Date },
892 + { "year", QMediaMetaData::Date },
893 + { "creation_time", QMediaMetaData::Date },
894 +
895 + { "language", QMediaMetaData::Language },
896 +
897 + { "copyright", QMediaMetaData::Copyright },
898 +
899 + // Music
900 + { "album", QMediaMetaData::AlbumTitle },
901 + { "album_artist", QMediaMetaData::AlbumArtist },
902 + { "artist", QMediaMetaData::ContributingArtist },
903 + { "track", QMediaMetaData::TrackNumber },
904 +
905 + // Movie
906 + { "performer", QMediaMetaData::LeadPerformer },
907 +
908 + { nullptr, QMediaMetaData::Title }
909 +};
910 +
911 +}
912 +
913 +static QMediaMetaData::Key tagToKey(const char *tag)
914 +{
915 + auto *map = ffmpegTagToMetaDataKey;
916 + while (map->tag) {
917 + if (!strcmp(map->tag, tag))
918 + return map->key;
919 + ++map;
920 + }
921 + return QMediaMetaData::Key(-1);
922 +}
923 +
924 +static const char *keyToTag(QMediaMetaData::Key key)
925 +{
926 + auto *map = ffmpegTagToMetaDataKey;
927 + while (map->tag) {
928 + if (map->key == key)
929 + return map->tag;
930 + ++map;
931 + }
932 + return nullptr;
933 +}
934 +
935 +QByteArray QFFmpegMetaData::value(const QMediaMetaData &metaData, QMediaMetaData::Key key)
936 +{
937 + return {};
938 +}
939 +
940 +QT_END_NAMESPACE
941 diff --git a/src/plugins/multimedia/v4l2/qffmpegmediametadata_p.h b/src/plugins/multimedia/v4l2/qffmpegmediametadata_p.h
942 new file mode 100644
943 index 000000000..95b069b64
944 --- /dev/null
945 +++ b/src/plugins/multimedia/v4l2/qffmpegmediametadata_p.h
946 @@ -0,0 +1,30 @@
947 +// Copyright (C) 2022 The Qt Company Ltd.
948 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
949 +
950 +#ifndef QFFMPEGMEDIAMETADATA_H
951 +#define QFFMPEGMEDIAMETADATA_H
952 +
953 +//
954 +// W A R N I N G
955 +// -------------
956 +//
957 +// This file is not part of the Qt API. It exists purely as an
958 +// implementation detail. This header file may change from version to
959 +// version without notice, or even be removed.
960 +//
961 +// We mean it.
962 +//
963 +
964 +#include <qmediametadata.h>
965 +
966 +QT_BEGIN_NAMESPACE
967 +
968 +class QFFmpegMetaData : public QMediaMetaData
969 +{
970 +public:
971 + static QByteArray value(const QMediaMetaData &metaData, QMediaMetaData::Key key);
972 +};
973 +
974 +QT_END_NAMESPACE
975 +
976 +#endif // QFFMPEGMEDIAMETADATA_H
977 diff --git a/src/plugins/multimedia/v4l2/qffmpegvideosink.cpp b/src/plugins/multimedia/v4l2/qffmpegvideosink.cpp
978 new file mode 100644
979 index 000000000..128ce2f56
980 --- /dev/null
981 +++ b/src/plugins/multimedia/v4l2/qffmpegvideosink.cpp
982 @@ -0,0 +1,17 @@
983 +// Copyright (C) 2021 The Qt Company Ltd.
984 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
985 +#include <qffmpegvideosink_p.h>
986 +
987 +QT_BEGIN_NAMESPACE
988 +
989 +QFFmpegVideoSink::QFFmpegVideoSink(QVideoSink *sink)
990 + : QPlatformVideoSink(sink)
991 +{
992 +}
993 +
994 +void QFFmpegVideoSink::onVideoFrameChanged(const QVideoFrame &frame)
995 +{
996 +
997 +}
998 +
999 +QT_END_NAMESPACE
1000 diff --git a/src/plugins/multimedia/v4l2/qffmpegvideosink_p.h b/src/plugins/multimedia/v4l2/qffmpegvideosink_p.h
1001 new file mode 100644
1002 index 000000000..9eac79129
1003 --- /dev/null
1004 +++ b/src/plugins/multimedia/v4l2/qffmpegvideosink_p.h
1005 @@ -0,0 +1,40 @@
1006 +// Copyright (C) 2021 The Qt Company Ltd.
1007 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
1008 +
1009 +#ifndef QFFMPEGVIDEOSINK_H
1010 +#define QFFMPEGVIDEOSINK_H
1011 +
1012 +//
1013 +// W A R N I N G
1014 +// -------------
1015 +//
1016 +// This file is not part of the Qt API. It exists purely as an
1017 +// implementation detail. This header file may change from version to
1018 +// version without notice, or even be removed.
1019 +//
1020 +// We mean it.
1021 +//
1022 +
1023 +#include <private/qplatformvideosink_p.h>
1024 +//#include <qffmpeghwaccel_p.h>
1025 +
1026 +QT_BEGIN_NAMESPACE
1027 +
1028 +// Required for QDoc workaround
1029 +class QString;
1030 +
1031 +class QFFmpegVideoSink : public QPlatformVideoSink
1032 +{
1033 + Q_OBJECT
1034 +
1035 +public:
1036 + QFFmpegVideoSink(QVideoSink *sink);
1037 +
1038 +protected:
1039 + void onVideoFrameChanged(const QVideoFrame &frame) override;
1040 +};
1041 +
1042 +QT_END_NAMESPACE
1043 +
1044 +
1045 +#endif
1046 diff --git a/src/plugins/multimedia/v4l2/qv4l2camera.cpp b/src/plugins/multimedia/v4l2/qv4l2camera.cpp
1047 new file mode 100644
1048 index 000000000..1ba05364d
1049 --- /dev/null
1050 +++ b/src/plugins/multimedia/v4l2/qv4l2camera.cpp
1051 @@ -0,0 +1,707 @@
1052 +// Copyright (C) 2021 The Qt Company Ltd.
1053 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
1054 +
1055 +#include "qv4l2camera_p.h"
1056 +#include "qv4l2filedescriptor_p.h"
1057 +#include "qv4l2memorytransfer_p.h"
1058 +
1059 +#include <private/qcameradevice_p.h>
1060 +#include <private/qmultimediautils_p.h>
1061 +#include <private/qmemoryvideobuffer_p.h>
1062 +#include <private/qcore_unix_p.h>
1063 +
1064 +#include <qsocketnotifier.h>
1065 +#include <qloggingcategory.h>
1066 +
1067 +QT_BEGIN_NAMESPACE
1068 +
1069 +static Q_LOGGING_CATEGORY(qLcV4L2Camera, "qt.multimedia.ffmpeg.v4l2camera");
1070 +
1071 +static const struct {
1072 + QVideoFrameFormat::PixelFormat fmt;
1073 + uint32_t v4l2Format;
1074 +} formatMap[] = {
1075 + // ### How do we handle V4L2_PIX_FMT_H264 and V4L2_PIX_FMT_MPEG4?
1076 + { QVideoFrameFormat::Format_YUV420P, V4L2_PIX_FMT_YUV420 },
1077 + { QVideoFrameFormat::Format_YUV422P, V4L2_PIX_FMT_YUV422P },
1078 + { QVideoFrameFormat::Format_YUYV, V4L2_PIX_FMT_YUYV },
1079 + { QVideoFrameFormat::Format_UYVY, V4L2_PIX_FMT_UYVY },
1080 + { QVideoFrameFormat::Format_XBGR8888, V4L2_PIX_FMT_XBGR32 },
1081 + { QVideoFrameFormat::Format_XRGB8888, V4L2_PIX_FMT_XRGB32 },
1082 + { QVideoFrameFormat::Format_ABGR8888, V4L2_PIX_FMT_ABGR32 },
1083 + { QVideoFrameFormat::Format_ARGB8888, V4L2_PIX_FMT_ARGB32 },
1084 + { QVideoFrameFormat::Format_BGRX8888, V4L2_PIX_FMT_BGR32 },
1085 + { QVideoFrameFormat::Format_RGBX8888, V4L2_PIX_FMT_RGB32 },
1086 + { QVideoFrameFormat::Format_BGRA8888, V4L2_PIX_FMT_BGRA32 },
1087 + { QVideoFrameFormat::Format_RGBA8888, V4L2_PIX_FMT_RGBA32 },
1088 + { QVideoFrameFormat::Format_Y8, V4L2_PIX_FMT_GREY },
1089 + { QVideoFrameFormat::Format_Y16, V4L2_PIX_FMT_Y16 },
1090 + { QVideoFrameFormat::Format_NV12, V4L2_PIX_FMT_NV12 },
1091 + { QVideoFrameFormat::Format_NV21, V4L2_PIX_FMT_NV21 },
1092 + { QVideoFrameFormat::Format_Jpeg, V4L2_PIX_FMT_MJPEG },
1093 + { QVideoFrameFormat::Format_Jpeg, V4L2_PIX_FMT_JPEG },
1094 + { QVideoFrameFormat::Format_Invalid, 0 },
1095 +};
1096 +
1097 +QVideoFrameFormat::PixelFormat formatForV4L2Format(uint32_t v4l2Format)
1098 +{
1099 + auto *f = formatMap;
1100 + while (f->v4l2Format) {
1101 + if (f->v4l2Format == v4l2Format)
1102 + return f->fmt;
1103 + ++f;
1104 + }
1105 + return QVideoFrameFormat::Format_Invalid;
1106 +}
1107 +
1108 +uint32_t v4l2FormatForPixelFormat(QVideoFrameFormat::PixelFormat format)
1109 +{
1110 + auto *f = formatMap;
1111 + while (f->v4l2Format) {
1112 + if (f->fmt == format)
1113 + return f->v4l2Format;
1114 + ++f;
1115 + }
1116 + return 0;
1117 +}
1118 +
1119 +QV4L2Camera::QV4L2Camera(QCamera *camera)
1120 + : QPlatformCamera(camera)
1121 +{
1122 +}
1123 +
1124 +QV4L2Camera::~QV4L2Camera()
1125 +{
1126 + stopCapturing();
1127 + closeV4L2Fd();
1128 +}
1129 +
1130 +bool QV4L2Camera::isActive() const
1131 +{
1132 + return m_active;
1133 +}
1134 +
1135 +void QV4L2Camera::setActive(bool active)
1136 +{
1137 + if (m_active == active)
1138 + return;
1139 + if (m_cameraDevice.isNull() && active)
1140 + return;
1141 +
1142 + if (m_cameraFormat.isNull())
1143 + resolveCameraFormat({});
1144 +
1145 + m_active = active;
1146 + if (m_active)
1147 + startCapturing();
1148 + else
1149 + stopCapturing();
1150 +
1151 + emit newVideoFrame({});
1152 +
1153 + emit activeChanged(active);
1154 +}
1155 +
1156 +void QV4L2Camera::setCamera(const QCameraDevice &camera)
1157 +{
1158 + if (m_cameraDevice == camera)
1159 + return;
1160 +
1161 + stopCapturing();
1162 + closeV4L2Fd();
1163 +
1164 + m_cameraDevice = camera;
1165 + resolveCameraFormat({});
1166 +
1167 + initV4L2Controls();
1168 +
1169 + if (m_active)
1170 + startCapturing();
1171 +}
1172 +
1173 +bool QV4L2Camera::setCameraFormat(const QCameraFormat &format)
1174 +{
1175 + if (!format.isNull() && !m_cameraDevice.videoFormats().contains(format))
1176 + return false;
1177 +
1178 + if (!resolveCameraFormat(format))
1179 + return true;
1180 +
1181 + if (m_active) {
1182 + stopCapturing();
1183 + closeV4L2Fd();
1184 +
1185 + initV4L2Controls();
1186 + startCapturing();
1187 + }
1188 +
1189 + return true;
1190 +}
1191 +
1192 +bool QV4L2Camera::resolveCameraFormat(const QCameraFormat &format)
1193 +{
1194 + auto fmt = format;
1195 + if (fmt.isNull())
1196 + fmt = findBestCameraFormat(m_cameraDevice);
1197 +
1198 + if (fmt == m_cameraFormat)
1199 + return false;
1200 +
1201 + m_cameraFormat = fmt;
1202 + return true;
1203 +}
1204 +
1205 +void QV4L2Camera::setFocusMode(QCamera::FocusMode mode)
1206 +{
1207 + if (mode == focusMode())
1208 + return;
1209 +
1210 + bool focusDist = supportedFeatures() & QCamera::Feature::FocusDistance;
1211 + if (!focusDist && !m_v4l2Info.rangedFocus)
1212 + return;
1213 +
1214 + switch (mode) {
1215 + default:
1216 + case QCamera::FocusModeAuto:
1217 + setV4L2Parameter(V4L2_CID_FOCUS_AUTO, 1);
1218 + if (m_v4l2Info.rangedFocus)
1219 + setV4L2Parameter(V4L2_CID_AUTO_FOCUS_RANGE, V4L2_AUTO_FOCUS_RANGE_AUTO);
1220 + break;
1221 + case QCamera::FocusModeAutoNear:
1222 + setV4L2Parameter(V4L2_CID_FOCUS_AUTO, 1);
1223 + if (m_v4l2Info.rangedFocus)
1224 + setV4L2Parameter(V4L2_CID_AUTO_FOCUS_RANGE, V4L2_AUTO_FOCUS_RANGE_MACRO);
1225 + else if (focusDist)
1226 + setV4L2Parameter(V4L2_CID_FOCUS_ABSOLUTE, m_v4l2Info.minFocus);
1227 + break;
1228 + case QCamera::FocusModeAutoFar:
1229 + setV4L2Parameter(V4L2_CID_FOCUS_AUTO, 1);
1230 + if (m_v4l2Info.rangedFocus)
1231 + setV4L2Parameter(V4L2_CID_AUTO_FOCUS_RANGE, V4L2_AUTO_FOCUS_RANGE_INFINITY);
1232 + break;
1233 + case QCamera::FocusModeInfinity:
1234 + setV4L2Parameter(V4L2_CID_FOCUS_AUTO, 0);
1235 + setV4L2Parameter(V4L2_CID_FOCUS_ABSOLUTE, m_v4l2Info.maxFocus);
1236 + break;
1237 + case QCamera::FocusModeManual:
1238 + setV4L2Parameter(V4L2_CID_FOCUS_AUTO, 0);
1239 + setFocusDistance(focusDistance());
1240 + break;
1241 + }
1242 + focusModeChanged(mode);
1243 +}
1244 +
1245 +void QV4L2Camera::setFocusDistance(float d)
1246 +{
1247 + int distance = m_v4l2Info.minFocus + int((m_v4l2Info.maxFocus - m_v4l2Info.minFocus) * d);
1248 + setV4L2Parameter(V4L2_CID_FOCUS_ABSOLUTE, distance);
1249 + focusDistanceChanged(d);
1250 +}
1251 +
1252 +void QV4L2Camera::zoomTo(float factor, float)
1253 +{
1254 + if (m_v4l2Info.maxZoom == m_v4l2Info.minZoom)
1255 + return;
1256 + factor = qBound(1., factor, 2.);
1257 + int zoom = m_v4l2Info.minZoom + (factor - 1.) * (m_v4l2Info.maxZoom - m_v4l2Info.minZoom);
1258 + setV4L2Parameter(V4L2_CID_ZOOM_ABSOLUTE, zoom);
1259 + zoomFactorChanged(factor);
1260 +}
1261 +
1262 +bool QV4L2Camera::isFocusModeSupported(QCamera::FocusMode mode) const
1263 +{
1264 + if (supportedFeatures() & QCamera::Feature::FocusDistance &&
1265 + (mode == QCamera::FocusModeManual || mode == QCamera::FocusModeAutoNear || mode == QCamera::FocusModeInfinity))
1266 + return true;
1267 +
1268 + return mode == QCamera::FocusModeAuto;
1269 +}
1270 +
1271 +void QV4L2Camera::setFlashMode(QCamera::FlashMode mode)
1272 +{
1273 + if (!m_v4l2Info.flashSupported || mode == QCamera::FlashOn)
1274 + return;
1275 + setV4L2Parameter(V4L2_CID_FLASH_LED_MODE, mode == QCamera::FlashAuto ? V4L2_FLASH_LED_MODE_FLASH : V4L2_FLASH_LED_MODE_NONE);
1276 + flashModeChanged(mode);
1277 +}
1278 +
1279 +bool QV4L2Camera::isFlashModeSupported(QCamera::FlashMode mode) const
1280 +{
1281 + if (m_v4l2Info.flashSupported && mode == QCamera::FlashAuto)
1282 + return true;
1283 + return mode == QCamera::FlashOff;
1284 +}
1285 +
1286 +bool QV4L2Camera::isFlashReady() const
1287 +{
1288 + struct v4l2_queryctrl queryControl;
1289 + ::memset(&queryControl, 0, sizeof(queryControl));
1290 + queryControl.id = V4L2_CID_AUTO_WHITE_BALANCE;
1291 +
1292 + return m_v4l2FileDescriptor && m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, &queryControl);
1293 +}
1294 +
1295 +void QV4L2Camera::setTorchMode(QCamera::TorchMode mode)
1296 +{
1297 + if (!m_v4l2Info.torchSupported || mode == QCamera::TorchOn)
1298 + return;
1299 + setV4L2Parameter(V4L2_CID_FLASH_LED_MODE, mode == QCamera::TorchOn ? V4L2_FLASH_LED_MODE_TORCH : V4L2_FLASH_LED_MODE_NONE);
1300 + torchModeChanged(mode);
1301 +}
1302 +
1303 +bool QV4L2Camera::isTorchModeSupported(QCamera::TorchMode mode) const
1304 +{
1305 + if (mode == QCamera::TorchOn)
1306 + return m_v4l2Info.torchSupported;
1307 + return mode == QCamera::TorchOff;
1308 +}
1309 +
1310 +void QV4L2Camera::setExposureMode(QCamera::ExposureMode mode)
1311 +{
1312 + if (m_v4l2Info.autoExposureSupported && m_v4l2Info.manualExposureSupported) {
1313 + if (mode != QCamera::ExposureAuto && mode != QCamera::ExposureManual)
1314 + return;
1315 + int value = QCamera::ExposureAuto ? V4L2_EXPOSURE_AUTO : V4L2_EXPOSURE_MANUAL;
1316 + setV4L2Parameter(V4L2_CID_EXPOSURE_AUTO, value);
1317 + exposureModeChanged(mode);
1318 + return;
1319 + }
1320 +}
1321 +
1322 +bool QV4L2Camera::isExposureModeSupported(QCamera::ExposureMode mode) const
1323 +{
1324 + if (mode == QCamera::ExposureAuto)
1325 + return true;
1326 + if (m_v4l2Info.manualExposureSupported && m_v4l2Info.autoExposureSupported)
1327 + return mode == QCamera::ExposureManual;
1328 + return false;
1329 +}
1330 +
1331 +void QV4L2Camera::setExposureCompensation(float compensation)
1332 +{
1333 + if ((m_v4l2Info.minExposureAdjustment != 0 || m_v4l2Info.maxExposureAdjustment != 0)) {
1334 + int value = qBound(m_v4l2Info.minExposureAdjustment, (int)(compensation * 1000),
1335 + m_v4l2Info.maxExposureAdjustment);
1336 + setV4L2Parameter(V4L2_CID_AUTO_EXPOSURE_BIAS, value);
1337 + exposureCompensationChanged(value/1000.);
1338 + return;
1339 + }
1340 +}
1341 +
1342 +void QV4L2Camera::setManualIsoSensitivity(int iso)
1343 +{
1344 + if (!(supportedFeatures() & QCamera::Feature::IsoSensitivity))
1345 + return;
1346 + setV4L2Parameter(V4L2_CID_ISO_SENSITIVITY_AUTO, iso <= 0 ? V4L2_ISO_SENSITIVITY_AUTO : V4L2_ISO_SENSITIVITY_MANUAL);
1347 + if (iso > 0) {
1348 + iso = qBound(minIso(), iso, maxIso());
1349 + setV4L2Parameter(V4L2_CID_ISO_SENSITIVITY, iso);
1350 + }
1351 + return;
1352 +}
1353 +
1354 +int QV4L2Camera::isoSensitivity() const
1355 +{
1356 + if (!(supportedFeatures() & QCamera::Feature::IsoSensitivity))
1357 + return -1;
1358 + return getV4L2Parameter(V4L2_CID_ISO_SENSITIVITY);
1359 +}
1360 +
1361 +void QV4L2Camera::setManualExposureTime(float secs)
1362 +{
1363 + if (m_v4l2Info.manualExposureSupported && m_v4l2Info.autoExposureSupported) {
1364 + int exposure =
1365 + qBound(m_v4l2Info.minExposure, qRound(secs * 10000.), m_v4l2Info.maxExposure);
1366 + setV4L2Parameter(V4L2_CID_EXPOSURE_ABSOLUTE, exposure);
1367 + exposureTimeChanged(exposure/10000.);
1368 + return;
1369 + }
1370 +}
1371 +
1372 +float QV4L2Camera::exposureTime() const
1373 +{
1374 + return getV4L2Parameter(V4L2_CID_EXPOSURE_ABSOLUTE)/10000.;
1375 +}
1376 +
1377 +bool QV4L2Camera::isWhiteBalanceModeSupported(QCamera::WhiteBalanceMode mode) const
1378 +{
1379 + if (m_v4l2Info.autoWhiteBalanceSupported && m_v4l2Info.colorTemperatureSupported)
1380 + return true;
1381 +
1382 + return mode == QCamera::WhiteBalanceAuto;
1383 +}
1384 +
1385 +void QV4L2Camera::setWhiteBalanceMode(QCamera::WhiteBalanceMode mode)
1386 +{
1387 + Q_ASSERT(isWhiteBalanceModeSupported(mode));
1388 +
1389 + int temperature = colorTemperatureForWhiteBalance(mode);
1390 + int t = setV4L2ColorTemperature(temperature);
1391 + if (t == 0)
1392 + mode = QCamera::WhiteBalanceAuto;
1393 + whiteBalanceModeChanged(mode);
1394 +}
1395 +
1396 +void QV4L2Camera::setColorTemperature(int temperature)
1397 +{
1398 + if (temperature == 0) {
1399 + setWhiteBalanceMode(QCamera::WhiteBalanceAuto);
1400 + return;
1401 + }
1402 +
1403 + Q_ASSERT(isWhiteBalanceModeSupported(QCamera::WhiteBalanceManual));
1404 +
1405 + int t = setV4L2ColorTemperature(temperature);
1406 + if (t)
1407 + colorTemperatureChanged(t);
1408 +}
1409 +
1410 +void QV4L2Camera::readFrame()
1411 +{
1412 + Q_ASSERT(m_memoryTransfer);
1413 +
1414 + auto buffer = m_memoryTransfer->dequeueBuffer();
1415 + if (!buffer) {
1416 + qCWarning(qLcV4L2Camera) << "Cannot take buffer";
1417 +
1418 + if (errno == ENODEV) {
1419 + // camera got removed while being active
1420 + stopCapturing();
1421 + closeV4L2Fd();
1422 + }
1423 +
1424 + return;
1425 + }
1426 +
1427 + auto videoBuffer = new QMemoryVideoBuffer(buffer->data, m_bytesPerLine);
1428 + QVideoFrame frame(videoBuffer, frameFormat());
1429 +
1430 + auto &v4l2Buffer = buffer->v4l2Buffer;
1431 +
1432 + if (m_firstFrameTime.tv_sec == -1)
1433 + m_firstFrameTime = v4l2Buffer.timestamp;
1434 + qint64 secs = v4l2Buffer.timestamp.tv_sec - m_firstFrameTime.tv_sec;
1435 + qint64 usecs = v4l2Buffer.timestamp.tv_usec - m_firstFrameTime.tv_usec;
1436 + frame.setStartTime(secs*1000000 + usecs);
1437 + frame.setEndTime(frame.startTime() + m_frameDuration);
1438 +
1439 + emit newVideoFrame(frame);
1440 +
1441 + if (!m_memoryTransfer->enqueueBuffer(v4l2Buffer.index))
1442 + qCWarning(qLcV4L2Camera) << "Cannot add buffer";
1443 +}
1444 +
1445 +void QV4L2Camera::setCameraBusy()
1446 +{
1447 + m_cameraBusy = true;
1448 + updateError(QCamera::CameraError, QLatin1String("Camera is in use"));
1449 +}
1450 +
1451 +void QV4L2Camera::initV4L2Controls()
1452 +{
1453 + m_v4l2Info = {};
1454 + QCamera::Features features;
1455 +
1456 + const QByteArray deviceName = m_cameraDevice.id();
1457 + Q_ASSERT(!deviceName.isEmpty());
1458 +
1459 + closeV4L2Fd();
1460 +
1461 + const int descriptor = qt_safe_open(deviceName.constData(), O_RDWR);
1462 + if (descriptor == -1) {
1463 + qCWarning(qLcV4L2Camera) << "Unable to open the camera" << deviceName
1464 + << "for read to query the parameter info:"
1465 + << qt_error_string(errno);
1466 + updateError(QCamera::CameraError, QLatin1String("Cannot open camera"));
1467 + return;
1468 + }
1469 +
1470 + m_v4l2FileDescriptor = std::make_shared<QV4L2FileDescriptor>(descriptor);
1471 +
1472 + qCDebug(qLcV4L2Camera) << "FD=" << descriptor;
1473 +
1474 + struct v4l2_queryctrl queryControl;
1475 + ::memset(&queryControl, 0, sizeof(queryControl));
1476 + queryControl.id = V4L2_CID_AUTO_WHITE_BALANCE;
1477 +
1478 + if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, &queryControl)) {
1479 + m_v4l2Info.autoWhiteBalanceSupported = true;
1480 + setV4L2Parameter(V4L2_CID_AUTO_WHITE_BALANCE, true);
1481 + }
1482 +
1483 + ::memset(&queryControl, 0, sizeof(queryControl));
1484 + queryControl.id = V4L2_CID_WHITE_BALANCE_TEMPERATURE;
1485 + if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, &queryControl)) {
1486 + m_v4l2Info.minColorTemp = queryControl.minimum;
1487 + m_v4l2Info.maxColorTemp = queryControl.maximum;
1488 + m_v4l2Info.colorTemperatureSupported = true;
1489 + features |= QCamera::Feature::ColorTemperature;
1490 + }
1491 +
1492 + ::memset(&queryControl, 0, sizeof(queryControl));
1493 + queryControl.id = V4L2_CID_EXPOSURE_AUTO;
1494 + if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, &queryControl)) {
1495 + m_v4l2Info.autoExposureSupported = true;
1496 + }
1497 +
1498 + ::memset(&queryControl, 0, sizeof(queryControl));
1499 + queryControl.id = V4L2_CID_EXPOSURE_ABSOLUTE;
1500 + if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, &queryControl)) {
1501 + m_v4l2Info.manualExposureSupported = true;
1502 + m_v4l2Info.minExposure = queryControl.minimum;
1503 + m_v4l2Info.maxExposure = queryControl.maximum;
1504 + features |= QCamera::Feature::ManualExposureTime;
1505 + }
1506 +
1507 + ::memset(&queryControl, 0, sizeof(queryControl));
1508 + queryControl.id = V4L2_CID_AUTO_EXPOSURE_BIAS;
1509 + if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, &queryControl)) {
1510 + m_v4l2Info.minExposureAdjustment = queryControl.minimum;
1511 + m_v4l2Info.maxExposureAdjustment = queryControl.maximum;
1512 + features |= QCamera::Feature::ExposureCompensation;
1513 + }
1514 +
1515 + ::memset(&queryControl, 0, sizeof(queryControl));
1516 + queryControl.id = V4L2_CID_ISO_SENSITIVITY_AUTO;
1517 + if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, &queryControl)) {
1518 + queryControl.id = V4L2_CID_ISO_SENSITIVITY;
1519 + if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, &queryControl)) {
1520 + features |= QCamera::Feature::IsoSensitivity;
1521 + minIsoChanged(queryControl.minimum);
1522 + maxIsoChanged(queryControl.minimum);
1523 + }
1524 + }
1525 +
1526 + ::memset(&queryControl, 0, sizeof(queryControl));
1527 + queryControl.id = V4L2_CID_FOCUS_ABSOLUTE;
1528 + if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, &queryControl)) {
1529 + m_v4l2Info.minExposureAdjustment = queryControl.minimum;
1530 + m_v4l2Info.maxExposureAdjustment = queryControl.maximum;
1531 + features |= QCamera::Feature::FocusDistance;
1532 + }
1533 +
1534 + ::memset(&queryControl, 0, sizeof(queryControl));
1535 + queryControl.id = V4L2_CID_AUTO_FOCUS_RANGE;
1536 + if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, &queryControl)) {
1537 + m_v4l2Info.rangedFocus = true;
1538 + }
1539 +
1540 + ::memset(&queryControl, 0, sizeof(queryControl));
1541 + queryControl.id = V4L2_CID_FLASH_LED_MODE;
1542 + if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, &queryControl)) {
1543 + m_v4l2Info.flashSupported = queryControl.minimum <= V4L2_FLASH_LED_MODE_FLASH
1544 + && queryControl.maximum >= V4L2_FLASH_LED_MODE_FLASH;
1545 + m_v4l2Info.torchSupported = queryControl.minimum <= V4L2_FLASH_LED_MODE_TORCH
1546 + && queryControl.maximum >= V4L2_FLASH_LED_MODE_TORCH;
1547 + }
1548 +
1549 + ::memset(&queryControl, 0, sizeof(queryControl));
1550 + queryControl.id = V4L2_CID_ZOOM_ABSOLUTE;
1551 + if (m_v4l2FileDescriptor->call(VIDIOC_QUERYCTRL, &queryControl)) {
1552 + m_v4l2Info.minZoom = queryControl.minimum;
1553 + m_v4l2Info.maxZoom = queryControl.maximum;
1554 + }
1555 + // zoom factors are in arbitrary units, so we simply normalize them to go from 1 to 2
1556 + // if they are different
1557 + minimumZoomFactorChanged(1);
1558 + maximumZoomFactorChanged(m_v4l2Info.minZoom != m_v4l2Info.maxZoom ? 2 : 1);
1559 +
1560 + supportedFeaturesChanged(features);
1561 +}
1562 +
1563 +void QV4L2Camera::closeV4L2Fd()
1564 +{
1565 + Q_ASSERT(!m_memoryTransfer);
1566 +
1567 + m_v4l2Info = {};
1568 + m_cameraBusy = false;
1569 + m_v4l2FileDescriptor = nullptr;
1570 +}
1571 +
1572 +int QV4L2Camera::setV4L2ColorTemperature(int temperature)
1573 +{
1574 + struct v4l2_control control;
1575 + ::memset(&control, 0, sizeof(control));
1576 +
1577 + if (m_v4l2Info.autoWhiteBalanceSupported) {
1578 + setV4L2Parameter(V4L2_CID_AUTO_WHITE_BALANCE, temperature == 0 ? true : false);
1579 + } else if (temperature == 0) {
1580 + temperature = 5600;
1581 + }
1582 +
1583 + if (temperature != 0 && m_v4l2Info.colorTemperatureSupported) {
1584 + temperature = qBound(m_v4l2Info.minColorTemp, temperature, m_v4l2Info.maxColorTemp);
1585 + if (!setV4L2Parameter(
1586 + V4L2_CID_WHITE_BALANCE_TEMPERATURE,
1587 + qBound(m_v4l2Info.minColorTemp, temperature, m_v4l2Info.maxColorTemp)))
1588 + temperature = 0;
1589 + } else {
1590 + temperature = 0;
1591 + }
1592 +
1593 + return temperature;
1594 +}
1595 +
1596 +bool QV4L2Camera::setV4L2Parameter(quint32 id, qint32 value)
1597 +{
1598 + v4l2_control control{ id, value };
1599 + if (!m_v4l2FileDescriptor->call(VIDIOC_S_CTRL, &control)) {
1600 + qWarning() << "Unable to set the V4L2 Parameter" << Qt::hex << id << "to" << value << qt_error_string(errno);
1601 + return false;
1602 + }
1603 + return true;
1604 +}
1605 +
1606 +int QV4L2Camera::getV4L2Parameter(quint32 id) const
1607 +{
1608 + struct v4l2_control control{id, 0};
1609 + if (!m_v4l2FileDescriptor->call(VIDIOC_G_CTRL, &control)) {
1610 + qWarning() << "Unable to get the V4L2 Parameter" << Qt::hex << id << qt_error_string(errno);
1611 + return 0;
1612 + }
1613 + return control.value;
1614 +}
1615 +
1616 +void QV4L2Camera::setV4L2CameraFormat()
1617 +{
1618 + if (m_v4l2Info.formatInitialized || !m_v4l2FileDescriptor)
1619 + return;
1620 +
1621 + Q_ASSERT(!m_cameraFormat.isNull());
1622 + qCDebug(qLcV4L2Camera) << "XXXXX" << this << m_cameraDevice.id() << m_cameraFormat.pixelFormat()
1623 + << m_cameraFormat.resolution();
1624 +
1625 + v4l2_format fmt = {};
1626 + fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1627 +
1628 + auto size = m_cameraFormat.resolution();
1629 + fmt.fmt.pix.width = size.width();
1630 + fmt.fmt.pix.height = size.height();
1631 + fmt.fmt.pix.pixelformat = v4l2FormatForPixelFormat(m_cameraFormat.pixelFormat());
1632 + fmt.fmt.pix.field = V4L2_FIELD_ANY;
1633 +
1634 + qCDebug(qLcV4L2Camera) << "setting camera format to" << size << fmt.fmt.pix.pixelformat;
1635 +
1636 + if (!m_v4l2FileDescriptor->call(VIDIOC_S_FMT, &fmt)) {
1637 + if (errno == EBUSY) {
1638 + setCameraBusy();
1639 + return;
1640 + }
1641 + qWarning() << "Couldn't set video format on v4l2 camera" << strerror(errno);
1642 + }
1643 +
1644 + m_v4l2Info.formatInitialized = true;
1645 + m_cameraBusy = false;
1646 +
1647 + m_bytesPerLine = fmt.fmt.pix.bytesperline;
1648 + m_imageSize = std::max(fmt.fmt.pix.sizeimage, m_bytesPerLine * fmt.fmt.pix.height);
1649 +
1650 + switch (v4l2_colorspace(fmt.fmt.pix.colorspace)) {
1651 + default:
1652 + case V4L2_COLORSPACE_DCI_P3:
1653 + m_colorSpace = QVideoFrameFormat::ColorSpace_Undefined;
1654 + break;
1655 + case V4L2_COLORSPACE_REC709:
1656 + m_colorSpace = QVideoFrameFormat::ColorSpace_BT709;
1657 + break;
1658 + case V4L2_COLORSPACE_JPEG:
1659 + m_colorSpace = QVideoFrameFormat::ColorSpace_AdobeRgb;
1660 + break;
1661 + case V4L2_COLORSPACE_SRGB:
1662 + // ##### is this correct???
1663 + m_colorSpace = QVideoFrameFormat::ColorSpace_BT601;
1664 + break;
1665 + case V4L2_COLORSPACE_BT2020:
1666 + m_colorSpace = QVideoFrameFormat::ColorSpace_BT2020;
1667 + break;
1668 + }
1669 +
1670 + v4l2_streamparm streamParam = {};
1671 + streamParam.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1672 +
1673 + streamParam.parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1674 + auto [num, den] = qRealToFraction(1./m_cameraFormat.maxFrameRate());
1675 + streamParam.parm.capture.timeperframe = { (uint)num, (uint)den };
1676 + m_v4l2FileDescriptor->call(VIDIOC_S_PARM, &streamParam);
1677 +
1678 + m_frameDuration = 1000000 * streamParam.parm.capture.timeperframe.numerator
1679 + / streamParam.parm.capture.timeperframe.denominator;
1680 +}
1681 +
1682 +void QV4L2Camera::initV4L2MemoryTransfer()
1683 +{
1684 + if (m_cameraBusy)
1685 + return;
1686 +
1687 + Q_ASSERT(!m_memoryTransfer);
1688 +
1689 + m_memoryTransfer = makeUserPtrMemoryTransfer(m_v4l2FileDescriptor, m_imageSize);
1690 +
1691 + if (m_memoryTransfer)
1692 + return;
1693 +
1694 + if (errno == EBUSY) {
1695 + setCameraBusy();
1696 + return;
1697 + }
1698 +
1699 + qCDebug(qLcV4L2Camera) << "Cannot init V4L2_MEMORY_USERPTR; trying V4L2_MEMORY_MMAP";
1700 +
1701 + m_memoryTransfer = makeMMapMemoryTransfer(m_v4l2FileDescriptor);
1702 +
1703 + if (!m_memoryTransfer) {
1704 + qCWarning(qLcV4L2Camera) << "Cannot init v4l2 memory transfer," << qt_error_string(errno);
1705 + updateError(QCamera::CameraError, QLatin1String("Cannot init V4L2 memory transfer"));
1706 + }
1707 +}
1708 +
1709 +void QV4L2Camera::stopCapturing()
1710 +{
1711 + if (!m_memoryTransfer || !m_v4l2FileDescriptor)
1712 + return;
1713 +
1714 + m_notifier = nullptr;
1715 +
1716 + if (!m_v4l2FileDescriptor->stopStream()) {
1717 + // TODO: handle the case carefully to avoid possible memory corruption
1718 + if (errno != ENODEV)
1719 + qWarning() << "failed to stop capture";
1720 + }
1721 +
1722 + m_memoryTransfer = nullptr;
1723 + m_cameraBusy = false;
1724 +}
1725 +
1726 +void QV4L2Camera::startCapturing()
1727 +{
1728 + if (!m_v4l2FileDescriptor)
1729 + return;
1730 +
1731 + setV4L2CameraFormat();
1732 + initV4L2MemoryTransfer();
1733 +
1734 + if (m_cameraBusy || !m_memoryTransfer)
1735 + return;
1736 +
1737 + if (!m_v4l2FileDescriptor->startStream()) {
1738 + qWarning() << "Couldn't start v4l2 camera stream";
1739 + return;
1740 + }
1741 +
1742 + m_notifier =
1743 + std::make_unique<QSocketNotifier>(m_v4l2FileDescriptor->get(), QSocketNotifier::Read);
1744 + connect(m_notifier.get(), &QSocketNotifier::activated, this, &QV4L2Camera::readFrame);
1745 +
1746 + m_firstFrameTime = { -1, -1 };
1747 +}
1748 +
1749 +QVideoFrameFormat QV4L2Camera::frameFormat() const
1750 +{
1751 + auto result = QPlatformCamera::frameFormat();
1752 + result.setColorSpace(m_colorSpace);
1753 + return result;
1754 +}
1755 +
1756 +QT_END_NAMESPACE
1757 +
1758 +#include "moc_qv4l2camera_p.cpp"
1759 diff --git a/src/plugins/multimedia/v4l2/qv4l2camera_p.h b/src/plugins/multimedia/v4l2/qv4l2camera_p.h
1760 new file mode 100644
1761 index 000000000..79cc3cfa9
1762 --- /dev/null
1763 +++ b/src/plugins/multimedia/v4l2/qv4l2camera_p.h
1764 @@ -0,0 +1,133 @@
1765 +// Copyright (C) 2021 The Qt Company Ltd.
1766 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
1767 +
1768 +#ifndef QV4L2CAMERA_H
1769 +#define QV4L2CAMERA_H
1770 +
1771 +//
1772 +// W A R N I N G
1773 +// -------------
1774 +//
1775 +// This file is not part of the Qt API. It exists purely as an
1776 +// implementation detail. This header file may change from version to
1777 +// version without notice, or even be removed.
1778 +//
1779 +// We mean it.
1780 +//
1781 +
1782 +#include <private/qplatformcamera_p.h>
1783 +#include <sys/time.h>
1784 +
1785 +QT_BEGIN_NAMESPACE
1786 +
1787 +class QV4L2FileDescriptor;
1788 +class QV4L2MemoryTransfer;
1789 +class QSocketNotifier;
1790 +
1791 +struct V4L2CameraInfo
1792 +{
1793 + bool formatInitialized = false;
1794 +
1795 + bool autoWhiteBalanceSupported = false;
1796 + bool colorTemperatureSupported = false;
1797 + bool autoExposureSupported = false;
1798 + bool manualExposureSupported = false;
1799 + bool flashSupported = false;
1800 + bool torchSupported = false;
1801 + qint32 minColorTemp = 5600; // Daylight...
1802 + qint32 maxColorTemp = 5600;
1803 + qint32 minExposure = 0;
1804 + qint32 maxExposure = 0;
1805 + qint32 minExposureAdjustment = 0;
1806 + qint32 maxExposureAdjustment = 0;
1807 + qint32 minFocus = 0;
1808 + qint32 maxFocus = 0;
1809 + qint32 rangedFocus = false;
1810 +
1811 + int minZoom = 0;
1812 + int maxZoom = 0;
1813 +};
1814 +
1815 +QVideoFrameFormat::PixelFormat formatForV4L2Format(uint32_t v4l2Format);
1816 +uint32_t v4l2FormatForPixelFormat(QVideoFrameFormat::PixelFormat format);
1817 +
1818 +class Q_MULTIMEDIA_EXPORT QV4L2Camera : public QPlatformCamera
1819 +{
1820 + Q_OBJECT
1821 +
1822 +public:
1823 + explicit QV4L2Camera(QCamera *parent);
1824 + ~QV4L2Camera();
1825 +
1826 + bool isActive() const override;
1827 + void setActive(bool active) override;
1828 +
1829 + void setCamera(const QCameraDevice &camera) override;
1830 + bool setCameraFormat(const QCameraFormat &format) override;
1831 + bool resolveCameraFormat(const QCameraFormat &format);
1832 +
1833 + bool isFocusModeSupported(QCamera::FocusMode mode) const override;
1834 + void setFocusMode(QCamera::FocusMode /*mode*/) override;
1835 +
1836 +// void setCustomFocusPoint(const QPointF &/*point*/) override;
1837 + void setFocusDistance(float) override;
1838 + void zoomTo(float /*newZoomFactor*/, float /*rate*/ = -1.) override;
1839 +
1840 + void setFlashMode(QCamera::FlashMode /*mode*/) override;
1841 + bool isFlashModeSupported(QCamera::FlashMode mode) const override;
1842 + bool isFlashReady() const override;
1843 +
1844 + void setTorchMode(QCamera::TorchMode /*mode*/) override;
1845 + bool isTorchModeSupported(QCamera::TorchMode mode) const override;
1846 +
1847 + void setExposureMode(QCamera::ExposureMode) override;
1848 + bool isExposureModeSupported(QCamera::ExposureMode mode) const override;
1849 + void setExposureCompensation(float) override;
1850 + int isoSensitivity() const override;
1851 + void setManualIsoSensitivity(int) override;
1852 + void setManualExposureTime(float) override;
1853 + float exposureTime() const override;
1854 +
1855 + bool isWhiteBalanceModeSupported(QCamera::WhiteBalanceMode mode) const override;
1856 + void setWhiteBalanceMode(QCamera::WhiteBalanceMode /*mode*/) override;
1857 + void setColorTemperature(int /*temperature*/) override;
1858 +
1859 + QVideoFrameFormat frameFormat() const override;
1860 +
1861 +private Q_SLOTS:
1862 + void readFrame();
1863 +
1864 +private:
1865 + void setCameraBusy();
1866 + void initV4L2Controls();
1867 + void closeV4L2Fd();
1868 + int setV4L2ColorTemperature(int temperature);
1869 + bool setV4L2Parameter(quint32 id, qint32 value);
1870 + int getV4L2Parameter(quint32 id) const;
1871 +
1872 + void setV4L2CameraFormat();
1873 + void initV4L2MemoryTransfer();
1874 + void startCapturing();
1875 + void stopCapturing();
1876 +
1877 +private:
1878 + bool m_active = false;
1879 + QCameraDevice m_cameraDevice;
1880 +
1881 + std::unique_ptr<QSocketNotifier> m_notifier;
1882 + std::unique_ptr<QV4L2MemoryTransfer> m_memoryTransfer;
1883 + std::shared_ptr<QV4L2FileDescriptor> m_v4l2FileDescriptor;
1884 +
1885 + V4L2CameraInfo m_v4l2Info;
1886 +
1887 + timeval m_firstFrameTime = { -1, -1 };
1888 + quint32 m_bytesPerLine = 0;
1889 + quint32 m_imageSize = 0;
1890 + QVideoFrameFormat::ColorSpace m_colorSpace = QVideoFrameFormat::ColorSpace_Undefined;
1891 + qint64 m_frameDuration = -1;
1892 + bool m_cameraBusy = false;
1893 +};
1894 +
1895 +QT_END_NAMESPACE
1896 +
1897 +#endif // QV4L2CAMERA_H
1898 diff --git a/src/plugins/multimedia/v4l2/qv4l2cameradevices.cpp b/src/plugins/multimedia/v4l2/qv4l2cameradevices.cpp
1899 new file mode 100644
1900 index 000000000..c89ff69c6
1901 --- /dev/null
1902 +++ b/src/plugins/multimedia/v4l2/qv4l2cameradevices.cpp
1903 @@ -0,0 +1,168 @@
1904 +// Copyright (C) 2023 The Qt Company Ltd.
1905 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
1906 +
1907 +#include "qv4l2cameradevices_p.h"
1908 +#include "qv4l2filedescriptor_p.h"
1909 +#include "qv4l2camera_p.h"
1910 +
1911 +#include <private/qcameradevice_p.h>
1912 +#include <private/qcore_unix_p.h>
1913 +
1914 +#include <qdir.h>
1915 +#include <qfile.h>
1916 +#include <qdebug.h>
1917 +#include <qloggingcategory.h>
1918 +
1919 +#include <linux/videodev2.h>
1920 +
1921 +QT_BEGIN_NAMESPACE
1922 +
1923 +static Q_LOGGING_CATEGORY(qLcV4L2CameraDevices, "qt.multimedia.ffmpeg.v4l2cameradevices");
1924 +
1925 +static bool areCamerasEqual(QList<QCameraDevice> a, QList<QCameraDevice> b)
1926 +{
1927 + auto areCamerasDataEqual = [](const QCameraDevice &a, const QCameraDevice &b) {
1928 + Q_ASSERT(QCameraDevicePrivate::handle(a));
1929 + Q_ASSERT(QCameraDevicePrivate::handle(b));
1930 + return *QCameraDevicePrivate::handle(a) == *QCameraDevicePrivate::handle(b);
1931 + };
1932 +
1933 + return std::equal(a.cbegin(), a.cend(), b.cbegin(), b.cend(), areCamerasDataEqual);
1934 +}
1935 +
1936 +QV4L2CameraDevices::QV4L2CameraDevices(QPlatformMediaIntegration *integration)
1937 + : QPlatformVideoDevices(integration)
1938 +{
1939 + m_deviceWatcher.addPath(QLatin1String("/dev"));
1940 + connect(&m_deviceWatcher, &QFileSystemWatcher::directoryChanged, this,
1941 + &QV4L2CameraDevices::checkCameras);
1942 + doCheckCameras();
1943 +}
1944 +
1945 +QList<QCameraDevice> QV4L2CameraDevices::findVideoInputs() const
1946 +{
1947 + return m_cameras;
1948 +}
1949 +
1950 +void QV4L2CameraDevices::checkCameras()
1951 +{
1952 + if (doCheckCameras())
1953 + onVideoInputsChanged();
1954 +}
1955 +
1956 +bool QV4L2CameraDevices::doCheckCameras()
1957 +{
1958 + QList<QCameraDevice> newCameras;
1959 +
1960 + QDir dir(QLatin1String("/dev"));
1961 + const auto devices = dir.entryList(QDir::System);
1962 +
1963 + bool first = true;
1964 +
1965 + for (auto device : devices) {
1966 + // qCDebug(qLcV4L2Camera) << "device:" << device;
1967 + if (!device.startsWith(QLatin1String("video")))
1968 + continue;
1969 +
1970 + QByteArray file = QFile::encodeName(dir.filePath(device));
1971 + const int fd = open(file.constData(), O_RDONLY);
1972 + if (fd < 0)
1973 + continue;
1974 +
1975 + auto fileCloseGuard = qScopeGuard([fd]() { close(fd); });
1976 +
1977 + v4l2_fmtdesc formatDesc = {};
1978 +
1979 + struct v4l2_capability cap;
1980 + if (xioctl(fd, VIDIOC_QUERYCAP, &cap) < 0)
1981 + continue;
1982 +
1983 + if (cap.device_caps & V4L2_CAP_META_CAPTURE)
1984 + continue;
1985 + if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
1986 + continue;
1987 + if (!(cap.capabilities & V4L2_CAP_STREAMING))
1988 + continue;
1989 +
1990 + auto camera = std::make_unique<QCameraDevicePrivate>();
1991 +
1992 + camera->id = file;
1993 + camera->description = QString::fromUtf8((const char *)cap.card);
1994 + qCDebug(qLcV4L2CameraDevices) << "found camera" << camera->id << camera->description;
1995 +
1996 + formatDesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1997 +
1998 + while (!xioctl(fd, VIDIOC_ENUM_FMT, &formatDesc)) {
1999 + auto pixelFmt = formatForV4L2Format(formatDesc.pixelformat);
2000 + qCDebug(qLcV4L2CameraDevices) << " " << pixelFmt;
2001 +
2002 + if (pixelFmt == QVideoFrameFormat::Format_Invalid) {
2003 + ++formatDesc.index;
2004 + continue;
2005 + }
2006 +
2007 + qCDebug(qLcV4L2CameraDevices) << "frame sizes:";
2008 + v4l2_frmsizeenum frameSize = {};
2009 + frameSize.pixel_format = formatDesc.pixelformat;
2010 +
2011 + while (!xioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frameSize)) {
2012 + ++frameSize.index;
2013 + if (frameSize.type != V4L2_FRMSIZE_TYPE_DISCRETE)
2014 + continue;
2015 +
2016 + QSize resolution(frameSize.discrete.width, frameSize.discrete.height);
2017 + float min = 1e10;
2018 + float max = 0;
2019 +
2020 + v4l2_frmivalenum frameInterval = {};
2021 + frameInterval.pixel_format = formatDesc.pixelformat;
2022 + frameInterval.width = frameSize.discrete.width;
2023 + frameInterval.height = frameSize.discrete.height;
2024 +
2025 + while (!xioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &frameInterval)) {
2026 + ++frameInterval.index;
2027 + if (frameInterval.type != V4L2_FRMIVAL_TYPE_DISCRETE)
2028 + continue;
2029 + float rate = float(frameInterval.discrete.denominator)
2030 + / float(frameInterval.discrete.numerator);
2031 + if (rate > max)
2032 + max = rate;
2033 + if (rate < min)
2034 + min = rate;
2035 + }
2036 +
2037 + qCDebug(qLcV4L2CameraDevices) << " " << resolution << min << max;
2038 +
2039 + if (min <= max) {
2040 + auto fmt = std::make_unique<QCameraFormatPrivate>();
2041 + fmt->pixelFormat = pixelFmt;
2042 + fmt->resolution = resolution;
2043 + fmt->minFrameRate = min;
2044 + fmt->maxFrameRate = max;
2045 + camera->videoFormats.append(fmt.release()->create());
2046 + camera->photoResolutions.append(resolution);
2047 + }
2048 + }
2049 +
2050 + ++formatDesc.index;
2051 + }
2052 +
2053 + if (camera->videoFormats.empty())
2054 + continue;
2055 +
2056 + // first camera is default
2057 + camera->isDefault = std::exchange(first, false);
2058 +
2059 + newCameras.append(camera.release()->create());
2060 + }
2061 +
2062 + if (areCamerasEqual(m_cameras, newCameras))
2063 + return false;
2064 +
2065 + m_cameras = std::move(newCameras);
2066 + return true;
2067 +}
2068 +
2069 +QT_END_NAMESPACE
2070 +
2071 +#include "moc_qv4l2cameradevices_p.cpp"
2072 diff --git a/src/plugins/multimedia/v4l2/qv4l2cameradevices_p.h b/src/plugins/multimedia/v4l2/qv4l2cameradevices_p.h
2073 new file mode 100644
2074 index 000000000..22ac44027
2075 --- /dev/null
2076 +++ b/src/plugins/multimedia/v4l2/qv4l2cameradevices_p.h
2077 @@ -0,0 +1,47 @@
2078 +// Copyright (C) 2023 The Qt Company Ltd.
2079 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
2080 +
2081 +#ifndef QV4L2CAMERADEVICES_P_H
2082 +#define QV4L2CAMERADEVICES_P_H
2083 +
2084 +//
2085 +// W A R N I N G
2086 +// -------------
2087 +//
2088 +// This file is not part of the Qt API. It exists purely as an
2089 +// implementation detail. This header file may change from version to
2090 +// version without notice, or even be removed.
2091 +//
2092 +// We mean it.
2093 +//
2094 +
2095 +#include <private/qplatformvideodevices_p.h>
2096 +#include <private/qplatformmediaintegration_p.h>
2097 +
2098 +#include <qfilesystemwatcher.h>
2099 +
2100 +QT_BEGIN_NAMESPACE
2101 +
2102 +class QV4L2CameraDevices : public QPlatformVideoDevices
2103 +{
2104 + Q_OBJECT
2105 +public:
2106 + QV4L2CameraDevices(QPlatformMediaIntegration *integration);
2107 +
2108 +public Q_SLOTS:
2109 + void checkCameras();
2110 +
2111 +protected:
2112 + QList<QCameraDevice> findVideoInputs() const override;
2113 +
2114 +private:
2115 + bool doCheckCameras();
2116 +
2117 +private:
2118 + QList<QCameraDevice> m_cameras;
2119 + QFileSystemWatcher m_deviceWatcher;
2120 +};
2121 +
2122 +QT_END_NAMESPACE
2123 +
2124 +#endif // QV4L2CAMERADEVICES_P_H
2125 diff --git a/src/plugins/multimedia/v4l2/qv4l2filedescriptor.cpp b/src/plugins/multimedia/v4l2/qv4l2filedescriptor.cpp
2126 new file mode 100644
2127 index 000000000..7f7b099c7
2128 --- /dev/null
2129 +++ b/src/plugins/multimedia/v4l2/qv4l2filedescriptor.cpp
2130 @@ -0,0 +1,71 @@
2131 +// Copyright (C) 2023 The Qt Company Ltd.
2132 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
2133 +
2134 +#include "qv4l2filedescriptor_p.h"
2135 +
2136 +#include <sys/ioctl.h>
2137 +#include <private/qcore_unix_p.h>
2138 +
2139 +#include <linux/videodev2.h>
2140 +
2141 +QT_BEGIN_NAMESPACE
2142 +
2143 +int xioctl(int fd, int request, void *arg)
2144 +{
2145 + int res;
2146 +
2147 + do {
2148 + res = ::ioctl(fd, request, arg);
2149 + } while (res == -1 && EINTR == errno);
2150 +
2151 + return res;
2152 +}
2153 +
2154 +QV4L2FileDescriptor::QV4L2FileDescriptor(int descriptor) : m_descriptor(descriptor)
2155 +{
2156 + Q_ASSERT(descriptor >= 0);
2157 +}
2158 +
2159 +QV4L2FileDescriptor::~QV4L2FileDescriptor()
2160 +{
2161 + qt_safe_close(m_descriptor);
2162 +}
2163 +
2164 +bool QV4L2FileDescriptor::call(int request, void *arg) const
2165 +{
2166 + return ::xioctl(m_descriptor, request, arg) >= 0;
2167 +}
2168 +
2169 +bool QV4L2FileDescriptor::requestBuffers(quint32 memoryType, quint32 &buffersCount) const
2170 +{
2171 + v4l2_requestbuffers req = {};
2172 + req.count = buffersCount;
2173 + req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2174 + req.memory = memoryType;
2175 +
2176 + if (!call(VIDIOC_REQBUFS, &req))
2177 + return false;
2178 +
2179 + buffersCount = req.count;
2180 + return true;
2181 +}
2182 +
2183 +bool QV4L2FileDescriptor::startStream()
2184 +{
2185 + int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2186 + if (!call(VIDIOC_STREAMON, &type))
2187 + return false;
2188 +
2189 + m_streamStarted = true;
2190 + return true;
2191 +}
2192 +
2193 +bool QV4L2FileDescriptor::stopStream()
2194 +{
2195 + int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2196 + auto result = call(VIDIOC_STREAMOFF, &type);
2197 + m_streamStarted = false;
2198 + return result;
2199 +}
2200 +
2201 +QT_END_NAMESPACE
2202 diff --git a/src/plugins/multimedia/v4l2/qv4l2filedescriptor_p.h b/src/plugins/multimedia/v4l2/qv4l2filedescriptor_p.h
2203 new file mode 100644
2204 index 000000000..1058c7a82
2205 --- /dev/null
2206 +++ b/src/plugins/multimedia/v4l2/qv4l2filedescriptor_p.h
2207 @@ -0,0 +1,50 @@
2208 +// Copyright (C) 2023 The Qt Company Ltd.
2209 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
2210 +
2211 +#ifndef QV4L2FILEDESCRIPTOR_P_H
2212 +#define QV4L2FILEDESCRIPTOR_P_H
2213 +
2214 +#include <private/qtmultimediaglobal_p.h>
2215 +
2216 +//
2217 +// W A R N I N G
2218 +// -------------
2219 +//
2220 +// This file is not part of the Qt API. It exists purely as an
2221 +// implementation detail. This header file may change from version to
2222 +// version without notice, or even be removed.
2223 +//
2224 +// We mean it.
2225 +//
2226 +
2227 +QT_BEGIN_NAMESPACE
2228 +
2229 +int xioctl(int fd, int request, void *arg);
2230 +
2231 +class QV4L2FileDescriptor
2232 +{
2233 +public:
2234 + QV4L2FileDescriptor(int descriptor);
2235 +
2236 + ~QV4L2FileDescriptor();
2237 +
2238 + bool call(int request, void *arg) const;
2239 +
2240 + int get() const { return m_descriptor; }
2241 +
2242 + bool requestBuffers(quint32 memoryType, quint32 &buffersCount) const;
2243 +
2244 + bool startStream();
2245 +
2246 + bool stopStream();
2247 +
2248 + bool streamStarted() const { return m_streamStarted; }
2249 +
2250 +private:
2251 + int m_descriptor;
2252 + bool m_streamStarted = false;
2253 +};
2254 +
2255 +QT_END_NAMESPACE
2256 +
2257 +#endif // QV4L2FILEDESCRIPTOR_P_H
2258 diff --git a/src/plugins/multimedia/v4l2/qv4l2memorytransfer.cpp b/src/plugins/multimedia/v4l2/qv4l2memorytransfer.cpp
2259 new file mode 100644
2260 index 000000000..32ee4f8f8
2261 --- /dev/null
2262 +++ b/src/plugins/multimedia/v4l2/qv4l2memorytransfer.cpp
2263 @@ -0,0 +1,223 @@
2264 +// Copyright (C) 2023 The Qt Company Ltd.
2265 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
2266 +
2267 +#include "qv4l2memorytransfer_p.h"
2268 +#include "qv4l2filedescriptor_p.h"
2269 +
2270 +#include <qloggingcategory.h>
2271 +#include <qdebug.h>
2272 +#include <sys/mman.h>
2273 +#include <optional>
2274 +
2275 +QT_BEGIN_NAMESPACE
2276 +
2277 +static Q_LOGGING_CATEGORY(qLcV4L2MemoryTransfer, "qt.multimedia.ffmpeg.v4l2camera.memorytransfer");
2278 +
2279 +namespace {
2280 +
2281 +v4l2_buffer makeV4l2Buffer(quint32 memoryType, quint32 index = 0)
2282 +{
2283 + v4l2_buffer buf = {};
2284 + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2285 + buf.memory = memoryType;
2286 + buf.index = index;
2287 + return buf;
2288 +}
2289 +
2290 +class UserPtrMemoryTransfer : public QV4L2MemoryTransfer
2291 +{
2292 +public:
2293 + static QV4L2MemoryTransferUPtr create(QV4L2FileDescriptorPtr fileDescriptor, quint32 imageSize)
2294 + {
2295 + quint32 buffersCount = 2;
2296 + if (!fileDescriptor->requestBuffers(V4L2_MEMORY_USERPTR, buffersCount)) {
2297 + qCWarning(qLcV4L2MemoryTransfer) << "Cannot request V4L2_MEMORY_USERPTR buffers";
2298 + return {};
2299 + }
2300 +
2301 + std::unique_ptr<UserPtrMemoryTransfer> result(
2302 + new UserPtrMemoryTransfer(std::move(fileDescriptor), buffersCount, imageSize));
2303 +
2304 + return result->enqueueBuffers() ? std::move(result) : nullptr;
2305 + }
2306 +
2307 + std::optional<Buffer> dequeueBuffer() override
2308 + {
2309 + auto v4l2Buffer = makeV4l2Buffer(V4L2_MEMORY_USERPTR);
2310 + if (!fileDescriptor().call(VIDIOC_DQBUF, &v4l2Buffer))
2311 + return {};
2312 +
2313 + Q_ASSERT(v4l2Buffer.index < m_byteArrays.size());
2314 + Q_ASSERT(!m_byteArrays[v4l2Buffer.index].isEmpty());
2315 +
2316 + return Buffer{ v4l2Buffer, std::move(m_byteArrays[v4l2Buffer.index]) };
2317 + }
2318 +
2319 + bool enqueueBuffer(quint32 index) override
2320 + {
2321 + Q_ASSERT(index < m_byteArrays.size());
2322 + Q_ASSERT(m_byteArrays[index].isEmpty());
2323 +
2324 + auto buf = makeV4l2Buffer(V4L2_MEMORY_USERPTR, index);
2325 + static_assert(sizeof(decltype(buf.m.userptr)) == sizeof(size_t), "Not compatible sizes");
2326 +
2327 + m_byteArrays[index] = QByteArray(static_cast<int>(m_imageSize), Qt::Uninitialized);
2328 +
2329 + buf.m.userptr = (decltype(buf.m.userptr))m_byteArrays[index].data();
2330 + buf.length = m_byteArrays[index].size();
2331 +
2332 + if (!fileDescriptor().call(VIDIOC_QBUF, &buf)) {
2333 + qWarning() << "Couldn't add V4L2 buffer" << errno << strerror(errno) << index;
2334 + return false;
2335 + }
2336 +
2337 + return true;
2338 + }
2339 +
2340 + quint32 buffersCount() const override { return static_cast<quint32>(m_byteArrays.size()); }
2341 +
2342 +private:
2343 + UserPtrMemoryTransfer(QV4L2FileDescriptorPtr fileDescriptor, quint32 buffersCount,
2344 + quint32 imageSize)
2345 + : QV4L2MemoryTransfer(std::move(fileDescriptor)),
2346 + m_imageSize(imageSize),
2347 + m_byteArrays(buffersCount)
2348 + {
2349 + }
2350 +
2351 +private:
2352 + quint32 m_imageSize;
2353 + std::vector<QByteArray> m_byteArrays;
2354 +};
2355 +
2356 +class MMapMemoryTransfer : public QV4L2MemoryTransfer
2357 +{
2358 +public:
2359 + struct MemorySpan
2360 + {
2361 + void *data = nullptr;
2362 + size_t size = 0;
2363 + bool inQueue = false;
2364 + };
2365 +
2366 + static QV4L2MemoryTransferUPtr create(QV4L2FileDescriptorPtr fileDescriptor)
2367 + {
2368 + quint32 buffersCount = 2;
2369 + if (!fileDescriptor->requestBuffers(V4L2_MEMORY_MMAP, buffersCount)) {
2370 + qCWarning(qLcV4L2MemoryTransfer) << "Cannot request V4L2_MEMORY_MMAP buffers";
2371 + return {};
2372 + }
2373 +
2374 + std::unique_ptr<MMapMemoryTransfer> result(
2375 + new MMapMemoryTransfer(std::move(fileDescriptor)));
2376 +
2377 + return result->init(buffersCount) ? std::move(result) : nullptr;
2378 + }
2379 +
2380 + bool init(quint32 buffersCount)
2381 + {
2382 + for (quint32 index = 0; index < buffersCount; ++index) {
2383 + auto buf = makeV4l2Buffer(V4L2_MEMORY_MMAP, index);
2384 +
2385 + if (!fileDescriptor().call(VIDIOC_QUERYBUF, &buf)) {
2386 + qWarning() << "Can't map buffer" << index;
2387 + return false;
2388 + }
2389 +
2390 + auto mappedData = mmap(nullptr, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED,
2391 + fileDescriptor().get(), buf.m.offset);
2392 +
2393 + if (mappedData == MAP_FAILED) {
2394 + qWarning() << "mmap failed" << index << buf.length << buf.m.offset;
2395 + return false;
2396 + }
2397 +
2398 + m_spans.push_back(MemorySpan{ mappedData, buf.length, false });
2399 + }
2400 +
2401 + m_spans.shrink_to_fit();
2402 +
2403 + return enqueueBuffers();
2404 + }
2405 +
2406 + ~MMapMemoryTransfer() override
2407 + {
2408 + for (const auto &span : m_spans)
2409 + munmap(span.data, span.size);
2410 + }
2411 +
2412 + std::optional<Buffer> dequeueBuffer() override
2413 + {
2414 + auto v4l2Buffer = makeV4l2Buffer(V4L2_MEMORY_MMAP);
2415 + if (!fileDescriptor().call(VIDIOC_DQBUF, &v4l2Buffer))
2416 + return {};
2417 +
2418 + const auto index = v4l2Buffer.index;
2419 +
2420 + Q_ASSERT(index < m_spans.size());
2421 +
2422 + auto &span = m_spans[index];
2423 +
2424 + Q_ASSERT(span.inQueue);
2425 + span.inQueue = false;
2426 +
2427 + return Buffer{ v4l2Buffer,
2428 + QByteArray(reinterpret_cast<const char *>(span.data), span.size) };
2429 + }
2430 +
2431 + bool enqueueBuffer(quint32 index) override
2432 + {
2433 + Q_ASSERT(index < m_spans.size());
2434 + Q_ASSERT(!m_spans[index].inQueue);
2435 +
2436 + auto buf = makeV4l2Buffer(V4L2_MEMORY_MMAP, index);
2437 + if (!fileDescriptor().call(VIDIOC_QBUF, &buf))
2438 + return false;
2439 +
2440 + m_spans[index].inQueue = true;
2441 + return true;
2442 + }
2443 +
2444 + quint32 buffersCount() const override { return static_cast<quint32>(m_spans.size()); }
2445 +
2446 +private:
2447 + using QV4L2MemoryTransfer::QV4L2MemoryTransfer;
2448 +
2449 +private:
2450 + std::vector<MemorySpan> m_spans;
2451 +};
2452 +} // namespace
2453 +
2454 +QV4L2MemoryTransfer::QV4L2MemoryTransfer(QV4L2FileDescriptorPtr fileDescriptor)
2455 + : m_fileDescriptor(std::move(fileDescriptor))
2456 +{
2457 + Q_ASSERT(m_fileDescriptor);
2458 + Q_ASSERT(!m_fileDescriptor->streamStarted());
2459 +}
2460 +
2461 +QV4L2MemoryTransfer::~QV4L2MemoryTransfer()
2462 +{
2463 + Q_ASSERT(!m_fileDescriptor->streamStarted()); // to avoid possible corruptions
2464 +}
2465 +
2466 +bool QV4L2MemoryTransfer::enqueueBuffers()
2467 +{
2468 + for (quint32 i = 0; i < buffersCount(); ++i)
2469 + if (!enqueueBuffer(i))
2470 + return false;
2471 +
2472 + return true;
2473 +}
2474 +
2475 +QV4L2MemoryTransferUPtr makeUserPtrMemoryTransfer(QV4L2FileDescriptorPtr fileDescriptor,
2476 + quint32 imageSize)
2477 +{
2478 + return UserPtrMemoryTransfer::create(std::move(fileDescriptor), imageSize);
2479 +}
2480 +
2481 +QV4L2MemoryTransferUPtr makeMMapMemoryTransfer(QV4L2FileDescriptorPtr fileDescriptor)
2482 +{
2483 + return MMapMemoryTransfer::create(std::move(fileDescriptor));
2484 +}
2485 +
2486 +QT_END_NAMESPACE
2487 diff --git a/src/plugins/multimedia/v4l2/qv4l2memorytransfer_p.h b/src/plugins/multimedia/v4l2/qv4l2memorytransfer_p.h
2488 new file mode 100644
2489 index 000000000..6b5e3913f
2490 --- /dev/null
2491 +++ b/src/plugins/multimedia/v4l2/qv4l2memorytransfer_p.h
2492 @@ -0,0 +1,66 @@
2493 +// Copyright (C) 2023 The Qt Company Ltd.
2494 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
2495 +
2496 +#ifndef QV4L2MEMORYTRANSFER_P_H
2497 +#define QV4L2MEMORYTRANSFER_P_H
2498 +
2499 +#include <private/qtmultimediaglobal_p.h>
2500 +#include <qbytearray.h>
2501 +#include <linux/videodev2.h>
2502 +
2503 +#include <memory>
2504 +
2505 +QT_BEGIN_NAMESPACE
2506 +
2507 +//
2508 +// W A R N I N G
2509 +// -------------
2510 +//
2511 +// This file is not part of the Qt API. It exists purely as an
2512 +// implementation detail. This header file may change from version to
2513 +// version without notice, or even be removed.
2514 +//
2515 +// We mean it.
2516 +//
2517 +
2518 +class QV4L2FileDescriptor;
2519 +using QV4L2FileDescriptorPtr = std::shared_ptr<QV4L2FileDescriptor>;
2520 +
2521 +class QV4L2MemoryTransfer
2522 +{
2523 +public:
2524 + struct Buffer
2525 + {
2526 + v4l2_buffer v4l2Buffer = {};
2527 + QByteArray data;
2528 + };
2529 +
2530 + QV4L2MemoryTransfer(QV4L2FileDescriptorPtr fileDescriptor);
2531 +
2532 + virtual ~QV4L2MemoryTransfer();
2533 +
2534 + virtual std::optional<Buffer> dequeueBuffer() = 0;
2535 +
2536 + virtual bool enqueueBuffer(quint32 index) = 0;
2537 +
2538 + virtual quint32 buffersCount() const = 0;
2539 +
2540 +protected:
2541 + bool enqueueBuffers();
2542 +
2543 + const QV4L2FileDescriptor &fileDescriptor() const { return *m_fileDescriptor; }
2544 +
2545 +private:
2546 + QV4L2FileDescriptorPtr m_fileDescriptor;
2547 +};
2548 +
2549 +using QV4L2MemoryTransferUPtr = std::unique_ptr<QV4L2MemoryTransfer>;
2550 +
2551 +QV4L2MemoryTransferUPtr makeUserPtrMemoryTransfer(QV4L2FileDescriptorPtr fileDescriptor,
2552 + quint32 imageSize);
2553 +
2554 +QV4L2MemoryTransferUPtr makeMMapMemoryTransfer(QV4L2FileDescriptorPtr fileDescriptor);
2555 +
2556 +QT_END_NAMESPACE
2557 +
2558 +#endif // QV4L2MEMORYTRANSFER_P_H