waylandsink: unref the buffer pool
[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_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_DISPLAY,
131       g_param_spec_string ("display", "Wayland Display name", "Wayland "
132           "display name to connect to, if not supplied with GstVideoOverlay",
133           NULL, 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_DISPLAY:
152       g_value_set_string (value, sink->display_name);
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_DISPLAY:
168       sink->display_name = g_value_dup_string (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   if (sink->pool)
188     gst_object_unref (sink->pool);
189
190   if (sink->display_name)
191     g_free (sink->display_name);
192
193   G_OBJECT_CLASS (parent_class)->finalize (object);
194 }
195
196 static GstCaps *
197 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
198 {
199   GstWaylandSink *sink;
200   GstCaps *caps;
201
202   sink = GST_WAYLAND_SINK (bsink);
203
204   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
205   if (filter) {
206     GstCaps *intersection;
207
208     intersection =
209         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
210     gst_caps_unref (caps);
211     caps = intersection;
212   }
213   return caps;
214 }
215
216 static gboolean
217 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
218 {
219   GstWaylandSink *sink;
220   GstBufferPool *newpool;
221   GstVideoInfo info;
222   GstStructure *structure;
223   static GstAllocationParams params = { 0, 0, 0, 15, };
224   guint size;
225
226   sink = GST_WAYLAND_SINK (bsink);
227
228   GST_LOG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
229
230   if (!gst_video_info_from_caps (&info, caps))
231     goto invalid_format;
232
233   if (!gst_wayland_sink_format_from_caps (&sink->format, caps))
234     goto invalid_format;
235
236   if (!(sink->display->formats & (1 << sink->format))) {
237     GST_DEBUG_OBJECT (sink, "%s not available",
238         gst_wayland_format_to_string (sink->format));
239     return FALSE;
240   }
241
242   sink->video_width = info.width;
243   sink->video_height = info.height;
244   size = info.size;
245
246   /* create a new pool for the new configuration */
247   newpool = gst_wayland_buffer_pool_new (sink->display);
248
249   if (!newpool) {
250     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
251     return FALSE;
252   }
253
254   structure = gst_buffer_pool_get_config (newpool);
255   gst_buffer_pool_config_set_params (structure, caps, size, 2, 0);
256   gst_buffer_pool_config_set_allocator (structure, NULL, &params);
257   if (!gst_buffer_pool_set_config (newpool, structure))
258     goto config_failed;
259
260   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
261   gst_object_unref (newpool);
262
263   return TRUE;
264
265 invalid_format:
266   {
267     GST_DEBUG_OBJECT (sink,
268         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
269     return FALSE;
270   }
271 config_failed:
272   {
273     GST_DEBUG_OBJECT (bsink, "failed setting config");
274     return FALSE;
275   }
276 }
277
278 static gboolean
279 gst_wayland_sink_start (GstBaseSink * bsink)
280 {
281   GstWaylandSink *sink = (GstWaylandSink *) bsink;
282   gboolean result = TRUE;
283   GError *error = NULL;
284
285   GST_DEBUG_OBJECT (sink, "start");
286
287   if (!sink->display)
288     sink->display = gst_wl_display_new (sink->display_name, &error);
289
290   if (sink->display == NULL) {
291     GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
292         ("Could not initialise Wayland output"),
293         ("Failed to create GstWlDisplay: '%s'", error->message));
294     return FALSE;
295   }
296
297   return result;
298 }
299
300 static gboolean
301 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
302 {
303   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
304   GstBufferPool *pool = NULL;
305   GstStructure *config;
306   GstCaps *caps;
307   guint size;
308   gboolean need_pool;
309
310   gst_query_parse_allocation (query, &caps, &need_pool);
311
312   if (caps == NULL)
313     goto no_caps;
314
315   if (sink->pool)
316     pool = gst_object_ref (sink->pool);
317
318   if (pool != NULL) {
319     GstCaps *pcaps;
320
321     /* we had a pool, check caps */
322     config = gst_buffer_pool_get_config (pool);
323     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
324
325     if (!gst_caps_is_equal (caps, pcaps)) {
326       /* different caps, we can't use this pool */
327       gst_object_unref (pool);
328       pool = NULL;
329     }
330     gst_structure_free (config);
331   }
332
333   if (pool == NULL && need_pool) {
334     GstVideoInfo info;
335
336     if (!gst_video_info_from_caps (&info, caps))
337       goto invalid_caps;
338
339     GST_DEBUG_OBJECT (sink, "create new pool");
340     pool = gst_wayland_buffer_pool_new (sink->display);
341
342     /* the normal size of a frame */
343     size = info.size;
344
345     config = gst_buffer_pool_get_config (pool);
346     gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
347     if (!gst_buffer_pool_set_config (pool, config))
348       goto config_failed;
349   }
350   if (pool) {
351     gst_query_add_allocation_pool (query, pool, size, 2, 0);
352     gst_object_unref (pool);
353   }
354
355   return TRUE;
356
357   /* ERRORS */
358 no_caps:
359   {
360     GST_DEBUG_OBJECT (bsink, "no caps specified");
361     return FALSE;
362   }
363 invalid_caps:
364   {
365     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
366     return FALSE;
367   }
368 config_failed:
369   {
370     GST_DEBUG_OBJECT (bsink, "failed setting config");
371     gst_object_unref (pool);
372     return FALSE;
373   }
374 }
375
376 static GstFlowReturn
377 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
378 {
379   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
380   return gst_wayland_sink_render (bsink, buffer);
381 }
382
383 static void
384 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
385 {
386   GST_LOG ("frame_redraw_cb");
387   wl_callback_destroy (callback);
388 }
389
390 static const struct wl_callback_listener frame_callback_listener = {
391   frame_redraw_callback
392 };
393
394 static GstFlowReturn
395 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
396 {
397   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
398   GstVideoRectangle src, dst, res;
399   GstBuffer *to_render;
400   GstWlMeta *meta;
401   GstFlowReturn ret;
402   struct wl_surface *surface;
403   struct wl_callback *callback;
404
405   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
406   if (!sink->window)
407     sink->window = gst_wl_window_new_toplevel (sink->display, sink->video_width,
408         sink->video_height);
409
410   meta = gst_buffer_get_wl_meta (buffer);
411
412   if (meta && meta->display == sink->display) {
413     GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
414     to_render = buffer;
415   } else {
416     GstMapInfo src;
417     GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
418
419     if (!sink->pool)
420       goto no_pool;
421
422     if (!gst_buffer_pool_set_active (sink->pool, TRUE))
423       goto activate_failed;
424
425     ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
426     if (ret != GST_FLOW_OK)
427       goto no_buffer;
428
429     gst_buffer_map (buffer, &src, GST_MAP_READ);
430     gst_buffer_fill (to_render, 0, src.data, src.size);
431     gst_buffer_unmap (buffer, &src);
432
433     meta = gst_buffer_get_wl_meta (to_render);
434   }
435
436   src.w = sink->video_width;
437   src.h = sink->video_height;
438   dst.w = sink->window->width;
439   dst.h = sink->window->height;
440
441   gst_video_sink_center_rect (src, dst, &res, FALSE);
442
443   surface = gst_wl_window_get_wl_surface (sink->window);
444
445   wl_surface_attach (surface, meta->wbuffer, 0, 0);
446   wl_surface_damage (surface, 0, 0, res.w, res.h);
447   callback = wl_surface_frame (surface);
448   wl_callback_add_listener (callback, &frame_callback_listener, NULL);
449   wl_surface_commit (surface);
450   wl_display_flush (sink->display->display);
451
452   if (buffer != to_render)
453     gst_buffer_unref (to_render);
454   return GST_FLOW_OK;
455
456 no_buffer:
457   {
458     GST_WARNING_OBJECT (sink, "could not create image");
459     return ret;
460   }
461 no_pool:
462   {
463     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
464         ("Internal error: can't allocate images"),
465         ("We don't have a bufferpool negotiated"));
466     return GST_FLOW_ERROR;
467   }
468 activate_failed:
469   {
470     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
471     ret = GST_FLOW_ERROR;
472     return ret;
473   }
474 }
475
476 static gboolean
477 plugin_init (GstPlugin * plugin)
478 {
479   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
480       " wayland video sink");
481
482   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
483       GST_TYPE_WAYLAND_SINK);
484 }
485
486 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
487     GST_VERSION_MINOR,
488     waylandsink,
489     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
490     GST_PACKAGE_ORIGIN)