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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
25 * @short_description: Asynchronous message bus subsystem
26 * @see_also: #GstMessage, #GstElement
28 * The #GstBus is an object responsible for delivering #GstMessage packets in
29 * a first-in first-out way from the streaming threads (see #GstTask) to the
32 * Since the application typically only wants to deal with delivery of these
33 * messages from one thread, the GstBus will marshall the messages between
34 * different threads. This is important since the actual streaming of media
35 * is done in another thread than the application.
37 * The GstBus provides support for #GSource based notifications. This makes it
38 * possible to handle the delivery in the glib mainloop.
40 * The #GSource callback function gst_bus_async_signal_func() can be used to
41 * convert all bus messages into signal emissions.
43 * A message is posted on the bus with the gst_bus_post() method. With the
44 * gst_bus_peek() and gst_bus_pop() methods one can look at or retrieve a
45 * previously posted message.
47 * The bus can be polled with the gst_bus_poll() method. This methods blocks
48 * up to the specified timeout value until one of the specified messages types
49 * is posted on the bus. The application can then gst_bus_pop() the messages
50 * from the bus to handle them.
51 * Alternatively the application can register an asynchronous bus function
52 * using gst_bus_add_watch_full() or gst_bus_add_watch(). This function will
53 * install a #GSource in the default glib main loop and will deliver messages
54 * a short while after they have been posted. Note that the main loop should
55 * be running for the asynchronous callbacks.
57 * It is also possible to get messages from the bus without any thread
58 * marshalling with the gst_bus_set_sync_handler() method. This makes it
59 * possible to react to a message in the same thread that posted the
60 * message on the bus. This should only be used if the application is able
61 * to deal with messages from different threads.
63 * Every #GstPipeline has one bus.
65 * Note that a #GstPipeline will set its bus into flushing state when changing
66 * from READY to NULL state.
69 #include "gst_private.h"
74 #include <sys/types.h>
76 #include "gstatomicqueue.h"
81 #include "glib-compat-private.h"
85 # define EWOULDBLOCK EAGAIN /* This is just to placate gcc */
87 #endif /* G_OS_WIN32 */
89 #define GST_CAT_DEFAULT GST_CAT_BUS
99 #define DEFAULT_ENABLE_ASYNC (TRUE)
107 static void gst_bus_dispose (GObject * object);
108 static void gst_bus_finalize (GObject * object);
110 static guint gst_bus_signals[LAST_SIGNAL] = { 0 };
112 struct _GstBusPrivate
114 GstAtomicQueue *queue;
117 GstBusSyncHandler sync_handler;
118 gpointer sync_handler_data;
119 GDestroyNotify sync_handler_notify;
121 guint num_signal_watchers;
123 guint num_sync_message_emitters;
124 GSource *signal_watch;
126 gboolean enable_async;
131 #define gst_bus_parent_class parent_class
132 G_DEFINE_TYPE_WITH_PRIVATE (GstBus, gst_bus, GST_TYPE_OBJECT);
135 gst_bus_set_property (GObject * object,
136 guint prop_id, const GValue * value, GParamSpec * pspec)
138 GstBus *bus = GST_BUS_CAST (object);
141 case PROP_ENABLE_ASYNC:
142 bus->priv->enable_async = g_value_get_boolean (value);
145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151 gst_bus_constructed (GObject * object)
153 GstBus *bus = GST_BUS_CAST (object);
155 if (bus->priv->enable_async) {
156 bus->priv->poll = gst_poll_new_timer ();
157 gst_poll_get_read_gpollfd (bus->priv->poll, &bus->priv->pollfd);
160 G_OBJECT_CLASS (gst_bus_parent_class)->constructed (object);
164 gst_bus_class_init (GstBusClass * klass)
166 GObjectClass *gobject_class = (GObjectClass *) klass;
168 gobject_class->dispose = gst_bus_dispose;
169 gobject_class->finalize = gst_bus_finalize;
170 gobject_class->set_property = gst_bus_set_property;
171 gobject_class->constructed = gst_bus_constructed;
174 * GstBus::enable-async:
176 * Enable async message delivery support for bus watches,
177 * gst_bus_pop() and similar API. Without this only the
178 * synchronous message handlers are called.
180 * This property is used to create the child element buses
183 g_object_class_install_property (gobject_class, PROP_ENABLE_ASYNC,
184 g_param_spec_boolean ("enable-async", "Enable Async",
185 "Enable async message delivery for bus watches and gst_bus_pop()",
186 DEFAULT_ENABLE_ASYNC,
187 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
190 * GstBus::sync-message:
191 * @bus: the object which received the signal
192 * @message: the message that has been posted synchronously
194 * A message has been posted on the bus. This signal is emitted from the
195 * thread that posted the message so one has to be careful with locking.
197 * This signal will not be emitted by default, you have to call
198 * gst_bus_enable_sync_message_emission() before.
200 gst_bus_signals[SYNC_MESSAGE] =
201 g_signal_new ("sync-message", G_TYPE_FROM_CLASS (klass),
202 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
203 G_STRUCT_OFFSET (GstBusClass, sync_message), NULL, NULL,
204 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_MESSAGE);
208 * @bus: the object which received the signal
209 * @message: the message that has been posted asynchronously
211 * A message has been posted on the bus. This signal is emitted from a
212 * GSource added to the mainloop. this signal will only be emitted when
213 * there is a mainloop running.
215 gst_bus_signals[ASYNC_MESSAGE] =
216 g_signal_new ("message", G_TYPE_FROM_CLASS (klass),
217 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
218 G_STRUCT_OFFSET (GstBusClass, message), NULL, NULL,
219 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_MESSAGE);
223 gst_bus_init (GstBus * bus)
225 bus->priv = gst_bus_get_instance_private (bus);
226 bus->priv->enable_async = DEFAULT_ENABLE_ASYNC;
227 g_mutex_init (&bus->priv->queue_lock);
228 bus->priv->queue = gst_atomic_queue_new (32);
230 GST_DEBUG_OBJECT (bus, "created");
234 gst_bus_dispose (GObject * object)
236 GstBus *bus = GST_BUS (object);
238 if (bus->priv->queue) {
241 g_mutex_lock (&bus->priv->queue_lock);
243 message = gst_atomic_queue_pop (bus->priv->queue);
245 gst_message_unref (message);
246 } while (message != NULL);
247 gst_atomic_queue_unref (bus->priv->queue);
248 bus->priv->queue = NULL;
249 g_mutex_unlock (&bus->priv->queue_lock);
250 g_mutex_clear (&bus->priv->queue_lock);
253 gst_poll_free (bus->priv->poll);
254 bus->priv->poll = NULL;
257 G_OBJECT_CLASS (parent_class)->dispose (object);
261 gst_bus_finalize (GObject * object)
263 GstBus *bus = GST_BUS (object);
265 if (bus->priv->sync_handler_notify)
266 bus->priv->sync_handler_notify (bus->priv->sync_handler_data);
268 G_OBJECT_CLASS (parent_class)->finalize (object);
274 * Creates a new #GstBus instance.
276 * Returns: (transfer full): a new #GstBus instance
283 result = g_object_new (gst_bus_get_type (), NULL);
284 GST_DEBUG_OBJECT (result, "created new bus");
286 /* clear floating flag */
287 gst_object_ref_sink (result);
294 * @bus: a #GstBus to post on
295 * @message: (transfer full): the #GstMessage to post
297 * Post a message on the given bus. Ownership of the message
298 * is taken by the bus.
300 * Returns: %TRUE if the message could be posted, %FALSE if the bus is flushing.
305 gst_bus_post (GstBus * bus, GstMessage * message)
307 GstBusSyncReply reply = GST_BUS_PASS;
308 GstBusSyncHandler handler;
309 gboolean emit_sync_message;
310 gpointer handler_data;
312 g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
313 g_return_val_if_fail (GST_IS_MESSAGE (message), FALSE);
315 GST_DEBUG_OBJECT (bus, "[msg %p] posting on bus %" GST_PTR_FORMAT, message,
318 /* check we didn't accidentally add a public flag that maps to same value */
319 g_assert (!GST_MINI_OBJECT_FLAG_IS_SET (message,
320 GST_MESSAGE_FLAG_ASYNC_DELIVERY));
322 GST_OBJECT_LOCK (bus);
323 /* check if the bus is flushing */
324 if (GST_OBJECT_FLAG_IS_SET (bus, GST_BUS_FLUSHING))
327 handler = bus->priv->sync_handler;
328 handler_data = bus->priv->sync_handler_data;
329 emit_sync_message = bus->priv->num_sync_message_emitters > 0;
330 GST_OBJECT_UNLOCK (bus);
332 /* first call the sync handler if it is installed */
334 reply = handler (bus, message, handler_data);
336 /* emit sync-message if requested to do so via
337 gst_bus_enable_sync_message_emission. terrible but effective */
338 if (emit_sync_message && reply != GST_BUS_DROP
339 && handler != gst_bus_sync_signal_handler)
340 gst_bus_sync_signal_handler (bus, message, NULL);
342 /* If this is a bus without async message delivery
343 * always drop the message */
344 if (!bus->priv->poll)
345 reply = GST_BUS_DROP;
347 /* now see what we should do with the message */
350 /* drop the message */
351 GST_DEBUG_OBJECT (bus, "[msg %p] dropped", message);
354 /* pass the message to the async queue, refcount passed in the queue */
355 GST_DEBUG_OBJECT (bus, "[msg %p] pushing on async queue", message);
356 gst_atomic_queue_push (bus->priv->queue, message);
357 gst_poll_write_control (bus->priv->poll);
358 GST_DEBUG_OBJECT (bus, "[msg %p] pushed on async queue", message);
363 /* async delivery, we need a mutex and a cond to block
365 GCond *cond = GST_MESSAGE_GET_COND (message);
366 GMutex *lock = GST_MESSAGE_GET_LOCK (message);
371 GST_MINI_OBJECT_FLAG_SET (message, GST_MESSAGE_FLAG_ASYNC_DELIVERY);
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 */
380 gst_atomic_queue_push (bus->priv->queue, message);
381 gst_poll_write_control (bus->priv->poll);
383 /* now block till the message is freed */
384 g_cond_wait (cond, lock);
386 /* we acquired a new ref from gst_message_dispose() so we can clean up */
387 g_mutex_unlock (lock);
389 GST_DEBUG_OBJECT (bus, "[msg %p] delivered asynchronously", message);
391 GST_MINI_OBJECT_FLAG_UNSET (message, GST_MESSAGE_FLAG_ASYNC_DELIVERY);
393 g_mutex_clear (lock);
396 gst_message_unref (message);
400 g_warning ("invalid return from bus sync handler");
408 GST_DEBUG_OBJECT (bus, "bus is flushing");
409 GST_OBJECT_UNLOCK (bus);
410 gst_message_unref (message);
417 * gst_bus_have_pending:
418 * @bus: a #GstBus to check
420 * Check if there are pending messages on the bus that
423 * Returns: %TRUE if there are messages on the bus to be handled, %FALSE
429 gst_bus_have_pending (GstBus * bus)
433 g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
435 /* see if there is a message on the bus */
436 result = gst_atomic_queue_length (bus->priv->queue) != 0;
442 * gst_bus_set_flushing:
444 * @flushing: whether or not to flush the bus
446 * If @flushing, flush out and unref any messages queued in the bus. Releases
447 * references to the message origin objects. Will flush future messages until
448 * gst_bus_set_flushing() sets @flushing to %FALSE.
453 gst_bus_set_flushing (GstBus * bus, gboolean flushing)
456 GList *message_list = NULL;
458 g_return_if_fail (GST_IS_BUS (bus));
460 GST_OBJECT_LOCK (bus);
463 GST_OBJECT_FLAG_SET (bus, GST_BUS_FLUSHING);
465 GST_DEBUG_OBJECT (bus, "set bus flushing");
467 while ((message = gst_bus_pop (bus)))
468 message_list = g_list_prepend (message_list, message);
470 GST_DEBUG_OBJECT (bus, "unset bus flushing");
471 GST_OBJECT_FLAG_UNSET (bus, GST_BUS_FLUSHING);
474 GST_OBJECT_UNLOCK (bus);
476 g_list_free_full (message_list, (GDestroyNotify) gst_message_unref);
480 * gst_bus_timed_pop_filtered:
481 * @bus: a #GstBus to pop from
482 * @timeout: a timeout in nanoseconds, or GST_CLOCK_TIME_NONE to wait forever
483 * @types: message types to take into account, GST_MESSAGE_ANY for any type
485 * Get a message from the bus whose type matches the message type mask @types,
486 * waiting up to the specified timeout (and discarding any messages that do not
487 * match the mask provided).
489 * If @timeout is 0, this function behaves like gst_bus_pop_filtered(). If
490 * @timeout is #GST_CLOCK_TIME_NONE, this function will block forever until a
491 * matching message was posted on the bus.
493 * Returns: (transfer full) (nullable): a #GstMessage matching the
494 * filter in @types, or %NULL if no matching message was found on
495 * the bus until the timeout expired. The message is taken from
496 * the bus and needs to be unreffed with gst_message_unref() after
502 gst_bus_timed_pop_filtered (GstBus * bus, GstClockTime timeout,
503 GstMessageType types)
507 gboolean first_round = TRUE;
508 GstClockTime elapsed = 0;
510 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
511 g_return_val_if_fail (types != 0, NULL);
512 g_return_val_if_fail (timeout == 0 || bus->priv->poll != NULL, NULL);
514 g_mutex_lock (&bus->priv->queue_lock);
519 GST_LOG_OBJECT (bus, "have %d messages",
520 gst_atomic_queue_length (bus->priv->queue));
522 while ((message = gst_atomic_queue_pop (bus->priv->queue))) {
523 if (bus->priv->poll) {
524 while (!gst_poll_read_control (bus->priv->poll)) {
525 if (errno == EWOULDBLOCK) {
526 /* Retry, this can happen if pushing to the queue has finished,
527 * popping here succeeded but writing control did not finish
528 * before we got to this line. */
529 /* Give other threads the chance to do something */
533 /* This is a real error and means that either the bus is in an
534 * inconsistent state, or the GstPoll is invalid. GstPoll already
535 * prints a critical warning about this, no need to do that again
542 GST_DEBUG_OBJECT (bus, "got message %p, %s from %s, type mask is %u",
543 message, GST_MESSAGE_TYPE_NAME (message),
544 GST_MESSAGE_SRC_NAME (message), (guint) types);
545 if ((GST_MESSAGE_TYPE (message) & types) != 0) {
546 /* Extra check to ensure extended types don't get matched unless
548 if ((!GST_MESSAGE_TYPE_IS_EXTENDED (message))
549 || (types & GST_MESSAGE_EXTENDED)) {
550 /* exit the loop, we have a message */
555 GST_DEBUG_OBJECT (bus, "discarding message, does not match mask");
556 gst_message_unref (message);
560 /* no need to wait, exit loop */
564 else if (timeout != GST_CLOCK_TIME_NONE) {
566 g_get_current_time (&then);
569 g_get_current_time (&now);
571 elapsed = GST_TIMEVAL_TO_TIME (now) - GST_TIMEVAL_TO_TIME (then);
573 if (elapsed > timeout)
578 /* only here in timeout case */
579 g_assert (bus->priv->poll);
580 g_mutex_unlock (&bus->priv->queue_lock);
581 ret = gst_poll_wait (bus->priv->poll, timeout - elapsed);
582 g_mutex_lock (&bus->priv->queue_lock);
585 GST_INFO_OBJECT (bus, "timed out, breaking loop");
588 GST_INFO_OBJECT (bus, "we got woken up, recheck for message");
594 g_mutex_unlock (&bus->priv->queue_lock);
602 * @bus: a #GstBus to pop
603 * @timeout: a timeout
605 * Get a message from the bus, waiting up to the specified timeout.
607 * If @timeout is 0, this function behaves like gst_bus_pop(). If @timeout is
608 * #GST_CLOCK_TIME_NONE, this function will block forever until a message was
611 * Returns: (transfer full) (nullable): the #GstMessage that is on the
612 * bus after the specified timeout or %NULL if the bus is empty
613 * after the timeout expired. The message is taken from the bus
614 * and needs to be unreffed with gst_message_unref() after usage.
619 gst_bus_timed_pop (GstBus * bus, GstClockTime timeout)
621 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
623 return gst_bus_timed_pop_filtered (bus, timeout, GST_MESSAGE_ANY);
627 * gst_bus_pop_filtered:
628 * @bus: a #GstBus to pop
629 * @types: message types to take into account
631 * Get a message matching @type from the bus. Will discard all messages on
632 * the bus that do not match @type and that have been posted before the first
633 * message that does match @type. If there is no message matching @type on
634 * the bus, all messages will be discarded. It is not possible to use message
635 * enums beyond #GST_MESSAGE_EXTENDED in the @events mask.
637 * Returns: (transfer full) (nullable): the next #GstMessage matching
638 * @type that is on the bus, or %NULL if the bus is empty or there
639 * is no message matching @type. The message is taken from the bus
640 * and needs to be unreffed with gst_message_unref() after usage.
645 gst_bus_pop_filtered (GstBus * bus, GstMessageType types)
647 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
648 g_return_val_if_fail (types != 0, NULL);
650 return gst_bus_timed_pop_filtered (bus, 0, types);
655 * @bus: a #GstBus to pop
657 * Get a message from the bus.
659 * Returns: (transfer full) (nullable): the #GstMessage that is on the
660 * bus, or %NULL if the bus is empty. The message is taken from
661 * the bus and needs to be unreffed with gst_message_unref() after
667 gst_bus_pop (GstBus * bus)
669 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
671 return gst_bus_timed_pop_filtered (bus, 0, GST_MESSAGE_ANY);
678 * Peek the message on the top of the bus' queue. The message will remain
679 * on the bus' message queue. A reference is returned, and needs to be unreffed
682 * Returns: (transfer full) (nullable): the #GstMessage that is on the
683 * bus, or %NULL if the bus is empty.
688 gst_bus_peek (GstBus * bus)
692 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
694 g_mutex_lock (&bus->priv->queue_lock);
695 message = gst_atomic_queue_peek (bus->priv->queue);
697 gst_message_ref (message);
698 g_mutex_unlock (&bus->priv->queue_lock);
700 GST_DEBUG_OBJECT (bus, "peek on bus, got message %p", message);
706 * gst_bus_set_sync_handler:
707 * @bus: a #GstBus to install the handler on
708 * @func: (allow-none): The handler function to install
709 * @user_data: User data that will be sent to the handler function.
710 * @notify: called when @user_data becomes unused
712 * Sets the synchronous handler on the bus. The function will be called
713 * every time a new message is posted on the bus. Note that the function
714 * will be called in the same thread context as the posting object. This
715 * function is usually only called by the creator of the bus. Applications
716 * should handle messages asynchronously using the gst_bus watch and poll
719 * You cannot replace an existing sync_handler. You can pass %NULL to this
720 * function, which will clear the existing handler.
723 gst_bus_set_sync_handler (GstBus * bus, GstBusSyncHandler func,
724 gpointer user_data, GDestroyNotify notify)
726 GDestroyNotify old_notify;
728 g_return_if_fail (GST_IS_BUS (bus));
730 GST_OBJECT_LOCK (bus);
731 /* Assert if the user attempts to replace an existing sync_handler,
732 * other than to clear it */
733 if (func != NULL && bus->priv->sync_handler != NULL)
736 if ((old_notify = bus->priv->sync_handler_notify)) {
737 gpointer old_data = bus->priv->sync_handler_data;
739 bus->priv->sync_handler_data = NULL;
740 bus->priv->sync_handler_notify = NULL;
741 GST_OBJECT_UNLOCK (bus);
743 old_notify (old_data);
745 GST_OBJECT_LOCK (bus);
747 bus->priv->sync_handler = func;
748 bus->priv->sync_handler_data = user_data;
749 bus->priv->sync_handler_notify = notify;
750 GST_OBJECT_UNLOCK (bus);
756 GST_OBJECT_UNLOCK (bus);
757 g_warning ("cannot replace existing sync handler");
763 * gst_bus_get_pollfd:
765 * @fd: (out): A GPollFD to fill
767 * Gets the file descriptor from the bus which can be used to get notified about
768 * messages being available with functions like g_poll(), and allows integration
769 * into other event loops based on file descriptors.
770 * Whenever a message is available, the POLLIN / %G_IO_IN event is set.
772 * Warning: NEVER read or write anything to the returned fd but only use it
773 * for getting notifications via g_poll() or similar and then use the normal
774 * GstBus API, e.g. gst_bus_pop().
779 gst_bus_get_pollfd (GstBus * bus, GPollFD * fd)
781 g_return_if_fail (GST_IS_BUS (bus));
782 g_return_if_fail (bus->priv->poll != NULL);
784 *fd = bus->priv->pollfd;
787 /* GSource for the bus
796 gst_bus_source_prepare (GSource * source, gint * timeout)
803 gst_bus_source_check (GSource * source)
805 GstBusSource *bsrc = (GstBusSource *) source;
807 return bsrc->bus->priv->pollfd.revents & (G_IO_IN | G_IO_HUP | G_IO_ERR);
811 gst_bus_source_dispatch (GSource * source, GSourceFunc callback,
814 GstBusFunc handler = (GstBusFunc) callback;
815 GstBusSource *bsource = (GstBusSource *) source;
820 g_return_val_if_fail (bsource != NULL, FALSE);
824 g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
826 message = gst_bus_pop (bus);
828 /* The message queue might be empty if some other thread or callback set
829 * the bus to flushing between check/prepare and dispatch */
830 if (G_UNLIKELY (message == NULL))
836 GST_DEBUG_OBJECT (bus, "source %p calling dispatch with %" GST_PTR_FORMAT,
839 keep = handler (bus, message, user_data);
840 gst_message_unref (message);
842 GST_DEBUG_OBJECT (bus, "source %p handler returns %d", source, keep);
848 g_warning ("GstBus watch dispatched without callback\n"
849 "You must call g_source_set_callback().");
850 gst_message_unref (message);
856 gst_bus_source_finalize (GSource * source)
858 GstBusSource *bsource = (GstBusSource *) source;
863 GST_DEBUG_OBJECT (bus, "finalize source %p", source);
865 GST_OBJECT_LOCK (bus);
866 if (bus->priv->signal_watch == source)
867 bus->priv->signal_watch = NULL;
868 GST_OBJECT_UNLOCK (bus);
870 gst_object_unref (bsource->bus);
874 static GSourceFuncs gst_bus_source_funcs = {
875 gst_bus_source_prepare,
876 gst_bus_source_check,
877 gst_bus_source_dispatch,
878 gst_bus_source_finalize
882 * gst_bus_create_watch:
883 * @bus: a #GstBus to create the watch for
885 * Create watch for this bus. The GSource will be dispatched whenever
886 * a message is on the bus. After the GSource is dispatched, the
887 * message is popped off the bus and unreffed.
889 * Returns: (transfer full) (nullable): a #GSource that can be added to a mainloop.
892 gst_bus_create_watch (GstBus * bus)
894 GstBusSource *source;
896 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
897 g_return_val_if_fail (bus->priv->poll != NULL, NULL);
899 source = (GstBusSource *) g_source_new (&gst_bus_source_funcs,
900 sizeof (GstBusSource));
902 g_source_set_name ((GSource *) source, "GStreamer message bus watch");
904 source->bus = gst_object_ref (bus);
905 g_source_add_poll ((GSource *) source, &bus->priv->pollfd);
907 return (GSource *) source;
910 /* must be called with the bus OBJECT LOCK */
912 gst_bus_add_watch_full_unlocked (GstBus * bus, gint priority,
913 GstBusFunc func, gpointer user_data, GDestroyNotify notify)
919 if (bus->priv->signal_watch) {
920 GST_ERROR_OBJECT (bus,
921 "Tried to add new watch while one was already there");
925 source = gst_bus_create_watch (bus);
927 g_critical ("Creating bus watch failed");
931 if (priority != G_PRIORITY_DEFAULT)
932 g_source_set_priority (source, priority);
934 g_source_set_callback (source, (GSourceFunc) func, user_data, notify);
936 ctx = g_main_context_get_thread_default ();
937 id = g_source_attach (source, ctx);
938 g_source_unref (source);
941 bus->priv->signal_watch = source;
944 GST_DEBUG_OBJECT (bus, "New source %p with id %u", source, id);
949 * gst_bus_add_watch_full: (rename-to gst_bus_add_watch)
950 * @bus: a #GstBus to create the watch for.
951 * @priority: The priority of the watch.
952 * @func: A function to call when a message is received.
953 * @user_data: user data passed to @func.
954 * @notify: the function to call when the source is removed.
956 * Adds a bus watch to the default main context with the given @priority (e.g.
957 * %G_PRIORITY_DEFAULT). It is also possible to use a non-default main
958 * context set up using g_main_context_push_thread_default() (before
959 * one had to create a bus watch source and attach it to the desired main
960 * context 'manually').
962 * This function is used to receive asynchronous messages in the main loop.
963 * There can only be a single bus watch per bus, you must remove it before you
966 * The bus watch will only work if a GLib main loop is being run.
968 * When @func is called, the message belongs to the caller; if you want to
969 * keep a copy of it, call gst_message_ref() before leaving @func.
971 * The watch can be removed using gst_bus_remove_watch() or by returning %FALSE
972 * from @func. If the watch was added to the default main context it is also
973 * possible to remove the watch using g_source_remove().
975 * The bus watch will take its own reference to the @bus, so it is safe to unref
976 * @bus using gst_object_unref() after setting the bus watch.
980 * Returns: The event source id or 0 if @bus already got an event source.
983 gst_bus_add_watch_full (GstBus * bus, gint priority,
984 GstBusFunc func, gpointer user_data, GDestroyNotify notify)
988 g_return_val_if_fail (GST_IS_BUS (bus), 0);
990 GST_OBJECT_LOCK (bus);
991 id = gst_bus_add_watch_full_unlocked (bus, priority, func, user_data, notify);
992 GST_OBJECT_UNLOCK (bus);
998 * gst_bus_add_watch: (skip)
999 * @bus: a #GstBus to create the watch for
1000 * @func: A function to call when a message is received.
1001 * @user_data: user data passed to @func.
1003 * Adds a bus watch to the default main context with the default priority
1004 * (%G_PRIORITY_DEFAULT). It is also possible to use a non-default main
1005 * context set up using g_main_context_push_thread_default() (before
1006 * one had to create a bus watch source and attach it to the desired main
1007 * context 'manually').
1009 * This function is used to receive asynchronous messages in the main loop.
1010 * There can only be a single bus watch per bus, you must remove it before you
1011 * can set a new one.
1013 * The bus watch will only work if a GLib main loop is being run.
1015 * The watch can be removed using gst_bus_remove_watch() or by returning %FALSE
1016 * from @func. If the watch was added to the default main context it is also
1017 * possible to remove the watch using g_source_remove().
1019 * The bus watch will take its own reference to the @bus, so it is safe to unref
1020 * @bus using gst_object_unref() after setting the bus watch.
1024 * Returns: The event source id or 0 if @bus already got an event source.
1027 gst_bus_add_watch (GstBus * bus, GstBusFunc func, gpointer user_data)
1029 return gst_bus_add_watch_full (bus, G_PRIORITY_DEFAULT, func,
1034 * gst_bus_remove_watch:
1035 * @bus: a #GstBus to remove the watch from.
1037 * Removes an installed bus watch from @bus.
1039 * Returns: %TRUE on success or %FALSE if @bus has no event source.
1045 gst_bus_remove_watch (GstBus * bus)
1049 g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
1051 GST_OBJECT_LOCK (bus);
1053 if (bus->priv->signal_watch == NULL) {
1054 GST_ERROR_OBJECT (bus, "no bus watch was present");
1058 watch_id = bus->priv->signal_watch;
1060 GST_OBJECT_UNLOCK (bus);
1062 g_source_destroy (watch_id);
1067 GST_OBJECT_UNLOCK (bus);
1076 gboolean source_running;
1077 GstMessageType events;
1078 GstMessage *message;
1082 poll_func (GstBus * bus, GstMessage * message, GstBusPollData * poll_data)
1084 GstMessageType type;
1086 if (!g_main_loop_is_running (poll_data->loop)) {
1087 GST_DEBUG ("mainloop %p not running", poll_data->loop);
1091 type = GST_MESSAGE_TYPE (message);
1093 if (type & poll_data->events) {
1094 g_assert (poll_data->message == NULL);
1095 /* keep ref to message */
1096 poll_data->message = gst_message_ref (message);
1097 GST_DEBUG ("mainloop %p quit", poll_data->loop);
1098 g_main_loop_quit (poll_data->loop);
1100 GST_DEBUG ("type %08x does not match %08x", type, poll_data->events);
1105 poll_timeout (GstBusPollData * poll_data)
1107 GST_DEBUG ("mainloop %p quit", poll_data->loop);
1108 g_main_loop_quit (poll_data->loop);
1110 /* we don't remove the GSource as this would free our poll_data,
1111 * which we still need */
1116 poll_destroy (GstBusPollData * poll_data, gpointer unused)
1118 poll_data->source_running = FALSE;
1119 if (!poll_data->timeout_id) {
1120 g_main_loop_unref (poll_data->loop);
1121 g_slice_free (GstBusPollData, poll_data);
1126 poll_destroy_timeout (GstBusPollData * poll_data)
1128 poll_data->timeout_id = 0;
1129 if (!poll_data->source_running) {
1130 g_main_loop_unref (poll_data->loop);
1131 g_slice_free (GstBusPollData, poll_data);
1138 * @events: a mask of #GstMessageType, representing the set of message types to
1139 * poll for (note special handling of extended message types below)
1140 * @timeout: the poll timeout, as a #GstClockTime, or #GST_CLOCK_TIME_NONE to poll
1143 * Poll the bus for messages. Will block while waiting for messages to come.
1144 * You can specify a maximum time to poll with the @timeout parameter. If
1145 * @timeout is negative, this function will block indefinitely.
1147 * All messages not in @events will be popped off the bus and will be ignored.
1148 * It is not possible to use message enums beyond #GST_MESSAGE_EXTENDED in the
1151 * Because poll is implemented using the "message" signal enabled by
1152 * gst_bus_add_signal_watch(), calling gst_bus_poll() will cause the "message"
1153 * signal to be emitted for every message that poll sees. Thus a "message"
1154 * signal handler will see the same messages that this function sees -- neither
1155 * will steal messages from the other.
1157 * This function will run a main loop from the default main context when
1160 * You should never use this function, since it is pure evil. This is
1161 * especially true for GUI applications based on Gtk+ or Qt, but also for any
1162 * other non-trivial application that uses the GLib main loop. As this function
1163 * runs a GLib main loop, any callback attached to the default GLib main
1164 * context may be invoked. This could be timeouts, GUI events, I/O events etc.;
1165 * even if gst_bus_poll() is called with a 0 timeout. Any of these callbacks
1166 * may do things you do not expect, e.g. destroy the main application window or
1167 * some other resource; change other application state; display a dialog and
1168 * run another main loop until the user clicks it away. In short, using this
1169 * function may add a lot of complexity to your code through unexpected
1170 * re-entrancy and unexpected changes to your application's state.
1172 * For 0 timeouts use gst_bus_pop_filtered() instead of this function; for
1173 * other short timeouts use gst_bus_timed_pop_filtered(); everything else is
1174 * better handled by setting up an asynchronous bus watch and doing things
1177 * Returns: (transfer full) (nullable): the message that was received,
1178 * or %NULL if the poll timed out. The message is taken from the
1179 * bus and needs to be unreffed with gst_message_unref() after
1183 gst_bus_poll (GstBus * bus, GstMessageType events, GstClockTime timeout)
1185 GstBusPollData *poll_data;
1189 g_return_val_if_fail (GST_IS_BUS (bus), NULL);
1191 poll_data = g_slice_new (GstBusPollData);
1192 poll_data->source_running = TRUE;
1193 poll_data->loop = g_main_loop_new (NULL, FALSE);
1194 poll_data->events = events;
1195 poll_data->message = NULL;
1197 if (timeout != GST_CLOCK_TIME_NONE)
1198 poll_data->timeout_id = g_timeout_add_full (G_PRIORITY_DEFAULT_IDLE,
1199 timeout / GST_MSECOND, (GSourceFunc) poll_timeout, poll_data,
1200 (GDestroyNotify) poll_destroy_timeout);
1202 poll_data->timeout_id = 0;
1204 id = g_signal_connect_data (bus, "message", G_CALLBACK (poll_func), poll_data,
1205 (GClosureNotify) poll_destroy, 0);
1207 /* these can be nested, so it's ok */
1208 gst_bus_add_signal_watch (bus);
1210 GST_DEBUG ("running mainloop %p", poll_data->loop);
1211 g_main_loop_run (poll_data->loop);
1212 GST_DEBUG ("mainloop stopped %p", poll_data->loop);
1214 gst_bus_remove_signal_watch (bus);
1217 ret = poll_data->message;
1219 if (poll_data->timeout_id)
1220 g_source_remove (poll_data->timeout_id);
1222 /* poll_data will be freed now */
1223 g_signal_handler_disconnect (bus, id);
1225 GST_DEBUG_OBJECT (bus, "finished poll with message %p", ret);
1231 * gst_bus_async_signal_func:
1233 * @message: the #GstMessage received
1236 * A helper #GstBusFunc that can be used to convert all asynchronous messages
1242 gst_bus_async_signal_func (GstBus * bus, GstMessage * message, gpointer data)
1246 g_return_val_if_fail (GST_IS_BUS (bus), TRUE);
1247 g_return_val_if_fail (message != NULL, TRUE);
1249 detail = gst_message_type_to_quark (GST_MESSAGE_TYPE (message));
1251 g_signal_emit (bus, gst_bus_signals[ASYNC_MESSAGE], detail, message);
1253 /* we never remove this source based on signal emission return values */
1258 * gst_bus_sync_signal_handler:
1260 * @message: the #GstMessage received
1263 * A helper GstBusSyncHandler that can be used to convert all synchronous
1264 * messages into signals.
1266 * Returns: GST_BUS_PASS
1269 gst_bus_sync_signal_handler (GstBus * bus, GstMessage * message, gpointer data)
1273 g_return_val_if_fail (GST_IS_BUS (bus), GST_BUS_DROP);
1274 g_return_val_if_fail (message != NULL, GST_BUS_DROP);
1276 detail = gst_message_type_to_quark (GST_MESSAGE_TYPE (message));
1278 g_signal_emit (bus, gst_bus_signals[SYNC_MESSAGE], detail, message);
1280 return GST_BUS_PASS;
1284 * gst_bus_enable_sync_message_emission:
1285 * @bus: a #GstBus on which you want to receive the "sync-message" signal
1287 * Instructs GStreamer to emit the "sync-message" signal after running the bus's
1288 * sync handler. This function is here so that code can ensure that they can
1289 * synchronously receive messages without having to affect what the bin's sync
1292 * This function may be called multiple times. To clean up, the caller is
1293 * responsible for calling gst_bus_disable_sync_message_emission() as many times
1294 * as this function is called.
1296 * While this function looks similar to gst_bus_add_signal_watch(), it is not
1297 * exactly the same -- this function enables <emphasis>synchronous</emphasis> emission of
1298 * signals when messages arrive; gst_bus_add_signal_watch() adds an idle callback
1299 * to pop messages off the bus <emphasis>asynchronously</emphasis>. The sync-message signal
1300 * comes from the thread of whatever object posted the message; the "message"
1301 * signal is marshalled to the main thread via the main loop.
1306 gst_bus_enable_sync_message_emission (GstBus * bus)
1308 g_return_if_fail (GST_IS_BUS (bus));
1310 GST_OBJECT_LOCK (bus);
1311 bus->priv->num_sync_message_emitters++;
1312 GST_OBJECT_UNLOCK (bus);
1316 * gst_bus_disable_sync_message_emission:
1317 * @bus: a #GstBus on which you previously called
1318 * gst_bus_enable_sync_message_emission()
1320 * Instructs GStreamer to stop emitting the "sync-message" signal for this bus.
1321 * See gst_bus_enable_sync_message_emission() for more information.
1323 * In the event that multiple pieces of code have called
1324 * gst_bus_enable_sync_message_emission(), the sync-message emissions will only
1325 * be stopped after all calls to gst_bus_enable_sync_message_emission() were
1326 * "cancelled" by calling this function. In this way the semantics are exactly
1327 * the same as gst_object_ref() that which calls enable should also call
1333 gst_bus_disable_sync_message_emission (GstBus * bus)
1335 g_return_if_fail (GST_IS_BUS (bus));
1336 g_return_if_fail (bus->priv->num_sync_message_emitters > 0);
1338 GST_OBJECT_LOCK (bus);
1339 bus->priv->num_sync_message_emitters--;
1340 GST_OBJECT_UNLOCK (bus);
1344 * gst_bus_add_signal_watch_full:
1345 * @bus: a #GstBus on which you want to receive the "message" signal
1346 * @priority: The priority of the watch.
1348 * Adds a bus signal watch to the default main context with the given @priority
1349 * (e.g. %G_PRIORITY_DEFAULT). It is also possible to use a non-default main
1350 * context set up using g_main_context_push_thread_default()
1351 * (before one had to create a bus watch source and attach it to the desired
1352 * main context 'manually').
1354 * After calling this statement, the bus will emit the "message" signal for each
1355 * message posted on the bus when the main loop is running.
1357 * This function may be called multiple times. To clean up, the caller is
1358 * responsible for calling gst_bus_remove_signal_watch() as many times as this
1359 * function is called.
1361 * There can only be a single bus watch per bus, you must remove any signal
1362 * watch before you can set another type of watch.
1367 gst_bus_add_signal_watch_full (GstBus * bus, gint priority)
1369 g_return_if_fail (GST_IS_BUS (bus));
1371 /* I know the callees don't take this lock, so go ahead and abuse it */
1372 GST_OBJECT_LOCK (bus);
1374 if (bus->priv->num_signal_watchers > 0)
1377 /* this should not fail because the counter above takes care of it */
1378 g_assert (!bus->priv->signal_watch);
1380 gst_bus_add_watch_full_unlocked (bus, priority, gst_bus_async_signal_func,
1383 if (G_UNLIKELY (!bus->priv->signal_watch))
1388 bus->priv->num_signal_watchers++;
1390 GST_OBJECT_UNLOCK (bus);
1396 g_critical ("Could not add signal watch to bus %s", GST_OBJECT_NAME (bus));
1397 GST_OBJECT_UNLOCK (bus);
1403 * gst_bus_add_signal_watch:
1404 * @bus: a #GstBus on which you want to receive the "message" signal
1406 * Adds a bus signal watch to the default main context with the default priority
1407 * (%G_PRIORITY_DEFAULT). It is also possible to use a non-default
1408 * main context set up using g_main_context_push_thread_default() (before
1409 * one had to create a bus watch source and attach it to the desired main
1410 * context 'manually').
1412 * After calling this statement, the bus will emit the "message" signal for each
1413 * message posted on the bus.
1415 * This function may be called multiple times. To clean up, the caller is
1416 * responsible for calling gst_bus_remove_signal_watch() as many times as this
1417 * function is called.
1422 gst_bus_add_signal_watch (GstBus * bus)
1424 gst_bus_add_signal_watch_full (bus, G_PRIORITY_DEFAULT);
1428 * gst_bus_remove_signal_watch:
1429 * @bus: a #GstBus you previously added a signal watch to
1431 * Removes a signal watch previously added with gst_bus_add_signal_watch().
1436 gst_bus_remove_signal_watch (GstBus * bus)
1438 GSource *source = NULL;
1440 g_return_if_fail (GST_IS_BUS (bus));
1442 /* I know the callees don't take this lock, so go ahead and abuse it */
1443 GST_OBJECT_LOCK (bus);
1445 if (bus->priv->num_signal_watchers == 0)
1448 bus->priv->num_signal_watchers--;
1450 if (bus->priv->num_signal_watchers > 0)
1453 GST_DEBUG_OBJECT (bus, "removing signal watch %u",
1454 g_source_get_id (bus->priv->signal_watch));
1456 source = bus->priv->signal_watch;
1459 GST_OBJECT_UNLOCK (bus);
1462 g_source_destroy (source);
1469 g_critical ("Bus %s has no signal watches attached", GST_OBJECT_NAME (bus));
1470 GST_OBJECT_UNLOCK (bus);