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