## GstBus GstBus — Asynchronous message bus subsystem |

## Synopsis

    #include <gst/gst.h>

    struct              GstBus;
    enum                GstBusFlags;
    enum                GstBusSyncReply;
    gboolean            (*GstBusFunc)                       (GstBus *bus,
                                                             GstMessage *message,
                                                             gpointer data);
    GstBusSyncReply     (*GstBusSyncHandler)                (GstBus *bus,
                                                             GstMessage *message,
                                                             gpointer data);
    GstBus *            gst_bus_new                         (void);
    gboolean            gst_bus_post                        (GstBus *bus,
                                                             GstMessage *message);
    gboolean            gst_bus_have_pending                (GstBus *bus);
    GstMessage *        gst_bus_peek                        (GstBus *bus);
    GstMessage *        gst_bus_pop                         (GstBus *bus);
    GstMessage *        gst_bus_pop_filtered                (GstBus *bus,
                                                             GstMessageType types);
    GstMessage *        gst_bus_timed_pop                   (GstBus *bus,
                                                             GstClockTime timeout);
    GstMessage *        gst_bus_timed_pop_filtered          (GstBus *bus,
                                                             GstClockTime timeout,
                                                             GstMessageType types);
    void                gst_bus_set_flushing                (GstBus *bus,
                                                             gboolean flushing);
    void                gst_bus_set_sync_handler            (GstBus *bus,
                                                             GstBusSyncHandler func,
                                                             gpointer data);
    GstBusSyncReply     gst_bus_sync_signal_handler         (GstBus *bus,
                                                             GstMessage *message,
                                                             gpointer data);
    GSource *           gst_bus_create_watch                (GstBus *bus);
    guint               gst_bus_add_watch_full              (GstBus *bus,
                                                             gint priority,
                                                             GstBusFunc func,
                                                             gpointer user_data,
                                                             GDestroyNotify notify);
    guint               gst_bus_add_watch                   (GstBus *bus,
                                                             GstBusFunc func,
                                                             gpointer user_data);
    void                gst_bus_disable_sync_message_emission
                                                            (GstBus *bus);
    void                gst_bus_enable_sync_message_emission
                                                            (GstBus *bus);
    gboolean            gst_bus_async_signal_func           (GstBus *bus,
                                                             GstMessage *message,
                                                             gpointer data);
    void                gst_bus_add_signal_watch            (GstBus *bus);
    void                gst_bus_add_signal_watch_full       (GstBus *bus,
                                                             gint priority);
    void                gst_bus_remove_signal_watch         (GstBus *bus);
    GstMessage *        gst_bus_poll                        (GstBus *bus,
                                                             GstMessageType events,
                                                             GstClockTimeDiff timeout);

## Object Hierarchy

      GObject
       +----GstObject
             +----GstBus

## Signals

      "message"                                        : Has Details
      "sync-message"                                   : Has Details

## Description

The GstBus is an object responsible for delivering GstMessage packets in a first-in first-out way from the streaming threads (see GstTask) to the application.

Since the application typically only wants to deal with delivery of these messages from one thread, the GstBus will marshall the messages between different threads. This is important since the actual streaming of media is done in another thread than the application.

The GstBus provides support for GSource based notifications. This makes it possible to handle the delivery in the glib mainloop.

The GSource callback function `gst_bus_async_signal_func()` can be used to convert all bus messages into signal emissions.

A message is posted on the bus with the `gst_bus_post()` method. With the `gst_bus_peek()` and `gst_bus_pop()` methods one can look at or retrieve a previously posted message.

The bus can be polled with the `gst_bus_poll()` method. This methods blocks up to the specified timeout value until one of the specified messages types is posted on the bus. The application can then `gst_bus_pop()` the messages from the bus to handle them. Alternatively the application can register an asynchronous bus function using `gst_bus_add_watch_full()` or `gst_bus_add_watch()`. This function will install a GSource in the default glib main loop and will deliver messages a short while after they have been posted. Note that the main loop should be running for the asynchronous callbacks.

It is also possible to get messages from the bus without any thread marshalling with the `gst_bus_set_sync_handler()` method. This makes it possible to react to a message in the same thread that posted the message on the bus. This should only be used if the application is able to deal with messages from different threads.

