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;
208 static void gst_bin_dispose (GObject * object);
210 static void gst_bin_set_property (GObject * object, guint prop_id,
211 const GValue * value, GParamSpec * pspec);
212 static void gst_bin_get_property (GObject * object, guint prop_id,
213 GValue * value, GParamSpec * pspec);
215 static GstStateChangeReturn gst_bin_change_state_func (GstElement * element,
216 GstStateChange transition);
217 static gboolean gst_bin_post_message (GstElement * element, GstMessage * msg);
218 static GstStateChangeReturn gst_bin_get_state_func (GstElement * element,
219 GstState * state, GstState * pending, GstClockTime timeout);
220 static void bin_handle_async_done (GstBin * bin, GstStateChangeReturn ret,
221 gboolean flag_pending, GstClockTime running_time);
222 static void bin_handle_async_start (GstBin * bin);
223 static void bin_push_state_continue (GstBin * bin, BinContinueData * data);
224 static void bin_do_eos (GstBin * bin);
226 static gboolean gst_bin_add_func (GstBin * bin, GstElement * element);
227 static gboolean gst_bin_remove_func (GstBin * bin, GstElement * element);
228 static void gst_bin_deep_element_added_func (GstBin * bin, GstBin * sub_bin,
229 GstElement * element);
230 static void gst_bin_deep_element_removed_func (GstBin * bin, GstBin * sub_bin,
231 GstElement * element);
232 static void gst_bin_update_context (GstBin * bin, GstContext * context);
233 static void gst_bin_update_context_unlocked (GstBin * bin,
234 GstContext * context);
237 static void gst_bin_set_index_func (GstElement * element, GstIndex * index);
238 static GstIndex *gst_bin_get_index_func (GstElement * element);
241 static GstClock *gst_bin_provide_clock_func (GstElement * element);
242 static gboolean gst_bin_set_clock_func (GstElement * element, GstClock * clock);
244 static void gst_bin_handle_message_func (GstBin * bin, GstMessage * message);
245 static gboolean gst_bin_send_event (GstElement * element, GstEvent * event);
246 static GstBusSyncReply bin_bus_handler (GstBus * bus,
247 GstMessage * message, GstBin * bin);
248 static gboolean gst_bin_query (GstElement * element, GstQuery * query);
249 static void gst_bin_set_context (GstElement * element, GstContext * context);
251 static gboolean gst_bin_do_latency_func (GstBin * bin);
253 static void bin_remove_messages (GstBin * bin, GstObject * src,
254 GstMessageType types);
255 static void gst_bin_continue_func (GstBin * bin, BinContinueData * data);
256 static gint bin_element_is_sink (GstElement * child, GstBin * bin);
257 static gint bin_element_is_src (GstElement * child, GstBin * bin);
259 static GstIterator *gst_bin_sort_iterator_new (GstBin * bin);
261 /* Bin signals and properties */
268 DEEP_ELEMENT_REMOVED,
272 #define DEFAULT_ASYNC_HANDLING FALSE
273 #define DEFAULT_MESSAGE_FORWARD FALSE
279 PROP_MESSAGE_FORWARD,
283 static void gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data);
285 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
289 static const GInterfaceInfo iface_info = { \
290 gst_bin_child_proxy_init, \
294 g_type_add_interface_static (g_define_type_id, GST_TYPE_CHILD_PROXY, &iface_info); \
296 GST_DEBUG_CATEGORY_INIT (bin_debug, "bin", GST_DEBUG_BOLD, \
297 "debugging info for the 'bin' container element"); \
301 #define gst_bin_parent_class parent_class
302 G_DEFINE_TYPE_WITH_CODE (GstBin, gst_bin, GST_TYPE_ELEMENT, _do_init);
305 gst_bin_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
311 bin = GST_BIN_CAST (child_proxy);
313 GST_OBJECT_LOCK (bin);
314 if ((res = g_list_nth_data (bin->children, index)))
315 gst_object_ref (res);
316 GST_OBJECT_UNLOCK (bin);
318 return (GObject *) res;
322 gst_bin_child_proxy_get_children_count (GstChildProxy * child_proxy)
327 bin = GST_BIN_CAST (child_proxy);
329 GST_OBJECT_LOCK (bin);
330 num = bin->numchildren;
331 GST_OBJECT_UNLOCK (bin);
337 gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data)
339 GstChildProxyInterface *iface = g_iface;
341 iface->get_children_count = gst_bin_child_proxy_get_children_count;
342 iface->get_child_by_index = gst_bin_child_proxy_get_child_by_index;
346 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
347 GValue * return_accu, const GValue * handler_return, gpointer dummy)
351 myboolean = g_value_get_boolean (handler_return);
352 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
353 g_value_set_boolean (return_accu, myboolean);
355 GST_DEBUG ("invocation %d, %d", ihint->run_type, myboolean);
362 gst_bin_class_init (GstBinClass * klass)
364 GObjectClass *gobject_class;
365 GstElementClass *gstelement_class;
367 gobject_class = (GObjectClass *) klass;
368 gstelement_class = (GstElementClass *) klass;
370 g_type_class_add_private (klass, sizeof (GstBinPrivate));
372 gobject_class->set_property = gst_bin_set_property;
373 gobject_class->get_property = gst_bin_get_property;
376 * GstBin:async-handling:
378 * If set to %TRUE, the bin will handle asynchronous state changes.
379 * This should be used only if the bin subclass is modifying the state
380 * of its children on its own.
382 g_object_class_install_property (gobject_class, PROP_ASYNC_HANDLING,
383 g_param_spec_boolean ("async-handling", "Async Handling",
384 "The bin will handle Asynchronous state changes",
385 DEFAULT_ASYNC_HANDLING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
388 * GstBin::element-added:
390 * @element: the #GstElement that was added to the bin
392 * Will be emitted after the element was added to the bin.
394 gst_bin_signals[ELEMENT_ADDED] =
395 g_signal_new ("element-added", G_TYPE_FROM_CLASS (klass),
396 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_added), NULL,
397 NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
399 * GstBin::element-removed:
401 * @element: the #GstElement that was removed from the bin
403 * Will be emitted after the element was removed from the bin.
405 gst_bin_signals[ELEMENT_REMOVED] =
406 g_signal_new ("element-removed", G_TYPE_FROM_CLASS (klass),
407 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_removed), NULL,
408 NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
410 * GstBin::deep-element-added:
412 * @sub_bin: the #GstBin the element was added to
413 * @element: the #GstElement that was added to @sub_bin
415 * Will be emitted after the element was added to sub_bin.
419 gst_bin_signals[DEEP_ELEMENT_ADDED] =
420 g_signal_new ("deep-element-added", G_TYPE_FROM_CLASS (klass),
421 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, deep_element_added),
422 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, GST_TYPE_BIN,
425 * GstBin::deep-element-removed:
427 * @sub_bin: the #GstBin the element was removed from
428 * @element: the #GstElement that was removed from @sub_bin
430 * Will be emitted after the element was removed from sub_bin.
434 gst_bin_signals[DEEP_ELEMENT_REMOVED] =
435 g_signal_new ("deep-element-removed", G_TYPE_FROM_CLASS (klass),
436 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, deep_element_removed),
437 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, GST_TYPE_BIN,
440 * GstBin::do-latency:
443 * Will be emitted when the bin needs to perform latency calculations. This
444 * signal is only emitted for toplevel bins or when async-handling is
447 * Only one signal handler is invoked. If no signals are connected, the
448 * default handler is invoked, which will query and distribute the lowest
449 * possible latency to all sinks.
451 * Connect to this signal if the default latency calculations are not
452 * sufficient, like when you need different latencies for different sinks in
455 gst_bin_signals[DO_LATENCY] =
456 g_signal_new ("do-latency", G_TYPE_FROM_CLASS (klass),
457 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstBinClass, do_latency),
458 _gst_boolean_accumulator, NULL, g_cclosure_marshal_generic,
459 G_TYPE_BOOLEAN, 0, G_TYPE_NONE);
462 * GstBin:message-forward:
464 * Forward all children messages, even those that would normally be filtered by
465 * the bin. This can be interesting when one wants to be notified of the EOS
466 * state of individual elements, for example.
468 * The messages are converted to an ELEMENT message with the bin as the
469 * source. The structure of the message is named 'GstBinForwarded' and contains
470 * a field named 'message' of type GST_TYPE_MESSAGE that contains the original
473 g_object_class_install_property (gobject_class, PROP_MESSAGE_FORWARD,
474 g_param_spec_boolean ("message-forward", "Message Forward",
475 "Forwards all children messages",
476 DEFAULT_MESSAGE_FORWARD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
478 gobject_class->dispose = gst_bin_dispose;
480 gst_element_class_set_static_metadata (gstelement_class, "Generic bin",
482 "Simple container object",
483 "Erik Walthinsen <omega@cse.ogi.edu>,"
484 "Wim Taymans <wim.taymans@gmail.com>");
486 gstelement_class->change_state =
487 GST_DEBUG_FUNCPTR (gst_bin_change_state_func);
488 gstelement_class->post_message = GST_DEBUG_FUNCPTR (gst_bin_post_message);
489 gstelement_class->get_state = GST_DEBUG_FUNCPTR (gst_bin_get_state_func);
491 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_bin_get_index_func);
492 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_bin_set_index_func);
494 gstelement_class->provide_clock =
495 GST_DEBUG_FUNCPTR (gst_bin_provide_clock_func);
496 gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_bin_set_clock_func);
498 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_bin_send_event);
499 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_bin_query);
500 gstelement_class->set_context = GST_DEBUG_FUNCPTR (gst_bin_set_context);
502 klass->add_element = GST_DEBUG_FUNCPTR (gst_bin_add_func);
503 klass->remove_element = GST_DEBUG_FUNCPTR (gst_bin_remove_func);
504 klass->handle_message = GST_DEBUG_FUNCPTR (gst_bin_handle_message_func);
506 klass->deep_element_added = gst_bin_deep_element_added_func;
507 klass->deep_element_removed = gst_bin_deep_element_removed_func;
509 klass->do_latency = GST_DEBUG_FUNCPTR (gst_bin_do_latency_func);
513 gst_bin_init (GstBin * bin)
517 bin->numchildren = 0;
518 bin->children = NULL;
519 bin->children_cookie = 0;
520 bin->messages = NULL;
521 bin->provided_clock = NULL;
522 bin->clock_dirty = FALSE;
524 /* Set up a bus for listening to child elements */
525 bus = g_object_new (GST_TYPE_BUS, "enable-async", FALSE, NULL);
526 bin->child_bus = bus;
527 GST_DEBUG_OBJECT (bin, "using bus %" GST_PTR_FORMAT " to listen to children",
529 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bin_bus_handler, bin,
532 bin->priv = GST_BIN_GET_PRIVATE (bin);
533 bin->priv->asynchandling = DEFAULT_ASYNC_HANDLING;
534 bin->priv->structure_cookie = 0;
535 bin->priv->message_forward = DEFAULT_MESSAGE_FORWARD;
539 gst_bin_dispose (GObject * object)
541 GstBin *bin = GST_BIN_CAST (object);
542 GstBus **child_bus_p = &bin->child_bus;
543 GstClock **provided_clock_p = &bin->provided_clock;
544 GstElement **clock_provider_p = &bin->clock_provider;
546 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object, "%p dispose", object);
548 GST_OBJECT_LOCK (object);
549 gst_object_replace ((GstObject **) child_bus_p, NULL);
550 gst_object_replace ((GstObject **) provided_clock_p, NULL);
551 gst_object_replace ((GstObject **) clock_provider_p, NULL);
552 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
553 GST_OBJECT_UNLOCK (object);
555 while (bin->children) {
556 gst_bin_remove (bin, GST_ELEMENT_CAST (bin->children->data));
558 if (G_UNLIKELY (bin->children != NULL)) {
559 g_critical ("could not remove elements from bin '%s'",
560 GST_STR_NULL (GST_OBJECT_NAME (object)));
563 G_OBJECT_CLASS (parent_class)->dispose (object);
568 * @name: (allow-none): the name of the new bin
570 * Creates a new bin with the given name.
572 * Returns: (transfer floating): a new #GstBin
575 gst_bin_new (const gchar * name)
577 return gst_element_factory_make ("bin", name);
581 gst_bin_set_property (GObject * object, guint prop_id,
582 const GValue * value, GParamSpec * pspec)
586 gstbin = GST_BIN_CAST (object);
589 case PROP_ASYNC_HANDLING:
590 GST_OBJECT_LOCK (gstbin);
591 gstbin->priv->asynchandling = g_value_get_boolean (value);
592 GST_OBJECT_UNLOCK (gstbin);
594 case PROP_MESSAGE_FORWARD:
595 GST_OBJECT_LOCK (gstbin);
596 gstbin->priv->message_forward = g_value_get_boolean (value);
597 GST_OBJECT_UNLOCK (gstbin);
600 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
606 gst_bin_get_property (GObject * object, guint prop_id,
607 GValue * value, GParamSpec * pspec)
611 gstbin = GST_BIN_CAST (object);
614 case PROP_ASYNC_HANDLING:
615 GST_OBJECT_LOCK (gstbin);
616 g_value_set_boolean (value, gstbin->priv->asynchandling);
617 GST_OBJECT_UNLOCK (gstbin);
619 case PROP_MESSAGE_FORWARD:
620 GST_OBJECT_LOCK (gstbin);
621 g_value_set_boolean (value, gstbin->priv->message_forward);
622 GST_OBJECT_UNLOCK (gstbin);
625 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
631 /* return the cached index */
633 gst_bin_get_index_func (GstElement * element)
638 bin = GST_BIN_CAST (element);
640 GST_OBJECT_LOCK (bin);
641 if ((result = bin->priv->index))
642 gst_object_ref (result);
643 GST_OBJECT_UNLOCK (bin);
648 /* set the index on all elements in this bin
653 gst_bin_set_index_func (GstElement * element, GstIndex * index)
659 GValue data = { 0, };
661 bin = GST_BIN_CAST (element);
663 GST_OBJECT_LOCK (bin);
664 old = bin->priv->index;
665 if (G_UNLIKELY (old == index))
668 gst_object_ref (index);
669 bin->priv->index = index;
670 GST_OBJECT_UNLOCK (bin);
673 gst_object_unref (old);
675 it = gst_bin_iterate_elements (bin);
677 /* set the index on all elements in the bin */
680 switch (gst_iterator_next (it, &data)) {
681 case GST_ITERATOR_OK:
683 GstElement *child = g_value_get_object (&data);
685 GST_DEBUG_OBJECT (bin, "setting index on '%s'",
686 GST_ELEMENT_NAME (child));
687 gst_element_set_index (child, index);
689 g_value_reset (&data);
692 case GST_ITERATOR_RESYNC:
693 GST_DEBUG_OBJECT (bin, "iterator doing resync");
694 gst_iterator_resync (it);
697 case GST_ITERATOR_DONE:
698 GST_DEBUG_OBJECT (bin, "iterator done");
703 g_value_unset (&data);
704 gst_iterator_free (it);
709 GST_DEBUG_OBJECT (bin, "index was already set");
710 GST_OBJECT_UNLOCK (bin);
716 /* set the clock on all elements in this bin
721 gst_bin_set_clock_func (GstElement * element, GstClock * clock)
727 GValue data = { 0, };
729 bin = GST_BIN_CAST (element);
731 it = gst_bin_iterate_elements (bin);
735 switch (gst_iterator_next (it, &data)) {
736 case GST_ITERATOR_OK:
738 GstElement *child = g_value_get_object (&data);
740 res &= gst_element_set_clock (child, clock);
742 g_value_reset (&data);
745 case GST_ITERATOR_RESYNC:
746 GST_DEBUG_OBJECT (bin, "iterator doing resync");
747 gst_iterator_resync (it);
751 case GST_ITERATOR_DONE:
752 GST_DEBUG_OBJECT (bin, "iterator done");
757 g_value_unset (&data);
758 gst_iterator_free (it);
761 res = GST_ELEMENT_CLASS (parent_class)->set_clock (element, clock);
766 /* get the clock for this bin by asking all of the children in this bin
768 * The ref of the returned clock in increased so unref after usage.
770 * We loop the elements in state order and pick the last clock we can
771 * get. This makes sure we get a clock from the source.
776 gst_bin_provide_clock_func (GstElement * element)
778 GstClock *result = NULL;
779 GstElement *provider = NULL;
784 GstClock **provided_clock_p;
785 GstElement **clock_provider_p;
787 bin = GST_BIN_CAST (element);
789 GST_OBJECT_LOCK (bin);
790 if (!bin->clock_dirty)
793 GST_DEBUG_OBJECT (bin, "finding new clock");
795 it = gst_bin_sort_iterator_new (bin);
796 GST_OBJECT_UNLOCK (bin);
800 switch (gst_iterator_next (it, &val)) {
801 case GST_ITERATOR_OK:
803 GstElement *child = g_value_get_object (&val);
806 clock = gst_element_provide_clock (child);
808 GST_DEBUG_OBJECT (bin, "found candidate clock %p by element %s",
809 clock, GST_ELEMENT_NAME (child));
811 gst_object_unref (result);
812 gst_object_unref (provider);
815 provider = gst_object_ref (child);
818 g_value_reset (&val);
821 case GST_ITERATOR_RESYNC:
822 gst_iterator_resync (it);
825 case GST_ITERATOR_DONE:
830 g_value_unset (&val);
831 gst_iterator_free (it);
833 GST_OBJECT_LOCK (bin);
834 if (!bin->clock_dirty) {
836 gst_object_unref (provider);
838 gst_object_unref (result);
844 provided_clock_p = &bin->provided_clock;
845 clock_provider_p = &bin->clock_provider;
846 gst_object_replace ((GstObject **) provided_clock_p, (GstObject *) result);
847 gst_object_replace ((GstObject **) clock_provider_p, (GstObject *) provider);
848 bin->clock_dirty = FALSE;
849 GST_DEBUG_OBJECT (bin,
850 "provided new clock %" GST_PTR_FORMAT " by provider %" GST_PTR_FORMAT,
852 /* Provider is not being returned to caller, just the result */
854 gst_object_unref (provider);
855 GST_OBJECT_UNLOCK (bin);
861 if ((result = bin->provided_clock))
862 gst_object_ref (result);
863 GST_DEBUG_OBJECT (bin, "returning old clock %p", result);
864 GST_OBJECT_UNLOCK (bin);
871 * functions for manipulating cached messages
876 GstMessageType types;
879 /* check if a message is of given src and type */
881 message_check (GstMessage * message, MessageFind * target)
886 eq &= GST_MESSAGE_SRC (message) == target->src;
888 eq &= (GST_MESSAGE_TYPE (message) & target->types) != 0;
889 GST_LOG ("looking at message %p: %d", message, eq);
895 find_message (GstBin * bin, GstObject * src, GstMessageType types)
903 result = g_list_find_custom (bin->messages, &find,
904 (GCompareFunc) message_check);
907 GST_DEBUG_OBJECT (bin, "we found a message %p from %s matching types %08x",
908 result->data, GST_OBJECT_NAME (GST_MESSAGE_CAST (result->data)->src),
911 GST_DEBUG_OBJECT (bin, "no message found matching types %08x", types);
912 #ifndef GST_DISABLE_GST_DEBUG
916 for (i = 0; i < 32; i++)
917 if (types & (1U << i))
918 GST_DEBUG_OBJECT (bin, " %s", gst_message_type_get_name (1U << i));
926 /* with LOCK, returns TRUE if message had a valid SRC, takes ownership of
929 * A message that is cached and has the same SRC and type is replaced
930 * by the given message.
933 bin_replace_message (GstBin * bin, GstMessage * message, GstMessageType types)
939 if ((src = GST_MESSAGE_SRC (message))) {
940 /* first find the previous message posted by this element */
941 if ((previous = find_message (bin, src, types))) {
942 GstMessage *previous_msg;
944 /* if we found a previous message, replace it */
945 previous_msg = previous->data;
946 previous->data = message;
948 GST_DEBUG_OBJECT (bin, "replace old message %s from %s with %s message",
949 GST_MESSAGE_TYPE_NAME (previous_msg), GST_ELEMENT_NAME (src),
950 GST_MESSAGE_TYPE_NAME (message));
952 gst_message_unref (previous_msg);
954 /* keep new message */
955 bin->messages = g_list_prepend (bin->messages, message);
957 GST_DEBUG_OBJECT (bin, "got new message %p, %s from %s",
958 message, GST_MESSAGE_TYPE_NAME (message), GST_ELEMENT_NAME (src));
961 GST_DEBUG_OBJECT (bin, "got message %s from (NULL), not processing",
962 GST_MESSAGE_TYPE_NAME (message));
964 gst_message_unref (message);
969 /* with LOCK. Remove all messages of given types */
971 bin_remove_messages (GstBin * bin, GstObject * src, GstMessageType types)
979 for (walk = bin->messages; walk; walk = next) {
980 GstMessage *message = (GstMessage *) walk->data;
982 next = g_list_next (walk);
984 if (message_check (message, &find) == 0) {
985 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (message),
986 "deleting message %p of types 0x%08x", message, types);
987 bin->messages = g_list_delete_link (bin->messages, walk);
988 gst_message_unref (message);
990 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (message),
991 "not deleting message %p of type 0x%08x", message,
992 GST_MESSAGE_TYPE (message));
998 /* Check if the bin is EOS. We do this by scanning all sinks and
999 * checking if they posted an EOS message.
1001 * call with bin LOCK */
1003 is_eos (GstBin * bin, guint32 * seqnum)
1010 for (walk = bin->children; walk; walk = g_list_next (walk)) {
1011 GstElement *element;
1013 element = GST_ELEMENT_CAST (walk->data);
1014 if (bin_element_is_sink (element, bin) == 0) {
1015 /* check if element posted EOS */
1017 find_message (bin, GST_OBJECT_CAST (element), GST_MESSAGE_EOS))) {
1018 GST_DEBUG ("sink '%s' posted EOS", GST_ELEMENT_NAME (element));
1019 *seqnum = gst_message_get_seqnum (GST_MESSAGE_CAST (msgs->data));
1022 GST_DEBUG ("sink '%s' did not post EOS yet",
1023 GST_ELEMENT_NAME (element));
1029 /* FIXME: Some tests (e.g. elements/capsfilter) use
1030 * pipelines with a dangling sinkpad but no sink element.
1031 * These tests assume that no EOS message is ever
1032 * posted on the bus so let's keep that behaviour.
1033 * In valid pipelines this doesn't make a difference.
1035 return result && n_eos > 0;
1039 /* Check if the bin is STREAM_START. We do this by scanning all sinks and
1040 * checking if they posted an STREAM_START message.
1042 * call with bin LOCK */
1044 is_stream_start (GstBin * bin, guint32 * seqnum, gboolean * have_group_id,
1050 gboolean first = TRUE, same_group_id = TRUE;
1052 *have_group_id = TRUE;
1055 for (walk = bin->children; walk; walk = g_list_next (walk)) {
1056 GstElement *element;
1058 element = GST_ELEMENT_CAST (walk->data);
1059 if (bin_element_is_sink (element, bin) == 0) {
1060 /* check if element posted STREAM_START */
1062 find_message (bin, GST_OBJECT_CAST (element),
1063 GST_MESSAGE_STREAM_START))) {
1064 GST_DEBUG ("sink '%s' posted STREAM_START", GST_ELEMENT_NAME (element));
1065 *seqnum = gst_message_get_seqnum (GST_MESSAGE_CAST (msgs->data));
1066 if (gst_message_parse_group_id (GST_MESSAGE_CAST (msgs->data),
1070 *group_id = tmp_group_id;
1072 if (tmp_group_id != *group_id)
1073 same_group_id = FALSE;
1076 *have_group_id = FALSE;
1079 GST_DEBUG ("sink '%s' did not post STREAM_START yet",
1080 GST_ELEMENT_NAME (element));
1087 /* If all have a group_id we only consider this stream started
1088 * if all group ids were the same and all sinks posted a stream-start
1091 return same_group_id && result;
1092 /* otherwise consider this stream started after all sinks
1093 * have reported stream-start for backward compatibility.
1094 * FIXME 2.0: This should go away! */
1099 unlink_pads (const GValue * item, gpointer user_data)
1104 pad = g_value_get_object (item);
1106 if ((peer = gst_pad_get_peer (pad))) {
1107 if (gst_pad_get_direction (pad) == GST_PAD_SRC)
1108 gst_pad_unlink (pad, peer);
1110 gst_pad_unlink (peer, pad);
1111 gst_object_unref (peer);
1115 /* vmethod that adds an element to a bin
1120 gst_bin_add_func (GstBin * bin, GstElement * element)
1124 gboolean is_sink, is_source, provides_clock, requires_clock;
1125 GstMessage *clock_message = NULL, *async_message = NULL;
1126 GstStateChangeReturn ret;
1127 GList *l, *elem_contexts, *need_context_messages;
1129 GST_DEBUG_OBJECT (bin, "element :%s", GST_ELEMENT_NAME (element));
1131 /* we obviously can't add ourself to ourself */
1132 if (G_UNLIKELY (element == GST_ELEMENT_CAST (bin)))
1135 /* get the element name to make sure it is unique in this bin. */
1136 GST_OBJECT_LOCK (element);
1137 elem_name = g_strdup (GST_ELEMENT_NAME (element));
1138 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1139 is_source = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE);
1141 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1143 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1144 GST_OBJECT_UNLOCK (element);
1146 GST_OBJECT_LOCK (bin);
1148 /* then check to see if the element's name is already taken in the bin,
1149 * we can safely take the lock here. This check is probably bogus because
1150 * you can safely change the element name after this check and before setting
1151 * the object parent. The window is very small though... */
1152 if (G_UNLIKELY (!gst_object_check_uniqueness (bin->children, elem_name)))
1153 goto duplicate_name;
1155 /* set the element's parent and add the element to the bin's list of children */
1156 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (element),
1157 GST_OBJECT_CAST (bin))))
1160 /* if we add a sink we become a sink */
1162 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "element \"%s\" was sink",
1164 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SINK);
1167 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "element \"%s\" was source",
1169 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SOURCE);
1171 if (provides_clock) {
1172 GST_DEBUG_OBJECT (bin, "element \"%s\" can provide a clock", elem_name);
1174 gst_message_new_clock_provide (GST_OBJECT_CAST (element), NULL, TRUE);
1175 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1177 if (requires_clock) {
1178 GST_DEBUG_OBJECT (bin, "element \"%s\" requires a clock", elem_name);
1179 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1182 bin->children = g_list_prepend (bin->children, element);
1184 bin->children_cookie++;
1185 if (!GST_BIN_IS_NO_RESYNC (bin))
1186 bin->priv->structure_cookie++;
1188 /* distribute the bus */
1189 gst_element_set_bus (element, bin->child_bus);
1191 /* propagate the current base_time, start_time and clock */
1192 gst_element_set_base_time (element, GST_ELEMENT_CAST (bin)->base_time);
1193 gst_element_set_start_time (element, GST_ELEMENT_START_TIME (bin));
1194 /* it's possible that the element did not accept the clock but
1195 * that is not important right now. When the pipeline goes to PLAYING,
1196 * a new clock will be selected */
1197 gst_element_set_clock (element, GST_ELEMENT_CLOCK (bin));
1199 /* get the element's list of contexts before propagating our own */
1200 elem_contexts = gst_element_get_contexts (element);
1201 for (l = GST_ELEMENT_CAST (bin)->contexts; l; l = l->next)
1202 gst_element_set_context (element, l->data);
1204 need_context_messages = NULL;
1205 for (l = elem_contexts; l; l = l->next) {
1206 GstContext *replacement, *context = l->data;
1207 const gchar *context_type;
1209 context_type = gst_context_get_context_type (context);
1211 /* we already set this context above? */
1213 gst_element_get_context_unlocked (GST_ELEMENT (bin), context_type);
1215 gst_context_unref (replacement);
1220 /* ask our parent for the context */
1221 msg = gst_message_new_need_context (GST_OBJECT_CAST (bin), context_type);
1222 s = (GstStructure *) gst_message_get_structure (msg);
1223 gst_structure_set (s, "bin.old.context", GST_TYPE_CONTEXT, context, NULL);
1225 need_context_messages = g_list_prepend (need_context_messages, msg);
1230 /* set the cached index on the children */
1231 if (bin->priv->index)
1232 gst_element_set_index (element, bin->priv->index);
1235 ret = GST_STATE_RETURN (bin);
1236 /* no need to update the state if we are in error */
1237 if (ret == GST_STATE_CHANGE_FAILURE)
1238 goto no_state_recalc;
1240 /* update the bin state, the new element could have been an ASYNC or
1241 * NO_PREROLL element */
1242 ret = GST_STATE_RETURN (element);
1243 GST_DEBUG_OBJECT (bin, "added %s element",
1244 gst_element_state_change_return_get_name (ret));
1247 case GST_STATE_CHANGE_ASYNC:
1249 /* create message to track this aync element when it posts an async-done
1251 async_message = gst_message_new_async_start (GST_OBJECT_CAST (element));
1254 case GST_STATE_CHANGE_NO_PREROLL:
1255 /* ignore all async elements we might have and commit our state */
1256 bin_handle_async_done (bin, ret, FALSE, GST_CLOCK_TIME_NONE);
1258 case GST_STATE_CHANGE_FAILURE:
1265 GST_OBJECT_UNLOCK (bin);
1267 for (l = need_context_messages; l; l = l->next) {
1268 GstMessage *msg = l->data;
1270 const gchar *context_type;
1271 GstContext *replacement, *context;
1273 gst_message_parse_context_type (msg, &context_type);
1275 GST_LOG_OBJECT (bin, "asking parent for context type: %s "
1276 "from %" GST_PTR_FORMAT, context_type, element);
1278 s = (GstStructure *) gst_message_get_structure (msg);
1279 gst_structure_get (s, "bin.old.context", GST_TYPE_CONTEXT, &context, NULL);
1280 gst_structure_remove_field (s, "bin.old.context");
1281 gst_element_post_message (GST_ELEMENT_CAST (bin), msg);
1283 /* lock to avoid losing a potential write */
1284 GST_OBJECT_LOCK (bin);
1286 gst_element_get_context_unlocked (GST_ELEMENT_CAST (bin), context_type);
1289 /* we got the context set from GstElement::set_context */
1290 gst_context_unref (replacement);
1291 GST_OBJECT_UNLOCK (bin);
1293 /* Propagate the element's context upwards */
1294 GST_LOG_OBJECT (bin, "propagating existing context type: %s %p "
1295 "from %" GST_PTR_FORMAT, context_type, context, element);
1297 gst_bin_update_context_unlocked (bin, context);
1300 gst_message_new_have_context (GST_OBJECT_CAST (bin),
1301 gst_context_ref (context));
1302 GST_OBJECT_UNLOCK (bin);
1303 gst_element_post_message (GST_ELEMENT_CAST (bin), msg);
1305 gst_context_unref (context);
1307 g_list_free_full (elem_contexts, (GDestroyNotify) gst_context_unref);
1308 g_list_free (need_context_messages);
1310 /* post the messages on the bus of the element so that the bin can handle
1313 gst_element_post_message (element, clock_message);
1316 gst_element_post_message (element, async_message);
1318 /* unlink all linked pads */
1319 it = gst_element_iterate_pads (element);
1320 gst_iterator_foreach (it, (GstIteratorForeachFunction) unlink_pads, NULL);
1321 gst_iterator_free (it);
1323 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "added element \"%s\"",
1326 g_signal_emit (bin, gst_bin_signals[ELEMENT_ADDED], 0, element);
1327 gst_child_proxy_child_added ((GstChildProxy *) bin, (GObject *) element,
1330 g_signal_emit (bin, gst_bin_signals[DEEP_ELEMENT_ADDED], 0, bin, element);
1336 /* ERROR handling here */
1339 GST_OBJECT_LOCK (bin);
1340 g_warning ("Cannot add bin '%s' to itself", GST_ELEMENT_NAME (bin));
1341 GST_OBJECT_UNLOCK (bin);
1346 g_warning ("Name '%s' is not unique in bin '%s', not adding",
1347 elem_name, GST_ELEMENT_NAME (bin));
1348 GST_OBJECT_UNLOCK (bin);
1354 g_warning ("Element '%s' already has parent", elem_name);
1355 GST_OBJECT_UNLOCK (bin);
1361 /* signal vfunc, will be called when a new element was added */
1363 gst_bin_deep_element_added_func (GstBin * bin, GstBin * sub_bin,
1368 parent_bin = (GstBin *) gst_object_get_parent (GST_OBJECT_CAST (bin));
1369 if (parent_bin == NULL) {
1370 GST_LOG_OBJECT (bin, "no parent, reached top-level");
1374 GST_LOG_OBJECT (parent_bin, "emitting deep-element-added for element "
1375 "%" GST_PTR_FORMAT " which has just been added to %" GST_PTR_FORMAT,
1378 g_signal_emit (parent_bin, gst_bin_signals[DEEP_ELEMENT_ADDED], 0, sub_bin,
1381 gst_object_unref (parent_bin);
1384 /* signal vfunc, will be called when an element was removed */
1386 gst_bin_deep_element_removed_func (GstBin * bin, GstBin * sub_bin,
1391 parent_bin = (GstBin *) gst_object_get_parent (GST_OBJECT_CAST (bin));
1392 if (parent_bin == NULL) {
1393 GST_LOG_OBJECT (bin, "no parent, reached top-level");
1397 GST_LOG_OBJECT (parent_bin, "emitting deep-element-removed for element "
1398 "%" GST_PTR_FORMAT " which has just been removed from %" GST_PTR_FORMAT,
1401 g_signal_emit (parent_bin, gst_bin_signals[DEEP_ELEMENT_REMOVED], 0, sub_bin,
1404 gst_object_unref (parent_bin);
1410 * @element: (transfer full): the #GstElement to add
1412 * Adds the given element to the bin. Sets the element's parent, and thus
1413 * takes ownership of the element. An element can only be added to one bin.
1415 * If the element's pads are linked to other pads, the pads will be unlinked
1416 * before the element is added to the bin.
1419 * When you add an element to an already-running pipeline, you will have to
1420 * take care to set the state of the newly-added element to the desired
1421 * state (usually PLAYING or PAUSED, same you set the pipeline to originally)
1422 * with gst_element_set_state(), or use gst_element_sync_state_with_parent().
1423 * The bin or pipeline will not take care of this for you.
1428 * Returns: %TRUE if the element could be added, %FALSE if
1429 * the bin does not want to accept the element.
1432 gst_bin_add (GstBin * bin, GstElement * element)
1434 GstBinClass *bclass;
1437 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1438 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1439 g_return_val_if_fail (GST_ELEMENT_CAST (bin) != element, FALSE);
1441 bclass = GST_BIN_GET_CLASS (bin);
1443 if (G_UNLIKELY (bclass->add_element == NULL))
1446 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "adding element %s to bin %s",
1447 GST_STR_NULL (GST_ELEMENT_NAME (element)),
1448 GST_STR_NULL (GST_ELEMENT_NAME (bin)));
1450 GST_TRACER_BIN_ADD_PRE (bin, element);
1451 result = bclass->add_element (bin, element);
1452 GST_TRACER_BIN_ADD_POST (bin, element, result);
1456 /* ERROR handling */
1459 g_warning ("adding elements to bin '%s' is not supported",
1460 GST_ELEMENT_NAME (bin));
1465 /* remove an element from the bin
1470 gst_bin_remove_func (GstBin * bin, GstElement * element)
1474 gboolean is_sink, is_source, provides_clock, requires_clock;
1475 gboolean othersink, othersource, otherprovider, otherrequirer, found;
1476 GstMessage *clock_message = NULL;
1477 GstClock **provided_clock_p;
1478 GstElement **clock_provider_p;
1480 gboolean other_async, this_async, have_no_preroll;
1481 GstStateChangeReturn ret;
1483 GST_DEBUG_OBJECT (bin, "element :%s", GST_ELEMENT_NAME (element));
1485 /* we obviously can't remove ourself from ourself */
1486 if (G_UNLIKELY (element == GST_ELEMENT_CAST (bin)))
1487 goto removing_itself;
1489 GST_OBJECT_LOCK (bin);
1491 GST_OBJECT_LOCK (element);
1492 elem_name = g_strdup (GST_ELEMENT_NAME (element));
1494 if (GST_OBJECT_PARENT (element) != GST_OBJECT_CAST (bin))
1497 /* remove the parent ref */
1498 GST_OBJECT_PARENT (element) = NULL;
1500 /* grab element name so we can print it */
1501 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1502 is_source = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE);
1504 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1506 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1507 GST_OBJECT_UNLOCK (element);
1511 othersource = FALSE;
1512 otherprovider = FALSE;
1513 otherrequirer = FALSE;
1514 have_no_preroll = FALSE;
1515 /* iterate the elements, we collect which ones are async and no_preroll. We
1516 * also remove the element when we find it. */
1517 for (walk = bin->children; walk; walk = next) {
1518 GstElement *child = GST_ELEMENT_CAST (walk->data);
1520 next = g_list_next (walk);
1522 if (child == element) {
1524 /* remove the element */
1525 bin->children = g_list_delete_link (bin->children, walk);
1527 gboolean child_sink, child_source, child_provider, child_requirer;
1529 GST_OBJECT_LOCK (child);
1530 child_sink = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SINK);
1531 child_source = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SOURCE);
1533 GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1535 GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1536 /* when we remove a sink, check if there are other sinks. */
1537 if (is_sink && !othersink && child_sink)
1539 if (is_source && !othersource && child_source)
1541 if (provides_clock && !otherprovider && child_provider)
1542 otherprovider = TRUE;
1543 if (requires_clock && !otherrequirer && child_requirer)
1544 otherrequirer = TRUE;
1545 /* check if we have NO_PREROLL children */
1546 if (GST_STATE_RETURN (child) == GST_STATE_CHANGE_NO_PREROLL)
1547 have_no_preroll = TRUE;
1548 GST_OBJECT_UNLOCK (child);
1552 /* the element must have been in the bin's list of children */
1553 if (G_UNLIKELY (!found))
1556 /* we now removed the element from the list of elements, increment the cookie
1557 * so that others can detect a change in the children list. */
1559 bin->children_cookie++;
1560 if (!GST_BIN_IS_NO_RESYNC (bin))
1561 bin->priv->structure_cookie++;
1563 if (is_sink && !othersink) {
1564 /* we're not a sink anymore */
1565 GST_DEBUG_OBJECT (bin, "we removed the last sink");
1566 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_SINK);
1568 if (is_source && !othersource) {
1569 /* we're not a source anymore */
1570 GST_DEBUG_OBJECT (bin, "we removed the last source");
1571 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_SOURCE);
1573 if (provides_clock && !otherprovider) {
1574 /* we're not a clock provider anymore */
1575 GST_DEBUG_OBJECT (bin, "we removed the last clock provider");
1576 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1578 if (requires_clock && !otherrequirer) {
1579 /* we're not a clock requirer anymore */
1580 GST_DEBUG_OBJECT (bin, "we removed the last clock requirer");
1581 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1584 /* if the clock provider for this element is removed, we lost
1585 * the clock as well, we need to inform the parent of this
1586 * so that it can select a new clock */
1587 if (bin->clock_provider == element) {
1588 GST_DEBUG_OBJECT (bin, "element \"%s\" provided the clock", elem_name);
1589 bin->clock_dirty = TRUE;
1591 gst_message_new_clock_lost (GST_OBJECT_CAST (bin), bin->provided_clock);
1592 provided_clock_p = &bin->provided_clock;
1593 clock_provider_p = &bin->clock_provider;
1594 gst_object_replace ((GstObject **) provided_clock_p, NULL);
1595 gst_object_replace ((GstObject **) clock_provider_p, NULL);
1598 /* remove messages for the element, if there was a pending ASYNC_START
1599 * message we must see if removing the element caused the bin to lose its
1602 other_async = FALSE;
1603 for (walk = bin->messages; walk; walk = next) {
1604 GstMessage *message = (GstMessage *) walk->data;
1605 GstElement *src = GST_ELEMENT_CAST (GST_MESSAGE_SRC (message));
1608 next = g_list_next (walk);
1611 switch (GST_MESSAGE_TYPE (message)) {
1612 case GST_MESSAGE_ASYNC_START:
1618 GST_DEBUG_OBJECT (src, "looking at message %p", message);
1620 case GST_MESSAGE_STRUCTURE_CHANGE:
1624 GST_DEBUG_OBJECT (src, "looking at structure change message %p",
1626 /* it's unlikely that this message is still in the list of messages
1627 * because this would mean that a link/unlink is busy in another thread
1628 * while we remove the element. We still have to remove the message
1629 * because we might not receive the done message anymore when the element
1630 * is removed from the bin. */
1631 gst_message_parse_structure_change (message, NULL, &owner, NULL);
1632 if (owner == element)
1643 /* delete all message types */
1644 GST_DEBUG_OBJECT (src, "deleting message %p of element \"%s\"",
1645 message, elem_name);
1646 bin->messages = g_list_delete_link (bin->messages, walk);
1647 gst_message_unref (message);
1651 /* get last return */
1652 ret = GST_STATE_RETURN (bin);
1654 /* no need to update the state if we are in error */
1655 if (ret == GST_STATE_CHANGE_FAILURE)
1656 goto no_state_recalc;
1658 if (!other_async && this_async) {
1659 /* all other elements were not async and we removed the async one,
1660 * handle the async-done case because we are not async anymore now. */
1661 GST_DEBUG_OBJECT (bin,
1662 "we removed the last async element, have no_preroll %d",
1665 /* the current state return of the bin depends on if there are no_preroll
1666 * elements in the pipeline or not */
1667 if (have_no_preroll)
1668 ret = GST_STATE_CHANGE_NO_PREROLL;
1670 ret = GST_STATE_CHANGE_SUCCESS;
1672 bin_handle_async_done (bin, ret, FALSE, GST_CLOCK_TIME_NONE);
1674 GST_DEBUG_OBJECT (bin,
1675 "recalc state preroll: %d, other async: %d, this async %d",
1676 have_no_preroll, other_async, this_async);
1678 if (have_no_preroll) {
1679 ret = GST_STATE_CHANGE_NO_PREROLL;
1680 } else if (other_async) {
1681 /* there are other async elements and we were not doing an async state
1682 * change, change our pending state and go async */
1683 if (GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING) {
1684 GST_STATE_NEXT (bin) = GST_STATE (bin);
1685 GST_STATE_PENDING (bin) = GST_STATE (bin);
1687 ret = GST_STATE_CHANGE_ASYNC;
1689 GST_STATE_RETURN (bin) = ret;
1693 gst_element_set_bus (element, NULL);
1694 /* Clear the clock we provided to the element */
1695 gst_element_set_clock (element, NULL);
1696 GST_OBJECT_UNLOCK (bin);
1699 gst_element_post_message (GST_ELEMENT_CAST (bin), clock_message);
1701 /* unlink all linked pads */
1702 it = gst_element_iterate_pads (element);
1703 gst_iterator_foreach (it, (GstIteratorForeachFunction) unlink_pads, NULL);
1704 gst_iterator_free (it);
1706 GST_CAT_INFO_OBJECT (GST_CAT_PARENTAGE, bin, "removed child \"%s\"",
1709 g_signal_emit (bin, gst_bin_signals[ELEMENT_REMOVED], 0, element);
1710 gst_child_proxy_child_removed ((GstChildProxy *) bin, (GObject *) element,
1713 g_signal_emit (bin, gst_bin_signals[DEEP_ELEMENT_REMOVED], 0, bin, element);
1716 /* element is really out of our control now */
1717 gst_object_unref (element);
1721 /* ERROR handling */
1724 GST_OBJECT_LOCK (bin);
1725 g_warning ("Cannot remove bin '%s' from itself", GST_ELEMENT_NAME (bin));
1726 GST_OBJECT_UNLOCK (bin);
1731 g_warning ("Element '%s' is not in bin '%s'", elem_name,
1732 GST_ELEMENT_NAME (bin));
1733 GST_OBJECT_UNLOCK (element);
1734 GST_OBJECT_UNLOCK (bin);
1743 * @element: (transfer none): the #GstElement to remove
1745 * Removes the element from the bin, unparenting it as well.
1746 * Unparenting the element means that the element will be dereferenced,
1747 * so if the bin holds the only reference to the element, the element
1748 * will be freed in the process of removing it from the bin. If you
1749 * want the element to still exist after removing, you need to call
1750 * gst_object_ref() before removing it from the bin.
1752 * If the element's pads are linked to other pads, the pads will be unlinked
1753 * before the element is removed from the bin.
1757 * Returns: %TRUE if the element could be removed, %FALSE if
1758 * the bin does not want to remove the element.
1761 gst_bin_remove (GstBin * bin, GstElement * element)
1763 GstBinClass *bclass;
1766 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1767 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1768 g_return_val_if_fail (GST_ELEMENT_CAST (bin) != element, FALSE);
1770 bclass = GST_BIN_GET_CLASS (bin);
1772 if (G_UNLIKELY (bclass->remove_element == NULL))
1775 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "removing element %s from bin %s",
1776 GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin));
1778 GST_TRACER_BIN_REMOVE_PRE (bin, element);
1779 result = bclass->remove_element (bin, element);
1780 GST_TRACER_BIN_REMOVE_POST (bin, result);
1784 /* ERROR handling */
1787 g_warning ("removing elements from bin '%s' is not supported",
1788 GST_ELEMENT_NAME (bin));
1794 * gst_bin_iterate_elements:
1797 * Gets an iterator for the elements in this bin.
1799 * MT safe. Caller owns returned value.
1801 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
1805 gst_bin_iterate_elements (GstBin * bin)
1807 GstIterator *result;
1809 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1811 GST_OBJECT_LOCK (bin);
1812 result = gst_iterator_new_list (GST_TYPE_ELEMENT,
1813 GST_OBJECT_GET_LOCK (bin),
1814 &bin->children_cookie, &bin->children, (GObject *) bin, NULL);
1815 GST_OBJECT_UNLOCK (bin);
1820 static GstIteratorItem
1821 iterate_child_recurse (GstIterator * it, const GValue * item)
1823 GstElement *child = g_value_get_object (item);
1825 if (GST_IS_BIN (child)) {
1826 GstIterator *other = gst_bin_iterate_recurse (GST_BIN_CAST (child));
1828 gst_iterator_push (it, other);
1830 return GST_ITERATOR_ITEM_PASS;
1834 * gst_bin_iterate_recurse:
1837 * Gets an iterator for the elements in this bin.
1838 * This iterator recurses into GstBin children.
1840 * MT safe. Caller owns returned value.
1842 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
1846 gst_bin_iterate_recurse (GstBin * bin)
1848 GstIterator *result;
1850 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1852 GST_OBJECT_LOCK (bin);
1853 result = gst_iterator_new_list (GST_TYPE_ELEMENT,
1854 GST_OBJECT_GET_LOCK (bin),
1855 &bin->children_cookie,
1857 (GObject *) bin, (GstIteratorItemFunction) iterate_child_recurse);
1858 GST_OBJECT_UNLOCK (bin);
1863 /* returns 0 when TRUE because this is a GCompareFunc */
1866 bin_element_is_sink (GstElement * child, GstBin * bin)
1870 /* we lock the child here for the remainder of the function to
1871 * get its name and flag safely. */
1872 GST_OBJECT_LOCK (child);
1873 is_sink = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SINK);
1875 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
1876 "child %s %s sink", GST_OBJECT_NAME (child), is_sink ? "is" : "is not");
1878 GST_OBJECT_UNLOCK (child);
1879 return is_sink ? 0 : 1;
1883 sink_iterator_filter (const GValue * vchild, GValue * vbin)
1885 GstBin *bin = g_value_get_object (vbin);
1886 GstElement *child = g_value_get_object (vchild);
1888 return (bin_element_is_sink (child, bin));
1892 * gst_bin_iterate_sinks:
1895 * Gets an iterator for all elements in the bin that have the
1896 * #GST_ELEMENT_FLAG_SINK flag set.
1898 * MT safe. Caller owns returned value.
1900 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
1904 gst_bin_iterate_sinks (GstBin * bin)
1906 GstIterator *children;
1907 GstIterator *result;
1908 GValue vbin = { 0, };
1910 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1912 g_value_init (&vbin, GST_TYPE_BIN);
1913 g_value_set_object (&vbin, bin);
1915 children = gst_bin_iterate_elements (bin);
1916 result = gst_iterator_filter (children,
1917 (GCompareFunc) sink_iterator_filter, &vbin);
1919 g_value_unset (&vbin);
1924 /* returns 0 when TRUE because this is a GCompareFunc */
1927 bin_element_is_src (GstElement * child, GstBin * bin)
1931 /* we lock the child here for the remainder of the function to
1932 * get its name and other info safely. */
1933 GST_OBJECT_LOCK (child);
1934 is_src = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SOURCE);
1936 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
1937 "child %s %s src", GST_OBJECT_NAME (child), is_src ? "is" : "is not");
1939 GST_OBJECT_UNLOCK (child);
1940 return is_src ? 0 : 1;
1944 src_iterator_filter (const GValue * vchild, GValue * vbin)
1946 GstBin *bin = g_value_get_object (vbin);
1947 GstElement *child = g_value_get_object (vchild);
1949 return (bin_element_is_src (child, bin));
1953 * gst_bin_iterate_sources:
1956 * Gets an iterator for all elements in the bin that have the
1957 * #GST_ELEMENT_FLAG_SOURCE flag set.
1959 * MT safe. Caller owns returned value.
1961 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
1965 gst_bin_iterate_sources (GstBin * bin)
1967 GstIterator *children;
1968 GstIterator *result;
1969 GValue vbin = { 0, };
1971 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1973 g_value_init (&vbin, GST_TYPE_BIN);
1974 g_value_set_object (&vbin, bin);
1976 children = gst_bin_iterate_elements (bin);
1977 result = gst_iterator_filter (children,
1978 (GCompareFunc) src_iterator_filter, &vbin);
1980 g_value_unset (&vbin);
1988 static GstStateChangeReturn
1989 gst_bin_get_state_func (GstElement * element, GstState * state,
1990 GstState * pending, GstClockTime timeout)
1992 GstStateChangeReturn ret;
1994 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
1997 GST_ELEMENT_CLASS (parent_class)->get_state (element, state, pending,
2003 /***********************************************
2004 * Topologically sorted iterator
2005 * see http://en.wikipedia.org/wiki/Topological_sorting
2007 * For each element in the graph, an entry is kept in a HashTable
2008 * with its number of srcpad connections (degree).
2009 * We then change state of all elements without dependencies
2010 * (degree 0) and decrement the degree of all elements connected
2011 * on the sinkpads. When an element reaches degree 0, its state is
2013 * When all elements are handled the algorithm stops.
2015 typedef struct _GstBinSortIterator
2018 GQueue queue; /* elements queued for state change */
2019 GstBin *bin; /* bin we iterate */
2020 gint mode; /* adding or removing dependency */
2021 GstElement *best; /* next element with least dependencies */
2022 gint best_deg; /* best degree */
2023 GHashTable *hash; /* hashtable with element dependencies */
2024 gboolean dirty; /* we detected structure change */
2025 } GstBinSortIterator;
2028 gst_bin_sort_iterator_copy (const GstBinSortIterator * it,
2029 GstBinSortIterator * copy)
2031 GHashTableIter iter;
2032 gpointer key, value;
2034 copy->queue = it->queue;
2035 g_queue_foreach (©->queue, (GFunc) gst_object_ref, NULL);
2037 copy->bin = gst_object_ref (it->bin);
2039 copy->best = gst_object_ref (it->best);
2041 copy->hash = g_hash_table_new (NULL, NULL);
2042 g_hash_table_iter_init (&iter, it->hash);
2043 while (g_hash_table_iter_next (&iter, &key, &value))
2044 g_hash_table_insert (copy->hash, key, value);
2047 /* we add and subtract 1 to make sure we don't confuse NULL and 0 */
2048 #define HASH_SET_DEGREE(bit, elem, deg) \
2049 g_hash_table_replace (bit->hash, elem, GINT_TO_POINTER(deg+1))
2050 #define HASH_GET_DEGREE(bit, elem) \
2051 (GPOINTER_TO_INT(g_hash_table_lookup (bit->hash, elem))-1)
2053 /* add element to queue of next elements in the iterator.
2054 * We push at the tail to give higher priority elements a
2057 add_to_queue (GstBinSortIterator * bit, GstElement * element)
2059 GST_DEBUG_OBJECT (bit->bin, "adding '%s' to queue",
2060 GST_ELEMENT_NAME (element));
2061 gst_object_ref (element);
2062 g_queue_push_tail (&bit->queue, element);
2063 HASH_SET_DEGREE (bit, element, -1);
2067 remove_from_queue (GstBinSortIterator * bit, GstElement * element)
2071 if ((find = g_queue_find (&bit->queue, element))) {
2072 GST_DEBUG_OBJECT (bit->bin, "removing '%s' from queue",
2073 GST_ELEMENT_NAME (element));
2075 g_queue_delete_link (&bit->queue, find);
2076 gst_object_unref (element);
2078 GST_DEBUG_OBJECT (bit->bin, "unable to remove '%s' from queue",
2079 GST_ELEMENT_NAME (element));
2083 /* clear the queue, unref all objects as we took a ref when
2084 * we added them to the queue */
2086 clear_queue (GQueue * queue)
2090 while ((p = g_queue_pop_head (queue)))
2091 gst_object_unref (p);
2094 /* set all degrees to 0. Elements marked as a sink are
2095 * added to the queue immediately. Since we only look at the SINK flag of the
2096 * element, it is possible that we add non-sinks to the queue. These will be
2097 * removed from the queue again when we can prove that it provides data for some
2100 reset_degree (GstElement * element, GstBinSortIterator * bit)
2104 /* sinks are added right away */
2105 GST_OBJECT_LOCK (element);
2106 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
2107 GST_OBJECT_UNLOCK (element);
2110 add_to_queue (bit, element);
2112 /* others are marked with 0 and handled when sinks are done */
2113 HASH_SET_DEGREE (bit, element, 0);
2117 /* adjust the degree of all elements connected to the given
2118 * element. If a degree of an element drops to 0, it is
2119 * added to the queue of elements to schedule next.
2121 * We have to make sure not to cross the bin boundary this element
2125 update_degree (GstElement * element, GstBinSortIterator * bit)
2127 gboolean linked = FALSE;
2129 GST_OBJECT_LOCK (element);
2130 /* don't touch degree if element has no sinkpads */
2131 if (element->numsinkpads != 0) {
2132 /* loop over all sinkpads, decrement degree for all connected
2133 * elements in this bin */
2136 for (pads = element->sinkpads; pads; pads = g_list_next (pads)) {
2139 pad = GST_PAD_CAST (pads->data);
2141 /* we're iterating over the sinkpads, check if it's busy in a link/unlink */
2142 if (G_UNLIKELY (find_message (bit->bin, GST_OBJECT_CAST (pad),
2143 GST_MESSAGE_STRUCTURE_CHANGE))) {
2144 /* mark the iterator as dirty because we won't be updating the degree
2145 * of the peer parent now. This would result in the 'loop detected'
2146 * later on because the peer parent element could become the best next
2147 * element with a degree > 0. We will simply continue our state
2148 * changes and we'll eventually resync when the unlink completed and
2149 * the iterator cookie is updated. */
2154 if ((peer = gst_pad_get_peer (pad))) {
2155 GstElement *peer_element;
2157 if ((peer_element = gst_pad_get_parent_element (peer))) {
2158 GST_OBJECT_LOCK (peer_element);
2159 /* check that we don't go outside of this bin */
2160 if (GST_OBJECT_CAST (peer_element)->parent ==
2161 GST_OBJECT_CAST (bit->bin)) {
2162 gint old_deg, new_deg;
2164 old_deg = HASH_GET_DEGREE (bit, peer_element);
2166 /* check to see if we added an element as sink that was not really a
2167 * sink because it was connected to some other element. */
2168 if (old_deg == -1) {
2169 remove_from_queue (bit, peer_element);
2172 new_deg = old_deg + bit->mode;
2174 GST_DEBUG_OBJECT (bit->bin,
2175 "change element %s, degree %d->%d, linked to %s",
2176 GST_ELEMENT_NAME (peer_element), old_deg, new_deg,
2177 GST_ELEMENT_NAME (element));
2179 /* update degree, it is possible that an element was in 0 and
2180 * reaches -1 here. This would mean that the element had no sinkpads
2181 * but became linked while the state change was happening. We will
2182 * resync on this with the structure change message. */
2184 /* degree hit 0, add to queue */
2185 add_to_queue (bit, peer_element);
2187 HASH_SET_DEGREE (bit, peer_element, new_deg);
2191 GST_OBJECT_UNLOCK (peer_element);
2192 gst_object_unref (peer_element);
2194 gst_object_unref (peer);
2199 GST_DEBUG_OBJECT (bit->bin, "element %s not linked on any sinkpads",
2200 GST_ELEMENT_NAME (element));
2202 GST_OBJECT_UNLOCK (element);
2205 /* find the next best element not handled yet. This is the one
2206 * with the lowest non-negative degree */
2208 find_element (GstElement * element, GstBinSortIterator * bit)
2212 /* element is already handled */
2213 if ((degree = HASH_GET_DEGREE (bit, element)) < 0)
2216 /* first element or element with smaller degree */
2217 if (bit->best == NULL || bit->best_deg > degree) {
2218 bit->best = element;
2219 bit->best_deg = degree;
2223 /* get next element in iterator. */
2224 static GstIteratorResult
2225 gst_bin_sort_iterator_next (GstBinSortIterator * bit, GValue * result)
2228 GstBin *bin = bit->bin;
2230 /* empty queue, we have to find a next best element */
2231 if (g_queue_is_empty (&bit->queue)) {
2233 bit->best_deg = G_MAXINT;
2234 g_list_foreach (bin->children, (GFunc) find_element, bit);
2235 if ((best = bit->best)) {
2236 /* when we detected an unlink, don't warn because our degrees might be
2237 * screwed up. We will resync later */
2238 if (bit->best_deg != 0 && !bit->dirty) {
2239 /* we don't fail on this one yet */
2240 GST_WARNING_OBJECT (bin, "loop dected in graph");
2241 g_warning ("loop detected in the graph of bin '%s'!!",
2242 GST_ELEMENT_NAME (bin));
2244 /* best unhandled element, schedule as next element */
2245 GST_DEBUG_OBJECT (bin, "queue empty, next best: %s",
2246 GST_ELEMENT_NAME (best));
2247 HASH_SET_DEGREE (bit, best, -1);
2248 g_value_set_object (result, best);
2250 GST_DEBUG_OBJECT (bin, "queue empty, elements exhausted");
2251 /* no more unhandled elements, we are done */
2252 return GST_ITERATOR_DONE;
2255 /* everything added to the queue got reffed */
2256 best = g_queue_pop_head (&bit->queue);
2257 g_value_set_object (result, best);
2258 gst_object_unref (best);
2261 GST_DEBUG_OBJECT (bin, "queue head gives %s", GST_ELEMENT_NAME (best));
2262 /* update degrees of linked elements */
2263 update_degree (best, bit);
2265 return GST_ITERATOR_OK;
2268 /* clear queues, recalculate the degrees and restart. */
2270 gst_bin_sort_iterator_resync (GstBinSortIterator * bit)
2272 GstBin *bin = bit->bin;
2274 GST_DEBUG_OBJECT (bin, "resync");
2276 clear_queue (&bit->queue);
2278 g_list_foreach (bin->children, (GFunc) reset_degree, bit);
2279 /* calc degrees, incrementing */
2281 g_list_foreach (bin->children, (GFunc) update_degree, bit);
2282 /* for the rest of the function we decrement the degrees */
2286 /* clear queues, unref bin and free iterator. */
2288 gst_bin_sort_iterator_free (GstBinSortIterator * bit)
2290 GstBin *bin = bit->bin;
2292 GST_DEBUG_OBJECT (bin, "free");
2293 clear_queue (&bit->queue);
2294 g_hash_table_destroy (bit->hash);
2295 gst_object_unref (bin);
2298 /* should be called with the bin LOCK held */
2299 static GstIterator *
2300 gst_bin_sort_iterator_new (GstBin * bin)
2302 GstBinSortIterator *result;
2304 /* we don't need an ItemFunction because we ref the items in the _next
2306 result = (GstBinSortIterator *)
2307 gst_iterator_new (sizeof (GstBinSortIterator),
2309 GST_OBJECT_GET_LOCK (bin),
2310 &bin->priv->structure_cookie,
2311 (GstIteratorCopyFunction) gst_bin_sort_iterator_copy,
2312 (GstIteratorNextFunction) gst_bin_sort_iterator_next,
2313 (GstIteratorItemFunction) NULL,
2314 (GstIteratorResyncFunction) gst_bin_sort_iterator_resync,
2315 (GstIteratorFreeFunction) gst_bin_sort_iterator_free);
2316 g_queue_init (&result->queue);
2317 result->hash = g_hash_table_new (NULL, NULL);
2318 gst_object_ref (bin);
2320 gst_bin_sort_iterator_resync (result);
2322 return (GstIterator *) result;
2326 * gst_bin_iterate_sorted:
2329 * Gets an iterator for the elements in this bin in topologically
2330 * sorted order. This means that the elements are returned from
2331 * the most downstream elements (sinks) to the sources.
2333 * This function is used internally to perform the state changes
2334 * of the bin elements and for clock selection.
2336 * MT safe. Caller owns returned value.
2338 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
2342 gst_bin_iterate_sorted (GstBin * bin)
2344 GstIterator *result;
2346 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
2348 GST_OBJECT_LOCK (bin);
2349 result = gst_bin_sort_iterator_new (bin);
2350 GST_OBJECT_UNLOCK (bin);
2355 static GstStateChangeReturn
2356 gst_bin_element_set_state (GstBin * bin, GstElement * element,
2357 GstClockTime base_time, GstClockTime start_time, GstState current,
2360 GstStateChangeReturn ret;
2361 GstState child_current, child_pending;
2365 GST_STATE_LOCK (element);
2367 GST_OBJECT_LOCK (element);
2368 /* set base_time and start time on child */
2369 GST_ELEMENT_START_TIME (element) = start_time;
2370 element->base_time = base_time;
2371 /* peel off the locked flag */
2372 locked = GST_ELEMENT_IS_LOCKED_STATE (element);
2373 /* Get the previous set_state result to preserve NO_PREROLL and ASYNC */
2374 ret = GST_STATE_RETURN (element);
2375 child_current = GST_STATE (element);
2376 child_pending = GST_STATE_PENDING (element);
2377 GST_OBJECT_UNLOCK (element);
2379 /* skip locked elements */
2380 if (G_UNLIKELY (locked))
2383 /* if the element was no preroll, just start changing the state regardless
2384 * if it had async elements (in the case of a bin) because they won't preroll
2386 if (G_UNLIKELY (ret == GST_STATE_CHANGE_NO_PREROLL)) {
2387 GST_DEBUG_OBJECT (element, "element is NO_PREROLL, ignore async elements");
2391 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2392 "current %s pending %s, desired next %s",
2393 gst_element_state_get_name (child_current),
2394 gst_element_state_get_name (child_pending),
2395 gst_element_state_get_name (next));
2397 /* always recurse into bins so that we can set the base time */
2398 if (GST_IS_BIN (element))
2401 /* Try not to change the state of elements that are already in the state we're
2403 if (child_current == next && child_pending == GST_STATE_VOID_PENDING) {
2404 /* child is already at the requested state, return previous return. Note that
2405 * if the child has a pending state to next, we will still call the
2406 * set_state function */
2408 } else if (next > current) {
2409 /* upward state change */
2410 if (child_pending == GST_STATE_VOID_PENDING) {
2411 /* .. and the child is not busy doing anything */
2412 if (child_current > next) {
2413 /* .. and is already past the requested state, assume it got there
2415 ret = GST_STATE_CHANGE_SUCCESS;
2418 } else if (child_pending > child_current) {
2419 /* .. and the child is busy going upwards */
2420 if (child_current >= next) {
2421 /* .. and is already past the requested state, assume it got there
2423 ret = GST_STATE_CHANGE_SUCCESS;
2427 /* .. and the child is busy going downwards */
2428 if (child_current > next) {
2429 /* .. and is already past the requested state, assume it got there
2431 ret = GST_STATE_CHANGE_SUCCESS;
2435 } else if (next < current) {
2436 /* downward state change */
2437 if (child_pending == GST_STATE_VOID_PENDING) {
2438 /* .. and the child is not busy doing anything */
2439 if (child_current < next) {
2440 /* .. and is already past the requested state, assume it got there
2442 ret = GST_STATE_CHANGE_SUCCESS;
2445 } else if (child_pending < child_current) {
2446 /* .. and the child is busy going downwards */
2447 if (child_current <= next) {
2448 /* .. and is already past the requested state, assume it got there
2450 ret = GST_STATE_CHANGE_SUCCESS;
2454 /* .. and the child is busy going upwards */
2455 if (child_current < next) {
2456 /* .. and is already past the requested state, assume it got there
2458 ret = GST_STATE_CHANGE_SUCCESS;
2465 GST_OBJECT_LOCK (bin);
2466 /* the element was busy with an upwards async state change, we must wait for
2467 * an ASYNC_DONE message before we attemp to change the state. */
2469 find_message (bin, GST_OBJECT_CAST (element),
2470 GST_MESSAGE_ASYNC_START))) {
2471 #ifndef GST_DISABLE_GST_DEBUG
2472 GstMessage *message = GST_MESSAGE_CAST (found->data);
2474 GST_DEBUG_OBJECT (element, "element message %p, %s async busy",
2475 message, GST_ELEMENT_NAME (GST_MESSAGE_SRC (message)));
2477 /* only wait for upward state changes */
2478 if (next > current) {
2479 /* We found an async element check if we can force its state to change or
2480 * if we have to wait for it to preroll. */
2484 GST_OBJECT_UNLOCK (bin);
2487 GST_DEBUG_OBJECT (bin,
2488 "setting element %s to %s, base_time %" GST_TIME_FORMAT,
2489 GST_ELEMENT_NAME (element), gst_element_state_get_name (next),
2490 GST_TIME_ARGS (base_time));
2493 ret = gst_element_set_state (element, next);
2495 GST_STATE_UNLOCK (element);
2501 GST_DEBUG_OBJECT (element,
2502 "element is locked, return previous return %s",
2503 gst_element_state_change_return_get_name (ret));
2504 GST_STATE_UNLOCK (element);
2509 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2510 "skipping transition from %s to %s",
2511 gst_element_state_get_name (child_current),
2512 gst_element_state_get_name (next));
2513 GST_STATE_UNLOCK (element);
2518 GST_DEBUG_OBJECT (element, "element was busy, delaying state change");
2519 GST_OBJECT_UNLOCK (bin);
2520 GST_STATE_UNLOCK (element);
2521 return GST_STATE_CHANGE_ASYNC;
2525 /* gst_iterator_fold functions for pads_activate
2526 * Stop the iterator if activating one pad failed, but only if that pad
2527 * has not been removed from the element. */
2529 activate_pads (const GValue * vpad, GValue * ret, gboolean * active)
2531 GstPad *pad = g_value_get_object (vpad);
2532 gboolean cont = TRUE;
2534 if (!gst_pad_set_active (pad, *active)) {
2535 if (GST_PAD_PARENT (pad) != NULL) {
2537 g_value_set_boolean (ret, FALSE);
2544 /* returns false on error or early cutout of the fold, true if all
2545 * pads in @iter were (de)activated successfully. */
2547 iterator_activate_fold_with_resync (GstIterator * iter, gpointer user_data)
2549 GstIteratorResult ires;
2552 /* no need to unset this later, it's just a boolean */
2553 g_value_init (&ret, G_TYPE_BOOLEAN);
2554 g_value_set_boolean (&ret, TRUE);
2557 ires = gst_iterator_fold (iter, (GstIteratorFoldFunction) activate_pads,
2560 case GST_ITERATOR_RESYNC:
2561 /* need to reset the result again */
2562 g_value_set_boolean (&ret, TRUE);
2563 gst_iterator_resync (iter);
2565 case GST_ITERATOR_DONE:
2566 /* all pads iterated, return collected value */
2569 /* iterator returned _ERROR or premature end with _OK,
2570 * mark an error and exit */
2571 g_value_set_boolean (&ret, FALSE);
2576 /* return collected value */
2577 return g_value_get_boolean (&ret);
2580 /* is called with STATE_LOCK
2583 gst_bin_src_pads_activate (GstBin * bin, gboolean active)
2588 GST_DEBUG_OBJECT (bin, "%s pads", active ? "activate" : "deactivate");
2590 iter = gst_element_iterate_src_pads ((GstElement *) bin);
2591 fold_ok = iterator_activate_fold_with_resync (iter, &active);
2592 gst_iterator_free (iter);
2593 if (G_UNLIKELY (!fold_ok))
2596 GST_DEBUG_OBJECT (bin, "pad %sactivation successful", active ? "" : "de");
2603 GST_DEBUG_OBJECT (bin, "pad %sactivation failed", active ? "" : "de");
2609 * gst_bin_recalculate_latency:
2612 * Query @bin for the current latency using and reconfigures this latency to all the
2613 * elements with a LATENCY event.
2615 * This method is typically called on the pipeline when a #GST_MESSAGE_LATENCY
2616 * is posted on the bus.
2618 * This function simply emits the 'do-latency' signal so any custom latency
2619 * calculations will be performed.
2621 * Returns: %TRUE if the latency could be queried and reconfigured.
2624 gst_bin_recalculate_latency (GstBin * bin)
2628 g_signal_emit (bin, gst_bin_signals[DO_LATENCY], 0, &res);
2629 GST_DEBUG_OBJECT (bin, "latency returned %d", res);
2635 gst_bin_do_latency_func (GstBin * bin)
2638 GstElement *element;
2639 GstClockTime min_latency, max_latency;
2642 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2644 element = GST_ELEMENT_CAST (bin);
2646 GST_DEBUG_OBJECT (element, "querying latency");
2648 query = gst_query_new_latency ();
2649 if ((res = gst_element_query (element, query))) {
2652 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
2654 GST_DEBUG_OBJECT (element,
2655 "got min latency %" GST_TIME_FORMAT ", max latency %"
2656 GST_TIME_FORMAT ", live %d", GST_TIME_ARGS (min_latency),
2657 GST_TIME_ARGS (max_latency), live);
2659 if (max_latency < min_latency) {
2660 /* this is an impossible situation, some parts of the pipeline might not
2661 * work correctly. We post a warning for now. */
2662 GST_ELEMENT_WARNING (element, CORE, CLOCK, (NULL),
2663 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
2664 GST_TIME_FORMAT ". Add queues or other buffering elements.",
2665 GST_TIME_ARGS (max_latency), GST_TIME_ARGS (min_latency)));
2668 /* configure latency on elements */
2669 res = gst_element_send_event (element, gst_event_new_latency (min_latency));
2671 GST_INFO_OBJECT (element, "configured latency of %" GST_TIME_FORMAT,
2672 GST_TIME_ARGS (min_latency));
2674 GST_WARNING_OBJECT (element,
2675 "did not really configure latency of %" GST_TIME_FORMAT,
2676 GST_TIME_ARGS (min_latency));
2679 /* this is not a real problem, we just don't configure any latency. */
2680 GST_WARNING_OBJECT (element, "failed to query latency");
2682 gst_query_unref (query);
2688 gst_bin_post_message (GstElement * element, GstMessage * msg)
2690 GstElementClass *pklass = (GstElementClass *) parent_class;
2693 ret = pklass->post_message (element, gst_message_ref (msg));
2695 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_STATE_CHANGED &&
2696 GST_MESSAGE_SRC (msg) == GST_OBJECT_CAST (element)) {
2697 GstState newstate, pending;
2699 gst_message_parse_state_changed (msg, NULL, &newstate, &pending);
2700 if (newstate == GST_STATE_PLAYING && pending == GST_STATE_VOID_PENDING) {
2701 GST_BIN_CAST (element)->priv->posted_playing = TRUE;
2702 bin_do_eos (GST_BIN_CAST (element));
2704 GST_BIN_CAST (element)->priv->posted_playing = FALSE;
2708 gst_message_unref (msg);
2714 reset_state (const GValue * data, gpointer user_data)
2716 GstElement *e = g_value_get_object (data);
2717 GstState state = GPOINTER_TO_INT (user_data);
2719 if (gst_element_set_state (e, state) == GST_STATE_CHANGE_FAILURE)
2720 GST_WARNING_OBJECT (e, "Failed to switch back down to %s",
2721 gst_element_state_get_name (state));
2724 static GstStateChangeReturn
2725 gst_bin_change_state_func (GstElement * element, GstStateChange transition)
2728 GstStateChangeReturn ret;
2729 GstState current, next;
2730 gboolean have_async;
2731 gboolean have_no_preroll;
2732 GstClockTime base_time, start_time;
2735 GValue data = { 0, };
2737 /* we don't need to take the STATE_LOCK, it is already taken */
2738 current = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2739 next = (GstState) GST_STATE_TRANSITION_NEXT (transition);
2741 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2742 "changing state of children from %s to %s",
2743 gst_element_state_get_name (current), gst_element_state_get_name (next));
2745 bin = GST_BIN_CAST (element);
2748 case GST_STATE_PLAYING:
2750 gboolean toplevel, asynchandling;
2752 GST_OBJECT_LOCK (bin);
2753 toplevel = BIN_IS_TOPLEVEL (bin);
2754 asynchandling = bin->priv->asynchandling;
2755 GST_OBJECT_UNLOCK (bin);
2758 gst_bin_recalculate_latency (bin);
2760 gst_element_post_message (element,
2761 gst_message_new_latency (GST_OBJECT_CAST (element)));
2764 case GST_STATE_PAUSED:
2765 /* Clear EOS list on next PAUSED */
2766 GST_OBJECT_LOCK (bin);
2767 GST_DEBUG_OBJECT (element, "clearing EOS elements");
2768 bin_remove_messages (bin, NULL, GST_MESSAGE_EOS);
2769 bin->priv->posted_eos = FALSE;
2770 if (current == GST_STATE_READY)
2771 bin_remove_messages (bin, NULL, GST_MESSAGE_STREAM_START);
2772 GST_OBJECT_UNLOCK (bin);
2773 if (current == GST_STATE_READY)
2774 if (!(gst_bin_src_pads_activate (bin, TRUE)))
2775 goto activate_failure;
2777 case GST_STATE_READY:
2778 /* Clear message list on next READY */
2779 GST_OBJECT_LOCK (bin);
2780 GST_DEBUG_OBJECT (element, "clearing all cached messages");
2781 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
2782 GST_OBJECT_UNLOCK (bin);
2783 /* We might not have reached PAUSED yet due to async errors,
2784 * make sure to always deactivate the pads nonetheless */
2785 if (!(gst_bin_src_pads_activate (bin, FALSE)))
2786 goto activate_failure;
2788 case GST_STATE_NULL:
2789 /* Clear message list on next NULL */
2790 GST_OBJECT_LOCK (bin);
2791 GST_DEBUG_OBJECT (element, "clearing all cached messages");
2792 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
2793 GST_OBJECT_UNLOCK (bin);
2794 if (current == GST_STATE_READY) {
2795 if (!(gst_bin_src_pads_activate (bin, FALSE)))
2796 goto activate_failure;
2803 /* this flag is used to make the async state changes return immediately. We
2804 * don't want them to interfere with this state change */
2805 GST_OBJECT_LOCK (bin);
2806 bin->polling = TRUE;
2807 GST_OBJECT_UNLOCK (bin);
2809 /* iterate in state change order */
2810 it = gst_bin_iterate_sorted (bin);
2812 /* mark if we've seen an ASYNC element in the bin when we did a state change.
2813 * Note how we don't reset this value when a resync happens, the reason being
2814 * that the async element posted ASYNC_START and we want to post ASYNC_DONE
2815 * even after a resync when the async element is gone */
2819 /* take base_time */
2820 base_time = gst_element_get_base_time (element);
2821 start_time = gst_element_get_start_time (element);
2823 have_no_preroll = FALSE;
2827 switch (gst_iterator_next (it, &data)) {
2828 case GST_ITERATOR_OK:
2832 child = g_value_get_object (&data);
2834 /* set state and base_time now */
2835 ret = gst_bin_element_set_state (bin, child, base_time, start_time,
2839 case GST_STATE_CHANGE_SUCCESS:
2840 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2841 "child '%s' changed state to %d(%s) successfully",
2842 GST_ELEMENT_NAME (child), next,
2843 gst_element_state_get_name (next));
2845 case GST_STATE_CHANGE_ASYNC:
2847 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2848 "child '%s' is changing state asynchronously to %s",
2849 GST_ELEMENT_NAME (child), gst_element_state_get_name (next));
2853 case GST_STATE_CHANGE_FAILURE:{
2856 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2857 "child '%s' failed to go to state %d(%s)",
2858 GST_ELEMENT_NAME (child),
2859 next, gst_element_state_get_name (next));
2861 /* Only fail if the child is still inside
2862 * this bin. It might've been removed already
2863 * because of the error by the bin subclass
2864 * to ignore the error. */
2865 parent = gst_object_get_parent (GST_OBJECT_CAST (child));
2866 if (parent == GST_OBJECT_CAST (element)) {
2867 /* element is still in bin, really error now */
2868 gst_object_unref (parent);
2871 /* child removed from bin, let the resync code redo the state
2873 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2874 "child '%s' was removed from the bin",
2875 GST_ELEMENT_NAME (child));
2878 gst_object_unref (parent);
2882 case GST_STATE_CHANGE_NO_PREROLL:
2883 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2884 "child '%s' changed state to %d(%s) successfully without preroll",
2885 GST_ELEMENT_NAME (child), next,
2886 gst_element_state_get_name (next));
2887 have_no_preroll = TRUE;
2890 g_assert_not_reached ();
2893 g_value_reset (&data);
2896 case GST_ITERATOR_RESYNC:
2897 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "iterator doing resync");
2898 gst_iterator_resync (it);
2901 case GST_ITERATOR_DONE:
2902 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "iterator done");
2908 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2909 if (G_UNLIKELY (ret == GST_STATE_CHANGE_FAILURE))
2912 if (have_no_preroll) {
2913 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
2914 "we have NO_PREROLL elements %s -> NO_PREROLL",
2915 gst_element_state_change_return_get_name (ret));
2916 ret = GST_STATE_CHANGE_NO_PREROLL;
2917 } else if (have_async) {
2918 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
2919 "we have ASYNC elements %s -> ASYNC",
2920 gst_element_state_change_return_get_name (ret));
2921 ret = GST_STATE_CHANGE_ASYNC;
2925 g_value_unset (&data);
2926 gst_iterator_free (it);
2928 GST_OBJECT_LOCK (bin);
2929 bin->polling = FALSE;
2930 /* it's possible that we did not get ASYNC from the children while the bin is
2931 * simulating ASYNC behaviour by posting an ASYNC_DONE message on the bus with
2932 * itself as the source. In that case we still want to check if the state
2933 * change completed. */
2934 if (ret != GST_STATE_CHANGE_ASYNC && !bin->priv->pending_async_done) {
2935 /* no element returned ASYNC and there are no pending async_done messages,
2936 * we can just complete. */
2937 GST_DEBUG_OBJECT (bin, "no async elements");
2940 /* when we get here an ASYNC element was found */
2941 if (GST_STATE_TARGET (bin) <= GST_STATE_READY) {
2942 /* we ignore ASYNC state changes when we go to READY or NULL */
2943 GST_DEBUG_OBJECT (bin, "target state %s <= READY",
2944 gst_element_state_get_name (GST_STATE_TARGET (bin)));
2948 GST_DEBUG_OBJECT (bin, "check async elements");
2949 /* check if all elements managed to commit their state already */
2950 if (!find_message (bin, NULL, GST_MESSAGE_ASYNC_START)) {
2951 /* nothing found, remove all old ASYNC_DONE messages. This can happen when
2952 * all the elements commited their state while we were doing the state
2953 * change. We will still return ASYNC for consistency but we commit the
2954 * state already so that a _get_state() will return immediately. */
2955 bin_remove_messages (bin, NULL, GST_MESSAGE_ASYNC_DONE);
2957 GST_DEBUG_OBJECT (bin, "async elements commited");
2958 bin_handle_async_done (bin, GST_STATE_CHANGE_SUCCESS, FALSE,
2959 GST_CLOCK_TIME_NONE);
2963 bin->priv->pending_async_done = FALSE;
2964 GST_OBJECT_UNLOCK (bin);
2966 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2967 "done changing bin's state from %s to %s, now in %s, ret %s",
2968 gst_element_state_get_name (current),
2969 gst_element_state_get_name (next),
2970 gst_element_state_get_name (GST_STATE (element)),
2971 gst_element_state_change_return_get_name (ret));
2978 GST_CAT_WARNING_OBJECT (GST_CAT_STATES, element,
2979 "failure (de)activating src pads");
2980 return GST_STATE_CHANGE_FAILURE;
2985 if (current < next) {
2986 GstIterator *it = gst_bin_iterate_sorted (GST_BIN (element));
2987 GstIteratorResult ret;
2989 GST_DEBUG_OBJECT (element,
2990 "Bin failed to change state, switching children back to %s",
2991 gst_element_state_get_name (current));
2994 gst_iterator_foreach (it, &reset_state, GINT_TO_POINTER (current));
2995 } while (ret == GST_ITERATOR_RESYNC);
2996 gst_iterator_free (it);
3003 * This function is a utility event handler for seek events.
3004 * It will send the event to all sinks or sources and appropriate
3005 * ghost pads depending on the event-direction.
3007 * Applications are free to override this behaviour and
3008 * implement their own seek handler, but this will work for
3009 * pretty much all cases in practice.
3012 gst_bin_send_event (GstElement * element, GstEvent * event)
3014 GstBin *bin = GST_BIN_CAST (element);
3016 gboolean res = TRUE;
3017 gboolean done = FALSE;
3018 GValue data = { 0, };
3020 if (GST_EVENT_IS_DOWNSTREAM (event)) {
3021 iter = gst_bin_iterate_sources (bin);
3022 GST_DEBUG_OBJECT (bin, "Sending %s event to src children",
3023 GST_EVENT_TYPE_NAME (event));
3025 iter = gst_bin_iterate_sinks (bin);
3026 GST_DEBUG_OBJECT (bin, "Sending %s event to sink children",
3027 GST_EVENT_TYPE_NAME (event));
3031 switch (gst_iterator_next (iter, &data)) {
3032 case GST_ITERATOR_OK:
3034 GstElement *child = g_value_get_object (&data);
3036 gst_event_ref (event);
3037 res &= gst_element_send_event (child, event);
3039 GST_LOG_OBJECT (child, "After handling %s event: %d",
3040 GST_EVENT_TYPE_NAME (event), res);
3042 g_value_reset (&data);
3045 case GST_ITERATOR_RESYNC:
3046 gst_iterator_resync (iter);
3049 case GST_ITERATOR_DONE:
3052 case GST_ITERATOR_ERROR:
3053 g_assert_not_reached ();
3057 g_value_unset (&data);
3058 gst_iterator_free (iter);
3060 if (GST_EVENT_IS_DOWNSTREAM (event)) {
3061 iter = gst_element_iterate_sink_pads (GST_ELEMENT (bin));
3062 GST_DEBUG_OBJECT (bin, "Sending %s event to sink pads",
3063 GST_EVENT_TYPE_NAME (event));
3065 iter = gst_element_iterate_src_pads (GST_ELEMENT (bin));
3066 GST_DEBUG_OBJECT (bin, "Sending %s event to src pads",
3067 GST_EVENT_TYPE_NAME (event));
3072 switch (gst_iterator_next (iter, &data)) {
3073 case GST_ITERATOR_OK:
3075 GstPad *pad = g_value_get_object (&data);
3077 gst_event_ref (event);
3078 res &= gst_pad_send_event (pad, event);
3079 GST_LOG_OBJECT (pad, "After handling %s event: %d",
3080 GST_EVENT_TYPE_NAME (event), res);
3083 case GST_ITERATOR_RESYNC:
3084 gst_iterator_resync (iter);
3087 case GST_ITERATOR_DONE:
3090 case GST_ITERATOR_ERROR:
3091 g_assert_not_reached ();
3096 g_value_unset (&data);
3097 gst_iterator_free (iter);
3098 gst_event_unref (event);
3103 /* this is the function called by the threadpool. When async elements commit
3104 * their state, this function will attempt to bring the bin to the next state.
3107 gst_bin_continue_func (GstBin * bin, BinContinueData * data)
3109 GstState current, next, pending;
3110 GstStateChange transition;
3112 pending = data->pending;
3114 GST_DEBUG_OBJECT (bin, "waiting for state lock");
3115 GST_STATE_LOCK (bin);
3117 GST_DEBUG_OBJECT (bin, "doing state continue");
3118 GST_OBJECT_LOCK (bin);
3120 /* if a new state change happened after this thread was scheduled, we return
3122 if (data->cookie != GST_ELEMENT_CAST (bin)->state_cookie)
3125 current = GST_STATE (bin);
3126 next = GST_STATE_GET_NEXT (current, pending);
3127 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
3129 GST_STATE_NEXT (bin) = next;
3130 GST_STATE_PENDING (bin) = pending;
3132 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3133 GST_OBJECT_UNLOCK (bin);
3135 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3136 "continue state change %s to %s, final %s",
3137 gst_element_state_get_name (current),
3138 gst_element_state_get_name (next), gst_element_state_get_name (pending));
3140 gst_element_change_state (GST_ELEMENT_CAST (bin), transition);
3142 GST_STATE_UNLOCK (bin);
3143 GST_DEBUG_OBJECT (bin, "state continue done");
3149 GST_OBJECT_UNLOCK (bin);
3150 GST_STATE_UNLOCK (bin);
3151 GST_DEBUG_OBJECT (bin, "state continue aborted due to intervening change");
3156 static GstBusSyncReply
3157 bin_bus_handler (GstBus * bus, GstMessage * message, GstBin * bin)
3159 GstBinClass *bclass;
3161 bclass = GST_BIN_GET_CLASS (bin);
3162 if (bclass->handle_message)
3163 bclass->handle_message (bin, message);
3165 gst_message_unref (message);
3167 return GST_BUS_DROP;
3171 free_bin_continue_data (BinContinueData * data)
3173 g_slice_free (BinContinueData, data);
3177 bin_push_state_continue (GstBin * bin, BinContinueData * data)
3179 GST_DEBUG_OBJECT (bin, "pushing continue on thread pool");
3180 gst_element_call_async (GST_ELEMENT_CAST (bin),
3181 (GstElementCallAsyncFunc) gst_bin_continue_func, data,
3182 (GDestroyNotify) free_bin_continue_data);
3185 /* an element started an async state change, if we were not busy with a state
3186 * change, we perform a lost state.
3187 * This function is called with the OBJECT lock.
3190 bin_handle_async_start (GstBin * bin)
3192 GstState old_state, new_state;
3194 GstMessage *amessage = NULL;
3196 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_FAILURE)
3199 /* get our toplevel state */
3200 toplevel = BIN_IS_TOPLEVEL (bin);
3202 /* prepare an ASYNC_START message, we always post the start message even if we
3203 * are busy with a state change or when we are NO_PREROLL. */
3205 /* non toplevel bin, prepare async-start for the parent */
3206 amessage = gst_message_new_async_start (GST_OBJECT_CAST (bin));
3208 if (bin->polling || GST_STATE_PENDING (bin) != GST_STATE_VOID_PENDING)
3211 /* async starts are ignored when we are NO_PREROLL */
3212 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_NO_PREROLL)
3213 goto was_no_preroll;
3215 old_state = GST_STATE (bin);
3217 /* when we PLAYING we go back to PAUSED, when preroll happens, we go back to
3218 * PLAYING after optionally redistributing the base_time. */
3219 if (old_state > GST_STATE_PAUSED)
3220 new_state = GST_STATE_PAUSED;
3222 new_state = old_state;
3224 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
3225 "lost state of %s, new %s", gst_element_state_get_name (old_state),
3226 gst_element_state_get_name (new_state));
3228 GST_STATE (bin) = new_state;
3229 GST_STATE_NEXT (bin) = new_state;
3230 GST_STATE_PENDING (bin) = new_state;
3231 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3232 GST_OBJECT_UNLOCK (bin);
3235 _priv_gst_element_state_changed (GST_ELEMENT_CAST (bin), new_state, new_state,
3240 /* post our ASYNC_START. */
3241 GST_DEBUG_OBJECT (bin, "posting ASYNC_START to parent");
3242 gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
3244 GST_OBJECT_LOCK (bin);
3250 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "we had an error");
3255 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "state change busy");
3256 GST_OBJECT_UNLOCK (bin);
3261 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "ignoring, we are NO_PREROLL");
3262 GST_OBJECT_UNLOCK (bin);
3267 /* this function is called when there are no more async elements in the bin. We
3268 * post a state changed message and an ASYNC_DONE message.
3269 * This function is called with the OBJECT lock.
3272 bin_handle_async_done (GstBin * bin, GstStateChangeReturn ret,
3273 gboolean flag_pending, GstClockTime running_time)
3275 GstState current, pending, target;
3276 GstStateChangeReturn old_ret;
3277 GstState old_state, old_next;
3278 gboolean toplevel, state_changed = FALSE;
3279 GstMessage *amessage = NULL;
3280 BinContinueData *cont = NULL;
3282 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_FAILURE)
3285 pending = GST_STATE_PENDING (bin);
3290 /* check if there is something to commit */
3291 if (pending == GST_STATE_VOID_PENDING)
3292 goto nothing_pending;
3294 old_ret = GST_STATE_RETURN (bin);
3295 GST_STATE_RETURN (bin) = ret;
3297 /* move to the next target state */
3298 target = GST_STATE_TARGET (bin);
3299 pending = GST_STATE_PENDING (bin) = target;
3301 amessage = gst_message_new_async_done (GST_OBJECT_CAST (bin), running_time);
3303 old_state = GST_STATE (bin);
3304 /* this is the state we should go to next */
3305 old_next = GST_STATE_NEXT (bin);
3307 if (old_next != GST_STATE_PLAYING) {
3308 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3309 "committing state from %s to %s, old pending %s",
3310 gst_element_state_get_name (old_state),
3311 gst_element_state_get_name (old_next),
3312 gst_element_state_get_name (pending));
3314 /* update current state */
3315 current = GST_STATE (bin) = old_next;
3317 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3318 "setting state from %s to %s, pending %s",
3319 gst_element_state_get_name (old_state),
3320 gst_element_state_get_name (old_state),
3321 gst_element_state_get_name (pending));
3322 current = old_state;
3325 /* get our toplevel state */
3326 toplevel = BIN_IS_TOPLEVEL (bin);
3328 /* see if we reached the final state. If we are not toplevel, we also have to
3329 * stop here, the parent will continue our state. */
3330 if ((pending == current) || !toplevel) {
3331 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3332 "completed state change, pending VOID");
3334 /* mark VOID pending */
3335 pending = GST_STATE_VOID_PENDING;
3336 GST_STATE_PENDING (bin) = pending;
3337 GST_STATE_NEXT (bin) = GST_STATE_VOID_PENDING;
3339 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3340 "continue state change, pending %s",
3341 gst_element_state_get_name (pending));
3343 cont = g_slice_new (BinContinueData);
3345 /* cookie to detect concurrent state change */
3346 cont->cookie = GST_ELEMENT_CAST (bin)->state_cookie;
3347 /* pending target state */
3348 cont->pending = pending;
3350 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3351 GST_STATE_NEXT (bin) = GST_STATE_GET_NEXT (old_state, pending);
3354 if (old_next != GST_STATE_PLAYING) {
3355 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC) {
3356 state_changed = TRUE;
3359 GST_OBJECT_UNLOCK (bin);
3361 if (state_changed) {
3362 _priv_gst_element_state_changed (GST_ELEMENT_CAST (bin), old_state,
3366 /* post our combined ASYNC_DONE when all is ASYNC_DONE. */
3367 GST_DEBUG_OBJECT (bin, "posting ASYNC_DONE to parent");
3368 gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
3371 GST_OBJECT_LOCK (bin);
3373 /* toplevel, start continue state */
3374 GST_DEBUG_OBJECT (bin, "all async-done, starting state continue");
3375 bin_push_state_continue (bin, cont);
3377 GST_DEBUG_OBJECT (bin, "state change complete");
3378 GST_STATE_BROADCAST (bin);
3384 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "we had an error");
3389 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "state change busy");
3390 /* if we were busy with a state change and we are requested to flag a
3391 * pending async done, we do so here */
3393 bin->priv->pending_async_done = TRUE;
3398 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin, "nothing pending");
3404 bin_do_eos (GstBin * bin)
3409 GST_OBJECT_LOCK (bin);
3410 /* If all sinks are EOS, we're in PLAYING and no state change is pending
3411 * (or we're doing playing to playing and noone else will trigger posting
3412 * EOS for us) we forward the EOS message to the parent bin or application
3414 eos = GST_STATE (bin) == GST_STATE_PLAYING
3415 && (GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING ||
3416 GST_STATE_PENDING (bin) == GST_STATE_PLAYING)
3417 && bin->priv->posted_playing && is_eos (bin, &seqnum);
3418 GST_OBJECT_UNLOCK (bin);
3421 && g_atomic_int_compare_and_exchange (&bin->priv->posted_eos, FALSE,
3423 GstMessage *tmessage;
3425 /* Clear out any further messages, and reset posted_eos so we can
3426 detect any new EOS that happens (eg, after a seek). Since all
3427 sinks have now posted an EOS, there will be no further EOS events
3428 seen unless there is a new logical EOS */
3429 GST_OBJECT_LOCK (bin);
3430 bin_remove_messages (bin, NULL, GST_MESSAGE_EOS);
3431 bin->priv->posted_eos = FALSE;
3432 GST_OBJECT_UNLOCK (bin);
3434 tmessage = gst_message_new_eos (GST_OBJECT_CAST (bin));
3435 gst_message_set_seqnum (tmessage, seqnum);
3436 GST_DEBUG_OBJECT (bin,
3437 "all sinks posted EOS, posting seqnum #%" G_GUINT32_FORMAT, seqnum);
3438 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3440 GST_LOG_OBJECT (bin, "Not forwarding EOS due to in progress state change, "
3441 " or already posted, or waiting for more EOS");
3446 bin_do_stream_start (GstBin * bin)
3449 gboolean stream_start;
3450 gboolean have_group_id = FALSE;
3453 GST_OBJECT_LOCK (bin);
3454 /* If all sinks are STREAM_START we forward the STREAM_START message
3455 * to the parent bin or application
3457 stream_start = is_stream_start (bin, &seqnum, &have_group_id, &group_id);
3458 GST_OBJECT_UNLOCK (bin);
3461 GstMessage *tmessage;
3463 GST_OBJECT_LOCK (bin);
3464 bin_remove_messages (bin, NULL, GST_MESSAGE_STREAM_START);
3465 GST_OBJECT_UNLOCK (bin);
3467 tmessage = gst_message_new_stream_start (GST_OBJECT_CAST (bin));
3468 gst_message_set_seqnum (tmessage, seqnum);
3470 gst_message_set_group_id (tmessage, group_id);
3472 GST_DEBUG_OBJECT (bin,
3473 "all sinks posted STREAM_START, posting seqnum #%" G_GUINT32_FORMAT,
3475 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3479 /* must be called without the object lock as it posts messages */
3481 bin_do_message_forward (GstBin * bin, GstMessage * message)
3483 if (bin->priv->message_forward) {
3484 GstMessage *forwarded;
3486 GST_DEBUG_OBJECT (bin, "pass %s message upward",
3487 GST_MESSAGE_TYPE_NAME (message));
3489 /* we need to convert these messages to element messages so that our parent
3490 * bin can easily ignore them and so that the application can easily
3491 * distinguish between the internally forwarded and the real messages. */
3492 forwarded = gst_message_new_element (GST_OBJECT_CAST (bin),
3493 gst_structure_new ("GstBinForwarded",
3494 "message", GST_TYPE_MESSAGE, message, NULL));
3496 gst_element_post_message (GST_ELEMENT_CAST (bin), forwarded);
3501 gst_bin_update_context (GstBin * bin, GstContext * context)
3503 GST_OBJECT_LOCK (bin);
3504 gst_bin_update_context_unlocked (bin, context);
3505 GST_OBJECT_UNLOCK (bin);
3509 gst_bin_update_context_unlocked (GstBin * bin, GstContext * context)
3511 const gchar *context_type;
3512 GList *l, **contexts;
3514 contexts = &GST_ELEMENT_CAST (bin)->contexts;
3515 context_type = gst_context_get_context_type (context);
3517 GST_DEBUG_OBJECT (bin, "set context %p %" GST_PTR_FORMAT, context,
3518 gst_context_get_structure (context));
3519 for (l = *contexts; l; l = l->next) {
3520 GstContext *tmp = l->data;
3521 const gchar *tmp_type = gst_context_get_context_type (tmp);
3523 /* Always store newest context but never replace
3524 * a persistent one by a non-persistent one */
3525 if (strcmp (context_type, tmp_type) == 0 &&
3526 (gst_context_is_persistent (context) ||
3527 !gst_context_is_persistent (tmp))) {
3528 gst_context_replace ((GstContext **) & l->data, context);
3532 /* Not found? Add */
3534 *contexts = g_list_prepend (*contexts, gst_context_ref (context));
3538 /* handle child messages:
3540 * This method is called synchronously when a child posts a message on
3543 * GST_MESSAGE_EOS: This message is only posted by sinks
3544 * in the PLAYING state. If all sinks posted the EOS message, post
3547 * GST_MESSAGE_STATE_DIRTY: Deprecated
3549 * GST_MESSAGE_SEGMENT_START: just collect, never forward upwards. If an
3550 * element posts segment_start twice, only the last message is kept.
3552 * GST_MESSAGE_SEGMENT_DONE: replace SEGMENT_START message from same poster
3553 * with the segment_done message. If there are no more segment_start
3554 * messages, post segment_done message upwards.
3556 * GST_MESSAGE_DURATION_CHANGED: clear any cached durations.
3557 * Whenever someone performs a duration query on the bin, we store the
3558 * result so we can answer it quicker the next time. Any element that
3559 * changes its duration marks our cached values invalid.
3560 * This message is also posted upwards. This is currently disabled
3561 * because too many elements don't post DURATION_CHANGED messages when
3562 * the duration changes.
3564 * GST_MESSAGE_CLOCK_LOST: This message is posted by an element when it
3565 * can no longer provide a clock. The default bin behaviour is to
3566 * check if the lost clock was the one provided by the bin. If so and
3567 * we are currently in the PLAYING state, we forward the message to
3569 * This message is also generated when we remove a clock provider from
3570 * a bin. If this message is received by the application, it should
3571 * PAUSE the pipeline and set it back to PLAYING to force a new clock
3572 * and a new base_time distribution.
3574 * GST_MESSAGE_CLOCK_PROVIDE: This message is generated when an element
3575 * can provide a clock. This mostly happens when we add a new clock
3576 * provider to the bin. The default behaviour of the bin is to mark the
3577 * currently selected clock as dirty, which will perform a clock
3578 * recalculation the next time we are asked to provide a clock.
3579 * This message is never sent to the application but is forwarded to
3582 * GST_MESSAGE_ASYNC_START: Create an internal ELEMENT message that stores
3583 * the state of the element and the fact that the element will need a
3584 * new base_time. This message is not forwarded to the application.
3586 * GST_MESSAGE_ASYNC_DONE: Find the internal ELEMENT message we kept for the
3587 * element when it posted ASYNC_START. If all elements are done, post a
3588 * ASYNC_DONE message to the parent.
3590 * OTHER: post upwards.
3593 gst_bin_handle_message_func (GstBin * bin, GstMessage * message)
3596 GstMessageType type;
3597 GstMessage *tmessage;
3600 src = GST_MESSAGE_SRC (message);
3601 type = GST_MESSAGE_TYPE (message);
3603 GST_DEBUG_OBJECT (bin, "[msg %p] handling child %s message of type %s",
3604 message, src ? GST_ELEMENT_NAME (src) : "(NULL)",
3605 GST_MESSAGE_TYPE_NAME (message));
3608 case GST_MESSAGE_ERROR:
3610 GST_OBJECT_LOCK (bin);
3612 GST_DEBUG_OBJECT (bin, "got ERROR message, unlocking state change");
3613 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_FAILURE;
3614 GST_STATE_BROADCAST (bin);
3615 GST_OBJECT_UNLOCK (bin);
3619 case GST_MESSAGE_EOS:
3622 /* collect all eos messages from the children */
3623 bin_do_message_forward (bin, message);
3624 GST_OBJECT_LOCK (bin);
3625 /* ref message for future use */
3626 bin_replace_message (bin, message, GST_MESSAGE_EOS);
3627 GST_OBJECT_UNLOCK (bin);
3632 case GST_MESSAGE_STREAM_START:
3635 /* collect all stream_start messages from the children */
3636 GST_OBJECT_LOCK (bin);
3637 /* ref message for future use */
3638 bin_replace_message (bin, message, GST_MESSAGE_STREAM_START);
3639 GST_OBJECT_UNLOCK (bin);
3641 bin_do_stream_start (bin);
3644 case GST_MESSAGE_STATE_DIRTY:
3646 GST_WARNING_OBJECT (bin, "received deprecated STATE_DIRTY message");
3649 gst_message_unref (message);
3652 case GST_MESSAGE_SEGMENT_START:{
3653 gboolean post = FALSE;
3657 gst_message_parse_segment_start (message, &format, &position);
3658 seqnum = gst_message_get_seqnum (message);
3660 bin_do_message_forward (bin, message);
3662 GST_OBJECT_LOCK (bin);
3663 /* if this is the first segment-start, post to parent but not to the
3665 if (!find_message (bin, NULL, GST_MESSAGE_SEGMENT_START) &&
3666 (GST_OBJECT_PARENT (bin) != NULL)) {
3669 /* replace any previous segment_start message from this source
3670 * with the new segment start message */
3671 bin_replace_message (bin, message, GST_MESSAGE_SEGMENT_START);
3672 GST_OBJECT_UNLOCK (bin);
3674 tmessage = gst_message_new_segment_start (GST_OBJECT_CAST (bin),
3676 gst_message_set_seqnum (tmessage, seqnum);
3678 /* post segment start with initial format and position. */
3679 GST_DEBUG_OBJECT (bin, "posting SEGMENT_START (%u) bus message: %p",
3681 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3685 case GST_MESSAGE_SEGMENT_DONE:
3687 gboolean post = FALSE;
3691 gst_message_parse_segment_done (message, &format, &position);
3692 seqnum = gst_message_get_seqnum (message);
3694 bin_do_message_forward (bin, message);
3696 GST_OBJECT_LOCK (bin);
3697 bin_replace_message (bin, message, GST_MESSAGE_SEGMENT_START);
3698 /* if there are no more segment_start messages, everybody posted
3699 * a segment_done and we can post one on the bus. */
3701 /* we don't care who still has a pending segment start */
3702 if (!find_message (bin, NULL, GST_MESSAGE_SEGMENT_START)) {
3705 /* remove all old segment_done messages */
3706 bin_remove_messages (bin, NULL, GST_MESSAGE_SEGMENT_DONE);
3708 GST_OBJECT_UNLOCK (bin);
3710 tmessage = gst_message_new_segment_done (GST_OBJECT_CAST (bin),
3712 gst_message_set_seqnum (tmessage, seqnum);
3714 /* post segment done with latest format and position. */
3715 GST_DEBUG_OBJECT (bin, "posting SEGMENT_DONE (%u) bus message: %p",
3717 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3721 case GST_MESSAGE_DURATION_CHANGED:
3723 /* FIXME: remove all cached durations, next time somebody asks
3724 * for duration, we will recalculate. */
3726 GST_OBJECT_LOCK (bin);
3727 bin_remove_messages (bin, NULL, GST_MESSAGE_DURATION_CHANGED);
3728 GST_OBJECT_UNLOCK (bin);
3732 case GST_MESSAGE_CLOCK_LOST:
3734 GstClock **provided_clock_p;
3735 GstElement **clock_provider_p;
3736 gboolean playing, toplevel, provided, forward;
3739 gst_message_parse_clock_lost (message, &clock);
3741 GST_OBJECT_LOCK (bin);
3742 bin->clock_dirty = TRUE;
3743 /* if we lost the clock that we provided, post to parent but
3744 * only if we are not a top-level bin or PLAYING.
3745 * The reason for this is that applications should be able
3746 * to PAUSE/PLAY if they receive this message without worrying
3747 * about the state of the pipeline. */
3748 provided = (clock == bin->provided_clock);
3749 playing = (GST_STATE (bin) == GST_STATE_PLAYING);
3750 toplevel = GST_OBJECT_PARENT (bin) == NULL;
3751 forward = provided && (playing || !toplevel);
3753 GST_DEBUG_OBJECT (bin,
3754 "Lost clock %" GST_PTR_FORMAT " provided by %" GST_PTR_FORMAT,
3755 bin->provided_clock, bin->clock_provider);
3756 provided_clock_p = &bin->provided_clock;
3757 clock_provider_p = &bin->clock_provider;
3758 gst_object_replace ((GstObject **) provided_clock_p, NULL);
3759 gst_object_replace ((GstObject **) clock_provider_p, NULL);
3761 GST_DEBUG_OBJECT (bin, "provided %d, playing %d, forward %d",
3762 provided, playing, forward);
3763 GST_OBJECT_UNLOCK (bin);
3769 gst_message_unref (message);
3772 case GST_MESSAGE_CLOCK_PROVIDE:
3776 GST_OBJECT_LOCK (bin);
3777 bin->clock_dirty = TRUE;
3778 /* a new clock is available, post to parent but not
3779 * to the application */
3780 forward = GST_OBJECT_PARENT (bin) != NULL;
3781 GST_OBJECT_UNLOCK (bin);
3787 gst_message_unref (message);
3790 case GST_MESSAGE_ASYNC_START:
3794 GST_DEBUG_OBJECT (bin, "ASYNC_START message %p, %s", message,
3795 src ? GST_OBJECT_NAME (src) : "(NULL)");
3797 bin_do_message_forward (bin, message);
3799 GST_OBJECT_LOCK (bin);
3800 /* we ignore the message if we are going to <= READY */
3801 if ((target = GST_STATE_TARGET (bin)) <= GST_STATE_READY)
3802 goto ignore_start_message;
3804 /* takes ownership of the message */
3805 bin_replace_message (bin, message, GST_MESSAGE_ASYNC_START);
3807 bin_handle_async_start (bin);
3808 GST_OBJECT_UNLOCK (bin);
3811 ignore_start_message:
3813 GST_DEBUG_OBJECT (bin, "ignoring message, target %s",
3814 gst_element_state_get_name (target));
3815 GST_OBJECT_UNLOCK (bin);
3816 gst_message_unref (message);
3820 case GST_MESSAGE_ASYNC_DONE:
3822 GstClockTime running_time;
3825 GST_DEBUG_OBJECT (bin, "ASYNC_DONE message %p, %s", message,
3826 src ? GST_OBJECT_NAME (src) : "(NULL)");
3828 gst_message_parse_async_done (message, &running_time);
3830 bin_do_message_forward (bin, message);
3832 GST_OBJECT_LOCK (bin);
3833 /* ignore messages if we are shutting down */
3834 if ((target = GST_STATE_TARGET (bin)) <= GST_STATE_READY)
3835 goto ignore_done_message;
3837 bin_replace_message (bin, message, GST_MESSAGE_ASYNC_START);
3838 /* if there are no more ASYNC_START messages, everybody posted
3839 * a ASYNC_DONE and we can post one on the bus. When checking, we
3840 * don't care who still has a pending ASYNC_START */
3841 if (!find_message (bin, NULL, GST_MESSAGE_ASYNC_START)) {
3842 /* nothing found, remove all old ASYNC_DONE messages */
3843 bin_remove_messages (bin, NULL, GST_MESSAGE_ASYNC_DONE);
3845 GST_DEBUG_OBJECT (bin, "async elements commited");
3846 /* when we get an async done message when a state change was busy, we
3847 * need to set the pending_done flag so that at the end of the state
3848 * change we can see if we need to verify pending async elements, hence
3849 * the TRUE argument here. */
3850 bin_handle_async_done (bin, GST_STATE_CHANGE_SUCCESS, TRUE,
3853 GST_DEBUG_OBJECT (bin, "there are more async elements pending");
3855 GST_OBJECT_UNLOCK (bin);
3858 ignore_done_message:
3860 GST_DEBUG_OBJECT (bin, "ignoring message, target %s",
3861 gst_element_state_get_name (target));
3862 GST_OBJECT_UNLOCK (bin);
3863 gst_message_unref (message);
3867 case GST_MESSAGE_STRUCTURE_CHANGE:
3871 gst_message_parse_structure_change (message, NULL, NULL, &busy);
3873 GST_OBJECT_LOCK (bin);
3875 /* while the pad is busy, avoid following it when doing state changes.
3876 * Don't update the cookie yet, we will do that after the structure
3877 * change finished and we are ready to inspect the new updated
3879 bin_replace_message (bin, message, GST_MESSAGE_STRUCTURE_CHANGE);
3882 /* a pad link/unlink ended, signal the state change iterator that we
3883 * need to resync by updating the structure_cookie. */
3884 bin_remove_messages (bin, GST_MESSAGE_SRC (message),
3885 GST_MESSAGE_STRUCTURE_CHANGE);
3886 if (!GST_BIN_IS_NO_RESYNC (bin))
3887 bin->priv->structure_cookie++;
3889 GST_OBJECT_UNLOCK (bin);
3892 gst_message_unref (message);
3896 case GST_MESSAGE_NEED_CONTEXT:{
3897 const gchar *context_type;
3898 GList *l, *contexts;
3900 gst_message_parse_context_type (message, &context_type);
3901 GST_OBJECT_LOCK (bin);
3902 contexts = GST_ELEMENT_CAST (bin)->contexts;
3903 GST_LOG_OBJECT (bin, "got need-context message type: %s", context_type);
3904 for (l = contexts; l; l = l->next) {
3905 GstContext *tmp = l->data;
3906 const gchar *tmp_type = gst_context_get_context_type (tmp);
3908 if (strcmp (context_type, tmp_type) == 0) {
3909 gst_element_set_context (GST_ELEMENT (src), l->data);
3913 GST_OBJECT_UNLOCK (bin);
3915 /* Forward if we couldn't answer the message */
3919 gst_message_unref (message);
3924 case GST_MESSAGE_HAVE_CONTEXT:{
3925 GstContext *context;
3927 gst_message_parse_have_context (message, &context);
3928 gst_bin_update_context (bin, context);
3929 gst_context_unref (context);
3941 /* Send all other messages upward */
3942 GST_DEBUG_OBJECT (bin, "posting message upward");
3943 gst_element_post_message (GST_ELEMENT_CAST (bin), message);
3948 /* generic struct passed to all query fold methods */
3957 typedef void (*QueryInitFunction) (GstBin * bin, QueryFold * fold);
3958 typedef void (*QueryDoneFunction) (GstBin * bin, QueryFold * fold);
3960 /* for duration/position we collect all durations/positions and take
3961 * the MAX of all valid results */
3963 bin_query_min_max_init (GstBin * bin, QueryFold * fold)
3971 bin_query_duration_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
3973 gboolean res = FALSE;
3974 GstObject *item = g_value_get_object (vitem);
3975 if (GST_IS_PAD (item))
3976 res = gst_pad_query (GST_PAD (item), fold->query);
3978 res = gst_element_query (GST_ELEMENT (item), fold->query);
3983 g_value_set_boolean (ret, TRUE);
3985 gst_query_parse_duration (fold->query, NULL, &duration);
3987 GST_DEBUG_OBJECT (item, "got duration %" G_GINT64_FORMAT, duration);
3989 if (duration == -1) {
3990 /* duration query succeeded, but duration is unknown */
3995 if (duration > fold->max)
3996 fold->max = duration;
4003 bin_query_duration_done (GstBin * bin, QueryFold * fold)
4007 gst_query_parse_duration (fold->query, &format, NULL);
4008 /* store max in query result */
4009 gst_query_set_duration (fold->query, format, fold->max);
4011 GST_DEBUG_OBJECT (bin, "max duration %" G_GINT64_FORMAT, fold->max);
4013 /* FIXME: re-implement duration caching */
4016 GST_OBJECT_LOCK (bin);
4017 bin->messages = g_list_prepend (bin->messages,
4018 gst_message_new_duration (GST_OBJECT_CAST (bin), format, fold->max));
4019 GST_OBJECT_UNLOCK (bin);
4024 bin_query_position_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4026 gboolean res = FALSE;
4027 GstObject *item = g_value_get_object (vitem);
4028 if (GST_IS_PAD (item))
4029 res = gst_pad_query (GST_PAD (item), fold->query);
4031 res = gst_element_query (GST_ELEMENT (item), fold->query);
4036 g_value_set_boolean (ret, TRUE);
4038 gst_query_parse_position (fold->query, NULL, &position);
4040 GST_DEBUG_OBJECT (item, "got position %" G_GINT64_FORMAT, position);
4042 if (position > fold->max)
4043 fold->max = position;
4050 bin_query_position_done (GstBin * bin, QueryFold * fold)
4054 gst_query_parse_position (fold->query, &format, NULL);
4055 /* store max in query result */
4056 gst_query_set_position (fold->query, format, fold->max);
4058 GST_DEBUG_OBJECT (bin, "max position %" G_GINT64_FORMAT, fold->max);
4062 bin_query_latency_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4064 gboolean res = FALSE;
4065 GstObject *item = g_value_get_object (vitem);
4066 if (GST_IS_PAD (item))
4067 res = gst_pad_query (GST_PAD (item), fold->query);
4069 res = gst_element_query (GST_ELEMENT (item), fold->query);
4071 GstClockTime min, max;
4074 gst_query_parse_latency (fold->query, &live, &min, &max);
4076 GST_DEBUG_OBJECT (item,
4077 "got latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
4078 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
4080 /* for the combined latency we collect the MAX of all min latencies and
4081 * the MIN of all max latencies */
4083 if (min > fold->min)
4085 if (fold->max == -1)
4087 else if (max < fold->max)
4093 g_value_set_boolean (ret, FALSE);
4094 GST_DEBUG_OBJECT (item, "failed query");
4101 bin_query_latency_done (GstBin * bin, QueryFold * fold)
4103 /* store max in query result */
4104 gst_query_set_latency (fold->query, fold->live, fold->min, fold->max);
4106 GST_DEBUG_OBJECT (bin,
4107 "latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
4108 ", live %d", GST_TIME_ARGS (fold->min), GST_TIME_ARGS (fold->max),
4112 /* generic fold, return first valid result */
4114 bin_query_generic_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4116 gboolean res = FALSE;
4117 GstObject *item = g_value_get_object (vitem);
4118 if (GST_IS_PAD (item))
4119 res = gst_pad_query (GST_PAD (item), fold->query);
4121 res = gst_element_query (GST_ELEMENT (item), fold->query);
4123 g_value_set_boolean (ret, TRUE);
4124 GST_DEBUG_OBJECT (item, "answered query %p", fold->query);
4127 /* and stop as soon as we have a valid result */
4131 /* Perform a query iteration for the given bin. The query is stored in
4132 * QueryFold and iter should be either a GstPad iterator or a
4133 * GstElement iterator. */
4135 bin_iterate_fold (GstBin * bin, GstIterator * iter, QueryInitFunction fold_init,
4136 QueryDoneFunction fold_done, GstIteratorFoldFunction fold_func,
4137 QueryFold * fold_data, gboolean default_return)
4139 gboolean res = default_return;
4141 /* set the result of the query to FALSE initially */
4142 g_value_init (&ret, G_TYPE_BOOLEAN);
4143 g_value_set_boolean (&ret, res);
4146 GstIteratorResult ires;
4148 ires = gst_iterator_fold (iter, fold_func, &ret, fold_data);
4151 case GST_ITERATOR_RESYNC:
4152 gst_iterator_resync (iter);
4154 fold_init (bin, fold_data);
4155 g_value_set_boolean (&ret, res);
4157 case GST_ITERATOR_OK:
4158 case GST_ITERATOR_DONE:
4159 res = g_value_get_boolean (&ret);
4160 if (fold_done != NULL && res)
4161 fold_done (bin, fold_data);
4173 gst_bin_query (GstElement * element, GstQuery * query)
4175 GstBin *bin = GST_BIN_CAST (element);
4177 gboolean default_return = FALSE;
4178 gboolean res = FALSE;
4179 gboolean src_pads_query_result = FALSE;
4180 GstIteratorFoldFunction fold_func;
4181 QueryInitFunction fold_init = NULL;
4182 QueryDoneFunction fold_done = NULL;
4183 QueryFold fold_data;
4185 switch (GST_QUERY_TYPE (query)) {
4186 case GST_QUERY_DURATION:
4188 /* FIXME: implement duration caching in GstBin again */
4193 gst_query_parse_duration (query, &qformat, NULL);
4195 /* find cached duration query */
4196 GST_OBJECT_LOCK (bin);
4197 for (cached = bin->messages; cached; cached = g_list_next (cached)) {
4198 GstMessage *message = (GstMessage *) cached->data;
4200 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_DURATION_CHANGED &&
4201 GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (bin)) {
4205 gst_message_parse_duration (message, &format, &duration);
4207 /* if cached same format, copy duration in query result */
4208 if (format == qformat) {
4209 GST_DEBUG_OBJECT (bin, "return cached duration %" G_GINT64_FORMAT,
4211 GST_OBJECT_UNLOCK (bin);
4213 gst_query_set_duration (query, qformat, duration);
4219 GST_OBJECT_UNLOCK (bin);
4221 GST_FIXME ("implement duration caching in GstBin again");
4223 /* no cached value found, iterate and collect durations */
4224 fold_func = (GstIteratorFoldFunction) bin_query_duration_fold;
4225 fold_init = bin_query_min_max_init;
4226 fold_done = bin_query_duration_done;
4229 case GST_QUERY_POSITION:
4231 fold_func = (GstIteratorFoldFunction) bin_query_position_fold;
4232 fold_init = bin_query_min_max_init;
4233 fold_done = bin_query_position_done;
4236 case GST_QUERY_LATENCY:
4238 fold_func = (GstIteratorFoldFunction) bin_query_latency_fold;
4239 fold_init = bin_query_min_max_init;
4240 fold_done = bin_query_latency_done;
4241 default_return = TRUE;
4245 fold_func = (GstIteratorFoldFunction) bin_query_generic_fold;
4249 fold_data.query = query;
4251 iter = gst_bin_iterate_sinks (bin);
4252 GST_DEBUG_OBJECT (bin, "Sending query %p (type %s) to sink children",
4253 query, GST_QUERY_TYPE_NAME (query));
4256 fold_init (bin, &fold_data);
4259 bin_iterate_fold (bin, iter, fold_init, fold_done, fold_func, &fold_data,
4261 gst_iterator_free (iter);
4264 /* Query the source pads of the element */
4265 iter = gst_element_iterate_src_pads (element);
4266 src_pads_query_result =
4267 bin_iterate_fold (bin, iter, fold_init, fold_done, fold_func,
4268 &fold_data, default_return);
4269 gst_iterator_free (iter);
4271 if (src_pads_query_result)
4275 GST_DEBUG_OBJECT (bin, "query %p result %d", query, res);
4281 set_context (const GValue * item, gpointer user_data)
4283 GstElement *element = g_value_get_object (item);
4285 gst_element_set_context (element, user_data);
4289 gst_bin_set_context (GstElement * element, GstContext * context)
4292 GstIterator *children;
4294 g_return_if_fail (GST_IS_BIN (element));
4296 bin = GST_BIN (element);
4298 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
4300 children = gst_bin_iterate_elements (bin);
4301 while (gst_iterator_foreach (children, set_context,
4302 context) == GST_ITERATOR_RESYNC)
4303 gst_iterator_resync (children);
4304 gst_iterator_free (children);
4308 compare_name (const GValue * velement, const gchar * name)
4311 GstElement *element = g_value_get_object (velement);
4313 GST_OBJECT_LOCK (element);
4314 eq = strcmp (GST_ELEMENT_NAME (element), name);
4315 GST_OBJECT_UNLOCK (element);
4321 * gst_bin_get_by_name:
4323 * @name: the element name to search for
4325 * Gets the element with the given name from a bin. This
4326 * function recurses into child bins.
4328 * Returns %NULL if no element with the given name is found in the bin.
4330 * MT safe. Caller owns returned reference.
4332 * Returns: (transfer full) (nullable): the #GstElement with the given
4336 gst_bin_get_by_name (GstBin * bin, const gchar * name)
4338 GstIterator *children;
4339 GValue result = { 0, };
4340 GstElement *element;
4343 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4345 GST_CAT_INFO (GST_CAT_PARENTAGE, "[%s]: looking up child element %s",
4346 GST_ELEMENT_NAME (bin), name);
4348 children = gst_bin_iterate_recurse (bin);
4349 found = gst_iterator_find_custom (children,
4350 (GCompareFunc) compare_name, &result, (gpointer) name);
4351 gst_iterator_free (children);
4354 element = g_value_dup_object (&result);
4355 g_value_unset (&result);
4364 * gst_bin_get_by_name_recurse_up:
4366 * @name: the element name to search for
4368 * Gets the element with the given name from this bin. If the
4369 * element is not found, a recursion is performed on the parent bin.
4372 * - no element with the given name is found in the bin
4374 * MT safe. Caller owns returned reference.
4376 * Returns: (transfer full) (nullable): the #GstElement with the given
4380 gst_bin_get_by_name_recurse_up (GstBin * bin, const gchar * name)
4384 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4385 g_return_val_if_fail (name != NULL, NULL);
4387 result = gst_bin_get_by_name (bin, name);
4392 parent = gst_object_get_parent (GST_OBJECT_CAST (bin));
4394 if (GST_IS_BIN (parent)) {
4395 result = gst_bin_get_by_name_recurse_up (GST_BIN_CAST (parent), name);
4397 gst_object_unref (parent);
4405 compare_interface (const GValue * velement, GValue * interface)
4407 GstElement *element = g_value_get_object (velement);
4408 GType interface_type = (GType) g_value_get_pointer (interface);
4411 if (G_TYPE_CHECK_INSTANCE_TYPE (element, interface_type)) {
4420 * gst_bin_get_by_interface:
4422 * @iface: the #GType of an interface
4424 * Looks for an element inside the bin that implements the given
4425 * interface. If such an element is found, it returns the element.
4426 * You can cast this element to the given interface afterwards. If you want
4427 * all elements that implement the interface, use
4428 * gst_bin_iterate_all_by_interface(). This function recurses into child bins.
4430 * MT safe. Caller owns returned reference.
4432 * Returns: (transfer full): A #GstElement inside the bin implementing the interface
4435 gst_bin_get_by_interface (GstBin * bin, GType iface)
4437 GstIterator *children;
4438 GValue result = { 0, };
4439 GstElement *element;
4441 GValue viface = { 0, };
4443 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4444 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface), NULL);
4446 g_value_init (&viface, G_TYPE_POINTER);
4447 g_value_set_pointer (&viface, (gpointer) iface);
4449 children = gst_bin_iterate_recurse (bin);
4450 found = gst_iterator_find_custom (children, (GCompareFunc) compare_interface,
4452 gst_iterator_free (children);
4455 element = g_value_dup_object (&result);
4456 g_value_unset (&result);
4460 g_value_unset (&viface);
4466 * gst_bin_iterate_all_by_interface:
4468 * @iface: the #GType of an interface
4470 * Looks for all elements inside the bin that implements the given
4471 * interface. You can safely cast all returned elements to the given interface.
4472 * The function recurses inside child bins. The iterator will yield a series
4473 * of #GstElement that should be unreffed after use.
4475 * MT safe. Caller owns returned value.
4477 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
4478 * for all elements in the bin implementing the given interface,
4482 gst_bin_iterate_all_by_interface (GstBin * bin, GType iface)
4484 GstIterator *children;
4485 GstIterator *result;
4486 GValue viface = { 0, };
4488 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4489 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface), NULL);
4491 g_value_init (&viface, G_TYPE_POINTER);
4492 g_value_set_pointer (&viface, (gpointer) iface);
4494 children = gst_bin_iterate_recurse (bin);
4495 result = gst_iterator_filter (children, (GCompareFunc) compare_interface,
4498 g_value_unset (&viface);