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>
6 * Copyright (C) 2014 Collabora Ltd.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301 USA.
25 * SECTION:element-waylandsink
27 * The waylandsink is creating its own window and render the decoded video frames to that.
28 * Setup the Wayland environment as described in
29 * <ulink url="http://wayland.freedesktop.org/building.html">Wayland</ulink> home page.
30 * The current implementaion is based on weston compositor.
33 * <title>Example pipelines</title>
35 * gst-launch -v videotestsrc ! waylandsink
36 * ]| test the video rendering in wayland
44 #include "gstwaylandsink.h"
45 #include "wlvideoformat.h"
46 #include "waylandpool.h"
48 #include <gst/wayland/wayland.h>
49 #include <gst/video/videooverlay.h>
65 GST_DEBUG_CATEGORY (gstwayland_debug);
66 #define GST_CAT_DEFAULT gstwayland_debug
68 #if G_BYTE_ORDER == G_BIG_ENDIAN
69 #define CAPS "{xRGB, ARGB}"
71 #define CAPS "{BGRx, BGRA}"
74 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
77 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (CAPS))
80 static void gst_wayland_sink_get_property (GObject * object,
81 guint prop_id, GValue * value, GParamSpec * pspec);
82 static void gst_wayland_sink_set_property (GObject * object,
83 guint prop_id, const GValue * value, GParamSpec * pspec);
84 static void gst_wayland_sink_finalize (GObject * object);
86 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
87 GstStateChange transition);
89 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
91 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
92 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
95 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
96 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
99 /* VideoOverlay interface */
100 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
102 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
104 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
106 /* WaylandVideo interface */
107 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
109 static void gst_wayland_sink_set_surface_size (GstWaylandVideo * video,
111 static void gst_wayland_sink_pause_rendering (GstWaylandVideo * video);
112 static void gst_wayland_sink_resume_rendering (GstWaylandVideo * video);
114 #define gst_wayland_sink_parent_class parent_class
115 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
116 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
117 gst_wayland_sink_videooverlay_init)
118 G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
119 gst_wayland_sink_waylandvideo_init));
122 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
124 GObjectClass *gobject_class;
125 GstElementClass *gstelement_class;
126 GstBaseSinkClass *gstbasesink_class;
128 gobject_class = (GObjectClass *) klass;
129 gstelement_class = (GstElementClass *) klass;
130 gstbasesink_class = (GstBaseSinkClass *) klass;
132 gobject_class->set_property = gst_wayland_sink_set_property;
133 gobject_class->get_property = gst_wayland_sink_get_property;
134 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
136 gst_element_class_add_pad_template (gstelement_class,
137 gst_static_pad_template_get (&sink_template));
139 gst_element_class_set_static_metadata (gstelement_class,
140 "wayland video sink", "Sink/Video",
141 "Output to wayland surface",
142 "Sreerenj Balachandran <sreerenj.balachandran@intel.com>");
144 gstelement_class->change_state =
145 GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
147 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
148 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
149 gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
150 gstbasesink_class->propose_allocation =
151 GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
152 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
154 g_object_class_install_property (gobject_class, PROP_DISPLAY,
155 g_param_spec_string ("display", "Wayland Display name", "Wayland "
156 "display name to connect to, if not supplied with GstVideoOverlay",
157 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
161 gst_wayland_sink_init (GstWaylandSink * sink)
163 g_cond_init (&sink->render_cond);
167 gst_wayland_sink_get_property (GObject * object,
168 guint prop_id, GValue * value, GParamSpec * pspec)
170 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
174 g_value_set_string (value, sink->display_name);
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 gst_wayland_sink_set_property (GObject * object,
184 guint prop_id, const GValue * value, GParamSpec * pspec)
186 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
190 sink->display_name = g_value_dup_string (value);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 gst_wayland_sink_finalize (GObject * object)
201 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
203 GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
205 if (sink->last_buffer)
206 gst_buffer_unref (sink->last_buffer);
208 /* see comment about this call in gst_wayland_sink_change_state() */
210 gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
213 g_object_unref (sink->display);
216 g_object_unref (sink->window);
218 gst_object_unref (sink->pool);
220 if (sink->display_name)
221 g_free (sink->display_name);
223 g_cond_clear (&sink->render_cond);
225 G_OBJECT_CLASS (parent_class)->finalize (object);
228 static GstStateChangeReturn
229 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
231 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
232 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
234 switch (transition) {
235 case GST_STATE_CHANGE_READY_TO_PAUSED:
237 gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
243 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
244 if (ret == GST_STATE_CHANGE_FAILURE)
247 switch (transition) {
248 case GST_STATE_CHANGE_PAUSED_TO_READY:
249 if (gst_wl_window_is_toplevel (sink->window)) {
250 /* Force all buffers to return to the pool, regardless of
251 * whether the compositor has released them or not. We are
252 * going to kill the display, so we need to return all buffers
253 * to be destroyed before this happens.
254 * Note that this is done here instead of the pool destructor
255 * because the buffers hold a reference to the pool. Also,
256 * the buffers can only be unref'ed from the display's event loop
257 * and the pool holds a reference to the display. If we drop
258 * our references here, when the compositor releases the buffers,
259 * they will be unref'ed from the event loop thread, which will
260 * unref the pool and therefore the display, which will try to
261 * stop the thread from within itself and cause a deadlock.
264 gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
267 gst_buffer_replace (&sink->last_buffer, NULL);
268 g_clear_object (&sink->window);
269 g_clear_object (&sink->display);
270 g_clear_object (&sink->pool);
281 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
283 GstWaylandSink *sink;
286 sink = GST_WAYLAND_SINK (bsink);
288 caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
291 GstCaps *intersection;
294 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
295 gst_caps_unref (caps);
302 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
304 GstWaylandSink *sink;
305 GstBufferPool *newpool;
307 GError *error = NULL;
308 enum wl_shm_format format;
311 GstStructure *structure;
312 static GstAllocationParams params = { 0, 0, 0, 15, };
314 sink = GST_WAYLAND_SINK (bsink);
315 GST_OBJECT_LOCK (sink);
317 GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
319 /* extract info from caps */
320 if (!gst_video_info_from_caps (&info, caps))
323 format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
327 /* store the video size */
328 sink->video_width = info.width;
329 sink->video_height = info.height;
331 /* create the output window if needed */
334 sink->display = gst_wl_display_new (sink->display_name, &error);
336 if (sink->display == NULL)
339 sink->window = gst_wl_window_new_toplevel (sink->display, sink->video_width,
343 /* verify we support the requested format */
344 formats = sink->display->formats;
345 for (i = 0; i < formats->len; i++) {
346 if (g_array_index (formats, uint32_t, i) == format)
350 if (i >= formats->len)
351 goto unsupported_format;
353 /* create a new pool for the new configuration */
354 newpool = gst_wayland_buffer_pool_new (sink->display);
358 structure = gst_buffer_pool_get_config (newpool);
359 gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
360 gst_buffer_pool_config_set_allocator (structure, NULL, ¶ms);
361 if (!gst_buffer_pool_set_config (newpool, structure))
364 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
365 gst_object_unref (newpool);
367 GST_OBJECT_UNLOCK (sink);
372 GST_DEBUG_OBJECT (sink,
373 "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
378 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
379 ("Could not initialise Wayland output"),
380 ("Failed to create GstWlDisplay: '%s'", error->message));
381 g_error_free (error);
386 GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
387 gst_wayland_format_to_string (format));
392 GST_DEBUG_OBJECT (sink, "Failed to create new pool");
397 GST_DEBUG_OBJECT (bsink, "failed setting config");
402 GST_OBJECT_UNLOCK (sink);
408 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
410 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
411 GstBufferPool *pool = NULL;
412 GstStructure *config;
417 gst_query_parse_allocation (query, &caps, &need_pool);
423 pool = gst_object_ref (sink->pool);
428 /* we had a pool, check caps */
429 config = gst_buffer_pool_get_config (pool);
430 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
432 if (!gst_caps_is_equal (caps, pcaps)) {
433 /* different caps, we can't use this pool */
434 gst_object_unref (pool);
437 gst_structure_free (config);
440 if (pool == NULL && need_pool) {
443 if (!gst_video_info_from_caps (&info, caps))
446 GST_DEBUG_OBJECT (sink, "create new pool");
447 pool = gst_wayland_buffer_pool_new (sink->display);
449 /* the normal size of a frame */
452 config = gst_buffer_pool_get_config (pool);
453 gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
454 if (!gst_buffer_pool_set_config (pool, config))
458 gst_query_add_allocation_pool (query, pool, size, 2, 0);
459 gst_object_unref (pool);
467 GST_DEBUG_OBJECT (bsink, "no caps specified");
472 GST_DEBUG_OBJECT (bsink, "invalid caps specified");
477 GST_DEBUG_OBJECT (bsink, "failed setting config");
478 gst_object_unref (pool);
484 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
486 GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
487 return gst_wayland_sink_render (bsink, buffer);
491 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
493 GstWaylandSink *sink = data;
495 GST_LOG ("frame_redraw_cb");
497 g_atomic_int_set (&sink->redraw_pending, FALSE);
498 wl_callback_destroy (callback);
501 static const struct wl_callback_listener frame_callback_listener = {
502 frame_redraw_callback
505 /* must be called with the object lock */
507 render_last_buffer (GstWaylandSink * sink)
510 struct wl_surface *surface;
511 struct wl_callback *callback;
512 GstVideoRectangle src, dst, res;
514 meta = gst_buffer_get_wl_meta (sink->last_buffer);
515 surface = gst_wl_window_get_wl_surface (sink->window);
517 g_atomic_int_set (&sink->redraw_pending, TRUE);
518 callback = wl_surface_frame (surface);
519 wl_callback_add_listener (callback, &frame_callback_listener, sink);
521 /* Here we essentially add a reference to the buffer. This represents
522 * the fact that the compositor is using the buffer and it should
523 * not return back to the pool and be reused until the compositor
524 * releases it. The release is handled internally in the pool */
525 gst_wayland_compositor_acquire_buffer (meta->pool, sink->last_buffer);
527 src.w = sink->video_width;
528 src.h = sink->video_height;
529 dst.w = sink->window->width;
530 dst.h = sink->window->height;
531 gst_video_sink_center_rect (src, dst, &res, FALSE);
533 wl_viewport_set (sink->window->viewport, wl_fixed_from_int (0),
534 wl_fixed_from_int (0), wl_fixed_from_int (src.w),
535 wl_fixed_from_int (src.h), res.w, res.h);
537 wl_surface_attach (surface, meta->wbuffer, 0, 0);
538 wl_surface_damage (surface, 0, 0, res.w, res.h);
540 wl_surface_commit (surface);
541 wl_display_flush (sink->display->display);
545 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
547 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
548 GstBuffer *to_render;
550 GstFlowReturn ret = GST_FLOW_OK;
552 GST_OBJECT_LOCK (sink);
554 GST_LOG_OBJECT (sink, "render buffer %p", buffer);
556 /* surface is resizing - drop buffers until finished */
557 if (sink->drawing_frozen)
560 /* drop buffers until we get a frame callback */
561 if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
564 meta = gst_buffer_get_wl_meta (buffer);
566 if (meta && meta->pool->display == sink->display) {
567 GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
571 GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
576 if (!gst_buffer_pool_set_active (sink->pool, TRUE))
577 goto activate_failed;
579 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
580 if (ret != GST_FLOW_OK)
583 gst_buffer_map (buffer, &src, GST_MAP_READ);
584 gst_buffer_fill (to_render, 0, src.data, src.size);
585 gst_buffer_unmap (buffer, &src);
588 gst_buffer_replace (&sink->last_buffer, to_render);
589 render_last_buffer (sink);
591 /* notify _resume_rendering() in case it's waiting */
592 sink->rendered = TRUE;
593 g_cond_broadcast (&sink->render_cond);
595 if (buffer != to_render)
596 gst_buffer_unref (to_render);
601 GST_WARNING_OBJECT (sink, "could not create image");
606 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
607 ("Internal error: can't allocate images"),
608 ("We don't have a bufferpool negotiated"));
609 ret = GST_FLOW_ERROR;
614 GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
615 ret = GST_FLOW_ERROR;
620 GST_OBJECT_UNLOCK (sink);
626 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
628 iface->set_window_handle = gst_wayland_sink_set_window_handle;
629 iface->expose = gst_wayland_sink_expose;
633 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
635 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
636 GstWaylandWindowHandle *whandle = (GstWaylandWindowHandle *) handle;
637 GError *error = NULL;
640 g_return_if_fail (sink != NULL);
642 GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
645 if (GST_STATE (sink) > GST_STATE_READY)
646 gst_wayland_sink_pause_rendering (GST_WAYLAND_VIDEO (sink));
648 g_clear_object (&sink->window);
649 g_clear_object (&sink->display);
653 gst_wl_display_new_existing (whandle->display, FALSE, &error);
655 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
656 ("Could not set window handle"),
657 ("Failed to use the external wayland display: '%s'", error->message));
658 g_error_free (error);
660 wl_proxy_set_queue ((struct wl_proxy *) whandle->surface,
661 sink->display->queue);
662 sink->window = gst_wl_window_new_from_surface (sink->display,
663 whandle->surface, whandle->width, whandle->height);
666 /* reconfigure to force creation of new window in set_caps */
667 sink->negotiated = FALSE;
668 peer = gst_pad_get_peer (GST_VIDEO_SINK_PAD (sink));
670 gst_pad_send_event (peer, gst_event_new_reconfigure ());
671 gst_object_unref (peer);
675 if (GST_STATE (sink) > GST_STATE_READY)
676 gst_wayland_sink_resume_rendering (GST_WAYLAND_VIDEO (sink));
680 gst_wayland_sink_expose (GstVideoOverlay * overlay)
682 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
684 g_return_if_fail (sink != NULL);
686 GST_DEBUG_OBJECT (sink, "expose");
688 GST_OBJECT_LOCK (sink);
689 if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
690 GST_DEBUG_OBJECT (sink, "redrawing last buffer");
691 render_last_buffer (sink);
693 GST_OBJECT_UNLOCK (sink);
697 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
699 iface->set_surface_size = gst_wayland_sink_set_surface_size;
700 iface->pause_rendering = gst_wayland_sink_pause_rendering;
701 iface->resume_rendering = gst_wayland_sink_resume_rendering;
705 gst_wayland_sink_set_surface_size (GstWaylandVideo * video, gint w, gint h)
707 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
709 g_return_if_fail (sink != NULL);
710 g_return_if_fail (sink->window != NULL);
712 GST_OBJECT_LOCK (sink);
714 GST_OBJECT_UNLOCK (sink);
715 GST_WARNING_OBJECT (sink,
716 "set_surface_size called without window, ignoring");
720 GST_DEBUG_OBJECT (sink, "changing window size to %d x %d", w, h);
721 gst_wl_window_set_size (sink->window, w, h);
722 GST_OBJECT_UNLOCK (sink);
726 gst_wayland_sink_pause_rendering (GstWaylandVideo * video)
728 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
729 g_return_if_fail (sink != NULL);
731 GST_OBJECT_LOCK (sink);
732 sink->drawing_frozen = TRUE;
733 GST_OBJECT_UNLOCK (sink);
737 gst_wayland_sink_resume_rendering (GstWaylandVideo * video)
739 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
740 g_return_if_fail (sink != NULL);
742 GST_DEBUG_OBJECT (sink, "resuming rendering");
744 GST_OBJECT_LOCK (sink);
745 sink->drawing_frozen = FALSE;
747 if (GST_STATE (sink) == GST_STATE_PLAYING) {
748 sink->rendered = FALSE;
749 while (sink->rendered == FALSE)
750 g_cond_wait (&sink->render_cond, GST_OBJECT_GET_LOCK (sink));
751 GST_DEBUG_OBJECT (sink, "synchronized with render()");
752 } else if (sink->window && sink->last_buffer &&
753 g_atomic_int_get (&sink->redraw_pending) == FALSE) {
754 render_last_buffer (sink);
755 GST_DEBUG_OBJECT (sink, "last buffer redrawn");
758 GST_OBJECT_UNLOCK (sink);
762 plugin_init (GstPlugin * plugin)
764 GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
765 " wayland video sink");
767 return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
768 GST_TYPE_WAYLAND_SINK);
771 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
774 "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,