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
28 * The waylandsink is creating its own window and render the decoded video frames to that.
29 * Setup the Wayland environment as described in
30 * [Wayland](http://wayland.freedesktop.org/building.html) home page.
32 * The current implementation is based on weston compositor.
34 * ## Example pipelines
36 * gst-launch-1.0 -v videotestsrc ! waylandsink
37 * ]| test the video rendering in wayland
45 #include "gstwaylandsink.h"
46 #include "wlvideoformat.h"
48 #include "wlshmallocator.h"
49 #include "wllinuxdmabuf.h"
51 #include <gst/wayland/wayland.h>
52 #include <gst/video/videooverlay.h>
69 GST_DEBUG_CATEGORY (gstwayland_debug);
70 #define GST_CAT_DEFAULT gstwayland_debug
72 #define WL_VIDEO_FORMATS \
73 "{ BGRx, BGRA, RGBx, xBGR, xRGB, RGBA, ABGR, ARGB, RGB, BGR, " \
74 "RGB16, BGR16, YUY2, YVYU, UYVY, AYUV, NV12, NV21, NV16, " \
75 "YUV9, YVU9, Y41B, I420, YV12, Y42B, v308 }"
77 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
80 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (WL_VIDEO_FORMATS) ";"
81 GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_DMABUF,
85 static void gst_wayland_sink_get_property (GObject * object,
86 guint prop_id, GValue * value, GParamSpec * pspec);
87 static void gst_wayland_sink_set_property (GObject * object,
88 guint prop_id, const GValue * value, GParamSpec * pspec);
89 static void gst_wayland_sink_finalize (GObject * object);
91 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
92 GstStateChange transition);
93 static void gst_wayland_sink_set_context (GstElement * element,
94 GstContext * context);
96 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
98 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
100 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
101 static GstFlowReturn gst_wayland_sink_show_frame (GstVideoSink * bsink,
104 /* VideoOverlay interface */
105 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
107 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
109 static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
110 gint x, gint y, gint w, gint h);
111 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
113 /* WaylandVideo interface */
114 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
116 static void gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video);
117 static void gst_wayland_sink_end_geometry_change (GstWaylandVideo * video);
119 #define gst_wayland_sink_parent_class parent_class
120 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
121 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
122 gst_wayland_sink_videooverlay_init)
123 G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
124 gst_wayland_sink_waylandvideo_init));
126 /* A tiny GstVideoBufferPool subclass that modify the options to remove
127 * VideoAlignment. To support VideoAlignment we would need to pass the padded
128 * width/height + stride and use the viewporter interface to crop, a bit like
129 * we use to do with XV. It would still be quite limited. It's a bit retro,
130 * hopefully there will be a better Wayland interface in the future. */
132 GType gst_wayland_pool_get_type (void);
136 GstVideoBufferPool parent;
141 GstVideoBufferPoolClass parent;
142 } GstWaylandPoolClass;
144 G_DEFINE_TYPE (GstWaylandPool, gst_wayland_pool, GST_TYPE_VIDEO_BUFFER_POOL);
146 static const gchar **
147 gst_wayland_pool_get_options (GstBufferPool * pool)
149 static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META, NULL };
154 gst_wayland_pool_class_init (GstWaylandPoolClass * klass)
156 GstBufferPoolClass *pool_class = GST_BUFFER_POOL_CLASS (klass);
157 pool_class->get_options = gst_wayland_pool_get_options;
161 gst_wayland_pool_init (GstWaylandPool * pool)
166 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
168 GObjectClass *gobject_class;
169 GstElementClass *gstelement_class;
170 GstBaseSinkClass *gstbasesink_class;
171 GstVideoSinkClass *gstvideosink_class;
173 gobject_class = (GObjectClass *) klass;
174 gstelement_class = (GstElementClass *) klass;
175 gstbasesink_class = (GstBaseSinkClass *) klass;
176 gstvideosink_class = (GstVideoSinkClass *) klass;
178 gobject_class->set_property = gst_wayland_sink_set_property;
179 gobject_class->get_property = gst_wayland_sink_get_property;
180 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
182 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
184 gst_element_class_set_static_metadata (gstelement_class,
185 "wayland video sink", "Sink/Video",
186 "Output to wayland surface",
187 "Sreerenj Balachandran <sreerenj.balachandran@intel.com>, "
188 "George Kiagiadakis <george.kiagiadakis@collabora.com>");
190 gstelement_class->change_state =
191 GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
192 gstelement_class->set_context =
193 GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
195 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
196 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
197 gstbasesink_class->propose_allocation =
198 GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
200 gstvideosink_class->show_frame =
201 GST_DEBUG_FUNCPTR (gst_wayland_sink_show_frame);
203 g_object_class_install_property (gobject_class, PROP_DISPLAY,
204 g_param_spec_string ("display", "Wayland Display name", "Wayland "
205 "display name to connect to, if not supplied via the GstContext",
206 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
208 g_object_class_install_property (gobject_class, PROP_FULLSCREEN,
209 g_param_spec_boolean ("fullscreen", "Fullscreen",
210 "Whether the surface should be made fullscreen ", FALSE,
211 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215 gst_wayland_sink_init (GstWaylandSink * sink)
217 g_mutex_init (&sink->display_lock);
218 g_mutex_init (&sink->render_lock);
222 gst_wayland_sink_set_fullscreen (GstWaylandSink * sink, gboolean fullscreen)
224 if (fullscreen == sink->fullscreen)
227 g_mutex_lock (&sink->render_lock);
228 sink->fullscreen = fullscreen;
229 gst_wl_window_ensure_fullscreen (sink->window, fullscreen);
230 g_mutex_unlock (&sink->render_lock);
234 gst_wayland_sink_get_property (GObject * object,
235 guint prop_id, GValue * value, GParamSpec * pspec)
237 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
241 GST_OBJECT_LOCK (sink);
242 g_value_set_string (value, sink->display_name);
243 GST_OBJECT_UNLOCK (sink);
245 case PROP_FULLSCREEN:
246 GST_OBJECT_LOCK (sink);
247 g_value_set_boolean (value, sink->fullscreen);
248 GST_OBJECT_UNLOCK (sink);
251 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257 gst_wayland_sink_set_property (GObject * object,
258 guint prop_id, const GValue * value, GParamSpec * pspec)
260 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
264 GST_OBJECT_LOCK (sink);
265 sink->display_name = g_value_dup_string (value);
266 GST_OBJECT_UNLOCK (sink);
268 case PROP_FULLSCREEN:
269 GST_OBJECT_LOCK (sink);
270 gst_wayland_sink_set_fullscreen (sink, g_value_get_boolean (value));
271 GST_OBJECT_UNLOCK (sink);
274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280 gst_wayland_sink_finalize (GObject * object)
282 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
284 GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
286 if (sink->last_buffer)
287 gst_buffer_unref (sink->last_buffer);
289 g_object_unref (sink->display);
291 g_object_unref (sink->window);
293 gst_object_unref (sink->pool);
295 g_free (sink->display_name);
297 g_mutex_clear (&sink->display_lock);
298 g_mutex_clear (&sink->render_lock);
300 G_OBJECT_CLASS (parent_class)->finalize (object);
303 /* must be called with the display_lock */
305 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
306 GstContext * context)
308 struct wl_display *display;
309 GError *error = NULL;
311 display = gst_wayland_display_handle_context_get_handle (context);
312 sink->display = gst_wl_display_new_existing (display, FALSE, &error);
315 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
316 ("Could not set display handle"),
317 ("Failed to use the external wayland display: '%s'", error->message));
318 g_error_free (error);
323 gst_wayland_sink_find_display (GstWaylandSink * sink)
327 GstContext *context = NULL;
328 GError *error = NULL;
331 g_mutex_lock (&sink->display_lock);
333 if (!sink->display) {
334 /* first query upstream for the needed display handle */
335 query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
336 if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
337 gst_query_parse_context (query, &context);
338 gst_wayland_sink_set_display_from_context (sink, context);
340 gst_query_unref (query);
342 if (G_LIKELY (!sink->display)) {
343 /* now ask the application to set the display handle */
344 msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
345 GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
347 g_mutex_unlock (&sink->display_lock);
348 gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
349 /* at this point we expect gst_wayland_sink_set_context
350 * to get called and fill sink->display */
351 g_mutex_lock (&sink->display_lock);
353 if (!sink->display) {
354 /* if the application didn't set a display, let's create it ourselves */
355 GST_OBJECT_LOCK (sink);
356 sink->display = gst_wl_display_new (sink->display_name, &error);
357 GST_OBJECT_UNLOCK (sink);
360 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
361 ("Could not initialise Wayland output"),
362 ("Failed to create GstWlDisplay: '%s'", error->message));
363 g_error_free (error);
370 g_mutex_unlock (&sink->display_lock);
375 static GstStateChangeReturn
376 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
378 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
379 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
381 switch (transition) {
382 case GST_STATE_CHANGE_NULL_TO_READY:
383 if (!gst_wayland_sink_find_display (sink))
384 return GST_STATE_CHANGE_FAILURE;
390 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
391 if (ret == GST_STATE_CHANGE_FAILURE)
394 switch (transition) {
395 case GST_STATE_CHANGE_PAUSED_TO_READY:
396 gst_buffer_replace (&sink->last_buffer, NULL);
398 if (gst_wl_window_is_toplevel (sink->window)) {
399 g_clear_object (&sink->window);
401 /* remove buffer from surface, show nothing */
402 gst_wl_window_render (sink->window, NULL, NULL);
406 case GST_STATE_CHANGE_READY_TO_NULL:
407 g_mutex_lock (&sink->display_lock);
408 /* If we had a toplevel window, we most likely have our own connection
409 * to the display too, and it is a good idea to disconnect and allow
410 * potentially the application to embed us with GstVideoOverlay
411 * (which requires to re-use the same display connection as the parent
412 * surface). If we didn't have a toplevel window, then the display
413 * connection that we have is definitely shared with the application
414 * and it's better to keep it around (together with the window handle)
415 * to avoid requesting them again from the application if/when we are
416 * restarted (GstVideoOverlay behaves like that in other sinks)
418 if (sink->display && !sink->window) { /* -> the window was toplevel */
419 g_clear_object (&sink->display);
420 g_mutex_lock (&sink->render_lock);
421 sink->redraw_pending = FALSE;
422 g_mutex_unlock (&sink->render_lock);
424 g_mutex_unlock (&sink->display_lock);
425 g_clear_object (&sink->pool);
435 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
437 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
439 if (gst_context_has_context_type (context,
440 GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
441 g_mutex_lock (&sink->display_lock);
442 if (G_LIKELY (!sink->display)) {
443 gst_wayland_sink_set_display_from_context (sink, context);
445 GST_WARNING_OBJECT (element, "changing display handle is not supported");
446 g_mutex_unlock (&sink->display_lock);
449 g_mutex_unlock (&sink->display_lock);
452 if (GST_ELEMENT_CLASS (parent_class)->set_context)
453 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
457 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
459 GstWaylandSink *sink;
462 sink = GST_WAYLAND_SINK (bsink);
464 caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
465 caps = gst_caps_make_writable (caps);
467 g_mutex_lock (&sink->display_lock);
470 GValue shm_list = G_VALUE_INIT, dmabuf_list = G_VALUE_INIT;
471 GValue value = G_VALUE_INIT;
477 g_value_init (&shm_list, GST_TYPE_LIST);
478 g_value_init (&dmabuf_list, GST_TYPE_LIST);
480 /* Add corresponding shm formats */
481 formats = sink->display->shm_formats;
482 for (i = 0; i < formats->len; i++) {
483 fmt = g_array_index (formats, uint32_t, i);
484 gfmt = gst_wl_shm_format_to_video_format (fmt);
485 if (gfmt != GST_VIDEO_FORMAT_UNKNOWN) {
486 g_value_init (&value, G_TYPE_STRING);
487 g_value_set_static_string (&value, gst_video_format_to_string (gfmt));
488 gst_value_list_append_and_take_value (&shm_list, &value);
492 gst_structure_take_value (gst_caps_get_structure (caps, 0), "format",
495 /* Add corresponding dmabuf formats */
496 formats = sink->display->dmabuf_formats;
497 for (i = 0; i < formats->len; i++) {
498 fmt = g_array_index (formats, uint32_t, i);
499 gfmt = gst_wl_dmabuf_format_to_video_format (fmt);
500 if (gfmt != GST_VIDEO_FORMAT_UNKNOWN) {
501 g_value_init (&value, G_TYPE_STRING);
502 g_value_set_static_string (&value, gst_video_format_to_string (gfmt));
503 gst_value_list_append_and_take_value (&dmabuf_list, &value);
507 gst_structure_take_value (gst_caps_get_structure (caps, 1), "format",
510 GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
513 g_mutex_unlock (&sink->display_lock);
516 GstCaps *intersection;
519 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
520 gst_caps_unref (caps);
527 static GstBufferPool *
528 gst_wayland_create_pool (GstWaylandSink * sink, GstCaps * caps)
530 GstBufferPool *pool = NULL;
531 GstStructure *structure;
532 gsize size = sink->video_info.size;
535 pool = g_object_new (gst_wayland_pool_get_type (), NULL);
537 structure = gst_buffer_pool_get_config (pool);
538 gst_buffer_pool_config_set_params (structure, caps, size, 2, 0);
540 alloc = gst_wl_shm_allocator_get ();
541 gst_buffer_pool_config_set_allocator (structure, alloc, NULL);
542 if (!gst_buffer_pool_set_config (pool, structure)) {
543 g_object_unref (pool);
546 g_object_unref (alloc);
552 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
554 GstWaylandSink *sink;
556 GstVideoFormat format;
558 sink = GST_WAYLAND_SINK (bsink);
560 GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
562 /* extract info from caps */
563 if (!gst_video_info_from_caps (&sink->video_info, caps))
566 format = GST_VIDEO_INFO_FORMAT (&sink->video_info);
567 sink->video_info_changed = TRUE;
569 /* create a new pool for the new caps */
571 gst_object_unref (sink->pool);
572 sink->pool = gst_wayland_create_pool (sink, caps);
574 use_dmabuf = gst_caps_features_contains (gst_caps_get_features (caps, 0),
575 GST_CAPS_FEATURE_MEMORY_DMABUF);
577 /* validate the format base on the memory type. */
579 if (!gst_wl_display_check_format_for_dmabuf (sink->display, format))
580 goto unsupported_format;
581 } else if (!gst_wl_display_check_format_for_shm (sink->display, format)) {
582 goto unsupported_format;
585 sink->use_dmabuf = use_dmabuf;
591 GST_ERROR_OBJECT (sink,
592 "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
597 GST_ERROR_OBJECT (sink, "Format %s is not available on the display",
598 gst_video_format_to_string (format));
604 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
606 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
608 GstBufferPool *pool = NULL;
612 gst_query_parse_allocation (query, &caps, &need_pool);
615 pool = gst_wayland_create_pool (sink, caps);
617 gst_query_add_allocation_pool (query, pool, sink->video_info.size, 2, 0);
619 g_object_unref (pool);
621 alloc = gst_wl_shm_allocator_get ();
622 gst_query_add_allocation_param (query, alloc, NULL);
623 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
624 g_object_unref (alloc);
630 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
632 GstWaylandSink *sink = data;
634 GST_LOG ("frame_redraw_cb");
636 g_mutex_lock (&sink->render_lock);
637 sink->redraw_pending = FALSE;
638 g_mutex_unlock (&sink->render_lock);
640 wl_callback_destroy (callback);
643 static const struct wl_callback_listener frame_callback_listener = {
644 frame_redraw_callback
647 /* must be called with the render lock */
649 render_last_buffer (GstWaylandSink * sink, gboolean redraw)
651 GstWlBuffer *wlbuffer;
652 const GstVideoInfo *info = NULL;
653 struct wl_surface *surface;
654 struct wl_callback *callback;
656 wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
657 surface = gst_wl_window_get_wl_surface (sink->window);
659 sink->redraw_pending = TRUE;
660 callback = wl_surface_frame (surface);
661 wl_callback_add_listener (callback, &frame_callback_listener, sink);
663 if (G_UNLIKELY (sink->video_info_changed && !redraw)) {
664 info = &sink->video_info;
665 sink->video_info_changed = FALSE;
667 gst_wl_window_render (sink->window, wlbuffer, info);
671 on_window_closed (GstWlWindow * window, gpointer user_data)
673 GstWaylandSink *sink = GST_WAYLAND_SINK (user_data);
675 /* Handle window closure by posting an error on the bus */
676 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
677 ("Output window was closed"), (NULL));
681 gst_wayland_sink_show_frame (GstVideoSink * vsink, GstBuffer * buffer)
683 GstWaylandSink *sink = GST_WAYLAND_SINK (vsink);
684 GstBuffer *to_render;
685 GstWlBuffer *wlbuffer;
687 GstVideoFormat format;
688 GstVideoInfo old_vinfo;
690 struct wl_buffer *wbuf = NULL;
692 GstFlowReturn ret = GST_FLOW_OK;
694 g_mutex_lock (&sink->render_lock);
696 GST_LOG_OBJECT (sink, "render buffer %p", buffer);
698 if (G_UNLIKELY (!sink->window)) {
699 /* ask for window handle. Unlock render_lock while doing that because
700 * set_window_handle & friends will lock it in this context */
701 g_mutex_unlock (&sink->render_lock);
702 gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
703 g_mutex_lock (&sink->render_lock);
706 /* if we were not provided a window, create one ourselves */
707 sink->window = gst_wl_window_new_toplevel (sink->display,
708 &sink->video_info, sink->fullscreen, &sink->render_lock);
709 g_signal_connect_object (sink->window, "closed",
710 G_CALLBACK (on_window_closed), sink, 0);
714 /* drop buffers until we get a frame callback */
715 if (sink->redraw_pending) {
716 GST_LOG_OBJECT (sink, "buffer %p dropped (redraw pending)", buffer);
720 /* make sure that the application has called set_render_rectangle() */
721 if (G_UNLIKELY (sink->window->render_rectangle.w == 0))
724 wlbuffer = gst_buffer_get_wl_buffer (buffer);
726 if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) {
727 GST_LOG_OBJECT (sink, "buffer %p has a wl_buffer from our display, "
728 "writing directly", buffer);
733 /* update video info from video meta */
734 mem = gst_buffer_peek_memory (buffer, 0);
736 old_vinfo = sink->video_info;
737 vmeta = gst_buffer_get_video_meta (buffer);
741 for (i = 0; i < vmeta->n_planes; i++) {
742 sink->video_info.offset[i] = vmeta->offset[i];
743 sink->video_info.stride[i] = vmeta->stride[i];
745 sink->video_info.size = gst_buffer_get_size (buffer);
748 GST_LOG_OBJECT (sink, "buffer %p does not have a wl_buffer from our "
749 "display, creating it", buffer);
751 format = GST_VIDEO_INFO_FORMAT (&sink->video_info);
752 if (gst_wl_display_check_format_for_dmabuf (sink->display, format)) {
753 guint i, nb_dmabuf = 0;
755 for (i = 0; i < gst_buffer_n_memory (buffer); i++)
756 if (gst_is_dmabuf_memory (gst_buffer_peek_memory (buffer, i)))
759 if (nb_dmabuf && (nb_dmabuf == gst_buffer_n_memory (buffer)))
760 wbuf = gst_wl_linux_dmabuf_construct_wl_buffer (buffer, sink->display,
764 if (!wbuf && gst_wl_display_check_format_for_shm (sink->display, format)) {
765 if (gst_buffer_n_memory (buffer) == 1 && gst_is_fd_memory (mem))
766 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
769 /* If nothing worked, copy into our internal pool */
771 GstVideoFrame src, dst;
772 GstVideoInfo src_info = sink->video_info;
774 /* rollback video info changes */
775 sink->video_info = old_vinfo;
777 /* we don't know how to create a wl_buffer directly from the provided
778 * memory, so we have to copy the data to shm memory that we know how
781 GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, "
782 "copying to wl_shm memory", buffer);
784 /* sink->pool always exists (created in set_caps), but it may not
785 * be active if upstream is not using it */
786 if (!gst_buffer_pool_is_active (sink->pool)) {
787 GstStructure *config;
790 config = gst_buffer_pool_get_config (sink->pool);
791 gst_buffer_pool_config_get_params (config, &caps, NULL, NULL, NULL);
793 /* revert back to default strides and offsets */
794 gst_video_info_from_caps (&sink->video_info, caps);
795 gst_buffer_pool_config_set_params (config, caps, sink->video_info.size,
798 /* This is a video pool, it should not fail with basic setings */
799 if (!gst_buffer_pool_set_config (sink->pool, config) ||
800 !gst_buffer_pool_set_active (sink->pool, TRUE))
801 goto activate_failed;
804 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
805 if (ret != GST_FLOW_OK)
808 wlbuffer = gst_buffer_get_wl_buffer (to_render);
810 /* attach a wl_buffer if there isn't one yet */
811 if (G_UNLIKELY (!wlbuffer)) {
812 mem = gst_buffer_peek_memory (to_render, 0);
813 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
816 if (G_UNLIKELY (!wbuf))
817 goto no_wl_buffer_shm;
819 gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
822 if (!gst_video_frame_map (&dst, &sink->video_info, to_render,
826 if (!gst_video_frame_map (&src, &src_info, buffer, GST_MAP_READ)) {
827 gst_video_frame_unmap (&dst);
831 gst_video_frame_copy (&dst, &src);
833 gst_video_frame_unmap (&src);
834 gst_video_frame_unmap (&dst);
843 gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
847 /* drop double rendering */
848 if (G_UNLIKELY (to_render == sink->last_buffer)) {
849 GST_LOG_OBJECT (sink, "Buffer already being rendered");
853 gst_buffer_replace (&sink->last_buffer, to_render);
854 render_last_buffer (sink, FALSE);
856 if (buffer != to_render)
857 gst_buffer_unref (to_render);
862 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
863 ("Window has no size set"),
864 ("Make sure you set the size after calling set_window_handle"));
865 ret = GST_FLOW_ERROR;
870 GST_WARNING_OBJECT (sink, "could not create buffer");
875 GST_ERROR_OBJECT (sink, "could not create wl_buffer out of wl_shm memory");
876 ret = GST_FLOW_ERROR;
881 GST_ERROR_OBJECT (sink, "buffer %p cannot have a wl_buffer", buffer);
882 ret = GST_FLOW_ERROR;
887 GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
888 ret = GST_FLOW_ERROR;
893 GST_ELEMENT_ERROR (sink, RESOURCE, READ,
894 ("Video memory can not be read from userspace."), (NULL));
895 ret = GST_FLOW_ERROR;
900 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
901 ("Video memory can not be written from userspace."), (NULL));
902 ret = GST_FLOW_ERROR;
907 g_mutex_unlock (&sink->render_lock);
913 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
915 iface->set_window_handle = gst_wayland_sink_set_window_handle;
916 iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
917 iface->expose = gst_wayland_sink_expose;
921 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
923 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
924 struct wl_surface *surface = (struct wl_surface *) handle;
926 g_return_if_fail (sink != NULL);
928 if (sink->window != NULL) {
929 GST_WARNING_OBJECT (sink, "changing window handle is not supported");
933 g_mutex_lock (&sink->render_lock);
935 GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
938 g_clear_object (&sink->window);
941 if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
942 /* we cannot use our own display with an external window handle */
943 if (G_UNLIKELY (sink->display->own_display)) {
944 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
945 ("Application did not provide a wayland display handle"),
946 ("waylandsink cannot use an externally-supplied surface without "
947 "an externally-supplied display handle. Consider providing a "
948 "display handle from your application with GstContext"));
950 sink->window = gst_wl_window_new_in_surface (sink->display, surface,
954 GST_ERROR_OBJECT (sink, "Failed to find display handle, "
955 "ignoring window handle");
959 g_mutex_unlock (&sink->render_lock);
963 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
964 gint x, gint y, gint w, gint h)
966 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
968 g_return_if_fail (sink != NULL);
970 g_mutex_lock (&sink->render_lock);
972 g_mutex_unlock (&sink->render_lock);
973 GST_WARNING_OBJECT (sink,
974 "set_render_rectangle called without window, ignoring");
978 GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
980 gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
982 g_mutex_unlock (&sink->render_lock);
986 gst_wayland_sink_expose (GstVideoOverlay * overlay)
988 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
990 g_return_if_fail (sink != NULL);
992 GST_DEBUG_OBJECT (sink, "expose");
994 g_mutex_lock (&sink->render_lock);
995 if (sink->last_buffer && !sink->redraw_pending) {
996 GST_DEBUG_OBJECT (sink, "redrawing last buffer");
997 render_last_buffer (sink, TRUE);
999 g_mutex_unlock (&sink->render_lock);
1003 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
1005 iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
1006 iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
1010 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
1012 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1013 g_return_if_fail (sink != NULL);
1015 g_mutex_lock (&sink->render_lock);
1016 if (!sink->window || !sink->window->area_subsurface) {
1017 g_mutex_unlock (&sink->render_lock);
1018 GST_INFO_OBJECT (sink,
1019 "begin_geometry_change called without window, ignoring");
1023 wl_subsurface_set_sync (sink->window->area_subsurface);
1024 g_mutex_unlock (&sink->render_lock);
1028 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
1030 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1031 g_return_if_fail (sink != NULL);
1033 g_mutex_lock (&sink->render_lock);
1034 if (!sink->window || !sink->window->area_subsurface) {
1035 g_mutex_unlock (&sink->render_lock);
1036 GST_INFO_OBJECT (sink,
1037 "end_geometry_change called without window, ignoring");
1041 wl_subsurface_set_desync (sink->window->area_subsurface);
1042 g_mutex_unlock (&sink->render_lock);
1046 plugin_init (GstPlugin * plugin)
1048 GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
1049 " wayland video sink");
1051 gst_wl_shm_allocator_register ();
1053 return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
1054 GST_TYPE_WAYLAND_SINK);
1057 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1060 "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,