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.
29 * @short_description: Base class and element that can contain other elements
31 * #GstBin is an element that can contain other #GstElement, allowing them to be
33 * Pads from the child elements can be ghosted to the bin, see #GstGhostPad.
34 * This makes the bin look like any other elements and enables creation of
35 * higher-level abstraction elements.
37 * A new #GstBin is created with gst_bin_new(). Use a #GstPipeline instead if you
38 * want to create a toplevel bin because a normal bin doesn't have a bus or
39 * handle clock distribution of its own.
41 * After the bin has been created you will typically add elements to it with
42 * gst_bin_add(). You can remove elements with gst_bin_remove().
44 * An element can be retrieved from a bin with gst_bin_get_by_name(), using the
45 * elements name. gst_bin_get_by_name_recurse_up() is mainly used for internal
46 * purposes and will query the parent bins when the element is not found in the
49 * An iterator of elements in a bin can be retrieved with
50 * gst_bin_iterate_elements(). Various other iterators exist to retrieve the
53 * gst_object_unref() is used to drop your reference to the bin.
55 * The #GstBin::element-added signal is fired whenever a new element is added to
56 * the bin. Likewise the #GstBin::element-removed signal is fired whenever an
57 * element is removed from the bin.
61 * A #GstBin internally intercepts every #GstMessage posted by its children and
62 * implements the following default behaviour for each of them:
64 * * GST_MESSAGE_EOS: This message is only posted by sinks in the PLAYING
65 * state. If all sinks posted the EOS message, this bin will post and EOS
68 * * GST_MESSAGE_SEGMENT_START: Just collected and never forwarded upwards.
69 * The messages are used to decide when all elements have completed playback
72 * * GST_MESSAGE_SEGMENT_DONE: Is posted by #GstBin when all elements that posted
73 * a SEGMENT_START have posted a SEGMENT_DONE.
75 * * GST_MESSAGE_DURATION_CHANGED: Is posted by an element that detected a change
76 * in the stream duration. The default bin behaviour is to clear any
77 * cached duration values so that the next duration query will perform
78 * a full duration recalculation. The duration change is posted to the
79 * application so that it can refetch the new duration with a duration
80 * query. Note that these messages can be posted before the bin is
81 * prerolled, in which case the duration query might fail.
83 * * GST_MESSAGE_CLOCK_LOST: This message is posted by an element when it
84 * can no longer provide a clock. The default bin behaviour is to
85 * check if the lost clock was the one provided by the bin. If so and
86 * the bin is currently in the PLAYING state, the message is forwarded to
88 * This message is also generated when a clock provider is removed from
89 * the bin. If this message is received by the application, it should
90 * PAUSE the pipeline and set it back to PLAYING to force a new clock
93 * * GST_MESSAGE_CLOCK_PROVIDE: This message is generated when an element
94 * can provide a clock. This mostly happens when a new clock
95 * provider is added to the bin. The default behaviour of the bin is to
96 * mark the currently selected clock as dirty, which will perform a clock
97 * recalculation the next time the bin is asked to provide a clock.
98 * This message is never sent tot the application but is forwarded to
99 * the parent of the bin.
101 * * OTHERS: posted upwards.
103 * A #GstBin implements the following default behaviour for answering to a
106 * * GST_QUERY_DURATION:If the query has been asked before with the same format
107 * and the bin is a toplevel bin (ie. has no parent),
108 * use the cached previous value. If no previous value was cached, the
109 * query is sent to all sink elements in the bin and the MAXIMUM of all
110 * values is returned. If the bin is a toplevel bin the value is cached.
111 * If no sinks are available in the bin, the query fails.
113 * * GST_QUERY_POSITION:The query is sent to all sink elements in the bin and the
114 * MAXIMUM of all values is returned. If no sinks are available in the bin,
117 * * OTHERS:the query is forwarded to all sink elements, the result
118 * of the first sink that answers the query successfully is returned. If no
119 * sink is in the bin, the query fails.
121 * A #GstBin will by default forward any event sent to it to all sink
122 * (#GST_EVENT_TYPE_DOWNSTREAM) or source (#GST_EVENT_TYPE_UPSTREAM) elements
123 * depending on the event type.
124 * If all the elements return %TRUE, the bin will also return %TRUE, else %FALSE
125 * is returned. If no elements of the required type are in the bin, the event
126 * handler will return %TRUE.
130 #include "gst_private.h"
132 #include "gstevent.h"
135 #include "gsterror.h"
137 #include "gstutils.h"
138 #include "gstchildproxy.h"
140 GST_DEBUG_CATEGORY_STATIC (bin_debug);
141 #define GST_CAT_DEFAULT bin_debug
143 /* a bin is toplevel if it has no parent or when it is configured to behave like
145 #define BIN_IS_TOPLEVEL(bin) ((GST_OBJECT_PARENT (bin) == NULL) || bin->priv->asynchandling)
147 struct _GstBinPrivate
149 gboolean asynchandling;
150 /* if we get an ASYNC_DONE message from ourselves, this means that the
151 * subclass will simulate ASYNC behaviour without having ASYNC children. When
152 * such an ASYNC_DONE message is posted while we are doing a state change, we
153 * have to process the message after finishing the state change even when no
154 * child returned GST_STATE_CHANGE_ASYNC. */
155 gboolean pending_async_done;
157 guint32 structure_cookie;
164 /* forward messages from our children */
165 gboolean message_forward;
168 gboolean posted_playing;
169 GstElementFlags suppressed_flags;
178 static void gst_bin_dispose (GObject * object);
180 static void gst_bin_set_property (GObject * object, guint prop_id,
181 const GValue * value, GParamSpec * pspec);
182 static void gst_bin_get_property (GObject * object, guint prop_id,
183 GValue * value, GParamSpec * pspec);
185 static GstStateChangeReturn gst_bin_change_state_func (GstElement * element,
186 GstStateChange transition);
187 static gboolean gst_bin_post_message (GstElement * element, GstMessage * msg);
188 static GstStateChangeReturn gst_bin_get_state_func (GstElement * element,
189 GstState * state, GstState * pending, GstClockTime timeout);
190 static void bin_handle_async_done (GstBin * bin, GstStateChangeReturn ret,
191 gboolean flag_pending, GstClockTime running_time);
192 static void bin_handle_async_start (GstBin * bin);
193 static void bin_push_state_continue (GstBin * bin, BinContinueData * data);
194 static void bin_do_eos (GstBin * bin);
196 static gboolean gst_bin_add_func (GstBin * bin, GstElement * element);
197 static gboolean gst_bin_remove_func (GstBin * bin, GstElement * element);
198 static void gst_bin_deep_element_added_func (GstBin * bin, GstBin * sub_bin,
199 GstElement * element);
200 static void gst_bin_deep_element_removed_func (GstBin * bin, GstBin * sub_bin,
201 GstElement * element);
202 static void gst_bin_update_context (GstBin * bin, GstContext * context);
203 static void gst_bin_update_context_unlocked (GstBin * bin,
204 GstContext * context);
207 static void gst_bin_set_index_func (GstElement * element, GstIndex * index);
208 static GstIndex *gst_bin_get_index_func (GstElement * element);
211 static GstClock *gst_bin_provide_clock_func (GstElement * element);
212 static gboolean gst_bin_set_clock_func (GstElement * element, GstClock * clock);
214 static void gst_bin_handle_message_func (GstBin * bin, GstMessage * message);
215 static gboolean gst_bin_send_event (GstElement * element, GstEvent * event);
216 static GstBusSyncReply bin_bus_handler (GstBus * bus,
217 GstMessage * message, GstBin * bin);
218 static gboolean gst_bin_query (GstElement * element, GstQuery * query);
219 static void gst_bin_set_context (GstElement * element, GstContext * context);
221 static gboolean gst_bin_do_latency_func (GstBin * bin);
223 static void bin_remove_messages (GstBin * bin, GstObject * src,
224 GstMessageType types);
225 static void gst_bin_continue_func (GstBin * bin, BinContinueData * data);
226 static gint bin_element_is_sink (GstElement * child, GstBin * bin);
227 static gint bin_element_is_src (GstElement * child, GstBin * bin);
229 static GstIterator *gst_bin_sort_iterator_new (GstBin * bin);
231 /* Bin signals and properties */
238 DEEP_ELEMENT_REMOVED,
242 #define DEFAULT_ASYNC_HANDLING FALSE
243 #define DEFAULT_MESSAGE_FORWARD FALSE
249 PROP_MESSAGE_FORWARD,
253 static void gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data);
255 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
259 static const GInterfaceInfo iface_info = { \
260 gst_bin_child_proxy_init, \
264 g_type_add_interface_static (g_define_type_id, GST_TYPE_CHILD_PROXY, &iface_info); \
266 GST_DEBUG_CATEGORY_INIT (bin_debug, "bin", GST_DEBUG_BOLD, \
267 "debugging info for the 'bin' container element"); \
271 #define gst_bin_parent_class parent_class
272 G_DEFINE_TYPE_WITH_CODE (GstBin, gst_bin, GST_TYPE_ELEMENT,
273 G_ADD_PRIVATE (GstBin)
277 gst_bin_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
283 bin = GST_BIN_CAST (child_proxy);
285 GST_OBJECT_LOCK (bin);
286 if ((res = g_list_nth_data (bin->children, index)))
287 gst_object_ref (res);
288 GST_OBJECT_UNLOCK (bin);
290 return (GObject *) res;
294 gst_bin_child_proxy_get_children_count (GstChildProxy * child_proxy)
299 bin = GST_BIN_CAST (child_proxy);
301 GST_OBJECT_LOCK (bin);
302 num = bin->numchildren;
303 GST_OBJECT_UNLOCK (bin);
309 gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data)
311 GstChildProxyInterface *iface = g_iface;
313 iface->get_children_count = gst_bin_child_proxy_get_children_count;
314 iface->get_child_by_index = gst_bin_child_proxy_get_child_by_index;
318 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
319 GValue * return_accu, const GValue * handler_return, gpointer dummy)
323 myboolean = g_value_get_boolean (handler_return);
324 if (!(ihint->run_type & G_SIGNAL_RUN_CLEANUP))
325 g_value_set_boolean (return_accu, myboolean);
327 GST_DEBUG ("invocation %d, %d", ihint->run_type, myboolean);
334 gst_bin_class_init (GstBinClass * klass)
336 GObjectClass *gobject_class;
337 GstElementClass *gstelement_class;
339 gobject_class = (GObjectClass *) klass;
340 gstelement_class = (GstElementClass *) klass;
342 gobject_class->set_property = gst_bin_set_property;
343 gobject_class->get_property = gst_bin_get_property;
346 * GstBin:async-handling:
348 * If set to %TRUE, the bin will handle asynchronous state changes.
349 * This should be used only if the bin subclass is modifying the state
350 * of its children on its own.
352 g_object_class_install_property (gobject_class, PROP_ASYNC_HANDLING,
353 g_param_spec_boolean ("async-handling", "Async Handling",
354 "The bin will handle Asynchronous state changes",
355 DEFAULT_ASYNC_HANDLING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
358 * GstBin::element-added:
360 * @element: the #GstElement that was added to the bin
362 * Will be emitted after the element was added to the bin.
364 gst_bin_signals[ELEMENT_ADDED] =
365 g_signal_new ("element-added", G_TYPE_FROM_CLASS (klass),
366 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_added), NULL,
367 NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
369 * GstBin::element-removed:
371 * @element: the #GstElement that was removed from the bin
373 * Will be emitted after the element was removed from the bin.
375 gst_bin_signals[ELEMENT_REMOVED] =
376 g_signal_new ("element-removed", G_TYPE_FROM_CLASS (klass),
377 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_removed), NULL,
378 NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
380 * GstBin::deep-element-added:
382 * @sub_bin: the #GstBin the element was added to
383 * @element: the #GstElement that was added to @sub_bin
385 * Will be emitted after the element was added to sub_bin.
389 gst_bin_signals[DEEP_ELEMENT_ADDED] =
390 g_signal_new ("deep-element-added", G_TYPE_FROM_CLASS (klass),
391 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, deep_element_added),
392 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, GST_TYPE_BIN,
395 * GstBin::deep-element-removed:
397 * @sub_bin: the #GstBin the element was removed from
398 * @element: the #GstElement that was removed from @sub_bin
400 * Will be emitted after the element was removed from sub_bin.
404 gst_bin_signals[DEEP_ELEMENT_REMOVED] =
405 g_signal_new ("deep-element-removed", G_TYPE_FROM_CLASS (klass),
406 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, deep_element_removed),
407 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, GST_TYPE_BIN,
410 * GstBin::do-latency:
413 * Will be emitted when the bin needs to perform latency calculations. This
414 * signal is only emitted for toplevel bins or when async-handling is
417 * Only one signal handler is invoked. If no signals are connected, the
418 * default handler is invoked, which will query and distribute the lowest
419 * possible latency to all sinks.
421 * Connect to this signal if the default latency calculations are not
422 * sufficient, like when you need different latencies for different sinks in
425 gst_bin_signals[DO_LATENCY] =
426 g_signal_new ("do-latency", G_TYPE_FROM_CLASS (klass),
427 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstBinClass, do_latency),
428 _gst_boolean_accumulator, NULL, g_cclosure_marshal_generic,
429 G_TYPE_BOOLEAN, 0, G_TYPE_NONE);
432 * GstBin:message-forward:
434 * Forward all children messages, even those that would normally be filtered by
435 * the bin. This can be interesting when one wants to be notified of the EOS
436 * state of individual elements, for example.
438 * The messages are converted to an ELEMENT message with the bin as the
439 * source. The structure of the message is named 'GstBinForwarded' and contains
440 * a field named 'message' of type GST_TYPE_MESSAGE that contains the original
443 g_object_class_install_property (gobject_class, PROP_MESSAGE_FORWARD,
444 g_param_spec_boolean ("message-forward", "Message Forward",
445 "Forwards all children messages",
446 DEFAULT_MESSAGE_FORWARD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 gobject_class->dispose = gst_bin_dispose;
450 gst_element_class_set_static_metadata (gstelement_class, "Generic bin",
452 "Simple container object",
453 "Erik Walthinsen <omega@cse.ogi.edu>,"
454 "Wim Taymans <wim.taymans@gmail.com>");
456 gstelement_class->change_state =
457 GST_DEBUG_FUNCPTR (gst_bin_change_state_func);
458 gstelement_class->post_message = GST_DEBUG_FUNCPTR (gst_bin_post_message);
459 gstelement_class->get_state = GST_DEBUG_FUNCPTR (gst_bin_get_state_func);
461 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_bin_get_index_func);
462 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_bin_set_index_func);
464 gstelement_class->provide_clock =
465 GST_DEBUG_FUNCPTR (gst_bin_provide_clock_func);
466 gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_bin_set_clock_func);
468 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_bin_send_event);
469 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_bin_query);
470 gstelement_class->set_context = GST_DEBUG_FUNCPTR (gst_bin_set_context);
472 klass->add_element = GST_DEBUG_FUNCPTR (gst_bin_add_func);
473 klass->remove_element = GST_DEBUG_FUNCPTR (gst_bin_remove_func);
474 klass->handle_message = GST_DEBUG_FUNCPTR (gst_bin_handle_message_func);
476 klass->deep_element_added = gst_bin_deep_element_added_func;
477 klass->deep_element_removed = gst_bin_deep_element_removed_func;
479 klass->do_latency = GST_DEBUG_FUNCPTR (gst_bin_do_latency_func);
483 gst_bin_init (GstBin * bin)
487 bin->numchildren = 0;
488 bin->children = NULL;
489 bin->children_cookie = 0;
490 bin->messages = NULL;
491 bin->provided_clock = NULL;
492 bin->clock_dirty = FALSE;
494 /* Set up a bus for listening to child elements */
495 bus = g_object_new (GST_TYPE_BUS, "enable-async", FALSE, NULL);
496 gst_object_ref_sink (bus);
497 bin->child_bus = bus;
498 GST_DEBUG_OBJECT (bin, "using bus %" GST_PTR_FORMAT " to listen to children",
500 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bin_bus_handler, bin,
503 bin->priv = gst_bin_get_instance_private (bin);
504 bin->priv->asynchandling = DEFAULT_ASYNC_HANDLING;
505 bin->priv->structure_cookie = 0;
506 bin->priv->message_forward = DEFAULT_MESSAGE_FORWARD;
510 gst_bin_dispose (GObject * object)
512 GstBin *bin = GST_BIN_CAST (object);
513 GstBus **child_bus_p = &bin->child_bus;
514 GstClock **provided_clock_p = &bin->provided_clock;
515 GstElement **clock_provider_p = &bin->clock_provider;
517 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object, "%p dispose", object);
519 GST_OBJECT_LOCK (object);
520 gst_object_replace ((GstObject **) child_bus_p, NULL);
521 gst_object_replace ((GstObject **) provided_clock_p, NULL);
522 gst_object_replace ((GstObject **) clock_provider_p, NULL);
523 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
524 GST_OBJECT_UNLOCK (object);
526 while (bin->children) {
527 gst_bin_remove (bin, GST_ELEMENT_CAST (bin->children->data));
529 if (G_UNLIKELY (bin->children != NULL)) {
530 g_critical ("could not remove elements from bin '%s'",
531 GST_STR_NULL (GST_OBJECT_NAME (object)));
534 G_OBJECT_CLASS (parent_class)->dispose (object);
539 * @name: (allow-none): the name of the new bin
541 * Creates a new bin with the given name.
543 * Returns: (transfer floating): a new #GstBin
546 gst_bin_new (const gchar * name)
548 return gst_element_factory_make ("bin", name);
552 gst_bin_set_property (GObject * object, guint prop_id,
553 const GValue * value, GParamSpec * pspec)
557 gstbin = GST_BIN_CAST (object);
560 case PROP_ASYNC_HANDLING:
561 GST_OBJECT_LOCK (gstbin);
562 gstbin->priv->asynchandling = g_value_get_boolean (value);
563 GST_OBJECT_UNLOCK (gstbin);
565 case PROP_MESSAGE_FORWARD:
566 GST_OBJECT_LOCK (gstbin);
567 gstbin->priv->message_forward = g_value_get_boolean (value);
568 GST_OBJECT_UNLOCK (gstbin);
571 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
577 gst_bin_get_property (GObject * object, guint prop_id,
578 GValue * value, GParamSpec * pspec)
582 gstbin = GST_BIN_CAST (object);
585 case PROP_ASYNC_HANDLING:
586 GST_OBJECT_LOCK (gstbin);
587 g_value_set_boolean (value, gstbin->priv->asynchandling);
588 GST_OBJECT_UNLOCK (gstbin);
590 case PROP_MESSAGE_FORWARD:
591 GST_OBJECT_LOCK (gstbin);
592 g_value_set_boolean (value, gstbin->priv->message_forward);
593 GST_OBJECT_UNLOCK (gstbin);
596 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
602 /* return the cached index */
604 gst_bin_get_index_func (GstElement * element)
609 bin = GST_BIN_CAST (element);
611 GST_OBJECT_LOCK (bin);
612 if ((result = bin->priv->index))
613 gst_object_ref (result);
614 GST_OBJECT_UNLOCK (bin);
619 /* set the index on all elements in this bin
624 gst_bin_set_index_func (GstElement * element, GstIndex * index)
630 GValue data = { 0, };
632 bin = GST_BIN_CAST (element);
634 GST_OBJECT_LOCK (bin);
635 old = bin->priv->index;
636 if (G_UNLIKELY (old == index))
639 gst_object_ref (index);
640 bin->priv->index = index;
641 GST_OBJECT_UNLOCK (bin);
644 gst_object_unref (old);
646 it = gst_bin_iterate_elements (bin);
648 /* set the index on all elements in the bin */
651 switch (gst_iterator_next (it, &data)) {
652 case GST_ITERATOR_OK:
654 GstElement *child = g_value_get_object (&data);
656 GST_DEBUG_OBJECT (bin, "setting index on '%s'",
657 GST_ELEMENT_NAME (child));
658 gst_element_set_index (child, index);
660 g_value_reset (&data);
663 case GST_ITERATOR_RESYNC:
664 GST_DEBUG_OBJECT (bin, "iterator doing resync");
665 gst_iterator_resync (it);
668 case GST_ITERATOR_DONE:
669 GST_DEBUG_OBJECT (bin, "iterator done");
674 g_value_unset (&data);
675 gst_iterator_free (it);
680 GST_DEBUG_OBJECT (bin, "index was already set");
681 GST_OBJECT_UNLOCK (bin);
687 /* set the clock on all elements in this bin
692 gst_bin_set_clock_func (GstElement * element, GstClock * clock)
698 GValue data = { 0, };
700 bin = GST_BIN_CAST (element);
702 it = gst_bin_iterate_elements (bin);
706 switch (gst_iterator_next (it, &data)) {
707 case GST_ITERATOR_OK:
709 GstElement *child = g_value_get_object (&data);
711 res &= gst_element_set_clock (child, clock);
713 g_value_reset (&data);
716 case GST_ITERATOR_RESYNC:
717 GST_DEBUG_OBJECT (bin, "iterator doing resync");
718 gst_iterator_resync (it);
722 case GST_ITERATOR_DONE:
723 GST_DEBUG_OBJECT (bin, "iterator done");
728 g_value_unset (&data);
729 gst_iterator_free (it);
732 res = GST_ELEMENT_CLASS (parent_class)->set_clock (element, clock);
737 /* get the clock for this bin by asking all of the children in this bin
739 * The ref of the returned clock in increased so unref after usage.
741 * We loop the elements in state order and pick the last clock we can
742 * get. This makes sure we get a clock from the source.
747 gst_bin_provide_clock_func (GstElement * element)
749 GstClock *result = NULL;
750 GstElement *provider = NULL;
755 GstClock **provided_clock_p;
756 GstElement **clock_provider_p;
758 bin = GST_BIN_CAST (element);
760 GST_OBJECT_LOCK (bin);
761 if (!bin->clock_dirty)
764 GST_DEBUG_OBJECT (bin, "finding new clock");
766 it = gst_bin_sort_iterator_new (bin);
767 GST_OBJECT_UNLOCK (bin);
771 switch (gst_iterator_next (it, &val)) {
772 case GST_ITERATOR_OK:
774 GstElement *child = g_value_get_object (&val);
777 clock = gst_element_provide_clock (child);
779 GST_DEBUG_OBJECT (bin, "found candidate clock %p by element %s",
780 clock, GST_ELEMENT_NAME (child));
782 gst_object_unref (result);
783 gst_object_unref (provider);
786 provider = gst_object_ref (child);
789 g_value_reset (&val);
792 case GST_ITERATOR_RESYNC:
793 gst_iterator_resync (it);
796 case GST_ITERATOR_DONE:
801 g_value_unset (&val);
802 gst_iterator_free (it);
804 GST_OBJECT_LOCK (bin);
805 if (!bin->clock_dirty) {
807 gst_object_unref (provider);
809 gst_object_unref (result);
815 provided_clock_p = &bin->provided_clock;
816 clock_provider_p = &bin->clock_provider;
817 gst_object_replace ((GstObject **) provided_clock_p, (GstObject *) result);
818 gst_object_replace ((GstObject **) clock_provider_p, (GstObject *) provider);
819 bin->clock_dirty = FALSE;
820 GST_DEBUG_OBJECT (bin,
821 "provided new clock %" GST_PTR_FORMAT " by provider %" GST_PTR_FORMAT,
823 /* Provider is not being returned to caller, just the result */
825 gst_object_unref (provider);
826 GST_OBJECT_UNLOCK (bin);
832 if ((result = bin->provided_clock))
833 gst_object_ref (result);
834 GST_DEBUG_OBJECT (bin, "returning old clock %p", result);
835 GST_OBJECT_UNLOCK (bin);
842 * functions for manipulating cached messages
847 GstMessageType types;
850 /* check if a message is of given src and type */
852 message_check (GstMessage * message, MessageFind * target)
857 eq &= GST_MESSAGE_SRC (message) == target->src;
859 eq &= (GST_MESSAGE_TYPE (message) & target->types) != 0;
860 GST_LOG ("looking at message %p: %d", message, eq);
866 find_message (GstBin * bin, GstObject * src, GstMessageType types)
874 result = g_list_find_custom (bin->messages, &find,
875 (GCompareFunc) message_check);
878 GST_DEBUG_OBJECT (bin, "we found a message %p from %s matching types %08x",
879 result->data, GST_OBJECT_NAME (GST_MESSAGE_CAST (result->data)->src),
882 GST_DEBUG_OBJECT (bin, "no message found matching types %08x", types);
883 #ifndef GST_DISABLE_GST_DEBUG
887 for (i = 0; i < 32; i++)
888 if (types & (1U << i))
889 GST_DEBUG_OBJECT (bin, " %s", gst_message_type_get_name (1U << i));
897 /* with LOCK, returns TRUE if message had a valid SRC, takes ownership of
900 * A message that is cached and has the same SRC and type is replaced
901 * by the given message.
904 bin_replace_message (GstBin * bin, GstMessage * message, GstMessageType types)
910 if ((src = GST_MESSAGE_SRC (message))) {
911 /* first find the previous message posted by this element */
912 if ((previous = find_message (bin, src, types))) {
913 GstMessage *previous_msg;
915 /* if we found a previous message, replace it */
916 previous_msg = previous->data;
917 previous->data = message;
919 GST_DEBUG_OBJECT (bin, "replace old message %s from %s with %s message",
920 GST_MESSAGE_TYPE_NAME (previous_msg), GST_ELEMENT_NAME (src),
921 GST_MESSAGE_TYPE_NAME (message));
923 gst_message_unref (previous_msg);
925 /* keep new message */
926 bin->messages = g_list_prepend (bin->messages, message);
928 GST_DEBUG_OBJECT (bin, "got new message %p, %s from %s",
929 message, GST_MESSAGE_TYPE_NAME (message), GST_ELEMENT_NAME (src));
932 GST_DEBUG_OBJECT (bin, "got message %s from (NULL), not processing",
933 GST_MESSAGE_TYPE_NAME (message));
935 gst_message_unref (message);
940 /* with LOCK. Remove all messages of given types */
942 bin_remove_messages (GstBin * bin, GstObject * src, GstMessageType types)
950 for (walk = bin->messages; walk; walk = next) {
951 GstMessage *message = (GstMessage *) walk->data;
953 next = g_list_next (walk);
955 if (message_check (message, &find) == 0) {
956 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (message),
957 "deleting message %p of type %s (types 0x%08x)", message,
958 GST_MESSAGE_TYPE_NAME (message), types);
959 bin->messages = g_list_delete_link (bin->messages, walk);
960 gst_message_unref (message);
962 GST_DEBUG_OBJECT (GST_MESSAGE_SRC (message),
963 "not deleting message %p of type 0x%08x", message,
964 GST_MESSAGE_TYPE (message));
970 /* Check if the bin is EOS. We do this by scanning all sinks and
971 * checking if they posted an EOS message.
973 * call with bin LOCK */
975 is_eos (GstBin * bin, guint32 * seqnum)
982 for (walk = bin->children; walk; walk = g_list_next (walk)) {
985 element = GST_ELEMENT_CAST (walk->data);
986 if (bin_element_is_sink (element, bin) == 0) {
987 /* check if element posted EOS */
989 find_message (bin, GST_OBJECT_CAST (element), GST_MESSAGE_EOS))) {
990 GST_DEBUG ("sink '%s' posted EOS", GST_ELEMENT_NAME (element));
991 *seqnum = gst_message_get_seqnum (GST_MESSAGE_CAST (msgs->data));
994 GST_DEBUG ("sink '%s' did not post EOS yet",
995 GST_ELEMENT_NAME (element));
1001 /* FIXME: Some tests (e.g. elements/capsfilter) use
1002 * pipelines with a dangling sinkpad but no sink element.
1003 * These tests assume that no EOS message is ever
1004 * posted on the bus so let's keep that behaviour.
1005 * In valid pipelines this doesn't make a difference.
1007 return result && n_eos > 0;
1011 /* Check if the bin is STREAM_START. We do this by scanning all sinks and
1012 * checking if they posted an STREAM_START message.
1014 * call with bin LOCK */
1016 is_stream_start (GstBin * bin, guint32 * seqnum, gboolean * have_group_id,
1022 gboolean first = TRUE, same_group_id = TRUE;
1024 *have_group_id = TRUE;
1027 for (walk = bin->children; walk; walk = g_list_next (walk)) {
1028 GstElement *element;
1030 element = GST_ELEMENT_CAST (walk->data);
1031 if (bin_element_is_sink (element, bin) == 0) {
1032 /* check if element posted STREAM_START */
1034 find_message (bin, GST_OBJECT_CAST (element),
1035 GST_MESSAGE_STREAM_START))) {
1036 GST_DEBUG ("sink '%s' posted STREAM_START", GST_ELEMENT_NAME (element));
1037 *seqnum = gst_message_get_seqnum (GST_MESSAGE_CAST (msgs->data));
1038 if (gst_message_parse_group_id (GST_MESSAGE_CAST (msgs->data),
1042 *group_id = tmp_group_id;
1044 if (tmp_group_id != *group_id)
1045 same_group_id = FALSE;
1048 *have_group_id = FALSE;
1051 GST_DEBUG ("sink '%s' did not post STREAM_START yet",
1052 GST_ELEMENT_NAME (element));
1059 /* If all have a group_id we only consider this stream started
1060 * if all group ids were the same and all sinks posted a stream-start
1063 return same_group_id && result;
1064 /* otherwise consider this stream started after all sinks
1065 * have reported stream-start for backward compatibility.
1066 * FIXME 2.0: This should go away! */
1071 unlink_pads (const GValue * item, gpointer user_data)
1076 pad = g_value_get_object (item);
1078 if ((peer = gst_pad_get_peer (pad))) {
1079 if (gst_pad_get_direction (pad) == GST_PAD_SRC)
1080 gst_pad_unlink (pad, peer);
1082 gst_pad_unlink (peer, pad);
1083 gst_object_unref (peer);
1088 bin_deep_iterator_foreach (const GValue * item, gpointer user_data)
1090 GQueue *queue = user_data;
1092 g_queue_push_tail (queue, g_value_dup_object (item));
1096 gst_bin_do_deep_add_remove (GstBin * bin, gint sig_id, const gchar * sig_name,
1097 GstElement * element)
1099 g_signal_emit (bin, sig_id, 0, bin, element);
1101 /* When removing a bin, emit deep-element-* for everything in the bin too */
1102 if (GST_IS_BIN (element)) {
1104 GstIteratorResult ires;
1105 GQueue elements = G_QUEUE_INIT;
1107 GST_LOG_OBJECT (bin, "Recursing into bin %" GST_PTR_FORMAT " for %s",
1109 it = gst_bin_iterate_recurse (GST_BIN_CAST (element));
1111 ires = gst_iterator_foreach (it, bin_deep_iterator_foreach, &elements);
1112 if (ires != GST_ITERATOR_DONE) {
1113 g_queue_foreach (&elements, (GFunc) g_object_unref, NULL);
1114 g_queue_clear (&elements);
1116 if (ires == GST_ITERATOR_RESYNC)
1117 gst_iterator_resync (it);
1118 } while (ires == GST_ITERATOR_RESYNC);
1119 if (ires != GST_ITERATOR_ERROR) {
1122 while ((e = g_queue_pop_head (&elements))) {
1123 GstObject *parent = gst_object_get_parent (GST_OBJECT_CAST (e));
1125 GST_LOG_OBJECT (bin, "calling %s for element %" GST_PTR_FORMAT
1126 " in bin %" GST_PTR_FORMAT, sig_name, e, parent);
1127 g_signal_emit (bin, sig_id, 0, parent, e);
1128 gst_object_unref (parent);
1132 gst_iterator_free (it);
1136 /* vmethod that adds an element to a bin
1141 gst_bin_add_func (GstBin * bin, GstElement * element)
1145 gboolean is_sink, is_source, provides_clock, requires_clock;
1146 GstMessage *clock_message = NULL, *async_message = NULL;
1147 GstStateChangeReturn ret;
1148 GList *l, *elem_contexts, *need_context_messages;
1150 GST_DEBUG_OBJECT (bin, "element :%s", GST_ELEMENT_NAME (element));
1152 /* we obviously can't add ourself to ourself */
1153 if (G_UNLIKELY (element == GST_ELEMENT_CAST (bin)))
1156 /* get the element name to make sure it is unique in this bin. */
1157 GST_OBJECT_LOCK (element);
1158 elem_name = g_strdup (GST_ELEMENT_NAME (element));
1159 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1160 is_source = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE);
1162 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1164 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1165 GST_OBJECT_UNLOCK (element);
1167 GST_OBJECT_LOCK (bin);
1169 /* then check to see if the element's name is already taken in the bin,
1170 * we can safely take the lock here. This check is probably bogus because
1171 * you can safely change the element name after this check and before setting
1172 * the object parent. The window is very small though... */
1173 if (G_UNLIKELY (!gst_object_check_uniqueness (bin->children, elem_name)))
1174 goto duplicate_name;
1176 /* set the element's parent and add the element to the bin's list of children */
1177 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (element),
1178 GST_OBJECT_CAST (bin))))
1181 /* if we add a sink we become a sink */
1182 if (is_sink && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_SINK)) {
1183 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "element \"%s\" was sink",
1185 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SINK);
1187 if (is_source && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_SOURCE)) {
1188 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "element \"%s\" was source",
1190 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SOURCE);
1193 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_PROVIDE_CLOCK)) {
1194 GST_DEBUG_OBJECT (bin, "element \"%s\" can provide a clock", elem_name);
1196 gst_message_new_clock_provide (GST_OBJECT_CAST (element), NULL, TRUE);
1197 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1200 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_REQUIRE_CLOCK)) {
1201 GST_DEBUG_OBJECT (bin, "element \"%s\" requires a clock", elem_name);
1202 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1205 bin->children = g_list_prepend (bin->children, element);
1207 bin->children_cookie++;
1208 if (!GST_BIN_IS_NO_RESYNC (bin))
1209 bin->priv->structure_cookie++;
1211 /* distribute the bus */
1212 gst_element_set_bus (element, bin->child_bus);
1214 /* propagate the current base_time, start_time and clock */
1215 gst_element_set_base_time (element, GST_ELEMENT_CAST (bin)->base_time);
1216 gst_element_set_start_time (element, GST_ELEMENT_START_TIME (bin));
1217 /* it's possible that the element did not accept the clock but
1218 * that is not important right now. When the pipeline goes to PLAYING,
1219 * a new clock will be selected */
1220 gst_element_set_clock (element, GST_ELEMENT_CLOCK (bin));
1222 /* get the element's list of contexts before propagating our own */
1223 elem_contexts = gst_element_get_contexts (element);
1224 for (l = GST_ELEMENT_CAST (bin)->contexts; l; l = l->next)
1225 gst_element_set_context (element, l->data);
1227 need_context_messages = NULL;
1228 for (l = elem_contexts; l; l = l->next) {
1229 GstContext *replacement, *context = l->data;
1230 const gchar *context_type;
1232 context_type = gst_context_get_context_type (context);
1234 /* we already set this context above? */
1236 gst_element_get_context_unlocked (GST_ELEMENT (bin), context_type);
1238 gst_context_unref (replacement);
1243 /* ask our parent for the context */
1244 msg = gst_message_new_need_context (GST_OBJECT_CAST (bin), context_type);
1245 s = (GstStructure *) gst_message_get_structure (msg);
1246 gst_structure_set (s, "bin.old.context", GST_TYPE_CONTEXT, context, NULL);
1248 need_context_messages = g_list_prepend (need_context_messages, msg);
1253 /* set the cached index on the children */
1254 if (bin->priv->index)
1255 gst_element_set_index (element, bin->priv->index);
1258 ret = GST_STATE_RETURN (bin);
1259 /* no need to update the state if we are in error */
1260 if (ret == GST_STATE_CHANGE_FAILURE)
1261 goto no_state_recalc;
1263 /* update the bin state, the new element could have been an ASYNC or
1264 * NO_PREROLL element */
1265 ret = GST_STATE_RETURN (element);
1266 GST_DEBUG_OBJECT (bin, "added %s element",
1267 gst_element_state_change_return_get_name (ret));
1270 case GST_STATE_CHANGE_ASYNC:
1272 /* create message to track this aync element when it posts an async-done
1274 async_message = gst_message_new_async_start (GST_OBJECT_CAST (element));
1277 case GST_STATE_CHANGE_NO_PREROLL:
1278 /* ignore all async elements we might have and commit our state */
1279 bin_handle_async_done (bin, ret, FALSE, GST_CLOCK_TIME_NONE);
1281 case GST_STATE_CHANGE_FAILURE:
1288 GST_OBJECT_UNLOCK (bin);
1290 for (l = need_context_messages; l; l = l->next) {
1291 GstMessage *msg = l->data;
1293 const gchar *context_type;
1294 GstContext *replacement, *context;
1296 gst_message_parse_context_type (msg, &context_type);
1298 GST_LOG_OBJECT (bin, "asking parent for context type: %s "
1299 "from %" GST_PTR_FORMAT, context_type, element);
1301 s = (GstStructure *) gst_message_get_structure (msg);
1302 gst_structure_get (s, "bin.old.context", GST_TYPE_CONTEXT, &context, NULL);
1303 gst_structure_remove_field (s, "bin.old.context");
1304 gst_element_post_message (GST_ELEMENT_CAST (bin), msg);
1306 /* lock to avoid losing a potential write */
1307 GST_OBJECT_LOCK (bin);
1309 gst_element_get_context_unlocked (GST_ELEMENT_CAST (bin), context_type);
1312 /* we got the context set from GstElement::set_context */
1313 gst_context_unref (replacement);
1314 GST_OBJECT_UNLOCK (bin);
1316 /* Propagate the element's context upwards */
1317 GST_LOG_OBJECT (bin, "propagating existing context type: %s %p "
1318 "from %" GST_PTR_FORMAT, context_type, context, element);
1320 gst_bin_update_context_unlocked (bin, context);
1323 gst_message_new_have_context (GST_OBJECT_CAST (bin),
1324 gst_context_ref (context));
1325 GST_OBJECT_UNLOCK (bin);
1326 gst_element_post_message (GST_ELEMENT_CAST (bin), msg);
1328 gst_context_unref (context);
1330 g_list_free_full (elem_contexts, (GDestroyNotify) gst_context_unref);
1331 g_list_free (need_context_messages);
1333 /* post the messages on the bus of the element so that the bin can handle
1336 gst_element_post_message (element, clock_message);
1339 gst_element_post_message (element, async_message);
1341 /* unlink all linked pads */
1342 it = gst_element_iterate_pads (element);
1343 while (gst_iterator_foreach (it, (GstIteratorForeachFunction) unlink_pads,
1344 NULL) == GST_ITERATOR_RESYNC)
1345 gst_iterator_resync (it);
1346 gst_iterator_free (it);
1348 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "added element \"%s\"",
1351 g_signal_emit (bin, gst_bin_signals[ELEMENT_ADDED], 0, element);
1352 gst_child_proxy_child_added ((GstChildProxy *) bin, (GObject *) element,
1355 gst_bin_do_deep_add_remove (bin, gst_bin_signals[DEEP_ELEMENT_ADDED],
1356 "deep-element-added", element);
1362 /* ERROR handling here */
1365 GST_OBJECT_LOCK (bin);
1366 g_warning ("Cannot add bin '%s' to itself", GST_ELEMENT_NAME (bin));
1367 GST_OBJECT_UNLOCK (bin);
1368 gst_object_ref_sink (element);
1369 gst_object_unref (element);
1374 g_warning ("Name '%s' is not unique in bin '%s', not adding",
1375 elem_name, GST_ELEMENT_NAME (bin));
1376 GST_OBJECT_UNLOCK (bin);
1378 gst_object_ref_sink (element);
1379 gst_object_unref (element);
1384 g_warning ("Element '%s' already has parent", elem_name);
1385 GST_OBJECT_UNLOCK (bin);
1392 * gst_bin_set_suppressed_flags:
1394 * @flags: the #GstElementFlags to suppress
1396 * Suppress the given flags on the bin. #GstElementFlags of a
1397 * child element are propagated when it is added to the bin.
1398 * When suppressed flags are set, those specified flags will
1399 * not be propagated to the bin.
1406 gst_bin_set_suppressed_flags (GstBin * bin, GstElementFlags flags)
1408 g_return_if_fail (GST_IS_BIN (bin));
1410 GST_OBJECT_LOCK (bin);
1411 bin->priv->suppressed_flags = bin->priv->suppressed_flags | flags;
1412 GST_OBJECT_UNLOCK (bin);
1414 GST_DEBUG_OBJECT (bin, "Set suppressed flags(0x%x) to bin '%s'", flags,
1415 GST_ELEMENT_NAME (bin));
1419 * gst_bin_get_suppressed_flags:
1422 * Return the suppressed flags of the bin.
1426 * Returns: the bin's suppressed #GstElementFlags.
1431 gst_bin_get_suppressed_flags (GstBin * bin)
1433 GstElementFlags res;
1435 g_return_val_if_fail (GST_IS_BIN (bin), 0);
1437 GST_OBJECT_LOCK (bin);
1438 res = bin->priv->suppressed_flags;
1439 GST_OBJECT_UNLOCK (bin);
1444 /* signal vfunc, will be called when a new element was added */
1446 gst_bin_deep_element_added_func (GstBin * bin, GstBin * sub_bin,
1451 parent_bin = (GstBin *) gst_object_get_parent (GST_OBJECT_CAST (bin));
1452 if (parent_bin == NULL) {
1453 GST_LOG_OBJECT (bin, "no parent, reached top-level");
1457 GST_LOG_OBJECT (parent_bin, "emitting deep-element-added for element "
1458 "%" GST_PTR_FORMAT " which has just been added to %" GST_PTR_FORMAT,
1461 g_signal_emit (parent_bin, gst_bin_signals[DEEP_ELEMENT_ADDED], 0, sub_bin,
1464 gst_object_unref (parent_bin);
1467 /* signal vfunc, will be called when an element was removed */
1469 gst_bin_deep_element_removed_func (GstBin * bin, GstBin * sub_bin,
1474 parent_bin = (GstBin *) gst_object_get_parent (GST_OBJECT_CAST (bin));
1475 if (parent_bin == NULL) {
1476 GST_LOG_OBJECT (bin, "no parent, reached top-level");
1480 GST_LOG_OBJECT (parent_bin, "emitting deep-element-removed for element "
1481 "%" GST_PTR_FORMAT " which has just been removed from %" GST_PTR_FORMAT,
1484 g_signal_emit (parent_bin, gst_bin_signals[DEEP_ELEMENT_REMOVED], 0, sub_bin,
1487 gst_object_unref (parent_bin);
1493 * @element: (transfer floating): the #GstElement to add
1495 * Adds the given element to the bin. Sets the element's parent, and thus
1496 * takes ownership of the element. An element can only be added to one bin.
1498 * If the element's pads are linked to other pads, the pads will be unlinked
1499 * before the element is added to the bin.
1501 * > When you add an element to an already-running pipeline, you will have to
1502 * > take care to set the state of the newly-added element to the desired
1503 * > state (usually PLAYING or PAUSED, same you set the pipeline to originally)
1504 * > with gst_element_set_state(), or use gst_element_sync_state_with_parent().
1505 * > The bin or pipeline will not take care of this for you.
1509 * Returns: %TRUE if the element could be added, %FALSE if
1510 * the bin does not want to accept the element.
1513 gst_bin_add (GstBin * bin, GstElement * element)
1515 GstBinClass *bclass;
1518 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1519 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1520 g_return_val_if_fail (GST_ELEMENT_CAST (bin) != element, FALSE);
1522 bclass = GST_BIN_GET_CLASS (bin);
1524 if (G_UNLIKELY (bclass->add_element == NULL))
1527 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "adding element %s to bin %s",
1528 GST_STR_NULL (GST_ELEMENT_NAME (element)),
1529 GST_STR_NULL (GST_ELEMENT_NAME (bin)));
1531 GST_TRACER_BIN_ADD_PRE (bin, element);
1532 result = bclass->add_element (bin, element);
1533 GST_TRACER_BIN_ADD_POST (bin, element, result);
1537 /* ERROR handling */
1540 g_warning ("adding elements to bin '%s' is not supported",
1541 GST_ELEMENT_NAME (bin));
1542 gst_object_ref_sink (element);
1543 gst_object_unref (element);
1548 /* remove an element from the bin
1553 gst_bin_remove_func (GstBin * bin, GstElement * element)
1557 gboolean is_sink, is_source, provides_clock, requires_clock;
1558 gboolean othersink, othersource, otherprovider, otherrequirer, found;
1559 GstMessage *clock_message = NULL;
1560 GstClock **provided_clock_p;
1561 GstElement **clock_provider_p;
1563 gboolean other_async, this_async, have_no_preroll;
1564 GstStateChangeReturn ret;
1566 GST_DEBUG_OBJECT (bin, "element :%s", GST_ELEMENT_NAME (element));
1568 /* we obviously can't remove ourself from ourself */
1569 if (G_UNLIKELY (element == GST_ELEMENT_CAST (bin)))
1570 goto removing_itself;
1572 GST_OBJECT_LOCK (bin);
1574 GST_OBJECT_LOCK (element);
1575 elem_name = g_strdup (GST_ELEMENT_NAME (element));
1577 if (GST_OBJECT_PARENT (element) != GST_OBJECT_CAST (bin))
1580 /* remove the parent ref */
1581 GST_OBJECT_PARENT (element) = NULL;
1583 /* grab element name so we can print it */
1584 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1585 is_source = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE);
1587 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1589 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1590 GST_OBJECT_UNLOCK (element);
1594 othersource = FALSE;
1595 otherprovider = FALSE;
1596 otherrequirer = FALSE;
1597 have_no_preroll = FALSE;
1598 /* iterate the elements, we collect which ones are async and no_preroll. We
1599 * also remove the element when we find it. */
1600 for (walk = bin->children; walk; walk = next) {
1601 GstElement *child = GST_ELEMENT_CAST (walk->data);
1603 next = g_list_next (walk);
1605 if (child == element) {
1607 /* remove the element */
1608 bin->children = g_list_delete_link (bin->children, walk);
1610 gboolean child_sink, child_source, child_provider, child_requirer;
1612 GST_OBJECT_LOCK (child);
1613 child_sink = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SINK);
1614 child_source = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SOURCE);
1616 GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1618 GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1619 /* when we remove a sink, check if there are other sinks. */
1620 if (is_sink && !othersink && child_sink)
1622 if (is_source && !othersource && child_source)
1624 if (provides_clock && !otherprovider && child_provider)
1625 otherprovider = TRUE;
1626 if (requires_clock && !otherrequirer && child_requirer)
1627 otherrequirer = TRUE;
1628 /* check if we have NO_PREROLL children */
1629 if (GST_STATE_RETURN (child) == GST_STATE_CHANGE_NO_PREROLL)
1630 have_no_preroll = TRUE;
1631 GST_OBJECT_UNLOCK (child);
1635 /* the element must have been in the bin's list of children */
1636 if (G_UNLIKELY (!found))
1639 /* we now removed the element from the list of elements, increment the cookie
1640 * so that others can detect a change in the children list. */
1642 bin->children_cookie++;
1643 if (!GST_BIN_IS_NO_RESYNC (bin))
1644 bin->priv->structure_cookie++;
1646 if (is_sink && !othersink
1647 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_SINK)) {
1648 /* we're not a sink anymore */
1649 GST_DEBUG_OBJECT (bin, "we removed the last sink");
1650 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_SINK);
1652 if (is_source && !othersource
1653 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_SOURCE)) {
1654 /* we're not a source anymore */
1655 GST_DEBUG_OBJECT (bin, "we removed the last source");
1656 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_SOURCE);
1658 if (provides_clock && !otherprovider
1659 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_PROVIDE_CLOCK)) {
1660 /* we're not a clock provider anymore */
1661 GST_DEBUG_OBJECT (bin, "we removed the last clock provider");
1662 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1664 if (requires_clock && !otherrequirer
1665 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_REQUIRE_CLOCK)) {
1666 /* we're not a clock requirer anymore */
1667 GST_DEBUG_OBJECT (bin, "we removed the last clock requirer");
1668 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1671 /* if the clock provider for this element is removed, we lost
1672 * the clock as well, we need to inform the parent of this
1673 * so that it can select a new clock */
1674 if (bin->clock_provider == element) {
1675 GST_DEBUG_OBJECT (bin, "element \"%s\" provided the clock", elem_name);
1676 bin->clock_dirty = TRUE;
1678 gst_message_new_clock_lost (GST_OBJECT_CAST (bin), bin->provided_clock);
1679 provided_clock_p = &bin->provided_clock;
1680 clock_provider_p = &bin->clock_provider;
1681 gst_object_replace ((GstObject **) provided_clock_p, NULL);
1682 gst_object_replace ((GstObject **) clock_provider_p, NULL);
1685 /* remove messages for the element, if there was a pending ASYNC_START
1686 * message we must see if removing the element caused the bin to lose its
1689 other_async = FALSE;
1690 for (walk = bin->messages; walk; walk = next) {
1691 GstMessage *message = (GstMessage *) walk->data;
1692 GstElement *src = GST_ELEMENT_CAST (GST_MESSAGE_SRC (message));
1695 next = g_list_next (walk);
1698 switch (GST_MESSAGE_TYPE (message)) {
1699 case GST_MESSAGE_ASYNC_START:
1705 GST_DEBUG_OBJECT (src, "looking at message %p", message);
1707 case GST_MESSAGE_STRUCTURE_CHANGE:
1711 GST_DEBUG_OBJECT (src, "looking at structure change message %p",
1713 /* it's unlikely that this message is still in the list of messages
1714 * because this would mean that a link/unlink is busy in another thread
1715 * while we remove the element. We still have to remove the message
1716 * because we might not receive the done message anymore when the element
1717 * is removed from the bin. */
1718 gst_message_parse_structure_change (message, NULL, &owner, NULL);
1719 if (owner == element)
1730 /* delete all message types */
1731 GST_DEBUG_OBJECT (src, "deleting message %p of element \"%s\"",
1732 message, elem_name);
1733 bin->messages = g_list_delete_link (bin->messages, walk);
1734 gst_message_unref (message);
1738 /* get last return */
1739 ret = GST_STATE_RETURN (bin);
1741 /* no need to update the state if we are in error */
1742 if (ret == GST_STATE_CHANGE_FAILURE)
1743 goto no_state_recalc;
1745 if (!other_async && this_async) {
1746 /* all other elements were not async and we removed the async one,
1747 * handle the async-done case because we are not async anymore now. */
1748 GST_DEBUG_OBJECT (bin,
1749 "we removed the last async element, have no_preroll %d",
1752 /* the current state return of the bin depends on if there are no_preroll
1753 * elements in the pipeline or not */
1754 if (have_no_preroll)
1755 ret = GST_STATE_CHANGE_NO_PREROLL;
1757 ret = GST_STATE_CHANGE_SUCCESS;
1759 bin_handle_async_done (bin, ret, FALSE, GST_CLOCK_TIME_NONE);
1761 GST_DEBUG_OBJECT (bin,
1762 "recalc state preroll: %d, other async: %d, this async %d",
1763 have_no_preroll, other_async, this_async);
1765 if (have_no_preroll) {
1766 ret = GST_STATE_CHANGE_NO_PREROLL;
1767 } else if (other_async) {
1768 /* there are other async elements and we were not doing an async state
1769 * change, change our pending state and go async */
1770 if (GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING) {
1771 GST_STATE_NEXT (bin) = GST_STATE (bin);
1772 GST_STATE_PENDING (bin) = GST_STATE (bin);
1774 ret = GST_STATE_CHANGE_ASYNC;
1776 GST_STATE_RETURN (bin) = ret;
1780 gst_element_set_bus (element, NULL);
1781 /* Clear the clock we provided to the element */
1782 gst_element_set_clock (element, NULL);
1783 GST_OBJECT_UNLOCK (bin);
1786 gst_element_post_message (GST_ELEMENT_CAST (bin), clock_message);
1788 /* unlink all linked pads */
1789 it = gst_element_iterate_pads (element);
1790 while (gst_iterator_foreach (it, (GstIteratorForeachFunction) unlink_pads,
1791 NULL) == GST_ITERATOR_RESYNC)
1792 gst_iterator_resync (it);
1793 gst_iterator_free (it);
1795 GST_CAT_INFO_OBJECT (GST_CAT_PARENTAGE, bin, "removed child \"%s\"",
1798 g_signal_emit (bin, gst_bin_signals[ELEMENT_REMOVED], 0, element);
1799 gst_child_proxy_child_removed ((GstChildProxy *) bin, (GObject *) element,
1802 gst_bin_do_deep_add_remove (bin, gst_bin_signals[DEEP_ELEMENT_REMOVED],
1803 "deep-element-removed", element);
1806 /* element is really out of our control now */
1807 gst_object_unref (element);
1811 /* ERROR handling */
1814 GST_OBJECT_LOCK (bin);
1815 g_warning ("Cannot remove bin '%s' from itself", GST_ELEMENT_NAME (bin));
1816 GST_OBJECT_UNLOCK (bin);
1821 g_warning ("Element '%s' is not in bin '%s'", elem_name,
1822 GST_ELEMENT_NAME (bin));
1823 GST_OBJECT_UNLOCK (element);
1824 GST_OBJECT_UNLOCK (bin);
1833 * @element: (transfer none): the #GstElement to remove
1835 * Removes the element from the bin, unparenting it as well.
1836 * Unparenting the element means that the element will be dereferenced,
1837 * so if the bin holds the only reference to the element, the element
1838 * will be freed in the process of removing it from the bin. If you
1839 * want the element to still exist after removing, you need to call
1840 * gst_object_ref() before removing it from the bin.
1842 * If the element's pads are linked to other pads, the pads will be unlinked
1843 * before the element is removed from the bin.
1847 * Returns: %TRUE if the element could be removed, %FALSE if
1848 * the bin does not want to remove the element.
1851 gst_bin_remove (GstBin * bin, GstElement * element)
1853 GstBinClass *bclass;
1856 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1857 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1858 g_return_val_if_fail (GST_ELEMENT_CAST (bin) != element, FALSE);
1860 bclass = GST_BIN_GET_CLASS (bin);
1862 if (G_UNLIKELY (bclass->remove_element == NULL))
1865 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "removing element %s from bin %s",
1866 GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin));
1868 GST_TRACER_BIN_REMOVE_PRE (bin, element);
1869 result = bclass->remove_element (bin, element);
1870 GST_TRACER_BIN_REMOVE_POST (bin, result);
1874 /* ERROR handling */
1877 g_warning ("removing elements from bin '%s' is not supported",
1878 GST_ELEMENT_NAME (bin));
1884 * gst_bin_iterate_elements:
1887 * Gets an iterator for the elements in this bin.
1889 * MT safe. Caller owns returned value.
1891 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
1895 gst_bin_iterate_elements (GstBin * bin)
1897 GstIterator *result;
1899 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1901 GST_OBJECT_LOCK (bin);
1902 result = gst_iterator_new_list (GST_TYPE_ELEMENT,
1903 GST_OBJECT_GET_LOCK (bin),
1904 &bin->children_cookie, &bin->children, (GObject *) bin, NULL);
1905 GST_OBJECT_UNLOCK (bin);
1910 static GstIteratorItem
1911 iterate_child_recurse (GstIterator * it, const GValue * item)
1913 GstElement *child = g_value_get_object (item);
1915 if (GST_IS_BIN (child)) {
1916 GstIterator *other = gst_bin_iterate_recurse (GST_BIN_CAST (child));
1918 gst_iterator_push (it, other);
1920 return GST_ITERATOR_ITEM_PASS;
1924 * gst_bin_iterate_recurse:
1927 * Gets an iterator for the elements in this bin.
1928 * This iterator recurses into GstBin children.
1930 * MT safe. Caller owns returned value.
1932 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
1936 gst_bin_iterate_recurse (GstBin * bin)
1938 GstIterator *result;
1940 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1942 GST_OBJECT_LOCK (bin);
1943 result = gst_iterator_new_list (GST_TYPE_ELEMENT,
1944 GST_OBJECT_GET_LOCK (bin),
1945 &bin->children_cookie,
1947 (GObject *) bin, (GstIteratorItemFunction) iterate_child_recurse);
1948 GST_OBJECT_UNLOCK (bin);
1953 /* returns 0 when TRUE because this is a GCompareFunc */
1956 bin_element_is_sink (GstElement * child, GstBin * bin)
1960 /* we lock the child here for the remainder of the function to
1961 * get its name and flag safely. */
1962 GST_OBJECT_LOCK (child);
1963 is_sink = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SINK);
1965 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
1966 "child %s %s sink", GST_OBJECT_NAME (child), is_sink ? "is" : "is not");
1968 GST_OBJECT_UNLOCK (child);
1969 return is_sink ? 0 : 1;
1973 sink_iterator_filter (const GValue * vchild, GValue * vbin)
1975 GstBin *bin = g_value_get_object (vbin);
1976 GstElement *child = g_value_get_object (vchild);
1978 return (bin_element_is_sink (child, bin));
1982 * gst_bin_iterate_sinks:
1985 * Gets an iterator for all elements in the bin that have the
1986 * #GST_ELEMENT_FLAG_SINK flag set.
1988 * MT safe. Caller owns returned value.
1990 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
1994 gst_bin_iterate_sinks (GstBin * bin)
1996 GstIterator *children;
1997 GstIterator *result;
1998 GValue vbin = { 0, };
2000 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
2002 g_value_init (&vbin, GST_TYPE_BIN);
2003 g_value_set_object (&vbin, bin);
2005 children = gst_bin_iterate_elements (bin);
2006 result = gst_iterator_filter (children,
2007 (GCompareFunc) sink_iterator_filter, &vbin);
2009 g_value_unset (&vbin);
2014 /* returns 0 when TRUE because this is a GCompareFunc */
2017 bin_element_is_src (GstElement * child, GstBin * bin)
2021 /* we lock the child here for the remainder of the function to
2022 * get its name and other info safely. */
2023 GST_OBJECT_LOCK (child);
2024 is_src = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SOURCE);
2026 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
2027 "child %s %s src", GST_OBJECT_NAME (child), is_src ? "is" : "is not");
2029 GST_OBJECT_UNLOCK (child);
2030 return is_src ? 0 : 1;
2034 src_iterator_filter (const GValue * vchild, GValue * vbin)
2036 GstBin *bin = g_value_get_object (vbin);
2037 GstElement *child = g_value_get_object (vchild);
2039 return (bin_element_is_src (child, bin));
2043 * gst_bin_iterate_sources:
2046 * Gets an iterator for all elements in the bin that have the
2047 * #GST_ELEMENT_FLAG_SOURCE flag set.
2049 * MT safe. Caller owns returned value.
2051 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
2055 gst_bin_iterate_sources (GstBin * bin)
2057 GstIterator *children;
2058 GstIterator *result;
2059 GValue vbin = { 0, };
2061 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
2063 g_value_init (&vbin, GST_TYPE_BIN);
2064 g_value_set_object (&vbin, bin);
2066 children = gst_bin_iterate_elements (bin);
2067 result = gst_iterator_filter (children,
2068 (GCompareFunc) src_iterator_filter, &vbin);
2070 g_value_unset (&vbin);
2078 static GstStateChangeReturn
2079 gst_bin_get_state_func (GstElement * element, GstState * state,
2080 GstState * pending, GstClockTime timeout)
2082 GstStateChangeReturn ret;
2084 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
2087 GST_ELEMENT_CLASS (parent_class)->get_state (element, state, pending,
2093 /***********************************************
2094 * Topologically sorted iterator
2095 * see http://en.wikipedia.org/wiki/Topological_sorting
2097 * For each element in the graph, an entry is kept in a HashTable
2098 * with its number of srcpad connections (degree).
2099 * We then change state of all elements without dependencies
2100 * (degree 0) and decrement the degree of all elements connected
2101 * on the sinkpads. When an element reaches degree 0, its state is
2103 * When all elements are handled the algorithm stops.
2105 typedef struct _GstBinSortIterator
2108 GQueue queue; /* elements queued for state change */
2109 GstBin *bin; /* bin we iterate */
2110 gint mode; /* adding or removing dependency */
2111 GstElement *best; /* next element with least dependencies */
2112 gint best_deg; /* best degree */
2113 GHashTable *hash; /* hashtable with element dependencies */
2114 gboolean dirty; /* we detected structure change */
2115 } GstBinSortIterator;
2118 copy_to_queue (gpointer data, gpointer user_data)
2120 GstElement *element = data;
2121 GQueue *queue = user_data;
2123 gst_object_ref (element);
2124 g_queue_push_tail (queue, element);
2128 gst_bin_sort_iterator_copy (const GstBinSortIterator * it,
2129 GstBinSortIterator * copy)
2131 GHashTableIter iter;
2132 gpointer key, value;
2134 g_queue_init (©->queue);
2135 g_queue_foreach ((GQueue *) & it->queue, copy_to_queue, ©->queue);
2137 copy->bin = gst_object_ref (it->bin);
2139 copy->best = gst_object_ref (it->best);
2141 copy->hash = g_hash_table_new (NULL, NULL);
2142 g_hash_table_iter_init (&iter, it->hash);
2143 while (g_hash_table_iter_next (&iter, &key, &value))
2144 g_hash_table_insert (copy->hash, key, value);
2147 /* we add and subtract 1 to make sure we don't confuse NULL and 0 */
2148 #define HASH_SET_DEGREE(bit, elem, deg) \
2149 g_hash_table_replace (bit->hash, elem, GINT_TO_POINTER(deg+1))
2150 #define HASH_GET_DEGREE(bit, elem) \
2151 (GPOINTER_TO_INT(g_hash_table_lookup (bit->hash, elem))-1)
2153 /* add element to queue of next elements in the iterator.
2154 * We push at the tail to give higher priority elements a
2157 add_to_queue (GstBinSortIterator * bit, GstElement * element)
2159 GST_DEBUG_OBJECT (bit->bin, "adding '%s' to queue",
2160 GST_ELEMENT_NAME (element));
2161 gst_object_ref (element);
2162 g_queue_push_tail (&bit->queue, element);
2163 HASH_SET_DEGREE (bit, element, -1);
2167 remove_from_queue (GstBinSortIterator * bit, GstElement * element)
2171 if ((find = g_queue_find (&bit->queue, element))) {
2172 GST_DEBUG_OBJECT (bit->bin, "removing '%s' from queue",
2173 GST_ELEMENT_NAME (element));
2175 g_queue_delete_link (&bit->queue, find);
2176 gst_object_unref (element);
2178 GST_DEBUG_OBJECT (bit->bin, "unable to remove '%s' from queue",
2179 GST_ELEMENT_NAME (element));
2183 /* clear the queue, unref all objects as we took a ref when
2184 * we added them to the queue */
2186 clear_queue (GQueue * queue)
2190 while ((p = g_queue_pop_head (queue)))
2191 gst_object_unref (p);
2194 /* set all degrees to 0. Elements marked as a sink are
2195 * added to the queue immediately. Since we only look at the SINK flag of the
2196 * element, it is possible that we add non-sinks to the queue. These will be
2197 * removed from the queue again when we can prove that it provides data for some
2200 reset_degree (GstElement * element, GstBinSortIterator * bit)
2204 /* sinks are added right away */
2205 GST_OBJECT_LOCK (element);
2206 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
2207 GST_OBJECT_UNLOCK (element);
2210 add_to_queue (bit, element);
2212 /* others are marked with 0 and handled when sinks are done */
2213 HASH_SET_DEGREE (bit, element, 0);
2217 /* adjust the degree of all elements connected to the given
2218 * element. If a degree of an element drops to 0, it is
2219 * added to the queue of elements to schedule next.
2221 * We have to make sure not to cross the bin boundary this element
2225 update_degree (GstElement * element, GstBinSortIterator * bit)
2227 gboolean linked = FALSE;
2229 GST_OBJECT_LOCK (element);
2230 /* don't touch degree if element has no sinkpads */
2231 if (element->numsinkpads != 0) {
2232 /* loop over all sinkpads, decrement degree for all connected
2233 * elements in this bin */
2236 for (pads = element->sinkpads; pads; pads = g_list_next (pads)) {
2239 pad = GST_PAD_CAST (pads->data);
2241 /* we're iterating over the sinkpads, check if it's busy in a link/unlink */
2242 if (G_UNLIKELY (find_message (bit->bin, GST_OBJECT_CAST (pad),
2243 GST_MESSAGE_STRUCTURE_CHANGE))) {
2244 /* mark the iterator as dirty because we won't be updating the degree
2245 * of the peer parent now. This would result in the 'loop detected'
2246 * later on because the peer parent element could become the best next
2247 * element with a degree > 0. We will simply continue our state
2248 * changes and we'll eventually resync when the unlink completed and
2249 * the iterator cookie is updated. */
2254 if ((peer = gst_pad_get_peer (pad))) {
2255 GstElement *peer_element;
2257 if ((peer_element = gst_pad_get_parent_element (peer))) {
2258 GST_OBJECT_LOCK (peer_element);
2259 /* check that we don't go outside of this bin */
2260 if (GST_OBJECT_CAST (peer_element)->parent ==
2261 GST_OBJECT_CAST (bit->bin)) {
2262 gint old_deg, new_deg;
2264 old_deg = HASH_GET_DEGREE (bit, peer_element);
2266 /* check to see if we added an element as sink that was not really a
2267 * sink because it was connected to some other element. */
2268 if (old_deg == -1) {
2269 remove_from_queue (bit, peer_element);
2272 new_deg = old_deg + bit->mode;
2274 GST_DEBUG_OBJECT (bit->bin,
2275 "change element %s, degree %d->%d, linked to %s",
2276 GST_ELEMENT_NAME (peer_element), old_deg, new_deg,
2277 GST_ELEMENT_NAME (element));
2279 /* update degree, it is possible that an element was in 0 and
2280 * reaches -1 here. This would mean that the element had no sinkpads
2281 * but became linked while the state change was happening. We will
2282 * resync on this with the structure change message. */
2284 /* degree hit 0, add to queue */
2285 add_to_queue (bit, peer_element);
2287 HASH_SET_DEGREE (bit, peer_element, new_deg);
2291 GST_OBJECT_UNLOCK (peer_element);
2292 gst_object_unref (peer_element);
2294 gst_object_unref (peer);
2299 GST_DEBUG_OBJECT (bit->bin, "element %s not linked on any sinkpads",
2300 GST_ELEMENT_NAME (element));
2302 GST_OBJECT_UNLOCK (element);
2305 /* find the next best element not handled yet. This is the one
2306 * with the lowest non-negative degree */
2308 find_element (GstElement * element, GstBinSortIterator * bit)
2312 /* element is already handled */
2313 if ((degree = HASH_GET_DEGREE (bit, element)) < 0)
2316 /* first element or element with smaller degree */
2317 if (bit->best == NULL || bit->best_deg > degree) {
2318 bit->best = element;
2319 bit->best_deg = degree;
2320 } else if (bit->best_deg == degree
2321 && GST_OBJECT_FLAG_IS_SET (bit->best, GST_ELEMENT_FLAG_SOURCE)
2322 && !GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE)) {
2323 /* If two elements have the same degree, we want to ensure we
2324 * return non-source elements first. */
2325 bit->best = element;
2329 /* get next element in iterator. */
2330 static GstIteratorResult
2331 gst_bin_sort_iterator_next (GstBinSortIterator * bit, GValue * result)
2334 GstBin *bin = bit->bin;
2336 /* empty queue, we have to find a next best element */
2337 if (g_queue_is_empty (&bit->queue)) {
2339 bit->best_deg = G_MAXINT;
2340 g_list_foreach (bin->children, (GFunc) find_element, bit);
2341 if ((best = bit->best)) {
2342 /* when we detected an unlink, don't warn because our degrees might be
2343 * screwed up. We will resync later */
2344 if (bit->best_deg != 0 && !bit->dirty) {
2345 /* we don't fail on this one yet */
2346 GST_WARNING_OBJECT (bin, "loop dected in graph");
2347 g_warning ("loop detected in the graph of bin '%s'!!",
2348 GST_ELEMENT_NAME (bin));
2350 /* best unhandled element, schedule as next element */
2351 GST_DEBUG_OBJECT (bin, "queue empty, next best: %s",
2352 GST_ELEMENT_NAME (best));
2353 HASH_SET_DEGREE (bit, best, -1);
2354 g_value_set_object (result, best);
2356 GST_DEBUG_OBJECT (bin, "queue empty, elements exhausted");
2357 /* no more unhandled elements, we are done */
2358 return GST_ITERATOR_DONE;
2361 /* everything added to the queue got reffed */
2362 best = g_queue_pop_head (&bit->queue);
2363 g_value_set_object (result, best);
2364 gst_object_unref (best);
2367 GST_DEBUG_OBJECT (bin, "queue head gives %s", GST_ELEMENT_NAME (best));
2368 /* update degrees of linked elements */
2369 update_degree (best, bit);
2371 return GST_ITERATOR_OK;
2374 /* clear queues, recalculate the degrees and restart. */
2376 gst_bin_sort_iterator_resync (GstBinSortIterator * bit)
2378 GstBin *bin = bit->bin;
2380 GST_DEBUG_OBJECT (bin, "resync");
2382 clear_queue (&bit->queue);
2384 g_list_foreach (bin->children, (GFunc) reset_degree, bit);
2385 /* calc degrees, incrementing */
2387 g_list_foreach (bin->children, (GFunc) update_degree, bit);
2388 /* for the rest of the function we decrement the degrees */
2392 /* clear queues, unref bin and free iterator. */
2394 gst_bin_sort_iterator_free (GstBinSortIterator * bit)
2396 GstBin *bin = bit->bin;
2398 GST_DEBUG_OBJECT (bin, "free");
2399 clear_queue (&bit->queue);
2400 g_hash_table_destroy (bit->hash);
2401 gst_object_unref (bin);
2404 /* should be called with the bin LOCK held */
2405 static GstIterator *
2406 gst_bin_sort_iterator_new (GstBin * bin)
2408 GstBinSortIterator *result;
2410 /* we don't need an ItemFunction because we ref the items in the _next
2412 result = (GstBinSortIterator *)
2413 gst_iterator_new (sizeof (GstBinSortIterator),
2415 GST_OBJECT_GET_LOCK (bin),
2416 &bin->priv->structure_cookie,
2417 (GstIteratorCopyFunction) gst_bin_sort_iterator_copy,
2418 (GstIteratorNextFunction) gst_bin_sort_iterator_next,
2419 (GstIteratorItemFunction) NULL,
2420 (GstIteratorResyncFunction) gst_bin_sort_iterator_resync,
2421 (GstIteratorFreeFunction) gst_bin_sort_iterator_free);
2422 g_queue_init (&result->queue);
2423 result->hash = g_hash_table_new (NULL, NULL);
2424 gst_object_ref (bin);
2426 gst_bin_sort_iterator_resync (result);
2428 return (GstIterator *) result;
2432 * gst_bin_iterate_sorted:
2435 * Gets an iterator for the elements in this bin in topologically
2436 * sorted order. This means that the elements are returned from
2437 * the most downstream elements (sinks) to the sources.
2439 * This function is used internally to perform the state changes
2440 * of the bin elements and for clock selection.
2442 * MT safe. Caller owns returned value.
2444 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement,
2448 gst_bin_iterate_sorted (GstBin * bin)
2450 GstIterator *result;
2452 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
2454 GST_OBJECT_LOCK (bin);
2455 result = gst_bin_sort_iterator_new (bin);
2456 GST_OBJECT_UNLOCK (bin);
2461 static GstStateChangeReturn
2462 gst_bin_element_set_state (GstBin * bin, GstElement * element,
2463 GstClockTime base_time, GstClockTime start_time, GstState current,
2466 GstStateChangeReturn ret;
2467 GstState child_current, child_pending;
2471 GST_STATE_LOCK (element);
2473 GST_OBJECT_LOCK (element);
2474 /* set base_time and start time on child */
2475 GST_ELEMENT_START_TIME (element) = start_time;
2476 element->base_time = base_time;
2477 /* peel off the locked flag */
2478 locked = GST_ELEMENT_IS_LOCKED_STATE (element);
2479 /* Get the previous set_state result to preserve NO_PREROLL and ASYNC */
2480 ret = GST_STATE_RETURN (element);
2481 child_current = GST_STATE (element);
2482 child_pending = GST_STATE_PENDING (element);
2483 GST_OBJECT_UNLOCK (element);
2485 /* skip locked elements */
2486 if (G_UNLIKELY (locked))
2489 /* if the element was no preroll, just start changing the state regardless
2490 * if it had async elements (in the case of a bin) because they won't preroll
2492 if (G_UNLIKELY (ret == GST_STATE_CHANGE_NO_PREROLL)) {
2493 GST_DEBUG_OBJECT (element, "element is NO_PREROLL, ignore async elements");
2497 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2498 "current %s pending %s, desired next %s",
2499 gst_element_state_get_name (child_current),
2500 gst_element_state_get_name (child_pending),
2501 gst_element_state_get_name (next));
2503 /* always recurse into bins so that we can set the base time */
2504 if (GST_IS_BIN (element))
2507 /* Try not to change the state of elements that are already in the state we're
2509 if (child_current == next && child_pending == GST_STATE_VOID_PENDING) {
2510 /* child is already at the requested state, return previous return. Note that
2511 * if the child has a pending state to next, we will still call the
2512 * set_state function */
2514 } else if (next > current) {
2515 /* upward state change */
2516 if (child_pending == GST_STATE_VOID_PENDING) {
2517 /* .. and the child is not busy doing anything */
2518 if (child_current > next) {
2519 /* .. and is already past the requested state, assume it got there
2521 ret = GST_STATE_CHANGE_SUCCESS;
2524 } else if (child_pending > child_current) {
2525 /* .. and the child is busy going upwards */
2526 if (child_current >= next) {
2527 /* .. and is already past the requested state, assume it got there
2529 ret = GST_STATE_CHANGE_SUCCESS;
2533 /* .. and the child is busy going downwards */
2534 if (child_current > next) {
2535 /* .. and is already past the requested state, assume it got there
2537 ret = GST_STATE_CHANGE_SUCCESS;
2541 } else if (next < current) {
2542 /* downward state change */
2543 if (child_pending == GST_STATE_VOID_PENDING) {
2544 /* .. and the child is not busy doing anything */
2545 if (child_current < next) {
2546 /* .. and is already past the requested state, assume it got there
2548 ret = GST_STATE_CHANGE_SUCCESS;
2551 } else if (child_pending < child_current) {
2552 /* .. and the child is busy going downwards */
2553 if (child_current <= next) {
2554 /* .. and is already past the requested state, assume it got there
2556 ret = GST_STATE_CHANGE_SUCCESS;
2560 /* .. and the child is busy going upwards */
2561 if (child_current < next) {
2562 /* .. and is already past the requested state, assume it got there
2564 ret = GST_STATE_CHANGE_SUCCESS;
2571 GST_OBJECT_LOCK (bin);
2572 /* the element was busy with an upwards async state change, we must wait for
2573 * an ASYNC_DONE message before we attempt to change the state. */
2575 find_message (bin, GST_OBJECT_CAST (element),
2576 GST_MESSAGE_ASYNC_START))) {
2577 #ifndef GST_DISABLE_GST_DEBUG
2578 GstMessage *message = GST_MESSAGE_CAST (found->data);
2580 GST_DEBUG_OBJECT (element, "element message %p, %s async busy",
2581 message, GST_ELEMENT_NAME (GST_MESSAGE_SRC (message)));
2583 /* only wait for upward state changes */
2584 if (next > current) {
2585 /* We found an async element check if we can force its state to change or
2586 * if we have to wait for it to preroll. */
2590 GST_OBJECT_UNLOCK (bin);
2593 GST_DEBUG_OBJECT (bin,
2594 "setting element %s to %s, base_time %" GST_TIME_FORMAT,
2595 GST_ELEMENT_NAME (element), gst_element_state_get_name (next),
2596 GST_TIME_ARGS (base_time));
2599 ret = gst_element_set_state (element, next);
2601 GST_STATE_UNLOCK (element);
2607 GST_DEBUG_OBJECT (element,
2608 "element is locked, return previous return %s",
2609 gst_element_state_change_return_get_name (ret));
2610 GST_STATE_UNLOCK (element);
2615 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2616 "skipping transition from %s to %s",
2617 gst_element_state_get_name (child_current),
2618 gst_element_state_get_name (next));
2619 GST_STATE_UNLOCK (element);
2624 GST_DEBUG_OBJECT (element, "element was busy, delaying state change");
2625 GST_OBJECT_UNLOCK (bin);
2626 GST_STATE_UNLOCK (element);
2627 return GST_STATE_CHANGE_ASYNC;
2631 /* gst_iterator_fold functions for pads_activate
2632 * Stop the iterator if activating one pad failed, but only if that pad
2633 * has not been removed from the element. */
2635 activate_pads (const GValue * vpad, GValue * ret, gboolean * active)
2637 GstPad *pad = g_value_get_object (vpad);
2638 gboolean cont = TRUE;
2640 if (!gst_pad_set_active (pad, *active)) {
2641 if (GST_PAD_PARENT (pad) != NULL) {
2643 g_value_set_boolean (ret, FALSE);
2650 /* returns false on error or early cutout of the fold, true if all
2651 * pads in @iter were (de)activated successfully. */
2653 iterator_activate_fold_with_resync (GstIterator * iter, gpointer user_data)
2655 GstIteratorResult ires;
2658 /* no need to unset this later, it's just a boolean */
2659 g_value_init (&ret, G_TYPE_BOOLEAN);
2660 g_value_set_boolean (&ret, TRUE);
2663 ires = gst_iterator_fold (iter, (GstIteratorFoldFunction) activate_pads,
2666 case GST_ITERATOR_RESYNC:
2667 /* need to reset the result again */
2668 g_value_set_boolean (&ret, TRUE);
2669 gst_iterator_resync (iter);
2671 case GST_ITERATOR_DONE:
2672 /* all pads iterated, return collected value */
2675 /* iterator returned _ERROR or premature end with _OK,
2676 * mark an error and exit */
2677 g_value_set_boolean (&ret, FALSE);
2682 /* return collected value */
2683 return g_value_get_boolean (&ret);
2686 /* is called with STATE_LOCK
2689 gst_bin_src_pads_activate (GstBin * bin, gboolean active)
2694 GST_DEBUG_OBJECT (bin, "%s pads", active ? "activate" : "deactivate");
2696 iter = gst_element_iterate_src_pads ((GstElement *) bin);
2697 fold_ok = iterator_activate_fold_with_resync (iter, &active);
2698 gst_iterator_free (iter);
2699 if (G_UNLIKELY (!fold_ok))
2702 GST_DEBUG_OBJECT (bin, "pad %sactivation successful", active ? "" : "de");
2709 GST_DEBUG_OBJECT (bin, "pad %sactivation failed", active ? "" : "de");
2715 * gst_bin_recalculate_latency:
2718 * Query @bin for the current latency using and reconfigures this latency to all the
2719 * elements with a LATENCY event.
2721 * This method is typically called on the pipeline when a #GST_MESSAGE_LATENCY
2722 * is posted on the bus.
2724 * This function simply emits the 'do-latency' signal so any custom latency
2725 * calculations will be performed.
2727 * Returns: %TRUE if the latency could be queried and reconfigured.
2730 gst_bin_recalculate_latency (GstBin * bin)
2734 g_signal_emit (bin, gst_bin_signals[DO_LATENCY], 0, &res);
2735 GST_DEBUG_OBJECT (bin, "latency returned %d", res);
2741 gst_bin_do_latency_func (GstBin * bin)
2744 GstElement *element;
2745 GstClockTime min_latency, max_latency;
2748 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2750 element = GST_ELEMENT_CAST (bin);
2752 GST_DEBUG_OBJECT (element, "querying latency");
2754 query = gst_query_new_latency ();
2755 if ((res = gst_element_query (element, query))) {
2758 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
2760 GST_DEBUG_OBJECT (element,
2761 "got min latency %" GST_TIME_FORMAT ", max latency %"
2762 GST_TIME_FORMAT ", live %d", GST_TIME_ARGS (min_latency),
2763 GST_TIME_ARGS (max_latency), live);
2765 if (max_latency < min_latency) {
2766 /* this is an impossible situation, some parts of the pipeline might not
2767 * work correctly. We post a warning for now. */
2768 GST_ELEMENT_WARNING (element, CORE, CLOCK, (NULL),
2769 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
2770 GST_TIME_FORMAT ". Add queues or other buffering elements.",
2771 GST_TIME_ARGS (max_latency), GST_TIME_ARGS (min_latency)));
2774 /* configure latency on elements */
2775 res = gst_element_send_event (element, gst_event_new_latency (min_latency));
2777 GST_INFO_OBJECT (element, "configured latency of %" GST_TIME_FORMAT,
2778 GST_TIME_ARGS (min_latency));
2780 GST_WARNING_OBJECT (element,
2781 "did not really configure latency of %" GST_TIME_FORMAT,
2782 GST_TIME_ARGS (min_latency));
2785 /* this is not a real problem, we just don't configure any latency. */
2786 GST_WARNING_OBJECT (element, "failed to query latency");
2788 gst_query_unref (query);
2794 gst_bin_post_message (GstElement * element, GstMessage * msg)
2796 GstElementClass *pklass = (GstElementClass *) parent_class;
2799 ret = pklass->post_message (element, gst_message_ref (msg));
2801 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_STATE_CHANGED &&
2802 GST_MESSAGE_SRC (msg) == GST_OBJECT_CAST (element)) {
2803 GstState newstate, pending;
2805 gst_message_parse_state_changed (msg, NULL, &newstate, &pending);
2806 if (newstate == GST_STATE_PLAYING && pending == GST_STATE_VOID_PENDING) {
2807 GST_BIN_CAST (element)->priv->posted_playing = TRUE;
2808 bin_do_eos (GST_BIN_CAST (element));
2810 GST_BIN_CAST (element)->priv->posted_playing = FALSE;
2814 gst_message_unref (msg);
2820 reset_state (const GValue * data, gpointer user_data)
2822 GstElement *e = g_value_get_object (data);
2823 GstState state = GPOINTER_TO_INT (user_data);
2825 if (gst_element_set_state (e, state) == GST_STATE_CHANGE_FAILURE)
2826 GST_WARNING_OBJECT (e, "Failed to switch back down to %s",
2827 gst_element_state_get_name (state));
2830 static GstStateChangeReturn
2831 gst_bin_change_state_func (GstElement * element, GstStateChange transition)
2834 GstStateChangeReturn ret;
2835 GstState current, next;
2836 gboolean have_async;
2837 gboolean have_no_preroll;
2838 GstClockTime base_time, start_time;
2841 GValue data = { 0, };
2843 /* we don't need to take the STATE_LOCK, it is already taken */
2844 current = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2845 next = (GstState) GST_STATE_TRANSITION_NEXT (transition);
2847 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2848 "changing state of children from %s to %s",
2849 gst_element_state_get_name (current), gst_element_state_get_name (next));
2851 bin = GST_BIN_CAST (element);
2854 case GST_STATE_PLAYING:
2856 gboolean toplevel, asynchandling;
2858 GST_OBJECT_LOCK (bin);
2859 toplevel = BIN_IS_TOPLEVEL (bin);
2860 asynchandling = bin->priv->asynchandling;
2861 GST_OBJECT_UNLOCK (bin);
2864 gst_bin_recalculate_latency (bin);
2866 gst_element_post_message (element,
2867 gst_message_new_latency (GST_OBJECT_CAST (element)));
2870 case GST_STATE_PAUSED:
2871 /* Clear EOS list on next PAUSED */
2872 GST_OBJECT_LOCK (bin);
2873 GST_DEBUG_OBJECT (element, "clearing EOS elements");
2874 bin_remove_messages (bin, NULL, GST_MESSAGE_EOS);
2875 bin->priv->posted_eos = FALSE;
2876 if (current == GST_STATE_READY)
2877 bin_remove_messages (bin, NULL, GST_MESSAGE_STREAM_START);
2878 GST_OBJECT_UNLOCK (bin);
2879 if (current == GST_STATE_READY)
2880 if (!(gst_bin_src_pads_activate (bin, TRUE)))
2881 goto activate_failure;
2883 case GST_STATE_READY:
2884 /* Clear message list on next READY */
2885 GST_OBJECT_LOCK (bin);
2886 GST_DEBUG_OBJECT (element, "clearing all cached messages");
2887 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
2888 GST_OBJECT_UNLOCK (bin);
2889 /* We might not have reached PAUSED yet due to async errors,
2890 * make sure to always deactivate the pads nonetheless */
2891 if (!(gst_bin_src_pads_activate (bin, FALSE)))
2892 goto activate_failure;
2894 case GST_STATE_NULL:
2895 /* Clear message list on next NULL */
2896 GST_OBJECT_LOCK (bin);
2897 GST_DEBUG_OBJECT (element, "clearing all cached messages");
2898 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
2899 GST_OBJECT_UNLOCK (bin);
2900 if (current == GST_STATE_READY) {
2901 if (!(gst_bin_src_pads_activate (bin, FALSE)))
2902 goto activate_failure;
2909 /* this flag is used to make the async state changes return immediately. We
2910 * don't want them to interfere with this state change */
2911 GST_OBJECT_LOCK (bin);
2912 bin->polling = TRUE;
2913 GST_OBJECT_UNLOCK (bin);
2915 /* iterate in state change order */
2916 it = gst_bin_iterate_sorted (bin);
2918 /* mark if we've seen an ASYNC element in the bin when we did a state change.
2919 * Note how we don't reset this value when a resync happens, the reason being
2920 * that the async element posted ASYNC_START and we want to post ASYNC_DONE
2921 * even after a resync when the async element is gone */
2925 /* take base_time */
2926 base_time = gst_element_get_base_time (element);
2927 start_time = gst_element_get_start_time (element);
2929 have_no_preroll = FALSE;
2933 switch (gst_iterator_next (it, &data)) {
2934 case GST_ITERATOR_OK:
2938 child = g_value_get_object (&data);
2940 /* set state and base_time now */
2941 ret = gst_bin_element_set_state (bin, child, base_time, start_time,
2945 case GST_STATE_CHANGE_SUCCESS:
2946 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2947 "child '%s' changed state to %d(%s) successfully",
2948 GST_ELEMENT_NAME (child), next,
2949 gst_element_state_get_name (next));
2951 case GST_STATE_CHANGE_ASYNC:
2953 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2954 "child '%s' is changing state asynchronously to %s",
2955 GST_ELEMENT_NAME (child), gst_element_state_get_name (next));
2959 case GST_STATE_CHANGE_FAILURE:{
2962 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2963 "child '%s' failed to go to state %d(%s)",
2964 GST_ELEMENT_NAME (child),
2965 next, gst_element_state_get_name (next));
2967 /* Only fail if the child is still inside
2968 * this bin. It might've been removed already
2969 * because of the error by the bin subclass
2970 * to ignore the error. */
2971 parent = gst_object_get_parent (GST_OBJECT_CAST (child));
2972 if (parent == GST_OBJECT_CAST (element)) {
2973 /* element is still in bin, really error now */
2974 gst_object_unref (parent);
2977 /* child removed from bin, let the resync code redo the state
2979 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2980 "child '%s' was removed from the bin",
2981 GST_ELEMENT_NAME (child));
2984 gst_object_unref (parent);
2988 case GST_STATE_CHANGE_NO_PREROLL:
2989 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2990 "child '%s' changed state to %d(%s) successfully without preroll",
2991 GST_ELEMENT_NAME (child), next,
2992 gst_element_state_get_name (next));
2993 have_no_preroll = TRUE;
2996 g_assert_not_reached ();
2999 g_value_reset (&data);
3002 case GST_ITERATOR_RESYNC:
3003 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "iterator doing resync");
3004 gst_iterator_resync (it);
3007 case GST_ITERATOR_DONE:
3008 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "iterator done");
3014 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3015 if (G_UNLIKELY (ret == GST_STATE_CHANGE_FAILURE))
3018 if (have_no_preroll) {
3019 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
3020 "we have NO_PREROLL elements %s -> NO_PREROLL",
3021 gst_element_state_change_return_get_name (ret));
3022 ret = GST_STATE_CHANGE_NO_PREROLL;
3023 } else if (have_async) {
3024 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
3025 "we have ASYNC elements %s -> ASYNC",
3026 gst_element_state_change_return_get_name (ret));
3027 ret = GST_STATE_CHANGE_ASYNC;
3031 g_value_unset (&data);
3032 gst_iterator_free (it);
3034 GST_OBJECT_LOCK (bin);
3035 bin->polling = FALSE;
3036 /* it's possible that we did not get ASYNC from the children while the bin is
3037 * simulating ASYNC behaviour by posting an ASYNC_DONE message on the bus with
3038 * itself as the source. In that case we still want to check if the state
3039 * change completed. */
3040 if (ret != GST_STATE_CHANGE_ASYNC && !bin->priv->pending_async_done) {
3041 /* no element returned ASYNC and there are no pending async_done messages,
3042 * we can just complete. */
3043 GST_DEBUG_OBJECT (bin, "no async elements");
3046 /* when we get here an ASYNC element was found */
3047 if (GST_STATE_TARGET (bin) <= GST_STATE_READY) {
3048 /* we ignore ASYNC state changes when we go to READY or NULL */
3049 GST_DEBUG_OBJECT (bin, "target state %s <= READY",
3050 gst_element_state_get_name (GST_STATE_TARGET (bin)));
3054 GST_DEBUG_OBJECT (bin, "check async elements");
3055 /* check if all elements managed to commit their state already */
3056 if (!find_message (bin, NULL, GST_MESSAGE_ASYNC_START)) {
3057 /* nothing found, remove all old ASYNC_DONE messages. This can happen when
3058 * all the elements committed their state while we were doing the state
3059 * change. We will still return ASYNC for consistency but we commit the
3060 * state already so that a _get_state() will return immediately. */
3061 bin_remove_messages (bin, NULL, GST_MESSAGE_ASYNC_DONE);
3063 GST_DEBUG_OBJECT (bin, "async elements committed");
3064 bin_handle_async_done (bin, GST_STATE_CHANGE_SUCCESS, FALSE,
3065 GST_CLOCK_TIME_NONE);
3069 bin->priv->pending_async_done = FALSE;
3070 GST_OBJECT_UNLOCK (bin);
3072 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3073 "done changing bin's state from %s to %s, now in %s, ret %s",
3074 gst_element_state_get_name (current),
3075 gst_element_state_get_name (next),
3076 gst_element_state_get_name (GST_STATE (element)),
3077 gst_element_state_change_return_get_name (ret));
3084 GST_CAT_WARNING_OBJECT (GST_CAT_STATES, element,
3085 "failure (de)activating src pads");
3086 return GST_STATE_CHANGE_FAILURE;
3091 if (current < next) {
3092 GstIterator *it = gst_bin_iterate_sorted (GST_BIN (element));
3093 GstIteratorResult ret;
3095 GST_DEBUG_OBJECT (element,
3096 "Bin failed to change state, switching children back to %s",
3097 gst_element_state_get_name (current));
3100 gst_iterator_foreach (it, &reset_state, GINT_TO_POINTER (current));
3101 if (ret != GST_ITERATOR_RESYNC)
3103 gst_iterator_resync (it);
3105 gst_iterator_free (it);
3112 * This function is a utility event handler. It will send the event to all sinks
3113 * or sources and appropriate ghost pads depending on the event-direction.
3115 * Applications are free to override this behaviour and implement their own
3116 * handler, but this will work for pretty much all cases in practice.
3119 gst_bin_send_event (GstElement * element, GstEvent * event)
3121 GstBin *bin = GST_BIN_CAST (element);
3123 gboolean res = TRUE;
3124 gboolean done = FALSE;
3125 GValue data = { 0, };
3127 if (GST_EVENT_IS_DOWNSTREAM (event)) {
3128 iter = gst_bin_iterate_sources (bin);
3129 GST_DEBUG_OBJECT (bin, "Sending %s event to src children",
3130 GST_EVENT_TYPE_NAME (event));
3132 iter = gst_bin_iterate_sinks (bin);
3133 GST_DEBUG_OBJECT (bin, "Sending %s event to sink children",
3134 GST_EVENT_TYPE_NAME (event));
3138 switch (gst_iterator_next (iter, &data)) {
3139 case GST_ITERATOR_OK:
3141 GstElement *child = g_value_get_object (&data);
3143 gst_event_ref (event);
3144 res &= gst_element_send_event (child, event);
3146 GST_LOG_OBJECT (child, "After handling %s event: %d",
3147 GST_EVENT_TYPE_NAME (event), res);
3149 g_value_reset (&data);
3152 case GST_ITERATOR_RESYNC:
3153 gst_iterator_resync (iter);
3156 case GST_ITERATOR_DONE:
3159 case GST_ITERATOR_ERROR:
3160 g_assert_not_reached ();
3164 g_value_unset (&data);
3165 gst_iterator_free (iter);
3167 if (GST_EVENT_IS_DOWNSTREAM (event)) {
3168 iter = gst_element_iterate_sink_pads (GST_ELEMENT (bin));
3169 GST_DEBUG_OBJECT (bin, "Sending %s event to sink pads",
3170 GST_EVENT_TYPE_NAME (event));
3172 iter = gst_element_iterate_src_pads (GST_ELEMENT (bin));
3173 GST_DEBUG_OBJECT (bin, "Sending %s event to src pads",
3174 GST_EVENT_TYPE_NAME (event));
3179 switch (gst_iterator_next (iter, &data)) {
3180 case GST_ITERATOR_OK:
3182 GstPad *pad = g_value_get_object (&data);
3184 gst_event_ref (event);
3185 res &= gst_pad_send_event (pad, event);
3186 GST_LOG_OBJECT (pad, "After handling %s event: %d",
3187 GST_EVENT_TYPE_NAME (event), res);
3190 case GST_ITERATOR_RESYNC:
3191 gst_iterator_resync (iter);
3194 case GST_ITERATOR_DONE:
3197 case GST_ITERATOR_ERROR:
3198 g_assert_not_reached ();
3203 g_value_unset (&data);
3204 gst_iterator_free (iter);
3205 gst_event_unref (event);
3210 /* this is the function called by the threadpool. When async elements commit
3211 * their state, this function will attempt to bring the bin to the next state.
3214 gst_bin_continue_func (GstBin * bin, BinContinueData * data)
3216 GstState current, next, pending;
3217 GstStateChange transition;
3219 pending = data->pending;
3221 GST_DEBUG_OBJECT (bin, "waiting for state lock");
3222 GST_STATE_LOCK (bin);
3224 GST_DEBUG_OBJECT (bin, "doing state continue");
3225 GST_OBJECT_LOCK (bin);
3227 /* if a new state change happened after this thread was scheduled, we return
3229 if (data->cookie != GST_ELEMENT_CAST (bin)->state_cookie)
3232 current = GST_STATE (bin);
3233 next = GST_STATE_GET_NEXT (current, pending);
3234 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
3236 GST_STATE_NEXT (bin) = next;
3237 GST_STATE_PENDING (bin) = pending;
3239 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3240 GST_OBJECT_UNLOCK (bin);
3242 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3243 "continue state change %s to %s, final %s",
3244 gst_element_state_get_name (current),
3245 gst_element_state_get_name (next), gst_element_state_get_name (pending));
3247 gst_element_change_state (GST_ELEMENT_CAST (bin), transition);
3249 GST_STATE_UNLOCK (bin);
3250 GST_DEBUG_OBJECT (bin, "state continue done");
3256 GST_OBJECT_UNLOCK (bin);
3257 GST_STATE_UNLOCK (bin);
3258 GST_DEBUG_OBJECT (bin, "state continue aborted due to intervening change");
3263 static GstBusSyncReply
3264 bin_bus_handler (GstBus * bus, GstMessage * message, GstBin * bin)
3266 GstBinClass *bclass;
3268 bclass = GST_BIN_GET_CLASS (bin);
3269 if (bclass->handle_message)
3270 bclass->handle_message (bin, message);
3272 gst_message_unref (message);
3274 return GST_BUS_DROP;
3278 free_bin_continue_data (BinContinueData * data)
3280 g_slice_free (BinContinueData, data);
3284 bin_push_state_continue (GstBin * bin, BinContinueData * data)
3286 GST_DEBUG_OBJECT (bin, "pushing continue on thread pool");
3287 gst_element_call_async (GST_ELEMENT_CAST (bin),
3288 (GstElementCallAsyncFunc) gst_bin_continue_func, data,
3289 (GDestroyNotify) free_bin_continue_data);
3292 /* an element started an async state change, if we were not busy with a state
3293 * change, we perform a lost state.
3294 * This function is called with the OBJECT lock.
3297 bin_handle_async_start (GstBin * bin)
3299 GstState old_state, new_state;
3301 GstMessage *amessage = NULL;
3303 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_FAILURE)
3306 /* get our toplevel state */
3307 toplevel = BIN_IS_TOPLEVEL (bin);
3309 /* prepare an ASYNC_START message, we always post the start message even if we
3310 * are busy with a state change or when we are NO_PREROLL. */
3312 /* non toplevel bin, prepare async-start for the parent */
3313 amessage = gst_message_new_async_start (GST_OBJECT_CAST (bin));
3315 if (bin->polling || GST_STATE_PENDING (bin) != GST_STATE_VOID_PENDING)
3318 /* async starts are ignored when we are NO_PREROLL */
3319 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_NO_PREROLL)
3320 goto was_no_preroll;
3322 old_state = GST_STATE (bin);
3324 /* when we PLAYING we go back to PAUSED, when preroll happens, we go back to
3325 * PLAYING after optionally redistributing the base_time. */
3326 if (old_state > GST_STATE_PAUSED)
3327 new_state = GST_STATE_PAUSED;
3329 new_state = old_state;
3331 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
3332 "lost state of %s, new %s", gst_element_state_get_name (old_state),
3333 gst_element_state_get_name (new_state));
3335 GST_STATE (bin) = new_state;
3336 GST_STATE_NEXT (bin) = new_state;
3337 GST_STATE_PENDING (bin) = new_state;
3338 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3339 GST_OBJECT_UNLOCK (bin);
3342 _priv_gst_element_state_changed (GST_ELEMENT_CAST (bin), new_state, new_state,
3347 /* post our ASYNC_START. */
3348 GST_DEBUG_OBJECT (bin, "posting ASYNC_START to parent");
3349 gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
3351 GST_OBJECT_LOCK (bin);
3357 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "we had an error");
3362 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "state change busy");
3363 GST_OBJECT_UNLOCK (bin);
3368 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "ignoring, we are NO_PREROLL");
3369 GST_OBJECT_UNLOCK (bin);
3374 /* this function is called when there are no more async elements in the bin. We
3375 * post a state changed message and an ASYNC_DONE message.
3376 * This function is called with the OBJECT lock.
3379 bin_handle_async_done (GstBin * bin, GstStateChangeReturn ret,
3380 gboolean flag_pending, GstClockTime running_time)
3382 GstState current, pending, target;
3383 GstStateChangeReturn old_ret;
3384 GstState old_state, old_next;
3385 gboolean toplevel, state_changed = FALSE;
3386 GstMessage *amessage = NULL;
3387 BinContinueData *cont = NULL;
3389 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_FAILURE)
3392 pending = GST_STATE_PENDING (bin);
3397 /* check if there is something to commit */
3398 if (pending == GST_STATE_VOID_PENDING)
3399 goto nothing_pending;
3401 old_ret = GST_STATE_RETURN (bin);
3402 GST_STATE_RETURN (bin) = ret;
3404 /* move to the next target state */
3405 target = GST_STATE_TARGET (bin);
3406 pending = GST_STATE_PENDING (bin) = target;
3408 amessage = gst_message_new_async_done (GST_OBJECT_CAST (bin), running_time);
3410 old_state = GST_STATE (bin);
3411 /* this is the state we should go to next */
3412 old_next = GST_STATE_NEXT (bin);
3414 if (old_next != GST_STATE_PLAYING) {
3415 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3416 "committing state from %s to %s, old pending %s",
3417 gst_element_state_get_name (old_state),
3418 gst_element_state_get_name (old_next),
3419 gst_element_state_get_name (pending));
3421 /* update current state */
3422 current = GST_STATE (bin) = old_next;
3424 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3425 "setting state from %s to %s, pending %s",
3426 gst_element_state_get_name (old_state),
3427 gst_element_state_get_name (old_state),
3428 gst_element_state_get_name (pending));
3429 current = old_state;
3432 /* get our toplevel state */
3433 toplevel = BIN_IS_TOPLEVEL (bin);
3435 /* see if we reached the final state. If we are not toplevel, we also have to
3436 * stop here, the parent will continue our state. */
3437 if ((pending == current) || !toplevel) {
3438 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3439 "completed state change, pending VOID");
3441 /* mark VOID pending */
3442 pending = GST_STATE_VOID_PENDING;
3443 GST_STATE_PENDING (bin) = pending;
3444 GST_STATE_NEXT (bin) = GST_STATE_VOID_PENDING;
3446 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3447 "continue state change, pending %s",
3448 gst_element_state_get_name (pending));
3450 cont = g_slice_new (BinContinueData);
3452 /* cookie to detect concurrent state change */
3453 cont->cookie = GST_ELEMENT_CAST (bin)->state_cookie;
3454 /* pending target state */
3455 cont->pending = pending;
3457 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3458 GST_STATE_NEXT (bin) = GST_STATE_GET_NEXT (old_state, pending);
3461 if (old_next != GST_STATE_PLAYING) {
3462 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC) {
3463 state_changed = TRUE;
3466 GST_OBJECT_UNLOCK (bin);
3468 if (state_changed) {
3469 _priv_gst_element_state_changed (GST_ELEMENT_CAST (bin), old_state,
3473 /* post our combined ASYNC_DONE when all is ASYNC_DONE. */
3474 GST_DEBUG_OBJECT (bin, "posting ASYNC_DONE to parent");
3475 gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
3478 GST_OBJECT_LOCK (bin);
3480 /* toplevel, start continue state */
3481 GST_DEBUG_OBJECT (bin, "all async-done, starting state continue");
3482 bin_push_state_continue (bin, cont);
3484 GST_DEBUG_OBJECT (bin, "state change complete");
3485 GST_STATE_BROADCAST (bin);
3491 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "we had an error");
3496 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "state change busy");
3497 /* if we were busy with a state change and we are requested to flag a
3498 * pending async done, we do so here */
3500 bin->priv->pending_async_done = TRUE;
3505 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin, "nothing pending");
3511 bin_do_eos (GstBin * bin)
3513 guint32 seqnum = GST_SEQNUM_INVALID;
3516 GST_OBJECT_LOCK (bin);
3517 /* If all sinks are EOS, we're in PLAYING and no state change is pending
3518 * (or we're doing playing to playing and no one else will trigger posting
3519 * EOS for us) we forward the EOS message to the parent bin or application
3521 eos = GST_STATE (bin) == GST_STATE_PLAYING
3522 && (GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING ||
3523 GST_STATE_PENDING (bin) == GST_STATE_PLAYING)
3524 && bin->priv->posted_playing && is_eos (bin, &seqnum);
3525 GST_OBJECT_UNLOCK (bin);
3528 && g_atomic_int_compare_and_exchange (&bin->priv->posted_eos, FALSE,
3530 GstMessage *tmessage;
3532 /* Clear out any further messages, and reset posted_eos so we can
3533 detect any new EOS that happens (eg, after a seek). Since all
3534 sinks have now posted an EOS, there will be no further EOS events
3535 seen unless there is a new logical EOS */
3536 GST_OBJECT_LOCK (bin);
3537 bin_remove_messages (bin, NULL, GST_MESSAGE_EOS);
3538 bin->priv->posted_eos = FALSE;
3539 GST_OBJECT_UNLOCK (bin);
3541 tmessage = gst_message_new_eos (GST_OBJECT_CAST (bin));
3542 if (seqnum != GST_SEQNUM_INVALID)
3543 gst_message_set_seqnum (tmessage, seqnum);
3544 GST_DEBUG_OBJECT (bin,
3545 "all sinks posted EOS, posting seqnum #%" G_GUINT32_FORMAT, seqnum);
3546 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3548 GST_LOG_OBJECT (bin, "Not forwarding EOS due to in progress state change, "
3549 " or already posted, or waiting for more EOS");
3554 bin_do_stream_start (GstBin * bin)
3556 guint32 seqnum = GST_SEQNUM_INVALID;
3557 gboolean stream_start;
3558 gboolean have_group_id = FALSE;
3561 GST_OBJECT_LOCK (bin);
3562 /* If all sinks are STREAM_START we forward the STREAM_START message
3563 * to the parent bin or application
3565 stream_start = is_stream_start (bin, &seqnum, &have_group_id, &group_id);
3566 GST_OBJECT_UNLOCK (bin);
3569 GstMessage *tmessage;
3571 GST_OBJECT_LOCK (bin);
3572 bin_remove_messages (bin, NULL, GST_MESSAGE_STREAM_START);
3573 GST_OBJECT_UNLOCK (bin);
3575 tmessage = gst_message_new_stream_start (GST_OBJECT_CAST (bin));
3576 if (seqnum != GST_SEQNUM_INVALID)
3577 gst_message_set_seqnum (tmessage, seqnum);
3579 gst_message_set_group_id (tmessage, group_id);
3581 GST_DEBUG_OBJECT (bin,
3582 "all sinks posted STREAM_START, posting seqnum #%" G_GUINT32_FORMAT,
3584 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3588 /* must be called without the object lock as it posts messages */
3590 bin_do_message_forward (GstBin * bin, GstMessage * message)
3592 if (bin->priv->message_forward) {
3593 GstMessage *forwarded;
3595 GST_DEBUG_OBJECT (bin, "pass %s message upward",
3596 GST_MESSAGE_TYPE_NAME (message));
3598 /* we need to convert these messages to element messages so that our parent
3599 * bin can easily ignore them and so that the application can easily
3600 * distinguish between the internally forwarded and the real messages. */
3601 forwarded = gst_message_new_element (GST_OBJECT_CAST (bin),
3602 gst_structure_new ("GstBinForwarded",
3603 "message", GST_TYPE_MESSAGE, message, NULL));
3605 gst_element_post_message (GST_ELEMENT_CAST (bin), forwarded);
3610 gst_bin_update_context (GstBin * bin, GstContext * context)
3612 GST_OBJECT_LOCK (bin);
3613 gst_bin_update_context_unlocked (bin, context);
3614 GST_OBJECT_UNLOCK (bin);
3618 gst_bin_update_context_unlocked (GstBin * bin, GstContext * context)
3620 const gchar *context_type;
3621 GList *l, **contexts;
3623 contexts = &GST_ELEMENT_CAST (bin)->contexts;
3624 context_type = gst_context_get_context_type (context);
3626 GST_DEBUG_OBJECT (bin, "set context %p %" GST_PTR_FORMAT, context,
3627 gst_context_get_structure (context));
3628 for (l = *contexts; l; l = l->next) {
3629 GstContext *tmp = l->data;
3630 const gchar *tmp_type = gst_context_get_context_type (tmp);
3632 /* Always store newest context but never replace
3633 * a persistent one by a non-persistent one */
3634 if (strcmp (context_type, tmp_type) == 0 &&
3635 (gst_context_is_persistent (context) ||
3636 !gst_context_is_persistent (tmp))) {
3637 gst_context_replace ((GstContext **) & l->data, context);
3641 /* Not found? Add */
3643 *contexts = g_list_prepend (*contexts, gst_context_ref (context));
3647 /* handle child messages:
3649 * This method is called synchronously when a child posts a message on
3652 * GST_MESSAGE_EOS: This message is only posted by sinks
3653 * in the PLAYING state. If all sinks posted the EOS message, post
3656 * GST_MESSAGE_STATE_DIRTY: Deprecated
3658 * GST_MESSAGE_SEGMENT_START: just collect, never forward upwards. If an
3659 * element posts segment_start twice, only the last message is kept.
3661 * GST_MESSAGE_SEGMENT_DONE: replace SEGMENT_START message from same poster
3662 * with the segment_done message. If there are no more segment_start
3663 * messages, post segment_done message upwards.
3665 * GST_MESSAGE_DURATION_CHANGED: clear any cached durations.
3666 * Whenever someone performs a duration query on the bin, we store the
3667 * result so we can answer it quicker the next time. Any element that
3668 * changes its duration marks our cached values invalid.
3669 * This message is also posted upwards. This is currently disabled
3670 * because too many elements don't post DURATION_CHANGED messages when
3671 * the duration changes.
3673 * GST_MESSAGE_CLOCK_LOST: This message is posted by an element when it
3674 * can no longer provide a clock. The default bin behaviour is to
3675 * check if the lost clock was the one provided by the bin. If so and
3676 * we are currently in the PLAYING state, we forward the message to
3678 * This message is also generated when we remove a clock provider from
3679 * a bin. If this message is received by the application, it should
3680 * PAUSE the pipeline and set it back to PLAYING to force a new clock
3681 * and a new base_time distribution.
3683 * GST_MESSAGE_CLOCK_PROVIDE: This message is generated when an element
3684 * can provide a clock. This mostly happens when we add a new clock
3685 * provider to the bin. The default behaviour of the bin is to mark the
3686 * currently selected clock as dirty, which will perform a clock
3687 * recalculation the next time we are asked to provide a clock.
3688 * This message is never sent to the application but is forwarded to
3691 * GST_MESSAGE_ASYNC_START: Create an internal ELEMENT message that stores
3692 * the state of the element and the fact that the element will need a
3693 * new base_time. This message is not forwarded to the application.
3695 * GST_MESSAGE_ASYNC_DONE: Find the internal ELEMENT message we kept for the
3696 * element when it posted ASYNC_START. If all elements are done, post a
3697 * ASYNC_DONE message to the parent.
3699 * OTHER: post upwards.
3702 gst_bin_handle_message_func (GstBin * bin, GstMessage * message)
3705 GstMessageType type;
3706 GstMessage *tmessage;
3709 src = GST_MESSAGE_SRC (message);
3710 type = GST_MESSAGE_TYPE (message);
3712 GST_DEBUG_OBJECT (bin, "[msg %p] handling child %s message of type %s",
3713 message, src ? GST_ELEMENT_NAME (src) : "(NULL)",
3714 GST_MESSAGE_TYPE_NAME (message));
3717 case GST_MESSAGE_ERROR:
3719 GST_OBJECT_LOCK (bin);
3721 GST_DEBUG_OBJECT (bin, "got ERROR message, unlocking state change");
3722 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_FAILURE;
3723 GST_STATE_BROADCAST (bin);
3724 GST_OBJECT_UNLOCK (bin);
3728 case GST_MESSAGE_EOS:
3731 /* collect all eos messages from the children */
3732 bin_do_message_forward (bin, message);
3733 GST_OBJECT_LOCK (bin);
3734 /* ref message for future use */
3735 bin_replace_message (bin, message, GST_MESSAGE_EOS);
3736 GST_OBJECT_UNLOCK (bin);
3741 case GST_MESSAGE_STREAM_START:
3744 /* collect all stream_start messages from the children */
3745 GST_OBJECT_LOCK (bin);
3746 /* ref message for future use */
3747 bin_replace_message (bin, message, GST_MESSAGE_STREAM_START);
3748 GST_OBJECT_UNLOCK (bin);
3750 bin_do_stream_start (bin);
3753 case GST_MESSAGE_STATE_DIRTY:
3755 GST_WARNING_OBJECT (bin, "received deprecated STATE_DIRTY message");
3758 gst_message_unref (message);
3761 case GST_MESSAGE_SEGMENT_START:{
3762 gboolean post = FALSE;
3766 gst_message_parse_segment_start (message, &format, &position);
3767 seqnum = gst_message_get_seqnum (message);
3769 bin_do_message_forward (bin, message);
3771 GST_OBJECT_LOCK (bin);
3772 /* if this is the first segment-start, post to parent but not to the
3774 if (!find_message (bin, NULL, GST_MESSAGE_SEGMENT_START) &&
3775 (GST_OBJECT_PARENT (bin) != NULL)) {
3778 /* replace any previous segment_start message from this source
3779 * with the new segment start message */
3780 bin_replace_message (bin, message, GST_MESSAGE_SEGMENT_START);
3781 GST_OBJECT_UNLOCK (bin);
3783 tmessage = gst_message_new_segment_start (GST_OBJECT_CAST (bin),
3785 gst_message_set_seqnum (tmessage, seqnum);
3787 /* post segment start with initial format and position. */
3788 GST_DEBUG_OBJECT (bin, "posting SEGMENT_START (%u) bus message: %p",
3790 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3794 case GST_MESSAGE_SEGMENT_DONE:
3796 gboolean post = FALSE;
3800 gst_message_parse_segment_done (message, &format, &position);
3801 seqnum = gst_message_get_seqnum (message);
3803 bin_do_message_forward (bin, message);
3805 GST_OBJECT_LOCK (bin);
3806 bin_replace_message (bin, message, GST_MESSAGE_SEGMENT_START);
3807 /* if there are no more segment_start messages, everybody posted
3808 * a segment_done and we can post one on the bus. */
3810 /* we don't care who still has a pending segment start */
3811 if (!find_message (bin, NULL, GST_MESSAGE_SEGMENT_START)) {
3814 /* remove all old segment_done messages */
3815 bin_remove_messages (bin, NULL, GST_MESSAGE_SEGMENT_DONE);
3817 GST_OBJECT_UNLOCK (bin);
3819 tmessage = gst_message_new_segment_done (GST_OBJECT_CAST (bin),
3821 gst_message_set_seqnum (tmessage, seqnum);
3823 /* post segment done with latest format and position. */
3824 GST_DEBUG_OBJECT (bin, "posting SEGMENT_DONE (%u) bus message: %p",
3826 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3830 case GST_MESSAGE_DURATION_CHANGED:
3832 /* FIXME: remove all cached durations, next time somebody asks
3833 * for duration, we will recalculate. */
3835 GST_OBJECT_LOCK (bin);
3836 bin_remove_messages (bin, NULL, GST_MESSAGE_DURATION_CHANGED);
3837 GST_OBJECT_UNLOCK (bin);
3841 case GST_MESSAGE_CLOCK_LOST:
3843 GstClock **provided_clock_p;
3844 GstElement **clock_provider_p;
3845 gboolean playing, toplevel, provided, forward;
3848 gst_message_parse_clock_lost (message, &clock);
3850 GST_OBJECT_LOCK (bin);
3851 bin->clock_dirty = TRUE;
3852 /* if we lost the clock that we provided, post to parent but
3853 * only if we are not a top-level bin or PLAYING.
3854 * The reason for this is that applications should be able
3855 * to PAUSE/PLAY if they receive this message without worrying
3856 * about the state of the pipeline. */
3857 provided = (clock == bin->provided_clock);
3858 playing = (GST_STATE (bin) == GST_STATE_PLAYING);
3859 toplevel = GST_OBJECT_PARENT (bin) == NULL;
3860 forward = provided && (playing || !toplevel);
3862 GST_DEBUG_OBJECT (bin,
3863 "Lost clock %" GST_PTR_FORMAT " provided by %" GST_PTR_FORMAT,
3864 bin->provided_clock, bin->clock_provider);
3865 provided_clock_p = &bin->provided_clock;
3866 clock_provider_p = &bin->clock_provider;
3867 gst_object_replace ((GstObject **) provided_clock_p, NULL);
3868 gst_object_replace ((GstObject **) clock_provider_p, NULL);
3870 GST_DEBUG_OBJECT (bin, "provided %d, playing %d, forward %d",
3871 provided, playing, forward);
3872 GST_OBJECT_UNLOCK (bin);
3878 gst_message_unref (message);
3881 case GST_MESSAGE_CLOCK_PROVIDE:
3885 GST_OBJECT_LOCK (bin);
3886 bin->clock_dirty = TRUE;
3887 /* a new clock is available, post to parent but not
3888 * to the application */
3889 forward = GST_OBJECT_PARENT (bin) != NULL;
3890 GST_OBJECT_UNLOCK (bin);
3896 gst_message_unref (message);
3899 case GST_MESSAGE_ASYNC_START:
3903 GST_DEBUG_OBJECT (bin, "ASYNC_START message %p, %s", message,
3904 src ? GST_OBJECT_NAME (src) : "(NULL)");
3906 bin_do_message_forward (bin, message);
3908 GST_OBJECT_LOCK (bin);
3909 /* we ignore the message if we are going to <= READY */
3910 if ((target = GST_STATE_TARGET (bin)) <= GST_STATE_READY)
3911 goto ignore_start_message;
3913 /* takes ownership of the message */
3914 bin_replace_message (bin, message, GST_MESSAGE_ASYNC_START);
3916 bin_handle_async_start (bin);
3917 GST_OBJECT_UNLOCK (bin);
3920 ignore_start_message:
3922 GST_DEBUG_OBJECT (bin, "ignoring message, target %s",
3923 gst_element_state_get_name (target));
3924 GST_OBJECT_UNLOCK (bin);
3925 gst_message_unref (message);
3929 case GST_MESSAGE_ASYNC_DONE:
3931 GstClockTime running_time;
3934 GST_DEBUG_OBJECT (bin, "ASYNC_DONE message %p, %s", message,
3935 src ? GST_OBJECT_NAME (src) : "(NULL)");
3937 gst_message_parse_async_done (message, &running_time);
3939 bin_do_message_forward (bin, message);
3941 GST_OBJECT_LOCK (bin);
3942 /* ignore messages if we are shutting down */
3943 if ((target = GST_STATE_TARGET (bin)) <= GST_STATE_READY)
3944 goto ignore_done_message;
3946 bin_replace_message (bin, message, GST_MESSAGE_ASYNC_START);
3947 /* if there are no more ASYNC_START messages, everybody posted
3948 * a ASYNC_DONE and we can post one on the bus. When checking, we
3949 * don't care who still has a pending ASYNC_START */
3950 if (!find_message (bin, NULL, GST_MESSAGE_ASYNC_START)) {
3951 /* nothing found, remove all old ASYNC_DONE messages */
3952 bin_remove_messages (bin, NULL, GST_MESSAGE_ASYNC_DONE);
3954 GST_DEBUG_OBJECT (bin, "async elements committed");
3955 /* when we get an async done message when a state change was busy, we
3956 * need to set the pending_done flag so that at the end of the state
3957 * change we can see if we need to verify pending async elements, hence
3958 * the TRUE argument here. */
3959 bin_handle_async_done (bin, GST_STATE_CHANGE_SUCCESS, TRUE,
3962 GST_DEBUG_OBJECT (bin, "there are more async elements pending");
3964 GST_OBJECT_UNLOCK (bin);
3967 ignore_done_message:
3969 GST_DEBUG_OBJECT (bin, "ignoring message, target %s",
3970 gst_element_state_get_name (target));
3971 GST_OBJECT_UNLOCK (bin);
3972 gst_message_unref (message);
3976 case GST_MESSAGE_STRUCTURE_CHANGE:
3980 gst_message_parse_structure_change (message, NULL, NULL, &busy);
3982 GST_OBJECT_LOCK (bin);
3984 /* while the pad is busy, avoid following it when doing state changes.
3985 * Don't update the cookie yet, we will do that after the structure
3986 * change finished and we are ready to inspect the new updated
3988 bin_replace_message (bin, message, GST_MESSAGE_STRUCTURE_CHANGE);
3991 /* a pad link/unlink ended, signal the state change iterator that we
3992 * need to resync by updating the structure_cookie. */
3993 bin_remove_messages (bin, GST_MESSAGE_SRC (message),
3994 GST_MESSAGE_STRUCTURE_CHANGE);
3995 if (!GST_BIN_IS_NO_RESYNC (bin))
3996 bin->priv->structure_cookie++;
3998 GST_OBJECT_UNLOCK (bin);
4001 gst_message_unref (message);
4005 case GST_MESSAGE_NEED_CONTEXT:{
4006 const gchar *context_type;
4007 GList *l, *contexts;
4009 gst_message_parse_context_type (message, &context_type);
4010 GST_OBJECT_LOCK (bin);
4011 contexts = GST_ELEMENT_CAST (bin)->contexts;
4012 GST_LOG_OBJECT (bin, "got need-context message type: %s", context_type);
4013 for (l = contexts; l; l = l->next) {
4014 GstContext *tmp = l->data;
4015 const gchar *tmp_type = gst_context_get_context_type (tmp);
4017 if (strcmp (context_type, tmp_type) == 0) {
4018 gst_element_set_context (GST_ELEMENT (src), l->data);
4022 GST_OBJECT_UNLOCK (bin);
4024 /* Forward if we couldn't answer the message */
4028 gst_message_unref (message);
4033 case GST_MESSAGE_HAVE_CONTEXT:{
4034 GstContext *context;
4036 gst_message_parse_have_context (message, &context);
4037 gst_bin_update_context (bin, context);
4038 gst_context_unref (context);
4050 /* Send all other messages upward */
4051 GST_DEBUG_OBJECT (bin, "posting message upward");
4052 gst_element_post_message (GST_ELEMENT_CAST (bin), message);
4057 /* generic struct passed to all query fold methods */
4066 typedef void (*QueryInitFunction) (GstBin * bin, QueryFold * fold);
4067 typedef void (*QueryDoneFunction) (GstBin * bin, QueryFold * fold);
4069 /* for duration/position we collect all durations/positions and take
4070 * the MAX of all valid results */
4072 bin_query_min_max_init (GstBin * bin, QueryFold * fold)
4080 bin_query_duration_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4082 gboolean res = FALSE;
4083 GstObject *item = g_value_get_object (vitem);
4084 if (GST_IS_PAD (item))
4085 res = gst_pad_query (GST_PAD (item), fold->query);
4087 res = gst_element_query (GST_ELEMENT (item), fold->query);
4092 g_value_set_boolean (ret, TRUE);
4094 gst_query_parse_duration (fold->query, NULL, &duration);
4096 GST_DEBUG_OBJECT (item, "got duration %" G_GINT64_FORMAT, duration);
4098 if (duration == -1) {
4099 /* duration query succeeded, but duration is unknown */
4104 if (duration > fold->max)
4105 fold->max = duration;
4112 bin_query_duration_done (GstBin * bin, QueryFold * fold)
4116 gst_query_parse_duration (fold->query, &format, NULL);
4117 /* store max in query result */
4118 gst_query_set_duration (fold->query, format, fold->max);
4120 GST_DEBUG_OBJECT (bin, "max duration %" G_GINT64_FORMAT, fold->max);
4122 /* FIXME: re-implement duration caching */
4125 GST_OBJECT_LOCK (bin);
4126 bin->messages = g_list_prepend (bin->messages,
4127 gst_message_new_duration (GST_OBJECT_CAST (bin), format, fold->max));
4128 GST_OBJECT_UNLOCK (bin);
4133 bin_query_position_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4135 gboolean res = FALSE;
4136 GstObject *item = g_value_get_object (vitem);
4137 if (GST_IS_PAD (item))
4138 res = gst_pad_query (GST_PAD (item), fold->query);
4140 res = gst_element_query (GST_ELEMENT (item), fold->query);
4145 g_value_set_boolean (ret, TRUE);
4147 gst_query_parse_position (fold->query, NULL, &position);
4149 GST_DEBUG_OBJECT (item, "got position %" G_GINT64_FORMAT, position);
4151 if (position > fold->max)
4152 fold->max = position;
4159 bin_query_position_done (GstBin * bin, QueryFold * fold)
4163 gst_query_parse_position (fold->query, &format, NULL);
4164 /* store max in query result */
4165 gst_query_set_position (fold->query, format, fold->max);
4167 GST_DEBUG_OBJECT (bin, "max position %" G_GINT64_FORMAT, fold->max);
4171 bin_query_latency_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4173 gboolean res = FALSE;
4174 GstObject *item = g_value_get_object (vitem);
4175 if (GST_IS_PAD (item))
4176 res = gst_pad_query (GST_PAD (item), fold->query);
4178 res = gst_element_query (GST_ELEMENT (item), fold->query);
4180 GstClockTime min, max;
4183 gst_query_parse_latency (fold->query, &live, &min, &max);
4185 GST_DEBUG_OBJECT (item,
4186 "got latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
4187 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
4189 /* for the combined latency we collect the MAX of all min latencies and
4190 * the MIN of all max latencies */
4192 if (min > fold->min)
4194 if (fold->max == -1)
4196 else if (max < fold->max)
4202 g_value_set_boolean (ret, FALSE);
4203 GST_DEBUG_OBJECT (item, "failed query");
4210 bin_query_latency_done (GstBin * bin, QueryFold * fold)
4212 /* store max in query result */
4213 gst_query_set_latency (fold->query, fold->live, fold->min, fold->max);
4215 GST_DEBUG_OBJECT (bin,
4216 "latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
4217 ", live %d", GST_TIME_ARGS (fold->min), GST_TIME_ARGS (fold->max),
4221 /* generic fold, return first valid result */
4223 bin_query_generic_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4225 gboolean res = FALSE;
4226 GstObject *item = g_value_get_object (vitem);
4227 if (GST_IS_PAD (item))
4228 res = gst_pad_query (GST_PAD (item), fold->query);
4230 res = gst_element_query (GST_ELEMENT (item), fold->query);
4232 g_value_set_boolean (ret, TRUE);
4233 GST_DEBUG_OBJECT (item, "answered query %p", fold->query);
4236 /* and stop as soon as we have a valid result */
4240 /* Perform a query iteration for the given bin. The query is stored in
4241 * QueryFold and iter should be either a GstPad iterator or a
4242 * GstElement iterator. */
4244 bin_iterate_fold (GstBin * bin, GstIterator * iter, QueryInitFunction fold_init,
4245 QueryDoneFunction fold_done, GstIteratorFoldFunction fold_func,
4246 QueryFold * fold_data, gboolean default_return)
4248 gboolean res = default_return;
4250 /* set the result of the query to FALSE initially */
4251 g_value_init (&ret, G_TYPE_BOOLEAN);
4252 g_value_set_boolean (&ret, res);
4255 GstIteratorResult ires;
4257 ires = gst_iterator_fold (iter, fold_func, &ret, fold_data);
4260 case GST_ITERATOR_RESYNC:
4261 gst_iterator_resync (iter);
4263 fold_init (bin, fold_data);
4264 g_value_set_boolean (&ret, res);
4266 case GST_ITERATOR_OK:
4267 case GST_ITERATOR_DONE:
4268 res = g_value_get_boolean (&ret);
4269 if (fold_done != NULL && res)
4270 fold_done (bin, fold_data);
4282 gst_bin_query (GstElement * element, GstQuery * query)
4284 GstBin *bin = GST_BIN_CAST (element);
4286 gboolean default_return = FALSE;
4287 gboolean res = FALSE;
4288 gboolean src_pads_query_result = FALSE;
4289 GstIteratorFoldFunction fold_func;
4290 QueryInitFunction fold_init = NULL;
4291 QueryDoneFunction fold_done = NULL;
4292 QueryFold fold_data;
4294 switch (GST_QUERY_TYPE (query)) {
4295 case GST_QUERY_DURATION:
4297 /* FIXME: implement duration caching in GstBin again */
4302 gst_query_parse_duration (query, &qformat, NULL);
4304 /* find cached duration query */
4305 GST_OBJECT_LOCK (bin);
4306 for (cached = bin->messages; cached; cached = g_list_next (cached)) {
4307 GstMessage *message = (GstMessage *) cached->data;
4309 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_DURATION_CHANGED &&
4310 GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (bin)) {
4314 gst_message_parse_duration (message, &format, &duration);
4316 /* if cached same format, copy duration in query result */
4317 if (format == qformat) {
4318 GST_DEBUG_OBJECT (bin, "return cached duration %" G_GINT64_FORMAT,
4320 GST_OBJECT_UNLOCK (bin);
4322 gst_query_set_duration (query, qformat, duration);
4328 GST_OBJECT_UNLOCK (bin);
4330 #ifndef GST_DISABLE_GST_DEBUG
4332 /* Quieten this particularly annoying FIXME a bit: */
4333 static gboolean printed_fixme = FALSE;
4334 if (!printed_fixme) {
4335 GST_FIXME ("implement duration caching in GstBin again");
4336 printed_fixme = TRUE;
4342 /* no cached value found, iterate and collect durations */
4343 fold_func = (GstIteratorFoldFunction) bin_query_duration_fold;
4344 fold_init = bin_query_min_max_init;
4345 fold_done = bin_query_duration_done;
4348 case GST_QUERY_POSITION:
4350 fold_func = (GstIteratorFoldFunction) bin_query_position_fold;
4351 fold_init = bin_query_min_max_init;
4352 fold_done = bin_query_position_done;
4355 case GST_QUERY_LATENCY:
4357 fold_func = (GstIteratorFoldFunction) bin_query_latency_fold;
4358 fold_init = bin_query_min_max_init;
4359 fold_done = bin_query_latency_done;
4360 default_return = TRUE;
4364 fold_func = (GstIteratorFoldFunction) bin_query_generic_fold;
4368 fold_data.query = query;
4370 iter = gst_bin_iterate_sinks (bin);
4371 GST_DEBUG_OBJECT (bin, "Sending query %p (type %s) to sink children",
4372 query, GST_QUERY_TYPE_NAME (query));
4375 fold_init (bin, &fold_data);
4378 bin_iterate_fold (bin, iter, fold_init, fold_done, fold_func, &fold_data,
4380 gst_iterator_free (iter);
4383 /* Query the source pads of the element */
4384 iter = gst_element_iterate_src_pads (element);
4385 src_pads_query_result =
4386 bin_iterate_fold (bin, iter, fold_init, fold_done, fold_func,
4387 &fold_data, default_return);
4388 gst_iterator_free (iter);
4390 if (src_pads_query_result)
4394 GST_DEBUG_OBJECT (bin, "query %p result %d", query, res);
4400 set_context (const GValue * item, gpointer user_data)
4402 GstElement *element = g_value_get_object (item);
4404 gst_element_set_context (element, user_data);
4408 gst_bin_set_context (GstElement * element, GstContext * context)
4411 GstIterator *children;
4413 g_return_if_fail (GST_IS_BIN (element));
4415 bin = GST_BIN (element);
4417 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
4419 children = gst_bin_iterate_elements (bin);
4420 while (gst_iterator_foreach (children, set_context,
4421 context) == GST_ITERATOR_RESYNC)
4422 gst_iterator_resync (children);
4423 gst_iterator_free (children);
4427 compare_name (const GValue * velement, const gchar * name)
4430 GstElement *element = g_value_get_object (velement);
4432 GST_OBJECT_LOCK (element);
4433 eq = strcmp (GST_ELEMENT_NAME (element), name);
4434 GST_OBJECT_UNLOCK (element);
4440 * gst_bin_get_by_name:
4442 * @name: the element name to search for
4444 * Gets the element with the given name from a bin. This
4445 * function recurses into child bins.
4447 * Returns %NULL if no element with the given name is found in the bin.
4449 * MT safe. Caller owns returned reference.
4451 * Returns: (transfer full) (nullable): the #GstElement with the given
4455 gst_bin_get_by_name (GstBin * bin, const gchar * name)
4457 GstIterator *children;
4458 GValue result = { 0, };
4459 GstElement *element;
4462 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4464 GST_CAT_INFO (GST_CAT_PARENTAGE, "[%s]: looking up child element %s",
4465 GST_ELEMENT_NAME (bin), name);
4467 children = gst_bin_iterate_recurse (bin);
4468 found = gst_iterator_find_custom (children,
4469 (GCompareFunc) compare_name, &result, (gpointer) name);
4470 gst_iterator_free (children);
4473 element = g_value_dup_object (&result);
4474 g_value_unset (&result);
4483 * gst_bin_get_by_name_recurse_up:
4485 * @name: the element name to search for
4487 * Gets the element with the given name from this bin. If the
4488 * element is not found, a recursion is performed on the parent bin.
4491 * - no element with the given name is found in the bin
4493 * MT safe. Caller owns returned reference.
4495 * Returns: (transfer full) (nullable): the #GstElement with the given
4499 gst_bin_get_by_name_recurse_up (GstBin * bin, const gchar * name)
4503 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4504 g_return_val_if_fail (name != NULL, NULL);
4506 result = gst_bin_get_by_name (bin, name);
4511 parent = gst_object_get_parent (GST_OBJECT_CAST (bin));
4513 if (GST_IS_BIN (parent)) {
4514 result = gst_bin_get_by_name_recurse_up (GST_BIN_CAST (parent), name);
4516 gst_object_unref (parent);
4524 compare_interface (const GValue * velement, GValue * interface)
4526 GstElement *element = g_value_get_object (velement);
4527 GType interface_type = (GType) g_value_get_pointer (interface);
4530 if (G_TYPE_CHECK_INSTANCE_TYPE (element, interface_type)) {
4539 * gst_bin_get_by_interface:
4541 * @iface: the #GType of an interface
4543 * Looks for an element inside the bin that implements the given
4544 * interface. If such an element is found, it returns the element.
4545 * You can cast this element to the given interface afterwards. If you want
4546 * all elements that implement the interface, use
4547 * gst_bin_iterate_all_by_interface(). This function recurses into child bins.
4549 * MT safe. Caller owns returned reference.
4551 * Returns: (transfer full) (nullable): A #GstElement inside the bin
4552 * implementing the interface
4555 gst_bin_get_by_interface (GstBin * bin, GType iface)
4557 GstIterator *children;
4558 GValue result = { 0, };
4559 GstElement *element;
4561 GValue viface = { 0, };
4563 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4564 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface), NULL);
4566 g_value_init (&viface, G_TYPE_POINTER);
4567 g_value_set_pointer (&viface, (gpointer) iface);
4569 children = gst_bin_iterate_recurse (bin);
4570 found = gst_iterator_find_custom (children, (GCompareFunc) compare_interface,
4572 gst_iterator_free (children);
4575 element = g_value_dup_object (&result);
4576 g_value_unset (&result);
4580 g_value_unset (&viface);
4586 * gst_bin_iterate_all_by_interface:
4588 * @iface: the #GType of an interface
4590 * Looks for all elements inside the bin that implements the given
4591 * interface. You can safely cast all returned elements to the given interface.
4592 * The function recurses inside child bins. The iterator will yield a series
4593 * of #GstElement that should be unreffed after use.
4595 * MT safe. Caller owns returned value.
4597 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
4598 * for all elements in the bin implementing the given interface,
4602 gst_bin_iterate_all_by_interface (GstBin * bin, GType iface)
4604 GstIterator *children;
4605 GstIterator *result;
4606 GValue viface = { 0, };
4608 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4609 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface), NULL);
4611 g_value_init (&viface, G_TYPE_POINTER);
4612 g_value_set_pointer (&viface, (gpointer) iface);
4614 children = gst_bin_iterate_recurse (bin);
4615 result = gst_iterator_filter (children, (GCompareFunc) compare_interface,
4618 g_value_unset (&viface);