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
|
/*
* Copyright (C) 2007 OpenedHand
* Copyright (C) 2007 Alp Toker <alp@atoker.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 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* SECTION:webkit-video-sink
* @short_description: GStreamer video sink
*
* #WebKitVideoSink is a GStreamer sink element that triggers
* repaints in the WebKit GStreamer media player for the
* current video buffer.
*/
#include "config.h"
#include "VideoSinkGStreamer.h"
#if USE(GSTREAMER)
#include <glib.h>
#include <gst/gst.h>
#include <gst/video/video.h>
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE("sink",
GST_PAD_SINK, GST_PAD_ALWAYS,
// CAIRO_FORMAT_RGB24 used to render the video buffers is little/big endian dependant.
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
GST_STATIC_CAPS(GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_BGRA)
#else
GST_STATIC_CAPS(GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_ARGB)
#endif
);
GST_DEBUG_CATEGORY_STATIC(webkit_video_sink_debug);
#define GST_CAT_DEFAULT webkit_video_sink_debug
enum {
REPAINT_REQUESTED,
LAST_SIGNAL
};
enum {
PROP_0
};
static guint webkit_video_sink_signals[LAST_SIGNAL] = { 0, };
struct _WebKitVideoSinkPrivate {
GstBuffer* buffer;
guint timeout_id;
GMutex* buffer_mutex;
GCond* data_cond;
// If this is TRUE all processing should finish ASAP
// This is necessary because there could be a race between
// unlock() and render(), where unlock() wins, signals the
// GCond, then render() tries to render a frame although
// everything else isn't running anymore. This will lead
// to deadlocks because render() holds the stream lock.
//
// Protected by the buffer mutex
gboolean unlocked;
};
#define _do_init(bla) \
GST_DEBUG_CATEGORY_INIT(webkit_video_sink_debug, \
"webkitsink", \
0, \
"webkit video sink")
GST_BOILERPLATE_FULL(WebKitVideoSink,
webkit_video_sink,
GstVideoSink,
GST_TYPE_VIDEO_SINK,
_do_init);
static void
webkit_video_sink_base_init(gpointer g_class)
{
GstElementClass* element_class = GST_ELEMENT_CLASS(g_class);
gst_element_class_add_pad_template(element_class, gst_static_pad_template_get(&sinktemplate));
gst_element_class_set_details_simple(element_class, "WebKit video sink",
"Sink/Video", "Sends video data from a GStreamer pipeline to a Cairo surface",
"Alp Toker <alp@atoker.com>");
}
static void
webkit_video_sink_init(WebKitVideoSink* sink, WebKitVideoSinkClass* klass)
{
WebKitVideoSinkPrivate* priv;
sink->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE(sink, WEBKIT_TYPE_VIDEO_SINK, WebKitVideoSinkPrivate);
priv->data_cond = g_cond_new();
priv->buffer_mutex = g_mutex_new();
}
static gboolean
webkit_video_sink_timeout_func(gpointer data)
{
WebKitVideoSink* sink = reinterpret_cast<WebKitVideoSink*>(data);
WebKitVideoSinkPrivate* priv = sink->priv;
GstBuffer* buffer;
g_mutex_lock(priv->buffer_mutex);
buffer = priv->buffer;
priv->buffer = 0;
priv->timeout_id = 0;
if (!buffer || priv->unlocked || G_UNLIKELY(!GST_IS_BUFFER(buffer))) {
g_cond_signal(priv->data_cond);
g_mutex_unlock(priv->buffer_mutex);
return FALSE;
}
g_signal_emit(sink, webkit_video_sink_signals[REPAINT_REQUESTED], 0, buffer);
gst_buffer_unref(buffer);
g_cond_signal(priv->data_cond);
g_mutex_unlock(priv->buffer_mutex);
return FALSE;
}
static GstFlowReturn
webkit_video_sink_render(GstBaseSink* bsink, GstBuffer* buffer)
{
WebKitVideoSink* sink = WEBKIT_VIDEO_SINK(bsink);
WebKitVideoSinkPrivate* priv = sink->priv;
g_mutex_lock(priv->buffer_mutex);
if (priv->unlocked) {
g_mutex_unlock(priv->buffer_mutex);
return GST_FLOW_OK;
}
priv->buffer = gst_buffer_ref(buffer);
// For the unlikely case where the buffer has no caps, the caps
// are implicitely the caps of the pad. This shouldn't happen.
if (G_UNLIKELY(!GST_BUFFER_CAPS(buffer))) {
buffer = priv->buffer = gst_buffer_make_metadata_writable(priv->buffer);
gst_buffer_set_caps(priv->buffer, GST_PAD_CAPS(GST_BASE_SINK_PAD(bsink)));
}
GstCaps *caps = GST_BUFFER_CAPS(buffer);
GstVideoFormat format;
int width, height;
if (G_UNLIKELY(!gst_video_format_parse_caps(caps, &format, &width, &height))) {
gst_buffer_unref(buffer);
g_mutex_unlock(priv->buffer_mutex);
return GST_FLOW_ERROR;
}
// Cairo's ARGB has pre-multiplied alpha while GStreamer's doesn't.
// Here we convert to Cairo's ARGB.
if (format == GST_VIDEO_FORMAT_ARGB || format == GST_VIDEO_FORMAT_BGRA) {
// Because GstBaseSink::render() only owns the buffer reference in the
// method scope we can't use gst_buffer_make_writable() here. Also
// The buffer content should not be changed here because the same buffer
// could be passed multiple times to this method (in theory)
GstBuffer *newBuffer = gst_buffer_try_new_and_alloc(GST_BUFFER_SIZE(buffer));
// Check if allocation failed
if (G_UNLIKELY(!newBuffer)) {
gst_buffer_unref(buffer);
g_mutex_unlock(priv->buffer_mutex);
return GST_FLOW_ERROR;
}
gst_buffer_copy_metadata(newBuffer, buffer, (GstBufferCopyFlags) GST_BUFFER_COPY_ALL);
// We don't use Color::premultipliedARGBFromColor() here because
// one function call per video pixel is just too expensive:
// For 720p/PAL for example this means 1280*720*25=23040000
// function calls per second!
unsigned short alpha;
const guint8 *source = GST_BUFFER_DATA(buffer);
guint8 *destination = GST_BUFFER_DATA(newBuffer);
for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
alpha = source[3];
destination[0] = (source[0] * alpha + 128) / 255;
destination[1] = (source[1] * alpha + 128) / 255;
destination[2] = (source[2] * alpha + 128) / 255;
destination[3] = alpha;
#else
alpha = source[0];
destination[0] = alpha;
destination[1] = (source[1] * alpha + 128) / 255;
destination[2] = (source[2] * alpha + 128) / 255;
destination[3] = (source[3] * alpha + 128) / 255;
#endif
source += 4;
destination += 4;
}
}
gst_buffer_unref(buffer);
buffer = priv->buffer = newBuffer;
}
// This should likely use a lower priority, but glib currently starves
// lower priority sources.
// See: https://bugzilla.gnome.org/show_bug.cgi?id=610830.
priv->timeout_id = g_timeout_add_full(G_PRIORITY_DEFAULT, 0,
webkit_video_sink_timeout_func,
gst_object_ref(sink),
(GDestroyNotify)gst_object_unref);
g_cond_wait(priv->data_cond, priv->buffer_mutex);
g_mutex_unlock(priv->buffer_mutex);
return GST_FLOW_OK;
}
static void
webkit_video_sink_dispose(GObject* object)
{
WebKitVideoSink* sink = WEBKIT_VIDEO_SINK(object);
WebKitVideoSinkPrivate* priv = sink->priv;
if (priv->data_cond) {
g_cond_free(priv->data_cond);
priv->data_cond = 0;
}
if (priv->buffer_mutex) {
g_mutex_free(priv->buffer_mutex);
priv->buffer_mutex = 0;
}
G_OBJECT_CLASS(parent_class)->dispose(object);
}
static void
unlock_buffer_mutex(WebKitVideoSinkPrivate* priv)
{
g_mutex_lock(priv->buffer_mutex);
if (priv->buffer) {
gst_buffer_unref(priv->buffer);
priv->buffer = 0;
}
priv->unlocked = TRUE;
g_cond_signal(priv->data_cond);
g_mutex_unlock(priv->buffer_mutex);
}
static gboolean
webkit_video_sink_unlock(GstBaseSink* object)
{
WebKitVideoSink* sink = WEBKIT_VIDEO_SINK(object);
unlock_buffer_mutex(sink->priv);
return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock,
(object), TRUE);
}
static gboolean
webkit_video_sink_unlock_stop(GstBaseSink* object)
{
WebKitVideoSink* sink = WEBKIT_VIDEO_SINK(object);
WebKitVideoSinkPrivate* priv = sink->priv;
g_mutex_lock(priv->buffer_mutex);
priv->unlocked = FALSE;
g_mutex_unlock(priv->buffer_mutex);
return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SINK_CLASS, unlock_stop,
(object), TRUE);
}
static gboolean
webkit_video_sink_stop(GstBaseSink* base_sink)
{
WebKitVideoSinkPrivate* priv = WEBKIT_VIDEO_SINK(base_sink)->priv;
unlock_buffer_mutex(priv);
return TRUE;
}
static gboolean
webkit_video_sink_start(GstBaseSink* base_sink)
{
WebKitVideoSinkPrivate* priv = WEBKIT_VIDEO_SINK(base_sink)->priv;
g_mutex_lock(priv->buffer_mutex);
priv->unlocked = FALSE;
g_mutex_unlock(priv->buffer_mutex);
return TRUE;
}
static void
marshal_VOID__MINIOBJECT(GClosure * closure, GValue * return_value,
guint n_param_values, const GValue * param_values,
gpointer invocation_hint, gpointer marshal_data)
{
typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1, gpointer data2);
marshalfunc_VOID__MINIOBJECT callback;
GCClosure *cc = (GCClosure *) closure;
gpointer data1, data2;
g_return_if_fail(n_param_values == 2);
if (G_CCLOSURE_SWAP_DATA(closure)) {
data1 = closure->data;
data2 = g_value_peek_pointer(param_values + 0);
} else {
data1 = g_value_peek_pointer(param_values + 0);
data2 = closure->data;
}
callback = (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data : cc->callback);
callback(data1, gst_value_get_mini_object(param_values + 1), data2);
}
static void
webkit_video_sink_class_init(WebKitVideoSinkClass* klass)
{
GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
GstBaseSinkClass* gstbase_sink_class = GST_BASE_SINK_CLASS(klass);
g_type_class_add_private(klass, sizeof(WebKitVideoSinkPrivate));
gobject_class->dispose = webkit_video_sink_dispose;
gstbase_sink_class->unlock = webkit_video_sink_unlock;
gstbase_sink_class->unlock_stop = webkit_video_sink_unlock_stop;
gstbase_sink_class->render = webkit_video_sink_render;
gstbase_sink_class->preroll = webkit_video_sink_render;
gstbase_sink_class->stop = webkit_video_sink_stop;
gstbase_sink_class->start = webkit_video_sink_start;
webkit_video_sink_signals[REPAINT_REQUESTED] = g_signal_new("repaint-requested",
G_TYPE_FROM_CLASS(klass),
(GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
0,
0,
0,
marshal_VOID__MINIOBJECT,
G_TYPE_NONE, 1, GST_TYPE_BUFFER);
}
/**
* webkit_video_sink_new:
*
* Creates a new GStreamer video sink.
*
* Return value: a #GstElement for the newly created video sink
*/
GstElement*
webkit_video_sink_new(void)
{
return (GstElement*)g_object_new(WEBKIT_TYPE_VIDEO_SINK, 0);
}
#endif // USE(GSTREAMER)
|