/** * gst_bus_pop_filtered: * @bus: a #GstBus to pop * @types: message types to take into account * * Get a message matching @type from the bus. Will discard all messages on * the bus that do not match @type and that have been posted before the first * message that does match @type. If there is no message matching @type on * the bus, all messages will be discarded. * * Returns: The next #GstMessage matching @type that is on the bus, or NULL if * the bus is empty or there is no message matching @type. * The message is taken from the bus and needs to be unreffed with * gst_message_unref() after usage. * * MT safe. * * Since: 0.10.15 */ GstMessage * gst_bus_pop_filtered (GstBus * bus, GstMessageType types) { g_return_val_if_fail (GST_IS_BUS (bus), NULL); g_return_val_if_fail (types != 0, NULL); return gst_bus_timed_pop_filtered (bus, 0, types); }
/** * gst_bus_get_pollfd: * @bus: A #GstBus * @fd: (out): A GPollFD to fill * * Gets the file descriptor from the bus which can be used to get notified about * messages being available with functions like g_poll(), and allows integration * into other event loops based on file descriptors. * Whenever a message is available, the POLLIN / %G_IO_IN event is set. * * Warning: NEVER read or write anything to the returned fd but only use it * for getting notifications via g_poll() or similar and then use the normal * GstBus API, e.g. gst_bus_pop(). * * Since: 1.14 */ void gst_bus_get_pollfd (GstBus * bus, GPollFD * fd) { g_return_if_fail (GST_IS_BUS (bus)); g_return_if_fail (bus->priv->poll != NULL); *fd = bus->priv->pollfd; }
/** * gst_bus_enable_sync_message_emission: * @bus: a #GstBus on which you want to receive the "sync-message" signal * * Instructs GStreamer to emit the "sync-message" signal after running the bus's * sync handler. This function is here so that code can ensure that they can * synchronously receive messages without having to affect what the bin's sync * handler is. * * This function may be called multiple times. To clean up, the caller is * responsible for calling gst_bus_disable_sync_message_emission() as many times * as this function is called. * * While this function looks similar to gst_bus_add_signal_watch(), it is not * exactly the same -- this function enables <emphasis>synchronous</emphasis> emission of * signals when messages arrive; gst_bus_add_signal_watch() adds an idle callback * to pop messages off the bus <emphasis>asynchronously</emphasis>. The sync-message signal * comes from the thread of whatever object posted the message; the "message" * signal is marshalled to the main thread via the main loop. * * MT safe. */ void gst_bus_enable_sync_message_emission (GstBus * bus) { g_return_if_fail (GST_IS_BUS (bus)); GST_OBJECT_LOCK (bus); bus->priv->num_sync_message_emitters++; GST_OBJECT_UNLOCK (bus); }
/** * gst_bus_disable_sync_message_emission: * @bus: a #GstBus on which you previously called * gst_bus_enable_sync_message_emission() * * Instructs GStreamer to stop emitting the "sync-message" signal for this bus. * See gst_bus_enable_sync_message_emission() for more information. * * In the event that multiple pieces of code have called * gst_bus_enable_sync_message_emission(), the sync-message emissions will only * be stopped after all calls to gst_bus_enable_sync_message_emission() were * "cancelled" by calling this function. In this way the semantics are exactly * the same as gst_object_ref() that which calls enable should also call * disable. * * MT safe. */ void gst_bus_disable_sync_message_emission (GstBus * bus) { g_return_if_fail (GST_IS_BUS (bus)); g_return_if_fail (bus->num_signal_watchers == 0); GST_OBJECT_LOCK (bus); bus->priv->num_sync_message_emitters--; GST_OBJECT_UNLOCK (bus); }
/** * gst_bus_have_pending: * @bus: a #GstBus to check * * Check if there are pending messages on the bus that * should be handled. * * Returns: %TRUE if there are messages on the bus to be handled, %FALSE * otherwise. * * MT safe. */ gboolean gst_bus_have_pending (GstBus * bus) { gboolean result; g_return_val_if_fail (GST_IS_BUS (bus), FALSE); /* see if there is a message on the bus */ result = gst_atomic_queue_length (bus->priv->queue) != 0; return result; }
/** * gst_bus_sync_signal_handler: * @bus: a #GstBus * @message: the #GstMessage received * @data: user data * * A helper GstBusSyncHandler that can be used to convert all synchronous * messages into signals. * * Returns: GST_BUS_PASS */ GstBusSyncReply gst_bus_sync_signal_handler (GstBus * bus, GstMessage * message, gpointer data) { GQuark detail = 0; g_return_val_if_fail (GST_IS_BUS (bus), GST_BUS_DROP); g_return_val_if_fail (message != NULL, GST_BUS_DROP); detail = gst_message_type_to_quark (GST_MESSAGE_TYPE (message)); g_signal_emit (bus, gst_bus_signals[SYNC_MESSAGE], detail, message); return GST_BUS_PASS; }
/** * gst_bus_add_watch_full: * @bus: a #GstBus to create the watch for. * @priority: The priority of the watch. * @func: A function to call when a message is received. * @user_data: user data passed to @func. * @notify: the function to call when the source is removed. * * Adds a bus watch to the default main context with the given @priority. * This function is used to receive asynchronous messages in the main loop. * There can only be a single bus watch per bus, you must remove it before you * can set a new one. * * When @func is called, the message belongs to the caller; if you want to * keep a copy of it, call gst_message_ref() before leaving @func. * * The watch can be removed using g_source_remove() or by returning FALSE * from @func. * * Returns: The event source id. * * MT safe. */ guint gst_bus_add_watch_full (GstBus * bus, gint priority, GstBusFunc func, gpointer user_data, GDestroyNotify notify) { guint id; g_return_val_if_fail (GST_IS_BUS (bus), 0); GST_OBJECT_LOCK (bus); id = gst_bus_add_watch_full_unlocked (bus, priority, func, user_data, notify); GST_OBJECT_UNLOCK (bus); return id; }
/** * gst_bus_create_watch: * @bus: a #GstBus to create the watch for * * Create watch for this bus. The GSource will be dispatched whenever * a message is on the bus. After the GSource is dispatched, the * message is popped off the bus and unreffed. * * Returns: A #GSource that can be added to a mainloop. */ GSource * gst_bus_create_watch (GstBus * bus) { GstBusSource *source; g_return_val_if_fail (GST_IS_BUS (bus), NULL); source = (GstBusSource *) g_source_new (&gst_bus_source_funcs, sizeof (GstBusSource)); source->bus = gst_object_ref (bus); source->inited = FALSE; return (GSource *) source; }
/** * gst_bus_have_pending: * @bus: a #GstBus to check * * Check if there are pending messages on the bus that * should be handled. * * Returns: TRUE if there are messages on the bus to be handled, FALSE * otherwise. * * MT safe. */ gboolean gst_bus_have_pending (GstBus * bus) { gboolean result; g_return_val_if_fail (GST_IS_BUS (bus), FALSE); g_mutex_lock (bus->queue_lock); /* see if there is a message on the bus */ result = !g_queue_is_empty (bus->queue); g_mutex_unlock (bus->queue_lock); return result; }
/** * gst_bus_async_signal_func: * @bus: a #GstBus * @message: the #GstMessage received * @data: user data * * A helper #GstBusFunc that can be used to convert all asynchronous messages * into signals. * * Returns: TRUE */ gboolean gst_bus_async_signal_func (GstBus * bus, GstMessage * message, gpointer data) { GQuark detail = 0; g_return_val_if_fail (GST_IS_BUS (bus), TRUE); g_return_val_if_fail (message != NULL, TRUE); detail = gst_message_type_to_quark (GST_MESSAGE_TYPE (message)); g_signal_emit (bus, gst_bus_signals[ASYNC_MESSAGE], detail, message); /* we never remove this source based on signal emission return values */ return TRUE; }
/** * gst_bus_poll: * @bus: a #GstBus * @events: a mask of #GstMessageType, representing the set of message types to * poll for (note special handling of extended message types below) * @timeout: the poll timeout, as a #GstClockTime, or #GST_CLOCK_TIME_NONE to poll * indefinitely. * * Poll the bus for messages. Will block while waiting for messages to come. * You can specify a maximum time to poll with the @timeout parameter. If * @timeout is negative, this function will block indefinitely. * * All messages not in @events will be popped off the bus and will be ignored. * It is not possible to use message enums beyond #GST_MESSAGE_EXTENDED in the * @events mask * * Because poll is implemented using the "message" signal enabled by * gst_bus_add_signal_watch(), calling gst_bus_poll() will cause the "message" * signal to be emitted for every message that poll sees. Thus a "message" * signal handler will see the same messages that this function sees -- neither * will steal messages from the other. * * This function will run a main loop from the default main context when * polling. * * You should never use this function, since it is pure evil. This is * especially true for GUI applications based on Gtk+ or Qt, but also for any * other non-trivial application that uses the GLib main loop. As this function * runs a GLib main loop, any callback attached to the default GLib main * context may be invoked. This could be timeouts, GUI events, I/O events etc.; * even if gst_bus_poll() is called with a 0 timeout. Any of these callbacks * may do things you do not expect, e.g. destroy the main application window or * some other resource; change other application state; display a dialog and * run another main loop until the user clicks it away. In short, using this * function may add a lot of complexity to your code through unexpected * re-entrancy and unexpected changes to your application's state. * * For 0 timeouts use gst_bus_pop_filtered() instead of this function; for * other short timeouts use gst_bus_timed_pop_filtered(); everything else is * better handled by setting up an asynchronous bus watch and doing things * from there. * * Returns: (transfer full) (nullable): the message that was received, * or %NULL if the poll timed out. The message is taken from the * bus and needs to be unreffed with gst_message_unref() after * usage. */ GstMessage * gst_bus_poll (GstBus * bus, GstMessageType events, GstClockTime timeout) { GstBusPollData *poll_data; GstMessage *ret; gulong id; g_return_val_if_fail (GST_IS_BUS (bus), NULL); poll_data = g_slice_new (GstBusPollData); poll_data->source_running = TRUE; poll_data->loop = g_main_loop_new (NULL, FALSE); poll_data->events = events; poll_data->message = NULL; if (timeout != GST_CLOCK_TIME_NONE) poll_data->timeout_id = g_timeout_add_full (G_PRIORITY_DEFAULT_IDLE, timeout / GST_MSECOND, (GSourceFunc) poll_timeout, poll_data, (GDestroyNotify) poll_destroy_timeout); else poll_data->timeout_id = 0; id = g_signal_connect_data (bus, "message", G_CALLBACK (poll_func), poll_data, (GClosureNotify) poll_destroy, 0); /* these can be nested, so it's ok */ gst_bus_add_signal_watch (bus); GST_DEBUG ("running mainloop %p", poll_data->loop); g_main_loop_run (poll_data->loop); GST_DEBUG ("mainloop stopped %p", poll_data->loop); gst_bus_remove_signal_watch (bus); /* holds a ref */ ret = poll_data->message; if (poll_data->timeout_id) g_source_remove (poll_data->timeout_id); /* poll_data will be freed now */ g_signal_handler_disconnect (bus, id); GST_DEBUG_OBJECT (bus, "finished poll with message %p", ret); return ret; }
/** * gst_bus_peek: * @bus: a #GstBus * * Peek the message on the top of the bus' queue. The message will remain * on the bus' message queue. A reference is returned, and needs to be unreffed * by the caller. * * Returns: The #GstMessage that is on the bus, or NULL if the bus is empty. * * MT safe. */ GstMessage * gst_bus_peek (GstBus * bus) { GstMessage *message; g_return_val_if_fail (GST_IS_BUS (bus), NULL); g_mutex_lock (bus->queue_lock); message = g_queue_peek_head (bus->queue); if (message) gst_message_ref (message); g_mutex_unlock (bus->queue_lock); GST_DEBUG_OBJECT (bus, "peek on bus, got message %p", message); return message; }
/** * gst_bus_create_watch: * @bus: a #GstBus to create the watch for * * Create watch for this bus. The GSource will be dispatched whenever * a message is on the bus. After the GSource is dispatched, the * message is popped off the bus and unreffed. * * Returns: (transfer full) (nullable): a #GSource that can be added to a mainloop. */ GSource * gst_bus_create_watch (GstBus * bus) { GstBusSource *source; g_return_val_if_fail (GST_IS_BUS (bus), NULL); g_return_val_if_fail (bus->priv->poll != NULL, NULL); source = (GstBusSource *) g_source_new (&gst_bus_source_funcs, sizeof (GstBusSource)); g_source_set_name ((GSource *) source, "GStreamer message bus watch"); source->bus = gst_object_ref (bus); g_source_add_poll ((GSource *) source, &bus->priv->pollfd); return (GSource *) source; }
static gboolean gst_bus_source_dispatch (GSource * source, GSourceFunc callback, gpointer user_data) { GstBusFunc handler = (GstBusFunc) callback; GstBusSource *bsource = (GstBusSource *) source; GstMessage *message; gboolean keep; GstBus *bus; g_return_val_if_fail (bsource != NULL, FALSE); bus = bsource->bus; g_return_val_if_fail (GST_IS_BUS (bus), FALSE); message = gst_bus_pop (bus); /* The message queue might be empty if some other thread or callback set * the bus to flushing between check/prepare and dispatch */ if (G_UNLIKELY (message == NULL)) return TRUE; if (!handler) goto no_handler; GST_DEBUG_OBJECT (bus, "source %p calling dispatch with %" GST_PTR_FORMAT, source, message); keep = handler (bus, message, user_data); gst_message_unref (message); GST_DEBUG_OBJECT (bus, "source %p handler returns %d", source, keep); return keep; no_handler: { g_warning ("GstBus watch dispatched without callback\n" "You must call g_source_set_callback()."); gst_message_unref (message); return FALSE; } }
/** * gst_bus_remove_signal_watch: * @bus: a #GstBus you previously added a signal watch to * * Removes a signal watch previously added with gst_bus_add_signal_watch(). * * MT safe. */ void gst_bus_remove_signal_watch (GstBus * bus) { guint id = 0; g_return_if_fail (GST_IS_BUS (bus)); /* I know the callees don't take this lock, so go ahead and abuse it */ GST_OBJECT_LOCK (bus); if (bus->num_signal_watchers == 0) goto error; bus->num_signal_watchers--; if (bus->num_signal_watchers > 0) goto done; id = bus->signal_watch_id; bus->signal_watch_id = 0; GST_DEBUG_OBJECT (bus, "removing signal watch %u", id); done: GST_OBJECT_UNLOCK (bus); if (id) g_source_remove (id); return; /* ERRORS */ error: { g_critical ("Bus %s has no signal watches attached", GST_OBJECT_NAME (bus)); GST_OBJECT_UNLOCK (bus); return; } }
/** * gst_bus_remove_signal_watch: * @bus: a #GstBus you previously added a signal watch to * * Removes a signal watch previously added with gst_bus_add_signal_watch(). * * MT safe. */ void gst_bus_remove_signal_watch (GstBus * bus) { GSource *source = NULL; g_return_if_fail (GST_IS_BUS (bus)); /* I know the callees don't take this lock, so go ahead and abuse it */ GST_OBJECT_LOCK (bus); if (bus->priv->num_signal_watchers == 0) goto error; bus->priv->num_signal_watchers--; if (bus->priv->num_signal_watchers > 0) goto done; GST_DEBUG_OBJECT (bus, "removing signal watch %u", g_source_get_id (bus->priv->signal_watch)); source = bus->priv->signal_watch; done: GST_OBJECT_UNLOCK (bus); if (source) g_source_destroy (source); return; /* ERRORS */ error: { g_critical ("Bus %s has no signal watches attached", GST_OBJECT_NAME (bus)); GST_OBJECT_UNLOCK (bus); return; } }
/** * gst_bus_set_sync_handler: * @bus: a #GstBus to install the handler on * @func: (allow-none): The handler function to install * @user_data: User data that will be sent to the handler function. * @notify: called when @user_data becomes unused * * Sets the synchronous handler on the bus. The function will be called * every time a new message is posted on the bus. Note that the function * will be called in the same thread context as the posting object. This * function is usually only called by the creator of the bus. Applications * should handle messages asynchronously using the gst_bus watch and poll * functions. * * You cannot replace an existing sync_handler. You can pass %NULL to this * function, which will clear the existing handler. */ void gst_bus_set_sync_handler (GstBus * bus, GstBusSyncHandler func, gpointer user_data, GDestroyNotify notify) { GDestroyNotify old_notify; g_return_if_fail (GST_IS_BUS (bus)); GST_OBJECT_LOCK (bus); /* Assert if the user attempts to replace an existing sync_handler, * other than to clear it */ if (func != NULL && bus->priv->sync_handler != NULL) goto no_replace; if ((old_notify = bus->priv->sync_handler_notify)) { gpointer old_data = bus->priv->sync_handler_data; bus->priv->sync_handler_data = NULL; bus->priv->sync_handler_notify = NULL; GST_OBJECT_UNLOCK (bus); old_notify (old_data); GST_OBJECT_LOCK (bus); } bus->priv->sync_handler = func; bus->priv->sync_handler_data = user_data; bus->priv->sync_handler_notify = notify; GST_OBJECT_UNLOCK (bus); return; no_replace: { GST_OBJECT_UNLOCK (bus); g_warning ("cannot replace existing sync handler"); return; } }
/** * gst_bus_add_signal_watch_full: * @bus: a #GstBus on which you want to receive the "message" signal * @priority: The priority of the watch. * * Adds a bus signal watch to the default main context with the given priority. * After calling this statement, the bus will emit the "message" signal for each * message posted on the bus when the main loop is running. * * This function may be called multiple times. To clean up, the caller is * responsible for calling gst_bus_remove_signal_watch() as many times as this * function is called. * * There can only be a single bus watch per bus, you most remove all signal watch * before you can set another type of watch. * * MT safe. */ void gst_bus_add_signal_watch_full (GstBus * bus, gint priority) { g_return_if_fail (GST_IS_BUS (bus)); /* I know the callees don't take this lock, so go ahead and abuse it */ GST_OBJECT_LOCK (bus); if (bus->num_signal_watchers > 0) goto done; /* this should not fail because the counter above takes care of it */ g_assert (bus->signal_watch_id == 0); bus->signal_watch_id = gst_bus_add_watch_full_unlocked (bus, priority, gst_bus_async_signal_func, NULL, NULL); if (G_UNLIKELY (bus->signal_watch_id == 0)) goto add_failed; done: bus->num_signal_watchers++; GST_OBJECT_UNLOCK (bus); return; /* ERRORS */ add_failed: { g_critical ("Could not add signal watch to bus %s", GST_OBJECT_NAME (bus)); GST_OBJECT_UNLOCK (bus); return; } }
/** * gst_bus_timed_pop_filtered: * @bus: a #GstBus to pop from * @timeout: a timeout in nanoseconds, or GST_CLOCK_TIME_NONE to wait forever * @types: message types to take into account, GST_MESSAGE_ANY for any type * * Get a message from the bus whose type matches the message type mask @types, * waiting up to the specified timeout (and discarding any messages that do not * match the mask provided). * * If @timeout is 0, this function behaves like gst_bus_pop_filtered(). If * @timeout is #GST_CLOCK_TIME_NONE, this function will block forever until a * matching message was posted on the bus. * * Returns: (transfer full) (nullable): a #GstMessage matching the * filter in @types, or %NULL if no matching message was found on * the bus until the timeout expired. The message is taken from * the bus and needs to be unreffed with gst_message_unref() after * usage. * * MT safe. */ GstMessage * gst_bus_timed_pop_filtered (GstBus * bus, GstClockTime timeout, GstMessageType types) { GstMessage *message; GTimeVal now, then; gboolean first_round = TRUE; GstClockTime elapsed = 0; g_return_val_if_fail (GST_IS_BUS (bus), NULL); g_return_val_if_fail (types != 0, NULL); g_return_val_if_fail (timeout == 0 || bus->priv->poll != NULL, NULL); g_mutex_lock (&bus->priv->queue_lock); while (TRUE) { gint ret; GST_LOG_OBJECT (bus, "have %d messages", gst_atomic_queue_length (bus->priv->queue)); while ((message = gst_atomic_queue_pop (bus->priv->queue))) { if (bus->priv->poll) gst_poll_read_control (bus->priv->poll); GST_DEBUG_OBJECT (bus, "got message %p, %s from %s, type mask is %u", message, GST_MESSAGE_TYPE_NAME (message), GST_MESSAGE_SRC_NAME (message), (guint) types); if ((GST_MESSAGE_TYPE (message) & types) != 0) { /* Extra check to ensure extended types don't get matched unless * asked for */ if ((!GST_MESSAGE_TYPE_IS_EXTENDED (message)) || (types & GST_MESSAGE_EXTENDED)) { /* exit the loop, we have a message */ goto beach; } } GST_DEBUG_OBJECT (bus, "discarding message, does not match mask"); gst_message_unref (message); message = NULL; } /* no need to wait, exit loop */ if (timeout == 0) break; else if (timeout != GST_CLOCK_TIME_NONE) { if (first_round) { g_get_current_time (&then); first_round = FALSE; } else { g_get_current_time (&now); elapsed = GST_TIMEVAL_TO_TIME (now) - GST_TIMEVAL_TO_TIME (then); if (elapsed > timeout) break; } } /* only here in timeout case */ g_assert (bus->priv->poll); g_mutex_unlock (&bus->priv->queue_lock); ret = gst_poll_wait (bus->priv->poll, timeout - elapsed); g_mutex_lock (&bus->priv->queue_lock); if (ret == 0) { GST_INFO_OBJECT (bus, "timed out, breaking loop"); break; } else { GST_INFO_OBJECT (bus, "we got woken up, recheck for message"); } } beach: g_mutex_unlock (&bus->priv->queue_lock); return message; }
/* FIXME: Copy from owr/orw.c without any error handling whatsoever */ static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer user_data) { gboolean ret, is_warning = FALSE; GstStateChangeReturn change_status; gchar *message_type, *debug; GError *error; OwrMediaSource *media_source = user_data; GstElement *pipeline; g_return_val_if_fail(GST_IS_BUS(bus), TRUE); (void)user_data; switch (GST_MESSAGE_TYPE(msg)) { case GST_MESSAGE_LATENCY: pipeline = _owr_media_source_get_source_bin(media_source); g_return_val_if_fail(pipeline, TRUE); ret = gst_bin_recalculate_latency(GST_BIN(pipeline)); g_warn_if_fail(ret); g_object_unref(pipeline); break; case GST_MESSAGE_CLOCK_LOST: pipeline = _owr_media_source_get_source_bin(media_source); g_return_val_if_fail(pipeline, TRUE); change_status = gst_element_set_state(pipeline, GST_STATE_PAUSED); g_warn_if_fail(change_status != GST_STATE_CHANGE_FAILURE); change_status = gst_element_set_state(pipeline, GST_STATE_PLAYING); g_warn_if_fail(change_status != GST_STATE_CHANGE_FAILURE); g_object_unref(pipeline); break; case GST_MESSAGE_EOS: g_print("End of stream\n"); break; case GST_MESSAGE_WARNING: is_warning = TRUE; case GST_MESSAGE_ERROR: if (is_warning) { message_type = "Warning"; gst_message_parse_warning(msg, &error, &debug); } else { message_type = "Error"; gst_message_parse_error(msg, &error, &debug); } g_printerr("==== %s message start ====\n", message_type); g_printerr("%s in element %s.\n", message_type, GST_OBJECT_NAME(msg->src)); g_printerr("%s: %s\n", message_type, error->message); g_printerr("Debugging info: %s\n", (debug) ? debug : "none"); g_printerr("==== %s message stop ====\n", message_type); /*GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "pipeline.dot");*/ if (!is_warning) { OWR_POST_ERROR(media_source, PROCESSING_ERROR, NULL); } g_error_free(error); g_free(debug); break; default: break; } return TRUE; }
/** * gst_bus_timed_pop_filtered: * @bus: a #GstBus to pop from * @timeout: a timeout in nanoseconds, or GST_CLOCK_TIME_NONE to wait forever * @types: message types to take into account, GST_MESSAGE_ANY for any type * * Get a message from the bus whose type matches the message type mask @types, * waiting up to the specified timeout (and discarding any messages that do not * match the mask provided). * * If @timeout is 0, this function behaves like gst_bus_pop_filtered(). If * @timeout is #GST_CLOCK_TIME_NONE, this function will block forever until a * matching message was posted on the bus. * * Returns: (transfer full) (nullable): a #GstMessage matching the * filter in @types, or %NULL if no matching message was found on * the bus until the timeout expired. The message is taken from * the bus and needs to be unreffed with gst_message_unref() after * usage. * * MT safe. */ GstMessage * gst_bus_timed_pop_filtered (GstBus * bus, GstClockTime timeout, GstMessageType types) { GstMessage *message; GTimeVal now, then; gboolean first_round = TRUE; GstClockTime elapsed = 0; g_return_val_if_fail (GST_IS_BUS (bus), NULL); g_return_val_if_fail (types != 0, NULL); g_return_val_if_fail (timeout == 0 || bus->priv->poll != NULL, NULL); g_mutex_lock (&bus->priv->queue_lock); while (TRUE) { gint ret; GST_LOG_OBJECT (bus, "have %d messages", gst_atomic_queue_length (bus->priv->queue)); while ((message = gst_atomic_queue_pop (bus->priv->queue))) { if (bus->priv->poll) { while (!gst_poll_read_control (bus->priv->poll)) { if (errno == EWOULDBLOCK) { /* Retry, this can happen if pushing to the queue has finished, * popping here succeeded but writing control did not finish * before we got to this line. */ /* Give other threads the chance to do something */ g_thread_yield (); continue; } else { /* This is a real error and means that either the bus is in an * inconsistent state, or the GstPoll is invalid. GstPoll already * prints a critical warning about this, no need to do that again * ourselves */ break; } } } GST_DEBUG_OBJECT (bus, "got message %p, %s from %s, type mask is %u", message, GST_MESSAGE_TYPE_NAME (message), GST_MESSAGE_SRC_NAME (message), (guint) types); if ((GST_MESSAGE_TYPE (message) & types) != 0) { /* Extra check to ensure extended types don't get matched unless * asked for */ if ((!GST_MESSAGE_TYPE_IS_EXTENDED (message)) || (types & GST_MESSAGE_EXTENDED)) { /* exit the loop, we have a message */ goto beach; } } GST_DEBUG_OBJECT (bus, "discarding message, does not match mask"); gst_message_unref (message); message = NULL; } /* no need to wait, exit loop */ if (timeout == 0) break; else if (timeout != GST_CLOCK_TIME_NONE) { if (first_round) { g_get_current_time (&then); first_round = FALSE; } else { g_get_current_time (&now); elapsed = GST_TIMEVAL_TO_TIME (now) - GST_TIMEVAL_TO_TIME (then); if (elapsed > timeout) break; } } /* only here in timeout case */ g_assert (bus->priv->poll); g_mutex_unlock (&bus->priv->queue_lock); ret = gst_poll_wait (bus->priv->poll, timeout - elapsed); g_mutex_lock (&bus->priv->queue_lock); if (ret == 0) { GST_INFO_OBJECT (bus, "timed out, breaking loop"); break; } else { GST_INFO_OBJECT (bus, "we got woken up, recheck for message"); } } beach: g_mutex_unlock (&bus->priv->queue_lock); return message; }
/** * gst_bus_post: * @bus: a #GstBus to post on * @message: (transfer full): the #GstMessage to post * * Post a message on the given bus. Ownership of the message * is taken by the bus. * * Returns: %TRUE if the message could be posted, %FALSE if the bus is flushing. * * MT safe. */ gboolean gst_bus_post (GstBus * bus, GstMessage * message) { GstBusSyncReply reply = GST_BUS_PASS; GstBusSyncHandler handler; gboolean emit_sync_message; gpointer handler_data; g_return_val_if_fail (GST_IS_BUS (bus), FALSE); g_return_val_if_fail (GST_IS_MESSAGE (message), FALSE); GST_DEBUG_OBJECT (bus, "[msg %p] posting on bus %" GST_PTR_FORMAT, message, message); /* check we didn't accidentally add a public flag that maps to same value */ g_assert (!GST_MINI_OBJECT_FLAG_IS_SET (message, GST_MESSAGE_FLAG_ASYNC_DELIVERY)); GST_OBJECT_LOCK (bus); /* check if the bus is flushing */ if (GST_OBJECT_FLAG_IS_SET (bus, GST_BUS_FLUSHING)) goto is_flushing; handler = bus->priv->sync_handler; handler_data = bus->priv->sync_handler_data; emit_sync_message = bus->priv->num_sync_message_emitters > 0; GST_OBJECT_UNLOCK (bus); /* first call the sync handler if it is installed */ if (handler) reply = handler (bus, message, handler_data); /* emit sync-message if requested to do so via gst_bus_enable_sync_message_emission. terrible but effective */ if (emit_sync_message && reply != GST_BUS_DROP && handler != gst_bus_sync_signal_handler) gst_bus_sync_signal_handler (bus, message, NULL); /* If this is a bus without async message delivery * always drop the message */ if (!bus->priv->poll) reply = GST_BUS_DROP; /* now see what we should do with the message */ switch (reply) { case GST_BUS_DROP: /* drop the message */ GST_DEBUG_OBJECT (bus, "[msg %p] dropped", message); break; case GST_BUS_PASS: /* pass the message to the async queue, refcount passed in the queue */ GST_DEBUG_OBJECT (bus, "[msg %p] pushing on async queue", message); gst_atomic_queue_push (bus->priv->queue, message); gst_poll_write_control (bus->priv->poll); GST_DEBUG_OBJECT (bus, "[msg %p] pushed on async queue", message); break; case GST_BUS_ASYNC: { /* async delivery, we need a mutex and a cond to block * on */ GCond *cond = GST_MESSAGE_GET_COND (message); GMutex *lock = GST_MESSAGE_GET_LOCK (message); g_cond_init (cond); g_mutex_init (lock); GST_MINI_OBJECT_FLAG_SET (message, GST_MESSAGE_FLAG_ASYNC_DELIVERY); GST_DEBUG_OBJECT (bus, "[msg %p] waiting for async delivery", message); /* now we lock the message mutex, send the message to the async * queue. When the message is handled by the app and destroyed, * the cond will be signalled and we can continue */ g_mutex_lock (lock); gst_atomic_queue_push (bus->priv->queue, message); gst_poll_write_control (bus->priv->poll); /* now block till the message is freed */ g_cond_wait (cond, lock); /* we acquired a new ref from gst_message_dispose() so we can clean up */ g_mutex_unlock (lock); GST_DEBUG_OBJECT (bus, "[msg %p] delivered asynchronously", message); GST_MINI_OBJECT_FLAG_UNSET (message, GST_MESSAGE_FLAG_ASYNC_DELIVERY); g_mutex_clear (lock); g_cond_clear (cond); gst_message_unref (message); break; } default: g_warning ("invalid return from bus sync handler"); break; } return TRUE; /* ERRORS */ is_flushing: { GST_DEBUG_OBJECT (bus, "bus is flushing"); GST_OBJECT_UNLOCK (bus); gst_message_unref (message); return FALSE; } }
/** * gst_bus_timed_pop_filtered: * @bus: a #GstBus to pop from * @timeout: a timeout in nanoseconds, or GST_CLOCK_TIME_NONE to wait forever * @types: message types to take into account, GST_MESSAGE_ANY for any type * * Get a message from the bus whose type matches the message type mask @types, * waiting up to the specified timeout (and discarding any messages that do not * match the mask provided). * * If @timeout is 0, this function behaves like gst_bus_pop_filtered(). If * @timeout is #GST_CLOCK_TIME_NONE, this function will block forever until a * matching message was posted on the bus. * * Returns: a #GstMessage matching the filter in @types, or NULL if no matching * message was found on the bus until the timeout expired. * The message is taken from the bus and needs to be unreffed with * gst_message_unref() after usage. * * MT safe. * * Since: 0.10.15 */ GstMessage * gst_bus_timed_pop_filtered (GstBus * bus, GstClockTime timeout, GstMessageType types) { GstMessage *message; GTimeVal *timeval, abstimeout; gboolean first_round = TRUE; g_return_val_if_fail (GST_IS_BUS (bus), NULL); g_return_val_if_fail (types != 0, NULL); g_mutex_lock (bus->queue_lock); while (TRUE) { GST_LOG_OBJECT (bus, "have %d messages", g_queue_get_length (bus->queue)); while ((message = g_queue_pop_head (bus->queue))) { GST_DEBUG_OBJECT (bus, "got message %p, %s, type mask is %u", message, GST_MESSAGE_TYPE_NAME (message), (guint) types); if ((GST_MESSAGE_TYPE (message) & types) != 0) { /* exit the loop, we have a message */ goto beach; } else { GST_DEBUG_OBJECT (bus, "discarding message, does not match mask"); gst_message_unref (message); message = NULL; } } /* no need to wait, exit loop */ if (timeout == 0) break; if (timeout == GST_CLOCK_TIME_NONE) { /* wait forever */ timeval = NULL; } else if (first_round) { glong add = timeout / 1000; if (add == 0) /* no need to wait */ break; /* make timeout absolute */ g_get_current_time (&abstimeout); g_time_val_add (&abstimeout, add); timeval = &abstimeout; first_round = FALSE; GST_DEBUG_OBJECT (bus, "blocking for message, timeout %ld", add); } else { /* calculated the absolute end time already, no need to do it again */ GST_DEBUG_OBJECT (bus, "blocking for message, again"); timeval = &abstimeout; /* fool compiler */ } if (!g_cond_timed_wait (bus->priv->queue_cond, bus->queue_lock, timeval)) { GST_INFO_OBJECT (bus, "timed out, breaking loop"); break; } else { GST_INFO_OBJECT (bus, "we got woken up, recheck for message"); } } beach: g_mutex_unlock (bus->queue_lock); return message; }
/** * gst_bus_post: * @bus: a #GstBus to post on * @message: The #GstMessage to post * * Post a message on the given bus. Ownership of the message * is taken by the bus. * * Returns: TRUE if the message could be posted, FALSE if the bus is flushing. * * MT safe. */ gboolean gst_bus_post (GstBus * bus, GstMessage * message) { GstBusSyncReply reply = GST_BUS_PASS; GstBusSyncHandler handler; gboolean emit_sync_message; gpointer handler_data; g_return_val_if_fail (GST_IS_BUS (bus), FALSE); g_return_val_if_fail (GST_IS_MESSAGE (message), FALSE); GST_DEBUG_OBJECT (bus, "[msg %p] posting on bus, type %s, %" GST_PTR_FORMAT " from source %" GST_PTR_FORMAT, message, GST_MESSAGE_TYPE_NAME (message), message->structure, message->src); GST_OBJECT_LOCK (bus); /* check if the bus is flushing */ if (GST_OBJECT_FLAG_IS_SET (bus, GST_BUS_FLUSHING)) goto is_flushing; handler = bus->sync_handler; handler_data = bus->sync_handler_data; emit_sync_message = bus->priv->num_sync_message_emitters > 0; GST_OBJECT_UNLOCK (bus); /* first call the sync handler if it is installed */ if (handler) reply = handler (bus, message, handler_data); /* emit sync-message if requested to do so via gst_bus_enable_sync_message_emission. terrible but effective */ if (emit_sync_message && reply != GST_BUS_DROP && handler != gst_bus_sync_signal_handler) gst_bus_sync_signal_handler (bus, message, NULL); /* now see what we should do with the message */ switch (reply) { case GST_BUS_DROP: /* drop the message */ GST_DEBUG_OBJECT (bus, "[msg %p] dropped", message); break; case GST_BUS_PASS: /* pass the message to the async queue, refcount passed in the queue */ GST_DEBUG_OBJECT (bus, "[msg %p] pushing on async queue", message); g_mutex_lock (bus->queue_lock); g_queue_push_tail (bus->queue, message); g_cond_broadcast (bus->priv->queue_cond); g_mutex_unlock (bus->queue_lock); GST_DEBUG_OBJECT (bus, "[msg %p] pushed on async queue", message); gst_bus_wakeup_main_context (bus); break; case GST_BUS_ASYNC: { /* async delivery, we need a mutex and a cond to block * on */ GMutex *lock = g_mutex_new (); GCond *cond = g_cond_new (); GST_MESSAGE_COND (message) = cond; GST_MESSAGE_GET_LOCK (message) = lock; GST_DEBUG_OBJECT (bus, "[msg %p] waiting for async delivery", message); /* now we lock the message mutex, send the message to the async * queue. When the message is handled by the app and destroyed, * the cond will be signalled and we can continue */ g_mutex_lock (lock); g_mutex_lock (bus->queue_lock); g_queue_push_tail (bus->queue, message); g_cond_broadcast (bus->priv->queue_cond); g_mutex_unlock (bus->queue_lock); gst_bus_wakeup_main_context (bus); /* now block till the message is freed */ g_cond_wait (cond, lock); g_mutex_unlock (lock); GST_DEBUG_OBJECT (bus, "[msg %p] delivered asynchronously", message); g_mutex_free (lock); g_cond_free (cond); break; } default: g_warning ("invalid return from bus sync handler"); break; } return TRUE; /* ERRORS */ is_flushing: { GST_DEBUG_OBJECT (bus, "bus is flushing"); gst_message_unref (message); GST_OBJECT_UNLOCK (bus); return FALSE; } }