Blame gst/gstbus.c

Packit a6ee4b
/* GStreamer
Packit a6ee4b
 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
Packit a6ee4b
 *
Packit a6ee4b
 * gstbus.c: GstBus subsystem
Packit a6ee4b
 *
Packit a6ee4b
 * This library is free software; you can redistribute it and/or
Packit a6ee4b
 * modify it under the terms of the GNU Library General Public
Packit a6ee4b
 * License as published by the Free Software Foundation; either
Packit a6ee4b
 * version 2 of the License, or (at your option) any later version.
Packit a6ee4b
 *
Packit a6ee4b
 * This library is distributed in the hope that it will be useful,
Packit a6ee4b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a6ee4b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a6ee4b
 * Library General Public License for more details.
Packit a6ee4b
 *
Packit a6ee4b
 * You should have received a copy of the GNU Library General Public
Packit a6ee4b
 * License along with this library; if not, write to the
Packit a6ee4b
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit a6ee4b
 * Boston, MA 02110-1301, USA.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * SECTION:gstbus
Packit a6ee4b
 * @title: GstBus
Packit a6ee4b
 * @short_description: Asynchronous message bus subsystem
Packit a6ee4b
 * @see_also: #GstMessage, #GstElement
Packit a6ee4b
 *
Packit a6ee4b
 * The #GstBus is an object responsible for delivering #GstMessage packets in
Packit a6ee4b
 * a first-in first-out way from the streaming threads (see #GstTask) to the
Packit a6ee4b
 * application.
Packit a6ee4b
 *
Packit a6ee4b
 * Since the application typically only wants to deal with delivery of these
Packit a6ee4b
 * messages from one thread, the GstBus will marshall the messages between
Packit a6ee4b
 * different threads. This is important since the actual streaming of media
Packit a6ee4b
 * is done in another thread than the application.
Packit a6ee4b
 *
Packit a6ee4b
 * The GstBus provides support for #GSource based notifications. This makes it
Packit a6ee4b
 * possible to handle the delivery in the glib mainloop.
Packit a6ee4b
 *
Packit a6ee4b
 * The #GSource callback function gst_bus_async_signal_func() can be used to
Packit a6ee4b
 * convert all bus messages into signal emissions.
Packit a6ee4b
 *
Packit a6ee4b
 * A message is posted on the bus with the gst_bus_post() method. With the
Packit a6ee4b
 * gst_bus_peek() and gst_bus_pop() methods one can look at or retrieve a
Packit a6ee4b
 * previously posted message.
Packit a6ee4b
 *
Packit a6ee4b
 * The bus can be polled with the gst_bus_poll() method. This methods blocks
Packit a6ee4b
 * up to the specified timeout value until one of the specified messages types
Packit a6ee4b
 * is posted on the bus. The application can then gst_bus_pop() the messages
Packit a6ee4b
 * from the bus to handle them.
Packit a6ee4b
 * Alternatively the application can register an asynchronous bus function
Packit a6ee4b
 * using gst_bus_add_watch_full() or gst_bus_add_watch(). This function will
Packit a6ee4b
 * install a #GSource in the default glib main loop and will deliver messages
Packit a6ee4b
 * a short while after they have been posted. Note that the main loop should
Packit a6ee4b
 * be running for the asynchronous callbacks.
Packit a6ee4b
 *
Packit a6ee4b
 * It is also possible to get messages from the bus without any thread
Packit a6ee4b
 * marshalling with the gst_bus_set_sync_handler() method. This makes it
Packit a6ee4b
 * possible to react to a message in the same thread that posted the
Packit a6ee4b
 * message on the bus. This should only be used if the application is able
Packit a6ee4b
 * to deal with messages from different threads.
Packit a6ee4b
 *
Packit a6ee4b
 * Every #GstPipeline has one bus.
Packit a6ee4b
 *
Packit a6ee4b
 * Note that a #GstPipeline will set its bus into flushing state when changing
Packit a6ee4b
 * from READY to NULL state.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
#include "gst_private.h"
Packit a6ee4b
#include <errno.h>
Packit a6ee4b
#ifdef HAVE_UNISTD_H
Packit a6ee4b
#  include <unistd.h>
Packit a6ee4b
#endif
Packit a6ee4b
#include <sys/types.h>
Packit a6ee4b
Packit a6ee4b
#include "gstatomicqueue.h"
Packit a6ee4b
#include "gstinfo.h"
Packit a6ee4b
#include "gstpoll.h"
Packit a6ee4b
Packit a6ee4b
#include "gstbus.h"
Packit a6ee4b
#include "glib-compat-private.h"
Packit a6ee4b
Packit a6ee4b
#ifdef G_OS_WIN32
Packit a6ee4b
#  ifndef EWOULDBLOCK
Packit a6ee4b
#  define EWOULDBLOCK EAGAIN    /* This is just to placate gcc */
Packit a6ee4b
#  endif
Packit a6ee4b
#endif /* G_OS_WIN32 */
Packit a6ee4b
Packit a6ee4b
#define GST_CAT_DEFAULT GST_CAT_BUS
Packit a6ee4b
/* bus signals */
Packit a6ee4b
enum
Packit a6ee4b
{
Packit a6ee4b
  SYNC_MESSAGE,
Packit a6ee4b
  ASYNC_MESSAGE,
Packit a6ee4b
  /* add more above */
Packit a6ee4b
  LAST_SIGNAL
Packit a6ee4b
};
Packit a6ee4b
Packit a6ee4b
#define DEFAULT_ENABLE_ASYNC (TRUE)
Packit a6ee4b
Packit a6ee4b
enum
Packit a6ee4b
{
Packit a6ee4b
  PROP_0,
Packit a6ee4b
  PROP_ENABLE_ASYNC
Packit a6ee4b
};
Packit a6ee4b
Packit a6ee4b
static void gst_bus_dispose (GObject * object);
Packit a6ee4b
static void gst_bus_finalize (GObject * object);
Packit a6ee4b
Packit a6ee4b
static guint gst_bus_signals[LAST_SIGNAL] = { 0 };
Packit a6ee4b
Packit a6ee4b
struct _GstBusPrivate
Packit a6ee4b
{
Packit a6ee4b
  GstAtomicQueue *queue;
Packit a6ee4b
  GMutex queue_lock;
Packit a6ee4b
Packit a6ee4b
  GstBusSyncHandler sync_handler;
Packit a6ee4b
  gpointer sync_handler_data;
Packit a6ee4b
  GDestroyNotify sync_handler_notify;
Packit a6ee4b
Packit a6ee4b
  guint num_signal_watchers;
Packit a6ee4b
Packit a6ee4b
  guint num_sync_message_emitters;
Packit a6ee4b
  GSource *signal_watch;
Packit a6ee4b
Packit a6ee4b
  gboolean enable_async;
Packit a6ee4b
  GstPoll *poll;
Packit a6ee4b
  GPollFD pollfd;
Packit a6ee4b
};
Packit a6ee4b
Packit a6ee4b
#define gst_bus_parent_class parent_class
Packit a6ee4b
G_DEFINE_TYPE_WITH_PRIVATE (GstBus, gst_bus, GST_TYPE_OBJECT);
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
gst_bus_set_property (GObject * object,
Packit a6ee4b
    guint prop_id, const GValue * value, GParamSpec * pspec)
