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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, 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</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.
157 * Last reviewed on 2006-04-28 (0.10.6)
160 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
161 * with newer GLib versions (>= 2.31.0) */
162 #define GLIB_DISABLE_DEPRECATION_WARNINGS
163 #include "gst_private.h"
165 #include "gstevent.h"
167 #include "gstmarshal.h"
169 #include "gsterror.h"
171 #include "gstindex.h"
172 #include "gstindexfactory.h"
173 #include "gstutils.h"
174 #include "gstchildproxy.h"
176 /* latency is by default enabled now.
177 * live-preroll and no-live-preroll in the environment var GST_COMPAT
178 * to enables or disable it respectively.
180 static gboolean enable_latency = TRUE;
182 GST_DEBUG_CATEGORY_STATIC (bin_debug);
183 #define GST_CAT_DEFAULT bin_debug
185 /* a bin is toplevel if it has no parent or when it is configured to behave like
187 #define BIN_IS_TOPLEVEL(bin) ((GST_OBJECT_PARENT (bin) == NULL) || bin->priv->asynchandling)
189 #define GST_BIN_GET_PRIVATE(obj) \
190 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BIN, GstBinPrivate))
192 struct _GstBinPrivate
194 gboolean asynchandling;
195 /* if we get an ASYNC_DONE message from ourselves, this means that the
196 * subclass will simulate ASYNC behaviour without having ASYNC children. When
197 * such an ASYNC_DONE message is posted while we are doing a state change, we
198 * have to process the message after finishing the state change even when no
199 * child returned GST_STATE_CHANGE_ASYNC. */
200 gboolean pending_async_done;
202 guint32 structure_cookie;
206 /* forward messages from our children */
207 gboolean message_forward;
219 static void gst_bin_dispose (GObject * object);
221 static void gst_bin_set_property (GObject * object, guint prop_id,
222 const GValue * value, GParamSpec * pspec);
223 static void gst_bin_get_property (GObject * object, guint prop_id,
224 GValue * value, GParamSpec * pspec);
226 static GstStateChangeReturn gst_bin_change_state_func (GstElement * element,
227 GstStateChange transition);
228 static void gst_bin_state_changed (GstElement * element, GstState oldstate,
229 GstState newstate, GstState pending);
230 static GstStateChangeReturn gst_bin_get_state_func (GstElement * element,
231 GstState * state, GstState * pending, GstClockTime timeout);
232 static void bin_handle_async_done (GstBin * bin, GstStateChangeReturn ret,
233 gboolean flag_pending, gboolean reset_time);
234 static void bin_handle_async_start (GstBin * bin);
235 static void bin_push_state_continue (BinContinueData * data);
236 static void bin_do_eos (GstBin * bin);
238 static gboolean gst_bin_add_func (GstBin * bin, GstElement * element);
239 static gboolean gst_bin_remove_func (GstBin * bin, GstElement * element);
241 static void gst_bin_set_index_func (GstElement * element, GstIndex * index);
242 static GstIndex *gst_bin_get_index_func (GstElement * element);
244 static GstClock *gst_bin_provide_clock_func (GstElement * element);
245 static gboolean gst_bin_set_clock_func (GstElement * element, GstClock * clock);
247 static void gst_bin_handle_message_func (GstBin * bin, GstMessage * message);
248 static gboolean gst_bin_send_event (GstElement * element, GstEvent * event);
249 static GstBusSyncReply bin_bus_handler (GstBus * bus,
250 GstMessage * message, GstBin * bin);
251 static gboolean gst_bin_query (GstElement * element, GstQuery * query);
253 static gboolean gst_bin_do_latency_func (GstBin * bin);
255 static void bin_remove_messages (GstBin * bin, GstObject * src,
256 GstMessageType types);
257 static void gst_bin_continue_func (BinContinueData * data);
258 static gint bin_element_is_sink (GstElement * child, GstBin * bin);
259 static gint bin_element_is_src (GstElement * child, GstBin * bin);
261 static GstIterator *gst_bin_sort_iterator_new (GstBin * bin);
263 /* Bin signals and properties */
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 const gchar *compat; \
290 static const GInterfaceInfo iface_info = { \
291 gst_bin_child_proxy_init, \
295 g_type_add_interface_static (g_define_type_id, GST_TYPE_CHILD_PROXY, &iface_info); \
297 GST_DEBUG_CATEGORY_INIT (bin_debug, "bin", GST_DEBUG_BOLD, \
298 "debugging info for the 'bin' container element"); \
300 /* compatibility stuff */ \
301 compat = g_getenv ("GST_COMPAT"); \
302 if (compat != NULL) { \
303 if (strstr (compat, "no-live-preroll")) \
304 enable_latency = FALSE; \
305 else if (strstr (compat, "live-preroll")) \
306 enable_latency = TRUE; \
310 #define gst_bin_parent_class parent_class
311 G_DEFINE_TYPE_WITH_CODE (GstBin, gst_bin, GST_TYPE_ELEMENT, _do_init);
314 gst_bin_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
320 bin = GST_BIN_CAST (child_proxy);
322 GST_OBJECT_LOCK (bin);
323 if ((res = g_list_nth_data (bin->children, index)))
324 gst_object_ref (res);
325 GST_OBJECT_UNLOCK (bin);
331 gst_bin_child_proxy_get_children_count (GstChildProxy * child_proxy)
336 bin = GST_BIN_CAST (child_proxy);
338 GST_OBJECT_LOCK (bin);
339 num = bin->numchildren;
340 GST_OBJECT_UNLOCK (bin);
346 gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data)
348 GstChildProxyInterface *iface = g_iface;
350 iface->get_children_count = gst_bin_child_proxy_get_children_count;
351 iface->get_child_by_index = gst_bin_child_proxy_get_child_by_index;
355 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
356 GValue * return_accu, const GValue * handler_return, gpointer dummy)
360 myboolean = g_value_get_boolean (handler_return);
361 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
362 g_value_set_boolean (return_accu, myboolean);
364 GST_DEBUG ("invocation %d, %d", ihint->run_type, myboolean);
371 gst_bin_class_init (GstBinClass * klass)
373 GObjectClass *gobject_class;
374 GstElementClass *gstelement_class;
377 gobject_class = (GObjectClass *) klass;
378 gstelement_class = (GstElementClass *) klass;
380 g_type_class_add_private (klass, sizeof (GstBinPrivate));
382 gobject_class->set_property = gst_bin_set_property;
383 gobject_class->get_property = gst_bin_get_property;
386 * GstBin:async-handling
388 * If set to #TRUE, the bin will handle asynchronous state changes.
389 * This should be used only if the bin subclass is modifying the state
390 * of its children on its own.
394 g_object_class_install_property (gobject_class, PROP_ASYNC_HANDLING,
395 g_param_spec_boolean ("async-handling", "Async Handling",
396 "The bin will handle Asynchronous state changes",
397 DEFAULT_ASYNC_HANDLING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
400 * GstBin::element-added:
402 * @element: the #GstElement that was added to the bin
404 * Will be emitted after the element was added to the bin.
406 gst_bin_signals[ELEMENT_ADDED] =
407 g_signal_new ("element-added", G_TYPE_FROM_CLASS (klass),
408 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_added), NULL,
409 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
411 * GstBin::element-removed:
413 * @element: the #GstElement that was removed from the bin
415 * Will be emitted after the element was removed from the bin.
417 gst_bin_signals[ELEMENT_REMOVED] =
418 g_signal_new ("element-removed", G_TYPE_FROM_CLASS (klass),
419 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_removed), NULL,
420 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
422 * GstBin::do-latency:
425 * Will be emitted when the bin needs to perform latency calculations. This
426 * signal is only emited for toplevel bins or when async-handling is
429 * Only one signal handler is invoked. If no signals are connected, the
430 * default handler is invoked, which will query and distribute the lowest
431 * possible latency to all sinks.
433 * Connect to this signal if the default latency calculations are not
434 * sufficient, like when you need different latencies for different sinks in
439 gst_bin_signals[DO_LATENCY] =
440 g_signal_new ("do-latency", G_TYPE_FROM_CLASS (klass),
441 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstBinClass, do_latency),
442 _gst_boolean_accumulator, NULL, gst_marshal_BOOLEAN__VOID,
443 G_TYPE_BOOLEAN, 0, G_TYPE_NONE);
446 * GstBin:message-forward
448 * Forward all children messages, even those that would normally be filtered by
449 * the bin. This can be interesting when one wants to be notified of the EOS
450 * state of individual elements, for example.
452 * The messages are converted to an ELEMENT message with the bin as the
453 * source. The structure of the message is named 'GstBinForwarded' and contains
454 * a field named 'message' of type GST_TYPE_MESSAGE that contains the original
459 g_object_class_install_property (gobject_class, PROP_MESSAGE_FORWARD,
460 g_param_spec_boolean ("message-forward", "Message Forward",
461 "Forwards all children messages",
462 DEFAULT_MESSAGE_FORWARD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
464 gobject_class->dispose = gst_bin_dispose;
466 gst_element_class_set_metadata (gstelement_class, "Generic bin",
468 "Simple container object",
469 "Erik Walthinsen <omega@cse.ogi.edu>,"
470 "Wim Taymans <wim.taymans@gmail.com>");
472 gstelement_class->change_state =
473 GST_DEBUG_FUNCPTR (gst_bin_change_state_func);
474 gstelement_class->state_changed = GST_DEBUG_FUNCPTR (gst_bin_state_changed);
475 gstelement_class->get_state = GST_DEBUG_FUNCPTR (gst_bin_get_state_func);
476 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_bin_get_index_func);
477 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_bin_set_index_func);
478 gstelement_class->provide_clock =
479 GST_DEBUG_FUNCPTR (gst_bin_provide_clock_func);
480 gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_bin_set_clock_func);
482 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_bin_send_event);
483 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_bin_query);
485 klass->add_element = GST_DEBUG_FUNCPTR (gst_bin_add_func);
486 klass->remove_element = GST_DEBUG_FUNCPTR (gst_bin_remove_func);
487 klass->handle_message = GST_DEBUG_FUNCPTR (gst_bin_handle_message_func);
489 klass->do_latency = GST_DEBUG_FUNCPTR (gst_bin_do_latency_func);
491 GST_DEBUG ("creating bin thread pool");
494 g_thread_pool_new ((GFunc) gst_bin_continue_func, NULL, -1, FALSE, &err);
496 g_critical ("could alloc threadpool %s", err->message);
501 gst_bin_init (GstBin * bin)
505 bin->numchildren = 0;
506 bin->children = NULL;
507 bin->children_cookie = 0;
508 bin->messages = NULL;
509 bin->provided_clock = NULL;
510 bin->clock_dirty = FALSE;
512 /* Set up a bus for listening to child elements */
513 bus = g_object_new (GST_TYPE_BUS, "enable-async", FALSE, NULL);
514 bin->child_bus = bus;
515 GST_DEBUG_OBJECT (bin, "using bus %" GST_PTR_FORMAT " to listen to children",
517 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bin_bus_handler, bin);
519 bin->priv = GST_BIN_GET_PRIVATE (bin);
520 bin->priv->asynchandling = DEFAULT_ASYNC_HANDLING;
521 bin->priv->structure_cookie = 0;
522 bin->priv->message_forward = DEFAULT_MESSAGE_FORWARD;
526 gst_bin_dispose (GObject * object)
528 GstBin *bin = GST_BIN_CAST (object);
529 GstBus **child_bus_p = &bin->child_bus;
530 GstClock **provided_clock_p = &bin->provided_clock;
531 GstElement **clock_provider_p = &bin->clock_provider;
532 GstIndex **index_p = &bin->priv->index;
534 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
536 GST_OBJECT_LOCK (object);
537 gst_object_replace ((GstObject **) child_bus_p, NULL);
538 gst_object_replace ((GstObject **) provided_clock_p, NULL);
539 gst_object_replace ((GstObject **) clock_provider_p, NULL);
540 gst_object_replace ((GstObject **) index_p, NULL);
541 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
542 GST_OBJECT_UNLOCK (object);
544 while (bin->children) {
545 gst_bin_remove (bin, GST_ELEMENT_CAST (bin->children->data));
547 if (G_UNLIKELY (bin->children != NULL)) {
548 g_critical ("could not remove elements from bin '%s'",
549 GST_STR_NULL (GST_OBJECT_NAME (object)));
552 G_OBJECT_CLASS (parent_class)->dispose (object);
557 * @name: the name of the new bin
559 * Creates a new bin with the given name.
561 * Returns: (transfer full): a new #GstBin
564 gst_bin_new (const gchar * name)
566 return gst_element_factory_make ("bin", name);
570 gst_bin_set_property (GObject * object, guint prop_id,
571 const GValue * value, GParamSpec * pspec)
575 gstbin = GST_BIN_CAST (object);
578 case PROP_ASYNC_HANDLING:
579 GST_OBJECT_LOCK (gstbin);
580 gstbin->priv->asynchandling = g_value_get_boolean (value);
581 GST_OBJECT_UNLOCK (gstbin);
583 case PROP_MESSAGE_FORWARD:
584 GST_OBJECT_LOCK (gstbin);
585 gstbin->priv->message_forward = g_value_get_boolean (value);
586 GST_OBJECT_UNLOCK (gstbin);
589 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
595 gst_bin_get_property (GObject * object, guint prop_id,
596 GValue * value, GParamSpec * pspec)
600 gstbin = GST_BIN_CAST (object);
603 case PROP_ASYNC_HANDLING:
604 GST_OBJECT_LOCK (gstbin);
605 g_value_set_boolean (value, gstbin->priv->asynchandling);
606 GST_OBJECT_UNLOCK (gstbin);
608 case PROP_MESSAGE_FORWARD:
609 GST_OBJECT_LOCK (gstbin);
610 g_value_set_boolean (value, gstbin->priv->message_forward);
611 GST_OBJECT_UNLOCK (gstbin);
614 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
619 /* return the cached index */
621 gst_bin_get_index_func (GstElement * element)
626 bin = GST_BIN_CAST (element);
628 GST_OBJECT_LOCK (bin);
629 if ((result = bin->priv->index))
630 gst_object_ref (result);
631 GST_OBJECT_UNLOCK (bin);
636 /* set the index on all elements in this bin
641 gst_bin_set_index_func (GstElement * element, GstIndex * index)
647 GValue data = { 0, };
649 bin = GST_BIN_CAST (element);
651 GST_OBJECT_LOCK (bin);
652 old = bin->priv->index;
653 if (G_UNLIKELY (old == index))
656 gst_object_ref (index);
657 bin->priv->index = index;
658 GST_OBJECT_UNLOCK (bin);
661 gst_object_unref (old);
663 it = gst_bin_iterate_elements (bin);
665 /* set the index on all elements in the bin */
668 switch (gst_iterator_next (it, &data)) {
669 case GST_ITERATOR_OK:
671 GstElement *child = g_value_get_object (&data);
673 GST_DEBUG_OBJECT (bin, "setting index on '%s'",
674 GST_ELEMENT_NAME (child));
675 gst_element_set_index (child, index);
677 g_value_reset (&data);
680 case GST_ITERATOR_RESYNC:
681 GST_DEBUG_OBJECT (bin, "iterator doing resync");
682 gst_iterator_resync (it);
685 case GST_ITERATOR_DONE:
686 GST_DEBUG_OBJECT (bin, "iterator done");
691 g_value_unset (&data);
692 gst_iterator_free (it);
697 GST_DEBUG_OBJECT (bin, "index was already set");
698 GST_OBJECT_UNLOCK (bin);
703 /* set the clock on all elements in this bin
708 gst_bin_set_clock_func (GstElement * element, GstClock * clock)
714 GValue data = { 0, };
716 bin = GST_BIN_CAST (element);
718 it = gst_bin_iterate_elements (bin);
722 switch (gst_iterator_next (it, &data)) {
723 case GST_ITERATOR_OK:
725 GstElement *child = g_value_get_object (&data);
727 res &= gst_element_set_clock (child, clock);
729 g_value_reset (&data);
732 case GST_ITERATOR_RESYNC:
733 GST_DEBUG_OBJECT (bin, "iterator doing resync");
734 gst_iterator_resync (it);
738 case GST_ITERATOR_DONE:
739 GST_DEBUG_OBJECT (bin, "iterator done");
744 g_value_unset (&data);
745 gst_iterator_free (it);
748 res = GST_ELEMENT_CLASS (parent_class)->set_clock (element, clock);
753 /* get the clock for this bin by asking all of the children in this bin
755 * The ref of the returned clock in increased so unref after usage.
757 * We loop the elements in state order and pick the last clock we can
758 * get. This makes sure we get a clock from the source.
763 gst_bin_provide_clock_func (GstElement * element)
765 GstClock *result = NULL;
766 GstElement *provider = NULL;
771 GstClock **provided_clock_p;
772 GstElement **clock_provider_p;
774 bin = GST_BIN_CAST (element);
776 GST_OBJECT_LOCK (bin);
777 if (!bin->clock_dirty)
780 GST_DEBUG_OBJECT (bin, "finding new clock");
782 it = gst_bin_sort_iterator_new (bin);
783 GST_OBJECT_UNLOCK (bin);
787 switch (gst_iterator_next (it, &val)) {
788 case GST_ITERATOR_OK:
790 GstElement *child = g_value_get_object (&val);
793 clock = gst_element_provide_clock (child);
795 GST_DEBUG_OBJECT (bin, "found candidate clock %p by element %s",
796 clock, GST_ELEMENT_NAME (child));
798 gst_object_unref (result);
799 gst_object_unref (provider);
802 provider = gst_object_ref (child);
805 g_value_reset (&val);
808 case GST_ITERATOR_RESYNC:
809 gst_iterator_resync (it);
812 case GST_ITERATOR_DONE:
817 g_value_unset (&val);
818 gst_iterator_free (it);
820 GST_OBJECT_LOCK (bin);
821 if (!bin->clock_dirty) {
823 gst_object_unref (provider);
825 gst_object_unref (result);
831 provided_clock_p = &bin->provided_clock;
832 clock_provider_p = &bin->clock_provider;
833 gst_object_replace ((GstObject **) provided_clock_p, (GstObject *) result);
834 gst_object_replace ((GstObject **) clock_provider_p, (GstObject *) provider);
835 bin->clock_dirty = FALSE;
836 GST_DEBUG_OBJECT (bin,
837 "provided new clock %" GST_PTR_FORMAT " by provider %" GST_PTR_FORMAT,
839 /* Provider is not being returned to caller, just the result */
841 gst_object_unref (provider);
842 GST_OBJECT_UNLOCK (bin);
848 if ((result = bin->provided_clock))
849 gst_object_ref (result);
850 GST_DEBUG_OBJECT (bin, "returning old clock %p", result);
851 GST_OBJECT_UNLOCK (bin);
858 * functions for manipulating cached messages
863 GstMessageType types;
866 /* check if a message is of given src and type */
868 message_check (GstMessage * message, MessageFind * target)
873 eq &= GST_MESSAGE_SRC (message) == target->src;
875 eq &= (GST_MESSAGE_TYPE (message) & target->types) != 0;
876 GST_LOG ("looking at message %p: %d", message, eq);
882 find_message (GstBin * bin, GstObject * src, GstMessageType types)
890 result = g_list_find_custom (bin->messages, &find,
891 (GCompareFunc) message_check);
894 GST_DEBUG_OBJECT (bin, "we found a message %p from %s matching types %08x",
895 result->data, GST_OBJECT_NAME (GST_MESSAGE_CAST (result->data)->src),
898 GST_DEBUG_OBJECT (bin, "no message found matching types %08x", types);
899 #ifndef GST_DISABLE_GST_DEBUG
903 for (i = 0; i < 32; i++)
904 if (types & (1 << i))
905 GST_DEBUG_OBJECT (bin, " %s", gst_message_type_get_name (1 << i));
913 /* with LOCK, returns TRUE if message had a valid SRC, takes ownership of
916 * A message that is cached and has the same SRC and type is replaced
917 * by the given message.
920 bin_replace_message (GstBin * bin, GstMessage * message, GstMessageType types)
926 if ((src = GST_MESSAGE_SRC (message))) {
927 /* first find the previous message posted by this element */
928 if ((previous = find_message (bin, src, types))) {
929 GstMessage *previous_msg;
931 /* if we found a previous message, replace it */
932 previous_msg = previous->data;
933 previous->data = message;
935 GST_DEBUG_OBJECT (bin, "replace old message %s from %s with %s message",
936 GST_MESSAGE_TYPE_NAME (previous_msg), GST_ELEMENT_NAME (src),
937 GST_MESSAGE_TYPE_NAME (message));
939 gst_message_unref (previous_msg);
941 /* keep new message */
942 bin->messages = g_list_prepend (bin->messages, message);
944 GST_DEBUG_OBJECT (bin, "got new message %p, %s from %s",
945 message, GST_MESSAGE_TYPE_NAME (message), GST_ELEMENT_NAME (src));
948 GST_DEBUG_OBJECT (bin, "got message %s from (NULL), not processing",
949 GST_MESSAGE_TYPE_NAME (message));
951 gst_message_unref (message);
956 /* with LOCK. Remove all messages of given types */
958 bin_remove_messages (GstBin * bin, GstObject * src, GstMessageType types)
966 for (walk = bin->messages; walk; walk = next) {
967 GstMessage *message = (GstMessage *) walk->data;
969 next = g_list_next (walk);
971 if (message_check (message, &find) == 0) {
972 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (message),
973 "deleting message %p of types 0x%08x", message, types);
974 bin->messages = g_list_delete_link (bin->messages, walk);
975 gst_message_unref (message);
977 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (message),
978 "not deleting message %p of type 0x%08x", message,
979 GST_MESSAGE_TYPE (message));
985 /* Check if the bin is EOS. We do this by scanning all sinks and
986 * checking if they posted an EOS message.
988 * call with bin LOCK */
990 is_eos (GstBin * bin, guint32 * seqnum)
997 for (walk = bin->children; walk; walk = g_list_next (walk)) {
1000 element = GST_ELEMENT_CAST (walk->data);
1001 if (bin_element_is_sink (element, bin) == 0) {
1002 /* check if element posted EOS */
1004 find_message (bin, GST_OBJECT_CAST (element), GST_MESSAGE_EOS))) {
1005 GST_DEBUG ("sink '%s' posted EOS", GST_ELEMENT_NAME (element));
1006 *seqnum = gst_message_get_seqnum (GST_MESSAGE_CAST (msgs->data));
1009 GST_DEBUG ("sink '%s' did not post EOS yet",
1010 GST_ELEMENT_NAME (element));
1016 /* FIXME: Some tests (e.g. elements/capsfilter) use
1017 * pipelines with a dangling sinkpad but no sink element.
1018 * These tests assume that no EOS message is ever
1019 * posted on the bus so let's keep that behaviour.
1020 * In valid pipelines this doesn't make a difference.
1022 return result && n_eos > 0;
1026 unlink_pads (const GValue * item, gpointer user_data)
1031 pad = g_value_get_object (item);
1033 if ((peer = gst_pad_get_peer (pad))) {
1034 if (gst_pad_get_direction (pad) == GST_PAD_SRC)
1035 gst_pad_unlink (pad, peer);
1037 gst_pad_unlink (peer, pad);
1038 gst_object_unref (peer);
1042 /* vmethod that adds an element to a bin
1047 gst_bin_add_func (GstBin * bin, GstElement * element)
1051 gboolean is_sink, is_source, provides_clock, requires_clock;
1052 GstMessage *clock_message = NULL, *async_message = NULL;
1053 GstStateChangeReturn ret;
1055 GST_DEBUG_OBJECT (bin, "element :%s", GST_ELEMENT_NAME (element));
1057 /* we obviously can't add ourself to ourself */
1058 if (G_UNLIKELY (GST_ELEMENT_CAST (element) == GST_ELEMENT_CAST (bin)))
1061 /* get the element name to make sure it is unique in this bin. */
1062 GST_OBJECT_LOCK (element);
1063 elem_name = g_strdup (GST_ELEMENT_NAME (element));
1064 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1065 is_source = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE);
1067 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1069 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1070 GST_OBJECT_UNLOCK (element);
1072 GST_OBJECT_LOCK (bin);
1074 /* then check to see if the element's name is already taken in the bin,
1075 * we can safely take the lock here. This check is probably bogus because
1076 * you can safely change the element name after this check and before setting
1077 * the object parent. The window is very small though... */
1078 if (G_UNLIKELY (!gst_object_check_uniqueness (bin->children, elem_name)))
1079 goto duplicate_name;
1081 /* set the element's parent and add the element to the bin's list of children */
1082 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (element),
1083 GST_OBJECT_CAST (bin))))
1086 /* if we add a sink we become a sink */
1088 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "element \"%s\" was sink",
1090 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SINK);
1093 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "element \"%s\" was source",
1095 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SOURCE);
1097 if (provides_clock) {
1098 GST_DEBUG_OBJECT (bin, "element \"%s\" can provide a clock", elem_name);
1100 gst_message_new_clock_provide (GST_OBJECT_CAST (element), NULL, TRUE);
1101 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1103 if (requires_clock) {
1104 GST_DEBUG_OBJECT (bin, "element \"%s\" requires a clock", elem_name);
1105 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1108 bin->children = g_list_prepend (bin->children, element);
1110 bin->children_cookie++;
1111 bin->priv->structure_cookie++;
1113 /* distribute the bus */
1114 gst_element_set_bus (element, bin->child_bus);
1116 /* propagate the current base_time, start_time and clock */
1117 gst_element_set_base_time (element, GST_ELEMENT_CAST (bin)->base_time);
1118 gst_element_set_start_time (element, GST_ELEMENT_START_TIME (bin));
1119 /* it's possible that the element did not accept the clock but
1120 * that is not important right now. When the pipeline goes to PLAYING,
1121 * a new clock will be selected */
1122 gst_element_set_clock (element, GST_ELEMENT_CLOCK (bin));
1123 /* set the cached index on the children */
1124 if (bin->priv->index)
1125 gst_element_set_index (element, bin->priv->index);
1127 ret = GST_STATE_RETURN (bin);
1128 /* no need to update the state if we are in error */
1129 if (ret == GST_STATE_CHANGE_FAILURE)
1130 goto no_state_recalc;
1132 /* update the bin state, the new element could have been an ASYNC or
1133 * NO_PREROLL element */
1134 ret = GST_STATE_RETURN (element);
1135 GST_DEBUG_OBJECT (bin, "added %s element",
1136 gst_element_state_change_return_get_name (ret));
1139 case GST_STATE_CHANGE_ASYNC:
1141 /* create message to track this aync element when it posts an async-done
1143 async_message = gst_message_new_async_start (GST_OBJECT_CAST (element));
1146 case GST_STATE_CHANGE_NO_PREROLL:
1147 /* ignore all async elements we might have and commit our state */
1148 bin_handle_async_done (bin, ret, FALSE, FALSE);
1150 case GST_STATE_CHANGE_FAILURE:
1157 GST_OBJECT_UNLOCK (bin);
1159 /* post the messages on the bus of the element so that the bin can handle
1162 gst_element_post_message (GST_ELEMENT_CAST (element), clock_message);
1165 gst_element_post_message (GST_ELEMENT_CAST (element), async_message);
1167 /* unlink all linked pads */
1168 it = gst_element_iterate_pads (element);
1169 gst_iterator_foreach (it, (GstIteratorForeachFunction) unlink_pads, NULL);
1170 gst_iterator_free (it);
1172 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "added element \"%s\"",
1176 g_signal_emit (bin, gst_bin_signals[ELEMENT_ADDED], 0, element);
1177 gst_child_proxy_child_added ((GstObject *) bin, (GstObject *) element);
1181 /* ERROR handling here */
1184 GST_OBJECT_LOCK (bin);
1185 g_warning ("Cannot add bin '%s' to itself", GST_ELEMENT_NAME (bin));
1186 GST_OBJECT_UNLOCK (bin);
1191 g_warning ("Name '%s' is not unique in bin '%s', not adding",
1192 elem_name, GST_ELEMENT_NAME (bin));
1193 GST_OBJECT_UNLOCK (bin);
1199 g_warning ("Element '%s' already has parent", elem_name);
1200 GST_OBJECT_UNLOCK (bin);
1209 * @element: (transfer full): the #GstElement to add
1211 * Adds the given element to the bin. Sets the element's parent, and thus
1212 * takes ownership of the element. An element can only be added to one bin.
1214 * If the element's pads are linked to other pads, the pads will be unlinked
1215 * before the element is added to the bin.
1218 * When you add an element to an already-running pipeline, you will have to
1219 * take care to set the state of the newly-added element to the desired
1220 * state (usually PLAYING or PAUSED, same you set the pipeline to originally)
1221 * with gst_element_set_state(), or use gst_element_sync_state_with_parent().
1222 * The bin or pipeline will not take care of this for you.
1227 * Returns: TRUE if the element could be added, FALSE if
1228 * the bin does not want to accept the element.
1231 gst_bin_add (GstBin * bin, GstElement * element)
1233 GstBinClass *bclass;
1236 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1237 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1239 bclass = GST_BIN_GET_CLASS (bin);
1241 if (G_UNLIKELY (bclass->add_element == NULL))
1244 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "adding element %s to bin %s",
1245 GST_STR_NULL (GST_ELEMENT_NAME (element)),
1246 GST_STR_NULL (GST_ELEMENT_NAME (bin)));
1248 result = bclass->add_element (bin, element);
1252 /* ERROR handling */
1255 g_warning ("adding elements to bin '%s' is not supported",
1256 GST_ELEMENT_NAME (bin));
1261 /* remove an element from the bin
1266 gst_bin_remove_func (GstBin * bin, GstElement * element)
1270 gboolean is_sink, is_source, provides_clock, requires_clock;
1271 gboolean othersink, othersource, otherprovider, otherrequirer, found;
1272 GstMessage *clock_message = NULL;
1273 GstClock **provided_clock_p;
1274 GstElement **clock_provider_p;
1276 gboolean other_async, this_async, have_no_preroll;
1277 GstStateChangeReturn ret;
1279 GST_DEBUG_OBJECT (bin, "element :%s", GST_ELEMENT_NAME (element));
1281 GST_OBJECT_LOCK (element);
1282 /* Check if the element is already being removed and immediately
1284 if (G_UNLIKELY (GST_OBJECT_FLAG_IS_SET (element,
1285 GST_ELEMENT_FLAG_UNPARENTING)))
1286 goto already_removing;
1288 GST_OBJECT_FLAG_SET (element, GST_ELEMENT_FLAG_UNPARENTING);
1289 /* grab element name so we can print it */
1290 elem_name = g_strdup (GST_ELEMENT_NAME (element));
1291 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1292 is_source = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE);
1294 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1296 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1297 GST_OBJECT_UNLOCK (element);
1299 /* unlink all linked pads */
1300 it = gst_element_iterate_pads (element);
1301 gst_iterator_foreach (it, (GstIteratorForeachFunction) unlink_pads, NULL);
1302 gst_iterator_free (it);
1304 GST_OBJECT_LOCK (bin);
1307 othersource = FALSE;
1308 otherprovider = FALSE;
1309 otherrequirer = FALSE;
1310 have_no_preroll = FALSE;
1311 /* iterate the elements, we collect which ones are async and no_preroll. We
1312 * also remove the element when we find it. */
1313 for (walk = bin->children; walk; walk = next) {
1314 GstElement *child = GST_ELEMENT_CAST (walk->data);
1316 next = g_list_next (walk);
1318 if (child == element) {
1320 /* remove the element */
1321 bin->children = g_list_delete_link (bin->children, walk);
1323 gboolean child_sink, child_source, child_provider, child_requirer;
1325 GST_OBJECT_LOCK (child);
1326 child_sink = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SINK);
1327 child_source = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SOURCE);
1329 GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1331 GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1332 /* when we remove a sink, check if there are other sinks. */
1333 if (is_sink && !othersink && child_sink)
1335 if (is_source && !othersource && child_source)
1337 if (provides_clock && !otherprovider && child_provider)
1338 otherprovider = TRUE;
1339 if (requires_clock && !otherrequirer && child_requirer)
1340 otherrequirer = TRUE;
1341 /* check if we have NO_PREROLL children */
1342 if (GST_STATE_RETURN (child) == GST_STATE_CHANGE_NO_PREROLL)
1343 have_no_preroll = TRUE;
1344 GST_OBJECT_UNLOCK (child);
1348 /* the element must have been in the bin's list of children */
1349 if (G_UNLIKELY (!found))
1352 /* we now removed the element from the list of elements, increment the cookie
1353 * so that others can detect a change in the children list. */
1355 bin->children_cookie++;
1356 bin->priv->structure_cookie++;
1358 if (is_sink && !othersink) {
1359 /* we're not a sink anymore */
1360 GST_DEBUG_OBJECT (bin, "we removed the last sink");
1361 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_SINK);
1363 if (is_source && !othersource) {
1364 /* we're not a source anymore */
1365 GST_DEBUG_OBJECT (bin, "we removed the last source");
1366 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_SOURCE);
1368 if (provides_clock && !otherprovider) {
1369 /* we're not a clock provider anymore */
1370 GST_DEBUG_OBJECT (bin, "we removed the last clock provider");
1371 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1373 if (requires_clock && !otherrequirer) {
1374 /* we're not a clock requirer anymore */
1375 GST_DEBUG_OBJECT (bin, "we removed the last clock requirer");
1376 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1379 /* if the clock provider for this element is removed, we lost
1380 * the clock as well, we need to inform the parent of this
1381 * so that it can select a new clock */
1382 if (bin->clock_provider == element) {
1383 GST_DEBUG_OBJECT (bin, "element \"%s\" provided the clock", elem_name);
1384 bin->clock_dirty = TRUE;
1386 gst_message_new_clock_lost (GST_OBJECT_CAST (bin), bin->provided_clock);
1387 provided_clock_p = &bin->provided_clock;
1388 clock_provider_p = &bin->clock_provider;
1389 gst_object_replace ((GstObject **) provided_clock_p, NULL);
1390 gst_object_replace ((GstObject **) clock_provider_p, NULL);
1393 /* remove messages for the element, if there was a pending ASYNC_START
1394 * message we must see if removing the element caused the bin to lose its
1397 other_async = FALSE;
1398 for (walk = bin->messages; walk; walk = next) {
1399 GstMessage *message = (GstMessage *) walk->data;
1400 GstElement *src = GST_ELEMENT_CAST (GST_MESSAGE_SRC (message));
1403 next = g_list_next (walk);
1406 switch (GST_MESSAGE_TYPE (message)) {
1407 case GST_MESSAGE_ASYNC_START:
1413 GST_DEBUG_OBJECT (src, "looking at message %p", message);
1415 case GST_MESSAGE_STRUCTURE_CHANGE:
1419 GST_DEBUG_OBJECT (src, "looking at structure change message %p",
1421 /* it's unlikely that this message is still in the list of messages
1422 * because this would mean that a link/unlink is busy in another thread
1423 * while we remove the element. We still have to remove the message
1424 * because we might not receive the done message anymore when the element
1425 * is removed from the bin. */
1426 gst_message_parse_structure_change (message, NULL, &owner, NULL);
1427 if (owner == element)
1438 /* delete all message types */
1439 GST_DEBUG_OBJECT (src, "deleting message %p of element \"%s\"",
1440 message, elem_name);
1441 bin->messages = g_list_delete_link (bin->messages, walk);
1442 gst_message_unref (message);
1446 /* get last return */
1447 ret = GST_STATE_RETURN (bin);
1449 /* no need to update the state if we are in error */
1450 if (ret == GST_STATE_CHANGE_FAILURE)
1451 goto no_state_recalc;
1453 if (!other_async && this_async) {
1454 /* all other elements were not async and we removed the async one,
1455 * handle the async-done case because we are not async anymore now. */
1456 GST_DEBUG_OBJECT (bin,
1457 "we removed the last async element, have no_preroll %d",
1460 /* the current state return of the bin depends on if there are no_preroll
1461 * elements in the pipeline or not */
1462 if (have_no_preroll)
1463 ret = GST_STATE_CHANGE_NO_PREROLL;
1465 ret = GST_STATE_CHANGE_SUCCESS;
1467 bin_handle_async_done (bin, ret, FALSE, FALSE);
1469 GST_DEBUG_OBJECT (bin,
1470 "recalc state preroll: %d, other async: %d, this async %d",
1471 have_no_preroll, other_async, this_async);
1473 if (have_no_preroll) {
1474 ret = GST_STATE_CHANGE_NO_PREROLL;
1475 } else if (other_async) {
1476 /* there are other async elements and we were not doing an async state
1477 * change, change our pending state and go async */
1478 if (GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING) {
1479 GST_STATE_NEXT (bin) = GST_STATE (bin);
1480 GST_STATE_PENDING (bin) = GST_STATE (bin);
1482 ret = GST_STATE_CHANGE_ASYNC;
1484 GST_STATE_RETURN (bin) = ret;
1487 GST_OBJECT_UNLOCK (bin);
1490 gst_element_post_message (GST_ELEMENT_CAST (bin), clock_message);
1492 GST_CAT_INFO_OBJECT (GST_CAT_PARENTAGE, bin, "removed child \"%s\"",
1496 gst_element_set_bus (element, NULL);
1498 /* Clear the clock we provided to the element */
1499 gst_element_set_clock (element, NULL);
1501 /* we ref here because after the _unparent() the element can be disposed
1502 * and we still need it to reset the UNPARENTING flag and fire a signal. */
1503 gst_object_ref (element);
1504 gst_object_unparent (GST_OBJECT_CAST (element));
1506 GST_OBJECT_LOCK (element);
1507 GST_OBJECT_FLAG_UNSET (element, GST_ELEMENT_FLAG_UNPARENTING);
1508 GST_OBJECT_UNLOCK (element);
1510 g_signal_emit (bin, gst_bin_signals[ELEMENT_REMOVED], 0, element);
1511 gst_child_proxy_child_removed ((GstObject *) bin, (GstObject *) element);
1513 /* element is really out of our control now */
1514 gst_object_unref (element);
1518 /* ERROR handling */
1521 g_warning ("Element '%s' is not in bin '%s'", elem_name,
1522 GST_ELEMENT_NAME (bin));
1523 GST_OBJECT_UNLOCK (bin);
1529 GST_CAT_INFO_OBJECT (GST_CAT_PARENTAGE, bin, "already removing child");
1530 GST_OBJECT_UNLOCK (element);
1538 * @element: (transfer none): the #GstElement to remove
1540 * Removes the element from the bin, unparenting it as well.
1541 * Unparenting the element means that the element will be dereferenced,
1542 * so if the bin holds the only reference to the element, the element
1543 * will be freed in the process of removing it from the bin. If you
1544 * want the element to still exist after removing, you need to call
1545 * gst_object_ref() before removing it from the bin.
1547 * If the element's pads are linked to other pads, the pads will be unlinked
1548 * before the element is removed from the bin.
1552 * Returns: TRUE if the element could be removed, FALSE if
1553 * the bin does not want to remove the element.
1556 gst_bin_remove (GstBin * bin, GstElement * element)
1558 GstBinClass *bclass;
1561 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1562 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1564 bclass = GST_BIN_GET_CLASS (bin);
1566 if (G_UNLIKELY (bclass->remove_element == NULL))
1569 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "removing element %s from bin %s",
1570 GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin));
1572 result = bclass->remove_element (bin, element);
1576 /* ERROR handling */
1579 g_warning ("removing elements from bin '%s' is not supported",
1580 GST_ELEMENT_NAME (bin));
1586 * gst_bin_iterate_elements:
1589 * Gets an iterator for the elements in this bin.
1591 * Each element yielded by the iterator will have its refcount increased, so
1594 * MT safe. Caller owns returned value.
1596 * Returns: (transfer full): a #GstIterator of #GstElement, or NULL
1599 gst_bin_iterate_elements (GstBin * bin)
1601 GstIterator *result;
1603 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1605 GST_OBJECT_LOCK (bin);
1606 result = gst_iterator_new_list (GST_TYPE_ELEMENT,
1607 GST_OBJECT_GET_LOCK (bin),
1608 &bin->children_cookie, &bin->children, (GObject *) bin, NULL);
1609 GST_OBJECT_UNLOCK (bin);
1614 static GstIteratorItem
1615 iterate_child_recurse (GstIterator * it, const GValue * item)
1617 GstElement *child = g_value_get_object (item);
1619 if (GST_IS_BIN (child)) {
1620 GstIterator *other = gst_bin_iterate_recurse (GST_BIN_CAST (child));
1622 gst_iterator_push (it, other);
1624 return GST_ITERATOR_ITEM_PASS;
1628 * gst_bin_iterate_recurse:
1631 * Gets an iterator for the elements in this bin.
1632 * This iterator recurses into GstBin children.
1634 * Each element yielded by the iterator will have its refcount increased, so
1637 * MT safe. Caller owns returned value.
1639 * Returns: (transfer full): a #GstIterator of #GstElement, or NULL
1642 gst_bin_iterate_recurse (GstBin * bin)
1644 GstIterator *result;
1646 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1648 GST_OBJECT_LOCK (bin);
1649 result = gst_iterator_new_list (GST_TYPE_ELEMENT,
1650 GST_OBJECT_GET_LOCK (bin),
1651 &bin->children_cookie,
1653 (GObject *) bin, (GstIteratorItemFunction) iterate_child_recurse);
1654 GST_OBJECT_UNLOCK (bin);
1659 /* returns 0 when TRUE because this is a GCompareFunc */
1662 bin_element_is_sink (GstElement * child, GstBin * bin)
1666 /* we lock the child here for the remainder of the function to
1667 * get its name and flag safely. */
1668 GST_OBJECT_LOCK (child);
1669 is_sink = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SINK);
1671 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
1672 "child %s %s sink", GST_OBJECT_NAME (child), is_sink ? "is" : "is not");
1674 GST_OBJECT_UNLOCK (child);
1675 return is_sink ? 0 : 1;
1679 sink_iterator_filter (const GValue * vchild, GValue * vbin)
1681 GstBin *bin = g_value_get_object (vbin);
1682 GstElement *child = g_value_get_object (vchild);
1684 return (bin_element_is_sink (child, bin));
1688 * gst_bin_iterate_sinks:
1691 * Gets an iterator for all elements in the bin that have the
1692 * #GST_ELEMENT_FLAG_SINK flag set.
1694 * Each element yielded by the iterator will have its refcount increased, so
1697 * MT safe. Caller owns returned value.
1699 * Returns: (transfer full): a #GstIterator of #GstElement, or NULL
1702 gst_bin_iterate_sinks (GstBin * bin)
1704 GstIterator *children;
1705 GstIterator *result;
1706 GValue vbin = { 0, };
1708 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1710 g_value_init (&vbin, GST_TYPE_BIN);
1711 g_value_set_object (&vbin, bin);
1713 children = gst_bin_iterate_elements (bin);
1714 result = gst_iterator_filter (children,
1715 (GCompareFunc) sink_iterator_filter, &vbin);
1717 g_value_unset (&vbin);
1722 /* returns 0 when TRUE because this is a GCompareFunc */
1725 bin_element_is_src (GstElement * child, GstBin * bin)
1729 /* we lock the child here for the remainder of the function to
1730 * get its name and other info safely. */
1731 GST_OBJECT_LOCK (child);
1732 is_src = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SOURCE);
1734 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
1735 "child %s %s src", GST_OBJECT_NAME (child), is_src ? "is" : "is not");
1737 GST_OBJECT_UNLOCK (child);
1738 return is_src ? 0 : 1;
1742 src_iterator_filter (const GValue * vchild, GValue * vbin)
1744 GstBin *bin = g_value_get_object (vbin);
1745 GstElement *child = g_value_get_object (vchild);
1747 return (bin_element_is_src (child, bin));
1751 * gst_bin_iterate_sources:
1754 * Gets an iterator for all elements in the bin that have the
1755 * #GST_ELEMENT_FLAG_SOURCE flag set.
1757 * Each element yielded by the iterator will have its refcount increased, so
1760 * MT safe. Caller owns returned value.
1762 * Returns: (transfer full): a #GstIterator of #GstElement, or NULL
1765 gst_bin_iterate_sources (GstBin * bin)
1767 GstIterator *children;
1768 GstIterator *result;
1769 GValue vbin = { 0, };
1771 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1773 g_value_init (&vbin, GST_TYPE_BIN);
1774 g_value_set_object (&vbin, bin);
1776 children = gst_bin_iterate_elements (bin);
1777 result = gst_iterator_filter (children,
1778 (GCompareFunc) src_iterator_filter, &vbin);
1780 g_value_unset (&vbin);
1788 static GstStateChangeReturn
1789 gst_bin_get_state_func (GstElement * element, GstState * state,
1790 GstState * pending, GstClockTime timeout)
1792 GstStateChangeReturn ret;
1794 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
1797 GST_ELEMENT_CLASS (parent_class)->get_state (element, state, pending,
1803 /***********************************************
1804 * Topologically sorted iterator
1805 * see http://en.wikipedia.org/wiki/Topological_sorting
1807 * For each element in the graph, an entry is kept in a HashTable
1808 * with its number of srcpad connections (degree).
1809 * We then change state of all elements without dependencies
1810 * (degree 0) and decrement the degree of all elements connected
1811 * on the sinkpads. When an element reaches degree 0, its state is
1813 * When all elements are handled the algorithm stops.
1815 typedef struct _GstBinSortIterator
1818 GQueue queue; /* elements queued for state change */
1819 GstBin *bin; /* bin we iterate */
1820 gint mode; /* adding or removing dependency */
1821 GstElement *best; /* next element with least dependencies */
1822 gint best_deg; /* best degree */
1823 GHashTable *hash; /* hashtable with element dependencies */
1824 gboolean dirty; /* we detected structure change */
1825 } GstBinSortIterator;
1828 gst_bin_sort_iterator_copy (const GstBinSortIterator * it,
1829 GstBinSortIterator * copy)
1831 GHashTableIter iter;
1832 gpointer key, value;
1834 copy->queue = it->queue;
1835 g_queue_foreach (©->queue, (GFunc) gst_object_ref, NULL);
1837 copy->bin = gst_object_ref (it->bin);
1839 copy->best = gst_object_ref (it->best);
1841 copy->hash = g_hash_table_new (NULL, NULL);
1842 g_hash_table_iter_init (&iter, it->hash);
1843 while (g_hash_table_iter_next (&iter, &key, &value))
1844 g_hash_table_insert (copy->hash, key, value);
1847 /* we add and subtract 1 to make sure we don't confuse NULL and 0 */
1848 #define HASH_SET_DEGREE(bit, elem, deg) \
1849 g_hash_table_replace (bit->hash, elem, GINT_TO_POINTER(deg+1))
1850 #define HASH_GET_DEGREE(bit, elem) \
1851 (GPOINTER_TO_INT(g_hash_table_lookup (bit->hash, elem))-1)
1853 /* add element to queue of next elements in the iterator.
1854 * We push at the tail to give higher priority elements a
1857 add_to_queue (GstBinSortIterator * bit, GstElement * element)
1859 GST_DEBUG_OBJECT (bit->bin, "adding '%s' to queue",
1860 GST_ELEMENT_NAME (element));
1861 gst_object_ref (element);
1862 g_queue_push_tail (&bit->queue, element);
1863 HASH_SET_DEGREE (bit, element, -1);
1867 remove_from_queue (GstBinSortIterator * bit, GstElement * element)
1871 if ((find = g_queue_find (&bit->queue, element))) {
1872 GST_DEBUG_OBJECT (bit->bin, "removing '%s' from queue",
1873 GST_ELEMENT_NAME (element));
1875 g_queue_delete_link (&bit->queue, find);
1876 gst_object_unref (element);
1878 GST_DEBUG_OBJECT (bit->bin, "unable to remove '%s' from queue",
1879 GST_ELEMENT_NAME (element));
1883 /* clear the queue, unref all objects as we took a ref when
1884 * we added them to the queue */
1886 clear_queue (GQueue * queue)
1890 while ((p = g_queue_pop_head (queue)))
1891 gst_object_unref (p);
1894 /* set all degrees to 0. Elements marked as a sink are
1895 * added to the queue immediately. Since we only look at the SINK flag of the
1896 * element, it is possible that we add non-sinks to the queue. These will be
1897 * removed from the queue again when we can prove that it provides data for some
1900 reset_degree (GstElement * element, GstBinSortIterator * bit)
1904 /* sinks are added right away */
1905 GST_OBJECT_LOCK (element);
1906 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1907 GST_OBJECT_UNLOCK (element);
1910 add_to_queue (bit, element);
1912 /* others are marked with 0 and handled when sinks are done */
1913 HASH_SET_DEGREE (bit, element, 0);
1917 /* adjust the degree of all elements connected to the given
1918 * element. If a degree of an element drops to 0, it is
1919 * added to the queue of elements to schedule next.
1921 * We have to make sure not to cross the bin boundary this element
1925 update_degree (GstElement * element, GstBinSortIterator * bit)
1927 gboolean linked = FALSE;
1929 GST_OBJECT_LOCK (element);
1930 /* don't touch degree if element has no sinkpads */
1931 if (element->numsinkpads != 0) {
1932 /* loop over all sinkpads, decrement degree for all connected
1933 * elements in this bin */
1936 for (pads = element->sinkpads; pads; pads = g_list_next (pads)) {
1939 pad = GST_PAD_CAST (pads->data);
1941 /* we're iterating over the sinkpads, check if it's busy in a link/unlink */
1942 if (G_UNLIKELY (find_message (bit->bin, GST_OBJECT_CAST (pad),
1943 GST_MESSAGE_STRUCTURE_CHANGE))) {
1944 /* mark the iterator as dirty because we won't be updating the degree
1945 * of the peer parent now. This would result in the 'loop detected'
1946 * later on because the peer parent element could become the best next
1947 * element with a degree > 0. We will simply continue our state
1948 * changes and we'll eventually resync when the unlink completed and
1949 * the iterator cookie is updated. */
1954 if ((peer = gst_pad_get_peer (pad))) {
1955 GstElement *peer_element;
1957 if ((peer_element = gst_pad_get_parent_element (peer))) {
1958 GST_OBJECT_LOCK (peer_element);
1959 /* check that we don't go outside of this bin */
1960 if (GST_OBJECT_CAST (peer_element)->parent ==
1961 GST_OBJECT_CAST (bit->bin)) {
1962 gint old_deg, new_deg;
1964 old_deg = HASH_GET_DEGREE (bit, peer_element);
1966 /* check to see if we added an element as sink that was not really a
1967 * sink because it was connected to some other element. */
1968 if (old_deg == -1) {
1969 remove_from_queue (bit, peer_element);
1972 new_deg = old_deg + bit->mode;
1974 GST_DEBUG_OBJECT (bit->bin,
1975 "change element %s, degree %d->%d, linked to %s",
1976 GST_ELEMENT_NAME (peer_element), old_deg, new_deg,
1977 GST_ELEMENT_NAME (element));
1979 /* update degree, it is possible that an element was in 0 and
1980 * reaches -1 here. This would mean that the element had no sinkpads
1981 * but became linked while the state change was happening. We will
1982 * resync on this with the structure change message. */
1984 /* degree hit 0, add to queue */
1985 add_to_queue (bit, peer_element);
1987 HASH_SET_DEGREE (bit, peer_element, new_deg);
1991 GST_OBJECT_UNLOCK (peer_element);
1992 gst_object_unref (peer_element);
1994 gst_object_unref (peer);
1999 GST_DEBUG_OBJECT (bit->bin, "element %s not linked on any sinkpads",
2000 GST_ELEMENT_NAME (element));
2002 GST_OBJECT_UNLOCK (element);
2005 /* find the next best element not handled yet. This is the one
2006 * with the lowest non-negative degree */
2008 find_element (GstElement * element, GstBinSortIterator * bit)
2012 /* element is already handled */
2013 if ((degree = HASH_GET_DEGREE (bit, element)) < 0)
2016 /* first element or element with smaller degree */
2017 if (bit->best == NULL || bit->best_deg > degree) {
2018 bit->best = element;
2019 bit->best_deg = degree;
2023 /* get next element in iterator. the returned element has the
2024 * refcount increased */
2025 static GstIteratorResult
2026 gst_bin_sort_iterator_next (GstBinSortIterator * bit, GValue * result)
2029 GstBin *bin = bit->bin;
2031 /* empty queue, we have to find a next best element */
2032 if (g_queue_is_empty (&bit->queue)) {
2034 bit->best_deg = G_MAXINT;
2035 g_list_foreach (bin->children, (GFunc) find_element, bit);
2036 if ((best = bit->best)) {
2037 /* when we detected an unlink, don't warn because our degrees might be
2038 * screwed up. We will resync later */
2039 if (bit->best_deg != 0 && !bit->dirty) {
2040 /* we don't fail on this one yet */
2041 GST_WARNING_OBJECT (bin, "loop dected in graph");
2042 g_warning ("loop detected in the graph of bin '%s'!!",
2043 GST_ELEMENT_NAME (bin));
2045 /* best unhandled element, schedule as next element */
2046 GST_DEBUG_OBJECT (bin, "queue empty, next best: %s",
2047 GST_ELEMENT_NAME (best));
2048 HASH_SET_DEGREE (bit, best, -1);
2049 g_value_set_object (result, best);
2051 GST_DEBUG_OBJECT (bin, "queue empty, elements exhausted");
2052 /* no more unhandled elements, we are done */
2053 return GST_ITERATOR_DONE;
2056 /* everything added to the queue got reffed */
2057 best = g_queue_pop_head (&bit->queue);
2058 g_value_set_object (result, best);
2059 gst_object_unref (best);
2062 GST_DEBUG_OBJECT (bin, "queue head gives %s", GST_ELEMENT_NAME (best));
2063 /* update degrees of linked elements */
2064 update_degree (best, bit);
2066 return GST_ITERATOR_OK;
2069 /* clear queues, recalculate the degrees and restart. */
2071 gst_bin_sort_iterator_resync (GstBinSortIterator * bit)
2073 GstBin *bin = bit->bin;
2075 GST_DEBUG_OBJECT (bin, "resync");
2077 clear_queue (&bit->queue);
2079 g_list_foreach (bin->children, (GFunc) reset_degree, bit);
2080 /* calc degrees, incrementing */
2082 g_list_foreach (bin->children, (GFunc) update_degree, bit);
2083 /* for the rest of the function we decrement the degrees */
2087 /* clear queues, unref bin and free iterator. */
2089 gst_bin_sort_iterator_free (GstBinSortIterator * bit)
2091 GstBin *bin = bit->bin;
2093 GST_DEBUG_OBJECT (bin, "free");
2094 clear_queue (&bit->queue);
2095 g_hash_table_destroy (bit->hash);
2096 gst_object_unref (bin);
2099 /* should be called with the bin LOCK held */
2100 static GstIterator *
2101 gst_bin_sort_iterator_new (GstBin * bin)
2103 GstBinSortIterator *result;
2105 /* we don't need an ItemFunction because we ref the items in the _next
2107 result = (GstBinSortIterator *)
2108 gst_iterator_new (sizeof (GstBinSortIterator),
2110 GST_OBJECT_GET_LOCK (bin),
2111 &bin->priv->structure_cookie,
2112 (GstIteratorCopyFunction) gst_bin_sort_iterator_copy,
2113 (GstIteratorNextFunction) gst_bin_sort_iterator_next,
2114 (GstIteratorItemFunction) NULL,
2115 (GstIteratorResyncFunction) gst_bin_sort_iterator_resync,
2116 (GstIteratorFreeFunction) gst_bin_sort_iterator_free);
2117 g_queue_init (&result->queue);
2118 result->hash = g_hash_table_new (NULL, NULL);
2119 gst_object_ref (bin);
2121 gst_bin_sort_iterator_resync (result);
2123 return (GstIterator *) result;
2127 * gst_bin_iterate_sorted:
2130 * Gets an iterator for the elements in this bin in topologically
2131 * sorted order. This means that the elements are returned from
2132 * the most downstream elements (sinks) to the sources.
2134 * This function is used internally to perform the state changes
2135 * of the bin elements and for clock selection.
2137 * Each element yielded by the iterator will have its refcount increased, so
2140 * MT safe. Caller owns returned value.
2142 * Returns: (transfer full): a #GstIterator of #GstElement, or NULL
2145 gst_bin_iterate_sorted (GstBin * bin)
2147 GstIterator *result;
2149 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
2151 GST_OBJECT_LOCK (bin);
2152 result = gst_bin_sort_iterator_new (bin);
2153 GST_OBJECT_UNLOCK (bin);
2158 static GstStateChangeReturn
2159 gst_bin_element_set_state (GstBin * bin, GstElement * element,
2160 GstClockTime base_time, GstClockTime start_time, GstState current,
2163 GstStateChangeReturn ret;
2164 GstState pending, child_current, child_pending;
2168 GST_STATE_LOCK (element);
2170 GST_OBJECT_LOCK (element);
2171 /* set base_time and start time on child */
2172 GST_ELEMENT_START_TIME (element) = start_time;
2173 element->base_time = base_time;
2174 /* peel off the locked flag */
2175 locked = GST_ELEMENT_IS_LOCKED_STATE (element);
2176 /* Get the previous set_state result to preserve NO_PREROLL and ASYNC */
2177 ret = GST_STATE_RETURN (element);
2178 child_current = GST_STATE (element);
2179 child_pending = GST_STATE_PENDING (element);
2180 GST_OBJECT_UNLOCK (element);
2182 /* skip locked elements */
2183 if (G_UNLIKELY (locked))
2186 /* if the element was no preroll, just start changing the state regardless
2187 * if it had async elements (in the case of a bin) because they won't preroll
2189 if (G_UNLIKELY (ret == GST_STATE_CHANGE_NO_PREROLL)) {
2190 GST_DEBUG_OBJECT (element, "element is NO_PREROLL, ignore async elements");
2194 GST_OBJECT_LOCK (bin);
2195 pending = GST_STATE_PENDING (bin);
2197 /* Try not to change the state of elements that are already in the state we're
2199 if (!(next == GST_STATE_PLAYING || child_pending != GST_STATE_VOID_PENDING ||
2200 (child_pending == GST_STATE_VOID_PENDING &&
2201 ((pending > child_current && next > child_current) ||
2202 (pending < child_current && next < child_current)))))
2205 /* the element was busy with an upwards async state change, we must wait for
2206 * an ASYNC_DONE message before we attemp to change the state. */
2208 find_message (bin, GST_OBJECT_CAST (element),
2209 GST_MESSAGE_ASYNC_START))) {
2210 #ifndef GST_DISABLE_GST_DEBUG
2211 GstMessage *message = GST_MESSAGE_CAST (found->data);
2213 GST_DEBUG_OBJECT (element, "element message %p, %s async busy",
2214 message, GST_ELEMENT_NAME (GST_MESSAGE_SRC (message)));
2216 /* only wait for upward state changes */
2217 if (next > current) {
2218 /* We found an async element check if we can force its state to change or
2219 * if we have to wait for it to preroll. */
2220 if (G_UNLIKELY (!enable_latency)) {
2221 g_warning ("Future versions of GStreamer will wait for element \"%s\"\n"
2222 "\tto preroll in order to perform correct latency calculations.\n"
2223 "\tPlease verify that the application continues to work correctly by\n"
2224 "\tsetting the environment variable GST_COMPAT to a value containing\n"
2225 "\tthe string 'live-preroll'.", GST_ELEMENT_NAME (element));
2232 GST_OBJECT_UNLOCK (bin);
2235 GST_DEBUG_OBJECT (bin,
2236 "setting element %s to %s, base_time %" GST_TIME_FORMAT,
2237 GST_ELEMENT_NAME (element), gst_element_state_get_name (next),
2238 GST_TIME_ARGS (base_time));
2241 ret = gst_element_set_state (element, next);
2243 GST_STATE_UNLOCK (element);
2249 GST_DEBUG_OBJECT (element,
2250 "element is locked, return previous return %s",
2251 gst_element_state_change_return_get_name (ret));
2252 GST_STATE_UNLOCK (element);
2257 GST_DEBUG_OBJECT (element, "element was busy, delaying state change");
2258 GST_OBJECT_UNLOCK (bin);
2259 GST_STATE_UNLOCK (element);
2260 return GST_STATE_CHANGE_ASYNC;
2264 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2265 "skipping transition from %s to %s, since bin pending"
2266 " is %s : last change state return follows",
2267 gst_element_state_get_name (child_current),
2268 gst_element_state_get_name (next),
2269 gst_element_state_get_name (pending));
2270 GST_OBJECT_UNLOCK (bin);
2271 GST_STATE_UNLOCK (element);
2276 /* gst_iterator_fold functions for pads_activate
2277 * Stop the iterator if activating one pad failed. */
2279 activate_pads (const GValue * vpad, GValue * ret, gboolean * active)
2281 GstPad *pad = g_value_get_object (vpad);
2282 gboolean cont = TRUE;
2284 if (!(cont = gst_pad_set_active (pad, *active)))
2285 g_value_set_boolean (ret, FALSE);
2290 /* returns false on error or early cutout of the fold, true if all
2291 * pads in @iter were (de)activated successfully. */
2293 iterator_activate_fold_with_resync (GstIterator * iter, gpointer user_data)
2295 GstIteratorResult ires;
2298 /* no need to unset this later, it's just a boolean */
2299 g_value_init (&ret, G_TYPE_BOOLEAN);
2300 g_value_set_boolean (&ret, TRUE);
2303 ires = gst_iterator_fold (iter, (GstIteratorFoldFunction) activate_pads,
2306 case GST_ITERATOR_RESYNC:
2307 /* need to reset the result again */
2308 g_value_set_boolean (&ret, TRUE);
2309 gst_iterator_resync (iter);
2311 case GST_ITERATOR_DONE:
2312 /* all pads iterated, return collected value */
2315 /* iterator returned _ERROR or premature end with _OK,
2316 * mark an error and exit */
2317 g_value_set_boolean (&ret, FALSE);
2322 /* return collected value */
2323 return g_value_get_boolean (&ret);
2326 /* is called with STATE_LOCK
2329 gst_bin_src_pads_activate (GstBin * bin, gboolean active)
2334 GST_DEBUG_OBJECT (bin, "src_pads_activate with active %d", active);
2336 iter = gst_element_iterate_src_pads ((GstElement *) bin);
2337 fold_ok = iterator_activate_fold_with_resync (iter, &active);
2338 gst_iterator_free (iter);
2339 if (G_UNLIKELY (!fold_ok))
2342 GST_DEBUG_OBJECT (bin, "pads_activate successful");
2349 GST_DEBUG_OBJECT (bin, "source pads_activate failed");
2355 * gst_bin_recalculate_latency:
2358 * Query @bin for the current latency using and reconfigures this latency to all the
2359 * elements with a LATENCY event.
2361 * This method is typically called on the pipeline when a #GST_MESSAGE_LATENCY
2362 * is posted on the bus.
2364 * This function simply emits the 'do-latency' signal so any custom latency
2365 * calculations will be performed.
2367 * Returns: %TRUE if the latency could be queried and reconfigured.
2372 gst_bin_recalculate_latency (GstBin * bin)
2376 g_signal_emit (bin, gst_bin_signals[DO_LATENCY], 0, &res);
2377 GST_DEBUG_OBJECT (bin, "latency returned %d", res);
2383 gst_bin_do_latency_func (GstBin * bin)
2386 GstElement *element;
2387 GstClockTime min_latency, max_latency;
2390 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2392 element = GST_ELEMENT_CAST (bin);
2394 GST_DEBUG_OBJECT (element, "querying latency");
2396 query = gst_query_new_latency ();
2397 if ((res = gst_element_query (element, query))) {
2400 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
2402 GST_DEBUG_OBJECT (element,
2403 "got min latency %" GST_TIME_FORMAT ", max latency %"
2404 GST_TIME_FORMAT ", live %d", GST_TIME_ARGS (min_latency),
2405 GST_TIME_ARGS (max_latency), live);
2407 if (max_latency < min_latency) {
2408 /* this is an impossible situation, some parts of the pipeline might not
2409 * work correctly. We post a warning for now. */
2410 GST_ELEMENT_WARNING (element, CORE, CLOCK, (NULL),
2411 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
2412 GST_TIME_FORMAT ". Add queues or other buffering elements.",
2413 GST_TIME_ARGS (max_latency), GST_TIME_ARGS (min_latency)));
2416 /* configure latency on elements */
2417 res = gst_element_send_event (element, gst_event_new_latency (min_latency));
2419 GST_INFO_OBJECT (element, "configured latency of %" GST_TIME_FORMAT,
2420 GST_TIME_ARGS (min_latency));
2422 GST_WARNING_OBJECT (element,
2423 "did not really configure latency of %" GST_TIME_FORMAT,
2424 GST_TIME_ARGS (min_latency));
2427 /* this is not a real problem, we just don't configure any latency. */
2428 GST_WARNING_OBJECT (element, "failed to query latency");
2430 gst_query_unref (query);
2436 gst_bin_state_changed (GstElement * element, GstState oldstate,
2437 GstState newstate, GstState pending)
2439 GstElementClass *pklass = (GstElementClass *) parent_class;
2441 if (newstate == GST_STATE_PLAYING && pending == GST_STATE_VOID_PENDING)
2442 bin_do_eos (GST_BIN_CAST (element));
2444 if (pklass->state_changed)
2445 pklass->state_changed (element, oldstate, newstate, pending);
2448 static GstStateChangeReturn
2449 gst_bin_change_state_func (GstElement * element, GstStateChange transition)
2452 GstStateChangeReturn ret;
2453 GstState current, next;
2454 gboolean have_async;
2455 gboolean have_no_preroll;
2456 GstClockTime base_time, start_time;
2459 GValue data = { 0, };
2461 /* we don't need to take the STATE_LOCK, it is already taken */
2462 current = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2463 next = (GstState) GST_STATE_TRANSITION_NEXT (transition);
2465 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2466 "changing state of children from %s to %s",
2467 gst_element_state_get_name (current), gst_element_state_get_name (next));
2469 bin = GST_BIN_CAST (element);
2472 case GST_STATE_PLAYING:
2476 GST_OBJECT_LOCK (bin);
2477 toplevel = BIN_IS_TOPLEVEL (bin);
2478 GST_OBJECT_UNLOCK (bin);
2481 gst_bin_recalculate_latency (bin);
2484 case GST_STATE_PAUSED:
2485 /* Clear EOS list on next PAUSED */
2486 GST_OBJECT_LOCK (bin);
2487 GST_DEBUG_OBJECT (element, "clearing EOS elements");
2488 bin_remove_messages (bin, NULL, GST_MESSAGE_EOS);
2489 bin->priv->posted_eos = FALSE;
2490 GST_OBJECT_UNLOCK (bin);
2491 if (current == GST_STATE_READY)
2492 if (!(gst_bin_src_pads_activate (bin, TRUE)))
2493 goto activate_failure;
2495 case GST_STATE_READY:
2496 /* Clear message list on next READY */
2497 GST_OBJECT_LOCK (bin);
2498 GST_DEBUG_OBJECT (element, "clearing all cached messages");
2499 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
2500 GST_OBJECT_UNLOCK (bin);
2501 if (current == GST_STATE_PAUSED)
2502 if (!(gst_bin_src_pads_activate (bin, FALSE)))
2503 goto activate_failure;
2505 case GST_STATE_NULL:
2506 if (current == GST_STATE_READY)
2507 if (!(gst_bin_src_pads_activate (bin, FALSE)))
2508 goto activate_failure;
2514 /* this flag is used to make the async state changes return immediately. We
2515 * don't want them to interfere with this state change */
2516 GST_OBJECT_LOCK (bin);
2517 bin->polling = TRUE;
2518 GST_OBJECT_UNLOCK (bin);
2520 /* iterate in state change order */
2521 it = gst_bin_iterate_sorted (bin);
2523 /* mark if we've seen an ASYNC element in the bin when we did a state change.
2524 * Note how we don't reset this value when a resync happens, the reason being
2525 * that the async element posted ASYNC_START and we want to post ASYNC_DONE
2526 * even after a resync when the async element is gone */
2530 /* take base_time */
2531 base_time = gst_element_get_base_time (element);
2532 start_time = gst_element_get_start_time (element);
2534 have_no_preroll = FALSE;
2538 switch (gst_iterator_next (it, &data)) {
2539 case GST_ITERATOR_OK:
2543 child = g_value_get_object (&data);
2545 /* set state and base_time now */
2546 ret = gst_bin_element_set_state (bin, child, base_time, start_time,
2550 case GST_STATE_CHANGE_SUCCESS:
2551 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2552 "child '%s' changed state to %d(%s) successfully",
2553 GST_ELEMENT_NAME (child), next,
2554 gst_element_state_get_name (next));
2556 case GST_STATE_CHANGE_ASYNC:
2558 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2559 "child '%s' is changing state asynchronously to %s",
2560 GST_ELEMENT_NAME (child), gst_element_state_get_name (next));
2564 case GST_STATE_CHANGE_FAILURE:{
2567 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2568 "child '%s' failed to go to state %d(%s)",
2569 GST_ELEMENT_NAME (child),
2570 next, gst_element_state_get_name (next));
2572 /* Only fail if the child is still inside
2573 * this bin. It might've been removed already
2574 * because of the error by the bin subclass
2575 * to ignore the error. */
2576 parent = gst_object_get_parent (GST_OBJECT_CAST (child));
2577 if (parent == GST_OBJECT_CAST (element)) {
2578 /* element is still in bin, really error now */
2579 gst_object_unref (parent);
2582 /* child removed from bin, let the resync code redo the state
2584 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2585 "child '%s' was removed from the bin",
2586 GST_ELEMENT_NAME (child));
2589 gst_object_unref (parent);
2593 case GST_STATE_CHANGE_NO_PREROLL:
2594 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2595 "child '%s' changed state to %d(%s) successfully without preroll",
2596 GST_ELEMENT_NAME (child), next,
2597 gst_element_state_get_name (next));
2598 have_no_preroll = TRUE;
2601 g_assert_not_reached ();
2604 g_value_reset (&data);
2607 case GST_ITERATOR_RESYNC:
2608 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "iterator doing resync");
2609 gst_iterator_resync (it);
2612 case GST_ITERATOR_DONE:
2613 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "iterator done");
2619 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2620 if (G_UNLIKELY (ret == GST_STATE_CHANGE_FAILURE))
2623 if (have_no_preroll) {
2624 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
2625 "we have NO_PREROLL elements %s -> NO_PREROLL",
2626 gst_element_state_change_return_get_name (ret));
2627 ret = GST_STATE_CHANGE_NO_PREROLL;
2628 } else if (have_async) {
2629 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
2630 "we have ASYNC elements %s -> ASYNC",
2631 gst_element_state_change_return_get_name (ret));
2632 ret = GST_STATE_CHANGE_ASYNC;
2636 g_value_unset (&data);
2637 gst_iterator_free (it);
2639 GST_OBJECT_LOCK (bin);
2640 bin->polling = FALSE;
2641 /* it's possible that we did not get ASYNC from the children while the bin is
2642 * simulating ASYNC behaviour by posting an ASYNC_DONE message on the bus with
2643 * itself as the source. In that case we still want to check if the state
2644 * change completed. */
2645 if (ret != GST_STATE_CHANGE_ASYNC && !bin->priv->pending_async_done) {
2646 /* no element returned ASYNC and there are no pending async_done messages,
2647 * we can just complete. */
2648 GST_DEBUG_OBJECT (bin, "no async elements");
2651 /* when we get here an ASYNC element was found */
2652 if (GST_STATE_TARGET (bin) <= GST_STATE_READY) {
2653 /* we ignore ASYNC state changes when we go to READY or NULL */
2654 GST_DEBUG_OBJECT (bin, "target state %s <= READY",
2655 gst_element_state_get_name (GST_STATE_TARGET (bin)));
2659 GST_DEBUG_OBJECT (bin, "check async elements");
2660 /* check if all elements managed to commit their state already */
2661 if (!find_message (bin, NULL, GST_MESSAGE_ASYNC_START)) {
2662 /* nothing found, remove all old ASYNC_DONE messages. This can happen when
2663 * all the elements commited their state while we were doing the state
2664 * change. We will still return ASYNC for consistency but we commit the
2665 * state already so that a _get_state() will return immediately. */
2666 bin_remove_messages (bin, NULL, GST_MESSAGE_ASYNC_DONE);
2668 GST_DEBUG_OBJECT (bin, "async elements commited");
2669 bin_handle_async_done (bin, GST_STATE_CHANGE_SUCCESS, FALSE, FALSE);
2673 bin->priv->pending_async_done = FALSE;
2674 GST_OBJECT_UNLOCK (bin);
2676 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2677 "done changing bin's state from %s to %s, now in %s, ret %s",
2678 gst_element_state_get_name (current),
2679 gst_element_state_get_name (next),
2680 gst_element_state_get_name (GST_STATE (element)),
2681 gst_element_state_change_return_get_name (ret));
2688 GST_CAT_WARNING_OBJECT (GST_CAT_STATES, element,
2689 "failure (de)activating src pads");
2690 return GST_STATE_CHANGE_FAILURE;
2695 * This function is a utility event handler for seek events.
2696 * It will send the event to all sinks or sources depending on the
2699 * Applications are free to override this behaviour and
2700 * implement their own seek handler, but this will work for
2701 * pretty much all cases in practice.
2704 gst_bin_send_event (GstElement * element, GstEvent * event)
2706 GstBin *bin = GST_BIN_CAST (element);
2708 gboolean res = TRUE;
2709 gboolean done = FALSE;
2710 GValue data = { 0, };
2712 if (GST_EVENT_IS_DOWNSTREAM (event)) {
2713 iter = gst_bin_iterate_sources (bin);
2714 GST_DEBUG_OBJECT (bin, "Sending %s event to src children",
2715 GST_EVENT_TYPE_NAME (event));
2717 iter = gst_bin_iterate_sinks (bin);
2718 GST_DEBUG_OBJECT (bin, "Sending %s event to sink children",
2719 GST_EVENT_TYPE_NAME (event));
2723 switch (gst_iterator_next (iter, &data)) {
2724 case GST_ITERATOR_OK:
2726 GstElement *child = g_value_get_object (&data);;
2728 gst_event_ref (event);
2729 res &= gst_element_send_event (child, event);
2730 GST_LOG_OBJECT (child, "After handling %s event: %d",
2731 GST_EVENT_TYPE_NAME (event), res);
2734 case GST_ITERATOR_RESYNC:
2735 gst_iterator_resync (iter);
2738 case GST_ITERATOR_DONE:
2741 case GST_ITERATOR_ERROR:
2742 g_assert_not_reached ();
2746 g_value_unset (&data);
2747 gst_iterator_free (iter);
2748 gst_event_unref (event);
2753 /* this is the function called by the threadpool. When async elements commit
2754 * their state, this function will attempt to bring the bin to the next state.
2757 gst_bin_continue_func (BinContinueData * data)
2760 GstState current, next, pending;
2761 GstStateChange transition;
2764 pending = data->pending;
2766 GST_DEBUG_OBJECT (bin, "waiting for state lock");
2767 GST_STATE_LOCK (bin);
2769 GST_DEBUG_OBJECT (bin, "doing state continue");
2770 GST_OBJECT_LOCK (bin);
2772 /* if a new state change happened after this thread was scheduled, we return
2774 if (data->cookie != GST_ELEMENT_CAST (bin)->state_cookie)
2777 current = GST_STATE (bin);
2778 next = GST_STATE_GET_NEXT (current, pending);
2779 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2781 GST_STATE_NEXT (bin) = next;
2782 GST_STATE_PENDING (bin) = pending;
2784 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
2785 GST_OBJECT_UNLOCK (bin);
2787 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
2788 "continue state change %s to %s, final %s",
2789 gst_element_state_get_name (current),
2790 gst_element_state_get_name (next), gst_element_state_get_name (pending));
2792 gst_element_change_state (GST_ELEMENT_CAST (bin), transition);
2794 GST_STATE_UNLOCK (bin);
2795 GST_DEBUG_OBJECT (bin, "state continue done");
2797 gst_object_unref (bin);
2798 g_slice_free (BinContinueData, data);
2803 GST_OBJECT_UNLOCK (bin);
2804 GST_STATE_UNLOCK (bin);
2805 GST_DEBUG_OBJECT (bin, "state continue aborted due to intervening change");
2806 gst_object_unref (bin);
2807 g_slice_free (BinContinueData, data);
2812 static GstBusSyncReply
2813 bin_bus_handler (GstBus * bus, GstMessage * message, GstBin * bin)
2815 GstBinClass *bclass;
2817 bclass = GST_BIN_GET_CLASS (bin);
2818 if (bclass->handle_message)
2819 bclass->handle_message (bin, message);
2821 gst_message_unref (message);
2823 return GST_BUS_DROP;
2827 bin_push_state_continue (BinContinueData * data)
2834 klass = GST_BIN_GET_CLASS (bin);
2836 GST_DEBUG_OBJECT (bin, "pushing continue on thread pool");
2837 g_thread_pool_push (klass->pool, data, NULL);
2840 /* an element started an async state change, if we were not busy with a state
2841 * change, we perform a lost state.
2842 * This function is called with the OBJECT lock.
2845 bin_handle_async_start (GstBin * bin)
2847 GstState old_state, new_state;
2849 GstMessage *amessage = NULL;
2851 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_FAILURE)
2854 /* get our toplevel state */
2855 toplevel = BIN_IS_TOPLEVEL (bin);
2857 /* prepare an ASYNC_START message, we always post the start message even if we
2858 * are busy with a state change or when we are NO_PREROLL. */
2860 /* non toplevel bin, prepare async-start for the parent */
2861 amessage = gst_message_new_async_start (GST_OBJECT_CAST (bin));
2863 if (bin->polling || GST_STATE_PENDING (bin) != GST_STATE_VOID_PENDING)
2866 /* async starts are ignored when we are NO_PREROLL */
2867 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_NO_PREROLL)
2868 goto was_no_preroll;
2870 old_state = GST_STATE (bin);
2872 /* when we PLAYING we go back to PAUSED, when preroll happens, we go back to
2873 * PLAYING after optionally redistributing the base_time. */
2874 if (old_state > GST_STATE_PAUSED)
2875 new_state = GST_STATE_PAUSED;
2877 new_state = old_state;
2879 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
2880 "lost state of %s, new %s", gst_element_state_get_name (old_state),
2881 gst_element_state_get_name (new_state));
2883 GST_STATE (bin) = new_state;
2884 GST_STATE_NEXT (bin) = new_state;
2885 GST_STATE_PENDING (bin) = new_state;
2886 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
2887 GST_OBJECT_UNLOCK (bin);
2890 _priv_gst_element_state_changed (GST_ELEMENT_CAST (bin), new_state, new_state,
2895 /* post our ASYNC_START. */
2896 GST_DEBUG_OBJECT (bin, "posting ASYNC_START to parent");
2897 gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
2899 GST_OBJECT_LOCK (bin);
2905 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "we had an error");
2910 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "state change busy");
2911 GST_OBJECT_UNLOCK (bin);
2916 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "ignoring, we are NO_PREROLL");
2917 GST_OBJECT_UNLOCK (bin);
2922 /* this function is called when there are no more async elements in the bin. We
2923 * post a state changed message and an ASYNC_DONE message.
2924 * This function is called with the OBJECT lock.
2927 bin_handle_async_done (GstBin * bin, GstStateChangeReturn ret,
2928 gboolean flag_pending, gboolean reset_time)
2930 GstState current, pending, target;
2931 GstStateChangeReturn old_ret;
2932 GstState old_state, old_next;
2933 gboolean toplevel, state_changed = FALSE;
2934 GstMessage *amessage = NULL;
2935 BinContinueData *cont = NULL;
2937 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_FAILURE)
2940 pending = GST_STATE_PENDING (bin);
2945 /* check if there is something to commit */
2946 if (pending == GST_STATE_VOID_PENDING)
2947 goto nothing_pending;
2949 old_ret = GST_STATE_RETURN (bin);
2950 GST_STATE_RETURN (bin) = ret;
2952 /* move to the next target state */
2953 target = GST_STATE_TARGET (bin);
2954 pending = GST_STATE_PENDING (bin) = target;
2956 amessage = gst_message_new_async_done (GST_OBJECT_CAST (bin), reset_time);
2958 old_state = GST_STATE (bin);
2959 /* this is the state we should go to next */
2960 old_next = GST_STATE_NEXT (bin);
2962 if (old_next != GST_STATE_PLAYING) {
2963 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
2964 "committing state from %s to %s, old pending %s",
2965 gst_element_state_get_name (old_state),
2966 gst_element_state_get_name (old_next),
2967 gst_element_state_get_name (pending));
2969 /* update current state */
2970 current = GST_STATE (bin) = old_next;
2972 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
2973 "setting state from %s to %s, pending %s",
2974 gst_element_state_get_name (old_state),
2975 gst_element_state_get_name (old_state),
2976 gst_element_state_get_name (pending));
2977 current = old_state;
2980 /* get our toplevel state */
2981 toplevel = BIN_IS_TOPLEVEL (bin);
2983 /* see if we reached the final state. If we are not toplevel, we also have to
2984 * stop here, the parent will continue our state. */
2985 if ((pending == current) || !toplevel) {
2986 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
2987 "completed state change, pending VOID");
2989 /* mark VOID pending */
2990 pending = GST_STATE_VOID_PENDING;
2991 GST_STATE_PENDING (bin) = pending;
2992 GST_STATE_NEXT (bin) = GST_STATE_VOID_PENDING;
2994 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
2995 "continue state change, pending %s",
2996 gst_element_state_get_name (pending));
2998 cont = g_slice_new (BinContinueData);
3000 /* ref to the bin */
3001 cont->bin = gst_object_ref (bin);
3002 /* cookie to detect concurrent state change */
3003 cont->cookie = GST_ELEMENT_CAST (bin)->state_cookie;
3004 /* pending target state */
3005 cont->pending = pending;
3007 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3008 GST_STATE_NEXT (bin) = GST_STATE_GET_NEXT (old_state, pending);
3011 if (old_next != GST_STATE_PLAYING) {
3012 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC) {
3013 state_changed = TRUE;
3016 GST_OBJECT_UNLOCK (bin);
3018 if (state_changed) {
3019 _priv_gst_element_state_changed (GST_ELEMENT_CAST (bin), old_state,
3023 /* post our combined ASYNC_DONE when all is ASYNC_DONE. */
3024 GST_DEBUG_OBJECT (bin, "posting ASYNC_DONE to parent");
3025 gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
3028 GST_OBJECT_LOCK (bin);
3030 /* toplevel, start continue state */
3031 GST_DEBUG_OBJECT (bin, "all async-done, starting state continue");
3032 bin_push_state_continue (cont);
3034 GST_DEBUG_OBJECT (bin, "state change complete");
3035 GST_STATE_BROADCAST (bin);
3041 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "we had an error");
3046 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "state change busy");
3047 /* if we were busy with a state change and we are requested to flag a
3048 * pending async done, we do so here */
3050 bin->priv->pending_async_done = TRUE;
3055 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin, "nothing pending");
3061 bin_do_eos (GstBin * bin)
3066 GST_OBJECT_LOCK (bin);
3067 /* If all sinks are EOS, we're in PLAYING and no state change is pending
3068 * we forward the EOS message to the parent bin or application
3070 eos = GST_STATE (bin) == GST_STATE_PLAYING
3071 && GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING
3072 && is_eos (bin, &seqnum);
3073 GST_OBJECT_UNLOCK (bin);
3076 && g_atomic_int_compare_and_exchange (&bin->priv->posted_eos, FALSE,
3078 GstMessage *tmessage;
3079 tmessage = gst_message_new_eos (GST_OBJECT_CAST (bin));
3080 gst_message_set_seqnum (tmessage, seqnum);
3081 GST_DEBUG_OBJECT (bin,
3082 "all sinks posted EOS, posting seqnum #%" G_GUINT32_FORMAT, seqnum);
3083 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3087 /* must be called with the object lock. This function releases the lock to post
3090 bin_do_message_forward (GstBin * bin, GstMessage * message)
3092 if (bin->priv->message_forward) {
3093 GstMessage *forwarded;
3095 GST_DEBUG_OBJECT (bin, "pass %s message upward",
3096 GST_MESSAGE_TYPE_NAME (message));
3097 GST_OBJECT_UNLOCK (bin);
3099 /* we need to convert these messages to element messages so that our parent
3100 * bin can easily ignore them and so that the application can easily
3101 * distinguish between the internally forwarded and the real messages. */
3102 forwarded = gst_message_new_element (GST_OBJECT_CAST (bin),
3103 gst_structure_new ("GstBinForwarded",
3104 "message", GST_TYPE_MESSAGE, message, NULL));
3106 gst_element_post_message (GST_ELEMENT_CAST (bin), forwarded);
3108 GST_OBJECT_LOCK (bin);
3112 /* handle child messages:
3114 * This method is called synchronously when a child posts a message on
3117 * GST_MESSAGE_EOS: This message is only posted by sinks
3118 * in the PLAYING state. If all sinks posted the EOS message, post
3121 * GST_MESSAGE_STATE_DIRTY: Deprecated
3123 * GST_MESSAGE_SEGMENT_START: just collect, never forward upwards. If an
3124 * element posts segment_start twice, only the last message is kept.
3126 * GST_MESSAGE_SEGMENT_DONE: replace SEGMENT_START message from same poster
3127 * with the segment_done message. If there are no more segment_start
3128 * messages, post segment_done message upwards.
3130 * GST_MESSAGE_DURATION: remove all previously cached duration messages.
3131 * Whenever someone performs a duration query on the bin, we store the
3132 * result so we can answer it quicker the next time. Any element that
3133 * changes its duration marks our cached values invalid.
3134 * This message is also posted upwards. This is currently disabled
3135 * because too many elements don't post DURATION messages when the
3138 * GST_MESSAGE_CLOCK_LOST: This message is posted by an element when it
3139 * can no longer provide a clock. The default bin behaviour is to
3140 * check if the lost clock was the one provided by the bin. If so and
3141 * we are currently in the PLAYING state, we forward the message to
3143 * This message is also generated when we remove a clock provider from
3144 * a bin. If this message is received by the application, it should
3145 * PAUSE the pipeline and set it back to PLAYING to force a new clock
3146 * and a new base_time distribution.
3148 * GST_MESSAGE_CLOCK_PROVIDE: This message is generated when an element
3149 * can provide a clock. This mostly happens when we add a new clock
3150 * provider to the bin. The default behaviour of the bin is to mark the
3151 * currently selected clock as dirty, which will perform a clock
3152 * recalculation the next time we are asked to provide a clock.
3153 * This message is never sent to the application but is forwarded to
3156 * GST_MESSAGE_ASYNC_START: Create an internal ELEMENT message that stores
3157 * the state of the element and the fact that the element will need a
3158 * new base_time. This message is not forwarded to the application.
3160 * GST_MESSAGE_ASYNC_DONE: Find the internal ELEMENT message we kept for the
3161 * element when it posted ASYNC_START. If all elements are done, post a
3162 * ASYNC_DONE message to the parent.
3164 * OTHER: post upwards.
3167 gst_bin_handle_message_func (GstBin * bin, GstMessage * message)
3170 GstMessageType type;
3171 GstMessage *tmessage;
3174 src = GST_MESSAGE_SRC (message);
3175 type = GST_MESSAGE_TYPE (message);
3177 GST_DEBUG_OBJECT (bin, "[msg %p] handling child %s message of type %s",
3178 message, src ? GST_ELEMENT_NAME (src) : "(NULL)",
3179 GST_MESSAGE_TYPE_NAME (message));
3182 case GST_MESSAGE_ERROR:
3184 GST_OBJECT_LOCK (bin);
3186 GST_DEBUG_OBJECT (bin, "got ERROR message, unlocking state change");
3187 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_FAILURE;
3188 GST_STATE_BROADCAST (bin);
3189 GST_OBJECT_UNLOCK (bin);
3193 case GST_MESSAGE_EOS:
3196 /* collect all eos messages from the children */
3197 GST_OBJECT_LOCK (bin);
3198 bin_do_message_forward (bin, message);
3199 /* ref message for future use */
3200 bin_replace_message (bin, message, GST_MESSAGE_EOS);
3201 GST_OBJECT_UNLOCK (bin);
3206 case GST_MESSAGE_STATE_DIRTY:
3208 GST_WARNING_OBJECT (bin, "received deprecated STATE_DIRTY message");
3211 gst_message_unref (message);
3214 case GST_MESSAGE_SEGMENT_START:{
3215 gboolean post = FALSE;
3219 gst_message_parse_segment_start (message, &format, &position);
3220 seqnum = gst_message_get_seqnum (message);
3222 GST_OBJECT_LOCK (bin);
3223 bin_do_message_forward (bin, message);
3224 /* if this is the first segment-start, post to parent but not to the
3226 if (!find_message (bin, NULL, GST_MESSAGE_SEGMENT_START) &&
3227 (GST_OBJECT_PARENT (bin) != NULL)) {
3230 /* replace any previous segment_start message from this source
3231 * with the new segment start message */
3232 bin_replace_message (bin, message, GST_MESSAGE_SEGMENT_START);
3233 GST_OBJECT_UNLOCK (bin);
3235 tmessage = gst_message_new_segment_start (GST_OBJECT_CAST (bin),
3237 gst_message_set_seqnum (tmessage, seqnum);
3239 /* post segment start with initial format and position. */
3240 GST_DEBUG_OBJECT (bin, "posting SEGMENT_START (%u) bus message: %p",
3242 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3246 case GST_MESSAGE_SEGMENT_DONE:
3248 gboolean post = FALSE;
3252 gst_message_parse_segment_done (message, &format, &position);
3253 seqnum = gst_message_get_seqnum (message);
3255 GST_OBJECT_LOCK (bin);
3256 bin_do_message_forward (bin, message);
3257 bin_replace_message (bin, message, GST_MESSAGE_SEGMENT_START);
3258 /* if there are no more segment_start messages, everybody posted
3259 * a segment_done and we can post one on the bus. */
3261 /* we don't care who still has a pending segment start */
3262 if (!find_message (bin, NULL, GST_MESSAGE_SEGMENT_START)) {
3265 /* remove all old segment_done messages */
3266 bin_remove_messages (bin, NULL, GST_MESSAGE_SEGMENT_DONE);
3268 GST_OBJECT_UNLOCK (bin);
3270 tmessage = gst_message_new_segment_done (GST_OBJECT_CAST (bin),
3272 gst_message_set_seqnum (tmessage, seqnum);
3274 /* post segment done with latest format and position. */
3275 GST_DEBUG_OBJECT (bin, "posting SEGMENT_DONE (%u) bus message: %p",
3277 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3281 case GST_MESSAGE_DURATION:
3283 /* remove all cached duration messages, next time somebody asks
3284 * for duration, we will recalculate. */
3285 GST_OBJECT_LOCK (bin);
3286 bin_remove_messages (bin, NULL, GST_MESSAGE_DURATION);
3287 GST_OBJECT_UNLOCK (bin);
3290 case GST_MESSAGE_CLOCK_LOST:
3292 GstClock **provided_clock_p;
3293 GstElement **clock_provider_p;
3294 gboolean playing, provided, forward;
3297 gst_message_parse_clock_lost (message, &clock);
3299 GST_OBJECT_LOCK (bin);
3300 bin->clock_dirty = TRUE;
3301 /* if we lost the clock that we provided, post to parent but
3302 * only if we are PLAYING. */
3303 provided = (clock == bin->provided_clock);
3304 playing = (GST_STATE (bin) == GST_STATE_PLAYING);
3305 forward = playing & provided;
3307 GST_DEBUG_OBJECT (bin,
3308 "Lost clock %" GST_PTR_FORMAT " provided by %" GST_PTR_FORMAT,
3309 bin->provided_clock, bin->clock_provider);
3310 provided_clock_p = &bin->provided_clock;
3311 clock_provider_p = &bin->clock_provider;
3312 gst_object_replace ((GstObject **) provided_clock_p, NULL);
3313 gst_object_replace ((GstObject **) clock_provider_p, NULL);
3315 GST_DEBUG_OBJECT (bin, "provided %d, playing %d, forward %d",
3316 provided, playing, forward);
3317 GST_OBJECT_UNLOCK (bin);
3323 gst_message_unref (message);
3326 case GST_MESSAGE_CLOCK_PROVIDE:
3330 GST_OBJECT_LOCK (bin);
3331 bin->clock_dirty = TRUE;
3332 /* a new clock is available, post to parent but not
3333 * to the application */
3334 forward = GST_OBJECT_PARENT (bin) != NULL;
3335 GST_OBJECT_UNLOCK (bin);
3341 gst_message_unref (message);
3344 case GST_MESSAGE_ASYNC_START:
3348 GST_DEBUG_OBJECT (bin, "ASYNC_START message %p, %s", message,
3349 src ? GST_OBJECT_NAME (src) : "(NULL)");
3351 GST_OBJECT_LOCK (bin);
3352 bin_do_message_forward (bin, message);
3354 /* we ignore the message if we are going to <= READY */
3355 if ((target = GST_STATE_TARGET (bin)) <= GST_STATE_READY)
3356 goto ignore_start_message;
3358 /* takes ownership of the message */
3359 bin_replace_message (bin, message, GST_MESSAGE_ASYNC_START);
3361 bin_handle_async_start (bin);
3362 GST_OBJECT_UNLOCK (bin);
3365 ignore_start_message:
3367 GST_DEBUG_OBJECT (bin, "ignoring message, target %s",
3368 gst_element_state_get_name (target));
3369 GST_OBJECT_UNLOCK (bin);
3370 gst_message_unref (message);
3374 case GST_MESSAGE_ASYNC_DONE:
3376 gboolean reset_time;
3379 GST_DEBUG_OBJECT (bin, "ASYNC_DONE message %p, %s", message,
3380 src ? GST_OBJECT_NAME (src) : "(NULL)");
3382 gst_message_parse_async_done (message, &reset_time);
3384 GST_OBJECT_LOCK (bin);
3385 bin_do_message_forward (bin, message);
3387 /* ignore messages if we are shutting down */
3388 if ((target = GST_STATE_TARGET (bin)) <= GST_STATE_READY)
3389 goto ignore_done_message;
3391 bin_replace_message (bin, message, GST_MESSAGE_ASYNC_START);
3392 /* if there are no more ASYNC_START messages, everybody posted
3393 * a ASYNC_DONE and we can post one on the bus. When checking, we
3394 * don't care who still has a pending ASYNC_START */
3395 if (!find_message (bin, NULL, GST_MESSAGE_ASYNC_START)) {
3396 /* nothing found, remove all old ASYNC_DONE messages */
3397 bin_remove_messages (bin, NULL, GST_MESSAGE_ASYNC_DONE);
3399 GST_DEBUG_OBJECT (bin, "async elements commited");
3400 /* when we get an async done message when a state change was busy, we
3401 * need to set the pending_done flag so that at the end of the state
3402 * change we can see if we need to verify pending async elements, hence
3403 * the TRUE argument here. */
3404 bin_handle_async_done (bin, GST_STATE_CHANGE_SUCCESS, TRUE, reset_time);
3406 GST_DEBUG_OBJECT (bin, "there are more async elements pending");
3408 GST_OBJECT_UNLOCK (bin);
3411 ignore_done_message:
3413 GST_DEBUG_OBJECT (bin, "ignoring message, target %s",
3414 gst_element_state_get_name (target));
3415 GST_OBJECT_UNLOCK (bin);
3416 gst_message_unref (message);
3420 case GST_MESSAGE_STRUCTURE_CHANGE:
3424 gst_message_parse_structure_change (message, NULL, NULL, &busy);
3426 GST_OBJECT_LOCK (bin);
3428 /* while the pad is busy, avoid following it when doing state changes.
3429 * Don't update the cookie yet, we will do that after the structure
3430 * change finished and we are ready to inspect the new updated
3432 bin_replace_message (bin, message, GST_MESSAGE_STRUCTURE_CHANGE);
3435 /* a pad link/unlink ended, signal the state change iterator that we
3436 * need to resync by updating the structure_cookie. */
3437 bin_remove_messages (bin, GST_MESSAGE_SRC (message),
3438 GST_MESSAGE_STRUCTURE_CHANGE);
3439 bin->priv->structure_cookie++;
3441 GST_OBJECT_UNLOCK (bin);
3444 gst_message_unref (message);
3455 /* Send all other messages upward */
3456 GST_DEBUG_OBJECT (bin, "posting message upward");
3457 gst_element_post_message (GST_ELEMENT_CAST (bin), message);
3462 /* generic struct passed to all query fold methods */
3471 typedef void (*QueryInitFunction) (GstBin * bin, QueryFold * fold);
3472 typedef void (*QueryDoneFunction) (GstBin * bin, QueryFold * fold);
3474 /* for duration/position we collect all durations/positions and take
3475 * the MAX of all valid results */
3477 bin_query_min_max_init (GstBin * bin, QueryFold * fold)
3485 bin_query_duration_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
3487 GstElement *item = g_value_get_object (vitem);
3489 if (gst_element_query (item, fold->query)) {
3492 g_value_set_boolean (ret, TRUE);
3494 gst_query_parse_duration (fold->query, NULL, &duration);
3496 GST_DEBUG_OBJECT (item, "got duration %" G_GINT64_FORMAT, duration);
3498 if (duration > fold->max)
3499 fold->max = duration;
3506 bin_query_duration_done (GstBin * bin, QueryFold * fold)
3510 gst_query_parse_duration (fold->query, &format, NULL);
3511 /* store max in query result */
3512 gst_query_set_duration (fold->query, format, fold->max);
3514 GST_DEBUG_OBJECT (bin, "max duration %" G_GINT64_FORMAT, fold->max);
3517 GST_OBJECT_LOCK (bin);
3518 bin->messages = g_list_prepend (bin->messages,
3519 gst_message_new_duration (GST_OBJECT_CAST (bin), format, fold->max));
3520 GST_OBJECT_UNLOCK (bin);
3524 bin_query_position_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
3526 GstElement *item = g_value_get_object (vitem);
3528 if (gst_element_query (item, fold->query)) {
3531 g_value_set_boolean (ret, TRUE);
3533 gst_query_parse_position (fold->query, NULL, &position);
3535 GST_DEBUG_OBJECT (item, "got position %" G_GINT64_FORMAT, position);
3537 if (position > fold->max)
3538 fold->max = position;
3545 bin_query_position_done (GstBin * bin, QueryFold * fold)
3549 gst_query_parse_position (fold->query, &format, NULL);
3550 /* store max in query result */
3551 gst_query_set_position (fold->query, format, fold->max);
3553 GST_DEBUG_OBJECT (bin, "max position %" G_GINT64_FORMAT, fold->max);
3557 bin_query_latency_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
3559 GstElement *item = g_value_get_object (vitem);
3561 if (gst_element_query (item, fold->query)) {
3562 GstClockTime min, max;
3565 gst_query_parse_latency (fold->query, &live, &min, &max);
3567 GST_DEBUG_OBJECT (item,
3568 "got latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
3569 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
3571 /* for the combined latency we collect the MAX of all min latencies and
3572 * the MIN of all max latencies */
3574 if (min > fold->min)
3576 if (fold->max == -1)
3578 else if (max < fold->max)
3580 if (fold->live == FALSE)
3584 g_value_set_boolean (ret, FALSE);
3585 GST_DEBUG_OBJECT (item, "failed query");
3592 bin_query_latency_done (GstBin * bin, QueryFold * fold)
3594 /* store max in query result */
3595 gst_query_set_latency (fold->query, fold->live, fold->min, fold->max);
3597 GST_DEBUG_OBJECT (bin,
3598 "latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
3599 ", live %d", GST_TIME_ARGS (fold->min), GST_TIME_ARGS (fold->max),
3603 /* generic fold, return first valid result */
3605 bin_query_generic_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
3607 GstElement *item = g_value_get_object (vitem);
3610 if ((res = gst_element_query (item, fold->query))) {
3611 g_value_set_boolean (ret, TRUE);
3612 GST_DEBUG_OBJECT (item, "answered query %p", fold->query);
3615 /* and stop as soon as we have a valid result */
3620 gst_bin_query (GstElement * element, GstQuery * query)
3622 GstBin *bin = GST_BIN_CAST (element);
3624 gboolean res = FALSE;
3625 GstIteratorFoldFunction fold_func;
3626 QueryInitFunction fold_init = NULL;
3627 QueryDoneFunction fold_done = NULL;
3628 QueryFold fold_data;
3631 switch (GST_QUERY_TYPE (query)) {
3632 case GST_QUERY_DURATION:
3637 gst_query_parse_duration (query, &qformat, NULL);
3639 /* find cached duration query */
3640 GST_OBJECT_LOCK (bin);
3641 for (cached = bin->messages; cached; cached = g_list_next (cached)) {
3642 GstMessage *message = (GstMessage *) cached->data;
3644 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_DURATION &&
3645 GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (bin)) {
3649 gst_message_parse_duration (message, &format, &duration);
3651 /* if cached same format, copy duration in query result */
3652 if (format == qformat) {
3653 GST_DEBUG_OBJECT (bin, "return cached duration %" G_GINT64_FORMAT,
3655 GST_OBJECT_UNLOCK (bin);
3657 gst_query_set_duration (query, qformat, duration);
3663 GST_OBJECT_UNLOCK (bin);
3664 /* no cached value found, iterate and collect durations */
3665 fold_func = (GstIteratorFoldFunction) bin_query_duration_fold;
3666 fold_init = bin_query_min_max_init;
3667 fold_done = bin_query_duration_done;
3670 case GST_QUERY_POSITION:
3672 fold_func = (GstIteratorFoldFunction) bin_query_position_fold;
3673 fold_init = bin_query_min_max_init;
3674 fold_done = bin_query_position_done;
3677 case GST_QUERY_LATENCY:
3679 fold_func = (GstIteratorFoldFunction) bin_query_latency_fold;
3680 fold_init = bin_query_min_max_init;
3681 fold_done = bin_query_latency_done;
3686 fold_func = (GstIteratorFoldFunction) bin_query_generic_fold;
3690 fold_data.query = query;
3692 /* set the result of the query to FALSE initially */
3693 g_value_init (&ret, G_TYPE_BOOLEAN);
3694 g_value_set_boolean (&ret, res);
3696 iter = gst_bin_iterate_sinks (bin);
3697 GST_DEBUG_OBJECT (bin, "Sending query %p (type %s) to sink children",
3698 query, GST_QUERY_TYPE_NAME (query));
3701 fold_init (bin, &fold_data);
3704 GstIteratorResult ires;
3706 ires = gst_iterator_fold (iter, fold_func, &ret, &fold_data);
3709 case GST_ITERATOR_RESYNC:
3710 gst_iterator_resync (iter);
3712 fold_init (bin, &fold_data);
3713 g_value_set_boolean (&ret, res);
3715 case GST_ITERATOR_OK:
3716 case GST_ITERATOR_DONE:
3717 res = g_value_get_boolean (&ret);
3718 if (fold_done != NULL && res)
3719 fold_done (bin, &fold_data);
3727 gst_iterator_free (iter);
3730 GST_DEBUG_OBJECT (bin, "query %p result %d", query, res);
3736 compare_name (const GValue * velement, const gchar * name)
3739 GstElement *element = g_value_get_object (velement);
3741 GST_OBJECT_LOCK (element);
3742 eq = strcmp (GST_ELEMENT_NAME (element), name);
3743 GST_OBJECT_UNLOCK (element);
3749 * gst_bin_get_by_name:
3751 * @name: the element name to search for
3753 * Gets the element with the given name from a bin. This
3754 * function recurses into child bins.
3756 * Returns NULL if no element with the given name is found in the bin.
3758 * MT safe. Caller owns returned reference.
3760 * Returns: (transfer full): the #GstElement with the given name, or NULL
3763 gst_bin_get_by_name (GstBin * bin, const gchar * name)
3765 GstIterator *children;
3766 GValue result = { 0, };
3767 GstElement *element;
3770 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
3772 GST_CAT_INFO (GST_CAT_PARENTAGE, "[%s]: looking up child element %s",
3773 GST_ELEMENT_NAME (bin), name);
3775 children = gst_bin_iterate_recurse (bin);
3776 found = gst_iterator_find_custom (children,
3777 (GCompareFunc) compare_name, &result, (gpointer) name);
3778 gst_iterator_free (children);
3781 element = g_value_dup_object (&result);
3782 g_value_unset (&result);
3791 * gst_bin_get_by_name_recurse_up:
3793 * @name: the element name to search for
3795 * Gets the element with the given name from this bin. If the
3796 * element is not found, a recursion is performed on the parent bin.
3799 * - no element with the given name is found in the bin
3801 * MT safe. Caller owns returned reference.
3803 * Returns: (transfer full): the #GstElement with the given name, or NULL
3806 gst_bin_get_by_name_recurse_up (GstBin * bin, const gchar * name)
3810 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
3811 g_return_val_if_fail (name != NULL, NULL);
3813 result = gst_bin_get_by_name (bin, name);
3818 parent = gst_object_get_parent (GST_OBJECT_CAST (bin));
3820 if (GST_IS_BIN (parent)) {
3821 result = gst_bin_get_by_name_recurse_up (GST_BIN_CAST (parent), name);
3823 gst_object_unref (parent);
3831 compare_interface (const GValue * velement, GValue * interface)
3833 GstElement *element = g_value_get_object (velement);
3834 GType interface_type = (GType) g_value_get_pointer (interface);
3837 if (G_TYPE_CHECK_INSTANCE_TYPE (element, interface_type)) {
3846 * gst_bin_get_by_interface:
3848 * @iface: the #GType of an interface
3850 * Looks for an element inside the bin that implements the given
3851 * interface. If such an element is found, it returns the element.
3852 * You can cast this element to the given interface afterwards. If you want
3853 * all elements that implement the interface, use
3854 * gst_bin_iterate_all_by_interface(). This function recurses into child bins.
3856 * MT safe. Caller owns returned reference.
3858 * Returns: (transfer full): A #GstElement inside the bin implementing the interface
3861 gst_bin_get_by_interface (GstBin * bin, GType iface)
3863 GstIterator *children;
3864 GValue result = { 0, };
3865 GstElement *element;
3867 GValue viface = { 0, };
3869 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
3870 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface), NULL);
3872 g_value_init (&viface, G_TYPE_POINTER);
3873 g_value_set_pointer (&viface, (gpointer) iface);
3875 children = gst_bin_iterate_recurse (bin);
3876 found = gst_iterator_find_custom (children, (GCompareFunc) compare_interface,
3878 gst_iterator_free (children);
3881 element = g_value_dup_object (&result);
3882 g_value_unset (&result);
3886 g_value_unset (&viface);
3892 * gst_bin_iterate_all_by_interface:
3894 * @iface: the #GType of an interface
3896 * Looks for all elements inside the bin that implements the given
3897 * interface. You can safely cast all returned elements to the given interface.
3898 * The function recurses inside child bins. The iterator will yield a series
3899 * of #GstElement that should be unreffed after use.
3901 * Each element yielded by the iterator will have its refcount increased, so
3904 * MT safe. Caller owns returned value.
3906 * Returns: (transfer full): a #GstIterator of #GstElement for all elements
3907 * in the bin implementing the given interface, or NULL
3910 gst_bin_iterate_all_by_interface (GstBin * bin, GType iface)
3912 GstIterator *children;
3913 GstIterator *result;
3914 GValue viface = { 0, };
3916 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
3917 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface), NULL);
3919 g_value_init (&viface, G_TYPE_POINTER);
3920 g_value_set_pointer (&viface, (gpointer) iface);
3922 children = gst_bin_iterate_recurse (bin);
3923 result = gst_iterator_filter (children, (GCompareFunc) compare_interface,
3926 g_value_unset (&viface);