Every GstPipeline has one bus.

Note that a GstPipeline will set its bus into flushing state when changing from READY to NULL state.

Last reviewed on 2006-03-12 (0.10.5)

## Details

### struct GstBus

    struct GstBus;

The opaque GstBus data structure.

### enum GstBusFlags

    typedef enum {
      GST_BUS_FLUSHING      = (GST_OBJECT_FLAG_LAST << 0),
      /* padding */
      GST_BUS_FLAG_LAST     = (GST_OBJECT_FLAG_LAST << 1)
    } GstBusFlags;

The standard flags that a bus may have.

`GST_BUS_FLUSHING` | The bus is currently dropping all messages
`GST_BUS_FLAG_LAST` | offset to define more flags

### enum GstBusSyncReply

    typedef enum {
      GST_BUS_DROP = 0,
      GST_BUS_PASS = 1,
      GST_BUS_ASYNC = 2
    } GstBusSyncReply;

The result values for a GstBusSyncHandler.

`GST_BUS_DROP` | drop the message
`GST_BUS_PASS` | pass the message to the async queue
`GST_BUS_ASYNC` | pass message to async queue, continue if message is handled

### GstBusFunc ()

    gboolean            (*GstBusFunc)                       (GstBus *bus,
                                                             GstMessage *message,
                                                             gpointer data);

Specifies the type of function passed to `gst_bus_add_watch()` or `gst_bus_add_watch_full()`, which is called from the mainloop when a message is available on the bus.

The message passed to the function will be unreffed after execution of this function so it should not be freed in the function.

Note that this function is used as a GSourceFunc which means that returning FALSE will remove the GSource from the mainloop.

`bus` : | the GstBus that sent the message
`message` : | the GstMessage
`data` : | user data that has been given, when registering the handler
Returns : | `FALSE` if the event source should be removed.

### GstBusSyncHandler ()

    GstBusSyncReply     (*GstBusSyncHandler)                (GstBus *bus,
                                                             GstMessage *message,
                                                             gpointer data);

Handler will be invoked synchronously, when a new message has been injected into the bus. This function is mostly used internally. Only one sync handler can be attached to a given bus.

If the handler returns GST_BUS_DROP, it should unref the message, else the message should not be unreffed by the sync handler.

`bus` : | the GstBus that sent the message
`message` : | the GstMessage
`data` : | user data that has been given, when registering the handler
Returns : | GstBusSyncReply stating what to do with the message

### gst_bus_new ()

    GstBus *            gst_bus_new                         (void);

Creates a new GstBus instance.

Returns : | a new GstBus instance. [transfer full]

### gst_bus_post ()

    gboolean            gst_bus_post                        (GstBus *bus,
                                                             GstMessage *message);

Post a message on the given bus. Ownership of the message is taken by the bus.

`bus` : | a GstBus to post on
`message` : | the GstMessage to post. [transfer full]
Returns : | TRUE if the message could be posted, FALSE if the bus is flushing. MT safe.

### gst_bus_have_pending ()

    gboolean            gst_bus_have_pending                (GstBus *bus);

Check if there are pending messages on the bus that should be handled.

`bus` : | a GstBus to check
Returns : | TRUE if there are messages on the bus to be handled, FALSE otherwise. MT safe.

### gst_bus_peek ()

    GstMessage *        gst_bus_peek                        (GstBus *bus);

Peek the message on the top of the bus' queue. The message will remain on the bus' message queue. A reference is returned, and needs to be unreffed by the caller.

`bus` : | a GstBus
Returns : | the GstMessage that is on the bus, or NULL if the bus is empty. MT safe. [transfer full]

### gst_bus_pop ()

    GstMessage *        gst_bus_pop                         (GstBus *bus);

Get a message from the bus.

`bus` : | a GstBus to pop
Returns : | the GstMessage that is on the bus, or NULL if the bus is empty. The message is taken from the bus and needs to be unreffed with `gst_message_unref()` after usage. MT safe. [transfer full]

### gst_bus_pop_filtered ()

    GstMessage *        gst_bus_pop_filtered                (GstBus *bus,
                                                             GstMessageType types);

