summaryrefslogtreecommitdiff
path: root/elements/gstqtvideosink/gstqtquick2videosink.cpp
blob: 156f7baa6861faa4bed30f303c2b491ea2425e48 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
    Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). <qt-info@nokia.com>
    Copyright (C) 2011-2013 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 "gstqtquick2videosink.h"
#include "gstqtvideosinkplugin.h"
#include "gstqtvideosinkmarshal.h"
#include "delegates/qtquick2videosinkdelegate.h"

#include <gst/interfaces/colorbalance.h>

#include <cstring>
#include <QCoreApplication>

#define GST_QT_QUICK2_VIDEO_SINK_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_QT_QUICK2_VIDEO_SINK, GstQtQuick2VideoSinkPrivate))

struct _GstQtQuick2VideoSinkPrivate
{
    QtQuick2VideoSinkDelegate *delegate;
    GList *channels_list;
    bool formatDirty;
};

static void gst_qt_quick2_video_sink_init_interfaces (GType g_define_type_id);

GST_BOILERPLATE_FULL (GstQtQuick2VideoSink, gst_qt_quick2_video_sink,
        GstVideoSink, GST_TYPE_VIDEO_SINK,
        gst_qt_quick2_video_sink_init_interfaces);

enum {
    PROP_0,
    PROP_PIXEL_ASPECT_RATIO,
    PROP_FORCE_ASPECT_RATIO,
    PROP_CONTRAST,
    PROP_BRIGHTNESS,
    PROP_HUE,
    PROP_SATURATION,
};

enum {
    ACTION_UPDATE_NODE,
    SIGNAL_UPDATE,
    LAST_SIGNAL
};

static guint s_signals[LAST_SIGNAL] = { 0 };

const char * const s_colorbalance_labels[] = {
    "contrast", "brightness", "hue", "saturation"
};

//index for s_colorbalance_labels
enum {
    LABEL_CONTRAST = 0,
    LABEL_BRIGHTNESS,
    LABEL_HUE,
    LABEL_SATURATION,
    LABEL_LAST
};

static void
gst_qt_quick2_video_sink_init (GstQtQuick2VideoSink *self,
                               GstQtQuick2VideoSinkClass *klass)
{
    Q_UNUSED(klass);
    self->priv = GST_QT_QUICK2_VIDEO_SINK_GET_PRIVATE (self);

    // delegate
    self->priv->delegate = new QtQuick2VideoSinkDelegate(GST_ELEMENT(self));
    self->priv->formatDirty = true;

    // colorbalance
    GstColorBalanceChannel *channel;
    self->priv->channels_list = NULL;

    for (int i=0; i < LABEL_LAST; i++) {
        channel = GST_COLOR_BALANCE_CHANNEL(g_object_new(GST_TYPE_COLOR_BALANCE_CHANNEL, NULL));
        channel->label = g_strdup(s_colorbalance_labels[i]);
        channel->min_value = -100;
        channel->max_value = 100;

        self->priv->channels_list = g_list_append(self->priv->channels_list, channel);
    }
}

static void
gst_qt_quick2_video_sink_finalize (GObject *gobject)
{
    GstQtQuick2VideoSink *self = GST_QT_QUICK2_VIDEO_SINK (gobject);

    delete self->priv->delegate;
    self->priv->delegate = 0;

    while (self->priv->channels_list) {
        GstColorBalanceChannel *channel =
            GST_COLOR_BALANCE_CHANNEL(self->priv->channels_list->data);
        g_object_unref(channel);
        self->priv->channels_list = g_list_next(self->priv->channels_list);
    }

    g_list_free(self->priv->channels_list);

    G_OBJECT_CLASS (parent_class)->finalize (gobject);
}

