waylandsink: handle the list of supported formats properly
[platform/upstream/gstreamer.git] / ext / wayland / wldisplay.c
1 /* GStreamer Wayland video sink
2  *
3  * Copyright (C) 2014 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include "wldisplay.h"
26
27 #include <errno.h>
28
29 GST_DEBUG_CATEGORY_EXTERN (gstwayland_debug);
30 #define GST_CAT_DEFAULT gstwayland_debug
31
32 G_DEFINE_TYPE (GstWlDisplay, gst_wl_display, G_TYPE_OBJECT);
33
34 static void gst_wl_display_finalize (GObject * gobject);
35
36 static void
37 gst_wl_display_class_init (GstWlDisplayClass * klass)
38 {
39   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
40   gobject_class->finalize = gst_wl_display_finalize;
41 }
42
43 static void
44 gst_wl_display_init (GstWlDisplay * self)
45 {
46   self->formats = g_array_new (FALSE, FALSE, sizeof (uint32_t));
47   self->wl_fd_poll = gst_poll_new (TRUE);
48 }
49
50 static void
51 gst_wl_display_finalize (GObject * gobject)
52 {
53   GstWlDisplay *self = GST_WL_DISPLAY (gobject);
54
55   gst_poll_set_flushing (self->wl_fd_poll, TRUE);
56
57   if (self->thread)
58     g_thread_join (self->thread);
59
60   g_array_unref (self->formats);
61   gst_poll_free (self->wl_fd_poll);
62
63   if (self->shm)
64     wl_shm_destroy (self->shm);
65
66   if (self->shell)
67     wl_shell_destroy (self->shell);
68
69   if (self->compositor)
70     wl_compositor_destroy (self->compositor);
71
72   if (self->registry)
73     wl_registry_destroy (self->registry);
74
75   if (self->queue)
76     wl_event_queue_destroy (self->queue);
77
78   if (self->own_display) {
79     wl_display_flush (self->display);
80     wl_display_disconnect (self->display);
81   }
82
83   G_OBJECT_CLASS (gst_wl_display_parent_class)->finalize (gobject);
84 }
85
86 static void
87 sync_callback (void *data, struct wl_callback *callback, uint32_t serial)
88 {
89   gboolean *done = data;
90   *done = TRUE;
91 }
92
93 static const struct wl_callback_listener sync_listener = {
94   sync_callback
95 };
96
97 static gint
98 gst_wl_display_roundtrip (GstWlDisplay * self)
99 {
100   struct wl_callback *callback;
101   gint ret = 0;
102   gboolean done = FALSE;
103
104   g_return_val_if_fail (self != NULL, -1);
105
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);
113
114   return ret;
115 }
116
117 static void
118 shm_format (void *data, struct wl_shm *wl_shm, uint32_t format)
119 {
120   GstWlDisplay *self = data;
121
122   g_array_append_val (self->formats, format);
123 }
124
125 static const struct wl_shm_listener shm_listener = {
126   shm_format
127 };
128
129 static void
130 registry_handle_global (void *data, struct wl_registry *registry,
131     uint32_t id, const char *interface, uint32_t version)
132 {
133   GstWlDisplay *self = data;
134
135   if (g_strcmp0 (interface, "wl_compositor") == 0) {
136     self->compositor =
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);
143   }
144 }
145
146 static const struct wl_registry_listener registry_listener = {
147   registry_handle_global
148 };
149
150 static gpointer
151 gst_wl_display_thread_run (gpointer data)
152 {
153   GstWlDisplay *self = data;
154   GstPollFD pollfd = GST_POLL_FD_INIT;
155
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);
159
160   /* main loop */
161   while (1) {
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);
165
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);
169       if (normal)
170         break;
171       else
172         goto error;
173     } else {
174       wl_display_read_events (self->display);
175       wl_display_dispatch_queue_pending (self->display, self->queue);
176     }
177   }
178
179   return NULL;
180
181 error:
182   GST_ERROR ("Error communicating with the wayland server");
183   return NULL;
184 }
185
186 GstWlDisplay *
187 gst_wl_display_new (const gchar * name, GError ** error)
188 {
189   struct wl_display *display;
190
191   display = wl_display_connect (name);
192
193   if (!display) {
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)");
197     return NULL;
198   } else {
199     return gst_wl_display_new_existing (display, TRUE, error);
200   }
201 }
202
203 GstWlDisplay *
204 gst_wl_display_new_existing (struct wl_display * display,
205     gboolean take_ownership, GError ** error)
206 {
207   GstWlDisplay *self;
208   GError *err = NULL;
209   gint i;
210
211   g_return_val_if_fail (display != NULL, NULL);
212
213   self = g_object_new (GST_TYPE_WL_DISPLAY, NULL);
214   self->display = display;
215   self->own_display = take_ownership;
216
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, &registry_listener, self);
221
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);
228       return NULL;
229     }
230   }
231
232   self->thread = g_thread_try_new ("GstWlDisplay", gst_wl_display_thread_run,
233       self, &err);
234   if (err) {
235     g_propagate_prefixed_error (error, err,
236         "Failed to start thread for the display's events");
237     g_object_unref (self);
238     return NULL;
239   }
240
241   return self;
242 }