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