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_TYPE_WAYLANDSINK_DISPLAY_GEOMETRY_METHOD (gst_waylandsink_display_geometry_method_get_type())
63 #define GST_TYPE_WAYLANDSINK_ROTATE_ANGLE (gst_waylandsink_rotate_angle_get_type())
64 #define GST_TYPE_WAYLANDSINK_FLIP (gst_waylandsink_flip_get_type())
67 gst_waylandsink_rotate_angle_get_type (void)
69 static GType waylandsink_rotate_angle_type = 0;
70 static const GEnumValue rotate_angle_type[] = {
71 {0, "No rotate", "DEGREE_0"},
72 {1, "Rotate 90 degree", "DEGREE_90"},
73 {2, "Rotate 180 degree", "DEGREE_180"},
74 {3, "Rotate 270 degree", "DEGREE_270"},
78 if (!waylandsink_rotate_angle_type) {
79 waylandsink_rotate_angle_type =
80 g_enum_register_static ("GstWaylandSinkRotateAngleType",
84 return waylandsink_rotate_angle_type;
89 gst_waylandsink_display_geometry_method_get_type (void)
91 static GType waylandsink_display_geometry_method_type = 0;
92 static const GEnumValue display_geometry_method_type[] = {
93 {0, "Letter box", "LETTER_BOX"},
94 {1, "Origin size", "ORIGIN_SIZE"},
95 {2, "Full-screen", "FULL_SCREEN"},
96 {3, "Cropped full-screen", "CROPPED_FULL_SCREEN"},
97 {4, "Origin size(if screen size is larger than video size(width/height)) or Letter box(if video size(width/height) is larger than screen size)", "ORIGIN_SIZE_OR_LETTER_BOX"},
101 if (!waylandsink_display_geometry_method_type) {
102 waylandsink_display_geometry_method_type =
103 g_enum_register_static ("GstWaylandSinkDisplayGeometryMethodType",
104 display_geometry_method_type);
106 return waylandsink_display_geometry_method_type;
110 gst_waylandsink_flip_get_type (void)
112 static GType waylandsink_flip_type = 0;
113 static const GEnumValue flip_type[] = {
114 {FLIP_NONE, "Flip NONE", "FLIP_NONE"},
115 {FLIP_HORIZONTAL, "Flip HORIZONTAL", "FLIP_HORIZONTAL"},
116 {FLIP_VERTICAL, "Flip VERTICAL", "FLIP_VERTICAL"},
117 {FLIP_BOTH, "Flip BOTH", "FLIP_BOTH"},
118 {FLIP_NUM, NULL, NULL},
121 if (!waylandsink_flip_type) {
122 waylandsink_flip_type =
123 g_enum_register_static ("GstWaylandSinkFlipType", flip_type);
126 return waylandsink_flip_type;
143 #ifdef GST_WLSINK_ENHANCEMENT
146 PROP_DISPLAY_GEOMETRY_METHOD,
153 GST_DEBUG_CATEGORY (gstwayland_debug);
154 #define GST_CAT_DEFAULT gstwayland_debug
156 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
159 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
160 ("{ BGRx, BGRA, RGBx, xBGR, xRGB, RGBA, ABGR, ARGB, RGB, BGR, "
161 "RGB16, BGR16, YUY2, YVYU, UYVY, AYUV, NV12, NV21, NV16, "
162 #ifdef GST_WLSINK_ENHANCEMENT
165 "YUV9, YVU9, Y41B, I420, YV12, Y42B, v308 }"))
168 static void gst_wayland_sink_get_property (GObject * object,
169 guint prop_id, GValue * value, GParamSpec * pspec);
170 static void gst_wayland_sink_set_property (GObject * object,
171 guint prop_id, const GValue * value, GParamSpec * pspec);
172 static void gst_wayland_sink_finalize (GObject * object);
174 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
175 GstStateChange transition);
176 static void gst_wayland_sink_set_context (GstElement * element,
177 GstContext * context);
179 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
181 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
182 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
185 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
186 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
189 /* VideoOverlay interface */
190 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
192 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
195 gst_wayland_sink_set_wl_window_wl_surface_id (GstVideoOverlay * overlay,
196 guintptr wl_surface_id);
197 static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
198 gint x, gint y, gint w, gint h);
199 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
201 /* WaylandVideo interface */
202 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
204 static void gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video);
205 static void gst_wayland_sink_end_geometry_change (GstWaylandVideo * video);
206 #ifdef GST_WLSINK_ENHANCEMENT
207 static void gst_wayland_sink_update_window_geometry (GstWaylandSink * sink);
208 static void render_last_buffer (GstWaylandSink * sink);
210 #define gst_wayland_sink_parent_class parent_class
211 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
212 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
213 gst_wayland_sink_videooverlay_init)
214 G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
215 gst_wayland_sink_waylandvideo_init));
218 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
220 GObjectClass *gobject_class;
221 GstElementClass *gstelement_class;
222 GstBaseSinkClass *gstbasesink_class;
225 gobject_class = (GObjectClass *) klass;
226 gstelement_class = (GstElementClass *) klass;
227 gstbasesink_class = (GstBaseSinkClass *) klass;
229 gobject_class->set_property = gst_wayland_sink_set_property;
230 gobject_class->get_property = gst_wayland_sink_get_property;
231 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
233 gst_element_class_add_pad_template (gstelement_class,
234 gst_static_pad_template_get (&sink_template));
236 gst_element_class_set_static_metadata (gstelement_class,
237 "wayland video sink", "Sink/Video",
238 "Output to wayland surface",
239 "Sreerenj Balachandran <sreerenj.balachandran@intel.com>, "
240 "George Kiagiadakis <george.kiagiadakis@collabora.com>");
242 gstelement_class->change_state =
243 GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
244 gstelement_class->set_context =
245 GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
247 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
248 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
249 gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
250 gstbasesink_class->propose_allocation =
251 GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
252 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
254 g_object_class_install_property (gobject_class, PROP_DISPLAY,
255 g_param_spec_string ("display", "Wayland Display name", "Wayland "
256 "display name to connect to, if not supplied via the GstContext",
257 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
258 #ifdef GST_WLSINK_ENHANCEMENT
259 g_object_class_install_property (gobject_class, PROP_USE_TBM,
260 g_param_spec_boolean ("use-tbm",
261 "Use Tizen Buffer Memory insted of Shared memory",
262 "When enabled, Memory is alloced by TBM insted of SHM ", TRUE,
263 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
265 g_object_class_install_property (gobject_class, PROP_ROTATE_ANGLE,
266 g_param_spec_enum ("rotate", "Rotate angle",
267 "Rotate angle of display output",
268 GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
269 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
271 g_object_class_install_property (gobject_class, PROP_DISPLAY_GEOMETRY_METHOD,
272 g_param_spec_enum ("display-geometry-method", "Display geometry method",
273 "Geometrical method for display",
274 GST_TYPE_WAYLANDSINK_DISPLAY_GEOMETRY_METHOD,
275 DEF_DISPLAY_GEOMETRY_METHOD,
276 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
278 g_object_class_install_property (gobject_class, PROP_ORIENTATION,
279 g_param_spec_enum ("orientation",
280 "Orientation information used for ROI/ZOOM",
281 "Orientation information for display",
282 GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
283 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
285 g_object_class_install_property (gobject_class, PROP_FLIP,
286 g_param_spec_enum ("flip", "Display flip",
288 GST_TYPE_WAYLANDSINK_FLIP, DEF_DISPLAY_FLIP,
289 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296 __write_rawdata (const char *file, const void *data, unsigned int size)
300 fp = fopen (file, "wb");
304 fwrite ((char *) data, sizeof (char), size, fp);
311 gst_wayland_sink_init (GstWaylandSink * sink)
314 #ifdef GST_WLSINK_ENHANCEMENT
315 sink->USE_TBM = TRUE;
316 sink->display_geometry_method = DEF_DISPLAY_GEOMETRY_METHOD;
317 sink->flip = DEF_DISPLAY_FLIP;
318 sink->rotate_angle = DEGREE_0;
319 sink->orientation = DEGREE_0;
321 g_mutex_init (&sink->display_lock);
322 g_mutex_init (&sink->render_lock);
325 #ifdef GST_WLSINK_ENHANCEMENT
327 gst_wayland_sink_update_last_buffer_geometry (GstWaylandSink * sink)
329 GstWlBuffer *wlbuffer;
331 g_return_if_fail (sink != NULL);
333 wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
334 wlbuffer->used_by_compositor = FALSE;
336 /*need to render last buffer */
337 /* reuse current GstWlBuffer */
338 render_last_buffer (sink);
339 /* ref count is incresed in gst_wl_buffer_attach() of render_last_buffer(),
340 to call gst_wl_buffer_finalize(), we need to decrease buffer ref count */
341 gst_buffer_unref (wlbuffer->gstbuffer);
343 #ifdef USE_WL_FLUSH_BUFFER
345 gst_wayland_sink_make_flush_buffer (GstWlDisplay * display, MMVideoBuffer * mm_video_buf)
347 GstWlFlushBuffer *flush_buffer = NULL;
353 g_return_val_if_fail (display != NULL, FALSE);
354 g_return_val_if_fail (mm_video_buf != NULL, FALSE);
356 flush_buffer = (GstWlFlushBuffer *)malloc(sizeof(GstWlFlushBuffer));
358 GST_ERROR ("GstWlFlushBuffer alloc faile");
361 memset (flush_buffer, 0x0, sizeof(GstWlFlushBuffer));
363 display->flush_tbm_bufmgr = wayland_tbm_client_get_bufmgr (display->tbm_client);
364 g_return_if_fail (display->flush_tbm_bufmgr != NULL);
366 for (i=0; i<NV_BUF_PLANE_NUM; i++){
367 if (mm_video_buf->handle.bo[i] != NULL){
372 bo_size = tbm_bo_size (mm_video_buf->handle.bo[i]);
373 GST_LOG ("tbm bo size: %d", bo_size);
375 bo = tbm_bo_alloc (display->flush_tbm_bufmgr, bo_size, TBM_DEVICE_CPU);
377 GST_ERROR ("alloc tbm bo(size:%d) failed: %s", bo_size, strerror(errno));
380 GST_INFO ("flush buffer tbm_bo =(%p)", bo);
381 flush_buffer->bo[i] = bo;
382 /* get virtual address */
383 src.ptr = dst.ptr = NULL;
384 /* bo map, we can use tbm_bo_map too. */
385 src = tbm_bo_get_handle (mm_video_buf->handle.bo[i], TBM_DEVICE_CPU);
386 dst = tbm_bo_get_handle (bo, TBM_DEVICE_CPU);
387 if (!src.ptr || !dst.ptr) {
388 GST_ERROR ("get tbm bo handle failed src(%p) dst(%p): %s", src.ptr, dst.ptr, strerror (errno));
389 tbm_bo_unref (mm_video_buf->handle.bo[i]);
394 memcpy (dst.ptr, src.ptr, bo_size);
396 tbm_bo_unmap (mm_video_buf->handle.bo[i]);
400 display->flush_buffer = flush_buffer;
405 gst_wayland_sink_copy_mm_video_buf_info_to_flush (GstWlDisplay * display, MMVideoBuffer * mm_video_buf)
408 g_return_val_if_fail (display != NULL, FALSE);
409 g_return_val_if_fail (mm_video_buf != NULL, FALSE);
412 ret = gst_wayland_sink_make_flush_buffer(display, mm_video_buf);
415 for (i = 0; i < NV_BUF_PLANE_NUM; i++) {
416 if (display->flush_buffer->bo[i] != NULL) {
417 display->bo[i] = display->flush_buffer->bo[i];
418 GST_LOG("bo %p", display->bo[i]);
422 display->plane_size[i] = mm_video_buf->size[i];
423 display->stride_width[i] = mm_video_buf->stride_width[i];
424 display->stride_height[i] = mm_video_buf->stride_height[i];
425 display->native_video_size += display->plane_size[i];
433 gst_wayland_sink_add_mm_video_buf_info (GstWlDisplay * display, MMVideoBuffer * mm_video_buf)
436 g_return_if_fail (display != NULL);
437 g_return_if_fail (mm_video_buf != NULL);
440 for (i = 0; i < NV_BUF_PLANE_NUM; i++) {
441 if (mm_video_buf->handle.bo[i] != NULL) {
442 display->bo[i] = mm_video_buf->handle.bo[i];
446 display->plane_size[i] = mm_video_buf->size[i];
447 display->stride_width[i] = mm_video_buf->stride_width[i];
448 display->stride_height[i] = mm_video_buf->stride_height[i];
449 display->native_video_size += display->plane_size[i];
454 gst_wayland_sink_get_mm_video_buf_info(GstWlDisplay * display, GstBuffer * buffer)
457 GstMapInfo mem_info = GST_MAP_INFO_INIT;
458 MMVideoBuffer *mm_video_buf = NULL;
460 g_return_val_if_fail (display != NULL, FALSE);
461 g_return_val_if_fail (buffer != NULL, FALSE);
465 mem = gst_buffer_peek_memory (buffer, 1);
466 gst_memory_map (mem, &mem_info, GST_MAP_READ);
467 mm_video_buf = (MMVideoBuffer *) mem_info.data;
468 gst_memory_unmap (mem, &mem_info);
470 if (mm_video_buf == NULL) {
471 GST_WARNING ("mm_video_buf is NULL. Skip rendering");
474 /* assign mm_video_buf info */
475 if (mm_video_buf->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
476 GST_DEBUG ("TBM bo %p %p %p", mm_video_buf->handle.bo[0],
477 mm_video_buf->handle.bo[1], mm_video_buf->handle.bo[2]);
478 display->native_video_size = 0;
479 display->flush_request = mm_video_buf->flush_request;
480 GST_DEBUG ("flush_request value is %d",display->flush_request);
481 #ifdef USE_WL_FLUSH_BUFFER
482 if (display->flush_request) {
483 if(!gst_wayland_sink_copy_mm_video_buf_info_to_flush(display, mm_video_buf)){
484 GST_ERROR("cat not copy mm_video_buf info to flush");
490 gst_wayland_sink_add_mm_video_buf_info(display, mm_video_buf);
492 GST_ERROR ("Buffer type is not TBM");
500 gst_wayland_sink_get_property (GObject * object,
501 guint prop_id, GValue * value, GParamSpec * pspec)
503 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
508 GST_OBJECT_LOCK (sink);
509 g_value_set_string (value, sink->display_name);
510 GST_OBJECT_UNLOCK (sink);
512 #ifdef GST_WLSINK_ENHANCEMENT
514 g_value_set_boolean (value, sink->USE_TBM);
516 case PROP_ROTATE_ANGLE:
517 g_value_set_enum (value, sink->rotate_angle);
519 case PROP_DISPLAY_GEOMETRY_METHOD:
520 g_value_set_enum (value, sink->display_geometry_method);
522 case PROP_ORIENTATION:
523 g_value_set_enum (value, sink->orientation);
526 g_value_set_enum (value, sink->flip);
530 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
536 gst_wayland_sink_set_property (GObject * object,
537 guint prop_id, const GValue * value, GParamSpec * pspec)
539 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
541 g_mutex_lock (&sink->render_lock);
545 GST_OBJECT_LOCK (sink);
546 sink->display_name = g_value_dup_string (value);
547 GST_OBJECT_UNLOCK (sink);
549 #ifdef GST_WLSINK_ENHANCEMENT
551 sink->USE_TBM = g_value_get_boolean (value);
552 GST_LOG ("1:USE TBM 0: USE SHM set(%d)", sink->USE_TBM);
555 case PROP_ROTATE_ANGLE:
556 if (sink->rotate_angle == g_value_get_enum (value))
558 sink->rotate_angle = g_value_get_enum (value);
559 GST_WARNING_OBJECT (sink, "Rotate angle is set (%d)", sink->rotate_angle);
560 sink->video_info_changed = TRUE;
562 gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
566 case PROP_DISPLAY_GEOMETRY_METHOD:
567 if (sink->display_geometry_method == g_value_get_enum (value))
569 sink->display_geometry_method = g_value_get_enum (value);
570 GST_WARNING_OBJECT (sink, "Display geometry method is set (%d)",
571 sink->display_geometry_method);
572 sink->video_info_changed = TRUE;
574 gst_wl_window_set_disp_geo_method (sink->window,
575 sink->display_geometry_method);
579 case PROP_ORIENTATION:
580 if (sink->orientation == g_value_get_enum (value))
582 sink->orientation = g_value_get_enum (value);
583 GST_WARNING_OBJECT (sink, "Orientation is set (%d)", sink->orientation);
584 sink->video_info_changed = TRUE;
586 gst_wl_window_set_orientation (sink->window, sink->orientation);
591 if (sink->flip == g_value_get_enum (value))
593 sink->flip = g_value_get_enum (value);
594 GST_WARNING_OBJECT (sink, "flip is set (%d)", sink->flip);
595 sink->video_info_changed = TRUE;
597 gst_wl_window_set_flip (sink->window, sink->flip);
602 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
605 if (sink->video_info_changed && sink->window
606 && GST_STATE (sink) == GST_STATE_PAUSED) {
607 gst_wayland_sink_update_last_buffer_geometry (sink);
609 g_mutex_unlock (&sink->render_lock);
614 gst_wayland_sink_finalize (GObject * object)
616 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
618 GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
620 if (sink->last_buffer)
621 gst_buffer_unref (sink->last_buffer);
623 g_object_unref (sink->display);
625 g_object_unref (sink->window);
627 gst_object_unref (sink->pool);
629 if (sink->display_name)
630 g_free (sink->display_name);
632 g_mutex_clear (&sink->display_lock);
633 g_mutex_clear (&sink->render_lock);
635 G_OBJECT_CLASS (parent_class)->finalize (object);
638 /* must be called with the display_lock */
640 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
641 GstContext * context)
643 struct wl_display *display;
644 GError *error = NULL;
647 display = gst_wayland_display_handle_context_get_handle (context);
648 sink->display = gst_wl_display_new_existing (display, FALSE, &error);
651 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
652 ("Could not set display handle"),
653 ("Failed to use the external wayland display: '%s'", error->message));
654 g_error_free (error);
656 #ifdef GST_WLSINK_ENHANCEMENT
657 sink->display->USE_TBM = sink->USE_TBM;
662 gst_wayland_sink_find_display (GstWaylandSink * sink)
666 GstContext *context = NULL;
667 GError *error = NULL;
671 g_mutex_lock (&sink->display_lock);
673 if (!sink->display) {
674 /* first query upstream for the needed display handle */
675 query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
676 if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
677 gst_query_parse_context (query, &context);
678 gst_wayland_sink_set_display_from_context (sink, context);
680 gst_query_unref (query);
682 if (G_LIKELY (!sink->display)) {
683 /* now ask the application to set the display handle */
684 msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
685 GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
687 g_mutex_unlock (&sink->display_lock);
688 gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
689 /* at this point we expect gst_wayland_sink_set_context
690 * to get called and fill sink->display */
691 g_mutex_lock (&sink->display_lock);
693 if (!sink->display) {
694 /* if the application didn't set a display, let's create it ourselves */
695 GST_OBJECT_LOCK (sink);
696 sink->display = gst_wl_display_new (sink->display_name, &error);
697 GST_OBJECT_UNLOCK (sink);
700 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
701 ("Could not initialise Wayland output"),
702 ("Failed to create GstWlDisplay: '%s'", error->message));
703 g_error_free (error);
706 #ifdef GST_WLSINK_ENHANCEMENT
707 sink->display->USE_TBM = sink->USE_TBM;
713 g_mutex_unlock (&sink->display_lock);
718 static GstStateChangeReturn
719 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
721 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
722 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
725 switch (transition) {
726 case GST_STATE_CHANGE_NULL_TO_READY:
727 if (!gst_wayland_sink_find_display (sink))
728 return GST_STATE_CHANGE_FAILURE;
734 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
735 if (ret == GST_STATE_CHANGE_FAILURE)
738 switch (transition) {
739 case GST_STATE_CHANGE_PAUSED_TO_READY:
740 gst_buffer_replace (&sink->last_buffer, NULL);
742 if (gst_wl_window_is_toplevel (sink->window)) {
743 g_clear_object (&sink->window);
745 /* remove buffer from surface, show nothing */
746 #ifdef USE_WL_FLUSH_BUFFER
747 sink->display->flush_request = 0;
749 gst_wl_window_render (sink->window, NULL, NULL);
753 case GST_STATE_CHANGE_READY_TO_NULL:
754 g_mutex_lock (&sink->display_lock);
755 /* If we had a toplevel window, we most likely have our own connection
756 * to the display too, and it is a good idea to disconnect and allow
757 * potentially the application to embed us with GstVideoOverlay
758 * (which requires to re-use the same display connection as the parent
759 * surface). If we didn't have a toplevel window, then the display
760 * connection that we have is definitely shared with the application
761 * and it's better to keep it around (together with the window handle)
762 * to avoid requesting them again from the application if/when we are
763 * restarted (GstVideoOverlay behaves like that in other sinks)
765 if (sink->display && !sink->window) { /* -> the window was toplevel */
766 g_clear_object (&sink->display);
768 g_mutex_unlock (&sink->display_lock);
769 g_clear_object (&sink->pool);
779 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
781 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
784 if (gst_context_has_context_type (context,
785 GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
786 g_mutex_lock (&sink->display_lock);
787 if (G_LIKELY (!sink->display))
788 gst_wayland_sink_set_display_from_context (sink, context);
790 GST_WARNING_OBJECT (element, "changing display handle is not supported");
791 #ifdef GST_WLSINK_ENHANCEMENT
792 g_mutex_unlock (&sink->display_lock);
796 g_mutex_unlock (&sink->display_lock);
799 if (GST_ELEMENT_CLASS (parent_class)->set_context)
800 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
804 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
806 GstWaylandSink *sink;
810 sink = GST_WAYLAND_SINK (bsink);
812 caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
814 g_mutex_lock (&sink->display_lock);
817 GValue list = G_VALUE_INIT;
818 GValue value = G_VALUE_INIT;
821 #ifdef GST_WLSINK_ENHANCEMENT
824 enum wl_shm_format fmt;
826 g_value_init (&list, GST_TYPE_LIST);
827 g_value_init (&value, G_TYPE_STRING);
828 #ifdef GST_WLSINK_ENHANCEMENT
829 if (sink->display->USE_TBM)
830 formats = sink->display->tbm_formats;
833 formats = sink->display->formats;
835 for (i = 0; i < formats->len; i++) {
836 #ifdef GST_WLSINK_ENHANCEMENT
838 tbm_fmt = g_array_index (formats, uint32_t, i);
839 g_value_set_string (&value, gst_wl_tbm_format_to_string (tbm_fmt));
840 gst_value_list_append_value (&list, &value);
841 /* TBM doesn't support SN12. So we add SN12 manually as supported format.
842 * SN12 is exactly same with NV12.
844 if (tbm_fmt == TBM_FORMAT_NV12) {
845 g_value_set_string (&value,
846 gst_video_format_to_string (GST_VIDEO_FORMAT_SN12));
847 gst_value_list_append_value (&list, &value);
849 } else { /* USE SHM */
850 fmt = g_array_index (formats, uint32_t, i);
851 g_value_set_string (&value, gst_wl_shm_format_to_string (fmt));
852 gst_value_list_append_value (&list, &value);
854 #else /* open source */
855 fmt = g_array_index (formats, uint32_t, i);
856 g_value_set_string (&value, gst_wl_shm_format_to_string (fmt));
857 gst_value_list_append_value (&list, &value);
861 caps = gst_caps_make_writable (caps);
862 gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
864 GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
867 g_mutex_unlock (&sink->display_lock);
870 GstCaps *intersection;
873 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
874 gst_caps_unref (caps);
882 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
884 GstWaylandSink *sink;
885 GstBufferPool *newpool;
887 #ifdef GST_WLSINK_ENHANCEMENT
890 enum wl_shm_format format;
894 GstStructure *structure;
895 GstWlShmAllocator *self = NULL;
899 sink = GST_WAYLAND_SINK (bsink);
901 GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
903 /* extract info from caps */
904 if (!gst_video_info_from_caps (&info, caps))
906 #ifdef GST_WLSINK_ENHANCEMENT
907 sink->caps = gst_caps_copy (caps);
910 gst_video_format_to_wl_tbm_format (GST_VIDEO_INFO_FORMAT (&info));
911 if ((gint) tbm_format == -1)
914 format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (&info));
915 if ((gint) format == -1)
918 #else /* open source */
919 format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (&info));
921 if ((gint) format == -1)
925 /* verify we support the requested format */
926 #ifdef GST_WLSINK_ENHANCEMENT
927 if (sink->display->USE_TBM) {
928 GST_INFO ("USE TBM FORMAT");
929 formats = sink->display->tbm_formats;
930 for (i = 0; i < formats->len; i++) {
931 if (g_array_index (formats, uint32_t, i) == tbm_format)
934 } else { /* USE SHM */
935 GST_INFO ("USE SHM FORMAT");
936 formats = sink->display->formats;
937 for (i = 0; i < formats->len; i++) {
938 if (g_array_index (formats, uint32_t, i) == format)
942 #else /* open source */
944 formats = sink->display->formats;
945 for (i = 0; i < formats->len; i++) {
946 if (g_array_index (formats, uint32_t, i) == format)
950 if (i >= formats->len)
951 goto unsupported_format;
953 #ifdef GST_WLSINK_ENHANCEMENT
955 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_SN12 ||
956 GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ST12) {
957 sink->display->is_native_format = TRUE;
959 /* store the video info */
960 sink->video_info = info;
961 sink->video_info_changed = TRUE;
963 sink->display->is_native_format = FALSE;
964 self = GST_WL_SHM_ALLOCATOR (gst_wl_shm_allocator_get ());
965 self->display = sink->display;
966 /* create a new pool for the new configuration */
967 newpool = gst_video_buffer_pool_new ();
971 structure = gst_buffer_pool_get_config (newpool);
972 gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
973 gst_buffer_pool_config_set_allocator (structure,
974 gst_wl_shm_allocator_get (), NULL);
975 if (!gst_buffer_pool_set_config (newpool, structure))
978 /* store the video info */
979 sink->video_info = info;
980 sink->video_info_changed = TRUE;
982 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
983 gst_object_unref (newpool);
985 } else { /* USE SHM */
987 self = GST_WL_SHM_ALLOCATOR (gst_wl_shm_allocator_get ());
988 self->display = sink->display;
990 /* create a new pool for the new configuration */
991 newpool = gst_video_buffer_pool_new ();
995 structure = gst_buffer_pool_get_config (newpool);
996 gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
997 gst_buffer_pool_config_set_allocator (structure,
998 gst_wl_shm_allocator_get (), NULL);
999 if (!gst_buffer_pool_set_config (newpool, structure))
1002 /* store the video info */
1003 sink->video_info = info;
1004 sink->video_info_changed = TRUE;
1006 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1007 gst_object_unref (newpool);
1009 #else /*open source */
1010 /* create a new pool for the new configuration */
1011 newpool = gst_video_buffer_pool_new ();
1015 structure = gst_buffer_pool_get_config (newpool);
1016 gst_buffer_pool_config_set_params (structure, caps, info.size, 6, 0);
1017 gst_buffer_pool_config_set_allocator (structure,
1018 gst_wl_shm_allocator_get (), NULL);
1019 if (!gst_buffer_pool_set_config (newpool, structure))
1022 /* store the video info */
1023 sink->video_info = info;
1024 sink->video_info_changed = TRUE;
1026 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1027 gst_object_unref (newpool);
1034 GST_DEBUG_OBJECT (sink,
1035 "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
1040 #ifdef GST_WLSINK_ENHANCEMENT
1042 GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
1043 gst_wl_tbm_format_to_string (tbm_format));
1045 GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
1046 gst_wl_shm_format_to_string (format));
1047 #else /*open source */
1048 GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
1049 gst_wl_shm_format_to_string (format));
1055 GST_DEBUG_OBJECT (sink, "Failed to create new pool");
1060 GST_DEBUG_OBJECT (bsink, "failed setting config");
1061 gst_object_unref (newpool);
1067 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
1069 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
1070 GstStructure *config;
1071 guint size, min_bufs, max_bufs;
1072 #ifdef GST_WLSINK_ENHANCEMENT
1077 if (sink->USE_TBM) {
1078 if (sink->display->is_native_format == TRUE)
1081 gst_query_parse_allocation (query, &caps, &need_pool);
1084 GST_DEBUG_OBJECT (bsink, "no caps specified");
1089 config = gst_buffer_pool_get_config (sink->pool);
1090 gst_buffer_pool_config_get_params (config, NULL, &size, &min_bufs, &max_bufs);
1092 /* we do have a pool for sure (created in set_caps),
1093 * so let's propose it anyway, but also propose the allocator on its own */
1094 gst_query_add_allocation_pool (query, sink->pool, size, min_bufs, max_bufs);
1095 gst_query_add_allocation_param (query, gst_wl_shm_allocator_get (), NULL);
1097 gst_structure_free (config);
1102 static GstFlowReturn
1103 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
1106 GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
1107 return gst_wayland_sink_render (bsink, buffer);
1111 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
1113 GstWaylandSink *sink = data;
1116 GST_LOG ("frame_redraw_cb");
1118 g_atomic_int_set (&sink->redraw_pending, FALSE);
1119 wl_callback_destroy (callback);
1122 static const struct wl_callback_listener frame_callback_listener = {
1123 frame_redraw_callback
1126 #ifdef GST_WLSINK_ENHANCEMENT
1128 gst_wayland_sink_update_window_geometry (GstWaylandSink * sink)
1131 g_return_if_fail (sink != NULL);
1132 g_return_if_fail (sink->window != NULL);
1134 gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
1135 gst_wl_window_set_disp_geo_method (sink->window,
1136 sink->display_geometry_method);
1137 gst_wl_window_set_orientation (sink->window, sink->orientation);
1138 gst_wl_window_set_flip (sink->window, sink->flip);
1141 /* must be called with the render lock */
1143 render_last_buffer (GstWaylandSink * sink)
1145 GstWlBuffer *wlbuffer;
1146 const GstVideoInfo *info = NULL;
1147 struct wl_surface *surface;
1148 struct wl_callback *callback;
1151 wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
1152 surface = gst_wl_window_get_wl_surface (sink->window);
1154 g_atomic_int_set (&sink->redraw_pending, TRUE);
1155 callback = wl_surface_frame (surface);
1156 /* frame_callback_listener is called when wayland-client finish rendering the wl_buffer */
1157 wl_callback_add_listener (callback, &frame_callback_listener, sink);
1159 if (G_UNLIKELY (sink->video_info_changed)) {
1160 info = &sink->video_info;
1161 sink->video_info_changed = FALSE;
1163 #ifdef GST_WLSINK_ENHANCEMENT
1164 if (sink->last_buffer)
1165 gst_wl_window_render (sink->window, wlbuffer, info);
1167 if (G_UNLIKELY (info)) {
1168 gst_wl_window_set_video_info (sink->window, &info);
1172 gst_wl_window_render (sink->window, wlbuffer, info);
1176 static GstFlowReturn
1177 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
1179 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
1180 GstBuffer *to_render;
1181 GstWlBuffer *wlbuffer;
1182 GstFlowReturn ret = GST_FLOW_OK;
1185 g_mutex_lock (&sink->render_lock);
1187 GST_LOG_OBJECT (sink, "render buffer %p", buffer);
1189 if (G_UNLIKELY (!sink->window)) {
1190 /* ask for window handle. Unlock render_lock while doing that because
1191 * set_window_handle & friends will lock it in this context */
1192 g_mutex_unlock (&sink->render_lock);
1193 gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
1194 g_mutex_lock (&sink->render_lock);
1196 if (!sink->window) {
1197 /* if we were not provided a window, create one ourselves */
1199 gst_wl_window_new_toplevel (sink->display, &sink->video_info);
1202 #ifdef GST_WLSINK_ENHANCEMENT
1203 gst_wayland_sink_update_window_geometry (sink);
1204 sink->video_info_changed = TRUE;
1206 /* drop buffers until we get a frame callback */
1207 if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
1209 /* make sure that the application has called set_render_rectangle() */
1210 if (G_UNLIKELY (sink->window->render_rectangle.w == 0))
1211 goto no_window_size;
1213 #ifdef GST_WLSINK_ENHANCEMENT
1215 wlbuffer = gst_buffer_get_wl_buffer (buffer);
1216 if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) {
1217 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
1218 GST_INFO ("wl_buffer (%p)", wlbuffer->wlbuffer);
1222 GstMapInfo mem_info = GST_MAP_INFO_INIT;
1223 int size = GST_VIDEO_INFO_SIZE (&sink->video_info);
1224 mem = gst_buffer_peek_memory (to_render, 0);
1225 gst_memory_map (mem, &mem_info, GST_MAP_READ);
1227 data = mem_info.data;
1229 char file_name[128];
1231 sprintf (file_name, "/home/owner/DUMP/_WLSINK_OUT_DUMP_%2.2d.dump",
1233 ret = __write_rawdata (file_name, data, size);
1235 GST_ERROR ("_write_rawdata() failed");
1237 GST_INFO ("DUMP IMAGE %d, size (%d)", dump__cnt, size);
1238 gst_memory_unmap (mem, &mem_info);
1242 struct wl_buffer *wbuf = NULL;
1244 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
1245 mem = gst_buffer_peek_memory (buffer, 0);
1246 if (gst_is_wl_shm_memory (mem)) {
1248 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1251 gst_buffer_add_wl_buffer (buffer, wbuf, sink->display); //careat GstWlBuffer and add gstbuffer, wlbuffer, display and etc
1254 } else { //buffer is not from our pool and have not wl_buffer
1256 /* we don't know how to create a wl_buffer directly from the provided
1257 * memory, so we have to copy the data to a memory that we know how
1260 GST_LOG_OBJECT (sink, "buffer %p is not from our pool", buffer);
1261 GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, " "copying",
1264 if (sink->USE_TBM && sink->display->is_native_format) {
1265 /* in case of SN12 or ST12 */
1266 if (!gst_wayland_sink_get_mm_video_buf_info(sink->display, buffer))
1267 return GST_FLOW_ERROR;
1269 wlbuffer = gst_buffer_get_wl_buffer (buffer);
1270 if (G_UNLIKELY (!wlbuffer)) {
1272 gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1274 if (G_UNLIKELY (!wbuf))
1276 gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
1279 else if (sink->USE_TBM && !sink->display->is_native_format) {
1281 /* sink->pool always exists (created in set_caps), but it may not
1282 * be active if upstream is not using it */
1283 if (!gst_buffer_pool_is_active (sink->pool)
1284 && !gst_buffer_pool_set_active (sink->pool, TRUE))
1285 goto activate_failed;
1287 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1288 if (ret != GST_FLOW_OK)
1292 //mem = gst_buffer_peek_memory (to_render, 0);
1293 //if (gst_is_wl_shm_memory (mem)) {
1294 GST_INFO ("to_render buffer is our buffer");
1296 /* the first time we acquire a buffer,
1297 * we need to attach a wl_buffer on it */
1298 wlbuffer = gst_buffer_get_wl_buffer (buffer);
1299 if (G_UNLIKELY (!wlbuffer)) {
1300 mem = gst_buffer_peek_memory (to_render, 0);
1301 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1303 if (G_UNLIKELY (!wbuf))
1306 wlbuffer = gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
1309 gst_buffer_map (buffer, &src, GST_MAP_READ);
1310 gst_buffer_fill (to_render, 0, src.data, src.size);
1311 gst_buffer_unmap (buffer, &src);
1312 } else { /* USE SHM */
1313 /* sink->pool always exists (created in set_caps), but it may not
1314 * be active if upstream is not using it */
1315 if (!gst_buffer_pool_is_active (sink->pool) &&
1316 !gst_buffer_pool_set_active (sink->pool, TRUE))
1317 goto activate_failed;
1318 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1319 if (ret != GST_FLOW_OK)
1321 /* the first time we acquire a buffer,
1322 * we need to attach a wl_buffer on it */
1323 wlbuffer = gst_buffer_get_wl_buffer (buffer);
1324 if (G_UNLIKELY (!wlbuffer)) {
1325 mem = gst_buffer_peek_memory (to_render, 0);
1326 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1328 if (G_UNLIKELY (!wbuf))
1331 gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
1335 gst_buffer_map (buffer, &src, GST_MAP_READ);
1336 gst_buffer_fill (to_render, 0, src.data, src.size);
1337 gst_buffer_unmap (buffer, &src);
1342 if (sink->USE_TBM && sink->display->is_native_format) {
1343 if (G_UNLIKELY (buffer == sink->last_buffer)) {
1344 GST_LOG_OBJECT (sink, "Buffer already being rendered");
1347 gst_buffer_replace (&sink->last_buffer, buffer);
1348 render_last_buffer (sink);
1351 } else { /* USE SHM or normal format */
1352 /* drop double rendering */
1353 if (G_UNLIKELY (buffer == sink->last_buffer)) {
1354 GST_LOG_OBJECT (sink, "Buffer already being rendered");
1357 gst_buffer_replace (&sink->last_buffer, to_render);
1358 render_last_buffer (sink);
1360 if (buffer != to_render)
1361 gst_buffer_unref (to_render);
1366 #else /* open source */
1368 wlbuffer = gst_buffer_get_wl_buffer (buffer);
1370 if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) {
1371 GST_LOG_OBJECT (sink,
1372 "buffer %p has a wl_buffer from our display, " "writing directly",
1374 GST_INFO ("wl_buffer (%p)", wlbuffer->wlbuffer);
1379 struct wl_buffer *wbuf = NULL;
1381 GST_LOG_OBJECT (sink,
1382 "buffer %p does not have a wl_buffer from our " "display, creating it",
1384 mem = gst_buffer_peek_memory (buffer, 0);
1385 if (gst_is_wl_shm_memory (mem)) {
1387 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1391 gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
1396 /* we don't know how to create a wl_buffer directly from the provided
1397 * memory, so we have to copy the data to a memory that we know how
1400 GST_LOG_OBJECT (sink, "buffer %p is not from our pool", buffer);
1401 GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, " "copying",
1403 /* sink->pool always exists (created in set_caps), but it may not
1404 * be active if upstream is not using it */
1405 if (!gst_buffer_pool_is_active (sink->pool) &&
1406 !gst_buffer_pool_set_active (sink->pool, TRUE))
1407 goto activate_failed;
1409 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1410 if (ret != GST_FLOW_OK)
1413 /* the first time we acquire a buffer,
1414 * we need to attach a wl_buffer on it */
1415 wlbuffer = gst_buffer_get_wl_buffer (buffer);
1416 if (G_UNLIKELY (!wlbuffer)) {
1417 mem = gst_buffer_peek_memory (to_render, 0);
1418 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
1420 if (G_UNLIKELY (!wbuf))
1423 gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
1426 gst_buffer_map (buffer, &src, GST_MAP_READ);
1427 gst_buffer_fill (to_render, 0, src.data, src.size);
1428 gst_buffer_unmap (buffer, &src);
1431 /* drop double rendering */
1432 if (G_UNLIKELY (buffer == sink->last_buffer)) {
1433 GST_LOG_OBJECT (sink, "Buffer already being rendered");
1437 gst_buffer_replace (&sink->last_buffer, to_render);
1438 render_last_buffer (sink);
1440 if (buffer != to_render)
1441 gst_buffer_unref (to_render);
1445 #endif /* GST_WLSINK_ENHANCEMENT */
1449 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
1450 ("Window has no size set"),
1451 ("Make sure you set the size after calling set_window_handle"));
1452 ret = GST_FLOW_ERROR;
1457 GST_WARNING_OBJECT (sink, "could not create buffer");
1462 GST_ERROR_OBJECT (sink, "could not create wl_buffer out of wl_shm memory");
1463 ret = GST_FLOW_ERROR;
1468 GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
1469 ret = GST_FLOW_ERROR;
1474 g_mutex_unlock (&sink->render_lock);
1480 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
1482 iface->set_window_handle = gst_wayland_sink_set_window_handle;
1483 iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
1484 iface->expose = gst_wayland_sink_expose;
1485 #ifdef GST_WLSINK_ENHANCEMENT /* use unique_id */
1486 iface->set_wl_window_wl_surface_id =
1487 gst_wayland_sink_set_wl_window_wl_surface_id;
1491 #ifdef GST_WLSINK_ENHANCEMENT /* use unique_id */
1493 gst_wayland_sink_set_wl_window_wl_surface_id (GstVideoOverlay * overlay,
1494 guintptr wl_surface_id)
1496 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1498 g_return_if_fail (sink != NULL);
1500 if (sink->window != NULL) {
1501 GST_WARNING_OBJECT (sink, "changing window handle is not supported");
1504 g_mutex_lock (&sink->render_lock);
1505 g_clear_object (&sink->window);
1507 GST_INFO ("wl_surface_id %d %p", (int) wl_surface_id, (guintptr)wl_surface_id);
1509 if (wl_surface_id) {
1510 if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
1511 /* we cannot use our own display with an external window handle */
1512 if (G_UNLIKELY (sink->display->own_display)) {
1513 sink->display->wl_surface_id = (int) wl_surface_id;
1514 sink->window = gst_wl_window_new_in_surface (sink->display, NULL);
1517 GST_ERROR_OBJECT (sink, "Failed to find display handle, "
1518 "ignoring window handle");
1521 gst_wayland_sink_update_window_geometry (sink);
1523 g_mutex_unlock (&sink->render_lock);
1529 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
1531 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1532 struct wl_surface *surface = (struct wl_surface *) handle;
1535 g_return_if_fail (sink != NULL);
1537 #ifdef GST_WLSINK_ENHANCEMENT /* use unique_id */
1538 if (sink->window != NULL) {
1539 GST_WARNING_OBJECT (sink, "changing window handle is not supported");
1543 g_mutex_lock (&sink->render_lock);
1545 GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
1548 g_clear_object (&sink->window);
1551 if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
1552 /* we cannot use our own display with an external window handle */
1553 if (G_UNLIKELY (sink->display->own_display)) {
1554 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
1555 ("Application did not provide a wayland display handle"),
1556 ("Now waylandsink use internal display handle "
1557 "which is created ourselves. Consider providing a "
1558 "display handle from your application with GstContext"));
1559 sink->window = gst_wl_window_new_in_surface (sink->display, surface);
1561 sink->window = gst_wl_window_new_in_surface (sink->display, surface);
1564 GST_ERROR_OBJECT (sink, "Failed to find display handle, "
1565 "ignoring window handle");
1568 g_mutex_unlock (&sink->render_lock);
1573 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
1574 gint x, gint y, gint w, gint h)
1576 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1579 g_return_if_fail (sink != NULL);
1581 g_mutex_lock (&sink->render_lock);
1582 if (!sink->window) {
1583 g_mutex_unlock (&sink->render_lock);
1584 GST_WARNING_OBJECT (sink,
1585 "set_render_rectangle called without window, ignoring");
1589 GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
1591 gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
1593 g_mutex_unlock (&sink->render_lock);
1597 gst_wayland_sink_expose (GstVideoOverlay * overlay)
1599 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1602 g_return_if_fail (sink != NULL);
1604 GST_DEBUG_OBJECT (sink, "expose");
1606 g_mutex_lock (&sink->render_lock);
1607 if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
1608 GST_DEBUG_OBJECT (sink, "redrawing last buffer");
1609 render_last_buffer (sink);
1611 g_mutex_unlock (&sink->render_lock);
1615 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
1617 iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
1618 iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
1622 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
1624 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1626 g_return_if_fail (sink != NULL);
1628 g_mutex_lock (&sink->render_lock);
1629 if (!sink->window || !sink->window->area_subsurface) {
1630 g_mutex_unlock (&sink->render_lock);
1631 GST_INFO_OBJECT (sink,
1632 "begin_geometry_change called without window, ignoring");
1636 wl_subsurface_set_sync (sink->window->area_subsurface);
1637 g_mutex_unlock (&sink->render_lock);
1641 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
1643 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1645 g_return_if_fail (sink != NULL);
1647 g_mutex_lock (&sink->render_lock);
1648 if (!sink->window || !sink->window->area_subsurface) {
1649 g_mutex_unlock (&sink->render_lock);
1650 GST_INFO_OBJECT (sink,
1651 "end_geometry_change called without window, ignoring");
1655 wl_subsurface_set_desync (sink->window->area_subsurface);
1656 g_mutex_unlock (&sink->render_lock);
1660 plugin_init (GstPlugin * plugin)
1662 GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
1663 " wayland video sink");
1665 gst_wl_shm_allocator_register ();
1667 return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
1668 GST_TYPE_WAYLAND_SINK);
1671 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1674 "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,