waylandsink: protect access to the display with a new display_lock
[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 "waylandpool.h"
47
48 #include <gst/wayland/wayland.h>
49 #include <gst/video/videooverlay.h>
50
51 /* signals */
52 enum
53 {
54   SIGNAL_0,
55   LAST_SIGNAL
56 };
57
58 /* Properties */
59 enum
60 {
61   PROP_0,
62   PROP_DISPLAY
63 };
64
65 GST_DEBUG_CATEGORY (gstwayland_debug);
66 #define GST_CAT_DEFAULT gstwayland_debug
67
68 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
69     GST_PAD_SINK,
70     GST_PAD_ALWAYS,
71     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
72         ("{ BGRx, BGRA, RGBx, xBGR, xRGB, RGBA, ABGR, ARGB, RGB, BGR, "
73             "RGB16, BGR16, YUY2, YVYU, UYVY, AYUV, NV12, NV21, NV16, "
74             "YUV9, YVU9, Y41B, I420, YV12, Y42B, v308 }"))
75     );
76
77 static void gst_wayland_sink_get_property (GObject * object,
78     guint prop_id, GValue * value, GParamSpec * pspec);
79 static void gst_wayland_sink_set_property (GObject * object,
80     guint prop_id, const GValue * value, GParamSpec * pspec);
81 static void gst_wayland_sink_finalize (GObject * object);
82
83 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
84     GstStateChange transition);
85 static void gst_wayland_sink_set_context (GstElement * element,
86     GstContext * context);
87
88 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
89     GstCaps * filter);
90 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
91 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
92     GstBuffer * buffer);
93 static gboolean
94 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
95 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
96     GstBuffer * buffer);
97
98 /* VideoOverlay interface */
99 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
100     iface);
101 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
102     guintptr handle);
103 static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
104     gint x, gint y, gint w, gint h);
105 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
106
107 /* WaylandVideo interface */
108 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
109     iface);
110 static void gst_wayland_sink_pause_rendering (GstWaylandVideo * video);
111 static void gst_wayland_sink_resume_rendering (GstWaylandVideo * video);
112
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));
119
120 static void
121 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
122 {
123   GObjectClass *gobject_class;
124   GstElementClass *gstelement_class;
125   GstBaseSinkClass *gstbasesink_class;
126
127   gobject_class = (GObjectClass *) klass;
128   gstelement_class = (GstElementClass *) klass;
129   gstbasesink_class = (GstBaseSinkClass *) klass;
130
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);
134
135   gst_element_class_add_pad_template (gstelement_class,
136       gst_static_pad_template_get (&sink_template));
137
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>, "
142       "George Kiagiadakis <george.kiagiadakis@collabora.com>");
143
144   gstelement_class->change_state =
145       GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
146   gstelement_class->set_context =
147       GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
148
149   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
150   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
151   gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
152   gstbasesink_class->propose_allocation =
153       GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
154   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
155
156   g_object_class_install_property (gobject_class, PROP_DISPLAY,
157       g_param_spec_string ("display", "Wayland Display name", "Wayland "
158           "display name to connect to, if not supplied via the GstContext",
159           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
160 }
161
162 static void
163 gst_wayland_sink_init (GstWaylandSink * sink)
164 {
165   g_mutex_init (&sink->display_lock);
166   g_mutex_init (&sink->render_lock);
167   g_cond_init (&sink->render_cond);
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       g_value_set_string (value, sink->display_name);
179       break;
180     default:
181       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182       break;
183   }
184 }
185
186 static void
187 gst_wayland_sink_set_property (GObject * object,
188     guint prop_id, const GValue * value, GParamSpec * pspec)
189 {
190   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
191
192   switch (prop_id) {
193     case PROP_DISPLAY:
194       sink->display_name = g_value_dup_string (value);
195       break;
196     default:
197       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198       break;
199   }
200 }
201
202 static void
203 gst_wayland_sink_finalize (GObject * object)
204 {
205   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
206
207   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
208
209   if (sink->last_buffer)
210     gst_buffer_unref (sink->last_buffer);
211   if (sink->display) {
212     /* see comment about this call in gst_wayland_sink_change_state() */
213     if (sink->pool) {
214       gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
215           (sink->pool));
216     }
217     g_object_unref (sink->display);
218   }
219   if (sink->window)
220     g_object_unref (sink->window);
221   if (sink->pool)
222     gst_object_unref (sink->pool);
223
224   if (sink->display_name)
225     g_free (sink->display_name);
226
227   g_mutex_clear (&sink->display_lock);
228   g_mutex_clear (&sink->render_lock);
229   g_cond_clear (&sink->render_cond);
230
231   G_OBJECT_CLASS (parent_class)->finalize (object);
232 }
233
234 /* must be called with the display_lock */
235 static void
236 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
237     GstContext * context)
238 {
239   struct wl_display *display;
240   GError *error = NULL;
241
242   display = gst_wayland_display_handle_context_get_handle (context);
243   sink->display = gst_wl_display_new_existing (display, FALSE, &error);
244
245   if (error) {
246     GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
247         ("Could not set display handle"),
248         ("Failed to use the external wayland display: '%s'", error->message));
249     g_error_free (error);
250   }
251 }
252
253 static gboolean
254 gst_wayland_sink_find_display (GstWaylandSink * sink)
255 {
256   GstQuery *query;
257   GstMessage *msg;
258   GstContext *context = NULL;
259   GError *error = NULL;
260   gboolean ret = TRUE;
261
262   g_mutex_lock (&sink->display_lock);
263
264   if (!sink->display) {
265     /* first query upstream for the needed display handle */
266     query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
267     if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
268       gst_query_parse_context (query, &context);
269       gst_wayland_sink_set_display_from_context (sink, context);
270     }
271     gst_query_unref (query);
272
273     if (G_LIKELY (!sink->display)) {
274       /* now ask the application to set the display handle */
275       msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
276           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
277
278       g_mutex_unlock (&sink->display_lock);
279       gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
280       /* at this point we expect gst_wayland_sink_set_context
281        * to get called and fill sink->display */
282       g_mutex_lock (&sink->display_lock);
283
284       if (!sink->display) {
285         /* if the application didn't set a display, let's create it ourselves */
286         sink->display = gst_wl_display_new (sink->display_name, &error);
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       if (sink->window && gst_wl_window_is_toplevel (sink->window)) {
331         gst_buffer_replace (&sink->last_buffer, NULL);
332         g_clear_object (&sink->window);
333       }
334       break;
335     case GST_STATE_CHANGE_READY_TO_NULL:
336       g_mutex_lock (&sink->display_lock);
337       /* We don't need to keep the display around, unless we are embedded
338        * in another window as a subsurface, in which case we should continue
339        * to respond to expose() and therefore both the window and the display
340        * are kept alive */
341       if (sink->display && !sink->window) {     /* -> the window was toplevel */
342         /* Force all buffers to return to the pool, regardless of
343          * whether the compositor has released them or not. We are
344          * going to kill the display, so we need to return all buffers
345          * to be destroyed before this happens.
346          * Note that this is done here instead of the pool destructor
347          * because the buffers hold a reference to the pool. Also,
348          * the buffers can only be unref'ed from the display's event loop
349          * and the pool holds a reference to the display. If we drop
350          * our references here, when the compositor releases the buffers,
351          * they will be unref'ed from the event loop thread, which will
352          * unref the pool and therefore the display, which will try to
353          * stop the thread from within itself and cause a deadlock.
354          */
355         if (sink->pool) {
356           gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
357               (sink->pool));
358         }
359         g_clear_object (&sink->display);
360         g_clear_object (&sink->pool);
361       }
362       g_mutex_unlock (&sink->display_lock);
363       break;
364     default:
365       break;
366   }
367
368   return ret;
369 }
370
371 static void
372 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
373 {
374   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
375
376   if (gst_context_has_context_type (context,
377           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
378     g_mutex_lock (&sink->display_lock);
379     if (G_LIKELY (!sink->display))
380       gst_wayland_sink_set_display_from_context (sink, context);
381     else
382       GST_WARNING_OBJECT (element, "changing display handle is not supported");
383     g_mutex_unlock (&sink->display_lock);
384   }
385
386   if (GST_ELEMENT_CLASS (parent_class)->set_context)
387     GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
388 }
389
390 static GstCaps *
391 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
392 {
393   GstWaylandSink *sink;
394   GstCaps *caps;
395
396   sink = GST_WAYLAND_SINK (bsink);
397
398   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
399
400   g_mutex_lock (&sink->display_lock);
401
402   if (sink->display) {
403     GValue list = G_VALUE_INIT;
404     GValue value = G_VALUE_INIT;
405     GArray *formats;
406     gint i;
407     enum wl_shm_format fmt;
408
409     g_value_init (&list, GST_TYPE_LIST);
410     g_value_init (&value, G_TYPE_STRING);
411
412     formats = sink->display->formats;
413     for (i = 0; i < formats->len; i++) {
414       fmt = g_array_index (formats, uint32_t, i);
415       g_value_set_string (&value, gst_wayland_format_to_string (fmt));
416       gst_value_list_append_value (&list, &value);
417     }
418
419     caps = gst_caps_make_writable (caps);
420     gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
421
422     GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
423   }
424
425   g_mutex_unlock (&sink->display_lock);
426
427   if (filter) {
428     GstCaps *intersection;
429
430     intersection =
431         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
432     gst_caps_unref (caps);
433     caps = intersection;
434   }
435
436   return caps;
437 }
438
439 static gboolean
440 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
441 {
442   GstWaylandSink *sink;
443   GstBufferPool *newpool;
444   GstVideoInfo info;
445   enum wl_shm_format format;
446   GArray *formats;
447   gint i;
448   GstStructure *structure;
449   static GstAllocationParams params = { 0, 0, 0, 15, };
450
451   sink = GST_WAYLAND_SINK (bsink);
452
453   GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
454
455   /* extract info from caps */
456   if (!gst_video_info_from_caps (&info, caps))
457     goto invalid_format;
458
459   format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
460   if (format == -1)
461     goto invalid_format;
462
463   /* store the video size */
464   sink->video_width = info.width;
465   sink->video_height = info.height;
466
467   /* verify we support the requested format */
468   formats = sink->display->formats;
469   for (i = 0; i < formats->len; i++) {
470     if (g_array_index (formats, uint32_t, i) == format)
471       break;
472   }
473
474   if (i >= formats->len)
475     goto unsupported_format;
476
477   /* create a new pool for the new configuration */
478   newpool = gst_wayland_buffer_pool_new (sink->display);
479   if (!newpool)
480     goto pool_failed;
481
482   structure = gst_buffer_pool_get_config (newpool);
483   gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
484   gst_buffer_pool_config_set_allocator (structure, NULL, &params);
485   if (!gst_buffer_pool_set_config (newpool, structure))
486     goto config_failed;
487
488   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
489   gst_object_unref (newpool);
490
491   return TRUE;
492
493 invalid_format:
494   {
495     GST_DEBUG_OBJECT (sink,
496         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
497     return FALSE;
498   }
499 unsupported_format:
500   {
501     GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
502         gst_wayland_format_to_string (format));
503     return FALSE;
504   }
505 pool_failed:
506   {
507     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
508     return FALSE;
509   }
510 config_failed:
511   {
512     GST_DEBUG_OBJECT (bsink, "failed setting config");
513     return FALSE;
514   }
515 }
516
517 static gboolean
518 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
519 {
520   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
521   GstBufferPool *pool = NULL;
522   GstStructure *config;
523   GstCaps *caps;
524   guint size;
525   gboolean need_pool;
526
527   gst_query_parse_allocation (query, &caps, &need_pool);
528
529   if (caps == NULL)
530     goto no_caps;
531
532   if (sink->pool)
533     pool = gst_object_ref (sink->pool);
534
535   if (pool != NULL) {
536     GstCaps *pcaps;
537
538     /* we had a pool, check caps */
539     config = gst_buffer_pool_get_config (pool);
540     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
541
542     if (!gst_caps_is_equal (caps, pcaps)) {
543       /* different caps, we can't use this pool */
544       gst_object_unref (pool);
545       pool = NULL;
546     }
547     gst_structure_free (config);
548   }
549
550   if (pool == NULL && need_pool) {
551     GstVideoInfo info;
552
553     if (!gst_video_info_from_caps (&info, caps))
554       goto invalid_caps;
555
556     GST_DEBUG_OBJECT (sink, "create new pool");
557     pool = gst_wayland_buffer_pool_new (sink->display);
558
559     /* the normal size of a frame */
560     size = info.size;
561
562     config = gst_buffer_pool_get_config (pool);
563     gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
564     if (!gst_buffer_pool_set_config (pool, config))
565       goto config_failed;
566   }
567   if (pool) {
568     gst_query_add_allocation_pool (query, pool, size, 2, 0);
569     gst_object_unref (pool);
570   }
571
572   return TRUE;
573
574   /* ERRORS */
575 no_caps:
576   {
577     GST_DEBUG_OBJECT (bsink, "no caps specified");
578     return FALSE;
579   }
580 invalid_caps:
581   {
582     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
583     return FALSE;
584   }
585 config_failed:
586   {
587     GST_DEBUG_OBJECT (bsink, "failed setting config");
588     gst_object_unref (pool);
589     return FALSE;
590   }
591 }
592
593 static GstFlowReturn
594 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
595 {
596   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
597   return gst_wayland_sink_render (bsink, buffer);
598 }
599
600 static void
601 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
602 {
603   GstWaylandSink *sink = data;
604
605   GST_LOG ("frame_redraw_cb");
606
607   g_atomic_int_set (&sink->redraw_pending, FALSE);
608   wl_callback_destroy (callback);
609 }
610
611 static const struct wl_callback_listener frame_callback_listener = {
612   frame_redraw_callback
613 };
614
615 /* must be called with the render lock */
616 static void
617 render_last_buffer (GstWaylandSink * sink)
618 {
619   GstWlMeta *meta;
620   struct wl_surface *surface;
621   struct wl_callback *callback;
622
623   meta = gst_buffer_get_wl_meta (sink->last_buffer);
624   surface = gst_wl_window_get_wl_surface (sink->window);
625
626   g_atomic_int_set (&sink->redraw_pending, TRUE);
627   callback = wl_surface_frame (surface);
628   wl_callback_add_listener (callback, &frame_callback_listener, sink);
629
630   /* Here we essentially add a reference to the buffer. This represents
631    * the fact that the compositor is using the buffer and it should
632    * not return back to the pool and be reused until the compositor
633    * releases it. The release is handled internally in the pool */
634   gst_wayland_compositor_acquire_buffer (meta->pool, sink->last_buffer);
635
636   wl_surface_attach (surface, meta->wbuffer, 0, 0);
637   wl_surface_damage (surface, 0, 0, sink->window->surface_width,
638       sink->window->surface_height);
639
640   wl_surface_commit (surface);
641   wl_display_flush (sink->display->display);
642 }
643
644 static GstFlowReturn
645 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
646 {
647   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
648   GstBuffer *to_render;
649   GstWlMeta *meta;
650   GstFlowReturn ret = GST_FLOW_OK;
651
652   /* ask for window handle. do that before locking the sink, because
653    * set_window_handle & friends will lock it in this context */
654   if (!sink->window)
655     gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
656
657   g_mutex_lock (&sink->render_lock);
658
659   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
660
661   /* surface is resizing - drop buffers until finished */
662   if (sink->drawing_frozen)
663     goto done;
664
665   /* drop buffers until we get a frame callback */
666   if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
667     goto done;
668
669   /* if we were not provided a window, create one ourselves */
670   if (!sink->window) {
671     sink->window = gst_wl_window_new_toplevel (sink->display, sink->video_width,
672         sink->video_height);
673   } else {
674     gst_wl_window_set_video_size (sink->window, sink->video_width,
675         sink->video_height);
676     if (sink->window->surface_width == 0 || sink->window->surface_height == 0)
677       goto no_window_size;
678   }
679
680   meta = gst_buffer_get_wl_meta (buffer);
681
682   if (meta && meta->pool->display == sink->display) {
683     GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
684     to_render = buffer;
685   } else {
686     GstMapInfo src;
687     GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
688
689     if (!sink->pool)
690       goto no_pool;
691
692     if (!gst_buffer_pool_set_active (sink->pool, TRUE))
693       goto activate_failed;
694
695     ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
696     if (ret != GST_FLOW_OK)
697       goto no_buffer;
698
699     gst_buffer_map (buffer, &src, GST_MAP_READ);
700     gst_buffer_fill (to_render, 0, src.data, src.size);
701     gst_buffer_unmap (buffer, &src);
702   }
703
704   gst_buffer_replace (&sink->last_buffer, to_render);
705   render_last_buffer (sink);
706
707   /* notify _resume_rendering() in case it's waiting */
708   sink->rendered = TRUE;
709   g_cond_broadcast (&sink->render_cond);
710
711   if (buffer != to_render)
712     gst_buffer_unref (to_render);
713   goto done;
714
715 no_window_size:
716   {
717     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
718         ("Window has no size set"),
719         ("Make sure you set the size after calling set_window_handle"));
720     ret = GST_FLOW_ERROR;
721     goto done;
722   }
723 no_buffer:
724   {
725     GST_WARNING_OBJECT (sink, "could not create image");
726     goto done;
727   }
728 no_pool:
729   {
730     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
731         ("Internal error: can't allocate images"),
732         ("We don't have a bufferpool negotiated"));
733     ret = GST_FLOW_ERROR;
734     goto done;
735   }
736 activate_failed:
737   {
738     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
739     ret = GST_FLOW_ERROR;
740     goto done;
741   }
742 done:
743   {
744     g_mutex_unlock (&sink->render_lock);
745     return ret;
746   }
747 }
748
749 static void
750 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
751 {
752   iface->set_window_handle = gst_wayland_sink_set_window_handle;
753   iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
754   iface->expose = gst_wayland_sink_expose;
755 }
756
757 static void
758 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
759 {
760   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
761   struct wl_surface *surface = (struct wl_surface *) handle;
762
763   g_return_if_fail (sink != NULL);
764
765   g_mutex_lock (&sink->render_lock);
766
767   GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
768       (void *) handle);
769
770   g_clear_object (&sink->window);
771
772   if (handle) {
773     if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
774       /* we cannot use our own display with an external window handle */
775       if (G_UNLIKELY (sink->display->own_display)) {
776         GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
777             ("Application did not provide a wayland display handle"),
778             ("waylandsink cannot use an externally-supplied surface without "
779                 "an externally-supplied display handle. Consider providing a "
780                 "display handle from your application with GstContext"));
781       } else {
782         sink->window = gst_wl_window_new_in_surface (sink->display, surface);
783       }
784     } else {
785       GST_ERROR_OBJECT (sink, "Failed to find display handle, "
786           "ignoring window handle");
787     }
788   }
789
790   g_mutex_unlock (&sink->render_lock);
791 }
792
793 static void
794 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
795     gint x, gint y, gint w, gint h)
796 {
797   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
798
799   g_return_if_fail (sink != NULL);
800
801   g_mutex_lock (&sink->render_lock);
802   if (!sink->window) {
803     g_mutex_unlock (&sink->render_lock);
804     GST_WARNING_OBJECT (sink,
805         "set_render_rectangle called without window, ignoring");
806     return;
807   }
808
809   GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
810       x, y, w, h);
811   gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
812
813   g_mutex_unlock (&sink->render_lock);
814 }
815
816 static void
817 gst_wayland_sink_expose (GstVideoOverlay * overlay)
818 {
819   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
820
821   g_return_if_fail (sink != NULL);
822
823   GST_DEBUG_OBJECT (sink, "expose");
824
825   g_mutex_lock (&sink->render_lock);
826   if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
827     GST_DEBUG_OBJECT (sink, "redrawing last buffer");
828     render_last_buffer (sink);
829   }
830   g_mutex_unlock (&sink->render_lock);
831 }
832
833 static void
834 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
835 {
836   iface->pause_rendering = gst_wayland_sink_pause_rendering;
837   iface->resume_rendering = gst_wayland_sink_resume_rendering;
838 }
839
840 static void
841 gst_wayland_sink_pause_rendering (GstWaylandVideo * video)
842 {
843   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
844   g_return_if_fail (sink != NULL);
845
846   g_mutex_lock (&sink->render_lock);
847   sink->drawing_frozen = TRUE;
848   g_mutex_unlock (&sink->render_lock);
849 }
850
851 static void
852 gst_wayland_sink_resume_rendering (GstWaylandVideo * video)
853 {
854   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
855   g_return_if_fail (sink != NULL);
856
857   GST_DEBUG_OBJECT (sink, "resuming rendering");
858
859   g_mutex_lock (&sink->render_lock);
860   sink->drawing_frozen = FALSE;
861
862   if (GST_STATE (sink) == GST_STATE_PLAYING) {
863     sink->rendered = FALSE;
864     while (sink->rendered == FALSE)
865       g_cond_wait (&sink->render_cond, &sink->render_lock);
866     GST_DEBUG_OBJECT (sink, "synchronized with render()");
867   } else if (sink->window && sink->last_buffer &&
868       g_atomic_int_get (&sink->redraw_pending) == FALSE) {
869     render_last_buffer (sink);
870     GST_DEBUG_OBJECT (sink, "last buffer redrawn");
871   }
872
873   g_mutex_unlock (&sink->render_lock);
874 }
875
876 static gboolean
877 plugin_init (GstPlugin * plugin)
878 {
879   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
880       " wayland video sink");
881
882   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
883       GST_TYPE_WAYLAND_SINK);
884 }
885
886 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
887     GST_VERSION_MINOR,
888     waylandsink,
889     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
890     GST_PACKAGE_ORIGIN)