3 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
4 * 2004 Wim Taymans <wim.taymans@gmail.com>
6 * gstbin.c: GstBin container object and support code
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
28 * @short_description: Base class and element that can contain other elements
30 * #GstBin is an element that can contain other #GstElement, allowing them to be
32 * Pads from the child elements can be ghosted to the bin, see #GstGhostPad.
33 * This makes the bin look like any other elements and enables creation of
34 * higher-level abstraction elements.
36 * A new #GstBin is created with gst_bin_new(). Use a #GstPipeline instead if you
37 * want to create a toplevel bin because a normal bin doesn't have a bus or
38 * handle clock distribution of its own.
40 * After the bin has been created you will typically add elements to it with
41 * gst_bin_add(). You can remove elements with gst_bin_remove().
43 * An element can be retrieved from a bin with gst_bin_get_by_name(), using the
44 * elements name. gst_bin_get_by_name_recurse_up() is mainly used for internal
45 * purposes and will query the parent bins when the element is not found in the
48 * An iterator of elements in a bin can be retrieved with
49 * gst_bin_iterate_elements(). Various other iterators exist to retrieve the
52 * gst_object_unref() is used to drop your reference to the bin.
54 * The #GstBin::element-added signal is fired whenever a new element is added to
55 * the bin. Likewise the #GstBin::element-removed signal is fired whenever an
56 * element is removed from the bin.
58 * <refsect2><title>Notes</title>
60 * A #GstBin internally intercepts every #GstMessage posted by its children and
61 * implements the following default behaviour for each of them:
64 * <term>GST_MESSAGE_EOS</term>
65 * <listitem><para>This message is only posted by sinks in the PLAYING
66 * state. If all sinks posted the EOS message, this bin will post and EOS
67 * message upwards.</para></listitem>
70 * <term>GST_MESSAGE_SEGMENT_START</term>
71 * <listitem><para>just collected and never forwarded upwards.
72 * The messages are used to decide when all elements have completed playback
73 * of their segment.</para></listitem>
76 * <term>GST_MESSAGE_SEGMENT_DONE</term>
77 * <listitem><para> Is posted by #GstBin when all elements that posted
78 * a SEGMENT_START have posted a SEGMENT_DONE.</para></listitem>
81 * <term>GST_MESSAGE_DURATION_CHANGED</term>
82 * <listitem><para> Is posted by an element that detected a change
83 * in the stream duration. The default bin behaviour is to clear any
84 * cached duration values so that the next duration query will perform
85 * a full duration recalculation. The duration change is posted to the
86 * application so that it can refetch the new duration with a duration
87 * query. Note that these messages can be posted before the bin is
88 * prerolled, in which case the duration query might fail.
92 * <term>GST_MESSAGE_CLOCK_LOST</term>
93 * <listitem><para> This message is posted by an element when it
94 * can no longer provide a clock. The default bin behaviour is to
95 * check if the lost clock was the one provided by the bin. If so and
96 * the bin is currently in the PLAYING state, the message is forwarded to
98 * This message is also generated when a clock provider is removed from
99 * the bin. If this message is received by the application, it should
100 * PAUSE the pipeline and set it back to PLAYING to force a new clock
105 * <term>GST_MESSAGE_CLOCK_PROVIDE</term>
106 * <listitem><para> This message is generated when an element
107 * can provide a clock. This mostly happens when a new clock
108 * provider is added to the bin. The default behaviour of the bin is to
109 * mark the currently selected clock as dirty, which will perform a clock
110 * recalculation the next time the bin is asked to provide a clock.
111 * This message is never sent tot the application but is forwarded to
112 * the parent of the bin.
116 * <term>OTHERS</term>
117 * <listitem><para> posted upwards.</para></listitem>
122 * A #GstBin implements the following default behaviour for answering to a
126 * <term>GST_QUERY_DURATION</term>
127 * <listitem><para>If the query has been asked before with the same format
128 * and the bin is a toplevel bin (ie. has no parent),
129 * use the cached previous value. If no previous value was cached, the
130 * query is sent to all sink elements in the bin and the MAXIMUM of all
131 * values is returned. If the bin is a toplevel bin the value is cached.
132 * If no sinks are available in the bin, the query fails.
136 * <term>GST_QUERY_POSITION</term>
137 * <listitem><para>The query is sent to all sink elements in the bin and the
138 * MAXIMUM of all values is returned. If no sinks are available in the bin,
143 * <term>OTHERS</term>
144 * <listitem><para>the query is forwarded to all sink elements, the result
145 * of the first sink that answers the query successfully is returned. If no
146 * sink is in the bin, the query fails.</para></listitem>
150 * A #GstBin will by default forward any event sent to it to all sink elements.
151 * If all the sinks return TRUE, the bin will also return TRUE, else FALSE is
152 * returned. If no sinks are in the bin, the event handler will return TRUE.
157 * Last reviewed on 2012-03-28 (0.11.3)
160 #include "gst_private.h"
162 #include "gstevent.h"
165 #include "gsterror.h"
167 #include "gstutils.h"
168 #include "gstchildproxy.h"
170 GST_DEBUG_CATEGORY_STATIC (bin_debug);
171 #define GST_CAT_DEFAULT bin_debug
173 /* a bin is toplevel if it has no parent or when it is configured to behave like
175 #define BIN_IS_TOPLEVEL(bin) ((GST_OBJECT_PARENT (bin) == NULL) || bin->priv->asynchandling)
177 #define GST_BIN_GET_PRIVATE(obj) \
178 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BIN, GstBinPrivate))
180 struct _GstBinPrivate
182 gboolean asynchandling;
183 /* if we get an ASYNC_DONE message from ourselves, this means that the
184 * subclass will simulate ASYNC behaviour without having ASYNC children. When
185 * such an ASYNC_DONE message is posted while we are doing a state change, we
186 * have to process the message after finishing the state change even when no
187 * child returned GST_STATE_CHANGE_ASYNC. */
188 gboolean pending_async_done;
190 guint32 structure_cookie;
197 /* forward messages from our children */
198 gboolean message_forward;
212 static void gst_bin_dispose (GObject * object);
214 static void gst_bin_set_property (GObject * object, guint prop_id,
215 const GValue * value, GParamSpec * pspec);
216 static void gst_bin_get_property (GObject * object, guint prop_id,
217 GValue * value, GParamSpec * pspec);
219 static GstStateChangeReturn gst_bin_change_state_func (GstElement * element,
220 GstStateChange transition);
221 static void gst_bin_state_changed (GstElement * element, GstState oldstate,
222 GstState newstate, GstState pending);
223 static GstStateChangeReturn gst_bin_get_state_func (GstElement * element,
224 GstState * state, GstState * pending, GstClockTime timeout);
225 static void bin_handle_async_done (GstBin * bin, GstStateChangeReturn ret,
226 gboolean flag_pending, GstClockTime running_time);
227 static void bin_handle_async_start (GstBin * bin);
228 static void bin_push_state_continue (BinContinueData * data);
229 static void bin_do_eos (GstBin * bin);
231 static gboolean gst_bin_add_func (GstBin * bin, GstElement * element);
232 static gboolean gst_bin_remove_func (GstBin * bin, GstElement * element);
235 static void gst_bin_set_index_func (GstElement * element, GstIndex * index);
236 static GstIndex *gst_bin_get_index_func (GstElement * element);
239 static GstClock *gst_bin_provide_clock_func (GstElement * element);
240 static gboolean gst_bin_set_clock_func (GstElement * element, GstClock * clock);
242 static void gst_bin_handle_message_func (GstBin * bin, GstMessage * message);
243 static gboolean gst_bin_send_event (GstElement * element, GstEvent * event);
244 static GstBusSyncReply bin_bus_handler (GstBus * bus,
245 GstMessage * message, GstBin * bin);
246 static gboolean gst_bin_query (GstElement * element, GstQuery * query);
247 static void gst_bin_set_context (GstElement * element, GstContext * context);
249 static gboolean gst_bin_do_latency_func (GstBin * bin);
251 static void bin_remove_messages (GstBin * bin, GstObject * src,
252 GstMessageType types);
253 static void gst_bin_continue_func (BinContinueData * data);
254 static gint bin_element_is_sink (GstElement * child, GstBin * bin);
255 static gint bin_element_is_src (GstElement * child, GstBin * bin);
257 static GstIterator *gst_bin_sort_iterator_new (GstBin * bin);
259 /* Bin signals and properties */
268 #define DEFAULT_ASYNC_HANDLING FALSE
269 #define DEFAULT_MESSAGE_FORWARD FALSE
275 PROP_MESSAGE_FORWARD,
279 static void gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data);
281 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
285 static const GInterfaceInfo iface_info = { \
286 gst_bin_child_proxy_init, \
290 g_type_add_interface_static (g_define_type_id, GST_TYPE_CHILD_PROXY, &iface_info); \
292 GST_DEBUG_CATEGORY_INIT (bin_debug, "bin", GST_DEBUG_BOLD, \
293 "debugging info for the 'bin' container element"); \
297 #define gst_bin_parent_class parent_class
298 G_DEFINE_TYPE_WITH_CODE (GstBin, gst_bin, GST_TYPE_ELEMENT, _do_init);
301 gst_bin_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
307 bin = GST_BIN_CAST (child_proxy);
309 GST_OBJECT_LOCK (bin);
310 if ((res = g_list_nth_data (bin->children, index)))
311 gst_object_ref (res);
312 GST_OBJECT_UNLOCK (bin);
314 return (GObject *) res;
318 gst_bin_child_proxy_get_children_count (GstChildProxy * child_proxy)
323 bin = GST_BIN_CAST (child_proxy);
325 GST_OBJECT_LOCK (bin);
326 num = bin->numchildren;
327 GST_OBJECT_UNLOCK (bin);
333 gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data)
335 GstChildProxyInterface *iface = g_iface;
337 iface->get_children_count = gst_bin_child_proxy_get_children_count;
338 iface->get_child_by_index = gst_bin_child_proxy_get_child_by_index;
342 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
343 GValue * return_accu, const GValue * handler_return, gpointer dummy)
347 myboolean = g_value_get_boolean (handler_return);
348 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
349 g_value_set_boolean (return_accu, myboolean);
351 GST_DEBUG ("invocation %d, %d", ihint->run_type, myboolean);
358 gst_bin_class_init (GstBinClass * klass)
360 GObjectClass *gobject_class;
361 GstElementClass *gstelement_class;
364 gobject_class = (GObjectClass *) klass;
365 gstelement_class = (GstElementClass *) klass;
367 g_type_class_add_private (klass, sizeof (GstBinPrivate));
369 gobject_class->set_property = gst_bin_set_property;
370 gobject_class->get_property = gst_bin_get_property;
373 * GstBin:async-handling:
375 * If set to #TRUE, the bin will handle asynchronous state changes.
376 * This should be used only if the bin subclass is modifying the state
377 * of its children on its own.
379 g_object_class_install_property (gobject_class, PROP_ASYNC_HANDLING,
380 g_param_spec_boolean ("async-handling", "Async Handling",
381 "The bin will handle Asynchronous state changes",
382 DEFAULT_ASYNC_HANDLING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
385 * GstBin::element-added:
387 * @element: the #GstElement that was added to the bin
389 * Will be emitted after the element was added to the bin.
391 gst_bin_signals[ELEMENT_ADDED] =
392 g_signal_new ("element-added", G_TYPE_FROM_CLASS (klass),
393 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_added), NULL,
394 NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
396 * GstBin::element-removed:
398 * @element: the #GstElement that was removed from the bin
400 * Will be emitted after the element was removed from the bin.
402 gst_bin_signals[ELEMENT_REMOVED] =
403 g_signal_new ("element-removed", G_TYPE_FROM_CLASS (klass),
404 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_removed), NULL,
405 NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
407 * GstBin::do-latency:
410 * Will be emitted when the bin needs to perform latency calculations. This
411 * signal is only emited for toplevel bins or when async-handling is
414 * Only one signal handler is invoked. If no signals are connected, the
415 * default handler is invoked, which will query and distribute the lowest
416 * possible latency to all sinks.
418 * Connect to this signal if the default latency calculations are not
419 * sufficient, like when you need different latencies for different sinks in
422 gst_bin_signals[DO_LATENCY] =
423 g_signal_new ("do-latency", G_TYPE_FROM_CLASS (klass),
424 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstBinClass, do_latency),
425 _gst_boolean_accumulator, NULL, g_cclosure_marshal_generic,
426 G_TYPE_BOOLEAN, 0, G_TYPE_NONE);
429 * GstBin:message-forward:
431 * Forward all children messages, even those that would normally be filtered by
432 * the bin. This can be interesting when one wants to be notified of the EOS
433 * state of individual elements, for example.
435 * The messages are converted to an ELEMENT message with the bin as the
436 * source. The structure of the message is named 'GstBinForwarded' and contains
437 * a field named 'message' of type GST_TYPE_MESSAGE that contains the original
440 g_object_class_install_property (gobject_class, PROP_MESSAGE_FORWARD,
441 g_param_spec_boolean ("message-forward", "Message Forward",
442 "Forwards all children messages",
443 DEFAULT_MESSAGE_FORWARD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
445 gobject_class->dispose = gst_bin_dispose;
447 gst_element_class_set_static_metadata (gstelement_class, "Generic bin",
449 "Simple container object",
450 "Erik Walthinsen <omega@cse.ogi.edu>,"
451 "Wim Taymans <wim.taymans@gmail.com>");
453 gstelement_class->change_state =
454 GST_DEBUG_FUNCPTR (gst_bin_change_state_func);
455 gstelement_class->state_changed = GST_DEBUG_FUNCPTR (gst_bin_state_changed);
456 gstelement_class->get_state = GST_DEBUG_FUNCPTR (gst_bin_get_state_func);
458 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_bin_get_index_func);
459 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_bin_set_index_func);
461 gstelement_class->provide_clock =
462 GST_DEBUG_FUNCPTR (gst_bin_provide_clock_func);
463 gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_bin_set_clock_func);
465 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_bin_send_event);
466 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_bin_query);
467 gstelement_class->set_context = GST_DEBUG_FUNCPTR (gst_bin_set_context);
469 klass->add_element = GST_DEBUG_FUNCPTR (gst_bin_add_func);
470 klass->remove_element = GST_DEBUG_FUNCPTR (gst_bin_remove_func);
471 klass->handle_message = GST_DEBUG_FUNCPTR (gst_bin_handle_message_func);
473 klass->do_latency = GST_DEBUG_FUNCPTR (gst_bin_do_latency_func);
475 GST_DEBUG ("creating bin thread pool");
478 g_thread_pool_new ((GFunc) gst_bin_continue_func, NULL, -1, FALSE, &err);
480 g_critical ("could alloc threadpool %s", err->message);
485 gst_bin_init (GstBin * bin)
489 bin->numchildren = 0;
490 bin->children = NULL;
491 bin->children_cookie = 0;
492 bin->messages = NULL;
493 bin->provided_clock = NULL;
494 bin->clock_dirty = FALSE;
496 /* Set up a bus for listening to child elements */
497 bus = g_object_new (GST_TYPE_BUS, "enable-async", FALSE, NULL);
498 bin->child_bus = bus;
499 GST_DEBUG_OBJECT (bin, "using bus %" GST_PTR_FORMAT " to listen to children",
501 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bin_bus_handler, bin,
504 bin->priv = GST_BIN_GET_PRIVATE (bin);
505 bin->priv->asynchandling = DEFAULT_ASYNC_HANDLING;
506 bin->priv->structure_cookie = 0;
507 bin->priv->message_forward = DEFAULT_MESSAGE_FORWARD;
511 gst_bin_dispose (GObject * object)
513 GstBin *bin = GST_BIN_CAST (object);
514 GstBus **child_bus_p = &bin->child_bus;
515 GstClock **provided_clock_p = &bin->provided_clock;
516 GstElement **clock_provider_p = &bin->clock_provider;
518 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
520 GST_OBJECT_LOCK (object);
521 gst_object_replace ((GstObject **) child_bus_p, NULL);
522 gst_object_replace ((GstObject **) provided_clock_p, NULL);
523 gst_object_replace ((GstObject **) clock_provider_p, NULL);
524 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
525 GST_OBJECT_UNLOCK (object);
527 while (bin->children) {
528 gst_bin_remove (bin, GST_ELEMENT_CAST (bin->children->data));
530 if (G_UNLIKELY (bin->children != NULL)) {
531 g_critical ("could not remove elements from bin '%s'",
532 GST_STR_NULL (GST_OBJECT_NAME (object)));
535 g_list_free_full (bin->priv->contexts, (GDestroyNotify) gst_context_unref);
537 G_OBJECT_CLASS (parent_class)->dispose (object);
542 * @name: the name of the new bin
544 * Creates a new bin with the given name.
546 * Returns: (transfer floating): a new #GstBin
549 gst_bin_new (const gchar * name)
551 return gst_element_factory_make ("bin", name);
555 gst_bin_set_property (GObject * object, guint prop_id,
556 const GValue * value, GParamSpec * pspec)
560 gstbin = GST_BIN_CAST (object);
563 case PROP_ASYNC_HANDLING:
564 GST_OBJECT_LOCK (gstbin);
565 gstbin->priv->asynchandling = g_value_get_boolean (value);
566 GST_OBJECT_UNLOCK (gstbin);
568 case PROP_MESSAGE_FORWARD:
569 GST_OBJECT_LOCK (gstbin);
570 gstbin->priv->message_forward = g_value_get_boolean (value);
571 GST_OBJECT_UNLOCK (gstbin);
574 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
580 gst_bin_get_property (GObject * object, guint prop_id,
581 GValue * value, GParamSpec * pspec)
585 gstbin = GST_BIN_CAST (object);
588 case PROP_ASYNC_HANDLING:
589 GST_OBJECT_LOCK (gstbin);
590 g_value_set_boolean (value, gstbin->priv->asynchandling);
591 GST_OBJECT_UNLOCK (gstbin);
593 case PROP_MESSAGE_FORWARD:
594 GST_OBJECT_LOCK (gstbin);
595 g_value_set_boolean (value, gstbin->priv->message_forward);
596 GST_OBJECT_UNLOCK (gstbin);
599 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
605 /* return the cached index */
607 gst_bin_get_index_func (GstElement * element)
612 bin = GST_BIN_CAST (element);
614 GST_OBJECT_LOCK (bin);
615 if ((result = bin->priv->index))
616 gst_object_ref (result);
617 GST_OBJECT_UNLOCK (bin);
622 /* set the index on all elements in this bin
627 gst_bin_set_index_func (GstElement * element, GstIndex * index)
633 GValue data = { 0, };
635 bin = GST_BIN_CAST (element);
637 GST_OBJECT_LOCK (bin);
638 old = bin->priv->index;
639 if (G_UNLIKELY (old == index))
642 gst_object_ref (index);
643 bin->priv->index = index;
644 GST_OBJECT_UNLOCK (bin);
647 gst_object_unref (old);
649 it = gst_bin_iterate_elements (bin);
651 /* set the index on all elements in the bin */
654 switch (gst_iterator_next (it, &data)) {
655 case GST_ITERATOR_OK:
657 GstElement *child = g_value_get_object (&data);
659 GST_DEBUG_OBJECT (bin, "setting index on '%s'",
660 GST_ELEMENT_NAME (child));
661 gst_element_set_index (child, index);
663 g_value_reset (&data);
666 case GST_ITERATOR_RESYNC:
667 GST_DEBUG_OBJECT (bin, "iterator doing resync");
668 gst_iterator_resync (it);
671 case GST_ITERATOR_DONE:
672 GST_DEBUG_OBJECT (bin, "iterator done");
677 g_value_unset (&data);
678 gst_iterator_free (it);
683 GST_DEBUG_OBJECT (bin, "index was already set");
684 GST_OBJECT_UNLOCK (bin);
690 /* set the clock on all elements in this bin
695 gst_bin_set_clock_func (GstElement * element, GstClock * clock)
701 GValue data = { 0, };
703 bin = GST_BIN_CAST (element);
705 it = gst_bin_iterate_elements (bin);
709 switch (gst_iterator_next (it, &data)) {
710 case GST_ITERATOR_OK:
712 GstElement *child = g_value_get_object (&data);
714 res &= gst_element_set_clock (child, clock);
716 g_value_reset (&data);
719 case GST_ITERATOR_RESYNC:
720 GST_DEBUG_OBJECT (bin, "iterator doing resync");
721 gst_iterator_resync (it);
725 case GST_ITERATOR_DONE:
726 GST_DEBUG_OBJECT (bin, "iterator done");
731 g_value_unset (&data);
732 gst_iterator_free (it);
735 res = GST_ELEMENT_CLASS (parent_class)->set_clock (element, clock);
740 /* get the clock for this bin by asking all of the children in this bin
742 * The ref of the returned clock in increased so unref after usage.
744 * We loop the elements in state order and pick the last clock we can
745 * get. This makes sure we get a clock from the source.
750 gst_bin_provide_clock_func (GstElement * element)
752 GstClock *result = NULL;
753 GstElement *provider = NULL;
758 GstClock **provided_clock_p;
759 GstElement **clock_provider_p;
761 bin = GST_BIN_CAST (element);
763 GST_OBJECT_LOCK (bin);
764 if (!bin->clock_dirty)
767 GST_DEBUG_OBJECT (bin, "finding new clock");
769 it = gst_bin_sort_iterator_new (bin);
770 GST_OBJECT_UNLOCK (bin);
774 switch (gst_iterator_next (it, &val)) {
775 case GST_ITERATOR_OK:
777 GstElement *child = g_value_get_object (&val);
780 clock = gst_element_provide_clock (child);
782 GST_DEBUG_OBJECT (bin, "found candidate clock %p by element %s",
783 clock, GST_ELEMENT_NAME (child));
785 gst_object_unref (result);
786 gst_object_unref (provider);
789 provider = gst_object_ref (child);
792 g_value_reset (&val);
795 case GST_ITERATOR_RESYNC:
796 gst_iterator_resync (it);
799 case GST_ITERATOR_DONE:
804 g_value_unset (&val);
805 gst_iterator_free (it);
807 GST_OBJECT_LOCK (bin);
808 if (!bin->clock_dirty) {
810 gst_object_unref (provider);
812 gst_object_unref (result);
818 provided_clock_p = &bin->provided_clock;
819 clock_provider_p = &bin->clock_provider;
820 gst_object_replace ((GstObject **) provided_clock_p, (GstObject *) result);
821 gst_object_replace ((GstObject **) clock_provider_p, (GstObject *) provider);
822 bin->clock_dirty = FALSE;
823 GST_DEBUG_OBJECT (bin,
824 "provided new clock %" GST_PTR_FORMAT " by provider %" GST_PTR_FORMAT,
826 /* Provider is not being returned to caller, just the result */
828 gst_object_unref (provider);
829 GST_OBJECT_UNLOCK (bin);
835 if ((result = bin->provided_clock))
836 gst_object_ref (result);
837 GST_DEBUG_OBJECT (bin, "returning old clock %p", result);
838 GST_OBJECT_UNLOCK (bin);
845 * functions for manipulating cached messages
850 GstMessageType types;
853 /* check if a message is of given src and type */
855 message_check (GstMessage * message, MessageFind * target)
860 eq &= GST_MESSAGE_SRC (message) == target->src;
862 eq &= (GST_MESSAGE_TYPE (message) & target->types) != 0;
863 GST_LOG ("looking at message %p: %d", message, eq);
869 find_message (GstBin * bin, GstObject * src, GstMessageType types)
877 result = g_list_find_custom (bin->messages, &find,
878 (GCompareFunc) message_check);
881 GST_DEBUG_OBJECT (bin, "we found a message %p from %s matching types %08x",
882 result->data, GST_OBJECT_NAME (GST_MESSAGE_CAST (result->data)->src),
885 GST_DEBUG_OBJECT (bin, "no message found matching types %08x", types);
886 #ifndef GST_DISABLE_GST_DEBUG
890 for (i = 0; i < 32; i++)
891 if (types & (1 << i))
892 GST_DEBUG_OBJECT (bin, " %s", gst_message_type_get_name (1 << i));
900 /* with LOCK, returns TRUE if message had a valid SRC, takes ownership of
903 * A message that is cached and has the same SRC and type is replaced
904 * by the given message.
907 bin_replace_message (GstBin * bin, GstMessage * message, GstMessageType types)
913 if ((src = GST_MESSAGE_SRC (message))) {
914 /* first find the previous message posted by this element */
915 if ((previous = find_message (bin, src, types))) {
916 GstMessage *previous_msg;
918 /* if we found a previous message, replace it */
919 previous_msg = previous->data;
920 previous->data = message;
922 GST_DEBUG_OBJECT (bin, "replace old message %s from %s with %s message",
923 GST_MESSAGE_TYPE_NAME (previous_msg), GST_ELEMENT_NAME (src),
924 GST_MESSAGE_TYPE_NAME (message));
926 gst_message_unref (previous_msg);
928 /* keep new message */
929 bin->messages = g_list_prepend (bin->messages, message);
931 GST_DEBUG_OBJECT (bin, "got new message %p, %s from %s",
932 message, GST_MESSAGE_TYPE_NAME (message), GST_ELEMENT_NAME (src));
935 GST_DEBUG_OBJECT (bin, "got message %s from (NULL), not processing",
936 GST_MESSAGE_TYPE_NAME (message));
938 gst_message_unref (message);
943 /* with LOCK. Remove all messages of given types */
945 bin_remove_messages (GstBin * bin, GstObject * src, GstMessageType types)
953 for (walk = bin->messages; walk; walk = next) {
954 GstMessage *message = (GstMessage *) walk->data;
956 next = g_list_next (walk);
958 if (message_check (message, &find) == 0) {
959 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (message),
960 "deleting message %p of types 0x%08x", message, types);
961 bin->messages = g_list_delete_link (bin->messages, walk);
962 gst_message_unref (message);
964 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (message),
965 "not deleting message %p of type 0x%08x", message,
966 GST_MESSAGE_TYPE (message));
972 /* Check if the bin is EOS. We do this by scanning all sinks and
973 * checking if they posted an EOS message.
975 * call with bin LOCK */
977 is_eos (GstBin * bin, guint32 * seqnum)
984 for (walk = bin->children; walk; walk = g_list_next (walk)) {
987 element = GST_ELEMENT_CAST (walk->data);
988 if (bin_element_is_sink (element, bin) == 0) {
989 /* check if element posted EOS */
991 find_message (bin, GST_OBJECT_CAST (element), GST_MESSAGE_EOS))) {
992 GST_DEBUG ("sink '%s' posted EOS", GST_ELEMENT_NAME (element));
993 *seqnum = gst_message_get_seqnum (GST_MESSAGE_CAST (msgs->data));
996 GST_DEBUG ("sink '%s' did not post EOS yet",
997 GST_ELEMENT_NAME (element));
1003 /* FIXME: Some tests (e.g. elements/capsfilter) use
1004 * pipelines with a dangling sinkpad but no sink element.
1005 * These tests assume that no EOS message is ever
1006 * posted on the bus so let's keep that behaviour.
1007 * In valid pipelines this doesn't make a difference.
1009 return result && n_eos > 0;
1013 /* Check if the bin is STREAM_START. We do this by scanning all sinks and
1014 * checking if they posted an STREAM_START message.
1016 * call with bin LOCK */
1018 is_stream_start (GstBin * bin, guint32 * seqnum, gboolean * have_group_id,
1024 gboolean first = TRUE, same_group_id = TRUE;
1026 *have_group_id = TRUE;
1029 for (walk = bin->children; walk; walk = g_list_next (walk)) {
1030 GstElement *element;
1032 element = GST_ELEMENT_CAST (walk->data);
1033 if (bin_element_is_sink (element, bin) == 0) {
1034 /* check if element posted STREAM_START */
1036 find_message (bin, GST_OBJECT_CAST (element),
1037 GST_MESSAGE_STREAM_START))) {
1038 GST_DEBUG ("sink '%s' posted STREAM_START", GST_ELEMENT_NAME (element));
1039 *seqnum = gst_message_get_seqnum (GST_MESSAGE_CAST (msgs->data));
1040 if (gst_message_parse_group_id (GST_MESSAGE_CAST (msgs->data),
1044 *group_id = tmp_group_id;
1046 if (tmp_group_id != *group_id)
1047 same_group_id = FALSE;
1050 *have_group_id = FALSE;
1053 GST_DEBUG ("sink '%s' did not post STREAM_START yet",
1054 GST_ELEMENT_NAME (element));
1061 /* If all have a group_id we only consider this stream started
1062 * if all group ids were the same and all sinks posted a stream-start
1065 return same_group_id && result;
1066 /* otherwise consider this stream started after all sinks
1067 * have reported stream-start for backward compatibility.
1068 * FIXME 2.0: This should go away! */
1073 unlink_pads (const GValue * item, gpointer user_data)
1078 pad = g_value_get_object (item);
1080 if ((peer = gst_pad_get_peer (pad))) {
1081 if (gst_pad_get_direction (pad) == GST_PAD_SRC)
1082 gst_pad_unlink (pad, peer);
1084 gst_pad_unlink (peer, pad);
1085 gst_object_unref (peer);
1089 /* vmethod that adds an element to a bin
1094 gst_bin_add_func (GstBin * bin, GstElement * element)
1098 gboolean is_sink, is_source, provides_clock, requires_clock;
1099 GstMessage *clock_message = NULL, *async_message = NULL;
1100 GstStateChangeReturn ret;
1103 GST_DEBUG_OBJECT (bin, "element :%s", GST_ELEMENT_NAME (element));
1105 /* we obviously can't add ourself to ourself */
1106 if (G_UNLIKELY (element == GST_ELEMENT_CAST (bin)))
1109 /* get the element name to make sure it is unique in this bin. */
1110 GST_OBJECT_LOCK (element);
1111 elem_name = g_strdup (GST_ELEMENT_NAME (element));
1112 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1113 is_source = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE);
1115 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1117 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1118 GST_OBJECT_UNLOCK (element);
1120 GST_OBJECT_LOCK (bin);
1122 /* then check to see if the element's name is already taken in the bin,
1123 * we can safely take the lock here. This check is probably bogus because
1124 * you can safely change the element name after this check and before setting
1125 * the object parent. The window is very small though... */
1126 if (G_UNLIKELY (!gst_object_check_uniqueness (bin->children, elem_name)))
1127 goto duplicate_name;
1129 /* set the element's parent and add the element to the bin's list of children */
1130 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (element),
1131 GST_OBJECT_CAST (bin))))
1134 /* if we add a sink we become a sink */
1136 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "element \"%s\" was sink",
1138 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SINK);
1141 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "element \"%s\" was source",
1143 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SOURCE);
1145 if (provides_clock) {
1146 GST_DEBUG_OBJECT (bin, "element \"%s\" can provide a clock", elem_name);
1148 gst_message_new_clock_provide (GST_OBJECT_CAST (element), NULL, TRUE);
1149 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1151 if (requires_clock) {
1152 GST_DEBUG_OBJECT (bin, "element \"%s\" requires a clock", elem_name);
1153 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1156 bin->children = g_list_prepend (bin->children, element);
1158 bin->children_cookie++;
1159 if (!GST_BIN_IS_NO_RESYNC (bin))
1160 bin->priv->structure_cookie++;
1162 /* distribute the bus */
1163 gst_element_set_bus (element, bin->child_bus);
1165 /* propagate the current base_time, start_time and clock */
1166 gst_element_set_base_time (element, GST_ELEMENT_CAST (bin)->base_time);
1167 gst_element_set_start_time (element, GST_ELEMENT_START_TIME (bin));
1168 /* it's possible that the element did not accept the clock but
1169 * that is not important right now. When the pipeline goes to PLAYING,
1170 * a new clock will be selected */
1171 gst_element_set_clock (element, GST_ELEMENT_CLOCK (bin));
1173 for (l = bin->priv->contexts; l; l = l->next)
1174 gst_element_set_context (element, l->data);
1177 /* set the cached index on the children */
1178 if (bin->priv->index)
1179 gst_element_set_index (element, bin->priv->index);
1182 ret = GST_STATE_RETURN (bin);
1183 /* no need to update the state if we are in error */
1184 if (ret == GST_STATE_CHANGE_FAILURE)
1185 goto no_state_recalc;
1187 /* update the bin state, the new element could have been an ASYNC or
1188 * NO_PREROLL element */
1189 ret = GST_STATE_RETURN (element);
1190 GST_DEBUG_OBJECT (bin, "added %s element",
1191 gst_element_state_change_return_get_name (ret));
1194 case GST_STATE_CHANGE_ASYNC:
1196 /* create message to track this aync element when it posts an async-done
1198 async_message = gst_message_new_async_start (GST_OBJECT_CAST (element));
1201 case GST_STATE_CHANGE_NO_PREROLL:
1202 /* ignore all async elements we might have and commit our state */
1203 bin_handle_async_done (bin, ret, FALSE, GST_CLOCK_TIME_NONE);
1205 case GST_STATE_CHANGE_FAILURE:
1212 GST_OBJECT_UNLOCK (bin);
1214 /* post the messages on the bus of the element so that the bin can handle
1217 gst_element_post_message (element, clock_message);
1220 gst_element_post_message (element, async_message);
1222 /* unlink all linked pads */
1223 it = gst_element_iterate_pads (element);
1224 gst_iterator_foreach (it, (GstIteratorForeachFunction) unlink_pads, NULL);
1225 gst_iterator_free (it);
1227 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "added element \"%s\"",
1230 g_signal_emit (bin, gst_bin_signals[ELEMENT_ADDED], 0, element);
1231 gst_child_proxy_child_added ((GstChildProxy *) bin, (GObject *) element,
1238 /* ERROR handling here */
1241 GST_OBJECT_LOCK (bin);
1242 g_warning ("Cannot add bin '%s' to itself", GST_ELEMENT_NAME (bin));
1243 GST_OBJECT_UNLOCK (bin);
1248 g_warning ("Name '%s' is not unique in bin '%s', not adding",
1249 elem_name, GST_ELEMENT_NAME (bin));
1250 GST_OBJECT_UNLOCK (bin);
1256 g_warning ("Element '%s' already has parent", elem_name);
1257 GST_OBJECT_UNLOCK (bin);
1266 * @element: (transfer full): the #GstElement to add
1268 * Adds the given element to the bin. Sets the element's parent, and thus
1269 * takes ownership of the element. An element can only be added to one bin.
1271 * If the element's pads are linked to other pads, the pads will be unlinked
1272 * before the element is added to the bin.
1275 * When you add an element to an already-running pipeline, you will have to
1276 * take care to set the state of the newly-added element to the desired
1277 * state (usually PLAYING or PAUSED, same you set the pipeline to originally)
1278 * with gst_element_set_state(), or use gst_element_sync_state_with_parent().
1279 * The bin or pipeline will not take care of this for you.
1284 * Returns: TRUE if the element could be added, FALSE if
1285 * the bin does not want to accept the element.
1288 gst_bin_add (GstBin * bin, GstElement * element)
1290 GstBinClass *bclass;
1293 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1294 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1296 bclass = GST_BIN_GET_CLASS (bin);
1298 if (G_UNLIKELY (bclass->add_element == NULL))
1301 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "adding element %s to bin %s",
1302 GST_STR_NULL (GST_ELEMENT_NAME (element)),
1303 GST_STR_NULL (GST_ELEMENT_NAME (bin)));
1305 result = bclass->add_element (bin, element);
1309 /* ERROR handling */
1312 g_warning ("adding elements to bin '%s' is not supported",
1313 GST_ELEMENT_NAME (bin));
1318 /* remove an element from the bin
1323 gst_bin_remove_func (GstBin * bin, GstElement * element)
1327 gboolean is_sink, is_source, provides_clock, requires_clock;
1328 gboolean othersink, othersource, otherprovider, otherrequirer, found;
1329 GstMessage *clock_message = NULL;
1330 GstClock **provided_clock_p;
1331 GstElement **clock_provider_p;
1333 gboolean other_async, this_async, have_no_preroll;
1334 GstStateChangeReturn ret;
1336 GST_DEBUG_OBJECT (bin, "element :%s", GST_ELEMENT_NAME (element));
1338 GST_OBJECT_LOCK (bin);
1340 GST_OBJECT_LOCK (element);
1341 elem_name = g_strdup (GST_ELEMENT_NAME (element));
1343 if (GST_OBJECT_PARENT (element) != GST_OBJECT_CAST (bin))
1346 /* remove the parent ref */
1347 GST_OBJECT_PARENT (element) = NULL;
1349 /* grab element name so we can print it */
1350 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1351 is_source = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE);
1353 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1355 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1356 GST_OBJECT_UNLOCK (element);
1360 othersource = FALSE;
1361 otherprovider = FALSE;
1362 otherrequirer = FALSE;
1363 have_no_preroll = FALSE;
1364 /* iterate the elements, we collect which ones are async and no_preroll. We
1365 * also remove the element when we find it. */
1366 for (walk = bin->children; walk; walk = next) {
1367 GstElement *child = GST_ELEMENT_CAST (walk->data);
1369 next = g_list_next (walk);
1371 if (child == element) {
1373 /* remove the element */
1374 bin->children = g_list_delete_link (bin->children, walk);
1376 gboolean child_sink, child_source, child_provider, child_requirer;
1378 GST_OBJECT_LOCK (child);
1379 child_sink = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SINK);
1380 child_source = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SOURCE);
1382 GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1384 GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1385 /* when we remove a sink, check if there are other sinks. */
1386 if (is_sink && !othersink && child_sink)
1388 if (is_source && !othersource && child_source)
1390 if (provides_clock && !otherprovider && child_provider)
1391 otherprovider = TRUE;
1392 if (requires_clock && !otherrequirer && child_requirer)
1393 otherrequirer = TRUE;
1394 /* check if we have NO_PREROLL children */
1395 if (GST_STATE_RETURN (child) == GST_STATE_CHANGE_NO_PREROLL)
1396 have_no_preroll = TRUE;
1397 GST_OBJECT_UNLOCK (child);
1401 /* the element must have been in the bin's list of children */
1402 if (G_UNLIKELY (!found))
1405 /* we now removed the element from the list of elements, increment the cookie
1406 * so that others can detect a change in the children list. */
1408 bin->children_cookie++;
1409 if (!GST_BIN_IS_NO_RESYNC (bin))
1410 bin->priv->structure_cookie++;
1412 if (is_sink && !othersink) {
1413 /* we're not a sink anymore */
1414 GST_DEBUG_OBJECT (bin, "we removed the last sink");
1415 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_SINK);
1417 if (is_source && !othersource) {
1418 /* we're not a source anymore */
1419 GST_DEBUG_OBJECT (bin, "we removed the last source");
1420 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_SOURCE);
1422 if (provides_clock && !otherprovider) {
1423 /* we're not a clock provider anymore */
1424 GST_DEBUG_OBJECT (bin, "we removed the last clock provider");
1425 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1427 if (requires_clock && !otherrequirer) {
1428 /* we're not a clock requirer anymore */
1429 GST_DEBUG_OBJECT (bin, "we removed the last clock requirer");
1430 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1433 /* if the clock provider for this element is removed, we lost
1434 * the clock as well, we need to inform the parent of this
1435 * so that it can select a new clock */
1436 if (bin->clock_provider == element) {
1437 GST_DEBUG_OBJECT (bin, "element \"%s\" provided the clock", elem_name);
1438 bin->clock_dirty = TRUE;
1440 gst_message_new_clock_lost (GST_OBJECT_CAST (bin), bin->provided_clock);
1441 provided_clock_p = &bin->provided_clock;
1442 clock_provider_p = &bin->clock_provider;
1443 gst_object_replace ((GstObject **) provided_clock_p, NULL);
1444 gst_object_replace ((GstObject **) clock_provider_p, NULL);
1447 /* remove messages for the element, if there was a pending ASYNC_START
1448 * message we must see if removing the element caused the bin to lose its
1451 other_async = FALSE;
1452 for (walk = bin->messages; walk; walk = next) {
1453 GstMessage *message = (GstMessage *) walk->data;
1454 GstElement *src = GST_ELEMENT_CAST (GST_MESSAGE_SRC (message));
1457 next = g_list_next (walk);
1460 switch (GST_MESSAGE_TYPE (message)) {
1461 case GST_MESSAGE_ASYNC_START:
1467 GST_DEBUG_OBJECT (src, "looking at message %p", message);
1469 case GST_MESSAGE_STRUCTURE_CHANGE:
1473 GST_DEBUG_OBJECT (src, "looking at structure change message %p",
1475 /* it's unlikely that this message is still in the list of messages
1476 * because this would mean that a link/unlink is busy in another thread
1477 * while we remove the element. We still have to remove the message
1478 * because we might not receive the done message anymore when the element
1479 * is removed from the bin. */
1480 gst_message_parse_structure_change (message, NULL, &owner, NULL);
1481 if (owner == element)
1492 /* delete all message types */
1493 GST_DEBUG_OBJECT (src, "deleting message %p of element \"%s\"",
1494 message, elem_name);
1495 bin->messages = g_list_delete_link (bin->messages, walk);
1496 gst_message_unref (message);
1500 /* get last return */
1501 ret = GST_STATE_RETURN (bin);
1503 /* no need to update the state if we are in error */
1504 if (ret == GST_STATE_CHANGE_FAILURE)
1505 goto no_state_recalc;
1507 if (!other_async && this_async) {
1508 /* all other elements were not async and we removed the async one,
1509 * handle the async-done case because we are not async anymore now. */
1510 GST_DEBUG_OBJECT (bin,
1511 "we removed the last async element, have no_preroll %d",
1514 /* the current state return of the bin depends on if there are no_preroll
1515 * elements in the pipeline or not */
1516 if (have_no_preroll)
1517 ret = GST_STATE_CHANGE_NO_PREROLL;
1519 ret = GST_STATE_CHANGE_SUCCESS;
1521 bin_handle_async_done (bin, ret, FALSE, GST_CLOCK_TIME_NONE);
1523 GST_DEBUG_OBJECT (bin,
1524 "recalc state preroll: %d, other async: %d, this async %d",
1525 have_no_preroll, other_async, this_async);
1527 if (have_no_preroll) {
1528 ret = GST_STATE_CHANGE_NO_PREROLL;
1529 } else if (other_async) {
1530 /* there are other async elements and we were not doing an async state
1531 * change, change our pending state and go async */
1532 if (GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING) {
1533 GST_STATE_NEXT (bin) = GST_STATE (bin);
1534 GST_STATE_PENDING (bin) = GST_STATE (bin);
1536 ret = GST_STATE_CHANGE_ASYNC;
1538 GST_STATE_RETURN (bin) = ret;
1542 gst_element_set_bus (element, NULL);
1543 /* Clear the clock we provided to the element */
1544 gst_element_set_clock (element, NULL);
1545 GST_OBJECT_UNLOCK (bin);
1548 gst_element_post_message (GST_ELEMENT_CAST (bin), clock_message);
1550 /* unlink all linked pads */
1551 it = gst_element_iterate_pads (element);
1552 gst_iterator_foreach (it, (GstIteratorForeachFunction) unlink_pads, NULL);
1553 gst_iterator_free (it);
1555 GST_CAT_INFO_OBJECT (GST_CAT_PARENTAGE, bin, "removed child \"%s\"",
1558 g_signal_emit (bin, gst_bin_signals[ELEMENT_REMOVED], 0, element);
1559 gst_child_proxy_child_removed ((GstChildProxy *) bin, (GObject *) element,
1563 /* element is really out of our control now */
1564 gst_object_unref (element);
1568 /* ERROR handling */
1571 g_warning ("Element '%s' is not in bin '%s'", elem_name,
1572 GST_ELEMENT_NAME (bin));
1573 GST_OBJECT_UNLOCK (element);
1574 GST_OBJECT_UNLOCK (bin);
1583 * @element: (transfer none): the #GstElement to remove
1585 * Removes the element from the bin, unparenting it as well.
1586 * Unparenting the element means that the element will be dereferenced,
1587 * so if the bin holds the only reference to the element, the element
1588 * will be freed in the process of removing it from the bin. If you
1589 * want the element to still exist after removing, you need to call
1590 * gst_object_ref() before removing it from the bin.
1592 * If the element's pads are linked to other pads, the pads will be unlinked
1593 * before the element is removed from the bin.
1597 * Returns: TRUE if the element could be removed, FALSE if
1598 * the bin does not want to remove the element.
1601 gst_bin_remove (GstBin * bin, GstElement * element)
1603 GstBinClass *bclass;
1606 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1607 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1609 bclass = GST_BIN_GET_CLASS (bin);
1611 if (G_UNLIKELY (bclass->remove_element == NULL))
1614 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "removing element %s from bin %s",
1615 GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin));
1617 result = bclass->remove_element (bin, element);
1621 /* ERROR handling */
1624 g_warning ("removing elements from bin '%s' is not supported",
1625 GST_ELEMENT_NAME (bin));
1631 * gst_bin_iterate_elements:
1634 * Gets an iterator for the elements in this bin.
1636 * MT safe. Caller owns returned value.
1638 * Returns: (transfer full): a #GstIterator of #GstElement, or NULL
1641 gst_bin_iterate_elements (GstBin * bin)
1643 GstIterator *result;
1645 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1647 GST_OBJECT_LOCK (bin);
1648 result = gst_iterator_new_list (GST_TYPE_ELEMENT,
1649 GST_OBJECT_GET_LOCK (bin),
1650 &bin->children_cookie, &bin->children, (GObject *) bin, NULL);
1651 GST_OBJECT_UNLOCK (bin);
1656 static GstIteratorItem
1657 iterate_child_recurse (GstIterator * it, const GValue * item)
1659 GstElement *child = g_value_get_object (item);
1661 if (GST_IS_BIN (child)) {
1662 GstIterator *other = gst_bin_iterate_recurse (GST_BIN_CAST (child));
1664 gst_iterator_push (it, other);
1666 return GST_ITERATOR_ITEM_PASS;
1670 * gst_bin_iterate_recurse:
1673 * Gets an iterator for the elements in this bin.
1674 * This iterator recurses into GstBin children.
1676 * MT safe. Caller owns returned value.
1678 * Returns: (transfer full): a #GstIterator of #GstElement, or NULL
1681 gst_bin_iterate_recurse (GstBin * bin)
1683 GstIterator *result;
1685 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1687 GST_OBJECT_LOCK (bin);
1688 result = gst_iterator_new_list (GST_TYPE_ELEMENT,
1689 GST_OBJECT_GET_LOCK (bin),
1690 &bin->children_cookie,
1692 (GObject *) bin, (GstIteratorItemFunction) iterate_child_recurse);
1693 GST_OBJECT_UNLOCK (bin);
1698 /* returns 0 when TRUE because this is a GCompareFunc */
1701 bin_element_is_sink (GstElement * child, GstBin * bin)
1705 /* we lock the child here for the remainder of the function to
1706 * get its name and flag safely. */
1707 GST_OBJECT_LOCK (child);
1708 is_sink = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SINK);
1710 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
1711 "child %s %s sink", GST_OBJECT_NAME (child), is_sink ? "is" : "is not");
1713 GST_OBJECT_UNLOCK (child);
1714 return is_sink ? 0 : 1;
1718 sink_iterator_filter (const GValue * vchild, GValue * vbin)
1720 GstBin *bin = g_value_get_object (vbin);
1721 GstElement *child = g_value_get_object (vchild);
1723 return (bin_element_is_sink (child, bin));
1727 * gst_bin_iterate_sinks:
1730 * Gets an iterator for all elements in the bin that have the
1731 * #GST_ELEMENT_FLAG_SINK flag set.
1733 * MT safe. Caller owns returned value.
1735 * Returns: (transfer full): a #GstIterator of #GstElement, or NULL
1738 gst_bin_iterate_sinks (GstBin * bin)
1740 GstIterator *children;
1741 GstIterator *result;
1742 GValue vbin = { 0, };
1744 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1746 g_value_init (&vbin, GST_TYPE_BIN);
1747 g_value_set_object (&vbin, bin);
1749 children = gst_bin_iterate_elements (bin);
1750 result = gst_iterator_filter (children,
1751 (GCompareFunc) sink_iterator_filter, &vbin);
1753 g_value_unset (&vbin);
1758 /* returns 0 when TRUE because this is a GCompareFunc */
1761 bin_element_is_src (GstElement * child, GstBin * bin)
1765 /* we lock the child here for the remainder of the function to
1766 * get its name and other info safely. */
1767 GST_OBJECT_LOCK (child);
1768 is_src = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SOURCE);
1770 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
1771 "child %s %s src", GST_OBJECT_NAME (child), is_src ? "is" : "is not");
1773 GST_OBJECT_UNLOCK (child);
1774 return is_src ? 0 : 1;
1778 src_iterator_filter (const GValue * vchild, GValue * vbin)
1780 GstBin *bin = g_value_get_object (vbin);
1781 GstElement *child = g_value_get_object (vchild);
1783 return (bin_element_is_src (child, bin));
1787 * gst_bin_iterate_sources:
1790 * Gets an iterator for all elements in the bin that have the
1791 * #GST_ELEMENT_FLAG_SOURCE flag set.
1793 * MT safe. Caller owns returned value.
1795 * Returns: (transfer full): a #GstIterator of #GstElement, or NULL
1798 gst_bin_iterate_sources (GstBin * bin)
1800 GstIterator *children;
1801 GstIterator *result;
1802 GValue vbin = { 0, };
1804 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1806 g_value_init (&vbin, GST_TYPE_BIN);
1807 g_value_set_object (&vbin, bin);
1809 children = gst_bin_iterate_elements (bin);
1810 result = gst_iterator_filter (children,
1811 (GCompareFunc) src_iterator_filter, &vbin);
1813 g_value_unset (&vbin);
1821 static GstStateChangeReturn
1822 gst_bin_get_state_func (GstElement * element, GstState * state,
1823 GstState * pending, GstClockTime timeout)
1825 GstStateChangeReturn ret;
1827 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
1830 GST_ELEMENT_CLASS (parent_class)->get_state (element, state, pending,
1836 /***********************************************
1837 * Topologically sorted iterator
1838 * see http://en.wikipedia.org/wiki/Topological_sorting
1840 * For each element in the graph, an entry is kept in a HashTable
1841 * with its number of srcpad connections (degree).
1842 * We then change state of all elements without dependencies
1843 * (degree 0) and decrement the degree of all elements connected
1844 * on the sinkpads. When an element reaches degree 0, its state is
1846 * When all elements are handled the algorithm stops.
1848 typedef struct _GstBinSortIterator
1851 GQueue queue; /* elements queued for state change */
1852 GstBin *bin; /* bin we iterate */
1853 gint mode; /* adding or removing dependency */
1854 GstElement *best; /* next element with least dependencies */
1855 gint best_deg; /* best degree */
1856 GHashTable *hash; /* hashtable with element dependencies */
1857 gboolean dirty; /* we detected structure change */
1858 } GstBinSortIterator;
1861 gst_bin_sort_iterator_copy (const GstBinSortIterator * it,
1862 GstBinSortIterator * copy)
1864 GHashTableIter iter;
1865 gpointer key, value;
1867 copy->queue = it->queue;
1868 g_queue_foreach (©->queue, (GFunc) gst_object_ref, NULL);
1870 copy->bin = gst_object_ref (it->bin);
1872 copy->best = gst_object_ref (it->best);
1874 copy->hash = g_hash_table_new (NULL, NULL);
1875 g_hash_table_iter_init (&iter, it->hash);
1876 while (g_hash_table_iter_next (&iter, &key, &value))
1877 g_hash_table_insert (copy->hash, key, value);
1880 /* we add and subtract 1 to make sure we don't confuse NULL and 0 */
1881 #define HASH_SET_DEGREE(bit, elem, deg) \
1882 g_hash_table_replace (bit->hash, elem, GINT_TO_POINTER(deg+1))
1883 #define HASH_GET_DEGREE(bit, elem) \
1884 (GPOINTER_TO_INT(g_hash_table_lookup (bit->hash, elem))-1)
1886 /* add element to queue of next elements in the iterator.
1887 * We push at the tail to give higher priority elements a
1890 add_to_queue (GstBinSortIterator * bit, GstElement * element)
1892 GST_DEBUG_OBJECT (bit->bin, "adding '%s' to queue",
1893 GST_ELEMENT_NAME (element));
1894 gst_object_ref (element);
1895 g_queue_push_tail (&bit->queue, element);
1896 HASH_SET_DEGREE (bit, element, -1);
1900 remove_from_queue (GstBinSortIterator * bit, GstElement * element)
1904 if ((find = g_queue_find (&bit->queue, element))) {
1905 GST_DEBUG_OBJECT (bit->bin, "removing '%s' from queue",
1906 GST_ELEMENT_NAME (element));
1908 g_queue_delete_link (&bit->queue, find);
1909 gst_object_unref (element);
1911 GST_DEBUG_OBJECT (bit->bin, "unable to remove '%s' from queue",
1912 GST_ELEMENT_NAME (element));
1916 /* clear the queue, unref all objects as we took a ref when
1917 * we added them to the queue */
1919 clear_queue (GQueue * queue)
1923 while ((p = g_queue_pop_head (queue)))
1924 gst_object_unref (p);
1927 /* set all degrees to 0. Elements marked as a sink are
1928 * added to the queue immediately. Since we only look at the SINK flag of the
1929 * element, it is possible that we add non-sinks to the queue. These will be
1930 * removed from the queue again when we can prove that it provides data for some
1933 reset_degree (GstElement * element, GstBinSortIterator * bit)
1937 /* sinks are added right away */
1938 GST_OBJECT_LOCK (element);
1939 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1940 GST_OBJECT_UNLOCK (element);
1943 add_to_queue (bit, element);
1945 /* others are marked with 0 and handled when sinks are done */
1946 HASH_SET_DEGREE (bit, element, 0);
1950 /* adjust the degree of all elements connected to the given
1951 * element. If a degree of an element drops to 0, it is
1952 * added to the queue of elements to schedule next.
1954 * We have to make sure not to cross the bin boundary this element
1958 update_degree (GstElement * element, GstBinSortIterator * bit)
1960 gboolean linked = FALSE;
1962 GST_OBJECT_LOCK (element);
1963 /* don't touch degree if element has no sinkpads */
1964 if (element->numsinkpads != 0) {
1965 /* loop over all sinkpads, decrement degree for all connected
1966 * elements in this bin */
1969 for (pads = element->sinkpads; pads; pads = g_list_next (pads)) {
1972 pad = GST_PAD_CAST (pads->data);
1974 /* we're iterating over the sinkpads, check if it's busy in a link/unlink */
1975 if (G_UNLIKELY (find_message (bit->bin, GST_OBJECT_CAST (pad),
1976 GST_MESSAGE_STRUCTURE_CHANGE))) {
1977 /* mark the iterator as dirty because we won't be updating the degree
1978 * of the peer parent now. This would result in the 'loop detected'
1979 * later on because the peer parent element could become the best next
1980 * element with a degree > 0. We will simply continue our state
1981 * changes and we'll eventually resync when the unlink completed and
1982 * the iterator cookie is updated. */
1987 if ((peer = gst_pad_get_peer (pad))) {
1988 GstElement *peer_element;
1990 if ((peer_element = gst_pad_get_parent_element (peer))) {
1991 GST_OBJECT_LOCK (peer_element);
1992 /* check that we don't go outside of this bin */
1993 if (GST_OBJECT_CAST (peer_element)->parent ==
1994 GST_OBJECT_CAST (bit->bin)) {
1995 gint old_deg, new_deg;
1997 old_deg = HASH_GET_DEGREE (bit, peer_element);
1999 /* check to see if we added an element as sink that was not really a
2000 * sink because it was connected to some other element. */
2001 if (old_deg == -1) {
2002 remove_from_queue (bit, peer_element);
2005 new_deg = old_deg + bit->mode;
2007 GST_DEBUG_OBJECT (bit->bin,
2008 "change element %s, degree %d->%d, linked to %s",
2009 GST_ELEMENT_NAME (peer_element), old_deg, new_deg,
2010 GST_ELEMENT_NAME (element));
2012 /* update degree, it is possible that an element was in 0 and
2013 * reaches -1 here. This would mean that the element had no sinkpads
2014 * but became linked while the state change was happening. We will
2015 * resync on this with the structure change message. */
2017 /* degree hit 0, add to queue */
2018 add_to_queue (bit, peer_element);
2020 HASH_SET_DEGREE (bit, peer_element, new_deg);
2024 GST_OBJECT_UNLOCK (peer_element);
2025 gst_object_unref (peer_element);
2027 gst_object_unref (peer);
2032 GST_DEBUG_OBJECT (bit->bin, "element %s not linked on any sinkpads",
2033 GST_ELEMENT_NAME (element));
2035 GST_OBJECT_UNLOCK (element);
2038 /* find the next best element not handled yet. This is the one
2039 * with the lowest non-negative degree */
2041 find_element (GstElement * element, GstBinSortIterator * bit)
2045 /* element is already handled */
2046 if ((degree = HASH_GET_DEGREE (bit, element)) < 0)
2049 /* first element or element with smaller degree */
2050 if (bit->best == NULL || bit->best_deg > degree) {
2051 bit->best = element;
2052 bit->best_deg = degree;
2056 /* get next element in iterator. */
2057 static GstIteratorResult
2058 gst_bin_sort_iterator_next (GstBinSortIterator * bit, GValue * result)
2061 GstBin *bin = bit->bin;
2063 /* empty queue, we have to find a next best element */
2064 if (g_queue_is_empty (&bit->queue)) {
2066 bit->best_deg = G_MAXINT;
2067 g_list_foreach (bin->children, (GFunc) find_element, bit);
2068 if ((best = bit->best)) {
2069 /* when we detected an unlink, don't warn because our degrees might be
2070 * screwed up. We will resync later */
2071 if (bit->best_deg != 0 && !bit->dirty) {
2072 /* we don't fail on this one yet */
2073 GST_WARNING_OBJECT (bin, "loop dected in graph");
2074 g_warning ("loop detected in the graph of bin '%s'!!",
2075 GST_ELEMENT_NAME (bin));
2077 /* best unhandled element, schedule as next element */
2078 GST_DEBUG_OBJECT (bin, "queue empty, next best: %s",
2079 GST_ELEMENT_NAME (best));
2080 HASH_SET_DEGREE (bit, best, -1);
2081 g_value_set_object (result, best);
2083 GST_DEBUG_OBJECT (bin, "queue empty, elements exhausted");
2084 /* no more unhandled elements, we are done */
2085 return GST_ITERATOR_DONE;
2088 /* everything added to the queue got reffed */
2089 best = g_queue_pop_head (&bit->queue);
2090 g_value_set_object (result, best);
2091 gst_object_unref (best);
2094 GST_DEBUG_OBJECT (bin, "queue head gives %s", GST_ELEMENT_NAME (best));
2095 /* update degrees of linked elements */
2096 update_degree (best, bit);
2098 return GST_ITERATOR_OK;
2101 /* clear queues, recalculate the degrees and restart. */
2103 gst_bin_sort_iterator_resync (GstBinSortIterator * bit)
2105 GstBin *bin = bit->bin;
2107 GST_DEBUG_OBJECT (bin, "resync");
2109 clear_queue (&bit->queue);
2111 g_list_foreach (bin->children, (GFunc) reset_degree, bit);
2112 /* calc degrees, incrementing */
2114 g_list_foreach (bin->children, (GFunc) update_degree, bit);
2115 /* for the rest of the function we decrement the degrees */
2119 /* clear queues, unref bin and free iterator. */
2121 gst_bin_sort_iterator_free (GstBinSortIterator * bit)
2123 GstBin *bin = bit->bin;
2125 GST_DEBUG_OBJECT (bin, "free");
2126 clear_queue (&bit->queue);
2127 g_hash_table_destroy (bit->hash);
2128 gst_object_unref (bin);
2131 /* should be called with the bin LOCK held */
2132 static GstIterator *
2133 gst_bin_sort_iterator_new (GstBin * bin)
2135 GstBinSortIterator *result;
2137 /* we don't need an ItemFunction because we ref the items in the _next
2139 result = (GstBinSortIterator *)
2140 gst_iterator_new (sizeof (GstBinSortIterator),
2142 GST_OBJECT_GET_LOCK (bin),
2143 &bin->priv->structure_cookie,
2144 (GstIteratorCopyFunction) gst_bin_sort_iterator_copy,
2145 (GstIteratorNextFunction) gst_bin_sort_iterator_next,
2146 (GstIteratorItemFunction) NULL,
2147 (GstIteratorResyncFunction) gst_bin_sort_iterator_resync,
2148 (GstIteratorFreeFunction) gst_bin_sort_iterator_free);
2149 g_queue_init (&result->queue);
2150 result->hash = g_hash_table_new (NULL, NULL);
2151 gst_object_ref (bin);
2153 gst_bin_sort_iterator_resync (result);
2155 return (GstIterator *) result;
2159 * gst_bin_iterate_sorted:
2162 * Gets an iterator for the elements in this bin in topologically
2163 * sorted order. This means that the elements are returned from
2164 * the most downstream elements (sinks) to the sources.
2166 * This function is used internally to perform the state changes
2167 * of the bin elements and for clock selection.
2169 * MT safe. Caller owns returned value.
2171 * Returns: (transfer full): a #GstIterator of #GstElement, or NULL
2174 gst_bin_iterate_sorted (GstBin * bin)
2176 GstIterator *result;
2178 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
2180 GST_OBJECT_LOCK (bin);
2181 result = gst_bin_sort_iterator_new (bin);
2182 GST_OBJECT_UNLOCK (bin);
2187 static GstStateChangeReturn
2188 gst_bin_element_set_state (GstBin * bin, GstElement * element,
2189 GstClockTime base_time, GstClockTime start_time, GstState current,
2192 GstStateChangeReturn ret;
2193 GstState child_current, child_pending;
2197 GST_STATE_LOCK (element);
2199 GST_OBJECT_LOCK (element);
2200 /* set base_time and start time on child */
2201 GST_ELEMENT_START_TIME (element) = start_time;
2202 element->base_time = base_time;
2203 /* peel off the locked flag */
2204 locked = GST_ELEMENT_IS_LOCKED_STATE (element);
2205 /* Get the previous set_state result to preserve NO_PREROLL and ASYNC */
2206 ret = GST_STATE_RETURN (element);
2207 child_current = GST_STATE (element);
2208 child_pending = GST_STATE_PENDING (element);
2209 GST_OBJECT_UNLOCK (element);
2211 /* skip locked elements */
2212 if (G_UNLIKELY (locked))
2215 /* if the element was no preroll, just start changing the state regardless
2216 * if it had async elements (in the case of a bin) because they won't preroll
2218 if (G_UNLIKELY (ret == GST_STATE_CHANGE_NO_PREROLL)) {
2219 GST_DEBUG_OBJECT (element, "element is NO_PREROLL, ignore async elements");
2223 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2224 "current %s pending %s, desired next %s",
2225 gst_element_state_get_name (child_current),
2226 gst_element_state_get_name (child_pending),
2227 gst_element_state_get_name (next));
2229 /* always recurse into bins so that we can set the base time */
2230 if (GST_IS_BIN (element))
2233 /* Try not to change the state of elements that are already in the state we're
2235 if (child_current == next && child_pending == GST_STATE_VOID_PENDING) {
2236 /* child is already at the requested state, return previous return. Note that
2237 * if the child has a pending state to next, we will still call the
2238 * set_state function */
2240 } else if (next > current) {
2241 /* upward state change */
2242 if (child_pending == GST_STATE_VOID_PENDING) {
2243 /* .. and the child is not busy doing anything */
2244 if (child_current > next) {
2245 /* .. and is already past the requested state, assume it got there
2247 ret = GST_STATE_CHANGE_SUCCESS;
2250 } else if (child_pending > child_current) {
2251 /* .. and the child is busy going upwards */
2252 if (child_current >= next) {
2253 /* .. and is already past the requested state, assume it got there
2255 ret = GST_STATE_CHANGE_SUCCESS;
2259 /* .. and the child is busy going downwards */
2260 if (child_current > next) {
2261 /* .. and is already past the requested state, assume it got there
2263 ret = GST_STATE_CHANGE_SUCCESS;
2267 } else if (next < current) {
2268 /* downward state change */
2269 if (child_pending == GST_STATE_VOID_PENDING) {
2270 /* .. and the child is not busy doing anything */
2271 if (child_current < next) {
2272 /* .. and is already past the requested state, assume it got there
2274 ret = GST_STATE_CHANGE_SUCCESS;
2277 } else if (child_pending < child_current) {
2278 /* .. and the child is busy going downwards */
2279 if (child_current <= next) {
2280 /* .. and is already past the requested state, assume it got there
2282 ret = GST_STATE_CHANGE_SUCCESS;
2286 /* .. and the child is busy going upwards */
2287 if (child_current < next) {
2288 /* .. and is already past the requested state, assume it got there
2290 ret = GST_STATE_CHANGE_SUCCESS;
2297 GST_OBJECT_LOCK (bin);
2298 /* the element was busy with an upwards async state change, we must wait for
2299 * an ASYNC_DONE message before we attemp to change the state. */
2301 find_message (bin, GST_OBJECT_CAST (element),
2302 GST_MESSAGE_ASYNC_START))) {
2303 #ifndef GST_DISABLE_GST_DEBUG
2304 GstMessage *message = GST_MESSAGE_CAST (found->data);
2306 GST_DEBUG_OBJECT (element, "element message %p, %s async busy",
2307 message, GST_ELEMENT_NAME (GST_MESSAGE_SRC (message)));
2309 /* only wait for upward state changes */
2310 if (next > current) {
2311 /* We found an async element check if we can force its state to change or
2312 * if we have to wait for it to preroll. */
2316 GST_OBJECT_UNLOCK (bin);
2319 GST_DEBUG_OBJECT (bin,
2320 "setting element %s to %s, base_time %" GST_TIME_FORMAT,
2321 GST_ELEMENT_NAME (element), gst_element_state_get_name (next),
2322 GST_TIME_ARGS (base_time));
2325 ret = gst_element_set_state (element, next);
2327 GST_STATE_UNLOCK (element);
2333 GST_DEBUG_OBJECT (element,
2334 "element is locked, return previous return %s",
2335 gst_element_state_change_return_get_name (ret));
2336 GST_STATE_UNLOCK (element);
2341 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2342 "skipping transition from %s to %s",
2343 gst_element_state_get_name (child_current),
2344 gst_element_state_get_name (next));
2345 GST_STATE_UNLOCK (element);
2350 GST_DEBUG_OBJECT (element, "element was busy, delaying state change");
2351 GST_OBJECT_UNLOCK (bin);
2352 GST_STATE_UNLOCK (element);
2353 return GST_STATE_CHANGE_ASYNC;
2357 /* gst_iterator_fold functions for pads_activate
2358 * Stop the iterator if activating one pad failed. */
2360 activate_pads (const GValue * vpad, GValue * ret, gboolean * active)
2362 GstPad *pad = g_value_get_object (vpad);
2363 gboolean cont = TRUE;
2365 if (!(cont = gst_pad_set_active (pad, *active)))
2366 g_value_set_boolean (ret, FALSE);
2371 /* returns false on error or early cutout of the fold, true if all
2372 * pads in @iter were (de)activated successfully. */
2374 iterator_activate_fold_with_resync (GstIterator * iter, gpointer user_data)
2376 GstIteratorResult ires;
2379 /* no need to unset this later, it's just a boolean */
2380 g_value_init (&ret, G_TYPE_BOOLEAN);
2381 g_value_set_boolean (&ret, TRUE);
2384 ires = gst_iterator_fold (iter, (GstIteratorFoldFunction) activate_pads,
2387 case GST_ITERATOR_RESYNC:
2388 /* need to reset the result again */
2389 g_value_set_boolean (&ret, TRUE);
2390 gst_iterator_resync (iter);
2392 case GST_ITERATOR_DONE:
2393 /* all pads iterated, return collected value */
2396 /* iterator returned _ERROR or premature end with _OK,
2397 * mark an error and exit */
2398 g_value_set_boolean (&ret, FALSE);
2403 /* return collected value */
2404 return g_value_get_boolean (&ret);
2407 /* is called with STATE_LOCK
2410 gst_bin_src_pads_activate (GstBin * bin, gboolean active)
2415 GST_DEBUG_OBJECT (bin, "%s pads", active ? "activate" : "deactivate");
2417 iter = gst_element_iterate_src_pads ((GstElement *) bin);
2418 fold_ok = iterator_activate_fold_with_resync (iter, &active);
2419 gst_iterator_free (iter);
2420 if (G_UNLIKELY (!fold_ok))
2423 GST_DEBUG_OBJECT (bin, "pad %sactivation successful", active ? "" : "de");
2430 GST_DEBUG_OBJECT (bin, "pad %sactivation failed", active ? "" : "de");
2436 * gst_bin_recalculate_latency:
2439 * Query @bin for the current latency using and reconfigures this latency to all the
2440 * elements with a LATENCY event.
2442 * This method is typically called on the pipeline when a #GST_MESSAGE_LATENCY
2443 * is posted on the bus.
2445 * This function simply emits the 'do-latency' signal so any custom latency
2446 * calculations will be performed.
2448 * Returns: %TRUE if the latency could be queried and reconfigured.
2451 gst_bin_recalculate_latency (GstBin * bin)
2455 g_signal_emit (bin, gst_bin_signals[DO_LATENCY], 0, &res);
2456 GST_DEBUG_OBJECT (bin, "latency returned %d", res);
2462 gst_bin_do_latency_func (GstBin * bin)
2465 GstElement *element;
2466 GstClockTime min_latency, max_latency;
2469 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2471 element = GST_ELEMENT_CAST (bin);
2473 GST_DEBUG_OBJECT (element, "querying latency");
2475 query = gst_query_new_latency ();
2476 if ((res = gst_element_query (element, query))) {
2479 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
2481 GST_DEBUG_OBJECT (element,
2482 "got min latency %" GST_TIME_FORMAT ", max latency %"
2483 GST_TIME_FORMAT ", live %d", GST_TIME_ARGS (min_latency),
2484 GST_TIME_ARGS (max_latency), live);
2486 if (max_latency < min_latency) {
2487 /* this is an impossible situation, some parts of the pipeline might not
2488 * work correctly. We post a warning for now. */
2489 GST_ELEMENT_WARNING (element, CORE, CLOCK, (NULL),
2490 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
2491 GST_TIME_FORMAT ". Add queues or other buffering elements.",
2492 GST_TIME_ARGS (max_latency), GST_TIME_ARGS (min_latency)));
2495 /* configure latency on elements */
2496 res = gst_element_send_event (element, gst_event_new_latency (min_latency));
2498 GST_INFO_OBJECT (element, "configured latency of %" GST_TIME_FORMAT,
2499 GST_TIME_ARGS (min_latency));
2501 GST_WARNING_OBJECT (element,
2502 "did not really configure latency of %" GST_TIME_FORMAT,
2503 GST_TIME_ARGS (min_latency));
2506 /* this is not a real problem, we just don't configure any latency. */
2507 GST_WARNING_OBJECT (element, "failed to query latency");
2509 gst_query_unref (query);
2515 gst_bin_state_changed (GstElement * element, GstState oldstate,
2516 GstState newstate, GstState pending)
2518 GstElementClass *pklass = (GstElementClass *) parent_class;
2520 if (newstate == GST_STATE_PLAYING && pending == GST_STATE_VOID_PENDING)
2521 bin_do_eos (GST_BIN_CAST (element));
2523 if (pklass->state_changed)
2524 pklass->state_changed (element, oldstate, newstate, pending);
2527 static GstStateChangeReturn
2528 gst_bin_change_state_func (GstElement * element, GstStateChange transition)
2531 GstStateChangeReturn ret;
2532 GstState current, next;
2533 gboolean have_async;
2534 gboolean have_no_preroll;
2535 GstClockTime base_time, start_time;
2538 GValue data = { 0, };
2540 /* we don't need to take the STATE_LOCK, it is already taken */
2541 current = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2542 next = (GstState) GST_STATE_TRANSITION_NEXT (transition);
2544 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2545 "changing state of children from %s to %s",
2546 gst_element_state_get_name (current), gst_element_state_get_name (next));
2548 bin = GST_BIN_CAST (element);
2551 case GST_STATE_PLAYING:
2555 GST_OBJECT_LOCK (bin);
2556 toplevel = BIN_IS_TOPLEVEL (bin);
2557 GST_OBJECT_UNLOCK (bin);
2560 gst_bin_recalculate_latency (bin);
2563 case GST_STATE_PAUSED:
2564 /* Clear EOS list on next PAUSED */
2565 GST_OBJECT_LOCK (bin);
2566 GST_DEBUG_OBJECT (element, "clearing EOS elements");
2567 bin_remove_messages (bin, NULL, GST_MESSAGE_EOS);
2568 bin->priv->posted_eos = FALSE;
2569 if (current == GST_STATE_READY)
2570 bin_remove_messages (bin, NULL, GST_MESSAGE_STREAM_START);
2571 GST_OBJECT_UNLOCK (bin);
2572 if (current == GST_STATE_READY)
2573 if (!(gst_bin_src_pads_activate (bin, TRUE)))
2574 goto activate_failure;
2576 case GST_STATE_READY:
2577 /* Clear message list on next READY */
2578 GST_OBJECT_LOCK (bin);
2579 GST_DEBUG_OBJECT (element, "clearing all cached messages");
2580 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
2581 GST_OBJECT_UNLOCK (bin);
2582 if (current == GST_STATE_PAUSED)
2583 if (!(gst_bin_src_pads_activate (bin, FALSE)))
2584 goto activate_failure;
2586 case GST_STATE_NULL:
2587 if (current == GST_STATE_READY) {
2590 if (!(gst_bin_src_pads_activate (bin, FALSE)))
2591 goto activate_failure;
2593 /* Remove all non-persistent contexts */
2594 GST_OBJECT_LOCK (bin);
2595 for (l = bin->priv->contexts; l;) {
2596 GstContext *context = l->data;
2598 if (!gst_context_is_persistent (context)) {
2601 gst_context_unref (context);
2603 bin->priv->contexts = g_list_delete_link (bin->priv->contexts, l);
2609 GST_OBJECT_UNLOCK (bin);
2616 /* this flag is used to make the async state changes return immediately. We
2617 * don't want them to interfere with this state change */
2618 GST_OBJECT_LOCK (bin);
2619 bin->polling = TRUE;
2620 GST_OBJECT_UNLOCK (bin);
2622 /* iterate in state change order */
2623 it = gst_bin_iterate_sorted (bin);
2625 /* mark if we've seen an ASYNC element in the bin when we did a state change.
2626 * Note how we don't reset this value when a resync happens, the reason being
2627 * that the async element posted ASYNC_START and we want to post ASYNC_DONE
2628 * even after a resync when the async element is gone */
2632 /* take base_time */
2633 base_time = gst_element_get_base_time (element);
2634 start_time = gst_element_get_start_time (element);
2636 have_no_preroll = FALSE;
2640 switch (gst_iterator_next (it, &data)) {
2641 case GST_ITERATOR_OK:
2645 child = g_value_get_object (&data);
2647 /* set state and base_time now */
2648 ret = gst_bin_element_set_state (bin, child, base_time, start_time,
2652 case GST_STATE_CHANGE_SUCCESS:
2653 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2654 "child '%s' changed state to %d(%s) successfully",
2655 GST_ELEMENT_NAME (child), next,
2656 gst_element_state_get_name (next));
2658 case GST_STATE_CHANGE_ASYNC:
2660 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2661 "child '%s' is changing state asynchronously to %s",
2662 GST_ELEMENT_NAME (child), gst_element_state_get_name (next));
2666 case GST_STATE_CHANGE_FAILURE:{
2669 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2670 "child '%s' failed to go to state %d(%s)",
2671 GST_ELEMENT_NAME (child),
2672 next, gst_element_state_get_name (next));
2674 /* Only fail if the child is still inside
2675 * this bin. It might've been removed already
2676 * because of the error by the bin subclass
2677 * to ignore the error. */
2678 parent = gst_object_get_parent (GST_OBJECT_CAST (child));
2679 if (parent == GST_OBJECT_CAST (element)) {
2680 /* element is still in bin, really error now */
2681 gst_object_unref (parent);
2684 /* child removed from bin, let the resync code redo the state
2686 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2687 "child '%s' was removed from the bin",
2688 GST_ELEMENT_NAME (child));
2691 gst_object_unref (parent);
2695 case GST_STATE_CHANGE_NO_PREROLL:
2696 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2697 "child '%s' changed state to %d(%s) successfully without preroll",
2698 GST_ELEMENT_NAME (child), next,
2699 gst_element_state_get_name (next));
2700 have_no_preroll = TRUE;
2703 g_assert_not_reached ();
2706 g_value_reset (&data);
2709 case GST_ITERATOR_RESYNC:
2710 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "iterator doing resync");
2711 gst_iterator_resync (it);
2714 case GST_ITERATOR_DONE:
2715 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "iterator done");
2721 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2722 if (G_UNLIKELY (ret == GST_STATE_CHANGE_FAILURE))
2725 if (have_no_preroll) {
2726 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
2727 "we have NO_PREROLL elements %s -> NO_PREROLL",
2728 gst_element_state_change_return_get_name (ret));
2729 ret = GST_STATE_CHANGE_NO_PREROLL;
2730 } else if (have_async) {
2731 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
2732 "we have ASYNC elements %s -> ASYNC",
2733 gst_element_state_change_return_get_name (ret));
2734 ret = GST_STATE_CHANGE_ASYNC;
2738 g_value_unset (&data);
2739 gst_iterator_free (it);
2741 GST_OBJECT_LOCK (bin);
2742 bin->polling = FALSE;
2743 /* it's possible that we did not get ASYNC from the children while the bin is
2744 * simulating ASYNC behaviour by posting an ASYNC_DONE message on the bus with
2745 * itself as the source. In that case we still want to check if the state
2746 * change completed. */
2747 if (ret != GST_STATE_CHANGE_ASYNC && !bin->priv->pending_async_done) {
2748 /* no element returned ASYNC and there are no pending async_done messages,
2749 * we can just complete. */
2750 GST_DEBUG_OBJECT (bin, "no async elements");
2753 /* when we get here an ASYNC element was found */
2754 if (GST_STATE_TARGET (bin) <= GST_STATE_READY) {
2755 /* we ignore ASYNC state changes when we go to READY or NULL */
2756 GST_DEBUG_OBJECT (bin, "target state %s <= READY",
2757 gst_element_state_get_name (GST_STATE_TARGET (bin)));
2761 GST_DEBUG_OBJECT (bin, "check async elements");
2762 /* check if all elements managed to commit their state already */
2763 if (!find_message (bin, NULL, GST_MESSAGE_ASYNC_START)) {
2764 /* nothing found, remove all old ASYNC_DONE messages. This can happen when
2765 * all the elements commited their state while we were doing the state
2766 * change. We will still return ASYNC for consistency but we commit the
2767 * state already so that a _get_state() will return immediately. */
2768 bin_remove_messages (bin, NULL, GST_MESSAGE_ASYNC_DONE);
2770 GST_DEBUG_OBJECT (bin, "async elements commited");
2771 bin_handle_async_done (bin, GST_STATE_CHANGE_SUCCESS, FALSE,
2772 GST_CLOCK_TIME_NONE);
2776 bin->priv->pending_async_done = FALSE;
2777 GST_OBJECT_UNLOCK (bin);
2779 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2780 "done changing bin's state from %s to %s, now in %s, ret %s",
2781 gst_element_state_get_name (current),
2782 gst_element_state_get_name (next),
2783 gst_element_state_get_name (GST_STATE (element)),
2784 gst_element_state_change_return_get_name (ret));
2791 GST_CAT_WARNING_OBJECT (GST_CAT_STATES, element,
2792 "failure (de)activating src pads");
2793 return GST_STATE_CHANGE_FAILURE;
2798 * This function is a utility event handler for seek events.
2799 * It will send the event to all sinks or sources and appropriate
2800 * ghost pads depending on the event-direction.
2802 * Applications are free to override this behaviour and
2803 * implement their own seek handler, but this will work for
2804 * pretty much all cases in practice.
2807 gst_bin_send_event (GstElement * element, GstEvent * event)
2809 GstBin *bin = GST_BIN_CAST (element);
2811 gboolean res = TRUE;
2812 gboolean done = FALSE;
2813 GValue data = { 0, };
2815 if (GST_EVENT_IS_DOWNSTREAM (event)) {
2816 iter = gst_bin_iterate_sources (bin);
2817 GST_DEBUG_OBJECT (bin, "Sending %s event to src children",
2818 GST_EVENT_TYPE_NAME (event));
2820 iter = gst_bin_iterate_sinks (bin);
2821 GST_DEBUG_OBJECT (bin, "Sending %s event to sink children",
2822 GST_EVENT_TYPE_NAME (event));
2826 switch (gst_iterator_next (iter, &data)) {
2827 case GST_ITERATOR_OK:
2829 GstElement *child = g_value_get_object (&data);
2831 gst_event_ref (event);
2832 res &= gst_element_send_event (child, event);
2834 GST_LOG_OBJECT (child, "After handling %s event: %d",
2835 GST_EVENT_TYPE_NAME (event), res);
2837 g_value_reset (&data);
2840 case GST_ITERATOR_RESYNC:
2841 gst_iterator_resync (iter);
2844 case GST_ITERATOR_DONE:
2847 case GST_ITERATOR_ERROR:
2848 g_assert_not_reached ();
2852 g_value_unset (&data);
2853 gst_iterator_free (iter);
2855 if (GST_EVENT_IS_DOWNSTREAM (event)) {
2856 iter = gst_element_iterate_sink_pads (GST_ELEMENT (bin));
2857 GST_DEBUG_OBJECT (bin, "Sending %s event to sink pads",
2858 GST_EVENT_TYPE_NAME (event));
2860 iter = gst_element_iterate_src_pads (GST_ELEMENT (bin));
2861 GST_DEBUG_OBJECT (bin, "Sending %s event to src pads",
2862 GST_EVENT_TYPE_NAME (event));
2867 switch (gst_iterator_next (iter, &data)) {
2868 case GST_ITERATOR_OK:
2870 GstPad *pad = g_value_get_object (&data);
2872 gst_event_ref (event);
2873 res &= gst_pad_send_event (pad, event);
2874 GST_LOG_OBJECT (pad, "After handling %s event: %d",
2875 GST_EVENT_TYPE_NAME (event), res);
2878 case GST_ITERATOR_RESYNC:
2879 gst_iterator_resync (iter);
2882 case GST_ITERATOR_DONE:
2885 case GST_ITERATOR_ERROR:
2886 g_assert_not_reached ();
2891 g_value_unset (&data);
2892 gst_iterator_free (iter);
2893 gst_event_unref (event);
2898 /* this is the function called by the threadpool. When async elements commit
2899 * their state, this function will attempt to bring the bin to the next state.
2902 gst_bin_continue_func (BinContinueData * data)
2905 GstState current, next, pending;
2906 GstStateChange transition;
2909 pending = data->pending;
2911 GST_DEBUG_OBJECT (bin, "waiting for state lock");
2912 GST_STATE_LOCK (bin);
2914 GST_DEBUG_OBJECT (bin, "doing state continue");
2915 GST_OBJECT_LOCK (bin);
2917 /* if a new state change happened after this thread was scheduled, we return
2919 if (data->cookie != GST_ELEMENT_CAST (bin)->state_cookie)
2922 current = GST_STATE (bin);
2923 next = GST_STATE_GET_NEXT (current, pending);
2924 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2926 GST_STATE_NEXT (bin) = next;
2927 GST_STATE_PENDING (bin) = pending;
2929 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
2930 GST_OBJECT_UNLOCK (bin);
2932 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
2933 "continue state change %s to %s, final %s",
2934 gst_element_state_get_name (current),
2935 gst_element_state_get_name (next), gst_element_state_get_name (pending));
2937 gst_element_change_state (GST_ELEMENT_CAST (bin), transition);
2939 GST_STATE_UNLOCK (bin);
2940 GST_DEBUG_OBJECT (bin, "state continue done");
2942 gst_object_unref (bin);
2943 g_slice_free (BinContinueData, data);
2948 GST_OBJECT_UNLOCK (bin);
2949 GST_STATE_UNLOCK (bin);
2950 GST_DEBUG_OBJECT (bin, "state continue aborted due to intervening change");
2951 gst_object_unref (bin);
2952 g_slice_free (BinContinueData, data);
2957 static GstBusSyncReply
2958 bin_bus_handler (GstBus * bus, GstMessage * message, GstBin * bin)
2960 GstBinClass *bclass;
2962 bclass = GST_BIN_GET_CLASS (bin);
2963 if (bclass->handle_message)
2964 bclass->handle_message (bin, message);
2966 gst_message_unref (message);
2968 return GST_BUS_DROP;
2972 bin_push_state_continue (BinContinueData * data)
2979 klass = GST_BIN_GET_CLASS (bin);
2981 GST_DEBUG_OBJECT (bin, "pushing continue on thread pool");
2982 g_thread_pool_push (klass->pool, data, NULL);
2985 /* an element started an async state change, if we were not busy with a state
2986 * change, we perform a lost state.
2987 * This function is called with the OBJECT lock.
2990 bin_handle_async_start (GstBin * bin)
2992 GstState old_state, new_state;
2994 GstMessage *amessage = NULL;
2996 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_FAILURE)
2999 /* get our toplevel state */
3000 toplevel = BIN_IS_TOPLEVEL (bin);
3002 /* prepare an ASYNC_START message, we always post the start message even if we
3003 * are busy with a state change or when we are NO_PREROLL. */
3005 /* non toplevel bin, prepare async-start for the parent */
3006 amessage = gst_message_new_async_start (GST_OBJECT_CAST (bin));
3008 if (bin->polling || GST_STATE_PENDING (bin) != GST_STATE_VOID_PENDING)
3011 /* async starts are ignored when we are NO_PREROLL */
3012 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_NO_PREROLL)
3013 goto was_no_preroll;
3015 old_state = GST_STATE (bin);
3017 /* when we PLAYING we go back to PAUSED, when preroll happens, we go back to
3018 * PLAYING after optionally redistributing the base_time. */
3019 if (old_state > GST_STATE_PAUSED)
3020 new_state = GST_STATE_PAUSED;
3022 new_state = old_state;
3024 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
3025 "lost state of %s, new %s", gst_element_state_get_name (old_state),
3026 gst_element_state_get_name (new_state));
3028 GST_STATE (bin) = new_state;
3029 GST_STATE_NEXT (bin) = new_state;
3030 GST_STATE_PENDING (bin) = new_state;
3031 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3032 GST_OBJECT_UNLOCK (bin);
3035 _priv_gst_element_state_changed (GST_ELEMENT_CAST (bin), new_state, new_state,
3040 /* post our ASYNC_START. */
3041 GST_DEBUG_OBJECT (bin, "posting ASYNC_START to parent");
3042 gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
3044 GST_OBJECT_LOCK (bin);
3050 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "we had an error");
3055 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "state change busy");
3056 GST_OBJECT_UNLOCK (bin);
3061 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "ignoring, we are NO_PREROLL");
3062 GST_OBJECT_UNLOCK (bin);
3067 /* this function is called when there are no more async elements in the bin. We
3068 * post a state changed message and an ASYNC_DONE message.
3069 * This function is called with the OBJECT lock.
3072 bin_handle_async_done (GstBin * bin, GstStateChangeReturn ret,
3073 gboolean flag_pending, GstClockTime running_time)
3075 GstState current, pending, target;
3076 GstStateChangeReturn old_ret;
3077 GstState old_state, old_next;
3078 gboolean toplevel, state_changed = FALSE;
3079 GstMessage *amessage = NULL;
3080 BinContinueData *cont = NULL;
3082 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_FAILURE)
3085 pending = GST_STATE_PENDING (bin);
3090 /* check if there is something to commit */
3091 if (pending == GST_STATE_VOID_PENDING)
3092 goto nothing_pending;
3094 old_ret = GST_STATE_RETURN (bin);
3095 GST_STATE_RETURN (bin) = ret;
3097 /* move to the next target state */
3098 target = GST_STATE_TARGET (bin);
3099 pending = GST_STATE_PENDING (bin) = target;
3101 amessage = gst_message_new_async_done (GST_OBJECT_CAST (bin), running_time);
3103 old_state = GST_STATE (bin);
3104 /* this is the state we should go to next */
3105 old_next = GST_STATE_NEXT (bin);
3107 if (old_next != GST_STATE_PLAYING) {
3108 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3109 "committing state from %s to %s, old pending %s",
3110 gst_element_state_get_name (old_state),
3111 gst_element_state_get_name (old_next),
3112 gst_element_state_get_name (pending));
3114 /* update current state */
3115 current = GST_STATE (bin) = old_next;
3117 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3118 "setting state from %s to %s, pending %s",
3119 gst_element_state_get_name (old_state),
3120 gst_element_state_get_name (old_state),
3121 gst_element_state_get_name (pending));
3122 current = old_state;
3125 /* get our toplevel state */
3126 toplevel = BIN_IS_TOPLEVEL (bin);
3128 /* see if we reached the final state. If we are not toplevel, we also have to
3129 * stop here, the parent will continue our state. */
3130 if ((pending == current) || !toplevel) {
3131 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3132 "completed state change, pending VOID");
3134 /* mark VOID pending */
3135 pending = GST_STATE_VOID_PENDING;
3136 GST_STATE_PENDING (bin) = pending;
3137 GST_STATE_NEXT (bin) = GST_STATE_VOID_PENDING;
3139 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3140 "continue state change, pending %s",
3141 gst_element_state_get_name (pending));
3143 cont = g_slice_new (BinContinueData);
3145 /* ref to the bin */
3146 cont->bin = gst_object_ref (bin);
3147 /* cookie to detect concurrent state change */
3148 cont->cookie = GST_ELEMENT_CAST (bin)->state_cookie;
3149 /* pending target state */
3150 cont->pending = pending;
3152 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3153 GST_STATE_NEXT (bin) = GST_STATE_GET_NEXT (old_state, pending);
3156 if (old_next != GST_STATE_PLAYING) {
3157 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC) {
3158 state_changed = TRUE;
3161 GST_OBJECT_UNLOCK (bin);
3163 if (state_changed) {
3164 _priv_gst_element_state_changed (GST_ELEMENT_CAST (bin), old_state,
3168 /* post our combined ASYNC_DONE when all is ASYNC_DONE. */
3169 GST_DEBUG_OBJECT (bin, "posting ASYNC_DONE to parent");
3170 gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
3173 GST_OBJECT_LOCK (bin);
3175 /* toplevel, start continue state */
3176 GST_DEBUG_OBJECT (bin, "all async-done, starting state continue");
3177 bin_push_state_continue (cont);
3179 GST_DEBUG_OBJECT (bin, "state change complete");
3180 GST_STATE_BROADCAST (bin);
3186 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "we had an error");
3191 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "state change busy");
3192 /* if we were busy with a state change and we are requested to flag a
3193 * pending async done, we do so here */
3195 bin->priv->pending_async_done = TRUE;
3200 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin, "nothing pending");
3206 bin_do_eos (GstBin * bin)
3211 GST_OBJECT_LOCK (bin);
3212 /* If all sinks are EOS, we're in PLAYING and no state change is pending
3213 * we forward the EOS message to the parent bin or application
3215 eos = GST_STATE (bin) == GST_STATE_PLAYING
3216 && GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING
3217 && is_eos (bin, &seqnum);
3218 GST_OBJECT_UNLOCK (bin);
3221 && g_atomic_int_compare_and_exchange (&bin->priv->posted_eos, FALSE,
3223 GstMessage *tmessage;
3225 /* Clear out any further messages, and reset posted_eos so we can
3226 detect any new EOS that happens (eg, after a seek). Since all
3227 sinks have now posted an EOS, there will be no further EOS events
3228 seen unless there is a new logical EOS */
3229 GST_OBJECT_LOCK (bin);
3230 bin_remove_messages (bin, NULL, GST_MESSAGE_EOS);
3231 bin->priv->posted_eos = FALSE;
3232 GST_OBJECT_UNLOCK (bin);
3234 tmessage = gst_message_new_eos (GST_OBJECT_CAST (bin));
3235 gst_message_set_seqnum (tmessage, seqnum);
3236 GST_DEBUG_OBJECT (bin,
3237 "all sinks posted EOS, posting seqnum #%" G_GUINT32_FORMAT, seqnum);
3238 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3243 bin_do_stream_start (GstBin * bin)
3246 gboolean stream_start;
3247 gboolean have_group_id = FALSE;
3250 GST_OBJECT_LOCK (bin);
3251 /* If all sinks are STREAM_START we forward the STREAM_START message
3252 * to the parent bin or application
3254 stream_start = is_stream_start (bin, &seqnum, &have_group_id, &group_id);
3255 GST_OBJECT_UNLOCK (bin);
3258 GstMessage *tmessage;
3260 GST_OBJECT_LOCK (bin);
3261 bin_remove_messages (bin, NULL, GST_MESSAGE_STREAM_START);
3262 GST_OBJECT_UNLOCK (bin);
3264 tmessage = gst_message_new_stream_start (GST_OBJECT_CAST (bin));
3265 gst_message_set_seqnum (tmessage, seqnum);
3267 gst_message_set_group_id (tmessage, group_id);
3269 GST_DEBUG_OBJECT (bin,
3270 "all sinks posted STREAM_START, posting seqnum #%" G_GUINT32_FORMAT,
3272 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3276 /* must be called with the object lock. This function releases the lock to post
3279 bin_do_message_forward (GstBin * bin, GstMessage * message)
3281 if (bin->priv->message_forward) {
3282 GstMessage *forwarded;
3284 GST_DEBUG_OBJECT (bin, "pass %s message upward",
3285 GST_MESSAGE_TYPE_NAME (message));
3286 GST_OBJECT_UNLOCK (bin);
3288 /* we need to convert these messages to element messages so that our parent
3289 * bin can easily ignore them and so that the application can easily
3290 * distinguish between the internally forwarded and the real messages. */
3291 forwarded = gst_message_new_element (GST_OBJECT_CAST (bin),
3292 gst_structure_new ("GstBinForwarded",
3293 "message", GST_TYPE_MESSAGE, message, NULL));
3295 gst_element_post_message (GST_ELEMENT_CAST (bin), forwarded);
3297 GST_OBJECT_LOCK (bin);
3302 gst_bin_update_context (GstBin * bin, GstContext * context)
3305 const gchar *context_type;
3307 GST_OBJECT_LOCK (bin);
3308 context_type = gst_context_get_context_type (context);
3309 for (l = bin->priv->contexts; l; l = l->next) {
3310 GstContext *tmp = l->data;
3311 const gchar *tmp_type = gst_context_get_context_type (tmp);
3313 /* Always store newest context but never replace
3314 * a persistent one by a non-persistent one */
3315 if (strcmp (context_type, tmp_type) == 0 &&
3316 (gst_context_is_persistent (context) ||
3317 !gst_context_is_persistent (tmp))) {
3318 gst_context_replace ((GstContext **) & l->data, context);
3322 /* Not found? Add */
3324 bin->priv->contexts =
3325 g_list_prepend (bin->priv->contexts, gst_context_ref (context));
3326 GST_OBJECT_UNLOCK (bin);
3329 /* handle child messages:
3331 * This method is called synchronously when a child posts a message on
3334 * GST_MESSAGE_EOS: This message is only posted by sinks
3335 * in the PLAYING state. If all sinks posted the EOS message, post
3338 * GST_MESSAGE_STATE_DIRTY: Deprecated
3340 * GST_MESSAGE_SEGMENT_START: just collect, never forward upwards. If an
3341 * element posts segment_start twice, only the last message is kept.
3343 * GST_MESSAGE_SEGMENT_DONE: replace SEGMENT_START message from same poster
3344 * with the segment_done message. If there are no more segment_start
3345 * messages, post segment_done message upwards.
3347 * GST_MESSAGE_DURATION_CHANGED: clear any cached durations.
3348 * Whenever someone performs a duration query on the bin, we store the
3349 * result so we can answer it quicker the next time. Any element that
3350 * changes its duration marks our cached values invalid.
3351 * This message is also posted upwards. This is currently disabled
3352 * because too many elements don't post DURATION_CHANGED messages when
3353 * the duration changes.
3355 * GST_MESSAGE_CLOCK_LOST: This message is posted by an element when it
3356 * can no longer provide a clock. The default bin behaviour is to
3357 * check if the lost clock was the one provided by the bin. If so and
3358 * we are currently in the PLAYING state, we forward the message to
3360 * This message is also generated when we remove a clock provider from
3361 * a bin. If this message is received by the application, it should
3362 * PAUSE the pipeline and set it back to PLAYING to force a new clock
3363 * and a new base_time distribution.
3365 * GST_MESSAGE_CLOCK_PROVIDE: This message is generated when an element
3366 * can provide a clock. This mostly happens when we add a new clock
3367 * provider to the bin. The default behaviour of the bin is to mark the
3368 * currently selected clock as dirty, which will perform a clock
3369 * recalculation the next time we are asked to provide a clock.
3370 * This message is never sent to the application but is forwarded to
3373 * GST_MESSAGE_ASYNC_START: Create an internal ELEMENT message that stores
3374 * the state of the element and the fact that the element will need a
3375 * new base_time. This message is not forwarded to the application.
3377 * GST_MESSAGE_ASYNC_DONE: Find the internal ELEMENT message we kept for the
3378 * element when it posted ASYNC_START. If all elements are done, post a
3379 * ASYNC_DONE message to the parent.
3381 * OTHER: post upwards.
3384 gst_bin_handle_message_func (GstBin * bin, GstMessage * message)
3387 GstMessageType type;
3388 GstMessage *tmessage;
3391 src = GST_MESSAGE_SRC (message);
3392 type = GST_MESSAGE_TYPE (message);
3394 GST_DEBUG_OBJECT (bin, "[msg %p] handling child %s message of type %s",
3395 message, src ? GST_ELEMENT_NAME (src) : "(NULL)",
3396 GST_MESSAGE_TYPE_NAME (message));
3399 case GST_MESSAGE_ERROR:
3401 GST_OBJECT_LOCK (bin);
3403 GST_DEBUG_OBJECT (bin, "got ERROR message, unlocking state change");
3404 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_FAILURE;
3405 GST_STATE_BROADCAST (bin);
3406 GST_OBJECT_UNLOCK (bin);
3410 case GST_MESSAGE_EOS:
3413 /* collect all eos messages from the children */
3414 GST_OBJECT_LOCK (bin);
3415 bin_do_message_forward (bin, message);
3416 /* ref message for future use */
3417 bin_replace_message (bin, message, GST_MESSAGE_EOS);
3418 GST_OBJECT_UNLOCK (bin);
3423 case GST_MESSAGE_STREAM_START:
3426 /* collect all stream_start messages from the children */
3427 GST_OBJECT_LOCK (bin);
3428 /* ref message for future use */
3429 bin_replace_message (bin, message, GST_MESSAGE_STREAM_START);
3430 GST_OBJECT_UNLOCK (bin);
3432 bin_do_stream_start (bin);
3435 case GST_MESSAGE_STATE_DIRTY:
3437 GST_WARNING_OBJECT (bin, "received deprecated STATE_DIRTY message");
3440 gst_message_unref (message);
3443 case GST_MESSAGE_SEGMENT_START:{
3444 gboolean post = FALSE;
3448 gst_message_parse_segment_start (message, &format, &position);
3449 seqnum = gst_message_get_seqnum (message);
3451 GST_OBJECT_LOCK (bin);
3452 bin_do_message_forward (bin, message);
3453 /* if this is the first segment-start, post to parent but not to the
3455 if (!find_message (bin, NULL, GST_MESSAGE_SEGMENT_START) &&
3456 (GST_OBJECT_PARENT (bin) != NULL)) {
3459 /* replace any previous segment_start message from this source
3460 * with the new segment start message */
3461 bin_replace_message (bin, message, GST_MESSAGE_SEGMENT_START);
3462 GST_OBJECT_UNLOCK (bin);
3464 tmessage = gst_message_new_segment_start (GST_OBJECT_CAST (bin),
3466 gst_message_set_seqnum (tmessage, seqnum);
3468 /* post segment start with initial format and position. */
3469 GST_DEBUG_OBJECT (bin, "posting SEGMENT_START (%u) bus message: %p",
3471 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3475 case GST_MESSAGE_SEGMENT_DONE:
3477 gboolean post = FALSE;
3481 gst_message_parse_segment_done (message, &format, &position);
3482 seqnum = gst_message_get_seqnum (message);
3484 GST_OBJECT_LOCK (bin);
3485 bin_do_message_forward (bin, message);
3486 bin_replace_message (bin, message, GST_MESSAGE_SEGMENT_START);
3487 /* if there are no more segment_start messages, everybody posted
3488 * a segment_done and we can post one on the bus. */
3490 /* we don't care who still has a pending segment start */
3491 if (!find_message (bin, NULL, GST_MESSAGE_SEGMENT_START)) {
3494 /* remove all old segment_done messages */
3495 bin_remove_messages (bin, NULL, GST_MESSAGE_SEGMENT_DONE);
3497 GST_OBJECT_UNLOCK (bin);
3499 tmessage = gst_message_new_segment_done (GST_OBJECT_CAST (bin),
3501 gst_message_set_seqnum (tmessage, seqnum);
3503 /* post segment done with latest format and position. */
3504 GST_DEBUG_OBJECT (bin, "posting SEGMENT_DONE (%u) bus message: %p",
3506 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3510 case GST_MESSAGE_DURATION_CHANGED:
3512 /* FIXME: remove all cached durations, next time somebody asks
3513 * for duration, we will recalculate. */
3515 GST_OBJECT_LOCK (bin);
3516 bin_remove_messages (bin, NULL, GST_MESSAGE_DURATION_CHANGED);
3517 GST_OBJECT_UNLOCK (bin);
3521 case GST_MESSAGE_CLOCK_LOST:
3523 GstClock **provided_clock_p;
3524 GstElement **clock_provider_p;
3525 gboolean playing, toplevel, provided, forward;
3528 gst_message_parse_clock_lost (message, &clock);
3530 GST_OBJECT_LOCK (bin);
3531 bin->clock_dirty = TRUE;
3532 /* if we lost the clock that we provided, post to parent but
3533 * only if we are not a top-level bin or PLAYING.
3534 * The reason for this is that applications should be able
3535 * to PAUSE/PLAY if they receive this message without worrying
3536 * about the state of the pipeline. */
3537 provided = (clock == bin->provided_clock);
3538 playing = (GST_STATE (bin) == GST_STATE_PLAYING);
3539 toplevel = GST_OBJECT_PARENT (bin) == NULL;
3540 forward = provided && (playing || !toplevel);
3542 GST_DEBUG_OBJECT (bin,
3543 "Lost clock %" GST_PTR_FORMAT " provided by %" GST_PTR_FORMAT,
3544 bin->provided_clock, bin->clock_provider);
3545 provided_clock_p = &bin->provided_clock;
3546 clock_provider_p = &bin->clock_provider;
3547 gst_object_replace ((GstObject **) provided_clock_p, NULL);
3548 gst_object_replace ((GstObject **) clock_provider_p, NULL);
3550 GST_DEBUG_OBJECT (bin, "provided %d, playing %d, forward %d",
3551 provided, playing, forward);
3552 GST_OBJECT_UNLOCK (bin);
3558 gst_message_unref (message);
3561 case GST_MESSAGE_CLOCK_PROVIDE:
3565 GST_OBJECT_LOCK (bin);
3566 bin->clock_dirty = TRUE;
3567 /* a new clock is available, post to parent but not
3568 * to the application */
3569 forward = GST_OBJECT_PARENT (bin) != NULL;
3570 GST_OBJECT_UNLOCK (bin);
3576 gst_message_unref (message);
3579 case GST_MESSAGE_ASYNC_START:
3583 GST_DEBUG_OBJECT (bin, "ASYNC_START message %p, %s", message,
3584 src ? GST_OBJECT_NAME (src) : "(NULL)");
3586 GST_OBJECT_LOCK (bin);
3587 bin_do_message_forward (bin, message);
3589 /* we ignore the message if we are going to <= READY */
3590 if ((target = GST_STATE_TARGET (bin)) <= GST_STATE_READY)
3591 goto ignore_start_message;
3593 /* takes ownership of the message */
3594 bin_replace_message (bin, message, GST_MESSAGE_ASYNC_START);
3596 bin_handle_async_start (bin);
3597 GST_OBJECT_UNLOCK (bin);
3600 ignore_start_message:
3602 GST_DEBUG_OBJECT (bin, "ignoring message, target %s",
3603 gst_element_state_get_name (target));
3604 GST_OBJECT_UNLOCK (bin);
3605 gst_message_unref (message);
3609 case GST_MESSAGE_ASYNC_DONE:
3611 GstClockTime running_time;
3614 GST_DEBUG_OBJECT (bin, "ASYNC_DONE message %p, %s", message,
3615 src ? GST_OBJECT_NAME (src) : "(NULL)");
3617 gst_message_parse_async_done (message, &running_time);
3619 GST_OBJECT_LOCK (bin);
3620 bin_do_message_forward (bin, message);
3622 /* ignore messages if we are shutting down */
3623 if ((target = GST_STATE_TARGET (bin)) <= GST_STATE_READY)
3624 goto ignore_done_message;
3626 bin_replace_message (bin, message, GST_MESSAGE_ASYNC_START);
3627 /* if there are no more ASYNC_START messages, everybody posted
3628 * a ASYNC_DONE and we can post one on the bus. When checking, we
3629 * don't care who still has a pending ASYNC_START */
3630 if (!find_message (bin, NULL, GST_MESSAGE_ASYNC_START)) {
3631 /* nothing found, remove all old ASYNC_DONE messages */
3632 bin_remove_messages (bin, NULL, GST_MESSAGE_ASYNC_DONE);
3634 GST_DEBUG_OBJECT (bin, "async elements commited");
3635 /* when we get an async done message when a state change was busy, we
3636 * need to set the pending_done flag so that at the end of the state
3637 * change we can see if we need to verify pending async elements, hence
3638 * the TRUE argument here. */
3639 bin_handle_async_done (bin, GST_STATE_CHANGE_SUCCESS, TRUE,
3642 GST_DEBUG_OBJECT (bin, "there are more async elements pending");
3644 GST_OBJECT_UNLOCK (bin);
3647 ignore_done_message:
3649 GST_DEBUG_OBJECT (bin, "ignoring message, target %s",
3650 gst_element_state_get_name (target));
3651 GST_OBJECT_UNLOCK (bin);
3652 gst_message_unref (message);
3656 case GST_MESSAGE_STRUCTURE_CHANGE:
3660 gst_message_parse_structure_change (message, NULL, NULL, &busy);
3662 GST_OBJECT_LOCK (bin);
3664 /* while the pad is busy, avoid following it when doing state changes.
3665 * Don't update the cookie yet, we will do that after the structure
3666 * change finished and we are ready to inspect the new updated
3668 bin_replace_message (bin, message, GST_MESSAGE_STRUCTURE_CHANGE);
3671 /* a pad link/unlink ended, signal the state change iterator that we
3672 * need to resync by updating the structure_cookie. */
3673 bin_remove_messages (bin, GST_MESSAGE_SRC (message),
3674 GST_MESSAGE_STRUCTURE_CHANGE);
3675 if (!GST_BIN_IS_NO_RESYNC (bin))
3676 bin->priv->structure_cookie++;
3678 GST_OBJECT_UNLOCK (bin);
3681 gst_message_unref (message);
3685 case GST_MESSAGE_NEED_CONTEXT:{
3686 const gchar *context_type;
3689 gst_message_parse_context_type (message, &context_type);
3690 GST_OBJECT_LOCK (bin);
3691 for (l = bin->priv->contexts; l; l = l->next) {
3692 GstContext *tmp = l->data;
3693 const gchar *tmp_type = gst_context_get_context_type (tmp);
3695 if (strcmp (context_type, tmp_type) == 0) {
3696 gst_element_set_context (GST_ELEMENT (src), l->data);
3700 GST_OBJECT_UNLOCK (bin);
3702 /* Forward if we couldn't answer the message */
3706 gst_message_unref (message);
3711 case GST_MESSAGE_HAVE_CONTEXT:{
3712 GstContext *context;
3714 gst_message_parse_have_context (message, &context);
3715 gst_bin_update_context (bin, context);
3716 gst_context_unref (context);
3728 /* Send all other messages upward */
3729 GST_DEBUG_OBJECT (bin, "posting message upward");
3730 gst_element_post_message (GST_ELEMENT_CAST (bin), message);
3735 /* generic struct passed to all query fold methods */
3744 typedef void (*QueryInitFunction) (GstBin * bin, QueryFold * fold);
3745 typedef void (*QueryDoneFunction) (GstBin * bin, QueryFold * fold);
3747 /* for duration/position we collect all durations/positions and take
3748 * the MAX of all valid results */
3750 bin_query_min_max_init (GstBin * bin, QueryFold * fold)
3758 bin_query_duration_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
3760 gboolean res = FALSE;
3761 GstObject *item = g_value_get_object (vitem);
3762 if (GST_IS_PAD (item))
3763 res = gst_pad_query (GST_PAD (item), fold->query);
3765 res = gst_element_query (GST_ELEMENT (item), fold->query);
3770 g_value_set_boolean (ret, TRUE);
3772 gst_query_parse_duration (fold->query, NULL, &duration);
3774 GST_DEBUG_OBJECT (item, "got duration %" G_GINT64_FORMAT, duration);
3776 if (duration == -1) {
3777 /* duration query succeeded, but duration is unknown */
3782 if (duration > fold->max)
3783 fold->max = duration;
3790 bin_query_duration_done (GstBin * bin, QueryFold * fold)
3794 gst_query_parse_duration (fold->query, &format, NULL);
3795 /* store max in query result */
3796 gst_query_set_duration (fold->query, format, fold->max);
3798 GST_DEBUG_OBJECT (bin, "max duration %" G_GINT64_FORMAT, fold->max);
3800 /* FIXME: re-implement duration caching */
3803 GST_OBJECT_LOCK (bin);
3804 bin->messages = g_list_prepend (bin->messages,
3805 gst_message_new_duration (GST_OBJECT_CAST (bin), format, fold->max));
3806 GST_OBJECT_UNLOCK (bin);
3811 bin_query_position_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
3813 gboolean res = FALSE;
3814 GstObject *item = g_value_get_object (vitem);
3815 if (GST_IS_PAD (item))
3816 res = gst_pad_query (GST_PAD (item), fold->query);
3818 res = gst_element_query (GST_ELEMENT (item), fold->query);
3823 g_value_set_boolean (ret, TRUE);
3825 gst_query_parse_position (fold->query, NULL, &position);
3827 GST_DEBUG_OBJECT (item, "got position %" G_GINT64_FORMAT, position);
3829 if (position > fold->max)
3830 fold->max = position;
3837 bin_query_position_done (GstBin * bin, QueryFold * fold)
3841 gst_query_parse_position (fold->query, &format, NULL);
3842 /* store max in query result */
3843 gst_query_set_position (fold->query, format, fold->max);
3845 GST_DEBUG_OBJECT (bin, "max position %" G_GINT64_FORMAT, fold->max);
3849 bin_query_latency_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
3851 gboolean res = FALSE;
3852 GstObject *item = g_value_get_object (vitem);
3853 if (GST_IS_PAD (item))
3854 res = gst_pad_query (GST_PAD (item), fold->query);
3856 res = gst_element_query (GST_ELEMENT (item), fold->query);
3858 GstClockTime min, max;
3861 gst_query_parse_latency (fold->query, &live, &min, &max);
3863 GST_DEBUG_OBJECT (item,
3864 "got latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
3865 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
3867 /* for the combined latency we collect the MAX of all min latencies and
3868 * the MIN of all max latencies */
3870 if (min > fold->min)
3872 if (fold->max == -1)
3874 else if (max < fold->max)
3876 if (fold->live == FALSE)
3880 g_value_set_boolean (ret, FALSE);
3881 GST_DEBUG_OBJECT (item, "failed query");
3888 bin_query_latency_done (GstBin * bin, QueryFold * fold)
3890 /* store max in query result */
3891 gst_query_set_latency (fold->query, fold->live, fold->min, fold->max);
3893 GST_DEBUG_OBJECT (bin,
3894 "latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
3895 ", live %d", GST_TIME_ARGS (fold->min), GST_TIME_ARGS (fold->max),
3899 /* generic fold, return first valid result */
3901 bin_query_generic_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
3903 gboolean res = FALSE;
3904 GstObject *item = g_value_get_object (vitem);
3905 if (GST_IS_PAD (item))
3906 res = gst_pad_query (GST_PAD (item), fold->query);
3908 res = gst_element_query (GST_ELEMENT (item), fold->query);
3910 g_value_set_boolean (ret, TRUE);
3911 GST_DEBUG_OBJECT (item, "answered query %p", fold->query);
3914 /* and stop as soon as we have a valid result */
3918 /* Perform a query iteration for the given bin. The query is stored in
3919 * QueryFold and iter should be either a GstPad iterator or a
3920 * GstElement iterator. */
3922 bin_iterate_fold (GstBin * bin, GstIterator * iter, QueryInitFunction fold_init,
3923 QueryDoneFunction fold_done, GstIteratorFoldFunction fold_func,
3924 QueryFold fold_data, gboolean default_return)
3926 gboolean res = default_return;
3928 /* set the result of the query to FALSE initially */
3929 g_value_init (&ret, G_TYPE_BOOLEAN);
3930 g_value_set_boolean (&ret, res);
3933 GstIteratorResult ires;
3935 ires = gst_iterator_fold (iter, fold_func, &ret, &fold_data);
3938 case GST_ITERATOR_RESYNC:
3939 gst_iterator_resync (iter);
3941 fold_init (bin, &fold_data);
3942 g_value_set_boolean (&ret, res);
3944 case GST_ITERATOR_OK:
3945 case GST_ITERATOR_DONE:
3946 res = g_value_get_boolean (&ret);
3947 if (fold_done != NULL && res)
3948 fold_done (bin, &fold_data);
3960 gst_bin_query (GstElement * element, GstQuery * query)
3962 GstBin *bin = GST_BIN_CAST (element);
3964 gboolean default_return = FALSE;
3965 gboolean res = FALSE;
3966 gboolean src_pads_query_result = FALSE;
3967 GstIteratorFoldFunction fold_func;
3968 QueryInitFunction fold_init = NULL;
3969 QueryDoneFunction fold_done = NULL;
3970 QueryFold fold_data;
3972 switch (GST_QUERY_TYPE (query)) {
3973 case GST_QUERY_DURATION:
3975 /* FIXME: implement duration caching in GstBin again */
3980 gst_query_parse_duration (query, &qformat, NULL);
3982 /* find cached duration query */
3983 GST_OBJECT_LOCK (bin);
3984 for (cached = bin->messages; cached; cached = g_list_next (cached)) {
3985 GstMessage *message = (GstMessage *) cached->data;
3987 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_DURATION_CHANGED &&
3988 GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (bin)) {
3992 gst_message_parse_duration (message, &format, &duration);
3994 /* if cached same format, copy duration in query result */
3995 if (format == qformat) {
3996 GST_DEBUG_OBJECT (bin, "return cached duration %" G_GINT64_FORMAT,
3998 GST_OBJECT_UNLOCK (bin);
4000 gst_query_set_duration (query, qformat, duration);
4006 GST_OBJECT_UNLOCK (bin);
4008 GST_FIXME ("implement duration caching in GstBin again");
4010 /* no cached value found, iterate and collect durations */
4011 fold_func = (GstIteratorFoldFunction) bin_query_duration_fold;
4012 fold_init = bin_query_min_max_init;
4013 fold_done = bin_query_duration_done;
4016 case GST_QUERY_POSITION:
4018 fold_func = (GstIteratorFoldFunction) bin_query_position_fold;
4019 fold_init = bin_query_min_max_init;
4020 fold_done = bin_query_position_done;
4023 case GST_QUERY_LATENCY:
4025 fold_func = (GstIteratorFoldFunction) bin_query_latency_fold;
4026 fold_init = bin_query_min_max_init;
4027 fold_done = bin_query_latency_done;
4028 default_return = TRUE;
4032 fold_func = (GstIteratorFoldFunction) bin_query_generic_fold;
4036 fold_data.query = query;
4038 iter = gst_bin_iterate_sinks (bin);
4039 GST_DEBUG_OBJECT (bin, "Sending query %p (type %s) to sink children",
4040 query, GST_QUERY_TYPE_NAME (query));
4043 fold_init (bin, &fold_data);
4046 bin_iterate_fold (bin, iter, fold_init, fold_done, fold_func, fold_data,
4048 gst_iterator_free (iter);
4051 /* Query the source pads of the element */
4052 iter = gst_element_iterate_src_pads (element);
4053 src_pads_query_result =
4054 bin_iterate_fold (bin, iter, fold_init, fold_done, fold_func,
4055 fold_data, default_return);
4056 gst_iterator_free (iter);
4058 if (src_pads_query_result)
4062 GST_DEBUG_OBJECT (bin, "query %p result %d", query, res);
4068 set_context (const GValue * item, gpointer user_data)
4070 GstElement *element = g_value_get_object (item);
4072 gst_element_set_context (element, user_data);
4076 gst_bin_set_context (GstElement * element, GstContext * context)
4079 GstIterator *children;
4081 g_return_if_fail (GST_IS_BIN (element));
4083 bin = GST_BIN (element);
4085 gst_bin_update_context (bin, context);
4087 children = gst_bin_iterate_elements (bin);
4088 while (gst_iterator_foreach (children, set_context,
4089 context) == GST_ITERATOR_RESYNC);
4090 gst_iterator_free (children);
4094 compare_name (const GValue * velement, const gchar * name)
4097 GstElement *element = g_value_get_object (velement);
4099 GST_OBJECT_LOCK (element);
4100 eq = strcmp (GST_ELEMENT_NAME (element), name);
4101 GST_OBJECT_UNLOCK (element);
4107 * gst_bin_get_by_name:
4109 * @name: the element name to search for
4111 * Gets the element with the given name from a bin. This
4112 * function recurses into child bins.
4114 * Returns NULL if no element with the given name is found in the bin.
4116 * MT safe. Caller owns returned reference.
4118 * Returns: (transfer full): the #GstElement with the given name, or NULL
4121 gst_bin_get_by_name (GstBin * bin, const gchar * name)
4123 GstIterator *children;
4124 GValue result = { 0, };
4125 GstElement *element;
4128 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4130 GST_CAT_INFO (GST_CAT_PARENTAGE, "[%s]: looking up child element %s",
4131 GST_ELEMENT_NAME (bin), name);
4133 children = gst_bin_iterate_recurse (bin);
4134 found = gst_iterator_find_custom (children,
4135 (GCompareFunc) compare_name, &result, (gpointer) name);
4136 gst_iterator_free (children);
4139 element = g_value_dup_object (&result);
4140 g_value_unset (&result);
4149 * gst_bin_get_by_name_recurse_up:
4151 * @name: the element name to search for
4153 * Gets the element with the given name from this bin. If the
4154 * element is not found, a recursion is performed on the parent bin.
4157 * - no element with the given name is found in the bin
4159 * MT safe. Caller owns returned reference.
4161 * Returns: (transfer full): the #GstElement with the given name, or NULL
4164 gst_bin_get_by_name_recurse_up (GstBin * bin, const gchar * name)
4168 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4169 g_return_val_if_fail (name != NULL, NULL);
4171 result = gst_bin_get_by_name (bin, name);
4176 parent = gst_object_get_parent (GST_OBJECT_CAST (bin));
4178 if (GST_IS_BIN (parent)) {
4179 result = gst_bin_get_by_name_recurse_up (GST_BIN_CAST (parent), name);
4181 gst_object_unref (parent);
4189 compare_interface (const GValue * velement, GValue * interface)
4191 GstElement *element = g_value_get_object (velement);
4192 GType interface_type = (GType) g_value_get_pointer (interface);
4195 if (G_TYPE_CHECK_INSTANCE_TYPE (element, interface_type)) {
4204 * gst_bin_get_by_interface:
4206 * @iface: the #GType of an interface
4208 * Looks for an element inside the bin that implements the given
4209 * interface. If such an element is found, it returns the element.
4210 * You can cast this element to the given interface afterwards. If you want
4211 * all elements that implement the interface, use
4212 * gst_bin_iterate_all_by_interface(). This function recurses into child bins.
4214 * MT safe. Caller owns returned reference.
4216 * Returns: (transfer full): A #GstElement inside the bin implementing the interface
4219 gst_bin_get_by_interface (GstBin * bin, GType iface)
4221 GstIterator *children;
4222 GValue result = { 0, };
4223 GstElement *element;
4225 GValue viface = { 0, };
4227 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4228 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface), NULL);
4230 g_value_init (&viface, G_TYPE_POINTER);
4231 g_value_set_pointer (&viface, (gpointer) iface);
4233 children = gst_bin_iterate_recurse (bin);
4234 found = gst_iterator_find_custom (children, (GCompareFunc) compare_interface,
4236 gst_iterator_free (children);
4239 element = g_value_dup_object (&result);
4240 g_value_unset (&result);
4244 g_value_unset (&viface);
4250 * gst_bin_iterate_all_by_interface:
4252 * @iface: the #GType of an interface
4254 * Looks for all elements inside the bin that implements the given
4255 * interface. You can safely cast all returned elements to the given interface.
4256 * The function recurses inside child bins. The iterator will yield a series
4257 * of #GstElement that should be unreffed after use.
4259 * MT safe. Caller owns returned value.
4261 * Returns: (transfer full): a #GstIterator of #GstElement for all elements
4262 * in the bin implementing the given interface, or NULL
4265 gst_bin_iterate_all_by_interface (GstBin * bin, GType iface)
4267 GstIterator *children;
4268 GstIterator *result;
4269 GValue viface = { 0, };
4271 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4272 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface), NULL);
4274 g_value_init (&viface, G_TYPE_POINTER);
4275 g_value_set_pointer (&viface, (gpointer) iface);
4277 children = gst_bin_iterate_recurse (bin);
4278 result = gst_iterator_filter (children, (GCompareFunc) compare_interface,
4281 g_value_unset (&viface);