| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QGSTREAMERCAPTURESESSION_H |
| 41 | #define QGSTREAMERCAPTURESESSION_H |
| 42 | |
| 43 | #include <qmediarecordercontrol.h> |
| 44 | #include <qmediarecorder.h> |
| 45 | |
| 46 | #include <QtCore/qmutex.h> |
| 47 | #include <QtCore/qurl.h> |
| 48 | |
| 49 | #include <gst/gst.h> |
| 50 | #include <gst/video/video.h> |
| 51 | |
| 52 | #include <private/qgstreamerbushelper_p.h> |
| 53 | #include <private/qgstreamerbufferprobe_p.h> |
| 54 | |
| 55 | QT_BEGIN_NAMESPACE |
| 56 | |
| 57 | class QGstreamerMessage; |
| 58 | class QGstreamerBusHelper; |
| 59 | class QGstreamerAudioEncode; |
| 60 | class QGstreamerVideoEncode; |
| 61 | class QGstreamerImageEncode; |
| 62 | class QGstreamerRecorderControl; |
| 63 | class QGstreamerMediaContainerControl; |
| 64 | class QGstreamerVideoRendererInterface; |
| 65 | class QGstreamerAudioProbeControl; |
| 66 | |
| 67 | class QGstreamerElementFactory |
| 68 | { |
| 69 | public: |
| 70 | virtual GstElement *buildElement() = 0; |
| 71 | virtual void prepareWinId() {} |
| 72 | }; |
| 73 | |
| 74 | class QGstreamerVideoInput : public QGstreamerElementFactory |
| 75 | { |
| 76 | public: |
| 77 | virtual QList<qreal> supportedFrameRates(const QSize &frameSize = QSize()) const = 0; |
| 78 | virtual QList<QSize> supportedResolutions(qreal frameRate = -1) const = 0; |
| 79 | }; |
| 80 | |
| 81 | class QGstreamerCaptureSession |
| 82 | : public QObject |
| 83 | , public QGstreamerBusMessageFilter |
| 84 | , private QGstreamerBufferProbe |
| 85 | { |
| 86 | Q_OBJECT |
| 87 | Q_PROPERTY(qint64 duration READ duration NOTIFY durationChanged) |
| 88 | Q_ENUMS(State) |
| 89 | Q_ENUMS(CaptureMode) |
| 90 | Q_INTERFACES(QGstreamerBusMessageFilter) |
| 91 | public: |
| 92 | enum CaptureMode { Audio = 1, |
| 93 | Video = 2, |
| 94 | Image = 4, |
| 95 | AudioAndVideo = Audio | Video, |
| 96 | AudioAndVideoAndImage = Audio | Video | Image |
| 97 | }; |
| 98 | enum State { StoppedState, PreviewState, PausedState, RecordingState }; |
| 99 | |
| 100 | QGstreamerCaptureSession(CaptureMode captureMode, QObject *parent); |
| 101 | ~QGstreamerCaptureSession(); |
| 102 | |
| 103 | QGstreamerBusHelper *bus() { return m_busHelper; } |
| 104 | |
| 105 | CaptureMode captureMode() const { return m_captureMode; } |
| 106 | void setCaptureMode(CaptureMode); |
| 107 | |
| 108 | QUrl outputLocation() const; |
| 109 | bool setOutputLocation(const QUrl& sink); |
| 110 | |
| 111 | QGstreamerAudioEncode *audioEncodeControl() const { return m_audioEncodeControl; } |
| 112 | QGstreamerVideoEncode *videoEncodeControl() const { return m_videoEncodeControl; } |
| 113 | QGstreamerImageEncode *imageEncodeControl() const { return m_imageEncodeControl; } |
| 114 | |
| 115 | QGstreamerRecorderControl *recorderControl() const { return m_recorderControl; } |
| 116 | QGstreamerMediaContainerControl *mediaContainerControl() const { return m_mediaContainerControl; } |
| 117 | |
| 118 | QGstreamerElementFactory *audioInput() const { return m_audioInputFactory; } |
| 119 | void setAudioInput(QGstreamerElementFactory *audioInput); |
| 120 | |
| 121 | QGstreamerElementFactory *audioPreview() const { return m_audioPreviewFactory; } |
| 122 | void setAudioPreview(QGstreamerElementFactory *audioPreview); |
| 123 | |
| 124 | QGstreamerVideoInput *videoInput() const { return m_videoInputFactory; } |
| 125 | void setVideoInput(QGstreamerVideoInput *videoInput); |
| 126 | |
| 127 | QObject *videoPreview() const { return m_viewfinder; } |
| 128 | void setVideoPreview(QObject *viewfinder); |
| 129 | |
| 130 | void captureImage(int requestId, const QString &fileName); |
| 131 | |
| 132 | State state() const; |
| 133 | State pendingState() const; |
| 134 | |
| 135 | qint64 duration() const; |
| 136 | bool isMuted() const { return m_muted; } |
| 137 | qreal volume() const { return m_volume; } |
| 138 | |
| 139 | bool isReady() const; |
| 140 | |
| 141 | bool processBusMessage(const QGstreamerMessage &message) override; |
| 142 | |
| 143 | void addProbe(QGstreamerAudioProbeControl* probe); |
| 144 | void removeProbe(QGstreamerAudioProbeControl* probe); |
| 145 | |
| 146 | signals: |
| 147 | void stateChanged(QGstreamerCaptureSession::State state); |
| 148 | void durationChanged(qint64 duration); |
| 149 | void error(int error, const QString &errorString); |
| 150 | void imageExposed(int requestId); |
| 151 | void imageCaptured(int requestId, const QImage &img); |
| 152 | void imageSaved(int requestId, const QString &path); |
| 153 | void mutedChanged(bool); |
| 154 | void volumeChanged(qreal); |
| 155 | void readyChanged(bool); |
| 156 | void viewfinderChanged(); |
| 157 | |
| 158 | public slots: |
| 159 | void setState(QGstreamerCaptureSession::State); |
| 160 | void setCaptureDevice(const QString &deviceName); |
| 161 | |
| 162 | void dumpGraph(const QString &fileName); |
| 163 | |
| 164 | void setMetaData(const QMap<QByteArray, QVariant>&); |
| 165 | void setMuted(bool); |
| 166 | void setVolume(qreal volume); |
| 167 | |
| 168 | private: |
| 169 | void probeCaps(GstCaps *caps) override; |
| 170 | bool probeBuffer(GstBuffer *buffer) override; |
| 171 | |
| 172 | enum PipelineMode { EmptyPipeline, PreviewPipeline, RecordingPipeline, PreviewAndRecordingPipeline }; |
| 173 | |
| 174 | GstElement *buildEncodeBin(); |
| 175 | GstElement *buildAudioSrc(); |
| 176 | GstElement *buildAudioPreview(); |
| 177 | GstElement *buildVideoSrc(); |
| 178 | GstElement *buildVideoPreview(); |
| 179 | GstElement *buildImageCapture(); |
| 180 | |
| 181 | bool rebuildGraph(QGstreamerCaptureSession::PipelineMode newMode); |
| 182 | |
| 183 | GstPad *getAudioProbePad(); |
| 184 | void removeAudioBufferProbe(); |
| 185 | void addAudioBufferProbe(); |
| 186 | |
| 187 | QUrl m_sink; |
| 188 | QString m_captureDevice; |
| 189 | State m_state; |
| 190 | State m_pendingState; |
| 191 | bool m_waitingForEos; |
| 192 | PipelineMode m_pipelineMode; |
| 193 | QGstreamerCaptureSession::CaptureMode m_captureMode; |
| 194 | QMap<QByteArray, QVariant> m_metaData; |
| 195 | |
| 196 | QGstreamerAudioProbeControl *m_audioProbe; |
| 197 | |
| 198 | QGstreamerElementFactory *m_audioInputFactory; |
| 199 | QGstreamerElementFactory *m_audioPreviewFactory; |
| 200 | QGstreamerVideoInput *m_videoInputFactory; |
| 201 | QObject *m_viewfinder; |
| 202 | QGstreamerVideoRendererInterface *m_viewfinderInterface; |
| 203 | |
| 204 | QGstreamerAudioEncode *m_audioEncodeControl; |
| 205 | QGstreamerVideoEncode *m_videoEncodeControl; |
| 206 | QGstreamerImageEncode *m_imageEncodeControl; |
| 207 | QGstreamerRecorderControl *m_recorderControl; |
| 208 | QGstreamerMediaContainerControl *m_mediaContainerControl; |
| 209 | |
| 210 | QGstreamerBusHelper *m_busHelper; |
| 211 | GstBus* m_bus; |
| 212 | GstElement *m_pipeline; |
| 213 | |
| 214 | GstElement *m_audioSrc; |
| 215 | GstElement *m_audioTee; |
| 216 | GstElement *m_audioPreviewQueue; |
| 217 | GstElement *m_audioPreview; |
| 218 | GstElement *m_audioVolume; |
| 219 | gboolean m_muted; |
| 220 | double m_volume; |
| 221 | |
| 222 | GstElement *m_videoSrc; |
| 223 | GstElement *m_videoTee; |
| 224 | GstElement *m_videoPreviewQueue; |
| 225 | GstElement *m_videoPreview; |
| 226 | |
| 227 | GstElement *m_imageCaptureBin; |
| 228 | |
| 229 | GstElement *m_encodeBin; |
| 230 | |
| 231 | #if GST_CHECK_VERSION(1,0,0) |
| 232 | GstVideoInfo m_previewInfo; |
| 233 | #endif |
| 234 | |
| 235 | public: |
| 236 | bool m_passImage; |
| 237 | bool m_passPrerollImage; |
| 238 | QString m_imageFileName; |
| 239 | int m_imageRequestId; |
| 240 | }; |
| 241 | |
| 242 | QT_END_NAMESPACE |
| 243 | |
| 244 | #endif // QGSTREAMERCAPTURESESSION_H |
| 245 | |