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