Merge branch 'move_subdir_rtsp-server' into tizen_gst_1.19.2_mono
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / 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 #include "wlbuffer.h"
27 #include "wlvideoformat.h"
28
29 #include <errno.h>
30
31 GST_DEBUG_CATEGORY_EXTERN (gstwayland_debug);
32 #define GST_CAT_DEFAULT gstwayland_debug
33
34 G_DEFINE_TYPE (GstWlDisplay, gst_wl_display, G_TYPE_OBJECT);
35
36 static void gst_wl_display_finalize (GObject * gobject);
37
38 static void
39 gst_wl_display_class_init (GstWlDisplayClass * klass)
40 {
41   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
42   gobject_class->finalize = gst_wl_display_finalize;
43 }
44
45 static void
46 gst_wl_display_init (GstWlDisplay * self)
47 {
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);
53 }
54
55 static void
56 gst_wl_ref_wl_buffer (gpointer key, gpointer value, gpointer user_data)
57 {
58   g_object_ref (value);
59 }
60
61 static void
62 gst_wl_display_finalize (GObject * gobject)
63 {
64   GstWlDisplay *self = GST_WL_DISPLAY (gobject);
65
66   gst_poll_set_flushing (self->wl_fd_poll, TRUE);
67   if (self->thread)
68     g_thread_join (self->thread);
69
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);
76
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);
80
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);
86
87   if (self->viewporter)
88     wp_viewporter_destroy (self->viewporter);
89
90   if (self->shm)
91     wl_shm_destroy (self->shm);
92
93   if (self->dmabuf)
94     zwp_linux_dmabuf_v1_destroy (self->dmabuf);
95
96   if (self->wl_shell)
97     wl_shell_destroy (self->wl_shell);
98
99   if (self->xdg_wm_base)
100     xdg_wm_base_destroy (self->xdg_wm_base);
101
102   if (self->fullscreen_shell)
103     zwp_fullscreen_shell_v1_release (self->fullscreen_shell);
104
105   if (self->compositor)
106     wl_compositor_destroy (self->compositor);
107
108   if (self->subcompositor)
109     wl_subcompositor_destroy (self->subcompositor);
110
111   if (self->registry)
112     wl_registry_destroy (self->registry);
113
114   if (self->display_wrapper)
115     wl_proxy_wrapper_destroy (self->display_wrapper);
116
117   if (self->queue)
118     wl_event_queue_destroy (self->queue);
119
120   if (self->own_display) {
121     wl_display_flush (self->display);
122     wl_display_disconnect (self->display);
123   }
124
125   G_OBJECT_CLASS (gst_wl_display_parent_class)->finalize (gobject);
126 }
127
128 static void
129 shm_format (void *data, struct wl_shm *wl_shm, uint32_t format)
130 {
131   GstWlDisplay *self = data;
132
133   g_array_append_val (self->shm_formats, format);
134 }
135
136 static const struct wl_shm_listener shm_listener = {
137   shm_format
138 };
139
140 static void
141 dmabuf_format (void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf,
142     uint32_t format)
143 {
144   GstWlDisplay *self = data;
145
146   if (gst_wl_dmabuf_format_to_video_format (format) != GST_VIDEO_FORMAT_UNKNOWN)
147     g_array_append_val (self->dmabuf_formats, format);
148 }
149
150 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
151   dmabuf_format,
152 };
153
154 gboolean
155 gst_wl_display_check_format_for_shm (GstWlDisplay * display,
156     GstVideoFormat format)
157 {
158   enum wl_shm_format shm_fmt;
159   GArray *formats;
160   guint i;
161
162   shm_fmt = gst_video_format_to_wl_shm_format (format);
163   if (shm_fmt == (enum wl_shm_format) -1)
164     return FALSE;
165
166   formats = display->shm_formats;
167   for (i = 0; i < formats->len; i++) {
168     if (g_array_index (formats, uint32_t, i) == shm_fmt)
169       return TRUE;
170   }
171
172   return FALSE;
173 }
174
175 gboolean
176 gst_wl_display_check_format_for_dmabuf (GstWlDisplay * display,
177     GstVideoFormat format)
178 {
179   GArray *formats;
180   guint i, dmabuf_fmt;
181
182   if (!display->dmabuf)
183     return FALSE;
184
185   dmabuf_fmt = gst_video_format_to_wl_dmabuf_format (format);
186   if (dmabuf_fmt == (guint) - 1)
187     return FALSE;
188
189   formats = display->dmabuf_formats;
190   for (i = 0; i < formats->len; i++) {
191     if (g_array_index (formats, uint32_t, i) == dmabuf_fmt)
192       return TRUE;
193   }
194
195   return FALSE;
196 }
197
198 static void
199 handle_xdg_wm_base_ping (void *user_data, struct xdg_wm_base *xdg_wm_base,
200     uint32_t serial)
201 {
202   xdg_wm_base_pong (xdg_wm_base, serial);
203 }
204
205 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
206   handle_xdg_wm_base_ping
207 };
208
209 static void
210 registry_handle_global (void *data, struct wl_registry *registry,
211     uint32_t id, const char *interface, uint32_t version)
212 {
213   GstWlDisplay *self = data;
214
215   if (g_strcmp0 (interface, "wl_compositor") == 0) {
216     self->compositor = wl_registry_bind (registry, id, &wl_compositor_interface,
217         MIN (version, 3));
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) {
224     self->xdg_wm_base =
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) {
234     self->viewporter =
235         wl_registry_bind (registry, id, &wp_viewporter_interface, 1);
236   } else if (g_strcmp0 (interface, "zwp_linux_dmabuf_v1") == 0) {
237     self->dmabuf =
238         wl_registry_bind (registry, id, &zwp_linux_dmabuf_v1_interface, 1);
239     zwp_linux_dmabuf_v1_add_listener (self->dmabuf, &dmabuf_listener, self);
240   }
241 }
242
243 static void
244 registry_handle_global_remove (void *data, struct wl_registry *registry,
245     uint32_t name)
246 {
247   /* temporarily do nothing */
248   GST_LOG ("Removed global object: name(%d)", name);
249 }
250
251 static const struct wl_registry_listener registry_listener = {
252   registry_handle_global,
253   registry_handle_global_remove
254 };
255
256 static gpointer
257 gst_wl_display_thread_run (gpointer data)
258 {
259   GstWlDisplay *self = data;
260   GstPollFD pollfd = GST_POLL_FD_INIT;
261
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);
265
266   /* main loop */
267   while (1) {
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);
271
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);
275       if (normal)
276         break;
277       else
278         goto error;
279     }
280     if (wl_display_read_events (self->display) == -1)
281       goto error;
282     wl_display_dispatch_queue_pending (self->display, self->queue);
283   }
284
285   return NULL;
286
287 error:
288   GST_ERROR ("Error communicating with the wayland server");
289   return NULL;
290 }
291
292 GstWlDisplay *
293 gst_wl_display_new (const gchar * name, GError ** error)
294 {
295   struct wl_display *display;
296
297   display = wl_display_connect (name);
298
299   if (!display) {
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)");
303     return NULL;
304   } else {
305     return gst_wl_display_new_existing (display, TRUE, error);
306   }
307 }
308
309 GstWlDisplay *
310 gst_wl_display_new_existing (struct wl_display * display,
311     gboolean take_ownership, GError ** error)
312 {
313   GstWlDisplay *self;
314   GError *err = NULL;
315   gint i;
316
317   g_return_val_if_fail (display != NULL, NULL);
318
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;
323
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, &registry_listener, self);
328
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);
335       return NULL;
336     }
337   }
338
339   /* verify we got all the required interfaces */
340 #define VERIFY_INTERFACE_EXISTS(var, interface) \
341   if (!self->var) { \
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); \
346     return NULL; \
347   }
348
349   VERIFY_INTERFACE_EXISTS (compositor, "wl_compositor");
350   VERIFY_INTERFACE_EXISTS (subcompositor, "wl_subcompositor");
351   VERIFY_INTERFACE_EXISTS (shm, "wl_shm");
352
353 #undef VERIFY_INTERFACE_EXISTS
354
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.");
361   }
362
363   if (!self->dmabuf) {
364     g_warning ("Could not bind to zwp_linux_dmabuf_v1");
365   }
366
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.
371      */
372     g_warning ("Could not bind to either wl_shell, xdg_wm_base or "
373         "zwp_fullscreen_shell, video display may not work properly.");
374   }
375
376   self->thread = g_thread_try_new ("GstWlDisplay", gst_wl_display_thread_run,
377       self, &err);
378   if (err) {
379     g_propagate_prefixed_error (error, err,
380         "Failed to start thread for the display's events");
381     g_object_unref (self);
382     return NULL;
383   }
384
385   return self;
386 }
387
388 void
389 gst_wl_display_register_buffer (GstWlDisplay * self, gpointer gstmem,
390     gpointer wlbuffer)
391 {
392   g_assert (!self->shutting_down);
393
394   GST_TRACE_OBJECT (self, "registering GstWlBuffer %p to GstMem %p",
395       wlbuffer, gstmem);
396
397   g_mutex_lock (&self->buffers_mutex);
398   g_hash_table_replace (self->buffers, gstmem, wlbuffer);
399   g_mutex_unlock (&self->buffers_mutex);
400 }
401
402 gpointer
403 gst_wl_display_lookup_buffer (GstWlDisplay * self, gpointer gstmem)
404 {
405   gpointer wlbuffer;
406   g_mutex_lock (&self->buffers_mutex);
407   wlbuffer = g_hash_table_lookup (self->buffers, gstmem);
408   g_mutex_unlock (&self->buffers_mutex);
409   return wlbuffer;
410 }
411
412 void
413 gst_wl_display_unregister_buffer (GstWlDisplay * self, gpointer gstmem)
414 {
415   GST_TRACE_OBJECT (self, "unregistering GstWlBuffer owned by %p", gstmem);
416
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);
421 }