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;
146 #ifdef GST_WLSINK_ENHANCEMENT
148 PROP_KEEP_CAMERA_PREVIEW,
151 PROP_DISPLAY_GEOMETRY_METHOD,
154 PROP_FOLLOW_PARENT_TRANSFORM,
173 GST_DEBUG_CATEGORY (gstwayland_debug);
174 #define GST_CAT_DEFAULT gstwayland_debug
176 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
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
185 "YUV9, YVU9, Y41B, I420, YV12, Y42B, v308 }"))
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);
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);
199 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
201 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
202 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
205 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
206 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
209 /* VideoOverlay interface */
210 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
212 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
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);
221 /* WaylandVideo interface */
222 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
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);
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));
239 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
241 GObjectClass *gobject_class;
242 GstElementClass *gstelement_class;
243 GstBaseSinkClass *gstbasesink_class;
246 gobject_class = (GObjectClass *) klass;
247 gstelement_class = (GstElementClass *) klass;
248 gstbasesink_class = (GstBaseSinkClass *) klass;
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);
254 gst_element_class_add_pad_template (gstelement_class,
255 gst_static_pad_template_get (&sink_template));
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>");
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);
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);
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));
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));
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));
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));
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));
309 g_object_class_install_property (gobject_class, PROP_FLIP,
310 g_param_spec_enum ("flip", "Display flip",
312 GST_TYPE_WAYLANDSINK_FLIP, DEF_DISPLAY_FLIP,
313 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
432 __write_rawdata (const char *file, const void *data, unsigned int size)
436 fp = fopen (file, "wb");
440 fwrite ((char *) data, sizeof (char), size, fp);
447 gst_wayland_sink_init (GstWaylandSink * sink)
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;
465 g_mutex_init (&sink->display_lock);
466 g_mutex_init (&sink->render_lock);
469 #ifdef GST_WLSINK_ENHANCEMENT
471 gst_wayland_sink_stop_video (GstWaylandSink * sink)
474 g_return_if_fail (sink != NULL);
475 gst_wl_window_render (sink->window, NULL, NULL);
479 gst_wayland_sink_check_use_gapless (GstWaylandSink * sink)
481 g_return_val_if_fail (sink != NULL, FALSE);
482 g_return_val_if_fail (sink->display != NULL, FALSE);
484 if (sink->got_costum_event && sink->USE_TBM
485 && sink->display->is_native_format)
492 gst_wayland_sink_need_to_make_flush_buffer (GstWaylandSink * sink)
494 g_return_val_if_fail (sink != NULL, FALSE);
495 g_return_val_if_fail (sink->display != NULL, FALSE);
497 if ((gst_wayland_sink_check_use_gapless (sink))
498 || sink->keep_camera_preview || sink->display->flush_request) {
499 sink->display->flush_request = TRUE;
506 gst_wayland_sink_update_last_buffer_geometry (GstWaylandSink * sink)
508 GstWlBuffer *wlbuffer;
509 gboolean no_render_buffer = FALSE;
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);
515 g_return_if_fail (wlbuffer != NULL);
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;
521 /* unused last buffer by compositor will receive buffer release event when attach */
522 no_render_buffer = TRUE;
525 GST_LOG ("gstbuffer(%p) ref_count(%d)", sink->last_buffer,
526 GST_OBJECT_REFCOUNT_VALUE (sink->last_buffer));
529 /* need to render last buffer, reuse current GstWlBuffer */
530 render_last_buffer (sink);
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 */
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");
543 gst_buffer_unref (wlbuffer->gstbuffer);
547 GST_LOG ("skip rendering");
550 GST_LOG ("gstbuffer(%p) ref_count(%d)", sink->last_buffer,
551 GST_OBJECT_REFCOUNT_VALUE (sink->last_buffer));
555 #ifdef USE_WL_FLUSH_BUFFER
557 gst_wayland_sink_make_flush_buffer (GstWlDisplay * display,
558 MMVideoBuffer * mm_video_buf)
560 GstWlFlushBuffer *flush_buffer = NULL;
566 g_return_val_if_fail (display != NULL, FALSE);
567 g_return_val_if_fail (mm_video_buf != NULL, FALSE);
569 flush_buffer = (GstWlFlushBuffer *) malloc (sizeof (GstWlFlushBuffer));
571 GST_ERROR ("GstWlFlushBuffer alloc faile");
574 memset (flush_buffer, 0x0, sizeof (GstWlFlushBuffer));
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);
580 for (i = 0; i < NV_BUF_PLANE_NUM; i++) {
581 if (mm_video_buf->handle.bo[i] != NULL) {
586 bo_size = tbm_bo_size (mm_video_buf->handle.bo[i]);
587 GST_LOG ("tbm bo size: %d", bo_size);
589 bo = tbm_bo_alloc (display->flush_tbm_bufmgr, bo_size, TBM_DEVICE_CPU);
591 GST_ERROR ("alloc tbm bo(size:%d) failed: %s", bo_size,
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]);
610 memcpy (dst.ptr, src.ptr, bo_size);
612 tbm_bo_unmap (mm_video_buf->handle.bo[i]);
616 display->flush_buffer = flush_buffer;
621 gst_wayland_sink_copy_mm_video_buf_info_to_flush (GstWlDisplay * display,
622 MMVideoBuffer * mm_video_buf)
625 g_return_val_if_fail (display != NULL, FALSE);
626 g_return_val_if_fail (mm_video_buf != NULL, FALSE);
629 ret = gst_wayland_sink_make_flush_buffer (display, mm_video_buf);
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]);
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];
644 memset (mm_video_buf, 0, sizeof (MMVideoBuffer));
651 gst_wayland_sink_add_mm_video_buf_info (GstWlDisplay * display,
652 MMVideoBuffer * mm_video_buf)
655 g_return_if_fail (display != NULL);
656 g_return_if_fail (mm_video_buf != NULL);
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];
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];
673 gst_wayland_sink_get_mm_video_buf_info (GstWaylandSink * sink,
676 GstWlDisplay *display;
678 GstMapInfo mem_info = GST_MAP_INFO_INIT;
679 MMVideoBuffer *mm_video_buf = NULL;
681 g_return_val_if_fail (sink != NULL, FALSE);
682 g_return_val_if_fail (buffer != NULL, FALSE);
685 display = sink->display;
686 g_return_val_if_fail (sink->display != NULL, FALSE);
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);
693 if (mm_video_buf == NULL) {
694 GST_WARNING ("mm_video_buf is NULL. Skip rendering");
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,
708 GST_ERROR ("cat not copy mm_video_buf info to flush");
714 gst_wayland_sink_add_mm_video_buf_info (display, mm_video_buf);
716 GST_ERROR ("Buffer type is not TBM");
723 gst_wayland_sink_render_flush_buffer (GstBaseSink * bsink)
725 GstWaylandSink *sink;
727 sink = GST_WAYLAND_SINK (bsink);
729 g_return_if_fail (sink != NULL);
730 g_return_if_fail (sink->last_buffer != NULL);
732 buffer = gst_buffer_copy_deep (sink->last_buffer);
734 gst_wayland_sink_render (bsink, buffer);
736 gst_buffer_unref (buffer);
740 gst_wayland_sink_gapless_render (GstBaseSink * bsink)
742 g_return_if_fail (bsink != NULL);
744 gst_wayland_sink_render_flush_buffer (bsink);
748 gst_wayland_sink_keep_camera_preview (GstBaseSink * bsink)
750 g_return_if_fail (bsink != NULL);
752 gst_wayland_sink_render_flush_buffer (bsink);
758 gst_wayland_sink_get_property (GObject * object,
759 guint prop_id, GValue * value, GParamSpec * pspec)
761 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
766 GST_OBJECT_LOCK (sink);
767 g_value_set_string (value, sink->display_name);
768 GST_OBJECT_UNLOCK (sink);
770 #ifdef GST_WLSINK_ENHANCEMENT
771 case PROP_KEEP_CAMERA_PREVIEW:
772 g_value_set_boolean (value, sink->keep_camera_preview);
775 g_value_set_boolean (value, sink->USE_TBM);
777 case PROP_ROTATE_ANGLE:
778 g_value_set_enum (value, sink->rotate_angle);
780 case PROP_DISPLAY_GEOMETRY_METHOD:
781 g_value_set_enum (value, sink->display_geometry_method);
784 g_value_set_enum (value, sink->flip);
787 g_value_set_boolean (value, sink->visible);
789 case PROP_FOLLOW_PARENT_TRANSFORM:
790 g_value_set_boolean (value, sink->follow_parent_transform);
793 g_value_set_uint (value, sink->crop_x);
796 g_value_set_uint (value, sink->crop_y);
798 case PROP_CROP_WIDTH:
799 g_value_set_uint (value, sink->crop_w);
801 case PROP_CROP_HEIGHT:
802 g_value_set_uint (value, sink->crop_h);
804 case PROP_RATIO_WIDTH:
805 g_value_set_double (value, sink->ratio_w);
807 case PROP_RATIO_HEIGHT:
808 g_value_set_double (value, sink->ratio_h);
810 case PROP_SCALE_WIDTH:
811 g_value_set_double (value, sink->scale_w);
813 case PROP_SCALE_HEIGHT:
814 g_value_set_double (value, sink->scale_h);
817 g_value_set_uint (value, sink->offset_x);
820 g_value_set_uint (value, sink->offset_y);
822 case PROP_OFFSET_WIDTH:
823 g_value_set_uint (value, sink->offset_w);
825 case PROP_OFFSET_HEIGHT:
826 g_value_set_uint (value, sink->offset_h);
828 case PROP_ALIGN_WIDTH:
829 g_value_set_double (value, sink->align_w);
831 case PROP_ALIGN_HEIGHT:
832 g_value_set_double (value, sink->align_h);
836 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
842 gst_wayland_sink_set_property (GObject * object,
843 guint prop_id, const GValue * value, GParamSpec * pspec)
845 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
847 g_mutex_lock (&sink->render_lock);
851 GST_OBJECT_LOCK (sink);
852 sink->display_name = g_value_dup_string (value);
853 GST_OBJECT_UNLOCK (sink);
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);
861 sink->USE_TBM = g_value_get_boolean (value);
862 GST_LOG ("1:USE TBM 0: USE SHM set(%d)", sink->USE_TBM);
864 case PROP_ROTATE_ANGLE:
865 if (sink->rotate_angle == g_value_get_enum (value))
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;
871 gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
874 case PROP_DISPLAY_GEOMETRY_METHOD:
875 if (sink->display_geometry_method == g_value_get_enum (value))
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;
882 gst_wl_window_set_destination_mode (sink->window,
883 sink->display_geometry_method);
887 if (sink->flip == g_value_get_enum (value))
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;
893 gst_wl_window_set_flip (sink->window, sink->flip);
897 if (sink->visible == g_value_get_boolean (value))
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) {
907 gst_wayland_sink_stop_video (sink);
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)
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;
920 gst_wl_window_set_destination_mode_follow_parent_transform
921 (sink->window, sink->follow_parent_transform);
925 if (sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
927 sink->crop_x = g_value_get_uint (value);
928 GST_WARNING_OBJECT (sink, "crop-x is set (%d)", sink->crop_x);
931 if (sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
933 sink->crop_y = g_value_get_uint (value);
934 GST_WARNING_OBJECT (sink, "crop-y is set (%d)", sink->crop_y);
936 case PROP_CROP_WIDTH:
937 if (sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
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) {
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)",
950 case PROP_CROP_HEIGHT:
951 if (sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
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)",
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);
967 case PROP_RATIO_WIDTH:
968 if (sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
970 sink->ratio_w = g_value_get_double (value);
971 GST_WARNING_OBJECT (sink, "ratio-w is set (%f)", sink->ratio_w);
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)
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;
981 gst_wl_window_set_destination_mode_ratio (sink->window, sink->ratio_w,
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)
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;
992 gst_wl_window_set_destination_mode_scale (sink->window, sink->ratio_w,
996 if (sink->offset_x == g_value_get_uint (value)
997 || sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
999 sink->offset_x = g_value_get_uint (value);
1000 GST_WARNING_OBJECT (sink, "offset-x is set (%d)", sink->offset_x);
1003 if (sink->offset_y == g_value_get_uint (value)
1004 || sink->display_geometry_method == DISP_GEO_METHOD_CUSTOM_ROI)
1006 sink->offset_y = g_value_get_uint (value);
1007 GST_WARNING_OBJECT (sink, "offset-y is set (%d)", sink->offset_y);
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)
1013 sink->offset_w = g_value_get_uint (value);
1014 GST_WARNING_OBJECT (sink, "offset-w is set (%d)", sink->offset_w);
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)
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;
1024 gst_wl_window_set_destination_mode_offset (sink->window, sink->offset_x,
1025 sink->offset_y, sink->offset_w, sink->offset_h);
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)
1031 sink->align_w = g_value_get_double (value);
1032 GST_WARNING_OBJECT (sink, "align_w is set (%f)", sink->align_w);
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)
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;
1042 gst_wl_window_set_destination_mode_align (sink->window, sink->align_w,
1048 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
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);
1058 g_mutex_unlock (&sink->render_lock);
1063 gst_wayland_sink_finalize (GObject * object)
1065 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
1067 GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
1069 if (sink->last_buffer)
1070 gst_buffer_unref (sink->last_buffer);
1072 g_object_unref (sink->display);
1074 g_object_unref (sink->window);
1076 gst_object_unref (sink->pool);
1078 if (sink->display_name)
1079 g_free (sink->display_name);
1081 g_mutex_clear (&sink->display_lock);
1082 g_mutex_clear (&sink->render_lock);
1084 G_OBJECT_CLASS (parent_class)->finalize (object);
1087 #ifdef GST_WLSINK_ENHANCEMENT
1089 gst_wayland_sink_event (GstBaseSink * bsink, GstEvent * event)
1091 GstWaylandSink *sink;
1092 const GstStructure *s;
1094 sink = GST_WAYLAND_SINK (bsink);
1096 switch (GST_EVENT_TYPE (event)) {
1098 GST_LOG ("get GST_EVENT_EOS event..state is %d", GST_STATE (sink));
1100 case GST_EVENT_CUSTOM_DOWNSTREAM:
1101 s = gst_event_get_structure (event);
1103 || !gst_structure_has_name (s, GST_APP_EVENT_FLUSH_BUFFER_NAME))
1106 GST_LOG ("get GST_EVENT_CUSTOM_DOWNSTREAM EVENT: %s..state is %d",
1107 gst_structure_get_name (s), GST_STATE (sink));
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;
1114 sink->got_costum_event = FALSE;
1119 return GST_BASE_SINK_CLASS (parent_class)->event (bsink, event);
1123 /* must be called with the display_lock */
1125 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
1126 GstContext * context)
1128 struct wl_display *display;
1129 GError *error = NULL;
1132 display = gst_wayland_display_handle_context_get_handle (context);
1133 sink->display = gst_wl_display_new_existing (display, FALSE, &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);
1141 #ifdef GST_WLSINK_ENHANCEMENT
1142 sink->display->USE_TBM = sink->USE_TBM;
1147 gst_wayland_sink_find_display (GstWaylandSink * sink)
1151 GstContext *context = NULL;
1152 GError *error = NULL;
1153 gboolean ret = TRUE;
1156 g_mutex_lock (&sink->display_lock);
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);
1165 gst_query_unref (query);
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);
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);
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);
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);
1191 #ifdef GST_WLSINK_ENHANCEMENT
1193 sink->display->USE_TBM = sink->USE_TBM;
1199 g_mutex_unlock (&sink->display_lock);
1204 static GstStateChangeReturn
1205 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
1207 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
1208 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
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;
1217 case GST_STATE_CHANGE_READY_TO_PAUSED:
1218 GST_LOG ("WAYLANDSINK TRANSITION: READY_TO_PAUSED");
1220 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1221 GST_LOG ("WAYLANDSINK TRANSITION: PAUSED_TO_PLAYING");
1227 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1228 if (ret == GST_STATE_CHANGE_FAILURE)
1231 switch (transition) {
1232 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1233 GST_LOG ("WAYLANDSINK TRANSITION: PLAYING_TO_PAUSED");
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) {
1240 if (!gst_wl_window_is_toplevel (sink->window)) {
1241 GstBaseSink *bsink = GST_BASE_SINK (element);
1242 gst_wayland_sink_keep_camera_preview (bsink);
1248 gst_buffer_replace (&sink->last_buffer, NULL);
1250 if (gst_wl_window_is_toplevel (sink->window)) {
1251 GST_DEBUG ("internal window");
1252 g_clear_object (&sink->window);
1254 /* remove buffer from surface, show nothing */
1255 GST_DEBUG ("external window");
1256 gst_wl_window_render (sink->window, NULL, NULL);
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)
1273 if (sink->display && !sink->window) { /* -> the window was toplevel */
1274 g_clear_object (&sink->display);
1276 g_mutex_unlock (&sink->display_lock);
1277 g_clear_object (&sink->pool);
1287 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
1289 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
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);
1298 GST_WARNING_OBJECT (element, "changing display handle is not supported");
1299 #ifdef GST_WLSINK_ENHANCEMENT
1300 g_mutex_unlock (&sink->display_lock);
1304 g_mutex_unlock (&sink->display_lock);
1307 if (GST_ELEMENT_CLASS (parent_class)->set_context)
1308 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
1312 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
1314 GstWaylandSink *sink;
1318 sink = GST_WAYLAND_SINK (bsink);
1320 caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
1322 g_mutex_lock (&sink->display_lock);
1324 if (sink->display) {
1325 GValue list = G_VALUE_INIT;
1326 GValue value = G_VALUE_INIT;
1329 #ifdef GST_WLSINK_ENHANCEMENT
1332 enum wl_shm_format fmt;
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;
1341 formats = sink->display->formats;
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
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);
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);
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);
1373 caps = gst_caps_make_writable (caps);
1374 gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
1376 GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
1377 g_value_unset (&value);
1378 g_value_unset (&list);
1382 g_mutex_unlock (&sink->display_lock);
1385 GstCaps *intersection;
1388 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
1389 gst_caps_unref (caps);
1390 caps = intersection;
1397 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
1399 GstWaylandSink *sink;
1400 GstBufferPool *newpool;
1402 #ifdef GST_WLSINK_ENHANCEMENT
1403 uint32_t tbm_format = -1;
1405 enum wl_shm_format format = -1;
1409 GstStructure *structure;
1410 GstWlShmAllocator *self = NULL;
1414 sink = GST_WAYLAND_SINK (bsink);
1416 GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
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) {
1424 gst_video_format_to_wl_tbm_format (GST_VIDEO_INFO_FORMAT (&info));
1425 if ((gint) tbm_format == -1)
1426 goto invalid_format;
1428 format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (&info));
1429 if ((gint) format == -1)
1430 goto invalid_format;
1432 #else /* open source */
1433 format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (&info));
1435 if ((gint) format == -1)
1436 goto invalid_format;
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)
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)
1456 #else /* open source */
1458 formats = sink->display->formats;
1459 for (i = 0; i < formats->len; i++) {
1460 if (g_array_index (formats, uint32_t, i) == format)
1464 if (i >= formats->len)
1465 goto unsupported_format;
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;
1473 /* store the video info */
1474 sink->video_info = info;
1475 sink->video_info_changed = TRUE;
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 ();
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))
1492 /* store the video info */
1493 sink->video_info = info;
1494 sink->video_info_changed = TRUE;
1496 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1497 gst_object_unref (newpool);
1499 } else { /* USE SHM */
1501 self = GST_WL_SHM_ALLOCATOR (gst_wl_shm_allocator_get ());
1502 self->display = sink->display;
1504 /* create a new pool for the new configuration */
1505 newpool = gst_video_buffer_pool_new ();
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))
1516 /* store the video info */
1517 sink->video_info = info;
1518 sink->video_info_changed = TRUE;
1520 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1521 gst_object_unref (newpool);
1523 #ifdef GST_WLSINK_ENHANCEMENT
1525 gst_wayland_sink_update_window_geometry (sink);
1528 #else /*open source */
1529 /* create a new pool for the new configuration */
1530 newpool = gst_video_buffer_pool_new ();
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))
1541 /* store the video info */
1542 sink->video_info = info;
1543 sink->video_info_changed = TRUE;
1545 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1546 gst_object_unref (newpool);
1553 GST_DEBUG_OBJECT (sink,
1554 "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
1559 #ifdef GST_WLSINK_ENHANCEMENT
1561 GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
1562 gst_wl_tbm_format_to_string (tbm_format));
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));
1574 GST_DEBUG_OBJECT (sink, "Failed to create new pool");
1579 GST_DEBUG_OBJECT (bsink, "failed setting config");
1580 gst_object_unref (newpool);
1586 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
1588 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
1589 GstStructure *config;
1590 guint size, min_bufs, max_bufs;
1591 #ifdef GST_WLSINK_ENHANCEMENT
1596 if (sink->USE_TBM) {
1597 if (sink->display->is_native_format == TRUE)
1600 gst_query_parse_allocation (query, &caps, &need_pool);
1603 GST_DEBUG_OBJECT (bsink, "no caps specified");
1608 config = gst_buffer_pool_get_config (sink->pool);
1609 gst_buffer_pool_config_get_params (config, NULL, &size, &min_bufs, &max_bufs);
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);
1616 gst_structure_free (config);
1621 static GstFlowReturn
1622 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
1625 GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
1626 return gst_wayland_sink_render (bsink, buffer);
1630 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
1632 GstWaylandSink *sink = data;
1635 GST_LOG ("frame_redraw_cb");
1637 g_atomic_int_set (&sink->redraw_pending, FALSE);
1638 GST_INFO ("wl_callback_destroy (wl_callback@%p)", callback);
1639 wl_callback_destroy (callback);
1642 static const struct wl_callback_listener frame_callback_listener = {
1643 frame_redraw_callback
1646 #ifdef GST_WLSINK_ENHANCEMENT
1648 gst_wayland_sink_update_window_geometry (GstWaylandSink * sink)
1651 g_return_if_fail (sink != NULL);
1652 g_return_if_fail (sink->window != NULL);
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);
1661 if (sink->crop_w == 0 && sink->crop_h == 0) {
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;
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,
1671 gst_wl_window_set_destination_mode_scale (sink->window, sink->scale_w,
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,
1680 /* must be called with the render lock */
1682 render_last_buffer (GstWaylandSink * sink)
1684 GstWlBuffer *wlbuffer;
1685 const GstVideoInfo *info = NULL;
1686 struct wl_surface *surface;
1687 struct wl_callback *callback;
1689 #ifdef GST_WLSINK_ENHANCEMENT
1690 g_return_if_fail (sink->last_buffer != NULL);
1691 g_return_if_fail (sink->window != NULL);
1694 wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
1695 surface = gst_wl_window_get_wl_surface (sink->window);
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,
1702 /* frame_callback_listener is called when wayland-client finish rendering the wl_buffer */
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);
1708 if (G_UNLIKELY (sink->video_info_changed)) {
1709 #ifdef GST_WLSINK_ENHANCEMENT
1710 gst_wl_window_set_video_info_change (sink->window, TRUE);
1712 info = &sink->video_info;
1713 sink->video_info_changed = FALSE;
1715 gst_wl_window_render (sink->window, wlbuffer, info);
1718 static GstFlowReturn
1719 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
1721 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
1722 GstBuffer *to_render;
1723 GstWlBuffer *wlbuffer;
1724 GstFlowReturn ret = GST_FLOW_OK;
1727 g_mutex_lock (&sink->render_lock);
1729 GST_LOG_OBJECT (sink, "render gstbuffer %p, ref_count(%d)", buffer,
1730 GST_OBJECT_REFCOUNT_VALUE (buffer));
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);
1739 if (!sink->window) {
1740 /* if we were not provided a window, create one ourselves */
1742 gst_wl_window_new_toplevel (sink->display, &sink->video_info);
1744 #ifdef GST_WLSINK_ENHANCEMENT
1745 gst_wayland_sink_update_window_geometry (sink);
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))
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;
1758 #ifdef GST_WLSINK_ENHANCEMENT
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);
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);
1774 data = mem_info.data;
1776 char file_name[128];
1778 sprintf (file_name, "/home/owner/DUMP/_WLSINK_OUT_DUMP_%2.2d.dump",
1780 ret = __write_rawdata (file_name, data, size);
1782 GST_ERROR ("_write_rawdata() failed");
1784 GST_LOG ("DUMP IMAGE %d, size (%d)", dump__cnt, size);
1785 gst_memory_unmap (mem, &mem_info);
1789 struct wl_buffer *wbuf = NULL;
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)) {
1795 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1798 gst_buffer_add_wl_buffer (buffer, wbuf, sink->display); //careat GstWlBuffer and add gstbuffer, wlbuffer, display and etc
1801 } else { //buffer is not from our pool and have not wl_buffer
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
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",
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;
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) {
1822 gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1824 if (G_UNLIKELY (!wbuf))
1826 gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
1828 } else if (sink->USE_TBM && !sink->display->is_native_format) {
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;
1836 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1837 if (ret != GST_FLOW_OK)
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");
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,
1852 if (G_UNLIKELY (!wbuf))
1855 wlbuffer = gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
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)
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,
1877 if (G_UNLIKELY (!wbuf))
1880 gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
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);
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");
1900 GST_LOG_OBJECT (sink, "replace last_buffer: (%p) -> (%p)",
1901 sink->last_buffer, buffer);
1902 gst_buffer_replace (&sink->last_buffer, buffer);
1904 GST_LOG_OBJECT (sink, "after gst_buffer_replace buffer %p, ref_count(%d)",
1905 buffer, GST_OBJECT_REFCOUNT_VALUE (buffer));
1907 if (sink->visible) {
1908 render_last_buffer (sink);
1910 GST_LOG ("skip rendering");
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");
1921 gst_buffer_replace (&sink->last_buffer, to_render);
1923 if (sink->visible) {
1924 render_last_buffer (sink);
1926 GST_LOG ("skip rendering");
1929 if (buffer != to_render)
1930 gst_buffer_unref (to_render);
1935 #else /* open source */
1937 wlbuffer = gst_buffer_get_wl_buffer (buffer);
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",
1943 GST_LOG ("wl_buffer (%p)", wlbuffer->wlbuffer);
1948 struct wl_buffer *wbuf = NULL;
1950 GST_LOG_OBJECT (sink,
1951 "buffer %p does not have a wl_buffer from our " "display, creating it",
1953 mem = gst_buffer_peek_memory (buffer, 0);
1954 if (gst_is_wl_shm_memory (mem)) {
1956 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1960 gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
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
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",
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;
1978 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1979 if (ret != GST_FLOW_OK)
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,
1989 if (G_UNLIKELY (!wbuf))
1992 gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
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);
2000 /* drop double rendering */
2001 if (G_UNLIKELY (buffer == sink->last_buffer)) {
2002 GST_LOG_OBJECT (sink, "Buffer already being rendered");
2006 gst_buffer_replace (&sink->last_buffer, to_render);
2007 render_last_buffer (sink);
2009 if (buffer != to_render)
2010 gst_buffer_unref (to_render);
2014 #endif /* GST_WLSINK_ENHANCEMENT */
2016 #ifndef GST_WLSINK_ENHANCEMENT
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;
2028 GST_WARNING_OBJECT (sink, "could not create buffer");
2033 GST_ERROR_OBJECT (sink, "could not create wl_buffer out of wl_shm memory");
2034 ret = GST_FLOW_ERROR;
2039 GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
2040 ret = GST_FLOW_ERROR;
2045 g_mutex_unlock (&sink->render_lock);
2051 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
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;
2062 #ifdef GST_WLSINK_ENHANCEMENT
2065 gst_wayland_sink_set_wl_window_wl_surface_id (GstVideoOverlay * overlay,
2066 guintptr wl_surface_id)
2068 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
2070 g_return_if_fail (sink != NULL);
2072 if (sink->window != NULL) {
2073 GST_WARNING_OBJECT (sink, "changing window handle is not supported");
2076 g_mutex_lock (&sink->render_lock);
2077 g_clear_object (&sink->window);
2079 GST_LOG ("wl_surface_id %d %x", (int) wl_surface_id,
2080 (guintptr) wl_surface_id);
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);
2090 GST_ERROR_OBJECT (sink, "Failed to find display handle, "
2091 "ignoring window handle");
2094 g_mutex_unlock (&sink->render_lock);
2100 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
2102 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
2103 struct wl_surface *surface = (struct wl_surface *) handle;
2106 g_return_if_fail (sink != NULL);
2108 #ifdef GST_WLSINK_ENHANCEMENT /* use unique_id */
2109 if (sink->window != NULL) {
2110 GST_WARNING_OBJECT (sink, "changing window handle is not supported");
2114 g_mutex_lock (&sink->render_lock);
2116 GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
2119 g_clear_object (&sink->window);
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);
2132 sink->window = gst_wl_window_new_in_surface (sink->display, surface);
2135 GST_ERROR_OBJECT (sink, "Failed to find display handle, "
2136 "ignoring window handle");
2139 g_mutex_unlock (&sink->render_lock);
2143 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
2144 gint x, gint y, gint w, gint h)
2146 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
2149 g_return_if_fail (sink != NULL);
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");
2159 GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
2161 gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
2163 g_mutex_unlock (&sink->render_lock);
2167 gst_wayland_sink_expose (GstVideoOverlay * overlay)
2169 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
2172 g_return_if_fail (sink != NULL);
2174 GST_DEBUG_OBJECT (sink, "expose");
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);
2181 g_mutex_unlock (&sink->render_lock);
2185 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
2187 iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
2188 iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
2192 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
2194 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
2196 g_return_if_fail (sink != NULL);
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");
2206 wl_subsurface_set_sync (sink->window->area_subsurface);
2207 g_mutex_unlock (&sink->render_lock);
2211 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
2213 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
2215 g_return_if_fail (sink != NULL);
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");
2225 wl_subsurface_set_desync (sink->window->area_subsurface);
2226 g_mutex_unlock (&sink->render_lock);
2230 plugin_init (GstPlugin * plugin)
2232 GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
2233 " wayland video sink");
2235 gst_wl_shm_allocator_register ();
2237 return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
2238 GST_TYPE_WAYLAND_SINK);
2241 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2244 "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,