VideoItem.cc 3.81 KB
Newer Older
Gus Grubba's avatar
Gus Grubba 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
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

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

#include <QtCore/QPointer>
#include <QtQuick/QSGNode>
#include <QtQuick/QSGFlatColorMaterial>

Gus Grubba's avatar
Gus Grubba committed
34
#include "VideoItem.h"
35
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
36
#include "VideoSurface_p.h"
37
#endif
Gus Grubba's avatar
Gus Grubba committed
38

39
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
40 41 42 43 44 45
struct VideoItem::Private
{
    QPointer<VideoSurface> surface;
    bool surfaceDirty;
    QRectF targetArea;
};
46
#endif
Gus Grubba's avatar
Gus Grubba committed
47 48

VideoItem::VideoItem(QQuickItem *parent)
49 50 51 52
    : QQuickItem(parent)
#if defined(QGC_GST_STREAMING)
    , _data(new Private)
#endif
Gus Grubba's avatar
Gus Grubba committed
53
{
54
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
55 56
    _data->surfaceDirty = true;
    setFlag(QQuickItem::ItemHasContents, true);
57
#endif
Gus Grubba's avatar
Gus Grubba committed
58 59 60 61
}

VideoItem::~VideoItem()
{
62
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
63 64
    setSurface(0);
    delete _data;
65
#endif
Gus Grubba's avatar
Gus Grubba committed
66 67 68 69
}

VideoSurface *VideoItem::surface() const
{
70
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
71
    return _data->surface.data();
72 73 74
#else
    return NULL;
#endif
Gus Grubba's avatar
Gus Grubba committed
75 76 77 78
}

void VideoItem::setSurface(VideoSurface *surface)
{
79
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
80 81 82 83 84 85 86 87
    if (_data->surface) {
        _data->surface.data()->_data->items.remove(this);
    }
    _data->surface = surface;
    _data->surfaceDirty = true;
    if (_data->surface) {
        _data->surface.data()->_data->items.insert(this);
    }
dogmaphobic's avatar
dogmaphobic committed
88 89
#else
    Q_UNUSED(surface)
90
#endif
Gus Grubba's avatar
Gus Grubba committed
91 92
}

93
#if defined(QGC_GST_STREAMING)
Gus Grubba's avatar
Gus Grubba committed
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 124 125 126 127 128 129 130 131 132 133
QSGNode* VideoItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData*)
{
    QRectF r = boundingRect();
    QSGNode* newNode = 0;

    if (_data->surfaceDirty) {
        delete oldNode;
        oldNode = 0;
        _data->surfaceDirty = false;
    }

    if (!_data->surface || _data->surface.data()->_data->videoSink == NULL) {
        if (!oldNode) {
            QSGFlatColorMaterial *material = new QSGFlatColorMaterial;
            material->setColor(Qt::black);
            QSGGeometryNode *node = new QSGGeometryNode;
            node->setMaterial(material);
            node->setFlag(QSGNode::OwnsMaterial);
            node->setFlag(QSGNode::OwnsGeometry);
            newNode = node;
            _data->targetArea = QRectF(); //force geometry to be set
        } else {
            newNode = oldNode;
        }
        if (r != _data->targetArea) {
            QSGGeometry *geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 4);
            geometry->vertexDataAsPoint2D()[0].set(r.x(), r.y());
            geometry->vertexDataAsPoint2D()[1].set(r.x(), r.height());
            geometry->vertexDataAsPoint2D()[2].set(r.width(), r.y());
            geometry->vertexDataAsPoint2D()[3].set(r.width(), r.height());
            QSGGeometryNode *node = static_cast<QSGGeometryNode*>(newNode);
            node->setGeometry(geometry);
            _data->targetArea = r;
        }
    } else {
        g_signal_emit_by_name(_data->surface.data()->_data->videoSink, "update-node", (void*)oldNode, r.x(), r.y(), r.width(), r.height(), (void**)&newNode);
    }

    return newNode;
}
134
#endif