Waylandsink : Add rotate, display_geometry_method, orientation and flip property
[platform/upstream/gstreamer.git] / ext / wayland / gstwaylandsink.c
1 /* GStreamer Wayland video sink
2  *
3  * Copyright (C) 2011 Intel Corporation
4  * Copyright (C) 2011 Sreerenj Balachandran <sreerenj.balachandran@intel.com>
5  * Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.com>
6  * Copyright (C) 2014 Collabora Ltd.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * SECTION:element-waylandsink
26  *
27  *  The waylandsink is creating its own window and render the decoded video frames to that.
28  *  Setup the Wayland environment as described in
29  *  <ulink url="http://wayland.freedesktop.org/building.html">Wayland</ulink> home page.
30  *  The current implementaion is based on weston compositor.
31  *
32  * <refsect2>
33  * <title>Example pipelines</title>
34  * |[
35  * gst-launch -v videotestsrc ! waylandsink
36  * ]| test the video rendering in wayland
37  * </refsect2>
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include "gstwaylandsink.h"
45 #ifdef GST_WLSINK_ENHANCEMENT
46 #include <mm_types.h>
47 #include "tizen-wlvideoformat.h"
48 #else
49 #include "wlvideoformat.h"
50 #endif
51 #include "waylandpool.h"
52
53 #include <gst/wayland/wayland.h>
54 #include <gst/video/videooverlay.h>
55
56 #ifdef GST_WLSINK_ENHANCEMENT
57 #define GST_TYPE_WAYLANDSINK_DISPLAY_GEOMETRY_METHOD (gst_waylandsink_display_geometry_method_get_type())
58 #define GST_TYPE_WAYLANDSINK_ROTATE_ANGLE (gst_waylandsink_rotate_angle_get_type())
59 #define GST_TYPE_WAYLANDSINK_FLIP (gst_waylandsink_flip_get_type())
60
61 static GType
62 gst_waylandsink_rotate_angle_get_type (void)
63 {
64   static GType waylandsink_rotate_angle_type = 0;
65   static const GEnumValue rotate_angle_type[] = {
66     {0, "No rotate", "DEGREE_0"},
67     {1, "Rotate 90 degree", "DEGREE_90"},
68     {2, "Rotate 180 degree", "DEGREE_180"},
69     {3, "Rotate 270 degree", "DEGREE_270"},
70     {4, NULL, NULL},
71   };
72
73   if (!waylandsink_rotate_angle_type) {
74     waylandsink_rotate_angle_type =
75         g_enum_register_static ("GstWaylandSinkRotateAngleType",
76         rotate_angle_type);
77   }
78
79   return waylandsink_rotate_angle_type;
80 }
81
82
83 static GType
84 gst_waylandsink_display_geometry_method_get_type (void)
85 {
86   static GType waylandsink_display_geometry_method_type = 0;
87   static const GEnumValue display_geometry_method_type[] = {
88     {0, "Letter box", "LETTER_BOX"},
89     {1, "Origin size", "ORIGIN_SIZE"},
90     {2, "Full-screen", "FULL_SCREEN"},
91     {3, "Cropped full-screen", "CROPPED_FULL_SCREEN"},
92     {4, "Origin size(if screen size is larger than video size(width/height)) or Letter box(if video size(width/height) is larger than screen size)", "ORIGIN_SIZE_OR_LETTER_BOX"},
93     {5, NULL, NULL},
94   };
95
96   if (!waylandsink_display_geometry_method_type) {
97     waylandsink_display_geometry_method_type =
98         g_enum_register_static ("GstWaylandSinkDisplayGeometryMethodType",
99         display_geometry_method_type);
100   }
101   return waylandsink_display_geometry_method_type;
102 }
103
104 static GType
105 gst_waylandsink_flip_get_type (void)
106 {
107   static GType waylandsink_flip_type = 0;
108   static const GEnumValue flip_type[] = {
109     {FLIP_NONE, "Flip NONE", "FLIP_NONE"},
110     {FLIP_HORIZONTAL, "Flip HORIZONTAL", "FLIP_HORIZONTAL"},
111     {FLIP_VERTICAL, "Flip VERTICAL", "FLIP_VERTICAL"},
112     {FLIP_BOTH, "Flip BOTH", "FLIP_BOTH"},
113     {FLIP_NUM, NULL, NULL},
114   };
115
116   if (!waylandsink_flip_type) {
117     waylandsink_flip_type =
118         g_enum_register_static ("GstWaylandSinkFlipType", flip_type);
119   }
120
121   return waylandsink_flip_type;
122 }
123
124 #endif
125
126
127 /* signals */
128 enum
129 {
130   SIGNAL_0,
131   LAST_SIGNAL
132 };
133
134 /* Properties */
135 enum
136 {
137   PROP_0,
138   PROP_DISPLAY,
139 #ifdef GST_WLSINK_ENHANCEMENT
140   PROP_ROTATE_ANGLE,
141   PROP_DISPLAY_GEOMETRY_METHOD,
142   PROP_ORIENTATION,
143   PROP_FLIP
144 #endif
145 };
146
147 GST_DEBUG_CATEGORY (gstwayland_debug);
148 #define GST_CAT_DEFAULT gstwayland_debug
149
150 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
151     GST_PAD_SINK,
152     GST_PAD_ALWAYS,
153     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
154         ("{ BGRx, BGRA, RGBx, xBGR, xRGB, RGBA, ABGR, ARGB, RGB, BGR, "
155             "RGB16, BGR16, YUY2, YVYU, UYVY, AYUV, NV12, NV21, NV16, "
156 #ifdef GST_WLSINK_ENHANCEMENT
157             "SN12, ST12, "
158 #endif
159             "YUV9, YVU9, Y41B, I420, YV12, Y42B, v308 }"))
160     );
161
162 static void gst_wayland_sink_get_property (GObject * object,
163     guint prop_id, GValue * value, GParamSpec * pspec);
164 static void gst_wayland_sink_set_property (GObject * object,
165     guint prop_id, const GValue * value, GParamSpec * pspec);
166 static void gst_wayland_sink_finalize (GObject * object);
167
168 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
169     GstStateChange transition);
170 static void gst_wayland_sink_set_context (GstElement * element,
171     GstContext * context);
172
173 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
174     GstCaps * filter);
175 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
176 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
177     GstBuffer * buffer);
178 static gboolean
179 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
180 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
181     GstBuffer * buffer);
182
183 /* VideoOverlay interface */
184 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
185     iface);
186 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
187     guintptr handle);
188 static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
189     gint x, gint y, gint w, gint h);
190 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
191
192 /* WaylandVideo interface */
193 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
194     iface);
195 static void gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video);
196 static void gst_wayland_sink_end_geometry_change (GstWaylandVideo * video);
197 #ifdef GST_WLSINK_ENHANCEMENT
198 static void gst_wayland_sink_update_window_geometry (GstWaylandSink * sink);
199 #endif
200
201 #define gst_wayland_sink_parent_class parent_class
202 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
203     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
204         gst_wayland_sink_videooverlay_init)
205     G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
206         gst_wayland_sink_waylandvideo_init));
207
208 static void
209 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
210 {
211   FUNCTION_ENTER ();
212   GObjectClass *gobject_class;
213   GstElementClass *gstelement_class;
214   GstBaseSinkClass *gstbasesink_class;
215
216   gobject_class = (GObjectClass *) klass;
217   gstelement_class = (GstElementClass *) klass;
218   gstbasesink_class = (GstBaseSinkClass *) klass;
219
220   gobject_class->set_property = gst_wayland_sink_set_property;
221   gobject_class->get_property = gst_wayland_sink_get_property;
222   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
223
224   gst_element_class_add_pad_template (gstelement_class,
225       gst_static_pad_template_get (&sink_template));
226
227   gst_element_class_set_static_metadata (gstelement_class,
228       "wayland video sink", "Sink/Video",
229       "Output to wayland surface",
230       "Sreerenj Balachandran <sreerenj.balachandran@intel.com>, "
231       "George Kiagiadakis <george.kiagiadakis@collabora.com>");
232
233   gstelement_class->change_state =
234       GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
235   gstelement_class->set_context =
236       GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
237
238   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
239   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
240   gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
241   gstbasesink_class->propose_allocation =
242       GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
243   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
244
245   g_object_class_install_property (gobject_class, PROP_DISPLAY,
246       g_param_spec_string ("display", "Wayland Display name", "Wayland "
247           "display name to connect to, if not supplied via the GstContext",
248           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
249 #ifdef GST_WLSINK_ENHANCEMENT
250   g_object_class_install_property (gobject_class, PROP_ROTATE_ANGLE,
251       g_param_spec_enum ("rotate", "Rotate angle",
252           "Rotate angle of display output",
253           GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
254           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
255
256   g_object_class_install_property (gobject_class, PROP_DISPLAY_GEOMETRY_METHOD,
257       g_param_spec_enum ("display-geometry-method", "Display geometry method",
258           "Geometrical method for display",
259           GST_TYPE_WAYLANDSINK_DISPLAY_GEOMETRY_METHOD,
260           DEF_DISPLAY_GEOMETRY_METHOD,
261           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262
263   g_object_class_install_property (gobject_class, PROP_ORIENTATION,
264       g_param_spec_enum ("orientation",
265           "Orientation information used for ROI/ZOOM",
266           "Orientation information for display",
267           GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
268           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
269
270   g_object_class_install_property (gobject_class, PROP_FLIP,
271       g_param_spec_enum ("flip", "Display flip",
272           "Flip for display",
273           GST_TYPE_WAYLANDSINK_FLIP, DEF_DISPLAY_FLIP,
274           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275
276 #endif
277 }
278
279 static void
280 gst_wayland_sink_init (GstWaylandSink * sink)
281 {
282   FUNCTION_ENTER ();
283
284   sink->display_geometry_method = DEF_DISPLAY_GEOMETRY_METHOD;
285   sink->flip = DEF_DISPLAY_FLIP;
286   sink->rotate_angle = DEGREE_0;
287   sink->orientation = DEGREE_0;
288
289   g_mutex_init (&sink->display_lock);
290   g_mutex_init (&sink->render_lock);
291 }
292
293 static void
294 gst_wayland_sink_get_property (GObject * object,
295     guint prop_id, GValue * value, GParamSpec * pspec)
296 {
297   FUNCTION_ENTER ();
298
299   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
300   switch (prop_id) {
301     case PROP_DISPLAY:
302       GST_OBJECT_LOCK (sink);
303       g_value_set_string (value, sink->display_name);
304       GST_OBJECT_UNLOCK (sink);
305       break;
306 #ifdef GST_WLSINK_ENHANCEMENT
307     case PROP_ROTATE_ANGLE:
308       g_value_set_enum (value, sink->rotate_angle);
309       break;
310     case PROP_DISPLAY_GEOMETRY_METHOD:
311       g_value_set_enum (value, sink->display_geometry_method);
312       break;
313     case PROP_ORIENTATION:
314       g_value_set_enum (value, sink->orientation);
315       break;
316     case PROP_FLIP:
317       g_value_set_enum (value, sink->flip);
318       break;
319 #endif
320     default:
321       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
322       break;
323   }
324 }
325
326 static void
327 gst_wayland_sink_set_property (GObject * object,
328     guint prop_id, const GValue * value, GParamSpec * pspec)
329 {
330   FUNCTION_ENTER ();
331
332   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
333   switch (prop_id) {
334     case PROP_DISPLAY:
335       GST_OBJECT_LOCK (sink);
336       sink->display_name = g_value_dup_string (value);
337       GST_OBJECT_UNLOCK (sink);
338       break;
339 #ifdef GST_WLSINK_ENHANCEMENT
340     case PROP_ROTATE_ANGLE:
341       sink->rotate_angle = g_value_get_enum (value);
342       GST_INFO_OBJECT (sink, "Rotate angle is set (%d)", sink->rotate_angle);
343       if (sink->window) {
344         gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
345       }
346       sink->video_info_changed = TRUE;
347       if (GST_STATE (sink) == GST_STATE_PAUSED) {
348         /*need to render current buffer*/
349       }
350       break;
351     case PROP_DISPLAY_GEOMETRY_METHOD:
352       sink->display_geometry_method = g_value_get_enum (value);
353       GST_INFO_OBJECT (sink, "Display geometry method is set (%d)",
354           sink->display_geometry_method);
355       if (sink->window) {
356         gst_wl_window_set_disp_geo_method (sink->window, sink->display_geometry_method);
357       }
358       sink->video_info_changed = TRUE;
359       if (GST_STATE (sink) == GST_STATE_PAUSED) {
360         /*need to render current buffer*/
361       }
362       break;
363     case PROP_ORIENTATION:
364       sink->orientation = g_value_get_enum (value);
365       GST_INFO_OBJECT (sink, "Orientation is set (%d)", sink->orientation);
366       if (sink->window) {
367         gst_wl_window_set_orientation (sink->window, sink->orientation);
368       }
369       sink->video_info_changed = TRUE;
370       if (GST_STATE (sink) == GST_STATE_PAUSED) {
371         /*need to render current buffer*/
372       }
373       break;
374     case PROP_FLIP:
375       sink->flip = g_value_get_enum (value);
376       GST_INFO_OBJECT (sink, "flip is set (%d)", sink->flip);
377       if (sink->flip) {
378         gst_wl_window_set_flip (sink->window, sink->flip);
379       }
380       sink->video_info_changed = TRUE;
381       if (GST_STATE (sink) == GST_STATE_PAUSED) {
382         /*need to render current buffer*/
383       }
384       break;
385 #endif
386     default:
387       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
388       break;
389   }
390 }
391
392 static void
393 gst_wayland_sink_finalize (GObject * object)
394 {
395   FUNCTION_ENTER ();
396
397   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
398   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
399
400   if (sink->last_buffer)
401     gst_buffer_unref (sink->last_buffer);
402   if (sink->display) {
403     /* see comment about this call in gst_wayland_sink_change_state() */
404 #ifdef GST_WLSINK_ENHANCEMENT
405     if (sink->pool && !sink->display->is_native_format)
406 #else
407     if (sink->pool)
408 #endif
409       gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
410           (sink->pool));
411
412     g_object_unref (sink->display);
413     sink->display = NULL;
414   }
415   if (sink->window) {
416     g_object_unref (sink->window);
417     sink->window = NULL;
418   }
419   if (sink->pool) {
420     gst_object_unref (sink->pool);
421     sink->pool = NULL;
422   }
423
424   if (sink->display_name) {
425     g_free (sink->display_name);
426     sink->display_name = NULL;
427   }
428
429   g_mutex_clear (&sink->display_lock);
430   g_mutex_clear (&sink->render_lock);
431
432   G_OBJECT_CLASS (parent_class)->finalize (object);
433 }
434
435 /* must be called with the display_lock */
436 static void
437 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
438     GstContext * context)
439 {
440   FUNCTION_ENTER ();
441
442   struct wl_display *display;
443   GError *error = NULL;
444
445   display = gst_wayland_display_handle_context_get_handle (context);
446   sink->display = gst_wl_display_new_existing (display, FALSE, &error);
447
448   if (error) {
449     GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
450         ("Could not set display handle"),
451         ("Failed to use the external wayland display: '%s'", error->message));
452     g_error_free (error);
453   }
454 }
455
456 static gboolean
457 gst_wayland_sink_find_display (GstWaylandSink * sink)
458 {
459   FUNCTION_ENTER ();
460
461   GstQuery *query;
462   GstMessage *msg;
463   GstContext *context = NULL;
464   GError *error = NULL;
465   gboolean ret = TRUE;
466
467   g_mutex_lock (&sink->display_lock);
468
469   if (!sink->display) {
470     /* first query upstream for the needed display handle */
471     query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
472     if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
473       gst_query_parse_context (query, &context);
474       gst_wayland_sink_set_display_from_context (sink, context);
475     }
476     gst_query_unref (query);
477
478     if (G_LIKELY (!sink->display)) {
479       /* now ask the application to set the display handle */
480       msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
481           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
482
483       g_mutex_unlock (&sink->display_lock);
484       gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
485       /* at this point we expect gst_wayland_sink_set_context
486        * to get called and fill sink->display */
487       g_mutex_lock (&sink->display_lock);
488
489       if (!sink->display) {
490         /* if the application didn't set a display, let's create it ourselves */
491         GST_OBJECT_LOCK (sink);
492         sink->display = gst_wl_display_new (sink->display_name, &error);
493         GST_OBJECT_UNLOCK (sink);
494
495         if (error) {
496           GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
497               ("Could not initialise Wayland output"),
498               ("Failed to create GstWlDisplay: '%s'", error->message));
499           g_error_free (error);
500           ret = FALSE;
501         } else {
502           /* inform the world about the new display */
503           context =
504               gst_wayland_display_handle_context_new (sink->display->display);
505           msg = gst_message_new_have_context (GST_OBJECT_CAST (sink), context);
506           gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
507         }
508       }
509     }
510   }
511
512   g_mutex_unlock (&sink->display_lock);
513
514   return ret;
515 }
516
517 static GstStateChangeReturn
518 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
519 {
520   FUNCTION_ENTER ();
521
522   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
523   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
524
525   switch (transition) {
526     case GST_STATE_CHANGE_NULL_TO_READY:
527       if (!gst_wayland_sink_find_display (sink))
528         return GST_STATE_CHANGE_FAILURE;
529       break;
530     default:
531       break;
532   }
533
534   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
535   if (ret == GST_STATE_CHANGE_FAILURE)
536     return ret;
537
538   switch (transition) {
539     case GST_STATE_CHANGE_PAUSED_TO_READY:
540       gst_buffer_replace (&sink->last_buffer, NULL);
541       if (sink->window) {
542         if (gst_wl_window_is_toplevel (sink->window)) {
543           g_clear_object (&sink->window);
544         } else {
545           /* remove buffer from surface, show nothing */
546           wl_surface_attach (sink->window->surface, NULL, 0, 0);
547           wl_surface_damage (sink->window->surface, 0, 0,
548               sink->window->surface_width, sink->window->surface_height);
549           wl_surface_commit (sink->window->surface);
550           wl_display_flush (sink->display->display);
551         }
552       }
553       break;
554     case GST_STATE_CHANGE_READY_TO_NULL:
555       g_mutex_lock (&sink->display_lock);
556       /* If we had a toplevel window, we most likely have our own connection
557        * to the display too, and it is a good idea to disconnect and allow
558        * potentially the application to embed us with GstVideoOverlay
559        * (which requires to re-use the same display connection as the parent
560        * surface). If we didn't have a toplevel window, then the display
561        * connection that we have is definitely shared with the application
562        * and it's better to keep it around (together with the window handle)
563        * to avoid requesting them again from the application if/when we are
564        * restarted (GstVideoOverlay behaves like that in other sinks)
565        */
566       if (sink->display && !sink->window) {     /* -> the window was toplevel */
567         /* Force all buffers to return to the pool, regardless of
568          * whether the compositor has released them or not. We are
569          * going to kill the display, so we need to return all buffers
570          * to be destroyed before this happens.
571          * Note that this is done here instead of the pool destructor
572          * because the buffers hold a reference to the pool. Also,
573          * the buffers can only be unref'ed from the display's event loop
574          * and the pool holds a reference to the display. If we drop
575          * our references here, when the compositor releases the buffers,
576          * they will be unref'ed from the event loop thread, which will
577          * unref the pool and therefore the display, which will try to
578          * stop the thread from within itself and cause a deadlock.
579          */
580         if (sink->pool) {
581           gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
582               (sink->pool));
583         }
584         g_clear_object (&sink->display);
585         g_clear_object (&sink->pool);
586       }
587       g_mutex_unlock (&sink->display_lock);
588       break;
589     default:
590       break;
591   }
592
593   return ret;
594 }
595
596 static void
597 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
598 {
599   FUNCTION_ENTER ();
600
601   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
602   if (gst_context_has_context_type (context,
603           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
604     g_mutex_lock (&sink->display_lock);
605     if (G_LIKELY (!sink->display)) {
606       gst_wayland_sink_set_display_from_context (sink, context);
607     } else {
608       GST_WARNING_OBJECT (element, "changing display handle is not supported");
609       g_mutex_unlock (&sink->display_lock);
610       return;
611     }
612     g_mutex_unlock (&sink->display_lock);
613   }
614
615   GST_INFO ("element %p context %p", element, context);
616   if (GST_ELEMENT_CLASS (parent_class)->set_context)
617     GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
618 }
619
620 static GstCaps *
621 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
622 {
623   FUNCTION_ENTER ();
624
625   GstWaylandSink *sink;
626   GstCaps *caps;
627   sink = GST_WAYLAND_SINK (bsink);
628
629   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
630
631   g_mutex_lock (&sink->display_lock);
632
633   if (sink->display) {
634     GValue list = G_VALUE_INIT;
635     GValue value = G_VALUE_INIT;
636     GArray *formats;
637     gint i;
638 #ifdef GST_WLSINK_ENHANCEMENT
639     enum tizen_buffer_pool_format fmt;
640 #else
641     enum wl_shm_format fmt;
642 #endif
643
644     g_value_init (&list, GST_TYPE_LIST);
645     g_value_init (&value, G_TYPE_STRING);
646
647     formats = sink->display->formats;
648     for (i = 0; i < formats->len; i++) {
649       fmt = g_array_index (formats, uint32_t, i);
650       g_value_set_string (&value, gst_wayland_format_to_string (fmt));
651       gst_value_list_append_value (&list, &value);
652     }
653
654     caps = gst_caps_make_writable (caps);
655     gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
656
657     GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
658   }
659
660   g_mutex_unlock (&sink->display_lock);
661
662   if (filter) {
663     GstCaps *intersection;
664
665     intersection =
666         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
667     gst_caps_unref (caps);
668     caps = intersection;
669   }
670
671   return caps;
672 }
673
674 static gboolean
675 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
676 {
677   FUNCTION_ENTER ();
678
679   GstWaylandSink *sink;
680   GstBufferPool *newpool;
681   GstVideoInfo info;
682 #ifdef GST_WLSINK_ENHANCEMENT
683   enum tizen_buffer_pool_format format;
684 #else
685   enum wl_shm_format format;
686 #endif
687   GArray *formats;
688   gint i;
689   GstStructure *structure;
690   static GstAllocationParams params = { 0, 0, 0, 15, };
691   sink = GST_WAYLAND_SINK (bsink);
692
693   GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
694
695   /* extract info from caps */
696   if (!gst_video_info_from_caps (&info, caps))
697     goto invalid_format;
698 #ifdef GST_WLSINK_ENHANCEMENT
699   sink->caps = gst_caps_copy (caps);
700 #endif
701
702   format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
703   if ((gint) format == -1)
704     goto invalid_format;
705
706   /* verify we support the requested format */
707   formats = sink->display->formats;
708   for (i = 0; i < formats->len; i++) {
709     if (g_array_index (formats, uint32_t, i) == format)
710       break;
711   }
712
713   if (i >= formats->len)
714     goto unsupported_format;
715
716 #ifdef GST_WLSINK_ENHANCEMENT
717   if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_SN12 ||
718       GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ST12) {
719     sink->display->is_native_format = TRUE;
720   } else {
721     sink->display->is_native_format = FALSE;
722
723     /* create a new pool for the new configuration */
724     newpool = gst_wayland_buffer_pool_new (sink->display);
725     if (!newpool)
726       goto pool_failed;
727
728     structure = gst_buffer_pool_get_config (newpool);
729     gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
730     gst_buffer_pool_config_set_allocator (structure, NULL, &params);
731     if (!gst_buffer_pool_set_config (newpool, structure))
732       goto config_failed;
733
734     gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
735     gst_object_unref (newpool);
736
737   }
738   /* store the video info */
739   sink->video_info = info;
740   sink->video_info_changed = TRUE;
741 #else
742   /* create a new pool for the new configuration */
743   newpool = gst_wayland_buffer_pool_new (sink->display);
744   if (!newpool)
745     goto pool_failed;
746
747   structure = gst_buffer_pool_get_config (newpool);
748   gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
749   gst_buffer_pool_config_set_allocator (structure, NULL, &params);
750   if (!gst_buffer_pool_set_config (newpool, structure))
751     goto config_failed;
752
753   /* store the video info */
754   sink->video_info = info;
755   sink->video_info_changed = TRUE;
756
757   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
758   gst_object_unref (newpool);
759 #endif
760   return TRUE;
761
762 invalid_format:
763   {
764     GST_DEBUG_OBJECT (sink,
765         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
766     return FALSE;
767   }
768 unsupported_format:
769   {
770     GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
771         gst_wayland_format_to_string (format));
772     return FALSE;
773   }
774 pool_failed:
775   {
776     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
777     return FALSE;
778   }
779 config_failed:
780   {
781     GST_DEBUG_OBJECT (bsink, "failed setting config");
782     gst_object_unref (newpool);
783     return FALSE;
784   }
785 }
786
787 static gboolean
788 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
789 {
790   FUNCTION_ENTER ();
791
792   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
793   GstBufferPool *pool = NULL;
794   GstStructure *config;
795   GstCaps *caps;
796   guint size;
797   gboolean need_pool;
798
799   if (sink->display->is_native_format == TRUE)
800     return TRUE;
801
802   gst_query_parse_allocation (query, &caps, &need_pool);
803
804   if (caps == NULL)
805     goto no_caps;
806
807   if (sink->pool)
808     pool = gst_object_ref (sink->pool);
809
810   if (pool != NULL) {
811     GstCaps *pcaps;
812
813     /* we had a pool, check caps */
814     config = gst_buffer_pool_get_config (pool);
815     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
816
817     if (!gst_caps_is_equal (caps, pcaps)) {
818       /* different caps, we can't use this pool */
819       gst_object_unref (pool);
820       pool = NULL;
821     }
822     gst_structure_free (config);
823   }
824
825   if (pool == NULL && need_pool) {
826     GstVideoInfo info;
827
828     if (!gst_video_info_from_caps (&info, caps))
829       goto invalid_caps;
830
831     GST_DEBUG_OBJECT (sink, "create new pool");
832     pool = gst_wayland_buffer_pool_new (sink->display);
833
834     /* the normal size of a frame */
835     size = info.size;
836
837     config = gst_buffer_pool_get_config (pool);
838     gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
839     if (!gst_buffer_pool_set_config (pool, config))
840       goto config_failed;
841   }
842   if (pool) {
843     gst_query_add_allocation_pool (query, pool, size, 2, 0);
844     gst_object_unref (pool);
845   }
846
847   return TRUE;
848
849   /* ERRORS */
850 no_caps:
851   {
852     GST_DEBUG_OBJECT (bsink, "no caps specified");
853     return FALSE;
854   }
855 invalid_caps:
856   {
857     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
858     return FALSE;
859   }
860 config_failed:
861   {
862     GST_DEBUG_OBJECT (bsink, "failed setting config");
863     gst_object_unref (pool);
864     return FALSE;
865   }
866 }
867
868 static GstFlowReturn
869 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
870 {
871   FUNCTION_ENTER ();
872
873   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
874   return gst_wayland_sink_render (bsink, buffer);
875 }
876
877 static void
878 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
879 {
880   FUNCTION_ENTER ();
881
882   GstWaylandSink *sink = data;
883
884   GST_LOG ("frame_redraw_cb");
885
886   g_atomic_int_set (&sink->redraw_pending, FALSE);
887   wl_callback_destroy (callback);
888 }
889
890 static const struct wl_callback_listener frame_callback_listener = {
891   frame_redraw_callback
892 };
893
894 /* must be called with the render lock */
895 static void
896 render_last_buffer (GstWaylandSink * sink)
897 {
898   FUNCTION_ENTER ();
899
900   GstWlMeta *meta;
901   struct wl_surface *surface;
902   struct wl_callback *callback;
903
904   meta = gst_buffer_get_wl_meta (sink->last_buffer);
905   surface = gst_wl_window_get_wl_surface (sink->window);
906
907   g_atomic_int_set (&sink->redraw_pending, TRUE);
908   callback = wl_surface_frame (surface);
909   wl_callback_add_listener (callback, &frame_callback_listener, sink);
910
911   /* Here we essentially add a reference to the buffer. This represents
912    * the fact that the compositor is using the buffer and it should
913    * not return back to the pool and be reused until the compositor
914    * releases it. The release is handled internally in the pool */
915   gst_wayland_compositor_acquire_buffer (meta->pool, sink->last_buffer);
916
917   GST_DEBUG ("wl_surface_attach wl_buffer %p", meta->wbuffer);
918
919   wl_surface_attach (surface, meta->wbuffer, 0, 0);
920   wl_surface_damage (surface, 0, 0, sink->window->surface_width,
921       sink->window->surface_height);
922
923   wl_surface_commit (surface);
924   wl_display_flush (sink->display->display);
925 }
926
927 static GstFlowReturn
928 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
929 {
930   FUNCTION_ENTER ();
931
932   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
933   GstBuffer *to_render;
934   GstWlMeta *meta;
935   GstFlowReturn ret = GST_FLOW_OK;
936
937 #ifdef GST_WLSINK_ENHANCEMENT
938   GstBufferPool *newpool;
939   GstStructure *structure;
940   static GstAllocationParams params = { 0, 0, 0, 15, };
941 #endif
942
943   g_mutex_lock (&sink->render_lock);
944
945   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
946
947   if (G_UNLIKELY (!sink->window)) {
948     /* ask for window handle. Unlock render_lock while doing that because
949      * set_window_handle & friends will lock it in this context */
950     g_mutex_unlock (&sink->render_lock);
951     gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
952     g_mutex_lock (&sink->render_lock);
953
954     if (sink->window) {
955       /* inform the window about our caps */
956       gst_wl_window_set_video_info (sink->window, &sink->video_info);
957     } else {
958       /* if we were not provided a window, create one ourselves */
959       sink->window =
960           gst_wl_window_new_toplevel (sink->display, &sink->video_info);
961     }
962 #ifdef GST_WLSINK_ENHANCEMENT
963     gst_wayland_sink_update_window_geometry (sink);
964     sink->video_info_changed = TRUE;
965 #else
966     sink->video_info_changed = FALSE;
967 #endif
968   }
969
970   /* drop buffers until we get a frame callback */
971   if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
972     goto done;
973
974   if (G_UNLIKELY (sink->video_info_changed)) {
975     gst_wl_window_set_video_info (sink->window, &sink->video_info);
976     sink->video_info_changed = FALSE;
977
978   }
979   GST_INFO ("window->render_rectangle(%d,%d %d x %d)",
980       sink->window->render_rectangle.x,
981       sink->window->render_rectangle.y,
982       sink->window->render_rectangle.w, sink->window->render_rectangle.h);
983   GST_INFO ("window->surface_width(%d),window->surface_height(%d)",
984       sink->window->surface_width, sink->window->surface_height);
985
986   /* now that we have for sure set the video info on the window, it must have
987    * a valid size, otherwise this means that the application has called
988    * set_window_handle() without calling set_render_rectangle(), which is
989    * absolutely necessary for us.
990    */
991   if (G_UNLIKELY (sink->window->surface_width == 0 ||
992           sink->window->surface_height == 0))
993     goto no_window_size;
994
995   meta = gst_buffer_get_wl_meta (buffer);
996
997   if (meta && meta->pool->display == sink->display) {
998     GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
999     to_render = buffer;
1000   } else {
1001     GstMapInfo src;
1002     GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
1003
1004 #ifdef GST_WLSINK_ENHANCEMENT
1005     if (sink->display->is_native_format == TRUE) {
1006       /*in case of SN12 or ST12 video  format */
1007       GstMemory *mem;
1008       GstMapInfo mem_info = GST_MAP_INFO_INIT;
1009       MMVideoBuffer *mm_video_buf = NULL;
1010
1011       mem = gst_buffer_peek_memory (buffer, 1);
1012       gst_memory_map (mem, &mem_info, GST_MAP_READ);
1013       mm_video_buf = (MMVideoBuffer *) mem_info.data;
1014       gst_memory_unmap (mem, &mem_info);
1015
1016       if (mm_video_buf == NULL) {
1017         GST_WARNING_OBJECT (sink, "mm_video_buf is NULL. Skip rendering");
1018         return ret;
1019       }
1020       /* assign mm_video_buf info */
1021       if (mm_video_buf->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
1022         GST_DEBUG_OBJECT (sink, "TBM bo %p %p %p", mm_video_buf->handle.bo[0],
1023             mm_video_buf->handle.bo[1], mm_video_buf->handle.bo[2]);
1024
1025         sink->display->native_video_size = 0;
1026
1027         for (int i = 0; i < NV_BUF_PLANE_NUM; i++) {
1028           if (mm_video_buf->handle.bo[i] != NULL) {
1029             sink->display->bo[i] = mm_video_buf->handle.bo[i];
1030           } else {
1031             sink->display->bo[i] = 0;
1032           }
1033           sink->display->plane_size[i] = mm_video_buf->size[i];
1034           sink->display->stride_width[i] = mm_video_buf->stride_width[i];
1035           sink->display->stride_height[i] = mm_video_buf->stride_height[i];
1036           sink->display->native_video_size += sink->display->plane_size[i];
1037         }
1038       } else {
1039         GST_ERROR_OBJECT (sink, "Buffer type is not TBM");
1040         return ret;
1041       }
1042
1043       if (!sink->pool) {
1044
1045         /* create a new pool for the new configuration */
1046         newpool = gst_wayland_buffer_pool_new (sink->display);
1047         if (!newpool) {
1048           GST_DEBUG_OBJECT (sink, "Failed to create new pool");
1049           return FALSE;
1050         }
1051         structure = gst_buffer_pool_get_config (newpool);
1052         gst_buffer_pool_config_set_params (structure, sink->caps,
1053             sink->video_info.size, 2, 0);
1054         gst_buffer_pool_config_set_allocator (structure, NULL, &params);
1055         if (!gst_buffer_pool_set_config (newpool, structure)) {
1056           GST_DEBUG_OBJECT (bsink, "failed setting config");
1057           gst_object_unref (newpool);
1058           return FALSE;
1059         }
1060
1061         gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1062         gst_object_unref (newpool);
1063
1064       }
1065
1066       if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1067         goto activate_failed;
1068
1069       ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1070       if (ret != GST_FLOW_OK)
1071         goto no_buffer;
1072
1073     } else {
1074       /*in case of normal video format and pool is not our pool */
1075
1076       if (!sink->pool)
1077         goto no_pool;
1078
1079       if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1080         goto activate_failed;
1081
1082       ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1083       if (ret != GST_FLOW_OK)
1084         goto no_buffer;
1085
1086       gst_buffer_map (buffer, &src, GST_MAP_READ);
1087       gst_buffer_fill (to_render, 0, src.data, src.size);
1088       gst_buffer_unmap (buffer, &src);
1089     }
1090
1091 #else
1092     if (!sink->pool)
1093       goto no_pool;
1094
1095     if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1096       goto activate_failed;
1097
1098     ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1099     if (ret != GST_FLOW_OK)
1100       goto no_buffer;
1101
1102     gst_buffer_map (buffer, &src, GST_MAP_READ);
1103     gst_buffer_fill (to_render, 0, src.data, src.size);
1104     gst_buffer_unmap (buffer, &src);
1105 #endif
1106   }
1107
1108   gst_buffer_replace (&sink->last_buffer, to_render);
1109   render_last_buffer (sink);
1110
1111   if (buffer != to_render) {
1112     GST_LOG_OBJECT (sink, "Decrease ref count of buffer");
1113     gst_buffer_unref (to_render);
1114   }
1115   goto done;
1116
1117 no_window_size:
1118   {
1119     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
1120         ("Window has no size set"),
1121         ("Make sure you set the size after calling set_window_handle"));
1122     ret = GST_FLOW_ERROR;
1123     goto done;
1124   }
1125 no_buffer:
1126   {
1127     GST_WARNING_OBJECT (sink, "could not create image");
1128     goto done;
1129   }
1130 no_pool:
1131   {
1132     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
1133         ("Internal error: can't allocate images"),
1134         ("We don't have a bufferpool negotiated"));
1135     ret = GST_FLOW_ERROR;
1136     goto done;
1137   }
1138 activate_failed:
1139   {
1140     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
1141     ret = GST_FLOW_ERROR;
1142     goto done;
1143   }
1144 done:
1145   {
1146     g_mutex_unlock (&sink->render_lock);
1147     return ret;
1148   }
1149 }
1150
1151 static void
1152 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
1153 {
1154   FUNCTION_ENTER ();
1155
1156   iface->set_window_handle = gst_wayland_sink_set_window_handle;
1157   iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
1158   iface->expose = gst_wayland_sink_expose;
1159 }
1160
1161 static void
1162 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
1163 {
1164   FUNCTION_ENTER ();
1165
1166   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1167   struct wl_surface *surface = (struct wl_surface *) handle;
1168
1169   g_return_if_fail (sink != NULL);
1170
1171   if (sink->window != NULL) {
1172     GST_WARNING_OBJECT (sink, "changing window handle is not supported");
1173     return;
1174   }
1175
1176   g_mutex_lock (&sink->render_lock);
1177
1178   GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
1179       (void *) handle);
1180
1181   g_clear_object (&sink->window);
1182
1183   if (handle) {
1184     if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
1185       /* we cannot use our own display with an external window handle */
1186       if (G_UNLIKELY (sink->display->own_display)) {
1187         GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
1188             ("Application did not provide a wayland display handle"),
1189             ("waylandsink cannot use an externally-supplied surface without "
1190                 "an externally-supplied display handle. Consider providing a "
1191                 "display handle from your application with GstContext"));
1192       } else {
1193         sink->window = gst_wl_window_new_in_surface (sink->display, surface);
1194         GST_DEBUG ("sink->window %p", sink->window);
1195       }
1196     } else {
1197       GST_ERROR_OBJECT (sink, "Failed to find display handle, "
1198           "ignoring window handle");
1199     }
1200   }
1201 #ifdef GST_WLSINK_ENHANCEMENT
1202   gst_wayland_sink_update_window_geometry (sink);
1203 #endif
1204
1205   g_mutex_unlock (&sink->render_lock);
1206 }
1207
1208 #ifdef GST_WLSINK_ENHANCEMENT
1209 static void
1210 gst_wayland_sink_update_window_geometry (GstWaylandSink * sink)
1211 {
1212   FUNCTION_ENTER ();
1213
1214   if (sink == NULL || sink->window == NULL)
1215     return;
1216
1217   gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
1218   gst_wl_window_set_disp_geo_method (sink->window, sink->display_geometry_method);
1219   gst_wl_window_set_orientation (sink->window, sink->orientation);
1220   gst_wl_window_set_flip (sink->window, sink->flip);
1221 }
1222 #endif
1223 static void
1224 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
1225     gint x, gint y, gint w, gint h)
1226 {
1227   FUNCTION_ENTER ();
1228
1229   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1230
1231   g_return_if_fail (sink != NULL);
1232
1233   g_mutex_lock (&sink->render_lock);
1234   if (!sink->window) {
1235     g_mutex_unlock (&sink->render_lock);
1236     GST_WARNING_OBJECT (sink,
1237         "set_render_rectangle called without window, ignoring");
1238     return;
1239   }
1240
1241   GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
1242       x, y, w, h);
1243   gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
1244
1245   g_mutex_unlock (&sink->render_lock);
1246 }
1247
1248 static void
1249 gst_wayland_sink_expose (GstVideoOverlay * overlay)
1250 {
1251   FUNCTION_ENTER ();
1252
1253   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1254
1255   g_return_if_fail (sink != NULL);
1256
1257   GST_DEBUG_OBJECT (sink, "expose");
1258
1259   g_mutex_lock (&sink->render_lock);
1260   if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
1261     GST_DEBUG_OBJECT (sink, "redrawing last buffer");
1262     render_last_buffer (sink);
1263   }
1264   g_mutex_unlock (&sink->render_lock);
1265 }
1266
1267 static void
1268 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
1269 {
1270   FUNCTION_ENTER ();
1271
1272   iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
1273   iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
1274 }
1275
1276 static void
1277 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
1278 {
1279   FUNCTION_ENTER ();
1280
1281   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1282   g_return_if_fail (sink != NULL);
1283
1284   g_mutex_lock (&sink->render_lock);
1285   if (!sink->window || !sink->window->subsurface) {
1286     g_mutex_unlock (&sink->render_lock);
1287     GST_INFO_OBJECT (sink,
1288         "begin_geometry_change called without window, ignoring");
1289     return;
1290   }
1291
1292   wl_subsurface_set_sync (sink->window->subsurface);
1293   g_mutex_unlock (&sink->render_lock);
1294 }
1295
1296 static void
1297 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
1298 {
1299   FUNCTION_ENTER ();
1300
1301   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1302   g_return_if_fail (sink != NULL);
1303
1304   g_mutex_lock (&sink->render_lock);
1305   if (!sink->window || !sink->window->subsurface) {
1306     g_mutex_unlock (&sink->render_lock);
1307     GST_INFO_OBJECT (sink,
1308         "end_geometry_change called without window, ignoring");
1309     return;
1310   }
1311
1312   wl_subsurface_set_desync (sink->window->subsurface);
1313   g_mutex_unlock (&sink->render_lock);
1314 }
1315
1316 static gboolean
1317 plugin_init (GstPlugin * plugin)
1318 {
1319   FUNCTION_ENTER ();
1320
1321   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
1322       " wayland video sink");
1323
1324   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
1325       GST_TYPE_WAYLAND_SINK);
1326 }
1327
1328 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1329     GST_VERSION_MINOR,
1330     waylandsink,
1331     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1332     GST_PACKAGE_ORIGIN)