summaryrefslogtreecommitdiff
path: root/elements/gstqtvideosink/gstqwidgetvideosink.cpp
blob: ed045695dbda12f1d8b3356d4858ca9fd722babc (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
    Copyright (C) 2010 George Kiagiadakis <kiagiadakis.george@gmail.com>
    Copyright (C) 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 as published
    by the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.

    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 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/>.
*/

/**
 * SECTION:element-qwidgetvideosink
 *
 * qwidgetvideosink is a video sink element that can draw directly on a QWidget
 * using the QPainter API. To use it, you only have to set a QWidget pointer as
 * the "widget" property and the video will then be rendered on that QWidget.
 *
 * This is useful for cases where you cannot or you do not want to use one of the
 * sinks that implement the GstXOverlay interface, for example for rendering video
 * inside a QGraphicsView or for rendering video on the QWS (Qt/Embedded) platform.
 * This sink is guaranteed to work on all platforms supported by Qt, however it
 * is not recommended to use it if you have another choice. For example, on X11 it
 * is recommended to use "xvimagesink" instead, which uses hardware acceleration.
 *
 * There are certain rules for using qwidgetvideosink with threads. It must be
 * created in the main thread, it must be destructed in the main thread and the
 * "widget" property may only be read or written from the main thread as well.
 * This is a (reasonable) limitation implied by Qt.
 */

#include "gstqwidgetvideosink.h"
#include "qtvideosinkdelegate.h"
#include <QtCore/QWeakPointer>
#include <QtCore/QEvent>
#include <QtGui/QWidget>
#include <QtGui/QPainter>

//BEGIN ******** WidgetProxy ********

class WidgetProxy : public QObject
{
    Q_OBJECT
public:
    WidgetProxy(GstQWidgetVideoSink *sink);
    virtual ~WidgetProxy();

    // "widget" property
    QWidget *widget() const;
    void setWidget(QWidget *widget);

private Q_SLOTS:
    void widgetDestroyed();

protected:
    virtual bool eventFilter(QObject *filteredObject, QEvent *event);

private:
    GstQWidgetVideoSink *m_sink;

    // "widget" property
    QWeakPointer<QWidget> m_widget;

    // original value of the Qt::WA_OpaquePaintEvent attribute
    bool m_opaquePaintEventAttribute;
};

WidgetProxy::WidgetProxy(GstQWidgetVideoSink *sink)
    : QObject(),
      m_sink(sink)
{
}

WidgetProxy::~WidgetProxy()
{
    setWidget(NULL);
}

QWidget *WidgetProxy::widget() const
{
    return m_widget.data();
}

void WidgetProxy::setWidget(QWidget *widget)
{
    GST_LOG_OBJECT(m_sink, "Setting \"widget\" property to %"GST_PTR_FORMAT, widget);

    if (m_widget) {
        m_widget.data()->removeEventFilter(this);
        m_widget.data()->setAttribute(Qt::WA_OpaquePaintEvent, m_opaquePaintEventAttribute);
        m_widget.data()->update();
        disconnect(m_widget.data(), SIGNAL(destroyed(QObject*)), this, SLOT(widgetDestroyed()));

        m_widget = QWeakPointer<QWidget>();
    }

    if (widget) {
        widget->installEventFilter(this);
        m_opaquePaintEventAttribute = widget->testAttribute(Qt::WA_OpaquePaintEvent);
        widget->setAttribute(Qt::WA_OpaquePaintEvent, true);
        widget->update();
        connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(widgetDestroyed()));

        m_widget = widget;
    }
}

void WidgetProxy::widgetDestroyed()
{
    m_widget = QWeakPointer<QWidget>();
}

bool WidgetProxy::eventFilter(QObject *filteredObject, QEvent *event)
{
    if (filteredObject == m_widget.data()) {
        switch(event->type()) {
        case QEvent::Paint:
          {
            QPainter painter(m_widget.data());
            QRect rect = m_widget.data()->rect();
            GST_QT_VIDEO_SINK_BASE(m_sink)->surface->paint(&painter,
                    rect.x(), rect.y(), rect.width(), rect.height());
            return true;
          }
        default:
            return false;
        }
    } else {
        return QObject::eventFilter(filteredObject, event);
    }
}

//END ******** WidgetProxy ********
//BEGIN ******** GstQWidgetVideoSink ********

GstQtVideoSinkBaseClass *GstQWidgetVideoSink::s_parent_class = NULL;

