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