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 "waylandpool.h"
53 #include <gst/wayland/wayland.h>
54 #include <gst/video/videooverlay.h>
56 #ifdef GST_WLSINK_ENHANCEMENT
57 #define GST_TYPE_WAYLANDSINK_DISPLAY_GEOMETRY_METHOD (gst_waylandsink_display_geometry_method_get_type())
58 #define GST_TYPE_WAYLANDSINK_ROTATE_ANGLE (gst_waylandsink_rotate_angle_get_type())
59 #define GST_TYPE_WAYLANDSINK_FLIP (gst_waylandsink_flip_get_type())
62 gst_waylandsink_rotate_angle_get_type (void)
64 static GType waylandsink_rotate_angle_type = 0;
65 static const GEnumValue rotate_angle_type[] = {
66 {0, "No rotate", "DEGREE_0"},
67 {1, "Rotate 90 degree", "DEGREE_90"},
68 {2, "Rotate 180 degree", "DEGREE_180"},
69 {3, "Rotate 270 degree", "DEGREE_270"},
73 if (!waylandsink_rotate_angle_type) {
74 waylandsink_rotate_angle_type =
75 g_enum_register_static ("GstWaylandSinkRotateAngleType",
79 return waylandsink_rotate_angle_type;
84 gst_waylandsink_display_geometry_method_get_type (void)
86 static GType waylandsink_display_geometry_method_type = 0;
87 static const GEnumValue display_geometry_method_type[] = {
88 {0, "Letter box", "LETTER_BOX"},
89 {1, "Origin size", "ORIGIN_SIZE"},
90 {2, "Full-screen", "FULL_SCREEN"},
91 {3, "Cropped full-screen", "CROPPED_FULL_SCREEN"},
92 {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"},
96 if (!waylandsink_display_geometry_method_type) {
97 waylandsink_display_geometry_method_type =
98 g_enum_register_static ("GstWaylandSinkDisplayGeometryMethodType",
99 display_geometry_method_type);
101 return waylandsink_display_geometry_method_type;
105 gst_waylandsink_flip_get_type (void)
107 static GType waylandsink_flip_type = 0;
108 static const GEnumValue flip_type[] = {
109 {FLIP_NONE, "Flip NONE", "FLIP_NONE"},
110 {FLIP_HORIZONTAL, "Flip HORIZONTAL", "FLIP_HORIZONTAL"},
111 {FLIP_VERTICAL, "Flip VERTICAL", "FLIP_VERTICAL"},
112 {FLIP_BOTH, "Flip BOTH", "FLIP_BOTH"},
113 {FLIP_NUM, NULL, NULL},
116 if (!waylandsink_flip_type) {
117 waylandsink_flip_type =
118 g_enum_register_static ("GstWaylandSinkFlipType", flip_type);
121 return waylandsink_flip_type;
139 #ifdef GST_WLSINK_ENHANCEMENT
141 PROP_DISPLAY_GEOMETRY_METHOD,
147 GST_DEBUG_CATEGORY (gstwayland_debug);
148 #define GST_CAT_DEFAULT gstwayland_debug
150 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
153 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
154 ("{ BGRx, BGRA, RGBx, xBGR, xRGB, RGBA, ABGR, ARGB, RGB, BGR, "
155 "RGB16, BGR16, YUY2, YVYU, UYVY, AYUV, NV12, NV21, NV16, "
156 #ifdef GST_WLSINK_ENHANCEMENT
159 "YUV9, YVU9, Y41B, I420, YV12, Y42B, v308 }"))
162 static void gst_wayland_sink_get_property (GObject * object,
163 guint prop_id, GValue * value, GParamSpec * pspec);
164 static void gst_wayland_sink_set_property (GObject * object,
165 guint prop_id, const GValue * value, GParamSpec * pspec);
166 static void gst_wayland_sink_finalize (GObject * object);
168 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
169 GstStateChange transition);
170 static void gst_wayland_sink_set_context (GstElement * element,
171 GstContext * context);
173 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
175 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
176 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
179 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
180 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
183 /* VideoOverlay interface */
184 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
186 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
188 static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
189 gint x, gint y, gint w, gint h);
190 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
192 /* WaylandVideo interface */
193 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
195 static void gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video);
196 static void gst_wayland_sink_end_geometry_change (GstWaylandVideo * video);
197 #ifdef GST_WLSINK_ENHANCEMENT
198 static void gst_wayland_sink_update_window_geometry (GstWaylandSink * sink);
199 static void render_last_buffer (GstWaylandSink * sink);
200 static void gst_wayland_sink_render_last_buffer (GstWaylandSink * sink);
204 #define gst_wayland_sink_parent_class parent_class
205 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
206 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
207 gst_wayland_sink_videooverlay_init)
208 G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
209 gst_wayland_sink_waylandvideo_init));
212 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
215 GObjectClass *gobject_class;
216 GstElementClass *gstelement_class;
217 GstBaseSinkClass *gstbasesink_class;
219 gobject_class = (GObjectClass *) klass;
220 gstelement_class = (GstElementClass *) klass;
221 gstbasesink_class = (GstBaseSinkClass *) klass;
223 gobject_class->set_property = gst_wayland_sink_set_property;
224 gobject_class->get_property = gst_wayland_sink_get_property;
225 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
227 gst_element_class_add_pad_template (gstelement_class,
228 gst_static_pad_template_get (&sink_template));
230 gst_element_class_set_static_metadata (gstelement_class,
231 "wayland video sink", "Sink/Video",
232 "Output to wayland surface",
233 "Sreerenj Balachandran <sreerenj.balachandran@intel.com>, "
234 "George Kiagiadakis <george.kiagiadakis@collabora.com>");
236 gstelement_class->change_state =
237 GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
238 gstelement_class->set_context =
239 GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
241 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
242 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
243 gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
244 gstbasesink_class->propose_allocation =
245 GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
246 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
248 g_object_class_install_property (gobject_class, PROP_DISPLAY,
249 g_param_spec_string ("display", "Wayland Display name", "Wayland "
250 "display name to connect to, if not supplied via the GstContext",
251 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 #ifdef GST_WLSINK_ENHANCEMENT
253 g_object_class_install_property (gobject_class, PROP_ROTATE_ANGLE,
254 g_param_spec_enum ("rotate", "Rotate angle",
255 "Rotate angle of display output",
256 GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
257 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
259 g_object_class_install_property (gobject_class, PROP_DISPLAY_GEOMETRY_METHOD,
260 g_param_spec_enum ("display-geometry-method", "Display geometry method",
261 "Geometrical method for display",
262 GST_TYPE_WAYLANDSINK_DISPLAY_GEOMETRY_METHOD,
263 DEF_DISPLAY_GEOMETRY_METHOD,
264 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266 g_object_class_install_property (gobject_class, PROP_ORIENTATION,
267 g_param_spec_enum ("orientation",
268 "Orientation information used for ROI/ZOOM",
269 "Orientation information for display",
270 GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
271 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
273 g_object_class_install_property (gobject_class, PROP_FLIP,
274 g_param_spec_enum ("flip", "Display flip",
276 GST_TYPE_WAYLANDSINK_FLIP, DEF_DISPLAY_FLIP,
277 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
283 gst_wayland_sink_init (GstWaylandSink * sink)
287 sink->display_geometry_method = DEF_DISPLAY_GEOMETRY_METHOD;
288 sink->flip = DEF_DISPLAY_FLIP;
289 sink->rotate_angle = DEGREE_0;
290 sink->orientation = DEGREE_0;
292 g_mutex_init (&sink->display_lock);
293 g_mutex_init (&sink->render_lock);
297 gst_wayland_sink_get_property (GObject * object,
298 guint prop_id, GValue * value, GParamSpec * pspec)
302 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
305 GST_OBJECT_LOCK (sink);
306 g_value_set_string (value, sink->display_name);
307 GST_OBJECT_UNLOCK (sink);
309 #ifdef GST_WLSINK_ENHANCEMENT
310 case PROP_ROTATE_ANGLE:
311 g_value_set_enum (value, sink->rotate_angle);
313 case PROP_DISPLAY_GEOMETRY_METHOD:
314 g_value_set_enum (value, sink->display_geometry_method);
316 case PROP_ORIENTATION:
317 g_value_set_enum (value, sink->orientation);
320 g_value_set_enum (value, sink->flip);
324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
330 gst_wayland_sink_set_property (GObject * object,
331 guint prop_id, const GValue * value, GParamSpec * pspec)
335 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
338 GST_OBJECT_LOCK (sink);
339 sink->display_name = g_value_dup_string (value);
340 GST_OBJECT_UNLOCK (sink);
342 #ifdef GST_WLSINK_ENHANCEMENT
343 case PROP_ROTATE_ANGLE:
344 sink->rotate_angle = g_value_get_enum (value);
345 GST_INFO_OBJECT (sink, "Rotate angle is set (%d)", sink->rotate_angle);
347 gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
349 sink->video_info_changed = TRUE;
350 if (GST_STATE (sink) == GST_STATE_PAUSED) {
351 /*need to render last buffer */
352 gst_wayland_sink_render_last_buffer (sink);
355 case PROP_DISPLAY_GEOMETRY_METHOD:
356 sink->display_geometry_method = g_value_get_enum (value);
357 GST_INFO_OBJECT (sink, "Display geometry method is set (%d)",
358 sink->display_geometry_method);
360 gst_wl_window_set_disp_geo_method (sink->window,
361 sink->display_geometry_method);
363 sink->video_info_changed = TRUE;
364 if (GST_STATE (sink) == GST_STATE_PAUSED) {
365 /*need to render last buffer */
366 gst_wayland_sink_render_last_buffer (sink);
369 case PROP_ORIENTATION:
370 sink->orientation = g_value_get_enum (value);
371 GST_INFO_OBJECT (sink, "Orientation is set (%d)", sink->orientation);
373 gst_wl_window_set_orientation (sink->window, sink->orientation);
375 sink->video_info_changed = TRUE;
376 if (GST_STATE (sink) == GST_STATE_PAUSED) {
377 /*need to render last buffer */
378 gst_wayland_sink_render_last_buffer (sink);
382 sink->flip = g_value_get_enum (value);
383 GST_INFO_OBJECT (sink, "flip is set (%d)", sink->flip);
385 gst_wl_window_set_flip (sink->window, sink->flip);
387 sink->video_info_changed = TRUE;
388 if (GST_STATE (sink) == GST_STATE_PAUSED) {
389 /*need to render last buffer */
390 gst_wayland_sink_render_last_buffer (sink);
395 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
401 gst_wayland_sink_finalize (GObject * object)
405 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
406 GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
408 if (sink->last_buffer)
409 gst_buffer_unref (sink->last_buffer);
411 /* see comment about this call in gst_wayland_sink_change_state() */
412 #ifdef GST_WLSINK_ENHANCEMENT
413 if (sink->pool && !sink->display->is_native_format)
417 gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
420 g_object_unref (sink->display);
421 sink->display = NULL;
424 g_object_unref (sink->window);
428 gst_object_unref (sink->pool);
432 if (sink->display_name) {
433 g_free (sink->display_name);
434 sink->display_name = NULL;
437 g_mutex_clear (&sink->display_lock);
438 g_mutex_clear (&sink->render_lock);
440 G_OBJECT_CLASS (parent_class)->finalize (object);
443 /* must be called with the display_lock */
445 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
446 GstContext * context)
450 struct wl_display *display;
451 GError *error = NULL;
453 display = gst_wayland_display_handle_context_get_handle (context);
454 sink->display = gst_wl_display_new_existing (display, FALSE, &error);
457 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
458 ("Could not set display handle"),
459 ("Failed to use the external wayland display: '%s'", error->message));
460 g_error_free (error);
465 gst_wayland_sink_find_display (GstWaylandSink * sink)
471 GstContext *context = NULL;
472 GError *error = NULL;
475 g_mutex_lock (&sink->display_lock);
477 if (!sink->display) {
478 /* first query upstream for the needed display handle */
479 query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
480 if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
481 gst_query_parse_context (query, &context);
482 gst_wayland_sink_set_display_from_context (sink, context);
484 gst_query_unref (query);
486 if (G_LIKELY (!sink->display)) {
487 /* now ask the application to set the display handle */
488 msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
489 GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
491 g_mutex_unlock (&sink->display_lock);
492 gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
493 /* at this point we expect gst_wayland_sink_set_context
494 * to get called and fill sink->display */
495 g_mutex_lock (&sink->display_lock);
497 if (!sink->display) {
498 /* if the application didn't set a display, let's create it ourselves */
499 GST_OBJECT_LOCK (sink);
500 sink->display = gst_wl_display_new (sink->display_name, &error);
501 GST_OBJECT_UNLOCK (sink);
504 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
505 ("Could not initialise Wayland output"),
506 ("Failed to create GstWlDisplay: '%s'", error->message));
507 g_error_free (error);
510 /* inform the world about the new display */
512 gst_wayland_display_handle_context_new (sink->display->display);
513 msg = gst_message_new_have_context (GST_OBJECT_CAST (sink), context);
514 gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
520 g_mutex_unlock (&sink->display_lock);
525 static GstStateChangeReturn
526 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
530 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
531 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
533 switch (transition) {
534 case GST_STATE_CHANGE_NULL_TO_READY:
535 if (!gst_wayland_sink_find_display (sink))
536 return GST_STATE_CHANGE_FAILURE;
542 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
543 if (ret == GST_STATE_CHANGE_FAILURE)
546 switch (transition) {
547 case GST_STATE_CHANGE_PAUSED_TO_READY:
548 gst_buffer_replace (&sink->last_buffer, NULL);
550 if (gst_wl_window_is_toplevel (sink->window)) {
551 g_clear_object (&sink->window);
553 /* remove buffer from surface, show nothing */
554 wl_surface_attach (sink->window->surface, NULL, 0, 0);
555 wl_surface_damage (sink->window->surface, 0, 0,
556 sink->window->surface_width, sink->window->surface_height);
557 wl_surface_commit (sink->window->surface);
558 wl_display_flush (sink->display->display);
562 case GST_STATE_CHANGE_READY_TO_NULL:
563 g_mutex_lock (&sink->display_lock);
564 /* If we had a toplevel window, we most likely have our own connection
565 * to the display too, and it is a good idea to disconnect and allow
566 * potentially the application to embed us with GstVideoOverlay
567 * (which requires to re-use the same display connection as the parent
568 * surface). If we didn't have a toplevel window, then the display
569 * connection that we have is definitely shared with the application
570 * and it's better to keep it around (together with the window handle)
571 * to avoid requesting them again from the application if/when we are
572 * restarted (GstVideoOverlay behaves like that in other sinks)
574 if (sink->display && !sink->window) { /* -> the window was toplevel */
575 /* Force all buffers to return to the pool, regardless of
576 * whether the compositor has released them or not. We are
577 * going to kill the display, so we need to return all buffers
578 * to be destroyed before this happens.
579 * Note that this is done here instead of the pool destructor
580 * because the buffers hold a reference to the pool. Also,
581 * the buffers can only be unref'ed from the display's event loop
582 * and the pool holds a reference to the display. If we drop
583 * our references here, when the compositor releases the buffers,
584 * they will be unref'ed from the event loop thread, which will
585 * unref the pool and therefore the display, which will try to
586 * stop the thread from within itself and cause a deadlock.
589 gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
592 g_clear_object (&sink->display);
593 g_clear_object (&sink->pool);
595 g_mutex_unlock (&sink->display_lock);
605 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
609 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
610 if (gst_context_has_context_type (context,
611 GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
612 g_mutex_lock (&sink->display_lock);
613 if (G_LIKELY (!sink->display)) {
614 gst_wayland_sink_set_display_from_context (sink, context);
616 GST_WARNING_OBJECT (element, "changing display handle is not supported");
617 g_mutex_unlock (&sink->display_lock);
620 g_mutex_unlock (&sink->display_lock);
623 GST_INFO ("element %p context %p", element, context);
624 if (GST_ELEMENT_CLASS (parent_class)->set_context)
625 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
629 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
633 GstWaylandSink *sink;
635 sink = GST_WAYLAND_SINK (bsink);
637 caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
639 g_mutex_lock (&sink->display_lock);
642 GValue list = G_VALUE_INIT;
643 GValue value = G_VALUE_INIT;
646 #ifdef GST_WLSINK_ENHANCEMENT
647 enum tizen_buffer_pool_format fmt;
649 enum wl_shm_format fmt;
652 g_value_init (&list, GST_TYPE_LIST);
653 g_value_init (&value, G_TYPE_STRING);
655 formats = sink->display->formats;
656 for (i = 0; i < formats->len; i++) {
657 fmt = g_array_index (formats, uint32_t, i);
658 g_value_set_string (&value, gst_wayland_format_to_string (fmt));
659 gst_value_list_append_value (&list, &value);
662 caps = gst_caps_make_writable (caps);
663 gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
665 GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
668 g_mutex_unlock (&sink->display_lock);
671 GstCaps *intersection;
674 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
675 gst_caps_unref (caps);
683 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
687 GstWaylandSink *sink;
688 GstBufferPool *newpool;
690 #ifdef GST_WLSINK_ENHANCEMENT
691 enum tizen_buffer_pool_format format;
693 enum wl_shm_format format;
697 GstStructure *structure;
698 static GstAllocationParams params = { 0, 0, 0, 15, };
699 sink = GST_WAYLAND_SINK (bsink);
701 GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
703 /* extract info from caps */
704 if (!gst_video_info_from_caps (&info, caps))
706 #ifdef GST_WLSINK_ENHANCEMENT
707 sink->caps = gst_caps_copy (caps);
710 format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
711 if ((gint) format == -1)
714 /* verify we support the requested format */
715 formats = sink->display->formats;
716 for (i = 0; i < formats->len; i++) {
717 if (g_array_index (formats, uint32_t, i) == format)
721 if (i >= formats->len)
722 goto unsupported_format;
724 #ifdef GST_WLSINK_ENHANCEMENT
725 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_SN12 ||
726 GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ST12) {
727 sink->display->is_native_format = TRUE;
729 sink->display->is_native_format = FALSE;
731 /* create a new pool for the new configuration */
732 newpool = gst_wayland_buffer_pool_new (sink->display);
736 structure = gst_buffer_pool_get_config (newpool);
737 gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
738 gst_buffer_pool_config_set_allocator (structure, NULL, ¶ms);
739 if (!gst_buffer_pool_set_config (newpool, structure))
742 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
743 gst_object_unref (newpool);
746 /* store the video info */
747 sink->video_info = info;
748 sink->video_info_changed = TRUE;
750 /* create a new pool for the new configuration */
751 newpool = gst_wayland_buffer_pool_new (sink->display);
755 structure = gst_buffer_pool_get_config (newpool);
756 gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
757 gst_buffer_pool_config_set_allocator (structure, NULL, ¶ms);
758 if (!gst_buffer_pool_set_config (newpool, structure))
761 /* store the video info */
762 sink->video_info = info;
763 sink->video_info_changed = TRUE;
765 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
766 gst_object_unref (newpool);
772 GST_DEBUG_OBJECT (sink,
773 "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
778 GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
779 gst_wayland_format_to_string (format));
784 GST_DEBUG_OBJECT (sink, "Failed to create new pool");
789 GST_DEBUG_OBJECT (bsink, "failed setting config");
790 gst_object_unref (newpool);
796 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
800 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
801 GstBufferPool *pool = NULL;
802 GstStructure *config;
807 if (sink->display->is_native_format == TRUE)
810 gst_query_parse_allocation (query, &caps, &need_pool);
816 pool = gst_object_ref (sink->pool);
821 /* we had a pool, check caps */
822 config = gst_buffer_pool_get_config (pool);
823 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
825 if (!gst_caps_is_equal (caps, pcaps)) {
826 /* different caps, we can't use this pool */
827 gst_object_unref (pool);
830 gst_structure_free (config);
833 if (pool == NULL && need_pool) {
836 if (!gst_video_info_from_caps (&info, caps))
839 GST_DEBUG_OBJECT (sink, "create new pool");
840 pool = gst_wayland_buffer_pool_new (sink->display);
842 /* the normal size of a frame */
845 config = gst_buffer_pool_get_config (pool);
846 gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
847 if (!gst_buffer_pool_set_config (pool, config))
851 gst_query_add_allocation_pool (query, pool, size, 2, 0);
852 gst_object_unref (pool);
860 GST_DEBUG_OBJECT (bsink, "no caps specified");
865 GST_DEBUG_OBJECT (bsink, "invalid caps specified");
870 GST_DEBUG_OBJECT (bsink, "failed setting config");
871 gst_object_unref (pool);
877 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
881 GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
882 return gst_wayland_sink_render (bsink, buffer);
886 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
890 GstWaylandSink *sink = data;
892 GST_LOG ("frame_redraw_cb");
894 g_atomic_int_set (&sink->redraw_pending, FALSE);
895 wl_callback_destroy (callback);
898 static const struct wl_callback_listener frame_callback_listener = {
899 frame_redraw_callback
902 /* must be called with the render lock */
904 render_last_buffer (GstWaylandSink * sink)
909 struct wl_surface *surface;
910 struct wl_callback *callback;
912 meta = gst_buffer_get_wl_meta (sink->last_buffer);
913 surface = gst_wl_window_get_wl_surface (sink->window);
915 g_atomic_int_set (&sink->redraw_pending, TRUE);
916 callback = wl_surface_frame (surface);
917 wl_callback_add_listener (callback, &frame_callback_listener, sink);
919 /* Here we essentially add a reference to the buffer. This represents
920 * the fact that the compositor is using the buffer and it should
921 * not return back to the pool and be reused until the compositor
922 * releases it. The release is handled internally in the pool */
923 gst_wayland_compositor_acquire_buffer (meta->pool, sink->last_buffer);
925 GST_DEBUG ("wl_surface_attach wl_buffer %p", meta->wbuffer);
927 wl_surface_attach (surface, meta->wbuffer, 0, 0);
928 wl_surface_damage (surface, 0, 0, sink->window->surface_width,
929 sink->window->surface_height);
931 wl_surface_commit (surface);
932 wl_display_flush (sink->display->display);
936 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
940 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
941 GstBuffer *to_render;
943 GstFlowReturn ret = GST_FLOW_OK;
945 #ifdef GST_WLSINK_ENHANCEMENT
946 GstBufferPool *newpool;
947 GstStructure *structure;
948 static GstAllocationParams params = { 0, 0, 0, 15, };
951 g_mutex_lock (&sink->render_lock);
953 GST_LOG_OBJECT (sink, "render buffer %p", buffer);
955 if (G_UNLIKELY (!sink->window)) {
956 /* ask for window handle. Unlock render_lock while doing that because
957 * set_window_handle & friends will lock it in this context */
958 g_mutex_unlock (&sink->render_lock);
959 gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
960 g_mutex_lock (&sink->render_lock);
963 /* inform the window about our caps */
964 gst_wl_window_set_video_info (sink->window, &sink->video_info);
966 /* if we were not provided a window, create one ourselves */
968 gst_wl_window_new_toplevel (sink->display, &sink->video_info);
970 #ifdef GST_WLSINK_ENHANCEMENT
971 gst_wayland_sink_update_window_geometry (sink);
972 sink->video_info_changed = TRUE;
974 sink->video_info_changed = FALSE;
978 /* drop buffers until we get a frame callback */
979 if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
982 if (G_UNLIKELY (sink->video_info_changed)) {
983 gst_wl_window_set_video_info (sink->window, &sink->video_info);
984 sink->video_info_changed = FALSE;
986 GST_INFO ("window->render_rectangle(%d,%d %d x %d)",
987 sink->window->render_rectangle.x,
988 sink->window->render_rectangle.y,
989 sink->window->render_rectangle.w, sink->window->render_rectangle.h);
990 GST_INFO ("window->surface_width(%d),window->surface_height(%d)",
991 sink->window->surface_width, sink->window->surface_height);
993 /* now that we have for sure set the video info on the window, it must have
994 * a valid size, otherwise this means that the application has called
995 * set_window_handle() without calling set_render_rectangle(), which is
996 * absolutely necessary for us.
998 if (G_UNLIKELY (sink->window->surface_width == 0 ||
999 sink->window->surface_height == 0))
1000 goto no_window_size;
1002 meta = gst_buffer_get_wl_meta (buffer);
1004 if (meta && meta->pool->display == sink->display) {
1005 GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
1009 GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
1011 #ifdef GST_WLSINK_ENHANCEMENT
1012 if (sink->display->is_native_format == TRUE) {
1013 /*in case of SN12 or ST12 video format */
1015 GstMapInfo mem_info = GST_MAP_INFO_INIT;
1016 MMVideoBuffer *mm_video_buf = NULL;
1018 mem = gst_buffer_peek_memory (buffer, 1);
1019 gst_memory_map (mem, &mem_info, GST_MAP_READ);
1020 mm_video_buf = (MMVideoBuffer *) mem_info.data;
1021 gst_memory_unmap (mem, &mem_info);
1023 if (mm_video_buf == NULL) {
1024 GST_WARNING_OBJECT (sink, "mm_video_buf is NULL. Skip rendering");
1027 /* assign mm_video_buf info */
1028 if (mm_video_buf->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
1029 GST_DEBUG_OBJECT (sink, "TBM bo %p %p %p", mm_video_buf->handle.bo[0],
1030 mm_video_buf->handle.bo[1], mm_video_buf->handle.bo[2]);
1032 sink->display->native_video_size = 0;
1034 for (int i = 0; i < NV_BUF_PLANE_NUM; i++) {
1035 if (mm_video_buf->handle.bo[i] != NULL) {
1036 sink->display->bo[i] = mm_video_buf->handle.bo[i];
1038 sink->display->bo[i] = 0;
1040 sink->display->plane_size[i] = mm_video_buf->size[i];
1041 sink->display->stride_width[i] = mm_video_buf->stride_width[i];
1042 sink->display->stride_height[i] = mm_video_buf->stride_height[i];
1043 sink->display->native_video_size += sink->display->plane_size[i];
1046 GST_ERROR_OBJECT (sink, "Buffer type is not TBM");
1052 /* create a new pool for the new configuration */
1053 newpool = gst_wayland_buffer_pool_new (sink->display);
1055 GST_DEBUG_OBJECT (sink, "Failed to create new pool");
1058 structure = gst_buffer_pool_get_config (newpool);
1059 gst_buffer_pool_config_set_params (structure, sink->caps,
1060 sink->video_info.size, 2, 0);
1061 gst_buffer_pool_config_set_allocator (structure, NULL, ¶ms);
1062 if (!gst_buffer_pool_set_config (newpool, structure)) {
1063 GST_DEBUG_OBJECT (bsink, "failed setting config");
1064 gst_object_unref (newpool);
1068 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1069 gst_object_unref (newpool);
1073 if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1074 goto activate_failed;
1076 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1077 if (ret != GST_FLOW_OK)
1081 /*add displaying buffer */
1083 meta = gst_buffer_get_wl_meta (to_render);
1084 gst_wayland_buffer_pool_add_displaying_buffer (sink->pool, meta, buffer);
1087 /*in case of normal video format and pool is not our pool */
1092 if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1093 goto activate_failed;
1095 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1096 if (ret != GST_FLOW_OK)
1099 gst_buffer_map (buffer, &src, GST_MAP_READ);
1100 gst_buffer_fill (to_render, 0, src.data, src.size);
1101 gst_buffer_unmap (buffer, &src);
1107 if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1108 goto activate_failed;
1110 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1111 if (ret != GST_FLOW_OK)
1114 gst_buffer_map (buffer, &src, GST_MAP_READ);
1115 gst_buffer_fill (to_render, 0, src.data, src.size);
1116 gst_buffer_unmap (buffer, &src);
1120 gst_buffer_replace (&sink->last_buffer, to_render);
1121 render_last_buffer (sink);
1123 if (buffer != to_render) {
1124 GST_LOG_OBJECT (sink, "Decrease ref count of buffer");
1125 gst_buffer_unref (to_render);
1131 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
1132 ("Window has no size set"),
1133 ("Make sure you set the size after calling set_window_handle"));
1134 ret = GST_FLOW_ERROR;
1139 GST_WARNING_OBJECT (sink, "could not create image");
1144 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
1145 ("Internal error: can't allocate images"),
1146 ("We don't have a bufferpool negotiated"));
1147 ret = GST_FLOW_ERROR;
1152 GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
1153 ret = GST_FLOW_ERROR;
1158 g_mutex_unlock (&sink->render_lock);
1164 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
1168 iface->set_window_handle = gst_wayland_sink_set_window_handle;
1169 iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
1170 iface->expose = gst_wayland_sink_expose;
1174 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
1178 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1179 struct wl_surface *surface = (struct wl_surface *) handle;
1181 g_return_if_fail (sink != NULL);
1183 if (sink->window != NULL) {
1184 GST_WARNING_OBJECT (sink, "changing window handle is not supported");
1188 g_mutex_lock (&sink->render_lock);
1190 GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
1193 g_clear_object (&sink->window);
1196 if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
1197 /* we cannot use our own display with an external window handle */
1198 if (G_UNLIKELY (sink->display->own_display)) {
1199 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
1200 ("Application did not provide a wayland display handle"),
1201 ("waylandsink cannot use an externally-supplied surface without "
1202 "an externally-supplied display handle. Consider providing a "
1203 "display handle from your application with GstContext"));
1205 sink->window = gst_wl_window_new_in_surface (sink->display, surface);
1206 GST_DEBUG ("sink->window %p", sink->window);
1209 GST_ERROR_OBJECT (sink, "Failed to find display handle, "
1210 "ignoring window handle");
1213 #ifdef GST_WLSINK_ENHANCEMENT
1214 gst_wayland_sink_update_window_geometry (sink);
1217 g_mutex_unlock (&sink->render_lock);
1220 #ifdef GST_WLSINK_ENHANCEMENT
1222 gst_wayland_sink_update_window_geometry (GstWaylandSink * sink)
1225 g_return_if_fail (sink != NULL);
1226 g_return_if_fail (sink->window != NULL);
1228 gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
1229 gst_wl_window_set_disp_geo_method (sink->window,
1230 sink->display_geometry_method);
1231 gst_wl_window_set_orientation (sink->window, sink->orientation);
1232 gst_wl_window_set_flip (sink->window, sink->flip);
1236 gst_wayland_sink_render_last_buffer (GstWaylandSink * sink)
1239 g_return_if_fail (sink != NULL);
1241 g_mutex_lock (&sink->render_lock);
1242 gst_wl_window_set_video_info (sink->window, &sink->video_info);
1243 sink->video_info_changed = FALSE;
1244 if (sink->last_buffer)
1245 render_last_buffer (sink);
1246 g_mutex_unlock (&sink->render_lock);
1250 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
1251 gint x, gint y, gint w, gint h)
1255 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1257 g_return_if_fail (sink != NULL);
1259 g_mutex_lock (&sink->render_lock);
1260 if (!sink->window) {
1261 g_mutex_unlock (&sink->render_lock);
1262 GST_WARNING_OBJECT (sink,
1263 "set_render_rectangle called without window, ignoring");
1267 GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
1269 gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
1271 g_mutex_unlock (&sink->render_lock);
1275 gst_wayland_sink_expose (GstVideoOverlay * overlay)
1279 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1281 g_return_if_fail (sink != NULL);
1283 GST_DEBUG_OBJECT (sink, "expose");
1285 g_mutex_lock (&sink->render_lock);
1286 if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
1287 GST_DEBUG_OBJECT (sink, "redrawing last buffer");
1288 render_last_buffer (sink);
1290 g_mutex_unlock (&sink->render_lock);
1294 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
1298 iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
1299 iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
1303 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
1307 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1308 g_return_if_fail (sink != NULL);
1310 g_mutex_lock (&sink->render_lock);
1311 if (!sink->window || !sink->window->subsurface) {
1312 g_mutex_unlock (&sink->render_lock);
1313 GST_INFO_OBJECT (sink,
1314 "begin_geometry_change called without window, ignoring");
1318 wl_subsurface_set_sync (sink->window->subsurface);
1319 g_mutex_unlock (&sink->render_lock);
1323 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
1327 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1328 g_return_if_fail (sink != NULL);
1330 g_mutex_lock (&sink->render_lock);
1331 if (!sink->window || !sink->window->subsurface) {
1332 g_mutex_unlock (&sink->render_lock);
1333 GST_INFO_OBJECT (sink,
1334 "end_geometry_change called without window, ignoring");
1338 wl_subsurface_set_desync (sink->window->subsurface);
1339 g_mutex_unlock (&sink->render_lock);
1343 plugin_init (GstPlugin * plugin)
1347 GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
1348 " wayland video sink");
1350 return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
1351 GST_TYPE_WAYLAND_SINK);
1354 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1357 "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,