Get a message matching `type` from the bus. Will discard all messages on the bus that do not match `type` and that have been posted before the first message that does match `type`. If there is no message matching `type` on the bus, all messages will be discarded.

`bus` : | a GstBus to pop
`types` : | message types to take into account
Returns : | the next GstMessage matching `type` that is on the bus, or NULL if the bus is empty or there is no message matching `type`. The message is taken from the bus and needs to be unreffed with `gst_message_unref()` after usage. MT safe. [transfer full]

Since 0.10.15

### gst_bus_timed_pop ()

    GstMessage *        gst_bus_timed_pop                   (GstBus *bus,
                                                             GstClockTime timeout);

Get a message from the bus, waiting up to the specified timeout.

If `timeout` is 0, this function behaves like `gst_bus_pop()`. If `timeout` is GST_CLOCK_TIME_NONE, this function will block forever until a message was posted on the bus.

`bus` : | a GstBus to pop
`timeout` : | a timeout
Returns : | the GstMessage that is on the bus after the specified timeout or NULL if the bus is empty after the timeout expired. The message is taken from the bus and needs to be unreffed with `gst_message_unref()` after usage. MT safe. [transfer full]

Since 0.10.12

### gst_bus_timed_pop_filtered ()

    GstMessage *        gst_bus_timed_pop_filtered          (GstBus *bus,
                                                             GstClockTime timeout,
                                                             GstMessageType types);

Get a message from the bus whose type matches the message type mask `types`, waiting up to the specified timeout (and discarding any messages that do not match the mask provided).

If `timeout` is 0, this function behaves like `gst_bus_pop_filtered()`. If `timeout` is GST_CLOCK_TIME_NONE, this function will block forever until a matching message was posted on the bus.

`bus` : | a GstBus to pop from
`timeout` : | a timeout in nanoseconds, or GST_CLOCK_TIME_NONE to wait forever
`types` : | message types to take into account, GST_MESSAGE_ANY for any type
Returns : | a GstMessage matching the filter in `types`, or NULL if no matching message was found on the bus until the timeout expired. The message is taken from the bus and needs to be unreffed with `gst_message_unref()` after usage. MT safe. [transfer full]

Since 0.10.15

### gst_bus_set_flushing ()

    void                gst_bus_set_flushing                (GstBus *bus,
                                                             gboolean flushing);

If `flushing`, flush out and unref any messages queued in the bus. Releases references to the message origin objects. Will flush future messages until `gst_bus_set_flushing()` sets `flushing` to FALSE.

MT safe.

`bus` : | a GstBus
`flushing` : | whether or not to flush the bus

### gst_bus_set_sync_handler ()

    void                gst_bus_set_sync_handler            (GstBus *bus,
                                                             GstBusSyncHandler func,
                                                             gpointer data);

Sets the synchronous handler on the bus. The function will be called every time a new message is posted on the bus. Note that the function will be called in the same thread context as the posting object. This function is usually only called by the creator of the bus. Applications should handle messages asynchronously using the gst_bus watch and poll functions.

You cannot replace an existing sync_handler. You can pass NULL to this function, which will clear the existing handler.

`bus` : | a GstBus to install the handler on
`func` : | The handler function to install
`data` : | User data that will be sent to the handler function.

### gst_bus_sync_signal_handler ()

    GstBusSyncReply     gst_bus_sync_signal_handler         (GstBus *bus,
                                                             GstMessage *message,
                                                             gpointer data);

A helper GstBusSyncHandler that can be used to convert all synchronous messages into signals.

`bus` : | a GstBus
`message` : | the GstMessage received
`data` : | user data
Returns : | GST_BUS_PASS

### gst_bus_create_watch ()

    GSource *           gst_bus_create_watch                (GstBus *bus);

Create watch for this bus. The GSource will be dispatched whenever a message is on the bus. After the GSource is dispatched, the message is popped off the bus and unreffed.

`bus` : | a GstBus to create the watch for
Returns : | a GSource that can be added to a mainloop. [transfer full]

### gst_bus_add_watch_full ()

    guint               gst_bus_add_watch_full              (GstBus *bus,
                                                             gint priority,
                                                             GstBusFunc func,
                                                             gpointer user_data,
                                                             GDestroyNotify notify);

