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