qgstreamerappsrc.cpp 5.28 KB
Newer Older
Val Doroshchuk's avatar
Val Doroshchuk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

Val Doroshchuk's avatar
Val Doroshchuk committed
40
#include "qgstreamerappsrc_p.h"
Val Doroshchuk's avatar
Val Doroshchuk committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <private/qgstutils_p.h>

QT_BEGIN_NAMESPACE

class QGstreamerAppSrcPrivate
{
    Q_DECLARE_PUBLIC(QGstreamerAppSrc)

public:
    QGstreamerAppSrcPrivate(GstElement *src, QGstreamerAppSrc *q)
        : appsrc(src)
        , q_ptr(q)
    {
    }

Val Doroshchuk's avatar
Val Doroshchuk committed
56
    bool restart(QVideoFrame &frame);
Val Doroshchuk's avatar
Val Doroshchuk committed
57 58 59 60 61 62 63 64 65

private:
    GstElement *appsrc = nullptr;
    QGstreamerAppSrc *q_ptr = nullptr;
    int timerMsec = 30;
    int timerId = 0;
    QSize size;
};

Val Doroshchuk's avatar
Val Doroshchuk committed
66
bool QGstreamerAppSrcPrivate::restart(QVideoFrame &frame)
Val Doroshchuk's avatar
Val Doroshchuk committed
67 68
{
    QList<QVideoFrame::PixelFormat> cc;
Val Doroshchuk's avatar
Val Doroshchuk committed
69
    cc << frame.pixelFormat();
Val Doroshchuk's avatar
Val Doroshchuk committed
70
    GstCaps *caps = QGstUtils::capsForFormats(cc);
Val Doroshchuk's avatar
Val Doroshchuk committed
71
    if (gst_caps_is_empty(caps)) {
Val Doroshchuk's avatar
Val Doroshchuk committed
72
        qWarning() << "Could not create caps for" << frame.pixelFormat();
Val Doroshchuk's avatar
Val Doroshchuk committed
73
        gst_caps_unref(caps);
Val Doroshchuk's avatar
Val Doroshchuk committed
74 75 76
        return false;
    }

Val Doroshchuk's avatar
Val Doroshchuk committed
77 78 79 80 81 82 83 84 85
    gst_caps_set_simple(
        caps,
        "framerate", GST_TYPE_FRACTION , 30, 1,
        "width", G_TYPE_INT, frame.width(),
        "height", G_TYPE_INT, frame.height(),
        NULL);

    g_object_set(G_OBJECT(appsrc), "caps", caps, NULL);
    gst_caps_unref(caps);
Val Doroshchuk's avatar
Val Doroshchuk committed
86
    size = frame.size();
Val Doroshchuk's avatar
Val Doroshchuk committed
87
    return true;
Val Doroshchuk's avatar
Val Doroshchuk committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
}

static void start_feed(GstElement *pipeline, guint size, QGstreamerAppSrc *q)
{
    Q_UNUSED(pipeline);
    Q_UNUSED(size);
    QMetaObject::invokeMethod(q, "startFeed", Qt::QueuedConnection);
}

static void stop_feed(GstElement *pipeline, QGstreamerAppSrc *q)
{
    Q_UNUSED(pipeline);
    QMetaObject::invokeMethod(q, "stopFeed", Qt::QueuedConnection);
}

void QGstreamerAppSrc::startFeed()
{
    Q_D(QGstreamerAppSrc);

    if (d->timerId)
        return;

    d->timerId = startTimer(d->timerMsec);
}

void QGstreamerAppSrc::stopFeed()
{
    Q_D(QGstreamerAppSrc);

    if (!d->timerId)
        return;

    killTimer(d->timerId);
    d->timerId = 0;
}

Val Doroshchuk's avatar
Val Doroshchuk committed
124
bool QGstreamerAppSrc::readFrame(QVideoFrame &frame) const
Val Doroshchuk's avatar
Val Doroshchuk committed
125
{
Val Doroshchuk's avatar
Val Doroshchuk committed
126 127
    Q_UNUSED(frame);
    return false;
Val Doroshchuk's avatar
Val Doroshchuk committed
128 129 130 131 132 133 134 135 136
}

void QGstreamerAppSrc::timerEvent(QTimerEvent *event)
{
    Q_D(QGstreamerAppSrc);

    if (event->timerId() != d->timerId)
        return;

Val Doroshchuk's avatar
Val Doroshchuk committed
137 138
    QVideoFrame frame;
    if (!readFrame(frame) || !frame.isValid() || (d->size != frame.size() && !d->restart(frame)))
Val Doroshchuk's avatar
Val Doroshchuk committed
139 140
        return;

Val Doroshchuk's avatar
Val Doroshchuk committed
141
    if (!frame.map(QAbstractVideoBuffer::ReadOnly))
Val Doroshchuk's avatar
Val Doroshchuk committed
142
        return;
Val Doroshchuk's avatar
Val Doroshchuk committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

    auto app_buffer = gst_buffer_new();
    int size = frame.bytesPerLine() * frame.height();
    auto mem = gst_allocator_alloc (NULL, size, NULL);

    GstMapInfo info;
    gst_memory_map(mem, &info, GST_MAP_WRITE);
    memcpy(info.data, frame.bits(), size);
    gst_memory_unmap(mem, &info);

    gst_buffer_append_memory(app_buffer, mem);
    gst_buffer_set_size(app_buffer, size);

    GstFlowReturn ret;
    g_signal_emit_by_name (d->appsrc, "push-buffer", app_buffer, &ret);
    if (ret != GST_FLOW_OK) {
Val Doroshchuk's avatar
Val Doroshchuk committed
159
        qWarning() << "Error has occurred while pushing a buffer";
Val Doroshchuk's avatar
Val Doroshchuk committed
160 161 162 163 164 165 166 167 168 169 170 171 172
        stopFeed();
    }

    gst_buffer_unref(app_buffer);
    frame.unmap();
}

QGstreamerAppSrc::QGstreamerAppSrc(GstElement *appsrc, QObject *parent)
    : QObject(parent)
    , d_ptr(new QGstreamerAppSrcPrivate(appsrc, this))
{
    g_signal_connect(appsrc, "need-data", G_CALLBACK(start_feed), this);
    g_signal_connect(appsrc, "enough-data", G_CALLBACK(stop_feed), this);    
Val Doroshchuk's avatar
Val Doroshchuk committed
173 174 175
    g_object_set(G_OBJECT(appsrc), "do-timestamp", TRUE, NULL);
    g_object_set(G_OBJECT(appsrc), "duration", GST_CLOCK_TIME_NONE, NULL);
    g_object_set(G_OBJECT(appsrc), "format", GST_FORMAT_TIME, NULL);
Val Doroshchuk's avatar
Val Doroshchuk committed
176 177 178 179 180 181 182 183
}

QGstreamerAppSrc::~QGstreamerAppSrc()
{
    delete d_ptr;
}

QT_END_NAMESPACE