GType GstQWidgetVideoSink::get_type()
{
  /* The typedef for GType may be gulong or gsize, depending on the
   * system and whether the compiler is c++ or not. The g_once_init_*
   * functions always take a gsize * though ... */
    static volatile gsize gonce_data = 0;
    if (g_once_init_enter(&gonce_data)) {
        GType type;
        type = gst_type_register_static_full(
            GST_TYPE_QT_VIDEO_SINK_BASE,
            g_intern_static_string("GstQWidgetVideoSink"),
            sizeof(GstQWidgetVideoSinkClass),
            &GstQWidgetVideoSink::base_init,
            NULL,   /* base_finalize */
            &GstQWidgetVideoSink::class_init,
            NULL,   /* class_finalize */
            NULL,   /* class_data */
            sizeof(GstQWidgetVideoSink),
            0,      /* n_preallocs */
            &GstQWidgetVideoSink::init,
            NULL,
            (GTypeFlags) 0);
        g_once_init_leave(&gonce_data, (gsize) type);
    }
    return (GType) gonce_data;
}

void GstQWidgetVideoSink::base_init(gpointer gclass)
{
    GstElementClass *element_class = GST_ELEMENT_CLASS(gclass);

    gst_element_class_set_details_simple(element_class, "QWidget video sink", "Sink/Video",
        "A video sink that draws on a QWidget using QPainter",
        "George Kiagiadakis <george.kiagiadakis@collabora.com>");
}

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

    s_parent_class = GST_QT_VIDEO_SINK_BASE_CLASS(g_type_class_peek_parent(g_class));

    GObjectClass *gobject_class = G_OBJECT_CLASS(g_class);
    gobject_class->finalize = GstQWidgetVideoSink::finalize;
    gobject_class->set_property = GstQWidgetVideoSink::set_property;
    gobject_class->get_property = GstQWidgetVideoSink::get_property;

    GstQtVideoSinkBaseClass *qt_video_sink_base_class = GST_QT_VIDEO_SINK_BASE_CLASS(g_class);
    qt_video_sink_base_class->update = GstQWidgetVideoSink::update;

    /**
     * GstQWidgetVideoSink::widget
     *
     * This property holds a pointer to the QWidget on which the sink will paint the video.
     * You can set this property at any time, even if the element is in PLAYING
     * state. You can also set this property to NULL at any time to release
     * the widget. In this case, qwidgetvideosink will behave like a fakesink,
     * i.e. it will silently drop all the frames that it receives. It is also safe
     * to delete the widget that has been set as this property; the sink will be
     * signaled and this property will automatically be set to NULL.
     **/
    g_object_class_install_property(gobject_class, PROP_WIDGET,
        g_param_spec_pointer("widget", "Widget",
                             "The widget on which this element will paint the video",
                             static_cast<GParamFlags>(G_PARAM_READWRITE)));
}

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

    GstQWidgetVideoSink *sink = GST_QWIDGET_VIDEO_SINK(instance);
    sink->proxy = new WidgetProxy(sink);
}

void GstQWidgetVideoSink::finalize(GObject *object)
{
    GstQWidgetVideoSink *sink = GST_QWIDGET_VIDEO_SINK(object);

    delete sink->proxy;
    sink->proxy = NULL;

    G_OBJECT_CLASS(s_parent_class)->finalize(object);
}

void GstQWidgetVideoSink::set_property(GObject *object, guint prop_id,
                                       const GValue *value, GParamSpec *pspec)
{
    GstQWidgetVideoSink *sink = GST_QWIDGET_VIDEO_SINK(object);

    switch (prop_id) {
    case PROP_WIDGET:
        sink->proxy->setWidget(static_cast<QWidget*>(g_value_get_pointer(value)));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

void GstQWidgetVideoSink::get_property(GObject *object, guint prop_id,
                                       GValue *value, GParamSpec *pspec)
{
    GstQWidgetVideoSink *sink = GST_QWIDGET_VIDEO_SINK(object);

    switch (prop_id) {
    case PROP_WIDGET:
        g_value_set_pointer(value, sink->proxy->widget());
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

void GstQWidgetVideoSink::update(GstQtVideoSinkBase *sink)
{
    QWidget *w = GST_QWIDGET_VIDEO_SINK(sink)->proxy->widget();
    if (w) {
        w->update();
    }
}

//END ******** GstQWidgetVideoSink ********

#include "gstqwidgetvideosink.moc"