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