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..");
206 /* see comment about this call in gst_wayland_sink_change_state() */
207 gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
209 g_object_unref (sink->display);
212 g_object_unref (sink->window);
214 gst_object_unref (sink->pool);
216 if (sink->display_name)
217 g_free (sink->display_name);
219 g_cond_clear (&sink->render_cond);
221 G_OBJECT_CLASS (parent_class)->finalize (object);
224 static GstStateChangeReturn
225 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
227 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
228 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
230 switch (transition) {
231 case GST_STATE_CHANGE_READY_TO_PAUSED:
233 gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
239 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
240 if (ret == GST_STATE_CHANGE_FAILURE)
243 switch (transition) {
244 case GST_STATE_CHANGE_PAUSED_TO_READY:
245 if (gst_wl_window_is_toplevel (sink->window)) {
246 /* Force all buffers to return to the pool, regardless of
247 * whether the compositor has released them or not. We are
248 * going to kill the display, so we need to return all buffers
249 * to be destroyed before this happens.
250 * Note that this is done here instead of the pool destructor
251 * because the buffers hold a reference to the pool. Also,
252 * the buffers can only be unref'ed from the display's event loop
253 * and the pool holds a reference to the display. If we drop
254 * our references here, when the compositor releases the buffers,
255 * they will be unref'ed from the event loop thread, which will
256 * unref the pool and therefore the display, which will try to
257 * stop the thread from within itself and cause a deadlock.
259 gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
261 g_clear_object (&sink->window);
262 g_clear_object (&sink->display);
263 g_clear_object (&sink->pool);
274 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
276 GstWaylandSink *sink;
279 sink = GST_WAYLAND_SINK (bsink);
281 caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
284 GstCaps *intersection;
287 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
288 gst_caps_unref (caps);
295 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
297 GstWaylandSink *sink;
298 GstBufferPool *newpool;
300 GError *error = NULL;
301 enum wl_shm_format format;
304 GstStructure *structure;
305 static GstAllocationParams params = { 0, 0, 0, 15, };
307 sink = GST_WAYLAND_SINK (bsink);
308 GST_OBJECT_LOCK (sink);
310 GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
312 /* extract info from caps */
313 if (!gst_video_info_from_caps (&info, caps))
316 format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
320 /* store the video size */
321 sink->video_width = info.width;
322 sink->video_height = info.height;
324 /* create the output window if needed */
327 sink->display = gst_wl_display_new (sink->display_name, &error);
329 if (sink->display == NULL)
332 sink->window = gst_wl_window_new_toplevel (sink->display, sink->video_width,
336 /* verify we support the requested format */
337 formats = sink->display->formats;
338 for (i = 0; i < formats->len; i++) {
339 if (g_array_index (formats, uint32_t, i) == format)
343 if (i >= formats->len)
344 goto unsupported_format;
346 /* create a new pool for the new configuration */
347 newpool = gst_wayland_buffer_pool_new (sink->display);
351 structure = gst_buffer_pool_get_config (newpool);
352 gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
353 gst_buffer_pool_config_set_allocator (structure, NULL, ¶ms);
354 if (!gst_buffer_pool_set_config (newpool, structure))
357 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
358 gst_object_unref (newpool);
360 GST_OBJECT_UNLOCK (sink);
365 GST_DEBUG_OBJECT (sink,
366 "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
371 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
372 ("Could not initialise Wayland output"),
373 ("Failed to create GstWlDisplay: '%s'", error->message));
374 g_error_free (error);
379 GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
380 gst_wayland_format_to_string (format));
385 GST_DEBUG_OBJECT (sink, "Failed to create new pool");
390 GST_DEBUG_OBJECT (bsink, "failed setting config");
395 GST_OBJECT_UNLOCK (sink);
401 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
403 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
404 GstBufferPool *pool = NULL;
405 GstStructure *config;
410 gst_query_parse_allocation (query, &caps, &need_pool);
416 pool = gst_object_ref (sink->pool);
421 /* we had a pool, check caps */
422 config = gst_buffer_pool_get_config (pool);
423 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
425 if (!gst_caps_is_equal (caps, pcaps)) {
426 /* different caps, we can't use this pool */
427 gst_object_unref (pool);
430 gst_structure_free (config);
433 if (pool == NULL && need_pool) {
436 if (!gst_video_info_from_caps (&info, caps))
439 GST_DEBUG_OBJECT (sink, "create new pool");
440 pool = gst_wayland_buffer_pool_new (sink->display);
442 /* the normal size of a frame */
445 config = gst_buffer_pool_get_config (pool);
446 gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
447 if (!gst_buffer_pool_set_config (pool, config))
451 gst_query_add_allocation_pool (query, pool, size, 2, 0);
452 gst_object_unref (pool);
460 GST_DEBUG_OBJECT (bsink, "no caps specified");
465 GST_DEBUG_OBJECT (bsink, "invalid caps specified");
470 GST_DEBUG_OBJECT (bsink, "failed setting config");
471 gst_object_unref (pool);
477 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
479 GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
480 return gst_wayland_sink_render (bsink, buffer);
484 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
486 GstWaylandSink *sink = data;
488 GST_LOG ("frame_redraw_cb");
490 g_atomic_int_set (&sink->redraw_pending, FALSE);
491 wl_callback_destroy (callback);
494 static const struct wl_callback_listener frame_callback_listener = {
495 frame_redraw_callback
499 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
501 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
502 GstVideoRectangle src, dst, res;
503 GstBuffer *to_render;
505 GstFlowReturn ret = GST_FLOW_OK;
506 struct wl_surface *surface;
507 struct wl_callback *callback;
509 GST_OBJECT_LOCK (sink);
511 GST_LOG_OBJECT (sink, "render buffer %p", buffer);
513 /* surface is resizing - drop buffers until finished */
514 if (sink->drawing_frozen)
517 /* drop buffers until we get a frame callback */
518 if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
521 meta = gst_buffer_get_wl_meta (buffer);
523 if (meta && meta->pool->display == sink->display) {
524 GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
528 GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
533 if (!gst_buffer_pool_set_active (sink->pool, TRUE))
534 goto activate_failed;
536 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
537 if (ret != GST_FLOW_OK)
540 gst_buffer_map (buffer, &src, GST_MAP_READ);
541 gst_buffer_fill (to_render, 0, src.data, src.size);
542 gst_buffer_unmap (buffer, &src);
544 meta = gst_buffer_get_wl_meta (to_render);
547 src.w = sink->video_width;
548 src.h = sink->video_height;
549 dst.w = sink->window->width;
550 dst.h = sink->window->height;
552 gst_video_sink_center_rect (src, dst, &res, FALSE);
554 surface = gst_wl_window_get_wl_surface (sink->window);
556 /* Here we essentially add a reference to the buffer. This represents
557 * the fact that the compositor is using the buffer and it should
558 * not return back to the pool and be reused until the compositor
559 * releases it. The release is handled internally in the pool */
560 gst_wayland_compositor_acquire_buffer (meta->pool, to_render);
562 g_atomic_int_set (&sink->redraw_pending, TRUE);
564 wl_surface_attach (surface, meta->wbuffer, 0, 0);
565 wl_surface_damage (surface, 0, 0, res.w, res.h);
567 wl_viewport_set (sink->window->viewport, wl_fixed_from_int (0),
568 wl_fixed_from_int (0), wl_fixed_from_int (src.w),
569 wl_fixed_from_int (src.h), res.w, res.h);
571 callback = wl_surface_frame (surface);
572 wl_callback_add_listener (callback, &frame_callback_listener, sink);
573 wl_surface_commit (surface);
574 wl_display_flush (sink->display->display);
576 /* notify _resume_rendering() in case it's waiting */
577 g_cond_broadcast (&sink->render_cond);
579 if (buffer != to_render)
580 gst_buffer_unref (to_render);
585 GST_WARNING_OBJECT (sink, "could not create image");
590 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
591 ("Internal error: can't allocate images"),
592 ("We don't have a bufferpool negotiated"));
593 ret = GST_FLOW_ERROR;
598 GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
599 ret = GST_FLOW_ERROR;
604 GST_OBJECT_UNLOCK (sink);
610 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
612 iface->set_window_handle = gst_wayland_sink_set_window_handle;
613 iface->expose = gst_wayland_sink_expose;
617 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
619 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
620 GstWaylandWindowHandle *whandle = (GstWaylandWindowHandle *) handle;
621 GError *error = NULL;
624 g_return_if_fail (sink != NULL);
626 GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
629 if (GST_STATE (sink) > GST_STATE_READY)
630 gst_wayland_sink_pause_rendering (GST_WAYLAND_VIDEO (sink));
632 g_clear_object (&sink->window);
633 g_clear_object (&sink->display);
637 gst_wl_display_new_existing (whandle->display, FALSE, &error);
639 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
640 ("Could not set window handle"),
641 ("Failed to use the external wayland display: '%s'", error->message));
642 g_error_free (error);
644 wl_proxy_set_queue ((struct wl_proxy *) whandle->surface,
645 sink->display->queue);
646 sink->window = gst_wl_window_new_from_surface (sink->display,
647 whandle->surface, whandle->width, whandle->height);
650 /* reconfigure to force creation of new window in set_caps */
651 sink->negotiated = FALSE;
652 peer = gst_pad_get_peer (GST_VIDEO_SINK_PAD (sink));
654 gst_pad_send_event (peer, gst_event_new_reconfigure ());
655 gst_object_unref (peer);
659 if (GST_STATE (sink) > GST_STATE_READY)
660 gst_wayland_sink_resume_rendering (GST_WAYLAND_VIDEO (sink));
664 gst_wayland_sink_expose (GstVideoOverlay * overlay)
666 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
667 g_return_if_fail (sink != NULL);
671 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
673 iface->set_surface_size = gst_wayland_sink_set_surface_size;
674 iface->pause_rendering = gst_wayland_sink_pause_rendering;
675 iface->resume_rendering = gst_wayland_sink_resume_rendering;
679 gst_wayland_sink_set_surface_size (GstWaylandVideo * video, gint w, gint h)
681 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
683 g_return_if_fail (sink != NULL);
684 g_return_if_fail (sink->window != NULL);
686 GST_OBJECT_LOCK (sink);
688 GST_OBJECT_UNLOCK (sink);
689 GST_WARNING_OBJECT (sink,
690 "set_surface_size called without window, ignoring");
694 gst_wl_window_set_size (sink->window, w, h);
695 GST_OBJECT_UNLOCK (sink);
699 gst_wayland_sink_pause_rendering (GstWaylandVideo * video)
701 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
702 g_return_if_fail (sink != NULL);
704 GST_OBJECT_LOCK (sink);
705 sink->drawing_frozen = TRUE;
706 GST_OBJECT_UNLOCK (sink);
710 gst_wayland_sink_resume_rendering (GstWaylandVideo * video)
712 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
713 g_return_if_fail (sink != NULL);
715 GST_OBJECT_LOCK (sink);
716 sink->drawing_frozen = FALSE;
717 g_cond_wait (&sink->render_cond, GST_OBJECT_GET_LOCK (sink));
718 GST_OBJECT_UNLOCK (sink);
722 plugin_init (GstPlugin * plugin)
724 GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
725 " wayland video sink");
727 return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
728 GST_TYPE_WAYLAND_SINK);
731 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
734 "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,