gstqtquick2videosink.cpp 16.3 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 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
/*
    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/>.
*/

/**
 * @file
 *   @brief Extracted from QtGstreamer to avoid overly complex dependency
 *   @author Gus Grubba <mavlink@grubba.com>
 */

#include "gstqtquick2videosink.h"
#include "gstqtvideosinkplugin.h"
#include "gstqtvideosinkmarshal.h"
#include "delegates/qtquick2videosinkdelegate.h"

#include <gst/video/colorbalance.h>

#include <cstring>
#include <QCoreApplication>

#define CAPS_FORMATS "{ BGRA, BGRx, ARGB, xRGB, RGB, RGB16, BGR, v308, AYUV, YV12, I420 }"

#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;
};

static void gst_qt_quick2_video_sink_colorbalance_init (GstColorBalanceInterface * iface, gpointer data);

#define parent_class gst_qt_quick2_video_sink_parent_class
G_DEFINE_TYPE_WITH_CODE (GstQtQuick2VideoSink, gst_qt_quick2_video_sink,
        GST_TYPE_VIDEO_SINK,
        G_IMPLEMENT_INTERFACE (GST_TYPE_COLOR_BALANCE,
                gst_qt_quick2_video_sink_colorbalance_init));

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)
{
    self->priv = GST_QT_QUICK2_VIDEO_SINK_GET_PRIVATE (self);

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

    // colorbalance
    GstColorBalanceChannel *channel;
94
    self->priv->channels_list = nullptr;
Gus Grubba's avatar
Gus Grubba committed
95 96

    for (int i=0; i < LABEL_LAST; i++) {
97
        channel = GST_COLOR_BALANCE_CHANNEL(g_object_new(GST_TYPE_COLOR_BALANCE_CHANNEL, nullptr));
Gus Grubba's avatar
Gus Grubba committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111
        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;
112
    self->priv->delegate = nullptr;
Gus Grubba's avatar
Gus Grubba committed
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

    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);
    BufferFormat format = BufferFormat::fromCaps(caps);

    //too lazy to do proper checks. if the format is not UNKNOWN, then
    //it should conform to the template caps formats, unless gstreamer
    //core has a bug.
    if (format.videoFormat() != GST_VIDEO_FORMAT_UNKNOWN) {
        QCoreApplication::postEvent(self->priv->delegate,
                                    new BaseDelegate::BufferFormatEvent(format));
        return TRUE;
    } else {
        return FALSE;
    }
}

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

dogmaphobic's avatar
dogmaphobic committed
257
    GST_TRACE_OBJECT(self, "Posting new buffer (%" GST_PTR_FORMAT ") for rendering.", buffer);
Gus Grubba's avatar
Gus Grubba committed
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

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

    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 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 GstColorBalanceType
gst_qt_quick2_video_sink_colorbalance_get_balance_type (GstColorBalance * balance)
{
    Q_UNUSED(balance);
    return GST_COLOR_BALANCE_HARDWARE;
}

static void
gst_qt_quick2_video_sink_colorbalance_init(GstColorBalanceInterface *iface, gpointer data)
{
    Q_UNUSED(data);
    iface->list_channels = gst_qt_quick2_video_sink_colorbalance_list_channels;
    iface->set_value = gst_qt_quick2_video_sink_colorbalance_set_value;
    iface->get_value = gst_qt_quick2_video_sink_colorbalance_get_value;
    iface->get_balance_type = gst_qt_quick2_video_sink_colorbalance_get_balance_type;
}

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

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
Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
413
     * emitted.
Gus Grubba's avatar
Gus Grubba committed
414 415 416 417 418 419 420 421 422 423 424
     *
     * 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),
425
            nullptr, nullptr,
Gus Grubba's avatar
Gus Grubba committed
426 427 428 429 430 431 432 433 434
            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
     *
Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
435
     * This signal is emitted when the surface should be repainted. It should
Gus Grubba's avatar
Gus Grubba committed
436 437 438 439 440
     * be connected to QQuickItem::update().
     */
    s_signals[SIGNAL_UPDATE] =
        g_signal_new("update", G_TYPE_FROM_CLASS(klass),
            G_SIGNAL_RUN_LAST,
441
            0, nullptr, nullptr,
Gus Grubba's avatar
Gus Grubba committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
            g_cclosure_marshal_VOID__VOID,
            G_TYPE_NONE, 0);

    g_type_class_add_private (klass, sizeof (GstQtQuick2VideoSinkPrivate));

    static GstStaticPadTemplate sink_pad_template =
        GST_STATIC_PAD_TEMPLATE("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
            GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (CAPS_FORMATS))
        );

    gst_element_class_add_pad_template(
            element_class, gst_static_pad_template_get(&sink_pad_template));

    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>");
}