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 * Copyright (C) 2014 Collabora Ltd.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301 USA.
22 */
23
24 /**
25 * SECTION:element-waylandsink
26 * @title: waylandsink
27 *
28 * The waylandsink is creating its own window and render the decoded video frames to that.
29 * Setup the Wayland environment as described in
30 * <ulink url="http://wayland.freedesktop.org/building.html">Wayland</ulink> home page.
31 * The current implementaion is based on weston compositor.
32 *
33 * ## Example pipelines
34 * |[
35 * gst-launch-1.0 -v videotestsrc ! waylandsink
36 * ]| test the video rendering in wayland
37 *
38 */
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include "gstwaylandsink.h"
45 #include "wlvideoformat.h"
46 #include "wlbuffer.h"
47 #include "wlshmallocator.h"
48 #include "wllinuxdmabuf.h"
49
50 #include <gst/wayland/wayland.h>
51 #include <gst/video/videooverlay.h>
52
53 /* signals */
54 enum
55 {
56 SIGNAL_0,
57 LAST_SIGNAL
58 };
59
60 /* Properties */
61 enum
62 {
63 PROP_0,
64 PROP_DISPLAY,
65 PROP_FULLSCREEN
66 };
67
68 GST_DEBUG_CATEGORY (gstwayland_debug);
69 #define GST_CAT_DEFAULT gstwayland_debug
70
71 #define WL_VIDEO_FORMATS \
72 "{ BGRx, BGRA, RGBx, xBGR, xRGB, RGBA, ABGR, ARGB, RGB, BGR, " \
73 "RGB16, BGR16, YUY2, YVYU, UYVY, AYUV, NV12, NV21, NV16, " \
74 "YUV9, YVU9, Y41B, I420, YV12, Y42B, v308 }"
75
76 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
77 GST_PAD_SINK,
78 GST_PAD_ALWAYS,
79 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (WL_VIDEO_FORMATS) ";"
80 GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_DMABUF,
81 WL_VIDEO_FORMATS))
82 );
83
84 static void gst_wayland_sink_get_property (GObject * object,
85 guint prop_id, GValue * value, GParamSpec * pspec);
86 static void gst_wayland_sink_set_property (GObject * object,
87 guint prop_id, const GValue * value, GParamSpec * pspec);
88 static void gst_wayland_sink_finalize (GObject * object);
89
90 static GstStateChangeReturn gst_wayland_sink_change_state (GstElement * element,
91 GstStateChange transition);
92 static void gst_wayland_sink_set_context (GstElement * element,
93 GstContext * context);
94
95 static GstCaps *gst_wayland_sink_get_caps (GstBaseSink * bsink,
96 GstCaps * filter);
97 static gboolean gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
98 static gboolean
99 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query);
100 static gboolean gst_wayland_sink_show_frame (GstVideoSink * bsink,
101 GstBuffer * buffer);
102
103 /* VideoOverlay interface */
104 static void gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface *
105 iface);
106 static void gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay,
107 guintptr handle);
108 static void gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
109 gint x, gint y, gint w, gint h);
110 static void gst_wayland_sink_expose (GstVideoOverlay * overlay);
111
112 /* WaylandVideo interface */
113 static void gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface *
114 iface);
115 static void gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video);
116 static void gst_wayland_sink_end_geometry_change (GstWaylandVideo * video);
117
118 #define gst_wayland_sink_parent_class parent_class
119 G_DEFINE_TYPE_WITH_CODE (GstWaylandSink, gst_wayland_sink, GST_TYPE_VIDEO_SINK,
120 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
121 gst_wayland_sink_videooverlay_init)
122 G_IMPLEMENT_INTERFACE (GST_TYPE_WAYLAND_VIDEO,
123 gst_wayland_sink_waylandvideo_init));
124
125 /* A tiny GstVideoBufferPool subclass that modify the options to remove
126 * VideoAlignment. To support VideoAlignment we would need to pass the padded
127 * width/height + stride and use the viewporter interface to crop, a bit like
128 * we use to do with XV. It would still be quite limited. It's a bit retro,
129 * hopefully there will be a better Wayland interface in the future. */
130
131 GType gst_wayland_pool_get_type (void);
132
133 typedef struct
134 {
135 GstVideoBufferPool parent;
136 } GstWaylandPool;
137
138 typedef struct
139 {
140 GstVideoBufferPoolClass parent;
141 } GstWaylandPoolClass;
142
143 G_DEFINE_TYPE (GstWaylandPool, gst_wayland_pool, GST_TYPE_VIDEO_BUFFER_POOL);
144
145 static const gchar **
gst_wayland_pool_get_options(GstBufferPool * pool)146 gst_wayland_pool_get_options (GstBufferPool * pool)
147 {
148 static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META, NULL };
149 return options;
150 }
151
152 static void
gst_wayland_pool_class_init(GstWaylandPoolClass * klass)153 gst_wayland_pool_class_init (GstWaylandPoolClass * klass)
154 {
155 GstBufferPoolClass *pool_class = GST_BUFFER_POOL_CLASS (klass);
156 pool_class->get_options = gst_wayland_pool_get_options;
157 }
158
159 static void
gst_wayland_pool_init(GstWaylandPool * pool)160 gst_wayland_pool_init (GstWaylandPool * pool)
161 {
162 }
163
164 static void
gst_wayland_sink_class_init(GstWaylandSinkClass * klass)165 gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
166 {
167 GObjectClass *gobject_class;
168 GstElementClass *gstelement_class;
169 GstBaseSinkClass *gstbasesink_class;
170 GstVideoSinkClass *gstvideosink_class;
171
172 gobject_class = (GObjectClass *) klass;
173 gstelement_class = (GstElementClass *) klass;
174 gstbasesink_class = (GstBaseSinkClass *) klass;
175 gstvideosink_class = (GstVideoSinkClass *) klass;
176
177 gobject_class->set_property = gst_wayland_sink_set_property;
178 gobject_class->get_property = gst_wayland_sink_get_property;
179 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_wayland_sink_finalize);
180
181 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
182
183 gst_element_class_set_static_metadata (gstelement_class,
184 "wayland video sink", "Sink/Video",
185 "Output to wayland surface",
186 "Sreerenj Balachandran <sreerenj.balachandran@intel.com>, "
187 "George Kiagiadakis <george.kiagiadakis@collabora.com>");
188
189 gstelement_class->change_state =
190 GST_DEBUG_FUNCPTR (gst_wayland_sink_change_state);
191 gstelement_class->set_context =
192 GST_DEBUG_FUNCPTR (gst_wayland_sink_set_context);
193
194 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_get_caps);
195 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_wayland_sink_set_caps);
196 gstbasesink_class->propose_allocation =
197 GST_DEBUG_FUNCPTR (gst_wayland_sink_propose_allocation);
198
199 gstvideosink_class->show_frame =
200 GST_DEBUG_FUNCPTR (gst_wayland_sink_show_frame);
201
202 g_object_class_install_property (gobject_class, PROP_DISPLAY,
203 g_param_spec_string ("display", "Wayland Display name", "Wayland "
204 "display name to connect to, if not supplied via the GstContext",
205 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
206
207 g_object_class_install_property (gobject_class, PROP_FULLSCREEN,
208 g_param_spec_boolean ("fullscreen", "Fullscreen",
209 "Whether the surface should be made fullscreen ", FALSE,
210 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
211 }
212
213 static void
gst_wayland_sink_init(GstWaylandSink * sink)214 gst_wayland_sink_init (GstWaylandSink * sink)
215 {
216 g_mutex_init (&sink->display_lock);
217 g_mutex_init (&sink->render_lock);
218 }
219
220 static void
gst_wayland_sink_set_fullscreen(GstWaylandSink * sink,gboolean fullscreen)221 gst_wayland_sink_set_fullscreen (GstWaylandSink * sink, gboolean fullscreen)
222 {
223 if (fullscreen == sink->fullscreen)
224 return;
225
226 g_mutex_lock (&sink->render_lock);
227 sink->fullscreen = fullscreen;
228 gst_wl_window_ensure_fullscreen (sink->window, fullscreen);
229 g_mutex_unlock (&sink->render_lock);
230 }
231
232 static void
gst_wayland_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)233 gst_wayland_sink_get_property (GObject * object,
234 guint prop_id, GValue * value, GParamSpec * pspec)
235 {
236 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
237
238 switch (prop_id) {
239 case PROP_DISPLAY:
240 GST_OBJECT_LOCK (sink);
241 g_value_set_string (value, sink->display_name);
242 GST_OBJECT_UNLOCK (sink);
243 break;
244 case PROP_FULLSCREEN:
245 GST_OBJECT_LOCK (sink);
246 g_value_set_boolean (value, sink->fullscreen);
247 GST_OBJECT_UNLOCK (sink);
248 break;
249 default:
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251 break;
252 }
253 }
254
255 static void
gst_wayland_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)256 gst_wayland_sink_set_property (GObject * object,
257 guint prop_id, const GValue * value, GParamSpec * pspec)
258 {
259 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
260
261 switch (prop_id) {
262 case PROP_DISPLAY:
263 GST_OBJECT_LOCK (sink);
264 sink->display_name = g_value_dup_string (value);
265 GST_OBJECT_UNLOCK (sink);
266 break;
267 case PROP_FULLSCREEN:
268 GST_OBJECT_LOCK (sink);
269 gst_wayland_sink_set_fullscreen (sink, g_value_get_boolean (value));
270 GST_OBJECT_UNLOCK (sink);
271 break;
272 default:
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274 break;
275 }
276 }
277
278 static void
gst_wayland_sink_finalize(GObject * object)279 gst_wayland_sink_finalize (GObject * object)
280 {
281 GstWaylandSink *sink = GST_WAYLAND_SINK (object);
282
283 GST_DEBUG_OBJECT (sink, "Finalizing the sink..");
284
285 if (sink->last_buffer)
286 gst_buffer_unref (sink->last_buffer);
287 if (sink->display)
288 g_object_unref (sink->display);
289 if (sink->window)
290 g_object_unref (sink->window);
291 if (sink->pool)
292 gst_object_unref (sink->pool);
293
294 g_free (sink->display_name);
295
296 g_mutex_clear (&sink->display_lock);
297 g_mutex_clear (&sink->render_lock);
298
299 G_OBJECT_CLASS (parent_class)->finalize (object);
300 }
301
302 /* must be called with the display_lock */
303 static void
gst_wayland_sink_set_display_from_context(GstWaylandSink * sink,GstContext * context)304 gst_wayland_sink_set_display_from_context (GstWaylandSink * sink,
305 GstContext * context)
306 {
307 struct wl_display *display;
308 GError *error = NULL;
309
310 display = gst_wayland_display_handle_context_get_handle (context);
311 sink->display = gst_wl_display_new_existing (display, FALSE, &error);
312
313 if (error) {
314 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
315 ("Could not set display handle"),
316 ("Failed to use the external wayland display: '%s'", error->message));
317 g_error_free (error);
318 }
319 }
320
321 static gboolean
gst_wayland_sink_find_display(GstWaylandSink * sink)322 gst_wayland_sink_find_display (GstWaylandSink * sink)
323 {
324 GstQuery *query;
325 GstMessage *msg;
326 GstContext *context = NULL;
327 GError *error = NULL;
328 gboolean ret = TRUE;
329
330 g_mutex_lock (&sink->display_lock);
331
332 if (!sink->display) {
333 /* first query upstream for the needed display handle */
334 query = gst_query_new_context (GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
335 if (gst_pad_peer_query (GST_VIDEO_SINK_PAD (sink), query)) {
336 gst_query_parse_context (query, &context);
337 gst_wayland_sink_set_display_from_context (sink, context);
338 }
339 gst_query_unref (query);
340
341 if (G_LIKELY (!sink->display)) {
342 /* now ask the application to set the display handle */
343 msg = gst_message_new_need_context (GST_OBJECT_CAST (sink),
344 GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE);
345
346 g_mutex_unlock (&sink->display_lock);
347 gst_element_post_message (GST_ELEMENT_CAST (sink), msg);
348 /* at this point we expect gst_wayland_sink_set_context
349 * to get called and fill sink->display */
350 g_mutex_lock (&sink->display_lock);
351
352 if (!sink->display) {
353 /* if the application didn't set a display, let's create it ourselves */
354 GST_OBJECT_LOCK (sink);
355 sink->display = gst_wl_display_new (sink->display_name, &error);
356 GST_OBJECT_UNLOCK (sink);
357
358 if (error) {
359 GST_ELEMENT_WARNING (sink, RESOURCE, OPEN_READ_WRITE,
360 ("Could not initialise Wayland output"),
361 ("Failed to create GstWlDisplay: '%s'", error->message));
362 g_error_free (error);
363 ret = FALSE;
364 }
365 }
366 }
367 }
368
369 g_mutex_unlock (&sink->display_lock);
370
371 return ret;
372 }
373
374 static GstStateChangeReturn
gst_wayland_sink_change_state(GstElement * element,GstStateChange transition)375 gst_wayland_sink_change_state (GstElement * element, GstStateChange transition)
376 {
377 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
378 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
379
380 switch (transition) {
381 case GST_STATE_CHANGE_NULL_TO_READY:
382 if (!gst_wayland_sink_find_display (sink))
383 return GST_STATE_CHANGE_FAILURE;
384 break;
385 default:
386 break;
387 }
388
389 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
390 if (ret == GST_STATE_CHANGE_FAILURE)
391 return ret;
392
393 switch (transition) {
394 case GST_STATE_CHANGE_PAUSED_TO_READY:
395 gst_buffer_replace (&sink->last_buffer, NULL);
396 if (sink->window) {
397 if (gst_wl_window_is_toplevel (sink->window)) {
398 g_clear_object (&sink->window);
399 } else {
400 /* remove buffer from surface, show nothing */
401 gst_wl_window_render (sink->window, NULL, NULL);
402 }
403 }
404 break;
405 case GST_STATE_CHANGE_READY_TO_NULL:
406 g_mutex_lock (&sink->display_lock);
407 /* If we had a toplevel window, we most likely have our own connection
408 * to the display too, and it is a good idea to disconnect and allow
409 * potentially the application to embed us with GstVideoOverlay
410 * (which requires to re-use the same display connection as the parent
411 * surface). If we didn't have a toplevel window, then the display
412 * connection that we have is definitely shared with the application
413 * and it's better to keep it around (together with the window handle)
414 * to avoid requesting them again from the application if/when we are
415 * restarted (GstVideoOverlay behaves like that in other sinks)
416 */
417 if (sink->display && !sink->window) { /* -> the window was toplevel */
418 g_clear_object (&sink->display);
419 g_mutex_lock (&sink->render_lock);
420 sink->redraw_pending = FALSE;
421 g_mutex_unlock (&sink->render_lock);
422 }
423 g_mutex_unlock (&sink->display_lock);
424 g_clear_object (&sink->pool);
425 break;
426 default:
427 break;
428 }
429
430 return ret;
431 }
432
433 static void
gst_wayland_sink_set_context(GstElement * element,GstContext * context)434 gst_wayland_sink_set_context (GstElement * element, GstContext * context)
435 {
436 GstWaylandSink *sink = GST_WAYLAND_SINK (element);
437
438 if (gst_context_has_context_type (context,
439 GST_WAYLAND_DISPLAY_HANDLE_CONTEXT_TYPE)) {
440 g_mutex_lock (&sink->display_lock);
441 if (G_LIKELY (!sink->display)) {
442 gst_wayland_sink_set_display_from_context (sink, context);
443 } else {
444 GST_WARNING_OBJECT (element, "changing display handle is not supported");
445 g_mutex_unlock (&sink->display_lock);
446 return;
447 }
448 g_mutex_unlock (&sink->display_lock);
449 }
450
451 if (GST_ELEMENT_CLASS (parent_class)->set_context)
452 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
453 }
454
455 static GstCaps *
gst_wayland_sink_get_caps(GstBaseSink * bsink,GstCaps * filter)456 gst_wayland_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
457 {
458 GstWaylandSink *sink;
459 GstCaps *caps;
460
461 sink = GST_WAYLAND_SINK (bsink);
462
463 caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
464 caps = gst_caps_make_writable (caps);
465
466 g_mutex_lock (&sink->display_lock);
467
468 if (sink->display) {
469 GValue shm_list = G_VALUE_INIT, dmabuf_list = G_VALUE_INIT;
470 GValue value = G_VALUE_INIT;
471 GArray *formats;
472 gint i;
473 guint fmt;
474
475 g_value_init (&shm_list, GST_TYPE_LIST);
476 g_value_init (&dmabuf_list, GST_TYPE_LIST);
477
478 /* Add corresponding shm formats */
479 formats = sink->display->shm_formats;
480 for (i = 0; i < formats->len; i++) {
481 g_value_init (&value, G_TYPE_STRING);
482 fmt = g_array_index (formats, uint32_t, i);
483 g_value_set_static_string (&value, gst_wl_shm_format_to_string (fmt));
484 gst_value_list_append_and_take_value (&shm_list, &value);
485 }
486
487 gst_structure_take_value (gst_caps_get_structure (caps, 0), "format",
488 &shm_list);
489
490 /* Add corresponding dmabuf formats */
491 formats = sink->display->dmabuf_formats;
492 for (i = 0; i < formats->len; i++) {
493 g_value_init (&value, G_TYPE_STRING);
494 fmt = g_array_index (formats, uint32_t, i);
495 g_value_set_static_string (&value, gst_wl_dmabuf_format_to_string (fmt));
496 gst_value_list_append_and_take_value (&dmabuf_list, &value);
497 }
498
499 gst_structure_take_value (gst_caps_get_structure (caps, 1), "format",
500 &dmabuf_list);
501
502 GST_DEBUG_OBJECT (sink, "display caps: %" GST_PTR_FORMAT, caps);
503 }
504
505 g_mutex_unlock (&sink->display_lock);
506
507 if (filter) {
508 GstCaps *intersection;
509
510 intersection =
511 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
512 gst_caps_unref (caps);
513 caps = intersection;
514 }
515
516 return caps;
517 }
518
519 static GstBufferPool *
gst_wayland_create_pool(GstWaylandSink * sink,GstCaps * caps)520 gst_wayland_create_pool (GstWaylandSink * sink, GstCaps * caps)
521 {
522 GstBufferPool *pool = NULL;
523 GstStructure *structure;
524 gsize size = sink->video_info.size;
525 GstAllocator *alloc;
526
527 pool = g_object_new (gst_wayland_pool_get_type (), NULL);
528
529 structure = gst_buffer_pool_get_config (pool);
530 gst_buffer_pool_config_set_params (structure, caps, size, 2, 0);
531
532 alloc = gst_wl_shm_allocator_get ();
533 gst_buffer_pool_config_set_allocator (structure, alloc, NULL);
534 if (!gst_buffer_pool_set_config (pool, structure)) {
535 g_object_unref (pool);
536 pool = NULL;
537 }
538 g_object_unref (alloc);
539
540 return pool;
541 }
542
543 static gboolean
gst_wayland_sink_set_caps(GstBaseSink * bsink,GstCaps * caps)544 gst_wayland_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
545 {
546 GstWaylandSink *sink;
547 gboolean use_dmabuf;
548 GstVideoFormat format;
549
550 sink = GST_WAYLAND_SINK (bsink);
551
552 GST_DEBUG_OBJECT (sink, "set caps %" GST_PTR_FORMAT, caps);
553
554 /* extract info from caps */
555 if (!gst_video_info_from_caps (&sink->video_info, caps))
556 goto invalid_format;
557
558 format = GST_VIDEO_INFO_FORMAT (&sink->video_info);
559 sink->video_info_changed = TRUE;
560
561 /* create a new pool for the new caps */
562 if (sink->pool)
563 gst_object_unref (sink->pool);
564 sink->pool = gst_wayland_create_pool (sink, caps);
565
566 use_dmabuf = gst_caps_features_contains (gst_caps_get_features (caps, 0),
567 GST_CAPS_FEATURE_MEMORY_DMABUF);
568
569 /* validate the format base on the memory type. */
570 if (use_dmabuf) {
571 if (!gst_wl_display_check_format_for_dmabuf (sink->display, format))
572 goto unsupported_format;
573 } else if (!gst_wl_display_check_format_for_shm (sink->display, format)) {
574 goto unsupported_format;
575 }
576
577 sink->use_dmabuf = use_dmabuf;
578
579 return TRUE;
580
581 invalid_format:
582 {
583 GST_ERROR_OBJECT (sink,
584 "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
585 return FALSE;
586 }
587 unsupported_format:
588 {
589 GST_ERROR_OBJECT (sink, "Format %s is not available on the display",
590 gst_video_format_to_string (format));
591 return FALSE;
592 }
593 }
594
595 static gboolean
gst_wayland_sink_propose_allocation(GstBaseSink * bsink,GstQuery * query)596 gst_wayland_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
597 {
598 GstWaylandSink *sink = GST_WAYLAND_SINK (bsink);
599 GstCaps *caps;
600 GstBufferPool *pool = NULL;
601 gboolean need_pool;
602 GstAllocator *alloc;
603
604 gst_query_parse_allocation (query, &caps, &need_pool);
605
606 if (need_pool)
607 pool = gst_wayland_create_pool (sink, caps);
608
609 gst_query_add_allocation_pool (query, pool, sink->video_info.size, 2, 0);
610 if (pool)
611 g_object_unref (pool);
612
613 alloc = gst_wl_shm_allocator_get ();
614 gst_query_add_allocation_param (query, alloc, NULL);
615 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
616 g_object_unref (alloc);
617
618 return TRUE;
619 }
620
621 static void
frame_redraw_callback(void * data,struct wl_callback * callback,uint32_t time)622 frame_redraw_callback (void *data, struct wl_callback *callback, uint32_t time)
623 {
624 GstWaylandSink *sink = data;
625
626 GST_LOG ("frame_redraw_cb");
627
628 g_mutex_lock (&sink->render_lock);
629 sink->redraw_pending = FALSE;
630 g_mutex_unlock (&sink->render_lock);
631
632 wl_callback_destroy (callback);
633 }
634
635 static const struct wl_callback_listener frame_callback_listener = {
636 frame_redraw_callback
637 };
638
639 /* must be called with the render lock */
640 static void
render_last_buffer(GstWaylandSink * sink,gboolean redraw)641 render_last_buffer (GstWaylandSink * sink, gboolean redraw)
642 {
643 GstWlBuffer *wlbuffer;
644 const GstVideoInfo *info = NULL;
645 struct wl_surface *surface;
646 struct wl_callback *callback;
647
648 wlbuffer = gst_buffer_get_wl_buffer (sink->last_buffer);
649 surface = gst_wl_window_get_wl_surface (sink->window);
650
651 sink->redraw_pending = TRUE;
652 callback = wl_surface_frame (surface);
653 wl_callback_add_listener (callback, &frame_callback_listener, sink);
654
655 if (G_UNLIKELY (sink->video_info_changed && !redraw)) {
656 info = &sink->video_info;
657 sink->video_info_changed = FALSE;
658 }
659 gst_wl_window_render (sink->window, wlbuffer, info);
660 }
661
662 static void
on_window_closed(GstWlWindow * window,gpointer user_data)663 on_window_closed (GstWlWindow * window, gpointer user_data)
664 {
665 GstWaylandSink *sink = GST_WAYLAND_SINK (user_data);
666
667 /* Handle window closure by posting an error on the bus */
668 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
669 ("Output window was closed"), (NULL));
670 }
671
672 static GstFlowReturn
gst_wayland_sink_show_frame(GstVideoSink * vsink,GstBuffer * buffer)673 gst_wayland_sink_show_frame (GstVideoSink * vsink, GstBuffer * buffer)
674 {
675 GstWaylandSink *sink = GST_WAYLAND_SINK (vsink);
676 GstBuffer *to_render;
677 GstWlBuffer *wlbuffer;
678 GstVideoMeta *vmeta;
679 GstVideoFormat format;
680 GstVideoInfo old_vinfo;
681 GstMemory *mem;
682 struct wl_buffer *wbuf = NULL;
683
684 GstFlowReturn ret = GST_FLOW_OK;
685
686 g_mutex_lock (&sink->render_lock);
687
688 GST_LOG_OBJECT (sink, "render buffer %p", buffer);
689
690 if (G_UNLIKELY (!sink->window)) {
691 /* ask for window handle. Unlock render_lock while doing that because
692 * set_window_handle & friends will lock it in this context */
693 g_mutex_unlock (&sink->render_lock);
694 gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (sink));
695 g_mutex_lock (&sink->render_lock);
696
697 if (!sink->window) {
698 /* if we were not provided a window, create one ourselves */
699 sink->window = gst_wl_window_new_toplevel (sink->display,
700 &sink->video_info, sink->fullscreen, &sink->render_lock);
701 g_signal_connect_object (sink->window, "closed",
702 G_CALLBACK (on_window_closed), sink, 0);
703 }
704 }
705
706 /* drop buffers until we get a frame callback */
707 if (sink->redraw_pending) {
708 GST_LOG_OBJECT (sink, "buffer %p dropped (redraw pending)", buffer);
709 goto done;
710 }
711
712 /* make sure that the application has called set_render_rectangle() */
713 if (G_UNLIKELY (sink->window->render_rectangle.w == 0))
714 goto no_window_size;
715
716 wlbuffer = gst_buffer_get_wl_buffer (buffer);
717
718 if (G_LIKELY (wlbuffer && wlbuffer->display == sink->display)) {
719 GST_LOG_OBJECT (sink, "buffer %p has a wl_buffer from our display, "
720 "writing directly", buffer);
721 to_render = buffer;
722 goto render;
723 }
724
725 /* update video info from video meta */
726 mem = gst_buffer_peek_memory (buffer, 0);
727
728 old_vinfo = sink->video_info;
729 vmeta = gst_buffer_get_video_meta (buffer);
730 if (vmeta) {
731 gint i;
732
733 for (i = 0; i < vmeta->n_planes; i++) {
734 sink->video_info.offset[i] = vmeta->offset[i];
735 sink->video_info.stride[i] = vmeta->stride[i];
736 }
737 sink->video_info.size = gst_buffer_get_size (buffer);
738 }
739
740 GST_LOG_OBJECT (sink, "buffer %p does not have a wl_buffer from our "
741 "display, creating it", buffer);
742
743 format = GST_VIDEO_INFO_FORMAT (&sink->video_info);
744 if (gst_wl_display_check_format_for_dmabuf (sink->display, format)) {
745 guint i, nb_dmabuf = 0;
746
747 for (i = 0; i < gst_buffer_n_memory (buffer); i++)
748 if (gst_is_dmabuf_memory (gst_buffer_peek_memory (buffer, i)))
749 nb_dmabuf++;
750
751 if (nb_dmabuf && (nb_dmabuf == gst_buffer_n_memory (buffer)))
752 wbuf = gst_wl_linux_dmabuf_construct_wl_buffer (buffer, sink->display,
753 &sink->video_info);
754 }
755
756 if (!wbuf && gst_wl_display_check_format_for_shm (sink->display, format)) {
757 if (gst_buffer_n_memory (buffer) == 1 && gst_is_fd_memory (mem))
758 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
759 &sink->video_info);
760
761 /* If nothing worked, copy into our internal pool */
762 if (!wbuf) {
763 GstVideoFrame src, dst;
764 GstVideoInfo src_info = sink->video_info;
765
766 /* rollback video info changes */
767 sink->video_info = old_vinfo;
768
769 /* we don't know how to create a wl_buffer directly from the provided
770 * memory, so we have to copy the data to shm memory that we know how
771 * to handle... */
772
773 GST_LOG_OBJECT (sink, "buffer %p cannot have a wl_buffer, "
774 "copying to wl_shm memory", buffer);
775
776 /* sink->pool always exists (created in set_caps), but it may not
777 * be active if upstream is not using it */
778 if (!gst_buffer_pool_is_active (sink->pool)) {
779 GstStructure *config;
780 GstCaps *caps;
781
782 config = gst_buffer_pool_get_config (sink->pool);
783 gst_buffer_pool_config_get_params (config, &caps, NULL, NULL, NULL);
784
785 /* revert back to default strides and offsets */
786 gst_video_info_from_caps (&sink->video_info, caps);
787 gst_buffer_pool_config_set_params (config, caps, sink->video_info.size,
788 2, 0);
789
790 /* This is a video pool, it should not fail with basic setings */
791 if (!gst_buffer_pool_set_config (sink->pool, config) ||
792 !gst_buffer_pool_set_active (sink->pool, TRUE))
793 goto activate_failed;
794 }
795
796 ret = gst_buffer_pool_acquire_buffer (sink->pool, &to_render, NULL);
797 if (ret != GST_FLOW_OK)
798 goto no_buffer;
799
800 wlbuffer = gst_buffer_get_wl_buffer (to_render);
801
802 /* attach a wl_buffer if there isn't one yet */
803 if (G_UNLIKELY (!wlbuffer)) {
804 mem = gst_buffer_peek_memory (to_render, 0);
805 wbuf = gst_wl_shm_memory_construct_wl_buffer (mem, sink->display,
806 &sink->video_info);
807
808 if (G_UNLIKELY (!wbuf))
809 goto no_wl_buffer_shm;
810
811 gst_buffer_add_wl_buffer (to_render, wbuf, sink->display);
812 }
813
814 if (!gst_video_frame_map (&dst, &sink->video_info, to_render,
815 GST_MAP_WRITE))
816 goto dst_map_failed;
817
818 if (!gst_video_frame_map (&src, &src_info, buffer, GST_MAP_READ)) {
819 gst_video_frame_unmap (&dst);
820 goto src_map_failed;
821 }
822
823 gst_video_frame_copy (&dst, &src);
824
825 gst_video_frame_unmap (&src);
826 gst_video_frame_unmap (&dst);
827
828 goto render;
829 }
830 }
831
832 if (!wbuf)
833 goto no_wl_buffer;
834
835 gst_buffer_add_wl_buffer (buffer, wbuf, sink->display);
836 to_render = buffer;
837
838 render:
839 /* drop double rendering */
840 if (G_UNLIKELY (to_render == sink->last_buffer)) {
841 GST_LOG_OBJECT (sink, "Buffer already being rendered");
842 goto done;
843 }
844
845 gst_buffer_replace (&sink->last_buffer, to_render);
846 render_last_buffer (sink, FALSE);
847
848 if (buffer != to_render)
849 gst_buffer_unref (to_render);
850 goto done;
851
852 no_window_size:
853 {
854 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
855 ("Window has no size set"),
856 ("Make sure you set the size after calling set_window_handle"));
857 ret = GST_FLOW_ERROR;
858 goto done;
859 }
860 no_buffer:
861 {
862 GST_WARNING_OBJECT (sink, "could not create buffer");
863 goto done;
864 }
865 no_wl_buffer_shm:
866 {
867 GST_ERROR_OBJECT (sink, "could not create wl_buffer out of wl_shm memory");
868 ret = GST_FLOW_ERROR;
869 goto done;
870 }
871 no_wl_buffer:
872 {
873 GST_ERROR_OBJECT (sink, "buffer %p cannot have a wl_buffer", buffer);
874 ret = GST_FLOW_ERROR;
875 goto done;
876 }
877 activate_failed:
878 {
879 GST_ERROR_OBJECT (sink, "failed to activate bufferpool.");
880 ret = GST_FLOW_ERROR;
881 goto done;
882 }
883 src_map_failed:
884 {
885 GST_ELEMENT_ERROR (sink, RESOURCE, READ,
886 ("Video memory can not be read from userspace."), (NULL));
887 ret = GST_FLOW_ERROR;
888 goto done;
889 }
890 dst_map_failed:
891 {
892 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
893 ("Video memory can not be written from userspace."), (NULL));
894 ret = GST_FLOW_ERROR;
895 goto done;
896 }
897 done:
898 {
899 g_mutex_unlock (&sink->render_lock);
900 return ret;
901 }
902 }
903
904 static void
gst_wayland_sink_videooverlay_init(GstVideoOverlayInterface * iface)905 gst_wayland_sink_videooverlay_init (GstVideoOverlayInterface * iface)
906 {
907 iface->set_window_handle = gst_wayland_sink_set_window_handle;
908 iface->set_render_rectangle = gst_wayland_sink_set_render_rectangle;
909 iface->expose = gst_wayland_sink_expose;
910 }
911
912 static void
gst_wayland_sink_set_window_handle(GstVideoOverlay * overlay,guintptr handle)913 gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
914 {
915 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
916 struct wl_surface *surface = (struct wl_surface *) handle;
917
918 g_return_if_fail (sink != NULL);
919
920 if (sink->window != NULL) {
921 GST_WARNING_OBJECT (sink, "changing window handle is not supported");
922 return;
923 }
924
925 g_mutex_lock (&sink->render_lock);
926
927 GST_DEBUG_OBJECT (sink, "Setting window handle %" GST_PTR_FORMAT,
928 (void *) handle);
929
930 g_clear_object (&sink->window);
931
932 if (handle) {
933 if (G_LIKELY (gst_wayland_sink_find_display (sink))) {
934 /* we cannot use our own display with an external window handle */
935 if (G_UNLIKELY (sink->display->own_display)) {
936 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE,
937 ("Application did not provide a wayland display handle"),
938 ("waylandsink cannot use an externally-supplied surface without "
939 "an externally-supplied display handle. Consider providing a "
940 "display handle from your application with GstContext"));
941 } else {
942 sink->window = gst_wl_window_new_in_surface (sink->display, surface,
943 &sink->render_lock);
944 }
945 } else {
946 GST_ERROR_OBJECT (sink, "Failed to find display handle, "
947 "ignoring window handle");
948 }
949 }
950
951 g_mutex_unlock (&sink->render_lock);
952 }
953
954 static void
gst_wayland_sink_set_render_rectangle(GstVideoOverlay * overlay,gint x,gint y,gint w,gint h)955 gst_wayland_sink_set_render_rectangle (GstVideoOverlay * overlay,
956 gint x, gint y, gint w, gint h)
957 {
958 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
959
960 g_return_if_fail (sink != NULL);
961
962 g_mutex_lock (&sink->render_lock);
963 if (!sink->window) {
964 g_mutex_unlock (&sink->render_lock);
965 GST_WARNING_OBJECT (sink,
966 "set_render_rectangle called without window, ignoring");
967 return;
968 }
969
970 GST_DEBUG_OBJECT (sink, "window geometry changed to (%d, %d) %d x %d",
971 x, y, w, h);
972 gst_wl_window_set_render_rectangle (sink->window, x, y, w, h);
973
974 g_mutex_unlock (&sink->render_lock);
975 }
976
977 static void
gst_wayland_sink_expose(GstVideoOverlay * overlay)978 gst_wayland_sink_expose (GstVideoOverlay * overlay)
979 {
980 GstWaylandSink *sink = GST_WAYLAND_SINK (overlay);
981
982 g_return_if_fail (sink != NULL);
983
984 GST_DEBUG_OBJECT (sink, "expose");
985
986 g_mutex_lock (&sink->render_lock);
987 if (sink->last_buffer && !sink->redraw_pending) {
988 GST_DEBUG_OBJECT (sink, "redrawing last buffer");
989 render_last_buffer (sink, TRUE);
990 }
991 g_mutex_unlock (&sink->render_lock);
992 }
993
994 static void
gst_wayland_sink_waylandvideo_init(GstWaylandVideoInterface * iface)995 gst_wayland_sink_waylandvideo_init (GstWaylandVideoInterface * iface)
996 {
997 iface->begin_geometry_change = gst_wayland_sink_begin_geometry_change;
998 iface->end_geometry_change = gst_wayland_sink_end_geometry_change;
999 }
1000
1001 static void
gst_wayland_sink_begin_geometry_change(GstWaylandVideo * video)1002 gst_wayland_sink_begin_geometry_change (GstWaylandVideo * video)
1003 {
1004 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1005 g_return_if_fail (sink != NULL);
1006
1007 g_mutex_lock (&sink->render_lock);
1008 if (!sink->window || !sink->window->area_subsurface) {
1009 g_mutex_unlock (&sink->render_lock);
1010 GST_INFO_OBJECT (sink,
1011 "begin_geometry_change called without window, ignoring");
1012 return;
1013 }
1014
1015 wl_subsurface_set_sync (sink->window->area_subsurface);
1016 g_mutex_unlock (&sink->render_lock);
1017 }
1018
1019 static void
gst_wayland_sink_end_geometry_change(GstWaylandVideo * video)1020 gst_wayland_sink_end_geometry_change (GstWaylandVideo * video)
1021 {
1022 GstWaylandSink *sink = GST_WAYLAND_SINK (video);
1023 g_return_if_fail (sink != NULL);
1024
1025 g_mutex_lock (&sink->render_lock);
1026 if (!sink->window || !sink->window->area_subsurface) {
1027 g_mutex_unlock (&sink->render_lock);
1028 GST_INFO_OBJECT (sink,
1029 "end_geometry_change called without window, ignoring");
1030 return;
1031 }
1032
1033 wl_subsurface_set_desync (sink->window->area_subsurface);
1034 g_mutex_unlock (&sink->render_lock);
1035 }
1036
1037 static gboolean
plugin_init(GstPlugin * plugin)1038 plugin_init (GstPlugin * plugin)
1039 {
1040 GST_DEBUG_CATEGORY_INIT (gstwayland_debug, "waylandsink", 0,
1041 " wayland video sink");
1042
1043 gst_wl_shm_allocator_register ();
1044
1045 return gst_element_register (plugin, "waylandsink", GST_RANK_MARGINAL,
1046 GST_TYPE_WAYLAND_SINK);
1047 }
1048
1049 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1050 GST_VERSION_MINOR,
1051 waylandsink,
1052 "Wayland Video Sink", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
1053 GST_PACKAGE_ORIGIN)
1054