Adds a bus watch to the default main context with the given `priority` (e.g. `G_PRIORITY_DEFAULT`). Since 0.10.33 it is also possible to use a non-default main context set up using `g_main_context_push_thread_default()` (before one had to create a bus watch source and attach it to the desired main context 'manually').

This function is used to receive asynchronous messages in the main loop. There can only be a single bus watch per bus, you must remove it before you can set a new one.

When `func` is called, the message belongs to the caller; if you want to keep a copy of it, call `gst_message_ref()` before leaving `func`.

The watch can be removed using `g_source_remove()` or by returning FALSE from `func`.

`bus` : | a GstBus to create the watch for.
`priority` : | The priority of the watch.
`func` : | A function to call when a message is received.
`user_data` : | user data passed to `func`.
`notify` : | the function to call when the source is removed.
Returns : | The event source id. MT safe.

### gst_bus_add_watch ()

    guint               gst_bus_add_watch                   (GstBus *bus,
                                                             GstBusFunc func,
                                                             gpointer user_data);

Adds a bus watch to the default main context with the default priority (`G_PRIORITY_DEFAULT`). Since 0.10.33 it is also possible to use a non-default main context set up using `g_main_context_push_thread_default()` (before one had to create a bus watch source and attach it to the desired main context 'manually').

This function is used to receive asynchronous messages in the main loop. There can only be a single bus watch per bus, you must remove it before you can set a new one.

The watch can be removed using `g_source_remove()` or by returning FALSE from `func`.

`bus` : | a GstBus to create the watch for
`func` : | A function to call when a message is received.
`user_data` : | user data passed to `func`.
Returns : | The event source id. MT safe.

### gst_bus_disable_sync_message_emission ()

    void                gst_bus_disable_sync_message_emission
                                                            (GstBus *bus);

Instructs GStreamer to stop emitting the "sync-message" signal for this bus. See `gst_bus_enable_sync_message_emission()` for more information.

In the event that multiple pieces of code have called `gst_bus_enable_sync_message_emission()`, the sync-message emissions will only be stopped after all calls to `gst_bus_enable_sync_message_emission()` were "cancelled" by calling this function. In this way the semantics are exactly the same as `gst_object_ref()` that which calls enable should also call disable.

MT safe.

`bus` : | a GstBus on which you previously called `gst_bus_enable_sync_message_emission()`

### gst_bus_enable_sync_message_emission ()

    void                gst_bus_enable_sync_message_emission
                                                            (GstBus *bus);

Instructs GStreamer to emit the "sync-message" signal after running the bus's sync handler. This function is here so that code can ensure that they can synchronously receive messages without having to affect what the bin's sync handler is.

This function may be called multiple times. To clean up, the caller is responsible for calling `gst_bus_disable_sync_message_emission()` as many times as this function is called.

While this function looks similar to `gst_bus_add_signal_watch()`, it is not exactly the same -- this function enables synchronous emission of signals when messages arrive; `gst_bus_add_signal_watch()` adds an idle callback to pop messages off the bus asynchronously. The sync-message signal comes from the thread of whatever object posted the message; the "message" signal is marshalled to the main thread via the main loop.

MT safe.

`bus` : | a GstBus on which you want to receive the "sync-message" signal

### gst_bus_async_signal_func ()

    gboolean            gst_bus_async_signal_func           (GstBus *bus,
                                                             GstMessage *message,
                                                             gpointer data);

A helper GstBusFunc that can be used to convert all asynchronous messages into signals.

`bus` : | a GstBus
`message` : | the GstMessage received
`data` : | user data
Returns : | TRUE

### gst_bus_add_signal_watch ()

    void                gst_bus_add_signal_watch            (GstBus *bus);

Adds a bus signal watch to the default main context with the default priority (`G_PRIORITY_DEFAULT`). Since 0.10.33 it is also possible to use a non-default main context set up using `g_main_context_push_thread_default()` (before one had to create a bus watch source and attach it to the desired main context 'manually').

After calling this statement, the bus will emit the "message" signal for each message posted on the bus.

This function may be called multiple times. To clean up, the caller is responsible for calling `gst_bus_remove_signal_watch()` as many times as this function is called.

MT safe.

