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, NV61, " \
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));
125 GST_ELEMENT_REGISTER_DEFINE (waylandsink, "waylandsink", GST_RANK_MARGINAL,
126 GST_TYPE_WAYLAND_SINK);
128 /* A tiny GstVideoBufferPool subclass that modify the options to remove
129 * VideoAlignment. To support VideoAlignment we would need to pass the padded
130 * width/height + stride and use the viewporter interface to crop, a bit like
131 * we use to do with XV. It would still be quite limited. It's a bit retro,
132 * hopefully there will be a better Wayland interface in the future. */
134 GType gst_wayland_pool_get_type (void);
138 GstVideoBufferPool parent;
143 GstVideoBufferPoolClass parent;
144 } GstWaylandPoolClass;
146 G_DEFINE_TYPE (GstWaylandPool, gst_wayland_pool, GST_TYPE_VIDEO_BUFFER_POOL);
148 static const gchar **
149 gst_wayland_pool_get_options (GstBufferPool * pool)
151 static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META, NULL };
156 gst_wayland_pool_class_init (GstWaylandPoolClass * klass)
158 GstBufferPoolClass *pool_class = GST_BUFFER_POOL_CLASS (klass);
159 pool_class->get_options = gst_wayland_pool_get_options;
163 gst_wayland_pool_init (GstWaylandPool * pool)
168 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
170 GObjectClass *gobject_class;
171 GstElementClass *gstelement_class;
172 GstBaseSinkClass *gstbasesink_class;
173 GstVideoSinkClass *gstvideosink_class;
175 gobject_class = (GObjectClass *) klass;
176 gstelement_class = (GstElementClass *) klass;
177 gstbasesink_class = (GstBaseSinkClass *) klass;
178 gstvideosink_class = (GstVideoSinkClass *) klass;
180 gobject_class->set_property = gst_wayland_sink_set_property;
181 gobject_class->get_property = gst_wayland_sink_get_property;
182 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
184 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
186 gst_element_class_set_static_metadata (gstelement_class,
187 "wayland video sink", "Sink/Video",
188 "Output to wayland surface",
189 "Sreerenj Balachandran <sreerenj.balachandran@intel.com>, "
190 "George Kiagiadakis <george.kiagiadakis@collabora.com>");
192 gstelement_class->change_state =
193 GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
194 gstelement_class->set_context =
195 GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
197 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
198 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
199 gstbasesink_class->propose_allocation =
200 GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
202 gstvideosink_class->show_frame =
203 GST_DEBUG_FUNCPTR (gst_wayland_sink_show_frame);
205 g_object_class_install_property (gobject_class, PROP_DISPLAY,
206 g_param_spec_string ("display", "Wayland Display name", "Wayland "
207 "display name to connect to, if not supplied via the GstContext",
208 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210 g_object_class_install_property (gobject_class, PROP_FULLSCREEN,
211 g_param_spec_boolean ("fullscreen", "Fullscreen",
212 "Whether the surface should be made fullscreen ", FALSE,
213 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215 gst_type_mark_as_plugin_api (GST_TYPE_WAYLAND_VIDEO, 0);
219 gst_wayland_sink_init (GstWaylandSink * sink)
221 g_mutex_init (&sink->display_lock);
222 g_mutex_init (&sink->render_lock);
226 gst_wayland_sink_set_fullscreen (GstWaylandSink * sink, gboolean fullscreen)
228 if (fullscreen == sink->fullscreen)
231 g_mutex_lock (&sink->render_lock);
232 sink->fullscreen = fullscreen;
233 gst_wl_window_ensure_fullscreen (sink->window, fullscreen);
234 g_mutex_unlock (&sink->render_lock);
238 gst_wayland_sink_get_property (GObject * object,
239 guint prop_id, GValue * value, GParamSpec * pspec)
241 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
245 GST_OBJECT_LOCK (sink);
246 g_value_set_string (value, sink->display_name);
247 GST_OBJECT_UNLOCK (sink);
249 case PROP_FULLSCREEN:
250 GST_OBJECT_LOCK (sink);
251 g_value_set_boolean (value, sink->fullscreen);
252 GST_OBJECT_UNLOCK (sink);
255 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261 gst_wayland_sink_set_property (GObject * object,
262 guint prop_id, const GValue * value, GParamSpec * pspec)
264 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
268 GST_OBJECT_LOCK (sink);
269 sink->display_name = g_value_dup_string (value);
270 GST_OBJECT_UNLOCK (sink);
272 case PROP_FULLSCREEN:
273 GST_OBJECT_LOCK (sink);
274 gst_wayland_sink_set_fullscreen (sink, g_value_get_boolean (value));
275 GST_OBJECT_UNLOCK (sink);
278 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284 gst_wayland_sink_finalize (GObject * object)
286 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
288 GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
290 if (sink->last_buffer)
291 gst_buffer_unref (sink->last_buffer);
293 g_object_unref (sink->display);
295 g_object_unref (sink->window);
297 gst_object_unref (sink->pool);
299 g_free (sink->display_name);
301 g_mutex_clear (&sink->display_lock);
302 g_mutex_clear (&sink->render_lock);
304 G_OBJECT_CLASS (parent_class)->finalize (object);
307 /* must be called with the display_lock */
309 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
310 GstContext * context)
312 struct wl_display *display;
313 GError *error = NULL;
315 display = gst_wayland_display_handle_context_get_handle (context);
316 sink->display = gst_wl_display_new_existing (display, FALSE, &error);
319 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
320 ("Could not set display handle"),
321 ("Failed to use the external wayland display: '%s'", error->message));
322 g_error_free (error);
327 gst_wayland_sink_find_display (GstWaylandSink * sink)
331 GstContext *context = NULL;
332 GError *error = NULL;
335 g_mutex_lock (&sink->display_lock);
337 if (!sink->display) {
338 /* first query upstream for the needed display handle */
339 query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
340 if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
341 gst_query_parse_context (query, &context);
342 gst_wayland_sink_set_display_from_context (sink, context);
344 gst_query_unref (query);
346 if (G_LIKELY (!sink->display)) {
347 /* now ask the application to set the display handle */
348 msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
349 GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
351 g_mutex_unlock (&sink->display_lock);
352 gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
353 /* at this point we expect gst_wayland_sink_set_context
354 * to get called and fill sink->display */
355 g_mutex_lock (&sink->display_lock);
357 if (!sink->display) {
358 /* if the application didn't set a display, let's create it ourselves */
359 GST_OBJECT_LOCK (sink);
360 sink->display = gst_wl_display_new (sink->display_name, &error);
361 GST_OBJECT_UNLOCK (sink);
364 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
365 ("Could not initialise Wayland output"),
366 ("Failed to create GstWlDisplay: '%s'", error->message));
367 g_error_free (error);
374 g_mutex_unlock (&sink->display_lock);
379 static GstStateChangeReturn
380 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
382 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
383 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
385 switch (transition) {
386 case GST_STATE_CHANGE_NULL_TO_READY:
387 if (!gst_wayland_sink_find_display (sink))
388 return GST_STATE_CHANGE_FAILURE;
394 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
395 if (ret == GST_STATE_CHANGE_FAILURE)
398 switch (transition) {
399 case GST_STATE_CHANGE_PAUSED_TO_READY:
400 gst_buffer_replace (&sink->last_buffer, NULL);
402 if (gst_wl_window_is_toplevel (sink->window)) {
403 g_clear_object (&sink->window);
405 /* remove buffer from surface, show nothing */
406 gst_wl_window_render (sink->window, NULL, NULL);
410 g_mutex_lock (&sink->render_lock);
411 if (sink->callback) {
412 wl_callback_destroy (sink->callback);
413 sink->callback = NULL;
415 sink->redraw_pending = FALSE;
416 g_mutex_unlock (&sink->render_lock);
418 case GST_STATE_CHANGE_READY_TO_NULL:
419 g_mutex_lock (&sink->display_lock);
420 /* If we had a toplevel window, we most likely have our own connection
421 * to the display too, and it is a good idea to disconnect and allow
422 * potentially the application to embed us with GstVideoOverlay
423 * (which requires to re-use the same display connection as the parent
424 * surface). If we didn't have a toplevel window, then the display
425 * connection that we have is definitely shared with the application
426 * and it's better to keep it around (together with the window handle)
427 * to avoid requesting them again from the application if/when we are
428 * restarted (GstVideoOverlay behaves like that in other sinks)
430 if (sink->display && !sink->window) /* -> the window was toplevel */
431 g_clear_object (&sink->display);
433 g_mutex_unlock (&sink->display_lock);
434 g_clear_object (&sink->pool);
444 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
446 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
448 if (gst_context_has_context_type (context,
449 GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
450 g_mutex_lock (&sink->display_lock);
451 if (G_LIKELY (!sink->display)) {
452 gst_wayland_sink_set_display_from_context (sink, context);
454 GST_WARNING_OBJECT (element, "changing display handle is not supported");
455 g_mutex_unlock (&sink->display_lock);
458 g_mutex_unlock (&sink->display_lock);
461 if (GST_ELEMENT_CLASS (parent_class)->set_context)
462 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
466 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
468 GstWaylandSink *sink;
471 sink = GST_WAYLAND_SINK (bsink);
473 caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
474 caps = gst_caps_make_writable (caps);
476 g_mutex_lock (&sink->display_lock);
479 GValue shm_list = G_VALUE_INIT, dmabuf_list = G_VALUE_INIT;
480 GValue value = G_VALUE_INIT;
486 g_value_init (&shm_list, GST_TYPE_LIST);
487 g_value_init (&dmabuf_list, GST_TYPE_LIST);
489 /* Add corresponding shm formats */
490 formats = sink->display->shm_formats;
491 for (i = 0; i < formats->len; i++) {
492 fmt = g_array_index (formats, uint32_t, i);
493 gfmt = gst_wl_shm_format_to_video_format (fmt);
494 if (gfmt != GST_VIDEO_FORMAT_UNKNOWN) {
495 g_value_init (&value, G_TYPE_STRING);
496 g_value_set_static_string (&value, gst_video_format_to_string (gfmt));
497 gst_value_list_append_and_take_value (&shm_list, &value);
501 gst_structure_take_value (gst_caps_get_structure (caps, 0), "format",
504 /* Add corresponding dmabuf formats */
505 formats = sink->display->dmabuf_formats;
506 for (i = 0; i < formats->len; i++) {
507 fmt = g_array_index (formats, uint32_t, i);
508 gfmt = gst_wl_dmabuf_format_to_video_format (fmt);
509 if (gfmt != GST_VIDEO_FORMAT_UNKNOWN) {
510 g_value_init (&value, G_TYPE_STRING);
511 g_value_set_static_string (&value, gst_video_format_to_string (gfmt));
512 gst_value_list_append_and_take_value (&dmabuf_list, &value);
516 gst_structure_take_value (gst_caps_get_structure (caps, 1), "format",
519 GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
522 g_mutex_unlock (&sink->display_lock);
525 GstCaps *intersection;
528 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
529 gst_caps_unref (caps);
536 static GstBufferPool *
537 gst_wayland_create_pool (GstWaylandSink * sink, GstCaps * caps)
539 GstBufferPool *pool = NULL;
540 GstStructure *structure;
541 gsize size = sink->video_info.size;
544 pool = g_object_new (gst_wayland_pool_get_type (), NULL);
546 structure = gst_buffer_pool_get_config (pool);
547 gst_buffer_pool_config_set_params (structure, caps, size, 2, 0);
549 alloc = gst_wl_shm_allocator_get ();
550 gst_buffer_pool_config_set_allocator (structure, alloc, NULL);
551 if (!gst_buffer_pool_set_config (pool, structure)) {
552 g_object_unref (pool);
555 g_object_unref (alloc);
561 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
563 GstWaylandSink *sink;
565 GstVideoFormat format;
567 sink = GST_WAYLAND_SINK (bsink);
569 GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
571 /* extract info from caps */
572 if (!gst_video_info_from_caps (&sink->video_info, caps))
575 format = GST_VIDEO_INFO_FORMAT (&sink->video_info);
576 sink->video_info_changed = TRUE;
578 /* create a new pool for the new caps */
580 gst_object_unref (sink->pool);
581 sink->pool = gst_wayland_create_pool (sink, caps);
583 use_dmabuf = gst_caps_features_contains (gst_caps_get_features (caps, 0),
584 GST_CAPS_FEATURE_MEMORY_DMABUF);
586 /* validate the format base on the memory type. */
588 if (!gst_wl_display_check_format_for_dmabuf (sink->display, format))
589 goto unsupported_format;
590 } else if (!gst_wl_display_check_format_for_shm (sink->display, format)) {
591 goto unsupported_format;
594 sink->use_dmabuf = use_dmabuf;
600 GST_ERROR_OBJECT (sink,
601 "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
606 GST_ERROR_OBJECT (sink, "Format %s is not available on the display",
607 gst_video_format_to_string (format));
613 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
615 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
617 GstBufferPool *pool = NULL;
621 gst_query_parse_allocation (query, &caps, &need_pool);
624 pool = gst_wayland_create_pool (sink, caps);
626 gst_query_add_allocation_pool (query, pool, sink->video_info.size, 2, 0);
628 g_object_unref (pool);
630 alloc = gst_wl_shm_allocator_get ();
631 gst_query_add_allocation_param (query, alloc, NULL);
632 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
633 g_object_unref (alloc);
639 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
641 GstWaylandSink *sink = data;
643 GST_LOG ("frame_redraw_cb");
645 g_mutex_lock (&sink->render_lock);
646 sink->redraw_pending = FALSE;
648 if (sink->callback) {
649 wl_callback_destroy (callback);
650 sink->callback = NULL;
652 g_mutex_unlock (&sink->render_lock);
655 static const struct wl_callback_listener frame_callback_listener = {
656 frame_redraw_callback
659 /* must be called with the render lock */
661 render_last_buffer (GstWaylandSink * sink, gboolean redraw)
663 GstWlBuffer *wlbuffer;
664 const GstVideoInfo *info = NULL;
665 struct wl_surface *surface;
666 struct wl_callback *callback;
668 wlbuffer = gst_buffer_get_wl_buffer (sink->display, sink->last_buffer);
669 surface = gst_wl_window_get_wl_surface (sink->window);
671 sink->redraw_pending = TRUE;
672 callback = wl_surface_frame (surface);
673 sink->callback = callback;
674 wl_callback_add_listener (callback, &frame_callback_listener, sink);
676 if (G_UNLIKELY (sink->video_info_changed && !redraw)) {
677 info = &sink->video_info;
678 sink->video_info_changed = FALSE;
680 gst_wl_window_render (sink->window, wlbuffer, info);
684 on_window_closed (GstWlWindow * window, gpointer user_data)
686 GstWaylandSink *sink = GST_WAYLAND_SINK (user_data);
688 /* Handle window closure by posting an error on the bus */
689 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
690 ("Output window was closed"), (NULL));
694 gst_wayland_sink_show_frame (GstVideoSink * vsink, GstBuffer * buffer)
696 GstWaylandSink *sink = GST_WAYLAND_SINK (vsink);
697 GstBuffer *to_render;
698 GstWlBuffer *wlbuffer;
700 GstVideoFormat format;
701 GstVideoInfo old_vinfo;
703 struct wl_buffer *wbuf = NULL;
705 GstFlowReturn ret = GST_FLOW_OK;
707 g_mutex_lock (&sink->render_lock);
709 GST_LOG_OBJECT (sink, "render buffer %p", buffer);
711 if (G_UNLIKELY (!sink->window)) {
712 /* ask for window handle. Unlock render_lock while doing that because
713 * set_window_handle & friends will lock it in this context */
714 g_mutex_unlock (&sink->render_lock);
715 gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
716 g_mutex_lock (&sink->render_lock);
719 /* if we were not provided a window, create one ourselves */
720 sink->window = gst_wl_window_new_toplevel (sink->display,
721 &sink->video_info, sink->fullscreen, &sink->render_lock);
722 g_signal_connect_object (sink->window, "closed",
723 G_CALLBACK (on_window_closed), sink, 0);
727 /* drop buffers until we get a frame callback */
728 if (sink->redraw_pending) {
729 GST_LOG_OBJECT (sink, "buffer %p dropped (redraw pending)", buffer);
733 /* make sure that the application has called set_render_rectangle() */
734 if (G_UNLIKELY (sink->window->render_rectangle.w == 0))
737 wlbuffer = gst_buffer_get_wl_buffer (sink->display, buffer);
739 if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) {
740 GST_LOG_OBJECT (sink, "buffer %p has a wl_buffer from our display, "
741 "writing directly", buffer);
746 /* update video info from video meta */
747 mem = gst_buffer_peek_memory (buffer, 0);
749 old_vinfo = sink->video_info;
750 vmeta = gst_buffer_get_video_meta (buffer);
754 for (i = 0; i < vmeta->n_planes; i++) {
755 sink->video_info.offset[i] = vmeta->offset[i];
756 sink->video_info.stride[i] = vmeta->stride[i];
758 sink->video_info.size = gst_buffer_get_size (buffer);
761 GST_LOG_OBJECT (sink, "buffer %p does not have a wl_buffer from our "
762 "display, creating it", buffer);
764 format = GST_VIDEO_INFO_FORMAT (&sink->video_info);
765 if (gst_wl_display_check_format_for_dmabuf (sink->display, format)) {
766 guint i, nb_dmabuf = 0;
768 for (i = 0; i < gst_buffer_n_memory (buffer); i++)
769 if (gst_is_dmabuf_memory (gst_buffer_peek_memory (buffer, i)))
772 if (nb_dmabuf && (nb_dmabuf == gst_buffer_n_memory (buffer)))
773 wbuf = gst_wl_linux_dmabuf_construct_wl_buffer (buffer, sink->display,
777 if (!wbuf && gst_wl_display_check_format_for_shm (sink->display, format)) {
778 if (gst_buffer_n_memory (buffer) == 1 && gst_is_fd_memory (mem))
779 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
782 /* If nothing worked, copy into our internal pool */
784 GstVideoFrame src, dst;
785 GstVideoInfo src_info = sink->video_info;
787 /* rollback video info changes */
788 sink->video_info = old_vinfo;
790 /* we don't know how to create a wl_buffer directly from the provided
791 * memory, so we have to copy the data to shm memory that we know how
794 GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, "
795 "copying to wl_shm memory", buffer);
797 /* sink->pool always exists (created in set_caps), but it may not
798 * be active if upstream is not using it */
799 if (!gst_buffer_pool_is_active (sink->pool)) {
800 GstStructure *config;
803 config = gst_buffer_pool_get_config (sink->pool);
804 gst_buffer_pool_config_get_params (config, &caps, NULL, NULL, NULL);
806 /* revert back to default strides and offsets */
807 gst_video_info_from_caps (&sink->video_info, caps);
808 gst_buffer_pool_config_set_params (config, caps, sink->video_info.size,
811 /* This is a video pool, it should not fail with basic settings */
812 if (!gst_buffer_pool_set_config (sink->pool, config) ||
813 !gst_buffer_pool_set_active (sink->pool, TRUE))
814 goto activate_failed;
817 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
818 if (ret != GST_FLOW_OK)
821 wlbuffer = gst_buffer_get_wl_buffer (sink->display, to_render);
823 /* attach a wl_buffer if there isn't one yet */
824 if (G_UNLIKELY (!wlbuffer)) {
825 mem = gst_buffer_peek_memory (to_render, 0);
826 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
829 if (G_UNLIKELY (!wbuf))
830 goto no_wl_buffer_shm;
832 wlbuffer = gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
835 if (!gst_video_frame_map (&dst, &sink->video_info, to_render,
839 if (!gst_video_frame_map (&src, &src_info, buffer, GST_MAP_READ)) {
840 gst_video_frame_unmap (&dst);
844 gst_video_frame_copy (&dst, &src);
846 gst_video_frame_unmap (&src);
847 gst_video_frame_unmap (&dst);
856 wlbuffer = gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
860 /* drop double rendering */
861 if (G_UNLIKELY (wlbuffer ==
862 gst_buffer_get_wl_buffer (sink->display, sink->last_buffer))) {
863 GST_LOG_OBJECT (sink, "Buffer already being rendered");
867 gst_buffer_replace (&sink->last_buffer, to_render);
868 render_last_buffer (sink, FALSE);
870 if (buffer != to_render)
871 gst_buffer_unref (to_render);
876 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
877 ("Window has no size set"),
878 ("Make sure you set the size after calling set_window_handle"));
879 ret = GST_FLOW_ERROR;
884 GST_WARNING_OBJECT (sink, "could not create buffer");
889 GST_ERROR_OBJECT (sink, "could not create wl_buffer out of wl_shm memory");
890 ret = GST_FLOW_ERROR;
895 GST_ERROR_OBJECT (sink, "buffer %p cannot have a wl_buffer", buffer);
896 ret = GST_FLOW_ERROR;
901 GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
902 ret = GST_FLOW_ERROR;
907 GST_ELEMENT_ERROR (sink, RESOURCE, READ,
908 ("Video memory can not be read from userspace."), (NULL));
909 ret = GST_FLOW_ERROR;
914 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
915 ("Video memory can not be written from userspace."), (NULL));
916 ret = GST_FLOW_ERROR;
921 g_mutex_unlock (&sink->render_lock);
927 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
929 iface->set_window_handle = gst_wayland_sink_set_window_handle;
930 iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
931 iface->expose = gst_wayland_sink_expose;
935 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
937 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
938 struct wl_surface *surface = (struct wl_surface *) handle;
940 g_return_if_fail (sink != NULL);
942 if (sink->window != NULL) {
943 GST_WARNING_OBJECT (sink, "changing window handle is not supported");
947 g_mutex_lock (&sink->render_lock);
949 GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
952 g_clear_object (&sink->window);
955 if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
956 /* we cannot use our own display with an external window handle */
957 if (G_UNLIKELY (sink->display->own_display)) {
958 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
959 ("Application did not provide a wayland display handle"),
960 ("waylandsink cannot use an externally-supplied surface without "
961 "an externally-supplied display handle. Consider providing a "
962 "display handle from your application with GstContext"));
964 sink->window = gst_wl_window_new_in_surface (sink->display, surface,
968 GST_ERROR_OBJECT (sink, "Failed to find display handle, "
969 "ignoring window handle");
973 g_mutex_unlock (&sink->render_lock);
977 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
978 gint x, gint y, gint w, gint h)
980 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
982 g_return_if_fail (sink != NULL);
984 g_mutex_lock (&sink->render_lock);
986 g_mutex_unlock (&sink->render_lock);
987 GST_WARNING_OBJECT (sink,
988 "set_render_rectangle called without window, ignoring");
992 GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
994 gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
996 g_mutex_unlock (&sink->render_lock);
1000 gst_wayland_sink_expose (GstVideoOverlay * overlay)
1002 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1004 g_return_if_fail (sink != NULL);
1006 GST_DEBUG_OBJECT (sink, "expose");
1008 g_mutex_lock (&sink->render_lock);
1009 if (sink->last_buffer && !sink->redraw_pending) {
1010 GST_DEBUG_OBJECT (sink, "redrawing last buffer");
1011 render_last_buffer (sink, TRUE);
1013 g_mutex_unlock (&sink->render_lock);
1017 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
1019 iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
1020 iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
1024 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
1026 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1027 g_return_if_fail (sink != NULL);
1029 g_mutex_lock (&sink->render_lock);
1030 if (!sink->window || !sink->window->area_subsurface) {
1031 g_mutex_unlock (&sink->render_lock);
1032 GST_INFO_OBJECT (sink,
1033 "begin_geometry_change called without window, ignoring");
1037 wl_subsurface_set_sync (sink->window->area_subsurface);
1038 g_mutex_unlock (&sink->render_lock);
1042 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
1044 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1045 g_return_if_fail (sink != NULL);
1047 g_mutex_lock (&sink->render_lock);
1048 if (!sink->window || !sink->window->area_subsurface) {
1049 g_mutex_unlock (&sink->render_lock);
1050 GST_INFO_OBJECT (sink,
1051 "end_geometry_change called without window, ignoring");
1055 wl_subsurface_set_desync (sink->window->area_subsurface);
1056 g_mutex_unlock (&sink->render_lock);
1060 plugin_init (GstPlugin * plugin)
1062 GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
1063 " wayland video sink");
1065 gst_wl_shm_allocator_register ();
1067 return GST_ELEMENT_REGISTER (waylandsink, plugin);
1070 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1073 "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,