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>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA.
24 * SECTION:element-waylandsink
26 * The waylandsink is creating its own window and render the decoded video frames to that.
27 * Setup the Wayland environment as described in
28 * <ulink url="http://wayland.freedesktop.org/building.html">Wayland</ulink> home page.
29 * The current implementaion is based on weston compositor.
32 * <title>Example pipelines</title>
34 * gst-launch -v videotestsrc ! waylandsink
35 * ]| test the video rendering in wayland
43 #include "gstwaylandsink.h"
44 #include "wlvideoformat.h"
45 #include "waylandpool.h"
61 GST_DEBUG_CATEGORY (gstwayland_debug);
62 #define GST_CAT_DEFAULT gstwayland_debug
64 #if G_BYTE_ORDER == G_BIG_ENDIAN
65 #define CAPS "{xRGB, ARGB}"
67 #define CAPS "{BGRx, BGRA}"
70 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
73 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (CAPS))
76 /*Fixme: Add more interfaces */
77 #define gst_wayland_sink_parent_class parent_class
78 G_DEFINE_TYPE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK);
80 static void gst_wayland_sink_get_property (GObject * object,
81 guint prop_id, GValue * value, GParamSpec * pspec);
82 static void gst_wayland_sink_set_property (GObject * object,
83 guint prop_id, const GValue * value, GParamSpec * pspec);
84 static void gst_wayland_sink_finalize (GObject * object);
85 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
87 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
88 static gboolean gst_wayland_sink_start (GstBaseSink * bsink);
89 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
92 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
93 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
96 static void frame_redraw_callback (void *data,
97 struct wl_callback *callback, uint32_t time);
100 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
102 GObjectClass *gobject_class;
103 GstElementClass *gstelement_class;
104 GstBaseSinkClass *gstbasesink_class;
106 gobject_class = (GObjectClass *) klass;
107 gstelement_class = (GstElementClass *) klass;
108 gstbasesink_class = (GstBaseSinkClass *) klass;
110 gobject_class->set_property = gst_wayland_sink_set_property;
111 gobject_class->get_property = gst_wayland_sink_get_property;
112 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
114 gst_element_class_add_pad_template (gstelement_class,
115 gst_static_pad_template_get (&sink_template));
117 gst_element_class_set_static_metadata (gstelement_class,
118 "wayland video sink", "Sink/Video",
119 "Output to wayland surface",
120 "Sreerenj Balachandran <sreerenj.balachandran@intel.com>");
122 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
123 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
124 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_wayland_sink_start);
125 gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
126 gstbasesink_class->propose_allocation =
127 GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
128 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
130 g_object_class_install_property (gobject_class, PROP_WAYLAND_DISPLAY,
131 g_param_spec_pointer ("wayland-display", "Wayland Display",
132 "Wayland Display handle created by the application ",
133 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
137 gst_wayland_sink_init (GstWaylandSink * sink)
139 sink->display = NULL;
145 gst_wayland_sink_get_property (GObject * object,
146 guint prop_id, GValue * value, GParamSpec * pspec)
148 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
151 case PROP_WAYLAND_DISPLAY:
152 g_value_set_pointer (value, sink->display);
155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161 gst_wayland_sink_set_property (GObject * object,
162 guint prop_id, const GValue * value, GParamSpec * pspec)
164 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
167 case PROP_WAYLAND_DISPLAY:
168 sink->display = g_value_get_pointer (value);
171 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
177 gst_wayland_sink_finalize (GObject * object)
179 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
181 GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
184 g_object_unref (sink->window);
186 g_object_unref (sink->display);
188 G_OBJECT_CLASS (parent_class)->finalize (object);
192 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
194 GstWaylandSink *sink;
197 sink = GST_WAYLAND_SINK (bsink);
199 caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
201 GstCaps *intersection;
204 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
205 gst_caps_unref (caps);
212 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
214 GstWaylandSink *sink;
215 GstBufferPool *newpool;
217 GstStructure *structure;
218 static GstAllocationParams params = { 0, 0, 0, 15, };
221 sink = GST_WAYLAND_SINK (bsink);
223 GST_LOG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
225 if (!gst_video_info_from_caps (&info, caps))
228 if (!gst_wayland_sink_format_from_caps (&sink->format, caps))
231 if (!(sink->display->formats & (1 << sink->format))) {
232 GST_DEBUG_OBJECT (sink, "%s not available",
233 gst_wayland_format_to_string (sink->format));
237 sink->video_width = info.width;
238 sink->video_height = info.height;
241 /* create a new pool for the new configuration */
242 newpool = gst_wayland_buffer_pool_new (sink);
245 GST_DEBUG_OBJECT (sink, "Failed to create new pool");
249 structure = gst_buffer_pool_get_config (newpool);
250 gst_buffer_pool_config_set_params (structure, caps, size, 2, 0);
251 gst_buffer_pool_config_set_allocator (structure, NULL, ¶ms);
252 if (!gst_buffer_pool_set_config (newpool, structure))
255 gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
256 gst_object_unref (newpool);
262 GST_DEBUG_OBJECT (sink,
263 "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
268 GST_DEBUG_OBJECT (bsink, "failed setting config");
274 gst_wayland_sink_start (GstBaseSink * bsink)
276 GstWaylandSink *sink = (GstWaylandSink *) bsink;
277 gboolean result = TRUE;
278 GError *error = NULL;
280 GST_DEBUG_OBJECT (sink, "start");
283 sink->display = gst_wl_display_new (NULL, &error);
285 if (sink->display == NULL) {
286 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
287 ("Could not initialise Wayland output"),
288 ("Failed to create GstWlDisplay: '%s'", error->message));
296 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
298 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
299 GstBufferPool *pool = NULL;
300 GstStructure *config;
305 gst_query_parse_allocation (query, &caps, &need_pool);
311 pool = gst_object_ref (sink->pool);
316 /* we had a pool, check caps */
317 config = gst_buffer_pool_get_config (pool);
318 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
320 if (!gst_caps_is_equal (caps, pcaps)) {
321 /* different caps, we can't use this pool */
322 gst_object_unref (pool);
325 gst_structure_free (config);
328 if (pool == NULL && need_pool) {
331 if (!gst_video_info_from_caps (&info, caps))
334 GST_DEBUG_OBJECT (sink, "create new pool");
335 pool = gst_wayland_buffer_pool_new (sink);
337 /* the normal size of a frame */
340 config = gst_buffer_pool_get_config (pool);
341 gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
342 if (!gst_buffer_pool_set_config (pool, config))
346 gst_query_add_allocation_pool (query, pool, size, 2, 0);
347 gst_object_unref (pool);
355 GST_DEBUG_OBJECT (bsink, "no caps specified");
360 GST_DEBUG_OBJECT (bsink, "invalid caps specified");
365 GST_DEBUG_OBJECT (bsink, "failed setting config");
366 gst_object_unref (pool);
372 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
374 GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
375 return gst_wayland_sink_render (bsink, buffer);
379 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
381 GST_LOG ("frame_redraw_cb");
382 wl_callback_destroy (callback);
385 static const struct wl_callback_listener frame_callback_listener = {
386 frame_redraw_callback
390 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
392 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
393 GstVideoRectangle src, dst, res;
394 GstBuffer *to_render;
397 struct wl_surface *surface;
398 struct wl_callback *callback;
400 GST_LOG_OBJECT (sink, "render buffer %p", buffer);
402 sink->window = gst_wl_window_new_toplevel (sink->display, sink->video_width,
405 meta = gst_buffer_get_wl_meta (buffer);
407 if (meta && meta->sink == sink) {
408 GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
412 GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
417 if (!gst_buffer_pool_set_active (sink->pool, TRUE))
418 goto activate_failed;
420 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
421 if (ret != GST_FLOW_OK)
424 gst_buffer_map (buffer, &src, GST_MAP_READ);
425 gst_buffer_fill (to_render, 0, src.data, src.size);
426 gst_buffer_unmap (buffer, &src);
428 meta = gst_buffer_get_wl_meta (to_render);
431 src.w = sink->video_width;
432 src.h = sink->video_height;
433 dst.w = sink->window->width;
434 dst.h = sink->window->height;
436 gst_video_sink_center_rect (src, dst, &res, FALSE);
438 surface = gst_wl_window_get_wl_surface (sink->window);
440 wl_surface_attach (surface, meta->wbuffer, 0, 0);
441 wl_surface_damage (surface, 0, 0, res.w, res.h);
442 callback = wl_surface_frame (surface);
443 wl_callback_add_listener (callback, &frame_callback_listener, NULL);
444 wl_surface_commit (surface);
445 wl_display_flush (sink->display->display);
447 if (buffer != to_render)
448 gst_buffer_unref (to_render);
453 GST_WARNING_OBJECT (sink, "could not create image");
458 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
459 ("Internal error: can't allocate images"),
460 ("We don't have a bufferpool negotiated"));
461 return GST_FLOW_ERROR;
465 GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
466 ret = GST_FLOW_ERROR;
472 plugin_init (GstPlugin * plugin)
474 GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
475 " wayland video sink");
477 return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
478 GST_TYPE_WAYLAND_SINK);
481 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
484 "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,