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 GST_DEBUG_OBJECT (bus, "%d messages on the bus",
459 g_queue_get_length (bus->queue));
461 g_mutex_lock (bus->queue_lock);
462 message = g_queue_pop_head (bus->queue);
463 g_mutex_unlock (bus->queue_lock);
466 GST_DEBUG_OBJECT (bus, "pop on bus, got message %p, %s", message,
467 gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
469 GST_DEBUG_OBJECT (bus, "pop on bus, no message");
478 * Peek the message on the top of the bus' queue. The message will remain
479 * on the bus' message queue. A reference is returned, and needs to be unreffed
482 * Returns: The #GstMessage that is on the bus, or NULL if the bus is empty.
487 gst_bus_peek (GstBus * bus)
491 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
493 g_mutex_lock (bus->queue_lock);
494 message = g_queue_peek_head (bus->queue);
496 gst_message_ref (message);
497 g_mutex_unlock (bus->queue_lock);
499 GST_DEBUG_OBJECT (bus, "peek on bus, got message %p", message);
505 * gst_bus_set_sync_handler:
506 * @bus: a #GstBus to install the handler on
507 * @func: The handler function to install
508 * @data: User data that will be sent to the handler function.
510 * Sets the synchronous handler on the bus. The function will be called
511 * every time a new message is posted on the bus. Note that the function
512 * will be called in the same thread context as the posting object. This
513 * function is usually only called by the creator of the bus. Applications
514 * should handle messages asynchronously using the gst_bus watch and poll
518 gst_bus_set_sync_handler (GstBus * bus, GstBusSyncHandler func, gpointer data)
520 g_return_if_fail (GST_IS_BUS (bus));
524 /* Assert if the user attempts to replace an existing sync_handler,
525 * other than to clear it */
526 g_assert (func == NULL || bus->sync_handler == NULL);
528 bus->sync_handler = func;
529 bus->sync_handler_data = data;
533 /* GSource for the bus
542 gst_bus_source_prepare (GSource * source, gint * timeout)
544 GstBusSource *bsrc = (GstBusSource *) source;
547 return gst_bus_have_pending (bsrc->bus);
551 gst_bus_source_check (GSource * source)
553 GstBusSource *bsrc = (GstBusSource *) source;
555 return gst_bus_have_pending (bsrc->bus);
559 gst_bus_source_dispatch (GSource * source, GSourceFunc callback,
562 GstBusFunc handler = (GstBusFunc) callback;
563 GstBusSource *bsource = (GstBusSource *) source;
568 g_return_val_if_fail (bsource != NULL, FALSE);
572 g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
574 message = gst_bus_pop (bus);
575 g_return_val_if_fail (message != NULL, FALSE);
580 GST_DEBUG_OBJECT (bus, "source %p calling dispatch with %p", source, message);
582 keep = handler (bus, message, user_data);
583 gst_message_unref (message);
585 GST_DEBUG_OBJECT (bus, "source %p handler returns %d", source, keep);
591 g_warning ("GstBus watch dispatched without callback\n"
592 "You must call g_source_connect().");
593 gst_message_unref (message);
599 gst_bus_source_finalize (GSource * source)
601 GstBusSource *bsource = (GstBusSource *) source;
603 gst_object_unref (bsource->bus);
607 static GSourceFuncs gst_bus_source_funcs = {
608 gst_bus_source_prepare,
609 gst_bus_source_check,
610 gst_bus_source_dispatch,
611 gst_bus_source_finalize
615 * gst_bus_create_watch:
616 * @bus: a #GstBus to create the watch for
618 * Create watch for this bus. The GSource will be dispatched whenever
619 * a message is on the bus. After the GSource is dispatched, the
620 * message is popped off the bus and unreffed.
622 * Returns: A #GSource that can be added to a mainloop.
625 gst_bus_create_watch (GstBus * bus)
627 GstBusSource *source;
629 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
631 source = (GstBusSource *) g_source_new (&gst_bus_source_funcs,
632 sizeof (GstBusSource));
633 gst_object_ref (bus);
636 return (GSource *) source;
640 * gst_bus_add_watch_full:
641 * @bus: a #GstBus to create the watch for.
642 * @priority: The priority of the watch.
643 * @func: A function to call when a message is received.
644 * @user_data: user data passed to @func.
645 * @notify: the function to call when the source is removed.
647 * Adds a bus watch to the default main context with the given priority.
648 * If the func returns FALSE, the source will be removed.
650 * When the func is called, the message belongs to the caller; if you want to
651 * keep a copy of it, call gst_message_ref() before leaving the func.
653 * The watch can be removed using #g_source_remove().
655 * Returns: The event source id.
660 gst_bus_add_watch_full (GstBus * bus, gint priority,
661 GstBusFunc func, gpointer user_data, GDestroyNotify notify)
666 g_return_val_if_fail (GST_IS_BUS (bus), 0);
668 source = gst_bus_create_watch (bus);
670 if (priority != G_PRIORITY_DEFAULT)
671 g_source_set_priority (source, priority);
673 g_source_set_callback (source, (GSourceFunc) func, user_data, notify);
675 id = g_source_attach (source, NULL);
676 g_source_unref (source);
678 GST_DEBUG_OBJECT (bus, "New source %p", source);
684 * @bus: a #GstBus to create the watch for
685 * @func: A function to call when a message is received.
686 * @user_data: user data passed to @func.
688 * Adds a bus watch to the default main context with the default priority.
690 * The watch can be removed using #g_source_remove().
692 * Returns: The event source id.
697 gst_bus_add_watch (GstBus * bus, GstBusFunc func, gpointer user_data)
699 return gst_bus_add_watch_full (bus, G_PRIORITY_DEFAULT, func,
707 gboolean source_running;
708 GstMessageType events;
713 poll_func (GstBus * bus, GstMessage * message, GstBusPollData * poll_data)
715 if (!g_main_loop_is_running (poll_data->loop)) {
716 GST_DEBUG ("mainloop %p not running", poll_data->loop);
720 if (GST_MESSAGE_TYPE (message) & poll_data->events) {
721 g_return_val_if_fail (poll_data->message == NULL, FALSE);
722 /* keep ref to message */
723 poll_data->message = gst_message_ref (message);
724 GST_DEBUG ("mainloop %p quit", poll_data->loop);
725 g_main_loop_quit (poll_data->loop);
728 /* we always keep the source alive so that we don't accidentialy
729 * free the poll_data */
734 poll_timeout (GstBusPollData * poll_data)
736 GST_DEBUG ("mainloop %p quit", poll_data->loop);
737 g_main_loop_quit (poll_data->loop);
739 /* we don't remove the GSource as this would free our poll_data,
740 * which we still need */
745 poll_destroy (GstBusPollData * poll_data)
747 poll_data->source_running = FALSE;
748 if (!poll_data->timeout_id) {
749 g_main_loop_unref (poll_data->loop);
755 poll_destroy_timeout (GstBusPollData * poll_data)
757 poll_data->timeout_id = 0;
758 if (!poll_data->source_running) {
759 g_main_loop_unref (poll_data->loop);
767 * @events: a mask of #GstMessageType, representing the set of message types to
769 * @timeout: the poll timeout, as a #GstClockTimeDiff, or -1 to poll indefinitely.
771 * Poll the bus for events. Will block while waiting for events to come. You can
772 * specify a maximum time to poll with the @timeout parameter. If @timeout is
773 * negative, this function will block indefinitely.
775 * All messages not in @events will be popped off the bus and will be ignored.
777 * This function will enter the default mainloop while polling.
779 * Returns: The message that was received, or NULL if the poll timed out.
780 * The message is taken from the bus and needs to be unreffed after usage.
783 gst_bus_poll (GstBus * bus, GstMessageType events, GstClockTimeDiff timeout)
785 GstBusPollData *poll_data;
789 poll_data = g_new0 (GstBusPollData, 1);
790 poll_data->source_running = TRUE;
791 poll_data->loop = g_main_loop_new (NULL, FALSE);
792 poll_data->events = events;
793 poll_data->message = NULL;
796 poll_data->timeout_id = g_timeout_add_full (G_PRIORITY_DEFAULT_IDLE,
797 timeout / GST_MSECOND, (GSourceFunc) poll_timeout, poll_data,
798 (GDestroyNotify) poll_destroy_timeout);
800 poll_data->timeout_id = 0;
802 id = gst_bus_add_watch_full (bus, G_PRIORITY_DEFAULT,
803 (GstBusFunc) poll_func, poll_data, (GDestroyNotify) poll_destroy);
805 GST_DEBUG ("running mainloop %p", poll_data->loop);
806 g_main_loop_run (poll_data->loop);
807 GST_DEBUG ("mainloop stopped %p", poll_data->loop);
809 ret = poll_data->message;
811 if (poll_data->timeout_id)
812 g_source_remove (poll_data->timeout_id);
814 /* poll_data may get destroyed at any time now */
815 g_source_remove (id);
817 GST_DEBUG_OBJECT (bus, "finished poll with message %p", ret);
823 * gst_bus_async_signal_func:
825 * @message: the message received
828 * A helper GstBusFunc that can be used to convert all asynchronous messages
834 gst_bus_async_signal_func (GstBus * bus, GstMessage * message, gpointer data)
838 g_return_val_if_fail (GST_IS_BUS (bus), TRUE);
839 g_return_val_if_fail (message != NULL, TRUE);
841 detail = gst_message_type_to_quark (GST_MESSAGE_TYPE (message));
843 g_signal_emit (bus, gst_bus_signals[ASYNC_MESSAGE], detail, message);
845 /* we never remove this source based on signal emission return values */
850 * gst_bus_sync_signal_handler:
852 * @message: the message received
855 * A helper GstBusSyncHandler that can be used to convert all synchronous
856 * messages into signals.
858 * Returns: GST_BUS_PASS
861 gst_bus_sync_signal_handler (GstBus * bus, GstMessage * message, gpointer data)
865 g_return_val_if_fail (GST_IS_BUS (bus), GST_BUS_DROP);
866 g_return_val_if_fail (message != NULL, GST_BUS_DROP);
868 detail = gst_message_type_to_quark (GST_MESSAGE_TYPE (message));
870 g_signal_emit (bus, gst_bus_signals[SYNC_MESSAGE], detail, message);
876 * gst_bus_add_signal_watch:
877 * @bus: a #GstBus on which you want to recieve the "message" signal
879 * Adds a bus signal watch to the default main context with the default priority.
880 * After calling this statement, the bus will emit the message signal for each
881 * message posted on the bus.
883 * This function may be called multiple times. To clean up, the caller is
884 * responsible for calling gst_bus_remove_signal_watch() as many times as this
885 * function is called.
890 gst_bus_add_signal_watch (GstBus * bus)
892 g_return_if_fail (GST_IS_BUS (bus));
894 /* I know the callees don't take this lock, so go ahead and abuse it */
897 if (bus->num_signal_watchers > 0)
900 g_assert (bus->signal_watch_id == 0);
902 bus->signal_watch_id =
903 gst_bus_add_watch (bus, gst_bus_async_signal_func, NULL);
907 bus->num_signal_watchers++;
913 * gst_bus_remove_signal_watch:
914 * @bus: a #GstBus you previously added a signal watch to
916 * Removes a signal watch previously added with gst_bus_add_signal_watch().
921 gst_bus_remove_signal_watch (GstBus * bus)
923 g_return_if_fail (GST_IS_BUS (bus));
925 /* I know the callees don't take this lock, so go ahead and abuse it */
928 if (bus->num_signal_watchers == 0)
931 bus->num_signal_watchers--;
933 if (bus->num_signal_watchers > 0)
936 g_source_remove (bus->signal_watch_id);
937 bus->signal_watch_id = 0;
945 g_critical ("Bus %s has no signal watches attached", GST_OBJECT_NAME (bus));