VideoSurface.cc 1.84 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
Gus Grubba's avatar
Gus Grubba committed
9 10 11 12 13 14 15 16


/**
 * @file
 *   @brief QGC Video Surface
 *   @author Gus Grubba <mavlink@grubba.com>
 */

17
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
18
#include "VideoSurface_p.h"
19 20
#endif
#include "VideoSurface.h"
Gus Grubba's avatar
Gus Grubba committed
21 22 23 24 25 26

#include <QtCore/QDebug>
#include <QtQuick/QQuickItem>

VideoSurface::VideoSurface(QObject *parent)
    : QObject(parent)
27
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
28
    , _data(new VideoSurfacePrivate)
dogmaphobic's avatar
dogmaphobic committed
29
    , _lastFrame(0)
30
    , _refed(false)
31
#endif
Gus Grubba's avatar
Gus Grubba committed
32 33 34 35 36
{
}

VideoSurface::~VideoSurface()
{
37
#if defined(QGC_GST_STREAMING)
38
    if (!_refed && _data->videoSink != NULL) {
Gus Grubba's avatar
Gus Grubba committed
39 40 41
        gst_element_set_state(_data->videoSink, GST_STATE_NULL);
    }
    delete _data;
42
#endif
Gus Grubba's avatar
Gus Grubba committed
43 44
}

45
#if defined(QGC_GST_STREAMING)
46
GstElement* VideoSurface::videoSink()
Gus Grubba's avatar
Gus Grubba committed
47 48 49 50 51 52
{
    if (_data->videoSink == NULL) {
        if ((_data->videoSink = gst_element_factory_make("qtquick2videosink", NULL)) == NULL) {
            qCritical("Failed to create qtquick2videosink. Make sure it is installed correctly");
            return NULL;
        }
53
        g_object_set(G_OBJECT(_data->videoSink), "sync", gboolean(false), NULL);
Gus Grubba's avatar
Gus Grubba committed
54
        g_signal_connect(_data->videoSink, "update", G_CALLBACK(onUpdateThunk), (void* )this);
55
        _refed = true;
Gus Grubba's avatar
Gus Grubba committed
56 57 58 59 60 61
    }
    return _data->videoSink;
}

void VideoSurface::onUpdate()
{
dogmaphobic's avatar
dogmaphobic committed
62
    _lastFrame = time(0);
Gus Grubba's avatar
Gus Grubba committed
63 64 65 66 67 68 69 70 71 72 73
    Q_FOREACH(QQuickItem *item, _data->items) {
        item->update();
    }
}

void VideoSurface::onUpdateThunk(GstElement* sink, gpointer data)
{
    Q_UNUSED(sink);
    VideoSurface* pThis = (VideoSurface* )data;
    pThis->onUpdate();
}
74
#endif
Gus Grubba's avatar
Gus Grubba committed
75