waylandsink: implement with stubs the GstWaylandVideo & GstVideoOverlay interfaces
[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 #include <gst/wayland/wayland.h>
48 #include <gst/video/videooverlay.h>
49
50 /* signals */
51 enum
52 {
53   SIGNAL_0,
54   LAST_SIGNAL
55 };
56
57 /* Properties */
58 enum
59 {
60   PROP_0,
61   PROP_DISPLAY
62 };
63
64 GST_DEBUG_CATEGORY (gstwayland_debug);
65 #define GST_CAT_DEFAULT gstwayland_debug
66
67 #if G_BYTE_ORDER == G_BIG_ENDIAN
68 #define CAPS "{xRGB, ARGB}"
69 #else
70 #define CAPS "{BGRx, BGRA}"
71 #endif
72
73 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
74     GST_PAD_SINK,
75     GST_PAD_ALWAYS,
76     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (CAPS))
77     );
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 /* VideoOverlay interface */
96 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
97     iface);
98 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
99     guintptr handle);
100 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
101
102 /* WaylandVideo interface */
103 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
104     iface);
105 static void gst_wayland_sink_set_surface_size (GstWaylandVideo * video,
106     gint w, gint h);
107 static void gst_wayland_sink_pause_rendering (GstWaylandVideo * video);
108 static void gst_wayland_sink_resume_rendering (GstWaylandVideo * video);
109
110 #define gst_wayland_sink_parent_class parent_class
111 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
112     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
113         gst_wayland_sink_videooverlay_init)
114     G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
115         gst_wayland_sink_waylandvideo_init));
116
117 static void
118 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
119 {
120   GObjectClass *gobject_class;
121   GstElementClass *gstelement_class;
122   GstBaseSinkClass *gstbasesink_class;
123
124   gobject_class = (GObjectClass *) klass;
125   gstelement_class = (GstElementClass *) klass;
126   gstbasesink_class = (GstBaseSinkClass *) klass;
127
128   gobject_class->set_property = gst_wayland_sink_set_property;
129   gobject_class->get_property = gst_wayland_sink_get_property;
130   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
131
132   gst_element_class_add_pad_template (gstelement_class,
133       gst_static_pad_template_get (&sink_template));
134
135   gst_element_class_set_static_metadata (gstelement_class,
136       "wayland video sink", "Sink/Video",
137       "Output to wayland surface",
138       "Sreerenj Balachandran <sreerenj.balachandran@intel.com>");
139
140   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
141   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
142   gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_wayland_sink_start);
143   gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_wayland_sink_preroll);
144   gstbasesink_class->propose_allocation =
145       GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
146   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_wayland_sink_render);
147
148   g_object_class_install_property (gobject_class, PROP_DISPLAY,
149       g_param_spec_string ("display", "Wayland Display name", "Wayland "
150           "display name to connect to, if not supplied with GstVideoOverlay",
151           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152 }
153
154 static void
155 gst_wayland_sink_init (GstWaylandSink * sink)
156 {
157   sink->display = NULL;
158   sink->window = NULL;
159   sink->pool = NULL;
160 }
161
162 static void
163 gst_wayland_sink_get_property (GObject * object,
164     guint prop_id, GValue * value, GParamSpec * pspec)
165 {
166   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
167
168   switch (prop_id) {
169     case PROP_DISPLAY:
170       g_value_set_string (value, sink->display_name);
171       break;
172     default:
173       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174       break;
175   }
176 }
177
178 static void
179 gst_wayland_sink_set_property (GObject * object,
180     guint prop_id, const GValue * value, GParamSpec * pspec)
181 {
182   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
183
184   switch (prop_id) {
185     case PROP_DISPLAY:
186       sink->display_name = g_value_dup_string (value);
187       break;
188     default:
189       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190       break;
191   }
192 }
193
194 static void
195 gst_wayland_sink_finalize (GObject * object)
196 {
197   GstWaylandSink *sink = GST_WAYLAND_SINK (object);
198
199   GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
200
201   if (sink->window)
202     g_object_unref (sink->window);
203   if (sink->display)
204     g_object_unref (sink->display);
205   if (sink->pool)
206     gst_object_unref (sink->pool);
207
208   if (sink->display_name)
209     g_free (sink->display_name);
210
211   G_OBJECT_CLASS (parent_class)->finalize (object);
212 }
213
214 static GstCaps *
215 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
216 {
217   GstWaylandSink *sink;
218   GstCaps *caps;
219
220   sink = GST_WAYLAND_SINK (bsink);
221
222   caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
223   if (filter) {
224     GstCaps *intersection;
225
226     intersection =
227         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
228     gst_caps_unref (caps);
229     caps = intersection;
230   }
231   return caps;
232 }
233
234 static gboolean
235 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
236 {
237   GstWaylandSink *sink;
238   GstBufferPool *newpool;
239   GstVideoInfo info;
240   enum wl_shm_format format;
241   GArray *formats;
242   gint i;
243   GstStructure *structure;
244   static GstAllocationParams params = { 0, 0, 0, 15, };
245
246   sink = GST_WAYLAND_SINK (bsink);
247
248   GST_LOG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
249
250   /* extract info from caps */
251   if (!gst_video_info_from_caps (&info, caps))
252     goto invalid_format;
253
254   format = gst_video_format_to_wayland_format (GST_VIDEO_INFO_FORMAT (&info));
255   if (format == -1)
256     goto invalid_format;
257
258   /* verify we support the requested format */
259   formats = sink->display->formats;
260   for (i = 0; i < formats->len; i++) {
261     if (g_array_index (formats, uint32_t, i) == format)
262       break;
263   }
264
265   if (i >= formats->len)
266     goto unsupported_format;
267
268   /* store the video size */
269   sink->video_width = info.width;
270   sink->video_height = info.height;
271
272   /* create a new pool for the new configuration */
273   newpool = gst_wayland_buffer_pool_new (sink->display);
274
275   if (!newpool) {
276     GST_DEBUG_OBJECT (sink, "Failed to create new pool");
277     return FALSE;
278   }
279
280   structure = gst_buffer_pool_get_config (newpool);
281   gst_buffer_pool_config_set_params (structure, caps, info.size, 2, 0);
282   gst_buffer_pool_config_set_allocator (structure, NULL, &params);
283   if (!gst_buffer_pool_set_config (newpool, structure))
284     goto config_failed;
285
286   gst_object_replace ((GstObject **) & sink->pool, (GstObject *) newpool);
287   gst_object_unref (newpool);
288
289   return TRUE;
290
291 invalid_format:
292   {
293     GST_DEBUG_OBJECT (sink,
294         "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
295     return FALSE;
296   }
297 unsupported_format:
298   {
299     GST_DEBUG_OBJECT (sink, "Format %s is not available on the display",
300         gst_wayland_format_to_string (format));
301     return FALSE;
302   }
303 config_failed:
304   {
305     GST_DEBUG_OBJECT (bsink, "failed setting config");
306     return FALSE;
307   }
308 }
309
310 static gboolean
311 gst_wayland_sink_start (GstBaseSink * bsink)
312 {
313   GstWaylandSink *sink = (GstWaylandSink *) bsink;
314   gboolean result = TRUE;
315   GError *error = NULL;
316
317   GST_DEBUG_OBJECT (sink, "start");
318
319   if (!sink->display)
320     sink->display = gst_wl_display_new (sink->display_name, &error);
321
322   if (sink->display == NULL) {
323     GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
324         ("Could not initialise Wayland output"),
325         ("Failed to create GstWlDisplay: '%s'", error->message));
326     return FALSE;
327   }
328
329   return result;
330 }
331
332 static gboolean
333 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
334 {
335   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
336   GstBufferPool *pool = NULL;
337   GstStructure *config;
338   GstCaps *caps;
339   guint size;
340   gboolean need_pool;
341
342   gst_query_parse_allocation (query, &caps, &need_pool);
343
344   if (caps == NULL)
345     goto no_caps;
346
347   if (sink->pool)
348     pool = gst_object_ref (sink->pool);
349
350   if (pool != NULL) {
351     GstCaps *pcaps;
352
353     /* we had a pool, check caps */
354     config = gst_buffer_pool_get_config (pool);
355     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
356
357     if (!gst_caps_is_equal (caps, pcaps)) {
358       /* different caps, we can't use this pool */
359       gst_object_unref (pool);
360       pool = NULL;
361     }
362     gst_structure_free (config);
363   }
364
365   if (pool == NULL && need_pool) {
366     GstVideoInfo info;
367
368     if (!gst_video_info_from_caps (&info, caps))
369       goto invalid_caps;
370
371     GST_DEBUG_OBJECT (sink, "create new pool");
372     pool = gst_wayland_buffer_pool_new (sink->display);
373
374     /* the normal size of a frame */
375     size = info.size;
376
377     config = gst_buffer_pool_get_config (pool);
378     gst_buffer_pool_config_set_params (config, caps, size, 2, 0);
379     if (!gst_buffer_pool_set_config (pool, config))
380       goto config_failed;
381   }
382   if (pool) {
383     gst_query_add_allocation_pool (query, pool, size, 2, 0);
384     gst_object_unref (pool);
385   }
386
387   return TRUE;
388
389   /* ERRORS */
390 no_caps:
391   {
392     GST_DEBUG_OBJECT (bsink, "no caps specified");
393     return FALSE;
394   }
395 invalid_caps:
396   {
397     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
398     return FALSE;
399   }
400 config_failed:
401   {
402     GST_DEBUG_OBJECT (bsink, "failed setting config");
403     gst_object_unref (pool);
404     return FALSE;
405   }
406 }
407
408 static GstFlowReturn
409 gst_wayland_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
410 {
411   GST_DEBUG_OBJECT (bsink, "preroll buffer %p", buffer);
412   return gst_wayland_sink_render (bsink, buffer);
413 }
414
415 static void
416 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
417 {
418   GST_LOG ("frame_redraw_cb");
419   wl_callback_destroy (callback);
420 }
421
422 static const struct wl_callback_listener frame_callback_listener = {
423   frame_redraw_callback
424 };
425
426 static GstFlowReturn
427 gst_wayland_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
428 {
429   GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
430   GstVideoRectangle src, dst, res;
431   GstBuffer *to_render;
432   GstWlMeta *meta;
433   GstFlowReturn ret;
434   struct wl_surface *surface;
435   struct wl_callback *callback;
436
437   GST_LOG_OBJECT (sink, "render buffer %p", buffer);
438   if (!sink->window)
439     sink->window = gst_wl_window_new_toplevel (sink->display, sink->video_width,
440         sink->video_height);
441
442   meta = gst_buffer_get_wl_meta (buffer);
443
444   if (meta && meta->display == sink->display) {
445     GST_LOG_OBJECT (sink, "buffer %p from our pool, writing directly", buffer);
446     to_render = buffer;
447   } else {
448     GstMapInfo src;
449     GST_LOG_OBJECT (sink, "buffer %p not from our pool, copying", buffer);
450
451     if (!sink->pool)
452       goto no_pool;
453
454     if (!gst_buffer_pool_set_active (sink->pool, TRUE))
455       goto activate_failed;
456
457     ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
458     if (ret != GST_FLOW_OK)
459       goto no_buffer;
460
461     gst_buffer_map (buffer, &src, GST_MAP_READ);
462     gst_buffer_fill (to_render, 0, src.data, src.size);
463     gst_buffer_unmap (buffer, &src);
464
465     meta = gst_buffer_get_wl_meta (to_render);
466   }
467
468   src.w = sink->video_width;
469   src.h = sink->video_height;
470   dst.w = sink->window->width;
471   dst.h = sink->window->height;
472
473   gst_video_sink_center_rect (src, dst, &res, FALSE);
474
475   surface = gst_wl_window_get_wl_surface (sink->window);
476
477   wl_surface_attach (surface, meta->wbuffer, 0, 0);
478   wl_surface_damage (surface, 0, 0, res.w, res.h);
479   callback = wl_surface_frame (surface);
480   wl_callback_add_listener (callback, &frame_callback_listener, NULL);
481   wl_surface_commit (surface);
482   wl_display_flush (sink->display->display);
483
484   if (buffer != to_render)
485     gst_buffer_unref (to_render);
486   return GST_FLOW_OK;
487
488 no_buffer:
489   {
490     GST_WARNING_OBJECT (sink, "could not create image");
491     return ret;
492   }
493 no_pool:
494   {
495     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
496         ("Internal error: can't allocate images"),
497         ("We don't have a bufferpool negotiated"));
498     return GST_FLOW_ERROR;
499   }
500 activate_failed:
501   {
502     GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
503     ret = GST_FLOW_ERROR;
504     return ret;
505   }
506 }
507
508 static void
509 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
510 {
511   iface->set_window_handle = gst_wayland_sink_set_window_handle;
512   iface->expose = gst_wayland_sink_expose;
513 }
514
515 static void
516 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
517 {
518   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
519   g_return_if_fail (sink != NULL);
520 }
521
522 static void
523 gst_wayland_sink_expose (GstVideoOverlay * overlay)
524 {
525   GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
526   g_return_if_fail (sink != NULL);
527 }
528
529 static void
530 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
531 {
532   iface->set_surface_size = gst_wayland_sink_set_surface_size;
533   iface->pause_rendering = gst_wayland_sink_pause_rendering;
534   iface->resume_rendering = gst_wayland_sink_resume_rendering;
535 }
536
537 static void
538 gst_wayland_sink_set_surface_size (GstWaylandVideo * video, gint w, gint h)
539 {
540   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
541   g_return_if_fail (sink != NULL);
542 }
543
544 static void
545 gst_wayland_sink_pause_rendering (GstWaylandVideo * video)
546 {
547   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
548   g_return_if_fail (sink != NULL);
549 }
550
551 static void
552 gst_wayland_sink_resume_rendering (GstWaylandVideo * video)
553 {
554   GstWaylandSink *sink = GST_WAYLAND_SINK (video);
555   g_return_if_fail (sink != NULL);
556 }
557
558 static gboolean
559 plugin_init (GstPlugin * plugin)
560 {
561   GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
562       " wayland video sink");
563
564   return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
565       GST_TYPE_WAYLAND_SINK);
566 }
567
568 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
569     GST_VERSION_MINOR,
570     waylandsink,
571     "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
572     GST_PACKAGE_ORIGIN)