/**
* SECTION:gstbus
+ * @title: GstBus
* @short_description: Asynchronous message bus subsystem
* @see_also: #GstMessage, #GstElement
*
#include "gstbus.h"
#include "glib-compat-private.h"
+#ifdef G_OS_WIN32
+# ifndef EWOULDBLOCK
+# define EWOULDBLOCK EAGAIN /* This is just to placate gcc */
+# endif
+#endif /* G_OS_WIN32 */
+
#define GST_CAT_DEFAULT GST_CAT_BUS
/* bus signals */
enum
gboolean enable_async;
GstPoll *poll;
GPollFD pollfd;
-
- GList *sources;
};
#define gst_bus_parent_class parent_class
-G_DEFINE_TYPE (GstBus, gst_bus, GST_TYPE_OBJECT);
+G_DEFINE_TYPE_WITH_PRIVATE (GstBus, gst_bus, GST_TYPE_OBJECT);
static void
gst_bus_set_property (GObject * object,
bus->priv->poll = gst_poll_new_timer ();
gst_poll_get_read_gpollfd (bus->priv->poll, &bus->priv->pollfd);
}
+
+ G_OBJECT_CLASS (gst_bus_parent_class)->constructed (object);
}
static void
G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
G_STRUCT_OFFSET (GstBusClass, message), NULL, NULL,
g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_MESSAGE);
-
- g_type_class_add_private (klass, sizeof (GstBusPrivate));
}
static void
gst_bus_init (GstBus * bus)
{
- bus->priv = G_TYPE_INSTANCE_GET_PRIVATE (bus, GST_TYPE_BUS, GstBusPrivate);
+ bus->priv = gst_bus_get_instance_private (bus);
bus->priv->enable_async = DEFAULT_ENABLE_ASYNC;
g_mutex_init (&bus->priv->queue_lock);
bus->priv->queue = gst_atomic_queue_new (32);
- /* clear floating flag */
- gst_object_ref_sink (bus);
-
GST_DEBUG_OBJECT (bus, "created");
}
if (bus->priv->queue) {
GstMessage *message;
- GList *l;
g_mutex_lock (&bus->priv->queue_lock);
do {
g_mutex_unlock (&bus->priv->queue_lock);
g_mutex_clear (&bus->priv->queue_lock);
- GST_OBJECT_LOCK (bus);
- for (l = bus->priv->sources; l; l = l->next)
- g_source_remove_poll (l->data, &bus->priv->pollfd);
- g_list_free (bus->priv->sources);
- bus->priv->sources = NULL;
- GST_OBJECT_UNLOCK (bus);
-
if (bus->priv->poll)
gst_poll_free (bus->priv->poll);
bus->priv->poll = NULL;
{
GstBus *result;
- result = g_object_newv (gst_bus_get_type (), 0, NULL);
+ result = g_object_new (gst_bus_get_type (), NULL);
GST_DEBUG_OBJECT (result, "created new bus");
+ /* clear floating flag */
+ gst_object_ref_sink (result);
+
return result;
}
gst_atomic_queue_length (bus->priv->queue));
while ((message = gst_atomic_queue_pop (bus->priv->queue))) {
- if (bus->priv->poll)
- gst_poll_read_control (bus->priv->poll);
+ if (bus->priv->poll) {
+ while (!gst_poll_read_control (bus->priv->poll)) {
+ if (errno == EWOULDBLOCK) {
+ /* Retry, this can happen if pushing to the queue has finished,
+ * popping here succeeded but writing control did not finish
+ * before we got to this line. */
+ /* Give other threads the chance to do something */
+ g_thread_yield ();
+ continue;
+ } else {
+ /* This is a real error and means that either the bus is in an
+ * inconsistent state, or the GstPoll is invalid. GstPoll already
+ * prints a critical warning about this, no need to do that again
+ * ourselves */
+ break;
+ }
+ }
+ }
GST_DEBUG_OBJECT (bus, "got message %p, %s from %s, type mask is %u",
message, GST_MESSAGE_TYPE_NAME (message),
}
}
+/**
+ * gst_bus_get_pollfd:
+ * @bus: A #GstBus
+ * @fd: (out): A GPollFD to fill
+ *
+ * Gets the file descriptor from the bus which can be used to get notified about
+ * messages being available with functions like g_poll(), and allows integration
+ * into other event loops based on file descriptors.
+ * Whenever a message is available, the POLLIN / %G_IO_IN event is set.
+ *
+ * Warning: NEVER read or write anything to the returned fd but only use it
+ * for getting notifications via g_poll() or similar and then use the normal
+ * GstBus API, e.g. gst_bus_pop().
+ *
+ * Since: 1.14
+ */
+void
+gst_bus_get_pollfd (GstBus * bus, GPollFD * fd)
+{
+ g_return_if_fail (GST_IS_BUS (bus));
+ g_return_if_fail (bus->priv->poll != NULL);
+
+ *fd = bus->priv->pollfd;
+}
+
/* GSource for the bus
*/
typedef struct
{
GSource source;
- GWeakRef bus_ref;
+ GstBus *bus;
} GstBusSource;
static gboolean
gst_bus_source_check (GSource * source)
{
GstBusSource *bsrc = (GstBusSource *) source;
- GstBus *bus;
- gboolean ret = FALSE;
- bus = g_weak_ref_get (&bsrc->bus_ref);
- if (bus) {
- ret = bus->priv->pollfd.revents & (G_IO_IN | G_IO_HUP | G_IO_ERR);
- g_object_unref (bus);
- }
-
- return ret;
+ return bsrc->bus->priv->pollfd.revents & (G_IO_IN | G_IO_HUP | G_IO_ERR);
}
static gboolean
GstBusFunc handler = (GstBusFunc) callback;
GstBusSource *bsource = (GstBusSource *) source;
GstMessage *message;
- gboolean keep = TRUE;
+ gboolean keep;
GstBus *bus;
g_return_val_if_fail (bsource != NULL, FALSE);
- bus = g_weak_ref_get (&bsource->bus_ref);
+ bus = bsource->bus;
- if (bus) {
- g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
+ g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
- message = gst_bus_pop (bus);
+ message = gst_bus_pop (bus);
- /* The message queue might be empty if some other thread or callback set
- * the bus to flushing between check/prepare and dispatch */
- if (G_UNLIKELY (message == NULL))
- return TRUE;
+ /* The message queue might be empty if some other thread or callback set
+ * the bus to flushing between check/prepare and dispatch */
+ if (G_UNLIKELY (message == NULL))
+ return TRUE;
- if (!handler)
- goto no_handler;
+ if (!handler)
+ goto no_handler;
- GST_DEBUG_OBJECT (bus, "source %p calling dispatch with %" GST_PTR_FORMAT,
- source, message);
+ GST_DEBUG_OBJECT (bus, "source %p calling dispatch with %" GST_PTR_FORMAT,
+ source, message);
- keep = handler (bus, message, user_data);
- gst_message_unref (message);
+ keep = handler (bus, message, user_data);
+ gst_message_unref (message);
- GST_DEBUG_OBJECT (bus, "source %p handler returns %d", source, keep);
- g_object_unref (bus);
- } else {
- GST_WARNING ("GstBusSource without a bus and still attached to a context."
- " The application is responsible for removing the GstBus"
- " watch when it isn't needed anymore.");
- }
+ GST_DEBUG_OBJECT (bus, "source %p handler returns %d", source, keep);
return keep;
GstBusSource *bsource = (GstBusSource *) source;
GstBus *bus;
- bus = g_weak_ref_get (&bsource->bus_ref);
+ bus = bsource->bus;
- if (bus) {
- GST_DEBUG_OBJECT (bus, "finalize source %p", source);
+ GST_DEBUG_OBJECT (bus, "finalize source %p", source);
- GST_OBJECT_LOCK (bus);
- if (bus->priv->signal_watch == source)
- bus->priv->signal_watch = NULL;
- bus->priv->sources = g_list_remove (bus->priv->sources, source);
- GST_OBJECT_UNLOCK (bus);
+ GST_OBJECT_LOCK (bus);
+ if (bus->priv->signal_watch == source)
+ bus->priv->signal_watch = NULL;
+ GST_OBJECT_UNLOCK (bus);
- g_object_unref (bus);
- }
- g_weak_ref_clear (&bsource->bus_ref);
+ gst_object_unref (bsource->bus);
+ bsource->bus = NULL;
}
static GSourceFuncs gst_bus_source_funcs = {
gst_bus_source_finalize
};
-static GSource *
-gst_bus_create_watch_unlocked (GstBus * bus)
-{
- GstBusSource *source;
-
- g_return_val_if_fail (GST_IS_BUS (bus), NULL);
- g_return_val_if_fail (bus->priv->poll != NULL, NULL);
-
- source = (GstBusSource *) g_source_new (&gst_bus_source_funcs,
- sizeof (GstBusSource));
-
- g_source_set_name ((GSource *) source, "GStreamer message bus watch");
-
- g_weak_ref_init (&source->bus_ref, (GObject *) bus);
- bus->priv->sources = g_list_prepend (bus->priv->sources, source);
- g_source_add_poll ((GSource *) source, &bus->priv->pollfd);
-
- return (GSource *) source;
-}
-
/**
* gst_bus_create_watch:
* @bus: a #GstBus to create the watch for
* a message is on the bus. After the GSource is dispatched, the
* message is popped off the bus and unreffed.
*
- * Returns: (transfer full): a #GSource that can be added to a mainloop.
+ * Returns: (transfer full) (nullable): a #GSource that can be added to a mainloop.
*/
GSource *
gst_bus_create_watch (GstBus * bus)
{
- GSource *source;
+ GstBusSource *source;
g_return_val_if_fail (GST_IS_BUS (bus), NULL);
g_return_val_if_fail (bus->priv->poll != NULL, NULL);
- GST_OBJECT_LOCK (bus);
- source = gst_bus_create_watch_unlocked (bus);
- GST_OBJECT_UNLOCK (bus);
+ source = (GstBusSource *) g_source_new (&gst_bus_source_funcs,
+ sizeof (GstBusSource));
+
+ g_source_set_name ((GSource *) source, "GStreamer message bus watch");
+
+ source->bus = gst_object_ref (bus);
+ g_source_add_poll ((GSource *) source, &bus->priv->pollfd);
return (GSource *) source;
}
return 0;
}
- source = gst_bus_create_watch_unlocked (bus);
+ source = gst_bus_create_watch (bus);
if (!source) {
g_critical ("Creating bus watch failed");
return 0;
* from @func. If the watch was added to the default main context it is also
* possible to remove the watch using g_source_remove().
*
+ * The bus watch will take its own reference to the @bus, so it is safe to unref
+ * @bus using gst_object_unref() after setting the bus watch.
+ *
* MT safe.
*
* Returns: The event source id or 0 if @bus already got an event source.
* from @func. If the watch was added to the default main context it is also
* possible to remove the watch using g_source_remove().
*
- * Returns: The event source id or 0 if @bus already got an event source.
+ * The bus watch will take its own reference to the @bus, so it is safe to unref
+ * @bus using gst_object_unref() after setting the bus watch.
*
* MT safe.
+ *
+ * Returns: The event source id or 0 if @bus already got an event source.
*/
guint
gst_bus_add_watch (GstBus * bus, GstBusFunc func, gpointer user_data)