| 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 VIDEOSURFACEGSTSINK_P_H |
| 41 | #define VIDEOSURFACEGSTSINK_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <gst/gst.h> |
| 55 | |
| 56 | #if GST_CHECK_VERSION(1,0,0) |
| 57 | |
| 58 | #include "qgstvideorenderersink_p.h" |
| 59 | |
| 60 | QT_BEGIN_NAMESPACE |
| 61 | typedef QGstVideoRendererSink QVideoSurfaceGstSink; |
| 62 | QT_END_NAMESPACE |
| 63 | |
| 64 | #else |
| 65 | |
| 66 | #include <gst/video/gstvideosink.h> |
| 67 | |
| 68 | #include <QtCore/qlist.h> |
| 69 | #include <QtCore/qmutex.h> |
| 70 | #include <QtCore/qqueue.h> |
| 71 | #include <QtCore/qpointer.h> |
| 72 | #include <QtCore/qwaitcondition.h> |
| 73 | #include <qvideosurfaceformat.h> |
| 74 | #include <qvideoframe.h> |
| 75 | #include <qabstractvideobuffer.h> |
| 76 | |
| 77 | #include "qgstbufferpoolinterface_p.h" |
| 78 | |
| 79 | QT_BEGIN_NAMESPACE |
| 80 | class QAbstractVideoSurface; |
| 81 | |
| 82 | class QVideoSurfaceGstDelegate : public QObject |
| 83 | { |
| 84 | Q_OBJECT |
| 85 | public: |
| 86 | QVideoSurfaceGstDelegate(QAbstractVideoSurface *surface); |
| 87 | ~QVideoSurfaceGstDelegate(); |
| 88 | |
| 89 | QList<QVideoFrame::PixelFormat> supportedPixelFormats( |
| 90 | QAbstractVideoBuffer::HandleType handleType = QAbstractVideoBuffer::NoHandle) const; |
| 91 | |
| 92 | QVideoSurfaceFormat surfaceFormat() const; |
| 93 | |
| 94 | bool start(const QVideoSurfaceFormat &format, int bytesPerLine); |
| 95 | void stop(); |
| 96 | |
| 97 | void unlock(); |
| 98 | |
| 99 | bool isActive(); |
| 100 | |
| 101 | QGstBufferPoolInterface *pool() { return m_pool; } |
| 102 | QMutex *poolMutex() { return &m_poolMutex; } |
| 103 | void clearPoolBuffers(); |
| 104 | |
| 105 | void flush(); |
| 106 | |
| 107 | GstFlowReturn render(GstBuffer *buffer); |
| 108 | |
| 109 | private slots: |
| 110 | void queuedStart(); |
| 111 | void queuedStop(); |
| 112 | void queuedFlush(); |
| 113 | void queuedRender(); |
| 114 | |
| 115 | void updateSupportedFormats(); |
| 116 | |
| 117 | private: |
| 118 | QPointer<QAbstractVideoSurface> m_surface; |
| 119 | QList<QVideoFrame::PixelFormat> m_supportedPixelFormats; |
| 120 | //pixel formats of buffers pool native type |
| 121 | QList<QVideoFrame::PixelFormat> m_supportedPoolPixelFormats; |
| 122 | QGstBufferPoolInterface *m_pool = nullptr; |
| 123 | QList<QGstBufferPoolInterface *> m_pools; |
| 124 | QMutex m_poolMutex; |
| 125 | QMutex m_mutex; |
| 126 | QWaitCondition m_setupCondition; |
| 127 | QWaitCondition m_renderCondition; |
| 128 | QVideoSurfaceFormat m_format; |
| 129 | QVideoFrame m_frame; |
| 130 | GstFlowReturn m_renderReturn = GST_FLOW_ERROR; |
| 131 | int m_bytesPerLine = 0; |
| 132 | bool m_started = false; |
| 133 | bool m_startCanceled = false; |
| 134 | }; |
| 135 | |
| 136 | class QVideoSurfaceGstSink |
| 137 | { |
| 138 | public: |
| 139 | GstVideoSink parent; |
| 140 | |
| 141 | static QVideoSurfaceGstSink *createSink(QAbstractVideoSurface *surface); |
| 142 | static void setSurface(QAbstractVideoSurface *surface) { Q_UNUSED(surface); } |
| 143 | |
| 144 | private: |
| 145 | static GType get_type(); |
| 146 | static void class_init(gpointer g_class, gpointer class_data); |
| 147 | static void base_init(gpointer g_class); |
| 148 | static void instance_init(GTypeInstance *instance, gpointer g_class); |
| 149 | |
| 150 | static void finalize(GObject *object); |
| 151 | |
| 152 | static void handleShowPrerollChange(GObject *o, GParamSpec *p, gpointer d); |
| 153 | |
| 154 | static GstStateChangeReturn change_state(GstElement *element, GstStateChange transition); |
| 155 | |
| 156 | static GstCaps *get_caps(GstBaseSink *sink); |
| 157 | static gboolean set_caps(GstBaseSink *sink, GstCaps *caps); |
| 158 | |
| 159 | static GstFlowReturn buffer_alloc( |
| 160 | GstBaseSink *sink, guint64 offset, guint size, GstCaps *caps, GstBuffer **buffer); |
| 161 | |
| 162 | static gboolean start(GstBaseSink *sink); |
| 163 | static gboolean stop(GstBaseSink *sink); |
| 164 | |
| 165 | static gboolean unlock(GstBaseSink *sink); |
| 166 | |
| 167 | #if GST_CHECK_VERSION(0, 10, 25) |
| 168 | static GstFlowReturn show_frame(GstVideoSink *sink, GstBuffer *buffer); |
| 169 | #else |
| 170 | static GstFlowReturn preroll(GstBaseSink *sink, GstBuffer *buffer); |
| 171 | static GstFlowReturn render(GstBaseSink *sink, GstBuffer *buffer); |
| 172 | #endif |
| 173 | |
| 174 | private: |
| 175 | QVideoSurfaceGstDelegate *delegate = nullptr; |
| 176 | |
| 177 | GstCaps *lastRequestedCaps = nullptr; |
| 178 | GstCaps *lastBufferCaps = nullptr; |
| 179 | QVideoSurfaceFormat *lastSurfaceFormat = nullptr; |
| 180 | }; |
| 181 | |
| 182 | class QVideoSurfaceGstSinkClass |
| 183 | { |
| 184 | public: |
| 185 | GstVideoSinkClass parent_class; |
| 186 | }; |
| 187 | |
| 188 | QT_END_NAMESPACE |
| 189 | |
| 190 | #endif |
| 191 | |
| 192 | #endif |
| 193 | |