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