waylandsink: rename video format conversion functions to indicate they are about...
[platform/upstream/gstreamer.git] / ext / wayland / gstwaylandsink.c
1 /* GStreamer Wayland video sink
2  *
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.
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 /**
25  * SECTION:element-waylandsink
26  *
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.
31  *
32  * <refsect2>
33  * <title>Example pipelines</title>
34  * |[
35  * gst-launch -v videotestsrc ! waylandsink
36  * ]| test the video rendering in wayland
37  * </refsect2>
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include "gstwaylandsink.h"
45 #include "wlvideoformat.h"
46 #include "wlbuffer.h"
47 #include "wlshmallocator.h"
48
49 #include <gst/wayland/wayland.h>
50 #include <gst/video/videooverlay.h>
51
52 /* signals */
53 enum
54 {
55   SIGNAL_0,
56   LAST_SIGNAL
57 };
58
59 /* Properties */
60 enum
61 {
62   PROP_0,
63   PROP_DISPLAY
64 };
65
66 GST_DEBUG_CATEGORY (gstwayland_debug);
67 #define GST_CAT_DEFAULT gstwayland_debug
68
69 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
70     GST_PAD_SINK,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
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 }"))
76     );
77
78 static void gst_wayland_sink_get_property (GObject * object,
79     guint prop_id, GValue * value, GParamSpec * pspec);
80 static void gst_wayland_sink_set_property (GObject * object,
81     guint prop_id, const GValue * value, GParamSpec * pspec);
82 static void gst_wayland_sink_finalize (GObject * object);
83
84 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
85     GstStateChange transition);
86 static void gst_wayland_sink_set_context (GstElement * element,
87     GstContext * context);
88
89 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
90     GstCaps * filter);
91 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
92 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
93     GstBuffer * buffer);
94 static gboolean
95 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
96 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
97     GstBuffer * buffer);
98
99 /* VideoOverlay interface */
100 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
101     iface);
102 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
103     guintptr handle);
104 static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
105     gint x, gint y, gint w, gint h);
106 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
107
108 /* WaylandVideo interface */
109 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
110     iface);
111 static void gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video);
112 static void gst_wayland_sink_end_geometry_change (GstWaylandVideo * video);
113
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));
120
121 static void
122 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
123 {
124   GObjectClass *gobject_class;
125   GstElementClass *gstelement_class;
126   GstBaseSinkClass *gstbasesink_class;
127
128   gobject_class = (GObjectClass *) klass;
129   gstelement_class = (GstElementClass *) klass;
130   gstbasesink_class = (GstBaseSinkClass *) klass;
131
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);
135
136   gst_element_class_add_pad_template (gstelement_class,
137       gst_static_pad_template_get (&sink_template));
138
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>, "
143       "George Kiagiadakis <george.kiagiadakis@collabora.com>");
144
145   gstelement_class->change_state =
146       GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
147   gstelement_class->set_context =
148       GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
149
150   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
151   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
152   gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
153   gstbasesink_class->propose_allocation =
154       GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
155   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
156
157   g_object_class_install_property (gobject_class, PROP_DISPLAY,
158       g_param_spec_string ("display", "Wayland Display name", "Wayland "
159           "display name to connect to, if not supplied via the GstContext",
160           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
161 }
162
163 static void
164 gst_wayland_sink_init (GstWaylandSink * sink)
165 {
166   g_mutex_init (&sink->display_lock);
167   g_mutex_init (&sink->render_lock);
168 }
169
170 static void
171 gst_wayland_sink_get_property (GObject * object,
172     guint prop_id, GValue * value, GParamSpec * pspec)
173 {
174   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
175
176   switch (prop_id) {
177     case PROP_DISPLAY:
178       GST_OBJECT_LOCK (sink);
179       g_value_set_string (value, sink->display_name);
180       GST_OBJECT_UNLOCK (sink);
181       break;
182     default:
183       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184       break;
185   }
186 }
187
188 static void
189 gst_wayland_sink_set_property (GObject * object,
190     guint prop_id, const GValue * value, GParamSpec * pspec)
191 {
192   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
193
194   switch (prop_id) {
195     case PROP_DISPLAY:
196       GST_OBJECT_LOCK (sink);
197       sink->display_name = g_value_dup_string (value);
198       GST_OBJECT_UNLOCK (sink);
199       break;
200     default:
201       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202       break;
203   }
204 }
205
206 static void
207 gst_wayland_sink_finalize (GObject * object)
208 {
209   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
210
211   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
212
213   if (sink->last_buffer)
214     gst_buffer_unref (sink->last_buffer);
215   if (sink->display)
216     g_object_unref (sink->display);
217   if (sink->window)
218     g_object_unref (sink->window);
219   if (sink->pool)
220     gst_object_unref (sink->pool);
221
222   if (sink->display_name)
223     g_free (sink->display_name);
224
225   g_mutex_clear (&sink->display_lock);
226   g_mutex_clear (&sink->render_lock);
227
228   G_OBJECT_CLASS (parent_class)->finalize (object);
229 }
230
231 /* must be called with the display_lock */
232 static void
233 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
234     GstContext * context)
235 {
236   struct wl_display *display;
237   GError *error = NULL;
238
239   display = gst_wayland_display_handle_context_get_handle (context);
240   sink->display = gst_wl_display_new_existing (display, FALSE, &error);
241
242   if (error) {
243     GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
244         ("Could not set display handle"),
245         ("Failed to use the external wayland display: '%s'", error->message));
246     g_error_free (error);
247   }
248 }
249
250 static gboolean
251 gst_wayland_sink_find_display (GstWaylandSink * sink)
252 {
253   GstQuery *query;
254   GstMessage *msg;
255   GstContext *context = NULL;
256   GError *error = NULL;
257   gboolean ret = TRUE;
258
259   g_mutex_lock (&sink->display_lock);
260
261   if (!sink->display) {
262     /* first query upstream for the needed display handle */
263     query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
264     if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
265       gst_query_parse_context (query, &context);
266       gst_wayland_sink_set_display_from_context (sink, context);
267     }
268     gst_query_unref (query);
269
270     if (G_LIKELY (!sink->display)) {
271       /* now ask the application to set the display handle */
272       msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
273           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
274
275       g_mutex_unlock (&sink->display_lock);
276       gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
277       /* at this point we expect gst_wayland_sink_set_context
278        * to get called and fill sink->display */
279       g_mutex_lock (&sink->display_lock);
280
281       if (!sink->display) {
282         /* if the application didn't set a display, let's create it ourselves */
283         GST_OBJECT_LOCK (sink);
284         sink->display = gst_wl_display_new (sink->display_name, &error);
285         GST_OBJECT_UNLOCK (sink);
286
287         if (error) {
288           GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
289               ("Could not initialise Wayland output"),
290               ("Failed to create GstWlDisplay: '%s'", error->message));
291           g_error_free (error);
292           ret = FALSE;
293         } else {
294           /* inform the world about the new display */
295           context =
296               gst_wayland_display_handle_context_new (sink->display->display);
297           msg = gst_message_new_have_context (GST_OBJECT_CAST (sink), context);
298           gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
299         }
300       }
301     }
302   }
303
304   g_mutex_unlock (&sink->display_lock);
305
306   return ret;
307 }
308
309 static GstStateChangeReturn
310 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
311 {
312   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
313   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
314
315   switch (transition) {
316     case GST_STATE_CHANGE_NULL_TO_READY:
317       if (!gst_wayland_sink_find_display (sink))
318         return GST_STATE_CHANGE_FAILURE;
319       break;
320     default:
321       break;
322   }
323
324   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
325   if (ret == GST_STATE_CHANGE_FAILURE)
326     return ret;
327
328   switch (transition) {
329     case GST_STATE_CHANGE_PAUSED_TO_READY:
330       gst_buffer_replace (&sink->last_buffer, NULL);
331       if (sink->window) {
332         if (gst_wl_window_is_toplevel (sink->window)) {
333           g_clear_object (&sink->window);
334         } else {
335           /* remove buffer from surface, show nothing */
336           wl_surface_attach (sink->window->surface, NULL, 0, 0);
337           wl_surface_damage (sink->window->surface, 0, 0,
338               sink->window->surface_width, sink->window->surface_height);
339           wl_surface_commit (sink->window->surface);
340           wl_display_flush (sink->display->display);
341         }
342       }
343       break;
344     case GST_STATE_CHANGE_READY_TO_NULL:
345       g_mutex_lock (&sink->display_lock);
346       /* If we had a toplevel window, we most likely have our own connection
347        * to the display too, and it is a good idea to disconnect and allow
348        * potentially the application to embed us with GstVideoOverlay
349        * (which requires to re-use the same display connection as the parent
350        * surface). If we didn't have a toplevel window, then the display
351        * connection that we have is definitely shared with the application
352        * and it's better to keep it around (together with the window handle)
353        * to avoid requesting them again from the application if/when we are
354        * restarted (GstVideoOverlay behaves like that in other sinks)
355        */
356       if (sink->display && !sink->window) {     /* -> the window was toplevel */
357         g_clear_object (&sink->display);
358       }
359       g_mutex_unlock (&sink->display_lock);
360       g_clear_object (&sink->pool);
361       break;
362     default:
363       break;
364   }
365
366   return ret;
367 }
368
369 static void
370 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
371 {
372   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
373
374   if (gst_context_has_context_type (context,
375           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
376     g_mutex_lock (&sink->display_lock);
377     if (G_LIKELY (!sink->display))
378       gst_wayland_sink_set_display_from_context (sink, context);
379     else
380       GST_WARNING_OBJECT (element, "changing display handle is not supported");
381     g_mutex_unlock (&sink->display_lock);
382   }
383
384   if (GST_ELEMENT_CLASS (parent_class)->set_context)
385     GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
386 }
387
388 static GstCaps *
389 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
390 {
391   GstWaylandSink *sink;
392   GstCaps *caps;
393
394   sink = GST_WAYLAND_SINK (bsink);
395
396   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
397
398   g_mutex_lock (&sink->display_lock);
399
400   if (sink->display) {
401     GValue list = G_VALUE_INIT;
402     GValue value = G_VALUE_INIT;
403     GArray *formats;
404     gint i;
405     enum wl_shm_format fmt;
406
407     g_value_init (&list, GST_TYPE_LIST);
408     g_value_init (&value, G_TYPE_STRING);
409
410     formats = sink->display->formats;
411     for (i = 0; i < formats->len; i++) {
412       fmt = g_array_index (formats, uint32_t, i);
413       g_value_set_string (&value, gst_wl_shm_format_to_string (fmt));
414       gst_value_list_append_value (&list, &value);
415     }
416
417     caps = gst_caps_make_writable (caps);
418     gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
419
420     GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
421   }
422
423   g_mutex_unlock (&sink->display_lock);
424
425   if (filter) {
426     GstCaps *intersection;
427
428     intersection =
429         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
430     gst_caps_unref (caps);
431     caps = intersection;
432   }
433
434   return caps;
435 }
436
437 static gboolean
438 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
439 {
440   GstWaylandSink *sink;
441   GstBufferPool *newpool;
442   GstVideoInfo info;
443   enum wl_shm_format format;
444   GArray *formats;
445   gint i;
446   GstStructure *structure;
447
448   sink = GST_WAYLAND_SINK (bsink);
449
450   GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
451
452   /* extract info from caps */
453   if (!gst_video_info_from_caps (&info, caps))
454     goto invalid_format;
455
456   format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (&info));
457   if ((gint) format == -1)
458     goto invalid_format;
459
460   /* verify we support the requested format */
461   formats = sink->display->formats;
462   for (i = 0; i < formats->len; i++) {
463     if (g_array_index (formats, uint32_t, i) == format)
464       break;
465   }
466
467   if (i >= formats->len)
468     goto unsupported_format;
469
470   /* create a new pool for the new configuration */
471   newpool = gst_video_buffer_pool_new ();
472   if (!newpool)
473     goto pool_failed;
474
475   structure = gst_buffer_pool_get_config (newpool);
476   gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
477   gst_buffer_pool_config_set_allocator (structure, gst_wl_shm_allocator_get (),
478       NULL);
479   if (!gst_buffer_pool_set_config (newpool, structure))
480     goto config_failed;
481
482   /* store the video info */
483   sink->video_info = info;
484   sink->video_info_changed = TRUE;
485
486   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
487   gst_object_unref (newpool);
488
489   return TRUE;
490
491 invalid_format:
492   {
493     GST_DEBUG_OBJECT (sink,
494         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
495     return FALSE;
496   }
497 unsupported_format:
498   {
499     GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
500         gst_wl_shm_format_to_string (format));
501     return FALSE;
502   }
503 pool_failed:
504   {
505     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
506     return FALSE;
507   }
508 config_failed:
509   {
510     GST_DEBUG_OBJECT (bsink, "failed setting config");
511     gst_object_unref (newpool);
512     return FALSE;
513   }
514 }
515
516 static gboolean
517 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
518 {
519   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
520   GstStructure *config;
521   guint size, min_bufs, max_bufs;
522
523   config = gst_buffer_pool_get_config (sink->pool);
524   gst_buffer_pool_config_get_params (config, NULL, &size, &min_bufs, &max_bufs);
525
526   /* we do have a pool for sure (created in set_caps),
527    * so let's propose it anyway, but also propose the allocator on its own */
528   gst_query_add_allocation_pool (query, sink->pool, size, min_bufs, max_bufs);
529   gst_query_add_allocation_param (query, gst_wl_shm_allocator_get (), NULL);
530
531   return TRUE;
532 }
533
534 static GstFlowReturn
535 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
536 {
537   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
538   return gst_wayland_sink_render (bsink, buffer);
539 }
540
541 static void
542 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
543 {
544   GstWaylandSink *sink = data;
545
546   GST_LOG ("frame_redraw_cb");
547
548   g_atomic_int_set (&sink->redraw_pending, FALSE);
549   wl_callback_destroy (callback);
550 }
551
552 static const struct wl_callback_listener frame_callback_listener = {
553   frame_redraw_callback
554 };
555
556 /* must be called with the render lock */
557 static void
558 render_last_buffer (GstWaylandSink * sink)
559 {
560   GstWlBuffer *wlbuffer;
561   struct wl_surface *surface;
562   struct wl_callback *callback;
563
564   wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
565   surface = gst_wl_window_get_wl_surface (sink->window);
566
567   g_atomic_int_set (&sink->redraw_pending, TRUE);
568   callback = wl_surface_frame (surface);
569   wl_callback_add_listener (callback, &frame_callback_listener, sink);
570
571   gst_wl_buffer_attach (wlbuffer, sink->window);
572   wl_surface_damage (surface, 0, 0, sink->window->surface_width,
573       sink->window->surface_height);
574
575   wl_surface_commit (surface);
576   wl_display_flush (sink->display->display);
577 }
578
579 static GstFlowReturn
580 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
581 {
582   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
583   GstBuffer *to_render;
584   GstWlBuffer *wlbuffer;
585   GstFlowReturn ret = GST_FLOW_OK;
586
587   g_mutex_lock (&sink->render_lock);
588
589   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
590
591   if (G_UNLIKELY (!sink->window)) {
592     /* ask for window handle. Unlock render_lock while doing that because
593      * set_window_handle & friends will lock it in this context */
594     g_mutex_unlock (&sink->render_lock);
595     gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
596     g_mutex_lock (&sink->render_lock);
597
598     if (sink->window) {
599       /* inform the window about our caps */
600       gst_wl_window_set_video_info (sink->window, &sink->video_info);
601     } else {
602       /* if we were not provided a window, create one ourselves */
603       sink->window =
604           gst_wl_window_new_toplevel (sink->display, &sink->video_info);
605     }
606     sink->video_info_changed = FALSE;
607   }
608
609   /* drop buffers until we get a frame callback */
610   if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
611     goto done;
612
613   if (G_UNLIKELY (sink->video_info_changed)) {
614     gst_wl_window_set_video_info (sink->window, &sink->video_info);
615     sink->video_info_changed = FALSE;
616   }
617
618   /* now that we have for sure set the video info on the window, it must have
619    * a valid size, otherwise this means that the application has called
620    * set_window_handle() without calling set_render_rectangle(), which is
621    * absolutely necessary for us.
622    */
623   if (G_UNLIKELY (sink->window->surface_width == 0 ||
624           sink->window->surface_height == 0))
625     goto no_window_size;
626
627   wlbuffer = gst_buffer_get_wl_buffer (buffer);
628
629   if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) {
630     GST_LOG_OBJECT (sink, "buffer %p has a wl_buffer from our display, "
631         "writing directly", buffer);
632     to_render = buffer;
633   } else {
634     GstMemory *mem;
635     struct wl_buffer *wbuf = NULL;
636
637     GST_LOG_OBJECT (sink, "buffer %p does not have a wl_buffer from our "
638         "display, creating it", buffer);
639
640     mem = gst_buffer_peek_memory (buffer, 0);
641
642     if (gst_is_wl_shm_memory (mem)) {
643       wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
644           &sink->video_info);
645     }
646
647     if (wbuf) {
648       gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
649       to_render = buffer;
650     } else {
651       GstMapInfo src;
652       /* we don't know how to create a wl_buffer directly from the provided
653        * memory, so we have to copy the data to a memory that we know how
654        * to handle... */
655
656       GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, "
657           "copying to wl_shm memory", buffer);
658
659       /* sink->pool always exists (created in set_caps), but it may not
660        * be active if upstream is not using it */
661       if (!gst_buffer_pool_is_active (sink->pool) &&
662           !gst_buffer_pool_set_active (sink->pool, TRUE))
663         goto activate_failed;
664
665       ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
666       if (ret != GST_FLOW_OK)
667         goto no_buffer;
668
669       /* the first time we acquire a buffer,
670        * we need to attach a wl_buffer on it */
671       wlbuffer = gst_buffer_get_wl_buffer (buffer);
672       if (G_UNLIKELY (!wlbuffer)) {
673         mem = gst_buffer_peek_memory (to_render, 0);
674         wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
675             &sink->video_info);
676         if (G_UNLIKELY (!wbuf))
677           goto no_wl_buffer;
678
679         gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
680       }
681
682       gst_buffer_map (buffer, &src, GST_MAP_READ);
683       gst_buffer_fill (to_render, 0, src.data, src.size);
684       gst_buffer_unmap (buffer, &src);
685     }
686   }
687
688   gst_buffer_replace (&sink->last_buffer, to_render);
689   render_last_buffer (sink);
690
691   if (buffer != to_render)
692     gst_buffer_unref (to_render);
693   goto done;
694
695 no_window_size:
696   {
697     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
698         ("Window has no size set"),
699         ("Make sure you set the size after calling set_window_handle"));
700     ret = GST_FLOW_ERROR;
701     goto done;
702   }
703 no_buffer:
704   {
705     GST_WARNING_OBJECT (sink, "could not create buffer");
706     goto done;
707   }
708 no_wl_buffer:
709   {
710     GST_ERROR_OBJECT (sink, "could not create wl_buffer out of wl_shm memory");
711     ret = GST_FLOW_ERROR;
712     goto done;
713   }
714 activate_failed:
715   {
716     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
717     ret = GST_FLOW_ERROR;
718     goto done;
719   }
720 done:
721   {
722     g_mutex_unlock (&sink->render_lock);
723     return ret;
724   }
725 }
726
727 static void
728 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
729 {
730   iface->set_window_handle = gst_wayland_sink_set_window_handle;
731   iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
732   iface->expose = gst_wayland_sink_expose;
733 }
734
735 static void
736 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
737 {
738   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
739   struct wl_surface *surface = (struct wl_surface *) handle;
740
741   g_return_if_fail (sink != NULL);
742
743   g_mutex_lock (&sink->render_lock);
744
745   GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
746       (void *) handle);
747
748   g_clear_object (&sink->window);
749
750   if (handle) {
751     if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
752       /* we cannot use our own display with an external window handle */
753       if (G_UNLIKELY (sink->display->own_display)) {
754         GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
755             ("Application did not provide a wayland display handle"),
756             ("waylandsink cannot use an externally-supplied surface without "
757                 "an externally-supplied display handle. Consider providing a "
758                 "display handle from your application with GstContext"));
759       } else {
760         sink->window = gst_wl_window_new_in_surface (sink->display, surface);
761       }
762     } else {
763       GST_ERROR_OBJECT (sink, "Failed to find display handle, "
764           "ignoring window handle");
765     }
766   }
767
768   g_mutex_unlock (&sink->render_lock);
769 }
770
771 static void
772 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
773     gint x, gint y, gint w, gint h)
774 {
775   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
776
777   g_return_if_fail (sink != NULL);
778
779   g_mutex_lock (&sink->render_lock);
780   if (!sink->window) {
781     g_mutex_unlock (&sink->render_lock);
782     GST_WARNING_OBJECT (sink,
783         "set_render_rectangle called without window, ignoring");
784     return;
785   }
786
787   GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
788       x, y, w, h);
789   gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
790
791   g_mutex_unlock (&sink->render_lock);
792 }
793
794 static void
795 gst_wayland_sink_expose (GstVideoOverlay * overlay)
796 {
797   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
798
799   g_return_if_fail (sink != NULL);
800
801   GST_DEBUG_OBJECT (sink, "expose");
802
803   g_mutex_lock (&sink->render_lock);
804   if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
805     GST_DEBUG_OBJECT (sink, "redrawing last buffer");
806     render_last_buffer (sink);
807   }
808   g_mutex_unlock (&sink->render_lock);
809 }
810
811 static void
812 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
813 {
814   iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
815   iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
816 }
817
818 static void
819 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
820 {
821   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
822   g_return_if_fail (sink != NULL);
823
824   g_mutex_lock (&sink->render_lock);
825   if (!sink->window || !sink->window->subsurface) {
826     g_mutex_unlock (&sink->render_lock);
827     GST_INFO_OBJECT (sink,
828         "begin_geometry_change called without window, ignoring");
829     return;
830   }
831
832   wl_subsurface_set_sync (sink->window->subsurface);
833   g_mutex_unlock (&sink->render_lock);
834 }
835
836 static void
837 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
838 {
839   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
840   g_return_if_fail (sink != NULL);
841
842   g_mutex_lock (&sink->render_lock);
843   if (!sink->window || !sink->window->subsurface) {
844     g_mutex_unlock (&sink->render_lock);
845     GST_INFO_OBJECT (sink,
846         "end_geometry_change called without window, ignoring");
847     return;
848   }
849
850   wl_subsurface_set_desync (sink->window->subsurface);
851   g_mutex_unlock (&sink->render_lock);
852 }
853
854 static gboolean
855 plugin_init (GstPlugin * plugin)
856 {
857   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
858       " wayland video sink");
859
860   gst_wl_shm_allocator_register ();
861
862   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
863       GST_TYPE_WAYLAND_SINK);
864 }
865
866 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
867     GST_VERSION_MINOR,
868     waylandsink,
869     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
870     GST_PACKAGE_ORIGIN)