`bus` : | a GstBus on which you want to receive the "message" signal

### gst_bus_add_signal_watch_full ()

    void                gst_bus_add_signal_watch_full       (GstBus *bus,
                                                             gint priority);

Adds a bus signal watch to the default main context with the given `priority` (e.g. `G_PRIORITY_DEFAULT`). Since 0.10.33 it is also possible to use a non-default main context set up using `g_main_context_push_thread_default()` (before one had to create a bus watch source and attach it to the desired main context 'manually').

After calling this statement, the bus will emit the "message" signal for each message posted on the bus when the main loop is running.

This function may be called multiple times. To clean up, the caller is responsible for calling `gst_bus_remove_signal_watch()` as many times as this function is called.

There can only be a single bus watch per bus, you must remove any signal watch before you can set another type of watch.

MT safe.

`bus` : | a GstBus on which you want to receive the "message" signal
`priority` : | The priority of the watch.

### gst_bus_remove_signal_watch ()

    void                gst_bus_remove_signal_watch         (GstBus *bus);

Removes a signal watch previously added with `gst_bus_add_signal_watch()`.

MT safe.

`bus` : | a GstBus you previously added a signal watch to

### gst_bus_poll ()

    GstMessage *        gst_bus_poll                        (GstBus *bus,
                                                             GstMessageType events,
                                                             GstClockTimeDiff timeout);

Poll the bus for messages. Will block while waiting for messages to come. You can specify a maximum time to poll with the `timeout` parameter. If `timeout` is negative, this function will block indefinitely.

All messages not in `events` will be popped off the bus and will be ignored.

Because poll is implemented using the "message" signal enabled by `gst_bus_add_signal_watch()`, calling `gst_bus_poll()` will cause the "message" signal to be emitted for every message that poll sees. Thus a "message" signal handler will see the same messages that this function sees -- neither will steal messages from the other.

This function will run a main loop from the default main context when polling.

You should never use this function, since it is pure evil. This is especially true for GUI applications based on Gtk+ or Qt, but also for any other non-trivial application that uses the GLib main loop. As this function runs a GLib main loop, any callback attached to the default GLib main context may be invoked. This could be timeouts, GUI events, I/O events etc.; even if `gst_bus_poll()` is called with a 0 timeout. Any of these callbacks may do things you do not expect, e.g. destroy the main application window or some other resource; change other application state; display a dialog and run another main loop until the user clicks it away. In short, using this function may add a lot of complexity to your code through unexpected re-entrancy and unexpected changes to your application's state.

For 0 timeouts use `gst_bus_pop_filtered()` instead of this function; for other short timeouts use `gst_bus_timed_pop_filtered()`; everything else is better handled by setting up an asynchronous bus watch and doing things from there.

`bus` : | a GstBus
`events` : | a mask of GstMessageType, representing the set of message types to poll for.
`timeout` : | the poll timeout, as a GstClockTimeDiff, or -1 to poll indefinitely.
Returns : | the message that was received, or NULL if the poll timed out. The message is taken from the bus and needs to be unreffed with `gst_message_unref()` after usage. [transfer full]

## Signal Details

### The `"message"` signal

    void                user_function                      (GstBus     *bus,
                                                            GstMessage *message,
                                                            gpointer    user_data)      : Has Details

A message has been posted on the bus. This signal is emitted from a GSource added to the mainloop. this signal will only be emitted when there is a mainloop running.

`bus` : | the object which received the signal
`message` : | the message that has been posted asynchronously
`user_data` : | user data set when the signal handler was connected.

### The `"sync-message"` signal

    void                user_function                      (GstBus     *bus,
                                                            GstMessage *message,
                                                            gpointer    user_data)      : Has Details

A message has been posted on the bus. This signal is emitted from the thread that posted the message so one has to be careful with locking.

This signal will not be emitted by default, you have to set up `gst_bus_sync_signal_handler()` as a sync handler if you want this signal to be emitted when a message is posted on the bus, like this:

    gst_bus_set_sync_handler (bus, gst_bus_sync_signal_handler, yourdata);

`bus` : | the object which received the signal
`message` : | the message that has been posted synchronously
`user_data` : | user data set when the signal handler was connected.

## See Also

GstMessage, GstElement