3 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
4 * 2004 Wim Taymans <wim.taymans@gmail.com>
6 * gstbin.c: GstBin container object and support code
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
28 * @short_description: Base class and element that can contain other elements
30 * #GstBin is an element that can contain other #GstElement, allowing them to be
32 * Pads from the child elements can be ghosted to the bin, see #GstGhostPad.
33 * This makes the bin look like any other elements and enables creation of
34 * higher-level abstraction elements.
36 * A new #GstBin is created with gst_bin_new(). Use a #GstPipeline instead if you
37 * want to create a toplevel bin because a normal bin doesn't have a bus or
38 * handle clock distribution of its own.
40 * After the bin has been created you will typically add elements to it with
41 * gst_bin_add(). You can remove elements with gst_bin_remove().
43 * An element can be retrieved from a bin with gst_bin_get_by_name(), using the
44 * elements name. gst_bin_get_by_name_recurse_up() is mainly used for internal
45 * purposes and will query the parent bins when the element is not found in the
48 * An iterator of elements in a bin can be retrieved with
49 * gst_bin_iterate_elements(). Various other iterators exist to retrieve the
52 * gst_object_unref() is used to drop your reference to the bin.
54 * The #GstBin::element-added signal is fired whenever a new element is added to
55 * the bin. Likewise the #GstBin::element-removed signal is fired whenever an
56 * element is removed from the bin.
58 * <refsect2><title>Notes</title>
60 * A #GstBin internally intercepts every #GstMessage posted by its children and
61 * implements the following default behaviour for each of them:
64 * <term>GST_MESSAGE_EOS</term>
65 * <listitem><para>This message is only posted by sinks in the PLAYING
66 * state. If all sinks posted the EOS message, this bin will post and EOS
67 * message upwards.</para></listitem>
70 * <term>GST_MESSAGE_SEGMENT_START</term>
71 * <listitem><para>just collected and never forwarded upwards.
72 * The messages are used to decide when all elements have completed playback
73 * of their segment.</para></listitem>
76 * <term>GST_MESSAGE_SEGMENT_DONE</term>
77 * <listitem><para> Is posted by #GstBin when all elements that posted
78 * a SEGMENT_START have posted a SEGMENT_DONE.</para></listitem>
81 * <term>GST_MESSAGE_DURATION_CHANGED</term>
82 * <listitem><para> Is posted by an element that detected a change
83 * in the stream duration. The default bin behaviour is to clear any
84 * cached duration values so that the next duration query will perform
85 * a full duration recalculation. The duration change is posted to the
86 * application so that it can refetch the new duration with a duration
87 * query. Note that these messages can be posted before the bin is
88 * prerolled, in which case the duration query might fail.
92 * <term>GST_MESSAGE_CLOCK_LOST</term>
93 * <listitem><para> This message is posted by an element when it
94 * can no longer provide a clock. The default bin behaviour is to
95 * check if the lost clock was the one provided by the bin. If so and
96 * the bin is currently in the PLAYING state, the message is forwarded to
98 * This message is also generated when a clock provider is removed from
99 * the bin. If this message is received by the application, it should
100 * PAUSE the pipeline and set it back to PLAYING to force a new clock
105 * <term>GST_MESSAGE_CLOCK_PROVIDE</term>
106 * <listitem><para> This message is generated when an element
107 * can provide a clock. This mostly happens when a new clock
108 * provider is added to the bin. The default behaviour of the bin is to
109 * mark the currently selected clock as dirty, which will perform a clock
110 * recalculation the next time the bin is asked to provide a clock.
111 * This message is never sent tot the application but is forwarded to
112 * the parent of the bin.
116 * <term>OTHERS</term>
117 * <listitem><para> posted upwards.</para></listitem>
122 * A #GstBin implements the following default behaviour for answering to a
126 * <term>GST_QUERY_DURATION</term>
127 * <listitem><para>If the query has been asked before with the same format
128 * and the bin is a toplevel bin (ie. has no parent),
129 * use the cached previous value. If no previous value was cached, the
130 * query is sent to all sink elements in the bin and the MAXIMUM of all
131 * values is returned. If the bin is a toplevel bin the value is cached.
132 * If no sinks are available in the bin, the query fails.
136 * <term>GST_QUERY_POSITION</term>
137 * <listitem><para>The query is sent to all sink elements in the bin and the
138 * MAXIMUM of all values is returned. If no sinks are available in the bin,
143 * <term>OTHERS</term>
144 * <listitem><para>the query is forwarded to all sink elements, the result
145 * of the first sink that answers the query successfully is returned. If no
146 * sink is in the bin, the query fails.</para></listitem>
150 * A #GstBin will by default forward any event sent to it to all sink elements.
151 * If all the sinks return %TRUE, the bin will also return %TRUE, else %FALSE is
152 * returned. If no sinks are in the bin, the event handler will return %TRUE.
158 #include "gst_private.h"
160 #include "gstevent.h"
163 #include "gsterror.h"
165 #include "gstutils.h"
166 #include "gstchildproxy.h"
168 GST_DEBUG_CATEGORY_STATIC (bin_debug);
169 #define GST_CAT_DEFAULT bin_debug
171 /* a bin is toplevel if it has no parent or when it is configured to behave like
173 #define BIN_IS_TOPLEVEL(bin) ((GST_OBJECT_PARENT (bin) == NULL) || bin->priv->asynchandling)
175 #define GST_BIN_GET_PRIVATE(obj) \
176 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BIN, GstBinPrivate))
178 struct _GstBinPrivate
180 gboolean asynchandling;
181 /* if we get an ASYNC_DONE message from ourselves, this means that the
182 * subclass will simulate ASYNC behaviour without having ASYNC children. When
183 * such an ASYNC_DONE message is posted while we are doing a state change, we
184 * have to process the message after finishing the state change even when no
185 * child returned GST_STATE_CHANGE_ASYNC. */
186 gboolean pending_async_done;
188 guint32 structure_cookie;
195 /* forward messages from our children */
196 gboolean message_forward;
199 gboolean posted_playing;
200 GstElementFlags suppressed_flags;
209 static void gst_bin_dispose (GObject * object);
211 static void gst_bin_set_property (GObject * object, guint prop_id,
212 const GValue * value, GParamSpec * pspec);
213 static void gst_bin_get_property (GObject * object, guint prop_id,
214 GValue * value, GParamSpec * pspec);
216 static GstStateChangeReturn gst_bin_change_state_func (GstElement * element,
217 GstStateChange transition);
218 static gboolean gst_bin_post_message (GstElement * element, GstMessage * msg);
219 static GstStateChangeReturn gst_bin_get_state_func (GstElement * element,
220 GstState * state, GstState * pending, GstClockTime timeout);
221 static void bin_handle_async_done (GstBin * bin, GstStateChangeReturn ret,
222 gboolean flag_pending, GstClockTime running_time);
223 static void bin_handle_async_start (GstBin * bin);
224 static void bin_push_state_continue (GstBin * bin, BinContinueData * data);
225 static void bin_do_eos (GstBin * bin);
227 static gboolean gst_bin_add_func (GstBin * bin, GstElement * element);
228 static gboolean gst_bin_remove_func (GstBin * bin, GstElement * element);
229 static void gst_bin_deep_element_added_func (GstBin * bin, GstBin * sub_bin,
230 GstElement * element);
231 static void gst_bin_deep_element_removed_func (GstBin * bin, GstBin * sub_bin,
232 GstElement * element);
233 static void gst_bin_update_context (GstBin * bin, GstContext * context);
234 static void gst_bin_update_context_unlocked (GstBin * bin,
235 GstContext * context);
238 static void gst_bin_set_index_func (GstElement * element, GstIndex * index);
239 static GstIndex *gst_bin_get_index_func (GstElement * element);
242 static GstClock *gst_bin_provide_clock_func (GstElement * element);
243 static gboolean gst_bin_set_clock_func (GstElement * element, GstClock * clock);
245 static void gst_bin_handle_message_func (GstBin * bin, GstMessage * message);
246 static gboolean gst_bin_send_event (GstElement * element, GstEvent * event);
247 static GstBusSyncReply bin_bus_handler (GstBus * bus,
248 GstMessage * message, GstBin * bin);
249 static gboolean gst_bin_query (GstElement * element, GstQuery * query);
250 static void gst_bin_set_context (GstElement * element, GstContext * context);
252 static gboolean gst_bin_do_latency_func (GstBin * bin);
254 static void bin_remove_messages (GstBin * bin, GstObject * src,
255 GstMessageType types);
256 static void gst_bin_continue_func (GstBin * bin, BinContinueData * data);
257 static gint bin_element_is_sink (GstElement * child, GstBin * bin);
258 static gint bin_element_is_src (GstElement * child, GstBin * bin);
260 static GstIterator *gst_bin_sort_iterator_new (GstBin * bin);
262 /* Bin signals and properties */
269 DEEP_ELEMENT_REMOVED,
273 #define DEFAULT_ASYNC_HANDLING FALSE
274 #define DEFAULT_MESSAGE_FORWARD FALSE
280 PROP_MESSAGE_FORWARD,
284 static void gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data);
286 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
290 static const GInterfaceInfo iface_info = { \
291 gst_bin_child_proxy_init, \
295 g_type_add_interface_static (g_define_type_id, GST_TYPE_CHILD_PROXY, &iface_info); \
297 GST_DEBUG_CATEGORY_INIT (bin_debug, "bin", GST_DEBUG_BOLD, \
298 "debugging info for the 'bin' container element"); \
302 #define gst_bin_parent_class parent_class
303 G_DEFINE_TYPE_WITH_CODE (GstBin, gst_bin, GST_TYPE_ELEMENT, _do_init);
306 gst_bin_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
312 bin = GST_BIN_CAST (child_proxy);
314 GST_OBJECT_LOCK (bin);
315 if ((res = g_list_nth_data (bin->children, index)))
316 gst_object_ref (res);
317 GST_OBJECT_UNLOCK (bin);
319 return (GObject *) res;
323 gst_bin_child_proxy_get_children_count (GstChildProxy * child_proxy)
328 bin = GST_BIN_CAST (child_proxy);
330 GST_OBJECT_LOCK (bin);
331 num = bin->numchildren;
332 GST_OBJECT_UNLOCK (bin);
338 gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data)
340 GstChildProxyInterface *iface = g_iface;
342 iface->get_children_count = gst_bin_child_proxy_get_children_count;
343 iface->get_child_by_index = gst_bin_child_proxy_get_child_by_index;
347 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
348 GValue * return_accu, const GValue * handler_return, gpointer dummy)
352 myboolean = g_value_get_boolean (handler_return);
353 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
354 g_value_set_boolean (return_accu, myboolean);
356 GST_DEBUG ("invocation %d, %d", ihint->run_type, myboolean);
363 gst_bin_class_init (GstBinClass * klass)
365 GObjectClass *gobject_class;
366 GstElementClass *gstelement_class;
368 gobject_class = (GObjectClass *) klass;
369 gstelement_class = (GstElementClass *) klass;
371 g_type_class_add_private (klass, sizeof (GstBinPrivate));
373 gobject_class->set_property = gst_bin_set_property;
374 gobject_class->get_property = gst_bin_get_property;
377 * GstBin:async-handling:
379 * If set to %TRUE, the bin will handle asynchronous state changes.
380 * This should be used only if the bin subclass is modifying the state
381 * of its children on its own.
383 g_object_class_install_property (gobject_class, PROP_ASYNC_HANDLING,
384 g_param_spec_boolean ("async-handling", "Async Handling",
385 "The bin will handle Asynchronous state changes",
386 DEFAULT_ASYNC_HANDLING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
389 * GstBin::element-added:
391 * @element: the #GstElement that was added to the bin
393 * Will be emitted after the element was added to the bin.
395 gst_bin_signals[ELEMENT_ADDED] =
396 g_signal_new ("element-added", G_TYPE_FROM_CLASS (klass),
397 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_added), NULL,
398 NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
400 * GstBin::element-removed:
402 * @element: the #GstElement that was removed from the bin
404 * Will be emitted after the element was removed from the bin.
406 gst_bin_signals[ELEMENT_REMOVED] =
407 g_signal_new ("element-removed", G_TYPE_FROM_CLASS (klass),
408 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_removed), NULL,
409 NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
411 * GstBin::deep-element-added:
413 * @sub_bin: the #GstBin the element was added to
414 * @element: the #GstElement that was added to @sub_bin
416 * Will be emitted after the element was added to sub_bin.
420 gst_bin_signals[DEEP_ELEMENT_ADDED] =
421 g_signal_new ("deep-element-added", G_TYPE_FROM_CLASS (klass),
422 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, deep_element_added),
423 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, GST_TYPE_BIN,
426 * GstBin::deep-element-removed:
428 * @sub_bin: the #GstBin the element was removed from
429 * @element: the #GstElement that was removed from @sub_bin
431 * Will be emitted after the element was removed from sub_bin.
435 gst_bin_signals[DEEP_ELEMENT_REMOVED] =
436 g_signal_new ("deep-element-removed", G_TYPE_FROM_CLASS (klass),
437 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, deep_element_removed),
438 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, GST_TYPE_BIN,
441 * GstBin::do-latency:
444 * Will be emitted when the bin needs to perform latency calculations. This
445 * signal is only emitted for toplevel bins or when async-handling is
448 * Only one signal handler is invoked. If no signals are connected, the
449 * default handler is invoked, which will query and distribute the lowest
450 * possible latency to all sinks.
452 * Connect to this signal if the default latency calculations are not
453 * sufficient, like when you need different latencies for different sinks in
456 gst_bin_signals[DO_LATENCY] =
457 g_signal_new ("do-latency", G_TYPE_FROM_CLASS (klass),
458 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstBinClass, do_latency),
459 _gst_boolean_accumulator, NULL, g_cclosure_marshal_generic,
460 G_TYPE_BOOLEAN, 0, G_TYPE_NONE);
463 * GstBin:message-forward:
465 * Forward all children messages, even those that would normally be filtered by
466 * the bin. This can be interesting when one wants to be notified of the EOS
467 * state of individual elements, for example.
469 * The messages are converted to an ELEMENT message with the bin as the
470 * source. The structure of the message is named 'GstBinForwarded' and contains
471 * a field named 'message' of type GST_TYPE_MESSAGE that contains the original
474 g_object_class_install_property (gobject_class, PROP_MESSAGE_FORWARD,
475 g_param_spec_boolean ("message-forward", "Message Forward",
476 "Forwards all children messages",
477 DEFAULT_MESSAGE_FORWARD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
479 gobject_class->dispose = gst_bin_dispose;
481 gst_element_class_set_static_metadata (gstelement_class, "Generic bin",
483 "Simple container object",
484 "Erik Walthinsen <omega@cse.ogi.edu>,"
485 "Wim Taymans <wim.taymans@gmail.com>");
487 gstelement_class->change_state =
488 GST_DEBUG_FUNCPTR (gst_bin_change_state_func);
489 gstelement_class->post_message = GST_DEBUG_FUNCPTR (gst_bin_post_message);
490 gstelement_class->get_state = GST_DEBUG_FUNCPTR (gst_bin_get_state_func);
492 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_bin_get_index_func);
493 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_bin_set_index_func);
495 gstelement_class->provide_clock =
496 GST_DEBUG_FUNCPTR (gst_bin_provide_clock_func);
497 gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_bin_set_clock_func);
499 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_bin_send_event);
500 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_bin_query);
501 gstelement_class->set_context = GST_DEBUG_FUNCPTR (gst_bin_set_context);
503 klass->add_element = GST_DEBUG_FUNCPTR (gst_bin_add_func);
504 klass->remove_element = GST_DEBUG_FUNCPTR (gst_bin_remove_func);
505 klass->handle_message = GST_DEBUG_FUNCPTR (gst_bin_handle_message_func);
507 klass->deep_element_added = gst_bin_deep_element_added_func;
508 klass->deep_element_removed = gst_bin_deep_element_removed_func;
510 klass->do_latency = GST_DEBUG_FUNCPTR (gst_bin_do_latency_func);
514 gst_bin_init (GstBin * bin)
518 bin->numchildren = 0;
519 bin->children = NULL;
520 bin->children_cookie = 0;
521 bin->messages = NULL;
522 bin->provided_clock = NULL;
523 bin->clock_dirty = FALSE;
525 /* Set up a bus for listening to child elements */
526 bus = g_object_new (GST_TYPE_BUS, "enable-async", FALSE, NULL);
527 bin->child_bus = bus;
528 GST_DEBUG_OBJECT (bin, "using bus %" GST_PTR_FORMAT " to listen to children",
530 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bin_bus_handler, bin,
533 bin->priv = GST_BIN_GET_PRIVATE (bin);
534 bin->priv->asynchandling = DEFAULT_ASYNC_HANDLING;
535 bin->priv->structure_cookie = 0;
536 bin->priv->message_forward = DEFAULT_MESSAGE_FORWARD;
540 gst_bin_dispose (GObject * object)
542 GstBin *bin = GST_BIN_CAST (object);
543 GstBus **child_bus_p = &bin->child_bus;
544 GstClock **provided_clock_p = &bin->provided_clock;
545 GstElement **clock_provider_p = &bin->clock_provider;
547 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object, "%p dispose", object);
549 GST_OBJECT_LOCK (object);
550 gst_object_replace ((GstObject **) child_bus_p, NULL);
551 gst_object_replace ((GstObject **) provided_clock_p, NULL);
552 gst_object_replace ((GstObject **) clock_provider_p, NULL);
553 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
554 GST_OBJECT_UNLOCK (object);
556 while (bin->children) {
557 gst_bin_remove (bin, GST_ELEMENT_CAST (bin->children->data));
559 if (G_UNLIKELY (bin->children != NULL)) {
560 g_critical ("could not remove elements from bin '%s'",
561 GST_STR_NULL (GST_OBJECT_NAME (object)));
564 G_OBJECT_CLASS (parent_class)->dispose (object);
569 * @name: (allow-none): the name of the new bin
571 * Creates a new bin with the given name.
573 * Returns: (transfer floating): a new #GstBin
576 gst_bin_new (const gchar * name)
578 return gst_element_factory_make ("bin", name);
582 gst_bin_set_property (GObject * object, guint prop_id,
583 const GValue * value, GParamSpec * pspec)
587 gstbin = GST_BIN_CAST (object);
590 case PROP_ASYNC_HANDLING:
591 GST_OBJECT_LOCK (gstbin);
592 gstbin->priv->asynchandling = g_value_get_boolean (value);
593 GST_OBJECT_UNLOCK (gstbin);
595 case PROP_MESSAGE_FORWARD:
596 GST_OBJECT_LOCK (gstbin);
597 gstbin->priv->message_forward = g_value_get_boolean (value);
598 GST_OBJECT_UNLOCK (gstbin);
601 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
607 gst_bin_get_property (GObject * object, guint prop_id,
608 GValue * value, GParamSpec * pspec)
612 gstbin = GST_BIN_CAST (object);
615 case PROP_ASYNC_HANDLING:
616 GST_OBJECT_LOCK (gstbin);
617 g_value_set_boolean (value, gstbin->priv->asynchandling);
618 GST_OBJECT_UNLOCK (gstbin);
620 case PROP_MESSAGE_FORWARD:
621 GST_OBJECT_LOCK (gstbin);
622 g_value_set_boolean (value, gstbin->priv->message_forward);
623 GST_OBJECT_UNLOCK (gstbin);
626 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
632 /* return the cached index */
634 gst_bin_get_index_func (GstElement * element)
639 bin = GST_BIN_CAST (element);
641 GST_OBJECT_LOCK (bin);
642 if ((result = bin->priv->index))
643 gst_object_ref (result);
644 GST_OBJECT_UNLOCK (bin);
649 /* set the index on all elements in this bin
654 gst_bin_set_index_func (GstElement * element, GstIndex * index)
660 GValue data = { 0, };
662 bin = GST_BIN_CAST (element);
664 GST_OBJECT_LOCK (bin);
665 old = bin->priv->index;
666 if (G_UNLIKELY (old == index))
669 gst_object_ref (index);
670 bin->priv->index = index;
671 GST_OBJECT_UNLOCK (bin);
674 gst_object_unref (old);
676 it = gst_bin_iterate_elements (bin);
678 /* set the index on all elements in the bin */
681 switch (gst_iterator_next (it, &data)) {
682 case GST_ITERATOR_OK:
684 GstElement *child = g_value_get_object (&data);
686 GST_DEBUG_OBJECT (bin, "setting index on '%s'",
687 GST_ELEMENT_NAME (child));
688 gst_element_set_index (child, index);
690 g_value_reset (&data);
693 case GST_ITERATOR_RESYNC:
694 GST_DEBUG_OBJECT (bin, "iterator doing resync");
695 gst_iterator_resync (it);
698 case GST_ITERATOR_DONE:
699 GST_DEBUG_OBJECT (bin, "iterator done");
704 g_value_unset (&data);
705 gst_iterator_free (it);
710 GST_DEBUG_OBJECT (bin, "index was already set");
711 GST_OBJECT_UNLOCK (bin);
717 /* set the clock on all elements in this bin
722 gst_bin_set_clock_func (GstElement * element, GstClock * clock)
728 GValue data = { 0, };
730 bin = GST_BIN_CAST (element);
732 it = gst_bin_iterate_elements (bin);
736 switch (gst_iterator_next (it, &data)) {
737 case GST_ITERATOR_OK:
739 GstElement *child = g_value_get_object (&data);
741 res &= gst_element_set_clock (child, clock);
743 g_value_reset (&data);
746 case GST_ITERATOR_RESYNC:
747 GST_DEBUG_OBJECT (bin, "iterator doing resync");
748 gst_iterator_resync (it);
752 case GST_ITERATOR_DONE:
753 GST_DEBUG_OBJECT (bin, "iterator done");
758 g_value_unset (&data);
759 gst_iterator_free (it);
762 res = GST_ELEMENT_CLASS (parent_class)->set_clock (element, clock);
767 /* get the clock for this bin by asking all of the children in this bin
769 * The ref of the returned clock in increased so unref after usage.
771 * We loop the elements in state order and pick the last clock we can
772 * get. This makes sure we get a clock from the source.
777 gst_bin_provide_clock_func (GstElement * element)
779 GstClock *result = NULL;
780 GstElement *provider = NULL;
785 GstClock **provided_clock_p;
786 GstElement **clock_provider_p;
788 bin = GST_BIN_CAST (element);
790 GST_OBJECT_LOCK (bin);
791 if (!bin->clock_dirty)
794 GST_DEBUG_OBJECT (bin, "finding new clock");
796 it = gst_bin_sort_iterator_new (bin);
797 GST_OBJECT_UNLOCK (bin);
801 switch (gst_iterator_next (it, &val)) {
802 case GST_ITERATOR_OK:
804 GstElement *child = g_value_get_object (&val);
807 clock = gst_element_provide_clock (child);
809 GST_DEBUG_OBJECT (bin, "found candidate clock %p by element %s",
810 clock, GST_ELEMENT_NAME (child));
812 gst_object_unref (result);
813 gst_object_unref (provider);
816 provider = gst_object_ref (child);
819 g_value_reset (&val);
822 case GST_ITERATOR_RESYNC:
823 gst_iterator_resync (it);
826 case GST_ITERATOR_DONE:
831 g_value_unset (&val);
832 gst_iterator_free (it);
834 GST_OBJECT_LOCK (bin);
835 if (!bin->clock_dirty) {
837 gst_object_unref (provider);
839 gst_object_unref (result);
845 provided_clock_p = &bin->provided_clock;
846 clock_provider_p = &bin->clock_provider;
847 gst_object_replace ((GstObject **) provided_clock_p, (GstObject *) result);
848 gst_object_replace ((GstObject **) clock_provider_p, (GstObject *) provider);
849 bin->clock_dirty = FALSE;
850 GST_DEBUG_OBJECT (bin,
851 "provided new clock %" GST_PTR_FORMAT " by provider %" GST_PTR_FORMAT,
853 /* Provider is not being returned to caller, just the result */
855 gst_object_unref (provider);
856 GST_OBJECT_UNLOCK (bin);
862 if ((result = bin->provided_clock))
863 gst_object_ref (result);
864 GST_DEBUG_OBJECT (bin, "returning old clock %p", result);
865 GST_OBJECT_UNLOCK (bin);
872 * functions for manipulating cached messages
877 GstMessageType types;
880 /* check if a message is of given src and type */
882 message_check (GstMessage * message, MessageFind * target)
887 eq &= GST_MESSAGE_SRC (message) == target->src;
889 eq &= (GST_MESSAGE_TYPE (message) & target->types) != 0;
890 GST_LOG ("looking at message %p: %d", message, eq);
896 find_message (GstBin * bin, GstObject * src, GstMessageType types)
904 result = g_list_find_custom (bin->messages, &find,
905 (GCompareFunc) message_check);
908 GST_DEBUG_OBJECT (bin, "we found a message %p from %s matching types %08x",
909 result->data, GST_OBJECT_NAME (GST_MESSAGE_CAST (result->data)->src),
912 GST_DEBUG_OBJECT (bin, "no message found matching types %08x", types);
913 #ifndef GST_DISABLE_GST_DEBUG
917 for (i = 0; i < 32; i++)
918 if (types & (1U << i))
919 GST_DEBUG_OBJECT (bin, " %s", gst_message_type_get_name (1U << i));
927 /* with LOCK, returns TRUE if message had a valid SRC, takes ownership of
930 * A message that is cached and has the same SRC and type is replaced
931 * by the given message.
934 bin_replace_message (GstBin * bin, GstMessage * message, GstMessageType types)
940 if ((src = GST_MESSAGE_SRC (message))) {
941 /* first find the previous message posted by this element */
942 if ((previous = find_message (bin, src, types))) {
943 GstMessage *previous_msg;
945 /* if we found a previous message, replace it */
946 previous_msg = previous->data;
947 previous->data = message;
949 GST_DEBUG_OBJECT (bin, "replace old message %s from %s with %s message",
950 GST_MESSAGE_TYPE_NAME (previous_msg), GST_ELEMENT_NAME (src),
951 GST_MESSAGE_TYPE_NAME (message));
953 gst_message_unref (previous_msg);
955 /* keep new message */
956 bin->messages = g_list_prepend (bin->messages, message);
958 GST_DEBUG_OBJECT (bin, "got new message %p, %s from %s",
959 message, GST_MESSAGE_TYPE_NAME (message), GST_ELEMENT_NAME (src));
962 GST_DEBUG_OBJECT (bin, "got message %s from (NULL), not processing",
963 GST_MESSAGE_TYPE_NAME (message));
965 gst_message_unref (message);
970 /* with LOCK. Remove all messages of given types */
972 bin_remove_messages (GstBin * bin, GstObject * src, GstMessageType types)
980 for (walk = bin->messages; walk; walk = next) {
981 GstMessage *message = (GstMessage *) walk->data;
983 next = g_list_next (walk);
985 if (message_check (message, &find) == 0) {
986 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (message),
987 "deleting message %p of type %s (types 0x%08x)", message,
988 GST_MESSAGE_TYPE_NAME (message), types);
989 bin->messages = g_list_delete_link (bin->messages, walk);
990 gst_message_unref (message);
992 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (message),
993 "not deleting message %p of type 0x%08x", message,
994 GST_MESSAGE_TYPE (message));
1000 /* Check if the bin is EOS. We do this by scanning all sinks and
1001 * checking if they posted an EOS message.
1003 * call with bin LOCK */
1005 is_eos (GstBin * bin, guint32 * seqnum)
1012 for (walk = bin->children; walk; walk = g_list_next (walk)) {
1013 GstElement *element;
1015 element = GST_ELEMENT_CAST (walk->data);
1016 if (bin_element_is_sink (element, bin) == 0) {
1017 /* check if element posted EOS */
1019 find_message (bin, GST_OBJECT_CAST (element), GST_MESSAGE_EOS))) {
1020 GST_DEBUG ("sink '%s' posted EOS", GST_ELEMENT_NAME (element));
1021 *seqnum = gst_message_get_seqnum (GST_MESSAGE_CAST (msgs->data));
1024 GST_DEBUG ("sink '%s' did not post EOS yet",
1025 GST_ELEMENT_NAME (element));
1031 /* FIXME: Some tests (e.g. elements/capsfilter) use
1032 * pipelines with a dangling sinkpad but no sink element.
1033 * These tests assume that no EOS message is ever
1034 * posted on the bus so let's keep that behaviour.
1035 * In valid pipelines this doesn't make a difference.
1037 return result && n_eos > 0;
1041 /* Check if the bin is STREAM_START. We do this by scanning all sinks and
1042 * checking if they posted an STREAM_START message.
1044 * call with bin LOCK */
1046 is_stream_start (GstBin * bin, guint32 * seqnum, gboolean * have_group_id,
1052 gboolean first = TRUE, same_group_id = TRUE;
1054 *have_group_id = TRUE;
1057 for (walk = bin->children; walk; walk = g_list_next (walk)) {
1058 GstElement *element;
1060 element = GST_ELEMENT_CAST (walk->data);
1061 if (bin_element_is_sink (element, bin) == 0) {
1062 /* check if element posted STREAM_START */
1064 find_message (bin, GST_OBJECT_CAST (element),
1065 GST_MESSAGE_STREAM_START))) {
1066 GST_DEBUG ("sink '%s' posted STREAM_START", GST_ELEMENT_NAME (element));
1067 *seqnum = gst_message_get_seqnum (GST_MESSAGE_CAST (msgs->data));
1068 if (gst_message_parse_group_id (GST_MESSAGE_CAST (msgs->data),
1072 *group_id = tmp_group_id;
1074 if (tmp_group_id != *group_id)
1075 same_group_id = FALSE;
1078 *have_group_id = FALSE;
1081 GST_DEBUG ("sink '%s' did not post STREAM_START yet",
1082 GST_ELEMENT_NAME (element));
1089 /* If all have a group_id we only consider this stream started
1090 * if all group ids were the same and all sinks posted a stream-start
1093 return same_group_id && result;
1094 /* otherwise consider this stream started after all sinks
1095 * have reported stream-start for backward compatibility.
1096 * FIXME 2.0: This should go away! */
1101 unlink_pads (const GValue * item, gpointer user_data)
1106 pad = g_value_get_object (item);
1108 if ((peer = gst_pad_get_peer (pad))) {
1109 if (gst_pad_get_direction (pad) == GST_PAD_SRC)
1110 gst_pad_unlink (pad, peer);
1112 gst_pad_unlink (peer, pad);
1113 gst_object_unref (peer);
1118 bin_deep_iterator_foreach (const GValue * item, gpointer user_data)
1120 GQueue *queue = user_data;
1122 g_queue_push_tail (queue, g_value_dup_object (item));
1126 gst_bin_do_deep_add_remove (GstBin * bin, gint sig_id, const gchar * sig_name,
1127 GstElement * element)
1129 g_signal_emit (bin, sig_id, 0, bin, element);
1131 /* When removing a bin, emit deep-element-* for everything in the bin too */
1132 if (GST_IS_BIN (element)) {
1134 GstIteratorResult ires;
1135 GQueue elements = G_QUEUE_INIT;
1137 GST_LOG_OBJECT (bin, "Recursing into bin %" GST_PTR_FORMAT " for %s",
1139 it = gst_bin_iterate_recurse (GST_BIN_CAST (element));
1141 ires = gst_iterator_foreach (it, bin_deep_iterator_foreach, &elements);
1142 if (ires != GST_ITERATOR_DONE) {
1143 g_queue_foreach (&elements, (GFunc) g_object_unref, NULL);
1144 g_queue_clear (&elements);
1146 if (ires == GST_ITERATOR_RESYNC)
1147 gst_iterator_resync (it);
1148 } while (ires == GST_ITERATOR_RESYNC);
1149 if (ires != GST_ITERATOR_ERROR) {
1152 while ((e = g_queue_pop_head (&elements))) {
1153 GstObject *parent = gst_object_get_parent (GST_OBJECT_CAST (e));
1155 GST_LOG_OBJECT (bin, "calling %s for element %" GST_PTR_FORMAT
1156 " in bin %" GST_PTR_FORMAT, sig_name, e, parent);
1157 g_signal_emit (bin, sig_id, 0, parent, e);
1158 gst_object_unref (parent);
1162 gst_iterator_free (it);
1166 /* vmethod that adds an element to a bin
1171 gst_bin_add_func (GstBin * bin, GstElement * element)
1175 gboolean is_sink, is_source, provides_clock, requires_clock;
1176 GstMessage *clock_message = NULL, *async_message = NULL;
1177 GstStateChangeReturn ret;
1178 GList *l, *elem_contexts, *need_context_messages;
1180 GST_DEBUG_OBJECT (bin, "element :%s", GST_ELEMENT_NAME (element));
1182 /* we obviously can't add ourself to ourself */
1183 if (G_UNLIKELY (element == GST_ELEMENT_CAST (bin)))
1186 /* get the element name to make sure it is unique in this bin. */
1187 GST_OBJECT_LOCK (element);
1188 elem_name = g_strdup (GST_ELEMENT_NAME (element));
1189 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1190 is_source = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE);
1192 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1194 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1195 GST_OBJECT_UNLOCK (element);
1197 GST_OBJECT_LOCK (bin);
1199 /* then check to see if the element's name is already taken in the bin,
1200 * we can safely take the lock here. This check is probably bogus because
1201 * you can safely change the element name after this check and before setting
1202 * the object parent. The window is very small though... */
1203 if (G_UNLIKELY (!gst_object_check_uniqueness (bin->children, elem_name)))
1204 goto duplicate_name;
1206 /* set the element's parent and add the element to the bin's list of children */
1207 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (element),
1208 GST_OBJECT_CAST (bin))))
1211 /* if we add a sink we become a sink */
1212 if (is_sink && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_SINK)) {
1213 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "element \"%s\" was sink",
1215 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SINK);
1217 if (is_source && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_SOURCE)) {
1218 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "element \"%s\" was source",
1220 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SOURCE);
1223 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_PROVIDE_CLOCK)) {
1224 GST_DEBUG_OBJECT (bin, "element \"%s\" can provide a clock", elem_name);
1226 gst_message_new_clock_provide (GST_OBJECT_CAST (element), NULL, TRUE);
1227 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1230 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_REQUIRE_CLOCK)) {
1231 GST_DEBUG_OBJECT (bin, "element \"%s\" requires a clock", elem_name);
1232 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1235 bin->children = g_list_prepend (bin->children, element);
1237 bin->children_cookie++;
1238 if (!GST_BIN_IS_NO_RESYNC (bin))
1239 bin->priv->structure_cookie++;
1241 /* distribute the bus */
1242 gst_element_set_bus (element, bin->child_bus);
1244 /* propagate the current base_time, start_time and clock */
1245 gst_element_set_base_time (element, GST_ELEMENT_CAST (bin)->base_time);
1246 gst_element_set_start_time (element, GST_ELEMENT_START_TIME (bin));
1247 /* it's possible that the element did not accept the clock but
1248 * that is not important right now. When the pipeline goes to PLAYING,
1249 * a new clock will be selected */
1250 gst_element_set_clock (element, GST_ELEMENT_CLOCK (bin));
1252 /* get the element's list of contexts before propagating our own */
1253 elem_contexts = gst_element_get_contexts (element);
1254 for (l = GST_ELEMENT_CAST (bin)->contexts; l; l = l->next)
1255 gst_element_set_context (element, l->data);
1257 need_context_messages = NULL;
1258 for (l = elem_contexts; l; l = l->next) {
1259 GstContext *replacement, *context = l->data;
1260 const gchar *context_type;
1262 context_type = gst_context_get_context_type (context);
1264 /* we already set this context above? */
1266 gst_element_get_context_unlocked (GST_ELEMENT (bin), context_type);
1268 gst_context_unref (replacement);
1273 /* ask our parent for the context */
1274 msg = gst_message_new_need_context (GST_OBJECT_CAST (bin), context_type);
1275 s = (GstStructure *) gst_message_get_structure (msg);
1276 gst_structure_set (s, "bin.old.context", GST_TYPE_CONTEXT, context, NULL);
1278 need_context_messages = g_list_prepend (need_context_messages, msg);
1283 /* set the cached index on the children */
1284 if (bin->priv->index)
1285 gst_element_set_index (element, bin->priv->index);
1288 ret = GST_STATE_RETURN (bin);
1289 /* no need to update the state if we are in error */
1290 if (ret == GST_STATE_CHANGE_FAILURE)
1291 goto no_state_recalc;
1293 /* update the bin state, the new element could have been an ASYNC or
1294 * NO_PREROLL element */
1295 ret = GST_STATE_RETURN (element);
1296 GST_DEBUG_OBJECT (bin, "added %s element",
1297 gst_element_state_change_return_get_name (ret));
1300 case GST_STATE_CHANGE_ASYNC:
1302 /* create message to track this aync element when it posts an async-done
1304 async_message = gst_message_new_async_start (GST_OBJECT_CAST (element));
1307 case GST_STATE_CHANGE_NO_PREROLL:
1308 /* ignore all async elements we might have and commit our state */
1309 bin_handle_async_done (bin, ret, FALSE, GST_CLOCK_TIME_NONE);
1311 case GST_STATE_CHANGE_FAILURE:
1318 GST_OBJECT_UNLOCK (bin);
1320 for (l = need_context_messages; l; l = l->next) {
1321 GstMessage *msg = l->data;
1323 const gchar *context_type;
1324 GstContext *replacement, *context;
1326 gst_message_parse_context_type (msg, &context_type);
1328 GST_LOG_OBJECT (bin, "asking parent for context type: %s "
1329 "from %" GST_PTR_FORMAT, context_type, element);
1331 s = (GstStructure *) gst_message_get_structure (msg);
1332 gst_structure_get (s, "bin.old.context", GST_TYPE_CONTEXT, &context, NULL);
1333 gst_structure_remove_field (s, "bin.old.context");
1334 gst_element_post_message (GST_ELEMENT_CAST (bin), msg);
1336 /* lock to avoid losing a potential write */
1337 GST_OBJECT_LOCK (bin);
1339 gst_element_get_context_unlocked (GST_ELEMENT_CAST (bin), context_type);
1342 /* we got the context set from GstElement::set_context */
1343 gst_context_unref (replacement);
1344 GST_OBJECT_UNLOCK (bin);
1346 /* Propagate the element's context upwards */
1347 GST_LOG_OBJECT (bin, "propagating existing context type: %s %p "
1348 "from %" GST_PTR_FORMAT, context_type, context, element);
1350 gst_bin_update_context_unlocked (bin, context);
1353 gst_message_new_have_context (GST_OBJECT_CAST (bin),
1354 gst_context_ref (context));
1355 GST_OBJECT_UNLOCK (bin);
1356 gst_element_post_message (GST_ELEMENT_CAST (bin), msg);
1358 gst_context_unref (context);
1360 g_list_free_full (elem_contexts, (GDestroyNotify) gst_context_unref);
1361 g_list_free (need_context_messages);
1363 /* post the messages on the bus of the element so that the bin can handle
1366 gst_element_post_message (element, clock_message);
1369 gst_element_post_message (element, async_message);
1371 /* unlink all linked pads */
1372 it = gst_element_iterate_pads (element);
1373 while (gst_iterator_foreach (it, (GstIteratorForeachFunction) unlink_pads,
1374 NULL) == GST_ITERATOR_RESYNC)
1375 gst_iterator_resync (it);
1376 gst_iterator_free (it);
1378 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "added element \"%s\"",
1381 g_signal_emit (bin, gst_bin_signals[ELEMENT_ADDED], 0, element);
1382 gst_child_proxy_child_added ((GstChildProxy *) bin, (GObject *) element,
1385 gst_bin_do_deep_add_remove (bin, gst_bin_signals[DEEP_ELEMENT_ADDED],
1386 "deep-element-added", element);
1392 /* ERROR handling here */
1395 GST_OBJECT_LOCK (bin);
1396 g_warning ("Cannot add bin '%s' to itself", GST_ELEMENT_NAME (bin));
1397 GST_OBJECT_UNLOCK (bin);
1402 g_warning ("Name '%s' is not unique in bin '%s', not adding",
1403 elem_name, GST_ELEMENT_NAME (bin));
1404 GST_OBJECT_UNLOCK (bin);
1410 g_warning ("Element '%s' already has parent", elem_name);
1411 GST_OBJECT_UNLOCK (bin);
1418 * gst_bin_set_suppressed_flags:
1420 * @flags: the #GstElementFlags to suppress
1422 * Suppress the given flags on the bin. #GstElementFlags of a
1423 * child element are propagated when it is added to the bin.
1424 * When suppressed flags are set, those specified flags will
1425 * not be propagated to the bin.
1432 gst_bin_set_suppressed_flags (GstBin * bin, GstElementFlags flags)
1434 g_return_if_fail (GST_IS_BIN (bin));
1436 GST_OBJECT_LOCK (bin);
1437 bin->priv->suppressed_flags = bin->priv->suppressed_flags | flags;
1438 GST_OBJECT_UNLOCK (bin);
1440 GST_DEBUG_OBJECT (bin, "Set suppressed flags(0x%x) to bin '%s'", flags,
1441 GST_ELEMENT_NAME (bin));
1445 * gst_bin_get_suppressed_flags:
1448 * Return the suppressed flags of the bin.
1452 * Returns: the bin's suppressed #GstElementFlags.
1457 gst_bin_get_suppressed_flags (GstBin * bin)
1459 GstElementFlags res;
1461 g_return_val_if_fail (GST_IS_BIN (bin), 0);
1463 GST_OBJECT_LOCK (bin);
1464 res = bin->priv->suppressed_flags;
1465 GST_OBJECT_UNLOCK (bin);
1470 /* signal vfunc, will be called when a new element was added */
1472 gst_bin_deep_element_added_func (GstBin * bin, GstBin * sub_bin,
1477 parent_bin = (GstBin *) gst_object_get_parent (GST_OBJECT_CAST (bin));
1478 if (parent_bin == NULL) {
1479 GST_LOG_OBJECT (bin, "no parent, reached top-level");
1483 GST_LOG_OBJECT (parent_bin, "emitting deep-element-added for element "
1484 "%" GST_PTR_FORMAT " which has just been added to %" GST_PTR_FORMAT,
1487 g_signal_emit (parent_bin, gst_bin_signals[DEEP_ELEMENT_ADDED], 0, sub_bin,
1490 gst_object_unref (parent_bin);
1493 /* signal vfunc, will be called when an element was removed */
1495 gst_bin_deep_element_removed_func (GstBin * bin, GstBin * sub_bin,
1500 parent_bin = (GstBin *) gst_object_get_parent (GST_OBJECT_CAST (bin));
1501 if (parent_bin == NULL) {
1502 GST_LOG_OBJECT (bin, "no parent, reached top-level");
1506 GST_LOG_OBJECT (parent_bin, "emitting deep-element-removed for element "
1507 "%" GST_PTR_FORMAT " which has just been removed from %" GST_PTR_FORMAT,
1510 g_signal_emit (parent_bin, gst_bin_signals[DEEP_ELEMENT_REMOVED], 0, sub_bin,
1513 gst_object_unref (parent_bin);
1519 * @element: (transfer full): the #GstElement to add
1521 * Adds the given element to the bin. Sets the element's parent, and thus
1522 * takes ownership of the element. An element can only be added to one bin.
1524 * If the element's pads are linked to other pads, the pads will be unlinked
1525 * before the element is added to the bin.
1528 * When you add an element to an already-running pipeline, you will have to
1529 * take care to set the state of the newly-added element to the desired
1530 * state (usually PLAYING or PAUSED, same you set the pipeline to originally)
1531 * with gst_element_set_state(), or use gst_element_sync_state_with_parent().
1532 * The bin or pipeline will not take care of this for you.
1537 * Returns: %TRUE if the element could be added, %FALSE if
1538 * the bin does not want to accept the element.
1541 gst_bin_add (GstBin * bin, GstElement * element)
1543 GstBinClass *bclass;
1546 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1547 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1548 g_return_val_if_fail (GST_ELEMENT_CAST (bin) != element, FALSE);
1550 bclass = GST_BIN_GET_CLASS (bin);
1552 if (G_UNLIKELY (bclass->add_element == NULL))
1555 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "adding element %s to bin %s",
1556 GST_STR_NULL (GST_ELEMENT_NAME (element)),
1557 GST_STR_NULL (GST_ELEMENT_NAME (bin)));
1559 GST_TRACER_BIN_ADD_PRE (bin, element);
1560 result = bclass->add_element (bin, element);
1561 GST_TRACER_BIN_ADD_POST (bin, element, result);
1565 /* ERROR handling */
1568 g_warning ("adding elements to bin '%s' is not supported",
1569 GST_ELEMENT_NAME (bin));
1574 /* remove an element from the bin
1579 gst_bin_remove_func (GstBin * bin, GstElement * element)
1583 gboolean is_sink, is_source, provides_clock, requires_clock;
1584 gboolean othersink, othersource, otherprovider, otherrequirer, found;
1585 GstMessage *clock_message = NULL;
1586 GstClock **provided_clock_p;
1587 GstElement **clock_provider_p;
1589 gboolean other_async, this_async, have_no_preroll;
1590 GstStateChangeReturn ret;
1592 GST_DEBUG_OBJECT (bin, "element :%s", GST_ELEMENT_NAME (element));
1594 /* we obviously can't remove ourself from ourself */
1595 if (G_UNLIKELY (element == GST_ELEMENT_CAST (bin)))
1596 goto removing_itself;
1598 GST_OBJECT_LOCK (bin);
1600 GST_OBJECT_LOCK (element);
1601 elem_name = g_strdup (GST_ELEMENT_NAME (element));
1603 if (GST_OBJECT_PARENT (element) != GST_OBJECT_CAST (bin))
1606 /* remove the parent ref */
1607 GST_OBJECT_PARENT (element) = NULL;
1609 /* grab element name so we can print it */
1610 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1611 is_source = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE);
1613 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1615 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1616 GST_OBJECT_UNLOCK (element);
1620 othersource = FALSE;
1621 otherprovider = FALSE;
1622 otherrequirer = FALSE;
1623 have_no_preroll = FALSE;
1624 /* iterate the elements, we collect which ones are async and no_preroll. We
1625 * also remove the element when we find it. */
1626 for (walk = bin->children; walk; walk = next) {
1627 GstElement *child = GST_ELEMENT_CAST (walk->data);
1629 next = g_list_next (walk);
1631 if (child == element) {
1633 /* remove the element */
1634 bin->children = g_list_delete_link (bin->children, walk);
1636 gboolean child_sink, child_source, child_provider, child_requirer;
1638 GST_OBJECT_LOCK (child);
1639 child_sink = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SINK);
1640 child_source = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SOURCE);
1642 GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1644 GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1645 /* when we remove a sink, check if there are other sinks. */
1646 if (is_sink && !othersink && child_sink)
1648 if (is_source && !othersource && child_source)
1650 if (provides_clock && !otherprovider && child_provider)
1651 otherprovider = TRUE;
1652 if (requires_clock && !otherrequirer && child_requirer)
1653 otherrequirer = TRUE;
1654 /* check if we have NO_PREROLL children */
1655 if (GST_STATE_RETURN (child) == GST_STATE_CHANGE_NO_PREROLL)
1656 have_no_preroll = TRUE;
1657 GST_OBJECT_UNLOCK (child);
1661 /* the element must have been in the bin's list of children */
1662 if (G_UNLIKELY (!found))
1665 /* we now removed the element from the list of elements, increment the cookie
1666 * so that others can detect a change in the children list. */
1668 bin->children_cookie++;
1669 if (!GST_BIN_IS_NO_RESYNC (bin))
1670 bin->priv->structure_cookie++;
1672 if (is_sink && !othersink
1673 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_SINK)) {
1674 /* we're not a sink anymore */
1675 GST_DEBUG_OBJECT (bin, "we removed the last sink");
1676 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_SINK);
1678 if (is_source && !othersource
1679 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_SOURCE)) {
1680 /* we're not a source anymore */
1681 GST_DEBUG_OBJECT (bin, "we removed the last source");
1682 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_SOURCE);
1684 if (provides_clock && !otherprovider
1685 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_PROVIDE_CLOCK)) {
1686 /* we're not a clock provider anymore */
1687 GST_DEBUG_OBJECT (bin, "we removed the last clock provider");
1688 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1690 if (requires_clock && !otherrequirer
1691 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_REQUIRE_CLOCK)) {
1692 /* we're not a clock requirer anymore */
1693 GST_DEBUG_OBJECT (bin, "we removed the last clock requirer");
1694 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1697 /* if the clock provider for this element is removed, we lost
1698 * the clock as well, we need to inform the parent of this
1699 * so that it can select a new clock */
1700 if (bin->clock_provider == element) {
1701 GST_DEBUG_OBJECT (bin, "element \"%s\" provided the clock", elem_name);
1702 bin->clock_dirty = TRUE;
1704 gst_message_new_clock_lost (GST_OBJECT_CAST (bin), bin->provided_clock);
1705 provided_clock_p = &bin->provided_clock;
1706 clock_provider_p = &bin->clock_provider;
1707 gst_object_replace ((GstObject **) provided_clock_p, NULL);
1708 gst_object_replace ((GstObject **) clock_provider_p, NULL);
1711 /* remove messages for the element, if there was a pending ASYNC_START
1712 * message we must see if removing the element caused the bin to lose its
1715 other_async = FALSE;
1716 for (walk = bin->messages; walk; walk = next) {
1717 GstMessage *message = (GstMessage *) walk->data;
1718 GstElement *src = GST_ELEMENT_CAST (GST_MESSAGE_SRC (message));
1721 next = g_list_next (walk);
1724 switch (GST_MESSAGE_TYPE (message)) {
1725 case GST_MESSAGE_ASYNC_START:
1731 GST_DEBUG_OBJECT (src, "looking at message %p", message);
1733 case GST_MESSAGE_STRUCTURE_CHANGE:
1737 GST_DEBUG_OBJECT (src, "looking at structure change message %p",
1739 /* it's unlikely that this message is still in the list of messages
1740 * because this would mean that a link/unlink is busy in another thread
1741 * while we remove the element. We still have to remove the message
1742 * because we might not receive the done message anymore when the element
1743 * is removed from the bin. */
1744 gst_message_parse_structure_change (message, NULL, &owner, NULL);
1745 if (owner == element)
1756 /* delete all message types */
1757 GST_DEBUG_OBJECT (src, "deleting message %p of element \"%s\"",
1758 message, elem_name);
1759 bin->messages = g_list_delete_link (bin->messages, walk);
1760 gst_message_unref (message);
1764 /* get last return */
1765 ret = GST_STATE_RETURN (bin);
1767 /* no need to update the state if we are in error */
1768 if (ret == GST_STATE_CHANGE_FAILURE)
1769 goto no_state_recalc;
1771 if (!other_async && this_async) {
1772 /* all other elements were not async and we removed the async one,
1773 * handle the async-done case because we are not async anymore now. */
1774 GST_DEBUG_OBJECT (bin,
1775 "we removed the last async element, have no_preroll %d",
1778 /* the current state return of the bin depends on if there are no_preroll
1779 * elements in the pipeline or not */
1780 if (have_no_preroll)
1781 ret = GST_STATE_CHANGE_NO_PREROLL;
1783 ret = GST_STATE_CHANGE_SUCCESS;
1785 bin_handle_async_done (bin, ret, FALSE, GST_CLOCK_TIME_NONE);
1787 GST_DEBUG_OBJECT (bin,
1788 "recalc state preroll: %d, other async: %d, this async %d",
1789 have_no_preroll, other_async, this_async);
1791 if (have_no_preroll) {
1792 ret = GST_STATE_CHANGE_NO_PREROLL;
1793 } else if (other_async) {
1794 /* there are other async elements and we were not doing an async state
1795 * change, change our pending state and go async */
1796 if (GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING) {
1797 GST_STATE_NEXT (bin) = GST_STATE (bin);
1798 GST_STATE_PENDING (bin) = GST_STATE (bin);
1800 ret = GST_STATE_CHANGE_ASYNC;
1802 GST_STATE_RETURN (bin) = ret;
1806 gst_element_set_bus (element, NULL);
1807 /* Clear the clock we provided to the element */
1808 gst_element_set_clock (element, NULL);
1809 GST_OBJECT_UNLOCK (bin);
1812 gst_element_post_message (GST_ELEMENT_CAST (bin), clock_message);
1814 /* unlink all linked pads */
1815 it = gst_element_iterate_pads (element);
1816 while (gst_iterator_foreach (it, (GstIteratorForeachFunction) unlink_pads,
1817 NULL) == GST_ITERATOR_RESYNC)
1818 gst_iterator_resync (it);
1819 gst_iterator_free (it);
1821 GST_CAT_INFO_OBJECT (GST_CAT_PARENTAGE, bin, "removed child \"%s\"",
1824 g_signal_emit (bin, gst_bin_signals[ELEMENT_REMOVED], 0, element);
1825 gst_child_proxy_child_removed ((GstChildProxy *) bin, (GObject *) element,
1828 gst_bin_do_deep_add_remove (bin, gst_bin_signals[DEEP_ELEMENT_REMOVED],
1829 "deep-element-removed", element);
1832 /* element is really out of our control now */
1833 gst_object_unref (element);
1837 /* ERROR handling */
1840 GST_OBJECT_LOCK (bin);
1841 g_warning ("Cannot remove bin '%s' from itself", GST_ELEMENT_NAME (bin));
1842 GST_OBJECT_UNLOCK (bin);
1847 g_warning ("Element '%s' is not in bin '%s'", elem_name,
1848 GST_ELEMENT_NAME (bin));
1849 GST_OBJECT_UNLOCK (element);
1850 GST_OBJECT_UNLOCK (bin);
1859 * @element: (transfer none): the #GstElement to remove
1861 * Removes the element from the bin, unparenting it as well.
1862 * Unparenting the element means that the element will be dereferenced,
1863 * so if the bin holds the only reference to the element, the element
1864 * will be freed in the process of removing it from the bin. If you
1865 * want the element to still exist after removing, you need to call
1866 * gst_object_ref() before removing it from the bin.
1868 * If the element's pads are linked to other pads, the pads will be unlinked
1869 * before the element is removed from the bin.
1873 * Returns: %TRUE if the element could be removed, %FALSE if
1874 * the bin does not want to remove the element.
1877 gst_bin_remove (GstBin * bin, GstElement * element)
1879 GstBinClass *bclass;
1882 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1883 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1884 g_return_val_if_fail (GST_ELEMENT_CAST (bin) != element, FALSE);
1886 bclass = GST_BIN_GET_CLASS (bin);
1888 if (G_UNLIKELY (bclass->remove_element == NULL))
1891 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "removing element %s from bin %s",
1892 GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin));
1894 GST_TRACER_BIN_REMOVE_PRE (bin, element);
1895 result = bclass->remove_element (bin, element);
1896 GST_TRACER_BIN_REMOVE_POST (bin, result);
1900 /* ERROR handling */
1903 g_warning ("removing elements from bin '%s' is not supported",
1904 GST_ELEMENT_NAME (bin));
1910 * gst_bin_iterate_elements:
1913 * Gets an iterator for the elements in this bin.
1915 * MT safe. Caller owns returned value.
1917 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
1921 gst_bin_iterate_elements (GstBin * bin)
1923 GstIterator *result;
1925 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1927 GST_OBJECT_LOCK (bin);
1928 result = gst_iterator_new_list (GST_TYPE_ELEMENT,
1929 GST_OBJECT_GET_LOCK (bin),
1930 &bin->children_cookie, &bin->children, (GObject *) bin, NULL);
1931 GST_OBJECT_UNLOCK (bin);
1936 static GstIteratorItem
1937 iterate_child_recurse (GstIterator * it, const GValue * item)
1939 GstElement *child = g_value_get_object (item);
1941 if (GST_IS_BIN (child)) {
1942 GstIterator *other = gst_bin_iterate_recurse (GST_BIN_CAST (child));
1944 gst_iterator_push (it, other);
1946 return GST_ITERATOR_ITEM_PASS;
1950 * gst_bin_iterate_recurse:
1953 * Gets an iterator for the elements in this bin.
1954 * This iterator recurses into GstBin children.
1956 * MT safe. Caller owns returned value.
1958 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
1962 gst_bin_iterate_recurse (GstBin * bin)
1964 GstIterator *result;
1966 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1968 GST_OBJECT_LOCK (bin);
1969 result = gst_iterator_new_list (GST_TYPE_ELEMENT,
1970 GST_OBJECT_GET_LOCK (bin),
1971 &bin->children_cookie,
1973 (GObject *) bin, (GstIteratorItemFunction) iterate_child_recurse);
1974 GST_OBJECT_UNLOCK (bin);
1979 /* returns 0 when TRUE because this is a GCompareFunc */
1982 bin_element_is_sink (GstElement * child, GstBin * bin)
1986 /* we lock the child here for the remainder of the function to
1987 * get its name and flag safely. */
1988 GST_OBJECT_LOCK (child);
1989 is_sink = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SINK);
1991 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
1992 "child %s %s sink", GST_OBJECT_NAME (child), is_sink ? "is" : "is not");
1994 GST_OBJECT_UNLOCK (child);
1995 return is_sink ? 0 : 1;
1999 sink_iterator_filter (const GValue * vchild, GValue * vbin)
2001 GstBin *bin = g_value_get_object (vbin);
2002 GstElement *child = g_value_get_object (vchild);
2004 return (bin_element_is_sink (child, bin));
2008 * gst_bin_iterate_sinks:
2011 * Gets an iterator for all elements in the bin that have the
2012 * #GST_ELEMENT_FLAG_SINK flag set.
2014 * MT safe. Caller owns returned value.
2016 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
2020 gst_bin_iterate_sinks (GstBin * bin)
2022 GstIterator *children;
2023 GstIterator *result;
2024 GValue vbin = { 0, };
2026 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
2028 g_value_init (&vbin, GST_TYPE_BIN);
2029 g_value_set_object (&vbin, bin);
2031 children = gst_bin_iterate_elements (bin);
2032 result = gst_iterator_filter (children,
2033 (GCompareFunc) sink_iterator_filter, &vbin);
2035 g_value_unset (&vbin);
2040 /* returns 0 when TRUE because this is a GCompareFunc */
2043 bin_element_is_src (GstElement * child, GstBin * bin)
2047 /* we lock the child here for the remainder of the function to
2048 * get its name and other info safely. */
2049 GST_OBJECT_LOCK (child);
2050 is_src = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SOURCE);
2052 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
2053 "child %s %s src", GST_OBJECT_NAME (child), is_src ? "is" : "is not");
2055 GST_OBJECT_UNLOCK (child);
2056 return is_src ? 0 : 1;
2060 src_iterator_filter (const GValue * vchild, GValue * vbin)
2062 GstBin *bin = g_value_get_object (vbin);
2063 GstElement *child = g_value_get_object (vchild);
2065 return (bin_element_is_src (child, bin));
2069 * gst_bin_iterate_sources:
2072 * Gets an iterator for all elements in the bin that have the
2073 * #GST_ELEMENT_FLAG_SOURCE flag set.
2075 * MT safe. Caller owns returned value.
2077 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
2081 gst_bin_iterate_sources (GstBin * bin)
2083 GstIterator *children;
2084 GstIterator *result;
2085 GValue vbin = { 0, };
2087 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
2089 g_value_init (&vbin, GST_TYPE_BIN);
2090 g_value_set_object (&vbin, bin);
2092 children = gst_bin_iterate_elements (bin);
2093 result = gst_iterator_filter (children,
2094 (GCompareFunc) src_iterator_filter, &vbin);
2096 g_value_unset (&vbin);
2104 static GstStateChangeReturn
2105 gst_bin_get_state_func (GstElement * element, GstState * state,
2106 GstState * pending, GstClockTime timeout)
2108 GstStateChangeReturn ret;
2110 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
2113 GST_ELEMENT_CLASS (parent_class)->get_state (element, state, pending,
2119 /***********************************************
2120 * Topologically sorted iterator
2121 * see http://en.wikipedia.org/wiki/Topological_sorting
2123 * For each element in the graph, an entry is kept in a HashTable
2124 * with its number of srcpad connections (degree).
2125 * We then change state of all elements without dependencies
2126 * (degree 0) and decrement the degree of all elements connected
2127 * on the sinkpads. When an element reaches degree 0, its state is
2129 * When all elements are handled the algorithm stops.
2131 typedef struct _GstBinSortIterator
2134 GQueue queue; /* elements queued for state change */
2135 GstBin *bin; /* bin we iterate */
2136 gint mode; /* adding or removing dependency */
2137 GstElement *best; /* next element with least dependencies */
2138 gint best_deg; /* best degree */
2139 GHashTable *hash; /* hashtable with element dependencies */
2140 gboolean dirty; /* we detected structure change */
2141 } GstBinSortIterator;
2144 copy_to_queue (gpointer data, gpointer user_data)
2146 GstElement *element = data;
2147 GQueue *queue = user_data;
2149 gst_object_ref (element);
2150 g_queue_push_tail (queue, element);
2154 gst_bin_sort_iterator_copy (const GstBinSortIterator * it,
2155 GstBinSortIterator * copy)
2157 GHashTableIter iter;
2158 gpointer key, value;
2160 g_queue_init (©->queue);
2161 g_queue_foreach ((GQueue *) & it->queue, copy_to_queue, ©->queue);
2163 copy->bin = gst_object_ref (it->bin);
2165 copy->best = gst_object_ref (it->best);
2167 copy->hash = g_hash_table_new (NULL, NULL);
2168 g_hash_table_iter_init (&iter, it->hash);
2169 while (g_hash_table_iter_next (&iter, &key, &value))
2170 g_hash_table_insert (copy->hash, key, value);
2173 /* we add and subtract 1 to make sure we don't confuse NULL and 0 */
2174 #define HASH_SET_DEGREE(bit, elem, deg) \
2175 g_hash_table_replace (bit->hash, elem, GINT_TO_POINTER(deg+1))
2176 #define HASH_GET_DEGREE(bit, elem) \
2177 (GPOINTER_TO_INT(g_hash_table_lookup (bit->hash, elem))-1)
2179 /* add element to queue of next elements in the iterator.
2180 * We push at the tail to give higher priority elements a
2183 add_to_queue (GstBinSortIterator * bit, GstElement * element)
2185 GST_DEBUG_OBJECT (bit->bin, "adding '%s' to queue",
2186 GST_ELEMENT_NAME (element));
2187 gst_object_ref (element);
2188 g_queue_push_tail (&bit->queue, element);
2189 HASH_SET_DEGREE (bit, element, -1);
2193 remove_from_queue (GstBinSortIterator * bit, GstElement * element)
2197 if ((find = g_queue_find (&bit->queue, element))) {
2198 GST_DEBUG_OBJECT (bit->bin, "removing '%s' from queue",
2199 GST_ELEMENT_NAME (element));
2201 g_queue_delete_link (&bit->queue, find);
2202 gst_object_unref (element);
2204 GST_DEBUG_OBJECT (bit->bin, "unable to remove '%s' from queue",
2205 GST_ELEMENT_NAME (element));
2209 /* clear the queue, unref all objects as we took a ref when
2210 * we added them to the queue */
2212 clear_queue (GQueue * queue)
2216 while ((p = g_queue_pop_head (queue)))
2217 gst_object_unref (p);
2220 /* set all degrees to 0. Elements marked as a sink are
2221 * added to the queue immediately. Since we only look at the SINK flag of the
2222 * element, it is possible that we add non-sinks to the queue. These will be
2223 * removed from the queue again when we can prove that it provides data for some
2226 reset_degree (GstElement * element, GstBinSortIterator * bit)
2230 /* sinks are added right away */
2231 GST_OBJECT_LOCK (element);
2232 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
2233 GST_OBJECT_UNLOCK (element);
2236 add_to_queue (bit, element);
2238 /* others are marked with 0 and handled when sinks are done */
2239 HASH_SET_DEGREE (bit, element, 0);
2243 /* adjust the degree of all elements connected to the given
2244 * element. If a degree of an element drops to 0, it is
2245 * added to the queue of elements to schedule next.
2247 * We have to make sure not to cross the bin boundary this element
2251 update_degree (GstElement * element, GstBinSortIterator * bit)
2253 gboolean linked = FALSE;
2255 GST_OBJECT_LOCK (element);
2256 /* don't touch degree if element has no sinkpads */
2257 if (element->numsinkpads != 0) {
2258 /* loop over all sinkpads, decrement degree for all connected
2259 * elements in this bin */
2262 for (pads = element->sinkpads; pads; pads = g_list_next (pads)) {
2265 pad = GST_PAD_CAST (pads->data);
2267 /* we're iterating over the sinkpads, check if it's busy in a link/unlink */
2268 if (G_UNLIKELY (find_message (bit->bin, GST_OBJECT_CAST (pad),
2269 GST_MESSAGE_STRUCTURE_CHANGE))) {
2270 /* mark the iterator as dirty because we won't be updating the degree
2271 * of the peer parent now. This would result in the 'loop detected'
2272 * later on because the peer parent element could become the best next
2273 * element with a degree > 0. We will simply continue our state
2274 * changes and we'll eventually resync when the unlink completed and
2275 * the iterator cookie is updated. */
2280 if ((peer = gst_pad_get_peer (pad))) {
2281 GstElement *peer_element;
2283 if ((peer_element = gst_pad_get_parent_element (peer))) {
2284 GST_OBJECT_LOCK (peer_element);
2285 /* check that we don't go outside of this bin */
2286 if (GST_OBJECT_CAST (peer_element)->parent ==
2287 GST_OBJECT_CAST (bit->bin)) {
2288 gint old_deg, new_deg;
2290 old_deg = HASH_GET_DEGREE (bit, peer_element);
2292 /* check to see if we added an element as sink that was not really a
2293 * sink because it was connected to some other element. */
2294 if (old_deg == -1) {
2295 remove_from_queue (bit, peer_element);
2298 new_deg = old_deg + bit->mode;
2300 GST_DEBUG_OBJECT (bit->bin,
2301 "change element %s, degree %d->%d, linked to %s",
2302 GST_ELEMENT_NAME (peer_element), old_deg, new_deg,
2303 GST_ELEMENT_NAME (element));
2305 /* update degree, it is possible that an element was in 0 and
2306 * reaches -1 here. This would mean that the element had no sinkpads
2307 * but became linked while the state change was happening. We will
2308 * resync on this with the structure change message. */
2310 /* degree hit 0, add to queue */
2311 add_to_queue (bit, peer_element);
2313 HASH_SET_DEGREE (bit, peer_element, new_deg);
2317 GST_OBJECT_UNLOCK (peer_element);
2318 gst_object_unref (peer_element);
2320 gst_object_unref (peer);
2325 GST_DEBUG_OBJECT (bit->bin, "element %s not linked on any sinkpads",
2326 GST_ELEMENT_NAME (element));
2328 GST_OBJECT_UNLOCK (element);
2331 /* find the next best element not handled yet. This is the one
2332 * with the lowest non-negative degree */
2334 find_element (GstElement * element, GstBinSortIterator * bit)
2338 /* element is already handled */
2339 if ((degree = HASH_GET_DEGREE (bit, element)) < 0)
2342 /* first element or element with smaller degree */
2343 if (bit->best == NULL || bit->best_deg > degree) {
2344 bit->best = element;
2345 bit->best_deg = degree;
2349 /* get next element in iterator. */
2350 static GstIteratorResult
2351 gst_bin_sort_iterator_next (GstBinSortIterator * bit, GValue * result)
2354 GstBin *bin = bit->bin;
2356 /* empty queue, we have to find a next best element */
2357 if (g_queue_is_empty (&bit->queue)) {
2359 bit->best_deg = G_MAXINT;
2360 g_list_foreach (bin->children, (GFunc) find_element, bit);
2361 if ((best = bit->best)) {
2362 /* when we detected an unlink, don't warn because our degrees might be
2363 * screwed up. We will resync later */
2364 if (bit->best_deg != 0 && !bit->dirty) {
2365 /* we don't fail on this one yet */
2366 GST_WARNING_OBJECT (bin, "loop dected in graph");
2367 g_warning ("loop detected in the graph of bin '%s'!!",
2368 GST_ELEMENT_NAME (bin));
2370 /* best unhandled element, schedule as next element */
2371 GST_DEBUG_OBJECT (bin, "queue empty, next best: %s",
2372 GST_ELEMENT_NAME (best));
2373 HASH_SET_DEGREE (bit, best, -1);
2374 g_value_set_object (result, best);
2376 GST_DEBUG_OBJECT (bin, "queue empty, elements exhausted");
2377 /* no more unhandled elements, we are done */
2378 return GST_ITERATOR_DONE;
2381 /* everything added to the queue got reffed */
2382 best = g_queue_pop_head (&bit->queue);
2383 g_value_set_object (result, best);
2384 gst_object_unref (best);
2387 GST_DEBUG_OBJECT (bin, "queue head gives %s", GST_ELEMENT_NAME (best));
2388 /* update degrees of linked elements */
2389 update_degree (best, bit);
2391 return GST_ITERATOR_OK;
2394 /* clear queues, recalculate the degrees and restart. */
2396 gst_bin_sort_iterator_resync (GstBinSortIterator * bit)
2398 GstBin *bin = bit->bin;
2400 GST_DEBUG_OBJECT (bin, "resync");
2402 clear_queue (&bit->queue);
2404 g_list_foreach (bin->children, (GFunc) reset_degree, bit);
2405 /* calc degrees, incrementing */
2407 g_list_foreach (bin->children, (GFunc) update_degree, bit);
2408 /* for the rest of the function we decrement the degrees */
2412 /* clear queues, unref bin and free iterator. */
2414 gst_bin_sort_iterator_free (GstBinSortIterator * bit)
2416 GstBin *bin = bit->bin;
2418 GST_DEBUG_OBJECT (bin, "free");
2419 clear_queue (&bit->queue);
2420 g_hash_table_destroy (bit->hash);
2421 gst_object_unref (bin);
2424 /* should be called with the bin LOCK held */
2425 static GstIterator *
2426 gst_bin_sort_iterator_new (GstBin * bin)
2428 GstBinSortIterator *result;
2430 /* we don't need an ItemFunction because we ref the items in the _next
2432 result = (GstBinSortIterator *)
2433 gst_iterator_new (sizeof (GstBinSortIterator),
2435 GST_OBJECT_GET_LOCK (bin),
2436 &bin->priv->structure_cookie,
2437 (GstIteratorCopyFunction) gst_bin_sort_iterator_copy,
2438 (GstIteratorNextFunction) gst_bin_sort_iterator_next,
2439 (GstIteratorItemFunction) NULL,
2440 (GstIteratorResyncFunction) gst_bin_sort_iterator_resync,
2441 (GstIteratorFreeFunction) gst_bin_sort_iterator_free);
2442 g_queue_init (&result->queue);
2443 result->hash = g_hash_table_new (NULL, NULL);
2444 gst_object_ref (bin);
2446 gst_bin_sort_iterator_resync (result);
2448 return (GstIterator *) result;
2452 * gst_bin_iterate_sorted:
2455 * Gets an iterator for the elements in this bin in topologically
2456 * sorted order. This means that the elements are returned from
2457 * the most downstream elements (sinks) to the sources.
2459 * This function is used internally to perform the state changes
2460 * of the bin elements and for clock selection.
2462 * MT safe. Caller owns returned value.
2464 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
2468 gst_bin_iterate_sorted (GstBin * bin)
2470 GstIterator *result;
2472 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
2474 GST_OBJECT_LOCK (bin);
2475 result = gst_bin_sort_iterator_new (bin);
2476 GST_OBJECT_UNLOCK (bin);
2481 static GstStateChangeReturn
2482 gst_bin_element_set_state (GstBin * bin, GstElement * element,
2483 GstClockTime base_time, GstClockTime start_time, GstState current,
2486 GstStateChangeReturn ret;
2487 GstState child_current, child_pending;
2491 GST_STATE_LOCK (element);
2493 GST_OBJECT_LOCK (element);
2494 /* set base_time and start time on child */
2495 GST_ELEMENT_START_TIME (element) = start_time;
2496 element->base_time = base_time;
2497 /* peel off the locked flag */
2498 locked = GST_ELEMENT_IS_LOCKED_STATE (element);
2499 /* Get the previous set_state result to preserve NO_PREROLL and ASYNC */
2500 ret = GST_STATE_RETURN (element);
2501 child_current = GST_STATE (element);
2502 child_pending = GST_STATE_PENDING (element);
2503 GST_OBJECT_UNLOCK (element);
2505 /* skip locked elements */
2506 if (G_UNLIKELY (locked))
2509 /* if the element was no preroll, just start changing the state regardless
2510 * if it had async elements (in the case of a bin) because they won't preroll
2512 if (G_UNLIKELY (ret == GST_STATE_CHANGE_NO_PREROLL)) {
2513 GST_DEBUG_OBJECT (element, "element is NO_PREROLL, ignore async elements");
2517 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2518 "current %s pending %s, desired next %s",
2519 gst_element_state_get_name (child_current),
2520 gst_element_state_get_name (child_pending),
2521 gst_element_state_get_name (next));
2523 /* always recurse into bins so that we can set the base time */
2524 if (GST_IS_BIN (element))
2527 /* Try not to change the state of elements that are already in the state we're
2529 if (child_current == next && child_pending == GST_STATE_VOID_PENDING) {
2530 /* child is already at the requested state, return previous return. Note that
2531 * if the child has a pending state to next, we will still call the
2532 * set_state function */
2534 } else if (next > current) {
2535 /* upward state change */
2536 if (child_pending == GST_STATE_VOID_PENDING) {
2537 /* .. and the child is not busy doing anything */
2538 if (child_current > next) {
2539 /* .. and is already past the requested state, assume it got there
2541 ret = GST_STATE_CHANGE_SUCCESS;
2544 } else if (child_pending > child_current) {
2545 /* .. and the child is busy going upwards */
2546 if (child_current >= next) {
2547 /* .. and is already past the requested state, assume it got there
2549 ret = GST_STATE_CHANGE_SUCCESS;
2553 /* .. and the child is busy going downwards */
2554 if (child_current > next) {
2555 /* .. and is already past the requested state, assume it got there
2557 ret = GST_STATE_CHANGE_SUCCESS;
2561 } else if (next < current) {
2562 /* downward state change */
2563 if (child_pending == GST_STATE_VOID_PENDING) {
2564 /* .. and the child is not busy doing anything */
2565 if (child_current < next) {
2566 /* .. and is already past the requested state, assume it got there
2568 ret = GST_STATE_CHANGE_SUCCESS;
2571 } else if (child_pending < child_current) {
2572 /* .. and the child is busy going downwards */
2573 if (child_current <= next) {
2574 /* .. and is already past the requested state, assume it got there
2576 ret = GST_STATE_CHANGE_SUCCESS;
2580 /* .. and the child is busy going upwards */
2581 if (child_current < next) {
2582 /* .. and is already past the requested state, assume it got there
2584 ret = GST_STATE_CHANGE_SUCCESS;
2591 GST_OBJECT_LOCK (bin);
2592 /* the element was busy with an upwards async state change, we must wait for
2593 * an ASYNC_DONE message before we attemp to change the state. */
2595 find_message (bin, GST_OBJECT_CAST (element),
2596 GST_MESSAGE_ASYNC_START))) {
2597 #ifndef GST_DISABLE_GST_DEBUG
2598 GstMessage *message = GST_MESSAGE_CAST (found->data);
2600 GST_DEBUG_OBJECT (element, "element message %p, %s async busy",
2601 message, GST_ELEMENT_NAME (GST_MESSAGE_SRC (message)));
2603 /* only wait for upward state changes */
2604 if (next > current) {
2605 /* We found an async element check if we can force its state to change or
2606 * if we have to wait for it to preroll. */
2610 GST_OBJECT_UNLOCK (bin);
2613 GST_DEBUG_OBJECT (bin,
2614 "setting element %s to %s, base_time %" GST_TIME_FORMAT,
2615 GST_ELEMENT_NAME (element), gst_element_state_get_name (next),
2616 GST_TIME_ARGS (base_time));
2619 ret = gst_element_set_state (element, next);
2621 GST_STATE_UNLOCK (element);
2627 GST_DEBUG_OBJECT (element,
2628 "element is locked, return previous return %s",
2629 gst_element_state_change_return_get_name (ret));
2630 GST_STATE_UNLOCK (element);
2635 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2636 "skipping transition from %s to %s",
2637 gst_element_state_get_name (child_current),
2638 gst_element_state_get_name (next));
2639 GST_STATE_UNLOCK (element);
2644 GST_DEBUG_OBJECT (element, "element was busy, delaying state change");
2645 GST_OBJECT_UNLOCK (bin);
2646 GST_STATE_UNLOCK (element);
2647 return GST_STATE_CHANGE_ASYNC;
2651 /* gst_iterator_fold functions for pads_activate
2652 * Stop the iterator if activating one pad failed, but only if that pad
2653 * has not been removed from the element. */
2655 activate_pads (const GValue * vpad, GValue * ret, gboolean * active)
2657 GstPad *pad = g_value_get_object (vpad);
2658 gboolean cont = TRUE;
2660 if (!gst_pad_set_active (pad, *active)) {
2661 if (GST_PAD_PARENT (pad) != NULL) {
2663 g_value_set_boolean (ret, FALSE);
2670 /* returns false on error or early cutout of the fold, true if all
2671 * pads in @iter were (de)activated successfully. */
2673 iterator_activate_fold_with_resync (GstIterator * iter, gpointer user_data)
2675 GstIteratorResult ires;
2678 /* no need to unset this later, it's just a boolean */
2679 g_value_init (&ret, G_TYPE_BOOLEAN);
2680 g_value_set_boolean (&ret, TRUE);
2683 ires = gst_iterator_fold (iter, (GstIteratorFoldFunction) activate_pads,
2686 case GST_ITERATOR_RESYNC:
2687 /* need to reset the result again */
2688 g_value_set_boolean (&ret, TRUE);
2689 gst_iterator_resync (iter);
2691 case GST_ITERATOR_DONE:
2692 /* all pads iterated, return collected value */
2695 /* iterator returned _ERROR or premature end with _OK,
2696 * mark an error and exit */
2697 g_value_set_boolean (&ret, FALSE);
2702 /* return collected value */
2703 return g_value_get_boolean (&ret);
2706 /* is called with STATE_LOCK
2709 gst_bin_src_pads_activate (GstBin * bin, gboolean active)
2714 GST_DEBUG_OBJECT (bin, "%s pads", active ? "activate" : "deactivate");
2716 iter = gst_element_iterate_src_pads ((GstElement *) bin);
2717 fold_ok = iterator_activate_fold_with_resync (iter, &active);
2718 gst_iterator_free (iter);
2719 if (G_UNLIKELY (!fold_ok))
2722 GST_DEBUG_OBJECT (bin, "pad %sactivation successful", active ? "" : "de");
2729 GST_DEBUG_OBJECT (bin, "pad %sactivation failed", active ? "" : "de");
2735 * gst_bin_recalculate_latency:
2738 * Query @bin for the current latency using and reconfigures this latency to all the
2739 * elements with a LATENCY event.
2741 * This method is typically called on the pipeline when a #GST_MESSAGE_LATENCY
2742 * is posted on the bus.
2744 * This function simply emits the 'do-latency' signal so any custom latency
2745 * calculations will be performed.
2747 * Returns: %TRUE if the latency could be queried and reconfigured.
2750 gst_bin_recalculate_latency (GstBin * bin)
2754 g_signal_emit (bin, gst_bin_signals[DO_LATENCY], 0, &res);
2755 GST_DEBUG_OBJECT (bin, "latency returned %d", res);
2761 gst_bin_do_latency_func (GstBin * bin)
2764 GstElement *element;
2765 GstClockTime min_latency, max_latency;
2768 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2770 element = GST_ELEMENT_CAST (bin);
2772 GST_DEBUG_OBJECT (element, "querying latency");
2774 query = gst_query_new_latency ();
2775 if ((res = gst_element_query (element, query))) {
2778 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
2780 GST_DEBUG_OBJECT (element,
2781 "got min latency %" GST_TIME_FORMAT ", max latency %"
2782 GST_TIME_FORMAT ", live %d", GST_TIME_ARGS (min_latency),
2783 GST_TIME_ARGS (max_latency), live);
2785 if (max_latency < min_latency) {
2786 /* this is an impossible situation, some parts of the pipeline might not
2787 * work correctly. We post a warning for now. */
2788 GST_ELEMENT_WARNING (element, CORE, CLOCK, (NULL),
2789 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
2790 GST_TIME_FORMAT ". Add queues or other buffering elements.",
2791 GST_TIME_ARGS (max_latency), GST_TIME_ARGS (min_latency)));
2794 /* configure latency on elements */
2795 res = gst_element_send_event (element, gst_event_new_latency (min_latency));
2797 GST_INFO_OBJECT (element, "configured latency of %" GST_TIME_FORMAT,
2798 GST_TIME_ARGS (min_latency));
2800 GST_WARNING_OBJECT (element,
2801 "did not really configure latency of %" GST_TIME_FORMAT,
2802 GST_TIME_ARGS (min_latency));
2805 /* this is not a real problem, we just don't configure any latency. */
2806 GST_WARNING_OBJECT (element, "failed to query latency");
2808 gst_query_unref (query);
2814 gst_bin_post_message (GstElement * element, GstMessage * msg)
2816 GstElementClass *pklass = (GstElementClass *) parent_class;
2819 ret = pklass->post_message (element, gst_message_ref (msg));
2821 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_STATE_CHANGED &&
2822 GST_MESSAGE_SRC (msg) == GST_OBJECT_CAST (element)) {
2823 GstState newstate, pending;
2825 gst_message_parse_state_changed (msg, NULL, &newstate, &pending);
2826 if (newstate == GST_STATE_PLAYING && pending == GST_STATE_VOID_PENDING) {
2827 GST_BIN_CAST (element)->priv->posted_playing = TRUE;
2828 bin_do_eos (GST_BIN_CAST (element));
2830 GST_BIN_CAST (element)->priv->posted_playing = FALSE;
2834 gst_message_unref (msg);
2840 reset_state (const GValue * data, gpointer user_data)
2842 GstElement *e = g_value_get_object (data);
2843 GstState state = GPOINTER_TO_INT (user_data);
2845 if (gst_element_set_state (e, state) == GST_STATE_CHANGE_FAILURE)
2846 GST_WARNING_OBJECT (e, "Failed to switch back down to %s",
2847 gst_element_state_get_name (state));
2850 static GstStateChangeReturn
2851 gst_bin_change_state_func (GstElement * element, GstStateChange transition)
2854 GstStateChangeReturn ret;
2855 GstState current, next;
2856 gboolean have_async;
2857 gboolean have_no_preroll;
2858 GstClockTime base_time, start_time;
2861 GValue data = { 0, };
2863 /* we don't need to take the STATE_LOCK, it is already taken */
2864 current = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2865 next = (GstState) GST_STATE_TRANSITION_NEXT (transition);
2867 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2868 "changing state of children from %s to %s",
2869 gst_element_state_get_name (current), gst_element_state_get_name (next));
2871 bin = GST_BIN_CAST (element);
2874 case GST_STATE_PLAYING:
2876 gboolean toplevel, asynchandling;
2878 GST_OBJECT_LOCK (bin);
2879 toplevel = BIN_IS_TOPLEVEL (bin);
2880 asynchandling = bin->priv->asynchandling;
2881 GST_OBJECT_UNLOCK (bin);
2884 gst_bin_recalculate_latency (bin);
2886 gst_element_post_message (element,
2887 gst_message_new_latency (GST_OBJECT_CAST (element)));
2890 case GST_STATE_PAUSED:
2891 /* Clear EOS list on next PAUSED */
2892 GST_OBJECT_LOCK (bin);
2893 GST_DEBUG_OBJECT (element, "clearing EOS elements");
2894 bin_remove_messages (bin, NULL, GST_MESSAGE_EOS);
2895 bin->priv->posted_eos = FALSE;
2896 if (current == GST_STATE_READY)
2897 bin_remove_messages (bin, NULL, GST_MESSAGE_STREAM_START);
2898 GST_OBJECT_UNLOCK (bin);
2899 if (current == GST_STATE_READY)
2900 if (!(gst_bin_src_pads_activate (bin, TRUE)))
2901 goto activate_failure;
2903 case GST_STATE_READY:
2904 /* Clear message list on next READY */
2905 GST_OBJECT_LOCK (bin);
2906 GST_DEBUG_OBJECT (element, "clearing all cached messages");
2907 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
2908 GST_OBJECT_UNLOCK (bin);
2909 /* We might not have reached PAUSED yet due to async errors,
2910 * make sure to always deactivate the pads nonetheless */
2911 if (!(gst_bin_src_pads_activate (bin, FALSE)))
2912 goto activate_failure;
2914 case GST_STATE_NULL:
2915 /* Clear message list on next NULL */
2916 GST_OBJECT_LOCK (bin);
2917 GST_DEBUG_OBJECT (element, "clearing all cached messages");
2918 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
2919 GST_OBJECT_UNLOCK (bin);
2920 if (current == GST_STATE_READY) {
2921 if (!(gst_bin_src_pads_activate (bin, FALSE)))
2922 goto activate_failure;
2929 /* this flag is used to make the async state changes return immediately. We
2930 * don't want them to interfere with this state change */
2931 GST_OBJECT_LOCK (bin);
2932 bin->polling = TRUE;
2933 GST_OBJECT_UNLOCK (bin);
2935 /* iterate in state change order */
2936 it = gst_bin_iterate_sorted (bin);
2938 /* mark if we've seen an ASYNC element in the bin when we did a state change.
2939 * Note how we don't reset this value when a resync happens, the reason being
2940 * that the async element posted ASYNC_START and we want to post ASYNC_DONE
2941 * even after a resync when the async element is gone */
2945 /* take base_time */
2946 base_time = gst_element_get_base_time (element);
2947 start_time = gst_element_get_start_time (element);
2949 have_no_preroll = FALSE;
2953 switch (gst_iterator_next (it, &data)) {
2954 case GST_ITERATOR_OK:
2958 child = g_value_get_object (&data);
2960 /* set state and base_time now */
2961 ret = gst_bin_element_set_state (bin, child, base_time, start_time,
2965 case GST_STATE_CHANGE_SUCCESS:
2966 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2967 "child '%s' changed state to %d(%s) successfully",
2968 GST_ELEMENT_NAME (child), next,
2969 gst_element_state_get_name (next));
2971 case GST_STATE_CHANGE_ASYNC:
2973 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2974 "child '%s' is changing state asynchronously to %s",
2975 GST_ELEMENT_NAME (child), gst_element_state_get_name (next));
2979 case GST_STATE_CHANGE_FAILURE:{
2982 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2983 "child '%s' failed to go to state %d(%s)",
2984 GST_ELEMENT_NAME (child),
2985 next, gst_element_state_get_name (next));
2987 /* Only fail if the child is still inside
2988 * this bin. It might've been removed already
2989 * because of the error by the bin subclass
2990 * to ignore the error. */
2991 parent = gst_object_get_parent (GST_OBJECT_CAST (child));
2992 if (parent == GST_OBJECT_CAST (element)) {
2993 /* element is still in bin, really error now */
2994 gst_object_unref (parent);
2997 /* child removed from bin, let the resync code redo the state
2999 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
3000 "child '%s' was removed from the bin",
3001 GST_ELEMENT_NAME (child));
3004 gst_object_unref (parent);
3008 case GST_STATE_CHANGE_NO_PREROLL:
3009 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
3010 "child '%s' changed state to %d(%s) successfully without preroll",
3011 GST_ELEMENT_NAME (child), next,
3012 gst_element_state_get_name (next));
3013 have_no_preroll = TRUE;
3016 g_assert_not_reached ();
3019 g_value_reset (&data);
3022 case GST_ITERATOR_RESYNC:
3023 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "iterator doing resync");
3024 gst_iterator_resync (it);
3027 case GST_ITERATOR_DONE:
3028 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "iterator done");
3034 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3035 if (G_UNLIKELY (ret == GST_STATE_CHANGE_FAILURE))
3038 if (have_no_preroll) {
3039 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
3040 "we have NO_PREROLL elements %s -> NO_PREROLL",
3041 gst_element_state_change_return_get_name (ret));
3042 ret = GST_STATE_CHANGE_NO_PREROLL;
3043 } else if (have_async) {
3044 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
3045 "we have ASYNC elements %s -> ASYNC",
3046 gst_element_state_change_return_get_name (ret));
3047 ret = GST_STATE_CHANGE_ASYNC;
3051 g_value_unset (&data);
3052 gst_iterator_free (it);
3054 GST_OBJECT_LOCK (bin);
3055 bin->polling = FALSE;
3056 /* it's possible that we did not get ASYNC from the children while the bin is
3057 * simulating ASYNC behaviour by posting an ASYNC_DONE message on the bus with
3058 * itself as the source. In that case we still want to check if the state
3059 * change completed. */
3060 if (ret != GST_STATE_CHANGE_ASYNC && !bin->priv->pending_async_done) {
3061 /* no element returned ASYNC and there are no pending async_done messages,
3062 * we can just complete. */
3063 GST_DEBUG_OBJECT (bin, "no async elements");
3066 /* when we get here an ASYNC element was found */
3067 if (GST_STATE_TARGET (bin) <= GST_STATE_READY) {
3068 /* we ignore ASYNC state changes when we go to READY or NULL */
3069 GST_DEBUG_OBJECT (bin, "target state %s <= READY",
3070 gst_element_state_get_name (GST_STATE_TARGET (bin)));
3074 GST_DEBUG_OBJECT (bin, "check async elements");
3075 /* check if all elements managed to commit their state already */
3076 if (!find_message (bin, NULL, GST_MESSAGE_ASYNC_START)) {
3077 /* nothing found, remove all old ASYNC_DONE messages. This can happen when
3078 * all the elements commited their state while we were doing the state
3079 * change. We will still return ASYNC for consistency but we commit the
3080 * state already so that a _get_state() will return immediately. */
3081 bin_remove_messages (bin, NULL, GST_MESSAGE_ASYNC_DONE);
3083 GST_DEBUG_OBJECT (bin, "async elements commited");
3084 bin_handle_async_done (bin, GST_STATE_CHANGE_SUCCESS, FALSE,
3085 GST_CLOCK_TIME_NONE);
3089 bin->priv->pending_async_done = FALSE;
3090 GST_OBJECT_UNLOCK (bin);
3092 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3093 "done changing bin's state from %s to %s, now in %s, ret %s",
3094 gst_element_state_get_name (current),
3095 gst_element_state_get_name (next),
3096 gst_element_state_get_name (GST_STATE (element)),
3097 gst_element_state_change_return_get_name (ret));
3104 GST_CAT_WARNING_OBJECT (GST_CAT_STATES, element,
3105 "failure (de)activating src pads");
3106 return GST_STATE_CHANGE_FAILURE;
3111 if (current < next) {
3112 GstIterator *it = gst_bin_iterate_sorted (GST_BIN (element));
3113 GstIteratorResult ret;
3115 GST_DEBUG_OBJECT (element,
3116 "Bin failed to change state, switching children back to %s",
3117 gst_element_state_get_name (current));
3120 gst_iterator_foreach (it, &reset_state, GINT_TO_POINTER (current));
3121 if (ret != GST_ITERATOR_RESYNC)
3123 gst_iterator_resync (it);
3125 gst_iterator_free (it);
3132 * This function is a utility event handler for seek events.
3133 * It will send the event to all sinks or sources and appropriate
3134 * ghost pads depending on the event-direction.
3136 * Applications are free to override this behaviour and
3137 * implement their own seek handler, but this will work for
3138 * pretty much all cases in practice.
3141 gst_bin_send_event (GstElement * element, GstEvent * event)
3143 GstBin *bin = GST_BIN_CAST (element);
3145 gboolean res = TRUE;
3146 gboolean done = FALSE;
3147 GValue data = { 0, };
3149 if (GST_EVENT_IS_DOWNSTREAM (event)) {
3150 iter = gst_bin_iterate_sources (bin);
3151 GST_DEBUG_OBJECT (bin, "Sending %s event to src children",
3152 GST_EVENT_TYPE_NAME (event));
3154 iter = gst_bin_iterate_sinks (bin);
3155 GST_DEBUG_OBJECT (bin, "Sending %s event to sink children",
3156 GST_EVENT_TYPE_NAME (event));
3160 switch (gst_iterator_next (iter, &data)) {
3161 case GST_ITERATOR_OK:
3163 GstElement *child = g_value_get_object (&data);
3165 gst_event_ref (event);
3166 res &= gst_element_send_event (child, event);
3168 GST_LOG_OBJECT (child, "After handling %s event: %d",
3169 GST_EVENT_TYPE_NAME (event), res);
3171 g_value_reset (&data);
3174 case GST_ITERATOR_RESYNC:
3175 gst_iterator_resync (iter);
3178 case GST_ITERATOR_DONE:
3181 case GST_ITERATOR_ERROR:
3182 g_assert_not_reached ();
3186 g_value_unset (&data);
3187 gst_iterator_free (iter);
3189 if (GST_EVENT_IS_DOWNSTREAM (event)) {
3190 iter = gst_element_iterate_sink_pads (GST_ELEMENT (bin));
3191 GST_DEBUG_OBJECT (bin, "Sending %s event to sink pads",
3192 GST_EVENT_TYPE_NAME (event));
3194 iter = gst_element_iterate_src_pads (GST_ELEMENT (bin));
3195 GST_DEBUG_OBJECT (bin, "Sending %s event to src pads",
3196 GST_EVENT_TYPE_NAME (event));
3201 switch (gst_iterator_next (iter, &data)) {
3202 case GST_ITERATOR_OK:
3204 GstPad *pad = g_value_get_object (&data);
3206 gst_event_ref (event);
3207 res &= gst_pad_send_event (pad, event);
3208 GST_LOG_OBJECT (pad, "After handling %s event: %d",
3209 GST_EVENT_TYPE_NAME (event), res);
3212 case GST_ITERATOR_RESYNC:
3213 gst_iterator_resync (iter);
3216 case GST_ITERATOR_DONE:
3219 case GST_ITERATOR_ERROR:
3220 g_assert_not_reached ();
3225 g_value_unset (&data);
3226 gst_iterator_free (iter);
3227 gst_event_unref (event);
3232 /* this is the function called by the threadpool. When async elements commit
3233 * their state, this function will attempt to bring the bin to the next state.
3236 gst_bin_continue_func (GstBin * bin, BinContinueData * data)
3238 GstState current, next, pending;
3239 GstStateChange transition;
3241 pending = data->pending;
3243 GST_DEBUG_OBJECT (bin, "waiting for state lock");
3244 GST_STATE_LOCK (bin);
3246 GST_DEBUG_OBJECT (bin, "doing state continue");
3247 GST_OBJECT_LOCK (bin);
3249 /* if a new state change happened after this thread was scheduled, we return
3251 if (data->cookie != GST_ELEMENT_CAST (bin)->state_cookie)
3254 current = GST_STATE (bin);
3255 next = GST_STATE_GET_NEXT (current, pending);
3256 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
3258 GST_STATE_NEXT (bin) = next;
3259 GST_STATE_PENDING (bin) = pending;
3261 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3262 GST_OBJECT_UNLOCK (bin);
3264 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3265 "continue state change %s to %s, final %s",
3266 gst_element_state_get_name (current),
3267 gst_element_state_get_name (next), gst_element_state_get_name (pending));
3269 gst_element_change_state (GST_ELEMENT_CAST (bin), transition);
3271 GST_STATE_UNLOCK (bin);
3272 GST_DEBUG_OBJECT (bin, "state continue done");
3278 GST_OBJECT_UNLOCK (bin);
3279 GST_STATE_UNLOCK (bin);
3280 GST_DEBUG_OBJECT (bin, "state continue aborted due to intervening change");
3285 static GstBusSyncReply
3286 bin_bus_handler (GstBus * bus, GstMessage * message, GstBin * bin)
3288 GstBinClass *bclass;
3290 bclass = GST_BIN_GET_CLASS (bin);
3291 if (bclass->handle_message)
3292 bclass->handle_message (bin, message);
3294 gst_message_unref (message);
3296 return GST_BUS_DROP;
3300 free_bin_continue_data (BinContinueData * data)
3302 g_slice_free (BinContinueData, data);
3306 bin_push_state_continue (GstBin * bin, BinContinueData * data)
3308 GST_DEBUG_OBJECT (bin, "pushing continue on thread pool");
3309 gst_element_call_async (GST_ELEMENT_CAST (bin),
3310 (GstElementCallAsyncFunc) gst_bin_continue_func, data,
3311 (GDestroyNotify) free_bin_continue_data);
3314 /* an element started an async state change, if we were not busy with a state
3315 * change, we perform a lost state.
3316 * This function is called with the OBJECT lock.
3319 bin_handle_async_start (GstBin * bin)
3321 GstState old_state, new_state;
3323 GstMessage *amessage = NULL;
3325 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_FAILURE)
3328 /* get our toplevel state */
3329 toplevel = BIN_IS_TOPLEVEL (bin);
3331 /* prepare an ASYNC_START message, we always post the start message even if we
3332 * are busy with a state change or when we are NO_PREROLL. */
3334 /* non toplevel bin, prepare async-start for the parent */
3335 amessage = gst_message_new_async_start (GST_OBJECT_CAST (bin));
3337 if (bin->polling || GST_STATE_PENDING (bin) != GST_STATE_VOID_PENDING)
3340 /* async starts are ignored when we are NO_PREROLL */
3341 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_NO_PREROLL)
3342 goto was_no_preroll;
3344 old_state = GST_STATE (bin);
3346 /* when we PLAYING we go back to PAUSED, when preroll happens, we go back to
3347 * PLAYING after optionally redistributing the base_time. */
3348 if (old_state > GST_STATE_PAUSED)
3349 new_state = GST_STATE_PAUSED;
3351 new_state = old_state;
3353 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
3354 "lost state of %s, new %s", gst_element_state_get_name (old_state),
3355 gst_element_state_get_name (new_state));
3357 GST_STATE (bin) = new_state;
3358 GST_STATE_NEXT (bin) = new_state;
3359 GST_STATE_PENDING (bin) = new_state;
3360 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3361 GST_OBJECT_UNLOCK (bin);
3364 _priv_gst_element_state_changed (GST_ELEMENT_CAST (bin), new_state, new_state,
3369 /* post our ASYNC_START. */
3370 GST_DEBUG_OBJECT (bin, "posting ASYNC_START to parent");
3371 gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
3373 GST_OBJECT_LOCK (bin);
3379 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "we had an error");
3384 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "state change busy");
3385 GST_OBJECT_UNLOCK (bin);
3390 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "ignoring, we are NO_PREROLL");
3391 GST_OBJECT_UNLOCK (bin);
3396 /* this function is called when there are no more async elements in the bin. We
3397 * post a state changed message and an ASYNC_DONE message.
3398 * This function is called with the OBJECT lock.
3401 bin_handle_async_done (GstBin * bin, GstStateChangeReturn ret,
3402 gboolean flag_pending, GstClockTime running_time)
3404 GstState current, pending, target;
3405 GstStateChangeReturn old_ret;
3406 GstState old_state, old_next;
3407 gboolean toplevel, state_changed = FALSE;
3408 GstMessage *amessage = NULL;
3409 BinContinueData *cont = NULL;
3411 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_FAILURE)
3414 pending = GST_STATE_PENDING (bin);
3419 /* check if there is something to commit */
3420 if (pending == GST_STATE_VOID_PENDING)
3421 goto nothing_pending;
3423 old_ret = GST_STATE_RETURN (bin);
3424 GST_STATE_RETURN (bin) = ret;
3426 /* move to the next target state */
3427 target = GST_STATE_TARGET (bin);
3428 pending = GST_STATE_PENDING (bin) = target;
3430 amessage = gst_message_new_async_done (GST_OBJECT_CAST (bin), running_time);
3432 old_state = GST_STATE (bin);
3433 /* this is the state we should go to next */
3434 old_next = GST_STATE_NEXT (bin);
3436 if (old_next != GST_STATE_PLAYING) {
3437 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3438 "committing state from %s to %s, old pending %s",
3439 gst_element_state_get_name (old_state),
3440 gst_element_state_get_name (old_next),
3441 gst_element_state_get_name (pending));
3443 /* update current state */
3444 current = GST_STATE (bin) = old_next;
3446 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3447 "setting state from %s to %s, pending %s",
3448 gst_element_state_get_name (old_state),
3449 gst_element_state_get_name (old_state),
3450 gst_element_state_get_name (pending));
3451 current = old_state;
3454 /* get our toplevel state */
3455 toplevel = BIN_IS_TOPLEVEL (bin);
3457 /* see if we reached the final state. If we are not toplevel, we also have to
3458 * stop here, the parent will continue our state. */
3459 if ((pending == current) || !toplevel) {
3460 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3461 "completed state change, pending VOID");
3463 /* mark VOID pending */
3464 pending = GST_STATE_VOID_PENDING;
3465 GST_STATE_PENDING (bin) = pending;
3466 GST_STATE_NEXT (bin) = GST_STATE_VOID_PENDING;
3468 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3469 "continue state change, pending %s",
3470 gst_element_state_get_name (pending));
3472 cont = g_slice_new (BinContinueData);
3474 /* cookie to detect concurrent state change */
3475 cont->cookie = GST_ELEMENT_CAST (bin)->state_cookie;
3476 /* pending target state */
3477 cont->pending = pending;
3479 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3480 GST_STATE_NEXT (bin) = GST_STATE_GET_NEXT (old_state, pending);
3483 if (old_next != GST_STATE_PLAYING) {
3484 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC) {
3485 state_changed = TRUE;
3488 GST_OBJECT_UNLOCK (bin);
3490 if (state_changed) {
3491 _priv_gst_element_state_changed (GST_ELEMENT_CAST (bin), old_state,
3495 /* post our combined ASYNC_DONE when all is ASYNC_DONE. */
3496 GST_DEBUG_OBJECT (bin, "posting ASYNC_DONE to parent");
3497 gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
3500 GST_OBJECT_LOCK (bin);
3502 /* toplevel, start continue state */
3503 GST_DEBUG_OBJECT (bin, "all async-done, starting state continue");
3504 bin_push_state_continue (bin, cont);
3506 GST_DEBUG_OBJECT (bin, "state change complete");
3507 GST_STATE_BROADCAST (bin);
3513 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "we had an error");
3518 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "state change busy");
3519 /* if we were busy with a state change and we are requested to flag a
3520 * pending async done, we do so here */
3522 bin->priv->pending_async_done = TRUE;
3527 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin, "nothing pending");
3533 bin_do_eos (GstBin * bin)
3538 GST_OBJECT_LOCK (bin);
3539 /* If all sinks are EOS, we're in PLAYING and no state change is pending
3540 * (or we're doing playing to playing and noone else will trigger posting
3541 * EOS for us) we forward the EOS message to the parent bin or application
3543 eos = GST_STATE (bin) == GST_STATE_PLAYING
3544 && (GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING ||
3545 GST_STATE_PENDING (bin) == GST_STATE_PLAYING)
3546 && bin->priv->posted_playing && is_eos (bin, &seqnum);
3547 GST_OBJECT_UNLOCK (bin);
3550 && g_atomic_int_compare_and_exchange (&bin->priv->posted_eos, FALSE,
3552 GstMessage *tmessage;
3554 /* Clear out any further messages, and reset posted_eos so we can
3555 detect any new EOS that happens (eg, after a seek). Since all
3556 sinks have now posted an EOS, there will be no further EOS events
3557 seen unless there is a new logical EOS */
3558 GST_OBJECT_LOCK (bin);
3559 bin_remove_messages (bin, NULL, GST_MESSAGE_EOS);
3560 bin->priv->posted_eos = FALSE;
3561 GST_OBJECT_UNLOCK (bin);
3563 tmessage = gst_message_new_eos (GST_OBJECT_CAST (bin));
3564 gst_message_set_seqnum (tmessage, seqnum);
3565 GST_DEBUG_OBJECT (bin,
3566 "all sinks posted EOS, posting seqnum #%" G_GUINT32_FORMAT, seqnum);
3567 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3569 GST_LOG_OBJECT (bin, "Not forwarding EOS due to in progress state change, "
3570 " or already posted, or waiting for more EOS");
3575 bin_do_stream_start (GstBin * bin)
3578 gboolean stream_start;
3579 gboolean have_group_id = FALSE;
3582 GST_OBJECT_LOCK (bin);
3583 /* If all sinks are STREAM_START we forward the STREAM_START message
3584 * to the parent bin or application
3586 stream_start = is_stream_start (bin, &seqnum, &have_group_id, &group_id);
3587 GST_OBJECT_UNLOCK (bin);
3590 GstMessage *tmessage;
3592 GST_OBJECT_LOCK (bin);
3593 bin_remove_messages (bin, NULL, GST_MESSAGE_STREAM_START);
3594 GST_OBJECT_UNLOCK (bin);
3596 tmessage = gst_message_new_stream_start (GST_OBJECT_CAST (bin));
3597 gst_message_set_seqnum (tmessage, seqnum);
3599 gst_message_set_group_id (tmessage, group_id);
3601 GST_DEBUG_OBJECT (bin,
3602 "all sinks posted STREAM_START, posting seqnum #%" G_GUINT32_FORMAT,
3604 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3608 /* must be called without the object lock as it posts messages */
3610 bin_do_message_forward (GstBin * bin, GstMessage * message)
3612 if (bin->priv->message_forward) {
3613 GstMessage *forwarded;
3615 GST_DEBUG_OBJECT (bin, "pass %s message upward",
3616 GST_MESSAGE_TYPE_NAME (message));
3618 /* we need to convert these messages to element messages so that our parent
3619 * bin can easily ignore them and so that the application can easily
3620 * distinguish between the internally forwarded and the real messages. */
3621 forwarded = gst_message_new_element (GST_OBJECT_CAST (bin),
3622 gst_structure_new ("GstBinForwarded",
3623 "message", GST_TYPE_MESSAGE, message, NULL));
3625 gst_element_post_message (GST_ELEMENT_CAST (bin), forwarded);
3630 gst_bin_update_context (GstBin * bin, GstContext * context)
3632 GST_OBJECT_LOCK (bin);
3633 gst_bin_update_context_unlocked (bin, context);
3634 GST_OBJECT_UNLOCK (bin);
3638 gst_bin_update_context_unlocked (GstBin * bin, GstContext * context)
3640 const gchar *context_type;
3641 GList *l, **contexts;
3643 contexts = &GST_ELEMENT_CAST (bin)->contexts;
3644 context_type = gst_context_get_context_type (context);
3646 GST_DEBUG_OBJECT (bin, "set context %p %" GST_PTR_FORMAT, context,
3647 gst_context_get_structure (context));
3648 for (l = *contexts; l; l = l->next) {
3649 GstContext *tmp = l->data;
3650 const gchar *tmp_type = gst_context_get_context_type (tmp);
3652 /* Always store newest context but never replace
3653 * a persistent one by a non-persistent one */
3654 if (strcmp (context_type, tmp_type) == 0 &&
3655 (gst_context_is_persistent (context) ||
3656 !gst_context_is_persistent (tmp))) {
3657 gst_context_replace ((GstContext **) & l->data, context);
3661 /* Not found? Add */
3663 *contexts = g_list_prepend (*contexts, gst_context_ref (context));
3667 /* handle child messages:
3669 * This method is called synchronously when a child posts a message on
3672 * GST_MESSAGE_EOS: This message is only posted by sinks
3673 * in the PLAYING state. If all sinks posted the EOS message, post
3676 * GST_MESSAGE_STATE_DIRTY: Deprecated
3678 * GST_MESSAGE_SEGMENT_START: just collect, never forward upwards. If an
3679 * element posts segment_start twice, only the last message is kept.
3681 * GST_MESSAGE_SEGMENT_DONE: replace SEGMENT_START message from same poster
3682 * with the segment_done message. If there are no more segment_start
3683 * messages, post segment_done message upwards.
3685 * GST_MESSAGE_DURATION_CHANGED: clear any cached durations.
3686 * Whenever someone performs a duration query on the bin, we store the
3687 * result so we can answer it quicker the next time. Any element that
3688 * changes its duration marks our cached values invalid.
3689 * This message is also posted upwards. This is currently disabled
3690 * because too many elements don't post DURATION_CHANGED messages when
3691 * the duration changes.
3693 * GST_MESSAGE_CLOCK_LOST: This message is posted by an element when it
3694 * can no longer provide a clock. The default bin behaviour is to
3695 * check if the lost clock was the one provided by the bin. If so and
3696 * we are currently in the PLAYING state, we forward the message to
3698 * This message is also generated when we remove a clock provider from
3699 * a bin. If this message is received by the application, it should
3700 * PAUSE the pipeline and set it back to PLAYING to force a new clock
3701 * and a new base_time distribution.
3703 * GST_MESSAGE_CLOCK_PROVIDE: This message is generated when an element
3704 * can provide a clock. This mostly happens when we add a new clock
3705 * provider to the bin. The default behaviour of the bin is to mark the
3706 * currently selected clock as dirty, which will perform a clock
3707 * recalculation the next time we are asked to provide a clock.
3708 * This message is never sent to the application but is forwarded to
3711 * GST_MESSAGE_ASYNC_START: Create an internal ELEMENT message that stores
3712 * the state of the element and the fact that the element will need a
3713 * new base_time. This message is not forwarded to the application.
3715 * GST_MESSAGE_ASYNC_DONE: Find the internal ELEMENT message we kept for the
3716 * element when it posted ASYNC_START. If all elements are done, post a
3717 * ASYNC_DONE message to the parent.
3719 * OTHER: post upwards.
3722 gst_bin_handle_message_func (GstBin * bin, GstMessage * message)
3725 GstMessageType type;
3726 GstMessage *tmessage;
3729 src = GST_MESSAGE_SRC (message);
3730 type = GST_MESSAGE_TYPE (message);
3732 GST_DEBUG_OBJECT (bin, "[msg %p] handling child %s message of type %s",
3733 message, src ? GST_ELEMENT_NAME (src) : "(NULL)",
3734 GST_MESSAGE_TYPE_NAME (message));
3737 case GST_MESSAGE_ERROR:
3739 GST_OBJECT_LOCK (bin);
3741 GST_DEBUG_OBJECT (bin, "got ERROR message, unlocking state change");
3742 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_FAILURE;
3743 GST_STATE_BROADCAST (bin);
3744 GST_OBJECT_UNLOCK (bin);
3748 case GST_MESSAGE_EOS:
3751 /* collect all eos messages from the children */
3752 bin_do_message_forward (bin, message);
3753 GST_OBJECT_LOCK (bin);
3754 /* ref message for future use */
3755 bin_replace_message (bin, message, GST_MESSAGE_EOS);
3756 GST_OBJECT_UNLOCK (bin);
3761 case GST_MESSAGE_STREAM_START:
3764 /* collect all stream_start messages from the children */
3765 GST_OBJECT_LOCK (bin);
3766 /* ref message for future use */
3767 bin_replace_message (bin, message, GST_MESSAGE_STREAM_START);
3768 GST_OBJECT_UNLOCK (bin);
3770 bin_do_stream_start (bin);
3773 case GST_MESSAGE_STATE_DIRTY:
3775 GST_WARNING_OBJECT (bin, "received deprecated STATE_DIRTY message");
3778 gst_message_unref (message);
3781 case GST_MESSAGE_SEGMENT_START:{
3782 gboolean post = FALSE;
3786 gst_message_parse_segment_start (message, &format, &position);
3787 seqnum = gst_message_get_seqnum (message);
3789 bin_do_message_forward (bin, message);
3791 GST_OBJECT_LOCK (bin);
3792 /* if this is the first segment-start, post to parent but not to the
3794 if (!find_message (bin, NULL, GST_MESSAGE_SEGMENT_START) &&
3795 (GST_OBJECT_PARENT (bin) != NULL)) {
3798 /* replace any previous segment_start message from this source
3799 * with the new segment start message */
3800 bin_replace_message (bin, message, GST_MESSAGE_SEGMENT_START);
3801 GST_OBJECT_UNLOCK (bin);
3803 tmessage = gst_message_new_segment_start (GST_OBJECT_CAST (bin),
3805 gst_message_set_seqnum (tmessage, seqnum);
3807 /* post segment start with initial format and position. */
3808 GST_DEBUG_OBJECT (bin, "posting SEGMENT_START (%u) bus message: %p",
3810 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3814 case GST_MESSAGE_SEGMENT_DONE:
3816 gboolean post = FALSE;
3820 gst_message_parse_segment_done (message, &format, &position);
3821 seqnum = gst_message_get_seqnum (message);
3823 bin_do_message_forward (bin, message);
3825 GST_OBJECT_LOCK (bin);
3826 bin_replace_message (bin, message, GST_MESSAGE_SEGMENT_START);
3827 /* if there are no more segment_start messages, everybody posted
3828 * a segment_done and we can post one on the bus. */
3830 /* we don't care who still has a pending segment start */
3831 if (!find_message (bin, NULL, GST_MESSAGE_SEGMENT_START)) {
3834 /* remove all old segment_done messages */
3835 bin_remove_messages (bin, NULL, GST_MESSAGE_SEGMENT_DONE);
3837 GST_OBJECT_UNLOCK (bin);
3839 tmessage = gst_message_new_segment_done (GST_OBJECT_CAST (bin),
3841 gst_message_set_seqnum (tmessage, seqnum);
3843 /* post segment done with latest format and position. */
3844 GST_DEBUG_OBJECT (bin, "posting SEGMENT_DONE (%u) bus message: %p",
3846 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3850 case GST_MESSAGE_DURATION_CHANGED:
3852 /* FIXME: remove all cached durations, next time somebody asks
3853 * for duration, we will recalculate. */
3855 GST_OBJECT_LOCK (bin);
3856 bin_remove_messages (bin, NULL, GST_MESSAGE_DURATION_CHANGED);
3857 GST_OBJECT_UNLOCK (bin);
3861 case GST_MESSAGE_CLOCK_LOST:
3863 GstClock **provided_clock_p;
3864 GstElement **clock_provider_p;
3865 gboolean playing, toplevel, provided, forward;
3868 gst_message_parse_clock_lost (message, &clock);
3870 GST_OBJECT_LOCK (bin);
3871 bin->clock_dirty = TRUE;
3872 /* if we lost the clock that we provided, post to parent but
3873 * only if we are not a top-level bin or PLAYING.
3874 * The reason for this is that applications should be able
3875 * to PAUSE/PLAY if they receive this message without worrying
3876 * about the state of the pipeline. */
3877 provided = (clock == bin->provided_clock);
3878 playing = (GST_STATE (bin) == GST_STATE_PLAYING);
3879 toplevel = GST_OBJECT_PARENT (bin) == NULL;
3880 forward = provided && (playing || !toplevel);
3882 GST_DEBUG_OBJECT (bin,
3883 "Lost clock %" GST_PTR_FORMAT " provided by %" GST_PTR_FORMAT,
3884 bin->provided_clock, bin->clock_provider);
3885 provided_clock_p = &bin->provided_clock;
3886 clock_provider_p = &bin->clock_provider;
3887 gst_object_replace ((GstObject **) provided_clock_p, NULL);
3888 gst_object_replace ((GstObject **) clock_provider_p, NULL);
3890 GST_DEBUG_OBJECT (bin, "provided %d, playing %d, forward %d",
3891 provided, playing, forward);
3892 GST_OBJECT_UNLOCK (bin);
3898 gst_message_unref (message);
3901 case GST_MESSAGE_CLOCK_PROVIDE:
3905 GST_OBJECT_LOCK (bin);
3906 bin->clock_dirty = TRUE;
3907 /* a new clock is available, post to parent but not
3908 * to the application */
3909 forward = GST_OBJECT_PARENT (bin) != NULL;
3910 GST_OBJECT_UNLOCK (bin);
3916 gst_message_unref (message);
3919 case GST_MESSAGE_ASYNC_START:
3923 GST_DEBUG_OBJECT (bin, "ASYNC_START message %p, %s", message,
3924 src ? GST_OBJECT_NAME (src) : "(NULL)");
3926 bin_do_message_forward (bin, message);
3928 GST_OBJECT_LOCK (bin);
3929 /* we ignore the message if we are going to <= READY */
3930 if ((target = GST_STATE_TARGET (bin)) <= GST_STATE_READY)
3931 goto ignore_start_message;
3933 /* takes ownership of the message */
3934 bin_replace_message (bin, message, GST_MESSAGE_ASYNC_START);
3936 bin_handle_async_start (bin);
3937 GST_OBJECT_UNLOCK (bin);
3940 ignore_start_message:
3942 GST_DEBUG_OBJECT (bin, "ignoring message, target %s",
3943 gst_element_state_get_name (target));
3944 GST_OBJECT_UNLOCK (bin);
3945 gst_message_unref (message);
3949 case GST_MESSAGE_ASYNC_DONE:
3951 GstClockTime running_time;
3954 GST_DEBUG_OBJECT (bin, "ASYNC_DONE message %p, %s", message,
3955 src ? GST_OBJECT_NAME (src) : "(NULL)");
3957 gst_message_parse_async_done (message, &running_time);
3959 bin_do_message_forward (bin, message);
3961 GST_OBJECT_LOCK (bin);
3962 /* ignore messages if we are shutting down */
3963 if ((target = GST_STATE_TARGET (bin)) <= GST_STATE_READY)
3964 goto ignore_done_message;
3966 bin_replace_message (bin, message, GST_MESSAGE_ASYNC_START);
3967 /* if there are no more ASYNC_START messages, everybody posted
3968 * a ASYNC_DONE and we can post one on the bus. When checking, we
3969 * don't care who still has a pending ASYNC_START */
3970 if (!find_message (bin, NULL, GST_MESSAGE_ASYNC_START)) {
3971 /* nothing found, remove all old ASYNC_DONE messages */
3972 bin_remove_messages (bin, NULL, GST_MESSAGE_ASYNC_DONE);
3974 GST_DEBUG_OBJECT (bin, "async elements commited");
3975 /* when we get an async done message when a state change was busy, we
3976 * need to set the pending_done flag so that at the end of the state
3977 * change we can see if we need to verify pending async elements, hence
3978 * the TRUE argument here. */
3979 bin_handle_async_done (bin, GST_STATE_CHANGE_SUCCESS, TRUE,
3982 GST_DEBUG_OBJECT (bin, "there are more async elements pending");
3984 GST_OBJECT_UNLOCK (bin);
3987 ignore_done_message:
3989 GST_DEBUG_OBJECT (bin, "ignoring message, target %s",
3990 gst_element_state_get_name (target));
3991 GST_OBJECT_UNLOCK (bin);
3992 gst_message_unref (message);
3996 case GST_MESSAGE_STRUCTURE_CHANGE:
4000 gst_message_parse_structure_change (message, NULL, NULL, &busy);
4002 GST_OBJECT_LOCK (bin);
4004 /* while the pad is busy, avoid following it when doing state changes.
4005 * Don't update the cookie yet, we will do that after the structure
4006 * change finished and we are ready to inspect the new updated
4008 bin_replace_message (bin, message, GST_MESSAGE_STRUCTURE_CHANGE);
4011 /* a pad link/unlink ended, signal the state change iterator that we
4012 * need to resync by updating the structure_cookie. */
4013 bin_remove_messages (bin, GST_MESSAGE_SRC (message),
4014 GST_MESSAGE_STRUCTURE_CHANGE);
4015 if (!GST_BIN_IS_NO_RESYNC (bin))
4016 bin->priv->structure_cookie++;
4018 GST_OBJECT_UNLOCK (bin);
4021 gst_message_unref (message);
4025 case GST_MESSAGE_NEED_CONTEXT:{
4026 const gchar *context_type;
4027 GList *l, *contexts;
4029 gst_message_parse_context_type (message, &context_type);
4030 GST_OBJECT_LOCK (bin);
4031 contexts = GST_ELEMENT_CAST (bin)->contexts;
4032 GST_LOG_OBJECT (bin, "got need-context message type: %s", context_type);
4033 for (l = contexts; l; l = l->next) {
4034 GstContext *tmp = l->data;
4035 const gchar *tmp_type = gst_context_get_context_type (tmp);
4037 if (strcmp (context_type, tmp_type) == 0) {
4038 gst_element_set_context (GST_ELEMENT (src), l->data);
4042 GST_OBJECT_UNLOCK (bin);
4044 /* Forward if we couldn't answer the message */
4048 gst_message_unref (message);
4053 case GST_MESSAGE_HAVE_CONTEXT:{
4054 GstContext *context;
4056 gst_message_parse_have_context (message, &context);
4057 gst_bin_update_context (bin, context);
4058 gst_context_unref (context);
4070 /* Send all other messages upward */
4071 GST_DEBUG_OBJECT (bin, "posting message upward");
4072 gst_element_post_message (GST_ELEMENT_CAST (bin), message);
4077 /* generic struct passed to all query fold methods */
4086 typedef void (*QueryInitFunction) (GstBin * bin, QueryFold * fold);
4087 typedef void (*QueryDoneFunction) (GstBin * bin, QueryFold * fold);
4089 /* for duration/position we collect all durations/positions and take
4090 * the MAX of all valid results */
4092 bin_query_min_max_init (GstBin * bin, QueryFold * fold)
4100 bin_query_duration_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4102 gboolean res = FALSE;
4103 GstObject *item = g_value_get_object (vitem);
4104 if (GST_IS_PAD (item))
4105 res = gst_pad_query (GST_PAD (item), fold->query);
4107 res = gst_element_query (GST_ELEMENT (item), fold->query);
4112 g_value_set_boolean (ret, TRUE);
4114 gst_query_parse_duration (fold->query, NULL, &duration);
4116 GST_DEBUG_OBJECT (item, "got duration %" G_GINT64_FORMAT, duration);
4118 if (duration == -1) {
4119 /* duration query succeeded, but duration is unknown */
4124 if (duration > fold->max)
4125 fold->max = duration;
4132 bin_query_duration_done (GstBin * bin, QueryFold * fold)
4136 gst_query_parse_duration (fold->query, &format, NULL);
4137 /* store max in query result */
4138 gst_query_set_duration (fold->query, format, fold->max);
4140 GST_DEBUG_OBJECT (bin, "max duration %" G_GINT64_FORMAT, fold->max);
4142 /* FIXME: re-implement duration caching */
4145 GST_OBJECT_LOCK (bin);
4146 bin->messages = g_list_prepend (bin->messages,
4147 gst_message_new_duration (GST_OBJECT_CAST (bin), format, fold->max));
4148 GST_OBJECT_UNLOCK (bin);
4153 bin_query_position_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4155 gboolean res = FALSE;
4156 GstObject *item = g_value_get_object (vitem);
4157 if (GST_IS_PAD (item))
4158 res = gst_pad_query (GST_PAD (item), fold->query);
4160 res = gst_element_query (GST_ELEMENT (item), fold->query);
4165 g_value_set_boolean (ret, TRUE);
4167 gst_query_parse_position (fold->query, NULL, &position);
4169 GST_DEBUG_OBJECT (item, "got position %" G_GINT64_FORMAT, position);
4171 if (position > fold->max)
4172 fold->max = position;
4179 bin_query_position_done (GstBin * bin, QueryFold * fold)
4183 gst_query_parse_position (fold->query, &format, NULL);
4184 /* store max in query result */
4185 gst_query_set_position (fold->query, format, fold->max);
4187 GST_DEBUG_OBJECT (bin, "max position %" G_GINT64_FORMAT, fold->max);
4191 bin_query_latency_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4193 gboolean res = FALSE;
4194 GstObject *item = g_value_get_object (vitem);
4195 if (GST_IS_PAD (item))
4196 res = gst_pad_query (GST_PAD (item), fold->query);
4198 res = gst_element_query (GST_ELEMENT (item), fold->query);
4200 GstClockTime min, max;
4203 gst_query_parse_latency (fold->query, &live, &min, &max);
4205 GST_DEBUG_OBJECT (item,
4206 "got latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
4207 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
4209 /* for the combined latency we collect the MAX of all min latencies and
4210 * the MIN of all max latencies */
4212 if (min > fold->min)
4214 if (fold->max == -1)
4216 else if (max < fold->max)
4222 g_value_set_boolean (ret, FALSE);
4223 GST_DEBUG_OBJECT (item, "failed query");
4230 bin_query_latency_done (GstBin * bin, QueryFold * fold)
4232 /* store max in query result */
4233 gst_query_set_latency (fold->query, fold->live, fold->min, fold->max);
4235 GST_DEBUG_OBJECT (bin,
4236 "latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
4237 ", live %d", GST_TIME_ARGS (fold->min), GST_TIME_ARGS (fold->max),
4241 /* generic fold, return first valid result */
4243 bin_query_generic_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4245 gboolean res = FALSE;
4246 GstObject *item = g_value_get_object (vitem);
4247 if (GST_IS_PAD (item))
4248 res = gst_pad_query (GST_PAD (item), fold->query);
4250 res = gst_element_query (GST_ELEMENT (item), fold->query);
4252 g_value_set_boolean (ret, TRUE);
4253 GST_DEBUG_OBJECT (item, "answered query %p", fold->query);
4256 /* and stop as soon as we have a valid result */
4260 /* Perform a query iteration for the given bin. The query is stored in
4261 * QueryFold and iter should be either a GstPad iterator or a
4262 * GstElement iterator. */
4264 bin_iterate_fold (GstBin * bin, GstIterator * iter, QueryInitFunction fold_init,
4265 QueryDoneFunction fold_done, GstIteratorFoldFunction fold_func,
4266 QueryFold * fold_data, gboolean default_return)
4268 gboolean res = default_return;
4270 /* set the result of the query to FALSE initially */
4271 g_value_init (&ret, G_TYPE_BOOLEAN);
4272 g_value_set_boolean (&ret, res);
4275 GstIteratorResult ires;
4277 ires = gst_iterator_fold (iter, fold_func, &ret, fold_data);
4280 case GST_ITERATOR_RESYNC:
4281 gst_iterator_resync (iter);
4283 fold_init (bin, fold_data);
4284 g_value_set_boolean (&ret, res);
4286 case GST_ITERATOR_OK:
4287 case GST_ITERATOR_DONE:
4288 res = g_value_get_boolean (&ret);
4289 if (fold_done != NULL && res)
4290 fold_done (bin, fold_data);
4302 gst_bin_query (GstElement * element, GstQuery * query)
4304 GstBin *bin = GST_BIN_CAST (element);
4306 gboolean default_return = FALSE;
4307 gboolean res = FALSE;
4308 gboolean src_pads_query_result = FALSE;
4309 GstIteratorFoldFunction fold_func;
4310 QueryInitFunction fold_init = NULL;
4311 QueryDoneFunction fold_done = NULL;
4312 QueryFold fold_data;
4314 switch (GST_QUERY_TYPE (query)) {
4315 case GST_QUERY_DURATION:
4317 /* FIXME: implement duration caching in GstBin again */
4322 gst_query_parse_duration (query, &qformat, NULL);
4324 /* find cached duration query */
4325 GST_OBJECT_LOCK (bin);
4326 for (cached = bin->messages; cached; cached = g_list_next (cached)) {
4327 GstMessage *message = (GstMessage *) cached->data;
4329 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_DURATION_CHANGED &&
4330 GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (bin)) {
4334 gst_message_parse_duration (message, &format, &duration);
4336 /* if cached same format, copy duration in query result */
4337 if (format == qformat) {
4338 GST_DEBUG_OBJECT (bin, "return cached duration %" G_GINT64_FORMAT,
4340 GST_OBJECT_UNLOCK (bin);
4342 gst_query_set_duration (query, qformat, duration);
4348 GST_OBJECT_UNLOCK (bin);
4350 #ifndef GST_DISABLE_GST_DEBUG
4352 /* Quieten this particularly annoying FIXME a bit: */
4353 static gboolean printed_fixme = FALSE;
4354 if (!printed_fixme) {
4355 GST_FIXME ("implement duration caching in GstBin again");
4356 printed_fixme = TRUE;
4362 /* no cached value found, iterate and collect durations */
4363 fold_func = (GstIteratorFoldFunction) bin_query_duration_fold;
4364 fold_init = bin_query_min_max_init;
4365 fold_done = bin_query_duration_done;
4368 case GST_QUERY_POSITION:
4370 fold_func = (GstIteratorFoldFunction) bin_query_position_fold;
4371 fold_init = bin_query_min_max_init;
4372 fold_done = bin_query_position_done;
4375 case GST_QUERY_LATENCY:
4377 fold_func = (GstIteratorFoldFunction) bin_query_latency_fold;
4378 fold_init = bin_query_min_max_init;
4379 fold_done = bin_query_latency_done;
4380 default_return = TRUE;
4384 fold_func = (GstIteratorFoldFunction) bin_query_generic_fold;
4388 fold_data.query = query;
4390 iter = gst_bin_iterate_sinks (bin);
4391 GST_DEBUG_OBJECT (bin, "Sending query %p (type %s) to sink children",
4392 query, GST_QUERY_TYPE_NAME (query));
4395 fold_init (bin, &fold_data);
4398 bin_iterate_fold (bin, iter, fold_init, fold_done, fold_func, &fold_data,
4400 gst_iterator_free (iter);
4403 /* Query the source pads of the element */
4404 iter = gst_element_iterate_src_pads (element);
4405 src_pads_query_result =
4406 bin_iterate_fold (bin, iter, fold_init, fold_done, fold_func,
4407 &fold_data, default_return);
4408 gst_iterator_free (iter);
4410 if (src_pads_query_result)
4414 GST_DEBUG_OBJECT (bin, "query %p result %d", query, res);
4420 set_context (const GValue * item, gpointer user_data)
4422 GstElement *element = g_value_get_object (item);
4424 gst_element_set_context (element, user_data);
4428 gst_bin_set_context (GstElement * element, GstContext * context)
4431 GstIterator *children;
4433 g_return_if_fail (GST_IS_BIN (element));
4435 bin = GST_BIN (element);
4437 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
4439 children = gst_bin_iterate_elements (bin);
4440 while (gst_iterator_foreach (children, set_context,
4441 context) == GST_ITERATOR_RESYNC)
4442 gst_iterator_resync (children);
4443 gst_iterator_free (children);
4447 compare_name (const GValue * velement, const gchar * name)
4450 GstElement *element = g_value_get_object (velement);
4452 GST_OBJECT_LOCK (element);
4453 eq = strcmp (GST_ELEMENT_NAME (element), name);
4454 GST_OBJECT_UNLOCK (element);
4460 * gst_bin_get_by_name:
4462 * @name: the element name to search for
4464 * Gets the element with the given name from a bin. This
4465 * function recurses into child bins.
4467 * Returns %NULL if no element with the given name is found in the bin.
4469 * MT safe. Caller owns returned reference.
4471 * Returns: (transfer full) (nullable): the #GstElement with the given
4475 gst_bin_get_by_name (GstBin * bin, const gchar * name)
4477 GstIterator *children;
4478 GValue result = { 0, };
4479 GstElement *element;
4482 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4484 GST_CAT_INFO (GST_CAT_PARENTAGE, "[%s]: looking up child element %s",
4485 GST_ELEMENT_NAME (bin), name);
4487 children = gst_bin_iterate_recurse (bin);
4488 found = gst_iterator_find_custom (children,
4489 (GCompareFunc) compare_name, &result, (gpointer) name);
4490 gst_iterator_free (children);
4493 element = g_value_dup_object (&result);
4494 g_value_unset (&result);
4503 * gst_bin_get_by_name_recurse_up:
4505 * @name: the element name to search for
4507 * Gets the element with the given name from this bin. If the
4508 * element is not found, a recursion is performed on the parent bin.
4511 * - no element with the given name is found in the bin
4513 * MT safe. Caller owns returned reference.
4515 * Returns: (transfer full) (nullable): the #GstElement with the given
4519 gst_bin_get_by_name_recurse_up (GstBin * bin, const gchar * name)
4523 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4524 g_return_val_if_fail (name != NULL, NULL);
4526 result = gst_bin_get_by_name (bin, name);
4531 parent = gst_object_get_parent (GST_OBJECT_CAST (bin));
4533 if (GST_IS_BIN (parent)) {
4534 result = gst_bin_get_by_name_recurse_up (GST_BIN_CAST (parent), name);
4536 gst_object_unref (parent);
4544 compare_interface (const GValue * velement, GValue * interface)
4546 GstElement *element = g_value_get_object (velement);
4547 GType interface_type = (GType) g_value_get_pointer (interface);
4550 if (G_TYPE_CHECK_INSTANCE_TYPE (element, interface_type)) {
4559 * gst_bin_get_by_interface:
4561 * @iface: the #GType of an interface
4563 * Looks for an element inside the bin that implements the given
4564 * interface. If such an element is found, it returns the element.
4565 * You can cast this element to the given interface afterwards. If you want
4566 * all elements that implement the interface, use
4567 * gst_bin_iterate_all_by_interface(). This function recurses into child bins.
4569 * MT safe. Caller owns returned reference.
4571 * Returns: (transfer full): A #GstElement inside the bin implementing the interface
4574 gst_bin_get_by_interface (GstBin * bin, GType iface)
4576 GstIterator *children;
4577 GValue result = { 0, };
4578 GstElement *element;
4580 GValue viface = { 0, };
4582 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4583 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface), NULL);
4585 g_value_init (&viface, G_TYPE_POINTER);
4586 g_value_set_pointer (&viface, (gpointer) iface);
4588 children = gst_bin_iterate_recurse (bin);
4589 found = gst_iterator_find_custom (children, (GCompareFunc) compare_interface,
4591 gst_iterator_free (children);
4594 element = g_value_dup_object (&result);
4595 g_value_unset (&result);
4599 g_value_unset (&viface);
4605 * gst_bin_iterate_all_by_interface:
4607 * @iface: the #GType of an interface
4609 * Looks for all elements inside the bin that implements the given
4610 * interface. You can safely cast all returned elements to the given interface.
4611 * The function recurses inside child bins. The iterator will yield a series
4612 * of #GstElement that should be unreffed after use.
4614 * MT safe. Caller owns returned value.
4616 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
4617 * for all elements in the bin implementing the given interface,
4621 gst_bin_iterate_all_by_interface (GstBin * bin, GType iface)
4623 GstIterator *children;
4624 GstIterator *result;
4625 GValue viface = { 0, };
4627 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4628 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface), NULL);
4630 g_value_init (&viface, G_TYPE_POINTER);
4631 g_value_set_pointer (&viface, (gpointer) iface);
4633 children = gst_bin_iterate_recurse (bin);
4634 result = gst_iterator_filter (children, (GCompareFunc) compare_interface,
4637 g_value_unset (&viface);