| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QGSTREAMERIMAGECAPTURECONTROL_H |
| 5 | #define QGSTREAMERIMAGECAPTURECONTROL_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtMultimedia/private/qplatformimagecapture_p.h> |
| 19 | #include <QtMultimedia/private/qmultimediautils_p.h> |
| 20 | |
| 21 | #include <QtCore/qmutex.h> |
| 22 | #include <QtCore/qqueue.h> |
| 23 | #include <QtCore/private/qexpected_p.h> |
| 24 | #include <QtConcurrent/QtConcurrentRun> |
| 25 | |
| 26 | #include <common/qgst_p.h> |
| 27 | #include <common/qgstreamerbufferprobe_p.h> |
| 28 | #include <mediacapture/qgstreamermediacapturesession_p.h> |
| 29 | #include <gst/video/video.h> |
| 30 | |
| 31 | #include <map> |
| 32 | |
| 33 | QT_BEGIN_NAMESPACE |
| 34 | |
| 35 | class QGstreamerImageCapture : public QPlatformImageCapture, private QGstreamerBufferProbe |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | |
| 39 | public: |
| 40 | static q23::expected<QPlatformImageCapture *, QString> create(QImageCapture *parent); |
| 41 | ~QGstreamerImageCapture() override; |
| 42 | |
| 43 | bool isReadyForCapture() const override; |
| 44 | int capture(const QString &fileName) override; |
| 45 | int captureToBuffer() override; |
| 46 | |
| 47 | QImageEncoderSettings imageSettings() const override; |
| 48 | void setImageSettings(const QImageEncoderSettings &settings) override; |
| 49 | |
| 50 | bool probeBuffer(GstBuffer *buffer) override; |
| 51 | |
| 52 | void setCaptureSession(QPlatformMediaCaptureSession *session); |
| 53 | |
| 54 | QGstElement gstElement() const { return bin; } |
| 55 | |
| 56 | void setMetaData(const QMediaMetaData &m) override; |
| 57 | |
| 58 | public Q_SLOTS: |
| 59 | void cameraActiveChanged(bool active); |
| 60 | void onCameraChanged(); |
| 61 | |
| 62 | private: |
| 63 | QGstreamerImageCapture(QImageCapture *parent); |
| 64 | |
| 65 | void setResolution(const QSize &resolution); |
| 66 | int doCapture(QString fileName); |
| 67 | void saveBufferToFile(QGstBufferHandle, QString filename, int id); |
| 68 | void convertBufferToImage(const QMutexLocker<QRecursiveMutex> &, QGstBufferHandle, QGstCaps, |
| 69 | QMediaMetaData, int id); |
| 70 | |
| 71 | mutable QRecursiveMutex m_mutex; // guard all elements accessed from gstreamer / worker threads |
| 72 | QGstreamerMediaCaptureSession *m_session = nullptr; |
| 73 | int m_lastId = 0; |
| 74 | QImageEncoderSettings m_settings; |
| 75 | |
| 76 | struct PendingImage { |
| 77 | int id; |
| 78 | QString filename; |
| 79 | }; |
| 80 | |
| 81 | QQueue<PendingImage> pendingImages; |
| 82 | |
| 83 | QGstBin bin; |
| 84 | QGstElement queue; |
| 85 | QGstElement filter; |
| 86 | QGstElement videoConvert; |
| 87 | QGstElement encoder; |
| 88 | QGstElement muxer; |
| 89 | QGstElement sink; |
| 90 | QGstPad videoSrcPad; |
| 91 | |
| 92 | std::atomic_bool m_captureNextBuffer{}; |
| 93 | bool cameraActive = false; |
| 94 | |
| 95 | QMutex m_pendingFuturesMutex; |
| 96 | std::map<int, QFuture<void>> m_pendingFutures; |
| 97 | std::atomic<int> m_futureIDAllocator{}; |
| 98 | |
| 99 | template <typename Functor> |
| 100 | void invokeDeferred(Functor &&fn); |
| 101 | |
| 102 | template <typename Functor> |
| 103 | void runInThreadPool(Functor); |
| 104 | }; |
| 105 | |
| 106 | QT_END_NAMESPACE |
| 107 | |
| 108 | #endif // QGSTREAMERCAPTURECORNTROL_H |
| 109 | |