static void
gst_qt_quick2_video_sink_set_property (GObject *object,
                                       guint property_id,
                                       const GValue *value,
                                       GParamSpec *pspec)
{
    GstQtQuick2VideoSink *self = GST_QT_QUICK2_VIDEO_SINK (object);

    switch (property_id) {
    case PROP_PIXEL_ASPECT_RATIO:
      {
        GValue tmp;
        std::memset(&tmp, 0, sizeof(GValue));
        g_value_init(&tmp, GST_TYPE_FRACTION);
        if (g_value_transform(value, &tmp)) {
            int n = gst_value_get_fraction_numerator(&tmp);
            int d = gst_value_get_fraction_denominator(&tmp);
            self->priv->delegate->setPixelAspectRatio(Fraction(n, d));
        } else {
            GST_WARNING_OBJECT(object, "Could not transform string to aspect ratio");
        }
        g_value_unset(&tmp);
        break;
      }
    case PROP_FORCE_ASPECT_RATIO:
        self->priv->delegate->setForceAspectRatio(g_value_get_boolean(value));
        break;
    case PROP_CONTRAST:
        self->priv->delegate->setContrast(g_value_get_int(value));
        break;
    case PROP_BRIGHTNESS:
        self->priv->delegate->setBrightness(g_value_get_int(value));
        break;
    case PROP_HUE:
        self->priv->delegate->setHue(g_value_get_int(value));
        break;
    case PROP_SATURATION:
        self->priv->delegate->setSaturation(g_value_get_int(value));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
gst_qt_quick2_video_sink_get_property (GObject *object,
                                       guint property_id,
                                       GValue *value,
                                       GParamSpec *pspec)
{
    GstQtQuick2VideoSink *self = GST_QT_QUICK2_VIDEO_SINK (object);

    switch (property_id) {
    case PROP_PIXEL_ASPECT_RATIO:
      {
        GValue tmp;
        Fraction par = self->priv->delegate->pixelAspectRatio();
        std::memset(&tmp, 0, sizeof(GValue));
        g_value_init(&tmp, GST_TYPE_FRACTION);
        gst_value_set_fraction(&tmp, par.numerator, par.denominator);
        g_value_transform(&tmp, value);
        g_value_unset(&tmp);
        break;
      }
    case PROP_FORCE_ASPECT_RATIO:
        g_value_set_boolean(value, self->priv->delegate->forceAspectRatio());
        break;
    case PROP_CONTRAST:
        g_value_set_int(value, self->priv->delegate->contrast());
        break;
    case PROP_BRIGHTNESS:
        g_value_set_int(value, self->priv->delegate->brightness());
        break;
    case PROP_HUE:
        g_value_set_int(value, self->priv->delegate->hue());
        break;
    case PROP_SATURATION:
        g_value_set_int(value, self->priv->delegate->saturation());
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static GstStateChangeReturn
gst_qt_quick2_video_sink_change_state(GstElement *element,
                                      GstStateChange transition)
{
    GstQtQuick2VideoSink *self = GST_QT_QUICK2_VIDEO_SINK (element);

    switch (transition) {
    case GST_STATE_CHANGE_READY_TO_PAUSED:
        self->priv->delegate->setActive(true);
        break;
    case GST_STATE_CHANGE_PAUSED_TO_READY:
        self->priv->delegate->setActive(false);
        break;
    default:
        break;
    }

    return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
}

static gboolean
gst_qt_quick2_video_sink_set_caps(GstBaseSink *sink, GstCaps *caps)
{
    GstQtQuick2VideoSink *self = GST_QT_QUICK2_VIDEO_SINK (sink);

    GST_LOG_OBJECT(self, "new caps %" GST_PTR_FORMAT, caps);
    self->priv->formatDirty = true;
    return TRUE;
}

static GstFlowReturn
gst_qt_quick2_video_sink_show_frame(GstVideoSink *sink, GstBuffer *buffer)
{
    GstQtQuick2VideoSink *self = GST_QT_QUICK2_VIDEO_SINK (sink);

    GST_TRACE_OBJECT(self, "Posting new buffer (%"GST_PTR_FORMAT") for rendering. "
                           "Format dirty: %d", buffer, (int)self->priv->formatDirty);

    QCoreApplication::postEvent(self->priv->delegate,
            new BaseDelegate::BufferEvent(buffer, self->priv->formatDirty));

    self->priv->formatDirty = false;
    return GST_FLOW_OK;
}

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

static gpointer
gst_qt_quick2_video_sink_update_node(GstQtQuick2VideoSink *self, gpointer node,
                                     qreal x, qreal y, qreal w, qreal h)
{
      return self->priv->delegate->updateNode(static_cast<QSGNode*>(node),
                                              QRectF(x, y, w, h));
}

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

static gboolean
gst_qt_quick2_video_sink_interface_supported(GstImplementsInterface *iface, GType type)
{
    Q_UNUSED(iface);
    return type == GST_TYPE_COLOR_BALANCE;
}

static void
gst_qt_quick2_video_sink_implementsiface_init(GstImplementsInterfaceClass *klass, gpointer data)
{
    Q_UNUSED(data);
    klass->supported = gst_qt_quick2_video_sink_interface_supported;
}

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

static const GList *
gst_qt_quick2_video_sink_colorbalance_list_channels(GstColorBalance *balance)
{
    return GST_QT_QUICK2_VIDEO_SINK (balance)->priv->channels_list;
}

static void
gst_qt_quick2_video_sink_colorbalance_set_value(GstColorBalance *balance,
                                                  GstColorBalanceChannel *channel, gint value)
{
    GstQtQuick2VideoSink *self = GST_QT_QUICK2_VIDEO_SINK (balance);

    if (!qstrcmp(channel->label, s_colorbalance_labels[LABEL_CONTRAST])) {
        self->priv->delegate->setContrast(value);
    } else if (!qstrcmp(channel->label, s_colorbalance_labels[LABEL_BRIGHTNESS])) {
        self->priv->delegate->setBrightness(value);
    } else if (!qstrcmp(channel->label, s_colorbalance_labels[LABEL_HUE])) {
        self->priv->delegate->setHue(value);
    } else if (!qstrcmp(channel->label, s_colorbalance_labels[LABEL_SATURATION])) {
        self->priv->delegate->setSaturation(value);
    } else {
        GST_WARNING_OBJECT(self, "Unknown colorbalance channel %s", channel->label);
    }
}

static gint
gst_qt_quick2_video_sink_colorbalance_get_value(GstColorBalance *balance,
                                                GstColorBalanceChannel *channel)
{
    GstQtQuick2VideoSink *self = GST_QT_QUICK2_VIDEO_SINK (balance);

    if (!qstrcmp(channel->label, s_colorbalance_labels[LABEL_CONTRAST])) {
        return self->priv->delegate->contrast();
    } else if (!qstrcmp(channel->label, s_colorbalance_labels[LABEL_BRIGHTNESS])) {
        return self->priv->delegate->brightness();
    } else if (!qstrcmp(channel->label, s_colorbalance_labels[LABEL_HUE])) {
        return self->priv->delegate->hue();
    } else if (!qstrcmp(channel->label, s_colorbalance_labels[LABEL_SATURATION])) {
        return self->priv->delegate->saturation();
    } else {
        GST_WARNING_OBJECT(self, "Unknown colorbalance channel %s", channel->label);
    }

    return 0;
}

static void
gst_qt_quick2_video_sink_colorbalance_init(GstColorBalanceClass *klass, gpointer data)
{
    Q_UNUSED(data);
    GST_COLOR_BALANCE_TYPE(klass) = GST_COLOR_BALANCE_HARDWARE;
    klass->list_channels = gst_qt_quick2_video_sink_colorbalance_list_channels;
    klass->set_value = gst_qt_quick2_video_sink_colorbalance_set_value;
    klass->get_value = gst_qt_quick2_video_sink_colorbalance_get_value;
}

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

static void
gst_qt_quick2_video_sink_base_init (gpointer g_class)
{
    GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);

    static GstVideoFormat supportedFormats[] = {
        GST_VIDEO_FORMAT_BGRA,
        GST_VIDEO_FORMAT_BGRx,
        GST_VIDEO_FORMAT_ARGB,
        GST_VIDEO_FORMAT_xRGB,
        GST_VIDEO_FORMAT_RGB,
        GST_VIDEO_FORMAT_RGB16,
        GST_VIDEO_FORMAT_BGR,
        GST_VIDEO_FORMAT_v308,
        GST_VIDEO_FORMAT_AYUV,
        GST_VIDEO_FORMAT_YV12,
        GST_VIDEO_FORMAT_I420
    };

    GstCaps *caps = gst_caps_new_empty();
    for (uint i = 0; i < sizeof(supportedFormats) / sizeof(GstVideoFormat); i++) {
        gst_caps_append(caps, BufferFormat::newTemplateCaps(supportedFormats[i]));
    }

    GstPadTemplate *pad_tmpl = gst_pad_template_new ("sink",
        GST_PAD_SINK, GST_PAD_ALWAYS, caps);
    gst_element_class_add_pad_template(element_class, pad_tmpl);

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

static void
gst_qt_quick2_video_sink_class_init (GstQtQuick2VideoSinkClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
    gobject_class->finalize = gst_qt_quick2_video_sink_finalize;
    gobject_class->set_property = gst_qt_quick2_video_sink_set_property;
    gobject_class->get_property = gst_qt_quick2_video_sink_get_property;

    GstElementClass *element_class = GST_ELEMENT_CLASS(klass);
    element_class->change_state = gst_qt_quick2_video_sink_change_state;

    GstBaseSinkClass *base_sink_class = GST_BASE_SINK_CLASS(klass);
    base_sink_class->set_caps = gst_qt_quick2_video_sink_set_caps;

    GstVideoSinkClass *video_sink_class = GST_VIDEO_SINK_CLASS(klass);
    video_sink_class->show_frame = gst_qt_quick2_video_sink_show_frame;

    GstQtQuick2VideoSinkClass *qtquick2_class = GST_QT_QUICK2_VIDEO_SINK_CLASS(klass);
    qtquick2_class->update_node = gst_qt_quick2_video_sink_update_node;

    /**
     * GstQtQuick2VideoSink::pixel-aspect-ratio
     *
     * The pixel aspect ratio of the display device.
     **/
    g_object_class_install_property(gobject_class, PROP_PIXEL_ASPECT_RATIO,
        g_param_spec_string("pixel-aspect-ratio", "Pixel aspect ratio",
                            "The pixel aspect ratio of the display device",
                            "1/1", static_cast<GParamFlags>(G_PARAM_READWRITE)));

    /**
     * GstQtQuick2VideoSink::force-aspect-ratio
     *
     * If set to TRUE, the sink will scale the video respecting its original aspect ratio
     * and any remaining space will be filled with black.
     * If set to FALSE, the sink will scale the video to fit the whole drawing area.
     **/
    g_object_class_install_property(gobject_class, PROP_FORCE_ASPECT_RATIO,
        g_param_spec_boolean("force-aspect-ratio", "Force aspect ratio",
                             "When enabled, scaling will respect original aspect ratio",
                             FALSE, static_cast<GParamFlags>(G_PARAM_READWRITE)));

    g_object_class_install_property(gobject_class, PROP_CONTRAST,
        g_param_spec_int("contrast", "Contrast", "The contrast of the video",
                         -100, 100, 0, static_cast<GParamFlags>(G_PARAM_READWRITE)));

    g_object_class_install_property(gobject_class, PROP_BRIGHTNESS,
        g_param_spec_int("brightness", "Brightness", "The brightness of the video",
                         -100, 100, 0, static_cast<GParamFlags>(G_PARAM_READWRITE)));

    g_object_class_install_property(gobject_class, PROP_HUE,
        g_param_spec_int("hue", "Hue", "The hue of the video",
                         -100, 100, 0, static_cast<GParamFlags>(G_PARAM_READWRITE)));

    g_object_class_install_property(gobject_class, PROP_SATURATION,
        g_param_spec_int("saturation", "Saturation", "The saturation of the video",
                         -100, 100, 0, static_cast<GParamFlags>(G_PARAM_READWRITE)));


    /**
     * GstQtQuick2VideoSink::update-node
     * @node: The QSGNode to update
     * @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
     * @returns: The updated QGSNode
     *
     * This is an action signal that you can call from your QQuickItem subclass
     * inside its updateNode function to render the video. It takes a QSGNode*
     * and the item's 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[ACTION_UPDATE_NODE] =
        g_signal_new("update-node", G_TYPE_FROM_CLASS(klass),
            static_cast<GSignalFlags>(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
            G_STRUCT_OFFSET(GstQtQuick2VideoSinkClass, update_node),
            NULL, NULL,
            qRealIsDouble() ?
              g_cclosure_user_marshal_POINTER__POINTER_DOUBLE_DOUBLE_DOUBLE_DOUBLE :
              g_cclosure_user_marshal_POINTER__POINTER_FLOAT_FLOAT_FLOAT_FLOAT,
            G_TYPE_POINTER, 5,
            G_TYPE_POINTER, G_TYPE_QREAL, G_TYPE_QREAL, G_TYPE_QREAL, G_TYPE_QREAL);

    /**
     * GstQtQuick2VideoSink::update
     *
     * This signal is emited when the surface should be repainted. It should
     * be connected to QQuickItem::update().
     */
    s_signals[SIGNAL_UPDATE] =
        g_signal_new("update", G_TYPE_FROM_CLASS(klass),
            G_SIGNAL_RUN_LAST,
            0, NULL, NULL,
            g_cclosure_marshal_VOID__VOID,
            G_TYPE_NONE, 0);

    g_type_class_add_private (klass, sizeof (GstQtQuick2VideoSinkPrivate));
}

static void
gst_qt_quick2_video_sink_init_interfaces (GType g_define_type_id)
{
    G_IMPLEMENT_INTERFACE (GST_TYPE_IMPLEMENTS_INTERFACE,
                           gst_qt_quick2_video_sink_implementsiface_init);
    G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
                           gst_qt_quick2_video_sink_colorbalance_init);
}