2 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
4 * gstbus.c: GstBus subsystem
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * @short_description: Asynchronous message bus subsystem
24 * @see_also: #GstMessage, #GstElement
26 * The #GstBus is an object responsible for delivering #GstMessages in
27 * a first-in first-out way from the streaming threads to the application.
29 * Since the application typically only wants to deal with delivery of these
30 * messages from one thread, the GstBus will marshall the messages between
31 * different threads. This is important since the actual streaming of media
32 * is done in another thread than the application.
34 * The GstBus provides support for #GSource based notifications. This makes it
35 * possible to handle the delivery in the glib mainloop.
37 * A message is posted on the bus with the gst_bus_post() method. With the
38 * gst_bus_peek() and _pop() methods one can look at or retrieve a previously
41 * The bus can be polled with the gst_bus_poll() method. This methods blocks
42 * up to the specified timeout value until one of the specified messages types
43 * is posted on the bus. The application can then _pop() the messages from the
45 * Alternatively the application can register an asynchronous bus function using
46 * gst_bus_add_watch_full() or gst_bus_add_watch(). This function will receive
47 * messages a short while after they have been posted.
49 * It is also possible to get messages from the bus without any thread
50 * marshalling with the gst_bus_set_sync_handler() method. This makes it
51 * possible to react to a message in the same thread that posted the
52 * message on the bus. This should only be used if the application is able
53 * to deal with messages from different threads.
55 * Every #GstBin has one bus.
57 * Note that a #GstBin will set its bus into flushing state when changing from
58 * READY to NULL state.
63 #include <sys/types.h>
64 #include <sys/socket.h>
66 #include "gst_private.h"
80 static void gst_bus_class_init (GstBusClass * klass);
81 static void gst_bus_init (GstBus * bus);
82 static void gst_bus_dispose (GObject * object);
84 static void gst_bus_set_property (GObject * object, guint prop_id,
85 const GValue * value, GParamSpec * pspec);
86 static void gst_bus_get_property (GObject * object, guint prop_id,
87 GValue * value, GParamSpec * pspec);
89 static GstObjectClass *parent_class = NULL;
90 static guint gst_bus_signals[LAST_SIGNAL] = { 0 };
93 gst_bus_get_type (void)
95 static GType bus_type = 0;
98 static const GTypeInfo bus_info = {
102 (GClassInitFunc) gst_bus_class_init,
107 (GInstanceInitFunc) gst_bus_init,
111 bus_type = g_type_register_static (GST_TYPE_OBJECT, "GstBus", &bus_info, 0);
116 /* fixme: do something about this */
118 marshal_VOID__MINIOBJECT (GClosure * closure, GValue * return_value,
119 guint n_param_values, const GValue * param_values, gpointer invocation_hint,
120 gpointer marshal_data)
122 typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1,
124 register marshalfunc_VOID__MINIOBJECT callback;
125 register GCClosure *cc = (GCClosure *) closure;
126 register gpointer data1, data2;
128 g_return_if_fail (n_param_values == 2);
130 if (G_CCLOSURE_SWAP_DATA (closure)) {
131 data1 = closure->data;
132 data2 = g_value_peek_pointer (param_values + 0);
134 data1 = g_value_peek_pointer (param_values + 0);
135 data2 = closure->data;
138 (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data : cc->
141 callback (data1, gst_value_get_mini_object (param_values + 1), data2);
145 gst_bus_class_init (GstBusClass * klass)
147 GObjectClass *gobject_class;
148 GstObjectClass *gstobject_class;
150 gobject_class = (GObjectClass *) klass;
151 gstobject_class = (GstObjectClass *) klass;
153 parent_class = g_type_class_peek_parent (klass);
155 if (!g_thread_supported ())
156 g_thread_init (NULL);
158 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_bus_dispose);
159 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_bus_set_property);
160 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_bus_get_property);
163 * GstBus::sync-message:
164 * @bus: the object which received the signal
165 * @message: the message that has been posted synchronously
167 * A message has been posted on the bus. This signal is emited from the
168 * thread that posted the message so one has to be carefull with locking.
170 gst_bus_signals[SYNC_MESSAGE] =
171 g_signal_new ("sync-message", G_TYPE_FROM_CLASS (klass),
172 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
173 G_STRUCT_OFFSET (GstBusClass, sync_message), NULL, NULL,
174 marshal_VOID__MINIOBJECT, G_TYPE_NONE, 1, GST_TYPE_MESSAGE);
178 * @bus: the object which received the signal
179 * @message: the message that has been posted asynchronously
181 * A message has been posted on the bus. This signal is emited from a
182 * GSource added to the mainloop.
184 gst_bus_signals[ASYNC_MESSAGE] =
185 g_signal_new ("message", G_TYPE_FROM_CLASS (klass),
186 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
187 G_STRUCT_OFFSET (GstBusClass, message), NULL, NULL,
188 marshal_VOID__MINIOBJECT, G_TYPE_NONE, 1, GST_TYPE_MESSAGE);
192 gst_bus_init (GstBus * bus)
194 bus->queue = g_queue_new ();
195 bus->queue_lock = g_mutex_new ();
197 GST_DEBUG_OBJECT (bus, "created");
203 gst_bus_dispose (GObject * object)
207 bus = GST_BUS (object);
212 g_mutex_lock (bus->queue_lock);
214 message = g_queue_pop_head (bus->queue);
216 gst_message_unref (message);
217 } while (message != NULL);
218 g_queue_free (bus->queue);
220 g_mutex_unlock (bus->queue_lock);
221 g_mutex_free (bus->queue_lock);
222 bus->queue_lock = NULL;
225 G_OBJECT_CLASS (parent_class)->dispose (object);
229 gst_bus_set_property (GObject * object, guint prop_id,
230 const GValue * value, GParamSpec * pspec)
234 bus = GST_BUS (object);
238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
244 gst_bus_get_property (GObject * object, guint prop_id,
245 GValue * value, GParamSpec * pspec)
249 bus = GST_BUS (object);
253 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261 * Creates a new #GstBuus instance.
263 * Returns: a new #GstBus instance
270 result = g_object_new (gst_bus_get_type (), NULL);
277 * @bus: a #GstBus to post on
278 * @message: The #GstMessage to post
280 * Post a message on the given bus. Ownership of the message
281 * is taken by the bus.
283 * Returns: TRUE if the message could be posted.
288 gst_bus_post (GstBus * bus, GstMessage * message)
290 GstBusSyncReply reply = GST_BUS_PASS;
291 GstBusSyncHandler handler;
292 gpointer handler_data;
294 g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
295 g_return_val_if_fail (GST_IS_MESSAGE (message), FALSE);
297 GST_DEBUG_OBJECT (bus, "[msg %p] posting on bus, type %s",
298 message, gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
301 /* check if the bus is flushing */
302 if (GST_FLAG_IS_SET (bus, GST_BUS_FLUSHING))
305 handler = bus->sync_handler;
306 handler_data = bus->sync_handler_data;
309 /* first call the sync handler if it is installed */
311 reply = handler (bus, message, handler_data);
313 /* now see what we should do with the message */
316 /* drop the message */
317 GST_DEBUG_OBJECT (bus, "[msg %p] dropped", message);
320 /* pass the message to the async queue, refcount passed in the queue */
321 GST_DEBUG_OBJECT (bus, "[msg %p] pushing on async queue", message);
322 g_mutex_lock (bus->queue_lock);
323 g_queue_push_tail (bus->queue, message);
324 g_mutex_unlock (bus->queue_lock);
325 GST_DEBUG_OBJECT (bus, "[msg %p] pushed on async queue", message);
327 /* FIXME cannot assume the source is only in the default context */
328 g_main_context_wakeup (NULL);
333 /* async delivery, we need a mutex and a cond to block
335 GMutex *lock = g_mutex_new ();
336 GCond *cond = g_cond_new ();
338 GST_MESSAGE_COND (message) = cond;
339 GST_MESSAGE_GET_LOCK (message) = lock;
341 GST_DEBUG_OBJECT (bus, "[msg %p] waiting for async delivery", message);
343 /* now we lock the message mutex, send the message to the async
344 * queue. When the message is handled by the app and destroyed,
345 * the cond will be signalled and we can continue */
347 g_mutex_lock (bus->queue_lock);
348 g_queue_push_tail (bus->queue, message);
349 g_mutex_unlock (bus->queue_lock);
351 /* FIXME cannot assume the source is only in the default context */
352 g_main_context_wakeup (NULL);
354 /* now block till the message is freed */
355 g_cond_wait (cond, lock);
356 g_mutex_unlock (lock);
358 GST_DEBUG_OBJECT (bus, "[msg %p] delivered asynchronously", message);
365 g_warning ("invalid return from bus sync handler");
373 GST_DEBUG_OBJECT (bus, "bus is flushing");
374 gst_message_unref (message);
382 * gst_bus_have_pending:
383 * @bus: a #GstBus to check
385 * Check if there are pending messages on the bus that
388 * Returns: TRUE if there are messages on the bus to be handled.
393 gst_bus_have_pending (GstBus * bus)
397 g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
399 g_mutex_lock (bus->queue_lock);
400 /* see if there is a message on the bus */
401 result = !g_queue_is_empty (bus->queue);
402 g_mutex_unlock (bus->queue_lock);
408 * gst_bus_set_flushing:
410 * @flushing: whether or not to flush the bus
412 * If @flushing, flush out and unref any messages queued in the bus. Releases
413 * references to the message origin objects. Will flush future messages until
414 * gst_bus_set_flushing() sets @flushing to #FALSE.
419 gst_bus_set_flushing (GstBus * bus, gboolean flushing)
426 GST_FLAG_SET (bus, GST_BUS_FLUSHING);
428 GST_DEBUG_OBJECT (bus, "set bus flushing");
430 while ((message = gst_bus_pop (bus)))
431 gst_message_unref (message);
433 GST_DEBUG_OBJECT (bus, "unset bus flushing");
434 GST_FLAG_UNSET (bus, GST_BUS_FLUSHING);
443 * @bus: a #GstBus to pop
445 * Get a message from the bus.
447 * Returns: The #GstMessage that is on the bus, or NULL if the bus is empty.
452 gst_bus_pop (GstBus * bus)
456 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
458 g_mutex_lock (bus->queue_lock);
459 message = g_queue_pop_head (bus->queue);
460 g_mutex_unlock (bus->queue_lock);
462 GST_DEBUG_OBJECT (bus, "pop on bus, got message %p", message);
471 * Peek the message on the top of the bus' queue. The message will remain
472 * on the bus' message queue. A reference is returned, and needs to be unreffed
475 * Returns: The #GstMessage that is on the bus, or NULL if the bus is empty.
480 gst_bus_peek (GstBus * bus)
484 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
486 g_mutex_lock (bus->queue_lock);
487 message = g_queue_peek_head (bus->queue);
489 gst_message_ref (message);
490 g_mutex_unlock (bus->queue_lock);
492 GST_DEBUG_OBJECT (bus, "peek on bus, got message %p", message);
498 * gst_bus_set_sync_handler:
499 * @bus: a #GstBus to install the handler on
500 * @func: The handler function to install
501 * @data: User data that will be sent to the handler function.
503 * Sets the synchronous handler on the bus. The function will be called
504 * every time a new message is posted on the bus. Note that the function
505 * will be called in the same thread context as the posting object. This
506 * function is usually only called by the creator of the bus. Applications
507 * should handle messages asynchronously using the gst_bus watch and poll
511 gst_bus_set_sync_handler (GstBus * bus, GstBusSyncHandler func, gpointer data)
513 g_return_if_fail (GST_IS_BUS (bus));
517 /* Assert if the user attempts to replace an existing sync_handler,
518 * other than to clear it */
519 g_assert (func == NULL || bus->sync_handler == NULL);
521 bus->sync_handler = func;
522 bus->sync_handler_data = data;
526 /* GSource for the bus
535 gst_bus_source_prepare (GSource * source, gint * timeout)
537 GstBusSource *bsrc = (GstBusSource *) source;
540 return gst_bus_have_pending (bsrc->bus);
544 gst_bus_source_check (GSource * source)
546 GstBusSource *bsrc = (GstBusSource *) source;
548 return gst_bus_have_pending (bsrc->bus);
552 gst_bus_source_dispatch (GSource * source, GSourceFunc callback,
555 GstBusFunc handler = (GstBusFunc) callback;
556 GstBusSource *bsource = (GstBusSource *) source;
561 g_return_val_if_fail (bsource != NULL, FALSE);
565 g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
567 message = gst_bus_pop (bus);
568 g_return_val_if_fail (message != NULL, FALSE);
573 GST_DEBUG_OBJECT (bus, "source %p calling dispatch with %p", source, message);
575 keep = handler (bus, message, user_data);
576 gst_message_unref (message);
578 GST_DEBUG_OBJECT (bus, "source %p handler returns %d", source, keep);
584 g_warning ("GstBus watch dispatched without callback\n"
585 "You must call g_source_connect().");
586 gst_message_unref (message);
592 gst_bus_source_finalize (GSource * source)
594 GstBusSource *bsource = (GstBusSource *) source;
596 gst_object_unref (bsource->bus);
600 static GSourceFuncs gst_bus_source_funcs = {
601 gst_bus_source_prepare,
602 gst_bus_source_check,
603 gst_bus_source_dispatch,
604 gst_bus_source_finalize
608 * gst_bus_create_watch:
609 * @bus: a #GstBus to create the watch for
611 * Create watch for this bus. The GSource will be dispatched whenever
612 * a message is on the bus. After the GSource is dispatched, the
613 * message is popped off the bus and unreffed.
615 * Returns: A #GSource that can be added to a mainloop.
618 gst_bus_create_watch (GstBus * bus)
620 GstBusSource *source;
622 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
624 source = (GstBusSource *) g_source_new (&gst_bus_source_funcs,
625 sizeof (GstBusSource));
626 gst_object_ref (bus);
629 return (GSource *) source;
633 * gst_bus_add_watch_full:
634 * @bus: a #GstBus to create the watch for.
635 * @priority: The priority of the watch.
636 * @func: A function to call when a message is received.
637 * @user_data: user data passed to @func.
638 * @notify: the function to call when the source is removed.
640 * Adds a bus watch to the default main context with the given priority.
641 * If the func returns FALSE, the source will be removed.
643 * When the func is called, the message belongs to the caller; if you want to
644 * keep a copy of it, call gst_message_ref() before leaving the func.
646 * The watch can be removed using #g_source_remove().
648 * Returns: The event source id.
653 gst_bus_add_watch_full (GstBus * bus, gint priority,
654 GstBusFunc func, gpointer user_data, GDestroyNotify notify)
659 g_return_val_if_fail (GST_IS_BUS (bus), 0);
661 source = gst_bus_create_watch (bus);
663 if (priority != G_PRIORITY_DEFAULT)
664 g_source_set_priority (source, priority);
666 g_source_set_callback (source, (GSourceFunc) func, user_data, notify);
668 id = g_source_attach (source, NULL);
669 g_source_unref (source);
671 GST_DEBUG_OBJECT (bus, "New source %p", source);
677 * @bus: a #GstBus to create the watch for
678 * @func: A function to call when a message is received.
679 * @user_data: user data passed to @func.
681 * Adds a bus watch to the default main context with the default priority.
683 * The watch can be removed using #g_source_remove().
685 * Returns: The event source id.
690 gst_bus_add_watch (GstBus * bus, GstBusFunc func, gpointer user_data)
692 return gst_bus_add_watch_full (bus, G_PRIORITY_DEFAULT, func,
700 gboolean source_running;
701 GstMessageType events;
706 poll_func (GstBus * bus, GstMessage * message, GstBusPollData * poll_data)
708 if (!g_main_loop_is_running (poll_data->loop)) {
709 GST_DEBUG ("mainloop %p not running", poll_data->loop);
713 if (GST_MESSAGE_TYPE (message) & poll_data->events) {
714 g_return_val_if_fail (poll_data->message == NULL, FALSE);
715 /* keep ref to message */
716 poll_data->message = gst_message_ref (message);
717 GST_DEBUG ("mainloop %p quit", poll_data->loop);
718 g_main_loop_quit (poll_data->loop);
721 /* we always keep the source alive so that we don't accidentialy
722 * free the poll_data */
727 poll_timeout (GstBusPollData * poll_data)
729 GST_DEBUG ("mainloop %p quit", poll_data->loop);
730 g_main_loop_quit (poll_data->loop);
732 /* we don't remove the GSource as this would free our poll_data,
733 * which we still need */
738 poll_destroy (GstBusPollData * poll_data)
740 poll_data->source_running = FALSE;
741 if (!poll_data->timeout_id) {
742 g_main_loop_unref (poll_data->loop);
748 poll_destroy_timeout (GstBusPollData * poll_data)
750 poll_data->timeout_id = 0;
751 if (!poll_data->source_running) {
752 g_main_loop_unref (poll_data->loop);
760 * @events: a mask of #GstMessageType, representing the set of message types to
762 * @timeout: the poll timeout, as a #GstClockTimeDiff, or -1 to poll indefinitely.
764 * Poll the bus for events. Will block while waiting for events to come. You can
765 * specify a maximum time to poll with the @timeout parameter. If @timeout is
766 * negative, this function will block indefinitely.
768 * All messages not in @events will be popped off the bus and will be ignored.
770 * This function will enter the default mainloop while polling.
772 * Returns: The message that was received, or NULL if the poll timed out.
773 * The message is taken from the bus and needs to be unreffed after usage.
776 gst_bus_poll (GstBus * bus, GstMessageType events, GstClockTimeDiff timeout)
778 GstBusPollData *poll_data;
782 poll_data = g_new0 (GstBusPollData, 1);
783 poll_data->source_running = TRUE;
784 poll_data->loop = g_main_loop_new (NULL, FALSE);
785 poll_data->events = events;
786 poll_data->message = NULL;
789 poll_data->timeout_id = g_timeout_add_full (G_PRIORITY_DEFAULT_IDLE,
790 timeout / GST_MSECOND, (GSourceFunc) poll_timeout, poll_data,
791 (GDestroyNotify) poll_destroy_timeout);
793 poll_data->timeout_id = 0;
795 id = gst_bus_add_watch_full (bus, G_PRIORITY_DEFAULT,
796 (GstBusFunc) poll_func, poll_data, (GDestroyNotify) poll_destroy);
798 GST_DEBUG ("running mainloop %p", poll_data->loop);
799 g_main_loop_run (poll_data->loop);
800 GST_DEBUG ("mainloop stopped %p", poll_data->loop);
802 ret = poll_data->message;
804 if (poll_data->timeout_id)
805 g_source_remove (poll_data->timeout_id);
807 /* poll_data may get destroyed at any time now */
808 g_source_remove (id);
810 GST_DEBUG_OBJECT (bus, "finished poll with message %p", ret);
816 * gst_bus_async_signal_func:
818 * @message: the message received
821 * A helper GstBusFunc that can be used to convert all asynchronous messages
827 gst_bus_async_signal_func (GstBus * bus, GstMessage * message, gpointer data)
831 g_return_val_if_fail (GST_IS_BUS (bus), TRUE);
832 g_return_val_if_fail (message != NULL, TRUE);
834 detail = gst_message_type_to_quark (GST_MESSAGE_TYPE (message));
836 g_signal_emit (bus, gst_bus_signals[ASYNC_MESSAGE], detail, message);
838 /* we never remove this source based on signal emission return values */
843 * gst_bus_sync_signal_handler:
845 * @message: the message received
848 * A helper GstBusSyncHandler that can be used to convert all synchronous
849 * messages into signals.
851 * Returns: GST_BUS_PASS
854 gst_bus_sync_signal_handler (GstBus * bus, GstMessage * message, gpointer data)
858 g_return_val_if_fail (GST_IS_BUS (bus), GST_BUS_DROP);
859 g_return_val_if_fail (message != NULL, GST_BUS_DROP);
861 detail = gst_message_type_to_quark (GST_MESSAGE_TYPE (message));
863 g_signal_emit (bus, gst_bus_signals[SYNC_MESSAGE], detail, message);
869 * gst_bus_add_signal_watch:
870 * @bus: a #GstBus to create the watch for
872 * Adds a bus signal watch to the default main context with the default priority.
873 * After calling this statement, the bus will emit the message signal for each
874 * message posted on the bus.
876 * The watch can be removed using #g_source_remove().
878 * Returns: The event source id.
883 gst_bus_add_signal_watch (GstBus * bus)
885 g_return_val_if_fail (GST_IS_BUS (bus), 0);
887 return gst_bus_add_watch (bus, gst_bus_async_signal_func, NULL);