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.
24 * @short_description: Asynchronous message bus subsystem
25 * @see_also: #GstMessage, #GstElement
27 * The #GstBus is an object responsible for delivering #GstMessages in
28 * a first-in first-out way from the streaming threads to the application.
30 * Since the application typically only wants to deal with delivery of these
31 * messages from one thread, the GstBus will marshall the messages between
32 * different threads. This is important since the actual streaming of media
33 * is done in another thread than the application.
35 * The GstBus provides support for #GSource based notifications. This makes it
36 * possible to handle the delivery in the glib mainloop.
38 * The #GSource callback function gst_bus_async_signal_func() can be used to
39 * convert all bus messages into signal emissions.
41 * A message is posted on the bus with the gst_bus_post() method. With the
42 * gst_bus_peek() and gst_bus_pop() methods one can look at or retrieve a
43 * previously posted message.
45 * The bus can be polled with the gst_bus_poll() method. This methods blocks
46 * up to the specified timeout value until one of the specified messages types
47 * is posted on the bus. The application can then _pop() the messages from the
49 * Alternatively the application can register an asynchronous bus function
50 * using gst_bus_add_watch_full() or gst_bus_add_watch(). This function will
51 * install a #GSource in the default glib main loop and will deliver messages
52 * a short while after they have been posted. Note that the main loop should
53 * be running for the asynchronous callbacks.
55 * It is also possible to get messages from the bus without any thread
56 * marshalling with the gst_bus_set_sync_handler() method. This makes it
57 * possible to react to a message in the same thread that posted the
58 * message on the bus. This should only be used if the application is able
59 * to deal with messages from different threads.
61 * Every #GstPipeline has one bus.
63 * Note that a #GstPipeline will set its bus into flushing state when changing
64 * from READY to NULL state.
66 * Last reviewed on 2006-03-12 (0.10.5)
69 #include "gst_private.h"
74 #include <sys/types.h>
80 #define GST_CAT_DEFAULT GST_CAT_BUS
90 static void gst_bus_class_init (GstBusClass * klass);
91 static void gst_bus_init (GstBus * bus);
92 static void gst_bus_dispose (GObject * object);
94 static void gst_bus_set_property (GObject * object, guint prop_id,
95 const GValue * value, GParamSpec * pspec);
96 static void gst_bus_get_property (GObject * object, guint prop_id,
97 GValue * value, GParamSpec * pspec);
99 static GstObjectClass *parent_class = NULL;
100 static guint gst_bus_signals[LAST_SIGNAL] = { 0 };
102 /* the context we wakeup when we posted a message on the bus */
103 static GMainContext *main_context;
105 struct _GstBusPrivate
107 guint num_sync_message_emitters;
111 gst_bus_get_type (void)
113 static GType bus_type = 0;
115 if (G_UNLIKELY (bus_type == 0)) {
116 static const GTypeInfo bus_info = {
117 sizeof (GstBusClass),
120 (GClassInitFunc) gst_bus_class_init,
125 (GInstanceInitFunc) gst_bus_init,
129 bus_type = g_type_register_static (GST_TYPE_OBJECT, "GstBus", &bus_info, 0);
134 /* fixme: do something about this */
136 marshal_VOID__MINIOBJECT (GClosure * closure, GValue * return_value,
137 guint n_param_values, const GValue * param_values, gpointer invocation_hint,
138 gpointer marshal_data)
140 typedef void (*marshalfunc_VOID__MINIOBJECT) (gpointer obj, gpointer arg1,
142 register marshalfunc_VOID__MINIOBJECT callback;
143 register GCClosure *cc = (GCClosure *) closure;
144 register gpointer data1, data2;
146 g_return_if_fail (n_param_values == 2);
148 if (G_CCLOSURE_SWAP_DATA (closure)) {
149 data1 = closure->data;
150 data2 = g_value_peek_pointer (param_values + 0);
152 data1 = g_value_peek_pointer (param_values + 0);
153 data2 = closure->data;
156 (marshalfunc_VOID__MINIOBJECT) (marshal_data ? marshal_data : cc->
159 callback (data1, gst_value_get_mini_object (param_values + 1), data2);
163 gst_bus_class_init (GstBusClass * klass)
165 GObjectClass *gobject_class;
166 GstObjectClass *gstobject_class;
168 gobject_class = (GObjectClass *) klass;
169 gstobject_class = (GstObjectClass *) klass;
171 parent_class = g_type_class_peek_parent (klass);
173 if (!g_thread_supported ())
174 g_thread_init (NULL);
176 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_bus_dispose);
177 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_bus_set_property);
178 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_bus_get_property);
181 * GstBus::sync-message:
182 * @bus: the object which received the signal
183 * @message: the message that has been posted synchronously
185 * A message has been posted on the bus. This signal is emitted from the
186 * thread that posted the message so one has to be careful with locking.
188 gst_bus_signals[SYNC_MESSAGE] =
189 g_signal_new ("sync-message", G_TYPE_FROM_CLASS (klass),
190 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
191 G_STRUCT_OFFSET (GstBusClass, sync_message), NULL, NULL,
192 marshal_VOID__MINIOBJECT, G_TYPE_NONE, 1, GST_TYPE_MESSAGE);
196 * @bus: the object which received the signal
197 * @message: the message that has been posted asynchronously
199 * A message has been posted on the bus. This signal is emitted from a
200 * GSource added to the mainloop. this signal will only be emitted when
201 * there is a mainloop running.
203 gst_bus_signals[ASYNC_MESSAGE] =
204 g_signal_new ("message", G_TYPE_FROM_CLASS (klass),
205 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
206 G_STRUCT_OFFSET (GstBusClass, message), NULL, NULL,
207 marshal_VOID__MINIOBJECT, G_TYPE_NONE, 1, GST_TYPE_MESSAGE);
209 main_context = g_main_context_default ();
211 g_type_class_add_private (klass, sizeof (GstBusPrivate));
215 gst_bus_init (GstBus * bus)
217 bus->queue = g_queue_new ();
218 bus->queue_lock = g_mutex_new ();
220 bus->priv = G_TYPE_INSTANCE_GET_PRIVATE (bus, GST_TYPE_BUS, GstBusPrivate);
222 GST_DEBUG_OBJECT (bus, "created");
226 gst_bus_dispose (GObject * object)
230 bus = GST_BUS (object);
235 g_mutex_lock (bus->queue_lock);
237 message = g_queue_pop_head (bus->queue);
239 gst_message_unref (message);
240 } while (message != NULL);
241 g_queue_free (bus->queue);
243 g_mutex_unlock (bus->queue_lock);
244 g_mutex_free (bus->queue_lock);
245 bus->queue_lock = NULL;
248 G_OBJECT_CLASS (parent_class)->dispose (object);
252 gst_bus_set_property (GObject * object, guint prop_id,
253 const GValue * value, GParamSpec * pspec)
257 bus = GST_BUS (object);
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
267 gst_bus_get_property (GObject * object, guint prop_id,
268 GValue * value, GParamSpec * pspec)
272 bus = GST_BUS (object);
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284 * Creates a new #GstBus instance.
286 * Returns: a new #GstBus instance
293 result = g_object_new (gst_bus_get_type (), NULL);
294 GST_DEBUG_OBJECT (result, "created new bus");
301 * @bus: a #GstBus to post on
302 * @message: The #GstMessage to post
304 * Post a message on the given bus. Ownership of the message
305 * is taken by the bus.
307 * Returns: TRUE if the message could be posted, FALSE if the bus is flushing.
312 gst_bus_post (GstBus * bus, GstMessage * message)
314 GstBusSyncReply reply = GST_BUS_PASS;
315 GstBusSyncHandler handler;
316 gboolean emit_sync_message;
317 gpointer handler_data;
319 g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
320 g_return_val_if_fail (GST_IS_MESSAGE (message), FALSE);
322 GST_DEBUG_OBJECT (bus, "[msg %p] posting on bus, type %s",
323 message, GST_MESSAGE_TYPE_NAME (message));
325 GST_OBJECT_LOCK (bus);
326 /* check if the bus is flushing */
327 if (GST_OBJECT_FLAG_IS_SET (bus, GST_BUS_FLUSHING))
330 handler = bus->sync_handler;
331 handler_data = bus->sync_handler_data;
332 emit_sync_message = bus->priv->num_sync_message_emitters > 0;
333 GST_OBJECT_UNLOCK (bus);
335 /* first call the sync handler if it is installed */
337 reply = handler (bus, message, handler_data);
339 /* emit sync-message if requested to do so via
340 gst_bus_enable_sync_message_emission. terrible but effective */
341 if (emit_sync_message && reply != GST_BUS_DROP
342 && handler != gst_bus_sync_signal_handler)
343 gst_bus_sync_signal_handler (bus, message, NULL);
345 /* now see what we should do with the message */
348 /* drop the message */
349 GST_DEBUG_OBJECT (bus, "[msg %p] dropped", message);
352 /* pass the message to the async queue, refcount passed in the queue */
353 GST_DEBUG_OBJECT (bus, "[msg %p] pushing on async queue", message);
354 g_mutex_lock (bus->queue_lock);
355 g_queue_push_tail (bus->queue, message);
356 g_mutex_unlock (bus->queue_lock);
357 GST_DEBUG_OBJECT (bus, "[msg %p] pushed on async queue", message);
359 /* FIXME cannot assume sources are only in the default context */
360 g_main_context_wakeup (main_context);
365 /* async delivery, we need a mutex and a cond to block
367 GMutex *lock = g_mutex_new ();
368 GCond *cond = g_cond_new ();
370 GST_MESSAGE_COND (message) = cond;
371 GST_MESSAGE_GET_LOCK (message) = lock;
373 GST_DEBUG_OBJECT (bus, "[msg %p] waiting for async delivery", message);
375 /* now we lock the message mutex, send the message to the async
376 * queue. When the message is handled by the app and destroyed,
377 * the cond will be signalled and we can continue */
379 g_mutex_lock (bus->queue_lock);
380 g_queue_push_tail (bus->queue, message);
381 g_mutex_unlock (bus->queue_lock);
383 /* FIXME cannot assume sources are only in the default context */
384 g_main_context_wakeup (main_context);
386 /* now block till the message is freed */
387 g_cond_wait (cond, lock);
388 g_mutex_unlock (lock);
390 GST_DEBUG_OBJECT (bus, "[msg %p] delivered asynchronously", message);
397 g_warning ("invalid return from bus sync handler");
405 GST_DEBUG_OBJECT (bus, "bus is flushing");
406 gst_message_unref (message);
407 GST_OBJECT_UNLOCK (bus);
414 * gst_bus_have_pending:
415 * @bus: a #GstBus to check
417 * Check if there are pending messages on the bus that
420 * Returns: TRUE if there are messages on the bus to be handled, FALSE
426 gst_bus_have_pending (GstBus * bus)
430 g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
432 g_mutex_lock (bus->queue_lock);
433 /* see if there is a message on the bus */
434 result = !g_queue_is_empty (bus->queue);
435 g_mutex_unlock (bus->queue_lock);
441 * gst_bus_set_flushing:
443 * @flushing: whether or not to flush the bus
445 * If @flushing, flush out and unref any messages queued in the bus. Releases
446 * references to the message origin objects. Will flush future messages until
447 * gst_bus_set_flushing() sets @flushing to #FALSE.
452 gst_bus_set_flushing (GstBus * bus, gboolean flushing)
456 GST_OBJECT_LOCK (bus);
459 GST_OBJECT_FLAG_SET (bus, GST_BUS_FLUSHING);
461 GST_DEBUG_OBJECT (bus, "set bus flushing");
463 while ((message = gst_bus_pop (bus)))
464 gst_message_unref (message);
466 GST_DEBUG_OBJECT (bus, "unset bus flushing");
467 GST_OBJECT_FLAG_UNSET (bus, GST_BUS_FLUSHING);
470 GST_OBJECT_UNLOCK (bus);
476 * @bus: a #GstBus to pop
478 * Get a message from the bus.
480 * Returns: The #GstMessage that is on the bus, or NULL if the bus is empty.
481 * The message is taken from the bus and needs to be unreffed with
482 * gst_message_unref() after usage.
487 gst_bus_pop (GstBus * bus)
491 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
493 g_mutex_lock (bus->queue_lock);
494 message = g_queue_pop_head (bus->queue);
496 GST_DEBUG_OBJECT (bus, "pop from bus, have %d messages, got message %p, %s",
497 g_queue_get_length (bus->queue) + 1, message,
498 GST_MESSAGE_TYPE_NAME (message));
500 GST_DEBUG_OBJECT (bus, "pop from bus, no messages");
501 g_mutex_unlock (bus->queue_lock);
510 * Peek the message on the top of the bus' queue. The message will remain
511 * on the bus' message queue. A reference is returned, and needs to be unreffed
514 * Returns: The #GstMessage that is on the bus, or NULL if the bus is empty.
519 gst_bus_peek (GstBus * bus)
523 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
525 g_mutex_lock (bus->queue_lock);
526 message = g_queue_peek_head (bus->queue);
528 gst_message_ref (message);
529 g_mutex_unlock (bus->queue_lock);
531 GST_DEBUG_OBJECT (bus, "peek on bus, got message %p", message);
537 * gst_bus_set_sync_handler:
538 * @bus: a #GstBus to install the handler on
539 * @func: The handler function to install
540 * @data: User data that will be sent to the handler function.
542 * Sets the synchronous handler on the bus. The function will be called
543 * every time a new message is posted on the bus. Note that the function
544 * will be called in the same thread context as the posting object. This
545 * function is usually only called by the creator of the bus. Applications
546 * should handle messages asynchronously using the gst_bus watch and poll
549 * You cannot replace an existing sync_handler. You can pass NULL to this
550 * function, which will clear the existing handler.
553 gst_bus_set_sync_handler (GstBus * bus, GstBusSyncHandler func, gpointer data)
555 g_return_if_fail (GST_IS_BUS (bus));
557 GST_OBJECT_LOCK (bus);
559 /* Assert if the user attempts to replace an existing sync_handler,
560 * other than to clear it */
561 if (func != NULL && bus->sync_handler != NULL)
564 bus->sync_handler = func;
565 bus->sync_handler_data = data;
566 GST_OBJECT_UNLOCK (bus);
572 GST_OBJECT_UNLOCK (bus);
573 g_warning ("cannot replace existing sync handler");
578 /* GSource for the bus
587 gst_bus_source_prepare (GSource * source, gint * timeout)
589 GstBusSource *bsrc = (GstBusSource *) source;
592 return gst_bus_have_pending (bsrc->bus);
596 gst_bus_source_check (GSource * source)
598 GstBusSource *bsrc = (GstBusSource *) source;
600 return gst_bus_have_pending (bsrc->bus);
604 gst_bus_source_dispatch (GSource * source, GSourceFunc callback,
607 GstBusFunc handler = (GstBusFunc) callback;
608 GstBusSource *bsource = (GstBusSource *) source;
613 g_return_val_if_fail (bsource != NULL, FALSE);
617 g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
619 message = gst_bus_pop (bus);
620 g_return_val_if_fail (message != NULL, FALSE);
625 GST_DEBUG_OBJECT (bus, "source %p calling dispatch with %p", source, message);
627 keep = handler (bus, message, user_data);
628 gst_message_unref (message);
630 GST_DEBUG_OBJECT (bus, "source %p handler returns %d", source, keep);
636 g_warning ("GstBus watch dispatched without callback\n"
637 "You must call g_source_connect().");
638 gst_message_unref (message);
644 gst_bus_source_finalize (GSource * source)
646 GstBusSource *bsource = (GstBusSource *) source;
648 gst_object_unref (bsource->bus);
652 static GSourceFuncs gst_bus_source_funcs = {
653 gst_bus_source_prepare,
654 gst_bus_source_check,
655 gst_bus_source_dispatch,
656 gst_bus_source_finalize
660 * gst_bus_create_watch:
661 * @bus: a #GstBus to create the watch for
663 * Create watch for this bus. The GSource will be dispatched whenever
664 * a message is on the bus. After the GSource is dispatched, the
665 * message is popped off the bus and unreffed.
667 * Returns: A #GSource that can be added to a mainloop.
670 gst_bus_create_watch (GstBus * bus)
672 GstBusSource *source;
674 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
676 source = (GstBusSource *) g_source_new (&gst_bus_source_funcs,
677 sizeof (GstBusSource));
678 gst_object_ref (bus);
681 return (GSource *) source;
685 * gst_bus_add_watch_full:
686 * @bus: a #GstBus to create the watch for.
687 * @priority: The priority of the watch.
688 * @func: A function to call when a message is received.
689 * @user_data: user data passed to @func.
690 * @notify: the function to call when the source is removed.
692 * Adds a bus watch to the default main context with the given @priority.
693 * This function is used to receive asynchronous messages in the main loop.
695 * When @func is called, the message belongs to the caller; if you want to
696 * keep a copy of it, call gst_message_ref() before leaving @func.
698 * The watch can be removed using g_source_remove() or by returning FALSE
701 * Returns: The event source id.
706 gst_bus_add_watch_full (GstBus * bus, gint priority,
707 GstBusFunc func, gpointer user_data, GDestroyNotify notify)
712 g_return_val_if_fail (GST_IS_BUS (bus), 0);
714 source = gst_bus_create_watch (bus);
716 if (priority != G_PRIORITY_DEFAULT)
717 g_source_set_priority (source, priority);
719 g_source_set_callback (source, (GSourceFunc) func, user_data, notify);
721 id = g_source_attach (source, NULL);
722 g_source_unref (source);
724 GST_DEBUG_OBJECT (bus, "New source %p", source);
730 * @bus: a #GstBus to create the watch for
731 * @func: A function to call when a message is received.
732 * @user_data: user data passed to @func.
734 * Adds a bus watch to the default main context with the default priority.
735 * This function is used to receive asynchronous messages in the main loop.
737 * The watch can be removed using g_source_remove() or by returning FALSE
740 * Returns: The event source id.
745 gst_bus_add_watch (GstBus * bus, GstBusFunc func, gpointer user_data)
747 return gst_bus_add_watch_full (bus, G_PRIORITY_DEFAULT, func,
755 gboolean source_running;
756 GstMessageType events;
761 poll_func (GstBus * bus, GstMessage * message, GstBusPollData * poll_data)
763 if (!g_main_loop_is_running (poll_data->loop)) {
764 GST_DEBUG ("mainloop %p not running", poll_data->loop);
768 if (GST_MESSAGE_TYPE (message) & poll_data->events) {
769 g_return_if_fail (poll_data->message == NULL);
770 /* keep ref to message */
771 poll_data->message = gst_message_ref (message);
772 GST_DEBUG ("mainloop %p quit", poll_data->loop);
773 g_main_loop_quit (poll_data->loop);
778 poll_timeout (GstBusPollData * poll_data)
780 GST_DEBUG ("mainloop %p quit", poll_data->loop);
781 g_main_loop_quit (poll_data->loop);
783 /* we don't remove the GSource as this would free our poll_data,
784 * which we still need */
789 poll_destroy (GstBusPollData * poll_data, gpointer unused)
791 poll_data->source_running = FALSE;
792 if (!poll_data->timeout_id) {
793 g_main_loop_unref (poll_data->loop);
799 poll_destroy_timeout (GstBusPollData * poll_data)
801 poll_data->timeout_id = 0;
802 if (!poll_data->source_running) {
803 g_main_loop_unref (poll_data->loop);
811 * @events: a mask of #GstMessageType, representing the set of message types to
813 * @timeout: the poll timeout, as a #GstClockTimeDiff, or -1 to poll indefinitely.
815 * Poll the bus for messages. Will block while waiting for messages to come.
816 * You can specify a maximum time to poll with the @timeout parameter. If
817 * @timeout is negative, this function will block indefinitely.
819 * All messages not in @events will be popped off the bus and will be ignored.
821 * Because poll is implemented using the "message" signal enabled by
822 * gst_bus_add_signal_watch(), calling gst_bus_poll() will cause the "message"
823 * signal to be emitted for every message that poll sees. Thus a "message"
824 * signal handler will see the same messages that this function sees -- neither
825 * will steal messages from the other.
827 * This function will run a main loop from the default main context when
830 * Returns: The message that was received, or NULL if the poll timed out.
831 * The message is taken from the bus and needs to be unreffed with
832 * gst_message_unref() after usage.
835 gst_bus_poll (GstBus * bus, GstMessageType events, GstClockTimeDiff timeout)
837 GstBusPollData *poll_data;
841 poll_data = g_new0 (GstBusPollData, 1);
842 poll_data->source_running = TRUE;
843 poll_data->loop = g_main_loop_new (NULL, FALSE);
844 poll_data->events = events;
845 poll_data->message = NULL;
848 poll_data->timeout_id = g_timeout_add_full (G_PRIORITY_DEFAULT_IDLE,
849 timeout / GST_MSECOND, (GSourceFunc) poll_timeout, poll_data,
850 (GDestroyNotify) poll_destroy_timeout);
852 poll_data->timeout_id = 0;
854 id = g_signal_connect_data (bus, "message", G_CALLBACK (poll_func), poll_data,
855 (GClosureNotify) poll_destroy, 0);
857 /* these can be nested, so it's ok */
858 gst_bus_add_signal_watch (bus);
860 GST_DEBUG ("running mainloop %p", poll_data->loop);
861 g_main_loop_run (poll_data->loop);
862 GST_DEBUG ("mainloop stopped %p", poll_data->loop);
864 gst_bus_remove_signal_watch (bus);
867 ret = poll_data->message;
869 if (poll_data->timeout_id)
870 g_source_remove (poll_data->timeout_id);
872 /* poll_data will be freed now */
873 g_signal_handler_disconnect (bus, id);
875 GST_DEBUG_OBJECT (bus, "finished poll with message %p", ret);
881 * gst_bus_async_signal_func:
883 * @message: the #GstMessage received
886 * A helper #GstBusFunc that can be used to convert all asynchronous messages
892 gst_bus_async_signal_func (GstBus * bus, GstMessage * message, gpointer data)
896 g_return_val_if_fail (GST_IS_BUS (bus), TRUE);
897 g_return_val_if_fail (message != NULL, TRUE);
899 detail = gst_message_type_to_quark (GST_MESSAGE_TYPE (message));
901 g_signal_emit (bus, gst_bus_signals[ASYNC_MESSAGE], detail, message);
903 /* we never remove this source based on signal emission return values */
908 * gst_bus_sync_signal_handler:
910 * @message: the #GstMessage received
913 * A helper GstBusSyncHandler that can be used to convert all synchronous
914 * messages into signals.
916 * Returns: GST_BUS_PASS
919 gst_bus_sync_signal_handler (GstBus * bus, GstMessage * message, gpointer data)
923 g_return_val_if_fail (GST_IS_BUS (bus), GST_BUS_DROP);
924 g_return_val_if_fail (message != NULL, GST_BUS_DROP);
926 detail = gst_message_type_to_quark (GST_MESSAGE_TYPE (message));
928 g_signal_emit (bus, gst_bus_signals[SYNC_MESSAGE], detail, message);
934 * gst_bus_enable_sync_message_emission:
935 * @bus: a #GstBus on which you want to receive the "sync-message" signal
937 * Instructs GStreamer to emit the "sync-message" signal after running the bus's
938 * sync handler. This function is here so that code can ensure that they can
939 * synchronously receive messages without having to affect what the bin's sync
942 * This function may be called multiple times. To clean up, the caller is
943 * responsible for calling gst_bus_disable_sync_message_emission() as many times
944 * as this function is called.
946 * While this function looks similar to gst_bus_add_signal_watch(), it is not
947 * exactly the same -- this function enables <emphasis>synchronous</emphasis> emission of
948 * signals when messages arrive; gst_bus_add_signal_watch() adds an idle callback
949 * to pop messages off the bus <emphasis>asynchronously</emphasis>. The sync-message signal
950 * comes from the thread of whatever object posted the message; the "message"
951 * signal is marshalled to the main thread via the main loop.
956 gst_bus_enable_sync_message_emission (GstBus * bus)
958 g_return_if_fail (GST_IS_BUS (bus));
960 GST_OBJECT_LOCK (bus);
962 bus->priv->num_sync_message_emitters++;
964 GST_OBJECT_UNLOCK (bus);
968 * gst_bus_disable_sync_message_emission:
969 * @bus: a #GstBus on which you previously called
970 * gst_bus_enable_sync_message_emission()
972 * Instructs GStreamer to stop emitting the "sync-message" signal for this bus.
973 * See gst_bus_enable_sync_message_emission() for more information.
975 * In the event that multiple pieces of code have called
976 * gst_bus_enable_sync_message_emission(), the sync-message emissions will only
977 * be stopped after all calls to gst_bus_enable_sync_message_emission() were
978 * "cancelled" by calling this function. In this way the semantics are exactly
979 * the same as gst_object_ref() that which calls enable should also call
985 gst_bus_disable_sync_message_emission (GstBus * bus)
987 g_return_if_fail (GST_IS_BUS (bus));
989 g_return_if_fail (bus->num_signal_watchers == 0);
991 GST_OBJECT_LOCK (bus);
993 bus->priv->num_sync_message_emitters--;
995 GST_OBJECT_UNLOCK (bus);
999 * gst_bus_add_signal_watch_full:
1000 * @bus: a #GstBus on which you want to receive the "message" signal
1001 * @priority: The priority of the watch.
1003 * Adds a bus signal watch to the default main context with the given priority.
1004 * After calling this statement, the bus will emit the "message" signal for each
1005 * message posted on the bus when the main loop is running.
1007 * This function may be called multiple times. To clean up, the caller is
1008 * responsible for calling gst_bus_remove_signal_watch() as many times as this
1009 * function is called.
1014 gst_bus_add_signal_watch_full (GstBus * bus, gint priority)
1016 g_return_if_fail (GST_IS_BUS (bus));
1018 /* I know the callees don't take this lock, so go ahead and abuse it */
1019 GST_OBJECT_LOCK (bus);
1021 if (bus->num_signal_watchers > 0)
1024 g_assert (bus->signal_watch_id == 0);
1026 bus->signal_watch_id =
1027 gst_bus_add_watch_full (bus, priority, gst_bus_async_signal_func, NULL,
1032 bus->num_signal_watchers++;
1034 GST_OBJECT_UNLOCK (bus);
1038 * gst_bus_add_signal_watch:
1039 * @bus: a #GstBus on which you want to receive the "message" signal
1041 * Adds a bus signal watch to the default main context with the default
1043 * After calling this statement, the bus will emit the "message" signal for each
1044 * message posted on the bus.
1046 * This function may be called multiple times. To clean up, the caller is
1047 * responsible for calling gst_bus_remove_signal_watch() as many times as this
1048 * function is called.
1053 gst_bus_add_signal_watch (GstBus * bus)
1055 gst_bus_add_signal_watch_full (bus, G_PRIORITY_DEFAULT);
1059 * gst_bus_remove_signal_watch:
1060 * @bus: a #GstBus you previously added a signal watch to
1062 * Removes a signal watch previously added with gst_bus_add_signal_watch().
1067 gst_bus_remove_signal_watch (GstBus * bus)
1069 g_return_if_fail (GST_IS_BUS (bus));
1071 /* I know the callees don't take this lock, so go ahead and abuse it */
1072 GST_OBJECT_LOCK (bus);
1074 if (bus->num_signal_watchers == 0)
1077 bus->num_signal_watchers--;
1079 if (bus->num_signal_watchers > 0)
1082 g_source_remove (bus->signal_watch_id);
1083 bus->signal_watch_id = 0;
1086 GST_OBJECT_UNLOCK (bus);
1091 g_critical ("Bus %s has no signal watches attached", GST_OBJECT_NAME (bus));
1092 GST_OBJECT_UNLOCK (bus);