documentation: fixed a heap o' typos
[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  * @title: waylandsink
27  *
28  *  The waylandsink is creating its own window and render the decoded video frames to that.
29  *  Setup the Wayland environment as described in
30  *  [Wayland](http://wayland.freedesktop.org/building.html) home page.
31  *
32  *  The current implementation is based on weston compositor.
33  *
34  * ## Example pipelines
35  * |[
36  * gst-launch-1.0 -v videotestsrc ! waylandsink
37  * ]| test the video rendering in wayland
38  *
39  */
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 #include "gstwaylandsink.h"
46 #include "wlvideoformat.h"
47 #include "wlbuffer.h"
48 #include "wlshmallocator.h"
49 #include "wllinuxdmabuf.h"
50
51 #include <gst/wayland/wayland.h>
52 #include <gst/video/videooverlay.h>
53
54 /* signals */
55 enum
56 {
57   SIGNAL_0,
58   LAST_SIGNAL
59 };
60
61 /* Properties */
62 enum
63 {
64   PROP_0,
65   PROP_DISPLAY,
66   PROP_FULLSCREEN
67 };
68
69 GST_DEBUG_CATEGORY (gstwayland_debug);
70 #define GST_CAT_DEFAULT gstwayland_debug
71
72 #define WL_VIDEO_FORMATS \
73     "{ BGRx, BGRA, RGBx, xBGR, xRGB, RGBA, ABGR, ARGB, RGB, BGR, " \
74     "RGB16, BGR16, YUY2, YVYU, UYVY, AYUV, NV12, NV21, NV16, " \
75     "YUV9, YVU9, Y41B, I420, YV12, Y42B, v308 }"
76
77 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
78     GST_PAD_SINK,
79     GST_PAD_ALWAYS,
80     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (WL_VIDEO_FORMATS) ";"
81         GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_DMABUF,
82             WL_VIDEO_FORMATS))
83     );
84
85 static void gst_wayland_sink_get_property (GObject * object,
86     guint prop_id, GValue * value, GParamSpec * pspec);
87 static void gst_wayland_sink_set_property (GObject * object,
88     guint prop_id, const GValue * value, GParamSpec * pspec);
89 static void gst_wayland_sink_finalize (GObject * object);
90
91 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
92     GstStateChange transition);
93 static void gst_wayland_sink_set_context (GstElement * element,
94     GstContext * context);
95
96 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
97     GstCaps * filter);
98 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
99 static gboolean
100 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
101 static GstFlowReturn gst_wayland_sink_show_frame (GstVideoSink * bsink,
102     GstBuffer * buffer);
103
104 /* VideoOverlay interface */
105 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
106     iface);
107 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
108     guintptr handle);
109 static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
110     gint x, gint y, gint w, gint h);
111 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
112
113 /* WaylandVideo interface */
114 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
115     iface);
116 static void gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video);
117 static void gst_wayland_sink_end_geometry_change (GstWaylandVideo * video);
118
119 #define gst_wayland_sink_parent_class parent_class
120 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
121     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
122         gst_wayland_sink_videooverlay_init)
123     G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
124         gst_wayland_sink_waylandvideo_init));
125
126 /* A tiny GstVideoBufferPool subclass that modify the options to remove
127  * VideoAlignment. To support VideoAlignment we would need to pass the padded
128  * width/height + stride and use the viewporter interface to crop, a bit like
129  * we use to do with XV. It would still be quite limited. It's a bit retro,
130  * hopefully there will be a better Wayland interface in the future. */
131
132 GType gst_wayland_pool_get_type (void);
133
134 typedef struct
135 {
136   GstVideoBufferPool parent;
137 } GstWaylandPool;
138
139 typedef struct
140 {
141   GstVideoBufferPoolClass parent;
142 } GstWaylandPoolClass;
143
144 G_DEFINE_TYPE (GstWaylandPool, gst_wayland_pool, GST_TYPE_VIDEO_BUFFER_POOL);
145
146 static const gchar **
147 gst_wayland_pool_get_options (GstBufferPool * pool)
148 {
149   static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META, NULL };
150   return options;
151 }
152
153 static void
154 gst_wayland_pool_class_init (GstWaylandPoolClass * klass)
155 {
156   GstBufferPoolClass *pool_class = GST_BUFFER_POOL_CLASS (klass);
157   pool_class->get_options = gst_wayland_pool_get_options;
158 }
159
160 static void
161 gst_wayland_pool_init (GstWaylandPool * pool)
162 {
163 }
164
165 static void
166 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
167 {
168   GObjectClass *gobject_class;
169   GstElementClass *gstelement_class;
170   GstBaseSinkClass *gstbasesink_class;
171   GstVideoSinkClass *gstvideosink_class;
172
173   gobject_class = (GObjectClass *) klass;
174   gstelement_class = (GstElementClass *) klass;
175   gstbasesink_class = (GstBaseSinkClass *) klass;
176   gstvideosink_class = (GstVideoSinkClass *) klass;
177
178   gobject_class->set_property = gst_wayland_sink_set_property;
179   gobject_class->get_property = gst_wayland_sink_get_property;
180   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
181
182   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
183
184   gst_element_class_set_static_metadata (gstelement_class,
185       "wayland video sink", "Sink/Video",
186       "Output to wayland surface",
187       "Sreerenj Balachandran <sreerenj.balachandran@intel.com>, "
188       "George Kiagiadakis <george.kiagiadakis@collabora.com>");
189
190   gstelement_class->change_state =
191       GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
192   gstelement_class->set_context =
193       GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
194
195   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
196   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
197   gstbasesink_class->propose_allocation =
198       GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
199
200   gstvideosink_class->show_frame =
201       GST_DEBUG_FUNCPTR (gst_wayland_sink_show_frame);
202
203   g_object_class_install_property (gobject_class, PROP_DISPLAY,
204       g_param_spec_string ("display", "Wayland Display name", "Wayland "
205           "display name to connect to, if not supplied via the GstContext",
206           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207
208   g_object_class_install_property (gobject_class, PROP_FULLSCREEN,
209       g_param_spec_boolean ("fullscreen", "Fullscreen",
210           "Whether the surface should be made fullscreen ", FALSE,
211           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212 }
213
214 static void
215 gst_wayland_sink_init (GstWaylandSink * sink)
216 {
217   g_mutex_init (&sink->display_lock);
218   g_mutex_init (&sink->render_lock);
219 }
220
221 static void
222 gst_wayland_sink_set_fullscreen (GstWaylandSink * sink, gboolean fullscreen)
223 {
224   if (fullscreen == sink->fullscreen)
225     return;
226
227   g_mutex_lock (&sink->render_lock);
228   sink->fullscreen = fullscreen;
229   gst_wl_window_ensure_fullscreen (sink->window, fullscreen);
230   g_mutex_unlock (&sink->render_lock);
231 }
232
233 static void
234 gst_wayland_sink_get_property (GObject * object,
235     guint prop_id, GValue * value, GParamSpec * pspec)
236 {
237   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
238
239   switch (prop_id) {
240     case PROP_DISPLAY:
241       GST_OBJECT_LOCK (sink);
242       g_value_set_string (value, sink->display_name);
243       GST_OBJECT_UNLOCK (sink);
244       break;
245     case PROP_FULLSCREEN:
246       GST_OBJECT_LOCK (sink);
247       g_value_set_boolean (value, sink->fullscreen);
248       GST_OBJECT_UNLOCK (sink);
249       break;
250     default:
251       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252       break;
253   }
254 }
255
256 static void
257 gst_wayland_sink_set_property (GObject * object,
258     guint prop_id, const GValue * value, GParamSpec * pspec)
259 {
260   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
261
262   switch (prop_id) {
263     case PROP_DISPLAY:
264       GST_OBJECT_LOCK (sink);
265       sink->display_name = g_value_dup_string (value);
266       GST_OBJECT_UNLOCK (sink);
267       break;
268     case PROP_FULLSCREEN:
269       GST_OBJECT_LOCK (sink);
270       gst_wayland_sink_set_fullscreen (sink, g_value_get_boolean (value));
271       GST_OBJECT_UNLOCK (sink);
272       break;
273     default:
274       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275       break;
276   }
277 }
278
279 static void
280 gst_wayland_sink_finalize (GObject * object)
281 {
282   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
283
284   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
285
286   if (sink->last_buffer)
287     gst_buffer_unref (sink->last_buffer);
288   if (sink->display)
289     g_object_unref (sink->display);
290   if (sink->window)
291     g_object_unref (sink->window);
292   if (sink->pool)
293     gst_object_unref (sink->pool);
294
295   g_free (sink->display_name);
296
297   g_mutex_clear (&sink->display_lock);
298   g_mutex_clear (&sink->render_lock);
299
300   G_OBJECT_CLASS (parent_class)->finalize (object);
301 }
302
303 /* must be called with the display_lock */
304 static void
305 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
306     GstContext * context)
307 {
308   struct wl_display *display;
309   GError *error = NULL;
310
311   display = gst_wayland_display_handle_context_get_handle (context);
312   sink->display = gst_wl_display_new_existing (display, FALSE, &error);
313
314   if (error) {
315     GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
316         ("Could not set display handle"),
317         ("Failed to use the external wayland display: '%s'", error->message));
318     g_error_free (error);
319   }
320 }
321
322 static gboolean
323 gst_wayland_sink_find_display (GstWaylandSink * sink)
324 {
325   GstQuery *query;
326   GstMessage *msg;
327   GstContext *context = NULL;
328   GError *error = NULL;
329   gboolean ret = TRUE;
330
331   g_mutex_lock (&sink->display_lock);
332
333   if (!sink->display) {
334     /* first query upstream for the needed display handle */
335     query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
336     if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
337       gst_query_parse_context (query, &context);
338       gst_wayland_sink_set_display_from_context (sink, context);
339     }
340     gst_query_unref (query);
341
342     if (G_LIKELY (!sink->display)) {
343       /* now ask the application to set the display handle */
344       msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
345           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
346
347       g_mutex_unlock (&sink->display_lock);
348       gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
349       /* at this point we expect gst_wayland_sink_set_context
350        * to get called and fill sink->display */
351       g_mutex_lock (&sink->display_lock);
352
353       if (!sink->display) {
354         /* if the application didn't set a display, let's create it ourselves */
355         GST_OBJECT_LOCK (sink);
356         sink->display = gst_wl_display_new (sink->display_name, &error);
357         GST_OBJECT_UNLOCK (sink);
358
359         if (error) {
360           GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
361               ("Could not initialise Wayland output"),
362               ("Failed to create GstWlDisplay: '%s'", error->message));
363           g_error_free (error);
364           ret = FALSE;
365         }
366       }
367     }
368   }
369
370   g_mutex_unlock (&sink->display_lock);
371
372   return ret;
373 }
374
375 static GstStateChangeReturn
376 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
377 {
378   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
379   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
380
381   switch (transition) {
382     case GST_STATE_CHANGE_NULL_TO_READY:
383       if (!gst_wayland_sink_find_display (sink))
384         return GST_STATE_CHANGE_FAILURE;
385       break;
386     default:
387       break;
388   }
389
390   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
391   if (ret == GST_STATE_CHANGE_FAILURE)
392     return ret;
393
394   switch (transition) {
395     case GST_STATE_CHANGE_PAUSED_TO_READY:
396       gst_buffer_replace (&sink->last_buffer, NULL);
397       if (sink->window) {
398         if (gst_wl_window_is_toplevel (sink->window)) {
399           g_clear_object (&sink->window);
400         } else {
401           /* remove buffer from surface, show nothing */
402           gst_wl_window_render (sink->window, NULL, NULL);
403         }
404       }
405       break;
406     case GST_STATE_CHANGE_READY_TO_NULL:
407       g_mutex_lock (&sink->display_lock);
408       /* If we had a toplevel window, we most likely have our own connection
409        * to the display too, and it is a good idea to disconnect and allow
410        * potentially the application to embed us with GstVideoOverlay
411        * (which requires to re-use the same display connection as the parent
412        * surface). If we didn't have a toplevel window, then the display
413        * connection that we have is definitely shared with the application
414        * and it's better to keep it around (together with the window handle)
415        * to avoid requesting them again from the application if/when we are
416        * restarted (GstVideoOverlay behaves like that in other sinks)
417        */
418       if (sink->display && !sink->window) {     /* -> the window was toplevel */
419         g_clear_object (&sink->display);
420         g_mutex_lock (&sink->render_lock);
421         sink->redraw_pending = FALSE;
422         g_mutex_unlock (&sink->render_lock);
423       }
424       g_mutex_unlock (&sink->display_lock);
425       g_clear_object (&sink->pool);
426       break;
427     default:
428       break;
429   }
430
431   return ret;
432 }
433
434 static void
435 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
436 {
437   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
438
439   if (gst_context_has_context_type (context,
440           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
441     g_mutex_lock (&sink->display_lock);
442     if (G_LIKELY (!sink->display)) {
443       gst_wayland_sink_set_display_from_context (sink, context);
444     } else {
445       GST_WARNING_OBJECT (element, "changing display handle is not supported");
446       g_mutex_unlock (&sink->display_lock);
447       return;
448     }
449     g_mutex_unlock (&sink->display_lock);
450   }
451
452   if (GST_ELEMENT_CLASS (parent_class)->set_context)
453     GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
454 }
455
456 static GstCaps *
457 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
458 {
459   GstWaylandSink *sink;
460   GstCaps *caps;
461
462   sink = GST_WAYLAND_SINK (bsink);
463
464   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
465   caps = gst_caps_make_writable (caps);
466
467   g_mutex_lock (&sink->display_lock);
468
469   if (sink->display) {
470     GValue shm_list = G_VALUE_INIT, dmabuf_list = G_VALUE_INIT;
471     GValue value = G_VALUE_INIT;
472     GArray *formats;
473     gint i;
474     guint fmt;
475     GstVideoFormat gfmt;
476
477     g_value_init (&shm_list, GST_TYPE_LIST);
478     g_value_init (&dmabuf_list, GST_TYPE_LIST);
479
480     /* Add corresponding shm formats */
481     formats = sink->display->shm_formats;
482     for (i = 0; i < formats->len; i++) {
483       fmt = g_array_index (formats, uint32_t, i);
484       gfmt = gst_wl_shm_format_to_video_format (fmt);
485       if (gfmt != GST_VIDEO_FORMAT_UNKNOWN) {
486         g_value_init (&value, G_TYPE_STRING);
487         g_value_set_static_string (&value, gst_video_format_to_string (gfmt));
488         gst_value_list_append_and_take_value (&shm_list, &value);
489       }
490     }
491
492     gst_structure_take_value (gst_caps_get_structure (caps, 0), "format",
493         &shm_list);
494
495     /* Add corresponding dmabuf formats */
496     formats = sink->display->dmabuf_formats;
497     for (i = 0; i < formats->len; i++) {
498       fmt = g_array_index (formats, uint32_t, i);
499       gfmt = gst_wl_dmabuf_format_to_video_format (fmt);
500       if (gfmt != GST_VIDEO_FORMAT_UNKNOWN) {
501         g_value_init (&value, G_TYPE_STRING);
502         g_value_set_static_string (&value, gst_video_format_to_string (gfmt));
503         gst_value_list_append_and_take_value (&dmabuf_list, &value);
504       }
505     }
506
507     gst_structure_take_value (gst_caps_get_structure (caps, 1), "format",
508         &dmabuf_list);
509
510     GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
511   }
512
513   g_mutex_unlock (&sink->display_lock);
514
515   if (filter) {
516     GstCaps *intersection;
517
518     intersection =
519         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
520     gst_caps_unref (caps);
521     caps = intersection;
522   }
523
524   return caps;
525 }
526
527 static GstBufferPool *
528 gst_wayland_create_pool (GstWaylandSink * sink, GstCaps * caps)
529 {
530   GstBufferPool *pool = NULL;
531   GstStructure *structure;
532   gsize size = sink->video_info.size;
533   GstAllocator *alloc;
534
535   pool = g_object_new (gst_wayland_pool_get_type (), NULL);
536
537   structure = gst_buffer_pool_get_config (pool);
538   gst_buffer_pool_config_set_params (structure, caps, size, 2, 0);
539
540   alloc = gst_wl_shm_allocator_get ();
541   gst_buffer_pool_config_set_allocator (structure, alloc, NULL);
542   if (!gst_buffer_pool_set_config (pool, structure)) {
543     g_object_unref (pool);
544     pool = NULL;
545   }
546   g_object_unref (alloc);
547
548   return pool;
549 }
550
551 static gboolean
552 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
553 {
554   GstWaylandSink *sink;
555   gboolean use_dmabuf;
556   GstVideoFormat format;
557
558   sink = GST_WAYLAND_SINK (bsink);
559
560   GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
561
562   /* extract info from caps */
563   if (!gst_video_info_from_caps (&sink->video_info, caps))
564     goto invalid_format;
565
566   format = GST_VIDEO_INFO_FORMAT (&sink->video_info);
567   sink->video_info_changed = TRUE;
568
569   /* create a new pool for the new caps */
570   if (sink->pool)
571     gst_object_unref (sink->pool);
572   sink->pool = gst_wayland_create_pool (sink, caps);
573
574   use_dmabuf = gst_caps_features_contains (gst_caps_get_features (caps, 0),
575       GST_CAPS_FEATURE_MEMORY_DMABUF);
576
577   /* validate the format base on the memory type. */
578   if (use_dmabuf) {
579     if (!gst_wl_display_check_format_for_dmabuf (sink->display, format))
580       goto unsupported_format;
581   } else if (!gst_wl_display_check_format_for_shm (sink->display, format)) {
582     goto unsupported_format;
583   }
584
585   sink->use_dmabuf = use_dmabuf;
586
587   return TRUE;
588
589 invalid_format:
590   {
591     GST_ERROR_OBJECT (sink,
592         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
593     return FALSE;
594   }
595 unsupported_format:
596   {
597     GST_ERROR_OBJECT (sink, "Format %s is not available on the display",
598         gst_video_format_to_string (format));
599     return FALSE;
600   }
601 }
602
603 static gboolean
604 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
605 {
606   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
607   GstCaps *caps;
608   GstBufferPool *pool = NULL;
609   gboolean need_pool;
610   GstAllocator *alloc;
611
612   gst_query_parse_allocation (query, &caps, &need_pool);
613
614   if (need_pool)
615     pool = gst_wayland_create_pool (sink, caps);
616
617   gst_query_add_allocation_pool (query, pool, sink->video_info.size, 2, 0);
618   if (pool)
619     g_object_unref (pool);
620
621   alloc = gst_wl_shm_allocator_get ();
622   gst_query_add_allocation_param (query, alloc, NULL);
623   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
624   g_object_unref (alloc);
625
626   return TRUE;
627 }
628
629 static void
630 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
631 {
632   GstWaylandSink *sink = data;
633
634   GST_LOG ("frame_redraw_cb");
635
636   g_mutex_lock (&sink->render_lock);
637   sink->redraw_pending = FALSE;
638   g_mutex_unlock (&sink->render_lock);
639
640   wl_callback_destroy (callback);
641 }
642
643 static const struct wl_callback_listener frame_callback_listener = {
644   frame_redraw_callback
645 };
646
647 /* must be called with the render lock */
648 static void
649 render_last_buffer (GstWaylandSink * sink, gboolean redraw)
650 {
651   GstWlBuffer *wlbuffer;
652   const GstVideoInfo *info = NULL;
653   struct wl_surface *surface;
654   struct wl_callback *callback;
655
656   wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
657   surface = gst_wl_window_get_wl_surface (sink->window);
658
659   sink->redraw_pending = TRUE;
660   callback = wl_surface_frame (surface);
661   wl_callback_add_listener (callback, &frame_callback_listener, sink);
662
663   if (G_UNLIKELY (sink->video_info_changed && !redraw)) {
664     info = &sink->video_info;
665     sink->video_info_changed = FALSE;
666   }
667   gst_wl_window_render (sink->window, wlbuffer, info);
668 }
669
670 static void
671 on_window_closed (GstWlWindow * window, gpointer user_data)
672 {
673   GstWaylandSink *sink = GST_WAYLAND_SINK (user_data);
674
675   /* Handle window closure by posting an error on the bus */
676   GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
677       ("Output window was closed"), (NULL));
678 }
679
680 static GstFlowReturn
681 gst_wayland_sink_show_frame (GstVideoSink * vsink, GstBuffer * buffer)
682 {
683   GstWaylandSink *sink = GST_WAYLAND_SINK (vsink);
684   GstBuffer *to_render;
685   GstWlBuffer *wlbuffer;
686   GstVideoMeta *vmeta;
687   GstVideoFormat format;
688   GstVideoInfo old_vinfo;
689   GstMemory *mem;
690   struct wl_buffer *wbuf = NULL;
691
692   GstFlowReturn ret = GST_FLOW_OK;
693
694   g_mutex_lock (&sink->render_lock);
695
696   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
697
698   if (G_UNLIKELY (!sink->window)) {
699     /* ask for window handle. Unlock render_lock while doing that because
700      * set_window_handle & friends will lock it in this context */
701     g_mutex_unlock (&sink->render_lock);
702     gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
703     g_mutex_lock (&sink->render_lock);
704
705     if (!sink->window) {
706       /* if we were not provided a window, create one ourselves */
707       sink->window = gst_wl_window_new_toplevel (sink->display,
708           &sink->video_info, sink->fullscreen, &sink->render_lock);
709       g_signal_connect_object (sink->window, "closed",
710           G_CALLBACK (on_window_closed), sink, 0);
711     }
712   }
713
714   /* drop buffers until we get a frame callback */
715   if (sink->redraw_pending) {
716     GST_LOG_OBJECT (sink, "buffer %p dropped (redraw pending)", buffer);
717     goto done;
718   }
719
720   /* make sure that the application has called set_render_rectangle() */
721   if (G_UNLIKELY (sink->window->render_rectangle.w == 0))
722     goto no_window_size;
723
724   wlbuffer = gst_buffer_get_wl_buffer (buffer);
725
726   if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) {
727     GST_LOG_OBJECT (sink, "buffer %p has a wl_buffer from our display, "
728         "writing directly", buffer);
729     to_render = buffer;
730     goto render;
731   }
732
733   /* update video info from video meta */
734   mem = gst_buffer_peek_memory (buffer, 0);
735
736   old_vinfo = sink->video_info;
737   vmeta = gst_buffer_get_video_meta (buffer);
738   if (vmeta) {
739     gint i;
740
741     for (i = 0; i < vmeta->n_planes; i++) {
742       sink->video_info.offset[i] = vmeta->offset[i];
743       sink->video_info.stride[i] = vmeta->stride[i];
744     }
745     sink->video_info.size = gst_buffer_get_size (buffer);
746   }
747
748   GST_LOG_OBJECT (sink, "buffer %p does not have a wl_buffer from our "
749       "display, creating it", buffer);
750
751   format = GST_VIDEO_INFO_FORMAT (&sink->video_info);
752   if (gst_wl_display_check_format_for_dmabuf (sink->display, format)) {
753     guint i, nb_dmabuf = 0;
754
755     for (i = 0; i < gst_buffer_n_memory (buffer); i++)
756       if (gst_is_dmabuf_memory (gst_buffer_peek_memory (buffer, i)))
757         nb_dmabuf++;
758
759     if (nb_dmabuf && (nb_dmabuf == gst_buffer_n_memory (buffer)))
760       wbuf = gst_wl_linux_dmabuf_construct_wl_buffer (buffer, sink->display,
761           &sink->video_info);
762   }
763
764   if (!wbuf && gst_wl_display_check_format_for_shm (sink->display, format)) {
765     if (gst_buffer_n_memory (buffer) == 1 && gst_is_fd_memory (mem))
766       wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
767           &sink->video_info);
768
769     /* If nothing worked, copy into our internal pool */
770     if (!wbuf) {
771       GstVideoFrame src, dst;
772       GstVideoInfo src_info = sink->video_info;
773
774       /* rollback video info changes */
775       sink->video_info = old_vinfo;
776
777       /* we don't know how to create a wl_buffer directly from the provided
778        * memory, so we have to copy the data to shm memory that we know how
779        * to handle... */
780
781       GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, "
782           "copying to wl_shm memory", buffer);
783
784       /* sink->pool always exists (created in set_caps), but it may not
785        * be active if upstream is not using it */
786       if (!gst_buffer_pool_is_active (sink->pool)) {
787         GstStructure *config;
788         GstCaps *caps;
789
790         config = gst_buffer_pool_get_config (sink->pool);
791         gst_buffer_pool_config_get_params (config, &caps, NULL, NULL, NULL);
792
793         /* revert back to default strides and offsets */
794         gst_video_info_from_caps (&sink->video_info, caps);
795         gst_buffer_pool_config_set_params (config, caps, sink->video_info.size,
796             2, 0);
797
798         /* This is a video pool, it should not fail with basic settings */
799         if (!gst_buffer_pool_set_config (sink->pool, config) ||
800             !gst_buffer_pool_set_active (sink->pool, TRUE))
801           goto activate_failed;
802       }
803
804       ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
805       if (ret != GST_FLOW_OK)
806         goto no_buffer;
807
808       wlbuffer = gst_buffer_get_wl_buffer (to_render);
809
810       /* attach a wl_buffer if there isn't one yet */
811       if (G_UNLIKELY (!wlbuffer)) {
812         mem = gst_buffer_peek_memory (to_render, 0);
813         wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
814             &sink->video_info);
815
816         if (G_UNLIKELY (!wbuf))
817           goto no_wl_buffer_shm;
818
819         gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
820       }
821
822       if (!gst_video_frame_map (&dst, &sink->video_info, to_render,
823               GST_MAP_WRITE))
824         goto dst_map_failed;
825
826       if (!gst_video_frame_map (&src, &src_info, buffer, GST_MAP_READ)) {
827         gst_video_frame_unmap (&dst);
828         goto src_map_failed;
829       }
830
831       gst_video_frame_copy (&dst, &src);
832
833       gst_video_frame_unmap (&src);
834       gst_video_frame_unmap (&dst);
835
836       goto render;
837     }
838   }
839
840   if (!wbuf)
841     goto no_wl_buffer;
842
843   gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
844   to_render = buffer;
845
846 render:
847   /* drop double rendering */
848   if (G_UNLIKELY (to_render == sink->last_buffer)) {
849     GST_LOG_OBJECT (sink, "Buffer already being rendered");
850     goto done;
851   }
852
853   gst_buffer_replace (&sink->last_buffer, to_render);
854   render_last_buffer (sink, FALSE);
855
856   if (buffer != to_render)
857     gst_buffer_unref (to_render);
858   goto done;
859
860 no_window_size:
861   {
862     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
863         ("Window has no size set"),
864         ("Make sure you set the size after calling set_window_handle"));
865     ret = GST_FLOW_ERROR;
866     goto done;
867   }
868 no_buffer:
869   {
870     GST_WARNING_OBJECT (sink, "could not create buffer");
871     goto done;
872   }
873 no_wl_buffer_shm:
874   {
875     GST_ERROR_OBJECT (sink, "could not create wl_buffer out of wl_shm memory");
876     ret = GST_FLOW_ERROR;
877     goto done;
878   }
879 no_wl_buffer:
880   {
881     GST_ERROR_OBJECT (sink, "buffer %p cannot have a wl_buffer", buffer);
882     ret = GST_FLOW_ERROR;
883     goto done;
884   }
885 activate_failed:
886   {
887     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
888     ret = GST_FLOW_ERROR;
889     goto done;
890   }
891 src_map_failed:
892   {
893     GST_ELEMENT_ERROR (sink, RESOURCE, READ,
894         ("Video memory can not be read from userspace."), (NULL));
895     ret = GST_FLOW_ERROR;
896     goto done;
897   }
898 dst_map_failed:
899   {
900     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
901         ("Video memory can not be written from userspace."), (NULL));
902     ret = GST_FLOW_ERROR;
903     goto done;
904   }
905 done:
906   {
907     g_mutex_unlock (&sink->render_lock);
908     return ret;
909   }
910 }
911
912 static void
913 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
914 {
915   iface->set_window_handle = gst_wayland_sink_set_window_handle;
916   iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
917   iface->expose = gst_wayland_sink_expose;
918 }
919
920 static void
921 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
922 {
923   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
924   struct wl_surface *surface = (struct wl_surface *) handle;
925
926   g_return_if_fail (sink != NULL);
927
928   if (sink->window != NULL) {
929     GST_WARNING_OBJECT (sink, "changing window handle is not supported");
930     return;
931   }
932
933   g_mutex_lock (&sink->render_lock);
934
935   GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
936       (void *) handle);
937
938   g_clear_object (&sink->window);
939
940   if (handle) {
941     if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
942       /* we cannot use our own display with an external window handle */
943       if (G_UNLIKELY (sink->display->own_display)) {
944         GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
945             ("Application did not provide a wayland display handle"),
946             ("waylandsink cannot use an externally-supplied surface without "
947                 "an externally-supplied display handle. Consider providing a "
948                 "display handle from your application with GstContext"));
949       } else {
950         sink->window = gst_wl_window_new_in_surface (sink->display, surface,
951             &sink->render_lock);
952       }
953     } else {
954       GST_ERROR_OBJECT (sink, "Failed to find display handle, "
955           "ignoring window handle");
956     }
957   }
958
959   g_mutex_unlock (&sink->render_lock);
960 }
961
962 static void
963 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
964     gint x, gint y, gint w, gint h)
965 {
966   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
967
968   g_return_if_fail (sink != NULL);
969
970   g_mutex_lock (&sink->render_lock);
971   if (!sink->window) {
972     g_mutex_unlock (&sink->render_lock);
973     GST_WARNING_OBJECT (sink,
974         "set_render_rectangle called without window, ignoring");
975     return;
976   }
977
978   GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
979       x, y, w, h);
980   gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
981
982   g_mutex_unlock (&sink->render_lock);
983 }
984
985 static void
986 gst_wayland_sink_expose (GstVideoOverlay * overlay)
987 {
988   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
989
990   g_return_if_fail (sink != NULL);
991
992   GST_DEBUG_OBJECT (sink, "expose");
993
994   g_mutex_lock (&sink->render_lock);
995   if (sink->last_buffer && !sink->redraw_pending) {
996     GST_DEBUG_OBJECT (sink, "redrawing last buffer");
997     render_last_buffer (sink, TRUE);
998   }
999   g_mutex_unlock (&sink->render_lock);
1000 }
1001
1002 static void
1003 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
1004 {
1005   iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
1006   iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
1007 }
1008
1009 static void
1010 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
1011 {
1012   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1013   g_return_if_fail (sink != NULL);
1014
1015   g_mutex_lock (&sink->render_lock);
1016   if (!sink->window || !sink->window->area_subsurface) {
1017     g_mutex_unlock (&sink->render_lock);
1018     GST_INFO_OBJECT (sink,
1019         "begin_geometry_change called without window, ignoring");
1020     return;
1021   }
1022
1023   wl_subsurface_set_sync (sink->window->area_subsurface);
1024   g_mutex_unlock (&sink->render_lock);
1025 }
1026
1027 static void
1028 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
1029 {
1030   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1031   g_return_if_fail (sink != NULL);
1032
1033   g_mutex_lock (&sink->render_lock);
1034   if (!sink->window || !sink->window->area_subsurface) {
1035     g_mutex_unlock (&sink->render_lock);
1036     GST_INFO_OBJECT (sink,
1037         "end_geometry_change called without window, ignoring");
1038     return;
1039   }
1040
1041   wl_subsurface_set_desync (sink->window->area_subsurface);
1042   g_mutex_unlock (&sink->render_lock);
1043 }
1044
1045 static gboolean
1046 plugin_init (GstPlugin * plugin)
1047 {
1048   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
1049       " wayland video sink");
1050
1051   gst_wl_shm_allocator_register ();
1052
1053   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
1054       GST_TYPE_WAYLAND_SINK);
1055 }
1056
1057 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1058     GST_VERSION_MINOR,
1059     waylandsink,
1060     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1061     GST_PACKAGE_ORIGIN)