1 /* GStreamer Wayland video sink
3 * Copyright (C) 2014 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA.
25 #include "wldisplay.h"
27 #include "wlvideoformat.h"
31 GST_DEBUG_CATEGORY_EXTERN (gstwayland_debug);
32 #define GST_CAT_DEFAULT gstwayland_debug
34 G_DEFINE_TYPE (GstWlDisplay, gst_wl_display, G_TYPE_OBJECT);
36 static void gst_wl_display_finalize (GObject * gobject);
39 gst_wl_display_class_init (GstWlDisplayClass * klass)
41 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
42 gobject_class->finalize = gst_wl_display_finalize;
46 gst_wl_display_init (GstWlDisplay * self)
48 self->shm_formats = g_array_new (FALSE, FALSE, sizeof (uint32_t));
49 self->dmabuf_formats = g_array_new (FALSE, FALSE, sizeof (uint32_t));
50 self->wl_fd_poll = gst_poll_new (TRUE);
51 self->buffers = g_hash_table_new (g_direct_hash, g_direct_equal);
52 g_mutex_init (&self->buffers_mutex);
56 gst_wl_ref_wl_buffer (gpointer key, gpointer value, gpointer user_data)
62 gst_wl_display_finalize (GObject * gobject)
64 GstWlDisplay *self = GST_WL_DISPLAY (gobject);
66 gst_poll_set_flushing (self->wl_fd_poll, TRUE);
68 g_thread_join (self->thread);
70 /* to avoid buffers being unregistered from another thread
71 * at the same time, take their ownership */
72 g_mutex_lock (&self->buffers_mutex);
73 self->shutting_down = TRUE;
74 g_hash_table_foreach (self->buffers, gst_wl_ref_wl_buffer, NULL);
75 g_mutex_unlock (&self->buffers_mutex);
77 g_hash_table_foreach (self->buffers,
78 (GHFunc) gst_wl_buffer_force_release_and_unref, NULL);
79 g_hash_table_remove_all (self->buffers);
81 g_array_unref (self->shm_formats);
82 g_array_unref (self->dmabuf_formats);
83 gst_poll_free (self->wl_fd_poll);
84 g_hash_table_unref (self->buffers);
85 g_mutex_clear (&self->buffers_mutex);
88 wp_viewporter_destroy (self->viewporter);
91 wl_shm_destroy (self->shm);
94 zwp_linux_dmabuf_v1_destroy (self->dmabuf);
97 wl_shell_destroy (self->wl_shell);
99 if (self->xdg_wm_base)
100 xdg_wm_base_destroy (self->xdg_wm_base);
102 if (self->fullscreen_shell)
103 zwp_fullscreen_shell_v1_release (self->fullscreen_shell);
105 if (self->compositor)
106 wl_compositor_destroy (self->compositor);
108 if (self->subcompositor)
109 wl_subcompositor_destroy (self->subcompositor);
112 wl_registry_destroy (self->registry);
114 if (self->display_wrapper)
115 wl_proxy_wrapper_destroy (self->display_wrapper);
118 wl_event_queue_destroy (self->queue);
120 if (self->own_display) {
121 wl_display_flush (self->display);
122 wl_display_disconnect (self->display);
125 G_OBJECT_CLASS (gst_wl_display_parent_class)->finalize (gobject);
129 shm_format (void *data, struct wl_shm *wl_shm, uint32_t format)
131 GstWlDisplay *self = data;
133 g_array_append_val (self->shm_formats, format);
136 static const struct wl_shm_listener shm_listener = {
141 dmabuf_format (void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf,
144 GstWlDisplay *self = data;
146 if (gst_wl_dmabuf_format_to_video_format (format) != GST_VIDEO_FORMAT_UNKNOWN)
147 g_array_append_val (self->dmabuf_formats, format);
150 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
155 gst_wl_display_check_format_for_shm (GstWlDisplay * display,
156 GstVideoFormat format)
158 enum wl_shm_format shm_fmt;
162 shm_fmt = gst_video_format_to_wl_shm_format (format);
163 if (shm_fmt == (enum wl_shm_format) -1)
166 formats = display->shm_formats;
167 for (i = 0; i < formats->len; i++) {
168 if (g_array_index (formats, uint32_t, i) == shm_fmt)
176 gst_wl_display_check_format_for_dmabuf (GstWlDisplay * display,
177 GstVideoFormat format)
182 if (!display->dmabuf)
185 dmabuf_fmt = gst_video_format_to_wl_dmabuf_format (format);
186 if (dmabuf_fmt == (guint) - 1)
189 formats = display->dmabuf_formats;
190 for (i = 0; i < formats->len; i++) {
191 if (g_array_index (formats, uint32_t, i) == dmabuf_fmt)
199 handle_xdg_wm_base_ping (void *user_data, struct xdg_wm_base *xdg_wm_base,
202 xdg_wm_base_pong (xdg_wm_base, serial);
205 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
206 handle_xdg_wm_base_ping
210 registry_handle_global (void *data, struct wl_registry *registry,
211 uint32_t id, const char *interface, uint32_t version)
213 GstWlDisplay *self = data;
215 if (g_strcmp0 (interface, "wl_compositor") == 0) {
216 self->compositor = wl_registry_bind (registry, id, &wl_compositor_interface,
218 } else if (g_strcmp0 (interface, "wl_subcompositor") == 0) {
219 self->subcompositor =
220 wl_registry_bind (registry, id, &wl_subcompositor_interface, 1);
221 } else if (g_strcmp0 (interface, "wl_shell") == 0) {
222 self->wl_shell = wl_registry_bind (registry, id, &wl_shell_interface, 1);
223 } else if (g_strcmp0 (interface, "xdg_wm_base") == 0) {
225 wl_registry_bind (registry, id, &xdg_wm_base_interface, 1);
226 xdg_wm_base_add_listener (self->xdg_wm_base, &xdg_wm_base_listener, self);
227 } else if (g_strcmp0 (interface, "zwp_fullscreen_shell_v1") == 0) {
228 self->fullscreen_shell = wl_registry_bind (registry, id,
229 &zwp_fullscreen_shell_v1_interface, 1);
230 } else if (g_strcmp0 (interface, "wl_shm") == 0) {
231 self->shm = wl_registry_bind (registry, id, &wl_shm_interface, 1);
232 wl_shm_add_listener (self->shm, &shm_listener, self);
233 } else if (g_strcmp0 (interface, "wp_viewporter") == 0) {
235 wl_registry_bind (registry, id, &wp_viewporter_interface, 1);
236 } else if (g_strcmp0 (interface, "zwp_linux_dmabuf_v1") == 0) {
238 wl_registry_bind (registry, id, &zwp_linux_dmabuf_v1_interface, 1);
239 zwp_linux_dmabuf_v1_add_listener (self->dmabuf, &dmabuf_listener, self);
244 registry_handle_global_remove (void *data, struct wl_registry *registry,
247 /* temporarily do nothing */
248 GST_LOG ("Removed global object: name(%d)", name);
251 static const struct wl_registry_listener registry_listener = {
252 registry_handle_global,
253 registry_handle_global_remove
257 gst_wl_display_thread_run (gpointer data)
259 GstWlDisplay *self = data;
260 GstPollFD pollfd = GST_POLL_FD_INIT;
262 pollfd.fd = wl_display_get_fd (self->display);
263 gst_poll_add_fd (self->wl_fd_poll, &pollfd);
264 gst_poll_fd_ctl_read (self->wl_fd_poll, &pollfd, TRUE);
268 while (wl_display_prepare_read_queue (self->display, self->queue) != 0)
269 wl_display_dispatch_queue_pending (self->display, self->queue);
270 wl_display_flush (self->display);
272 if (gst_poll_wait (self->wl_fd_poll, GST_CLOCK_TIME_NONE) < 0) {
273 gboolean normal = (errno == EBUSY);
274 wl_display_cancel_read (self->display);
280 if (wl_display_read_events (self->display) == -1)
282 wl_display_dispatch_queue_pending (self->display, self->queue);
288 GST_ERROR ("Error communicating with the wayland server");
293 gst_wl_display_new (const gchar * name, GError ** error)
295 struct wl_display *display;
297 display = wl_display_connect (name);
300 *error = g_error_new (g_quark_from_static_string ("GstWlDisplay"), 0,
301 "Failed to connect to the wayland display '%s'",
302 name ? name : "(default)");
305 return gst_wl_display_new_existing (display, TRUE, error);
310 gst_wl_display_new_existing (struct wl_display * display,
311 gboolean take_ownership, GError ** error)
317 g_return_val_if_fail (display != NULL, NULL);
319 self = g_object_new (GST_TYPE_WL_DISPLAY, NULL);
320 self->display = display;
321 self->display_wrapper = wl_proxy_create_wrapper (display);
322 self->own_display = take_ownership;
324 self->queue = wl_display_create_queue (self->display);
325 wl_proxy_set_queue ((struct wl_proxy *) self->display_wrapper, self->queue);
326 self->registry = wl_display_get_registry (self->display_wrapper);
327 wl_registry_add_listener (self->registry, ®istry_listener, self);
329 /* we need exactly 2 roundtrips to discover global objects and their state */
330 for (i = 0; i < 2; i++) {
331 if (wl_display_roundtrip_queue (self->display, self->queue) < 0) {
332 *error = g_error_new (g_quark_from_static_string ("GstWlDisplay"), 0,
333 "Error communicating with the wayland display");
334 g_object_unref (self);
339 /* verify we got all the required interfaces */
340 #define VERIFY_INTERFACE_EXISTS(var, interface) \
342 g_set_error (error, g_quark_from_static_string ("GstWlDisplay"), 0, \
343 "Could not bind to " interface ". Either it is not implemented in " \
344 "the compositor, or the implemented version doesn't match"); \
345 g_object_unref (self); \
349 VERIFY_INTERFACE_EXISTS (compositor, "wl_compositor");
350 VERIFY_INTERFACE_EXISTS (subcompositor, "wl_subcompositor");
351 VERIFY_INTERFACE_EXISTS (shm, "wl_shm");
353 #undef VERIFY_INTERFACE_EXISTS
355 /* We make the viewporter optional even though it may cause bad display.
356 * This is so one can test wayland display on older compositor or on
357 * compositor that don't implement this extension. */
358 if (!self->viewporter) {
359 g_warning ("Wayland compositor is missing the ability to scale, video "
360 "display may not work properly.");
364 g_warning ("Could not bind to zwp_linux_dmabuf_v1");
367 if (!self->wl_shell && !self->xdg_wm_base && !self->fullscreen_shell) {
368 /* If wl_surface and wl_display are passed via GstContext
369 * wl_shell, xdg_shell and zwp_fullscreen_shell are not used.
370 * In this case is correct to continue.
372 g_warning ("Could not bind to either wl_shell, xdg_wm_base or "
373 "zwp_fullscreen_shell, video display may not work properly.");
376 self->thread = g_thread_try_new ("GstWlDisplay", gst_wl_display_thread_run,
379 g_propagate_prefixed_error (error, err,
380 "Failed to start thread for the display's events");
381 g_object_unref (self);
389 gst_wl_display_register_buffer (GstWlDisplay * self, gpointer gstmem,
392 g_assert (!self->shutting_down);
394 GST_TRACE_OBJECT (self, "registering GstWlBuffer %p to GstMem %p",
397 g_mutex_lock (&self->buffers_mutex);
398 g_hash_table_replace (self->buffers, gstmem, wlbuffer);
399 g_mutex_unlock (&self->buffers_mutex);
403 gst_wl_display_lookup_buffer (GstWlDisplay * self, gpointer gstmem)
406 g_mutex_lock (&self->buffers_mutex);
407 wlbuffer = g_hash_table_lookup (self->buffers, gstmem);
408 g_mutex_unlock (&self->buffers_mutex);
413 gst_wl_display_unregister_buffer (GstWlDisplay * self, gpointer gstmem)
415 GST_TRACE_OBJECT (self, "unregistering GstWlBuffer owned by %p", gstmem);
417 g_mutex_lock (&self->buffers_mutex);
418 if (G_LIKELY (!self->shutting_down))
419 g_hash_table_remove (self->buffers, gstmem);
420 g_mutex_unlock (&self->buffers_mutex);