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"
29 GST_DEBUG_CATEGORY_EXTERN (gstwayland_debug);
30 #define GST_CAT_DEFAULT gstwayland_debug
32 G_DEFINE_TYPE (GstWlDisplay, gst_wl_display, G_TYPE_OBJECT);
34 static void gst_wl_display_finalize (GObject * gobject);
37 gst_wl_display_class_init (GstWlDisplayClass * klass)
39 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
40 gobject_class->finalize = gst_wl_display_finalize;
44 gst_wl_display_init (GstWlDisplay * self)
46 self->formats = g_array_new (FALSE, FALSE, sizeof (uint32_t));
47 self->wl_fd_poll = gst_poll_new (TRUE);
51 gst_wl_display_finalize (GObject * gobject)
53 GstWlDisplay *self = GST_WL_DISPLAY (gobject);
55 gst_poll_set_flushing (self->wl_fd_poll, TRUE);
58 g_thread_join (self->thread);
60 g_array_unref (self->formats);
61 gst_poll_free (self->wl_fd_poll);
64 wl_shm_destroy (self->shm);
67 wl_shell_destroy (self->shell);
70 wl_compositor_destroy (self->compositor);
73 wl_registry_destroy (self->registry);
76 wl_event_queue_destroy (self->queue);
78 if (self->own_display) {
79 wl_display_flush (self->display);
80 wl_display_disconnect (self->display);
83 G_OBJECT_CLASS (gst_wl_display_parent_class)->finalize (gobject);
87 sync_callback (void *data, struct wl_callback *callback, uint32_t serial)
89 gboolean *done = data;
93 static const struct wl_callback_listener sync_listener = {
98 gst_wl_display_roundtrip (GstWlDisplay * self)
100 struct wl_callback *callback;
102 gboolean done = FALSE;
104 g_return_val_if_fail (self != NULL, -1);
106 /* We don't own the display, process only our queue */
107 callback = wl_display_sync (self->display);
108 wl_callback_add_listener (callback, &sync_listener, &done);
109 wl_proxy_set_queue ((struct wl_proxy *) callback, self->queue);
110 while (ret != -1 && !done)
111 ret = wl_display_dispatch_queue (self->display, self->queue);
112 wl_callback_destroy (callback);
118 shm_format (void *data, struct wl_shm *wl_shm, uint32_t format)
120 GstWlDisplay *self = data;
122 g_array_append_val (self->formats, format);
125 static const struct wl_shm_listener shm_listener = {
130 registry_handle_global (void *data, struct wl_registry *registry,
131 uint32_t id, const char *interface, uint32_t version)
133 GstWlDisplay *self = data;
135 if (g_strcmp0 (interface, "wl_compositor") == 0) {
137 wl_registry_bind (registry, id, &wl_compositor_interface, 1);
138 } else if (g_strcmp0 (interface, "wl_shell") == 0) {
139 self->shell = wl_registry_bind (registry, id, &wl_shell_interface, 1);
140 } else if (g_strcmp0 (interface, "wl_shm") == 0) {
141 self->shm = wl_registry_bind (registry, id, &wl_shm_interface, 1);
142 wl_shm_add_listener (self->shm, &shm_listener, self);
146 static const struct wl_registry_listener registry_listener = {
147 registry_handle_global
151 gst_wl_display_thread_run (gpointer data)
153 GstWlDisplay *self = data;
154 GstPollFD pollfd = GST_POLL_FD_INIT;
156 pollfd.fd = wl_display_get_fd (self->display);
157 gst_poll_add_fd (self->wl_fd_poll, &pollfd);
158 gst_poll_fd_ctl_read (self->wl_fd_poll, &pollfd, TRUE);
162 while (wl_display_prepare_read_queue (self->display, self->queue) != 0)
163 wl_display_dispatch_queue_pending (self->display, self->queue);
164 wl_display_flush (self->display);
166 if (gst_poll_wait (self->wl_fd_poll, GST_CLOCK_TIME_NONE) < 0) {
167 gboolean normal = (errno == EBUSY);
168 wl_display_cancel_read (self->display);
174 wl_display_read_events (self->display);
175 wl_display_dispatch_queue_pending (self->display, self->queue);
182 GST_ERROR ("Error communicating with the wayland server");
187 gst_wl_display_new (const gchar * name, GError ** error)
189 struct wl_display *display;
191 display = wl_display_connect (name);
194 *error = g_error_new (g_quark_from_static_string ("GstWlDisplay"), 0,
195 "Failed to connect to the wayland display '%s'",
196 name ? name : "(default)");
199 return gst_wl_display_new_existing (display, TRUE, error);
204 gst_wl_display_new_existing (struct wl_display * display,
205 gboolean take_ownership, GError ** error)
211 g_return_val_if_fail (display != NULL, NULL);
213 self = g_object_new (GST_TYPE_WL_DISPLAY, NULL);
214 self->display = display;
215 self->own_display = take_ownership;
217 self->queue = wl_display_create_queue (self->display);
218 self->registry = wl_display_get_registry (self->display);
219 wl_proxy_set_queue ((struct wl_proxy *) self->registry, self->queue);
220 wl_registry_add_listener (self->registry, ®istry_listener, self);
222 /* we need exactly 2 roundtrips to discover global objects and their state */
223 for (i = 0; i < 2; i++) {
224 if (gst_wl_display_roundtrip (self) < 0) {
225 *error = g_error_new (g_quark_from_static_string ("GstWlDisplay"), 0,
226 "Error communicating with the wayland display");
227 g_object_unref (self);
232 self->thread = g_thread_try_new ("GstWlDisplay", gst_wl_display_thread_run,
235 g_propagate_prefixed_error (error, err,
236 "Failed to start thread for the display's events");
237 g_object_unref (self);