waylandsink: access sink->pool in a more atomic fashion
[platform/upstream/gstreamer.git] / ext / wayland / gstwaylandsink.c
1 /* GStreamer Wayland video sink
2  *
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  *
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.
11  *
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.
16  *
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.
21  */
22
23 /**
24  * SECTION:element-waylandsink
25  *
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.
30  *
31  * <refsect2>
32  * <title>Example pipelines</title>
33  * |[
34  * gst-launch -v videotestsrc ! waylandsink
35  * ]| test the video rendering in wayland
36  * </refsect2>
37  */
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #include "gstwaylandsink.h"
44 #include "wlvideoformat.h"
45 #include "waylandpool.h"
46
47 /* signals */
48 enum
49 {
50   SIGNAL_0,
51   LAST_SIGNAL
52 };
53
54 /* Properties */
55 enum
56 {
57   PROP_0,
58   PROP_WAYLAND_DISPLAY
59 };
60
61 GST_DEBUG_CATEGORY (gstwayland_debug);
62 #define GST_CAT_DEFAULT gstwayland_debug
63
64 #if G_BYTE_ORDER == G_BIG_ENDIAN
65 #define CAPS "{xRGB, ARGB}"
66 #else
67 #define CAPS "{BGRx, BGRA}"
68 #endif
69
70 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
71     GST_PAD_SINK,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (CAPS))
74     );
75
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);
79
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,
86     GstCaps * filter);
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,
90     GstBuffer * buffer);
91 static gboolean
92 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
93 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
94     GstBuffer * buffer);
95
96 static void frame_redraw_callback (void *data,
97     struct wl_callback *callback, uint32_t time);
98
99 static void
100 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
101 {
102   GObjectClass *gobject_class;
103   GstElementClass *gstelement_class;
104   GstBaseSinkClass *gstbasesink_class;
105
106   gobject_class = (GObjectClass *) klass;
107   gstelement_class = (GstElementClass *) klass;
108   gstbasesink_class = (GstBaseSinkClass *) klass;
109
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);
113
114   gst_element_class_add_pad_template (gstelement_class,
115       gst_static_pad_template_get (&sink_template));
116
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>");
121
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);
129
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));
134 }
135
136 static void
137 gst_wayland_sink_init (GstWaylandSink * sink)
138 {
139   sink->display = NULL;
140   sink->window = NULL;
141   sink->pool = NULL;
142 }
143
144 static void
145 gst_wayland_sink_get_property (GObject * object,
146     guint prop_id, GValue * value, GParamSpec * pspec)
147 {
148   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
149
150   switch (prop_id) {
151     case PROP_WAYLAND_DISPLAY:
152       g_value_set_pointer (value, sink->display);
153       break;
154     default:
155       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
156       break;
157   }
158 }
159
160 static void
161 gst_wayland_sink_set_property (GObject * object,
162     guint prop_id, const GValue * value, GParamSpec * pspec)
163 {
164   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
165
166   switch (prop_id) {
167     case PROP_WAYLAND_DISPLAY:
168       sink->display = g_value_get_pointer (value);
169       break;
170     default:
171       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172       break;
173   }
174 }
175
176 static void
177 gst_wayland_sink_finalize (GObject * object)
178 {
179   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
180
181   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
182
183   if (sink->window)
184     g_object_unref (sink->window);
185   if (sink->display)
186     g_object_unref (sink->display);
187
188   G_OBJECT_CLASS (parent_class)->finalize (object);
189 }
190
191 static GstCaps *
192 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
193 {
194   GstWaylandSink *sink;
195   GstCaps *caps;
196
197   sink = GST_WAYLAND_SINK (bsink);
198
199   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
200   if (filter) {
201     GstCaps *intersection;
202
203     intersection =
204         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
205     gst_caps_unref (caps);
206     caps = intersection;
207   }
208   return caps;
209 }
210
211 static gboolean
212 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
213 {
214   GstWaylandSink *sink;
215   GstBufferPool *newpool;
216   GstVideoInfo info;
217   GstStructure *structure;
218   static GstAllocationParams params = { 0, 0, 0, 15, };
219   guint size;
220
221   sink = GST_WAYLAND_SINK (bsink);
222
223   GST_LOG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
224
225   if (!gst_video_info_from_caps (&info, caps))
226     goto invalid_format;
227
228   if (!gst_wayland_sink_format_from_caps (&sink->format, caps))
229     goto invalid_format;
230
231   if (!(sink->display->formats & (1 << sink->format))) {
232     GST_DEBUG_OBJECT (sink, "%s not available",
233         gst_wayland_format_to_string (sink->format));
234     return FALSE;
235   }
236
237   sink->video_width = info.width;
238   sink->video_height = info.height;
239   size = info.size;
240
241   /* create a new pool for the new configuration */
242   newpool = gst_wayland_buffer_pool_new (sink);
243
244   if (!newpool) {
245     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
246     return FALSE;
247   }
248
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, &params);
252   if (!gst_buffer_pool_set_config (newpool, structure))
253     goto config_failed;
254
255   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
256   gst_object_unref (newpool);
257
258   return TRUE;
259
260 invalid_format:
261   {
262     GST_DEBUG_OBJECT (sink,
263         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
264     return FALSE;
265   }
266 config_failed:
267   {
268     GST_DEBUG_OBJECT (bsink, "failed setting config");
269     return FALSE;
270   }
271 }
272
273 static gboolean
274 gst_wayland_sink_start (GstBaseSink * bsink)
275 {
276   GstWaylandSink *sink = (GstWaylandSink *) bsink;
277   gboolean result = TRUE;
278   GError *error = NULL;
279
280   GST_DEBUG_OBJECT (sink, "start");
281
282   if (!sink->display)
283     sink->display = gst_wl_display_new (NULL, &error);
284
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));
289     return FALSE;
290   }
291
292   return result;
293 }
294
295 static gboolean
296 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
297 {
298   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
299   GstBufferPool *pool = NULL;
300   GstStructure *config;
301   GstCaps *caps;
302   guint size;
303   gboolean need_pool;
304
305   gst_query_parse_allocation (query, &caps, &need_pool);
306
307   if (caps == NULL)
308     goto no_caps;
309
310   if (sink->pool)
311     pool = gst_object_ref (sink->pool);
312
313   if (pool != NULL) {
314     GstCaps *pcaps;
315
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);
319
320     if (!gst_caps_is_equal (caps, pcaps)) {
321       /* different caps, we can't use this pool */
322       gst_object_unref (pool);
323       pool = NULL;
324     }
325     gst_structure_free (config);
326   }
327
328   if (pool == NULL && need_pool) {
329     GstVideoInfo info;
330
331     if (!gst_video_info_from_caps (&info, caps))
332       goto invalid_caps;
333
334     GST_DEBUG_OBJECT (sink, "create new pool");
335     pool = gst_wayland_buffer_pool_new (sink);
336
337     /* the normal size of a frame */
338     size = info.size;
339
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))
343       goto config_failed;
344   }
345   if (pool) {
346     gst_query_add_allocation_pool (query, pool, size, 2, 0);
347     gst_object_unref (pool);
348   }
349
350   return TRUE;
351
352   /* ERRORS */
353 no_caps:
354   {
355     GST_DEBUG_OBJECT (bsink, "no caps specified");
356     return FALSE;
357   }
358 invalid_caps:
359   {
360     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
361     return FALSE;
362   }
363 config_failed:
364   {
365     GST_DEBUG_OBJECT (bsink, "failed setting config");
366     gst_object_unref (pool);
367     return FALSE;
368   }
369 }
370
371 static GstFlowReturn
372 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
373 {
374   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
375   return gst_wayland_sink_render (bsink, buffer);
376 }
377
378 static void
379 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
380 {
381   GST_LOG ("frame_redraw_cb");
382   wl_callback_destroy (callback);
383 }
384
385 static const struct wl_callback_listener frame_callback_listener = {
386   frame_redraw_callback
387 };
388
389 static GstFlowReturn
390 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
391 {
392   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
393   GstVideoRectangle src, dst, res;
394   GstBuffer *to_render;
395   GstWlMeta *meta;
396   GstFlowReturn ret;
397   struct wl_surface *surface;
398   struct wl_callback *callback;
399
400   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
401   if (!sink->window)
402     sink->window = gst_wl_window_new_toplevel (sink->display, sink->video_width,
403         sink->video_height);
404
405   meta = gst_buffer_get_wl_meta (buffer);
406
407   if (meta && meta->sink == sink) {
408     GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
409     to_render = buffer;
410   } else {
411     GstMapInfo src;
412     GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
413
414     if (!sink->pool)
415       goto no_pool;
416
417     if (!gst_buffer_pool_set_active (sink->pool, TRUE))
418       goto activate_failed;
419
420     ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
421     if (ret != GST_FLOW_OK)
422       goto no_buffer;
423
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);
427
428     meta = gst_buffer_get_wl_meta (to_render);
429   }
430
431   src.w = sink->video_width;
432   src.h = sink->video_height;
433   dst.w = sink->window->width;
434   dst.h = sink->window->height;
435
436   gst_video_sink_center_rect (src, dst, &res, FALSE);
437
438   surface = gst_wl_window_get_wl_surface (sink->window);
439
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);
446
447   if (buffer != to_render)
448     gst_buffer_unref (to_render);
449   return GST_FLOW_OK;
450
451 no_buffer:
452   {
453     GST_WARNING_OBJECT (sink, "could not create image");
454     return ret;
455   }
456 no_pool:
457   {
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;
462   }
463 activate_failed:
464   {
465     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
466     ret = GST_FLOW_ERROR;
467     return ret;
468   }
469 }
470
471 static gboolean
472 plugin_init (GstPlugin * plugin)
473 {
474   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
475       " wayland video sink");
476
477   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
478       GST_TYPE_WAYLAND_SINK);
479 }
480
481 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
482     GST_VERSION_MINOR,
483     waylandsink,
484     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
485     GST_PACKAGE_ORIGIN)