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);
201 #define gst_wayland_sink_parent_class parent_class
202 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
203 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
204 gst_wayland_sink_videooverlay_init)
205 G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
206 gst_wayland_sink_waylandvideo_init));
209 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
212 GObjectClass *gobject_class;
213 GstElementClass *gstelement_class;
214 GstBaseSinkClass *gstbasesink_class;
216 gobject_class = (GObjectClass *) klass;
217 gstelement_class = (GstElementClass *) klass;
218 gstbasesink_class = (GstBaseSinkClass *) klass;
220 gobject_class->set_property = gst_wayland_sink_set_property;
221 gobject_class->get_property = gst_wayland_sink_get_property;
222 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
224 gst_element_class_add_pad_template (gstelement_class,
225 gst_static_pad_template_get (&sink_template));
227 gst_element_class_set_static_metadata (gstelement_class,
228 "wayland video sink", "Sink/Video",
229 "Output to wayland surface",
230 "Sreerenj Balachandran <sreerenj.balachandran@intel.com>, "
231 "George Kiagiadakis <george.kiagiadakis@collabora.com>");
233 gstelement_class->change_state =
234 GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
235 gstelement_class->set_context =
236 GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
238 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
239 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
240 gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
241 gstbasesink_class->propose_allocation =
242 GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
243 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
245 g_object_class_install_property (gobject_class, PROP_DISPLAY,
246 g_param_spec_string ("display", "Wayland Display name", "Wayland "
247 "display name to connect to, if not supplied via the GstContext",
248 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
249 #ifdef GST_WLSINK_ENHANCEMENT
250 g_object_class_install_property (gobject_class, PROP_ROTATE_ANGLE,
251 g_param_spec_enum ("rotate", "Rotate angle",
252 "Rotate angle of display output",
253 GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
254 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (gobject_class, PROP_DISPLAY_GEOMETRY_METHOD,
257 g_param_spec_enum ("display-geometry-method", "Display geometry method",
258 "Geometrical method for display",
259 GST_TYPE_WAYLANDSINK_DISPLAY_GEOMETRY_METHOD,
260 DEF_DISPLAY_GEOMETRY_METHOD,
261 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
263 g_object_class_install_property (gobject_class, PROP_ORIENTATION,
264 g_param_spec_enum ("orientation",
265 "Orientation information used for ROI/ZOOM",
266 "Orientation information for display",
267 GST_TYPE_WAYLANDSINK_ROTATE_ANGLE, DEGREE_0,
268 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
270 g_object_class_install_property (gobject_class, PROP_FLIP,
271 g_param_spec_enum ("flip", "Display flip",
273 GST_TYPE_WAYLANDSINK_FLIP, DEF_DISPLAY_FLIP,
274 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
280 gst_wayland_sink_init (GstWaylandSink * sink)
284 sink->display_geometry_method = DEF_DISPLAY_GEOMETRY_METHOD;
285 sink->flip = DEF_DISPLAY_FLIP;
286 sink->rotate_angle = DEGREE_0;
287 sink->orientation = DEGREE_0;
289 g_mutex_init (&sink->display_lock);
290 g_mutex_init (&sink->render_lock);
294 gst_wayland_sink_get_property (GObject * object,
295 guint prop_id, GValue * value, GParamSpec * pspec)
299 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
302 GST_OBJECT_LOCK (sink);
303 g_value_set_string (value, sink->display_name);
304 GST_OBJECT_UNLOCK (sink);
306 #ifdef GST_WLSINK_ENHANCEMENT
307 case PROP_ROTATE_ANGLE:
308 g_value_set_enum (value, sink->rotate_angle);
310 case PROP_DISPLAY_GEOMETRY_METHOD:
311 g_value_set_enum (value, sink->display_geometry_method);
313 case PROP_ORIENTATION:
314 g_value_set_enum (value, sink->orientation);
317 g_value_set_enum (value, sink->flip);
321 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
327 gst_wayland_sink_set_property (GObject * object,
328 guint prop_id, const GValue * value, GParamSpec * pspec)
332 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
335 GST_OBJECT_LOCK (sink);
336 sink->display_name = g_value_dup_string (value);
337 GST_OBJECT_UNLOCK (sink);
339 #ifdef GST_WLSINK_ENHANCEMENT
340 case PROP_ROTATE_ANGLE:
341 sink->rotate_angle = g_value_get_enum (value);
342 GST_INFO_OBJECT (sink, "Rotate angle is set (%d)", sink->rotate_angle);
344 gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
346 sink->video_info_changed = TRUE;
347 if (GST_STATE (sink) == GST_STATE_PAUSED) {
348 /*need to render current buffer*/
351 case PROP_DISPLAY_GEOMETRY_METHOD:
352 sink->display_geometry_method = g_value_get_enum (value);
353 GST_INFO_OBJECT (sink, "Display geometry method is set (%d)",
354 sink->display_geometry_method);
356 gst_wl_window_set_disp_geo_method (sink->window, sink->display_geometry_method);
358 sink->video_info_changed = TRUE;
359 if (GST_STATE (sink) == GST_STATE_PAUSED) {
360 /*need to render current buffer*/
363 case PROP_ORIENTATION:
364 sink->orientation = g_value_get_enum (value);
365 GST_INFO_OBJECT (sink, "Orientation is set (%d)", sink->orientation);
367 gst_wl_window_set_orientation (sink->window, sink->orientation);
369 sink->video_info_changed = TRUE;
370 if (GST_STATE (sink) == GST_STATE_PAUSED) {
371 /*need to render current buffer*/
375 sink->flip = g_value_get_enum (value);
376 GST_INFO_OBJECT (sink, "flip is set (%d)", sink->flip);
378 gst_wl_window_set_flip (sink->window, sink->flip);
380 sink->video_info_changed = TRUE;
381 if (GST_STATE (sink) == GST_STATE_PAUSED) {
382 /*need to render current buffer*/
387 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
393 gst_wayland_sink_finalize (GObject * object)
397 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
398 GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
400 if (sink->last_buffer)
401 gst_buffer_unref (sink->last_buffer);
403 /* see comment about this call in gst_wayland_sink_change_state() */
404 #ifdef GST_WLSINK_ENHANCEMENT
405 if (sink->pool && !sink->display->is_native_format)
409 gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
412 g_object_unref (sink->display);
413 sink->display = NULL;
416 g_object_unref (sink->window);
420 gst_object_unref (sink->pool);
424 if (sink->display_name) {
425 g_free (sink->display_name);
426 sink->display_name = NULL;
429 g_mutex_clear (&sink->display_lock);
430 g_mutex_clear (&sink->render_lock);
432 G_OBJECT_CLASS (parent_class)->finalize (object);
435 /* must be called with the display_lock */
437 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
438 GstContext * context)
442 struct wl_display *display;
443 GError *error = NULL;
445 display = gst_wayland_display_handle_context_get_handle (context);
446 sink->display = gst_wl_display_new_existing (display, FALSE, &error);
449 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
450 ("Could not set display handle"),
451 ("Failed to use the external wayland display: '%s'", error->message));
452 g_error_free (error);
457 gst_wayland_sink_find_display (GstWaylandSink * sink)
463 GstContext *context = NULL;
464 GError *error = NULL;
467 g_mutex_lock (&sink->display_lock);
469 if (!sink->display) {
470 /* first query upstream for the needed display handle */
471 query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
472 if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
473 gst_query_parse_context (query, &context);
474 gst_wayland_sink_set_display_from_context (sink, context);
476 gst_query_unref (query);
478 if (G_LIKELY (!sink->display)) {
479 /* now ask the application to set the display handle */
480 msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
481 GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
483 g_mutex_unlock (&sink->display_lock);
484 gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
485 /* at this point we expect gst_wayland_sink_set_context
486 * to get called and fill sink->display */
487 g_mutex_lock (&sink->display_lock);
489 if (!sink->display) {
490 /* if the application didn't set a display, let's create it ourselves */
491 GST_OBJECT_LOCK (sink);
492 sink->display = gst_wl_display_new (sink->display_name, &error);
493 GST_OBJECT_UNLOCK (sink);
496 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
497 ("Could not initialise Wayland output"),
498 ("Failed to create GstWlDisplay: '%s'", error->message));
499 g_error_free (error);
502 /* inform the world about the new display */
504 gst_wayland_display_handle_context_new (sink->display->display);
505 msg = gst_message_new_have_context (GST_OBJECT_CAST (sink), context);
506 gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
512 g_mutex_unlock (&sink->display_lock);
517 static GstStateChangeReturn
518 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
522 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
523 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
525 switch (transition) {
526 case GST_STATE_CHANGE_NULL_TO_READY:
527 if (!gst_wayland_sink_find_display (sink))
528 return GST_STATE_CHANGE_FAILURE;
534 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
535 if (ret == GST_STATE_CHANGE_FAILURE)
538 switch (transition) {
539 case GST_STATE_CHANGE_PAUSED_TO_READY:
540 gst_buffer_replace (&sink->last_buffer, NULL);
542 if (gst_wl_window_is_toplevel (sink->window)) {
543 g_clear_object (&sink->window);
545 /* remove buffer from surface, show nothing */
546 wl_surface_attach (sink->window->surface, NULL, 0, 0);
547 wl_surface_damage (sink->window->surface, 0, 0,
548 sink->window->surface_width, sink->window->surface_height);
549 wl_surface_commit (sink->window->surface);
550 wl_display_flush (sink->display->display);
554 case GST_STATE_CHANGE_READY_TO_NULL:
555 g_mutex_lock (&sink->display_lock);
556 /* If we had a toplevel window, we most likely have our own connection
557 * to the display too, and it is a good idea to disconnect and allow
558 * potentially the application to embed us with GstVideoOverlay
559 * (which requires to re-use the same display connection as the parent
560 * surface). If we didn't have a toplevel window, then the display
561 * connection that we have is definitely shared with the application
562 * and it's better to keep it around (together with the window handle)
563 * to avoid requesting them again from the application if/when we are
564 * restarted (GstVideoOverlay behaves like that in other sinks)
566 if (sink->display && !sink->window) { /* -> the window was toplevel */
567 /* Force all buffers to return to the pool, regardless of
568 * whether the compositor has released them or not. We are
569 * going to kill the display, so we need to return all buffers
570 * to be destroyed before this happens.
571 * Note that this is done here instead of the pool destructor
572 * because the buffers hold a reference to the pool. Also,
573 * the buffers can only be unref'ed from the display's event loop
574 * and the pool holds a reference to the display. If we drop
575 * our references here, when the compositor releases the buffers,
576 * they will be unref'ed from the event loop thread, which will
577 * unref the pool and therefore the display, which will try to
578 * stop the thread from within itself and cause a deadlock.
581 gst_wayland_compositor_release_all_buffers (GST_WAYLAND_BUFFER_POOL
584 g_clear_object (&sink->display);
585 g_clear_object (&sink->pool);
587 g_mutex_unlock (&sink->display_lock);
597 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
601 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
602 if (gst_context_has_context_type (context,
603 GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
604 g_mutex_lock (&sink->display_lock);
605 if (G_LIKELY (!sink->display)) {
606 gst_wayland_sink_set_display_from_context (sink, context);
608 GST_WARNING_OBJECT (element, "changing display handle is not supported");
609 g_mutex_unlock (&sink->display_lock);
612 g_mutex_unlock (&sink->display_lock);
615 GST_INFO ("element %p context %p", element, context);
616 if (GST_ELEMENT_CLASS (parent_class)->set_context)
617 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
621 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
625 GstWaylandSink *sink;
627 sink = GST_WAYLAND_SINK (bsink);
629 caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
631 g_mutex_lock (&sink->display_lock);
634 GValue list = G_VALUE_INIT;
635 GValue value = G_VALUE_INIT;
638 #ifdef GST_WLSINK_ENHANCEMENT
639 enum tizen_buffer_pool_format fmt;
641 enum wl_shm_format fmt;
644 g_value_init (&list, GST_TYPE_LIST);
645 g_value_init (&value, G_TYPE_STRING);
647 formats = sink->display->formats;
648 for (i = 0; i < formats->len; i++) {
649 fmt = g_array_index (formats, uint32_t, i);
650 g_value_set_string (&value, gst_wayland_format_to_string (fmt));
651 gst_value_list_append_value (&list, &value);
654 caps = gst_caps_make_writable (caps);
655 gst_structure_set_value (gst_caps_get_structure (caps, 0), "format", &list);
657 GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
660 g_mutex_unlock (&sink->display_lock);
663 GstCaps *intersection;
666 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
667 gst_caps_unref (caps);
675 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
679 GstWaylandSink *sink;
680 GstBufferPool *newpool;
682 #ifdef GST_WLSINK_ENHANCEMENT
683 enum tizen_buffer_pool_format format;
685 enum wl_shm_format format;
689 GstStructure *structure;
690 static GstAllocationParams params = { 0, 0, 0, 15, };
691 sink = GST_WAYLAND_SINK (bsink);
693 GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
695 /* extract info from caps */
696 if (!gst_video_info_from_caps (&info, caps))
698 #ifdef GST_WLSINK_ENHANCEMENT
699 sink->caps = gst_caps_copy (caps);
702 format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
703 if ((gint) format == -1)
706 /* verify we support the requested format */
707 formats = sink->display->formats;
708 for (i = 0; i < formats->len; i++) {
709 if (g_array_index (formats, uint32_t, i) == format)
713 if (i >= formats->len)
714 goto unsupported_format;
716 #ifdef GST_WLSINK_ENHANCEMENT
717 if (GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_SN12 ||
718 GST_VIDEO_INFO_FORMAT (&info) == GST_VIDEO_FORMAT_ST12) {
719 sink->display->is_native_format = TRUE;
721 sink->display->is_native_format = FALSE;
723 /* create a new pool for the new configuration */
724 newpool = gst_wayland_buffer_pool_new (sink->display);
728 structure = gst_buffer_pool_get_config (newpool);
729 gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
730 gst_buffer_pool_config_set_allocator (structure, NULL, ¶ms);
731 if (!gst_buffer_pool_set_config (newpool, structure))
734 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
735 gst_object_unref (newpool);
738 /* store the video info */
739 sink->video_info = info;
740 sink->video_info_changed = TRUE;
742 /* create a new pool for the new configuration */
743 newpool = gst_wayland_buffer_pool_new (sink->display);
747 structure = gst_buffer_pool_get_config (newpool);
748 gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
749 gst_buffer_pool_config_set_allocator (structure, NULL, ¶ms);
750 if (!gst_buffer_pool_set_config (newpool, structure))
753 /* store the video info */
754 sink->video_info = info;
755 sink->video_info_changed = TRUE;
757 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
758 gst_object_unref (newpool);
764 GST_DEBUG_OBJECT (sink,
765 "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
770 GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
771 gst_wayland_format_to_string (format));
776 GST_DEBUG_OBJECT (sink, "Failed to create new pool");
781 GST_DEBUG_OBJECT (bsink, "failed setting config");
782 gst_object_unref (newpool);
788 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
792 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
793 GstBufferPool *pool = NULL;
794 GstStructure *config;
799 if (sink->display->is_native_format == TRUE)
802 gst_query_parse_allocation (query, &caps, &need_pool);
808 pool = gst_object_ref (sink->pool);
813 /* we had a pool, check caps */
814 config = gst_buffer_pool_get_config (pool);
815 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
817 if (!gst_caps_is_equal (caps, pcaps)) {
818 /* different caps, we can't use this pool */
819 gst_object_unref (pool);
822 gst_structure_free (config);
825 if (pool == NULL && need_pool) {
828 if (!gst_video_info_from_caps (&info, caps))
831 GST_DEBUG_OBJECT (sink, "create new pool");
832 pool = gst_wayland_buffer_pool_new (sink->display);
834 /* the normal size of a frame */
837 config = gst_buffer_pool_get_config (pool);
838 gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
839 if (!gst_buffer_pool_set_config (pool, config))
843 gst_query_add_allocation_pool (query, pool, size, 2, 0);
844 gst_object_unref (pool);
852 GST_DEBUG_OBJECT (bsink, "no caps specified");
857 GST_DEBUG_OBJECT (bsink, "invalid caps specified");
862 GST_DEBUG_OBJECT (bsink, "failed setting config");
863 gst_object_unref (pool);
869 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
873 GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
874 return gst_wayland_sink_render (bsink, buffer);
878 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
882 GstWaylandSink *sink = data;
884 GST_LOG ("frame_redraw_cb");
886 g_atomic_int_set (&sink->redraw_pending, FALSE);
887 wl_callback_destroy (callback);
890 static const struct wl_callback_listener frame_callback_listener = {
891 frame_redraw_callback
894 /* must be called with the render lock */
896 render_last_buffer (GstWaylandSink * sink)
901 struct wl_surface *surface;
902 struct wl_callback *callback;
904 meta = gst_buffer_get_wl_meta (sink->last_buffer);
905 surface = gst_wl_window_get_wl_surface (sink->window);
907 g_atomic_int_set (&sink->redraw_pending, TRUE);
908 callback = wl_surface_frame (surface);
909 wl_callback_add_listener (callback, &frame_callback_listener, sink);
911 /* Here we essentially add a reference to the buffer. This represents
912 * the fact that the compositor is using the buffer and it should
913 * not return back to the pool and be reused until the compositor
914 * releases it. The release is handled internally in the pool */
915 gst_wayland_compositor_acquire_buffer (meta->pool, sink->last_buffer);
917 GST_DEBUG ("wl_surface_attach wl_buffer %p", meta->wbuffer);
919 wl_surface_attach (surface, meta->wbuffer, 0, 0);
920 wl_surface_damage (surface, 0, 0, sink->window->surface_width,
921 sink->window->surface_height);
923 wl_surface_commit (surface);
924 wl_display_flush (sink->display->display);
928 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
932 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
933 GstBuffer *to_render;
935 GstFlowReturn ret = GST_FLOW_OK;
937 #ifdef GST_WLSINK_ENHANCEMENT
938 GstBufferPool *newpool;
939 GstStructure *structure;
940 static GstAllocationParams params = { 0, 0, 0, 15, };
943 g_mutex_lock (&sink->render_lock);
945 GST_LOG_OBJECT (sink, "render buffer %p", buffer);
947 if (G_UNLIKELY (!sink->window)) {
948 /* ask for window handle. Unlock render_lock while doing that because
949 * set_window_handle & friends will lock it in this context */
950 g_mutex_unlock (&sink->render_lock);
951 gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
952 g_mutex_lock (&sink->render_lock);
955 /* inform the window about our caps */
956 gst_wl_window_set_video_info (sink->window, &sink->video_info);
958 /* if we were not provided a window, create one ourselves */
960 gst_wl_window_new_toplevel (sink->display, &sink->video_info);
962 #ifdef GST_WLSINK_ENHANCEMENT
963 gst_wayland_sink_update_window_geometry (sink);
964 sink->video_info_changed = TRUE;
966 sink->video_info_changed = FALSE;
970 /* drop buffers until we get a frame callback */
971 if (g_atomic_int_get (&sink->redraw_pending) == TRUE)
974 if (G_UNLIKELY (sink->video_info_changed)) {
975 gst_wl_window_set_video_info (sink->window, &sink->video_info);
976 sink->video_info_changed = FALSE;
979 GST_INFO ("window->render_rectangle(%d,%d %d x %d)",
980 sink->window->render_rectangle.x,
981 sink->window->render_rectangle.y,
982 sink->window->render_rectangle.w, sink->window->render_rectangle.h);
983 GST_INFO ("window->surface_width(%d),window->surface_height(%d)",
984 sink->window->surface_width, sink->window->surface_height);
986 /* now that we have for sure set the video info on the window, it must have
987 * a valid size, otherwise this means that the application has called
988 * set_window_handle() without calling set_render_rectangle(), which is
989 * absolutely necessary for us.
991 if (G_UNLIKELY (sink->window->surface_width == 0 ||
992 sink->window->surface_height == 0))
995 meta = gst_buffer_get_wl_meta (buffer);
997 if (meta && meta->pool->display == sink->display) {
998 GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
1002 GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
1004 #ifdef GST_WLSINK_ENHANCEMENT
1005 if (sink->display->is_native_format == TRUE) {
1006 /*in case of SN12 or ST12 video format */
1008 GstMapInfo mem_info = GST_MAP_INFO_INIT;
1009 MMVideoBuffer *mm_video_buf = NULL;
1011 mem = gst_buffer_peek_memory (buffer, 1);
1012 gst_memory_map (mem, &mem_info, GST_MAP_READ);
1013 mm_video_buf = (MMVideoBuffer *) mem_info.data;
1014 gst_memory_unmap (mem, &mem_info);
1016 if (mm_video_buf == NULL) {
1017 GST_WARNING_OBJECT (sink, "mm_video_buf is NULL. Skip rendering");
1020 /* assign mm_video_buf info */
1021 if (mm_video_buf->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
1022 GST_DEBUG_OBJECT (sink, "TBM bo %p %p %p", mm_video_buf->handle.bo[0],
1023 mm_video_buf->handle.bo[1], mm_video_buf->handle.bo[2]);
1025 sink->display->native_video_size = 0;
1027 for (int i = 0; i < NV_BUF_PLANE_NUM; i++) {
1028 if (mm_video_buf->handle.bo[i] != NULL) {
1029 sink->display->bo[i] = mm_video_buf->handle.bo[i];
1031 sink->display->bo[i] = 0;
1033 sink->display->plane_size[i] = mm_video_buf->size[i];
1034 sink->display->stride_width[i] = mm_video_buf->stride_width[i];
1035 sink->display->stride_height[i] = mm_video_buf->stride_height[i];
1036 sink->display->native_video_size += sink->display->plane_size[i];
1039 GST_ERROR_OBJECT (sink, "Buffer type is not TBM");
1045 /* create a new pool for the new configuration */
1046 newpool = gst_wayland_buffer_pool_new (sink->display);
1048 GST_DEBUG_OBJECT (sink, "Failed to create new pool");
1051 structure = gst_buffer_pool_get_config (newpool);
1052 gst_buffer_pool_config_set_params (structure, sink->caps,
1053 sink->video_info.size, 2, 0);
1054 gst_buffer_pool_config_set_allocator (structure, NULL, ¶ms);
1055 if (!gst_buffer_pool_set_config (newpool, structure)) {
1056 GST_DEBUG_OBJECT (bsink, "failed setting config");
1057 gst_object_unref (newpool);
1061 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
1062 gst_object_unref (newpool);
1066 if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1067 goto activate_failed;
1069 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1070 if (ret != GST_FLOW_OK)
1074 /*in case of normal video format and pool is not our pool */
1079 if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1080 goto activate_failed;
1082 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1083 if (ret != GST_FLOW_OK)
1086 gst_buffer_map (buffer, &src, GST_MAP_READ);
1087 gst_buffer_fill (to_render, 0, src.data, src.size);
1088 gst_buffer_unmap (buffer, &src);
1095 if (!gst_buffer_pool_set_active (sink->pool, TRUE))
1096 goto activate_failed;
1098 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
1099 if (ret != GST_FLOW_OK)
1102 gst_buffer_map (buffer, &src, GST_MAP_READ);
1103 gst_buffer_fill (to_render, 0, src.data, src.size);
1104 gst_buffer_unmap (buffer, &src);
1108 gst_buffer_replace (&sink->last_buffer, to_render);
1109 render_last_buffer (sink);
1111 if (buffer != to_render) {
1112 GST_LOG_OBJECT (sink, "Decrease ref count of buffer");
1113 gst_buffer_unref (to_render);
1119 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
1120 ("Window has no size set"),
1121 ("Make sure you set the size after calling set_window_handle"));
1122 ret = GST_FLOW_ERROR;
1127 GST_WARNING_OBJECT (sink, "could not create image");
1132 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
1133 ("Internal error: can't allocate images"),
1134 ("We don't have a bufferpool negotiated"));
1135 ret = GST_FLOW_ERROR;
1140 GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
1141 ret = GST_FLOW_ERROR;
1146 g_mutex_unlock (&sink->render_lock);
1152 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
1156 iface->set_window_handle = gst_wayland_sink_set_window_handle;
1157 iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
1158 iface->expose = gst_wayland_sink_expose;
1162 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
1166 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1167 struct wl_surface *surface = (struct wl_surface *) handle;
1169 g_return_if_fail (sink != NULL);
1171 if (sink->window != NULL) {
1172 GST_WARNING_OBJECT (sink, "changing window handle is not supported");
1176 g_mutex_lock (&sink->render_lock);
1178 GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
1181 g_clear_object (&sink->window);
1184 if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
1185 /* we cannot use our own display with an external window handle */
1186 if (G_UNLIKELY (sink->display->own_display)) {
1187 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
1188 ("Application did not provide a wayland display handle"),
1189 ("waylandsink cannot use an externally-supplied surface without "
1190 "an externally-supplied display handle. Consider providing a "
1191 "display handle from your application with GstContext"));
1193 sink->window = gst_wl_window_new_in_surface (sink->display, surface);
1194 GST_DEBUG ("sink->window %p", sink->window);
1197 GST_ERROR_OBJECT (sink, "Failed to find display handle, "
1198 "ignoring window handle");
1201 #ifdef GST_WLSINK_ENHANCEMENT
1202 gst_wayland_sink_update_window_geometry (sink);
1205 g_mutex_unlock (&sink->render_lock);
1208 #ifdef GST_WLSINK_ENHANCEMENT
1210 gst_wayland_sink_update_window_geometry (GstWaylandSink * sink)
1214 if (sink == NULL || sink->window == NULL)
1217 gst_wl_window_set_rotate_angle (sink->window, sink->rotate_angle);
1218 gst_wl_window_set_disp_geo_method (sink->window, sink->display_geometry_method);
1219 gst_wl_window_set_orientation (sink->window, sink->orientation);
1220 gst_wl_window_set_flip (sink->window, sink->flip);
1224 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
1225 gint x, gint y, gint w, gint h)
1229 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1231 g_return_if_fail (sink != NULL);
1233 g_mutex_lock (&sink->render_lock);
1234 if (!sink->window) {
1235 g_mutex_unlock (&sink->render_lock);
1236 GST_WARNING_OBJECT (sink,
1237 "set_render_rectangle called without window, ignoring");
1241 GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
1243 gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
1245 g_mutex_unlock (&sink->render_lock);
1249 gst_wayland_sink_expose (GstVideoOverlay * overlay)
1253 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
1255 g_return_if_fail (sink != NULL);
1257 GST_DEBUG_OBJECT (sink, "expose");
1259 g_mutex_lock (&sink->render_lock);
1260 if (sink->last_buffer && g_atomic_int_get (&sink->redraw_pending) == FALSE) {
1261 GST_DEBUG_OBJECT (sink, "redrawing last buffer");
1262 render_last_buffer (sink);
1264 g_mutex_unlock (&sink->render_lock);
1268 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
1272 iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
1273 iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
1277 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
1281 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1282 g_return_if_fail (sink != NULL);
1284 g_mutex_lock (&sink->render_lock);
1285 if (!sink->window || !sink->window->subsurface) {
1286 g_mutex_unlock (&sink->render_lock);
1287 GST_INFO_OBJECT (sink,
1288 "begin_geometry_change called without window, ignoring");
1292 wl_subsurface_set_sync (sink->window->subsurface);
1293 g_mutex_unlock (&sink->render_lock);
1297 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
1301 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1302 g_return_if_fail (sink != NULL);
1304 g_mutex_lock (&sink->render_lock);
1305 if (!sink->window || !sink->window->subsurface) {
1306 g_mutex_unlock (&sink->render_lock);
1307 GST_INFO_OBJECT (sink,
1308 "end_geometry_change called without window, ignoring");
1312 wl_subsurface_set_desync (sink->window->subsurface);
1313 g_mutex_unlock (&sink->render_lock);
1317 plugin_init (GstPlugin * plugin)
1321 GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
1322 " wayland video sink");
1324 return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
1325 GST_TYPE_WAYLAND_SINK);
1328 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1331 "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,