Packit a6ee4b
{
Packit a6ee4b
  GstBus *bus = GST_BUS_CAST (object);
Packit a6ee4b
Packit a6ee4b
  switch (prop_id) {
Packit a6ee4b
    case PROP_ENABLE_ASYNC:
Packit a6ee4b
      bus->priv->enable_async = g_value_get_boolean (value);
Packit a6ee4b
      break;
Packit a6ee4b
    default:
Packit a6ee4b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit a6ee4b
      break;
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
gst_bus_constructed (GObject * object)
Packit a6ee4b
{
Packit a6ee4b
  GstBus *bus = GST_BUS_CAST (object);
Packit a6ee4b
Packit a6ee4b
  if (bus->priv->enable_async) {
Packit a6ee4b
    bus->priv->poll = gst_poll_new_timer ();
Packit a6ee4b
    gst_poll_get_read_gpollfd (bus->priv->poll, &bus->priv->pollfd);
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  G_OBJECT_CLASS (gst_bus_parent_class)->constructed (object);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
gst_bus_class_init (GstBusClass * klass)
Packit a6ee4b
{
Packit a6ee4b
  GObjectClass *gobject_class = (GObjectClass *) klass;
Packit a6ee4b
Packit a6ee4b
  gobject_class->dispose = gst_bus_dispose;
Packit a6ee4b
  gobject_class->finalize = gst_bus_finalize;
Packit a6ee4b
  gobject_class->set_property = gst_bus_set_property;
Packit a6ee4b
  gobject_class->constructed = gst_bus_constructed;
Packit a6ee4b
Packit a6ee4b
  /**
Packit a6ee4b
   * GstBus::enable-async:
Packit a6ee4b
   *
Packit a6ee4b
   * Enable async message delivery support for bus watches,
Packit a6ee4b
   * gst_bus_pop() and similar API. Without this only the
Packit a6ee4b
   * synchronous message handlers are called.
Packit a6ee4b
   *
Packit a6ee4b
   * This property is used to create the child element buses
Packit a6ee4b
   * in #GstBin.
Packit a6ee4b
   */
Packit a6ee4b
  g_object_class_install_property (gobject_class, PROP_ENABLE_ASYNC,
Packit a6ee4b
      g_param_spec_boolean ("enable-async", "Enable Async",
Packit a6ee4b
          "Enable async message delivery for bus watches and gst_bus_pop()",
Packit a6ee4b
          DEFAULT_ENABLE_ASYNC,
Packit a6ee4b
          G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
Packit a6ee4b
Packit a6ee4b
  /**
Packit a6ee4b
   * GstBus::sync-message:
Packit a6ee4b
   * @bus: the object which received the signal
Packit a6ee4b
   * @message: the message that has been posted synchronously
Packit a6ee4b
   *
Packit a6ee4b
   * A message has been posted on the bus. This signal is emitted from the
Packit a6ee4b
   * thread that posted the message so one has to be careful with locking.
Packit a6ee4b
   *
Packit a6ee4b
   * This signal will not be emitted by default, you have to call
Packit a6ee4b
   * gst_bus_enable_sync_message_emission() before.
Packit a6ee4b
   */
Packit a6ee4b
  gst_bus_signals[SYNC_MESSAGE] =
Packit a6ee4b
      g_signal_new ("sync-message", G_TYPE_FROM_CLASS (klass),
Packit a6ee4b
      G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
Packit a6ee4b
      G_STRUCT_OFFSET (GstBusClass, sync_message), NULL, NULL,
Packit a6ee4b
      g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_MESSAGE);
Packit a6ee4b
Packit a6ee4b
  /**
Packit a6ee4b
   * GstBus::message:
Packit a6ee4b
   * @bus: the object which received the signal
Packit a6ee4b
   * @message: the message that has been posted asynchronously
Packit a6ee4b
   *
Packit a6ee4b
   * A message has been posted on the bus. This signal is emitted from a
Packit a6ee4b
   * GSource added to the mainloop. this signal will only be emitted when
Packit a6ee4b
   * there is a mainloop running.
Packit a6ee4b
   */
Packit a6ee4b
  gst_bus_signals[ASYNC_MESSAGE] =
Packit a6ee4b
      g_signal_new ("message", G_TYPE_FROM_CLASS (klass),
Packit a6ee4b
      G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
Packit a6ee4b
      G_STRUCT_OFFSET (GstBusClass, message), NULL, NULL,
Packit a6ee4b
      g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_MESSAGE);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
gst_bus_init (GstBus * bus)
Packit a6ee4b
{
Packit a6ee4b
  bus->priv = gst_bus_get_instance_private (bus);
Packit a6ee4b
  bus->priv->enable_async = DEFAULT_ENABLE_ASYNC;
Packit a6ee4b
  g_mutex_init (&bus->priv->queue_lock);
Packit a6ee4b
  bus->priv->queue = gst_atomic_queue_new (32);
Packit a6ee4b
Packit a6ee4b
  GST_DEBUG_OBJECT (bus, "created");
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
gst_bus_dispose (GObject * object)
Packit a6ee4b
{
Packit a6ee4b
  GstBus *bus = GST_BUS (object);
Packit a6ee4b
Packit a6ee4b
  if (bus->priv->queue) {
Packit a6ee4b
    GstMessage *message;
Packit a6ee4b
Packit a6ee4b
    g_mutex_lock (&bus->priv->queue_lock);
Packit a6ee4b
    do {
Packit a6ee4b
      message = gst_atomic_queue_pop (bus->priv->queue);
Packit a6ee4b
      if (message)
Packit a6ee4b
        gst_message_unref (message);
Packit a6ee4b
    } while (message != NULL);
Packit a6ee4b
    gst_atomic_queue_unref (bus->priv->queue);
Packit a6ee4b
    bus->priv->queue = NULL;
Packit a6ee4b
    g_mutex_unlock (&bus->priv->queue_lock);
Packit a6ee4b
    g_mutex_clear (&bus->priv->queue_lock);
Packit a6ee4b
Packit a6ee4b
    if (bus->priv->poll)
Packit a6ee4b
      gst_poll_free (bus->priv->poll);
Packit a6ee4b
    bus->priv->poll = NULL;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  G_OBJECT_CLASS (parent_class)->dispose (object);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
gst_bus_finalize (GObject * object)
Packit a6ee4b
{
Packit a6ee4b
  GstBus *bus = GST_BUS (object);
Packit a6ee4b
Packit a6ee4b
  if (bus->priv->sync_handler_notify)
Packit a6ee4b
    bus->priv->sync_handler_notify (bus->priv->sync_handler_data);
Packit a6ee4b
Packit a6ee4b
  G_OBJECT_CLASS (parent_class)->finalize (object);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_new:
Packit a6ee4b
 *
Packit a6ee4b
 * Creates a new #GstBus instance.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full): a new #GstBus instance
Packit a6ee4b
 */
Packit a6ee4b
GstBus *
Packit a6ee4b
gst_bus_new (void)
Packit a6ee4b
{
Packit a6ee4b
  GstBus *result;
Packit a6ee4b
Packit a6ee4b
  result = g_object_new (gst_bus_get_type (), NULL);
Packit a6ee4b
  GST_DEBUG_OBJECT (result, "created new bus");
Packit a6ee4b
Packit a6ee4b
  /* clear floating flag */
Packit a6ee4b
  gst_object_ref_sink (result);
Packit a6ee4b
Packit a6ee4b
  return result;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_post:
Packit a6ee4b
 * @bus: a #GstBus to post on
Packit a6ee4b
 * @message: (transfer full): the #GstMessage to post
Packit a6ee4b
 *
Packit a6ee4b
 * Post a message on the given bus. Ownership of the message
Packit a6ee4b
 * is taken by the bus.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if the message could be posted, %FALSE if the bus is flushing.
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_bus_post (GstBus * bus, GstMessage * message)
Packit a6ee4b
{
Packit a6ee4b
  GstBusSyncReply reply = GST_BUS_PASS;
Packit a6ee4b
  GstBusSyncHandler handler;
Packit a6ee4b
  gboolean emit_sync_message;
Packit a6ee4b
  gpointer handler_data;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
Packit a6ee4b
  g_return_val_if_fail (GST_IS_MESSAGE (message), FALSE);
Packit a6ee4b
Packit a6ee4b
  GST_DEBUG_OBJECT (bus, "[msg %p] posting on bus %" GST_PTR_FORMAT, message,
Packit a6ee4b
      message);
Packit a6ee4b
Packit a6ee4b
  /* check we didn't accidentally add a public flag that maps to same value */
Packit a6ee4b
  g_assert (!GST_MINI_OBJECT_FLAG_IS_SET (message,
Packit a6ee4b
          GST_MESSAGE_FLAG_ASYNC_DELIVERY));
Packit a6ee4b
Packit a6ee4b
  GST_OBJECT_LOCK (bus);
Packit a6ee4b
  /* check if the bus is flushing */
Packit a6ee4b
  if (GST_OBJECT_FLAG_IS_SET (bus, GST_BUS_FLUSHING))
Packit a6ee4b
    goto is_flushing;
Packit a6ee4b
Packit a6ee4b
  handler = bus->priv->sync_handler;
Packit a6ee4b
  handler_data = bus->priv->sync_handler_data;
Packit a6ee4b
  emit_sync_message = bus->priv->num_sync_message_emitters > 0;
Packit a6ee4b
  GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
Packit a6ee4b
  /* first call the sync handler if it is installed */
Packit a6ee4b
  if (handler)
Packit a6ee4b
    reply = handler (bus, message, handler_data);
Packit a6ee4b
Packit a6ee4b
  /* emit sync-message if requested to do so via
Packit a6ee4b
     gst_bus_enable_sync_message_emission. terrible but effective */
Packit a6ee4b
  if (emit_sync_message && reply != GST_BUS_DROP
Packit a6ee4b
      && handler != gst_bus_sync_signal_handler)
Packit a6ee4b
    gst_bus_sync_signal_handler (bus, message, NULL);
Packit a6ee4b
Packit a6ee4b
  /* If this is a bus without async message delivery
Packit a6ee4b
   * always drop the message */
Packit a6ee4b
  if (!bus->priv->poll)
Packit a6ee4b
    reply = GST_BUS_DROP;
Packit a6ee4b
Packit a6ee4b
  /* now see what we should do with the message */
Packit a6ee4b
  switch (reply) {
Packit a6ee4b
    case GST_BUS_DROP:
Packit a6ee4b
      /* drop the message */
Packit a6ee4b
      GST_DEBUG_OBJECT (bus, "[msg %p] dropped", message);
Packit a6ee4b
      break;
Packit a6ee4b
    case GST_BUS_PASS:
Packit a6ee4b
      /* pass the message to the async queue, refcount passed in the queue */
Packit a6ee4b
      GST_DEBUG_OBJECT (bus, "[msg %p] pushing on async queue", message);
Packit a6ee4b
      gst_atomic_queue_push (bus->priv->queue, message);
Packit a6ee4b
      gst_poll_write_control (bus->priv->poll);
Packit a6ee4b
      GST_DEBUG_OBJECT (bus, "[msg %p] pushed on async queue", message);
Packit a6ee4b
Packit a6ee4b
      break;
Packit a6ee4b
    case GST_BUS_ASYNC:
Packit a6ee4b
    {
Packit a6ee4b
      /* async delivery, we need a mutex and a cond to block
Packit a6ee4b
       * on */
Packit a6ee4b
      GCond *cond = GST_MESSAGE_GET_COND (message);
Packit a6ee4b
      GMutex *lock = GST_MESSAGE_GET_LOCK (message);
Packit a6ee4b
Packit a6ee4b
      g_cond_init (cond);
Packit a6ee4b
      g_mutex_init (lock);
Packit a6ee4b
Packit a6ee4b
      GST_MINI_OBJECT_FLAG_SET (message, GST_MESSAGE_FLAG_ASYNC_DELIVERY);
Packit a6ee4b
Packit a6ee4b
      GST_DEBUG_OBJECT (bus, "[msg %p] waiting for async delivery", message);
Packit a6ee4b
Packit a6ee4b
      /* now we lock the message mutex, send the message to the async
Packit a6ee4b
       * queue. When the message is handled by the app and destroyed,
Packit a6ee4b
       * the cond will be signalled and we can continue */
Packit a6ee4b
      g_mutex_lock (lock);
Packit a6ee4b
Packit a6ee4b
      gst_atomic_queue_push (bus->priv->queue, message);
Packit a6ee4b
      gst_poll_write_control (bus->priv->poll);
Packit a6ee4b
Packit a6ee4b
      /* now block till the message is freed */
Packit a6ee4b
      g_cond_wait (cond, lock);
Packit a6ee4b
Packit a6ee4b
      /* we acquired a new ref from gst_message_dispose() so we can clean up */
Packit a6ee4b
      g_mutex_unlock (lock);
Packit a6ee4b
Packit a6ee4b
      GST_DEBUG_OBJECT (bus, "[msg %p] delivered asynchronously", message);
Packit a6ee4b
Packit a6ee4b
      GST_MINI_OBJECT_FLAG_UNSET (message, GST_MESSAGE_FLAG_ASYNC_DELIVERY);
Packit a6ee4b
Packit a6ee4b
      g_mutex_clear (lock);
Packit a6ee4b
      g_cond_clear (cond);
Packit a6ee4b
Packit a6ee4b
      gst_message_unref (message);
Packit a6ee4b
      break;
Packit a6ee4b
    }
Packit a6ee4b
    default:
Packit a6ee4b
      g_warning ("invalid return from bus sync handler");
Packit a6ee4b
      break;
Packit a6ee4b
  }
Packit a6ee4b
  return TRUE;
Packit a6ee4b
Packit a6ee4b
  /* ERRORS */
Packit a6ee4b
is_flushing:
Packit a6ee4b
  {
Packit a6ee4b
    GST_DEBUG_OBJECT (bus, "bus is flushing");
Packit a6ee4b
    GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
    gst_message_unref (message);
Packit a6ee4b
Packit a6ee4b
    return FALSE;
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_have_pending:
Packit a6ee4b
 * @bus: a #GstBus to check
Packit a6ee4b
 *
Packit a6ee4b
 * Check if there are pending messages on the bus that
Packit a6ee4b
 * should be handled.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if there are messages on the bus to be handled, %FALSE
Packit a6ee4b
 * otherwise.
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_bus_have_pending (GstBus * bus)
Packit a6ee4b
{
Packit a6ee4b
  gboolean result;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
Packit a6ee4b
Packit a6ee4b
  /* see if there is a message on the bus */
Packit a6ee4b
  result = gst_atomic_queue_length (bus->priv->queue) != 0;
Packit a6ee4b
Packit a6ee4b
  return result;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_set_flushing:
Packit a6ee4b
 * @bus: a #GstBus
Packit a6ee4b
 * @flushing: whether or not to flush the bus
Packit a6ee4b
 *
Packit a6ee4b
 * If @flushing, flush out and unref any messages queued in the bus. Releases
Packit a6ee4b
 * references to the message origin objects. Will flush future messages until
Packit a6ee4b
 * gst_bus_set_flushing() sets @flushing to %FALSE.
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_bus_set_flushing (GstBus * bus, gboolean flushing)
Packit a6ee4b
{
Packit a6ee4b
  GstMessage *message;
Packit a6ee4b
  GList *message_list = NULL;
Packit a6ee4b
Packit a6ee4b
  g_return_if_fail (GST_IS_BUS (bus));
Packit a6ee4b
Packit a6ee4b
  GST_OBJECT_LOCK (bus);
Packit a6ee4b
Packit a6ee4b
  if (flushing) {
Packit a6ee4b
    GST_OBJECT_FLAG_SET (bus, GST_BUS_FLUSHING);
Packit a6ee4b
Packit a6ee4b
    GST_DEBUG_OBJECT (bus, "set bus flushing");
Packit a6ee4b
Packit a6ee4b
    while ((message = gst_bus_pop (bus)))
Packit a6ee4b
      message_list = g_list_prepend (message_list, message);
Packit a6ee4b
  } else {
Packit a6ee4b
    GST_DEBUG_OBJECT (bus, "unset bus flushing");
Packit a6ee4b
    GST_OBJECT_FLAG_UNSET (bus, GST_BUS_FLUSHING);
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
Packit a6ee4b
  g_list_free_full (message_list, (GDestroyNotify) gst_message_unref);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_timed_pop_filtered:
Packit a6ee4b
 * @bus: a #GstBus to pop from
Packit a6ee4b
 * @timeout: a timeout in nanoseconds, or GST_CLOCK_TIME_NONE to wait forever
Packit a6ee4b
 * @types: message types to take into account, GST_MESSAGE_ANY for any type
Packit a6ee4b
 *
Packit a6ee4b
 * Get a message from the bus whose type matches the message type mask @types,
Packit a6ee4b
 * waiting up to the specified timeout (and discarding any messages that do not
Packit a6ee4b
 * match the mask provided).
Packit a6ee4b
 *
Packit a6ee4b
 * If @timeout is 0, this function behaves like gst_bus_pop_filtered(). If
Packit a6ee4b
 * @timeout is #GST_CLOCK_TIME_NONE, this function will block forever until a
Packit a6ee4b
 * matching message was posted on the bus.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full) (nullable): a #GstMessage matching the
Packit a6ee4b
 *     filter in @types, or %NULL if no matching message was found on
Packit a6ee4b
 *     the bus until the timeout expired. The message is taken from
Packit a6ee4b
 *     the bus and needs to be unreffed with gst_message_unref() after
Packit a6ee4b
 *     usage.
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 */
Packit a6ee4b
GstMessage *
Packit a6ee4b
gst_bus_timed_pop_filtered (GstBus * bus, GstClockTime timeout,
Packit a6ee4b
    GstMessageType types)
Packit a6ee4b
{
Packit a6ee4b
  GstMessage *message;
Packit a6ee4b
  GTimeVal now, then;
Packit a6ee4b
  gboolean first_round = TRUE;
Packit a6ee4b
  GstClockTime elapsed = 0;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUS (bus), NULL);
Packit a6ee4b
  g_return_val_if_fail (types != 0, NULL);
Packit a6ee4b
  g_return_val_if_fail (timeout == 0 || bus->priv->poll != NULL, NULL);
Packit a6ee4b
Packit a6ee4b
  g_mutex_lock (&bus->priv->queue_lock);
Packit a6ee4b
Packit a6ee4b
  while (TRUE) {
Packit a6ee4b
    gint ret;
Packit a6ee4b
Packit a6ee4b
    GST_LOG_OBJECT (bus, "have %d messages",
Packit a6ee4b
        gst_atomic_queue_length (bus->priv->queue));
Packit a6ee4b
Packit a6ee4b
    while ((message = gst_atomic_queue_pop (bus->priv->queue))) {
Packit a6ee4b
      if (bus->priv->poll) {
Packit a6ee4b
        while (!gst_poll_read_control (bus->priv->poll)) {
Packit a6ee4b
          if (errno == EWOULDBLOCK) {
Packit a6ee4b
            /* Retry, this can happen if pushing to the queue has finished,
Packit a6ee4b
             * popping here succeeded but writing control did not finish
Packit a6ee4b
             * before we got to this line. */
Packit a6ee4b
            /* Give other threads the chance to do something */
Packit a6ee4b
            g_thread_yield ();
Packit a6ee4b
            continue;
Packit a6ee4b
          } else {
Packit a6ee4b
            /* This is a real error and means that either the bus is in an
Packit a6ee4b
             * inconsistent state, or the GstPoll is invalid. GstPoll already
Packit a6ee4b
             * prints a critical warning about this, no need to do that again
Packit a6ee4b
             * ourselves */
Packit a6ee4b
            break;
Packit a6ee4b
          }
Packit a6ee4b
        }
Packit a6ee4b
      }
Packit a6ee4b
Packit a6ee4b
      GST_DEBUG_OBJECT (bus, "got message %p, %s from %s, type mask is %u",
Packit a6ee4b
          message, GST_MESSAGE_TYPE_NAME (message),
Packit a6ee4b
          GST_MESSAGE_SRC_NAME (message), (guint) types);
Packit a6ee4b
      if ((GST_MESSAGE_TYPE (message) & types) != 0) {
Packit a6ee4b
        /* Extra check to ensure extended types don't get matched unless
Packit a6ee4b
         * asked for */
Packit a6ee4b
        if ((!GST_MESSAGE_TYPE_IS_EXTENDED (message))
Packit a6ee4b
            || (types & GST_MESSAGE_EXTENDED)) {
Packit a6ee4b
          /* exit the loop, we have a message */
Packit a6ee4b
          goto beach;
Packit a6ee4b
        }
Packit a6ee4b
      }
Packit a6ee4b
Packit a6ee4b
      GST_DEBUG_OBJECT (bus, "discarding message, does not match mask");
Packit a6ee4b
      gst_message_unref (message);
Packit a6ee4b
      message = NULL;
Packit a6ee4b
    }
Packit a6ee4b
Packit a6ee4b
    /* no need to wait, exit loop */
Packit a6ee4b
    if (timeout == 0)
Packit a6ee4b
      break;
Packit a6ee4b
Packit a6ee4b
    else if (timeout != GST_CLOCK_TIME_NONE) {
Packit a6ee4b
      if (first_round) {
Packit a6ee4b
        g_get_current_time (&then;;
Packit a6ee4b
        first_round = FALSE;
Packit a6ee4b
      } else {
Packit a6ee4b
        g_get_current_time (&now;;
Packit a6ee4b
Packit a6ee4b
        elapsed = GST_TIMEVAL_TO_TIME (now) - GST_TIMEVAL_TO_TIME (then);
Packit a6ee4b
Packit a6ee4b
        if (elapsed > timeout)
Packit a6ee4b
          break;
Packit a6ee4b
      }
Packit a6ee4b
    }
Packit a6ee4b
Packit a6ee4b
    /* only here in timeout case */
Packit a6ee4b
    g_assert (bus->priv->poll);
Packit a6ee4b
    g_mutex_unlock (&bus->priv->queue_lock);
Packit a6ee4b
    ret = gst_poll_wait (bus->priv->poll, timeout - elapsed);
Packit a6ee4b
    g_mutex_lock (&bus->priv->queue_lock);
Packit a6ee4b
Packit a6ee4b
    if (ret == 0) {
Packit a6ee4b
      GST_INFO_OBJECT (bus, "timed out, breaking loop");
Packit a6ee4b
      break;
Packit a6ee4b
    } else {
Packit a6ee4b
      GST_INFO_OBJECT (bus, "we got woken up, recheck for message");
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
beach:
Packit a6ee4b
Packit a6ee4b
  g_mutex_unlock (&bus->priv->queue_lock);
Packit a6ee4b
Packit a6ee4b
  return message;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_timed_pop:
Packit a6ee4b
 * @bus: a #GstBus to pop
Packit a6ee4b
 * @timeout: a timeout
Packit a6ee4b
 *
Packit a6ee4b
 * Get a message from the bus, waiting up to the specified timeout.
Packit a6ee4b
 *
Packit a6ee4b
 * If @timeout is 0, this function behaves like gst_bus_pop(). If @timeout is
Packit a6ee4b
 * #GST_CLOCK_TIME_NONE, this function will block forever until a message was
Packit a6ee4b
 * posted on the bus.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full) (nullable): the #GstMessage that is on the
Packit a6ee4b
 *     bus after the specified timeout or %NULL if the bus is empty
Packit a6ee4b
 *     after the timeout expired.  The message is taken from the bus
Packit a6ee4b
 *     and needs to be unreffed with gst_message_unref() after usage.
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 */
Packit a6ee4b
GstMessage *
Packit a6ee4b
gst_bus_timed_pop (GstBus * bus, GstClockTime timeout)
Packit a6ee4b
{
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUS (bus), NULL);
Packit a6ee4b
Packit a6ee4b
  return gst_bus_timed_pop_filtered (bus, timeout, GST_MESSAGE_ANY);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_pop_filtered:
Packit a6ee4b
 * @bus: a #GstBus to pop
Packit a6ee4b
 * @types: message types to take into account
Packit a6ee4b
 *
Packit a6ee4b
 * Get a message matching @type from the bus.  Will discard all messages on
Packit a6ee4b
 * the bus that do not match @type and that have been posted before the first
Packit a6ee4b
 * message that does match @type.  If there is no message matching @type on
Packit a6ee4b
 * the bus, all messages will be discarded. It is not possible to use message
Packit a6ee4b
 * enums beyond #GST_MESSAGE_EXTENDED in the @events mask.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full) (nullable): the next #GstMessage matching
Packit a6ee4b
 *     @type that is on the bus, or %NULL if the bus is empty or there
Packit a6ee4b
 *     is no message matching @type. The message is taken from the bus
Packit a6ee4b
 *     and needs to be unreffed with gst_message_unref() after usage.
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 */
Packit a6ee4b
GstMessage *
Packit a6ee4b
gst_bus_pop_filtered (GstBus * bus, GstMessageType types)
Packit a6ee4b
{
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUS (bus), NULL);
Packit a6ee4b
  g_return_val_if_fail (types != 0, NULL);
Packit a6ee4b
Packit a6ee4b
  return gst_bus_timed_pop_filtered (bus, 0, types);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_pop:
Packit a6ee4b
 * @bus: a #GstBus to pop
Packit a6ee4b
 *
Packit a6ee4b
 * Get a message from the bus.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full) (nullable): the #GstMessage that is on the
Packit a6ee4b
 *     bus, or %NULL if the bus is empty. The message is taken from
Packit a6ee4b
 *     the bus and needs to be unreffed with gst_message_unref() after
Packit a6ee4b
 *     usage.
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 */
Packit a6ee4b
GstMessage *
Packit a6ee4b
gst_bus_pop (GstBus * bus)
Packit a6ee4b
{
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUS (bus), NULL);
Packit a6ee4b
Packit a6ee4b
  return gst_bus_timed_pop_filtered (bus, 0, GST_MESSAGE_ANY);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_peek:
Packit a6ee4b
 * @bus: a #GstBus
Packit a6ee4b
 *
Packit a6ee4b
 * Peek the message on the top of the bus' queue. The message will remain
Packit a6ee4b
 * on the bus' message queue. A reference is returned, and needs to be unreffed
Packit a6ee4b
 * by the caller.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full) (nullable): the #GstMessage that is on the
Packit a6ee4b
 *     bus, or %NULL if the bus is empty.
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 */
Packit a6ee4b
GstMessage *
Packit a6ee4b
gst_bus_peek (GstBus * bus)
Packit a6ee4b
{
Packit a6ee4b
  GstMessage *message;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUS (bus), NULL);
Packit a6ee4b
Packit a6ee4b
  g_mutex_lock (&bus->priv->queue_lock);
Packit a6ee4b
  message = gst_atomic_queue_peek (bus->priv->queue);
Packit a6ee4b
  if (message)
Packit a6ee4b
    gst_message_ref (message);
Packit a6ee4b
  g_mutex_unlock (&bus->priv->queue_lock);
Packit a6ee4b
Packit a6ee4b
  GST_DEBUG_OBJECT (bus, "peek on bus, got message %p", message);
Packit a6ee4b
Packit a6ee4b
  return message;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_set_sync_handler:
Packit a6ee4b
 * @bus: a #GstBus to install the handler on
Packit a6ee4b
 * @func: (allow-none): The handler function to install
Packit a6ee4b
 * @user_data: User data that will be sent to the handler function.
Packit a6ee4b
 * @notify: called when @user_data becomes unused
Packit a6ee4b
 *
Packit a6ee4b
 * Sets the synchronous handler on the bus. The function will be called
Packit a6ee4b
 * every time a new message is posted on the bus. Note that the function
Packit a6ee4b
 * will be called in the same thread context as the posting object. This
Packit a6ee4b
 * function is usually only called by the creator of the bus. Applications
Packit a6ee4b
 * should handle messages asynchronously using the gst_bus watch and poll
Packit a6ee4b
 * functions.
Packit a6ee4b
 *
Packit a6ee4b
 * You cannot replace an existing sync_handler. You can pass %NULL to this
Packit a6ee4b
 * function, which will clear the existing handler.
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_bus_set_sync_handler (GstBus * bus, GstBusSyncHandler func,
Packit a6ee4b
    gpointer user_data, GDestroyNotify notify)
Packit a6ee4b
{
Packit a6ee4b
  GDestroyNotify old_notify;
Packit a6ee4b
Packit a6ee4b
  g_return_if_fail (GST_IS_BUS (bus));
Packit a6ee4b
Packit a6ee4b
  GST_OBJECT_LOCK (bus);
Packit a6ee4b
  /* Assert if the user attempts to replace an existing sync_handler,
Packit a6ee4b
   * other than to clear it */
Packit a6ee4b
  if (func != NULL && bus->priv->sync_handler != NULL)
Packit a6ee4b
    goto no_replace;
Packit a6ee4b
Packit a6ee4b
  if ((old_notify = bus->priv->sync_handler_notify)) {
Packit a6ee4b
    gpointer old_data = bus->priv->sync_handler_data;
Packit a6ee4b
Packit a6ee4b
    bus->priv->sync_handler_data = NULL;
Packit a6ee4b
    bus->priv->sync_handler_notify = NULL;
Packit a6ee4b
    GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
Packit a6ee4b
    old_notify (old_data);
Packit a6ee4b
Packit a6ee4b
    GST_OBJECT_LOCK (bus);
Packit a6ee4b
  }
Packit a6ee4b
  bus->priv->sync_handler = func;
Packit a6ee4b
  bus->priv->sync_handler_data = user_data;
Packit a6ee4b
  bus->priv->sync_handler_notify = notify;
Packit a6ee4b
  GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
Packit a6ee4b
  return;
Packit a6ee4b
Packit a6ee4b
no_replace:
Packit a6ee4b
  {
Packit a6ee4b
    GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
    g_warning ("cannot replace existing sync handler");
Packit a6ee4b
    return;
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_get_pollfd:
Packit a6ee4b
 * @bus: A #GstBus
Packit a6ee4b
 * @fd: (out): A GPollFD to fill
Packit a6ee4b
 *
Packit a6ee4b
 * Gets the file descriptor from the bus which can be used to get notified about
Packit a6ee4b
 * messages being available with functions like g_poll(), and allows integration
Packit a6ee4b
 * into other event loops based on file descriptors.
Packit a6ee4b
 * Whenever a message is available, the POLLIN / %G_IO_IN event is set.
Packit a6ee4b
 *
Packit a6ee4b
 * Warning: NEVER read or write anything to the returned fd but only use it
Packit a6ee4b
 * for getting notifications via g_poll() or similar and then use the normal
Packit a6ee4b
 * GstBus API, e.g. gst_bus_pop().
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.14
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_bus_get_pollfd (GstBus * bus, GPollFD * fd)
Packit a6ee4b
{
Packit a6ee4b
  g_return_if_fail (GST_IS_BUS (bus));
Packit a6ee4b
  g_return_if_fail (bus->priv->poll != NULL);
Packit a6ee4b
Packit a6ee4b
  *fd = bus->priv->pollfd;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/* GSource for the bus
Packit a6ee4b
 */
Packit a6ee4b
typedef struct
Packit a6ee4b
{
Packit a6ee4b
  GSource source;
Packit a6ee4b
  GstBus *bus;
Packit a6ee4b
} GstBusSource;
Packit a6ee4b
Packit a6ee4b
static gboolean
Packit a6ee4b
gst_bus_source_prepare (GSource * source, gint * timeout)
Packit a6ee4b
{
Packit a6ee4b
  *timeout = -1;
Packit a6ee4b
  return FALSE;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static gboolean
Packit a6ee4b
gst_bus_source_check (GSource * source)
Packit a6ee4b
{
Packit a6ee4b
  GstBusSource *bsrc = (GstBusSource *) source;
Packit a6ee4b
Packit a6ee4b
  return bsrc->bus->priv->pollfd.revents & (G_IO_IN | G_IO_HUP | G_IO_ERR);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static gboolean
Packit a6ee4b
gst_bus_source_dispatch (GSource * source, GSourceFunc callback,
Packit a6ee4b
    gpointer user_data)
Packit a6ee4b
{
Packit a6ee4b
  GstBusFunc handler = (GstBusFunc) callback;
Packit a6ee4b
  GstBusSource *bsource = (GstBusSource *) source;
Packit a6ee4b
  GstMessage *message;
Packit a6ee4b
  gboolean keep;
Packit a6ee4b
  GstBus *bus;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (bsource != NULL, FALSE);
Packit a6ee4b
Packit a6ee4b
  bus = bsource->bus;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
Packit a6ee4b
Packit a6ee4b
  message = gst_bus_pop (bus);
Packit a6ee4b
Packit a6ee4b
  /* The message queue might be empty if some other thread or callback set
Packit a6ee4b
   * the bus to flushing between check/prepare and dispatch */
Packit a6ee4b
  if (G_UNLIKELY (message == NULL))
Packit a6ee4b
    return TRUE;
Packit a6ee4b
Packit a6ee4b
  if (!handler)
Packit a6ee4b
    goto no_handler;
Packit a6ee4b
Packit a6ee4b
  GST_DEBUG_OBJECT (bus, "source %p calling dispatch with %" GST_PTR_FORMAT,
Packit a6ee4b
      source, message);
Packit a6ee4b
Packit a6ee4b
  keep = handler (bus, message, user_data);
Packit a6ee4b
  gst_message_unref (message);
Packit a6ee4b
Packit a6ee4b
  GST_DEBUG_OBJECT (bus, "source %p handler returns %d", source, keep);
Packit a6ee4b
Packit a6ee4b
  return keep;
Packit a6ee4b
Packit a6ee4b
no_handler:
Packit a6ee4b
  {
Packit a6ee4b
    g_warning ("GstBus watch dispatched without callback\n"
Packit a6ee4b
        "You must call g_source_set_callback().");
Packit a6ee4b
    gst_message_unref (message);
Packit a6ee4b
    return FALSE;
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
gst_bus_source_finalize (GSource * source)
Packit a6ee4b
{
Packit a6ee4b
  GstBusSource *bsource = (GstBusSource *) source;
Packit a6ee4b
  GstBus *bus;
Packit a6ee4b
Packit a6ee4b
  bus = bsource->bus;
Packit a6ee4b
Packit a6ee4b
  GST_DEBUG_OBJECT (bus, "finalize source %p", source);
Packit a6ee4b
Packit a6ee4b
  GST_OBJECT_LOCK (bus);
Packit a6ee4b
  if (bus->priv->signal_watch == source)
Packit a6ee4b
    bus->priv->signal_watch = NULL;
Packit a6ee4b
  GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
Packit a6ee4b
  gst_object_unref (bsource->bus);
Packit a6ee4b
  bsource->bus = NULL;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static GSourceFuncs gst_bus_source_funcs = {
Packit a6ee4b
  gst_bus_source_prepare,
Packit a6ee4b
  gst_bus_source_check,
Packit a6ee4b
  gst_bus_source_dispatch,
Packit a6ee4b
  gst_bus_source_finalize
Packit a6ee4b
};
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_create_watch:
Packit a6ee4b
 * @bus: a #GstBus to create the watch for
Packit a6ee4b
 *
Packit a6ee4b
 * Create watch for this bus. The GSource will be dispatched whenever
Packit a6ee4b
 * a message is on the bus. After the GSource is dispatched, the
Packit a6ee4b
 * message is popped off the bus and unreffed.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full) (nullable): a #GSource that can be added to a mainloop.
Packit a6ee4b
 */
Packit a6ee4b
GSource *
Packit a6ee4b
gst_bus_create_watch (GstBus * bus)
Packit a6ee4b
{
Packit a6ee4b
  GstBusSource *source;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUS (bus), NULL);
Packit a6ee4b
  g_return_val_if_fail (bus->priv->poll != NULL, NULL);
Packit a6ee4b
Packit a6ee4b
  source = (GstBusSource *) g_source_new (&gst_bus_source_funcs,
Packit a6ee4b
      sizeof (GstBusSource));
Packit a6ee4b
Packit a6ee4b
  g_source_set_name ((GSource *) source, "GStreamer message bus watch");
Packit a6ee4b
Packit a6ee4b
  source->bus = gst_object_ref (bus);
Packit a6ee4b
  g_source_add_poll ((GSource *) source, &bus->priv->pollfd);
Packit a6ee4b
Packit a6ee4b
  return (GSource *) source;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/* must be called with the bus OBJECT LOCK */
Packit a6ee4b
static guint
Packit a6ee4b
gst_bus_add_watch_full_unlocked (GstBus * bus, gint priority,
Packit a6ee4b
    GstBusFunc func, gpointer user_data, GDestroyNotify notify)
Packit a6ee4b
{
Packit a6ee4b
  GMainContext *ctx;
Packit a6ee4b
  guint id;
Packit a6ee4b
  GSource *source;
Packit a6ee4b
Packit a6ee4b
  if (bus->priv->signal_watch) {
Packit a6ee4b
    GST_ERROR_OBJECT (bus,
Packit a6ee4b
        "Tried to add new watch while one was already there");
Packit a6ee4b
    return 0;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  source = gst_bus_create_watch (bus);
Packit a6ee4b
  if (!source) {
Packit a6ee4b
    g_critical ("Creating bus watch failed");
Packit a6ee4b
    return 0;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (priority != G_PRIORITY_DEFAULT)
Packit a6ee4b
    g_source_set_priority (source, priority);
Packit a6ee4b
Packit a6ee4b
  g_source_set_callback (source, (GSourceFunc) func, user_data, notify);
Packit a6ee4b
Packit a6ee4b
  ctx = g_main_context_get_thread_default ();
Packit a6ee4b
  id = g_source_attach (source, ctx);
Packit a6ee4b
  g_source_unref (source);
Packit a6ee4b
Packit a6ee4b
  if (id) {
Packit a6ee4b
    bus->priv->signal_watch = source;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  GST_DEBUG_OBJECT (bus, "New source %p with id %u", source, id);
Packit a6ee4b
  return id;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_add_watch_full: (rename-to gst_bus_add_watch)
Packit a6ee4b
 * @bus: a #GstBus to create the watch for.
Packit a6ee4b
 * @priority: The priority of the watch.
Packit a6ee4b
 * @func: A function to call when a message is received.
Packit a6ee4b
 * @user_data: user data passed to @func.
Packit a6ee4b
 * @notify: the function to call when the source is removed.
Packit a6ee4b
 *
Packit a6ee4b
 * Adds a bus watch to the default main context with the given @priority (e.g.
Packit a6ee4b
 * %G_PRIORITY_DEFAULT). It is also possible to use a non-default  main
Packit a6ee4b
 * context set up using g_main_context_push_thread_default() (before
Packit a6ee4b
 * one had to create a bus watch source and attach it to the desired main
Packit a6ee4b
 * context 'manually').
Packit a6ee4b
 *
Packit a6ee4b
 * This function is used to receive asynchronous messages in the main loop.
Packit a6ee4b
 * There can only be a single bus watch per bus, you must remove it before you
Packit a6ee4b
 * can set a new one.
Packit a6ee4b
 *
Packit a6ee4b
 * The bus watch will only work if a GLib main loop is being run.
Packit a6ee4b
 *
Packit a6ee4b
 * When @func is called, the message belongs to the caller; if you want to
Packit a6ee4b
 * keep a copy of it, call gst_message_ref() before leaving @func.
Packit a6ee4b
 *
Packit a6ee4b
 * The watch can be removed using gst_bus_remove_watch() or by returning %FALSE
Packit a6ee4b
 * from @func. If the watch was added to the default main context it is also
Packit a6ee4b
 * possible to remove the watch using g_source_remove().
Packit a6ee4b
 *
Packit a6ee4b
 * The bus watch will take its own reference to the @bus, so it is safe to unref
Packit a6ee4b
 * @bus using gst_object_unref() after setting the bus watch.
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: The event source id or 0 if @bus already got an event source.
Packit a6ee4b
 */
Packit a6ee4b
guint
Packit a6ee4b
gst_bus_add_watch_full (GstBus * bus, gint priority,
Packit a6ee4b
    GstBusFunc func, gpointer user_data, GDestroyNotify notify)
Packit a6ee4b
{
Packit a6ee4b
  guint id;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUS (bus), 0);
Packit a6ee4b
Packit a6ee4b
  GST_OBJECT_LOCK (bus);
Packit a6ee4b
  id = gst_bus_add_watch_full_unlocked (bus, priority, func, user_data, notify);
Packit a6ee4b
  GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
Packit a6ee4b
  return id;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_add_watch: (skip)
Packit a6ee4b
 * @bus: a #GstBus to create the watch for
Packit a6ee4b
 * @func: A function to call when a message is received.
Packit a6ee4b
 * @user_data: user data passed to @func.
Packit a6ee4b
 *
Packit a6ee4b
 * Adds a bus watch to the default main context with the default priority
Packit a6ee4b
 * (%G_PRIORITY_DEFAULT). It is also possible to use a non-default main
Packit a6ee4b
 * context set up using g_main_context_push_thread_default() (before
Packit a6ee4b
 * one had to create a bus watch source and attach it to the desired main
Packit a6ee4b
 * context 'manually').
Packit a6ee4b
 *
Packit a6ee4b
 * This function is used to receive asynchronous messages in the main loop.
Packit a6ee4b
 * There can only be a single bus watch per bus, you must remove it before you
Packit a6ee4b
 * can set a new one.
Packit a6ee4b
 *
Packit a6ee4b
 * The bus watch will only work if a GLib main loop is being run.
Packit a6ee4b
 *
Packit a6ee4b
 * The watch can be removed using gst_bus_remove_watch() or by returning %FALSE
Packit a6ee4b
 * from @func. If the watch was added to the default main context it is also
Packit a6ee4b
 * possible to remove the watch using g_source_remove().
Packit a6ee4b
 *
Packit a6ee4b
 * The bus watch will take its own reference to the @bus, so it is safe to unref
Packit a6ee4b
 * @bus using gst_object_unref() after setting the bus watch.
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: The event source id or 0 if @bus already got an event source.
Packit a6ee4b
 */
Packit a6ee4b
guint
Packit a6ee4b
gst_bus_add_watch (GstBus * bus, GstBusFunc func, gpointer user_data)
Packit a6ee4b
{
Packit a6ee4b
  return gst_bus_add_watch_full (bus, G_PRIORITY_DEFAULT, func,
Packit a6ee4b
      user_data, NULL);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_remove_watch:
Packit a6ee4b
 * @bus: a #GstBus to remove the watch from.
Packit a6ee4b
 *
Packit a6ee4b
 * Removes an installed bus watch from @bus.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE on success or %FALSE if @bus has no event source.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.6
Packit a6ee4b
 *
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_bus_remove_watch (GstBus * bus)
Packit a6ee4b
{
Packit a6ee4b
  GSource *source;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
Packit a6ee4b
Packit a6ee4b
  GST_OBJECT_LOCK (bus);
Packit a6ee4b
Packit a6ee4b
  if (bus->priv->signal_watch == NULL) {
Packit a6ee4b
    GST_ERROR_OBJECT (bus, "no bus watch was present");
Packit a6ee4b
    goto error;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  if (bus->priv->num_signal_watchers > 0) {
Packit a6ee4b
    GST_ERROR_OBJECT (bus,
Packit a6ee4b
        "trying to remove signal watch with gst_bus_remove_watch()");
Packit a6ee4b
    goto error;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  source =
Packit a6ee4b
      bus->priv->signal_watch ? g_source_ref (bus->priv->signal_watch) : NULL;
Packit a6ee4b
Packit a6ee4b
  GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
Packit a6ee4b
  if (source) {
Packit a6ee4b
    g_source_destroy (source);
Packit a6ee4b
    g_source_unref (source);
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  return TRUE;
Packit a6ee4b
Packit a6ee4b
error:
Packit a6ee4b
  GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
Packit a6ee4b
  return FALSE;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
typedef struct
Packit a6ee4b
{
Packit a6ee4b
  GMainLoop *loop;
Packit a6ee4b
  guint timeout_id;
Packit a6ee4b
  gboolean source_running;
Packit a6ee4b
  GstMessageType events;
Packit a6ee4b
  GstMessage *message;
Packit a6ee4b
} GstBusPollData;
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
poll_func (GstBus * bus, GstMessage * message, GstBusPollData * poll_data)
Packit a6ee4b
{
Packit a6ee4b
  GstMessageType type;
Packit a6ee4b
Packit a6ee4b
  if (!g_main_loop_is_running (poll_data->loop)) {
Packit a6ee4b
    GST_DEBUG ("mainloop %p not running", poll_data->loop);
Packit a6ee4b
    return;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  type = GST_MESSAGE_TYPE (message);
Packit a6ee4b
Packit a6ee4b
  if (type & poll_data->events) {
Packit a6ee4b
    g_assert (poll_data->message == NULL);
Packit a6ee4b
    /* keep ref to message */
Packit a6ee4b
    poll_data->message = gst_message_ref (message);
Packit a6ee4b
    GST_DEBUG ("mainloop %p quit", poll_data->loop);
Packit a6ee4b
    g_main_loop_quit (poll_data->loop);
Packit a6ee4b
  } else {
Packit a6ee4b
    GST_DEBUG ("type %08x does not match %08x", type, poll_data->events);
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static gboolean
Packit a6ee4b
poll_timeout (GstBusPollData * poll_data)
Packit a6ee4b
{
Packit a6ee4b
  GST_DEBUG ("mainloop %p quit", poll_data->loop);
Packit a6ee4b
  g_main_loop_quit (poll_data->loop);
Packit a6ee4b
Packit a6ee4b
  /* we don't remove the GSource as this would free our poll_data,
Packit a6ee4b
   * which we still need */
Packit a6ee4b
  return TRUE;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
poll_destroy (GstBusPollData * poll_data, gpointer unused)
Packit a6ee4b
{
Packit a6ee4b
  poll_data->source_running = FALSE;
Packit a6ee4b
  if (!poll_data->timeout_id) {
Packit a6ee4b
    g_main_loop_unref (poll_data->loop);
Packit a6ee4b
    g_slice_free (GstBusPollData, poll_data);
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static void
Packit a6ee4b
poll_destroy_timeout (GstBusPollData * poll_data)
Packit a6ee4b
{
Packit a6ee4b
  poll_data->timeout_id = 0;
Packit a6ee4b
  if (!poll_data->source_running) {
Packit a6ee4b
    g_main_loop_unref (poll_data->loop);
Packit a6ee4b
    g_slice_free (GstBusPollData, poll_data);
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_poll:
Packit a6ee4b
 * @bus: a #GstBus
Packit a6ee4b
 * @events: a mask of #GstMessageType, representing the set of message types to
Packit a6ee4b
 * poll for (note special handling of extended message types below)
Packit a6ee4b
 * @timeout: the poll timeout, as a #GstClockTime, or #GST_CLOCK_TIME_NONE to poll
Packit a6ee4b
 * indefinitely.
Packit a6ee4b
 *
Packit a6ee4b
 * Poll the bus for messages. Will block while waiting for messages to come.
Packit a6ee4b
 * You can specify a maximum time to poll with the @timeout parameter. If
Packit a6ee4b
 * @timeout is negative, this function will block indefinitely.
Packit a6ee4b
 *
Packit a6ee4b
 * All messages not in @events will be popped off the bus and will be ignored.
Packit a6ee4b
 * It is not possible to use message enums beyond #GST_MESSAGE_EXTENDED in the
Packit a6ee4b
 * @events mask
Packit a6ee4b
 *
Packit a6ee4b
 * Because poll is implemented using the "message" signal enabled by
Packit a6ee4b
 * gst_bus_add_signal_watch(), calling gst_bus_poll() will cause the "message"
Packit a6ee4b
 * signal to be emitted for every message that poll sees. Thus a "message"
Packit a6ee4b
 * signal handler will see the same messages that this function sees -- neither
Packit a6ee4b
 * will steal messages from the other.
Packit a6ee4b
 *
Packit a6ee4b
 * This function will run a main loop from the default main context when
Packit a6ee4b
 * polling.
Packit a6ee4b
 *
Packit a6ee4b
 * You should never use this function, since it is pure evil. This is
Packit a6ee4b
 * especially true for GUI applications based on Gtk+ or Qt, but also for any
Packit a6ee4b
 * other non-trivial application that uses the GLib main loop. As this function
Packit a6ee4b
 * runs a GLib main loop, any callback attached to the default GLib main
Packit a6ee4b
 * context may be invoked. This could be timeouts, GUI events, I/O events etc.;
Packit a6ee4b
 * even if gst_bus_poll() is called with a 0 timeout. Any of these callbacks
Packit a6ee4b
 * may do things you do not expect, e.g. destroy the main application window or
Packit a6ee4b
 * some other resource; change other application state; display a dialog and
Packit a6ee4b
 * run another main loop until the user clicks it away. In short, using this
Packit a6ee4b
 * function may add a lot of complexity to your code through unexpected
Packit a6ee4b
 * re-entrancy and unexpected changes to your application's state.
Packit a6ee4b
 *
Packit a6ee4b
 * For 0 timeouts use gst_bus_pop_filtered() instead of this function; for
Packit a6ee4b
 * other short timeouts use gst_bus_timed_pop_filtered(); everything else is
Packit a6ee4b
 * better handled by setting up an asynchronous bus watch and doing things
Packit a6ee4b
 * from there.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full) (nullable): the message that was received,
Packit a6ee4b
 *     or %NULL if the poll timed out. The message is taken from the
Packit a6ee4b
 *     bus and needs to be unreffed with gst_message_unref() after
Packit a6ee4b
 *     usage.
Packit a6ee4b
 */
Packit a6ee4b
GstMessage *
Packit a6ee4b
gst_bus_poll (GstBus * bus, GstMessageType events, GstClockTime timeout)
Packit a6ee4b
{
Packit a6ee4b
  GstBusPollData *poll_data;
Packit a6ee4b
  GstMessage *ret;
Packit a6ee4b
  gulong id;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUS (bus), NULL);
Packit a6ee4b
Packit a6ee4b
  poll_data = g_slice_new (GstBusPollData);
Packit a6ee4b
  poll_data->source_running = TRUE;
Packit a6ee4b
  poll_data->loop = g_main_loop_new (NULL, FALSE);
Packit a6ee4b
  poll_data->events = events;
Packit a6ee4b
  poll_data->message = NULL;
Packit a6ee4b
Packit a6ee4b
  if (timeout != GST_CLOCK_TIME_NONE)
Packit a6ee4b
    poll_data->timeout_id = g_timeout_add_full (G_PRIORITY_DEFAULT_IDLE,
Packit a6ee4b
        timeout / GST_MSECOND, (GSourceFunc) poll_timeout, poll_data,
Packit a6ee4b
        (GDestroyNotify) poll_destroy_timeout);
Packit a6ee4b
  else
Packit a6ee4b
    poll_data->timeout_id = 0;
Packit a6ee4b
Packit a6ee4b
  id = g_signal_connect_data (bus, "message", G_CALLBACK (poll_func), poll_data,
Packit a6ee4b
      (GClosureNotify) poll_destroy, 0);
Packit a6ee4b
Packit a6ee4b
  /* these can be nested, so it's ok */
Packit a6ee4b
  gst_bus_add_signal_watch (bus);
Packit a6ee4b
Packit a6ee4b
  GST_DEBUG ("running mainloop %p", poll_data->loop);
Packit a6ee4b
  g_main_loop_run (poll_data->loop);
Packit a6ee4b
  GST_DEBUG ("mainloop stopped %p", poll_data->loop);
Packit a6ee4b
Packit a6ee4b
  gst_bus_remove_signal_watch (bus);
Packit a6ee4b
Packit a6ee4b
  /* holds a ref */
Packit a6ee4b
  ret = poll_data->message;
Packit a6ee4b
Packit a6ee4b
  if (poll_data->timeout_id)
Packit a6ee4b
    g_source_remove (poll_data->timeout_id);
Packit a6ee4b
Packit a6ee4b
  /* poll_data will be freed now */
Packit a6ee4b
  g_signal_handler_disconnect (bus, id);
Packit a6ee4b
Packit a6ee4b
  GST_DEBUG_OBJECT (bus, "finished poll with message %p", ret);
Packit a6ee4b
Packit a6ee4b
  return ret;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_async_signal_func:
Packit a6ee4b
 * @bus: a #GstBus
Packit a6ee4b
 * @message: the #GstMessage received
Packit a6ee4b
 * @data: user data
Packit a6ee4b
 *
Packit a6ee4b
 * A helper #GstBusFunc that can be used to convert all asynchronous messages
Packit a6ee4b
 * into signals.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_bus_async_signal_func (GstBus * bus, GstMessage * message, gpointer data)
Packit a6ee4b
{
Packit a6ee4b
  GQuark detail = 0;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUS (bus), TRUE);
Packit a6ee4b
  g_return_val_if_fail (message != NULL, TRUE);
Packit a6ee4b
Packit a6ee4b
  detail = gst_message_type_to_quark (GST_MESSAGE_TYPE (message));
Packit a6ee4b
Packit a6ee4b
  g_signal_emit (bus, gst_bus_signals[ASYNC_MESSAGE], detail, message);
Packit a6ee4b
Packit a6ee4b
  /* we never remove this source based on signal emission return values */
Packit a6ee4b
  return TRUE;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_sync_signal_handler:
Packit a6ee4b
 * @bus: a #GstBus
Packit a6ee4b
 * @message: the #GstMessage received
Packit a6ee4b
 * @data: user data
Packit a6ee4b
 *
Packit a6ee4b
 * A helper GstBusSyncHandler that can be used to convert all synchronous
Packit a6ee4b
 * messages into signals.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: GST_BUS_PASS
Packit a6ee4b
 */
Packit a6ee4b
GstBusSyncReply
Packit a6ee4b
gst_bus_sync_signal_handler (GstBus * bus, GstMessage * message, gpointer data)
Packit a6ee4b
{
Packit a6ee4b
  GQuark detail = 0;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (GST_IS_BUS (bus), GST_BUS_DROP);
Packit a6ee4b
  g_return_val_if_fail (message != NULL, GST_BUS_DROP);
Packit a6ee4b
Packit a6ee4b
  detail = gst_message_type_to_quark (GST_MESSAGE_TYPE (message));
Packit a6ee4b
Packit a6ee4b
  g_signal_emit (bus, gst_bus_signals[SYNC_MESSAGE], detail, message);
Packit a6ee4b
Packit a6ee4b
  return GST_BUS_PASS;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_enable_sync_message_emission:
Packit a6ee4b
 * @bus: a #GstBus on which you want to receive the "sync-message" signal
Packit a6ee4b
 *
Packit a6ee4b
 * Instructs GStreamer to emit the "sync-message" signal after running the bus's
Packit a6ee4b
 * sync handler. This function is here so that code can ensure that they can
Packit a6ee4b
 * synchronously receive messages without having to affect what the bin's sync
Packit a6ee4b
 * handler is.
Packit a6ee4b
 *
Packit a6ee4b
 * This function may be called multiple times. To clean up, the caller is
Packit a6ee4b
 * responsible for calling gst_bus_disable_sync_message_emission() as many times
Packit a6ee4b
 * as this function is called.
Packit a6ee4b
 *
Packit a6ee4b
 * While this function looks similar to gst_bus_add_signal_watch(), it is not
Packit a6ee4b
 * exactly the same -- this function enables <emphasis>synchronous</emphasis> emission of
Packit a6ee4b
 * signals when messages arrive; gst_bus_add_signal_watch() adds an idle callback
Packit a6ee4b
 * to pop messages off the bus <emphasis>asynchronously</emphasis>. The sync-message signal
Packit a6ee4b
 * comes from the thread of whatever object posted the message; the "message"
Packit a6ee4b
 * signal is marshalled to the main thread via the main loop.
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_bus_enable_sync_message_emission (GstBus * bus)
Packit a6ee4b
{
Packit a6ee4b
  g_return_if_fail (GST_IS_BUS (bus));
Packit a6ee4b
Packit a6ee4b
  GST_OBJECT_LOCK (bus);
Packit a6ee4b
  bus->priv->num_sync_message_emitters++;
Packit a6ee4b
  GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_disable_sync_message_emission:
Packit a6ee4b
 * @bus: a #GstBus on which you previously called
Packit a6ee4b
 * gst_bus_enable_sync_message_emission()
Packit a6ee4b
 *
Packit a6ee4b
 * Instructs GStreamer to stop emitting the "sync-message" signal for this bus.
Packit a6ee4b
 * See gst_bus_enable_sync_message_emission() for more information.
Packit a6ee4b
 *
Packit a6ee4b
 * In the event that multiple pieces of code have called
Packit a6ee4b
 * gst_bus_enable_sync_message_emission(), the sync-message emissions will only
Packit a6ee4b
 * be stopped after all calls to gst_bus_enable_sync_message_emission() were
Packit a6ee4b
 * "cancelled" by calling this function. In this way the semantics are exactly
Packit a6ee4b
 * the same as gst_object_ref() that which calls enable should also call
Packit a6ee4b
 * disable.
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_bus_disable_sync_message_emission (GstBus * bus)
Packit a6ee4b
{
Packit a6ee4b
  g_return_if_fail (GST_IS_BUS (bus));
Packit a6ee4b
  g_return_if_fail (bus->priv->num_sync_message_emitters > 0);
Packit a6ee4b
Packit a6ee4b
  GST_OBJECT_LOCK (bus);
Packit a6ee4b
  bus->priv->num_sync_message_emitters--;
Packit a6ee4b
  GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_add_signal_watch_full:
Packit a6ee4b
 * @bus: a #GstBus on which you want to receive the "message" signal
Packit a6ee4b
 * @priority: The priority of the watch.
Packit a6ee4b
 *
Packit a6ee4b
 * Adds a bus signal watch to the default main context with the given @priority
Packit a6ee4b
 * (e.g. %G_PRIORITY_DEFAULT). It is also possible to use a non-default main
Packit a6ee4b
 * context set up using g_main_context_push_thread_default()
Packit a6ee4b
 * (before one had to create a bus watch source and attach it to the desired
Packit a6ee4b
 * main context 'manually').
Packit a6ee4b
 *
Packit a6ee4b
 * After calling this statement, the bus will emit the "message" signal for each
Packit a6ee4b
 * message posted on the bus when the main loop is running.
Packit a6ee4b
 *
Packit a6ee4b
 * This function may be called multiple times. To clean up, the caller is
Packit a6ee4b
 * responsible for calling gst_bus_remove_signal_watch() as many times as this
Packit a6ee4b
 * function is called.
Packit a6ee4b
 *
Packit a6ee4b
 * There can only be a single bus watch per bus, you must remove any signal
Packit a6ee4b
 * watch before you can set another type of watch.
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_bus_add_signal_watch_full (GstBus * bus, gint priority)
Packit a6ee4b
{
Packit a6ee4b
  g_return_if_fail (GST_IS_BUS (bus));
Packit a6ee4b
Packit a6ee4b
  /* I know the callees don't take this lock, so go ahead and abuse it */
Packit a6ee4b
  GST_OBJECT_LOCK (bus);
Packit a6ee4b
Packit a6ee4b
  if (bus->priv->num_signal_watchers > 0)
Packit a6ee4b
    goto done;
Packit a6ee4b
Packit a6ee4b
  /* this should not fail because the counter above takes care of it */
Packit a6ee4b
  g_assert (!bus->priv->signal_watch);
Packit a6ee4b
Packit a6ee4b
  gst_bus_add_watch_full_unlocked (bus, priority, gst_bus_async_signal_func,
Packit a6ee4b
      NULL, NULL);
Packit a6ee4b
Packit a6ee4b
  if (G_UNLIKELY (!bus->priv->signal_watch))
Packit a6ee4b
    goto add_failed;
Packit a6ee4b
Packit a6ee4b
done:
Packit a6ee4b
Packit a6ee4b
  bus->priv->num_signal_watchers++;
Packit a6ee4b
Packit a6ee4b
  GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
  return;
Packit a6ee4b
Packit a6ee4b
  /* ERRORS */
Packit a6ee4b
add_failed:
Packit a6ee4b
  {
Packit a6ee4b
    g_critical ("Could not add signal watch to bus %s", GST_OBJECT_NAME (bus));
Packit a6ee4b
    GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
    return;
Packit a6ee4b
  }
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_add_signal_watch:
Packit a6ee4b
 * @bus: a #GstBus on which you want to receive the "message" signal
Packit a6ee4b
 *
Packit a6ee4b
 * Adds a bus signal watch to the default main context with the default priority
Packit a6ee4b
 * (%G_PRIORITY_DEFAULT). It is also possible to use a non-default
Packit a6ee4b
 * main context set up using g_main_context_push_thread_default() (before
Packit a6ee4b
 * one had to create a bus watch source and attach it to the desired main
Packit a6ee4b
 * context 'manually').
Packit a6ee4b
 *
Packit a6ee4b
 * After calling this statement, the bus will emit the "message" signal for each
Packit a6ee4b
 * message posted on the bus.
Packit a6ee4b
 *
Packit a6ee4b
 * This function may be called multiple times. To clean up, the caller is
Packit a6ee4b
 * responsible for calling gst_bus_remove_signal_watch() as many times as this
Packit a6ee4b
 * function is called.
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_bus_add_signal_watch (GstBus * bus)
Packit a6ee4b
{
Packit a6ee4b
  gst_bus_add_signal_watch_full (bus, G_PRIORITY_DEFAULT);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_bus_remove_signal_watch:
Packit a6ee4b
 * @bus: a #GstBus you previously added a signal watch to
Packit a6ee4b
 *
Packit a6ee4b
 * Removes a signal watch previously added with gst_bus_add_signal_watch().
Packit a6ee4b
 *
Packit a6ee4b
 * MT safe.
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_bus_remove_signal_watch (GstBus * bus)
Packit a6ee4b
{
Packit a6ee4b
  GSource *source = NULL;
Packit a6ee4b
Packit a6ee4b
  g_return_if_fail (GST_IS_BUS (bus));
Packit a6ee4b
Packit a6ee4b
  /* I know the callees don't take this lock, so go ahead and abuse it */
Packit a6ee4b
  GST_OBJECT_LOCK (bus);
Packit a6ee4b
Packit a6ee4b
  if (bus->priv->num_signal_watchers == 0)
Packit a6ee4b
    goto error;
Packit a6ee4b
Packit a6ee4b
  bus->priv->num_signal_watchers--;
Packit a6ee4b
Packit a6ee4b
  if (bus->priv->num_signal_watchers > 0)
Packit a6ee4b
    goto done;
Packit a6ee4b
Packit a6ee4b
  GST_DEBUG_OBJECT (bus, "removing signal watch %u",
Packit a6ee4b
      g_source_get_id (bus->priv->signal_watch));
Packit a6ee4b
Packit a6ee4b
  source =
Packit a6ee4b
      bus->priv->signal_watch ? g_source_ref (bus->priv->signal_watch) : NULL;
Packit a6ee4b
Packit a6ee4b
done:
Packit a6ee4b
  GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
Packit a6ee4b
  if (source) {
Packit a6ee4b
    g_source_destroy (source);
Packit a6ee4b
    g_source_unref (source);
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  return;
Packit a6ee4b
Packit a6ee4b
  /* ERRORS */
Packit a6ee4b
error:
Packit a6ee4b
  {
Packit a6ee4b
    g_critical ("Bus %s has no signal watches attached", GST_OBJECT_NAME (bus));
Packit a6ee4b
    GST_OBJECT_UNLOCK (bus);
Packit a6ee4b
    return;
Packit a6ee4b
  }
Packit a6ee4b
}