2014-02-12 10:28:40 +00:00
|
|
|
/* GStreamer Wayland video sink
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Collabora Ltd.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2014-02-13 12:15:31 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2014-02-12 10:28:40 +00:00
|
|
|
#include "wldisplay.h"
|
2014-06-20 11:47:57 +00:00
|
|
|
#include "wlbuffer.h"
|
2016-09-22 09:10:47 +00:00
|
|
|
#include "wlvideoformat.h"
|
2014-02-12 10:28:40 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
2014-02-13 12:28:40 +00:00
|
|
|
GST_DEBUG_CATEGORY_EXTERN (gstwayland_debug);
|
|
|
|
#define GST_CAT_DEFAULT gstwayland_debug
|
|
|
|
|
2014-02-12 10:28:40 +00:00
|
|
|
G_DEFINE_TYPE (GstWlDisplay, gst_wl_display, G_TYPE_OBJECT);
|
|
|
|
|
|
|
|
static void gst_wl_display_finalize (GObject * gobject);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_wl_display_class_init (GstWlDisplayClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
gobject_class->finalize = gst_wl_display_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_wl_display_init (GstWlDisplay * self)
|
|
|
|
{
|
2015-08-30 12:55:11 +00:00
|
|
|
self->shm_formats = g_array_new (FALSE, FALSE, sizeof (uint32_t));
|
2016-09-22 09:10:47 +00:00
|
|
|
self->dmabuf_formats = g_array_new (FALSE, FALSE, sizeof (uint32_t));
|
2014-02-12 10:28:40 +00:00
|
|
|
self->wl_fd_poll = gst_poll_new (TRUE);
|
2014-06-20 11:47:57 +00:00
|
|
|
self->buffers = g_hash_table_new (g_direct_hash, g_direct_equal);
|
2014-07-02 10:29:55 +00:00
|
|
|
g_mutex_init (&self->buffers_mutex);
|
2014-02-12 10:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_wl_display_finalize (GObject * gobject)
|
|
|
|
{
|
|
|
|
GstWlDisplay *self = GST_WL_DISPLAY (gobject);
|
|
|
|
|
2014-06-23 14:27:01 +00:00
|
|
|
gst_poll_set_flushing (self->wl_fd_poll, TRUE);
|
2016-03-16 11:19:25 +00:00
|
|
|
if (self->thread)
|
|
|
|
g_thread_join (self->thread);
|
2014-06-23 14:27:01 +00:00
|
|
|
|
2014-07-02 10:29:55 +00:00
|
|
|
/* to avoid buffers being unregistered from another thread
|
|
|
|
* at the same time, take their ownership */
|
|
|
|
g_mutex_lock (&self->buffers_mutex);
|
|
|
|
self->shutting_down = TRUE;
|
|
|
|
g_hash_table_foreach (self->buffers, (GHFunc) g_object_ref, NULL);
|
|
|
|
g_mutex_unlock (&self->buffers_mutex);
|
|
|
|
|
2014-06-23 14:27:01 +00:00
|
|
|
g_hash_table_foreach (self->buffers,
|
|
|
|
(GHFunc) gst_wl_buffer_force_release_and_unref, NULL);
|
|
|
|
g_hash_table_remove_all (self->buffers);
|
|
|
|
|
2015-08-30 12:55:11 +00:00
|
|
|
g_array_unref (self->shm_formats);
|
2016-09-22 09:10:47 +00:00
|
|
|
g_array_unref (self->dmabuf_formats);
|
2014-02-12 10:28:40 +00:00
|
|
|
gst_poll_free (self->wl_fd_poll);
|
2014-06-20 11:47:57 +00:00
|
|
|
g_hash_table_unref (self->buffers);
|
2014-07-02 10:29:55 +00:00
|
|
|
g_mutex_clear (&self->buffers_mutex);
|
2014-02-12 10:28:40 +00:00
|
|
|
|
2016-09-21 19:07:50 +00:00
|
|
|
if (self->viewporter)
|
|
|
|
wp_viewporter_destroy (self->viewporter);
|
|
|
|
|
2014-02-12 10:28:40 +00:00
|
|
|
if (self->shm)
|
|
|
|
wl_shm_destroy (self->shm);
|
|
|
|
|
2016-09-22 09:10:47 +00:00
|
|
|
if (self->dmabuf)
|
|
|
|
zwp_linux_dmabuf_v1_destroy (self->dmabuf);
|
|
|
|
|
2014-02-12 10:28:40 +00:00
|
|
|
if (self->shell)
|
|
|
|
wl_shell_destroy (self->shell);
|
|
|
|
|
|
|
|
if (self->compositor)
|
|
|
|
wl_compositor_destroy (self->compositor);
|
|
|
|
|
2014-05-23 15:18:32 +00:00
|
|
|
if (self->subcompositor)
|
|
|
|
wl_subcompositor_destroy (self->subcompositor);
|
|
|
|
|
2014-02-12 10:28:40 +00:00
|
|
|
if (self->registry)
|
|
|
|
wl_registry_destroy (self->registry);
|
|
|
|
|
|
|
|
if (self->queue)
|
|
|
|
wl_event_queue_destroy (self->queue);
|
|
|
|
|
|
|
|
if (self->own_display) {
|
|
|
|
wl_display_flush (self->display);
|
|
|
|
wl_display_disconnect (self->display);
|
|
|
|
}
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_wl_display_parent_class)->finalize (gobject);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sync_callback (void *data, struct wl_callback *callback, uint32_t serial)
|
|
|
|
{
|
|
|
|
gboolean *done = data;
|
|
|
|
*done = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_callback_listener sync_listener = {
|
|
|
|
sync_callback
|
|
|
|
};
|
|
|
|
|
|
|
|
static gint
|
|
|
|
gst_wl_display_roundtrip (GstWlDisplay * self)
|
|
|
|
{
|
|
|
|
struct wl_callback *callback;
|
|
|
|
gint ret = 0;
|
|
|
|
gboolean done = FALSE;
|
|
|
|
|
|
|
|
g_return_val_if_fail (self != NULL, -1);
|
|
|
|
|
|
|
|
/* We don't own the display, process only our queue */
|
|
|
|
callback = wl_display_sync (self->display);
|
|
|
|
wl_callback_add_listener (callback, &sync_listener, &done);
|
|
|
|
wl_proxy_set_queue ((struct wl_proxy *) callback, self->queue);
|
|
|
|
while (ret != -1 && !done)
|
|
|
|
ret = wl_display_dispatch_queue (self->display, self->queue);
|
|
|
|
wl_callback_destroy (callback);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
shm_format (void *data, struct wl_shm *wl_shm, uint32_t format)
|
|
|
|
{
|
|
|
|
GstWlDisplay *self = data;
|
|
|
|
|
2015-08-30 12:55:11 +00:00
|
|
|
g_array_append_val (self->shm_formats, format);
|
2014-02-12 10:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_shm_listener shm_listener = {
|
|
|
|
shm_format
|
|
|
|
};
|
|
|
|
|
2016-09-22 09:10:47 +00:00
|
|
|
static void
|
|
|
|
dmabuf_format (void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf,
|
|
|
|
uint32_t format)
|
|
|
|
{
|
|
|
|
GstWlDisplay *self = data;
|
|
|
|
|
|
|
|
if (gst_wl_dmabuf_format_to_video_format (format) != GST_VIDEO_FORMAT_UNKNOWN)
|
|
|
|
g_array_append_val (self->dmabuf_formats, format);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
|
|
|
|
dmabuf_format,
|
|
|
|
};
|
|
|
|
|
|
|
|
gboolean
|
2016-09-29 20:30:33 +00:00
|
|
|
gst_wl_display_check_format_for_shm (GstWlDisplay * display,
|
|
|
|
GstVideoFormat format)
|
2016-09-22 09:10:47 +00:00
|
|
|
{
|
2016-09-29 20:30:33 +00:00
|
|
|
enum wl_shm_format shm_fmt;
|
2016-09-22 09:10:47 +00:00
|
|
|
GArray *formats;
|
|
|
|
guint i;
|
|
|
|
|
2016-09-29 20:30:33 +00:00
|
|
|
shm_fmt = gst_video_format_to_wl_shm_format (format);
|
|
|
|
if (shm_fmt < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2016-09-22 09:10:47 +00:00
|
|
|
formats = display->shm_formats;
|
|
|
|
for (i = 0; i < formats->len; i++) {
|
2016-09-29 20:30:33 +00:00
|
|
|
if (g_array_index (formats, uint32_t, i) == shm_fmt)
|
2016-09-22 09:10:47 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2016-09-29 20:30:33 +00:00
|
|
|
gst_wl_display_check_format_for_dmabuf (GstWlDisplay * display,
|
|
|
|
GstVideoFormat format)
|
2016-09-22 09:10:47 +00:00
|
|
|
{
|
|
|
|
GArray *formats;
|
2016-09-29 20:30:33 +00:00
|
|
|
guint i, dmabuf_fmt;
|
|
|
|
|
|
|
|
if (!display->dmabuf)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
dmabuf_fmt = gst_video_format_to_wl_dmabuf_format (format);
|
|
|
|
if (dmabuf_fmt < 0)
|
|
|
|
return FALSE;
|
2016-09-22 09:10:47 +00:00
|
|
|
|
|
|
|
formats = display->dmabuf_formats;
|
|
|
|
for (i = 0; i < formats->len; i++) {
|
2016-09-29 20:30:33 +00:00
|
|
|
if (g_array_index (formats, uint32_t, i) == dmabuf_fmt)
|
2016-09-22 09:10:47 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-02-12 10:28:40 +00:00
|
|
|
static void
|
|
|
|
registry_handle_global (void *data, struct wl_registry *registry,
|
|
|
|
uint32_t id, const char *interface, uint32_t version)
|
|
|
|
{
|
|
|
|
GstWlDisplay *self = data;
|
|
|
|
|
|
|
|
if (g_strcmp0 (interface, "wl_compositor") == 0) {
|
2014-03-12 15:03:10 +00:00
|
|
|
self->compositor = wl_registry_bind (registry, id, &wl_compositor_interface,
|
|
|
|
MIN (version, 3));
|
2014-05-23 15:18:32 +00:00
|
|
|
} else if (g_strcmp0 (interface, "wl_subcompositor") == 0) {
|
|
|
|
self->subcompositor =
|
|
|
|
wl_registry_bind (registry, id, &wl_subcompositor_interface, 1);
|
2014-02-12 10:28:40 +00:00
|
|
|
} else if (g_strcmp0 (interface, "wl_shell") == 0) {
|
|
|
|
self->shell = wl_registry_bind (registry, id, &wl_shell_interface, 1);
|
|
|
|
} else if (g_strcmp0 (interface, "wl_shm") == 0) {
|
|
|
|
self->shm = wl_registry_bind (registry, id, &wl_shm_interface, 1);
|
|
|
|
wl_shm_add_listener (self->shm, &shm_listener, self);
|
2016-06-14 23:34:35 +00:00
|
|
|
} else if (g_strcmp0 (interface, "wp_viewporter") == 0) {
|
|
|
|
self->viewporter =
|
|
|
|
wl_registry_bind (registry, id, &wp_viewporter_interface, 1);
|
2016-09-22 09:10:47 +00:00
|
|
|
} else if (g_strcmp0 (interface, "zwp_linux_dmabuf_v1") == 0) {
|
|
|
|
self->dmabuf =
|
|
|
|
wl_registry_bind (registry, id, &zwp_linux_dmabuf_v1_interface, 1);
|
|
|
|
zwp_linux_dmabuf_v1_add_listener (self->dmabuf, &dmabuf_listener, self);
|
2014-02-12 10:28:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_registry_listener registry_listener = {
|
|
|
|
registry_handle_global
|
|
|
|
};
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gst_wl_display_thread_run (gpointer data)
|
|
|
|
{
|
|
|
|
GstWlDisplay *self = data;
|
|
|
|
GstPollFD pollfd = GST_POLL_FD_INIT;
|
|
|
|
|
|
|
|
pollfd.fd = wl_display_get_fd (self->display);
|
|
|
|
gst_poll_add_fd (self->wl_fd_poll, &pollfd);
|
|
|
|
gst_poll_fd_ctl_read (self->wl_fd_poll, &pollfd, TRUE);
|
|
|
|
|
|
|
|
/* main loop */
|
|
|
|
while (1) {
|
|
|
|
while (wl_display_prepare_read_queue (self->display, self->queue) != 0)
|
|
|
|
wl_display_dispatch_queue_pending (self->display, self->queue);
|
|
|
|
wl_display_flush (self->display);
|
|
|
|
|
|
|
|
if (gst_poll_wait (self->wl_fd_poll, GST_CLOCK_TIME_NONE) < 0) {
|
|
|
|
gboolean normal = (errno == EBUSY);
|
|
|
|
wl_display_cancel_read (self->display);
|
|
|
|
if (normal)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
goto error;
|
|
|
|
} else {
|
|
|
|
wl_display_read_events (self->display);
|
|
|
|
wl_display_dispatch_queue_pending (self->display, self->queue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
error:
|
|
|
|
GST_ERROR ("Error communicating with the wayland server");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstWlDisplay *
|
|
|
|
gst_wl_display_new (const gchar * name, GError ** error)
|
|
|
|
{
|
|
|
|
struct wl_display *display;
|
|
|
|
|
|
|
|
display = wl_display_connect (name);
|
|
|
|
|
|
|
|
if (!display) {
|
|
|
|
*error = g_error_new (g_quark_from_static_string ("GstWlDisplay"), 0,
|
|
|
|
"Failed to connect to the wayland display '%s'",
|
|
|
|
name ? name : "(default)");
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return gst_wl_display_new_existing (display, TRUE, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GstWlDisplay *
|
|
|
|
gst_wl_display_new_existing (struct wl_display * display,
|
|
|
|
gboolean take_ownership, GError ** error)
|
|
|
|
{
|
|
|
|
GstWlDisplay *self;
|
|
|
|
GError *err = NULL;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_return_val_if_fail (display != NULL, NULL);
|
|
|
|
|
|
|
|
self = g_object_new (GST_TYPE_WL_DISPLAY, NULL);
|
|
|
|
self->display = display;
|
|
|
|
self->own_display = take_ownership;
|
|
|
|
|
|
|
|
self->queue = wl_display_create_queue (self->display);
|
|
|
|
self->registry = wl_display_get_registry (self->display);
|
|
|
|
wl_proxy_set_queue ((struct wl_proxy *) self->registry, self->queue);
|
|
|
|
wl_registry_add_listener (self->registry, ®istry_listener, self);
|
|
|
|
|
|
|
|
/* we need exactly 2 roundtrips to discover global objects and their state */
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if (gst_wl_display_roundtrip (self) < 0) {
|
|
|
|
*error = g_error_new (g_quark_from_static_string ("GstWlDisplay"), 0,
|
|
|
|
"Error communicating with the wayland display");
|
|
|
|
g_object_unref (self);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-16 16:53:53 +00:00
|
|
|
/* verify we got all the required interfaces */
|
|
|
|
#define VERIFY_INTERFACE_EXISTS(var, interface) \
|
|
|
|
if (!self->var) { \
|
|
|
|
g_set_error (error, g_quark_from_static_string ("GstWlDisplay"), 0, \
|
|
|
|
"Could not bind to " interface ". Either it is not implemented in " \
|
|
|
|
"the compositor, or the implemented version doesn't match"); \
|
|
|
|
g_object_unref (self); \
|
|
|
|
return NULL; \
|
|
|
|
}
|
|
|
|
|
|
|
|
VERIFY_INTERFACE_EXISTS (compositor, "wl_compositor");
|
|
|
|
VERIFY_INTERFACE_EXISTS (subcompositor, "wl_subcompositor");
|
|
|
|
VERIFY_INTERFACE_EXISTS (shell, "wl_shell");
|
|
|
|
VERIFY_INTERFACE_EXISTS (shm, "wl_shm");
|
|
|
|
|
|
|
|
#undef VERIFY_INTERFACE_EXISTS
|
|
|
|
|
2016-09-15 18:10:02 +00:00
|
|
|
/* We make the viewporter optional even though it may cause bad display.
|
|
|
|
* This is so one can test wayland display on older compositor or on
|
|
|
|
* compositor that don't implement this extension. */
|
|
|
|
if (!self->viewporter) {
|
|
|
|
g_warning ("Wayland compositor is missing the ability to scale, video "
|
|
|
|
"display may not work properly.");
|
|
|
|
}
|
|
|
|
|
2016-09-22 09:10:47 +00:00
|
|
|
if (!self->dmabuf) {
|
|
|
|
g_warning ("Could not bind to zwp_linux_dmabuf_v1");
|
|
|
|
}
|
|
|
|
|
2014-02-12 10:28:40 +00:00
|
|
|
self->thread = g_thread_try_new ("GstWlDisplay", gst_wl_display_thread_run,
|
|
|
|
self, &err);
|
|
|
|
if (err) {
|
|
|
|
g_propagate_prefixed_error (error, err,
|
|
|
|
"Failed to start thread for the display's events");
|
|
|
|
g_object_unref (self);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
2014-06-20 11:47:57 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
gst_wl_display_register_buffer (GstWlDisplay * self, gpointer buf)
|
|
|
|
{
|
2014-07-02 10:29:55 +00:00
|
|
|
g_assert (!self->shutting_down);
|
|
|
|
|
|
|
|
GST_TRACE_OBJECT (self, "registering GstWlBuffer %p", buf);
|
|
|
|
|
|
|
|
g_mutex_lock (&self->buffers_mutex);
|
2014-06-20 11:47:57 +00:00
|
|
|
g_hash_table_add (self->buffers, buf);
|
2014-07-02 10:29:55 +00:00
|
|
|
g_mutex_unlock (&self->buffers_mutex);
|
2014-06-20 11:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_wl_display_unregister_buffer (GstWlDisplay * self, gpointer buf)
|
|
|
|
{
|
2014-07-02 10:29:55 +00:00
|
|
|
GST_TRACE_OBJECT (self, "unregistering GstWlBuffer %p", buf);
|
|
|
|
|
|
|
|
g_mutex_lock (&self->buffers_mutex);
|
|
|
|
if (G_LIKELY (!self->shutting_down))
|
|
|
|
g_hash_table_remove (self->buffers, buf);
|
|
|
|
g_mutex_unlock (&self->buffers_mutex);
|
2014-06-20 11:47:57 +00:00
|
|
|
}
|