waylandsink : 1. refactoring code about buffer ref management.
[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 #endif
49 #include "wlvideoformat.h"
50 #include "wlbuffer.h"
51 #include "wlshmallocator.h"
52
53 #include <gst/wayland/wayland.h>
54 #include <gst/video/videooverlay.h>
55
56
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60
61 #ifdef GST_WLSINK_ENHANCEMENT
62 #define GST_TYPE_WAYLANDSINK_DISPLAY_GEOMETRY_METHOD (gst_waylandsink_display_geometry_method_get_type())
63 #define GST_TYPE_WAYLANDSINK_ROTATE_ANGLE (gst_waylandsink_rotate_angle_get_type())
64 #define GST_TYPE_WAYLANDSINK_FLIP (gst_waylandsink_flip_get_type())
65
66 static GType
67 gst_waylandsink_rotate_angle_get_type (void)
68 {
69   static GType waylandsink_rotate_angle_type = 0;
70   static const GEnumValue rotate_angle_type[] = {
71     {0, "No rotate", "DEGREE_0"},
72     {1, "Rotate 90 degree", "DEGREE_90"},
73     {2, "Rotate 180 degree", "DEGREE_180"},
74     {3, "Rotate 270 degree", "DEGREE_270"},
75     {4, NULL, NULL},
76   };
77
78   if (!waylandsink_rotate_angle_type) {
79     waylandsink_rotate_angle_type =
80         g_enum_register_static ("GstWaylandSinkRotateAngleType",
81         rotate_angle_type);
82   }
83
84   return waylandsink_rotate_angle_type;
85 }
86
87
88 static GType
89 gst_waylandsink_display_geometry_method_get_type (void)
90 {
91   static GType waylandsink_display_geometry_method_type = 0;
92   static const GEnumValue display_geometry_method_type[] = {
93     {0, "Letter box", "LETTER_BOX"},
94     {1, "Origin size", "ORIGIN_SIZE"},
95     {2, "Full-screen", "FULL_SCREEN"},
96     {3, "Cropped full-screen", "CROPPED_FULL_SCREEN"},
97     {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"},
98     {5, NULL, NULL},
99   };
100
101   if (!waylandsink_display_geometry_method_type) {
102     waylandsink_display_geometry_method_type =
103         g_enum_register_static ("GstWaylandSinkDisplayGeometryMethodType",
104         display_geometry_method_type);
105   }
106   return waylandsink_display_geometry_method_type;
107 }
108
109 static GType
110 gst_waylandsink_flip_get_type (void)
111 {
112   static GType waylandsink_flip_type = 0;
113   static const GEnumValue flip_type[] = {
114     {FLIP_NONE, "Flip NONE", "FLIP_NONE"},
115     {FLIP_HORIZONTAL, "Flip HORIZONTAL", "FLIP_HORIZONTAL"},
116     {FLIP_VERTICAL, "Flip VERTICAL", "FLIP_VERTICAL"},
117     {FLIP_BOTH, "Flip BOTH", "FLIP_BOTH"},
118     {FLIP_NUM, NULL, NULL},
119   };
120
121   if (!waylandsink_flip_type) {
122     waylandsink_flip_type =
123         g_enum_register_static ("GstWaylandSinkFlipType", flip_type);
124   }
125
126   return waylandsink_flip_type;
127 }
128
129 #endif
130
131 /* signals */
132 enum
133 {
134   SIGNAL_0,
135   LAST_SIGNAL
136 };
137
138 /* Properties */
139 enum
140 {
141   PROP_0,
142   PROP_DISPLAY,
143 #ifdef GST_WLSINK_ENHANCEMENT
144   PROP_USE_GAPLESS,
145   PROP_KEEP_CAMERA_PREVIEW,
146   PROP_USE_TBM,
147   PROP_ROTATE_ANGLE,
148   PROP_DISPLAY_GEOMETRY_METHOD,
149   PROP_ORIENTATION,
150   PROP_FLIP,
151   PROP_VISIBLE
152 #endif
153 };
154 int dump__cnt = 0;
155
156 GST_DEBUG_CATEGORY (gstwayland_debug);
157 #define GST_CAT_DEFAULT gstwayland_debug
158
159 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
160     GST_PAD_SINK,
161     GST_PAD_ALWAYS,
162     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
163         ("{ BGRx, BGRA, RGBx, xBGR, xRGB, RGBA, ABGR, ARGB, RGB, BGR, "
164             "RGB16, BGR16, YUY2, YVYU, UYVY, AYUV, NV12, NV21, NV16, "
165 #ifdef GST_WLSINK_ENHANCEMENT
166             "SN12, ST12, "
167 #endif
168             "YUV9, YVU9, Y41B, I420, YV12, Y42B, v308 }"))
169     );
170
171 static void gst_wayland_sink_get_property (GObject * object,
172     guint prop_id, GValue * value, GParamSpec * pspec);
173 static void gst_wayland_sink_set_property (GObject * object,
174     guint prop_id, const GValue * value, GParamSpec * pspec);
175 static void gst_wayland_sink_finalize (GObject * object);
176
177 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
178     GstStateChange transition);
179 static void gst_wayland_sink_set_context (GstElement * element,
180     GstContext * context);
181
182 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
183     GstCaps * filter);
184 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
185 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
186     GstBuffer * buffer);
187 static gboolean
188 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
189 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
190     GstBuffer * buffer);
191
192 /* VideoOverlay interface */
193 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
194     iface);
195 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
196     guintptr handle);
197 static void
198 gst_wayland_sink_set_wl_window_wl_surface_id (GstVideoOverlay * overlay,
199     guintptr wl_surface_id);
200 static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
201     gint x, gint y, gint w, gint h);
202 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
203
204 /* WaylandVideo interface */
205 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
206     iface);
207 static void gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video);
208 static void gst_wayland_sink_end_geometry_change (GstWaylandVideo * video);
209 #ifdef GST_WLSINK_ENHANCEMENT
210 static gboolean gst_wayland_sink_event (GstBaseSink * bsink, GstEvent * event);
211 static void gst_wayland_sink_update_window_geometry (GstWaylandSink * sink);
212 static void render_last_buffer (GstWaylandSink * sink);
213 #endif
214 #define gst_wayland_sink_parent_class parent_class
215 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
216     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
217         gst_wayland_sink_videooverlay_init)
218     G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
219         gst_wayland_sink_waylandvideo_init));
220
221 static void
222 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
223 {
224   GObjectClass *gobject_class;
225   GstElementClass *gstelement_class;
226   GstBaseSinkClass *gstbasesink_class;
227   FUNCTION;
228
229   gobject_class = (GObjectClass *) klass;
230   gstelement_class = (GstElementClass *) klass;
231   gstbasesink_class = (GstBaseSinkClass *) klass;
232
233   gobject_class->set_property = gst_wayland_sink_set_property;
234   gobject_class->get_property = gst_wayland_sink_get_property;
235   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
236
237   gst_element_class_add_pad_template (gstelement_class,
238       gst_static_pad_template_get (&sink_template));
239
240   gst_element_class_set_static_metadata (gstelement_class,
241       "wayland video sink", "Sink/Video",
242       "Output to wayland surface",
243       "Sreerenj Balachandran <sreerenj.balachandran@intel.com>, "
244       "George Kiagiadakis <george.kiagiadakis@collabora.com>");
245
246   gstelement_class->change_state =
247       GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
248   gstelement_class->set_context =
249       GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
250
251   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
252   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
253   gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
254   gstbasesink_class->propose_allocation =
255       GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
256   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
257 #ifdef GST_WLSINK_ENHANCEMENT
258   gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_wayland_sink_event);
259 #endif
260
261   g_object_class_install_property (gobject_class, PROP_DISPLAY,
262       g_param_spec_string ("display", "Wayland Display name", "Wayland "
263           "display name to connect to, if not supplied via the GstContext",
264           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
265 #ifdef GST_WLSINK_ENHANCEMENT
266   g_object_class_install_property (gobject_class, PROP_USE_GAPLESS,
267       g_param_spec_boolean ("use-gapless", "use flush buffer mechanism",
268           "Use gapless playback on GST_STATE_PLAYING state, "
269           "Last tbm buffer is copied and returned to codec immediately when enabled",
270           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271
272   g_object_class_install_property (gobject_class, PROP_KEEP_CAMERA_PREVIEW,
273       g_param_spec_boolean ("keep-camera-preview", "use flush buffer mechanism",
274           "Last tbm buffer is copied and returned to camerasrc immediately "
275           "when state change(PAUSED_TO_READY)", FALSE,
276           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
277
278   g_object_class_install_property (gobject_class, PROP_USE_TBM,
279       g_param_spec_boolean ("use-tbm", "use tbm buffer",
280           "Use Tizen Buffer Memory insted of Shared memory, "
281           "Memory is alloced by TBM insted of SHM when enabled", TRUE,
282           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
283
284   g_object_class_install_property (gobject_class, PROP_ROTATE_ANGLE,
285       g_param_spec_enum ("rotate", "Rotate angle",
286           "Rotate angle of display output",
287           GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
288           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289
290   g_object_class_install_property (gobject_class, PROP_DISPLAY_GEOMETRY_METHOD,
291       g_param_spec_enum ("display-geometry-method", "Display geometry method",
292           "Geometrical method for display",
293           GST_TYPE_WAYLANDSINK_DISPLAY_GEOMETRY_METHOD,
294           DEF_DISPLAY_GEOMETRY_METHOD,
295           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296
297   g_object_class_install_property (gobject_class, PROP_ORIENTATION,
298       g_param_spec_enum ("orientation",
299           "Orientation information used for ROI/ZOOM",
300           "Orientation information for display",
301           GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
302           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
303
304   g_object_class_install_property (gobject_class, PROP_FLIP,
305       g_param_spec_enum ("flip", "Display flip",
306           "Flip for display",
307           GST_TYPE_WAYLANDSINK_FLIP, DEF_DISPLAY_FLIP,
308           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
309
310   g_object_class_install_property (gobject_class, PROP_VISIBLE,
311       g_param_spec_boolean ("visible", "Visible",
312           "Draws screen or blacks out, true means visible, false blacks out",
313           TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
314 #endif
315 }
316
317 #ifdef DUMP_BUFFER
318 int
319 __write_rawdata (const char *file, const void *data, unsigned int size)
320 {
321   FILE *fp;
322
323   fp = fopen (file, "wb");
324   if (fp == NULL)
325     return -1;
326
327   fwrite ((char *) data, sizeof (char), size, fp);
328   fclose (fp);
329
330   return 0;
331 }
332 #endif
333 static void
334 gst_wayland_sink_init (GstWaylandSink * sink)
335 {
336   FUNCTION;
337 #ifdef GST_WLSINK_ENHANCEMENT
338   sink->use_gapless = FALSE;
339   sink->keep_camera_preview = FALSE;
340   sink->got_eos_event = FALSE;
341   sink->USE_TBM = TRUE;
342   sink->display_geometry_method = DEF_DISPLAY_GEOMETRY_METHOD;
343   sink->flip = DEF_DISPLAY_FLIP;
344   sink->rotate_angle = DEGREE_0;
345   sink->orientation = DEGREE_0;
346   sink->visible = TRUE;
347 #endif
348   g_mutex_init (&sink->display_lock);
349   g_mutex_init (&sink->render_lock);
350 }
351
352 #ifdef GST_WLSINK_ENHANCEMENT
353 static void
354 gst_wayland_sink_stop_video (GstWaylandSink * sink)
355 {
356   FUNCTION;
357   g_return_if_fail (sink != NULL);
358   gst_wl_window_render (sink->window, NULL, NULL);
359 }
360
361 static int
362 gst_wayland_sink_check_use_gapless (GstWaylandSink * sink)
363 {
364   g_return_val_if_fail (sink != NULL, FALSE);
365   g_return_val_if_fail (sink->display != NULL, FALSE);
366
367   if (sink->use_gapless && sink->got_eos_event && sink->USE_TBM
368       && sink->display->is_native_format)
369     return TRUE;
370
371   return FALSE;
372 }
373
374 static int
375 gst_wayland_sink_need_to_make_flush_buffer (GstWaylandSink * sink)
376 {
377   g_return_val_if_fail (sink != NULL, FALSE);
378   g_return_val_if_fail (sink->display != NULL, FALSE);
379
380   if ((gst_wayland_sink_check_use_gapless (sink))
381       || sink->keep_camera_preview || sink->display->flush_request) {
382     sink->display->flush_request = TRUE;
383     return TRUE;
384   }
385   return FALSE;
386 }
387
388 static void
389 gst_wayland_sink_update_last_buffer_geometry (GstWaylandSink * sink)
390 {
391   GstWlBuffer *wlbuffer;
392   FUNCTION;
393   g_return_if_fail (sink != NULL);
394   g_return_if_fail (sink->last_buffer != NULL);
395   wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
396   g_return_if_fail (wlbuffer != NULL);
397   gboolean no_render_buffer = FALSE;
398
399   if (wlbuffer->used_by_compositor) {
400     /* used last buffer by compositor don't receive buffer-release-event when attach */
401     wlbuffer->used_by_compositor = FALSE;
402   } else {
403     /* unused last buffer by compositor will receive buffer release event when attach */
404     no_render_buffer = TRUE;
405   }
406
407   GST_LOG ("gstbuffer(%p) ref_count(%d)", sink->last_buffer,
408       GST_OBJECT_REFCOUNT_VALUE (sink->last_buffer));
409
410   if (sink->visible) {
411     /* need to render last buffer, reuse current GstWlBuffer */
412     render_last_buffer (sink);
413
414     /* ref count is incresed in gst_wl_buffer_attach() of render_last_buffer(),
415        to call gst_wl_buffer_finalize(), we need to decrease buffer ref count.
416        wayland server can not release buffer if we attach same buffer and
417        videosink can not receive buffer-release-event. if we use no visible,
418        we need to attach null buffer and videosink can receive buffer-release-event */
419
420     /* need to decrease buffer ref_count, if no_render_buffer is TRUE,
421        buffer is attached firstly so, videosink can receive buffer-release-event */
422     if (no_render_buffer) {
423       GST_LOG ("skip unref.. will get buffer-release-event");
424     } else {
425       gst_buffer_unref (wlbuffer->gstbuffer);
426     }
427
428   } else {
429     GST_LOG ("skip rendering");
430   }
431
432   GST_LOG ("gstbuffer(%p) ref_count(%d)", sink->last_buffer,
433       GST_OBJECT_REFCOUNT_VALUE (sink->last_buffer));
434
435 }
436
437 #ifdef USE_WL_FLUSH_BUFFER
438 static int
439 gst_wayland_sink_make_flush_buffer (GstWlDisplay * display,
440     MMVideoBuffer * mm_video_buf)
441 {
442   GstWlFlushBuffer *flush_buffer = NULL;
443   tbm_bo bo = NULL;
444   int bo_size = 0;
445   int i;
446   FUNCTION;
447
448   g_return_val_if_fail (display != NULL, FALSE);
449   g_return_val_if_fail (mm_video_buf != NULL, FALSE);
450
451   flush_buffer = (GstWlFlushBuffer *) malloc (sizeof (GstWlFlushBuffer));
452   if (!flush_buffer) {
453     GST_ERROR ("GstWlFlushBuffer alloc faile");
454     return FALSE;
455   }
456   memset (flush_buffer, 0x0, sizeof (GstWlFlushBuffer));
457
458   display->flush_tbm_bufmgr =
459       wayland_tbm_client_get_bufmgr (display->tbm_client);
460   g_return_val_if_fail (display->flush_tbm_bufmgr != NULL, FALSE);
461
462   for (i = 0; i < NV_BUF_PLANE_NUM; i++) {
463     if (mm_video_buf->handle.bo[i] != NULL) {
464       tbm_bo_handle src;
465       tbm_bo_handle dst;
466
467       /* get bo size */
468       bo_size = tbm_bo_size (mm_video_buf->handle.bo[i]);
469       GST_LOG ("tbm bo size: %d", bo_size);
470       /* alloc bo */
471       bo = tbm_bo_alloc (display->flush_tbm_bufmgr, bo_size, TBM_DEVICE_CPU);
472       if (!bo) {
473         GST_ERROR ("alloc tbm bo(size:%d) failed: %s", bo_size,
474             strerror (errno));
475         return FALSE;
476       }
477       GST_INFO ("flush buffer tbm_bo =(%p)", bo);
478       flush_buffer->bo[i] = bo;
479       /* get virtual address */
480       src.ptr = dst.ptr = NULL;
481       /* bo map, we can use tbm_bo_map too. */
482       src = tbm_bo_get_handle (mm_video_buf->handle.bo[i], TBM_DEVICE_CPU);
483       dst = tbm_bo_get_handle (bo, TBM_DEVICE_CPU);
484       if (!src.ptr || !dst.ptr) {
485         GST_ERROR ("get tbm bo handle failed src(%p) dst(%p): %s", src.ptr,
486             dst.ptr, strerror (errno));
487         tbm_bo_unref (mm_video_buf->handle.bo[i]);
488         tbm_bo_unref (bo);
489         return FALSE;
490       }
491       /* copy */
492       memcpy (dst.ptr, src.ptr, bo_size);
493       /* bo unmap */
494       tbm_bo_unmap (mm_video_buf->handle.bo[i]);
495       tbm_bo_unmap (bo);
496     }
497   }
498   display->flush_buffer = flush_buffer;
499   return TRUE;
500 }
501
502 static int
503 gst_wayland_sink_copy_mm_video_buf_info_to_flush (GstWlDisplay * display,
504     MMVideoBuffer * mm_video_buf)
505 {
506   int ret = FALSE;
507   g_return_val_if_fail (display != NULL, FALSE);
508   g_return_val_if_fail (mm_video_buf != NULL, FALSE);
509   FUNCTION;
510
511   ret = gst_wayland_sink_make_flush_buffer (display, mm_video_buf);
512   if (ret) {
513     int i;
514     for (i = 0; i < NV_BUF_PLANE_NUM; i++) {
515       if (display->flush_buffer->bo[i] != NULL) {
516         display->bo[i] = display->flush_buffer->bo[i];
517         GST_LOG ("bo %p", display->bo[i]);
518       } else {
519         display->bo[i] = 0;
520       }
521       display->plane_size[i] = mm_video_buf->size[i];
522       display->stride_width[i] = mm_video_buf->stride_width[i];
523       display->stride_height[i] = mm_video_buf->stride_height[i];
524       display->native_video_size += display->plane_size[i];
525     }
526   }
527   return ret;
528 }
529 #endif
530
531 static void
532 gst_wayland_sink_add_mm_video_buf_info (GstWlDisplay * display,
533     MMVideoBuffer * mm_video_buf)
534 {
535   int i;
536   g_return_if_fail (display != NULL);
537   g_return_if_fail (mm_video_buf != NULL);
538   FUNCTION;
539
540   for (i = 0; i < NV_BUF_PLANE_NUM; i++) {
541     if (mm_video_buf->handle.bo[i] != NULL) {
542       display->bo[i] = mm_video_buf->handle.bo[i];
543     } else {
544       display->bo[i] = 0;
545     }
546     display->plane_size[i] = mm_video_buf->size[i];
547     display->stride_width[i] = mm_video_buf->stride_width[i];
548     display->stride_height[i] = mm_video_buf->stride_height[i];
549     display->native_video_size += display->plane_size[i];
550   }
551 }
552
553 static int
554 gst_wayland_sink_get_mm_video_buf_info (GstWaylandSink * sink,
555     GstBuffer * buffer)
556 {
557   GstWlDisplay *display;
558   GstMemory *mem;
559   GstMapInfo mem_info = GST_MAP_INFO_INIT;
560   MMVideoBuffer *mm_video_buf = NULL;
561
562   g_return_val_if_fail (sink != NULL, FALSE);
563   g_return_val_if_fail (buffer != NULL, FALSE);
564
565   FUNCTION;
566   display = sink->display;
567   g_return_val_if_fail (sink->display != NULL, FALSE);
568
569   mem = gst_buffer_peek_memory (buffer, 1);
570   gst_memory_map (mem, &mem_info, GST_MAP_READ);
571   mm_video_buf = (MMVideoBuffer *) mem_info.data;
572   gst_memory_unmap (mem, &mem_info);
573
574   if (mm_video_buf == NULL) {
575     GST_WARNING ("mm_video_buf is NULL. Skip rendering");
576     return FALSE;
577   }
578   /* assign mm_video_buf info */
579   if (mm_video_buf->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
580     GST_DEBUG ("TBM bo %p %p %p", mm_video_buf->handle.bo[0],
581         mm_video_buf->handle.bo[1], mm_video_buf->handle.bo[2]);
582     display->native_video_size = 0;
583     display->flush_request = mm_video_buf->flush_request;
584     GST_DEBUG ("flush_request value is %d", display->flush_request);
585 #ifdef USE_WL_FLUSH_BUFFER
586     if (gst_wayland_sink_need_to_make_flush_buffer (sink)) {
587       if (!gst_wayland_sink_copy_mm_video_buf_info_to_flush (display,
588               mm_video_buf)) {
589         GST_ERROR ("cat not copy mm_video_buf info to flush");
590         return FALSE;
591       }
592     } else
593 #endif
594       /* normal routine */
595       gst_wayland_sink_add_mm_video_buf_info (display, mm_video_buf);
596   } else {
597     GST_ERROR ("Buffer type is not TBM");
598     return FALSE;
599   }
600   return TRUE;
601 }
602
603 static void
604 gst_wayland_sink_render_flush_buffer (GstBaseSink * bsink)
605 {
606   GstWaylandSink *sink;
607   GstBuffer *buffer;
608   sink = GST_WAYLAND_SINK (bsink);
609   FUNCTION;
610   g_return_if_fail (sink != NULL);
611   g_return_if_fail (sink->last_buffer != NULL);
612
613   buffer = gst_buffer_copy_deep (sink->last_buffer);
614
615   gst_wayland_sink_render (bsink, buffer);
616   if (buffer)
617     gst_buffer_unref (buffer);
618 }
619
620 static void
621 gst_wayland_sink_gapless_render (GstBaseSink * bsink)
622 {
623   g_return_if_fail (bsink != NULL);
624   FUNCTION;
625   gst_wayland_sink_render_flush_buffer (bsink);
626 }
627
628 static void
629 gst_wayland_sink_keep_camera_preview (GstBaseSink * bsink)
630 {
631   g_return_if_fail (bsink != NULL);
632   FUNCTION;
633   gst_wayland_sink_render_flush_buffer (bsink);
634 }
635
636 #endif
637
638 static void
639 gst_wayland_sink_get_property (GObject * object,
640     guint prop_id, GValue * value, GParamSpec * pspec)
641 {
642   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
643   FUNCTION;
644
645   switch (prop_id) {
646     case PROP_DISPLAY:
647       GST_OBJECT_LOCK (sink);
648       g_value_set_string (value, sink->display_name);
649       GST_OBJECT_UNLOCK (sink);
650       break;
651 #ifdef GST_WLSINK_ENHANCEMENT
652     case PROP_USE_GAPLESS:
653       g_value_set_boolean (value, sink->use_gapless);
654       break;
655     case PROP_KEEP_CAMERA_PREVIEW:
656       g_value_set_boolean (value, sink->keep_camera_preview);
657       break;
658     case PROP_USE_TBM:
659       g_value_set_boolean (value, sink->USE_TBM);
660       break;
661     case PROP_ROTATE_ANGLE:
662       g_value_set_enum (value, sink->rotate_angle);
663       break;
664     case PROP_DISPLAY_GEOMETRY_METHOD:
665       g_value_set_enum (value, sink->display_geometry_method);
666       break;
667     case PROP_ORIENTATION:
668       g_value_set_enum (value, sink->orientation);
669       break;
670     case PROP_FLIP:
671       g_value_set_enum (value, sink->flip);
672       break;
673     case PROP_VISIBLE:
674       g_value_set_boolean (value, sink->visible);
675       break;
676 #endif
677     default:
678       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
679       break;
680   }
681 }
682
683 static void
684 gst_wayland_sink_set_property (GObject * object,
685     guint prop_id, const GValue * value, GParamSpec * pspec)
686 {
687   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
688   FUNCTION;
689   g_mutex_lock (&sink->render_lock);
690
691   switch (prop_id) {
692     case PROP_DISPLAY:
693       GST_OBJECT_LOCK (sink);
694       sink->display_name = g_value_dup_string (value);
695       GST_OBJECT_UNLOCK (sink);
696       break;
697 #ifdef GST_WLSINK_ENHANCEMENT
698     case PROP_USE_GAPLESS:
699       sink->use_gapless = g_value_get_boolean (value);
700       GST_LOG ("use gapless is (%d)", sink->use_gapless);
701       break;
702     case PROP_KEEP_CAMERA_PREVIEW:
703       sink->keep_camera_preview = g_value_get_boolean (value);
704       GST_LOG ("keep_camera_preview (%d)", sink->keep_camera_preview);
705       break;
706     case PROP_USE_TBM:
707       sink->USE_TBM = g_value_get_boolean (value);
708       GST_LOG ("1:USE TBM 0: USE SHM set(%d)", sink->USE_TBM);
709       break;
710     case PROP_ROTATE_ANGLE:
711       if (sink->rotate_angle == g_value_get_enum (value))
712         break;
713       sink->rotate_angle = g_value_get_enum (value);
714       GST_WARNING_OBJECT (sink, "Rotate angle is set (%d)", sink->rotate_angle);
715       sink->video_info_changed = TRUE;
716       if (sink->window) {
717         gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
718       }
719       break;
720     case PROP_DISPLAY_GEOMETRY_METHOD:
721       if (sink->display_geometry_method == g_value_get_enum (value))
722         break;
723       sink->display_geometry_method = g_value_get_enum (value);
724       GST_WARNING_OBJECT (sink, "Display geometry method is set (%d)",
725           sink->display_geometry_method);
726       sink->video_info_changed = TRUE;
727       if (sink->window) {
728         gst_wl_window_set_disp_geo_method (sink->window,
729             sink->display_geometry_method);
730       }
731       break;
732     case PROP_ORIENTATION:
733       if (sink->orientation == g_value_get_enum (value))
734         break;
735       sink->orientation = g_value_get_enum (value);
736       GST_WARNING_OBJECT (sink, "Orientation is set (%d)", sink->orientation);
737       sink->video_info_changed = TRUE;
738       if (sink->window) {
739         gst_wl_window_set_orientation (sink->window, sink->orientation);
740       }
741       break;
742     case PROP_FLIP:
743       if (sink->flip == g_value_get_enum (value))
744         break;
745       sink->flip = g_value_get_enum (value);
746       GST_WARNING_OBJECT (sink, "flip is set (%d)", sink->flip);
747       sink->video_info_changed = TRUE;
748       if (sink->window) {
749         gst_wl_window_set_flip (sink->window, sink->flip);
750       }
751       break;
752     case PROP_VISIBLE:
753       if (sink->visible == g_value_get_boolean (value))
754         break;
755       sink->visible = g_value_get_boolean (value);
756       GST_WARNING_OBJECT (sink, "visible is set (%d)", sink->visible);
757       if (sink->visible && GST_STATE (sink) == GST_STATE_PAUSED) {
758         /* need to attatch last buffer */
759         sink->video_info_changed = TRUE;
760       } else if (!sink->visible && GST_STATE (sink) >= GST_STATE_PAUSED) {
761         /* video stop */
762         if (sink->window) {
763           gst_wayland_sink_stop_video (sink);
764         }
765       }
766       break;
767 #endif
768     default:
769       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
770       break;
771   }
772 #ifdef GST_WLSINK_ENHANCEMENT
773   if (sink->video_info_changed && sink->window) {
774     gst_wl_window_set_video_info_change (sink->window, TRUE);
775     if (GST_STATE (sink) == GST_STATE_PAUSED)
776       gst_wayland_sink_update_last_buffer_geometry (sink);
777   }
778 #endif
779   g_mutex_unlock (&sink->render_lock);
780
781 }
782
783 static void
784 gst_wayland_sink_finalize (GObject * object)
785 {
786   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
787   FUNCTION;
788   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
789
790   if (sink->last_buffer)
791     gst_buffer_unref (sink->last_buffer);
792   if (sink->display)
793     g_object_unref (sink->display);
794   if (sink->window)
795     g_object_unref (sink->window);
796   if (sink->pool)
797     gst_object_unref (sink->pool);
798
799   if (sink->display_name)
800     g_free (sink->display_name);
801
802   g_mutex_clear (&sink->display_lock);
803   g_mutex_clear (&sink->render_lock);
804
805   G_OBJECT_CLASS (parent_class)->finalize (object);
806 }
807
808 #ifdef GST_WLSINK_ENHANCEMENT
809 static gboolean
810 gst_wayland_sink_event (GstBaseSink * bsink, GstEvent * event)
811 {
812   GstWaylandSink *sink;
813   sink = GST_WAYLAND_SINK (bsink);
814
815   switch (GST_EVENT_TYPE (event)) {
816     case GST_EVENT_EOS:
817       GST_LOG ("get EOS event..state is %d", GST_STATE (sink));
818       sink->got_eos_event = TRUE;
819       if (gst_wayland_sink_check_use_gapless (sink)) {
820         gst_wayland_sink_gapless_render (bsink);
821         sink->got_eos_event = FALSE;
822       }
823       sink->got_eos_event = FALSE;
824       break;
825     default:
826       break;
827   }
828   return GST_BASE_SINK_CLASS (parent_class)->event (bsink, event);
829 }
830 #endif
831
832 /* must be called with the display_lock */
833 static void
834 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
835     GstContext * context)
836 {
837   struct wl_display *display;
838   GError *error = NULL;
839   FUNCTION;
840
841   display = gst_wayland_display_handle_context_get_handle (context);
842   sink->display = gst_wl_display_new_existing (display, FALSE, &error);
843
844   if (error) {
845     GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
846         ("Could not set display handle"),
847         ("Failed to use the external wayland display: '%s'", error->message));
848     g_error_free (error);
849   }
850 #ifdef GST_WLSINK_ENHANCEMENT
851   sink->display->USE_TBM = sink->USE_TBM;
852 #endif
853 }
854
855 static gboolean
856 gst_wayland_sink_find_display (GstWaylandSink * sink)
857 {
858   GstQuery *query;
859   GstMessage *msg;
860   GstContext *context = NULL;
861   GError *error = NULL;
862   gboolean ret = TRUE;
863   FUNCTION;
864
865   g_mutex_lock (&sink->display_lock);
866
867   if (!sink->display) {
868     /* first query upstream for the needed display handle */
869     query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
870     if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
871       gst_query_parse_context (query, &context);
872       gst_wayland_sink_set_display_from_context (sink, context);
873     }
874     gst_query_unref (query);
875
876     if (G_LIKELY (!sink->display)) {
877       /* now ask the application to set the display handle */
878       msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
879           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
880
881       g_mutex_unlock (&sink->display_lock);
882       gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
883       /* at this point we expect gst_wayland_sink_set_context
884        * to get called and fill sink->display */
885       g_mutex_lock (&sink->display_lock);
886
887       if (!sink->display) {
888         /* if the application didn't set a display, let's create it ourselves */
889         GST_OBJECT_LOCK (sink);
890         sink->display = gst_wl_display_new (sink->display_name, &error);
891         GST_OBJECT_UNLOCK (sink);
892
893         if (error) {
894           GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
895               ("Could not initialise Wayland output"),
896               ("Failed to create GstWlDisplay: '%s'", error->message));
897           g_error_free (error);
898           ret = FALSE;
899         }
900 #ifdef GST_WLSINK_ENHANCEMENT
901         if (sink->display)
902           sink->display->USE_TBM = sink->USE_TBM;
903 #endif
904       }
905     }
906   }
907
908   g_mutex_unlock (&sink->display_lock);
909
910   return ret;
911 }
912
913 static GstStateChangeReturn
914 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
915 {
916   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
917   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
918   FUNCTION;
919
920   switch (transition) {
921     case GST_STATE_CHANGE_NULL_TO_READY:
922       GST_LOG ("WAYLANDSINK TRANSITION: NULL_TO_READY");
923       if (!gst_wayland_sink_find_display (sink))
924         return GST_STATE_CHANGE_FAILURE;
925       break;
926     case GST_STATE_CHANGE_READY_TO_PAUSED:
927       GST_LOG ("WAYLANDSINK TRANSITION: READY_TO_PAUSED");
928       break;
929     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
930       GST_LOG ("WAYLANDSINK TRANSITION: PAUSED_TO_PLAYING");
931       break;
932     default:
933       break;
934   }
935
936   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
937   if (ret == GST_STATE_CHANGE_FAILURE)
938     return ret;
939
940   switch (transition) {
941     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
942       GST_LOG ("WAYLANDSINK TRANSITION: PLAYING_TO_PAUSED");
943       break;
944     case GST_STATE_CHANGE_PAUSED_TO_READY:
945       GST_LOG ("WAYLANDSINK TRANSITION: PAUSED_TO_READY");
946 #ifdef GST_WLSINK_ENHANCEMENT
947       if (sink->keep_camera_preview) {
948         if (sink->window) {
949           if (!gst_wl_window_is_toplevel (sink->window)) {
950             GstBaseSink *bsink = GST_BASE_SINK (element);
951             gst_wayland_sink_keep_camera_preview (bsink);
952             break;
953           }
954         }
955       }
956 #endif
957       gst_buffer_replace (&sink->last_buffer, NULL);
958       if (sink->window) {
959         if (gst_wl_window_is_toplevel (sink->window)) {
960           GST_DEBUG ("internal window");
961           g_clear_object (&sink->window);
962         } else {
963           /* remove buffer from surface, show nothing */
964           GST_DEBUG ("external window");
965           gst_wl_window_render (sink->window, NULL, NULL);
966         }
967       }
968       break;
969     case GST_STATE_CHANGE_READY_TO_NULL:
970       GST_LOG ("WAYLANDSINK TRANSITION: READY_TO_NULL");
971       g_mutex_lock (&sink->display_lock);
972       /* If we had a toplevel window, we most likely have our own connection
973        * to the display too, and it is a good idea to disconnect and allow
974        * potentially the application to embed us with GstVideoOverlay
975        * (which requires to re-use the same display connection as the parent
976        * surface). If we didn't have a toplevel window, then the display
977        * connection that we have is definitely shared with the application
978        * and it's better to keep it around (together with the window handle)
979        * to avoid requesting them again from the application if/when we are
980        * restarted (GstVideoOverlay behaves like that in other sinks)
981        */
982       if (sink->display && !sink->window) {     /* -> the window was toplevel */
983         g_clear_object (&sink->display);
984       }
985       g_mutex_unlock (&sink->display_lock);
986       g_clear_object (&sink->pool);
987       break;
988     default:
989       break;
990   }
991
992   return ret;
993 }
994
995 static void
996 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
997 {
998   GstWaylandSink *sink = GST_WAYLAND_SINK (element);
999   FUNCTION;
1000
1001   if (gst_context_has_context_type (context,
1002           GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
1003     g_mutex_lock (&sink->display_lock);
1004     if (G_LIKELY (!sink->display))
1005       gst_wayland_sink_set_display_from_context (sink, context);
1006     else {
1007       GST_WARNING_OBJECT (element, "changing display handle is not supported");
1008 #ifdef GST_WLSINK_ENHANCEMENT
1009       g_mutex_unlock (&sink->display_lock);
1010       return;
1011 #endif
1012     }
1013     g_mutex_unlock (&sink->display_lock);
1014   }
1015
1016   if (GST_ELEMENT_CLASS (parent_class)->set_context)
1017     GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
1018 }
1019
1020 static GstCaps *
1021 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
1022 {
1023   GstWaylandSink *sink;
1024   GstCaps *caps;
1025   FUNCTION;
1026
1027   sink = GST_WAYLAND_SINK (bsink);
1028
1029   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
1030
1031   g_mutex_lock (&sink->display_lock);
1032
1033   if (sink->display) {
1034     GValue list = G_VALUE_INIT;
1035     GValue value = G_VALUE_INIT;
1036     GArray *formats;
1037     gint i;
1038 #ifdef GST_WLSINK_ENHANCEMENT
1039     uint32_t tbm_fmt;
1040 #endif
1041     enum wl_shm_format fmt;
1042
1043     g_value_init (&list, GST_TYPE_LIST);
1044     g_value_init (&value, G_TYPE_STRING);
1045 #ifdef GST_WLSINK_ENHANCEMENT
1046     if (sink->display->USE_TBM)
1047       formats = sink->display->tbm_formats;
1048     else                        /* SHM */
1049 #endif
1050       formats = sink->display->formats;
1051
1052     for (i = 0; i < formats->len; i++) {
1053 #ifdef GST_WLSINK_ENHANCEMENT
1054       if (sink->USE_TBM) {
1055         tbm_fmt = g_array_index (formats, uint32_t, i);
1056         g_value_set_string (&value, gst_wl_tbm_format_to_string (tbm_fmt));
1057         gst_value_list_append_value (&list, &value);
1058         /* TBM doesn't support SN12 and ST12. So we add SN12 and ST12 manually as supported format.
1059          * SN12 is same with NV12, ST12 is same with NV12MT
1060          */
1061         if (tbm_fmt == TBM_FORMAT_NV12) {
1062           g_value_set_string (&value,
1063               gst_video_format_to_string (GST_VIDEO_FORMAT_SN12));
1064           gst_value_list_append_value (&list, &value);
1065         } else if (tbm_fmt == TBM_FORMAT_NV12MT) {
1066           g_value_set_string (&value,
1067               gst_video_format_to_string (GST_VIDEO_FORMAT_ST12));
1068           gst_value_list_append_value (&list, &value);
1069         }
1070       } else {                  /* USE SHM */
1071         fmt = g_array_index (formats, uint32_t, i);
1072         g_value_set_string (&value, gst_wl_shm_format_to_string (fmt));
1073         gst_value_list_append_value (&list, &value);
1074       }
1075 #else /* open source */
1076       fmt = g_array_index (formats, uint32_t, i);
1077       g_value_set_string (&value, gst_wl_shm_format_to_string (fmt));
1078       gst_value_list_append_value (&list, &value);
1079 #endif
1080     }
1081
1082     caps = gst_caps_make_writable (caps);
1083     gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
1084
1085     GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
1086     g_value_unset (&value);
1087     g_value_unset (&list);
1088
1089   }
1090
1091   g_mutex_unlock (&sink->display_lock);
1092
1093   if (filter) {
1094     GstCaps *intersection;
1095
1096     intersection =
1097         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
1098     gst_caps_unref (caps);
1099     caps = intersection;
1100   }
1101
1102   return caps;
1103 }
1104
1105 static gboolean
1106 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
1107 {
1108   GstWaylandSink *sink;
1109   GstBufferPool *newpool;
1110   GstVideoInfo info;
1111 #ifdef GST_WLSINK_ENHANCEMENT
1112   uint32_t tbm_format;
1113 #endif
1114   enum wl_shm_format format;
1115
1116   GArray *formats;
1117   gint i;
1118   GstStructure *structure;
1119   GstWlShmAllocator *self = NULL;
1120
1121   FUNCTION;
1122
1123   sink = GST_WAYLAND_SINK (bsink);
1124
1125   GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
1126
1127   /* extract info from caps */
1128   if (!gst_video_info_from_caps (&info, caps))
1129     goto invalid_format;
1130 #ifdef GST_WLSINK_ENHANCEMENT
1131   if (sink->USE_TBM) {
1132     tbm_format =
1133         gst_video_format_to_wl_tbm_format (GST_VIDEO_INFO_FORMAT (&info));
1134     if ((gint) tbm_format == -1)
1135       goto invalid_format;
1136   } else {
1137     format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (&info));
1138     if ((gint) format == -1)
1139       goto invalid_format;
1140   }
1141 #else /* open source */
1142   format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (&info));
1143
1144   if ((gint) format == -1)
1145     goto invalid_format;
1146 #endif
1147
1148   /* verify we support the requested format */
1149 #ifdef GST_WLSINK_ENHANCEMENT
1150   if (sink->display->USE_TBM) {
1151     GST_INFO ("USE TBM FORMAT");
1152     formats = sink->display->tbm_formats;
1153     for (i = 0; i < formats->len; i++) {
1154       if (g_array_index (formats, uint32_t, i) == tbm_format)
1155         break;
1156     }
1157   } else {                      /* USE SHM */
1158     GST_INFO ("USE SHM FORMAT");
1159     formats = sink->display->formats;
1160     for (i = 0; i < formats->len; i++) {
1161       if (g_array_index (formats, uint32_t, i) == format)
1162         break;
1163     }
1164   }
1165 #else /* open source */
1166
1167   formats = sink->display->formats;
1168   for (i = 0; i < formats->len; i++) {
1169     if (g_array_index (formats, uint32_t, i) == format)
1170       break;
1171   }
1172 #endif
1173   if (i >= formats->len)
1174     goto unsupported_format;
1175
1176 #ifdef GST_WLSINK_ENHANCEMENT
1177   if (sink->USE_TBM) {
1178     if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_SN12 ||
1179         GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ST12) {
1180       sink->display->is_native_format = TRUE;
1181
1182       /* store the video info */
1183       sink->video_info = info;
1184       sink->video_info_changed = TRUE;
1185     } else {
1186       sink->display->is_native_format = FALSE;
1187       self = GST_WL_SHM_ALLOCATOR (gst_wl_shm_allocator_get ());
1188       self->display = sink->display;
1189       /* create a new pool for the new configuration */
1190       newpool = gst_video_buffer_pool_new ();
1191       if (!newpool)
1192         goto pool_failed;
1193
1194       structure = gst_buffer_pool_get_config (newpool);
1195       gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
1196       gst_buffer_pool_config_set_allocator (structure,
1197           gst_wl_shm_allocator_get (), NULL);
1198       if (!gst_buffer_pool_set_config (newpool, structure))
1199         goto config_failed;
1200
1201       /* store the video info */
1202       sink->video_info = info;
1203       sink->video_info_changed = TRUE;
1204
1205       gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1206       gst_object_unref (newpool);
1207     }
1208   } else {                      /* USE SHM */
1209
1210     self = GST_WL_SHM_ALLOCATOR (gst_wl_shm_allocator_get ());
1211     self->display = sink->display;
1212
1213     /* create a new pool for the new configuration */
1214     newpool = gst_video_buffer_pool_new ();
1215     if (!newpool)
1216       goto pool_failed;
1217
1218     structure = gst_buffer_pool_get_config (newpool);
1219     gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
1220     gst_buffer_pool_config_set_allocator (structure,
1221         gst_wl_shm_allocator_get (), NULL);
1222     if (!gst_buffer_pool_set_config (newpool, structure))
1223       goto config_failed;
1224
1225     /* store the video info */
1226     sink->video_info = info;
1227     sink->video_info_changed = TRUE;
1228
1229     gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1230     gst_object_unref (newpool);
1231   }
1232 #else /*open source */
1233   /* create a new pool for the new configuration */
1234   newpool = gst_video_buffer_pool_new ();
1235   if (!newpool)
1236     goto pool_failed;
1237
1238   structure = gst_buffer_pool_get_config (newpool);
1239   gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
1240   gst_buffer_pool_config_set_allocator (structure,
1241       gst_wl_shm_allocator_get (), NULL);
1242   if (!gst_buffer_pool_set_config (newpool, structure))
1243     goto config_failed;
1244
1245   /* store the video info */
1246   sink->video_info = info;
1247   sink->video_info_changed = TRUE;
1248
1249   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1250   gst_object_unref (newpool);
1251 #endif
1252
1253   return TRUE;
1254
1255 invalid_format:
1256   {
1257     GST_DEBUG_OBJECT (sink,
1258         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
1259     return FALSE;
1260   }
1261 unsupported_format:
1262   {
1263 #ifdef GST_WLSINK_ENHANCEMENT
1264     if (sink->USE_TBM)
1265       GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
1266           gst_wl_tbm_format_to_string (tbm_format));
1267     else                        /*USE SHM */
1268       GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
1269           gst_wl_shm_format_to_string (format));
1270 #else /*open source */
1271     GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
1272         gst_wl_shm_format_to_string (format));
1273 #endif
1274     return FALSE;
1275   }
1276 pool_failed:
1277   {
1278     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
1279     return FALSE;
1280   }
1281 config_failed:
1282   {
1283     GST_DEBUG_OBJECT (bsink, "failed setting config");
1284     gst_object_unref (newpool);
1285     return FALSE;
1286   }
1287 }
1288
1289 static gboolean
1290 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
1291 {
1292   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
1293   GstStructure *config;
1294   guint size, min_bufs, max_bufs;
1295 #ifdef GST_WLSINK_ENHANCEMENT
1296   gboolean need_pool;
1297   GstCaps *caps;
1298   FUNCTION;
1299
1300   if (sink->USE_TBM) {
1301     if (sink->display->is_native_format == TRUE)
1302       return TRUE;
1303
1304     gst_query_parse_allocation (query, &caps, &need_pool);
1305
1306     if (caps == NULL) {
1307       GST_DEBUG_OBJECT (bsink, "no caps specified");
1308       return FALSE;
1309     }
1310   }
1311 #endif
1312   config = gst_buffer_pool_get_config (sink->pool);
1313   gst_buffer_pool_config_get_params (config, NULL, &size, &min_bufs, &max_bufs);
1314
1315   /* we do have a pool for sure (created in set_caps),
1316    * so let's propose it anyway, but also propose the allocator on its own */
1317   gst_query_add_allocation_pool (query, sink->pool, size, min_bufs, max_bufs);
1318   gst_query_add_allocation_param (query, gst_wl_shm_allocator_get (), NULL);
1319
1320   gst_structure_free (config);
1321
1322   return TRUE;
1323 }
1324
1325 static GstFlowReturn
1326 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
1327 {
1328   FUNCTION;
1329   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
1330   return gst_wayland_sink_render (bsink, buffer);
1331 }
1332
1333 static void
1334 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
1335 {
1336   GstWaylandSink *sink = data;
1337   FUNCTION;
1338
1339   GST_LOG ("frame_redraw_cb");
1340
1341   g_atomic_int_set (&sink->redraw_pending, FALSE);
1342   wl_callback_destroy (callback);
1343 }
1344
1345 static const struct wl_callback_listener frame_callback_listener = {
1346   frame_redraw_callback
1347 };
1348
1349 #ifdef GST_WLSINK_ENHANCEMENT
1350 static void
1351 gst_wayland_sink_update_window_geometry (GstWaylandSink * sink)
1352 {
1353   FUNCTION;
1354   g_return_if_fail (sink != NULL);
1355   g_return_if_fail (sink->window != NULL);
1356
1357   gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
1358   gst_wl_window_set_disp_geo_method (sink->window,
1359       sink->display_geometry_method);
1360   gst_wl_window_set_orientation (sink->window, sink->orientation);
1361   gst_wl_window_set_flip (sink->window, sink->flip);
1362 }
1363 #endif
1364 /* must be called with the render lock */
1365 static void
1366 render_last_buffer (GstWaylandSink * sink)
1367 {
1368   GstWlBuffer *wlbuffer;
1369   const GstVideoInfo *info = NULL;
1370   struct wl_surface *surface;
1371   struct wl_callback *callback;
1372   FUNCTION;
1373 #ifdef GST_WLSINK_ENHANCEMENT
1374   g_return_if_fail (sink->last_buffer != NULL);
1375   g_return_if_fail (sink->window != NULL);
1376 #endif
1377
1378   wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
1379   surface = gst_wl_window_get_wl_surface (sink->window);
1380
1381   g_atomic_int_set (&sink->redraw_pending, TRUE);
1382   callback = wl_surface_frame (surface);
1383   /* frame_callback_listener is called when wayland-client finish rendering the wl_buffer */
1384   wl_callback_add_listener (callback, &frame_callback_listener, sink);
1385
1386   if (G_UNLIKELY (sink->video_info_changed)) {
1387 #ifdef GST_WLSINK_ENHANCEMENT
1388     gst_wl_window_set_video_info_change (sink->window, TRUE);
1389 #endif
1390     info = &sink->video_info;
1391     sink->video_info_changed = FALSE;
1392   }
1393   gst_wl_window_render (sink->window, wlbuffer, info);
1394 }
1395
1396 static GstFlowReturn
1397 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
1398 {
1399   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
1400   GstBuffer *to_render;
1401   GstWlBuffer *wlbuffer;
1402   GstFlowReturn ret = GST_FLOW_OK;
1403   FUNCTION;
1404
1405   g_mutex_lock (&sink->render_lock);
1406
1407   GST_LOG_OBJECT (sink, "render gstbuffer %p, ref_count(%d)", buffer,
1408       GST_OBJECT_REFCOUNT_VALUE (buffer));
1409
1410   if (G_UNLIKELY (!sink->window)) {
1411     /* ask for window handle. Unlock render_lock while doing that because
1412      * set_window_handle & friends will lock it in this context */
1413     g_mutex_unlock (&sink->render_lock);
1414     gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
1415     g_mutex_lock (&sink->render_lock);
1416
1417     if (!sink->window) {
1418       /* if we were not provided a window, create one ourselves */
1419       sink->window =
1420           gst_wl_window_new_toplevel (sink->display, &sink->video_info);
1421     }
1422 #ifdef GST_WLSINK_ENHANCEMENT
1423     gst_wayland_sink_update_window_geometry (sink);
1424 #endif
1425   }
1426   /* drop buffers until we get a frame callback */
1427   if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
1428     goto done;
1429   /* make sure that the application has called set_render_rectangle() */
1430   if (G_UNLIKELY (sink->window->render_rectangle.w == 0))
1431     goto no_window_size;
1432
1433 #ifdef GST_WLSINK_ENHANCEMENT
1434
1435   wlbuffer = gst_buffer_get_wl_buffer (buffer);
1436   if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)
1437       && !(gst_wayland_sink_check_use_gapless (sink))
1438       && !sink->keep_camera_preview) {
1439     GST_LOG_OBJECT (sink, "buffer %p has a wl_buffer from our display, " "writing directly", buffer);   //buffer is from our  pool and have wl_buffer
1440     GST_INFO ("wl_buffer (%p)", wlbuffer->wlbuffer);
1441     to_render = buffer;
1442 #ifdef DUMP_BUFFER
1443     GstMemory *mem;
1444     GstMapInfo mem_info = GST_MAP_INFO_INIT;
1445     int size = GST_VIDEO_INFO_SIZE (&sink->video_info);
1446     mem = gst_buffer_peek_memory (to_render, 0);
1447     gst_memory_map (mem, &mem_info, GST_MAP_READ);
1448     void *data;
1449     data = mem_info.data;
1450     int ret;
1451     char file_name[128];
1452
1453     sprintf (file_name, "/home/owner/DUMP/_WLSINK_OUT_DUMP_%2.2d.dump",
1454         dump__cnt++);
1455     ret = __write_rawdata (file_name, data, size);
1456     if (ret) {
1457       GST_ERROR ("_write_rawdata() failed");
1458     }
1459     GST_INFO ("DUMP IMAGE %d, size (%d)", dump__cnt, size);
1460     gst_memory_unmap (mem, &mem_info);
1461 #endif
1462   } else {
1463     GstMemory *mem;
1464     struct wl_buffer *wbuf = NULL;
1465
1466     GST_LOG_OBJECT (sink, "buffer %p does not have a wl_buffer from our " "display, creating it", buffer);      //buffer is from our pool but have not wl_buffer
1467     mem = gst_buffer_peek_memory (buffer, 0);
1468     if (gst_is_wl_shm_memory (mem)) {
1469       FUNCTION;
1470       wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1471           &sink->video_info);
1472       if (wbuf) {
1473         gst_buffer_add_wl_buffer (buffer, wbuf, sink->display); //careat GstWlBuffer and add  gstbuffer, wlbuffer, display and etc
1474         to_render = buffer;
1475       }
1476     } else {                    //buffer is not from our pool and have not wl_buffer
1477       GstMapInfo src;
1478       /* we don't know how to create a wl_buffer directly from the provided
1479        * memory, so we have to copy the data to a memory that we know how
1480        * to handle... */
1481
1482       GST_LOG_OBJECT (sink, "buffer %p is not from our pool", buffer);
1483       GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, " "copying",
1484           buffer);
1485
1486       if (sink->USE_TBM && sink->display->is_native_format) {
1487         /* in case of SN12 or ST12 */
1488         if (!gst_wayland_sink_get_mm_video_buf_info (sink, buffer))
1489           return GST_FLOW_ERROR;
1490
1491         wlbuffer = gst_buffer_get_wl_buffer (buffer);
1492         /* last_buffer from gaplasee have wlbuffer */
1493         if (G_UNLIKELY (!wlbuffer)
1494             || (gst_wayland_sink_check_use_gapless (sink))
1495             || sink->keep_camera_preview) {
1496           wbuf =
1497               gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1498               &sink->video_info);
1499           if (G_UNLIKELY (!wbuf))
1500             goto no_wl_buffer;
1501           gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
1502         }
1503       } else if (sink->USE_TBM && !sink->display->is_native_format) {
1504
1505         /* sink->pool always exists (created in set_caps), but it may not
1506          * be active if upstream is not using it */
1507         if (!gst_buffer_pool_is_active (sink->pool)
1508             && !gst_buffer_pool_set_active (sink->pool, TRUE))
1509           goto activate_failed;
1510
1511         ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1512         if (ret != GST_FLOW_OK)
1513           goto no_buffer;
1514
1515         //GstMemory *mem;
1516         //mem = gst_buffer_peek_memory (to_render, 0);
1517         //if (gst_is_wl_shm_memory (mem)) {
1518         GST_INFO ("to_render buffer is our buffer");
1519         //}
1520         /* the first time we acquire a buffer,
1521          * we need to attach a wl_buffer on it */
1522         wlbuffer = gst_buffer_get_wl_buffer (buffer);
1523         if (G_UNLIKELY (!wlbuffer)) {
1524           mem = gst_buffer_peek_memory (to_render, 0);
1525           wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1526               &sink->video_info);
1527           if (G_UNLIKELY (!wbuf))
1528             goto no_wl_buffer;
1529
1530           wlbuffer = gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
1531         }
1532
1533         gst_buffer_map (buffer, &src, GST_MAP_READ);
1534         gst_buffer_fill (to_render, 0, src.data, src.size);
1535         gst_buffer_unmap (buffer, &src);
1536       } else {                  /* USE SHM */
1537         /* sink->pool always exists (created in set_caps), but it may not
1538          * be active if upstream is not using it */
1539         if (!gst_buffer_pool_is_active (sink->pool) &&
1540             !gst_buffer_pool_set_active (sink->pool, TRUE))
1541           goto activate_failed;
1542         ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1543         if (ret != GST_FLOW_OK)
1544           goto no_buffer;
1545         /* the first time we acquire a buffer,
1546          * we need to attach a wl_buffer on it */
1547         wlbuffer = gst_buffer_get_wl_buffer (buffer);
1548         if (G_UNLIKELY (!wlbuffer)) {
1549           mem = gst_buffer_peek_memory (to_render, 0);
1550           wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1551               &sink->video_info);
1552           if (G_UNLIKELY (!wbuf))
1553             goto no_wl_buffer;
1554
1555           gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
1556
1557         }
1558
1559         gst_buffer_map (buffer, &src, GST_MAP_READ);
1560         gst_buffer_fill (to_render, 0, src.data, src.size);
1561         gst_buffer_unmap (buffer, &src);
1562       }
1563     }
1564   }
1565
1566   if (sink->USE_TBM && sink->display->is_native_format) {
1567     if ((G_UNLIKELY (buffer == sink->last_buffer)
1568             && !(gst_wayland_sink_check_use_gapless (sink)))
1569         || (G_UNLIKELY (buffer == sink->last_buffer)
1570             && !sink->keep_camera_preview)) {
1571       GST_LOG_OBJECT (sink, "Buffer already being rendered");
1572       goto done;
1573     }
1574     gst_buffer_replace (&sink->last_buffer, buffer);
1575
1576     if (sink->visible) {
1577       render_last_buffer (sink);
1578     } else {
1579       GST_LOG ("skip rendering");
1580     }
1581
1582     goto done;
1583
1584   } else {                      /* USE SHM or normal format */
1585     /* drop double rendering */
1586     if (G_UNLIKELY (buffer == sink->last_buffer)) {
1587       GST_LOG_OBJECT (sink, "Buffer already being rendered");
1588       goto done;
1589     }
1590     gst_buffer_replace (&sink->last_buffer, to_render);
1591
1592     if (sink->visible) {
1593       render_last_buffer (sink);
1594     } else {
1595       GST_LOG ("skip rendering");
1596     }
1597
1598     if (buffer != to_render)
1599       gst_buffer_unref (to_render);
1600
1601     goto done;
1602   }
1603
1604 #else /* open source */
1605
1606   wlbuffer = gst_buffer_get_wl_buffer (buffer);
1607
1608   if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) {
1609     GST_LOG_OBJECT (sink,
1610         "buffer %p has a wl_buffer from our display, " "writing directly",
1611         buffer);
1612     GST_INFO ("wl_buffer (%p)", wlbuffer->wlbuffer);
1613     to_render = buffer;
1614
1615   } else {
1616     GstMemory *mem;
1617     struct wl_buffer *wbuf = NULL;
1618
1619     GST_LOG_OBJECT (sink,
1620         "buffer %p does not have a wl_buffer from our " "display, creating it",
1621         buffer);
1622     mem = gst_buffer_peek_memory (buffer, 0);
1623     if (gst_is_wl_shm_memory (mem)) {
1624       FUNCTION;
1625       wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1626           &sink->video_info);
1627     }
1628     if (wbuf) {
1629       gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
1630       to_render = buffer;
1631
1632     } else {
1633       GstMapInfo src;
1634       /* we don't know how to create a wl_buffer directly from the provided
1635        * memory, so we have to copy the data to a memory that we know how
1636        * to handle... */
1637
1638       GST_LOG_OBJECT (sink, "buffer %p is not from our pool", buffer);
1639       GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, " "copying",
1640           buffer);
1641       /* sink->pool always exists (created in set_caps), but it may not
1642        * be active if upstream is not using it */
1643       if (!gst_buffer_pool_is_active (sink->pool) &&
1644           !gst_buffer_pool_set_active (sink->pool, TRUE))
1645         goto activate_failed;
1646
1647       ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1648       if (ret != GST_FLOW_OK)
1649         goto no_buffer;
1650
1651       /* the first time we acquire a buffer,
1652        * we need to attach a wl_buffer on it */
1653       wlbuffer = gst_buffer_get_wl_buffer (buffer);
1654       if (G_UNLIKELY (!wlbuffer)) {
1655         mem = gst_buffer_peek_memory (to_render, 0);
1656         wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1657             &sink->video_info);
1658         if (G_UNLIKELY (!wbuf))
1659           goto no_wl_buffer;
1660
1661         gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
1662       }
1663
1664       gst_buffer_map (buffer, &src, GST_MAP_READ);
1665       gst_buffer_fill (to_render, 0, src.data, src.size);
1666       gst_buffer_unmap (buffer, &src);
1667     }
1668   }
1669   /* drop double rendering */
1670   if (G_UNLIKELY (buffer == sink->last_buffer)) {
1671     GST_LOG_OBJECT (sink, "Buffer already being rendered");
1672     goto done;
1673   }
1674
1675   gst_buffer_replace (&sink->last_buffer, to_render);
1676   render_last_buffer (sink);
1677
1678   if (buffer != to_render)
1679     gst_buffer_unref (to_render);
1680
1681   goto done;
1682
1683 #endif /* GST_WLSINK_ENHANCEMENT */
1684
1685 no_window_size:
1686   {
1687     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
1688         ("Window has no size set"),
1689         ("Make sure you set the size after calling set_window_handle"));
1690     ret = GST_FLOW_ERROR;
1691     goto done;
1692   }
1693 no_buffer:
1694   {
1695     GST_WARNING_OBJECT (sink, "could not create buffer");
1696     goto done;
1697   }
1698 no_wl_buffer:
1699   {
1700     GST_ERROR_OBJECT (sink, "could not create wl_buffer out of wl_shm memory");
1701     ret = GST_FLOW_ERROR;
1702     goto done;
1703   }
1704 activate_failed:
1705   {
1706     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
1707     ret = GST_FLOW_ERROR;
1708     goto done;
1709   }
1710 done:
1711   {
1712     g_mutex_unlock (&sink->render_lock);
1713     return ret;
1714   }
1715 }
1716
1717 static void
1718 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
1719 {
1720   iface->set_window_handle = gst_wayland_sink_set_window_handle;
1721   iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
1722   iface->expose = gst_wayland_sink_expose;
1723 #ifdef GST_WLSINK_ENHANCEMENT   /* use  unique_id */
1724   iface->set_wl_window_wl_surface_id =
1725       gst_wayland_sink_set_wl_window_wl_surface_id;
1726 #endif
1727 }
1728
1729 #ifdef GST_WLSINK_ENHANCEMENT   /* use  unique_id */
1730 static void
1731 gst_wayland_sink_set_wl_window_wl_surface_id (GstVideoOverlay * overlay,
1732     guintptr wl_surface_id)
1733 {
1734   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1735   FUNCTION;
1736   g_return_if_fail (sink != NULL);
1737
1738   if (sink->window != NULL) {
1739     GST_WARNING_OBJECT (sink, "changing window handle is not supported");
1740     return;
1741   }
1742   g_mutex_lock (&sink->render_lock);
1743   g_clear_object (&sink->window);
1744
1745   GST_INFO ("wl_surface_id %d %x", (int) wl_surface_id,
1746       (guintptr) wl_surface_id);
1747
1748   if (wl_surface_id) {
1749     if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
1750       /* we cannot use our own display with an external window handle */
1751       if (G_UNLIKELY (sink->display->own_display)) {
1752         sink->display->wl_surface_id = (int) wl_surface_id;
1753         sink->window = gst_wl_window_new_in_surface (sink->display, NULL);
1754       }
1755     } else {
1756       GST_ERROR_OBJECT (sink, "Failed to find display handle, "
1757           "ignoring window handle");
1758     }
1759   }
1760   gst_wayland_sink_update_window_geometry (sink);
1761   g_mutex_unlock (&sink->render_lock);
1762
1763 }
1764 #endif
1765
1766 static void
1767 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
1768 {
1769   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1770   struct wl_surface *surface = (struct wl_surface *) handle;
1771   FUNCTION;
1772
1773   g_return_if_fail (sink != NULL);
1774
1775 #ifdef GST_WLSINK_ENHANCEMENT   /* use  unique_id */
1776   if (sink->window != NULL) {
1777     GST_WARNING_OBJECT (sink, "changing window handle is not supported");
1778     return;
1779   }
1780 #endif
1781   g_mutex_lock (&sink->render_lock);
1782
1783   GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
1784       (void *) handle);
1785
1786   g_clear_object (&sink->window);
1787
1788   if (handle) {
1789     if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
1790       /* we cannot use our own display with an external window handle */
1791       if (G_UNLIKELY (sink->display->own_display)) {
1792         GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
1793             ("Application did not provide a wayland display handle"),
1794             ("Now waylandsink use internal display handle "
1795                 "which is created ourselves. Consider providing a "
1796                 "display handle from your application with GstContext"));
1797         sink->window = gst_wl_window_new_in_surface (sink->display, surface);
1798       } else {
1799         sink->window = gst_wl_window_new_in_surface (sink->display, surface);
1800       }
1801     } else {
1802       GST_ERROR_OBJECT (sink, "Failed to find display handle, "
1803           "ignoring window handle");
1804     }
1805   }
1806 #ifdef GST_WLSINK_ENHANCEMENT
1807   gst_wayland_sink_update_window_geometry (sink);
1808 #endif
1809   g_mutex_unlock (&sink->render_lock);
1810
1811 }
1812
1813 static void
1814 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
1815     gint x, gint y, gint w, gint h)
1816 {
1817   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1818   FUNCTION;
1819
1820   g_return_if_fail (sink != NULL);
1821
1822   g_mutex_lock (&sink->render_lock);
1823   if (!sink->window) {
1824     g_mutex_unlock (&sink->render_lock);
1825     GST_WARNING_OBJECT (sink,
1826         "set_render_rectangle called without window, ignoring");
1827     return;
1828   }
1829
1830   GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
1831       x, y, w, h);
1832   gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
1833
1834   g_mutex_unlock (&sink->render_lock);
1835 }
1836
1837 static void
1838 gst_wayland_sink_expose (GstVideoOverlay * overlay)
1839 {
1840   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1841   FUNCTION;
1842
1843   g_return_if_fail (sink != NULL);
1844
1845   GST_DEBUG_OBJECT (sink, "expose");
1846
1847   g_mutex_lock (&sink->render_lock);
1848   if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
1849     GST_DEBUG_OBJECT (sink, "redrawing last buffer");
1850     render_last_buffer (sink);
1851   }
1852   g_mutex_unlock (&sink->render_lock);
1853 }
1854
1855 static void
1856 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
1857 {
1858   iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
1859   iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
1860 }
1861
1862 static void
1863 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
1864 {
1865   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1866   FUNCTION;
1867   g_return_if_fail (sink != NULL);
1868
1869   g_mutex_lock (&sink->render_lock);
1870   if (!sink->window || !sink->window->area_subsurface) {
1871     g_mutex_unlock (&sink->render_lock);
1872     GST_INFO_OBJECT (sink,
1873         "begin_geometry_change called without window, ignoring");
1874     return;
1875   }
1876
1877   wl_subsurface_set_sync (sink->window->area_subsurface);
1878   g_mutex_unlock (&sink->render_lock);
1879 }
1880
1881 static void
1882 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
1883 {
1884   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1885   FUNCTION;
1886   g_return_if_fail (sink != NULL);
1887
1888   g_mutex_lock (&sink->render_lock);
1889   if (!sink->window || !sink->window->area_subsurface) {
1890     g_mutex_unlock (&sink->render_lock);
1891     GST_INFO_OBJECT (sink,
1892         "end_geometry_change called without window, ignoring");
1893     return;
1894   }
1895
1896   wl_subsurface_set_desync (sink->window->area_subsurface);
1897   g_mutex_unlock (&sink->render_lock);
1898 }
1899
1900 static gboolean
1901 plugin_init (GstPlugin * plugin)
1902 {
1903   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
1904       " wayland video sink");
1905
1906   gst_wl_shm_allocator_register ();
1907
1908   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
1909       GST_TYPE_WAYLAND_SINK);
1910 }
1911
1912 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1913     GST_VERSION_MINOR,
1914     waylandsink,
1915     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1916     GST_PACKAGE_ORIGIN)