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.
27 * @short_description: Base class and element that can contain other elements
29 * #GstBin is an element that can contain other #GstElement, allowing them to be
31 * Pads from the child elements can be ghosted to the bin, see #GstGhostPad.
32 * This makes the bin look like any other elements and enables creation of
33 * higher-level abstraction elements.
35 * A new #GstBin is created with gst_bin_new(). Use a #GstPipeline instead if you
36 * want to create a toplevel bin because a normal bin doesn't have a bus or
37 * handle clock distribution of its own.
39 * After the bin has been created you will typically add elements to it with
40 * gst_bin_add(). You can remove elements with gst_bin_remove().
42 * An element can be retrieved from a bin with gst_bin_get_by_name(), using the
43 * elements name. gst_bin_get_by_name_recurse_up() is mainly used for internal
44 * purposes and will query the parent bins when the element is not found in the
47 * An iterator of elements in a bin can be retrieved with
48 * gst_bin_iterate_elements(). Various other iterators exist to retrieve the
51 * gst_object_unref() is used to drop your reference to the bin.
53 * The #GstBin::element-added signal is fired whenever a new element is added to
54 * the bin. Likewise the #GstBin::element-removed signal is fired whenever an
55 * element is removed from the bin.
57 * A #GstBin internally intercepts every #GstMessage posted by its children and
58 * implements the following default behaviour for each of them:
60 * * %GST_MESSAGE_EOS: This message is only posted by sinks in the PLAYING
61 * state. If all sinks posted the EOS message, this bin will post and EOS
64 * * %GST_MESSAGE_SEGMENT_START: Just collected and never forwarded upwards.
65 * The messages are used to decide when all elements have completed playback
68 * * %GST_MESSAGE_SEGMENT_DONE: Is posted by #GstBin when all elements that posted
69 * a SEGMENT_START have posted a SEGMENT_DONE.
71 * * %GST_MESSAGE_DURATION_CHANGED: Is posted by an element that detected a change
72 * in the stream duration. The duration change is posted to the
73 * application so that it can refetch the new duration with a duration
76 * Note that these messages can be posted before the bin is prerolled, in which
77 * case the duration query might fail.
79 * Note also that there might be a discrepancy (due to internal buffering/queueing)
80 * between the stream being currently displayed and the returned duration query.
82 * Applications might want to also query for duration (and changes) by
83 * listening to the %GST_MESSAGE_STREAM_START message, signaling the active start
86 * * %GST_MESSAGE_CLOCK_LOST: This message is posted by an element when it
87 * can no longer provide a clock.
89 * The default bin behaviour is to check if the lost clock was the one provided
90 * by the bin. If so and the bin is currently in the PLAYING state, the message
91 * is forwarded to the bin parent.
93 * This message is also generated when a clock provider is removed from
94 * the bin. If this message is received by the application, it should
95 * PAUSE the pipeline and set it back to PLAYING to force a new clock
98 * * %GST_MESSAGE_CLOCK_PROVIDE: This message is generated when an element
99 * can provide a clock. This mostly happens when a new clock
100 * provider is added to the bin.
102 * The default behaviour of the bin is to mark the currently selected clock as
103 * dirty, which will perform a clock recalculation the next time the bin is
104 * asked to provide a clock.
106 * This message is never sent to the application but is forwarded to
107 * the parent of the bin.
109 * * OTHERS: posted upwards.
111 * A #GstBin implements the following default behaviour for answering to a
114 * * %GST_QUERY_DURATION: The bin will forward the query to all sink
115 * elements contained within and will return the maximum value.
116 * If no sinks are available in the bin, the query fails.
118 * * %GST_QUERY_POSITION: The query is sent to all sink elements in the bin and the
119 * MAXIMUM of all values is returned. If no sinks are available in the bin,
122 * * OTHERS: the query is forwarded to all sink elements, the result
123 * of the first sink that answers the query successfully is returned. If no
124 * sink is in the bin, the query fails.
126 * A #GstBin will by default forward any event sent to it to all sink
127 * ( %GST_EVENT_TYPE_DOWNSTREAM ) or source ( %GST_EVENT_TYPE_UPSTREAM ) elements
128 * depending on the event type.
130 * If all the elements return %TRUE, the bin will also return %TRUE, else %FALSE
131 * is returned. If no elements of the required type are in the bin, the event
132 * handler will return %TRUE.
135 #include "gst_private.h"
137 #include "gstevent.h"
140 #include "gsterror.h"
142 #include "gstutils.h"
143 #include "gstchildproxy.h"
145 GST_DEBUG_CATEGORY_STATIC (bin_debug);
146 #define GST_CAT_DEFAULT bin_debug
148 /* a bin is toplevel if it has no parent or when it is configured to behave like
150 #define BIN_IS_TOPLEVEL(bin) ((GST_OBJECT_PARENT (bin) == NULL) || bin->priv->asynchandling)
152 struct _GstBinPrivate
154 gboolean asynchandling;
155 /* if we get an ASYNC_DONE message from ourselves, this means that the
156 * subclass will simulate ASYNC behaviour without having ASYNC children. When
157 * such an ASYNC_DONE message is posted while we are doing a state change, we
158 * have to process the message after finishing the state change even when no
159 * child returned GST_STATE_CHANGE_ASYNC. */
160 gboolean pending_async_done;
162 guint32 structure_cookie;
169 /* forward messages from our children */
170 gboolean message_forward;
173 gboolean posted_playing;
174 GstElementFlags suppressed_flags;
183 static void gst_bin_dispose (GObject * object);
185 static void gst_bin_set_property (GObject * object, guint prop_id,
186 const GValue * value, GParamSpec * pspec);
187 static void gst_bin_get_property (GObject * object, guint prop_id,
188 GValue * value, GParamSpec * pspec);
190 static GstStateChangeReturn gst_bin_change_state_func (GstElement * element,
191 GstStateChange transition);
192 static gboolean gst_bin_post_message (GstElement * element, GstMessage * msg);
193 static GstStateChangeReturn gst_bin_get_state_func (GstElement * element,
194 GstState * state, GstState * pending, GstClockTime timeout);
195 static void bin_handle_async_done (GstBin * bin, GstStateChangeReturn ret,
196 gboolean flag_pending, GstClockTime running_time);
197 static void bin_handle_async_start (GstBin * bin);
198 static void bin_push_state_continue (GstBin * bin, BinContinueData * data);
199 static void bin_do_eos (GstBin * bin);
201 static gboolean gst_bin_add_func (GstBin * bin, GstElement * element);
202 static gboolean gst_bin_remove_func (GstBin * bin, GstElement * element);
203 static void gst_bin_deep_element_added_func (GstBin * bin, GstBin * sub_bin,
204 GstElement * element);
205 static void gst_bin_deep_element_removed_func (GstBin * bin, GstBin * sub_bin,
206 GstElement * element);
207 static void gst_bin_update_context (GstBin * bin, GstContext * context);
208 static void gst_bin_update_context_unlocked (GstBin * bin,
209 GstContext * context);
212 static void gst_bin_set_index_func (GstElement * element, GstIndex * index);
213 static GstIndex *gst_bin_get_index_func (GstElement * element);
216 static GstClock *gst_bin_provide_clock_func (GstElement * element);
217 static gboolean gst_bin_set_clock_func (GstElement * element, GstClock * clock);
219 static void gst_bin_handle_message_func (GstBin * bin, GstMessage * message);
220 static gboolean gst_bin_send_event (GstElement * element, GstEvent * event);
221 static GstBusSyncReply bin_bus_handler (GstBus * bus,
222 GstMessage * message, GstBin * bin);
223 static gboolean gst_bin_query (GstElement * element, GstQuery * query);
224 static void gst_bin_set_context (GstElement * element, GstContext * context);
226 static gboolean gst_bin_do_latency_func (GstBin * bin);
228 static void bin_remove_messages (GstBin * bin, GstObject * src,
229 GstMessageType types);
230 static void gst_bin_continue_func (GstBin * bin, BinContinueData * data);
231 static gint bin_element_is_sink (GstElement * child, GstBin * bin);
232 static gint bin_element_is_src (GstElement * child, GstBin * bin);
234 static GstIterator *gst_bin_sort_iterator_new (GstBin * bin);
236 /* Bin signals and properties */
243 DEEP_ELEMENT_REMOVED,
247 #define DEFAULT_ASYNC_HANDLING FALSE
248 #define DEFAULT_MESSAGE_FORWARD FALSE
254 PROP_MESSAGE_FORWARD,
258 static void gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data);
260 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
264 static const GInterfaceInfo iface_info = { \
265 gst_bin_child_proxy_init, \
269 g_type_add_interface_static (g_define_type_id, GST_TYPE_CHILD_PROXY, &iface_info); \
271 GST_DEBUG_CATEGORY_INIT (bin_debug, "bin", GST_DEBUG_BOLD, \
272 "debugging info for the 'bin' container element"); \
276 #define gst_bin_parent_class parent_class
277 G_DEFINE_TYPE_WITH_CODE (GstBin, gst_bin, GST_TYPE_ELEMENT,
278 G_ADD_PRIVATE (GstBin)
282 gst_bin_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
288 bin = GST_BIN_CAST (child_proxy);
290 GST_OBJECT_LOCK (bin);
291 if ((res = g_list_nth_data (bin->children, index)))
292 gst_object_ref (res);
293 GST_OBJECT_UNLOCK (bin);
295 return (GObject *) res;
299 gst_bin_child_proxy_get_children_count (GstChildProxy * child_proxy)
304 bin = GST_BIN_CAST (child_proxy);
306 GST_OBJECT_LOCK (bin);
307 num = bin->numchildren;
308 GST_OBJECT_UNLOCK (bin);
314 gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data)
316 GstChildProxyInterface *iface = g_iface;
318 iface->get_children_count = gst_bin_child_proxy_get_children_count;
319 iface->get_child_by_index = gst_bin_child_proxy_get_child_by_index;
323 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
324 GValue * return_accu, const GValue * handler_return, gpointer dummy)
328 myboolean = g_value_get_boolean (handler_return);
329 g_value_set_boolean (return_accu, myboolean);
331 GST_DEBUG ("invocation %d, %d", ihint->run_type, myboolean);
338 gst_bin_class_init (GstBinClass * klass)
340 GObjectClass *gobject_class;
341 GstElementClass *gstelement_class;
343 gobject_class = (GObjectClass *) klass;
344 gstelement_class = (GstElementClass *) klass;
346 gobject_class->set_property = gst_bin_set_property;
347 gobject_class->get_property = gst_bin_get_property;
350 * GstBin:async-handling:
352 * If set to %TRUE, the bin will handle asynchronous state changes.
353 * This should be used only if the bin subclass is modifying the state
354 * of its children on its own.
356 g_object_class_install_property (gobject_class, PROP_ASYNC_HANDLING,
357 g_param_spec_boolean ("async-handling", "Async Handling",
358 "The bin will handle Asynchronous state changes",
359 DEFAULT_ASYNC_HANDLING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
362 * GstBin::element-added:
364 * @element: the #GstElement that was added to the bin
366 * Will be emitted after the element was added to the bin.
368 gst_bin_signals[ELEMENT_ADDED] =
369 g_signal_new ("element-added", G_TYPE_FROM_CLASS (klass),
370 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_added), NULL,
371 NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
373 * GstBin::element-removed:
375 * @element: the #GstElement that was removed from the bin
377 * Will be emitted after the element was removed from the bin.
379 gst_bin_signals[ELEMENT_REMOVED] =
380 g_signal_new ("element-removed", G_TYPE_FROM_CLASS (klass),
381 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_removed), NULL,
382 NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
384 * GstBin::deep-element-added:
386 * @sub_bin: the #GstBin the element was added to
387 * @element: the #GstElement that was added to @sub_bin
389 * Will be emitted after the element was added to @sub_bin.
393 gst_bin_signals[DEEP_ELEMENT_ADDED] =
394 g_signal_new ("deep-element-added", G_TYPE_FROM_CLASS (klass),
395 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, deep_element_added),
396 NULL, NULL, NULL, G_TYPE_NONE, 2, GST_TYPE_BIN, GST_TYPE_ELEMENT);
398 * GstBin::deep-element-removed:
400 * @sub_bin: the #GstBin the element was removed from
401 * @element: the #GstElement that was removed from @sub_bin
403 * Will be emitted after the element was removed from @sub_bin.
407 gst_bin_signals[DEEP_ELEMENT_REMOVED] =
408 g_signal_new ("deep-element-removed", G_TYPE_FROM_CLASS (klass),
409 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, deep_element_removed),
410 NULL, NULL, NULL, G_TYPE_NONE, 2, GST_TYPE_BIN, GST_TYPE_ELEMENT);
412 * GstBin::do-latency:
415 * Will be emitted when the bin needs to perform latency calculations. This
416 * signal is only emitted for toplevel bins or when #GstBin:async-handling is
419 * Only one signal handler is invoked. If no signals are connected, the
420 * default handler is invoked, which will query and distribute the lowest
421 * possible latency to all sinks.
423 * Connect to this signal if the default latency calculations are not
424 * sufficient, like when you need different latencies for different sinks in
427 gst_bin_signals[DO_LATENCY] =
428 g_signal_new ("do-latency", G_TYPE_FROM_CLASS (klass),
429 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstBinClass, do_latency),
430 _gst_boolean_accumulator, NULL, NULL, G_TYPE_BOOLEAN, 0, G_TYPE_NONE);
433 * GstBin:message-forward:
435 * Forward all children messages, even those that would normally be filtered by
436 * the bin. This can be interesting when one wants to be notified of the EOS
437 * state of individual elements, for example.
439 * The messages are converted to an ELEMENT message with the bin as the
440 * source. The structure of the message is named `GstBinForwarded` and contains
441 * a field named `message` that contains the original forwarded #GstMessage.
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_stream_start = TRUE, first_group_id = TRUE;
1023 gboolean same_group_id = TRUE;
1025 *have_group_id = FALSE;
1028 for (walk = bin->children; walk; walk = g_list_next (walk)) {
1029 GstElement *element;
1031 element = GST_ELEMENT_CAST (walk->data);
1032 if (bin_element_is_sink (element, bin) == 0) {
1033 /* check if element posted STREAM_START */
1035 find_message (bin, GST_OBJECT_CAST (element),
1036 GST_MESSAGE_STREAM_START))) {
1037 /* Only initialize to TRUE if we have any stream-start messages at
1038 * all, otherwise it should be FALSE. */
1039 if (first_stream_start) {
1040 /* If any stream-start message do not contain a group id then we
1041 * will set it to FALSE below */
1042 *have_group_id = TRUE;
1043 /* Similarly if any sinks did not post stream-start then we will
1044 * set it to FALSE afterwards */
1046 first_stream_start = FALSE;
1049 GST_DEBUG ("sink '%s' posted STREAM_START", GST_ELEMENT_NAME (element));
1050 *seqnum = gst_message_get_seqnum (GST_MESSAGE_CAST (msgs->data));
1051 if (gst_message_parse_group_id (GST_MESSAGE_CAST (msgs->data),
1053 if (first_group_id) {
1054 first_group_id = FALSE;
1055 *group_id = tmp_group_id;
1057 if (tmp_group_id != *group_id)
1058 same_group_id = FALSE;
1061 *have_group_id = FALSE;
1064 GST_DEBUG ("sink '%s' did not post STREAM_START yet",
1065 GST_ELEMENT_NAME (element));
1072 /* If all have a group_id we only consider this stream started
1073 * if all group ids were the same and all sinks posted a stream-start
1076 return same_group_id && result;
1077 /* otherwise consider this stream started after all sinks
1078 * have reported stream-start for backward compatibility.
1079 * FIXME 2.0: This should go away! */
1084 unlink_pads (const GValue * item, gpointer user_data)
1089 pad = g_value_get_object (item);
1091 if ((peer = gst_pad_get_peer (pad))) {
1092 if (gst_pad_get_direction (pad) == GST_PAD_SRC)
1093 gst_pad_unlink (pad, peer);
1095 gst_pad_unlink (peer, pad);
1096 gst_object_unref (peer);
1101 bin_deep_iterator_foreach (const GValue * item, gpointer user_data)
1103 GQueue *queue = user_data;
1105 g_queue_push_tail (queue, g_value_dup_object (item));
1109 gst_bin_do_deep_add_remove (GstBin * bin, gint sig_id, const gchar * sig_name,
1110 GstElement * element)
1112 g_signal_emit (bin, sig_id, 0, bin, element);
1114 /* When removing a bin, emit deep-element-* for everything in the bin too */
1115 if (GST_IS_BIN (element)) {
1117 GstIteratorResult ires;
1118 GQueue elements = G_QUEUE_INIT;
1120 GST_LOG_OBJECT (bin, "Recursing into bin %" GST_PTR_FORMAT " for %s",
1122 it = gst_bin_iterate_recurse (GST_BIN_CAST (element));
1124 ires = gst_iterator_foreach (it, bin_deep_iterator_foreach, &elements);
1125 if (ires != GST_ITERATOR_DONE) {
1126 g_queue_foreach (&elements, (GFunc) gst_object_unref, NULL);
1127 g_queue_clear (&elements);
1129 if (ires == GST_ITERATOR_RESYNC)
1130 gst_iterator_resync (it);
1131 } while (ires == GST_ITERATOR_RESYNC);
1132 if (ires != GST_ITERATOR_ERROR) {
1135 while ((e = g_queue_pop_head (&elements))) {
1136 GstObject *parent = gst_object_get_parent (GST_OBJECT_CAST (e));
1138 /* an element could have removed some of its internal elements
1139 * meanwhile, so protect against that */
1141 GST_LOG_OBJECT (bin, "calling %s for element %" GST_PTR_FORMAT
1142 " in bin %" GST_PTR_FORMAT, sig_name, e, parent);
1143 g_signal_emit (bin, sig_id, 0, parent, e);
1144 gst_object_unref (parent);
1145 gst_object_unref (e);
1149 gst_iterator_free (it);
1153 /* vmethod that adds an element to a bin
1158 gst_bin_add_func (GstBin * bin, GstElement * element)
1162 gboolean is_sink, is_source, provides_clock, requires_clock;
1163 GstMessage *clock_message = NULL, *async_message = NULL;
1164 GstStateChangeReturn ret;
1165 GList *l, *elem_contexts, *need_context_messages;
1167 GST_DEBUG_OBJECT (bin, "element :%s", GST_ELEMENT_NAME (element));
1169 /* get the element name to make sure it is unique in this bin. */
1170 GST_OBJECT_LOCK (element);
1171 elem_name = g_strdup (GST_ELEMENT_NAME (element));
1172 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1173 is_source = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE);
1175 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1177 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1178 GST_OBJECT_UNLOCK (element);
1180 GST_OBJECT_LOCK (bin);
1182 /* then check to see if the element's name is already taken in the bin,
1183 * we can safely take the lock here. This check is probably bogus because
1184 * you can safely change the element name after this check and before setting
1185 * the object parent. The window is very small though... */
1186 if (G_UNLIKELY (!gst_object_check_uniqueness (bin->children, elem_name)))
1187 goto duplicate_name;
1189 /* set the element's parent and add the element to the bin's list of children */
1190 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (element),
1191 GST_OBJECT_CAST (bin))))
1194 /* if we add a sink we become a sink */
1195 if (is_sink && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_SINK)) {
1196 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "element \"%s\" was sink",
1198 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SINK);
1200 if (is_source && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_SOURCE)) {
1201 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "element \"%s\" was source",
1203 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SOURCE);
1206 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_PROVIDE_CLOCK)) {
1207 GST_DEBUG_OBJECT (bin, "element \"%s\" can provide a clock", elem_name);
1209 gst_message_new_clock_provide (GST_OBJECT_CAST (element), NULL, TRUE);
1210 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1213 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_REQUIRE_CLOCK)) {
1214 GST_DEBUG_OBJECT (bin, "element \"%s\" requires a clock", elem_name);
1215 GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1218 bin->children = g_list_prepend (bin->children, element);
1220 bin->children_cookie++;
1221 if (!GST_BIN_IS_NO_RESYNC (bin))
1222 bin->priv->structure_cookie++;
1224 /* distribute the bus */
1225 gst_element_set_bus (element, bin->child_bus);
1227 /* propagate the current base_time, start_time and clock */
1228 gst_element_set_base_time (element, GST_ELEMENT_CAST (bin)->base_time);
1229 gst_element_set_start_time (element, GST_ELEMENT_START_TIME (bin));
1230 /* it's possible that the element did not accept the clock but
1231 * that is not important right now. When the pipeline goes to PLAYING,
1232 * a new clock will be selected */
1233 gst_element_set_clock (element, GST_ELEMENT_CLOCK (bin));
1235 /* get the element's list of contexts before propagating our own */
1236 elem_contexts = gst_element_get_contexts (element);
1237 for (l = GST_ELEMENT_CAST (bin)->contexts; l; l = l->next)
1238 gst_element_set_context (element, l->data);
1240 need_context_messages = NULL;
1241 for (l = elem_contexts; l; l = l->next) {
1242 GstContext *replacement, *context = l->data;
1243 const gchar *context_type;
1245 context_type = gst_context_get_context_type (context);
1247 /* we already set this context above? */
1249 gst_element_get_context_unlocked (GST_ELEMENT (bin), context_type);
1251 gst_context_unref (replacement);
1256 /* ask our parent for the context */
1257 msg = gst_message_new_need_context (GST_OBJECT_CAST (bin), context_type);
1258 s = (GstStructure *) gst_message_get_structure (msg);
1259 gst_structure_set (s, "bin.old.context", GST_TYPE_CONTEXT, context, NULL);
1261 need_context_messages = g_list_prepend (need_context_messages, msg);
1266 /* set the cached index on the children */
1267 if (bin->priv->index)
1268 gst_element_set_index (element, bin->priv->index);
1271 ret = GST_STATE_RETURN (bin);
1272 /* no need to update the state if we are in error */
1273 if (ret == GST_STATE_CHANGE_FAILURE)
1274 goto no_state_recalc;
1276 /* update the bin state, the new element could have been an ASYNC or
1277 * NO_PREROLL element */
1278 ret = GST_STATE_RETURN (element);
1279 GST_DEBUG_OBJECT (bin, "added %s element",
1280 gst_element_state_change_return_get_name (ret));
1283 case GST_STATE_CHANGE_ASYNC:
1285 /* create message to track this aync element when it posts an async-done
1287 async_message = gst_message_new_async_start (GST_OBJECT_CAST (element));
1290 case GST_STATE_CHANGE_NO_PREROLL:
1291 /* ignore all async elements we might have and commit our state */
1292 bin_handle_async_done (bin, ret, FALSE, GST_CLOCK_TIME_NONE);
1294 case GST_STATE_CHANGE_FAILURE:
1301 GST_OBJECT_UNLOCK (bin);
1303 for (l = need_context_messages; l; l = l->next) {
1304 GstMessage *msg = l->data;
1306 const gchar *context_type;
1307 GstContext *replacement, *context;
1309 gst_message_parse_context_type (msg, &context_type);
1311 GST_LOG_OBJECT (bin, "asking parent for context type: %s "
1312 "from %" GST_PTR_FORMAT, context_type, element);
1314 s = (GstStructure *) gst_message_get_structure (msg);
1315 gst_structure_get (s, "bin.old.context", GST_TYPE_CONTEXT, &context, NULL);
1316 gst_structure_remove_field (s, "bin.old.context");
1317 /* Keep the msg around while we still need access to the context_type */
1318 gst_element_post_message (GST_ELEMENT_CAST (bin), gst_message_ref (msg));
1320 /* lock to avoid losing a potential write */
1321 GST_OBJECT_LOCK (bin);
1323 gst_element_get_context_unlocked (GST_ELEMENT_CAST (bin), context_type);
1324 gst_message_unref (msg);
1327 /* we got the context set from GstElement::set_context */
1328 gst_context_unref (replacement);
1329 GST_OBJECT_UNLOCK (bin);
1331 /* Propagate the element's context upwards */
1332 GST_LOG_OBJECT (bin, "propagating existing context type: %s %p "
1333 "from %" GST_PTR_FORMAT, context_type, context, element);
1335 gst_bin_update_context_unlocked (bin, context);
1338 gst_message_new_have_context (GST_OBJECT_CAST (bin),
1339 gst_context_ref (context));
1340 GST_OBJECT_UNLOCK (bin);
1341 gst_element_post_message (GST_ELEMENT_CAST (bin), msg);
1343 gst_context_unref (context);
1345 g_list_free_full (elem_contexts, (GDestroyNotify) gst_context_unref);
1346 g_list_free (need_context_messages);
1348 /* post the messages on the bus of the element so that the bin can handle
1351 gst_element_post_message (element, clock_message);
1354 gst_element_post_message (element, async_message);
1356 /* unlink all linked pads */
1357 it = gst_element_iterate_pads (element);
1358 while (gst_iterator_foreach (it, (GstIteratorForeachFunction) unlink_pads,
1359 NULL) == GST_ITERATOR_RESYNC)
1360 gst_iterator_resync (it);
1361 gst_iterator_free (it);
1363 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "added element \"%s\"",
1366 g_signal_emit (bin, gst_bin_signals[ELEMENT_ADDED], 0, element);
1367 gst_child_proxy_child_added ((GstChildProxy *) bin, (GObject *) element,
1370 gst_bin_do_deep_add_remove (bin, gst_bin_signals[DEEP_ELEMENT_ADDED],
1371 "deep-element-added", element);
1377 /* ERROR handling here */
1380 GST_OBJECT_UNLOCK (bin);
1381 GST_WARNING_OBJECT (bin, "Name '%s' is not unique in bin, not adding",
1384 gst_object_ref_sink (element);
1385 gst_object_unref (element);
1390 GST_OBJECT_UNLOCK (bin);
1391 GST_WARNING_OBJECT (bin, "Element '%s' already has parent", elem_name);
1398 * gst_bin_set_suppressed_flags:
1400 * @flags: the #GstElementFlags to suppress
1402 * Suppresses the given flags on the bin. #GstElementFlags of a
1403 * child element are propagated when it is added to the bin.
1404 * When suppressed flags are set, those specified flags will
1405 * not be propagated to the bin.
1410 gst_bin_set_suppressed_flags (GstBin * bin, GstElementFlags flags)
1412 g_return_if_fail (GST_IS_BIN (bin));
1414 GST_OBJECT_LOCK (bin);
1415 bin->priv->suppressed_flags = bin->priv->suppressed_flags | flags;
1416 GST_OBJECT_UNLOCK (bin);
1418 GST_DEBUG_OBJECT (bin, "Set suppressed flags(0x%x) to bin '%s'", flags,
1419 GST_ELEMENT_NAME (bin));
1423 * gst_bin_get_suppressed_flags:
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.
1507 * Returns: %TRUE if the element could be added, %FALSE if
1508 * the bin does not want to accept the element.
1511 gst_bin_add (GstBin * bin, GstElement * element)
1513 GstBinClass *bclass;
1516 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1517 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1518 g_return_val_if_fail (GST_ELEMENT_CAST (bin) != element, FALSE);
1520 bclass = GST_BIN_GET_CLASS (bin);
1522 if (G_UNLIKELY (bclass->add_element == NULL))
1525 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "adding element %s to bin %s",
1526 GST_STR_NULL (GST_ELEMENT_NAME (element)),
1527 GST_STR_NULL (GST_ELEMENT_NAME (bin)));
1529 GST_TRACER_BIN_ADD_PRE (bin, element);
1530 result = bclass->add_element (bin, element);
1531 GST_TRACER_BIN_ADD_POST (bin, element, result);
1535 /* ERROR handling */
1538 GST_WARNING_OBJECT (bin, "adding elements to bin is not supported");
1539 gst_object_ref_sink (element);
1540 gst_object_unref (element);
1545 /* remove an element from the bin
1550 gst_bin_remove_func (GstBin * bin, GstElement * element)
1554 gboolean is_sink, is_source, provides_clock, requires_clock;
1555 gboolean othersink, othersource, otherprovider, otherrequirer, found;
1556 GstMessage *clock_message = NULL;
1557 GstClock **provided_clock_p;
1558 GstElement **clock_provider_p;
1560 gboolean other_async, this_async, have_no_preroll, removed_eos;
1561 GstStateChangeReturn ret;
1563 GST_DEBUG_OBJECT (bin, "element :%s", GST_ELEMENT_NAME (element));
1565 GST_OBJECT_LOCK (bin);
1567 GST_OBJECT_LOCK (element);
1568 elem_name = g_strdup (GST_ELEMENT_NAME (element));
1570 if (GST_OBJECT_PARENT (element) != GST_OBJECT_CAST (bin))
1573 /* remove the parent ref */
1574 GST_OBJECT_PARENT (element) = NULL;
1576 /* grab element name so we can print it */
1577 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1578 is_source = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE);
1580 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1582 GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1583 GST_OBJECT_UNLOCK (element);
1587 othersource = FALSE;
1588 otherprovider = FALSE;
1589 otherrequirer = FALSE;
1590 have_no_preroll = FALSE;
1591 /* iterate the elements, we collect which ones are async and no_preroll. We
1592 * also remove the element when we find it. */
1593 for (walk = bin->children; walk; walk = next) {
1594 GstElement *child = GST_ELEMENT_CAST (walk->data);
1596 next = g_list_next (walk);
1598 if (child == element) {
1600 /* remove the element */
1601 bin->children = g_list_delete_link (bin->children, walk);
1603 gboolean child_sink, child_source, child_provider, child_requirer;
1605 GST_OBJECT_LOCK (child);
1606 child_sink = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SINK);
1607 child_source = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SOURCE);
1609 GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1611 GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1612 /* when we remove a sink, check if there are other sinks. */
1613 if (is_sink && !othersink && child_sink)
1615 if (is_source && !othersource && child_source)
1617 if (provides_clock && !otherprovider && child_provider)
1618 otherprovider = TRUE;
1619 if (requires_clock && !otherrequirer && child_requirer)
1620 otherrequirer = TRUE;
1621 /* check if we have NO_PREROLL children */
1622 if (GST_STATE_RETURN (child) == GST_STATE_CHANGE_NO_PREROLL)
1623 have_no_preroll = TRUE;
1624 GST_OBJECT_UNLOCK (child);
1628 /* the element must have been in the bin's list of children */
1629 if (G_UNLIKELY (!found))
1632 /* we now removed the element from the list of elements, increment the cookie
1633 * so that others can detect a change in the children list. */
1635 bin->children_cookie++;
1636 if (!GST_BIN_IS_NO_RESYNC (bin))
1637 bin->priv->structure_cookie++;
1639 if (is_sink && !othersink
1640 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_SINK)) {
1641 /* we're not a sink anymore */
1642 GST_DEBUG_OBJECT (bin, "we removed the last sink");
1643 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_SINK);
1645 if (is_source && !othersource
1646 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_SOURCE)) {
1647 /* we're not a source anymore */
1648 GST_DEBUG_OBJECT (bin, "we removed the last source");
1649 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_SOURCE);
1651 if (provides_clock && !otherprovider
1652 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_PROVIDE_CLOCK)) {
1653 /* we're not a clock provider anymore */
1654 GST_DEBUG_OBJECT (bin, "we removed the last clock provider");
1655 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1657 if (requires_clock && !otherrequirer
1658 && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_REQUIRE_CLOCK)) {
1659 /* we're not a clock requirer anymore */
1660 GST_DEBUG_OBJECT (bin, "we removed the last clock requirer");
1661 GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1664 /* if the clock provider for this element is removed, we lost
1665 * the clock as well, we need to inform the parent of this
1666 * so that it can select a new clock */
1667 if (bin->clock_provider == element) {
1668 GST_DEBUG_OBJECT (bin, "element \"%s\" provided the clock", elem_name);
1669 bin->clock_dirty = TRUE;
1671 gst_message_new_clock_lost (GST_OBJECT_CAST (bin), bin->provided_clock);
1672 provided_clock_p = &bin->provided_clock;
1673 clock_provider_p = &bin->clock_provider;
1674 gst_object_replace ((GstObject **) provided_clock_p, NULL);
1675 gst_object_replace ((GstObject **) clock_provider_p, NULL);
1678 /* remove messages for the element, if there was a pending ASYNC_START
1679 * message we must see if removing the element caused the bin to lose its
1682 other_async = FALSE;
1683 /* If we remove an EOSed element, the bin might go EOS */
1684 removed_eos = FALSE;
1685 for (walk = bin->messages; walk; walk = next) {
1686 GstMessage *message = (GstMessage *) walk->data;
1687 GstElement *src = GST_ELEMENT_CAST (GST_MESSAGE_SRC (message));
1690 next = g_list_next (walk);
1693 switch (GST_MESSAGE_TYPE (message)) {
1694 case GST_MESSAGE_ASYNC_START:
1700 GST_DEBUG_OBJECT (src, "looking at message %p", message);
1702 case GST_MESSAGE_STRUCTURE_CHANGE:
1706 GST_DEBUG_OBJECT (src, "looking at structure change message %p",
1708 /* it's unlikely that this message is still in the list of messages
1709 * because this would mean that a link/unlink is busy in another thread
1710 * while we remove the element. We still have to remove the message
1711 * because we might not receive the done message anymore when the element
1712 * is removed from the bin. */
1713 gst_message_parse_structure_change (message, NULL, &owner, NULL);
1714 if (owner == element)
1718 case GST_MESSAGE_EOS:
1729 /* delete all message types */
1730 GST_DEBUG_OBJECT (src, "deleting message %p of element \"%s\"",
1731 message, elem_name);
1732 bin->messages = g_list_delete_link (bin->messages, walk);
1733 gst_message_unref (message);
1737 /* get last return */
1738 ret = GST_STATE_RETURN (bin);
1740 /* no need to update the state if we are in error */
1741 if (ret == GST_STATE_CHANGE_FAILURE)
1742 goto no_state_recalc;
1744 if (!other_async && this_async) {
1745 /* all other elements were not async and we removed the async one,
1746 * handle the async-done case because we are not async anymore now. */
1747 GST_DEBUG_OBJECT (bin,
1748 "we removed the last async element, have no_preroll %d",
1751 /* the current state return of the bin depends on if there are no_preroll
1752 * elements in the pipeline or not */
1753 if (have_no_preroll)
1754 ret = GST_STATE_CHANGE_NO_PREROLL;
1756 ret = GST_STATE_CHANGE_SUCCESS;
1758 bin_handle_async_done (bin, ret, FALSE, GST_CLOCK_TIME_NONE);
1760 GST_DEBUG_OBJECT (bin,
1761 "recalc state preroll: %d, other async: %d, this async %d",
1762 have_no_preroll, other_async, this_async);
1764 if (have_no_preroll) {
1765 ret = GST_STATE_CHANGE_NO_PREROLL;
1766 } else if (other_async) {
1767 /* there are other async elements and we were not doing an async state
1768 * change, change our pending state and go async */
1769 if (GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING) {
1770 GST_STATE_NEXT (bin) = GST_STATE (bin);
1771 GST_STATE_PENDING (bin) = GST_STATE (bin);
1773 ret = GST_STATE_CHANGE_ASYNC;
1775 GST_STATE_RETURN (bin) = ret;
1779 gst_element_set_bus (element, NULL);
1780 /* Clear the clock we provided to the element */
1781 gst_element_set_clock (element, NULL);
1782 GST_OBJECT_UNLOCK (bin);
1784 /* If the element was a sink that had not posted EOS,
1785 * it might have been the last one we were waiting for,
1786 * so check if it's time to send EOS now */
1787 if (is_sink && !removed_eos) {
1788 GST_DEBUG_OBJECT (bin,
1789 "Removing sink that had not EOSed. Re-checking overall EOS status");
1794 gst_element_post_message (GST_ELEMENT_CAST (bin), clock_message);
1796 /* unlink all linked pads */
1797 it = gst_element_iterate_pads (element);
1798 while (gst_iterator_foreach (it, (GstIteratorForeachFunction) unlink_pads,
1799 NULL) == GST_ITERATOR_RESYNC)
1800 gst_iterator_resync (it);
1801 gst_iterator_free (it);
1803 GST_CAT_INFO_OBJECT (GST_CAT_PARENTAGE, bin, "removed child \"%s\"",
1806 g_signal_emit (bin, gst_bin_signals[ELEMENT_REMOVED], 0, element);
1807 gst_child_proxy_child_removed ((GstChildProxy *) bin, (GObject *) element,
1810 gst_bin_do_deep_add_remove (bin, gst_bin_signals[DEEP_ELEMENT_REMOVED],
1811 "deep-element-removed", element);
1814 /* element is really out of our control now */
1815 gst_object_unref (element);
1819 /* ERROR handling */
1822 GST_OBJECT_UNLOCK (element);
1823 GST_OBJECT_UNLOCK (bin);
1824 GST_WARNING_OBJECT (bin, "Element '%s' is not in bin", elem_name);
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.
1845 * Returns: %TRUE if the element could be removed, %FALSE if
1846 * the bin does not want to remove the element.
1849 gst_bin_remove (GstBin * bin, GstElement * element)
1851 GstBinClass *bclass;
1854 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1855 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1856 g_return_val_if_fail (GST_ELEMENT_CAST (bin) != element, FALSE);
1858 bclass = GST_BIN_GET_CLASS (bin);
1860 if (G_UNLIKELY (bclass->remove_element == NULL))
1863 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "removing element %s from bin %s",
1864 GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin));
1866 GST_TRACER_BIN_REMOVE_PRE (bin, element);
1867 result = bclass->remove_element (bin, element);
1868 GST_TRACER_BIN_REMOVE_POST (bin, result);
1872 /* ERROR handling */
1875 GST_WARNING_OBJECT (bin, "Removing elements from bin is not supported");
1881 * gst_bin_iterate_elements:
1884 * Gets an iterator for the elements in this bin.
1886 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
1889 gst_bin_iterate_elements (GstBin * bin)
1891 GstIterator *result;
1893 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1895 GST_OBJECT_LOCK (bin);
1896 result = gst_iterator_new_list (GST_TYPE_ELEMENT,
1897 GST_OBJECT_GET_LOCK (bin),
1898 &bin->children_cookie, &bin->children, (GObject *) bin, NULL);
1899 GST_OBJECT_UNLOCK (bin);
1904 static GstIteratorItem
1905 iterate_child_recurse (GstIterator * it, const GValue * item)
1907 GstElement *child = g_value_get_object (item);
1909 if (GST_IS_BIN (child)) {
1910 GstIterator *other = gst_bin_iterate_recurse (GST_BIN_CAST (child));
1912 gst_iterator_push (it, other);
1914 return GST_ITERATOR_ITEM_PASS;
1918 * gst_bin_iterate_recurse:
1921 * Gets an iterator for the elements in this bin.
1922 * This iterator recurses into GstBin children.
1924 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
1927 gst_bin_iterate_recurse (GstBin * bin)
1929 GstIterator *result;
1931 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1933 GST_OBJECT_LOCK (bin);
1934 result = gst_iterator_new_list (GST_TYPE_ELEMENT,
1935 GST_OBJECT_GET_LOCK (bin),
1936 &bin->children_cookie,
1938 (GObject *) bin, (GstIteratorItemFunction) iterate_child_recurse);
1939 GST_OBJECT_UNLOCK (bin);
1944 /* returns 0 when TRUE because this is a GCompareFunc */
1947 bin_element_is_sink (GstElement * child, GstBin * bin)
1951 /* we lock the child here for the remainder of the function to
1952 * get its name and flag safely. */
1953 GST_OBJECT_LOCK (child);
1954 is_sink = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SINK);
1956 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
1957 "child %s %s sink", GST_OBJECT_NAME (child), is_sink ? "is" : "is not");
1959 GST_OBJECT_UNLOCK (child);
1960 return is_sink ? 0 : 1;
1964 sink_iterator_filter (const GValue * vchild, GValue * vbin)
1966 GstBin *bin = g_value_get_object (vbin);
1967 GstElement *child = g_value_get_object (vchild);
1969 return (bin_element_is_sink (child, bin));
1973 * gst_bin_iterate_sinks:
1976 * Gets an iterator for all elements in the bin that have the
1977 * #GST_ELEMENT_FLAG_SINK flag set.
1979 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
1982 gst_bin_iterate_sinks (GstBin * bin)
1984 GstIterator *children;
1985 GstIterator *result;
1986 GValue vbin = { 0, };
1988 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1990 g_value_init (&vbin, GST_TYPE_BIN);
1991 g_value_set_object (&vbin, bin);
1993 children = gst_bin_iterate_elements (bin);
1994 result = gst_iterator_filter (children,
1995 (GCompareFunc) sink_iterator_filter, &vbin);
1997 g_value_unset (&vbin);
2002 /* returns 0 when TRUE because this is a GCompareFunc */
2005 bin_element_is_src (GstElement * child, GstBin * bin)
2009 /* we lock the child here for the remainder of the function to
2010 * get its name and other info safely. */
2011 GST_OBJECT_LOCK (child);
2012 is_src = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SOURCE);
2014 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
2015 "child %s %s src", GST_OBJECT_NAME (child), is_src ? "is" : "is not");
2017 GST_OBJECT_UNLOCK (child);
2018 return is_src ? 0 : 1;
2022 src_iterator_filter (const GValue * vchild, GValue * vbin)
2024 GstBin *bin = g_value_get_object (vbin);
2025 GstElement *child = g_value_get_object (vchild);
2027 return (bin_element_is_src (child, bin));
2031 * gst_bin_iterate_sources:
2034 * Gets an iterator for all elements in the bin that have the
2035 * #GST_ELEMENT_FLAG_SOURCE flag set.
2037 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
2040 gst_bin_iterate_sources (GstBin * bin)
2042 GstIterator *children;
2043 GstIterator *result;
2044 GValue vbin = { 0, };
2046 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
2048 g_value_init (&vbin, GST_TYPE_BIN);
2049 g_value_set_object (&vbin, bin);
2051 children = gst_bin_iterate_elements (bin);
2052 result = gst_iterator_filter (children,
2053 (GCompareFunc) src_iterator_filter, &vbin);
2055 g_value_unset (&vbin);
2063 static GstStateChangeReturn
2064 gst_bin_get_state_func (GstElement * element, GstState * state,
2065 GstState * pending, GstClockTime timeout)
2067 GstStateChangeReturn ret;
2069 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
2072 GST_ELEMENT_CLASS (parent_class)->get_state (element, state, pending,
2078 /***********************************************
2079 * Topologically sorted iterator
2080 * see http://en.wikipedia.org/wiki/Topological_sorting
2082 * For each element in the graph, an entry is kept in a HashTable
2083 * with its number of srcpad connections (degree).
2084 * We then change state of all elements without dependencies
2085 * (degree 0) and decrement the degree of all elements connected
2086 * on the sinkpads. When an element reaches degree 0, its state is
2088 * When all elements are handled the algorithm stops.
2090 typedef struct _GstBinSortIterator
2093 GQueue queue; /* elements queued for state change */
2094 GstBin *bin; /* bin we iterate */
2095 gint mode; /* adding or removing dependency */
2096 GstElement *best; /* next element with least dependencies */
2097 gint best_deg; /* best degree */
2098 GHashTable *hash; /* hashtable with element dependencies */
2099 gboolean dirty; /* we detected structure change */
2100 } GstBinSortIterator;
2103 copy_to_queue (gpointer data, gpointer user_data)
2105 GstElement *element = data;
2106 GQueue *queue = user_data;
2108 gst_object_ref (element);
2109 g_queue_push_tail (queue, element);
2113 gst_bin_sort_iterator_copy (const GstBinSortIterator * it,
2114 GstBinSortIterator * copy)
2116 GHashTableIter iter;
2117 gpointer key, value;
2119 g_queue_init (©->queue);
2120 g_queue_foreach ((GQueue *) & it->queue, copy_to_queue, ©->queue);
2122 copy->bin = gst_object_ref (it->bin);
2124 copy->best = gst_object_ref (it->best);
2126 copy->hash = g_hash_table_new (NULL, NULL);
2127 g_hash_table_iter_init (&iter, it->hash);
2128 while (g_hash_table_iter_next (&iter, &key, &value))
2129 g_hash_table_insert (copy->hash, key, value);
2132 /* we add and subtract 1 to make sure we don't confuse NULL and 0 */
2133 #define HASH_SET_DEGREE(bit, elem, deg) \
2134 g_hash_table_replace (bit->hash, elem, GINT_TO_POINTER(deg+1))
2135 #define HASH_GET_DEGREE(bit, elem) \
2136 (GPOINTER_TO_INT(g_hash_table_lookup (bit->hash, elem))-1)
2138 /* add element to queue of next elements in the iterator.
2139 * We push at the tail to give higher priority elements a
2142 add_to_queue (GstBinSortIterator * bit, GstElement * element)
2144 GST_DEBUG_OBJECT (bit->bin, "adding '%s' to queue",
2145 GST_ELEMENT_NAME (element));
2146 gst_object_ref (element);
2147 g_queue_push_tail (&bit->queue, element);
2148 HASH_SET_DEGREE (bit, element, -1);
2152 remove_from_queue (GstBinSortIterator * bit, GstElement * element)
2156 if ((find = g_queue_find (&bit->queue, element))) {
2157 GST_DEBUG_OBJECT (bit->bin, "removing '%s' from queue",
2158 GST_ELEMENT_NAME (element));
2160 g_queue_delete_link (&bit->queue, find);
2161 gst_object_unref (element);
2163 GST_DEBUG_OBJECT (bit->bin, "unable to remove '%s' from queue",
2164 GST_ELEMENT_NAME (element));
2168 /* clear the queue, unref all objects as we took a ref when
2169 * we added them to the queue */
2171 clear_queue (GQueue * queue)
2175 while ((p = g_queue_pop_head (queue)))
2176 gst_object_unref (p);
2179 /* set all degrees to 0. Elements marked as a sink are
2180 * added to the queue immediately. Since we only look at the SINK flag of the
2181 * element, it is possible that we add non-sinks to the queue. These will be
2182 * removed from the queue again when we can prove that it provides data for some
2185 reset_degree (GstElement * element, GstBinSortIterator * bit)
2189 /* sinks are added right away */
2190 GST_OBJECT_LOCK (element);
2191 is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
2192 GST_OBJECT_UNLOCK (element);
2195 add_to_queue (bit, element);
2197 /* others are marked with 0 and handled when sinks are done */
2198 HASH_SET_DEGREE (bit, element, 0);
2202 /* adjust the degree of all elements connected to the given
2203 * element. If a degree of an element drops to 0, it is
2204 * added to the queue of elements to schedule next.
2206 * We have to make sure not to cross the bin boundary this element
2210 update_degree (GstElement * element, GstBinSortIterator * bit)
2212 gboolean linked = FALSE;
2214 GST_OBJECT_LOCK (element);
2215 /* don't touch degree if element has no sinkpads */
2216 if (element->numsinkpads != 0) {
2217 /* loop over all sinkpads, decrement degree for all connected
2218 * elements in this bin */
2221 for (pads = element->sinkpads; pads; pads = g_list_next (pads)) {
2224 pad = GST_PAD_CAST (pads->data);
2226 /* we're iterating over the sinkpads, check if it's busy in a link/unlink */
2227 if (G_UNLIKELY (find_message (bit->bin, GST_OBJECT_CAST (pad),
2228 GST_MESSAGE_STRUCTURE_CHANGE))) {
2229 /* mark the iterator as dirty because we won't be updating the degree
2230 * of the peer parent now. This would result in the 'loop detected'
2231 * later on because the peer parent element could become the best next
2232 * element with a degree > 0. We will simply continue our state
2233 * changes and we'll eventually resync when the unlink completed and
2234 * the iterator cookie is updated. */
2239 if ((peer = gst_pad_get_peer (pad))) {
2240 GstElement *peer_element;
2242 if ((peer_element = gst_pad_get_parent_element (peer))) {
2243 GST_OBJECT_LOCK (peer_element);
2244 /* check that we don't go outside of this bin */
2245 if (GST_OBJECT_CAST (peer_element)->parent ==
2246 GST_OBJECT_CAST (bit->bin)) {
2247 gint old_deg, new_deg;
2249 old_deg = HASH_GET_DEGREE (bit, peer_element);
2251 /* check to see if we added an element as sink that was not really a
2252 * sink because it was connected to some other element. */
2253 if (old_deg == -1) {
2254 remove_from_queue (bit, peer_element);
2257 new_deg = old_deg + bit->mode;
2259 GST_DEBUG_OBJECT (bit->bin,
2260 "change element %s, degree %d->%d, linked to %s",
2261 GST_ELEMENT_NAME (peer_element), old_deg, new_deg,
2262 GST_ELEMENT_NAME (element));
2264 /* update degree, it is possible that an element was in 0 and
2265 * reaches -1 here. This would mean that the element had no sinkpads
2266 * but became linked while the state change was happening. We will
2267 * resync on this with the structure change message. */
2269 /* degree hit 0, add to queue */
2270 add_to_queue (bit, peer_element);
2272 HASH_SET_DEGREE (bit, peer_element, new_deg);
2276 GST_OBJECT_UNLOCK (peer_element);
2277 gst_object_unref (peer_element);
2279 gst_object_unref (peer);
2284 GST_DEBUG_OBJECT (bit->bin, "element %s not linked on any sinkpads",
2285 GST_ELEMENT_NAME (element));
2287 GST_OBJECT_UNLOCK (element);
2290 /* find the next best element not handled yet. This is the one
2291 * with the lowest non-negative degree */
2293 find_element (GstElement * element, GstBinSortIterator * bit)
2297 /* element is already handled */
2298 if ((degree = HASH_GET_DEGREE (bit, element)) < 0)
2301 /* first element or element with smaller degree */
2302 if (bit->best == NULL || bit->best_deg > degree) {
2303 bit->best = element;
2304 bit->best_deg = degree;
2305 } else if (bit->best_deg == degree
2306 && GST_OBJECT_FLAG_IS_SET (bit->best, GST_ELEMENT_FLAG_SOURCE)
2307 && !GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE)) {
2308 /* If two elements have the same degree, we want to ensure we
2309 * return non-source elements first. */
2310 bit->best = element;
2314 /* get next element in iterator. */
2315 static GstIteratorResult
2316 gst_bin_sort_iterator_next (GstBinSortIterator * bit, GValue * result)
2319 GstBin *bin = bit->bin;
2321 /* empty queue, we have to find a next best element */
2322 if (g_queue_is_empty (&bit->queue)) {
2324 bit->best_deg = G_MAXINT;
2325 g_list_foreach (bin->children, (GFunc) find_element, bit);
2326 if ((best = bit->best)) {
2327 /* when we detected an unlink, don't warn because our degrees might be
2328 * screwed up. We will resync later */
2329 if (bit->best_deg != 0 && !bit->dirty) {
2330 /* we don't fail on this one yet */
2331 GST_WARNING_OBJECT (bin, "loop dected in graph");
2332 g_warning ("loop detected in the graph of bin '%s'!!",
2333 GST_ELEMENT_NAME (bin));
2335 /* best unhandled element, schedule as next element */
2336 GST_DEBUG_OBJECT (bin, "queue empty, next best: %s",
2337 GST_ELEMENT_NAME (best));
2338 HASH_SET_DEGREE (bit, best, -1);
2339 g_value_set_object (result, best);
2341 GST_DEBUG_OBJECT (bin, "queue empty, elements exhausted");
2342 /* no more unhandled elements, we are done */
2343 return GST_ITERATOR_DONE;
2346 /* everything added to the queue got reffed */
2347 best = g_queue_pop_head (&bit->queue);
2348 g_value_set_object (result, best);
2349 gst_object_unref (best);
2352 GST_DEBUG_OBJECT (bin, "queue head gives %s", GST_ELEMENT_NAME (best));
2353 /* update degrees of linked elements */
2354 update_degree (best, bit);
2356 return GST_ITERATOR_OK;
2359 /* clear queues, recalculate the degrees and restart. */
2361 gst_bin_sort_iterator_resync (GstBinSortIterator * bit)
2363 GstBin *bin = bit->bin;
2365 GST_DEBUG_OBJECT (bin, "resync");
2367 clear_queue (&bit->queue);
2369 g_list_foreach (bin->children, (GFunc) reset_degree, bit);
2370 /* calc degrees, incrementing */
2372 g_list_foreach (bin->children, (GFunc) update_degree, bit);
2373 /* for the rest of the function we decrement the degrees */
2377 /* clear queues, unref bin and free iterator. */
2379 gst_bin_sort_iterator_free (GstBinSortIterator * bit)
2381 GstBin *bin = bit->bin;
2383 GST_DEBUG_OBJECT (bin, "free");
2384 clear_queue (&bit->queue);
2385 g_hash_table_destroy (bit->hash);
2386 gst_object_unref (bin);
2389 /* should be called with the bin LOCK held */
2390 static GstIterator *
2391 gst_bin_sort_iterator_new (GstBin * bin)
2393 GstBinSortIterator *result;
2395 /* we don't need an ItemFunction because we ref the items in the _next
2397 result = (GstBinSortIterator *)
2398 gst_iterator_new (sizeof (GstBinSortIterator),
2400 GST_OBJECT_GET_LOCK (bin),
2401 &bin->priv->structure_cookie,
2402 (GstIteratorCopyFunction) gst_bin_sort_iterator_copy,
2403 (GstIteratorNextFunction) gst_bin_sort_iterator_next,
2404 (GstIteratorItemFunction) NULL,
2405 (GstIteratorResyncFunction) gst_bin_sort_iterator_resync,
2406 (GstIteratorFreeFunction) gst_bin_sort_iterator_free);
2407 g_queue_init (&result->queue);
2408 result->hash = g_hash_table_new (NULL, NULL);
2409 gst_object_ref (bin);
2411 gst_bin_sort_iterator_resync (result);
2413 return (GstIterator *) result;
2417 * gst_bin_iterate_sorted:
2420 * Gets an iterator for the elements in this bin in topologically
2421 * sorted order. This means that the elements are returned from
2422 * the most downstream elements (sinks) to the sources.
2424 * This function is used internally to perform the state changes
2425 * of the bin elements and for clock selection.
2427 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
2430 gst_bin_iterate_sorted (GstBin * bin)
2432 GstIterator *result;
2434 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
2436 GST_OBJECT_LOCK (bin);
2437 result = gst_bin_sort_iterator_new (bin);
2438 GST_OBJECT_UNLOCK (bin);
2443 static GstStateChangeReturn
2444 gst_bin_element_set_state (GstBin * bin, GstElement * element,
2445 GstClockTime base_time, GstClockTime start_time, GstState current,
2448 GstStateChangeReturn ret;
2449 GstState child_current, child_pending;
2453 GST_STATE_LOCK (element);
2455 GST_OBJECT_LOCK (element);
2456 /* set base_time and start time on child */
2457 GST_ELEMENT_START_TIME (element) = start_time;
2458 element->base_time = base_time;
2459 /* peel off the locked flag */
2460 locked = GST_ELEMENT_IS_LOCKED_STATE (element);
2461 /* Get the previous set_state result to preserve NO_PREROLL and ASYNC */
2462 ret = GST_STATE_RETURN (element);
2463 child_current = GST_STATE (element);
2464 child_pending = GST_STATE_PENDING (element);
2465 GST_OBJECT_UNLOCK (element);
2467 /* skip locked elements */
2468 if (G_UNLIKELY (locked))
2471 /* if the element was no preroll, just start changing the state regardless
2472 * if it had async elements (in the case of a bin) because they won't preroll
2474 if (G_UNLIKELY (ret == GST_STATE_CHANGE_NO_PREROLL)) {
2475 GST_DEBUG_OBJECT (element, "element is NO_PREROLL, ignore async elements");
2479 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2480 "current %s pending %s, desired next %s",
2481 gst_element_state_get_name (child_current),
2482 gst_element_state_get_name (child_pending),
2483 gst_element_state_get_name (next));
2485 /* always recurse into bins so that we can set the base time */
2486 if (GST_IS_BIN (element))
2489 /* Try not to change the state of elements that are already in the state we're
2491 if (child_current == next && child_pending == GST_STATE_VOID_PENDING) {
2492 /* child is already at the requested state, return previous return. Note that
2493 * if the child has a pending state to next, we will still call the
2494 * set_state function */
2496 } else if (next > current) {
2497 /* upward state change */
2498 if (child_pending == GST_STATE_VOID_PENDING) {
2499 /* .. and the child is not busy doing anything */
2500 if (child_current > next) {
2501 /* .. and is already past the requested state, assume it got there
2503 ret = GST_STATE_CHANGE_SUCCESS;
2506 } else if (child_pending > child_current) {
2507 /* .. and the child is busy going upwards */
2508 if (child_current >= next) {
2509 /* .. and is already past the requested state, assume it got there
2511 ret = GST_STATE_CHANGE_SUCCESS;
2515 /* .. and the child is busy going downwards */
2516 if (child_current > next) {
2517 /* .. and is already past the requested state, assume it got there
2519 ret = GST_STATE_CHANGE_SUCCESS;
2523 } else if (next < current) {
2524 /* downward state change */
2525 if (child_pending == GST_STATE_VOID_PENDING) {
2526 /* .. and the child is not busy doing anything */
2527 if (child_current < next) {
2528 /* .. and is already past the requested state, assume it got there
2530 ret = GST_STATE_CHANGE_SUCCESS;
2533 } else if (child_pending < child_current) {
2534 /* .. and the child is busy going downwards */
2535 if (child_current <= next) {
2536 /* .. and is already past the requested state, assume it got there
2538 ret = GST_STATE_CHANGE_SUCCESS;
2542 /* .. and the child is busy going upwards */
2543 if (child_current < next) {
2544 /* .. and is already past the requested state, assume it got there
2546 ret = GST_STATE_CHANGE_SUCCESS;
2553 GST_OBJECT_LOCK (bin);
2554 /* the element was busy with an upwards async state change, we must wait for
2555 * an ASYNC_DONE message before we attempt to change the state. */
2557 find_message (bin, GST_OBJECT_CAST (element),
2558 GST_MESSAGE_ASYNC_START))) {
2559 #ifndef GST_DISABLE_GST_DEBUG
2560 GstMessage *message = GST_MESSAGE_CAST (found->data);
2562 GST_DEBUG_OBJECT (element, "element message %p, %s async busy",
2563 message, GST_ELEMENT_NAME (GST_MESSAGE_SRC (message)));
2565 /* only wait for upward state changes */
2566 if (next > current) {
2567 /* We found an async element check if we can force its state to change or
2568 * if we have to wait for it to preroll. */
2572 GST_OBJECT_UNLOCK (bin);
2575 GST_DEBUG_OBJECT (bin,
2576 "setting element %s to %s, base_time %" GST_TIME_FORMAT,
2577 GST_ELEMENT_NAME (element), gst_element_state_get_name (next),
2578 GST_TIME_ARGS (base_time));
2581 ret = gst_element_set_state (element, next);
2583 GST_STATE_UNLOCK (element);
2589 GST_DEBUG_OBJECT (element,
2590 "element is locked, return previous return %s",
2591 gst_element_state_change_return_get_name (ret));
2592 GST_STATE_UNLOCK (element);
2597 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2598 "skipping transition from %s to %s",
2599 gst_element_state_get_name (child_current),
2600 gst_element_state_get_name (next));
2601 GST_STATE_UNLOCK (element);
2606 GST_DEBUG_OBJECT (element, "element was busy, delaying state change");
2607 GST_OBJECT_UNLOCK (bin);
2608 GST_STATE_UNLOCK (element);
2609 return GST_STATE_CHANGE_ASYNC;
2613 /* gst_iterator_fold functions for pads_activate
2614 * Stop the iterator if activating one pad failed, but only if that pad
2615 * has not been removed from the element. */
2617 activate_pads (const GValue * vpad, GValue * ret, gboolean * active)
2619 GstPad *pad = g_value_get_object (vpad);
2620 gboolean cont = TRUE;
2622 if (!gst_pad_set_active (pad, *active)) {
2623 if (GST_PAD_PARENT (pad) != NULL) {
2625 g_value_set_boolean (ret, FALSE);
2632 /* returns false on error or early cutout of the fold, true if all
2633 * pads in @iter were (de)activated successfully. */
2635 iterator_activate_fold_with_resync (GstIterator * iter, gpointer user_data)
2637 GstIteratorResult ires;
2640 /* no need to unset this later, it's just a boolean */
2641 g_value_init (&ret, G_TYPE_BOOLEAN);
2642 g_value_set_boolean (&ret, TRUE);
2645 ires = gst_iterator_fold (iter, (GstIteratorFoldFunction) activate_pads,
2648 case GST_ITERATOR_RESYNC:
2649 /* need to reset the result again */
2650 g_value_set_boolean (&ret, TRUE);
2651 gst_iterator_resync (iter);
2653 case GST_ITERATOR_DONE:
2654 /* all pads iterated, return collected value */
2657 /* iterator returned _ERROR or premature end with _OK,
2658 * mark an error and exit */
2659 g_value_set_boolean (&ret, FALSE);
2664 /* return collected value */
2665 return g_value_get_boolean (&ret);
2668 /* is called with STATE_LOCK
2671 gst_bin_src_pads_activate (GstBin * bin, gboolean active)
2676 GST_DEBUG_OBJECT (bin, "%s pads", active ? "activate" : "deactivate");
2678 iter = gst_element_iterate_src_pads ((GstElement *) bin);
2679 fold_ok = iterator_activate_fold_with_resync (iter, &active);
2680 gst_iterator_free (iter);
2681 if (G_UNLIKELY (!fold_ok))
2684 GST_DEBUG_OBJECT (bin, "pad %sactivation successful", active ? "" : "de");
2691 GST_DEBUG_OBJECT (bin, "pad %sactivation failed", active ? "" : "de");
2697 * gst_bin_recalculate_latency:
2700 * Queries @bin for the current latency and reconfigures this latency on all the
2701 * elements using a LATENCY event.
2703 * This method is typically called on the pipeline when a #GST_MESSAGE_LATENCY
2704 * is posted on the bus.
2706 * This function simply emits the #GstBin::do-latency signal so any custom latency
2707 * calculations will be performed.
2709 * Returns: %TRUE if the latency could be queried and reconfigured.
2712 gst_bin_recalculate_latency (GstBin * bin)
2716 g_signal_emit (bin, gst_bin_signals[DO_LATENCY], 0, &res);
2717 GST_DEBUG_OBJECT (bin, "latency returned %d", res);
2723 gst_bin_do_latency_func (GstBin * bin)
2726 GstElement *element;
2727 GstClockTime min_latency, max_latency;
2730 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2732 element = GST_ELEMENT_CAST (bin);
2734 GST_DEBUG_OBJECT (element, "querying latency");
2736 query = gst_query_new_latency ();
2737 if ((res = gst_element_query (element, query))) {
2740 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
2742 GST_DEBUG_OBJECT (element,
2743 "got min latency %" GST_TIME_FORMAT ", max latency %"
2744 GST_TIME_FORMAT ", live %d", GST_TIME_ARGS (min_latency),
2745 GST_TIME_ARGS (max_latency), live);
2747 if (max_latency < min_latency) {
2748 /* this is an impossible situation, some parts of the pipeline might not
2749 * work correctly. We post a warning for now. */
2750 GST_ELEMENT_WARNING (element, CORE, CLOCK, (NULL),
2751 ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
2752 GST_TIME_FORMAT ". Add queues or other buffering elements.",
2753 GST_TIME_ARGS (max_latency), GST_TIME_ARGS (min_latency)));
2756 /* configure latency on elements */
2757 res = gst_element_send_event (element, gst_event_new_latency (min_latency));
2759 GST_INFO_OBJECT (element, "configured latency of %" GST_TIME_FORMAT,
2760 GST_TIME_ARGS (min_latency));
2762 GST_WARNING_OBJECT (element,
2763 "did not really configure latency of %" GST_TIME_FORMAT,
2764 GST_TIME_ARGS (min_latency));
2767 /* this is not a real problem, we just don't configure any latency. */
2768 GST_WARNING_OBJECT (element, "failed to query latency");
2770 gst_query_unref (query);
2776 gst_bin_post_message (GstElement * element, GstMessage * msg)
2778 GstElementClass *pklass = (GstElementClass *) parent_class;
2781 ret = pklass->post_message (element, gst_message_ref (msg));
2783 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_STATE_CHANGED &&
2784 GST_MESSAGE_SRC (msg) == GST_OBJECT_CAST (element)) {
2785 GstState newstate, pending;
2787 gst_message_parse_state_changed (msg, NULL, &newstate, &pending);
2788 if (newstate == GST_STATE_PLAYING && pending == GST_STATE_VOID_PENDING) {
2789 GST_BIN_CAST (element)->priv->posted_playing = TRUE;
2790 bin_do_eos (GST_BIN_CAST (element));
2792 GST_BIN_CAST (element)->priv->posted_playing = FALSE;
2796 gst_message_unref (msg);
2802 reset_state (const GValue * data, gpointer user_data)
2804 GstElement *e = g_value_get_object (data);
2805 GstState state = GPOINTER_TO_INT (user_data);
2807 if (gst_element_set_state (e, state) == GST_STATE_CHANGE_FAILURE)
2808 GST_WARNING_OBJECT (e, "Failed to switch back down to %s",
2809 gst_element_state_get_name (state));
2812 static GstStateChangeReturn
2813 gst_bin_change_state_func (GstElement * element, GstStateChange transition)
2816 GstStateChangeReturn ret;
2817 GstState current, next;
2818 gboolean have_async;
2819 gboolean have_no_preroll;
2820 GstClockTime base_time, start_time;
2823 GValue data = { 0, };
2825 /* we don't need to take the STATE_LOCK, it is already taken */
2826 current = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2827 next = (GstState) GST_STATE_TRANSITION_NEXT (transition);
2829 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2830 "changing state of children from %s to %s",
2831 gst_element_state_get_name (current), gst_element_state_get_name (next));
2833 bin = GST_BIN_CAST (element);
2836 case GST_STATE_PLAYING:
2838 gboolean toplevel, asynchandling;
2840 GST_OBJECT_LOCK (bin);
2841 toplevel = BIN_IS_TOPLEVEL (bin);
2842 asynchandling = bin->priv->asynchandling;
2843 GST_OBJECT_UNLOCK (bin);
2846 gst_bin_recalculate_latency (bin);
2848 gst_element_post_message (element,
2849 gst_message_new_latency (GST_OBJECT_CAST (element)));
2852 case GST_STATE_PAUSED:
2853 /* Clear EOS list on next PAUSED */
2854 GST_OBJECT_LOCK (bin);
2855 GST_DEBUG_OBJECT (element, "clearing EOS elements");
2856 bin_remove_messages (bin, NULL, GST_MESSAGE_EOS);
2857 bin->priv->posted_eos = FALSE;
2858 if (current == GST_STATE_READY)
2859 bin_remove_messages (bin, NULL, GST_MESSAGE_STREAM_START);
2860 GST_OBJECT_UNLOCK (bin);
2861 if (current == GST_STATE_READY)
2862 if (!(gst_bin_src_pads_activate (bin, TRUE)))
2863 goto activate_failure;
2865 case GST_STATE_READY:
2866 /* Clear message list on next READY */
2867 GST_OBJECT_LOCK (bin);
2868 GST_DEBUG_OBJECT (element, "clearing all cached messages");
2869 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
2870 GST_OBJECT_UNLOCK (bin);
2871 /* We might not have reached PAUSED yet due to async errors,
2872 * make sure to always deactivate the pads nonetheless */
2873 if (!(gst_bin_src_pads_activate (bin, FALSE)))
2874 goto activate_failure;
2876 case GST_STATE_NULL:
2877 /* Clear message list on next NULL */
2878 GST_OBJECT_LOCK (bin);
2879 GST_DEBUG_OBJECT (element, "clearing all cached messages");
2880 bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
2881 GST_OBJECT_UNLOCK (bin);
2882 if (current == GST_STATE_READY) {
2883 if (!(gst_bin_src_pads_activate (bin, FALSE)))
2884 goto activate_failure;
2891 /* this flag is used to make the async state changes return immediately. We
2892 * don't want them to interfere with this state change */
2893 GST_OBJECT_LOCK (bin);
2894 bin->polling = TRUE;
2895 GST_OBJECT_UNLOCK (bin);
2897 /* iterate in state change order */
2898 it = gst_bin_iterate_sorted (bin);
2900 /* mark if we've seen an ASYNC element in the bin when we did a state change.
2901 * Note how we don't reset this value when a resync happens, the reason being
2902 * that the async element posted ASYNC_START and we want to post ASYNC_DONE
2903 * even after a resync when the async element is gone */
2907 /* take base_time */
2908 base_time = gst_element_get_base_time (element);
2909 start_time = gst_element_get_start_time (element);
2911 have_no_preroll = FALSE;
2915 switch (gst_iterator_next (it, &data)) {
2916 case GST_ITERATOR_OK:
2920 child = g_value_get_object (&data);
2922 /* set state and base_time now */
2923 ret = gst_bin_element_set_state (bin, child, base_time, start_time,
2927 case GST_STATE_CHANGE_SUCCESS:
2928 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2929 "child '%s' changed state to %d(%s) successfully",
2930 GST_ELEMENT_NAME (child), next,
2931 gst_element_state_get_name (next));
2933 case GST_STATE_CHANGE_ASYNC:
2935 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2936 "child '%s' is changing state asynchronously to %s",
2937 GST_ELEMENT_NAME (child), gst_element_state_get_name (next));
2941 case GST_STATE_CHANGE_FAILURE:{
2944 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2945 "child '%s' failed to go to state %d(%s)",
2946 GST_ELEMENT_NAME (child),
2947 next, gst_element_state_get_name (next));
2949 /* Only fail if the child is still inside
2950 * this bin. It might've been removed already
2951 * because of the error by the bin subclass
2952 * to ignore the error. */
2953 parent = gst_object_get_parent (GST_OBJECT_CAST (child));
2954 if (parent == GST_OBJECT_CAST (element)) {
2955 /* element is still in bin, really error now */
2956 gst_object_unref (parent);
2959 /* child removed from bin, let the resync code redo the state
2961 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2962 "child '%s' was removed from the bin",
2963 GST_ELEMENT_NAME (child));
2966 gst_object_unref (parent);
2970 case GST_STATE_CHANGE_NO_PREROLL:
2971 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2972 "child '%s' changed state to %d(%s) successfully without preroll",
2973 GST_ELEMENT_NAME (child), next,
2974 gst_element_state_get_name (next));
2975 have_no_preroll = TRUE;
2978 g_assert_not_reached ();
2981 g_value_reset (&data);
2984 case GST_ITERATOR_RESYNC:
2985 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "iterator doing resync");
2986 gst_iterator_resync (it);
2989 case GST_ITERATOR_DONE:
2990 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "iterator done");
2996 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2997 if (G_UNLIKELY (ret == GST_STATE_CHANGE_FAILURE))
3000 if (have_no_preroll) {
3001 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
3002 "we have NO_PREROLL elements %s -> NO_PREROLL",
3003 gst_element_state_change_return_get_name (ret));
3004 ret = GST_STATE_CHANGE_NO_PREROLL;
3005 } else if (have_async) {
3006 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
3007 "we have ASYNC elements %s -> ASYNC",
3008 gst_element_state_change_return_get_name (ret));
3009 ret = GST_STATE_CHANGE_ASYNC;
3013 g_value_unset (&data);
3014 gst_iterator_free (it);
3016 GST_OBJECT_LOCK (bin);
3017 bin->polling = FALSE;
3018 /* it's possible that we did not get ASYNC from the children while the bin is
3019 * simulating ASYNC behaviour by posting an ASYNC_DONE message on the bus with
3020 * itself as the source. In that case we still want to check if the state
3021 * change completed. */
3022 if (ret != GST_STATE_CHANGE_ASYNC && !bin->priv->pending_async_done) {
3023 /* no element returned ASYNC and there are no pending async_done messages,
3024 * we can just complete. */
3025 GST_DEBUG_OBJECT (bin, "no async elements");
3028 /* when we get here an ASYNC element was found */
3029 if (GST_STATE_TARGET (bin) <= GST_STATE_READY) {
3030 /* we ignore ASYNC state changes when we go to READY or NULL */
3031 GST_DEBUG_OBJECT (bin, "target state %s <= READY",
3032 gst_element_state_get_name (GST_STATE_TARGET (bin)));
3036 GST_DEBUG_OBJECT (bin, "check async elements");
3037 /* check if all elements managed to commit their state already */
3038 if (!find_message (bin, NULL, GST_MESSAGE_ASYNC_START)) {
3039 /* nothing found, remove all old ASYNC_DONE messages. This can happen when
3040 * all the elements committed their state while we were doing the state
3041 * change. We will still return ASYNC for consistency but we commit the
3042 * state already so that a _get_state() will return immediately. */
3043 bin_remove_messages (bin, NULL, GST_MESSAGE_ASYNC_DONE);
3045 GST_DEBUG_OBJECT (bin, "async elements committed");
3046 bin_handle_async_done (bin, GST_STATE_CHANGE_SUCCESS, FALSE,
3047 GST_CLOCK_TIME_NONE);
3051 bin->priv->pending_async_done = FALSE;
3052 GST_OBJECT_UNLOCK (bin);
3054 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3055 "done changing bin's state from %s to %s, now in %s, ret %s",
3056 gst_element_state_get_name (current),
3057 gst_element_state_get_name (next),
3058 gst_element_state_get_name (GST_STATE (element)),
3059 gst_element_state_change_return_get_name (ret));
3066 GST_CAT_WARNING_OBJECT (GST_CAT_STATES, element,
3067 "failure (de)activating src pads");
3068 return GST_STATE_CHANGE_FAILURE;
3073 if (current < next) {
3074 GstIterator *it = gst_bin_iterate_sorted (GST_BIN (element));
3075 GstIteratorResult ret;
3077 GST_DEBUG_OBJECT (element,
3078 "Bin failed to change state, switching children back to %s",
3079 gst_element_state_get_name (current));
3082 gst_iterator_foreach (it, &reset_state, GINT_TO_POINTER (current));
3083 if (ret != GST_ITERATOR_RESYNC)
3085 gst_iterator_resync (it);
3087 gst_iterator_free (it);
3094 * This function is a utility event handler. It will send the event to all sinks
3095 * or sources and appropriate ghost pads depending on the event-direction.
3097 * Applications are free to override this behaviour and implement their own
3098 * handler, but this will work for pretty much all cases in practice.
3101 gst_bin_send_event (GstElement * element, GstEvent * event)
3103 GstBin *bin = GST_BIN_CAST (element);
3105 gboolean res = TRUE;
3106 gboolean done = FALSE;
3107 GValue data = { 0, };
3109 if (GST_EVENT_IS_DOWNSTREAM (event)) {
3110 iter = gst_bin_iterate_sources (bin);
3111 GST_DEBUG_OBJECT (bin, "Sending %s event to src children",
3112 GST_EVENT_TYPE_NAME (event));
3114 iter = gst_bin_iterate_sinks (bin);
3115 GST_DEBUG_OBJECT (bin, "Sending %s event to sink children",
3116 GST_EVENT_TYPE_NAME (event));
3120 switch (gst_iterator_next (iter, &data)) {
3121 case GST_ITERATOR_OK:
3123 GstElement *child = g_value_get_object (&data);
3125 gst_event_ref (event);
3126 res &= gst_element_send_event (child, event);
3128 GST_LOG_OBJECT (child, "After handling %s event: %d",
3129 GST_EVENT_TYPE_NAME (event), res);
3131 g_value_reset (&data);
3134 case GST_ITERATOR_RESYNC:
3135 gst_iterator_resync (iter);
3138 case GST_ITERATOR_DONE:
3141 case GST_ITERATOR_ERROR:
3142 g_assert_not_reached ();
3146 g_value_unset (&data);
3147 gst_iterator_free (iter);
3149 if (GST_EVENT_IS_DOWNSTREAM (event)) {
3150 iter = gst_element_iterate_sink_pads (GST_ELEMENT (bin));
3151 GST_DEBUG_OBJECT (bin, "Sending %s event to sink pads",
3152 GST_EVENT_TYPE_NAME (event));
3154 iter = gst_element_iterate_src_pads (GST_ELEMENT (bin));
3155 GST_DEBUG_OBJECT (bin, "Sending %s event to src pads",
3156 GST_EVENT_TYPE_NAME (event));
3161 switch (gst_iterator_next (iter, &data)) {
3162 case GST_ITERATOR_OK:
3164 GstPad *pad = g_value_get_object (&data);
3166 gst_event_ref (event);
3167 res &= gst_pad_send_event (pad, event);
3168 GST_LOG_OBJECT (pad, "After handling %s event: %d",
3169 GST_EVENT_TYPE_NAME (event), res);
3172 case GST_ITERATOR_RESYNC:
3173 gst_iterator_resync (iter);
3176 case GST_ITERATOR_DONE:
3179 case GST_ITERATOR_ERROR:
3180 g_assert_not_reached ();
3185 g_value_unset (&data);
3186 gst_iterator_free (iter);
3187 gst_event_unref (event);
3192 /* this is the function called by the threadpool. When async elements commit
3193 * their state, this function will attempt to bring the bin to the next state.
3196 gst_bin_continue_func (GstBin * bin, BinContinueData * data)
3198 GstState current, next, pending;
3199 GstStateChange transition;
3201 pending = data->pending;
3203 GST_DEBUG_OBJECT (bin, "waiting for state lock");
3204 GST_STATE_LOCK (bin);
3206 GST_DEBUG_OBJECT (bin, "doing state continue");
3207 GST_OBJECT_LOCK (bin);
3209 /* if a new state change happened after this thread was scheduled, we return
3211 if (data->cookie != GST_ELEMENT_CAST (bin)->state_cookie)
3214 current = GST_STATE (bin);
3215 next = GST_STATE_GET_NEXT (current, pending);
3216 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
3218 GST_STATE_NEXT (bin) = next;
3219 GST_STATE_PENDING (bin) = pending;
3221 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3222 GST_OBJECT_UNLOCK (bin);
3224 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3225 "continue state change %s to %s, final %s",
3226 gst_element_state_get_name (current),
3227 gst_element_state_get_name (next), gst_element_state_get_name (pending));
3229 gst_element_change_state (GST_ELEMENT_CAST (bin), transition);
3231 GST_STATE_UNLOCK (bin);
3232 GST_DEBUG_OBJECT (bin, "state continue done");
3238 GST_OBJECT_UNLOCK (bin);
3239 GST_STATE_UNLOCK (bin);
3240 GST_DEBUG_OBJECT (bin, "state continue aborted due to intervening change");
3245 static GstBusSyncReply
3246 bin_bus_handler (GstBus * bus, GstMessage * message, GstBin * bin)
3248 GstBinClass *bclass;
3250 bclass = GST_BIN_GET_CLASS (bin);
3251 if (bclass->handle_message)
3252 bclass->handle_message (bin, message);
3254 gst_message_unref (message);
3256 return GST_BUS_DROP;
3260 free_bin_continue_data (BinContinueData * data)
3262 g_slice_free (BinContinueData, data);
3266 bin_push_state_continue (GstBin * bin, BinContinueData * data)
3268 GST_DEBUG_OBJECT (bin, "pushing continue on thread pool");
3269 gst_element_call_async (GST_ELEMENT_CAST (bin),
3270 (GstElementCallAsyncFunc) gst_bin_continue_func, data,
3271 (GDestroyNotify) free_bin_continue_data);
3274 /* an element started an async state change, if we were not busy with a state
3275 * change, we perform a lost state.
3276 * This function is called with the OBJECT lock.
3279 bin_handle_async_start (GstBin * bin)
3281 GstState old_state, new_state;
3283 GstMessage *amessage = NULL;
3285 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_FAILURE)
3288 /* get our toplevel state */
3289 toplevel = BIN_IS_TOPLEVEL (bin);
3291 /* prepare an ASYNC_START message, we always post the start message even if we
3292 * are busy with a state change or when we are NO_PREROLL. */
3294 /* non toplevel bin, prepare async-start for the parent */
3295 amessage = gst_message_new_async_start (GST_OBJECT_CAST (bin));
3297 if (bin->polling || GST_STATE_PENDING (bin) != GST_STATE_VOID_PENDING)
3300 /* async starts are ignored when we are NO_PREROLL */
3301 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_NO_PREROLL)
3302 goto was_no_preroll;
3304 old_state = GST_STATE (bin);
3306 /* when we PLAYING we go back to PAUSED, when preroll happens, we go back to
3307 * PLAYING after optionally redistributing the base_time. */
3308 if (old_state > GST_STATE_PAUSED)
3309 new_state = GST_STATE_PAUSED;
3311 new_state = old_state;
3313 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
3314 "lost state of %s, new %s", gst_element_state_get_name (old_state),
3315 gst_element_state_get_name (new_state));
3317 GST_STATE (bin) = new_state;
3318 GST_STATE_NEXT (bin) = new_state;
3319 GST_STATE_PENDING (bin) = new_state;
3320 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3321 GST_OBJECT_UNLOCK (bin);
3324 _priv_gst_element_state_changed (GST_ELEMENT_CAST (bin), new_state, new_state,
3329 /* post our ASYNC_START. */
3330 GST_DEBUG_OBJECT (bin, "posting ASYNC_START to parent");
3331 gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
3333 GST_OBJECT_LOCK (bin);
3339 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "we had an error");
3344 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "state change busy");
3345 GST_OBJECT_UNLOCK (bin);
3350 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "ignoring, we are NO_PREROLL");
3351 GST_OBJECT_UNLOCK (bin);
3356 /* this function is called when there are no more async elements in the bin. We
3357 * post a state changed message and an ASYNC_DONE message.
3358 * This function is called with the OBJECT lock.
3361 bin_handle_async_done (GstBin * bin, GstStateChangeReturn ret,
3362 gboolean flag_pending, GstClockTime running_time)
3364 GstState current, pending, target;
3365 GstStateChangeReturn old_ret;
3366 GstState old_state, old_next;
3367 gboolean toplevel, state_changed = FALSE;
3368 GstMessage *amessage = NULL;
3369 BinContinueData *cont = NULL;
3371 if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_FAILURE)
3374 pending = GST_STATE_PENDING (bin);
3379 /* check if there is something to commit */
3380 if (pending == GST_STATE_VOID_PENDING)
3381 goto nothing_pending;
3383 old_ret = GST_STATE_RETURN (bin);
3384 GST_STATE_RETURN (bin) = ret;
3386 /* move to the next target state */
3387 target = GST_STATE_TARGET (bin);
3388 pending = GST_STATE_PENDING (bin) = target;
3390 amessage = gst_message_new_async_done (GST_OBJECT_CAST (bin), running_time);
3392 old_state = GST_STATE (bin);
3393 /* this is the state we should go to next */
3394 old_next = GST_STATE_NEXT (bin);
3396 if (old_next != GST_STATE_PLAYING) {
3397 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3398 "committing state from %s to %s, old pending %s",
3399 gst_element_state_get_name (old_state),
3400 gst_element_state_get_name (old_next),
3401 gst_element_state_get_name (pending));
3403 /* update current state */
3404 current = GST_STATE (bin) = old_next;
3406 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3407 "setting state from %s to %s, pending %s",
3408 gst_element_state_get_name (old_state),
3409 gst_element_state_get_name (old_state),
3410 gst_element_state_get_name (pending));
3411 current = old_state;
3414 /* get our toplevel state */
3415 toplevel = BIN_IS_TOPLEVEL (bin);
3417 /* see if we reached the final state. If we are not toplevel, we also have to
3418 * stop here, the parent will continue our state. */
3419 if ((pending == current) || !toplevel) {
3420 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3421 "completed state change, pending VOID");
3423 /* mark VOID pending */
3424 pending = GST_STATE_VOID_PENDING;
3425 GST_STATE_PENDING (bin) = pending;
3426 GST_STATE_NEXT (bin) = GST_STATE_VOID_PENDING;
3428 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3429 "continue state change, pending %s",
3430 gst_element_state_get_name (pending));
3432 cont = g_slice_new (BinContinueData);
3434 /* cookie to detect concurrent state change */
3435 cont->cookie = GST_ELEMENT_CAST (bin)->state_cookie;
3436 /* pending target state */
3437 cont->pending = pending;
3439 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3440 GST_STATE_NEXT (bin) = GST_STATE_GET_NEXT (old_state, pending);
3443 if (old_next != GST_STATE_PLAYING) {
3444 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC) {
3445 state_changed = TRUE;
3448 GST_OBJECT_UNLOCK (bin);
3450 if (state_changed) {
3451 _priv_gst_element_state_changed (GST_ELEMENT_CAST (bin), old_state,
3455 /* post our combined ASYNC_DONE when all is ASYNC_DONE. */
3456 GST_DEBUG_OBJECT (bin, "posting ASYNC_DONE to parent");
3457 gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
3460 GST_OBJECT_LOCK (bin);
3462 /* toplevel, start continue state */
3463 GST_DEBUG_OBJECT (bin, "all async-done, starting state continue");
3464 bin_push_state_continue (bin, cont);
3466 GST_DEBUG_OBJECT (bin, "state change complete");
3467 GST_STATE_BROADCAST (bin);
3473 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "we had an error");
3478 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "state change busy");
3479 /* if we were busy with a state change and we are requested to flag a
3480 * pending async done, we do so here */
3482 bin->priv->pending_async_done = TRUE;
3487 GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin, "nothing pending");
3493 bin_do_eos (GstBin * bin)
3495 guint32 seqnum = GST_SEQNUM_INVALID;
3498 GST_OBJECT_LOCK (bin);
3499 /* If all sinks are EOS, we're in PLAYING and no state change is pending
3500 * (or we're doing playing to playing and no one else will trigger posting
3501 * EOS for us) we forward the EOS message to the parent bin or application
3503 eos = GST_STATE (bin) == GST_STATE_PLAYING
3504 && (GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING ||
3505 GST_STATE_PENDING (bin) == GST_STATE_PLAYING)
3506 && bin->priv->posted_playing && is_eos (bin, &seqnum);
3507 GST_OBJECT_UNLOCK (bin);
3510 && g_atomic_int_compare_and_exchange (&bin->priv->posted_eos, FALSE,
3512 GstMessage *tmessage;
3514 /* Clear out any further messages, and reset posted_eos so we can
3515 detect any new EOS that happens (eg, after a seek). Since all
3516 sinks have now posted an EOS, there will be no further EOS events
3517 seen unless there is a new logical EOS */
3518 GST_OBJECT_LOCK (bin);
3519 bin_remove_messages (bin, NULL, GST_MESSAGE_EOS);
3520 bin->priv->posted_eos = FALSE;
3521 GST_OBJECT_UNLOCK (bin);
3523 tmessage = gst_message_new_eos (GST_OBJECT_CAST (bin));
3524 if (seqnum != GST_SEQNUM_INVALID)
3525 gst_message_set_seqnum (tmessage, seqnum);
3526 GST_DEBUG_OBJECT (bin,
3527 "all sinks posted EOS, posting seqnum #%" G_GUINT32_FORMAT, seqnum);
3528 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3530 GST_LOG_OBJECT (bin, "Not forwarding EOS due to in progress state change, "
3531 " or already posted, or waiting for more EOS");
3536 bin_do_stream_start (GstBin * bin)
3538 guint32 seqnum = GST_SEQNUM_INVALID;
3539 gboolean stream_start;
3540 gboolean have_group_id = FALSE;
3543 GST_OBJECT_LOCK (bin);
3544 /* If all sinks are STREAM_START we forward the STREAM_START message
3545 * to the parent bin or application
3547 stream_start = is_stream_start (bin, &seqnum, &have_group_id, &group_id);
3548 GST_OBJECT_UNLOCK (bin);
3551 GstMessage *tmessage;
3553 GST_OBJECT_LOCK (bin);
3554 bin_remove_messages (bin, NULL, GST_MESSAGE_STREAM_START);
3555 GST_OBJECT_UNLOCK (bin);
3557 tmessage = gst_message_new_stream_start (GST_OBJECT_CAST (bin));
3558 if (seqnum != GST_SEQNUM_INVALID)
3559 gst_message_set_seqnum (tmessage, seqnum);
3561 gst_message_set_group_id (tmessage, group_id);
3563 GST_DEBUG_OBJECT (bin,
3564 "all sinks posted STREAM_START, posting seqnum #%" G_GUINT32_FORMAT,
3566 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3570 /* must be called without the object lock as it posts messages */
3572 bin_do_message_forward (GstBin * bin, GstMessage * message)
3574 if (bin->priv->message_forward) {
3575 GstMessage *forwarded;
3577 GST_DEBUG_OBJECT (bin, "pass %s message upward",
3578 GST_MESSAGE_TYPE_NAME (message));
3580 /* we need to convert these messages to element messages so that our parent
3581 * bin can easily ignore them and so that the application can easily
3582 * distinguish between the internally forwarded and the real messages. */
3583 forwarded = gst_message_new_element (GST_OBJECT_CAST (bin),
3584 gst_structure_new ("GstBinForwarded",
3585 "message", GST_TYPE_MESSAGE, message, NULL));
3587 gst_element_post_message (GST_ELEMENT_CAST (bin), forwarded);
3592 gst_bin_update_context (GstBin * bin, GstContext * context)
3594 GST_OBJECT_LOCK (bin);
3595 gst_bin_update_context_unlocked (bin, context);
3596 GST_OBJECT_UNLOCK (bin);
3600 gst_bin_update_context_unlocked (GstBin * bin, GstContext * context)
3602 const gchar *context_type;
3603 GList *l, **contexts;
3605 contexts = &GST_ELEMENT_CAST (bin)->contexts;
3606 context_type = gst_context_get_context_type (context);
3608 GST_DEBUG_OBJECT (bin, "set context %p %" GST_PTR_FORMAT, context,
3609 gst_context_get_structure (context));
3610 for (l = *contexts; l; l = l->next) {
3611 GstContext *tmp = l->data;
3612 const gchar *tmp_type = gst_context_get_context_type (tmp);
3614 /* Always store newest context but never replace
3615 * a persistent one by a non-persistent one */
3616 if (strcmp (context_type, tmp_type) == 0 &&
3617 (gst_context_is_persistent (context) ||
3618 !gst_context_is_persistent (tmp))) {
3619 gst_context_replace ((GstContext **) & l->data, context);
3623 /* Not found? Add */
3625 *contexts = g_list_prepend (*contexts, gst_context_ref (context));
3629 /* handle child messages:
3631 * This method is called synchronously when a child posts a message on
3634 * GST_MESSAGE_EOS: This message is only posted by sinks
3635 * in the PLAYING state. If all sinks posted the EOS message, post
3638 * GST_MESSAGE_STATE_DIRTY: Deprecated
3640 * GST_MESSAGE_SEGMENT_START: just collect, never forward upwards. If an
3641 * element posts segment_start twice, only the last message is kept.
3643 * GST_MESSAGE_SEGMENT_DONE: replace SEGMENT_START message from same poster
3644 * with the segment_done message. If there are no more segment_start
3645 * messages, post segment_done message upwards.
3647 * GST_MESSAGE_CLOCK_LOST: This message is posted by an element when it
3648 * can no longer provide a clock. The default bin behaviour is to
3649 * check if the lost clock was the one provided by the bin. If so and
3650 * we are currently in the PLAYING state, we forward the message to
3652 * This message is also generated when we remove a clock provider from
3653 * a bin. If this message is received by the application, it should
3654 * PAUSE the pipeline and set it back to PLAYING to force a new clock
3655 * and a new base_time distribution.
3657 * GST_MESSAGE_CLOCK_PROVIDE: This message is generated when an element
3658 * can provide a clock. This mostly happens when we add a new clock
3659 * provider to the bin. The default behaviour of the bin is to mark the
3660 * currently selected clock as dirty, which will perform a clock
3661 * recalculation the next time we are asked to provide a clock.
3662 * This message is never sent to the application but is forwarded to
3665 * GST_MESSAGE_ASYNC_START: Create an internal ELEMENT message that stores
3666 * the state of the element and the fact that the element will need a
3667 * new base_time. This message is not forwarded to the application.
3669 * GST_MESSAGE_ASYNC_DONE: Find the internal ELEMENT message we kept for the
3670 * element when it posted ASYNC_START. If all elements are done, post a
3671 * ASYNC_DONE message to the parent.
3673 * OTHER: post upwards.
3676 gst_bin_handle_message_func (GstBin * bin, GstMessage * message)
3679 GstMessageType type;
3680 GstMessage *tmessage;
3683 src = GST_MESSAGE_SRC (message);
3684 type = GST_MESSAGE_TYPE (message);
3686 GST_DEBUG_OBJECT (bin, "[msg %p] handling child %s message of type %s",
3687 message, src ? GST_ELEMENT_NAME (src) : "(NULL)",
3688 GST_MESSAGE_TYPE_NAME (message));
3691 case GST_MESSAGE_ERROR:
3693 GST_OBJECT_LOCK (bin);
3695 GST_DEBUG_OBJECT (bin, "got ERROR message, unlocking state change");
3696 GST_STATE_RETURN (bin) = GST_STATE_CHANGE_FAILURE;
3697 GST_STATE_BROADCAST (bin);
3698 GST_OBJECT_UNLOCK (bin);
3702 case GST_MESSAGE_EOS:
3705 /* collect all eos messages from the children */
3706 bin_do_message_forward (bin, message);
3707 GST_OBJECT_LOCK (bin);
3708 /* ref message for future use */
3709 bin_replace_message (bin, message, GST_MESSAGE_EOS);
3710 GST_OBJECT_UNLOCK (bin);
3715 case GST_MESSAGE_STREAM_START:
3718 /* collect all stream_start messages from the children */
3719 GST_OBJECT_LOCK (bin);
3720 /* ref message for future use */
3721 bin_replace_message (bin, message, GST_MESSAGE_STREAM_START);
3722 GST_OBJECT_UNLOCK (bin);
3724 bin_do_stream_start (bin);
3727 case GST_MESSAGE_STATE_DIRTY:
3729 GST_WARNING_OBJECT (bin, "received deprecated STATE_DIRTY message");
3732 gst_message_unref (message);
3735 case GST_MESSAGE_SEGMENT_START:{
3736 gboolean post = FALSE;
3740 gst_message_parse_segment_start (message, &format, &position);
3741 seqnum = gst_message_get_seqnum (message);
3743 bin_do_message_forward (bin, message);
3745 GST_OBJECT_LOCK (bin);
3746 /* if this is the first segment-start, post to parent but not to the
3748 if (!find_message (bin, NULL, GST_MESSAGE_SEGMENT_START) &&
3749 (GST_OBJECT_PARENT (bin) != NULL)) {
3752 /* replace any previous segment_start message from this source
3753 * with the new segment start message */
3754 bin_replace_message (bin, message, GST_MESSAGE_SEGMENT_START);
3755 GST_OBJECT_UNLOCK (bin);
3757 tmessage = gst_message_new_segment_start (GST_OBJECT_CAST (bin),
3759 gst_message_set_seqnum (tmessage, seqnum);
3761 /* post segment start with initial format and position. */
3762 GST_DEBUG_OBJECT (bin, "posting SEGMENT_START (%u) bus message: %p",
3764 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3768 case GST_MESSAGE_SEGMENT_DONE:
3770 gboolean post = FALSE;
3774 gst_message_parse_segment_done (message, &format, &position);
3775 seqnum = gst_message_get_seqnum (message);
3777 bin_do_message_forward (bin, message);
3779 GST_OBJECT_LOCK (bin);
3780 bin_replace_message (bin, message, GST_MESSAGE_SEGMENT_START);
3781 /* if there are no more segment_start messages, everybody posted
3782 * a segment_done and we can post one on the bus. */
3784 /* we don't care who still has a pending segment start */
3785 if (!find_message (bin, NULL, GST_MESSAGE_SEGMENT_START)) {
3788 /* remove all old segment_done messages */
3789 bin_remove_messages (bin, NULL, GST_MESSAGE_SEGMENT_DONE);
3791 GST_OBJECT_UNLOCK (bin);
3793 tmessage = gst_message_new_segment_done (GST_OBJECT_CAST (bin),
3795 gst_message_set_seqnum (tmessage, seqnum);
3797 /* post segment done with latest format and position. */
3798 GST_DEBUG_OBJECT (bin, "posting SEGMENT_DONE (%u) bus message: %p",
3800 gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3804 case GST_MESSAGE_CLOCK_LOST:
3806 GstClock **provided_clock_p;
3807 GstElement **clock_provider_p;
3808 gboolean playing, toplevel, provided, forward;
3811 gst_message_parse_clock_lost (message, &clock);
3813 GST_OBJECT_LOCK (bin);
3814 bin->clock_dirty = TRUE;
3815 /* if we lost the clock that we provided, post to parent but
3816 * only if we are not a top-level bin or PLAYING.
3817 * The reason for this is that applications should be able
3818 * to PAUSE/PLAY if they receive this message without worrying
3819 * about the state of the pipeline. */
3820 provided = (clock == bin->provided_clock);
3821 playing = (GST_STATE (bin) == GST_STATE_PLAYING);
3822 toplevel = GST_OBJECT_PARENT (bin) == NULL;
3823 forward = provided && (playing || !toplevel);
3825 GST_DEBUG_OBJECT (bin,
3826 "Lost clock %" GST_PTR_FORMAT " provided by %" GST_PTR_FORMAT,
3827 bin->provided_clock, bin->clock_provider);
3828 provided_clock_p = &bin->provided_clock;
3829 clock_provider_p = &bin->clock_provider;
3830 gst_object_replace ((GstObject **) provided_clock_p, NULL);
3831 gst_object_replace ((GstObject **) clock_provider_p, NULL);
3833 GST_DEBUG_OBJECT (bin, "provided %d, playing %d, forward %d",
3834 provided, playing, forward);
3835 GST_OBJECT_UNLOCK (bin);
3841 gst_message_unref (message);
3844 case GST_MESSAGE_CLOCK_PROVIDE:
3848 GST_OBJECT_LOCK (bin);
3849 bin->clock_dirty = TRUE;
3850 /* a new clock is available, post to parent but not
3851 * to the application */
3852 forward = GST_OBJECT_PARENT (bin) != NULL;
3853 GST_OBJECT_UNLOCK (bin);
3859 gst_message_unref (message);
3862 case GST_MESSAGE_ASYNC_START:
3866 GST_DEBUG_OBJECT (bin, "ASYNC_START message %p, %s", message,
3867 src ? GST_OBJECT_NAME (src) : "(NULL)");
3869 bin_do_message_forward (bin, message);
3871 GST_OBJECT_LOCK (bin);
3872 /* we ignore the message if we are going to <= READY */
3873 if ((target = GST_STATE_TARGET (bin)) <= GST_STATE_READY)
3874 goto ignore_start_message;
3876 /* takes ownership of the message */
3877 bin_replace_message (bin, message, GST_MESSAGE_ASYNC_START);
3879 bin_handle_async_start (bin);
3880 GST_OBJECT_UNLOCK (bin);
3883 ignore_start_message:
3885 GST_DEBUG_OBJECT (bin, "ignoring message, target %s",
3886 gst_element_state_get_name (target));
3887 GST_OBJECT_UNLOCK (bin);
3888 gst_message_unref (message);
3892 case GST_MESSAGE_ASYNC_DONE:
3894 GstClockTime running_time;
3897 GST_DEBUG_OBJECT (bin, "ASYNC_DONE message %p, %s", message,
3898 src ? GST_OBJECT_NAME (src) : "(NULL)");
3900 gst_message_parse_async_done (message, &running_time);
3902 bin_do_message_forward (bin, message);
3904 GST_OBJECT_LOCK (bin);
3905 /* ignore messages if we are shutting down */
3906 if ((target = GST_STATE_TARGET (bin)) <= GST_STATE_READY)
3907 goto ignore_done_message;
3909 bin_replace_message (bin, message, GST_MESSAGE_ASYNC_START);
3910 /* if there are no more ASYNC_START messages, everybody posted
3911 * a ASYNC_DONE and we can post one on the bus. When checking, we
3912 * don't care who still has a pending ASYNC_START */
3913 if (!find_message (bin, NULL, GST_MESSAGE_ASYNC_START)) {
3914 /* nothing found, remove all old ASYNC_DONE messages */
3915 bin_remove_messages (bin, NULL, GST_MESSAGE_ASYNC_DONE);
3917 GST_DEBUG_OBJECT (bin, "async elements committed");
3918 /* when we get an async done message when a state change was busy, we
3919 * need to set the pending_done flag so that at the end of the state
3920 * change we can see if we need to verify pending async elements, hence
3921 * the TRUE argument here. */
3922 bin_handle_async_done (bin, GST_STATE_CHANGE_SUCCESS, TRUE,
3925 GST_DEBUG_OBJECT (bin, "there are more async elements pending");
3927 GST_OBJECT_UNLOCK (bin);
3930 ignore_done_message:
3932 GST_DEBUG_OBJECT (bin, "ignoring message, target %s",
3933 gst_element_state_get_name (target));
3934 GST_OBJECT_UNLOCK (bin);
3935 gst_message_unref (message);
3939 case GST_MESSAGE_STRUCTURE_CHANGE:
3943 gst_message_parse_structure_change (message, NULL, NULL, &busy);
3945 GST_OBJECT_LOCK (bin);
3947 /* while the pad is busy, avoid following it when doing state changes.
3948 * Don't update the cookie yet, we will do that after the structure
3949 * change finished and we are ready to inspect the new updated
3951 bin_replace_message (bin, message, GST_MESSAGE_STRUCTURE_CHANGE);
3954 /* a pad link/unlink ended, signal the state change iterator that we
3955 * need to resync by updating the structure_cookie. */
3956 bin_remove_messages (bin, GST_MESSAGE_SRC (message),
3957 GST_MESSAGE_STRUCTURE_CHANGE);
3958 if (!GST_BIN_IS_NO_RESYNC (bin))
3959 bin->priv->structure_cookie++;
3961 GST_OBJECT_UNLOCK (bin);
3964 gst_message_unref (message);
3968 case GST_MESSAGE_NEED_CONTEXT:{
3969 const gchar *context_type;
3970 GList *l, *contexts;
3972 gst_message_parse_context_type (message, &context_type);
3975 GST_OBJECT_LOCK (bin);
3976 contexts = GST_ELEMENT_CAST (bin)->contexts;
3977 GST_LOG_OBJECT (bin, "got need-context message type: %s", context_type);
3978 for (l = contexts; l; l = l->next) {
3979 GstContext *tmp = l->data;
3980 const gchar *tmp_type = gst_context_get_context_type (tmp);
3982 if (strcmp (context_type, tmp_type) == 0) {
3983 gst_element_set_context (GST_ELEMENT (src), l->data);
3987 GST_OBJECT_UNLOCK (bin);
3989 /* Forward if we couldn't answer the message */
3993 gst_message_unref (message);
3997 ("Got need-context message in bin '%s' without source element, dropping",
3998 GST_ELEMENT_NAME (bin));
3999 gst_message_unref (message);
4004 case GST_MESSAGE_HAVE_CONTEXT:{
4005 GstContext *context;
4007 gst_message_parse_have_context (message, &context);
4008 gst_bin_update_context (bin, context);
4009 gst_context_unref (context);
4021 /* Send all other messages upward */
4022 GST_DEBUG_OBJECT (bin, "posting message upward");
4023 gst_element_post_message (GST_ELEMENT_CAST (bin), message);
4028 /* generic struct passed to all query fold methods */
4037 typedef void (*QueryInitFunction) (GstBin * bin, QueryFold * fold);
4038 typedef void (*QueryDoneFunction) (GstBin * bin, QueryFold * fold);
4040 /* for duration/position we collect all durations/positions and take
4041 * the MAX of all valid results */
4043 bin_query_min_max_init (GstBin * bin, QueryFold * fold)
4051 bin_query_duration_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4053 gboolean res = FALSE;
4054 GstObject *item = g_value_get_object (vitem);
4055 if (GST_IS_PAD (item))
4056 res = gst_pad_query (GST_PAD (item), fold->query);
4058 res = gst_element_query (GST_ELEMENT (item), fold->query);
4063 g_value_set_boolean (ret, TRUE);
4065 gst_query_parse_duration (fold->query, NULL, &duration);
4067 GST_DEBUG_OBJECT (item, "got duration %" G_GINT64_FORMAT, duration);
4069 if (duration == -1) {
4070 /* duration query succeeded, but duration is unknown */
4075 if (duration > fold->max)
4076 fold->max = duration;
4083 bin_query_duration_done (GstBin * bin, QueryFold * fold)
4087 gst_query_parse_duration (fold->query, &format, NULL);
4088 /* store max in query result */
4089 gst_query_set_duration (fold->query, format, fold->max);
4091 GST_DEBUG_OBJECT (bin, "max duration %" G_GINT64_FORMAT, fold->max);
4095 bin_query_position_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4097 gboolean res = FALSE;
4098 GstObject *item = g_value_get_object (vitem);
4099 if (GST_IS_PAD (item))
4100 res = gst_pad_query (GST_PAD (item), fold->query);
4102 res = gst_element_query (GST_ELEMENT (item), fold->query);
4107 g_value_set_boolean (ret, TRUE);
4109 gst_query_parse_position (fold->query, NULL, &position);
4111 GST_DEBUG_OBJECT (item, "got position %" G_GINT64_FORMAT, position);
4113 if (position > fold->max)
4114 fold->max = position;
4121 bin_query_position_done (GstBin * bin, QueryFold * fold)
4125 gst_query_parse_position (fold->query, &format, NULL);
4126 /* store max in query result */
4127 gst_query_set_position (fold->query, format, fold->max);
4129 GST_DEBUG_OBJECT (bin, "max position %" G_GINT64_FORMAT, fold->max);
4133 bin_query_latency_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);
4142 GstClockTime min, max;
4145 gst_query_parse_latency (fold->query, &live, &min, &max);
4147 GST_DEBUG_OBJECT (item,
4148 "got latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
4149 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
4151 /* for the combined latency we collect the MAX of all min latencies and
4152 * the MIN of all max latencies */
4154 if (min > fold->min)
4156 if (fold->max == -1)
4158 else if (max < fold->max)
4164 g_value_set_boolean (ret, FALSE);
4165 GST_DEBUG_OBJECT (item, "failed query");
4172 bin_query_latency_done (GstBin * bin, QueryFold * fold)
4174 /* store max in query result */
4175 gst_query_set_latency (fold->query, fold->live, fold->min, fold->max);
4177 GST_DEBUG_OBJECT (bin,
4178 "latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
4179 ", live %d", GST_TIME_ARGS (fold->min), GST_TIME_ARGS (fold->max),
4183 /* generic fold, return first valid result */
4185 bin_query_generic_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4187 gboolean res = FALSE;
4188 GstObject *item = g_value_get_object (vitem);
4189 if (GST_IS_PAD (item))
4190 res = gst_pad_query (GST_PAD (item), fold->query);
4192 res = gst_element_query (GST_ELEMENT (item), fold->query);
4194 g_value_set_boolean (ret, TRUE);
4195 GST_DEBUG_OBJECT (item, "answered query %p", fold->query);
4198 /* and stop as soon as we have a valid result */
4202 /* Perform a query iteration for the given bin. The query is stored in
4203 * QueryFold and iter should be either a GstPad iterator or a
4204 * GstElement iterator. */
4206 bin_iterate_fold (GstBin * bin, GstIterator * iter, QueryInitFunction fold_init,
4207 QueryDoneFunction fold_done, GstIteratorFoldFunction fold_func,
4208 QueryFold * fold_data, gboolean default_return)
4210 gboolean res = default_return;
4212 /* set the result of the query to FALSE initially */
4213 g_value_init (&ret, G_TYPE_BOOLEAN);
4214 g_value_set_boolean (&ret, res);
4217 GstIteratorResult ires;
4219 ires = gst_iterator_fold (iter, fold_func, &ret, fold_data);
4222 case GST_ITERATOR_RESYNC:
4223 gst_iterator_resync (iter);
4225 fold_init (bin, fold_data);
4226 g_value_set_boolean (&ret, res);
4228 case GST_ITERATOR_OK:
4229 case GST_ITERATOR_DONE:
4230 res = g_value_get_boolean (&ret);
4231 if (fold_done != NULL && res)
4232 fold_done (bin, fold_data);
4244 gst_bin_query (GstElement * element, GstQuery * query)
4246 GstBin *bin = GST_BIN_CAST (element);
4248 gboolean default_return = FALSE;
4249 gboolean res = FALSE;
4250 gboolean src_pads_query_result = FALSE;
4251 GstIteratorFoldFunction fold_func;
4252 QueryInitFunction fold_init = NULL;
4253 QueryDoneFunction fold_done = NULL;
4254 QueryFold fold_data;
4256 switch (GST_QUERY_TYPE (query)) {
4257 case GST_QUERY_DURATION:
4259 /* iterate and collect durations */
4260 fold_func = (GstIteratorFoldFunction) bin_query_duration_fold;
4261 fold_init = bin_query_min_max_init;
4262 fold_done = bin_query_duration_done;
4265 case GST_QUERY_POSITION:
4267 fold_func = (GstIteratorFoldFunction) bin_query_position_fold;
4268 fold_init = bin_query_min_max_init;
4269 fold_done = bin_query_position_done;
4272 case GST_QUERY_LATENCY:
4274 fold_func = (GstIteratorFoldFunction) bin_query_latency_fold;
4275 fold_init = bin_query_min_max_init;
4276 fold_done = bin_query_latency_done;
4277 default_return = TRUE;
4281 fold_func = (GstIteratorFoldFunction) bin_query_generic_fold;
4285 fold_data.query = query;
4287 iter = gst_bin_iterate_sinks (bin);
4288 GST_DEBUG_OBJECT (bin, "Sending query %p (type %s) to sink children",
4289 query, GST_QUERY_TYPE_NAME (query));
4292 fold_init (bin, &fold_data);
4295 bin_iterate_fold (bin, iter, fold_init, fold_done, fold_func, &fold_data,
4297 gst_iterator_free (iter);
4300 /* Query the source pads of the element */
4301 iter = gst_element_iterate_src_pads (element);
4302 src_pads_query_result =
4303 bin_iterate_fold (bin, iter, fold_init, fold_done, fold_func,
4304 &fold_data, default_return);
4305 gst_iterator_free (iter);
4307 if (src_pads_query_result)
4311 GST_DEBUG_OBJECT (bin, "query %p result %d", query, res);
4317 set_context (const GValue * item, gpointer user_data)
4319 GstElement *element = g_value_get_object (item);
4321 gst_element_set_context (element, user_data);
4325 gst_bin_set_context (GstElement * element, GstContext * context)
4328 GstIterator *children;
4330 g_return_if_fail (GST_IS_BIN (element));
4332 bin = GST_BIN (element);
4334 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
4336 children = gst_bin_iterate_elements (bin);
4337 while (gst_iterator_foreach (children, set_context,
4338 context) == GST_ITERATOR_RESYNC)
4339 gst_iterator_resync (children);
4340 gst_iterator_free (children);
4344 compare_name (const GValue * velement, const gchar * name)
4347 GstElement *element = g_value_get_object (velement);
4349 GST_OBJECT_LOCK (element);
4350 eq = strcmp (GST_ELEMENT_NAME (element), name);
4351 GST_OBJECT_UNLOCK (element);
4357 * gst_bin_get_by_name:
4359 * @name: the element name to search for
4361 * Gets the element with the given name from a bin. This
4362 * function recurses into child bins.
4364 * Returns: (transfer full) (nullable): the #GstElement with the given
4368 gst_bin_get_by_name (GstBin * bin, const gchar * name)
4370 GstIterator *children;
4371 GValue result = { 0, };
4372 GstElement *element;
4375 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4377 GST_CAT_INFO (GST_CAT_PARENTAGE, "[%s]: looking up child element %s",
4378 GST_ELEMENT_NAME (bin), name);
4380 children = gst_bin_iterate_recurse (bin);
4381 found = gst_iterator_find_custom (children,
4382 (GCompareFunc) compare_name, &result, (gpointer) name);
4383 gst_iterator_free (children);
4386 element = g_value_dup_object (&result);
4387 g_value_unset (&result);
4396 * gst_bin_get_by_name_recurse_up:
4398 * @name: the element name to search for
4400 * Gets the element with the given name from this bin. If the
4401 * element is not found, a recursion is performed on the parent bin.
4403 * Returns: (transfer full) (nullable): the #GstElement with the given
4407 gst_bin_get_by_name_recurse_up (GstBin * bin, const gchar * name)
4411 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4412 g_return_val_if_fail (name != NULL, NULL);
4414 result = gst_bin_get_by_name (bin, name);
4419 parent = gst_object_get_parent (GST_OBJECT_CAST (bin));
4421 if (GST_IS_BIN (parent)) {
4422 result = gst_bin_get_by_name_recurse_up (GST_BIN_CAST (parent), name);
4424 gst_object_unref (parent);
4432 compare_interface (const GValue * velement, GValue * interface)
4434 GstElement *element = g_value_get_object (velement);
4435 GType interface_type = (GType) g_value_get_pointer (interface);
4438 if (G_TYPE_CHECK_INSTANCE_TYPE (element, interface_type)) {
4447 * gst_bin_get_by_interface:
4449 * @iface: the #GType of an interface
4451 * Looks for an element inside the bin that implements the given
4452 * interface. If such an element is found, it returns the element.
4453 * You can cast this element to the given interface afterwards. If you want
4454 * all elements that implement the interface, use
4455 * gst_bin_iterate_all_by_interface(). This function recurses into child bins.
4457 * Returns: (transfer full) (nullable): A #GstElement inside the bin
4458 * implementing the interface
4461 gst_bin_get_by_interface (GstBin * bin, GType iface)
4463 GstIterator *children;
4464 GValue result = { 0, };
4465 GstElement *element;
4467 GValue viface = { 0, };
4469 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4470 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface), NULL);
4472 g_value_init (&viface, G_TYPE_POINTER);
4473 g_value_set_pointer (&viface, (gpointer) iface);
4475 children = gst_bin_iterate_recurse (bin);
4476 found = gst_iterator_find_custom (children, (GCompareFunc) compare_interface,
4478 gst_iterator_free (children);
4481 element = g_value_dup_object (&result);
4482 g_value_unset (&result);
4486 g_value_unset (&viface);
4492 * gst_bin_iterate_all_by_interface:
4494 * @iface: the #GType of an interface
4496 * Looks for all elements inside the bin that implements the given
4497 * interface. You can safely cast all returned elements to the given interface.
4498 * The function recurses inside child bins. The iterator will yield a series
4501 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
4502 * for all elements in the bin implementing the given interface
4505 gst_bin_iterate_all_by_interface (GstBin * bin, GType iface)
4507 GstIterator *children;
4508 GstIterator *result;
4509 GValue viface = { 0, };
4511 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4512 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface), NULL);
4514 g_value_init (&viface, G_TYPE_POINTER);
4515 g_value_set_pointer (&viface, (gpointer) iface);
4517 children = gst_bin_iterate_recurse (bin);
4518 result = gst_iterator_filter (children, (GCompareFunc) compare_interface,
4521 g_value_unset (&viface);
4527 compare_factory_names (const GValue * velement, GValue * factory_name_val)
4529 GstElement *element = g_value_get_object (velement);
4530 GstElementFactory *factory = gst_element_get_factory (element);
4531 const gchar *factory_name = g_value_get_string (factory_name_val);
4533 if (factory == NULL)
4536 return g_strcmp0 (GST_OBJECT_NAME (factory), factory_name);
4540 * gst_bin_iterate_all_by_element_factory_name:
4542 * @factory_name: (not nullable): the name of the #GstElementFactory
4544 * Looks for all elements inside the bin with the given element factory name.
4545 * The function recurses inside child bins. The iterator will yield a series of
4548 * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
4549 * for all elements in the bin with the given element factory name
4554 gst_bin_iterate_all_by_element_factory_name (GstBin * bin,
4555 const gchar * factory_name)
4557 GstIterator *children;
4558 GstIterator *result;
4559 GValue factory_name_val = G_VALUE_INIT;
4561 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4562 g_return_val_if_fail (factory_name && *factory_name, NULL);
4564 g_value_init (&factory_name_val, G_TYPE_STRING);
4565 g_value_set_string (&factory_name_val, factory_name);
4567 children = gst_bin_iterate_recurse (bin);
4568 result = gst_iterator_filter (children, (GCompareFunc) compare_factory_names,
4571 g_value_unset (&factory_name_val);