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);
85 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
86 GstStateChange transition);
88 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
90 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
91 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
94 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
95 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
98 /* VideoOverlay interface */
99 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
101 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
103 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
105 /* WaylandVideo interface */
106 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
108 static void gst_wayland_sink_set_surface_size (GstWaylandVideo * video,
110 static void gst_wayland_sink_pause_rendering (GstWaylandVideo * video);
111 static void gst_wayland_sink_resume_rendering (GstWaylandVideo * video);
113 #define gst_wayland_sink_parent_class parent_class
114 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
115 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
116 gst_wayland_sink_videooverlay_init)
117 G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
118 gst_wayland_sink_waylandvideo_init));
121 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
123 GObjectClass *gobject_class;
124 GstElementClass *gstelement_class;
125 GstBaseSinkClass *gstbasesink_class;
127 gobject_class = (GObjectClass *) klass;
128 gstelement_class = (GstElementClass *) klass;
129 gstbasesink_class = (GstBaseSinkClass *) klass;
131 gobject_class->set_property = gst_wayland_sink_set_property;
132 gobject_class->get_property = gst_wayland_sink_get_property;
133 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
135 gst_element_class_add_pad_template (gstelement_class,
136 gst_static_pad_template_get (&sink_template));
138 gst_element_class_set_static_metadata (gstelement_class,
139 "wayland video sink", "Sink/Video",
140 "Output to wayland surface",
141 "Sreerenj Balachandran <sreerenj.balachandran@intel.com>");
143 gstelement_class->change_state =
144 GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
146 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
147 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
148 gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
149 gstbasesink_class->propose_allocation =
150 GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
151 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
153 g_object_class_install_property (gobject_class, PROP_DISPLAY,
154 g_param_spec_string ("display", "Wayland Display name", "Wayland "
155 "display name to connect to, if not supplied with GstVideoOverlay",
156 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
160 gst_wayland_sink_init (GstWaylandSink * sink)
162 g_cond_init (&sink->render_cond);
166 gst_wayland_sink_get_property (GObject * object,
167 guint prop_id, GValue * value, GParamSpec * pspec)
169 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
173 g_value_set_string (value, sink->display_name);
176 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182 gst_wayland_sink_set_property (GObject * object,
183 guint prop_id, const GValue * value, GParamSpec * pspec)
185 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
189 sink->display_name = g_value_dup_string (value);
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198 gst_wayland_sink_finalize (GObject * object)
200 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
202 GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
205 g_object_unref (sink->window);
207 g_object_unref (sink->display);
209 gst_object_unref (sink->pool);
211 if (sink->display_name)
212 g_free (sink->display_name);
214 g_cond_clear (&sink->render_cond);
216 G_OBJECT_CLASS (parent_class)->finalize (object);
219 static GstStateChangeReturn
220 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
222 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
223 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
225 switch (transition) {
226 case GST_STATE_CHANGE_READY_TO_PAUSED:
228 gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
234 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
235 if (ret == GST_STATE_CHANGE_FAILURE)
238 switch (transition) {
239 case GST_STATE_CHANGE_PAUSED_TO_READY:
240 if (gst_wl_window_is_toplevel (sink->window)) {
241 g_clear_object (&sink->window);
242 g_clear_object (&sink->display);
243 g_clear_object (&sink->pool);
254 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
256 GstWaylandSink *sink;
260 sink = GST_WAYLAND_SINK (bsink);
262 caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
264 /* If we don't have a window yet, it will
265 * be created using the upstream size.
266 * Otherwise, we should tell upstream exactly
267 * what size we want. We don't resize ourselves. */
269 caps = gst_caps_make_writable (caps);
270 gst_wl_window_get_size (sink->window, &width, &height);
271 gst_caps_set_simple (caps, "width", G_TYPE_INT, width,
272 "height", G_TYPE_INT, height, NULL);
276 GstCaps *intersection;
279 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
280 gst_caps_unref (caps);
287 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
289 GstWaylandSink *sink;
290 GstBufferPool *newpool;
292 GError *error = NULL;
293 enum wl_shm_format format;
296 GstStructure *structure;
297 static GstAllocationParams params = { 0, 0, 0, 15, };
299 sink = GST_WAYLAND_SINK (bsink);
300 GST_OBJECT_LOCK (sink);
302 GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
304 /* extract info from caps */
305 if (!gst_video_info_from_caps (&info, caps))
308 format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
312 /* store the video size */
313 sink->video_width = info.width;
314 sink->video_height = info.height;
316 /* create the output window if needed */
317 if (!sink->window || sink->video_width != sink->window->width ||
318 sink->video_height != sink->window->height) {
321 sink->display = gst_wl_display_new (sink->display_name, &error);
323 if (sink->display == NULL)
326 g_clear_object (&sink->window);
327 sink->window = gst_wl_window_new_toplevel (sink->display, sink->video_width,
331 /* verify we support the requested format */
332 formats = sink->display->formats;
333 for (i = 0; i < formats->len; i++) {
334 if (g_array_index (formats, uint32_t, i) == format)
338 if (i >= formats->len)
339 goto unsupported_format;
341 /* create a new pool for the new configuration */
342 newpool = gst_wayland_buffer_pool_new (sink->display);
346 structure = gst_buffer_pool_get_config (newpool);
347 gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
348 gst_buffer_pool_config_set_allocator (structure, NULL, ¶ms);
349 if (!gst_buffer_pool_set_config (newpool, structure))
352 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
353 gst_object_unref (newpool);
355 sink->negotiated = TRUE;
356 GST_OBJECT_UNLOCK (sink);
361 GST_DEBUG_OBJECT (sink,
362 "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
367 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
368 ("Could not initialise Wayland output"),
369 ("Failed to create GstWlDisplay: '%s'", error->message));
370 g_error_free (error);
375 GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
376 gst_wayland_format_to_string (format));
381 GST_DEBUG_OBJECT (sink, "Failed to create new pool");
386 GST_DEBUG_OBJECT (bsink, "failed setting config");
391 GST_OBJECT_UNLOCK (sink);
397 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
399 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
400 GstBufferPool *pool = NULL;
401 GstStructure *config;
406 gst_query_parse_allocation (query, &caps, &need_pool);
412 pool = gst_object_ref (sink->pool);
417 /* we had a pool, check caps */
418 config = gst_buffer_pool_get_config (pool);
419 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
421 if (!gst_caps_is_equal (caps, pcaps)) {
422 /* different caps, we can't use this pool */
423 gst_object_unref (pool);
426 gst_structure_free (config);
429 if (pool == NULL && need_pool) {
432 if (!gst_video_info_from_caps (&info, caps))
435 GST_DEBUG_OBJECT (sink, "create new pool");
436 pool = gst_wayland_buffer_pool_new (sink->display);
438 /* the normal size of a frame */
441 config = gst_buffer_pool_get_config (pool);
442 gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
443 if (!gst_buffer_pool_set_config (pool, config))
447 gst_query_add_allocation_pool (query, pool, size, 2, 0);
448 gst_object_unref (pool);
456 GST_DEBUG_OBJECT (bsink, "no caps specified");
461 GST_DEBUG_OBJECT (bsink, "invalid caps specified");
466 GST_DEBUG_OBJECT (bsink, "failed setting config");
467 gst_object_unref (pool);
473 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
475 GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
476 return gst_wayland_sink_render (bsink, buffer);
480 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
482 GST_LOG ("frame_redraw_cb");
483 wl_callback_destroy (callback);
486 static const struct wl_callback_listener frame_callback_listener = {
487 frame_redraw_callback
491 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
493 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
494 GstVideoRectangle src, dst, res;
495 GstBuffer *to_render;
497 GstFlowReturn ret = GST_FLOW_OK;
498 struct wl_surface *surface;
499 struct wl_callback *callback;
501 GST_OBJECT_LOCK (sink);
503 GST_LOG_OBJECT (sink, "render buffer %p", buffer);
505 /* surface is resizing - drop buffers until finished */
506 if (sink->drawing_frozen || !sink->negotiated)
509 meta = gst_buffer_get_wl_meta (buffer);
511 if (meta && meta->display == sink->display) {
512 GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
516 GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
521 if (!gst_buffer_pool_set_active (sink->pool, TRUE))
522 goto activate_failed;
524 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
525 if (ret != GST_FLOW_OK)
528 gst_buffer_map (buffer, &src, GST_MAP_READ);
529 gst_buffer_fill (to_render, 0, src.data, src.size);
530 gst_buffer_unmap (buffer, &src);
532 meta = gst_buffer_get_wl_meta (to_render);
535 src.w = sink->video_width;
536 src.h = sink->video_height;
537 dst.w = sink->window->width;
538 dst.h = sink->window->height;
540 gst_video_sink_center_rect (src, dst, &res, FALSE);
542 surface = gst_wl_window_get_wl_surface (sink->window);
544 wl_surface_attach (surface, meta->wbuffer, 0, 0);
545 wl_surface_damage (surface, 0, 0, res.w, res.h);
546 callback = wl_surface_frame (surface);
547 wl_callback_add_listener (callback, &frame_callback_listener, NULL);
548 wl_surface_commit (surface);
549 wl_display_flush (sink->display->display);
551 /* notify _resume_rendering() in case it's waiting */
552 g_cond_broadcast (&sink->render_cond);
554 if (buffer != to_render)
555 gst_buffer_unref (to_render);
560 GST_WARNING_OBJECT (sink, "could not create image");
565 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
566 ("Internal error: can't allocate images"),
567 ("We don't have a bufferpool negotiated"));
568 ret = GST_FLOW_ERROR;
573 GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
574 ret = GST_FLOW_ERROR;
579 GST_OBJECT_UNLOCK (sink);
585 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
587 iface->set_window_handle = gst_wayland_sink_set_window_handle;
588 iface->expose = gst_wayland_sink_expose;
592 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
594 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
595 GstWaylandWindowHandle *whandle = (GstWaylandWindowHandle *) handle;
596 GError *error = NULL;
599 g_return_if_fail (sink != NULL);
601 GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
604 if (GST_STATE (sink) > GST_STATE_READY)
605 gst_wayland_sink_pause_rendering (GST_WAYLAND_VIDEO (sink));
607 g_clear_object (&sink->window);
608 g_clear_object (&sink->display);
612 gst_wl_display_new_existing (whandle->display, FALSE, &error);
614 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
615 ("Could not set window handle"),
616 ("Failed to use the external wayland display: '%s'", error->message));
617 g_error_free (error);
619 sink->window = gst_wl_window_new_from_surface (sink->display,
620 whandle->surface, whandle->width, whandle->height);
623 /* reconfigure to force creation of new window in set_caps */
624 sink->negotiated = FALSE;
625 peer = gst_pad_get_peer (GST_VIDEO_SINK_PAD (sink));
627 gst_pad_send_event (peer, gst_event_new_reconfigure ());
628 gst_object_unref (peer);
632 if (GST_STATE (sink) > GST_STATE_READY)
633 gst_wayland_sink_resume_rendering (GST_WAYLAND_VIDEO (sink));
637 gst_wayland_sink_expose (GstVideoOverlay * overlay)
639 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
640 g_return_if_fail (sink != NULL);
644 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
646 iface->set_surface_size = gst_wayland_sink_set_surface_size;
647 iface->pause_rendering = gst_wayland_sink_pause_rendering;
648 iface->resume_rendering = gst_wayland_sink_resume_rendering;
652 gst_wayland_sink_set_surface_size (GstWaylandVideo * video, gint w, gint h)
654 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
657 g_return_if_fail (sink != NULL);
658 g_return_if_fail (sink->window != NULL);
660 GST_OBJECT_LOCK (sink);
662 GST_OBJECT_UNLOCK (sink);
663 GST_WARNING_OBJECT (sink,
664 "set_surface_size called without window, ignoring");
668 gst_wl_window_set_size (sink->window, w, h);
669 sink->negotiated = FALSE;
670 GST_OBJECT_UNLOCK (sink);
672 /* upstream must change video size because we can't resize ourselves.
673 * This can be removed when we move to wl_viewport */
674 if (GST_STATE (sink) > GST_STATE_READY) {
675 peer = gst_pad_get_peer (GST_VIDEO_SINK_PAD (sink));
677 gst_pad_send_event (peer, gst_event_new_reconfigure ());
678 gst_object_unref (peer);
684 gst_wayland_sink_pause_rendering (GstWaylandVideo * video)
686 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
687 g_return_if_fail (sink != NULL);
689 GST_OBJECT_LOCK (sink);
690 sink->drawing_frozen = TRUE;
691 GST_OBJECT_UNLOCK (sink);
695 gst_wayland_sink_resume_rendering (GstWaylandVideo * video)
697 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
698 g_return_if_fail (sink != NULL);
700 GST_OBJECT_LOCK (sink);
701 sink->drawing_frozen = FALSE;
702 g_cond_wait (&sink->render_cond, GST_OBJECT_GET_LOCK (sink));
703 GST_OBJECT_UNLOCK (sink);
707 plugin_init (GstPlugin * plugin)
709 GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
710 " wayland video sink");
712 return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
713 GST_TYPE_WAYLAND_SINK);
716 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
719 "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,