1 /* GStreamer Wayland video sink
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.
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.
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.
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.
25 * SECTION:element-waylandsink
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.
33 * <title>Example pipelines</title>
35 * gst-launch -v videotestsrc ! waylandsink
36 * ]| test the video rendering in wayland
44 #include "gstwaylandsink.h"
45 #ifdef GST_WLSINK_ENHANCEMENT
47 #include "tizen-wlvideoformat.h"
49 #include "wlvideoformat.h"
51 #include "wlshmallocator.h"
53 #include <gst/wayland/wayland.h>
54 #include <gst/video/videooverlay.h>
61 #ifdef GST_WLSINK_ENHANCEMENT
62 #define GST_APP_EVENT_FLUSH_BUFFER_NAME "application/flush-buffer"
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())
69 gst_waylandsink_rotate_angle_get_type (void)
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"},
80 if (!waylandsink_rotate_angle_type) {
81 waylandsink_rotate_angle_type =
82 g_enum_register_static ("GstWaylandSinkRotateAngleType",
86 return waylandsink_rotate_angle_type;
91 gst_waylandsink_display_geometry_method_get_type (void)
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"},
104 if (!waylandsink_display_geometry_method_type) {
105 waylandsink_display_geometry_method_type =
106 g_enum_register_static ("GstWaylandSinkDisplayGeometryMethodType",
107 display_geometry_method_type);
109 return waylandsink_display_geometry_method_type;
113 gst_waylandsink_flip_get_type (void)
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},
124 if (!waylandsink_flip_type) {
125 waylandsink_flip_type =
126 g_enum_register_static ("GstWaylandSinkFlipType", flip_type);
129 return waylandsink_flip_type;
138 #ifdef GST_WLSINK_ENHANCEMENT
140 SIGNAL_PREROLL_HANDOFF,
150 #ifdef GST_WLSINK_ENHANCEMENT
151 PROP_SIGNAL_HANDOFFS,
153 PROP_KEEP_CAMERA_PREVIEW,
156 PROP_DISPLAY_GEOMETRY_METHOD,
159 PROP_FOLLOW_PARENT_TRANSFORM,
178 GST_DEBUG_CATEGORY (gstwayland_debug);
179 #define GST_CAT_DEFAULT gstwayland_debug
181 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
184 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
185 ("{ BGRx, BGRA, RGBx, xBGR, xRGB, RGBA, ABGR, ARGB, RGB, BGR, "
186 "RGB16, BGR16, YUY2, YVYU, UYVY, AYUV, NV12, NV21, NV16, "
187 #ifdef GST_WLSINK_ENHANCEMENT
190 "YUV9, YVU9, Y41B, I420, YV12, Y42B, v308 }"))
193 static void gst_wayland_sink_get_property (GObject * object,
194 guint prop_id, GValue * value, GParamSpec * pspec);
195 static void gst_wayland_sink_set_property (GObject * object,
196 guint prop_id, const GValue * value, GParamSpec * pspec);
197 static void gst_wayland_sink_finalize (GObject * object);
199 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
200 GstStateChange transition);
201 static void gst_wayland_sink_set_context (GstElement * element,
202 GstContext * context);
204 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
206 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
207 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
210 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
211 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
214 /* VideoOverlay interface */
215 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
217 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
220 gst_wayland_sink_set_wl_window_wl_surface_id (GstVideoOverlay * overlay,
221 guintptr wl_surface_id);
222 static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
223 gint x, gint y, gint w, gint h);
224 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
226 /* WaylandVideo interface */
227 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
229 static void gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video);
230 static void gst_wayland_sink_end_geometry_change (GstWaylandVideo * video);
231 #ifdef GST_WLSINK_ENHANCEMENT
232 static gboolean gst_wayland_sink_event (GstBaseSink * bsink, GstEvent * event);
233 static void gst_wayland_sink_update_window_geometry (GstWaylandSink * sink);
234 static void render_last_buffer (GstWaylandSink * sink);
236 static guint gst_waylandsink_signals[LAST_SIGNAL] = { 0 };
239 #define gst_wayland_sink_parent_class parent_class
240 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
241 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
242 gst_wayland_sink_videooverlay_init)
243 G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
244 gst_wayland_sink_waylandvideo_init));
247 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
249 GObjectClass *gobject_class;
250 GstElementClass *gstelement_class;
251 GstBaseSinkClass *gstbasesink_class;
254 gobject_class = (GObjectClass *) klass;
255 gstelement_class = (GstElementClass *) klass;
256 gstbasesink_class = (GstBaseSinkClass *) klass;
258 gobject_class->set_property = gst_wayland_sink_set_property;
259 gobject_class->get_property = gst_wayland_sink_get_property;
260 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
262 gst_element_class_add_pad_template (gstelement_class,
263 gst_static_pad_template_get (&sink_template));
265 gst_element_class_set_static_metadata (gstelement_class,
266 "wayland video sink", "Sink/Video",
267 "Output to wayland surface",
268 "Sreerenj Balachandran <sreerenj.balachandran@intel.com>, "
269 "George Kiagiadakis <george.kiagiadakis@collabora.com>");
271 gstelement_class->change_state =
272 GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
273 gstelement_class->set_context =
274 GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
276 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
277 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
278 gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
279 gstbasesink_class->propose_allocation =
280 GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
281 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
282 #ifdef GST_WLSINK_ENHANCEMENT
283 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_wayland_sink_event);
286 g_object_class_install_property (gobject_class, PROP_DISPLAY,
287 g_param_spec_string ("display", "Wayland Display name", "Wayland "
288 "display name to connect to, if not supplied via the GstContext",
289 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
291 #ifdef GST_WLSINK_ENHANCEMENT
292 g_object_class_install_property (gobject_class, PROP_KEEP_CAMERA_PREVIEW,
293 g_param_spec_boolean ("keep-camera-preview", "use flush buffer mechanism",
294 "Last tbm buffer is copied and returned to camerasrc immediately "
295 "when state change(PAUSED_TO_READY)", FALSE,
296 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
298 g_object_class_install_property (gobject_class, PROP_USE_TBM,
299 g_param_spec_boolean ("use-tbm", "use tbm buffer",
300 "Use Tizen Buffer Memory insted of Shared memory, "
301 "Memory is alloced by TBM insted of SHM when enabled", TRUE,
302 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304 g_object_class_install_property (gobject_class, PROP_ROTATE_ANGLE,
305 g_param_spec_enum ("rotate", "Rotate angle",
306 "Rotate angle of display output",
307 GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
308 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
310 g_object_class_install_property (gobject_class, PROP_DISPLAY_GEOMETRY_METHOD,
311 g_param_spec_enum ("display-geometry-method", "Display geometry method",
312 "Geometrical method for display",
313 GST_TYPE_WAYLANDSINK_DISPLAY_GEOMETRY_METHOD,
314 DEF_DISPLAY_GEOMETRY_METHOD,
315 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
317 g_object_class_install_property (gobject_class, PROP_FLIP,
318 g_param_spec_enum ("flip", "Display flip",
320 GST_TYPE_WAYLANDSINK_FLIP, DEF_DISPLAY_FLIP,
321 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
323 g_object_class_install_property (gobject_class, PROP_VISIBLE,
324 g_param_spec_boolean ("visible", "Visible",
325 "Draws screen or blacks out, true means visible, false blacks out",
326 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
328 g_object_class_install_property (gobject_class, PROP_FOLLOW_PARENT_TRANSFORM,
329 g_param_spec_boolean ("follow-parent-transform",
330 "follow parent transform",
331 "Video is rotated automatically without setting rotate property by rotating Display"
332 "Function is not supported in DISP_GEO_METHOD_CUSTOM_ROI. ", TRUE,
333 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (gobject_class, PROP_CROP_X,
336 g_param_spec_uint ("crop-x", "crop x",
337 "x-coordinate for cropping video. "
338 "Please set crop-x, crop-y, crop-w and crop-h togethrer. "
339 "Function is not supported in DISP_GEO_METHOD_CUSTOM_ROI. ", 0,
340 G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
342 g_object_class_install_property (gobject_class, PROP_CROP_Y,
343 g_param_spec_uint ("crop-y", "crop y",
344 "y-coordinate for cropping video. "
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));
349 g_object_class_install_property (gobject_class, PROP_CROP_WIDTH,
350 g_param_spec_uint ("crop-w", "crop width",
351 "width for cropping video. "
352 "If value is not set or is set 0, Width is set to video width 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));
357 g_object_class_install_property (gobject_class, PROP_CROP_HEIGHT,
358 g_param_spec_uint ("crop-h", "crop height",
359 "height for cropping video. "
360 "If value is not set or is set 0, Hight is set to video height after set_caps. "
361 "Please set crop-x, crop-y, crop-w and crop-h togethrer. "
362 "Function is not supported in DISP_GEO_METHOD_CUSTOM_ROI. ", 0,
363 G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
365 g_object_class_install_property (gobject_class, PROP_RATIO_WIDTH,
366 g_param_spec_double ("ratio-w", "ratio width",
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 supported in DISP_GEO_METHOD_CUSTOM_ROI. ", -1.0,
371 G_MAXDOUBLE, -1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
373 g_object_class_install_property (gobject_class, PROP_RATIO_HEIGHT,
374 g_param_spec_double ("ratio-h", "ratio height",
375 "ratio width for rendering video, "
376 "If value is set, Original video ratio is ignored. to restore original size, set to -1"
377 "Please set ratio-w and ratio-h togethrer. "
378 "Function is not support in DISP_GEO_METHOD_CUSTOM_ROI. ", -1.0,
379 G_MAXDOUBLE, -1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
381 g_object_class_install_property (gobject_class, PROP_SCALE_WIDTH,
382 g_param_spec_double ("scale-w", "ratio width",
383 "scale width for rendering video,"
384 "Function is not supported in DISP_GEO_METHOD_CUSTOM_ROI. ", 0.0,
385 G_MAXDOUBLE, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
387 g_object_class_install_property (gobject_class, PROP_SCALE_HEIGHT,
388 g_param_spec_double ("scale-h", "scale height",
389 "scale width for rendering video, "
390 "Function is not support in DISP_GEO_METHOD_CUSTOM_ROI. ", 0.0,
391 G_MAXDOUBLE, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
393 g_object_class_install_property (gobject_class, PROP_OFFSET_X,
394 g_param_spec_uint ("offset-x", "offset x",
395 "x offset for moving x-coordinate of video pixel, "
396 "Please set x, y, w and h offset togethrer"
397 "Function is not supported in DISP_GEO_METHOD_CUSTOM_ROI. ", 0,
398 G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
400 g_object_class_install_property (gobject_class, PROP_OFFSET_Y,
401 g_param_spec_uint ("offset-y", "offset y",
402 "y offset for moving y-coordinate of video pixel, "
403 "Please set x, y, w and h offset togethrer"
404 "Function is not supported in DISP_GEO_METHOD_CUSTOM_ROI. ", 0,
405 G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
407 g_object_class_install_property (gobject_class, PROP_OFFSET_WIDTH,
408 g_param_spec_uint ("offset-w", "offset width",
409 "width offset for adjusting width of of video pixel, "
410 "Please set x, y, w and h offset togethrer"
411 "Function is not supported in DISP_GEO_METHOD_CUSTOM_ROI. ", 0,
412 G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
414 g_object_class_install_property (gobject_class, PROP_OFFSET_HEIGHT,
415 g_param_spec_uint ("offset-h", "offset height",
416 "height offset for adjusting height of of video pixel"
417 "Please set x, y, w and h offset togethrer"
418 "Function is not supported in DISP_GEO_METHOD_CUSTOM_ROI. ", 0,
419 G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
421 g_object_class_install_property (gobject_class, PROP_ALIGN_WIDTH,
422 g_param_spec_double ("align-w", "align width",
423 "Align with, Left: 0.0, Middle: 0.5, Right: 1.0, "
424 "Please set align-w and align-h togethrer"
425 "Function is not supported in DISP_GEO_METHOD_CUSTOM_ROI. ", 0.0,
426 1.0, 0.5, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
428 g_object_class_install_property (gobject_class, PROP_ALIGN_HEIGHT,
429 g_param_spec_double ("align-h", "align height",
430 "Align height, Left: 0.0, Middle: 0.5, Right: 1.0, "
431 "Please set align-w and align-h togethrer"
432 "Function is not supported in DISP_GEO_METHOD_CUSTOM_ROI. ", 0.0,
433 1.0, 0.5, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
435 /* fakesink function for stream callback of MSL with browser */
436 g_object_class_install_property (gobject_class, PROP_SIGNAL_HANDOFFS,
437 g_param_spec_boolean ("signal-handoffs", "Signal handoffs",
438 "Send a signal before unreffing the buffer", FALSE,
439 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
441 gst_waylandsink_signals[SIGNAL_HANDOFF] =
442 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
443 G_STRUCT_OFFSET (GstWaylandSinkClass, handoff), NULL, NULL,
444 g_cclosure_marshal_generic, G_TYPE_NONE, 2,
445 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE, GST_TYPE_PAD);
447 gst_waylandsink_signals[SIGNAL_PREROLL_HANDOFF] =
448 g_signal_new ("preroll-handoff", G_TYPE_FROM_CLASS (klass),
449 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstWaylandSinkClass, preroll_handoff),
450 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
451 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE, GST_TYPE_PAD);
458 __write_rawdata (const char *file, const void *data, unsigned int size)
462 fp = fopen (file, "wb");
466 fwrite ((char *) data, sizeof (char), size, fp);
473 gst_wayland_sink_init (GstWaylandSink * sink)
476 #ifdef GST_WLSINK_ENHANCEMENT
477 sink->signal_handoffs = FALSE;
478 sink->keep_camera_preview = FALSE;
479 sink->got_costum_event = FALSE;
480 sink->USE_TBM = TRUE;
481 sink->display_geometry_method = DEF_DISPLAY_GEOMETRY_METHOD;
482 sink->flip = DEF_DISPLAY_FLIP;
483 sink->rotate_angle = DEGREE_0;
484 sink->visible = TRUE;
485 sink->follow_parent_transform = FALSE;
486 sink->crop_x = sink->crop_y = sink->crop_w = sink->crop_h = 0;
487 sink->ratio_w = sink->ratio_h = -1.0; //need to set -1.0 for original video ratio
488 sink->scale_w = sink->scale_h = 1.0;
489 sink->align_w = sink->align_h = 0.5;
490 sink->offset_x = sink->offset_y = sink->offset_w = sink->offset_h = 0;
492 g_mutex_init (&sink->display_lock);
493 g_mutex_init (&sink->render_lock);
496 #ifdef GST_WLSINK_ENHANCEMENT
498 gst_wayland_sink_stop_video (GstWaylandSink * sink)
501 g_return_if_fail (sink != NULL);
502 gst_wl_window_render (sink->window, NULL, NULL);
506 gst_wayland_sink_check_use_gapless (GstWaylandSink * sink)
508 g_return_val_if_fail (sink != NULL, FALSE);
509 g_return_val_if_fail (sink->display != NULL, FALSE);
511 if (sink->got_costum_event && sink->USE_TBM
512 && sink->display->is_native_format)
519 gst_wayland_sink_need_to_make_flush_buffer (GstWaylandSink * sink)
521 g_return_val_if_fail (sink != NULL, FALSE);
522 g_return_val_if_fail (sink->display != NULL, FALSE);
524 if ((gst_wayland_sink_check_use_gapless (sink))
525 || sink->keep_camera_preview || sink->display->flush_request) {
526 sink->display->flush_request = TRUE;
533 gst_wayland_sink_update_last_buffer_geometry (GstWaylandSink * sink)
535 GstWlBuffer *wlbuffer;
536 gboolean no_render_buffer = FALSE;
538 g_return_if_fail (sink != NULL);
539 g_return_if_fail (sink->last_buffer != NULL);
540 wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
542 g_return_if_fail (wlbuffer != NULL);
544 if (wlbuffer->used_by_compositor) {
545 /* used last buffer by compositor don't receive buffer-release-event when attach */
546 wlbuffer->used_by_compositor = FALSE;
548 /* unused last buffer by compositor will receive buffer release event when attach */
549 no_render_buffer = TRUE;
552 GST_LOG ("gstbuffer(%p) ref_count(%d)", sink->last_buffer,
553 GST_OBJECT_REFCOUNT_VALUE (sink->last_buffer));
556 /* need to render last buffer, reuse current GstWlBuffer */
557 render_last_buffer (sink);
559 /* ref count is incresed in gst_wl_buffer_attach() of render_last_buffer(),
560 to call gst_wl_buffer_finalize(), we need to decrease buffer ref count.
561 wayland server can not release buffer if we attach same buffer and
562 videosink can not receive buffer-release-event. if we use no visible,
563 we need to attach null buffer and videosink can receive buffer-release-event */
565 /* need to decrease buffer ref_count, if no_render_buffer is TRUE,
566 buffer is attached firstly so, videosink can receive buffer-release-event */
567 if (no_render_buffer) {
568 GST_LOG ("skip unref.. will get buffer-release-event");
570 gst_buffer_unref (wlbuffer->gstbuffer);
574 GST_LOG ("skip rendering");
577 GST_LOG ("gstbuffer(%p) ref_count(%d)", sink->last_buffer,
578 GST_OBJECT_REFCOUNT_VALUE (sink->last_buffer));
582 #ifdef USE_WL_FLUSH_BUFFER
584 gst_wayland_sink_make_flush_buffer (GstWlDisplay * display,
585 MMVideoBuffer * mm_video_buf)
587 GstWlFlushBuffer *flush_buffer = NULL;
593 g_return_val_if_fail (display != NULL, FALSE);
594 g_return_val_if_fail (mm_video_buf != NULL, FALSE);
596 flush_buffer = (GstWlFlushBuffer *) malloc (sizeof (GstWlFlushBuffer));
598 GST_ERROR ("GstWlFlushBuffer alloc faile");
601 memset (flush_buffer, 0x0, sizeof (GstWlFlushBuffer));
603 display->flush_tbm_bufmgr =
604 wayland_tbm_client_get_bufmgr (display->tbm_client);
605 g_return_val_if_fail (display->flush_tbm_bufmgr != NULL, FALSE);
607 for (i = 0; i < NV_BUF_PLANE_NUM; i++) {
608 if (mm_video_buf->handle.bo[i] != NULL) {
613 bo_size = tbm_bo_size (mm_video_buf->handle.bo[i]);
614 GST_LOG ("tbm bo size: %d", bo_size);
616 bo = tbm_bo_alloc (display->flush_tbm_bufmgr, bo_size, TBM_DEVICE_CPU);
618 GST_ERROR ("alloc tbm bo(size:%d) failed: %s", bo_size,
622 GST_LOG ("flush buffer tbm_bo =(%p)", bo);
623 flush_buffer->bo[i] = bo;
624 /* get virtual address */
625 src.ptr = dst.ptr = NULL;
626 /* bo map, we can use tbm_bo_map too. */
627 src = tbm_bo_get_handle (mm_video_buf->handle.bo[i], TBM_DEVICE_CPU);
628 dst = tbm_bo_get_handle (bo, TBM_DEVICE_CPU);
629 if (!src.ptr || !dst.ptr) {
630 GST_ERROR ("get tbm bo handle failed src(%p) dst(%p): %s", src.ptr,
631 dst.ptr, strerror (errno));
632 tbm_bo_unref (mm_video_buf->handle.bo[i]);
637 memcpy (dst.ptr, src.ptr, bo_size);
639 tbm_bo_unmap (mm_video_buf->handle.bo[i]);
643 display->flush_buffer = flush_buffer;
648 gst_wayland_sink_copy_mm_video_buf_info_to_flush (GstWlDisplay * display,
649 MMVideoBuffer * mm_video_buf)
652 g_return_val_if_fail (display != NULL, FALSE);
653 g_return_val_if_fail (mm_video_buf != NULL, FALSE);
656 ret = gst_wayland_sink_make_flush_buffer (display, mm_video_buf);
659 for (i = 0; i < NV_BUF_PLANE_NUM; i++) {
660 if (display->flush_buffer->bo[i] != NULL) {
661 display->bo[i] = display->flush_buffer->bo[i];
662 GST_LOG ("bo %p", display->bo[i]);
666 display->plane_size[i] = mm_video_buf->size[i];
667 display->stride_width[i] = mm_video_buf->stride_width[i];
668 display->stride_height[i] = mm_video_buf->stride_height[i];
669 display->native_video_size += display->plane_size[i];
671 memset (mm_video_buf, 0, sizeof (MMVideoBuffer));
678 gst_wayland_sink_add_mm_video_buf_info (GstWlDisplay * display,
679 MMVideoBuffer * mm_video_buf)
682 g_return_if_fail (display != NULL);
683 g_return_if_fail (mm_video_buf != NULL);
686 for (i = 0; i < NV_BUF_PLANE_NUM; i++) {
687 if (mm_video_buf->handle.bo[i] != NULL) {
688 display->bo[i] = mm_video_buf->handle.bo[i];
692 display->plane_size[i] = mm_video_buf->size[i];
693 display->stride_width[i] = mm_video_buf->stride_width[i];
694 display->stride_height[i] = mm_video_buf->stride_height[i];
695 display->native_video_size += display->plane_size[i];
700 gst_wayland_sink_get_mm_video_buf_info (GstWaylandSink * sink,
703 GstWlDisplay *display;
705 GstMapInfo mem_info = GST_MAP_INFO_INIT;
706 MMVideoBuffer *mm_video_buf = NULL;
708 g_return_val_if_fail (sink != NULL, FALSE);
709 g_return_val_if_fail (buffer != NULL, FALSE);
712 display = sink->display;
713 g_return_val_if_fail (sink->display != NULL, FALSE);
715 mem = gst_buffer_peek_memory (buffer, 1);
716 gst_memory_map (mem, &mem_info, GST_MAP_READ);
717 mm_video_buf = (MMVideoBuffer *) mem_info.data;
718 gst_memory_unmap (mem, &mem_info);
720 if (mm_video_buf == NULL) {
721 GST_WARNING ("mm_video_buf is NULL. Skip rendering");
724 /* assign mm_video_buf info */
725 if (mm_video_buf->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
726 GST_DEBUG ("TBM bo %p %p", mm_video_buf->handle.bo[0],
727 mm_video_buf->handle.bo[1]);
728 display->native_video_size = 0;
729 display->flush_request = mm_video_buf->flush_request;
730 GST_DEBUG ("flush_request value is %d", display->flush_request);
731 #ifdef USE_WL_FLUSH_BUFFER
732 if (gst_wayland_sink_need_to_make_flush_buffer (sink)) {
733 if (!gst_wayland_sink_copy_mm_video_buf_info_to_flush (display,
735 GST_ERROR ("cat not copy mm_video_buf info to flush");
741 gst_wayland_sink_add_mm_video_buf_info (display, mm_video_buf);
743 GST_ERROR ("Buffer type is not TBM");
750 gst_wayland_sink_render_flush_buffer (GstBaseSink * bsink)
752 GstWaylandSink *sink;
754 sink = GST_WAYLAND_SINK (bsink);
756 g_return_if_fail (sink != NULL);
757 g_return_if_fail (sink->last_buffer != NULL);
759 buffer = gst_buffer_copy_deep (sink->last_buffer);
761 gst_wayland_sink_render (bsink, buffer);
763 gst_buffer_unref (buffer);
767 gst_wayland_sink_gapless_render (GstBaseSink * bsink)
769 g_return_if_fail (bsink != NULL);
771 gst_wayland_sink_render_flush_buffer (bsink);
775 gst_wayland_sink_keep_camera_preview (GstBaseSink * bsink)
777 g_return_if_fail (bsink != NULL);
779 gst_wayland_sink_render_flush_buffer (bsink);
785 gst_wayland_sink_get_property (GObject * object,
786 guint prop_id, GValue * value, GParamSpec * pspec)
788 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
793 GST_OBJECT_LOCK (sink);
794 g_value_set_string (value, sink->display_name);
795 GST_OBJECT_UNLOCK (sink);
797 #ifdef GST_WLSINK_ENHANCEMENT
798 case PROP_KEEP_CAMERA_PREVIEW:
799 g_value_set_boolean (value, sink->keep_camera_preview);
801 case PROP_SIGNAL_HANDOFFS:
802 g_value_set_boolean (value, sink->signal_handoffs);
805 g_value_set_boolean (value, sink->USE_TBM);
807 case PROP_ROTATE_ANGLE:
808 g_value_set_enum (value, sink->rotate_angle);
810 case PROP_DISPLAY_GEOMETRY_METHOD:
811 g_value_set_enum (value, sink->display_geometry_method);
814 g_value_set_enum (value, sink->flip);
817 g_value_set_boolean (value, sink->visible);
819 case PROP_FOLLOW_PARENT_TRANSFORM:
820 g_value_set_boolean (value, sink->follow_parent_transform);
823 g_value_set_uint (value, sink->crop_x);
826 g_value_set_uint (value, sink->crop_y);
828 case PROP_CROP_WIDTH:
829 g_value_set_uint (value, sink->crop_w);
831 case PROP_CROP_HEIGHT:
832 g_value_set_uint (value, sink->crop_h);
834 case PROP_RATIO_WIDTH:
835 g_value_set_double (value, sink->ratio_w);
837 case PROP_RATIO_HEIGHT:
838 g_value_set_double (value, sink->ratio_h);
840 case PROP_SCALE_WIDTH:
841 g_value_set_double (value, sink->scale_w);
843 case PROP_SCALE_HEIGHT:
844 g_value_set_double (value, sink->scale_h);
847 g_value_set_uint (value, sink->offset_x);
850 g_value_set_uint (value, sink->offset_y);
852 case PROP_OFFSET_WIDTH:
853 g_value_set_uint (value, sink->offset_w);
855 case PROP_OFFSET_HEIGHT:
856 g_value_set_uint (value, sink->offset_h);
858 case PROP_ALIGN_WIDTH:
859 g_value_set_double (value, sink->align_w);
861 case PROP_ALIGN_HEIGHT:
862 g_value_set_double (value, sink->align_h);
866 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
872 gst_wayland_sink_set_property (GObject * object,
873 guint prop_id, const GValue * value, GParamSpec * pspec)
875 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
877 g_mutex_lock (&sink->render_lock);
881 GST_OBJECT_LOCK (sink);
882 sink->display_name = g_value_dup_string (value);
883 GST_OBJECT_UNLOCK (sink);
885 #ifdef GST_WLSINK_ENHANCEMENT
886 case PROP_SIGNAL_HANDOFFS:
887 sink->signal_handoffs = g_value_get_boolean (value);
888 GST_LOG ("set signal_handoffs(%d)", sink->signal_handoffs);
889 if (sink->signal_handoffs && sink->window) {
890 /* overlay -> hand-off */
891 gst_wayland_sink_stop_video (sink);
894 case PROP_KEEP_CAMERA_PREVIEW:
895 sink->keep_camera_preview = g_value_get_boolean (value);
896 GST_LOG ("keep_camera_preview (%d)", sink->keep_camera_preview);
899 sink->USE_TBM = g_value_get_boolean (value);
900 GST_LOG ("1:USE TBM 0: USE SHM set(%d)", sink->USE_TBM);
902 case PROP_ROTATE_ANGLE:
903 if (sink->rotate_angle == g_value_get_enum (value))
905 sink->rotate_angle = g_value_get_enum (value);
906 GST_WARNING_OBJECT (sink, "Rotate angle is set (%d)", sink->rotate_angle);
907 sink->video_info_changed = TRUE;
909 gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
912 case PROP_DISPLAY_GEOMETRY_METHOD:
913 if (sink->display_geometry_method == g_value_get_enum (value))
915 sink->display_geometry_method = g_value_get_enum (value);
916 GST_WARNING_OBJECT (sink, "Display geometry method is set (%d)",
917 sink->display_geometry_method);
918 sink->video_info_changed = TRUE;
920 gst_wl_window_set_destination_mode (sink->window,
921 sink->display_geometry_method);
925 if (sink->flip == g_value_get_enum (value))
927 sink->flip = g_value_get_enum (value);
928 GST_WARNING_OBJECT (sink, "flip is set (%d)", sink->flip);
929 sink->video_info_changed = TRUE;
931 gst_wl_window_set_flip (sink->window, sink->flip);
935 if (sink->visible == g_value_get_boolean (value))
937 sink->visible = g_value_get_boolean (value);
938 GST_WARNING_OBJECT (sink, "visible is set (%d)", sink->visible);
939 if (sink->visible && GST_STATE (sink) == GST_STATE_PAUSED) {
940 /* need to attatch last buffer */
941 sink->video_info_changed = TRUE;
942 } else if (!sink->visible && GST_STATE (sink) >= GST_STATE_PAUSED) {
945 gst_wayland_sink_stop_video (sink);
949 case PROP_FOLLOW_PARENT_TRANSFORM:
950 if (sink->follow_parent_transform == g_value_get_boolean (value)
951 || sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
953 sink->follow_parent_transform = g_value_get_boolean (value);
954 GST_WARNING_OBJECT (sink, "follow parent transform is set (%d)",
955 sink->follow_parent_transform);
956 sink->video_info_changed = TRUE;
958 gst_wl_window_set_destination_mode_follow_parent_transform
959 (sink->window, sink->follow_parent_transform);
963 if (sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
965 sink->crop_x = g_value_get_uint (value);
966 GST_WARNING_OBJECT (sink, "crop-x is set (%d)", sink->crop_x);
969 if (sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
971 sink->crop_y = g_value_get_uint (value);
972 GST_WARNING_OBJECT (sink, "crop-y is set (%d)", sink->crop_y);
974 case PROP_CROP_WIDTH:
975 if (sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
977 sink->crop_w = g_value_get_uint (value);
978 GST_WARNING_OBJECT (sink, "crop_w is set (%d)", sink->crop_w);
979 /* crop-w is unset by 0, set to video width size */
980 if (sink->crop_w == 0 && sink->video_info.width > 0) {
982 gst_util_uint64_scale_int_round (sink->video_info.width,
983 sink->video_info.par_n, sink->video_info.par_d);
984 GST_LOG ("crop-w is unset by 0, set to video width size(%d)",
988 case PROP_CROP_HEIGHT:
989 if (sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
991 sink->crop_h = g_value_get_uint (value);
992 GST_WARNING_OBJECT (sink, "crop-h is set (%d)", sink->crop_h);
993 /* crop-h unset by 0, set to video height size */
994 if (sink->crop_h == 0 && sink->video_info.height > 0) {
995 sink->crop_h = sink->video_info.height;
996 GST_LOG ("crop-h is unset by 0, set to video height size(%d)",
999 sink->video_info_changed = TRUE;
1000 if (sink->window && sink->crop_w > 0 && sink->crop_h > 0) {
1001 gst_wl_window_set_destination_mode_crop_wl_buffer (sink->window,
1002 sink->crop_x, sink->crop_y, sink->crop_w, sink->crop_h);
1005 case PROP_RATIO_WIDTH:
1006 if (sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
1008 sink->ratio_w = g_value_get_double (value);
1009 GST_WARNING_OBJECT (sink, "ratio-w is set (%f)", sink->ratio_w);
1011 case PROP_RATIO_HEIGHT:
1012 if (sink->scale_w == g_value_get_double (value)
1013 || sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
1015 sink->ratio_h = g_value_get_double (value);
1016 GST_WARNING_OBJECT (sink, "ratio-h is set (%f)", sink->ratio_h);
1017 sink->video_info_changed = TRUE;
1019 gst_wl_window_set_destination_mode_ratio (sink->window, sink->ratio_w,
1022 case PROP_SCALE_WIDTH:
1023 if (sink->scale_w == g_value_get_double (value)
1024 || sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
1026 sink->scale_w = g_value_get_double (value);
1027 GST_WARNING_OBJECT (sink, "scale-h is set (%f)", sink->scale_w);
1028 sink->video_info_changed = TRUE;
1030 gst_wl_window_set_destination_mode_scale (sink->window, sink->ratio_w,
1034 if (sink->offset_x == g_value_get_uint (value)
1035 || sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
1037 sink->offset_x = g_value_get_uint (value);
1038 GST_WARNING_OBJECT (sink, "offset-x is set (%d)", sink->offset_x);
1041 if (sink->offset_y == g_value_get_uint (value)
1042 || sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
1044 sink->offset_y = g_value_get_uint (value);
1045 GST_WARNING_OBJECT (sink, "offset-y is set (%d)", sink->offset_y);
1047 case PROP_OFFSET_WIDTH:
1048 if (sink->offset_w == g_value_get_uint (value)
1049 || sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
1051 sink->offset_w = g_value_get_uint (value);
1052 GST_WARNING_OBJECT (sink, "offset-w is set (%d)", sink->offset_w);
1054 case PROP_OFFSET_HEIGHT:
1055 if (sink->offset_h == g_value_get_uint (value)
1056 || sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
1058 sink->offset_h = g_value_get_uint (value);
1059 GST_WARNING_OBJECT (sink, "offset-h is set (%d)", sink->offset_h);
1060 sink->video_info_changed = TRUE;
1062 gst_wl_window_set_destination_mode_offset (sink->window, sink->offset_x,
1063 sink->offset_y, sink->offset_w, sink->offset_h);
1065 case PROP_ALIGN_WIDTH:
1066 if (sink->align_w == g_value_get_double (value)
1067 || sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
1069 sink->align_w = g_value_get_double (value);
1070 GST_WARNING_OBJECT (sink, "align_w is set (%f)", sink->align_w);
1072 case PROP_ALIGN_HEIGHT:
1073 if (sink->align_h == g_value_get_double (value)
1074 || sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
1076 sink->align_h = g_value_get_double (value);
1077 GST_WARNING_OBJECT (sink, "align_h is set (%f)", sink->align_h);
1078 sink->video_info_changed = TRUE;
1080 gst_wl_window_set_destination_mode_align (sink->window, sink->align_w,
1086 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1089 #ifdef GST_WLSINK_ENHANCEMENT
1090 if (sink->video_info_changed && sink->window) {
1091 gst_wl_window_set_video_info_change (sink->window, TRUE);
1092 if (GST_STATE (sink) == GST_STATE_PAUSED)
1093 gst_wayland_sink_update_last_buffer_geometry (sink);
1096 g_mutex_unlock (&sink->render_lock);
1101 gst_wayland_sink_finalize (GObject * object)
1103 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
1105 GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
1107 if (sink->last_buffer)
1108 gst_buffer_unref (sink->last_buffer);
1110 g_object_unref (sink->display);
1112 g_object_unref (sink->window);
1114 gst_object_unref (sink->pool);
1116 if (sink->display_name)
1117 g_free (sink->display_name);
1119 g_mutex_clear (&sink->display_lock);
1120 g_mutex_clear (&sink->render_lock);
1122 G_OBJECT_CLASS (parent_class)->finalize (object);
1125 #ifdef GST_WLSINK_ENHANCEMENT
1127 gst_wayland_sink_event (GstBaseSink * bsink, GstEvent * event)
1129 GstWaylandSink *sink;
1130 const GstStructure *s;
1132 sink = GST_WAYLAND_SINK (bsink);
1134 switch (GST_EVENT_TYPE (event)) {
1136 GST_LOG ("get GST_EVENT_EOS event..state is %d", GST_STATE (sink));
1138 case GST_EVENT_CUSTOM_DOWNSTREAM:
1139 s = gst_event_get_structure (event);
1141 || !gst_structure_has_name (s, GST_APP_EVENT_FLUSH_BUFFER_NAME))
1144 GST_LOG ("get GST_EVENT_CUSTOM_DOWNSTREAM EVENT: %s..state is %d",
1145 gst_structure_get_name (s), GST_STATE (sink));
1147 sink->got_costum_event = TRUE;
1148 if (gst_wayland_sink_check_use_gapless (sink)) {
1149 gst_wayland_sink_gapless_render (bsink);
1150 sink->got_costum_event = FALSE;
1152 sink->got_costum_event = FALSE;
1157 return GST_BASE_SINK_CLASS (parent_class)->event (bsink, event);
1161 /* must be called with the display_lock */
1163 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
1164 GstContext * context)
1166 struct wl_display *display;
1167 GError *error = NULL;
1170 display = gst_wayland_display_handle_context_get_handle (context);
1171 sink->display = gst_wl_display_new_existing (display, FALSE, &error);
1174 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
1175 ("Could not set display handle"),
1176 ("Failed to use the external wayland display: '%s'", error->message));
1177 g_error_free (error);
1179 #ifdef GST_WLSINK_ENHANCEMENT
1180 sink->display->USE_TBM = sink->USE_TBM;
1185 gst_wayland_sink_find_display (GstWaylandSink * sink)
1189 GstContext *context = NULL;
1190 GError *error = NULL;
1191 gboolean ret = TRUE;
1194 g_mutex_lock (&sink->display_lock);
1196 if (!sink->display) {
1197 /* first query upstream for the needed display handle */
1198 query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
1199 if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
1200 gst_query_parse_context (query, &context);
1201 gst_wayland_sink_set_display_from_context (sink, context);
1203 gst_query_unref (query);
1205 if (G_LIKELY (!sink->display)) {
1206 /* now ask the application to set the display handle */
1207 msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
1208 GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
1210 g_mutex_unlock (&sink->display_lock);
1211 gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
1212 /* at this point we expect gst_wayland_sink_set_context
1213 * to get called and fill sink->display */
1214 g_mutex_lock (&sink->display_lock);
1216 if (!sink->display) {
1217 /* if the application didn't set a display, let's create it ourselves */
1218 GST_OBJECT_LOCK (sink);
1219 sink->display = gst_wl_display_new (sink->display_name, &error);
1220 GST_OBJECT_UNLOCK (sink);
1223 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
1224 ("Could not initialise Wayland output"),
1225 ("Failed to create GstWlDisplay: '%s'", error->message));
1226 g_error_free (error);
1229 #ifdef GST_WLSINK_ENHANCEMENT
1231 sink->display->USE_TBM = sink->USE_TBM;
1237 g_mutex_unlock (&sink->display_lock);
1242 static GstStateChangeReturn
1243 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
1245 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
1246 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1249 switch (transition) {
1250 case GST_STATE_CHANGE_NULL_TO_READY:
1251 GST_LOG ("WAYLANDSINK TRANSITION: NULL_TO_READY");
1252 if (!gst_wayland_sink_find_display (sink))
1253 return GST_STATE_CHANGE_FAILURE;
1255 case GST_STATE_CHANGE_READY_TO_PAUSED:
1256 GST_LOG ("WAYLANDSINK TRANSITION: READY_TO_PAUSED");
1258 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1259 GST_LOG ("WAYLANDSINK TRANSITION: PAUSED_TO_PLAYING");
1265 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1266 if (ret == GST_STATE_CHANGE_FAILURE)
1269 switch (transition) {
1270 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1271 GST_LOG ("WAYLANDSINK TRANSITION: PLAYING_TO_PAUSED");
1273 case GST_STATE_CHANGE_PAUSED_TO_READY:
1274 GST_LOG ("WAYLANDSINK TRANSITION: PAUSED_TO_READY");
1275 #ifdef GST_WLSINK_ENHANCEMENT
1276 if (sink->keep_camera_preview) {
1278 if (!gst_wl_window_is_toplevel (sink->window)) {
1279 GstBaseSink *bsink = GST_BASE_SINK (element);
1280 gst_wayland_sink_keep_camera_preview (bsink);
1286 gst_buffer_replace (&sink->last_buffer, NULL);
1288 if (gst_wl_window_is_toplevel (sink->window)) {
1289 GST_DEBUG ("internal window");
1290 g_clear_object (&sink->window);
1292 /* remove buffer from surface, show nothing */
1293 GST_DEBUG ("external window");
1294 gst_wl_window_render (sink->window, NULL, NULL);
1298 case GST_STATE_CHANGE_READY_TO_NULL:
1299 GST_LOG ("WAYLANDSINK TRANSITION: READY_TO_NULL");
1300 g_mutex_lock (&sink->display_lock);
1301 /* If we had a toplevel window, we most likely have our own connection
1302 * to the display too, and it is a good idea to disconnect and allow
1303 * potentially the application to embed us with GstVideoOverlay
1304 * (which requires to re-use the same display connection as the parent
1305 * surface). If we didn't have a toplevel window, then the display
1306 * connection that we have is definitely shared with the application
1307 * and it's better to keep it around (together with the window handle)
1308 * to avoid requesting them again from the application if/when we are
1309 * restarted (GstVideoOverlay behaves like that in other sinks)
1311 if (sink->display && !sink->window) { /* -> the window was toplevel */
1312 g_clear_object (&sink->display);
1314 g_mutex_unlock (&sink->display_lock);
1315 g_clear_object (&sink->pool);
1325 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
1327 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
1330 if (gst_context_has_context_type (context,
1331 GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
1332 g_mutex_lock (&sink->display_lock);
1333 if (G_LIKELY (!sink->display))
1334 gst_wayland_sink_set_display_from_context (sink, context);
1336 GST_WARNING_OBJECT (element, "changing display handle is not supported");
1337 #ifdef GST_WLSINK_ENHANCEMENT
1338 g_mutex_unlock (&sink->display_lock);
1342 g_mutex_unlock (&sink->display_lock);
1345 if (GST_ELEMENT_CLASS (parent_class)->set_context)
1346 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
1350 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
1352 GstWaylandSink *sink;
1356 sink = GST_WAYLAND_SINK (bsink);
1358 caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
1360 g_mutex_lock (&sink->display_lock);
1362 if (sink->display) {
1363 GValue list = G_VALUE_INIT;
1364 GValue value = G_VALUE_INIT;
1367 #ifdef GST_WLSINK_ENHANCEMENT
1370 enum wl_shm_format fmt;
1372 g_value_init (&list, GST_TYPE_LIST);
1373 g_value_init (&value, G_TYPE_STRING);
1374 #ifdef GST_WLSINK_ENHANCEMENT
1375 if (sink->display->USE_TBM)
1376 formats = sink->display->tbm_formats;
1379 formats = sink->display->formats;
1381 for (i = 0; i < formats->len; i++) {
1382 #ifdef GST_WLSINK_ENHANCEMENT
1383 if (sink->USE_TBM) {
1384 tbm_fmt = g_array_index (formats, uint32_t, i);
1385 g_value_set_string (&value, gst_wl_tbm_format_to_string (tbm_fmt));
1386 gst_value_list_append_value (&list, &value);
1387 /* TBM doesn't support SN12 and ST12. So we add SN12 and ST12 manually as supported format.
1388 * SN12 is same with NV12, ST12 is same with NV12MT
1390 if (tbm_fmt == TBM_FORMAT_NV12) {
1391 g_value_set_string (&value,
1392 gst_video_format_to_string (GST_VIDEO_FORMAT_SN12));
1393 gst_value_list_append_value (&list, &value);
1394 } else if (tbm_fmt == TBM_FORMAT_NV12MT) {
1395 g_value_set_string (&value,
1396 gst_video_format_to_string (GST_VIDEO_FORMAT_ST12));
1397 gst_value_list_append_value (&list, &value);
1399 } else { /* USE SHM */
1400 fmt = g_array_index (formats, uint32_t, i);
1401 g_value_set_string (&value, gst_wl_shm_format_to_string (fmt));
1402 gst_value_list_append_value (&list, &value);
1404 #else /* open source */
1405 fmt = g_array_index (formats, uint32_t, i);
1406 g_value_set_string (&value, gst_wl_shm_format_to_string (fmt));
1407 gst_value_list_append_value (&list, &value);
1411 caps = gst_caps_make_writable (caps);
1412 gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
1414 GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
1415 g_value_unset (&value);
1416 g_value_unset (&list);
1420 g_mutex_unlock (&sink->display_lock);
1423 GstCaps *intersection;
1426 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
1427 gst_caps_unref (caps);
1428 caps = intersection;
1435 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
1437 GstWaylandSink *sink;
1438 GstBufferPool *newpool;
1440 #ifdef GST_WLSINK_ENHANCEMENT
1441 uint32_t tbm_format = -1;
1443 enum wl_shm_format format = -1;
1447 GstStructure *structure;
1448 GstWlShmAllocator *self = NULL;
1452 sink = GST_WAYLAND_SINK (bsink);
1454 GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
1456 /* extract info from caps */
1457 if (!gst_video_info_from_caps (&info, caps))
1458 goto invalid_format;
1459 #ifdef GST_WLSINK_ENHANCEMENT
1460 if (sink->USE_TBM) {
1462 gst_video_format_to_wl_tbm_format (GST_VIDEO_INFO_FORMAT (&info));
1463 if ((gint) tbm_format == -1)
1464 goto invalid_format;
1466 format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (&info));
1467 if ((gint) format == -1)
1468 goto invalid_format;
1470 #else /* open source */
1471 format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (&info));
1473 if ((gint) format == -1)
1474 goto invalid_format;
1477 /* verify we support the requested format */
1478 #ifdef GST_WLSINK_ENHANCEMENT
1479 if (sink->display->USE_TBM) {
1480 GST_LOG ("USE TBM FORMAT");
1481 formats = sink->display->tbm_formats;
1482 for (i = 0; i < formats->len; i++) {
1483 if (g_array_index (formats, uint32_t, i) == tbm_format)
1486 } else { /* USE SHM */
1487 GST_LOG ("USE SHM FORMAT");
1488 formats = sink->display->formats;
1489 for (i = 0; i < formats->len; i++) {
1490 if (g_array_index (formats, uint32_t, i) == format)
1494 #else /* open source */
1496 formats = sink->display->formats;
1497 for (i = 0; i < formats->len; i++) {
1498 if (g_array_index (formats, uint32_t, i) == format)
1502 if (i >= formats->len)
1503 goto unsupported_format;
1505 #ifdef GST_WLSINK_ENHANCEMENT
1506 if (sink->USE_TBM) {
1507 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_SN12 ||
1508 GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ST12) {
1509 sink->display->is_native_format = TRUE;
1511 /* store the video info */
1512 sink->video_info = info;
1513 sink->video_info_changed = TRUE;
1515 sink->display->is_native_format = FALSE;
1516 self = GST_WL_SHM_ALLOCATOR (gst_wl_shm_allocator_get ());
1517 self->display = sink->display;
1518 /* create a new pool for the new configuration */
1519 newpool = gst_video_buffer_pool_new ();
1523 structure = gst_buffer_pool_get_config (newpool);
1524 gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
1525 gst_buffer_pool_config_set_allocator (structure,
1526 gst_wl_shm_allocator_get (), NULL);
1527 if (!gst_buffer_pool_set_config (newpool, structure))
1530 /* store the video info */
1531 sink->video_info = info;
1532 sink->video_info_changed = TRUE;
1534 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1535 gst_object_unref (newpool);
1537 } else { /* USE SHM */
1539 self = GST_WL_SHM_ALLOCATOR (gst_wl_shm_allocator_get ());
1540 self->display = sink->display;
1542 /* create a new pool for the new configuration */
1543 newpool = gst_video_buffer_pool_new ();
1547 structure = gst_buffer_pool_get_config (newpool);
1548 gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
1549 gst_buffer_pool_config_set_allocator (structure,
1550 gst_wl_shm_allocator_get (), NULL);
1551 if (!gst_buffer_pool_set_config (newpool, structure))
1554 /* store the video info */
1555 sink->video_info = info;
1556 sink->video_info_changed = TRUE;
1558 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1559 gst_object_unref (newpool);
1561 #ifdef GST_WLSINK_ENHANCEMENT
1563 gst_wayland_sink_update_window_geometry (sink);
1566 #else /*open source */
1567 /* create a new pool for the new configuration */
1568 newpool = gst_video_buffer_pool_new ();
1572 structure = gst_buffer_pool_get_config (newpool);
1573 gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
1574 gst_buffer_pool_config_set_allocator (structure,
1575 gst_wl_shm_allocator_get (), NULL);
1576 if (!gst_buffer_pool_set_config (newpool, structure))
1579 /* store the video info */
1580 sink->video_info = info;
1581 sink->video_info_changed = TRUE;
1583 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1584 gst_object_unref (newpool);
1591 GST_DEBUG_OBJECT (sink,
1592 "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
1597 #ifdef GST_WLSINK_ENHANCEMENT
1599 GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
1600 gst_wl_tbm_format_to_string (tbm_format));
1602 GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
1603 gst_wl_shm_format_to_string (format));
1604 #else /*open source */
1605 GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
1606 gst_wl_shm_format_to_string (format));
1612 GST_DEBUG_OBJECT (sink, "Failed to create new pool");
1617 GST_DEBUG_OBJECT (bsink, "failed setting config");
1618 gst_object_unref (newpool);
1624 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
1626 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
1627 GstStructure *config;
1628 guint size, min_bufs, max_bufs;
1629 #ifdef GST_WLSINK_ENHANCEMENT
1634 if (sink->USE_TBM) {
1635 if (sink->display->is_native_format == TRUE)
1638 gst_query_parse_allocation (query, &caps, &need_pool);
1641 GST_DEBUG_OBJECT (bsink, "no caps specified");
1646 config = gst_buffer_pool_get_config (sink->pool);
1647 gst_buffer_pool_config_get_params (config, NULL, &size, &min_bufs, &max_bufs);
1649 /* we do have a pool for sure (created in set_caps),
1650 * so let's propose it anyway, but also propose the allocator on its own */
1651 gst_query_add_allocation_pool (query, sink->pool, size, min_bufs, max_bufs);
1652 gst_query_add_allocation_param (query, gst_wl_shm_allocator_get (), NULL);
1654 gst_structure_free (config);
1659 static GstFlowReturn
1660 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
1662 #ifdef GST_WLSINK_ENHANCEMENT
1663 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
1666 GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
1668 if (sink->signal_handoffs) {
1669 GST_LOG ("g_signal_emit: preroll-handoff");
1670 g_signal_emit (sink,
1671 gst_waylandsink_signals[SIGNAL_PREROLL_HANDOFF], 0, buffer,
1677 return gst_wayland_sink_render (bsink, buffer);
1681 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
1683 GstWaylandSink *sink = data;
1686 GST_LOG ("frame_redraw_cb");
1688 g_atomic_int_set (&sink->redraw_pending, FALSE);
1689 GST_INFO ("wl_callback_destroy (wl_callback@%p)", callback);
1690 wl_callback_destroy (callback);
1693 static const struct wl_callback_listener frame_callback_listener = {
1694 frame_redraw_callback
1697 #ifdef GST_WLSINK_ENHANCEMENT
1699 gst_wayland_sink_update_window_geometry (GstWaylandSink * sink)
1702 g_return_if_fail (sink != NULL);
1703 g_return_if_fail (sink->window != NULL);
1705 gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
1706 gst_wl_window_set_destination_mode (sink->window,
1707 sink->display_geometry_method);
1708 gst_wl_window_set_flip (sink->window, sink->flip);
1709 gst_wl_window_set_destination_mode_follow_parent_transform (sink->window,
1710 sink->follow_parent_transform);
1712 if (sink->crop_w == 0 && sink->crop_h == 0) {
1714 gst_util_uint64_scale_int_round (sink->video_info.width,
1715 sink->video_info.par_n, sink->video_info.par_d);
1716 sink->crop_h = sink->video_info.height;
1718 gst_wl_window_set_destination_mode_crop_wl_buffer (sink->window, sink->crop_x,
1719 sink->crop_y, sink->crop_w, sink->crop_h);
1720 gst_wl_window_set_destination_mode_ratio (sink->window, sink->ratio_w,
1722 gst_wl_window_set_destination_mode_scale (sink->window, sink->scale_w,
1724 gst_wl_window_set_destination_mode_offset (sink->window, sink->offset_x,
1725 sink->offset_y, sink->offset_w, sink->offset_h);
1726 gst_wl_window_set_destination_mode_align (sink->window, sink->align_w,
1731 /* must be called with the render lock */
1733 render_last_buffer (GstWaylandSink * sink)
1735 GstWlBuffer *wlbuffer;
1736 const GstVideoInfo *info = NULL;
1737 struct wl_surface *surface;
1738 struct wl_callback *callback;
1740 #ifdef GST_WLSINK_ENHANCEMENT
1741 g_return_if_fail (sink->last_buffer != NULL);
1742 g_return_if_fail (sink->window != NULL);
1745 wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
1746 surface = gst_wl_window_get_wl_surface (sink->window);
1748 g_atomic_int_set (&sink->redraw_pending, TRUE);
1749 callback = wl_surface_frame (surface);
1750 GST_INFO ("wl_callback@%p = wl_surface_frame (video_surface@%p)", callback,
1753 /* frame_callback_listener is called when wayland-client finish rendering the wl_buffer */
1755 ("wl_callback_add_listener (wl_callback@%p, wl_callback_listener@%p, GstWaylandSink@%p)",
1756 callback, &frame_callback_listener, sink);
1757 wl_callback_add_listener (callback, &frame_callback_listener, sink);
1759 if (G_UNLIKELY (sink->video_info_changed)) {
1760 #ifdef GST_WLSINK_ENHANCEMENT
1761 gst_wl_window_set_video_info_change (sink->window, TRUE);
1763 info = &sink->video_info;
1764 sink->video_info_changed = FALSE;
1766 gst_wl_window_render (sink->window, wlbuffer, info);
1769 static GstFlowReturn
1770 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
1772 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
1773 GstBuffer *to_render;
1774 GstWlBuffer *wlbuffer;
1775 GstFlowReturn ret = GST_FLOW_OK;
1778 g_mutex_lock (&sink->render_lock);
1780 GST_LOG_OBJECT (sink, "render gstbuffer %p, ref_count(%d)", buffer,
1781 GST_OBJECT_REFCOUNT_VALUE (buffer));
1783 if (G_UNLIKELY (!sink->window)) {
1784 /* ask for window handle. Unlock render_lock while doing that because
1785 * set_window_handle & friends will lock it in this context */
1786 g_mutex_unlock (&sink->render_lock);
1787 gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
1788 g_mutex_lock (&sink->render_lock);
1790 if (!sink->window) {
1791 /* if we were not provided a window, create one ourselves */
1793 gst_wl_window_new_toplevel (sink->display, &sink->video_info);
1795 #ifdef GST_WLSINK_ENHANCEMENT
1796 gst_wayland_sink_update_window_geometry (sink);
1800 if (sink->signal_handoffs) {
1801 GST_LOG ("g_signal_emit: hand-off ");
1802 g_signal_emit (sink, gst_waylandsink_signals[SIGNAL_HANDOFF], 0, buffer,
1807 /* drop buffers until we get a frame callback */
1808 if (g_atomic_int_get (&sink->redraw_pending) == TRUE
1809 && !gst_wayland_sink_check_use_gapless (sink))
1812 #ifndef GST_WLSINK_ENHANCEMENT /* for tizen view_port, we don't know window size */
1813 /* make sure that the application has called set_render_rectangle() */
1814 if (G_UNLIKELY (sink->window->render_rectangle.w == 0))
1815 goto no_window_size;
1817 #ifdef GST_WLSINK_ENHANCEMENT
1819 wlbuffer = gst_buffer_get_wl_buffer (buffer);
1820 if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)
1821 && !(gst_wayland_sink_check_use_gapless (sink))
1822 && !sink->keep_camera_preview) {
1823 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
1824 GST_LOG ("wl_buffer (%p)", wlbuffer->wlbuffer);
1828 GstMapInfo mem_info = GST_MAP_INFO_INIT;
1829 int size = GST_VIDEO_INFO_SIZE (&sink->video_info);
1830 mem = gst_buffer_peek_memory (to_render, 0);
1831 gst_memory_map (mem, &mem_info, GST_MAP_READ);
1833 data = mem_info.data;
1835 char file_name[128];
1837 sprintf (file_name, "/home/owner/DUMP/_WLSINK_OUT_DUMP_%2.2d.dump",
1839 ret = __write_rawdata (file_name, data, size);
1841 GST_ERROR ("_write_rawdata() failed");
1843 GST_LOG ("DUMP IMAGE %d, size (%d)", dump__cnt, size);
1844 gst_memory_unmap (mem, &mem_info);
1848 struct wl_buffer *wbuf = NULL;
1850 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
1851 mem = gst_buffer_peek_memory (buffer, 0);
1852 if (gst_is_wl_shm_memory (mem)) {
1854 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1857 gst_buffer_add_wl_buffer (buffer, wbuf, sink->display); //careat GstWlBuffer and add gstbuffer, wlbuffer, display and etc
1860 } else { //buffer is not from our pool and have not wl_buffer
1862 /* we don't know how to create a wl_buffer directly from the provided
1863 * memory, so we have to copy the data to a memory that we know how
1866 GST_LOG_OBJECT (sink, "buffer %p is not from our pool", buffer);
1867 GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, " "copying",
1870 if (sink->USE_TBM && sink->display->is_native_format) {
1871 /* in case of SN12 or ST12 */
1872 if (!gst_wayland_sink_get_mm_video_buf_info (sink, buffer))
1873 return GST_FLOW_ERROR;
1875 wlbuffer = gst_buffer_get_wl_buffer (buffer);
1876 /* last_buffer from gaplasee have wlbuffer */
1877 if (G_UNLIKELY (!wlbuffer)
1878 || (gst_wayland_sink_check_use_gapless (sink))
1879 || sink->keep_camera_preview) {
1881 gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1883 if (G_UNLIKELY (!wbuf))
1885 gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
1887 } else if (sink->USE_TBM && !sink->display->is_native_format) {
1889 /* sink->pool always exists (created in set_caps), but it may not
1890 * be active if upstream is not using it */
1891 if (!gst_buffer_pool_is_active (sink->pool)
1892 && !gst_buffer_pool_set_active (sink->pool, TRUE))
1893 goto activate_failed;
1895 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1896 if (ret != GST_FLOW_OK)
1900 //mem = gst_buffer_peek_memory (to_render, 0);
1901 //if (gst_is_wl_shm_memory (mem)) {
1902 GST_LOG ("to_render buffer is our buffer");
1904 /* the first time we acquire a buffer,
1905 * we need to attach a wl_buffer on it */
1906 wlbuffer = gst_buffer_get_wl_buffer (buffer);
1907 if (G_UNLIKELY (!wlbuffer)) {
1908 mem = gst_buffer_peek_memory (to_render, 0);
1909 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1911 if (G_UNLIKELY (!wbuf))
1914 wlbuffer = gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
1917 gst_buffer_map (buffer, &src, GST_MAP_READ);
1918 gst_buffer_fill (to_render, 0, src.data, src.size);
1919 gst_buffer_unmap (buffer, &src);
1920 } else { /* USE SHM */
1921 /* sink->pool always exists (created in set_caps), but it may not
1922 * be active if upstream is not using it */
1923 if (!gst_buffer_pool_is_active (sink->pool) &&
1924 !gst_buffer_pool_set_active (sink->pool, TRUE))
1925 goto activate_failed;
1926 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1927 if (ret != GST_FLOW_OK)
1929 /* the first time we acquire a buffer,
1930 * we need to attach a wl_buffer on it */
1931 wlbuffer = gst_buffer_get_wl_buffer (buffer);
1932 if (G_UNLIKELY (!wlbuffer)) {
1933 mem = gst_buffer_peek_memory (to_render, 0);
1934 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1936 if (G_UNLIKELY (!wbuf))
1939 gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
1943 gst_buffer_map (buffer, &src, GST_MAP_READ);
1944 gst_buffer_fill (to_render, 0, src.data, src.size);
1945 gst_buffer_unmap (buffer, &src);
1950 if (sink->USE_TBM && sink->display->is_native_format) {
1951 if ((G_UNLIKELY (buffer == sink->last_buffer)
1952 && !(gst_wayland_sink_check_use_gapless (sink)))
1953 || (G_UNLIKELY (buffer == sink->last_buffer)
1954 && !sink->keep_camera_preview)) {
1955 GST_LOG_OBJECT (sink, "Buffer already being rendered");
1959 GST_LOG_OBJECT (sink, "replace last_buffer: (%p) -> (%p)",
1960 sink->last_buffer, buffer);
1961 gst_buffer_replace (&sink->last_buffer, buffer);
1963 GST_LOG_OBJECT (sink, "after gst_buffer_replace buffer %p, ref_count(%d)",
1964 buffer, GST_OBJECT_REFCOUNT_VALUE (buffer));
1966 if (sink->visible) {
1967 render_last_buffer (sink);
1969 GST_LOG ("skip rendering");
1974 } else { /* USE SHM or normal format */
1975 /* drop double rendering */
1976 if (G_UNLIKELY (buffer == sink->last_buffer)) {
1977 GST_LOG_OBJECT (sink, "Buffer already being rendered");
1980 gst_buffer_replace (&sink->last_buffer, to_render);
1982 if (sink->visible) {
1983 render_last_buffer (sink);
1985 GST_LOG ("skip rendering");
1988 if (buffer != to_render)
1989 gst_buffer_unref (to_render);
1994 #else /* open source */
1996 wlbuffer = gst_buffer_get_wl_buffer (buffer);
1998 if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) {
1999 GST_LOG_OBJECT (sink,
2000 "buffer %p has a wl_buffer from our display, " "writing directly",
2002 GST_LOG ("wl_buffer (%p)", wlbuffer->wlbuffer);
2007 struct wl_buffer *wbuf = NULL;
2009 GST_LOG_OBJECT (sink,
2010 "buffer %p does not have a wl_buffer from our " "display, creating it",
2012 mem = gst_buffer_peek_memory (buffer, 0);
2013 if (gst_is_wl_shm_memory (mem)) {
2015 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
2019 gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
2024 /* we don't know how to create a wl_buffer directly from the provided
2025 * memory, so we have to copy the data to a memory that we know how
2028 GST_LOG_OBJECT (sink, "buffer %p is not from our pool", buffer);
2029 GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, " "copying",
2031 /* sink->pool always exists (created in set_caps), but it may not
2032 * be active if upstream is not using it */
2033 if (!gst_buffer_pool_is_active (sink->pool) &&
2034 !gst_buffer_pool_set_active (sink->pool, TRUE))
2035 goto activate_failed;
2037 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
2038 if (ret != GST_FLOW_OK)
2041 /* the first time we acquire a buffer,
2042 * we need to attach a wl_buffer on it */
2043 wlbuffer = gst_buffer_get_wl_buffer (buffer);
2044 if (G_UNLIKELY (!wlbuffer)) {
2045 mem = gst_buffer_peek_memory (to_render, 0);
2046 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
2048 if (G_UNLIKELY (!wbuf))
2051 gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
2054 gst_buffer_map (buffer, &src, GST_MAP_READ);
2055 gst_buffer_fill (to_render, 0, src.data, src.size);
2056 gst_buffer_unmap (buffer, &src);
2059 /* drop double rendering */
2060 if (G_UNLIKELY (buffer == sink->last_buffer)) {
2061 GST_LOG_OBJECT (sink, "Buffer already being rendered");
2065 gst_buffer_replace (&sink->last_buffer, to_render);
2066 render_last_buffer (sink);
2068 if (buffer != to_render)
2069 gst_buffer_unref (to_render);
2073 #endif /* GST_WLSINK_ENHANCEMENT */
2075 #ifndef GST_WLSINK_ENHANCEMENT
2078 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
2079 ("Window has no size set"),
2080 ("Make sure you set the size after calling set_window_handle"));
2081 ret = GST_FLOW_ERROR;
2087 GST_WARNING_OBJECT (sink, "could not create buffer");
2092 GST_ERROR_OBJECT (sink, "could not create wl_buffer out of wl_shm memory");
2093 ret = GST_FLOW_ERROR;
2098 GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
2099 ret = GST_FLOW_ERROR;
2104 g_mutex_unlock (&sink->render_lock);
2110 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
2112 iface->set_window_handle = gst_wayland_sink_set_window_handle;
2113 iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
2114 iface->expose = gst_wayland_sink_expose;
2115 #ifdef GST_WLSINK_ENHANCEMENT /* use unique_id */
2116 iface->set_wl_window_wl_surface_id =
2117 gst_wayland_sink_set_wl_window_wl_surface_id;
2121 #ifdef GST_WLSINK_ENHANCEMENT
2124 gst_wayland_sink_set_wl_window_wl_surface_id (GstVideoOverlay * overlay,
2125 guintptr wl_surface_id)
2127 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
2129 g_return_if_fail (sink != NULL);
2131 if (sink->window != NULL) {
2132 GST_WARNING_OBJECT (sink, "changing window handle is not supported");
2135 g_mutex_lock (&sink->render_lock);
2136 g_clear_object (&sink->window);
2138 GST_LOG ("wl_surface_id %d %x", (int) wl_surface_id,
2139 (guintptr) wl_surface_id);
2141 if (wl_surface_id) {
2142 if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
2143 /* we cannot use our own display with an external window handle */
2144 if (G_UNLIKELY (sink->display->own_display)) {
2145 sink->display->wl_surface_id = (int) wl_surface_id;
2146 sink->window = gst_wl_window_new_in_surface (sink->display, NULL);
2149 GST_ERROR_OBJECT (sink, "Failed to find display handle, "
2150 "ignoring window handle");
2153 g_mutex_unlock (&sink->render_lock);
2159 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
2161 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
2162 struct wl_surface *surface = (struct wl_surface *) handle;
2165 g_return_if_fail (sink != NULL);
2167 #ifdef GST_WLSINK_ENHANCEMENT /* use unique_id */
2168 if (sink->window != NULL) {
2169 GST_WARNING_OBJECT (sink, "changing window handle is not supported");
2173 g_mutex_lock (&sink->render_lock);
2175 GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
2178 g_clear_object (&sink->window);
2181 if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
2182 /* we cannot use our own display with an external window handle */
2183 if (G_UNLIKELY (sink->display->own_display)) {
2184 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
2185 ("Application did not provide a wayland display handle"),
2186 ("Now waylandsink use internal display handle "
2187 "which is created ourselves. Consider providing a "
2188 "display handle from your application with GstContext"));
2189 sink->window = gst_wl_window_new_in_surface (sink->display, surface);
2191 sink->window = gst_wl_window_new_in_surface (sink->display, surface);
2194 GST_ERROR_OBJECT (sink, "Failed to find display handle, "
2195 "ignoring window handle");
2198 g_mutex_unlock (&sink->render_lock);
2202 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
2203 gint x, gint y, gint w, gint h)
2205 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
2208 g_return_if_fail (sink != NULL);
2210 g_mutex_lock (&sink->render_lock);
2211 if (!sink->window) {
2212 g_mutex_unlock (&sink->render_lock);
2213 GST_WARNING_OBJECT (sink,
2214 "set_render_rectangle called without window, ignoring");
2218 GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
2220 gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
2222 g_mutex_unlock (&sink->render_lock);
2226 gst_wayland_sink_expose (GstVideoOverlay * overlay)
2228 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
2231 g_return_if_fail (sink != NULL);
2233 GST_DEBUG_OBJECT (sink, "expose");
2235 g_mutex_lock (&sink->render_lock);
2236 if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
2237 GST_DEBUG_OBJECT (sink, "redrawing last buffer");
2238 render_last_buffer (sink);
2240 g_mutex_unlock (&sink->render_lock);
2244 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
2246 iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
2247 iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
2251 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
2253 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
2255 g_return_if_fail (sink != NULL);
2257 g_mutex_lock (&sink->render_lock);
2258 if (!sink->window || !sink->window->area_subsurface) {
2259 g_mutex_unlock (&sink->render_lock);
2260 GST_INFO_OBJECT (sink,
2261 "begin_geometry_change called without window, ignoring");
2265 wl_subsurface_set_sync (sink->window->area_subsurface);
2266 g_mutex_unlock (&sink->render_lock);
2270 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
2272 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
2274 g_return_if_fail (sink != NULL);
2276 g_mutex_lock (&sink->render_lock);
2277 if (!sink->window || !sink->window->area_subsurface) {
2278 g_mutex_unlock (&sink->render_lock);
2279 GST_INFO_OBJECT (sink,
2280 "end_geometry_change called without window, ignoring");
2284 wl_subsurface_set_desync (sink->window->area_subsurface);
2285 g_mutex_unlock (&sink->render_lock);
2289 plugin_init (GstPlugin * plugin)
2291 GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
2292 " wayland video sink");
2294 gst_wl_shm_allocator_register ();
2296 return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
2297 GST_TYPE_WAYLAND_SINK);
2300 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2303 "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,