| 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 | #include <QDebug> | 
| 41 |  | 
| 42 | #include "qgstappsrc_p.h" | 
| 43 |  | 
| 44 | QGstAppSrc::QGstAppSrc(QObject *parent) | 
| 45 |     : QObject(parent) | 
| 46 | { | 
| 47 |     m_callbacks.need_data   = &QGstAppSrc::on_need_data; | 
| 48 |     m_callbacks.enough_data = &QGstAppSrc::on_enough_data; | 
| 49 |     m_callbacks.seek_data   = &QGstAppSrc::on_seek_data; | 
| 50 | } | 
| 51 |  | 
| 52 | QGstAppSrc::~QGstAppSrc() | 
| 53 | { | 
| 54 |     if (m_appSrc) | 
| 55 |         gst_object_unref(G_OBJECT(m_appSrc)); | 
| 56 | } | 
| 57 |  | 
| 58 | bool QGstAppSrc::setup(GstElement* appsrc) | 
| 59 | { | 
| 60 |     if (m_appSrc) { | 
| 61 |         gst_object_unref(G_OBJECT(m_appSrc)); | 
| 62 |         m_appSrc = 0; | 
| 63 |     } | 
| 64 |  | 
| 65 |     if (!appsrc || !m_stream) | 
| 66 |         return false; | 
| 67 |  | 
| 68 |     m_appSrc = GST_APP_SRC(appsrc); | 
| 69 |     gst_object_ref(G_OBJECT(m_appSrc)); | 
| 70 |     gst_app_src_set_callbacks(appsrc: m_appSrc, callbacks: (GstAppSrcCallbacks*)&m_callbacks, user_data: this, notify: (GDestroyNotify)&QGstAppSrc::destroy_notify); | 
| 71 |  | 
| 72 |     g_object_get(G_OBJECT(m_appSrc), first_property_name: "max-bytes" , &m_maxBytes, nullptr); | 
| 73 |  | 
| 74 |     if (m_sequential) | 
| 75 |         m_streamType = GST_APP_STREAM_TYPE_STREAM; | 
| 76 |     else | 
| 77 |         m_streamType = GST_APP_STREAM_TYPE_RANDOM_ACCESS; | 
| 78 |     gst_app_src_set_stream_type(appsrc: m_appSrc, type: m_streamType); | 
| 79 |     gst_app_src_set_size(appsrc: m_appSrc, size: (m_sequential) ? -1 : m_stream->size()); | 
| 80 |  | 
| 81 |     return true; | 
| 82 | } | 
| 83 |  | 
| 84 | void QGstAppSrc::setStream(QIODevice *stream) | 
| 85 | { | 
| 86 |     if (m_stream) { | 
| 87 |         disconnect(sender: m_stream, SIGNAL(readyRead()), receiver: this, SLOT(onDataReady())); | 
| 88 |         disconnect(sender: m_stream, SIGNAL(destroyed()), receiver: this, SLOT(streamDestroyed())); | 
| 89 |         m_stream = 0; | 
| 90 |     } | 
| 91 |  | 
| 92 |     if (m_appSrc) { | 
| 93 |         gst_object_unref(G_OBJECT(m_appSrc)); | 
| 94 |         m_appSrc = 0; | 
| 95 |     } | 
| 96 |  | 
| 97 |     m_dataRequestSize = ~0; | 
| 98 |     m_dataRequested = false; | 
| 99 |     m_enoughData = false; | 
| 100 |     m_forceData = false; | 
| 101 |     m_sequential = false; | 
| 102 |     m_maxBytes = 0; | 
| 103 |  | 
| 104 |     if (stream) { | 
| 105 |         m_stream = stream; | 
| 106 |         connect(asender: m_stream, SIGNAL(destroyed()), SLOT(streamDestroyed())); | 
| 107 |         connect(sender: m_stream, SIGNAL(readyRead()), receiver: this, SLOT(onDataReady())); | 
| 108 |         m_sequential = m_stream->isSequential(); | 
| 109 |     } | 
| 110 | } | 
| 111 |  | 
| 112 | QIODevice *QGstAppSrc::stream() const | 
| 113 | { | 
| 114 |     return m_stream; | 
| 115 | } | 
| 116 |  | 
| 117 | GstAppSrc *QGstAppSrc::element() | 
| 118 | { | 
| 119 |     return m_appSrc; | 
| 120 | } | 
| 121 |  | 
| 122 | void QGstAppSrc::onDataReady() | 
| 123 | { | 
| 124 |     if (!m_enoughData) { | 
| 125 |         m_dataRequested = true; | 
| 126 |         pushDataToAppSrc(); | 
| 127 |     } | 
| 128 | } | 
| 129 |  | 
| 130 | void QGstAppSrc::streamDestroyed() | 
| 131 | { | 
| 132 |     if (sender() == m_stream) { | 
| 133 |         m_stream = 0; | 
| 134 |         sendEOS(); | 
| 135 |     } | 
| 136 | } | 
| 137 |  | 
| 138 | void QGstAppSrc::pushDataToAppSrc() | 
| 139 | { | 
| 140 |     if (!isStreamValid() || !m_appSrc) | 
| 141 |         return; | 
| 142 |  | 
| 143 |     if (m_dataRequested && !m_enoughData) { | 
| 144 |         qint64 size; | 
| 145 |         if (m_dataRequestSize == ~0u) | 
| 146 |             size = qMin(a: m_stream->bytesAvailable(), b: queueSize()); | 
| 147 |         else | 
| 148 |             size = qMin(a: m_stream->bytesAvailable(), b: (qint64)m_dataRequestSize); | 
| 149 |  | 
| 150 |         if (size) { | 
| 151 |             GstBuffer* buffer = gst_buffer_new_and_alloc(size); | 
| 152 |  | 
| 153 | #if GST_CHECK_VERSION(1,0,0) | 
| 154 |             GstMapInfo mapInfo; | 
| 155 |             gst_buffer_map(buffer, info: &mapInfo, flags: GST_MAP_WRITE); | 
| 156 |             void* bufferData = mapInfo.data; | 
| 157 | #else | 
| 158 |             void* bufferData = GST_BUFFER_DATA(buffer); | 
| 159 | #endif | 
| 160 |  | 
| 161 |             buffer->offset = m_stream->pos(); | 
| 162 |             qint64 bytesRead = m_stream->read(data: (char*)bufferData, maxlen: size); | 
| 163 |             buffer->offset_end =  buffer->offset + bytesRead - 1; | 
| 164 |  | 
| 165 | #if GST_CHECK_VERSION(1,0,0) | 
| 166 |             gst_buffer_unmap(buffer, info: &mapInfo); | 
| 167 | #endif | 
| 168 |  | 
| 169 |             if (bytesRead > 0) { | 
| 170 |                 m_dataRequested = false; | 
| 171 |                 m_enoughData = false; | 
| 172 |                 GstFlowReturn ret = gst_app_src_push_buffer (GST_APP_SRC (element()), buffer); | 
| 173 |                 if (ret == GST_FLOW_ERROR) { | 
| 174 |                     qWarning()<<"appsrc: push buffer error" ; | 
| 175 | #if GST_CHECK_VERSION(1,0,0) | 
| 176 |                 } else if (ret == GST_FLOW_FLUSHING) { | 
| 177 |                     qWarning()<<"appsrc: push buffer wrong state" ; | 
| 178 |                 } | 
| 179 | #else | 
| 180 |                 } else if (ret == GST_FLOW_WRONG_STATE) { | 
| 181 |                     qWarning()<<"appsrc: push buffer wrong state" ; | 
| 182 |                 } | 
| 183 | #endif | 
| 184 | #if GST_VERSION_MAJOR < 1 | 
| 185 |                 else if (ret == GST_FLOW_RESEND) { | 
| 186 |                     qWarning()<<"appsrc: push buffer resend" ; | 
| 187 |                 } | 
| 188 | #endif | 
| 189 |             } | 
| 190 |         } else if (!m_sequential) { | 
| 191 |             sendEOS(); | 
| 192 |         } | 
| 193 |     } else if (m_stream->atEnd() && !m_sequential) { | 
| 194 |         sendEOS(); | 
| 195 |     } | 
| 196 | } | 
| 197 |  | 
| 198 | bool QGstAppSrc::doSeek(qint64 value) | 
| 199 | { | 
| 200 |     if (isStreamValid()) | 
| 201 |         return stream()->seek(pos: value); | 
| 202 |     return false; | 
| 203 | } | 
| 204 |  | 
| 205 |  | 
| 206 | gboolean QGstAppSrc::on_seek_data(GstAppSrc *element, guint64 arg0, gpointer userdata) | 
| 207 | { | 
| 208 |     Q_UNUSED(element); | 
| 209 |     QGstAppSrc *self = reinterpret_cast<QGstAppSrc*>(userdata); | 
| 210 |     if (self && self->isStreamValid()) { | 
| 211 |         if (!self->stream()->isSequential()) | 
| 212 |             QMetaObject::invokeMethod(obj: self, member: "doSeek" , type: Qt::AutoConnection, Q_ARG(qint64, arg0)); | 
| 213 |     } | 
| 214 |     else | 
| 215 |         return false; | 
| 216 |  | 
| 217 |     return true; | 
| 218 | } | 
| 219 |  | 
| 220 | void QGstAppSrc::on_enough_data(GstAppSrc *element, gpointer userdata) | 
| 221 | { | 
| 222 |     Q_UNUSED(element); | 
| 223 |     QGstAppSrc *self = reinterpret_cast<QGstAppSrc*>(userdata); | 
| 224 |     if (self) | 
| 225 |         self->enoughData() = true; | 
| 226 | } | 
| 227 |  | 
| 228 | void QGstAppSrc::on_need_data(GstAppSrc *element, guint arg0, gpointer userdata) | 
| 229 | { | 
| 230 |     Q_UNUSED(element); | 
| 231 |     QGstAppSrc *self = reinterpret_cast<QGstAppSrc*>(userdata); | 
| 232 |     if (self) { | 
| 233 |         self->dataRequested() = true; | 
| 234 |         self->enoughData() = false; | 
| 235 |         self->dataRequestSize()= arg0; | 
| 236 |         QMetaObject::invokeMethod(obj: self, member: "pushDataToAppSrc" , type: Qt::AutoConnection); | 
| 237 |     } | 
| 238 | } | 
| 239 |  | 
| 240 | void QGstAppSrc::destroy_notify(gpointer data) | 
| 241 | { | 
| 242 |     Q_UNUSED(data); | 
| 243 | } | 
| 244 |  | 
| 245 | void QGstAppSrc::sendEOS() | 
| 246 | { | 
| 247 |     if (!m_appSrc) | 
| 248 |         return; | 
| 249 |  | 
| 250 |     gst_app_src_end_of_stream(GST_APP_SRC(m_appSrc)); | 
| 251 |     if (isStreamValid() && !stream()->isSequential()) | 
| 252 |         stream()->reset(); | 
| 253 | } | 
| 254 |  |