waylandsink: split window-related code out to a new GstWlWindow class
[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
46 /* signals */
47 enum
48 {
49   SIGNAL_0,
50   LAST_SIGNAL
51 };
52
53 /* Properties */
54 enum
55 {
56   PROP_0,
57   PROP_WAYLAND_DISPLAY
58 };
59
60 GST_DEBUG_CATEGORY (gstwayland_debug);
61 #define GST_CAT_DEFAULT gstwayland_debug
62
63 #if G_BYTE_ORDER == G_BIG_ENDIAN
64 #define CAPS "{xRGB, ARGB}"
65 #else
66 #define CAPS "{BGRx, BGRA}"
67 #endif
68
69 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
70     GST_PAD_SINK,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (CAPS))
73     );
74
75 /*Fixme: Add more interfaces */
76 #define gst_wayland_sink_parent_class parent_class
77 G_DEFINE_TYPE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK);
78
79 static void gst_wayland_sink_get_property (GObject * object,
80     guint prop_id, GValue * value, GParamSpec * pspec);
81 static void gst_wayland_sink_set_property (GObject * object,
82     guint prop_id, const GValue * value, GParamSpec * pspec);
83 static void gst_wayland_sink_finalize (GObject * object);
84 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
85     GstCaps * filter);
86 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
87 static gboolean gst_wayland_sink_start (GstBaseSink * bsink);
88 static gboolean gst_wayland_sink_preroll (GstBaseSink * bsink,
89     GstBuffer * buffer);
90 static gboolean
91 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
92 static gboolean gst_wayland_sink_render (GstBaseSink * bsink,
93     GstBuffer * buffer);
94
95 static void frame_redraw_callback (void *data,
96     struct wl_callback *callback, uint32_t time);
97
98 static void
99 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
100 {
101   GObjectClass *gobject_class;
102   GstElementClass *gstelement_class;
103   GstBaseSinkClass *gstbasesink_class;
104
105   gobject_class = (GObjectClass *) klass;
106   gstelement_class = (GstElementClass *) klass;
107   gstbasesink_class = (GstBaseSinkClass *) klass;
108
109   gobject_class->set_property = gst_wayland_sink_set_property;
110   gobject_class->get_property = gst_wayland_sink_get_property;
111   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
112
113   gst_element_class_add_pad_template (gstelement_class,
114       gst_static_pad_template_get (&sink_template));
115
116   gst_element_class_set_static_metadata (gstelement_class,
117       "wayland video sink", "Sink/Video",
118       "Output to wayland surface",
119       "Sreerenj Balachandran <sreerenj.balachandran@intel.com>");
120
121   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
122   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
123   gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_wayland_sink_start);
124   gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
125   gstbasesink_class->propose_allocation =
126       GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
127   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
128
129   g_object_class_install_property (gobject_class, PROP_WAYLAND_DISPLAY,
130       g_param_spec_pointer ("wayland-display", "Wayland Display",
131           "Wayland  Display handle created by the application ",
132           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
133 }
134
135 static void
136 gst_wayland_sink_init (GstWaylandSink * sink)
137 {
138   sink->display = NULL;
139   sink->window = NULL;
140   sink->pool = NULL;
141
142   g_mutex_init (&sink->wayland_lock);
143 }
144
145 static void
146 gst_wayland_sink_get_property (GObject * object,
147     guint prop_id, GValue * value, GParamSpec * pspec)
148 {
149   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
150
151   switch (prop_id) {
152     case PROP_WAYLAND_DISPLAY:
153       g_value_set_pointer (value, sink->display);
154       break;
155     default:
156       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157       break;
158   }
159 }
160
161 static void
162 gst_wayland_sink_set_property (GObject * object,
163     guint prop_id, const GValue * value, GParamSpec * pspec)
164 {
165   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
166
167   switch (prop_id) {
168     case PROP_WAYLAND_DISPLAY:
169       sink->display = g_value_get_pointer (value);
170       break;
171     default:
172       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173       break;
174   }
175 }
176
177 static void
178 gst_wayland_sink_finalize (GObject * object)
179 {
180   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
181
182   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
183
184   if (sink->window)
185     g_object_unref (sink->window);
186   if (sink->display)
187     g_object_unref (sink->display);
188
189   g_mutex_clear (&sink->wayland_lock);
190
191   G_OBJECT_CLASS (parent_class)->finalize (object);
192 }
193
194 static GstCaps *
195 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
196 {
197   GstWaylandSink *sink;
198   GstCaps *caps;
199
200   sink = GST_WAYLAND_SINK (bsink);
201
202   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
203   if (filter) {
204     GstCaps *intersection;
205
206     intersection =
207         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
208     gst_caps_unref (caps);
209     caps = intersection;
210   }
211   return caps;
212 }
213
214 static gboolean
215 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
216 {
217   GstWaylandSink *sink;
218   GstBufferPool *newpool, *oldpool;
219   GstVideoInfo info;
220   GstStructure *structure;
221   static GstAllocationParams params = { 0, 0, 0, 15, };
222   guint size;
223
224   sink = GST_WAYLAND_SINK (bsink);
225
226   GST_LOG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
227
228   if (!gst_video_info_from_caps (&info, caps))
229     goto invalid_format;
230
231   if (!gst_wayland_sink_format_from_caps (&sink->format, caps))
232     goto invalid_format;
233
234   if (!(sink->display->formats & (1 << sink->format))) {
235     GST_DEBUG_OBJECT (sink, "%s not available",
236         gst_wayland_format_to_string (sink->format));
237     return FALSE;
238   }
239
240   sink->video_width = info.width;
241   sink->video_height = info.height;
242   size = info.size;
243
244   /* create a new pool for the new configuration */
245   newpool = gst_wayland_buffer_pool_new (sink);
246
247   if (!newpool) {
248     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
249     return FALSE;
250   }
251
252   structure = gst_buffer_pool_get_config (newpool);
253   gst_buffer_pool_config_set_params (structure, caps, size, 2, 0);
254   gst_buffer_pool_config_set_allocator (structure, NULL, &params);
255   if (!gst_buffer_pool_set_config (newpool, structure))
256     goto config_failed;
257
258   oldpool = sink->pool;
259   sink->pool = newpool;
260   if (oldpool)
261     gst_object_unref (oldpool);
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 (NULL, &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;
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   g_mutex_lock (&sink->wayland_lock);
316   if ((pool = sink->pool))
317     gst_object_ref (pool);
318   g_mutex_unlock (&sink->wayland_lock);
319
320   if (pool != NULL) {
321     GstCaps *pcaps;
322
323     /* we had a pool, check caps */
324     config = gst_buffer_pool_get_config (pool);
325     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
326
327     if (!gst_caps_is_equal (caps, pcaps)) {
328       /* different caps, we can't use this pool */
329       gst_object_unref (pool);
330       pool = NULL;
331     }
332     gst_structure_free (config);
333   }
334
335   if (pool == NULL && need_pool) {
336     GstVideoInfo info;
337
338     if (!gst_video_info_from_caps (&info, caps))
339       goto invalid_caps;
340
341     GST_DEBUG_OBJECT (sink, "create new pool");
342     pool = gst_wayland_buffer_pool_new (sink);
343
344     /* the normal size of a frame */
345     size = info.size;
346
347     config = gst_buffer_pool_get_config (pool);
348     gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
349     if (!gst_buffer_pool_set_config (pool, config))
350       goto config_failed;
351   }
352   if (pool) {
353     gst_query_add_allocation_pool (query, pool, size, 2, 0);
354     gst_object_unref (pool);
355   }
356
357   return TRUE;
358
359   /* ERRORS */
360 no_caps:
361   {
362     GST_DEBUG_OBJECT (bsink, "no caps specified");
363     return FALSE;
364   }
365 invalid_caps:
366   {
367     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
368     return FALSE;
369   }
370 config_failed:
371   {
372     GST_DEBUG_OBJECT (bsink, "failed setting config");
373     gst_object_unref (pool);
374     return FALSE;
375   }
376 }
377
378 static GstFlowReturn
379 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
380 {
381   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
382   return gst_wayland_sink_render (bsink, buffer);
383 }
384
385 static void
386 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
387 {
388   GST_LOG ("frame_redraw_cb");
389   wl_callback_destroy (callback);
390 }
391
392 static const struct wl_callback_listener frame_callback_listener = {
393   frame_redraw_callback
394 };
395
396 static GstFlowReturn
397 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
398 {
399   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
400   GstVideoRectangle src, dst, res;
401   GstBuffer *to_render;
402   GstWlMeta *meta;
403   GstFlowReturn ret;
404   struct wl_surface *surface;
405   struct wl_callback *callback;
406
407   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
408   if (!sink->window)
409     sink->window = gst_wl_window_new_toplevel (sink->display, sink->video_width,
410         sink->video_height);
411
412   meta = gst_buffer_get_wl_meta (buffer);
413
414   if (meta && meta->sink == sink) {
415     GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
416     to_render = buffer;
417   } else {
418     GstMapInfo src;
419     GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
420
421     if (!sink->pool)
422       goto no_pool;
423
424     if (!gst_buffer_pool_set_active (sink->pool, TRUE))
425       goto activate_failed;
426
427     ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
428     if (ret != GST_FLOW_OK)
429       goto no_buffer;
430
431     gst_buffer_map (buffer, &src, GST_MAP_READ);
432     gst_buffer_fill (to_render, 0, src.data, src.size);
433     gst_buffer_unmap (buffer, &src);
434
435     meta = gst_buffer_get_wl_meta (to_render);
436   }
437
438   src.w = sink->video_width;
439   src.h = sink->video_height;
440   dst.w = sink->window->width;
441   dst.h = sink->window->height;
442
443   gst_video_sink_center_rect (src, dst, &res, FALSE);
444
445   surface = gst_wl_window_get_wl_surface (sink->window);
446
447   wl_surface_attach (surface, meta->wbuffer, 0, 0);
448   wl_surface_damage (surface, 0, 0, res.w, res.h);
449   callback = wl_surface_frame (surface);
450   wl_callback_add_listener (callback, &frame_callback_listener, NULL);
451   wl_surface_commit (surface);
452   wl_display_flush (sink->display->display);
453
454   if (buffer != to_render)
455     gst_buffer_unref (to_render);
456   return GST_FLOW_OK;
457
458 no_buffer:
459   {
460     GST_WARNING_OBJECT (sink, "could not create image");
461     return ret;
462   }
463 no_pool:
464   {
465     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
466         ("Internal error: can't allocate images"),
467         ("We don't have a bufferpool negotiated"));
468     return GST_FLOW_ERROR;
469   }
470 activate_failed:
471   {
472     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
473     ret = GST_FLOW_ERROR;
474     return ret;
475   }
476 }
477
478 static gboolean
479 plugin_init (GstPlugin * plugin)
480 {
481   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
482       " wayland video sink");
483
484   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
485       GST_TYPE_WAYLAND_SINK);
486 }
487
488 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
489     GST_VERSION_MINOR,
490     waylandsink,
491     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
492     GST_PACKAGE_ORIGIN)