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