waylandsink: create/destroy the display when entering/leaving the READY state instead...
[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
86 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
87     GstCaps * filter);
88 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
89 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
90     GstBuffer * buffer);
91 static gboolean
92 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
93 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
94     GstBuffer * buffer);
95
96 /* VideoOverlay interface */
97 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
98     iface);
99 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
100     guintptr handle);
101 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
102
103 /* WaylandVideo interface */
104 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
105     iface);
106 static void gst_wayland_sink_set_surface_size (GstWaylandVideo * video,
107     gint w, gint h);
108 static void gst_wayland_sink_pause_rendering (GstWaylandVideo * video);
109 static void gst_wayland_sink_resume_rendering (GstWaylandVideo * video);
110
111 #define gst_wayland_sink_parent_class parent_class
112 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
113     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
114         gst_wayland_sink_videooverlay_init)
115     G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
116         gst_wayland_sink_waylandvideo_init));
117
118 static void
119 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
120 {
121   GObjectClass *gobject_class;
122   GstElementClass *gstelement_class;
123   GstBaseSinkClass *gstbasesink_class;
124
125   gobject_class = (GObjectClass *) klass;
126   gstelement_class = (GstElementClass *) klass;
127   gstbasesink_class = (GstBaseSinkClass *) klass;
128
129   gobject_class->set_property = gst_wayland_sink_set_property;
130   gobject_class->get_property = gst_wayland_sink_get_property;
131   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
132
133   gst_element_class_add_pad_template (gstelement_class,
134       gst_static_pad_template_get (&sink_template));
135
136   gst_element_class_set_static_metadata (gstelement_class,
137       "wayland video sink", "Sink/Video",
138       "Output to wayland surface",
139       "Sreerenj Balachandran <sreerenj.balachandran@intel.com>");
140
141   gstelement_class->change_state =
142       GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
143
144   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
145   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
146   gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
147   gstbasesink_class->propose_allocation =
148       GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
149   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
150
151   g_object_class_install_property (gobject_class, PROP_DISPLAY,
152       g_param_spec_string ("display", "Wayland Display name", "Wayland "
153           "display name to connect to, if not supplied with GstVideoOverlay",
154           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 }
156
157 static void
158 gst_wayland_sink_init (GstWaylandSink * sink)
159 {
160   g_cond_init (&sink->render_cond);
161 }
162
163 static void
164 gst_wayland_sink_get_property (GObject * object,
165     guint prop_id, GValue * value, GParamSpec * pspec)
166 {
167   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
168
169   switch (prop_id) {
170     case PROP_DISPLAY:
171       g_value_set_string (value, sink->display_name);
172       break;
173     default:
174       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175       break;
176   }
177 }
178
179 static void
180 gst_wayland_sink_set_property (GObject * object,
181     guint prop_id, const GValue * value, GParamSpec * pspec)
182 {
183   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
184
185   switch (prop_id) {
186     case PROP_DISPLAY:
187       sink->display_name = g_value_dup_string (value);
188       break;
189     default:
190       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191       break;
192   }
193 }
194
195 static void
196 gst_wayland_sink_finalize (GObject * object)
197 {
198   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
199
200   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
201
202   if (sink->last_buffer)
203     gst_buffer_unref (sink->last_buffer);
204   if (sink->display) {
205     /* see comment about this call in gst_wayland_sink_change_state() */
206     if (sink->pool) {
207       gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
208           (sink->pool));
209     }
210     g_object_unref (sink->display);
211   }
212   if (sink->window)
213     g_object_unref (sink->window);
214   if (sink->pool)
215     gst_object_unref (sink->pool);
216
217   if (sink->display_name)
218     g_free (sink->display_name);
219
220   g_cond_clear (&sink->render_cond);
221
222   G_OBJECT_CLASS (parent_class)->finalize (object);
223 }
224
225 static GstStateChangeReturn
226 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
227 {
228   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
229   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
230
231   switch (transition) {
232     case GST_STATE_CHANGE_NULL_TO_READY:
233       if (!sink->window)
234         gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
235
236       /* if nobody set a window handle, create at least a display */
237       if (!sink->display) {
238         GError *error = NULL;
239
240         sink->display = gst_wl_display_new (sink->display_name, &error);
241
242         if (sink->display == NULL) {
243           GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
244               ("Could not initialise Wayland output"),
245               ("Failed to create GstWlDisplay: '%s'", error->message));
246           g_error_free (error);
247           return GST_STATE_CHANGE_FAILURE;
248         }
249       }
250       break;
251     default:
252       break;
253   }
254
255   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
256   if (ret == GST_STATE_CHANGE_FAILURE)
257     return ret;
258
259   switch (transition) {
260     case GST_STATE_CHANGE_PAUSED_TO_READY:
261       if (gst_wl_window_is_toplevel (sink->window)) {
262         gst_buffer_replace (&sink->last_buffer, NULL);
263         g_clear_object (&sink->window);
264       }
265       break;
266     case GST_STATE_CHANGE_READY_TO_NULL:
267       if (sink->display && !sink->window) {     /* -> the window was toplevel */
268         /* Force all buffers to return to the pool, regardless of
269          * whether the compositor has released them or not. We are
270          * going to kill the display, so we need to return all buffers
271          * to be destroyed before this happens.
272          * Note that this is done here instead of the pool destructor
273          * because the buffers hold a reference to the pool. Also,
274          * the buffers can only be unref'ed from the display's event loop
275          * and the pool holds a reference to the display. If we drop
276          * our references here, when the compositor releases the buffers,
277          * they will be unref'ed from the event loop thread, which will
278          * unref the pool and therefore the display, which will try to
279          * stop the thread from within itself and cause a deadlock.
280          */
281         if (sink->pool) {
282           gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
283               (sink->pool));
284         }
285         g_clear_object (&sink->display);
286         g_clear_object (&sink->pool);
287       }
288       break;
289     default:
290       break;
291   }
292
293   return ret;
294 }
295
296 static GstCaps *
297 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
298 {
299   GstWaylandSink *sink;
300   GstCaps *caps;
301
302   sink = GST_WAYLAND_SINK (bsink);
303
304   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
305
306   if (sink->display) {
307     GValue list = G_VALUE_INIT;
308     GValue value = G_VALUE_INIT;
309     GArray *formats;
310     gint i;
311     enum wl_shm_format fmt;
312
313     g_value_init (&list, GST_TYPE_LIST);
314     g_value_init (&value, G_TYPE_STRING);
315
316     formats = sink->display->formats;
317     for (i = 0; i < formats->len; i++) {
318       fmt = g_array_index (formats, uint32_t, i);
319       g_value_set_string (&value, gst_wayland_format_to_string (fmt));
320       gst_value_list_append_value (&list, &value);
321     }
322
323     caps = gst_caps_make_writable (caps);
324     gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
325
326     GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
327   }
328
329   if (filter) {
330     GstCaps *intersection;
331
332     intersection =
333         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
334     gst_caps_unref (caps);
335     caps = intersection;
336   }
337
338   return caps;
339 }
340
341 static gboolean
342 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
343 {
344   GstWaylandSink *sink;
345   GstBufferPool *newpool;
346   GstVideoInfo info;
347   enum wl_shm_format format;
348   GArray *formats;
349   gint i;
350   GstStructure *structure;
351   static GstAllocationParams params = { 0, 0, 0, 15, };
352
353   sink = GST_WAYLAND_SINK (bsink);
354   GST_OBJECT_LOCK (sink);
355
356   GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
357
358   /* extract info from caps */
359   if (!gst_video_info_from_caps (&info, caps))
360     goto invalid_format;
361
362   format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
363   if (format == -1)
364     goto invalid_format;
365
366   /* store the video size */
367   sink->video_width = info.width;
368   sink->video_height = info.height;
369
370   /* verify we support the requested format */
371   formats = sink->display->formats;
372   for (i = 0; i < formats->len; i++) {
373     if (g_array_index (formats, uint32_t, i) == format)
374       break;
375   }
376
377   if (i >= formats->len)
378     goto unsupported_format;
379
380   /* create a new pool for the new configuration */
381   newpool = gst_wayland_buffer_pool_new (sink->display);
382   if (!newpool)
383     goto pool_failed;
384
385   structure = gst_buffer_pool_get_config (newpool);
386   gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
387   gst_buffer_pool_config_set_allocator (structure, NULL, &params);
388   if (!gst_buffer_pool_set_config (newpool, structure))
389     goto config_failed;
390
391   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
392   gst_object_unref (newpool);
393
394   GST_OBJECT_UNLOCK (sink);
395   return TRUE;
396
397 invalid_format:
398   {
399     GST_DEBUG_OBJECT (sink,
400         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
401     goto failure;
402   }
403 unsupported_format:
404   {
405     GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
406         gst_wayland_format_to_string (format));
407     goto failure;
408   }
409 pool_failed:
410   {
411     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
412     goto failure;
413   }
414 config_failed:
415   {
416     GST_DEBUG_OBJECT (bsink, "failed setting config");
417     goto failure;
418   }
419 failure:
420   {
421     GST_OBJECT_UNLOCK (sink);
422     return FALSE;
423   }
424 }
425
426 static gboolean
427 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
428 {
429   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
430   GstBufferPool *pool = NULL;
431   GstStructure *config;
432   GstCaps *caps;
433   guint size;
434   gboolean need_pool;
435
436   gst_query_parse_allocation (query, &caps, &need_pool);
437
438   if (caps == NULL)
439     goto no_caps;
440
441   if (sink->pool)
442     pool = gst_object_ref (sink->pool);
443
444   if (pool != NULL) {
445     GstCaps *pcaps;
446
447     /* we had a pool, check caps */
448     config = gst_buffer_pool_get_config (pool);
449     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
450
451     if (!gst_caps_is_equal (caps, pcaps)) {
452       /* different caps, we can't use this pool */
453       gst_object_unref (pool);
454       pool = NULL;
455     }
456     gst_structure_free (config);
457   }
458
459   if (pool == NULL && need_pool) {
460     GstVideoInfo info;
461
462     if (!gst_video_info_from_caps (&info, caps))
463       goto invalid_caps;
464
465     GST_DEBUG_OBJECT (sink, "create new pool");
466     pool = gst_wayland_buffer_pool_new (sink->display);
467
468     /* the normal size of a frame */
469     size = info.size;
470
471     config = gst_buffer_pool_get_config (pool);
472     gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
473     if (!gst_buffer_pool_set_config (pool, config))
474       goto config_failed;
475   }
476   if (pool) {
477     gst_query_add_allocation_pool (query, pool, size, 2, 0);
478     gst_object_unref (pool);
479   }
480
481   return TRUE;
482
483   /* ERRORS */
484 no_caps:
485   {
486     GST_DEBUG_OBJECT (bsink, "no caps specified");
487     return FALSE;
488   }
489 invalid_caps:
490   {
491     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
492     return FALSE;
493   }
494 config_failed:
495   {
496     GST_DEBUG_OBJECT (bsink, "failed setting config");
497     gst_object_unref (pool);
498     return FALSE;
499   }
500 }
501
502 static GstFlowReturn
503 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
504 {
505   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
506   return gst_wayland_sink_render (bsink, buffer);
507 }
508
509 static void
510 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
511 {
512   GstWaylandSink *sink = data;
513
514   GST_LOG ("frame_redraw_cb");
515
516   g_atomic_int_set (&sink->redraw_pending, FALSE);
517   wl_callback_destroy (callback);
518 }
519
520 static const struct wl_callback_listener frame_callback_listener = {
521   frame_redraw_callback
522 };
523
524 /* must be called with the object lock */
525 static void
526 render_last_buffer (GstWaylandSink * sink)
527 {
528   GstWlMeta *meta;
529   struct wl_surface *surface;
530   struct wl_callback *callback;
531   GstVideoRectangle src, dst, res;
532
533   meta = gst_buffer_get_wl_meta (sink->last_buffer);
534   surface = gst_wl_window_get_wl_surface (sink->window);
535
536   g_atomic_int_set (&sink->redraw_pending, TRUE);
537   callback = wl_surface_frame (surface);
538   wl_callback_add_listener (callback, &frame_callback_listener, sink);
539
540   /* Here we essentially add a reference to the buffer. This represents
541    * the fact that the compositor is using the buffer and it should
542    * not return back to the pool and be reused until the compositor
543    * releases it. The release is handled internally in the pool */
544   gst_wayland_compositor_acquire_buffer (meta->pool, sink->last_buffer);
545
546   src.w = sink->video_width;
547   src.h = sink->video_height;
548   dst.w = sink->window->width;
549   dst.h = sink->window->height;
550   gst_video_sink_center_rect (src, dst, &res, FALSE);
551
552   wl_viewport_set (sink->window->viewport, wl_fixed_from_int (0),
553       wl_fixed_from_int (0), wl_fixed_from_int (src.w),
554       wl_fixed_from_int (src.h), res.w, res.h);
555
556   wl_surface_attach (surface, meta->wbuffer, 0, 0);
557   wl_surface_damage (surface, 0, 0, res.w, res.h);
558
559   wl_surface_commit (surface);
560   wl_display_flush (sink->display->display);
561 }
562
563 static GstFlowReturn
564 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
565 {
566   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
567   GstBuffer *to_render;
568   GstWlMeta *meta;
569   GstFlowReturn ret = GST_FLOW_OK;
570
571   GST_OBJECT_LOCK (sink);
572
573   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
574
575   if (!sink->window)
576     sink->window = gst_wl_window_new_toplevel (sink->display, sink->video_width,
577         sink->video_height);
578
579   /* surface is resizing - drop buffers until finished */
580   if (sink->drawing_frozen)
581     goto done;
582
583   /* drop buffers until we get a frame callback */
584   if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
585     goto done;
586
587   meta = gst_buffer_get_wl_meta (buffer);
588
589   if (meta && meta->pool->display == sink->display) {
590     GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
591     to_render = buffer;
592   } else {
593     GstMapInfo src;
594     GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
595
596     if (!sink->pool)
597       goto no_pool;
598
599     if (!gst_buffer_pool_set_active (sink->pool, TRUE))
600       goto activate_failed;
601
602     ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
603     if (ret != GST_FLOW_OK)
604       goto no_buffer;
605
606     gst_buffer_map (buffer, &src, GST_MAP_READ);
607     gst_buffer_fill (to_render, 0, src.data, src.size);
608     gst_buffer_unmap (buffer, &src);
609   }
610
611   gst_buffer_replace (&sink->last_buffer, to_render);
612   render_last_buffer (sink);
613
614   /* notify _resume_rendering() in case it's waiting */
615   sink->rendered = TRUE;
616   g_cond_broadcast (&sink->render_cond);
617
618   if (buffer != to_render)
619     gst_buffer_unref (to_render);
620   goto done;
621
622 no_buffer:
623   {
624     GST_WARNING_OBJECT (sink, "could not create image");
625     goto done;
626   }
627 no_pool:
628   {
629     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
630         ("Internal error: can't allocate images"),
631         ("We don't have a bufferpool negotiated"));
632     ret = GST_FLOW_ERROR;
633     goto done;
634   }
635 activate_failed:
636   {
637     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
638     ret = GST_FLOW_ERROR;
639     goto done;
640   }
641 done:
642   {
643     GST_OBJECT_UNLOCK (sink);
644     return ret;
645   }
646 }
647
648 static void
649 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
650 {
651   iface->set_window_handle = gst_wayland_sink_set_window_handle;
652   iface->expose = gst_wayland_sink_expose;
653 }
654
655 static void
656 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
657 {
658   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
659   GstWaylandWindowHandle *whandle = (GstWaylandWindowHandle *) handle;
660   GError *error = NULL;
661
662   g_return_if_fail (sink != NULL);
663   g_return_if_fail (GST_STATE (sink) < GST_STATE_PAUSED);
664
665   GST_OBJECT_LOCK (sink);
666
667   GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
668       (void *) handle);
669
670   g_clear_object (&sink->window);
671   g_clear_object (&sink->display);
672
673   if (handle) {
674     sink->display =
675         gst_wl_display_new_existing (whandle->display, FALSE, &error);
676     if (error) {
677       GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
678           ("Could not set window handle"),
679           ("Failed to use the external wayland display: '%s'", error->message));
680       g_error_free (error);
681     } else {
682       wl_proxy_set_queue ((struct wl_proxy *) whandle->surface,
683           sink->display->queue);
684       sink->window = gst_wl_window_new_from_surface (sink->display,
685           whandle->surface, whandle->width, whandle->height);
686     }
687   }
688
689   if (!sink->display && GST_STATE (sink) == GST_STATE_READY) {
690     /* we need a display to be in READY */
691     sink->display = gst_wl_display_new (sink->display_name, &error);
692
693     if (sink->display == NULL) {
694       GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
695           ("Could not initialise Wayland output"),
696           ("Failed to create GstWlDisplay: '%s'", error->message));
697       g_error_free (error);
698     }
699   }
700
701   GST_OBJECT_UNLOCK (sink);
702 }
703
704 static void
705 gst_wayland_sink_expose (GstVideoOverlay * overlay)
706 {
707   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
708
709   g_return_if_fail (sink != NULL);
710
711   GST_DEBUG_OBJECT (sink, "expose");
712
713   GST_OBJECT_LOCK (sink);
714   if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
715     GST_DEBUG_OBJECT (sink, "redrawing last buffer");
716     render_last_buffer (sink);
717   }
718   GST_OBJECT_UNLOCK (sink);
719 }
720
721 static void
722 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
723 {
724   iface->set_surface_size = gst_wayland_sink_set_surface_size;
725   iface->pause_rendering = gst_wayland_sink_pause_rendering;
726   iface->resume_rendering = gst_wayland_sink_resume_rendering;
727 }
728
729 static void
730 gst_wayland_sink_set_surface_size (GstWaylandVideo * video, gint w, gint h)
731 {
732   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
733
734   g_return_if_fail (sink != NULL);
735   g_return_if_fail (sink->window != NULL);
736
737   GST_OBJECT_LOCK (sink);
738   if (!sink->window) {
739     GST_OBJECT_UNLOCK (sink);
740     GST_WARNING_OBJECT (sink,
741         "set_surface_size called without window, ignoring");
742     return;
743   }
744
745   GST_DEBUG_OBJECT (sink, "changing window size to %d x %d", w, h);
746   gst_wl_window_set_size (sink->window, w, h);
747   GST_OBJECT_UNLOCK (sink);
748 }
749
750 static void
751 gst_wayland_sink_pause_rendering (GstWaylandVideo * video)
752 {
753   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
754   g_return_if_fail (sink != NULL);
755
756   GST_OBJECT_LOCK (sink);
757   sink->drawing_frozen = TRUE;
758   GST_OBJECT_UNLOCK (sink);
759 }
760
761 static void
762 gst_wayland_sink_resume_rendering (GstWaylandVideo * video)
763 {
764   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
765   g_return_if_fail (sink != NULL);
766
767   GST_DEBUG_OBJECT (sink, "resuming rendering");
768
769   GST_OBJECT_LOCK (sink);
770   sink->drawing_frozen = FALSE;
771
772   if (GST_STATE (sink) == GST_STATE_PLAYING) {
773     sink->rendered = FALSE;
774     while (sink->rendered == FALSE)
775       g_cond_wait (&sink->render_cond, GST_OBJECT_GET_LOCK (sink));
776     GST_DEBUG_OBJECT (sink, "synchronized with render()");
777   } else if (sink->window && sink->last_buffer &&
778       g_atomic_int_get (&sink->redraw_pending) == FALSE) {
779     render_last_buffer (sink);
780     GST_DEBUG_OBJECT (sink, "last buffer redrawn");
781   }
782
783   GST_OBJECT_UNLOCK (sink);
784 }
785
786 static gboolean
787 plugin_init (GstPlugin * plugin)
788 {
789   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
790       " wayland video sink");
791
792   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
793       GST_TYPE_WAYLAND_SINK);
794 }
795
796 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
797     GST_VERSION_MINOR,
798     waylandsink,
799     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
800     GST_PACKAGE_ORIGIN)