waylandsink: Support all video formats supported by the display
[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_READY_TO_PAUSED:
233       if (!sink->window)
234         gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
235       break;
236     default:
237       break;
238   }
239
240   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
241   if (ret == GST_STATE_CHANGE_FAILURE)
242     return ret;
243
244   switch (transition) {
245     case GST_STATE_CHANGE_PAUSED_TO_READY:
246       if (gst_wl_window_is_toplevel (sink->window)) {
247         /* Force all buffers to return to the pool, regardless of
248          * whether the compositor has released them or not. We are
249          * going to kill the display, so we need to return all buffers
250          * to be destroyed before this happens.
251          * Note that this is done here instead of the pool destructor
252          * because the buffers hold a reference to the pool. Also,
253          * the buffers can only be unref'ed from the display's event loop
254          * and the pool holds a reference to the display. If we drop
255          * our references here, when the compositor releases the buffers,
256          * they will be unref'ed from the event loop thread, which will
257          * unref the pool and therefore the display, which will try to
258          * stop the thread from within itself and cause a deadlock.
259          */
260         if (sink->pool) {
261           gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
262               (sink->pool));
263         }
264         gst_buffer_replace (&sink->last_buffer, NULL);
265         g_clear_object (&sink->window);
266         g_clear_object (&sink->display);
267         g_clear_object (&sink->pool);
268       }
269       break;
270     default:
271       break;
272   }
273
274   return ret;
275 }
276
277 static GstCaps *
278 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
279 {
280   GstWaylandSink *sink;
281   GstCaps *caps;
282
283   sink = GST_WAYLAND_SINK (bsink);
284
285   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
286
287   if (sink->display) {
288     GValue list = G_VALUE_INIT;
289     GValue value = G_VALUE_INIT;
290     GArray *formats;
291     gint i;
292     enum wl_shm_format fmt;
293
294     g_value_init (&list, GST_TYPE_LIST);
295     g_value_init (&value, G_TYPE_STRING);
296
297     formats = sink->display->formats;
298     for (i = 0; i < formats->len; i++) {
299       fmt = g_array_index (formats, uint32_t, i);
300       g_value_set_string (&value, gst_wayland_format_to_string (fmt));
301       gst_value_list_append_value (&list, &value);
302     }
303
304     caps = gst_caps_make_writable (caps);
305     gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
306
307     GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
308   }
309
310   if (filter) {
311     GstCaps *intersection;
312
313     intersection =
314         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
315     gst_caps_unref (caps);
316     caps = intersection;
317   }
318
319   return caps;
320 }
321
322 static gboolean
323 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
324 {
325   GstWaylandSink *sink;
326   GstBufferPool *newpool;
327   GstVideoInfo info;
328   GError *error = NULL;
329   enum wl_shm_format format;
330   GArray *formats;
331   gint i;
332   GstStructure *structure;
333   static GstAllocationParams params = { 0, 0, 0, 15, };
334
335   sink = GST_WAYLAND_SINK (bsink);
336   GST_OBJECT_LOCK (sink);
337
338   GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
339
340   /* extract info from caps */
341   if (!gst_video_info_from_caps (&info, caps))
342     goto invalid_format;
343
344   format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
345   if (format == -1)
346     goto invalid_format;
347
348   /* store the video size */
349   sink->video_width = info.width;
350   sink->video_height = info.height;
351
352   /* create the output window if needed */
353   if (!sink->window) {
354     if (!sink->display)
355       sink->display = gst_wl_display_new (sink->display_name, &error);
356
357     if (sink->display == NULL)
358       goto display_failed;
359
360     sink->window = gst_wl_window_new_toplevel (sink->display, sink->video_width,
361         sink->video_height);
362   }
363
364   /* verify we support the requested format */
365   formats = sink->display->formats;
366   for (i = 0; i < formats->len; i++) {
367     if (g_array_index (formats, uint32_t, i) == format)
368       break;
369   }
370
371   if (i >= formats->len)
372     goto unsupported_format;
373
374   /* create a new pool for the new configuration */
375   newpool = gst_wayland_buffer_pool_new (sink->display);
376   if (!newpool)
377     goto pool_failed;
378
379   structure = gst_buffer_pool_get_config (newpool);
380   gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
381   gst_buffer_pool_config_set_allocator (structure, NULL, &params);
382   if (!gst_buffer_pool_set_config (newpool, structure))
383     goto config_failed;
384
385   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
386   gst_object_unref (newpool);
387
388   GST_OBJECT_UNLOCK (sink);
389   return TRUE;
390
391 invalid_format:
392   {
393     GST_DEBUG_OBJECT (sink,
394         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
395     goto failure;
396   }
397 display_failed:
398   {
399     GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
400         ("Could not initialise Wayland output"),
401         ("Failed to create GstWlDisplay: '%s'", error->message));
402     g_error_free (error);
403     goto failure;
404   }
405 unsupported_format:
406   {
407     GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
408         gst_wayland_format_to_string (format));
409     goto failure;
410   }
411 pool_failed:
412   {
413     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
414     goto failure;
415   }
416 config_failed:
417   {
418     GST_DEBUG_OBJECT (bsink, "failed setting config");
419     goto failure;
420   }
421 failure:
422   {
423     GST_OBJECT_UNLOCK (sink);
424     return FALSE;
425   }
426 }
427
428 static gboolean
429 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
430 {
431   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
432   GstBufferPool *pool = NULL;
433   GstStructure *config;
434   GstCaps *caps;
435   guint size;
436   gboolean need_pool;
437
438   gst_query_parse_allocation (query, &caps, &need_pool);
439
440   if (caps == NULL)
441     goto no_caps;
442
443   if (sink->pool)
444     pool = gst_object_ref (sink->pool);
445
446   if (pool != NULL) {
447     GstCaps *pcaps;
448
449     /* we had a pool, check caps */
450     config = gst_buffer_pool_get_config (pool);
451     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
452
453     if (!gst_caps_is_equal (caps, pcaps)) {
454       /* different caps, we can't use this pool */
455       gst_object_unref (pool);
456       pool = NULL;
457     }
458     gst_structure_free (config);
459   }
460
461   if (pool == NULL && need_pool) {
462     GstVideoInfo info;
463
464     if (!gst_video_info_from_caps (&info, caps))
465       goto invalid_caps;
466
467     GST_DEBUG_OBJECT (sink, "create new pool");
468     pool = gst_wayland_buffer_pool_new (sink->display);
469
470     /* the normal size of a frame */
471     size = info.size;
472
473     config = gst_buffer_pool_get_config (pool);
474     gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
475     if (!gst_buffer_pool_set_config (pool, config))
476       goto config_failed;
477   }
478   if (pool) {
479     gst_query_add_allocation_pool (query, pool, size, 2, 0);
480     gst_object_unref (pool);
481   }
482
483   return TRUE;
484
485   /* ERRORS */
486 no_caps:
487   {
488     GST_DEBUG_OBJECT (bsink, "no caps specified");
489     return FALSE;
490   }
491 invalid_caps:
492   {
493     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
494     return FALSE;
495   }
496 config_failed:
497   {
498     GST_DEBUG_OBJECT (bsink, "failed setting config");
499     gst_object_unref (pool);
500     return FALSE;
501   }
502 }
503
504 static GstFlowReturn
505 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
506 {
507   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
508   return gst_wayland_sink_render (bsink, buffer);
509 }
510
511 static void
512 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
513 {
514   GstWaylandSink *sink = data;
515
516   GST_LOG ("frame_redraw_cb");
517
518   g_atomic_int_set (&sink->redraw_pending, FALSE);
519   wl_callback_destroy (callback);
520 }
521
522 static const struct wl_callback_listener frame_callback_listener = {
523   frame_redraw_callback
524 };
525
526 /* must be called with the object lock */
527 static void
528 render_last_buffer (GstWaylandSink * sink)
529 {
530   GstWlMeta *meta;
531   struct wl_surface *surface;
532   struct wl_callback *callback;
533   GstVideoRectangle src, dst, res;
534
535   meta = gst_buffer_get_wl_meta (sink->last_buffer);
536   surface = gst_wl_window_get_wl_surface (sink->window);
537
538   g_atomic_int_set (&sink->redraw_pending, TRUE);
539   callback = wl_surface_frame (surface);
540   wl_callback_add_listener (callback, &frame_callback_listener, sink);
541
542   /* Here we essentially add a reference to the buffer. This represents
543    * the fact that the compositor is using the buffer and it should
544    * not return back to the pool and be reused until the compositor
545    * releases it. The release is handled internally in the pool */
546   gst_wayland_compositor_acquire_buffer (meta->pool, sink->last_buffer);
547
548   src.w = sink->video_width;
549   src.h = sink->video_height;
550   dst.w = sink->window->width;
551   dst.h = sink->window->height;
552   gst_video_sink_center_rect (src, dst, &res, FALSE);
553
554   wl_viewport_set (sink->window->viewport, wl_fixed_from_int (0),
555       wl_fixed_from_int (0), wl_fixed_from_int (src.w),
556       wl_fixed_from_int (src.h), res.w, res.h);
557
558   wl_surface_attach (surface, meta->wbuffer, 0, 0);
559   wl_surface_damage (surface, 0, 0, res.w, res.h);
560
561   wl_surface_commit (surface);
562   wl_display_flush (sink->display->display);
563 }
564
565 static GstFlowReturn
566 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
567 {
568   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
569   GstBuffer *to_render;
570   GstWlMeta *meta;
571   GstFlowReturn ret = GST_FLOW_OK;
572
573   GST_OBJECT_LOCK (sink);
574
575   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
576
577   /* surface is resizing - drop buffers until finished */
578   if (sink->drawing_frozen)
579     goto done;
580
581   /* drop buffers until we get a frame callback */
582   if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
583     goto done;
584
585   meta = gst_buffer_get_wl_meta (buffer);
586
587   if (meta && meta->pool->display == sink->display) {
588     GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
589     to_render = buffer;
590   } else {
591     GstMapInfo src;
592     GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
593
594     if (!sink->pool)
595       goto no_pool;
596
597     if (!gst_buffer_pool_set_active (sink->pool, TRUE))
598       goto activate_failed;
599
600     ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
601     if (ret != GST_FLOW_OK)
602       goto no_buffer;
603
604     gst_buffer_map (buffer, &src, GST_MAP_READ);
605     gst_buffer_fill (to_render, 0, src.data, src.size);
606     gst_buffer_unmap (buffer, &src);
607   }
608
609   gst_buffer_replace (&sink->last_buffer, to_render);
610   render_last_buffer (sink);
611
612   /* notify _resume_rendering() in case it's waiting */
613   sink->rendered = TRUE;
614   g_cond_broadcast (&sink->render_cond);
615
616   if (buffer != to_render)
617     gst_buffer_unref (to_render);
618   goto done;
619
620 no_buffer:
621   {
622     GST_WARNING_OBJECT (sink, "could not create image");
623     goto done;
624   }
625 no_pool:
626   {
627     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
628         ("Internal error: can't allocate images"),
629         ("We don't have a bufferpool negotiated"));
630     ret = GST_FLOW_ERROR;
631     goto done;
632   }
633 activate_failed:
634   {
635     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
636     ret = GST_FLOW_ERROR;
637     goto done;
638   }
639 done:
640   {
641     GST_OBJECT_UNLOCK (sink);
642     return ret;
643   }
644 }
645
646 static void
647 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
648 {
649   iface->set_window_handle = gst_wayland_sink_set_window_handle;
650   iface->expose = gst_wayland_sink_expose;
651 }
652
653 static void
654 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
655 {
656   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
657   GstWaylandWindowHandle *whandle = (GstWaylandWindowHandle *) handle;
658   GError *error = NULL;
659   GstPad *peer;
660
661   g_return_if_fail (sink != NULL);
662
663   GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
664       (void *) handle);
665
666   if (GST_STATE (sink) > GST_STATE_READY)
667     gst_wayland_sink_pause_rendering (GST_WAYLAND_VIDEO (sink));
668
669   g_clear_object (&sink->window);
670   g_clear_object (&sink->display);
671
672   if (handle) {
673     sink->display =
674         gst_wl_display_new_existing (whandle->display, FALSE, &error);
675     if (error) {
676       GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
677           ("Could not set window handle"),
678           ("Failed to use the external wayland display: '%s'", error->message));
679       g_error_free (error);
680     } else {
681       wl_proxy_set_queue ((struct wl_proxy *) whandle->surface,
682           sink->display->queue);
683       sink->window = gst_wl_window_new_from_surface (sink->display,
684           whandle->surface, whandle->width, whandle->height);
685     }
686   } else {
687     /* reconfigure to force creation of new window in set_caps */
688     sink->negotiated = FALSE;
689     peer = gst_pad_get_peer (GST_VIDEO_SINK_PAD (sink));
690     if (peer) {
691       gst_pad_send_event (peer, gst_event_new_reconfigure ());
692       gst_object_unref (peer);
693     }
694   }
695
696   if (GST_STATE (sink) > GST_STATE_READY)
697     gst_wayland_sink_resume_rendering (GST_WAYLAND_VIDEO (sink));
698 }
699
700 static void
701 gst_wayland_sink_expose (GstVideoOverlay * overlay)
702 {
703   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
704
705   g_return_if_fail (sink != NULL);
706
707   GST_DEBUG_OBJECT (sink, "expose");
708
709   GST_OBJECT_LOCK (sink);
710   if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
711     GST_DEBUG_OBJECT (sink, "redrawing last buffer");
712     render_last_buffer (sink);
713   }
714   GST_OBJECT_UNLOCK (sink);
715 }
716
717 static void
718 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
719 {
720   iface->set_surface_size = gst_wayland_sink_set_surface_size;
721   iface->pause_rendering = gst_wayland_sink_pause_rendering;
722   iface->resume_rendering = gst_wayland_sink_resume_rendering;
723 }
724
725 static void
726 gst_wayland_sink_set_surface_size (GstWaylandVideo * video, gint w, gint h)
727 {
728   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
729
730   g_return_if_fail (sink != NULL);
731   g_return_if_fail (sink->window != NULL);
732
733   GST_OBJECT_LOCK (sink);
734   if (!sink->window) {
735     GST_OBJECT_UNLOCK (sink);
736     GST_WARNING_OBJECT (sink,
737         "set_surface_size called without window, ignoring");
738     return;
739   }
740
741   GST_DEBUG_OBJECT (sink, "changing window size to %d x %d", w, h);
742   gst_wl_window_set_size (sink->window, w, h);
743   GST_OBJECT_UNLOCK (sink);
744 }
745
746 static void
747 gst_wayland_sink_pause_rendering (GstWaylandVideo * video)
748 {
749   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
750   g_return_if_fail (sink != NULL);
751
752   GST_OBJECT_LOCK (sink);
753   sink->drawing_frozen = TRUE;
754   GST_OBJECT_UNLOCK (sink);
755 }
756
757 static void
758 gst_wayland_sink_resume_rendering (GstWaylandVideo * video)
759 {
760   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
761   g_return_if_fail (sink != NULL);
762
763   GST_DEBUG_OBJECT (sink, "resuming rendering");
764
765   GST_OBJECT_LOCK (sink);
766   sink->drawing_frozen = FALSE;
767
768   if (GST_STATE (sink) == GST_STATE_PLAYING) {
769     sink->rendered = FALSE;
770     while (sink->rendered == FALSE)
771       g_cond_wait (&sink->render_cond, GST_OBJECT_GET_LOCK (sink));
772     GST_DEBUG_OBJECT (sink, "synchronized with render()");
773   } else if (sink->window && sink->last_buffer &&
774       g_atomic_int_get (&sink->redraw_pending) == FALSE) {
775     render_last_buffer (sink);
776     GST_DEBUG_OBJECT (sink, "last buffer redrawn");
777   }
778
779   GST_OBJECT_UNLOCK (sink);
780 }
781
782 static gboolean
783 plugin_init (GstPlugin * plugin)
784 {
785   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
786       " wayland video sink");
787
788   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
789       GST_TYPE_WAYLAND_SINK);
790 }
791
792 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
793     GST_VERSION_MINOR,
794     waylandsink,
795     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
796     GST_PACKAGE_ORIGIN)