1 /* GStreamer Wayland video sink
3 * Copyright (C) 2011 Intel Corporation
4 * Copyright (C) 2011 Sreerenj Balachandran <sreerenj.balachandran@intel.com>
5 * Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA.
24 * SECTION:element-waylandsink
26 * The waylandsink is creating its own window and render the decoded video frames to that.
27 * Setup the Wayland environment as described in
28 * <ulink url="http://wayland.freedesktop.org/building.html">Wayland</ulink> home page.
29 * The current implementaion is based on weston compositor.
32 * <title>Example pipelines</title>
34 * gst-launch -v videotestsrc ! waylandsink
35 * ]| test the video rendering in wayland
43 #include "gstwaylandsink.h"
44 #include "wlvideoformat.h"
45 #include "waylandpool.h"
47 #include <gst/wayland/wayland.h>
48 #include <gst/video/videooverlay.h>
64 GST_DEBUG_CATEGORY (gstwayland_debug);
65 #define GST_CAT_DEFAULT gstwayland_debug
67 #if G_BYTE_ORDER == G_BIG_ENDIAN
68 #define CAPS "{xRGB, ARGB}"
70 #define CAPS "{BGRx, BGRA}"
73 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
76 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (CAPS))
79 static void gst_wayland_sink_get_property (GObject * object,
80 guint prop_id, GValue * value, GParamSpec * pspec);
81 static void gst_wayland_sink_set_property (GObject * object,
82 guint prop_id, const GValue * value, GParamSpec * pspec);
83 static void gst_wayland_sink_finalize (GObject * object);
84 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
86 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
87 static gboolean gst_wayland_sink_start (GstBaseSink * bsink);
88 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
91 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
92 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
95 /* VideoOverlay interface */
96 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
98 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
100 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
102 /* WaylandVideo interface */
103 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
105 static void gst_wayland_sink_set_surface_size (GstWaylandVideo * video,
107 static void gst_wayland_sink_pause_rendering (GstWaylandVideo * video);
108 static void gst_wayland_sink_resume_rendering (GstWaylandVideo * video);
110 #define gst_wayland_sink_parent_class parent_class
111 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
112 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
113 gst_wayland_sink_videooverlay_init)
114 G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
115 gst_wayland_sink_waylandvideo_init));
118 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
120 GObjectClass *gobject_class;
121 GstElementClass *gstelement_class;
122 GstBaseSinkClass *gstbasesink_class;
124 gobject_class = (GObjectClass *) klass;
125 gstelement_class = (GstElementClass *) klass;
126 gstbasesink_class = (GstBaseSinkClass *) klass;
128 gobject_class->set_property = gst_wayland_sink_set_property;
129 gobject_class->get_property = gst_wayland_sink_get_property;
130 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
132 gst_element_class_add_pad_template (gstelement_class,
133 gst_static_pad_template_get (&sink_template));
135 gst_element_class_set_static_metadata (gstelement_class,
136 "wayland video sink", "Sink/Video",
137 "Output to wayland surface",
138 "Sreerenj Balachandran <sreerenj.balachandran@intel.com>");
140 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
141 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
142 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_wayland_sink_start);
143 gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
144 gstbasesink_class->propose_allocation =
145 GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
146 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
148 g_object_class_install_property (gobject_class, PROP_DISPLAY,
149 g_param_spec_string ("display", "Wayland Display name", "Wayland "
150 "display name to connect to, if not supplied with GstVideoOverlay",
151 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 gst_wayland_sink_init (GstWaylandSink * sink)
157 sink->display = NULL;
163 gst_wayland_sink_get_property (GObject * object,
164 guint prop_id, GValue * value, GParamSpec * pspec)
166 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
170 g_value_set_string (value, sink->display_name);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 gst_wayland_sink_set_property (GObject * object,
180 guint prop_id, const GValue * value, GParamSpec * pspec)
182 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
186 sink->display_name = g_value_dup_string (value);
189 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
195 gst_wayland_sink_finalize (GObject * object)
197 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
199 GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
202 g_object_unref (sink->window);
204 g_object_unref (sink->display);
206 gst_object_unref (sink->pool);
208 if (sink->display_name)
209 g_free (sink->display_name);
211 G_OBJECT_CLASS (parent_class)->finalize (object);
215 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
217 GstWaylandSink *sink;
220 sink = GST_WAYLAND_SINK (bsink);
222 caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
224 GstCaps *intersection;
227 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
228 gst_caps_unref (caps);
235 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
237 GstWaylandSink *sink;
238 GstBufferPool *newpool;
240 enum wl_shm_format format;
243 GstStructure *structure;
244 static GstAllocationParams params = { 0, 0, 0, 15, };
246 sink = GST_WAYLAND_SINK (bsink);
248 GST_LOG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
250 /* extract info from caps */
251 if (!gst_video_info_from_caps (&info, caps))
254 format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
258 /* verify we support the requested format */
259 formats = sink->display->formats;
260 for (i = 0; i < formats->len; i++) {
261 if (g_array_index (formats, uint32_t, i) == format)
265 if (i >= formats->len)
266 goto unsupported_format;
268 /* store the video size */
269 sink->video_width = info.width;
270 sink->video_height = info.height;
272 /* create a new pool for the new configuration */
273 newpool = gst_wayland_buffer_pool_new (sink->display);
276 GST_DEBUG_OBJECT (sink, "Failed to create new pool");
280 structure = gst_buffer_pool_get_config (newpool);
281 gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
282 gst_buffer_pool_config_set_allocator (structure, NULL, ¶ms);
283 if (!gst_buffer_pool_set_config (newpool, structure))
286 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
287 gst_object_unref (newpool);
293 GST_DEBUG_OBJECT (sink,
294 "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
299 GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
300 gst_wayland_format_to_string (format));
305 GST_DEBUG_OBJECT (bsink, "failed setting config");
311 gst_wayland_sink_start (GstBaseSink * bsink)
313 GstWaylandSink *sink = (GstWaylandSink *) bsink;
314 gboolean result = TRUE;
315 GError *error = NULL;
317 GST_DEBUG_OBJECT (sink, "start");
320 sink->display = gst_wl_display_new (sink->display_name, &error);
322 if (sink->display == NULL) {
323 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
324 ("Could not initialise Wayland output"),
325 ("Failed to create GstWlDisplay: '%s'", error->message));
333 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
335 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
336 GstBufferPool *pool = NULL;
337 GstStructure *config;
342 gst_query_parse_allocation (query, &caps, &need_pool);
348 pool = gst_object_ref (sink->pool);
353 /* we had a pool, check caps */
354 config = gst_buffer_pool_get_config (pool);
355 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
357 if (!gst_caps_is_equal (caps, pcaps)) {
358 /* different caps, we can't use this pool */
359 gst_object_unref (pool);
362 gst_structure_free (config);
365 if (pool == NULL && need_pool) {
368 if (!gst_video_info_from_caps (&info, caps))
371 GST_DEBUG_OBJECT (sink, "create new pool");
372 pool = gst_wayland_buffer_pool_new (sink->display);
374 /* the normal size of a frame */
377 config = gst_buffer_pool_get_config (pool);
378 gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
379 if (!gst_buffer_pool_set_config (pool, config))
383 gst_query_add_allocation_pool (query, pool, size, 2, 0);
384 gst_object_unref (pool);
392 GST_DEBUG_OBJECT (bsink, "no caps specified");
397 GST_DEBUG_OBJECT (bsink, "invalid caps specified");
402 GST_DEBUG_OBJECT (bsink, "failed setting config");
403 gst_object_unref (pool);
409 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
411 GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
412 return gst_wayland_sink_render (bsink, buffer);
416 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
418 GST_LOG ("frame_redraw_cb");
419 wl_callback_destroy (callback);
422 static const struct wl_callback_listener frame_callback_listener = {
423 frame_redraw_callback
427 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
429 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
430 GstVideoRectangle src, dst, res;
431 GstBuffer *to_render;
434 struct wl_surface *surface;
435 struct wl_callback *callback;
437 GST_LOG_OBJECT (sink, "render buffer %p", buffer);
439 sink->window = gst_wl_window_new_toplevel (sink->display, sink->video_width,
442 meta = gst_buffer_get_wl_meta (buffer);
444 if (meta && meta->display == sink->display) {
445 GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
449 GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
454 if (!gst_buffer_pool_set_active (sink->pool, TRUE))
455 goto activate_failed;
457 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
458 if (ret != GST_FLOW_OK)
461 gst_buffer_map (buffer, &src, GST_MAP_READ);
462 gst_buffer_fill (to_render, 0, src.data, src.size);
463 gst_buffer_unmap (buffer, &src);
465 meta = gst_buffer_get_wl_meta (to_render);
468 src.w = sink->video_width;
469 src.h = sink->video_height;
470 dst.w = sink->window->width;
471 dst.h = sink->window->height;
473 gst_video_sink_center_rect (src, dst, &res, FALSE);
475 surface = gst_wl_window_get_wl_surface (sink->window);
477 wl_surface_attach (surface, meta->wbuffer, 0, 0);
478 wl_surface_damage (surface, 0, 0, res.w, res.h);
479 callback = wl_surface_frame (surface);
480 wl_callback_add_listener (callback, &frame_callback_listener, NULL);
481 wl_surface_commit (surface);
482 wl_display_flush (sink->display->display);
484 if (buffer != to_render)
485 gst_buffer_unref (to_render);
490 GST_WARNING_OBJECT (sink, "could not create image");
495 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
496 ("Internal error: can't allocate images"),
497 ("We don't have a bufferpool negotiated"));
498 return GST_FLOW_ERROR;
502 GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
503 ret = GST_FLOW_ERROR;
509 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
511 iface->set_window_handle = gst_wayland_sink_set_window_handle;
512 iface->expose = gst_wayland_sink_expose;
516 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
518 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
519 g_return_if_fail (sink != NULL);
523 gst_wayland_sink_expose (GstVideoOverlay * overlay)
525 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
526 g_return_if_fail (sink != NULL);
530 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
532 iface->set_surface_size = gst_wayland_sink_set_surface_size;
533 iface->pause_rendering = gst_wayland_sink_pause_rendering;
534 iface->resume_rendering = gst_wayland_sink_resume_rendering;
538 gst_wayland_sink_set_surface_size (GstWaylandVideo * video, gint w, gint h)
540 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
541 g_return_if_fail (sink != NULL);
545 gst_wayland_sink_pause_rendering (GstWaylandVideo * video)
547 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
548 g_return_if_fail (sink != NULL);
552 gst_wayland_sink_resume_rendering (GstWaylandVideo * video)
554 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
555 g_return_if_fail (sink != NULL);
559 plugin_init (GstPlugin * plugin)
561 GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
562 " wayland video sink");
564 return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
565 GST_TYPE_WAYLAND_SINK);
568 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
571 "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,