summaryrefslogtreecommitdiff
path: root/elements/gstqtvideosink/gstqtglvideosink.cpp
blob: f7786aef8b7376c58f29490dec01ac69af3d4dcd (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
    Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). <qt-info@nokia.com>
    Copyright (C) 2011-2012 Collabora Ltd. <info@collabora.com>

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License version 2.1
    as published by the Free Software Foundation.

    This program 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 Lesser General Public License for more details.

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

#include "gstqtglvideosink.h"
#include "gstqtvideosinkmarshal.h"
#include "qtvideosinkdelegate.h"


guint GstQtGLVideoSink::s_signals[];

DEFINE_TYPE(GstQtGLVideoSink, GST_TYPE_QT_GL_VIDEO_SINK_BASE)

//------------------------------

void GstQtGLVideoSink::emit_update(gpointer sink)
{
    g_signal_emit(sink, GstQtGLVideoSink::s_signals[UPDATE_SIGNAL], 0, NULL);
}

//------------------------------

void GstQtGLVideoSink::base_init(gpointer g_class)
{
    GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);

    gst_element_class_set_details_simple(element_class, "Qt GL video sink", "Sink/Video",
        "A video sink that can draw on any Qt GL surface",
        "George Kiagiadakis <george.kiagiadakis@collabora.com>");
}

void GstQtGLVideoSink::class_init(gpointer g_class, gpointer class_data)
{
    Q_UNUSED(class_data);

    GObjectClass *object_class = G_OBJECT_CLASS(g_class);
    object_class->set_property = GstQtGLVideoSink::set_property;

    GstQtGLVideoSinkClass *qt_video_sink_class = reinterpret_cast<GstQtGLVideoSinkClass*>(g_class);
    qt_video_sink_class->paint = GstQtGLVideoSink::paint;

    /**
     * GstQtGLVideoSink::paint
     * @painter: A valid QPainter pointer that will be used to paint the video
     * @x: The x coordinate of the target area rectangle
     * @y: The y coordinate of the target area rectangle
     * @width: The width of the target area rectangle
     * @height: The height of the target area rectangle
     *
     * This is an action signal that you can call from your Qt surface class inside
     * its paint function to render the video. It takes a QPainter* and the target
     * area rectangle as arguments. You should schedule to call this function to
     * repaint the surface whenever the ::update signal is emited.
     *
     * Note that the x,y,width and height arguments are actually qreal. This means
     * that on architectures like arm they will be float instead of double. You should
     * cast the arguments to qreal if they are not already when emitting this signal.
     */
    s_signals[PAINT_SIGNAL] =
        g_signal_new("paint", G_TYPE_FROM_CLASS(g_class),
                     static_cast<GSignalFlags>(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
                     G_STRUCT_OFFSET(GstQtGLVideoSinkClass, paint),
                     NULL, NULL,
                     qRealIsDouble() ?
                        g_cclosure_user_marshal_VOID__POINTER_DOUBLE_DOUBLE_DOUBLE_DOUBLE :
                        g_cclosure_user_marshal_VOID__POINTER_FLOAT_FLOAT_FLOAT_FLOAT,
                     G_TYPE_NONE, 5,
                     G_TYPE_POINTER, G_TYPE_QREAL, G_TYPE_QREAL, G_TYPE_QREAL, G_TYPE_QREAL);

    /**
     * GstQtGLVideoSink::update
     *
     * This signal is emited when the surface should be repainted. It should
     * be connected to QWidget::update() or QGraphicsItem::update() or any
     * other similar function in your surface.
     */
    s_signals[UPDATE_SIGNAL] =
        g_signal_new("update", G_TYPE_FROM_CLASS(g_class),
                     G_SIGNAL_RUN_LAST,
                     0, NULL, NULL,
                     g_cclosure_marshal_VOID__VOID,
                     G_TYPE_NONE, 0);


    /**
     * GstQtGLVideoSink::glcontext
     *
     * This property holds a pointer to the QGLContext that will be used to render
     * the video using OpenGL acceleration. You must set this to a valid QGLContext
     * pointer before the element changes state to READY, or else the state change will fail.
     **/
    g_object_class_install_property(object_class, PROP_GLCONTEXT,
        g_param_spec_pointer("glcontext", "GL context",
                             "The QGLContext that will be used to do OpenGL-accelerated rendering",
                             static_cast<GParamFlags>(G_PARAM_WRITABLE)));
}

void GstQtGLVideoSink::init(GTypeInstance *instance, gpointer g_class)
{
    Q_UNUSED(g_class);

    GstQtVideoSinkBase *sinkBase = GST_QT_VIDEO_SINK_BASE(instance);
    sinkBase->delegate = new QtVideoSinkDelegate(sinkBase);
}

//------------------------------

void GstQtGLVideoSink::set_property(GObject *object, guint prop_id,
                                     const GValue *value, GParamSpec *pspec)
{
    GstQtVideoSinkBase *sinkBase = GST_QT_VIDEO_SINK_BASE(object);

    switch (prop_id) {
    case PROP_GLCONTEXT:
        sinkBase->delegate->setGLContext(static_cast<QGLContext*>(g_value_get_pointer(value)));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

//------------------------------

void GstQtGLVideoSink::paint(GstQtGLVideoSink *sink, gpointer painter,
                           qreal x, qreal y, qreal width, qreal height)
{
    GST_QT_VIDEO_SINK_BASE(sink)->delegate->paint(static_cast<QPainter*>(painter),
                                                  QRectF(x, y, width, height));
}