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