| 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 <QtConcurrent/QtConcurrentRun> | 
| 24 | |
| 25 | #include <common/qgst_p.h> | 
| 26 | #include <common/qgstreamerbufferprobe_p.h> | 
| 27 | #include <mediacapture/qgstreamermediacapturesession_p.h> | 
| 28 | #include <gst/video/video.h> | 
| 29 | |
| 30 | #include <map> | 
| 31 | |
| 32 | QT_BEGIN_NAMESPACE | 
| 33 | |
| 34 | class QGstreamerImageCapture : public QPlatformImageCapture, private QGstreamerBufferProbe | 
| 35 | { | 
| 36 | Q_OBJECT | 
| 37 | |
| 38 | public: | 
| 39 | static QMaybe<QPlatformImageCapture *> create(QImageCapture *parent); | 
| 40 | ~QGstreamerImageCapture() override; | 
| 41 | |
| 42 | bool isReadyForCapture() const override; | 
| 43 | int capture(const QString &fileName) override; | 
| 44 | int captureToBuffer() override; | 
| 45 | |
| 46 | QImageEncoderSettings imageSettings() const override; | 
| 47 | void setImageSettings(const QImageEncoderSettings &settings) override; | 
| 48 | |
| 49 | bool probeBuffer(GstBuffer *buffer) override; | 
| 50 | |
| 51 | void setCaptureSession(QPlatformMediaCaptureSession *session); | 
| 52 | |
| 53 | QGstElement gstElement() const { return bin; } | 
| 54 | |
| 55 | void setMetaData(const QMediaMetaData &m) override; | 
| 56 | |
| 57 | public Q_SLOTS: | 
| 58 | void cameraActiveChanged(bool active); | 
| 59 | void onCameraChanged(); | 
| 60 | |
| 61 | private: | 
| 62 | QGstreamerImageCapture(QImageCapture *parent); | 
| 63 | |
| 64 | void setResolution(const QSize &resolution); | 
| 65 | int doCapture(QString fileName); | 
| 66 | void saveBufferToFile(QGstBufferHandle, QString filename, int id); | 
| 67 | void convertBufferToImage(const QMutexLocker<QRecursiveMutex> &, QGstBufferHandle, QGstCaps, | 
| 68 | QMediaMetaData, int id); | 
| 69 | |
| 70 | mutable QRecursiveMutex m_mutex; // guard all elements accessed from gstreamer / worker threads | 
| 71 | QGstreamerMediaCaptureSession *m_session = nullptr; | 
| 72 | int m_lastId = 0; | 
| 73 | QImageEncoderSettings m_settings; | 
| 74 | |
| 75 | struct PendingImage { | 
| 76 | int id; | 
| 77 | QString filename; | 
| 78 | }; | 
| 79 | |
| 80 | QQueue<PendingImage> pendingImages; | 
| 81 | |
| 82 | QGstBin bin; | 
| 83 | QGstElement queue; | 
| 84 | QGstElement filter; | 
| 85 | QGstElement videoConvert; | 
| 86 | QGstElement encoder; | 
| 87 | QGstElement muxer; | 
| 88 | QGstElement sink; | 
| 89 | QGstPad videoSrcPad; | 
| 90 | |
| 91 | std::atomic_bool m_captureNextBuffer{}; | 
| 92 | bool cameraActive = false; | 
| 93 | |
| 94 | QMutex m_pendingFuturesMutex; | 
| 95 | std::map<int, QFuture<void>> m_pendingFutures; | 
| 96 | std::atomic<int> m_futureIDAllocator{}; | 
| 97 | |
| 98 | template <typename Functor> | 
| 99 | void invokeDeferred(Functor &&fn); | 
| 100 | |
| 101 | template <typename Functor> | 
| 102 | void runInThreadPool(Functor); | 
| 103 | }; | 
| 104 | |
| 105 | QT_END_NAMESPACE | 
| 106 | |
| 107 | #endif // QGSTREAMERCAPTURECORNTROL_H | 
| 108 | 
