tools: Count argc after parsing GOption on Windows
[platform/upstream/gstreamer.git] / subprojects / gstreamer / gst / gstbin.c
1 /* GStreamer
2  *
3  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
4  *                    2004 Wim Taymans <wim.taymans@gmail.com>
5  *
6  * gstbin.c: GstBin container object and support code
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 /**
25  * SECTION:gstbin
26  * @title: GstBin
27  * @short_description: Base class and element that can contain other elements
28  *
29  * #GstBin is an element that can contain other #GstElement, allowing them to be
30  * managed as a group.
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.
34  *
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.
38  *
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().
41  *
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
45  * current bin.
46  *
47  * An iterator of elements in a bin can be retrieved with
48  * gst_bin_iterate_elements(). Various other iterators exist to retrieve the
49  * elements in a bin.
50  *
51  * gst_object_unref() is used to drop your reference to the bin.
52  *
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.
56  *
57  * A #GstBin internally intercepts every #GstMessage posted by its children and
58  * implements the following default behaviour for each of them:
59  *
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
62  * message upwards.
63  *
64  * * %GST_MESSAGE_SEGMENT_START: Just collected and never forwarded upwards.
65  *   The messages are used to decide when all elements have completed playback
66  *   of their segment.
67  *
68  * * %GST_MESSAGE_SEGMENT_DONE: Is posted by #GstBin when all elements that posted
69  *   a SEGMENT_START have posted a SEGMENT_DONE.
70  *
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
74  *   query.
75  *
76  *   Note that these messages can be posted before the bin is prerolled, in which
77  *   case the duration query might fail.
78  *
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.
81  *
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
84  *   of a (new) stream.
85  *
86  * * %GST_MESSAGE_CLOCK_LOST: This message is posted by an element when it
87  *   can no longer provide a clock.
88  *
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.
92  *
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
96  *   distribution.
97  *
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.
101  *
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.
105  *
106  *   This message is never sent to the application but is forwarded to
107  *   the parent of the bin.
108  *
109  * * OTHERS: posted upwards.
110  *
111  * A #GstBin implements the following default behaviour for answering to a
112  * #GstQuery:
113  *
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.
117  *
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,
120  *   the query fails.
121  *
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.
125  *
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.
129  *
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.
133  */
134
135 #include "gst_private.h"
136
137 #include "gstevent.h"
138 #include "gstbin.h"
139 #include "gstinfo.h"
140 #include "gsterror.h"
141
142 #include "gstutils.h"
143 #include "gstchildproxy.h"
144
145 GST_DEBUG_CATEGORY_STATIC (bin_debug);
146 #define GST_CAT_DEFAULT bin_debug
147
148 /* a bin is toplevel if it has no parent or when it is configured to behave like
149  * a toplevel bin */
150 #define BIN_IS_TOPLEVEL(bin) ((GST_OBJECT_PARENT (bin) == NULL) || bin->priv->asynchandling)
151
152 struct _GstBinPrivate
153 {
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;
161
162   guint32 structure_cookie;
163
164 #if 0
165   /* cached index */
166   GstIndex *index;
167 #endif
168
169   /* forward messages from our children */
170   gboolean message_forward;
171
172   gboolean posted_eos;
173   gboolean posted_playing;
174   GstElementFlags suppressed_flags;
175 };
176
177 typedef struct
178 {
179   guint32 cookie;
180   GstState pending;
181 } BinContinueData;
182
183 static void gst_bin_dispose (GObject * object);
184
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);
189
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);
200
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);
210
211 #if 0
212 static void gst_bin_set_index_func (GstElement * element, GstIndex * index);
213 static GstIndex *gst_bin_get_index_func (GstElement * element);
214 #endif
215
216 static GstClock *gst_bin_provide_clock_func (GstElement * element);
217 static gboolean gst_bin_set_clock_func (GstElement * element, GstClock * clock);
218
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);
225
226 static gboolean gst_bin_do_latency_func (GstBin * bin);
227
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);
233
234 static GstIterator *gst_bin_sort_iterator_new (GstBin * bin);
235
236 /* Bin signals and properties */
237 enum
238 {
239   ELEMENT_ADDED,
240   ELEMENT_REMOVED,
241   DO_LATENCY,
242   DEEP_ELEMENT_ADDED,
243   DEEP_ELEMENT_REMOVED,
244   LAST_SIGNAL
245 };
246
247 #define DEFAULT_ASYNC_HANDLING  FALSE
248 #define DEFAULT_MESSAGE_FORWARD FALSE
249
250 enum
251 {
252   PROP_0,
253   PROP_ASYNC_HANDLING,
254   PROP_MESSAGE_FORWARD,
255   PROP_LAST
256 };
257
258 static void gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data);
259
260 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
261
262 #define _do_init \
263 { \
264   static const GInterfaceInfo iface_info = { \
265     gst_bin_child_proxy_init, \
266     NULL, \
267     NULL}; \
268   \
269   g_type_add_interface_static (g_define_type_id, GST_TYPE_CHILD_PROXY, &iface_info); \
270   \
271   GST_DEBUG_CATEGORY_INIT (bin_debug, "bin", GST_DEBUG_BOLD, \
272       "debugging info for the 'bin' container element"); \
273   \
274 }
275
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)
279     _do_init);
280
281 static GObject *
282 gst_bin_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
283     guint index)
284 {
285   GstObject *res;
286   GstBin *bin;
287
288   bin = GST_BIN_CAST (child_proxy);
289
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);
294
295   return (GObject *) res;
296 }
297
298 static guint
299 gst_bin_child_proxy_get_children_count (GstChildProxy * child_proxy)
300 {
301   guint num;
302   GstBin *bin;
303
304   bin = GST_BIN_CAST (child_proxy);
305
306   GST_OBJECT_LOCK (bin);
307   num = bin->numchildren;
308   GST_OBJECT_UNLOCK (bin);
309
310   return num;
311 }
312
313 static void
314 gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data)
315 {
316   GstChildProxyInterface *iface = g_iface;
317
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;
320 }
321
322 static gboolean
323 _gst_boolean_accumulator (GSignalInvocationHint * ihint,
324     GValue * return_accu, const GValue * handler_return, gpointer dummy)
325 {
326   gboolean myboolean;
327
328   myboolean = g_value_get_boolean (handler_return);
329   g_value_set_boolean (return_accu, myboolean);
330
331   GST_DEBUG ("invocation %d, %d", ihint->run_type, myboolean);
332
333   /* stop emission */
334   return FALSE;
335 }
336
337 static void
338 gst_bin_class_init (GstBinClass * klass)
339 {
340   GObjectClass *gobject_class;
341   GstElementClass *gstelement_class;
342
343   gobject_class = (GObjectClass *) klass;
344   gstelement_class = (GstElementClass *) klass;
345
346   gobject_class->set_property = gst_bin_set_property;
347   gobject_class->get_property = gst_bin_get_property;
348
349   /**
350    * GstBin:async-handling:
351    *
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.
355    */
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));
360
361   /**
362    * GstBin::element-added:
363    * @self: the #GstBin
364    * @element: the #GstElement that was added to the bin
365    *
366    * Will be emitted after the element was added to the bin.
367    */
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);
372   /**
373    * GstBin::element-removed:
374    * @self: the #GstBin
375    * @element: the #GstElement that was removed from the bin
376    *
377    * Will be emitted after the element was removed from the bin.
378    */
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);
383   /**
384    * GstBin::deep-element-added:
385    * @self: the #GstBin
386    * @sub_bin: the #GstBin the element was added to
387    * @element: the #GstElement that was added to @sub_bin
388    *
389    * Will be emitted after the element was added to @sub_bin.
390    *
391    * Since: 1.10
392    */
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);
397   /**
398    * GstBin::deep-element-removed:
399    * @self: the #GstBin
400    * @sub_bin: the #GstBin the element was removed from
401    * @element: the #GstElement that was removed from @sub_bin
402    *
403    * Will be emitted after the element was removed from @sub_bin.
404    *
405    * Since: 1.10
406    */
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);
411   /**
412    * GstBin::do-latency:
413    * @self: the #GstBin
414    *
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
417    * enabled.
418    *
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.
422    *
423    * Connect to this signal if the default latency calculations are not
424    * sufficient, like when you need different latencies for different sinks in
425    * the same pipeline.
426    */
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);
431
432   /**
433    * GstBin:message-forward:
434    *
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.
438    *
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.
442    */
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));
447
448   gobject_class->dispose = gst_bin_dispose;
449
450   gst_element_class_set_static_metadata (gstelement_class, "Generic bin",
451       "Generic/Bin",
452       "Simple container object",
453       "Erik Walthinsen <omega@cse.ogi.edu>,"
454       "Wim Taymans <wim.taymans@gmail.com>");
455
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);
460 #if 0
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);
463 #endif
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);
467
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);
471
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);
475
476   klass->deep_element_added = gst_bin_deep_element_added_func;
477   klass->deep_element_removed = gst_bin_deep_element_removed_func;
478
479   klass->do_latency = GST_DEBUG_FUNCPTR (gst_bin_do_latency_func);
480 }
481
482 static void
483 gst_bin_init (GstBin * bin)
484 {
485   GstBus *bus;
486
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;
493
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",
499       bus);
500   gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bin_bus_handler, bin,
501       NULL);
502
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;
507 }
508
509 static void
510 gst_bin_dispose (GObject * object)
511 {
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;
516
517   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object, "%p dispose", object);
518
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);
525
526   while (bin->children) {
527     gst_bin_remove (bin, GST_ELEMENT_CAST (bin->children->data));
528   }
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)));
532   }
533
534   G_OBJECT_CLASS (parent_class)->dispose (object);
535 }
536
537 /**
538  * gst_bin_new:
539  * @name: (allow-none): the name of the new bin
540  *
541  * Creates a new bin with the given name.
542  *
543  * Returns: (transfer floating): a new #GstBin
544  */
545 GstElement *
546 gst_bin_new (const gchar * name)
547 {
548   return gst_element_factory_make ("bin", name);
549 }
550
551 static void
552 gst_bin_set_property (GObject * object, guint prop_id,
553     const GValue * value, GParamSpec * pspec)
554 {
555   GstBin *gstbin;
556
557   gstbin = GST_BIN_CAST (object);
558
559   switch (prop_id) {
560     case PROP_ASYNC_HANDLING:
561       GST_OBJECT_LOCK (gstbin);
562       gstbin->priv->asynchandling = g_value_get_boolean (value);
563       GST_OBJECT_UNLOCK (gstbin);
564       break;
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);
569       break;
570     default:
571       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
572       break;
573   }
574 }
575
576 static void
577 gst_bin_get_property (GObject * object, guint prop_id,
578     GValue * value, GParamSpec * pspec)
579 {
580   GstBin *gstbin;
581
582   gstbin = GST_BIN_CAST (object);
583
584   switch (prop_id) {
585     case PROP_ASYNC_HANDLING:
586       GST_OBJECT_LOCK (gstbin);
587       g_value_set_boolean (value, gstbin->priv->asynchandling);
588       GST_OBJECT_UNLOCK (gstbin);
589       break;
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);
594       break;
595     default:
596       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
597       break;
598   }
599 }
600
601 #if 0
602 /* return the cached index */
603 static GstIndex *
604 gst_bin_get_index_func (GstElement * element)
605 {
606   GstBin *bin;
607   GstIndex *result;
608
609   bin = GST_BIN_CAST (element);
610
611   GST_OBJECT_LOCK (bin);
612   if ((result = bin->priv->index))
613     gst_object_ref (result);
614   GST_OBJECT_UNLOCK (bin);
615
616   return result;
617 }
618
619 /* set the index on all elements in this bin
620  *
621  * MT safe
622  */
623 static void
624 gst_bin_set_index_func (GstElement * element, GstIndex * index)
625 {
626   GstBin *bin;
627   gboolean done;
628   GstIterator *it;
629   GstIndex *old;
630   GValue data = { 0, };
631
632   bin = GST_BIN_CAST (element);
633
634   GST_OBJECT_LOCK (bin);
635   old = bin->priv->index;
636   if (G_UNLIKELY (old == index))
637     goto was_set;
638   if (index)
639     gst_object_ref (index);
640   bin->priv->index = index;
641   GST_OBJECT_UNLOCK (bin);
642
643   if (old)
644     gst_object_unref (old);
645
646   it = gst_bin_iterate_elements (bin);
647
648   /* set the index on all elements in the bin */
649   done = FALSE;
650   while (!done) {
651     switch (gst_iterator_next (it, &data)) {
652       case GST_ITERATOR_OK:
653       {
654         GstElement *child = g_value_get_object (&data);
655
656         GST_DEBUG_OBJECT (bin, "setting index on '%s'",
657             GST_ELEMENT_NAME (child));
658         gst_element_set_index (child, index);
659
660         g_value_reset (&data);
661         break;
662       }
663       case GST_ITERATOR_RESYNC:
664         GST_DEBUG_OBJECT (bin, "iterator doing resync");
665         gst_iterator_resync (it);
666         break;
667       default:
668       case GST_ITERATOR_DONE:
669         GST_DEBUG_OBJECT (bin, "iterator done");
670         done = TRUE;
671         break;
672     }
673   }
674   g_value_unset (&data);
675   gst_iterator_free (it);
676   return;
677
678 was_set:
679   {
680     GST_DEBUG_OBJECT (bin, "index was already set");
681     GST_OBJECT_UNLOCK (bin);
682     return;
683   }
684 }
685 #endif
686
687 /* set the clock on all elements in this bin
688  *
689  * MT safe
690  */
691 static gboolean
692 gst_bin_set_clock_func (GstElement * element, GstClock * clock)
693 {
694   GstBin *bin;
695   gboolean done;
696   GstIterator *it;
697   gboolean res = TRUE;
698   GValue data = { 0, };
699
700   bin = GST_BIN_CAST (element);
701
702   it = gst_bin_iterate_elements (bin);
703
704   done = FALSE;
705   while (!done) {
706     switch (gst_iterator_next (it, &data)) {
707       case GST_ITERATOR_OK:
708       {
709         GstElement *child = g_value_get_object (&data);
710
711         res &= gst_element_set_clock (child, clock);
712
713         g_value_reset (&data);
714         break;
715       }
716       case GST_ITERATOR_RESYNC:
717         GST_DEBUG_OBJECT (bin, "iterator doing resync");
718         gst_iterator_resync (it);
719         res = TRUE;
720         break;
721       default:
722       case GST_ITERATOR_DONE:
723         GST_DEBUG_OBJECT (bin, "iterator done");
724         done = TRUE;
725         break;
726     }
727   }
728   g_value_unset (&data);
729   gst_iterator_free (it);
730
731   if (res)
732     res = GST_ELEMENT_CLASS (parent_class)->set_clock (element, clock);
733
734   return res;
735 }
736
737 /* get the clock for this bin by asking all of the children in this bin
738  *
739  * The ref of the returned clock in increased so unref after usage.
740  *
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.
743  *
744  * MT safe
745  */
746 static GstClock *
747 gst_bin_provide_clock_func (GstElement * element)
748 {
749   GstClock *result = NULL;
750   GstElement *provider = NULL;
751   GstBin *bin;
752   GstIterator *it;
753   gboolean done;
754   GValue val = { 0, };
755   GstClock **provided_clock_p;
756   GstElement **clock_provider_p;
757
758   bin = GST_BIN_CAST (element);
759
760   GST_OBJECT_LOCK (bin);
761   if (!bin->clock_dirty)
762     goto not_dirty;
763
764   GST_DEBUG_OBJECT (bin, "finding new clock");
765
766   it = gst_bin_sort_iterator_new (bin);
767   GST_OBJECT_UNLOCK (bin);
768
769   done = FALSE;
770   while (!done) {
771     switch (gst_iterator_next (it, &val)) {
772       case GST_ITERATOR_OK:
773       {
774         GstElement *child = g_value_get_object (&val);
775         GstClock *clock;
776
777         clock = gst_element_provide_clock (child);
778         if (clock) {
779           GST_DEBUG_OBJECT (bin, "found candidate clock %p by element %s",
780               clock, GST_ELEMENT_NAME (child));
781           if (result) {
782             gst_object_unref (result);
783             gst_object_unref (provider);
784           }
785           result = clock;
786           provider = gst_object_ref (child);
787         }
788
789         g_value_reset (&val);
790         break;
791       }
792       case GST_ITERATOR_RESYNC:
793         gst_iterator_resync (it);
794         break;
795       default:
796       case GST_ITERATOR_DONE:
797         done = TRUE;
798         break;
799     }
800   }
801   g_value_unset (&val);
802   gst_iterator_free (it);
803
804   GST_OBJECT_LOCK (bin);
805   if (!bin->clock_dirty) {
806     if (provider)
807       gst_object_unref (provider);
808     if (result)
809       gst_object_unref (result);
810     result = NULL;
811
812     goto not_dirty;
813   }
814
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,
822       result, provider);
823   /* Provider is not being returned to caller, just the result */
824   if (provider)
825     gst_object_unref (provider);
826   GST_OBJECT_UNLOCK (bin);
827
828   return result;
829
830 not_dirty:
831   {
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);
836
837     return result;
838   }
839 }
840
841 /*
842  * functions for manipulating cached messages
843  */
844 typedef struct
845 {
846   GstObject *src;
847   GstMessageType types;
848 } MessageFind;
849
850 /* check if a message is of given src and type */
851 static gint
852 message_check (GstMessage * message, MessageFind * target)
853 {
854   gboolean eq = TRUE;
855
856   if (target->src)
857     eq &= GST_MESSAGE_SRC (message) == target->src;
858   if (target->types)
859     eq &= (GST_MESSAGE_TYPE (message) & target->types) != 0;
860   GST_LOG ("looking at message %p: %d", message, eq);
861
862   return (eq ? 0 : 1);
863 }
864
865 static GList *
866 find_message (GstBin * bin, GstObject * src, GstMessageType types)
867 {
868   GList *result;
869   MessageFind find;
870
871   find.src = src;
872   find.types = types;
873
874   result = g_list_find_custom (bin->messages, &find,
875       (GCompareFunc) message_check);
876
877   if (result) {
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),
880         types);
881   } else {
882     GST_DEBUG_OBJECT (bin, "no message found matching types %08x", types);
883 #ifndef GST_DISABLE_GST_DEBUG
884     {
885       guint i;
886
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));
890     }
891 #endif
892   }
893
894   return result;
895 }
896
897 /* with LOCK, returns TRUE if message had a valid SRC, takes ownership of
898  * the message.
899  *
900  * A message that is cached and has the same SRC and type is replaced
901  * by the given message.
902  */
903 static gboolean
904 bin_replace_message (GstBin * bin, GstMessage * message, GstMessageType types)
905 {
906   GList *previous;
907   GstObject *src;
908   gboolean res = TRUE;
909
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;
914
915       /* if we found a previous message, replace it */
916       previous_msg = previous->data;
917       previous->data = message;
918
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));
922
923       gst_message_unref (previous_msg);
924     } else {
925       /* keep new message */
926       bin->messages = g_list_prepend (bin->messages, message);
927
928       GST_DEBUG_OBJECT (bin, "got new message %p, %s from %s",
929           message, GST_MESSAGE_TYPE_NAME (message), GST_ELEMENT_NAME (src));
930     }
931   } else {
932     GST_DEBUG_OBJECT (bin, "got message %s from (NULL), not processing",
933         GST_MESSAGE_TYPE_NAME (message));
934     res = FALSE;
935     gst_message_unref (message);
936   }
937   return res;
938 }
939
940 /* with LOCK. Remove all messages of given types */
941 static void
942 bin_remove_messages (GstBin * bin, GstObject * src, GstMessageType types)
943 {
944   MessageFind find;
945   GList *walk, *next;
946
947   find.src = src;
948   find.types = types;
949
950   for (walk = bin->messages; walk; walk = next) {
951     GstMessage *message = (GstMessage *) walk->data;
952
953     next = g_list_next (walk);
954
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);
961     } else {
962       GST_DEBUG_OBJECT (GST_MESSAGE_SRC (message),
963           "not deleting message %p of type 0x%08x", message,
964           GST_MESSAGE_TYPE (message));
965     }
966   }
967 }
968
969
970 /* Check if the bin is EOS. We do this by scanning all sinks and
971  * checking if they posted an EOS message.
972  *
973  * call with bin LOCK */
974 static gboolean
975 is_eos (GstBin * bin, guint32 * seqnum)
976 {
977   gboolean result;
978   gint n_eos = 0;
979   GList *walk, *msgs;
980
981   result = TRUE;
982   for (walk = bin->children; walk; walk = g_list_next (walk)) {
983     GstElement *element;
984
985     element = GST_ELEMENT_CAST (walk->data);
986     if (bin_element_is_sink (element, bin) == 0) {
987       /* check if element posted EOS */
988       if ((msgs =
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));
992         n_eos++;
993       } else {
994         GST_DEBUG ("sink '%s' did not post EOS yet",
995             GST_ELEMENT_NAME (element));
996         result = FALSE;
997         break;
998       }
999     }
1000   }
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.
1006    */
1007   return result && n_eos > 0;
1008 }
1009
1010
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.
1013  *
1014  * call with bin LOCK */
1015 static gboolean
1016 is_stream_start (GstBin * bin, guint32 * seqnum, gboolean * have_group_id,
1017     guint * group_id)
1018 {
1019   gboolean result;
1020   GList *walk, *msgs;
1021   guint tmp_group_id;
1022   gboolean first_stream_start = TRUE, first_group_id = TRUE;
1023   gboolean same_group_id = TRUE;
1024
1025   *have_group_id = FALSE;
1026   *group_id = 0;
1027   result = FALSE;
1028   for (walk = bin->children; walk; walk = g_list_next (walk)) {
1029     GstElement *element;
1030
1031     element = GST_ELEMENT_CAST (walk->data);
1032     if (bin_element_is_sink (element, bin) == 0) {
1033       /* check if element posted STREAM_START */
1034       if ((msgs =
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 */
1045           result = TRUE;
1046           first_stream_start = FALSE;
1047         }
1048
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),
1052                 &tmp_group_id)) {
1053           if (first_group_id) {
1054             first_group_id = FALSE;
1055             *group_id = tmp_group_id;
1056           } else {
1057             if (tmp_group_id != *group_id)
1058               same_group_id = FALSE;
1059           }
1060         } else {
1061           *have_group_id = FALSE;
1062         }
1063       } else {
1064         GST_DEBUG ("sink '%s' did not post STREAM_START yet",
1065             GST_ELEMENT_NAME (element));
1066         result = FALSE;
1067         break;
1068       }
1069     }
1070   }
1071
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
1074    * message */
1075   if (*have_group_id)
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! */
1080   return result;
1081 }
1082
1083 static void
1084 unlink_pads (const GValue * item, gpointer user_data)
1085 {
1086   GstPad *pad;
1087   GstPad *peer;
1088
1089   pad = g_value_get_object (item);
1090
1091   if ((peer = gst_pad_get_peer (pad))) {
1092     if (gst_pad_get_direction (pad) == GST_PAD_SRC)
1093       gst_pad_unlink (pad, peer);
1094     else
1095       gst_pad_unlink (peer, pad);
1096     gst_object_unref (peer);
1097   }
1098 }
1099
1100 static void
1101 bin_deep_iterator_foreach (const GValue * item, gpointer user_data)
1102 {
1103   GQueue *queue = user_data;
1104
1105   g_queue_push_tail (queue, g_value_dup_object (item));
1106 }
1107
1108 static void
1109 gst_bin_do_deep_add_remove (GstBin * bin, gint sig_id, const gchar * sig_name,
1110     GstElement * element)
1111 {
1112   g_signal_emit (bin, sig_id, 0, bin, element);
1113
1114   /* When removing a bin, emit deep-element-* for everything in the bin too */
1115   if (GST_IS_BIN (element)) {
1116     GstIterator *it;
1117     GstIteratorResult ires;
1118     GQueue elements = G_QUEUE_INIT;
1119
1120     GST_LOG_OBJECT (bin, "Recursing into bin %" GST_PTR_FORMAT " for %s",
1121         element, sig_name);
1122     it = gst_bin_iterate_recurse (GST_BIN_CAST (element));
1123     do {
1124       ires = gst_iterator_foreach (it, bin_deep_iterator_foreach, &elements);
1125       if (ires != GST_ITERATOR_DONE) {
1126         g_queue_clear_full (&elements, (GDestroyNotify) gst_object_unref);
1127       }
1128       if (ires == GST_ITERATOR_RESYNC)
1129         gst_iterator_resync (it);
1130     } while (ires == GST_ITERATOR_RESYNC);
1131     if (ires != GST_ITERATOR_ERROR) {
1132       GstElement *e;
1133
1134       while ((e = g_queue_pop_head (&elements))) {
1135         GstObject *parent = gst_object_get_parent (GST_OBJECT_CAST (e));
1136
1137         /* an element could have removed some of its internal elements
1138          * meanwhile, so protect against that */
1139         if (parent) {
1140           GST_LOG_OBJECT (bin, "calling %s for element %" GST_PTR_FORMAT
1141               " in bin %" GST_PTR_FORMAT, sig_name, e, parent);
1142           g_signal_emit (bin, sig_id, 0, parent, e);
1143           gst_object_unref (parent);
1144         }
1145
1146         gst_object_unref (e);
1147       }
1148     }
1149     gst_iterator_free (it);
1150   }
1151 }
1152
1153 /* vmethod that adds an element to a bin
1154  *
1155  * MT safe
1156  */
1157 static gboolean
1158 gst_bin_add_func (GstBin * bin, GstElement * element)
1159 {
1160   gchar *elem_name;
1161   GstIterator *it;
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;
1166
1167   GST_DEBUG_OBJECT (bin, "element :%s", GST_ELEMENT_NAME (element));
1168
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);
1174   provides_clock =
1175       GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1176   requires_clock =
1177       GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1178   GST_OBJECT_UNLOCK (element);
1179
1180   GST_OBJECT_LOCK (bin);
1181
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;
1188
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))))
1192     goto had_parent;
1193
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",
1197         elem_name);
1198     GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SINK);
1199   }
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",
1202         elem_name);
1203     GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_SOURCE);
1204   }
1205   if (provides_clock
1206       && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_PROVIDE_CLOCK)) {
1207     GST_DEBUG_OBJECT (bin, "element \"%s\" can provide a clock", elem_name);
1208     clock_message =
1209         gst_message_new_clock_provide (GST_OBJECT_CAST (element), NULL, TRUE);
1210     GST_OBJECT_FLAG_SET (bin, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1211   }
1212   if (requires_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);
1216   }
1217
1218   bin->children = g_list_prepend (bin->children, element);
1219   bin->numchildren++;
1220   bin->children_cookie++;
1221   if (!GST_BIN_IS_NO_RESYNC (bin))
1222     bin->priv->structure_cookie++;
1223
1224   /* distribute the bus */
1225   gst_element_set_bus (element, bin->child_bus);
1226
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));
1234
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);
1239
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;
1244
1245     context_type = gst_context_get_context_type (context);
1246
1247     /* we already set this context above? */
1248     replacement =
1249         gst_element_get_context_unlocked (GST_ELEMENT (bin), context_type);
1250     if (replacement) {
1251       gst_context_unref (replacement);
1252     } else {
1253       GstMessage *msg;
1254       GstStructure *s;
1255
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);
1260
1261       need_context_messages = g_list_prepend (need_context_messages, msg);
1262     }
1263   }
1264
1265 #if 0
1266   /* set the cached index on the children */
1267   if (bin->priv->index)
1268     gst_element_set_index (element, bin->priv->index);
1269 #endif
1270
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;
1275
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));
1281
1282   switch (ret) {
1283     case GST_STATE_CHANGE_ASYNC:
1284     {
1285       /* create message to track this aync element when it posts an async-done
1286        * message */
1287       async_message = gst_message_new_async_start (GST_OBJECT_CAST (element));
1288       break;
1289     }
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);
1293       break;
1294     case GST_STATE_CHANGE_FAILURE:
1295       break;
1296     default:
1297       break;
1298   }
1299
1300 no_state_recalc:
1301   GST_OBJECT_UNLOCK (bin);
1302
1303   for (l = need_context_messages; l; l = l->next) {
1304     GstMessage *msg = l->data;
1305     GstStructure *s;
1306     const gchar *context_type;
1307     GstContext *replacement, *context;
1308
1309     gst_message_parse_context_type (msg, &context_type);
1310
1311     GST_LOG_OBJECT (bin, "asking parent for context type: %s "
1312         "from %" GST_PTR_FORMAT, context_type, element);
1313
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));
1319
1320     /* lock to avoid losing a potential write */
1321     GST_OBJECT_LOCK (bin);
1322     replacement =
1323         gst_element_get_context_unlocked (GST_ELEMENT_CAST (bin), context_type);
1324     gst_message_unref (msg);
1325
1326     if (replacement) {
1327       /* we got the context set from GstElement::set_context */
1328       gst_context_unref (replacement);
1329       GST_OBJECT_UNLOCK (bin);
1330     } else {
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);
1334
1335       gst_bin_update_context_unlocked (bin, context);
1336
1337       msg =
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);
1342     }
1343     gst_context_unref (context);
1344   }
1345   g_list_free_full (elem_contexts, (GDestroyNotify) gst_context_unref);
1346   g_list_free (need_context_messages);
1347
1348   /* post the messages on the bus of the element so that the bin can handle
1349    * them */
1350   if (clock_message)
1351     gst_element_post_message (element, clock_message);
1352
1353   if (async_message)
1354     gst_element_post_message (element, async_message);
1355
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);
1362
1363   GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "added element \"%s\"",
1364       elem_name);
1365
1366   g_signal_emit (bin, gst_bin_signals[ELEMENT_ADDED], 0, element);
1367   gst_child_proxy_child_added ((GstChildProxy *) bin, (GObject *) element,
1368       elem_name);
1369
1370   gst_bin_do_deep_add_remove (bin, gst_bin_signals[DEEP_ELEMENT_ADDED],
1371       "deep-element-added", element);
1372
1373   g_free (elem_name);
1374
1375   return TRUE;
1376
1377   /* ERROR handling here */
1378 duplicate_name:
1379   {
1380     GST_OBJECT_UNLOCK (bin);
1381     GST_WARNING_OBJECT (bin, "Name '%s' is not unique in bin, not adding",
1382         elem_name);
1383     g_free (elem_name);
1384     gst_object_ref_sink (element);
1385     gst_object_unref (element);
1386     return FALSE;
1387   }
1388 had_parent:
1389   {
1390     GST_OBJECT_UNLOCK (bin);
1391     GST_WARNING_OBJECT (bin, "Element '%s' already has parent", elem_name);
1392     g_free (elem_name);
1393     return FALSE;
1394   }
1395 }
1396
1397 /**
1398  * gst_bin_set_suppressed_flags:
1399  * @bin: a #GstBin
1400  * @flags: the #GstElementFlags to suppress
1401  *
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.
1406  *
1407  * Since: 1.10
1408  */
1409 void
1410 gst_bin_set_suppressed_flags (GstBin * bin, GstElementFlags flags)
1411 {
1412   g_return_if_fail (GST_IS_BIN (bin));
1413
1414   GST_OBJECT_LOCK (bin);
1415   bin->priv->suppressed_flags = bin->priv->suppressed_flags | flags;
1416   GST_OBJECT_UNLOCK (bin);
1417
1418   GST_DEBUG_OBJECT (bin, "Set suppressed flags(0x%x) to bin '%s'", flags,
1419       GST_ELEMENT_NAME (bin));
1420 }
1421
1422 /**
1423  * gst_bin_get_suppressed_flags:
1424  * @bin: a #GstBin
1425  *
1426  * Returns: the bin's suppressed #GstElementFlags.
1427  *
1428  * Since: 1.10
1429  */
1430 GstElementFlags
1431 gst_bin_get_suppressed_flags (GstBin * bin)
1432 {
1433   GstElementFlags res;
1434
1435   g_return_val_if_fail (GST_IS_BIN (bin), 0);
1436
1437   GST_OBJECT_LOCK (bin);
1438   res = bin->priv->suppressed_flags;
1439   GST_OBJECT_UNLOCK (bin);
1440
1441   return res;
1442 }
1443
1444 /* signal vfunc, will be called when a new element was added */
1445 static void
1446 gst_bin_deep_element_added_func (GstBin * bin, GstBin * sub_bin,
1447     GstElement * child)
1448 {
1449   GstBin *parent_bin;
1450
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");
1454     return;
1455   }
1456
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,
1459       child, sub_bin);
1460
1461   g_signal_emit (parent_bin, gst_bin_signals[DEEP_ELEMENT_ADDED], 0, sub_bin,
1462       child);
1463
1464   gst_object_unref (parent_bin);
1465 }
1466
1467 /* signal vfunc, will be called when an element was removed */
1468 static void
1469 gst_bin_deep_element_removed_func (GstBin * bin, GstBin * sub_bin,
1470     GstElement * child)
1471 {
1472   GstBin *parent_bin;
1473
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");
1477     return;
1478   }
1479
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,
1482       child, sub_bin);
1483
1484   g_signal_emit (parent_bin, gst_bin_signals[DEEP_ELEMENT_REMOVED], 0, sub_bin,
1485       child);
1486
1487   gst_object_unref (parent_bin);
1488 }
1489
1490 /**
1491  * gst_bin_add:
1492  * @bin: a #GstBin
1493  * @element: (transfer floating): the #GstElement to add
1494  *
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.
1497  *
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.
1500  *
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.
1506  *
1507  * Returns: %TRUE if the element could be added, %FALSE if
1508  * the bin does not want to accept the element.
1509  */
1510 gboolean
1511 gst_bin_add (GstBin * bin, GstElement * element)
1512 {
1513   GstBinClass *bclass;
1514   gboolean result;
1515
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);
1519
1520   bclass = GST_BIN_GET_CLASS (bin);
1521
1522   if (G_UNLIKELY (bclass->add_element == NULL))
1523     goto no_function;
1524
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)));
1528
1529   GST_TRACER_BIN_ADD_PRE (bin, element);
1530   result = bclass->add_element (bin, element);
1531   GST_TRACER_BIN_ADD_POST (bin, element, result);
1532
1533   return result;
1534
1535   /* ERROR handling */
1536 no_function:
1537   {
1538     GST_WARNING_OBJECT (bin, "adding elements to bin is not supported");
1539     gst_object_ref_sink (element);
1540     gst_object_unref (element);
1541     return FALSE;
1542   }
1543 }
1544
1545 /* remove an element from the bin
1546  *
1547  * MT safe
1548  */
1549 static gboolean
1550 gst_bin_remove_func (GstBin * bin, GstElement * element)
1551 {
1552   gchar *elem_name;
1553   GstIterator *it;
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;
1559   GList *walk, *next;
1560   gboolean other_async, this_async, have_no_preroll, removed_eos;
1561   GstStateChangeReturn ret;
1562
1563   GST_DEBUG_OBJECT (bin, "element :%s", GST_ELEMENT_NAME (element));
1564
1565   GST_OBJECT_LOCK (bin);
1566
1567   GST_OBJECT_LOCK (element);
1568   elem_name = g_strdup (GST_ELEMENT_NAME (element));
1569
1570   if (GST_OBJECT_PARENT (element) != GST_OBJECT_CAST (bin)) {
1571     GST_OBJECT_UNLOCK (element);
1572     goto not_in_bin;
1573   }
1574
1575   /* remove the parent ref */
1576   GST_OBJECT_PARENT (element) = NULL;
1577
1578   /* grab element name so we can print it */
1579   is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
1580   is_source = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE);
1581   provides_clock =
1582       GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1583   requires_clock =
1584       GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1585   GST_OBJECT_UNLOCK (element);
1586
1587   found = FALSE;
1588   othersink = FALSE;
1589   othersource = FALSE;
1590   otherprovider = FALSE;
1591   otherrequirer = FALSE;
1592   have_no_preroll = FALSE;
1593   /* iterate the elements, we collect which ones are async and no_preroll. We
1594    * also remove the element when we find it. */
1595   for (walk = bin->children; walk; walk = next) {
1596     GstElement *child = GST_ELEMENT_CAST (walk->data);
1597
1598     next = g_list_next (walk);
1599
1600     if (child == element) {
1601       found = TRUE;
1602       /* remove the element */
1603       bin->children = g_list_delete_link (bin->children, walk);
1604     } else {
1605       gboolean child_sink, child_source, child_provider, child_requirer;
1606
1607       GST_OBJECT_LOCK (child);
1608       child_sink = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SINK);
1609       child_source = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SOURCE);
1610       child_provider =
1611           GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1612       child_requirer =
1613           GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1614       /* when we remove a sink, check if there are other sinks. */
1615       if (is_sink && !othersink && child_sink)
1616         othersink = TRUE;
1617       if (is_source && !othersource && child_source)
1618         othersource = TRUE;
1619       if (provides_clock && !otherprovider && child_provider)
1620         otherprovider = TRUE;
1621       if (requires_clock && !otherrequirer && child_requirer)
1622         otherrequirer = TRUE;
1623       /* check if we have NO_PREROLL children */
1624       if (GST_STATE_RETURN (child) == GST_STATE_CHANGE_NO_PREROLL)
1625         have_no_preroll = TRUE;
1626       GST_OBJECT_UNLOCK (child);
1627     }
1628   }
1629
1630   /* the element must have been in the bin's list of children */
1631   if (G_UNLIKELY (!found))
1632     goto not_in_bin;
1633
1634   /* we now removed the element from the list of elements, increment the cookie
1635    * so that others can detect a change in the children list. */
1636   bin->numchildren--;
1637   bin->children_cookie++;
1638   if (!GST_BIN_IS_NO_RESYNC (bin))
1639     bin->priv->structure_cookie++;
1640
1641   if (is_sink && !othersink
1642       && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_SINK)) {
1643     /* we're not a sink anymore */
1644     GST_DEBUG_OBJECT (bin, "we removed the last sink");
1645     GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_SINK);
1646   }
1647   if (is_source && !othersource
1648       && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_SOURCE)) {
1649     /* we're not a source anymore */
1650     GST_DEBUG_OBJECT (bin, "we removed the last source");
1651     GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_SOURCE);
1652   }
1653   if (provides_clock && !otherprovider
1654       && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_PROVIDE_CLOCK)) {
1655     /* we're not a clock provider anymore */
1656     GST_DEBUG_OBJECT (bin, "we removed the last clock provider");
1657     GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
1658   }
1659   if (requires_clock && !otherrequirer
1660       && !(bin->priv->suppressed_flags & GST_ELEMENT_FLAG_REQUIRE_CLOCK)) {
1661     /* we're not a clock requirer anymore */
1662     GST_DEBUG_OBJECT (bin, "we removed the last clock requirer");
1663     GST_OBJECT_FLAG_UNSET (bin, GST_ELEMENT_FLAG_REQUIRE_CLOCK);
1664   }
1665
1666   /* if the clock provider for this element is removed, we lost
1667    * the clock as well, we need to inform the parent of this
1668    * so that it can select a new clock */
1669   if (bin->clock_provider == element) {
1670     GST_DEBUG_OBJECT (bin, "element \"%s\" provided the clock", elem_name);
1671     bin->clock_dirty = TRUE;
1672     clock_message =
1673         gst_message_new_clock_lost (GST_OBJECT_CAST (bin), bin->provided_clock);
1674     provided_clock_p = &bin->provided_clock;
1675     clock_provider_p = &bin->clock_provider;
1676     gst_object_replace ((GstObject **) provided_clock_p, NULL);
1677     gst_object_replace ((GstObject **) clock_provider_p, NULL);
1678   }
1679
1680   /* remove messages for the element, if there was a pending ASYNC_START
1681    * message we must see if removing the element caused the bin to lose its
1682    * async state. */
1683   this_async = FALSE;
1684   other_async = FALSE;
1685   /* If we remove an EOSed element, the bin might go EOS */
1686   removed_eos = FALSE;
1687   for (walk = bin->messages; walk; walk = next) {
1688     GstMessage *message = (GstMessage *) walk->data;
1689     GstElement *src = GST_ELEMENT_CAST (GST_MESSAGE_SRC (message));
1690     gboolean remove;
1691
1692     next = g_list_next (walk);
1693     remove = FALSE;
1694
1695     switch (GST_MESSAGE_TYPE (message)) {
1696       case GST_MESSAGE_ASYNC_START:
1697         if (src == element)
1698           this_async = TRUE;
1699         else
1700           other_async = TRUE;
1701
1702         GST_DEBUG_OBJECT (src, "looking at message %p", message);
1703         break;
1704       case GST_MESSAGE_STRUCTURE_CHANGE:
1705       {
1706         GstElement *owner;
1707
1708         GST_DEBUG_OBJECT (src, "looking at structure change message %p",
1709             message);
1710         /* it's unlikely that this message is still in the list of messages
1711          * because this would mean that a link/unlink is busy in another thread
1712          * while we remove the element. We still have to remove the message
1713          * because we might not receive the done message anymore when the element
1714          * is removed from the bin. */
1715         gst_message_parse_structure_change (message, NULL, &owner, NULL);
1716         if (owner == element)
1717           remove = TRUE;
1718         break;
1719       }
1720       case GST_MESSAGE_EOS:
1721         if (src == element)
1722           removed_eos = TRUE;
1723         break;
1724       default:
1725         break;
1726     }
1727     if (src == element)
1728       remove = TRUE;
1729
1730     if (remove) {
1731       /* delete all message types */
1732       GST_DEBUG_OBJECT (src, "deleting message %p of element \"%s\"",
1733           message, elem_name);
1734       bin->messages = g_list_delete_link (bin->messages, walk);
1735       gst_message_unref (message);
1736     }
1737   }
1738
1739   /* get last return */
1740   ret = GST_STATE_RETURN (bin);
1741
1742   /* no need to update the state if we are in error */
1743   if (ret == GST_STATE_CHANGE_FAILURE)
1744     goto no_state_recalc;
1745
1746   if (!other_async && this_async) {
1747     /* all other elements were not async and we removed the async one,
1748      * handle the async-done case because we are not async anymore now. */
1749     GST_DEBUG_OBJECT (bin,
1750         "we removed the last async element, have no_preroll %d",
1751         have_no_preroll);
1752
1753     /* the current state return of the bin depends on if there are no_preroll
1754      * elements in the pipeline or not */
1755     if (have_no_preroll)
1756       ret = GST_STATE_CHANGE_NO_PREROLL;
1757     else
1758       ret = GST_STATE_CHANGE_SUCCESS;
1759
1760     bin_handle_async_done (bin, ret, FALSE, GST_CLOCK_TIME_NONE);
1761   } else {
1762     GST_DEBUG_OBJECT (bin,
1763         "recalc state preroll: %d, other async: %d, this async %d",
1764         have_no_preroll, other_async, this_async);
1765
1766     if (have_no_preroll) {
1767       ret = GST_STATE_CHANGE_NO_PREROLL;
1768     } else if (other_async) {
1769       /* there are other async elements and we were not doing an async state
1770        * change, change our pending state and go async */
1771       if (GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING) {
1772         GST_STATE_NEXT (bin) = GST_STATE (bin);
1773         GST_STATE_PENDING (bin) = GST_STATE (bin);
1774       }
1775       ret = GST_STATE_CHANGE_ASYNC;
1776     }
1777     GST_STATE_RETURN (bin) = ret;
1778   }
1779 no_state_recalc:
1780   /* clear bus */
1781   gst_element_set_bus (element, NULL);
1782   /* Clear the clock we provided to the element */
1783   gst_element_set_clock (element, NULL);
1784   GST_OBJECT_UNLOCK (bin);
1785
1786   /* If the element was a sink that had not posted EOS,
1787    * it might have been the last one we were waiting for,
1788    * so check if it's time to send EOS now */
1789   if (is_sink && !removed_eos) {
1790     GST_DEBUG_OBJECT (bin,
1791         "Removing sink that had not EOSed. Re-checking overall EOS status");
1792     bin_do_eos (bin);
1793   }
1794
1795   if (clock_message)
1796     gst_element_post_message (GST_ELEMENT_CAST (bin), clock_message);
1797
1798   /* unlink all linked pads */
1799   it = gst_element_iterate_pads (element);
1800   while (gst_iterator_foreach (it, (GstIteratorForeachFunction) unlink_pads,
1801           NULL) == GST_ITERATOR_RESYNC)
1802     gst_iterator_resync (it);
1803   gst_iterator_free (it);
1804
1805   GST_CAT_INFO_OBJECT (GST_CAT_PARENTAGE, bin, "removed child \"%s\"",
1806       elem_name);
1807
1808   g_signal_emit (bin, gst_bin_signals[ELEMENT_REMOVED], 0, element);
1809   gst_child_proxy_child_removed ((GstChildProxy *) bin, (GObject *) element,
1810       elem_name);
1811
1812   gst_bin_do_deep_add_remove (bin, gst_bin_signals[DEEP_ELEMENT_REMOVED],
1813       "deep-element-removed", element);
1814
1815   g_free (elem_name);
1816   /* element is really out of our control now */
1817   gst_object_unref (element);
1818
1819   return TRUE;
1820
1821   /* ERROR handling */
1822 not_in_bin:
1823   {
1824     GST_OBJECT_UNLOCK (bin);
1825     GST_WARNING_OBJECT (bin, "Element '%s' is not in bin", elem_name);
1826     g_free (elem_name);
1827     return FALSE;
1828   }
1829 }
1830
1831 /**
1832  * gst_bin_remove:
1833  * @bin: a #GstBin
1834  * @element: (transfer none): the #GstElement to remove
1835  *
1836  * Removes the element from the bin, unparenting it as well.
1837  * Unparenting the element means that the element will be dereferenced,
1838  * so if the bin holds the only reference to the element, the element
1839  * will be freed in the process of removing it from the bin.  If you
1840  * want the element to still exist after removing, you need to call
1841  * gst_object_ref() before removing it from the bin.
1842  *
1843  * If the element's pads are linked to other pads, the pads will be unlinked
1844  * before the element is removed from the bin.
1845  *
1846  * Returns: %TRUE if the element could be removed, %FALSE if
1847  * the bin does not want to remove the element.
1848  */
1849 gboolean
1850 gst_bin_remove (GstBin * bin, GstElement * element)
1851 {
1852   GstBinClass *bclass;
1853   gboolean result;
1854
1855   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1856   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1857   g_return_val_if_fail (GST_ELEMENT_CAST (bin) != element, FALSE);
1858
1859   bclass = GST_BIN_GET_CLASS (bin);
1860
1861   if (G_UNLIKELY (bclass->remove_element == NULL))
1862     goto no_function;
1863
1864   GST_CAT_DEBUG (GST_CAT_PARENTAGE, "removing element %s from bin %s",
1865       GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin));
1866
1867   GST_TRACER_BIN_REMOVE_PRE (bin, element);
1868   result = bclass->remove_element (bin, element);
1869   GST_TRACER_BIN_REMOVE_POST (bin, result);
1870
1871   return result;
1872
1873   /* ERROR handling */
1874 no_function:
1875   {
1876     GST_WARNING_OBJECT (bin, "Removing elements from bin is not supported");
1877     return FALSE;
1878   }
1879 }
1880
1881 /**
1882  * gst_bin_iterate_elements:
1883  * @bin: a #GstBin
1884  *
1885  * Gets an iterator for the elements in this bin.
1886  *
1887  * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
1888  */
1889 GstIterator *
1890 gst_bin_iterate_elements (GstBin * bin)
1891 {
1892   GstIterator *result;
1893
1894   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1895
1896   GST_OBJECT_LOCK (bin);
1897   result = gst_iterator_new_list (GST_TYPE_ELEMENT,
1898       GST_OBJECT_GET_LOCK (bin),
1899       &bin->children_cookie, &bin->children, (GObject *) bin, NULL);
1900   GST_OBJECT_UNLOCK (bin);
1901
1902   return result;
1903 }
1904
1905 static GstIteratorItem
1906 iterate_child_recurse (GstIterator * it, const GValue * item)
1907 {
1908   GstElement *child = g_value_get_object (item);
1909
1910   if (GST_IS_BIN (child)) {
1911     GstIterator *other = gst_bin_iterate_recurse (GST_BIN_CAST (child));
1912
1913     gst_iterator_push (it, other);
1914   }
1915   return GST_ITERATOR_ITEM_PASS;
1916 }
1917
1918 /**
1919  * gst_bin_iterate_recurse:
1920  * @bin: a #GstBin
1921  *
1922  * Gets an iterator for the elements in this bin.
1923  * This iterator recurses into GstBin children.
1924  *
1925  * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
1926  */
1927 GstIterator *
1928 gst_bin_iterate_recurse (GstBin * bin)
1929 {
1930   GstIterator *result;
1931
1932   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1933
1934   GST_OBJECT_LOCK (bin);
1935   result = gst_iterator_new_list (GST_TYPE_ELEMENT,
1936       GST_OBJECT_GET_LOCK (bin),
1937       &bin->children_cookie,
1938       &bin->children,
1939       (GObject *) bin, (GstIteratorItemFunction) iterate_child_recurse);
1940   GST_OBJECT_UNLOCK (bin);
1941
1942   return result;
1943 }
1944
1945 /* returns 0 when TRUE because this is a GCompareFunc */
1946 /* MT safe */
1947 static gint
1948 bin_element_is_sink (GstElement * child, GstBin * bin)
1949 {
1950   gboolean is_sink;
1951
1952   /* we lock the child here for the remainder of the function to
1953    * get its name and flag safely. */
1954   GST_OBJECT_LOCK (child);
1955   is_sink = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SINK);
1956
1957   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
1958       "child %s %s sink", GST_OBJECT_NAME (child), is_sink ? "is" : "is not");
1959
1960   GST_OBJECT_UNLOCK (child);
1961   return is_sink ? 0 : 1;
1962 }
1963
1964 static gint
1965 sink_iterator_filter (const GValue * vchild, GValue * vbin)
1966 {
1967   GstBin *bin = g_value_get_object (vbin);
1968   GstElement *child = g_value_get_object (vchild);
1969
1970   return (bin_element_is_sink (child, bin));
1971 }
1972
1973 /**
1974  * gst_bin_iterate_sinks:
1975  * @bin: a #GstBin
1976  *
1977  * Gets an iterator for all elements in the bin that have the
1978  * #GST_ELEMENT_FLAG_SINK flag set.
1979  *
1980  * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
1981  */
1982 GstIterator *
1983 gst_bin_iterate_sinks (GstBin * bin)
1984 {
1985   GstIterator *children;
1986   GstIterator *result;
1987   GValue vbin = { 0, };
1988
1989   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1990
1991   g_value_init (&vbin, GST_TYPE_BIN);
1992   g_value_set_object (&vbin, bin);
1993
1994   children = gst_bin_iterate_elements (bin);
1995   result = gst_iterator_filter (children,
1996       (GCompareFunc) sink_iterator_filter, &vbin);
1997
1998   g_value_unset (&vbin);
1999
2000   return result;
2001 }
2002
2003 /* returns 0 when TRUE because this is a GCompareFunc */
2004 /* MT safe */
2005 static gint
2006 bin_element_is_src (GstElement * child, GstBin * bin)
2007 {
2008   gboolean is_src;
2009
2010   /* we lock the child here for the remainder of the function to
2011    * get its name and other info safely. */
2012   GST_OBJECT_LOCK (child);
2013   is_src = GST_OBJECT_FLAG_IS_SET (child, GST_ELEMENT_FLAG_SOURCE);
2014
2015   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
2016       "child %s %s src", GST_OBJECT_NAME (child), is_src ? "is" : "is not");
2017
2018   GST_OBJECT_UNLOCK (child);
2019   return is_src ? 0 : 1;
2020 }
2021
2022 static gint
2023 src_iterator_filter (const GValue * vchild, GValue * vbin)
2024 {
2025   GstBin *bin = g_value_get_object (vbin);
2026   GstElement *child = g_value_get_object (vchild);
2027
2028   return (bin_element_is_src (child, bin));
2029 }
2030
2031 /**
2032  * gst_bin_iterate_sources:
2033  * @bin: a #GstBin
2034  *
2035  * Gets an iterator for all elements in the bin that have the
2036  * #GST_ELEMENT_FLAG_SOURCE flag set.
2037  *
2038  * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
2039  */
2040 GstIterator *
2041 gst_bin_iterate_sources (GstBin * bin)
2042 {
2043   GstIterator *children;
2044   GstIterator *result;
2045   GValue vbin = { 0, };
2046
2047   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
2048
2049   g_value_init (&vbin, GST_TYPE_BIN);
2050   g_value_set_object (&vbin, bin);
2051
2052   children = gst_bin_iterate_elements (bin);
2053   result = gst_iterator_filter (children,
2054       (GCompareFunc) src_iterator_filter, &vbin);
2055
2056   g_value_unset (&vbin);
2057
2058   return result;
2059 }
2060
2061 /*
2062  * MT safe
2063  */
2064 static GstStateChangeReturn
2065 gst_bin_get_state_func (GstElement * element, GstState * state,
2066     GstState * pending, GstClockTime timeout)
2067 {
2068   GstStateChangeReturn ret;
2069
2070   GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
2071
2072   ret =
2073       GST_ELEMENT_CLASS (parent_class)->get_state (element, state, pending,
2074       timeout);
2075
2076   return ret;
2077 }
2078
2079 /***********************************************
2080  * Topologically sorted iterator
2081  * see http://en.wikipedia.org/wiki/Topological_sorting
2082  *
2083  * For each element in the graph, an entry is kept in a HashTable
2084  * with its number of srcpad connections (degree).
2085  * We then change state of all elements without dependencies
2086  * (degree 0) and decrement the degree of all elements connected
2087  * on the sinkpads. When an element reaches degree 0, its state is
2088  * changed next.
2089  * When all elements are handled the algorithm stops.
2090  */
2091 typedef struct _GstBinSortIterator
2092 {
2093   GstIterator it;
2094   GQueue queue;                 /* elements queued for state change */
2095   GstBin *bin;                  /* bin we iterate */
2096   gint mode;                    /* adding or removing dependency */
2097   GstElement *best;             /* next element with least dependencies */
2098   gint best_deg;                /* best degree */
2099   GHashTable *hash;             /* hashtable with element dependencies */
2100   gboolean dirty;               /* we detected structure change */
2101 } GstBinSortIterator;
2102
2103 static void
2104 copy_to_queue (gpointer data, gpointer user_data)
2105 {
2106   GstElement *element = data;
2107   GQueue *queue = user_data;
2108
2109   gst_object_ref (element);
2110   g_queue_push_tail (queue, element);
2111 }
2112
2113 static void
2114 gst_bin_sort_iterator_copy (const GstBinSortIterator * it,
2115     GstBinSortIterator * copy)
2116 {
2117   GHashTableIter iter;
2118   gpointer key, value;
2119
2120   g_queue_init (&copy->queue);
2121   g_queue_foreach ((GQueue *) & it->queue, copy_to_queue, &copy->queue);
2122
2123   copy->bin = gst_object_ref (it->bin);
2124   if (it->best)
2125     copy->best = gst_object_ref (it->best);
2126
2127   copy->hash = g_hash_table_new (NULL, NULL);
2128   g_hash_table_iter_init (&iter, it->hash);
2129   while (g_hash_table_iter_next (&iter, &key, &value))
2130     g_hash_table_insert (copy->hash, key, value);
2131 }
2132
2133 /* we add and subtract 1 to make sure we don't confuse NULL and 0 */
2134 #define HASH_SET_DEGREE(bit, elem, deg) \
2135     g_hash_table_replace (bit->hash, elem, GINT_TO_POINTER(deg+1))
2136 #define HASH_GET_DEGREE(bit, elem) \
2137     (GPOINTER_TO_INT(g_hash_table_lookup (bit->hash, elem))-1)
2138
2139 /* add element to queue of next elements in the iterator.
2140  * We push at the tail to give higher priority elements a
2141  * chance first */
2142 static void
2143 add_to_queue (GstBinSortIterator * bit, GstElement * element)
2144 {
2145   GST_DEBUG_OBJECT (bit->bin, "adding '%s' to queue",
2146       GST_ELEMENT_NAME (element));
2147   gst_object_ref (element);
2148   g_queue_push_tail (&bit->queue, element);
2149   HASH_SET_DEGREE (bit, element, -1);
2150 }
2151
2152 static void
2153 remove_from_queue (GstBinSortIterator * bit, GstElement * element)
2154 {
2155   GList *find;
2156
2157   if ((find = g_queue_find (&bit->queue, element))) {
2158     GST_DEBUG_OBJECT (bit->bin, "removing '%s' from queue",
2159         GST_ELEMENT_NAME (element));
2160
2161     g_queue_delete_link (&bit->queue, find);
2162     gst_object_unref (element);
2163   } else {
2164     GST_DEBUG_OBJECT (bit->bin, "unable to remove '%s' from queue",
2165         GST_ELEMENT_NAME (element));
2166   }
2167 }
2168
2169 /* clear the queue, unref all objects as we took a ref when
2170  * we added them to the queue */
2171 static void
2172 clear_queue (GQueue * queue)
2173 {
2174   gpointer p;
2175
2176   while ((p = g_queue_pop_head (queue)))
2177     gst_object_unref (p);
2178 }
2179
2180 /* set all degrees to 0. Elements marked as a sink are
2181  * added to the queue immediately. Since we only look at the SINK flag of the
2182  * element, it is possible that we add non-sinks to the queue. These will be
2183  * removed from the queue again when we can prove that it provides data for some
2184  * other element. */
2185 static void
2186 reset_degree (GstElement * element, GstBinSortIterator * bit)
2187 {
2188   gboolean is_sink;
2189
2190   /* sinks are added right away */
2191   GST_OBJECT_LOCK (element);
2192   is_sink = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SINK);
2193   GST_OBJECT_UNLOCK (element);
2194
2195   if (is_sink) {
2196     add_to_queue (bit, element);
2197   } else {
2198     /* others are marked with 0 and handled when sinks are done */
2199     HASH_SET_DEGREE (bit, element, 0);
2200   }
2201 }
2202
2203 /* adjust the degree of all elements connected to the given
2204  * element. If a degree of an element drops to 0, it is
2205  * added to the queue of elements to schedule next.
2206  *
2207  * We have to make sure not to cross the bin boundary this element
2208  * belongs to.
2209  */
2210 static void
2211 update_degree (GstElement * element, GstBinSortIterator * bit)
2212 {
2213   gboolean linked = FALSE;
2214
2215   GST_OBJECT_LOCK (element);
2216   /* don't touch degree if element has no sinkpads */
2217   if (element->numsinkpads != 0) {
2218     /* loop over all sinkpads, decrement degree for all connected
2219      * elements in this bin */
2220     GList *pads;
2221
2222     for (pads = element->sinkpads; pads; pads = g_list_next (pads)) {
2223       GstPad *pad, *peer;
2224
2225       pad = GST_PAD_CAST (pads->data);
2226
2227       /* we're iterating over the sinkpads, check if it's busy in a link/unlink */
2228       if (G_UNLIKELY (find_message (bit->bin, GST_OBJECT_CAST (pad),
2229                   GST_MESSAGE_STRUCTURE_CHANGE))) {
2230         /* mark the iterator as dirty because we won't be updating the degree
2231          * of the peer parent now. This would result in the 'loop detected'
2232          * later on because the peer parent element could become the best next
2233          * element with a degree > 0. We will simply continue our state
2234          * changes and we'll eventually resync when the unlink completed and
2235          * the iterator cookie is updated. */
2236         bit->dirty = TRUE;
2237         continue;
2238       }
2239
2240       if ((peer = gst_pad_get_peer (pad))) {
2241         GstElement *peer_element;
2242
2243         if ((peer_element = gst_pad_get_parent_element (peer))) {
2244           GST_OBJECT_LOCK (peer_element);
2245           /* check that we don't go outside of this bin */
2246           if (GST_OBJECT_CAST (peer_element)->parent ==
2247               GST_OBJECT_CAST (bit->bin)) {
2248             gint old_deg, new_deg;
2249
2250             old_deg = HASH_GET_DEGREE (bit, peer_element);
2251
2252             /* check to see if we added an element as sink that was not really a
2253              * sink because it was connected to some other element. */
2254             if (old_deg == -1) {
2255               remove_from_queue (bit, peer_element);
2256               old_deg = 0;
2257             }
2258             new_deg = old_deg + bit->mode;
2259
2260             GST_DEBUG_OBJECT (bit->bin,
2261                 "change element %s, degree %d->%d, linked to %s",
2262                 GST_ELEMENT_NAME (peer_element), old_deg, new_deg,
2263                 GST_ELEMENT_NAME (element));
2264
2265             /* update degree, it is possible that an element was in 0 and
2266              * reaches -1 here. This would mean that the element had no sinkpads
2267              * but became linked while the state change was happening. We will
2268              * resync on this with the structure change message. */
2269             if (new_deg == 0) {
2270               /* degree hit 0, add to queue */
2271               add_to_queue (bit, peer_element);
2272             } else {
2273               HASH_SET_DEGREE (bit, peer_element, new_deg);
2274             }
2275             linked = TRUE;
2276           }
2277           GST_OBJECT_UNLOCK (peer_element);
2278           gst_object_unref (peer_element);
2279         }
2280         gst_object_unref (peer);
2281       }
2282     }
2283   }
2284   if (!linked) {
2285     GST_DEBUG_OBJECT (bit->bin, "element %s not linked on any sinkpads",
2286         GST_ELEMENT_NAME (element));
2287   }
2288   GST_OBJECT_UNLOCK (element);
2289 }
2290
2291 /* find the next best element not handled yet. This is the one
2292  * with the lowest non-negative degree */
2293 static void
2294 find_element (GstElement * element, GstBinSortIterator * bit)
2295 {
2296   gint degree;
2297
2298   /* element is already handled */
2299   if ((degree = HASH_GET_DEGREE (bit, element)) < 0)
2300     return;
2301
2302   /* first element or element with smaller degree */
2303   if (bit->best == NULL || bit->best_deg > degree) {
2304     bit->best = element;
2305     bit->best_deg = degree;
2306   } else if (bit->best_deg == degree
2307       && GST_OBJECT_FLAG_IS_SET (bit->best, GST_ELEMENT_FLAG_SOURCE)
2308       && !GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_FLAG_SOURCE)) {
2309     /* If two elements have the same degree, we want to ensure we
2310      * return non-source elements first. */
2311     bit->best = element;
2312   }
2313 }
2314
2315 /* get next element in iterator. */
2316 static GstIteratorResult
2317 gst_bin_sort_iterator_next (GstBinSortIterator * bit, GValue * result)
2318 {
2319   GstElement *best;
2320   GstBin *bin = bit->bin;
2321
2322   /* empty queue, we have to find a next best element */
2323   if (g_queue_is_empty (&bit->queue)) {
2324     bit->best = NULL;
2325     bit->best_deg = G_MAXINT;
2326     g_list_foreach (bin->children, (GFunc) find_element, bit);
2327     if ((best = bit->best)) {
2328       /* when we detected an unlink, don't warn because our degrees might be
2329        * screwed up. We will resync later */
2330       if (bit->best_deg != 0 && !bit->dirty) {
2331         /* we don't fail on this one yet */
2332         GST_WARNING_OBJECT (bin, "loop dected in graph");
2333         g_warning ("loop detected in the graph of bin '%s'!!",
2334             GST_ELEMENT_NAME (bin));
2335       }
2336       /* best unhandled element, schedule as next element */
2337       GST_DEBUG_OBJECT (bin, "queue empty, next best: %s",
2338           GST_ELEMENT_NAME (best));
2339       HASH_SET_DEGREE (bit, best, -1);
2340       g_value_set_object (result, best);
2341     } else {
2342       GST_DEBUG_OBJECT (bin, "queue empty, elements exhausted");
2343       /* no more unhandled elements, we are done */
2344       return GST_ITERATOR_DONE;
2345     }
2346   } else {
2347     /* everything added to the queue got reffed */
2348     best = g_queue_pop_head (&bit->queue);
2349     g_value_set_object (result, best);
2350     gst_object_unref (best);
2351   }
2352
2353   GST_DEBUG_OBJECT (bin, "queue head gives %s", GST_ELEMENT_NAME (best));
2354   /* update degrees of linked elements */
2355   update_degree (best, bit);
2356
2357   return GST_ITERATOR_OK;
2358 }
2359
2360 /* clear queues, recalculate the degrees and restart. */
2361 static void
2362 gst_bin_sort_iterator_resync (GstBinSortIterator * bit)
2363 {
2364   GstBin *bin = bit->bin;
2365
2366   GST_DEBUG_OBJECT (bin, "resync");
2367   bit->dirty = FALSE;
2368   clear_queue (&bit->queue);
2369   /* reset degrees */
2370   g_list_foreach (bin->children, (GFunc) reset_degree, bit);
2371   /* calc degrees, incrementing */
2372   bit->mode = 1;
2373   g_list_foreach (bin->children, (GFunc) update_degree, bit);
2374   /* for the rest of the function we decrement the degrees */
2375   bit->mode = -1;
2376 }
2377
2378 /* clear queues, unref bin and free iterator. */
2379 static void
2380 gst_bin_sort_iterator_free (GstBinSortIterator * bit)
2381 {
2382   GstBin *bin = bit->bin;
2383
2384   GST_DEBUG_OBJECT (bin, "free");
2385   clear_queue (&bit->queue);
2386   g_hash_table_destroy (bit->hash);
2387   gst_object_unref (bin);
2388 }
2389
2390 /* should be called with the bin LOCK held */
2391 static GstIterator *
2392 gst_bin_sort_iterator_new (GstBin * bin)
2393 {
2394   GstBinSortIterator *result;
2395
2396   /* we don't need an ItemFunction because we ref the items in the _next
2397    * method already */
2398   result = (GstBinSortIterator *)
2399       gst_iterator_new (sizeof (GstBinSortIterator),
2400       GST_TYPE_ELEMENT,
2401       GST_OBJECT_GET_LOCK (bin),
2402       &bin->priv->structure_cookie,
2403       (GstIteratorCopyFunction) gst_bin_sort_iterator_copy,
2404       (GstIteratorNextFunction) gst_bin_sort_iterator_next,
2405       (GstIteratorItemFunction) NULL,
2406       (GstIteratorResyncFunction) gst_bin_sort_iterator_resync,
2407       (GstIteratorFreeFunction) gst_bin_sort_iterator_free);
2408   g_queue_init (&result->queue);
2409   result->hash = g_hash_table_new (NULL, NULL);
2410   gst_object_ref (bin);
2411   result->bin = bin;
2412   gst_bin_sort_iterator_resync (result);
2413
2414   return (GstIterator *) result;
2415 }
2416
2417 /**
2418  * gst_bin_iterate_sorted:
2419  * @bin: a #GstBin
2420  *
2421  * Gets an iterator for the elements in this bin in topologically
2422  * sorted order. This means that the elements are returned from
2423  * the most downstream elements (sinks) to the sources.
2424  *
2425  * This function is used internally to perform the state changes
2426  * of the bin elements and for clock selection.
2427  *
2428  * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
2429  */
2430 GstIterator *
2431 gst_bin_iterate_sorted (GstBin * bin)
2432 {
2433   GstIterator *result;
2434
2435   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
2436
2437   GST_OBJECT_LOCK (bin);
2438   result = gst_bin_sort_iterator_new (bin);
2439   GST_OBJECT_UNLOCK (bin);
2440
2441   return result;
2442 }
2443
2444 static GstStateChangeReturn
2445 gst_bin_element_set_state (GstBin * bin, GstElement * element,
2446     GstClockTime base_time, GstClockTime start_time, GstState current,
2447     GstState next)
2448 {
2449   GstStateChangeReturn ret;
2450   GstState child_current, child_pending;
2451   gboolean locked;
2452   GList *found;
2453
2454   GST_STATE_LOCK (element);
2455
2456   GST_OBJECT_LOCK (element);
2457   /* set base_time and start time on child */
2458   GST_ELEMENT_START_TIME (element) = start_time;
2459   element->base_time = base_time;
2460   /* peel off the locked flag */
2461   locked = GST_ELEMENT_IS_LOCKED_STATE (element);
2462   /* Get the previous set_state result to preserve NO_PREROLL and ASYNC */
2463   ret = GST_STATE_RETURN (element);
2464   child_current = GST_STATE (element);
2465   child_pending = GST_STATE_PENDING (element);
2466   GST_OBJECT_UNLOCK (element);
2467
2468   /* skip locked elements */
2469   if (G_UNLIKELY (locked))
2470     goto locked;
2471
2472   /* if the element was no preroll, just start changing the state regardless
2473    * if it had async elements (in the case of a bin) because they won't preroll
2474    * anyway. */
2475   if (G_UNLIKELY (ret == GST_STATE_CHANGE_NO_PREROLL)) {
2476     GST_DEBUG_OBJECT (element, "element is NO_PREROLL, ignore async elements");
2477     goto no_preroll;
2478   }
2479
2480   GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2481       "current %s pending %s, desired next %s",
2482       gst_element_state_get_name (child_current),
2483       gst_element_state_get_name (child_pending),
2484       gst_element_state_get_name (next));
2485
2486   /* always recurse into bins so that we can set the base time */
2487   if (GST_IS_BIN (element))
2488     goto do_state;
2489
2490   /* Try not to change the state of elements that are already in the state we're
2491    * going to */
2492   if (child_current == next && child_pending == GST_STATE_VOID_PENDING) {
2493     /* child is already at the requested state, return previous return. Note that
2494      * if the child has a pending state to next, we will still call the
2495      * set_state function */
2496     goto unneeded;
2497   } else if (next > current) {
2498     /* upward state change */
2499     if (child_pending == GST_STATE_VOID_PENDING) {
2500       /* .. and the child is not busy doing anything */
2501       if (child_current > next) {
2502         /* .. and is already past the requested state, assume it got there
2503          * without error */
2504         ret = GST_STATE_CHANGE_SUCCESS;
2505         goto unneeded;
2506       }
2507     } else if (child_pending > child_current) {
2508       /* .. and the child is busy going upwards */
2509       if (child_current >= next) {
2510         /* .. and is already past the requested state, assume it got there
2511          * without error */
2512         ret = GST_STATE_CHANGE_SUCCESS;
2513         goto unneeded;
2514       }
2515     } else {
2516       /* .. and the child is busy going downwards */
2517       if (child_current > next) {
2518         /* .. and is already past the requested state, assume it got there
2519          * without error */
2520         ret = GST_STATE_CHANGE_SUCCESS;
2521         goto unneeded;
2522       }
2523     }
2524   } else if (next < current) {
2525     /* downward state change */
2526     if (child_pending == GST_STATE_VOID_PENDING) {
2527       /* .. and the child is not busy doing anything */
2528       if (child_current < next) {
2529         /* .. and is already past the requested state, assume it got there
2530          * without error */
2531         ret = GST_STATE_CHANGE_SUCCESS;
2532         goto unneeded;
2533       }
2534     } else if (child_pending < child_current) {
2535       /* .. and the child is busy going downwards */
2536       if (child_current <= next) {
2537         /* .. and is already past the requested state, assume it got there
2538          * without error */
2539         ret = GST_STATE_CHANGE_SUCCESS;
2540         goto unneeded;
2541       }
2542     } else {
2543       /* .. and the child is busy going upwards */
2544       if (child_current < next) {
2545         /* .. and is already past the requested state, assume it got there
2546          * without error */
2547         ret = GST_STATE_CHANGE_SUCCESS;
2548         goto unneeded;
2549       }
2550     }
2551   }
2552
2553 do_state:
2554   GST_OBJECT_LOCK (bin);
2555   /* the element was busy with an upwards async state change, we must wait for
2556    * an ASYNC_DONE message before we attempt to change the state. */
2557   if ((found =
2558           find_message (bin, GST_OBJECT_CAST (element),
2559               GST_MESSAGE_ASYNC_START))) {
2560 #ifndef GST_DISABLE_GST_DEBUG
2561     GstMessage *message = GST_MESSAGE_CAST (found->data);
2562
2563     GST_DEBUG_OBJECT (element, "element message %p, %s async busy",
2564         message, GST_ELEMENT_NAME (GST_MESSAGE_SRC (message)));
2565 #endif
2566     /* only wait for upward state changes */
2567     if (next > current) {
2568       /* We found an async element check if we can force its state to change or
2569        * if we have to wait for it to preroll. */
2570       goto was_busy;
2571     }
2572   }
2573   GST_OBJECT_UNLOCK (bin);
2574
2575 no_preroll:
2576   GST_DEBUG_OBJECT (bin,
2577       "setting element %s to %s, base_time %" GST_TIME_FORMAT,
2578       GST_ELEMENT_NAME (element), gst_element_state_get_name (next),
2579       GST_TIME_ARGS (base_time));
2580
2581   /* change state */
2582   ret = gst_element_set_state (element, next);
2583
2584   GST_STATE_UNLOCK (element);
2585
2586   return ret;
2587
2588 locked:
2589   {
2590     if (ret == GST_STATE_CHANGE_FAILURE) {
2591       GST_DEBUG_OBJECT (element,
2592           "element is locked, and previous state change failed, return %s",
2593           gst_element_state_change_return_get_name (GST_STATE_CHANGE_SUCCESS));
2594       ret = GST_STATE_CHANGE_SUCCESS;
2595     } else {
2596       GST_DEBUG_OBJECT (element,
2597           "element is locked, return previous return %s",
2598           gst_element_state_change_return_get_name (ret));
2599     }
2600     GST_STATE_UNLOCK (element);
2601     return ret;
2602   }
2603 unneeded:
2604   {
2605     GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2606         "skipping transition from %s to  %s",
2607         gst_element_state_get_name (child_current),
2608         gst_element_state_get_name (next));
2609     GST_STATE_UNLOCK (element);
2610     return ret;
2611   }
2612 was_busy:
2613   {
2614     GST_DEBUG_OBJECT (element, "element was busy, delaying state change");
2615     GST_OBJECT_UNLOCK (bin);
2616     GST_STATE_UNLOCK (element);
2617     return GST_STATE_CHANGE_ASYNC;
2618   }
2619 }
2620
2621 /* gst_iterator_fold functions for pads_activate
2622  * Stop the iterator if activating one pad failed, but only if that pad
2623  * has not been removed from the element. */
2624 static gboolean
2625 activate_pads (const GValue * vpad, GValue * ret, gboolean * active)
2626 {
2627   GstPad *pad = g_value_get_object (vpad);
2628   gboolean cont = TRUE;
2629
2630   if (!gst_pad_set_active (pad, *active)) {
2631     if (GST_PAD_PARENT (pad) != NULL) {
2632       cont = FALSE;
2633       g_value_set_boolean (ret, FALSE);
2634     }
2635   }
2636
2637   return cont;
2638 }
2639
2640 /* returns false on error or early cutout of the fold, true if all
2641  * pads in @iter were (de)activated successfully. */
2642 static gboolean
2643 iterator_activate_fold_with_resync (GstIterator * iter, gpointer user_data)
2644 {
2645   GstIteratorResult ires;
2646   GValue ret = { 0 };
2647
2648   /* no need to unset this later, it's just a boolean */
2649   g_value_init (&ret, G_TYPE_BOOLEAN);
2650   g_value_set_boolean (&ret, TRUE);
2651
2652   while (1) {
2653     ires = gst_iterator_fold (iter, (GstIteratorFoldFunction) activate_pads,
2654         &ret, user_data);
2655     switch (ires) {
2656       case GST_ITERATOR_RESYNC:
2657         /* need to reset the result again */
2658         g_value_set_boolean (&ret, TRUE);
2659         gst_iterator_resync (iter);
2660         break;
2661       case GST_ITERATOR_DONE:
2662         /* all pads iterated, return collected value */
2663         goto done;
2664       default:
2665         /* iterator returned _ERROR or premature end with _OK,
2666          * mark an error and exit */
2667         g_value_set_boolean (&ret, FALSE);
2668         goto done;
2669     }
2670   }
2671 done:
2672   /* return collected value */
2673   return g_value_get_boolean (&ret);
2674 }
2675
2676 /* is called with STATE_LOCK
2677  */
2678 static gboolean
2679 gst_bin_src_pads_activate (GstBin * bin, gboolean active)
2680 {
2681   GstIterator *iter;
2682   gboolean fold_ok;
2683
2684   GST_DEBUG_OBJECT (bin, "%s pads", active ? "activate" : "deactivate");
2685
2686   iter = gst_element_iterate_src_pads ((GstElement *) bin);
2687   fold_ok = iterator_activate_fold_with_resync (iter, &active);
2688   gst_iterator_free (iter);
2689   if (G_UNLIKELY (!fold_ok))
2690     goto failed;
2691
2692   GST_DEBUG_OBJECT (bin, "pad %sactivation successful", active ? "" : "de");
2693
2694   return TRUE;
2695
2696   /* ERRORS */
2697 failed:
2698   {
2699     GST_DEBUG_OBJECT (bin, "pad %sactivation failed", active ? "" : "de");
2700     return FALSE;
2701   }
2702 }
2703
2704 /**
2705  * gst_bin_recalculate_latency:
2706  * @bin: a #GstBin
2707  *
2708  * Queries @bin for the current latency and reconfigures this latency on all the
2709  * elements using a LATENCY event.
2710  *
2711  * This method is typically called on the pipeline when a #GST_MESSAGE_LATENCY
2712  * is posted on the bus.
2713  *
2714  * This function simply emits the #GstBin::do-latency signal so any custom latency
2715  * calculations will be performed.
2716  *
2717  * Returns: %TRUE if the latency could be queried and reconfigured.
2718  */
2719 gboolean
2720 gst_bin_recalculate_latency (GstBin * bin)
2721 {
2722   gboolean res;
2723
2724   g_signal_emit (bin, gst_bin_signals[DO_LATENCY], 0, &res);
2725   GST_DEBUG_OBJECT (bin, "latency returned %d", res);
2726
2727   return res;
2728 }
2729
2730 static gboolean
2731 gst_bin_do_latency_func (GstBin * bin)
2732 {
2733   GstQuery *query;
2734   GstElement *element;
2735   GstClockTime min_latency, max_latency;
2736   gboolean res;
2737
2738   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2739
2740   element = GST_ELEMENT_CAST (bin);
2741
2742   GST_DEBUG_OBJECT (element, "querying latency");
2743
2744   query = gst_query_new_latency ();
2745   if ((res = gst_element_query (element, query))) {
2746     gboolean live;
2747
2748     gst_query_parse_latency (query, &live, &min_latency, &max_latency);
2749
2750     GST_DEBUG_OBJECT (element,
2751         "got min latency %" GST_TIME_FORMAT ", max latency %"
2752         GST_TIME_FORMAT ", live %d", GST_TIME_ARGS (min_latency),
2753         GST_TIME_ARGS (max_latency), live);
2754
2755     if (max_latency < min_latency) {
2756       /* this is an impossible situation, some parts of the pipeline might not
2757        * work correctly. We post a warning for now. */
2758       GST_ELEMENT_WARNING (element, CORE, CLOCK, (NULL),
2759           ("Impossible to configure latency: max %" GST_TIME_FORMAT " < min %"
2760               GST_TIME_FORMAT ". Add queues or other buffering elements.",
2761               GST_TIME_ARGS (max_latency), GST_TIME_ARGS (min_latency)));
2762     }
2763
2764     /* configure latency on elements */
2765     res = gst_element_send_event (element, gst_event_new_latency (min_latency));
2766     if (res) {
2767       GST_INFO_OBJECT (element, "configured latency of %" GST_TIME_FORMAT,
2768           GST_TIME_ARGS (min_latency));
2769     } else {
2770       GST_WARNING_OBJECT (element,
2771           "did not really configure latency of %" GST_TIME_FORMAT,
2772           GST_TIME_ARGS (min_latency));
2773     }
2774   } else {
2775     /* this is not a real problem, we just don't configure any latency. */
2776     GST_WARNING_OBJECT (element, "failed to query latency");
2777   }
2778   gst_query_unref (query);
2779
2780   return res;
2781 }
2782
2783 static gboolean
2784 gst_bin_post_message (GstElement * element, GstMessage * msg)
2785 {
2786   GstElementClass *pklass = (GstElementClass *) parent_class;
2787   gboolean ret;
2788
2789   ret = pklass->post_message (element, gst_message_ref (msg));
2790
2791   if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_STATE_CHANGED &&
2792       GST_MESSAGE_SRC (msg) == GST_OBJECT_CAST (element)) {
2793     GstState newstate, pending;
2794
2795     gst_message_parse_state_changed (msg, NULL, &newstate, &pending);
2796     if (newstate == GST_STATE_PLAYING && pending == GST_STATE_VOID_PENDING) {
2797       GST_BIN_CAST (element)->priv->posted_playing = TRUE;
2798       bin_do_eos (GST_BIN_CAST (element));
2799     } else {
2800       GST_BIN_CAST (element)->priv->posted_playing = FALSE;
2801     }
2802   }
2803
2804   gst_message_unref (msg);
2805
2806   return ret;
2807 }
2808
2809 static void
2810 reset_state (const GValue * data, gpointer user_data)
2811 {
2812   GstElement *e = g_value_get_object (data);
2813   GstState state = GPOINTER_TO_INT (user_data);
2814
2815   if (gst_element_set_state (e, state) == GST_STATE_CHANGE_FAILURE)
2816     GST_WARNING_OBJECT (e, "Failed to switch back down to %s",
2817         gst_element_state_get_name (state));
2818 }
2819
2820 static GstStateChangeReturn
2821 gst_bin_change_state_func (GstElement * element, GstStateChange transition)
2822 {
2823   GstBin *bin;
2824   GstStateChangeReturn ret;
2825   GstState current, next;
2826   gboolean have_async;
2827   gboolean have_no_preroll;
2828   GstClockTime base_time, start_time;
2829   GstIterator *it;
2830   gboolean done;
2831   GValue data = { 0, };
2832
2833   /* we don't need to take the STATE_LOCK, it is already taken */
2834   current = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2835   next = (GstState) GST_STATE_TRANSITION_NEXT (transition);
2836
2837   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2838       "changing state of children from %s to %s",
2839       gst_element_state_get_name (current), gst_element_state_get_name (next));
2840
2841   bin = GST_BIN_CAST (element);
2842
2843   switch (next) {
2844     case GST_STATE_PLAYING:
2845     {
2846       gboolean toplevel, asynchandling;
2847
2848       GST_OBJECT_LOCK (bin);
2849       toplevel = BIN_IS_TOPLEVEL (bin);
2850       asynchandling = bin->priv->asynchandling;
2851       GST_OBJECT_UNLOCK (bin);
2852
2853       if (toplevel)
2854         gst_bin_recalculate_latency (bin);
2855       if (asynchandling)
2856         gst_element_post_message (element,
2857             gst_message_new_latency (GST_OBJECT_CAST (element)));
2858       break;
2859     }
2860     case GST_STATE_PAUSED:
2861       /* Clear EOS list on next PAUSED */
2862       GST_OBJECT_LOCK (bin);
2863       GST_DEBUG_OBJECT (element, "clearing EOS elements");
2864       bin_remove_messages (bin, NULL, GST_MESSAGE_EOS);
2865       bin->priv->posted_eos = FALSE;
2866       if (current == GST_STATE_READY)
2867         bin_remove_messages (bin, NULL, GST_MESSAGE_STREAM_START);
2868       GST_OBJECT_UNLOCK (bin);
2869       if (current == GST_STATE_READY)
2870         if (!(gst_bin_src_pads_activate (bin, TRUE)))
2871           goto activate_failure;
2872       break;
2873     case GST_STATE_READY:
2874       /* Clear message list on next READY */
2875       GST_OBJECT_LOCK (bin);
2876       GST_DEBUG_OBJECT (element, "clearing all cached messages");
2877       bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
2878       GST_OBJECT_UNLOCK (bin);
2879       /* We might not have reached PAUSED yet due to async errors,
2880        * make sure to always deactivate the pads nonetheless */
2881       if (!(gst_bin_src_pads_activate (bin, FALSE)))
2882         goto activate_failure;
2883       break;
2884     case GST_STATE_NULL:
2885       /* Clear message list on next NULL */
2886       GST_OBJECT_LOCK (bin);
2887       GST_DEBUG_OBJECT (element, "clearing all cached messages");
2888       bin_remove_messages (bin, NULL, GST_MESSAGE_ANY);
2889       GST_OBJECT_UNLOCK (bin);
2890       if (current == GST_STATE_READY) {
2891         if (!(gst_bin_src_pads_activate (bin, FALSE)))
2892           goto activate_failure;
2893       }
2894       break;
2895     default:
2896       break;
2897   }
2898
2899   /* this flag is used to make the async state changes return immediately. We
2900    * don't want them to interfere with this state change */
2901   GST_OBJECT_LOCK (bin);
2902   bin->polling = TRUE;
2903   GST_OBJECT_UNLOCK (bin);
2904
2905   /* iterate in state change order */
2906   it = gst_bin_iterate_sorted (bin);
2907
2908   /* mark if we've seen an ASYNC element in the bin when we did a state change.
2909    * Note how we don't reset this value when a resync happens, the reason being
2910    * that the async element posted ASYNC_START and we want to post ASYNC_DONE
2911    * even after a resync when the async element is gone */
2912   have_async = FALSE;
2913
2914 restart:
2915   /* take base_time */
2916   base_time = gst_element_get_base_time (element);
2917   start_time = gst_element_get_start_time (element);
2918
2919   have_no_preroll = FALSE;
2920
2921   done = FALSE;
2922   while (!done) {
2923     switch (gst_iterator_next (it, &data)) {
2924       case GST_ITERATOR_OK:
2925       {
2926         GstElement *child;
2927
2928         child = g_value_get_object (&data);
2929
2930         /* set state and base_time now */
2931         ret = gst_bin_element_set_state (bin, child, base_time, start_time,
2932             current, next);
2933
2934         switch (ret) {
2935           case GST_STATE_CHANGE_SUCCESS:
2936             GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2937                 "child '%s' changed state to %d(%s) successfully",
2938                 GST_ELEMENT_NAME (child), next,
2939                 gst_element_state_get_name (next));
2940             break;
2941           case GST_STATE_CHANGE_ASYNC:
2942           {
2943             GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2944                 "child '%s' is changing state asynchronously to %s",
2945                 GST_ELEMENT_NAME (child), gst_element_state_get_name (next));
2946             have_async = TRUE;
2947             break;
2948           }
2949           case GST_STATE_CHANGE_FAILURE:{
2950             GstObject *parent;
2951
2952             GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2953                 "child '%s' failed to go to state %d(%s)",
2954                 GST_ELEMENT_NAME (child),
2955                 next, gst_element_state_get_name (next));
2956
2957             /* Only fail if the child is still inside
2958              * this bin. It might've been removed already
2959              * because of the error by the bin subclass
2960              * to ignore the error.  */
2961             parent = gst_object_get_parent (GST_OBJECT_CAST (child));
2962             if (parent == GST_OBJECT_CAST (element)) {
2963               /* element is still in bin, really error now */
2964               gst_object_unref (parent);
2965               goto undo;
2966             }
2967             /* child removed from bin, let the resync code redo the state
2968              * change */
2969             GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2970                 "child '%s' was removed from the bin",
2971                 GST_ELEMENT_NAME (child));
2972
2973             if (parent)
2974               gst_object_unref (parent);
2975
2976             break;
2977           }
2978           case GST_STATE_CHANGE_NO_PREROLL:
2979             GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2980                 "child '%s' changed state to %d(%s) successfully without preroll",
2981                 GST_ELEMENT_NAME (child), next,
2982                 gst_element_state_get_name (next));
2983             have_no_preroll = TRUE;
2984             break;
2985           default:
2986             g_assert_not_reached ();
2987             break;
2988         }
2989         g_value_reset (&data);
2990         break;
2991       }
2992       case GST_ITERATOR_RESYNC:
2993         GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "iterator doing resync");
2994         gst_iterator_resync (it);
2995         goto restart;
2996       default:
2997       case GST_ITERATOR_DONE:
2998         GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "iterator done");
2999         done = TRUE;
3000         break;
3001     }
3002   }
3003
3004   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3005   if (G_UNLIKELY (ret == GST_STATE_CHANGE_FAILURE))
3006     goto done;
3007
3008   if (have_no_preroll) {
3009     GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
3010         "we have NO_PREROLL elements %s -> NO_PREROLL",
3011         gst_element_state_change_return_get_name (ret));
3012     ret = GST_STATE_CHANGE_NO_PREROLL;
3013   } else if (have_async) {
3014     GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
3015         "we have ASYNC elements %s -> ASYNC",
3016         gst_element_state_change_return_get_name (ret));
3017     ret = GST_STATE_CHANGE_ASYNC;
3018   }
3019
3020 done:
3021   g_value_unset (&data);
3022   gst_iterator_free (it);
3023
3024   GST_OBJECT_LOCK (bin);
3025   bin->polling = FALSE;
3026   /* it's possible that we did not get ASYNC from the children while the bin is
3027    * simulating ASYNC behaviour by posting an ASYNC_DONE message on the bus with
3028    * itself as the source. In that case we still want to check if the state
3029    * change completed. */
3030   if (ret != GST_STATE_CHANGE_ASYNC && !bin->priv->pending_async_done) {
3031     /* no element returned ASYNC and there are no pending async_done messages,
3032      * we can just complete. */
3033     GST_DEBUG_OBJECT (bin, "no async elements");
3034     goto state_end;
3035   }
3036   /* when we get here an ASYNC element was found */
3037   if (GST_STATE_TARGET (bin) <= GST_STATE_READY) {
3038     /* we ignore ASYNC state changes when we go to READY or NULL */
3039     GST_DEBUG_OBJECT (bin, "target state %s <= READY",
3040         gst_element_state_get_name (GST_STATE_TARGET (bin)));
3041     goto state_end;
3042   }
3043
3044   GST_DEBUG_OBJECT (bin, "check async elements");
3045   /* check if all elements managed to commit their state already */
3046   if (!find_message (bin, NULL, GST_MESSAGE_ASYNC_START)) {
3047     /* nothing found, remove all old ASYNC_DONE messages. This can happen when
3048      * all the elements committed their state while we were doing the state
3049      * change. We will still return ASYNC for consistency but we commit the
3050      * state already so that a _get_state() will return immediately. */
3051     bin_remove_messages (bin, NULL, GST_MESSAGE_ASYNC_DONE);
3052
3053     GST_DEBUG_OBJECT (bin, "async elements committed");
3054     bin_handle_async_done (bin, GST_STATE_CHANGE_SUCCESS, FALSE,
3055         GST_CLOCK_TIME_NONE);
3056   }
3057
3058 state_end:
3059   bin->priv->pending_async_done = FALSE;
3060   GST_OBJECT_UNLOCK (bin);
3061
3062   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3063       "done changing bin's state from %s to %s, now in %s, ret %s",
3064       gst_element_state_get_name (current),
3065       gst_element_state_get_name (next),
3066       gst_element_state_get_name (GST_STATE (element)),
3067       gst_element_state_change_return_get_name (ret));
3068
3069   return ret;
3070
3071   /* ERRORS */
3072 activate_failure:
3073   {
3074     GST_CAT_WARNING_OBJECT (GST_CAT_STATES, element,
3075         "failure (de)activating src pads");
3076     return GST_STATE_CHANGE_FAILURE;
3077   }
3078
3079 undo:
3080   {
3081     if (current < next) {
3082       GstIterator *it = gst_bin_iterate_sorted (GST_BIN (element));
3083       GstIteratorResult ret;
3084
3085       GST_DEBUG_OBJECT (element,
3086           "Bin failed to change state, switching children back to %s",
3087           gst_element_state_get_name (current));
3088       while (TRUE) {
3089         ret =
3090             gst_iterator_foreach (it, &reset_state, GINT_TO_POINTER (current));
3091         if (ret != GST_ITERATOR_RESYNC)
3092           break;
3093         gst_iterator_resync (it);
3094       }
3095       gst_iterator_free (it);
3096     }
3097     goto done;
3098   }
3099 }
3100
3101 /*
3102  * This function is a utility event handler. It will send the event to all sinks
3103  * or sources and appropriate ghost pads depending on the event-direction.
3104  *
3105  * Applications are free to override this behaviour and implement their own
3106  * handler, but this will work for pretty much all cases in practice.
3107  */
3108 static gboolean
3109 gst_bin_send_event (GstElement * element, GstEvent * event)
3110 {
3111   GstBin *bin = GST_BIN_CAST (element);
3112   GstIterator *iter;
3113   gboolean res = TRUE;
3114   gboolean done = FALSE;
3115   GValue data = { 0, };
3116
3117   if (GST_EVENT_IS_DOWNSTREAM (event)) {
3118     iter = gst_bin_iterate_sources (bin);
3119     GST_DEBUG_OBJECT (bin, "Sending %s event to src children",
3120         GST_EVENT_TYPE_NAME (event));
3121   } else {
3122     iter = gst_bin_iterate_sinks (bin);
3123     GST_DEBUG_OBJECT (bin, "Sending %s event to sink children",
3124         GST_EVENT_TYPE_NAME (event));
3125   }
3126
3127   while (!done) {
3128     switch (gst_iterator_next (iter, &data)) {
3129       case GST_ITERATOR_OK:
3130       {
3131         GstElement *child = g_value_get_object (&data);
3132
3133         gst_event_ref (event);
3134         res &= gst_element_send_event (child, event);
3135
3136         GST_LOG_OBJECT (child, "After handling %s event: %d",
3137             GST_EVENT_TYPE_NAME (event), res);
3138
3139         g_value_reset (&data);
3140         break;
3141       }
3142       case GST_ITERATOR_RESYNC:
3143         gst_iterator_resync (iter);
3144         res = TRUE;
3145         break;
3146       case GST_ITERATOR_DONE:
3147         done = TRUE;
3148         break;
3149       case GST_ITERATOR_ERROR:
3150         g_assert_not_reached ();
3151         break;
3152     }
3153   }
3154   g_value_unset (&data);
3155   gst_iterator_free (iter);
3156
3157   if (GST_EVENT_IS_DOWNSTREAM (event)) {
3158     iter = gst_element_iterate_sink_pads (GST_ELEMENT (bin));
3159     GST_DEBUG_OBJECT (bin, "Sending %s event to sink pads",
3160         GST_EVENT_TYPE_NAME (event));
3161   } else {
3162     iter = gst_element_iterate_src_pads (GST_ELEMENT (bin));
3163     GST_DEBUG_OBJECT (bin, "Sending %s event to src pads",
3164         GST_EVENT_TYPE_NAME (event));
3165   }
3166
3167   done = FALSE;
3168   while (!done) {
3169     switch (gst_iterator_next (iter, &data)) {
3170       case GST_ITERATOR_OK:
3171       {
3172         GstPad *pad = g_value_get_object (&data);
3173
3174         gst_event_ref (event);
3175         res &= gst_pad_send_event (pad, event);
3176         GST_LOG_OBJECT (pad, "After handling %s event: %d",
3177             GST_EVENT_TYPE_NAME (event), res);
3178         break;
3179       }
3180       case GST_ITERATOR_RESYNC:
3181         gst_iterator_resync (iter);
3182         res = TRUE;
3183         break;
3184       case GST_ITERATOR_DONE:
3185         done = TRUE;
3186         break;
3187       case GST_ITERATOR_ERROR:
3188         g_assert_not_reached ();
3189         break;
3190     }
3191   }
3192
3193   g_value_unset (&data);
3194   gst_iterator_free (iter);
3195   gst_event_unref (event);
3196
3197   return res;
3198 }
3199
3200 /* this is the function called by the threadpool. When async elements commit
3201  * their state, this function will attempt to bring the bin to the next state.
3202  */
3203 static void
3204 gst_bin_continue_func (GstBin * bin, BinContinueData * data)
3205 {
3206   GstState current, next, pending;
3207   GstStateChange transition;
3208
3209   pending = data->pending;
3210
3211   GST_DEBUG_OBJECT (bin, "waiting for state lock");
3212   GST_STATE_LOCK (bin);
3213
3214   GST_DEBUG_OBJECT (bin, "doing state continue");
3215   GST_OBJECT_LOCK (bin);
3216
3217   /* if a new state change happened after this thread was scheduled, we return
3218    * immediately. */
3219   if (data->cookie != GST_ELEMENT_CAST (bin)->state_cookie)
3220     goto interrupted;
3221
3222   current = GST_STATE (bin);
3223   next = GST_STATE_GET_NEXT (current, pending);
3224   transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
3225
3226   GST_STATE_NEXT (bin) = next;
3227   GST_STATE_PENDING (bin) = pending;
3228   /* mark busy */
3229   GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3230   GST_OBJECT_UNLOCK (bin);
3231
3232   GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3233       "continue state change %s to %s, final %s",
3234       gst_element_state_get_name (current),
3235       gst_element_state_get_name (next), gst_element_state_get_name (pending));
3236
3237   gst_element_change_state (GST_ELEMENT_CAST (bin), transition);
3238
3239   GST_STATE_UNLOCK (bin);
3240   GST_DEBUG_OBJECT (bin, "state continue done");
3241
3242   return;
3243
3244 interrupted:
3245   {
3246     GST_OBJECT_UNLOCK (bin);
3247     GST_STATE_UNLOCK (bin);
3248     GST_DEBUG_OBJECT (bin, "state continue aborted due to intervening change");
3249     return;
3250   }
3251 }
3252
3253 static GstBusSyncReply
3254 bin_bus_handler (GstBus * bus, GstMessage * message, GstBin * bin)
3255 {
3256   GstBinClass *bclass;
3257
3258   bclass = GST_BIN_GET_CLASS (bin);
3259   if (bclass->handle_message)
3260     bclass->handle_message (bin, message);
3261   else
3262     gst_message_unref (message);
3263
3264   return GST_BUS_DROP;
3265 }
3266
3267 static void
3268 free_bin_continue_data (BinContinueData * data)
3269 {
3270   g_slice_free (BinContinueData, data);
3271 }
3272
3273 static void
3274 bin_push_state_continue (GstBin * bin, BinContinueData * data)
3275 {
3276   GST_DEBUG_OBJECT (bin, "pushing continue on thread pool");
3277   gst_element_call_async (GST_ELEMENT_CAST (bin),
3278       (GstElementCallAsyncFunc) gst_bin_continue_func, data,
3279       (GDestroyNotify) free_bin_continue_data);
3280 }
3281
3282 /* an element started an async state change, if we were not busy with a state
3283  * change, we perform a lost state.
3284  * This function is called with the OBJECT lock.
3285  */
3286 static void
3287 bin_handle_async_start (GstBin * bin)
3288 {
3289   GstState old_state, new_state;
3290   gboolean toplevel;
3291   GstMessage *amessage = NULL;
3292
3293   if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_FAILURE)
3294     goto had_error;
3295
3296   /* get our toplevel state */
3297   toplevel = BIN_IS_TOPLEVEL (bin);
3298
3299   /* prepare an ASYNC_START message, we always post the start message even if we
3300    * are busy with a state change or when we are NO_PREROLL. */
3301   if (!toplevel)
3302     /* non toplevel bin, prepare async-start for the parent */
3303     amessage = gst_message_new_async_start (GST_OBJECT_CAST (bin));
3304
3305   if (bin->polling || GST_STATE_PENDING (bin) != GST_STATE_VOID_PENDING)
3306     goto was_busy;
3307
3308   /* async starts are ignored when we are NO_PREROLL */
3309   if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_NO_PREROLL)
3310     goto was_no_preroll;
3311
3312   old_state = GST_STATE (bin);
3313
3314   /* when we PLAYING we go back to PAUSED, when preroll happens, we go back to
3315    * PLAYING after optionally redistributing the base_time. */
3316   if (old_state > GST_STATE_PAUSED)
3317     new_state = GST_STATE_PAUSED;
3318   else
3319     new_state = old_state;
3320
3321   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
3322       "lost state of %s, new %s", gst_element_state_get_name (old_state),
3323       gst_element_state_get_name (new_state));
3324
3325   GST_STATE (bin) = new_state;
3326   GST_STATE_NEXT (bin) = new_state;
3327   GST_STATE_PENDING (bin) = new_state;
3328   GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3329   GST_OBJECT_UNLOCK (bin);
3330
3331   /* post message */
3332   _priv_gst_element_state_changed (GST_ELEMENT_CAST (bin), new_state, new_state,
3333       new_state);
3334
3335 post_start:
3336   if (amessage) {
3337     /* post our ASYNC_START. */
3338     GST_DEBUG_OBJECT (bin, "posting ASYNC_START to parent");
3339     gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
3340   }
3341   GST_OBJECT_LOCK (bin);
3342
3343   return;
3344
3345 had_error:
3346   {
3347     GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "we had an error");
3348     return;
3349   }
3350 was_busy:
3351   {
3352     GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "state change busy");
3353     GST_OBJECT_UNLOCK (bin);
3354     goto post_start;
3355   }
3356 was_no_preroll:
3357   {
3358     GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "ignoring, we are NO_PREROLL");
3359     GST_OBJECT_UNLOCK (bin);
3360     goto post_start;
3361   }
3362 }
3363
3364 /* this function is called when there are no more async elements in the bin. We
3365  * post a state changed message and an ASYNC_DONE message.
3366  * This function is called with the OBJECT lock.
3367  */
3368 static void
3369 bin_handle_async_done (GstBin * bin, GstStateChangeReturn ret,
3370     gboolean flag_pending, GstClockTime running_time)
3371 {
3372   GstState current, pending, target;
3373   GstStateChangeReturn old_ret;
3374   GstState old_state, old_next;
3375   gboolean toplevel, state_changed = FALSE;
3376   GstMessage *amessage = NULL;
3377   BinContinueData *cont = NULL;
3378
3379   if (GST_STATE_RETURN (bin) == GST_STATE_CHANGE_FAILURE)
3380     goto had_error;
3381
3382   pending = GST_STATE_PENDING (bin);
3383
3384   if (bin->polling)
3385     goto was_busy;
3386
3387   /* check if there is something to commit */
3388   if (pending == GST_STATE_VOID_PENDING)
3389     goto nothing_pending;
3390
3391   old_ret = GST_STATE_RETURN (bin);
3392   GST_STATE_RETURN (bin) = ret;
3393
3394   /* move to the next target state */
3395   target = GST_STATE_TARGET (bin);
3396   pending = GST_STATE_PENDING (bin) = target;
3397
3398   amessage = gst_message_new_async_done (GST_OBJECT_CAST (bin), running_time);
3399
3400   old_state = GST_STATE (bin);
3401   /* this is the state we should go to next */
3402   old_next = GST_STATE_NEXT (bin);
3403
3404   if (old_next != GST_STATE_PLAYING) {
3405     GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3406         "committing state from %s to %s, old pending %s",
3407         gst_element_state_get_name (old_state),
3408         gst_element_state_get_name (old_next),
3409         gst_element_state_get_name (pending));
3410
3411     /* update current state */
3412     current = GST_STATE (bin) = old_next;
3413   } else {
3414     GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3415         "setting state from %s to %s, pending %s",
3416         gst_element_state_get_name (old_state),
3417         gst_element_state_get_name (old_state),
3418         gst_element_state_get_name (pending));
3419     current = old_state;
3420   }
3421
3422   /* get our toplevel state */
3423   toplevel = BIN_IS_TOPLEVEL (bin);
3424
3425   /* see if we reached the final state. If we are not toplevel, we also have to
3426    * stop here, the parent will continue our state. */
3427   if ((pending == current) || !toplevel) {
3428     GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3429         "completed state change, pending VOID");
3430
3431     /* mark VOID pending */
3432     pending = GST_STATE_VOID_PENDING;
3433     GST_STATE_PENDING (bin) = pending;
3434     GST_STATE_NEXT (bin) = GST_STATE_VOID_PENDING;
3435   } else {
3436     GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin,
3437         "continue state change, pending %s",
3438         gst_element_state_get_name (pending));
3439
3440     cont = g_slice_new (BinContinueData);
3441
3442     /* cookie to detect concurrent state change */
3443     cont->cookie = GST_ELEMENT_CAST (bin)->state_cookie;
3444     /* pending target state */
3445     cont->pending = pending;
3446     /* mark busy */
3447     GST_STATE_RETURN (bin) = GST_STATE_CHANGE_ASYNC;
3448     GST_STATE_NEXT (bin) = GST_STATE_GET_NEXT (old_state, pending);
3449   }
3450
3451   if (old_next != GST_STATE_PLAYING) {
3452     if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC) {
3453       state_changed = TRUE;
3454     }
3455   }
3456   GST_OBJECT_UNLOCK (bin);
3457
3458   if (state_changed) {
3459     _priv_gst_element_state_changed (GST_ELEMENT_CAST (bin), old_state,
3460         old_next, pending);
3461   }
3462   if (amessage) {
3463     /* post our combined ASYNC_DONE when all is ASYNC_DONE. */
3464     GST_DEBUG_OBJECT (bin, "posting ASYNC_DONE to parent");
3465     gst_element_post_message (GST_ELEMENT_CAST (bin), amessage);
3466   }
3467
3468   GST_OBJECT_LOCK (bin);
3469   if (cont) {
3470     /* toplevel, start continue state */
3471     GST_DEBUG_OBJECT (bin, "all async-done, starting state continue");
3472     bin_push_state_continue (bin, cont);
3473   } else {
3474     GST_DEBUG_OBJECT (bin, "state change complete");
3475     GST_STATE_BROADCAST (bin);
3476   }
3477   return;
3478
3479 had_error:
3480   {
3481     GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "we had an error");
3482     return;
3483   }
3484 was_busy:
3485   {
3486     GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin, "state change busy");
3487     /* if we were busy with a state change and we are requested to flag a
3488      * pending async done, we do so here */
3489     if (flag_pending)
3490       bin->priv->pending_async_done = TRUE;
3491     return;
3492   }
3493 nothing_pending:
3494   {
3495     GST_CAT_INFO_OBJECT (GST_CAT_STATES, bin, "nothing pending");
3496     return;
3497   }
3498 }
3499
3500 static void
3501 bin_do_eos (GstBin * bin)
3502 {
3503   guint32 seqnum = GST_SEQNUM_INVALID;
3504   gboolean eos;
3505
3506   GST_OBJECT_LOCK (bin);
3507   /* If all sinks are EOS, we're in PLAYING and no state change is pending
3508    * (or we're doing playing to playing and no one else will trigger posting
3509    * EOS for us) we forward the EOS message to the parent bin or application
3510    */
3511   eos = GST_STATE (bin) == GST_STATE_PLAYING
3512       && (GST_STATE_PENDING (bin) == GST_STATE_VOID_PENDING ||
3513       GST_STATE_PENDING (bin) == GST_STATE_PLAYING)
3514       && bin->priv->posted_playing && is_eos (bin, &seqnum);
3515   GST_OBJECT_UNLOCK (bin);
3516
3517   if (eos
3518       && g_atomic_int_compare_and_exchange (&bin->priv->posted_eos, FALSE,
3519           TRUE)) {
3520     GstMessage *tmessage;
3521
3522     /* Clear out any further messages, and reset posted_eos so we can
3523        detect any new EOS that happens (eg, after a seek). Since all
3524        sinks have now posted an EOS, there will be no further EOS events
3525        seen unless there is a new logical EOS */
3526     GST_OBJECT_LOCK (bin);
3527     bin_remove_messages (bin, NULL, GST_MESSAGE_EOS);
3528     bin->priv->posted_eos = FALSE;
3529     GST_OBJECT_UNLOCK (bin);
3530
3531     tmessage = gst_message_new_eos (GST_OBJECT_CAST (bin));
3532     if (seqnum != GST_SEQNUM_INVALID)
3533       gst_message_set_seqnum (tmessage, seqnum);
3534     GST_DEBUG_OBJECT (bin,
3535         "all sinks posted EOS, posting seqnum #%" G_GUINT32_FORMAT, seqnum);
3536     gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3537   } else {
3538     GST_LOG_OBJECT (bin, "Not forwarding EOS due to in progress state change, "
3539         " or already posted, or waiting for more EOS");
3540   }
3541 }
3542
3543 static void
3544 bin_do_stream_start (GstBin * bin)
3545 {
3546   guint32 seqnum = GST_SEQNUM_INVALID;
3547   gboolean stream_start;
3548   gboolean have_group_id = FALSE;
3549   guint group_id = 0;
3550
3551   GST_OBJECT_LOCK (bin);
3552   /* If all sinks are STREAM_START we forward the STREAM_START message
3553    * to the parent bin or application
3554    */
3555   stream_start = is_stream_start (bin, &seqnum, &have_group_id, &group_id);
3556   GST_OBJECT_UNLOCK (bin);
3557
3558   if (stream_start) {
3559     GstMessage *tmessage;
3560
3561     GST_OBJECT_LOCK (bin);
3562     bin_remove_messages (bin, NULL, GST_MESSAGE_STREAM_START);
3563     GST_OBJECT_UNLOCK (bin);
3564
3565     tmessage = gst_message_new_stream_start (GST_OBJECT_CAST (bin));
3566     if (seqnum != GST_SEQNUM_INVALID)
3567       gst_message_set_seqnum (tmessage, seqnum);
3568     if (have_group_id)
3569       gst_message_set_group_id (tmessage, group_id);
3570
3571     GST_DEBUG_OBJECT (bin,
3572         "all sinks posted STREAM_START, posting seqnum #%" G_GUINT32_FORMAT,
3573         seqnum);
3574     gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3575   }
3576 }
3577
3578 /* must be called without the object lock as it posts messages */
3579 static void
3580 bin_do_message_forward (GstBin * bin, GstMessage * message)
3581 {
3582   if (bin->priv->message_forward) {
3583     GstMessage *forwarded;
3584
3585     GST_DEBUG_OBJECT (bin, "pass %s message upward",
3586         GST_MESSAGE_TYPE_NAME (message));
3587
3588     /* we need to convert these messages to element messages so that our parent
3589      * bin can easily ignore them and so that the application can easily
3590      * distinguish between the internally forwarded and the real messages. */
3591     forwarded = gst_message_new_element (GST_OBJECT_CAST (bin),
3592         gst_structure_new ("GstBinForwarded",
3593             "message", GST_TYPE_MESSAGE, message, NULL));
3594
3595     gst_element_post_message (GST_ELEMENT_CAST (bin), forwarded);
3596   }
3597 }
3598
3599 static void
3600 gst_bin_update_context (GstBin * bin, GstContext * context)
3601 {
3602   GST_OBJECT_LOCK (bin);
3603   gst_bin_update_context_unlocked (bin, context);
3604   GST_OBJECT_UNLOCK (bin);
3605 }
3606
3607 static void
3608 gst_bin_update_context_unlocked (GstBin * bin, GstContext * context)
3609 {
3610   const gchar *context_type;
3611   GList *l, **contexts;
3612
3613   contexts = &GST_ELEMENT_CAST (bin)->contexts;
3614   context_type = gst_context_get_context_type (context);
3615
3616   GST_DEBUG_OBJECT (bin, "set context %p %" GST_PTR_FORMAT, context,
3617       gst_context_get_structure (context));
3618   for (l = *contexts; l; l = l->next) {
3619     GstContext *tmp = l->data;
3620     const gchar *tmp_type = gst_context_get_context_type (tmp);
3621
3622     /* Always store newest context but never replace
3623      * a persistent one by a non-persistent one */
3624     if (strcmp (context_type, tmp_type) == 0 &&
3625         (gst_context_is_persistent (context) ||
3626             !gst_context_is_persistent (tmp))) {
3627       gst_context_replace ((GstContext **) & l->data, context);
3628       break;
3629     }
3630   }
3631   /* Not found? Add */
3632   if (l == NULL) {
3633     *contexts = g_list_prepend (*contexts, gst_context_ref (context));
3634   }
3635 }
3636
3637 /* handle child messages:
3638  *
3639  * This method is called synchronously when a child posts a message on
3640  * the internal bus.
3641  *
3642  * GST_MESSAGE_EOS: This message is only posted by sinks
3643  *     in the PLAYING state. If all sinks posted the EOS message, post
3644  *     one upwards.
3645  *
3646  * GST_MESSAGE_STATE_DIRTY: Deprecated
3647  *
3648  * GST_MESSAGE_SEGMENT_START: just collect, never forward upwards. If an
3649  *     element posts segment_start twice, only the last message is kept.
3650  *
3651  * GST_MESSAGE_SEGMENT_DONE: replace SEGMENT_START message from same poster
3652  *     with the segment_done message. If there are no more segment_start
3653  *     messages, post segment_done message upwards.
3654  *
3655  * GST_MESSAGE_CLOCK_LOST: This message is posted by an element when it
3656  *     can no longer provide a clock. The default bin behaviour is to
3657  *     check if the lost clock was the one provided by the bin. If so and
3658  *     we are currently in the PLAYING state, we forward the message to
3659  *     our parent.
3660  *     This message is also generated when we remove a clock provider from
3661  *     a bin. If this message is received by the application, it should
3662  *     PAUSE the pipeline and set it back to PLAYING to force a new clock
3663  *     and a new base_time distribution.
3664  *
3665  * GST_MESSAGE_CLOCK_PROVIDE: This message is generated when an element
3666  *     can provide a clock. This mostly happens when we add a new clock
3667  *     provider to the bin. The default behaviour of the bin is to mark the
3668  *     currently selected clock as dirty, which will perform a clock
3669  *     recalculation the next time we are asked to provide a clock.
3670  *     This message is never sent to the application but is forwarded to
3671  *     the parent.
3672  *
3673  * GST_MESSAGE_ASYNC_START: Create an internal ELEMENT message that stores
3674  *     the state of the element and the fact that the element will need a
3675  *     new base_time. This message is not forwarded to the application.
3676  *
3677  * GST_MESSAGE_ASYNC_DONE: Find the internal ELEMENT message we kept for the
3678  *     element when it posted ASYNC_START. If all elements are done, post a
3679  *     ASYNC_DONE message to the parent.
3680  *
3681  * OTHER: post upwards.
3682  */
3683 static void
3684 gst_bin_handle_message_func (GstBin * bin, GstMessage * message)
3685 {
3686   GstObject *src;
3687   GstMessageType type;
3688   GstMessage *tmessage;
3689   guint32 seqnum;
3690
3691   src = GST_MESSAGE_SRC (message);
3692   type = GST_MESSAGE_TYPE (message);
3693
3694   GST_DEBUG_OBJECT (bin, "[msg %p] handling child %s message of type %s",
3695       message, src ? GST_ELEMENT_NAME (src) : "(NULL)",
3696       GST_MESSAGE_TYPE_NAME (message));
3697
3698   switch (type) {
3699     case GST_MESSAGE_ERROR:
3700     {
3701       GST_OBJECT_LOCK (bin);
3702       /* flag error */
3703       GST_DEBUG_OBJECT (bin, "got ERROR message, unlocking state change");
3704       GST_STATE_RETURN (bin) = GST_STATE_CHANGE_FAILURE;
3705       GST_STATE_BROADCAST (bin);
3706       GST_OBJECT_UNLOCK (bin);
3707
3708       goto forward;
3709     }
3710     case GST_MESSAGE_EOS:
3711     {
3712
3713       /* collect all eos messages from the children */
3714       bin_do_message_forward (bin, message);
3715       GST_OBJECT_LOCK (bin);
3716       /* ref message for future use  */
3717       bin_replace_message (bin, message, GST_MESSAGE_EOS);
3718       GST_OBJECT_UNLOCK (bin);
3719
3720       bin_do_eos (bin);
3721       break;
3722     }
3723     case GST_MESSAGE_STREAM_START:
3724     {
3725
3726       /* collect all stream_start messages from the children */
3727       GST_OBJECT_LOCK (bin);
3728       /* ref message for future use  */
3729       bin_replace_message (bin, message, GST_MESSAGE_STREAM_START);
3730       GST_OBJECT_UNLOCK (bin);
3731
3732       bin_do_stream_start (bin);
3733       break;
3734     }
3735     case GST_MESSAGE_STATE_DIRTY:
3736     {
3737       GST_WARNING_OBJECT (bin, "received deprecated STATE_DIRTY message");
3738
3739       /* free message */
3740       gst_message_unref (message);
3741       break;
3742     }
3743     case GST_MESSAGE_SEGMENT_START:{
3744       gboolean post = FALSE;
3745       GstFormat format;
3746       gint64 position;
3747
3748       gst_message_parse_segment_start (message, &format, &position);
3749       seqnum = gst_message_get_seqnum (message);
3750
3751       bin_do_message_forward (bin, message);
3752
3753       GST_OBJECT_LOCK (bin);
3754       /* if this is the first segment-start, post to parent but not to the
3755        * application */
3756       if (!find_message (bin, NULL, GST_MESSAGE_SEGMENT_START) &&
3757           (GST_OBJECT_PARENT (bin) != NULL)) {
3758         post = TRUE;
3759       }
3760       /* replace any previous segment_start message from this source
3761        * with the new segment start message */
3762       bin_replace_message (bin, message, GST_MESSAGE_SEGMENT_START);
3763       GST_OBJECT_UNLOCK (bin);
3764       if (post) {
3765         tmessage = gst_message_new_segment_start (GST_OBJECT_CAST (bin),
3766             format, position);
3767         gst_message_set_seqnum (tmessage, seqnum);
3768
3769         /* post segment start with initial format and position. */
3770         GST_DEBUG_OBJECT (bin, "posting SEGMENT_START (%u) bus message: %p",
3771             seqnum, message);
3772         gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3773       }
3774       break;
3775     }
3776     case GST_MESSAGE_SEGMENT_DONE:
3777     {
3778       gboolean post = FALSE;
3779       GstFormat format;
3780       gint64 position;
3781
3782       gst_message_parse_segment_done (message, &format, &position);
3783       seqnum = gst_message_get_seqnum (message);
3784
3785       bin_do_message_forward (bin, message);
3786
3787       GST_OBJECT_LOCK (bin);
3788       bin_replace_message (bin, message, GST_MESSAGE_SEGMENT_START);
3789       /* if there are no more segment_start messages, everybody posted
3790        * a segment_done and we can post one on the bus. */
3791
3792       /* we don't care who still has a pending segment start */
3793       if (!find_message (bin, NULL, GST_MESSAGE_SEGMENT_START)) {
3794         /* nothing found */
3795         post = TRUE;
3796         /* remove all old segment_done messages */
3797         bin_remove_messages (bin, NULL, GST_MESSAGE_SEGMENT_DONE);
3798       }
3799       GST_OBJECT_UNLOCK (bin);
3800       if (post) {
3801         tmessage = gst_message_new_segment_done (GST_OBJECT_CAST (bin),
3802             format, position);
3803         gst_message_set_seqnum (tmessage, seqnum);
3804
3805         /* post segment done with latest format and position. */
3806         GST_DEBUG_OBJECT (bin, "posting SEGMENT_DONE (%u) bus message: %p",
3807             seqnum, message);
3808         gst_element_post_message (GST_ELEMENT_CAST (bin), tmessage);
3809       }
3810       break;
3811     }
3812     case GST_MESSAGE_CLOCK_LOST:
3813     {
3814       GstClock **provided_clock_p;
3815       GstElement **clock_provider_p;
3816       gboolean playing, toplevel, provided, forward;
3817       GstClock *clock;
3818
3819       gst_message_parse_clock_lost (message, &clock);
3820
3821       GST_OBJECT_LOCK (bin);
3822       bin->clock_dirty = TRUE;
3823       /* if we lost the clock that we provided, post to parent but
3824        * only if we are not a top-level bin or PLAYING.
3825        * The reason for this is that applications should be able
3826        * to PAUSE/PLAY if they receive this message without worrying
3827        * about the state of the pipeline. */
3828       provided = (clock == bin->provided_clock);
3829       playing = (GST_STATE (bin) == GST_STATE_PLAYING);
3830       toplevel = GST_OBJECT_PARENT (bin) == NULL;
3831       forward = provided && (playing || !toplevel);
3832       if (provided) {
3833         GST_DEBUG_OBJECT (bin,
3834             "Lost clock %" GST_PTR_FORMAT " provided by %" GST_PTR_FORMAT,
3835             bin->provided_clock, bin->clock_provider);
3836         provided_clock_p = &bin->provided_clock;
3837         clock_provider_p = &bin->clock_provider;
3838         gst_object_replace ((GstObject **) provided_clock_p, NULL);
3839         gst_object_replace ((GstObject **) clock_provider_p, NULL);
3840       }
3841       GST_DEBUG_OBJECT (bin, "provided %d, playing %d, forward %d",
3842           provided, playing, forward);
3843       GST_OBJECT_UNLOCK (bin);
3844
3845       if (forward)
3846         goto forward;
3847
3848       /* free message */
3849       gst_message_unref (message);
3850       break;
3851     }
3852     case GST_MESSAGE_CLOCK_PROVIDE:
3853     {
3854       gboolean forward;
3855
3856       GST_OBJECT_LOCK (bin);
3857       bin->clock_dirty = TRUE;
3858       /* a new clock is available, post to parent but not
3859        * to the application */
3860       forward = GST_OBJECT_PARENT (bin) != NULL;
3861       GST_OBJECT_UNLOCK (bin);
3862
3863       if (forward)
3864         goto forward;
3865
3866       /* free message */
3867       gst_message_unref (message);
3868       break;
3869     }
3870     case GST_MESSAGE_ASYNC_START:
3871     {
3872       GstState target;
3873
3874       GST_DEBUG_OBJECT (bin, "ASYNC_START message %p, %s", message,
3875           src ? GST_OBJECT_NAME (src) : "(NULL)");
3876
3877       bin_do_message_forward (bin, message);
3878
3879       GST_OBJECT_LOCK (bin);
3880       /* we ignore the message if we are going to <= READY */
3881       if ((target = GST_STATE_TARGET (bin)) <= GST_STATE_READY)
3882         goto ignore_start_message;
3883
3884       /* takes ownership of the message */
3885       bin_replace_message (bin, message, GST_MESSAGE_ASYNC_START);
3886
3887       bin_handle_async_start (bin);
3888       GST_OBJECT_UNLOCK (bin);
3889       break;
3890
3891     ignore_start_message:
3892       {
3893         GST_DEBUG_OBJECT (bin, "ignoring message, target %s",
3894             gst_element_state_get_name (target));
3895         GST_OBJECT_UNLOCK (bin);
3896         gst_message_unref (message);
3897         break;
3898       }
3899     }
3900     case GST_MESSAGE_ASYNC_DONE:
3901     {
3902       GstClockTime running_time;
3903       GstState target;
3904
3905       GST_DEBUG_OBJECT (bin, "ASYNC_DONE message %p, %s", message,
3906           src ? GST_OBJECT_NAME (src) : "(NULL)");
3907
3908       gst_message_parse_async_done (message, &running_time);
3909
3910       bin_do_message_forward (bin, message);
3911
3912       GST_OBJECT_LOCK (bin);
3913       /* ignore messages if we are shutting down */
3914       if ((target = GST_STATE_TARGET (bin)) <= GST_STATE_READY)
3915         goto ignore_done_message;
3916
3917       bin_replace_message (bin, message, GST_MESSAGE_ASYNC_START);
3918       /* if there are no more ASYNC_START messages, everybody posted
3919        * a ASYNC_DONE and we can post one on the bus. When checking, we
3920        * don't care who still has a pending ASYNC_START */
3921       if (!find_message (bin, NULL, GST_MESSAGE_ASYNC_START)) {
3922         /* nothing found, remove all old ASYNC_DONE messages */
3923         bin_remove_messages (bin, NULL, GST_MESSAGE_ASYNC_DONE);
3924
3925         GST_DEBUG_OBJECT (bin, "async elements committed");
3926         /* when we get an async done message when a state change was busy, we
3927          * need to set the pending_done flag so that at the end of the state
3928          * change we can see if we need to verify pending async elements, hence
3929          * the TRUE argument here. */
3930         bin_handle_async_done (bin, GST_STATE_CHANGE_SUCCESS, TRUE,
3931             running_time);
3932       } else {
3933         GST_DEBUG_OBJECT (bin, "there are more async elements pending");
3934       }
3935       GST_OBJECT_UNLOCK (bin);
3936       break;
3937
3938     ignore_done_message:
3939       {
3940         GST_DEBUG_OBJECT (bin, "ignoring message, target %s",
3941             gst_element_state_get_name (target));
3942         GST_OBJECT_UNLOCK (bin);
3943         gst_message_unref (message);
3944         break;
3945       }
3946     }
3947     case GST_MESSAGE_STRUCTURE_CHANGE:
3948     {
3949       gboolean busy;
3950
3951       gst_message_parse_structure_change (message, NULL, NULL, &busy);
3952
3953       GST_OBJECT_LOCK (bin);
3954       if (busy) {
3955         /* while the pad is busy, avoid following it when doing state changes.
3956          * Don't update the cookie yet, we will do that after the structure
3957          * change finished and we are ready to inspect the new updated
3958          * structure. */
3959         bin_replace_message (bin, message, GST_MESSAGE_STRUCTURE_CHANGE);
3960         message = NULL;
3961       } else {
3962         /* a pad link/unlink ended, signal the state change iterator that we
3963          * need to resync by updating the structure_cookie. */
3964         bin_remove_messages (bin, GST_MESSAGE_SRC (message),
3965             GST_MESSAGE_STRUCTURE_CHANGE);
3966         if (!GST_BIN_IS_NO_RESYNC (bin))
3967           bin->priv->structure_cookie++;
3968       }
3969       GST_OBJECT_UNLOCK (bin);
3970
3971       if (message)
3972         gst_message_unref (message);
3973
3974       break;
3975     }
3976     case GST_MESSAGE_NEED_CONTEXT:{
3977       const gchar *context_type;
3978       GList *l, *contexts;
3979
3980       gst_message_parse_context_type (message, &context_type);
3981
3982       if (src) {
3983         GST_OBJECT_LOCK (bin);
3984         contexts = GST_ELEMENT_CAST (bin)->contexts;
3985         GST_LOG_OBJECT (bin, "got need-context message type: %s", context_type);
3986         for (l = contexts; l; l = l->next) {
3987           GstContext *tmp = l->data;
3988           const gchar *tmp_type = gst_context_get_context_type (tmp);
3989
3990           if (strcmp (context_type, tmp_type) == 0) {
3991             gst_element_set_context (GST_ELEMENT (src), l->data);
3992             break;
3993           }
3994         }
3995         GST_OBJECT_UNLOCK (bin);
3996
3997         /* Forward if we couldn't answer the message */
3998         if (l == NULL) {
3999           goto forward;
4000         } else {
4001           gst_message_unref (message);
4002         }
4003       } else {
4004         g_warning
4005             ("Got need-context message in bin '%s' without source element, dropping",
4006             GST_ELEMENT_NAME (bin));
4007         gst_message_unref (message);
4008       }
4009
4010       break;
4011     }
4012     case GST_MESSAGE_HAVE_CONTEXT:{
4013       GstContext *context;
4014
4015       gst_message_parse_have_context (message, &context);
4016       gst_bin_update_context (bin, context);
4017       gst_context_unref (context);
4018
4019       goto forward;
4020       break;
4021     }
4022     default:
4023       goto forward;
4024   }
4025   return;
4026
4027 forward:
4028   {
4029     /* Send all other messages upward */
4030     GST_DEBUG_OBJECT (bin, "posting message upward");
4031     gst_element_post_message (GST_ELEMENT_CAST (bin), message);
4032     return;
4033   }
4034 }
4035
4036 /* generic struct passed to all query fold methods */
4037 typedef struct
4038 {
4039   GstQuery *query;
4040   gint64 min;
4041   gint64 max;
4042   gboolean live;
4043 } QueryFold;
4044
4045 typedef void (*QueryInitFunction) (GstBin * bin, QueryFold * fold);
4046 typedef void (*QueryDoneFunction) (GstBin * bin, QueryFold * fold);
4047
4048 /* for duration/position we collect all durations/positions and take
4049  * the MAX of all valid results */
4050 static void
4051 bin_query_min_max_init (GstBin * bin, QueryFold * fold)
4052 {
4053   fold->min = 0;
4054   fold->max = -1;
4055   fold->live = FALSE;
4056 }
4057
4058 static gboolean
4059 bin_query_duration_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4060 {
4061   gboolean res = FALSE;
4062   GstObject *item = g_value_get_object (vitem);
4063   if (GST_IS_PAD (item))
4064     res = gst_pad_query (GST_PAD (item), fold->query);
4065   else
4066     res = gst_element_query (GST_ELEMENT (item), fold->query);
4067
4068   if (res) {
4069     gint64 duration;
4070
4071     g_value_set_boolean (ret, TRUE);
4072
4073     gst_query_parse_duration (fold->query, NULL, &duration);
4074
4075     GST_DEBUG_OBJECT (item, "got duration %" G_GINT64_FORMAT, duration);
4076
4077     if (duration == -1) {
4078       /* duration query succeeded, but duration is unknown */
4079       fold->max = -1;
4080       return FALSE;
4081     }
4082
4083     if (duration > fold->max)
4084       fold->max = duration;
4085   }
4086
4087   return TRUE;
4088 }
4089
4090 static void
4091 bin_query_duration_done (GstBin * bin, QueryFold * fold)
4092 {
4093   GstFormat format;
4094
4095   gst_query_parse_duration (fold->query, &format, NULL);
4096   /* store max in query result */
4097   gst_query_set_duration (fold->query, format, fold->max);
4098
4099   GST_DEBUG_OBJECT (bin, "max duration %" G_GINT64_FORMAT, fold->max);
4100 }
4101
4102 static gboolean
4103 bin_query_position_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4104 {
4105   gboolean res = FALSE;
4106   GstObject *item = g_value_get_object (vitem);
4107   if (GST_IS_PAD (item))
4108     res = gst_pad_query (GST_PAD (item), fold->query);
4109   else
4110     res = gst_element_query (GST_ELEMENT (item), fold->query);
4111
4112   if (res) {
4113     gint64 position;
4114
4115     g_value_set_boolean (ret, TRUE);
4116
4117     gst_query_parse_position (fold->query, NULL, &position);
4118
4119     GST_DEBUG_OBJECT (item, "got position %" G_GINT64_FORMAT, position);
4120
4121     if (position > fold->max)
4122       fold->max = position;
4123   }
4124
4125   return TRUE;
4126 }
4127
4128 static void
4129 bin_query_position_done (GstBin * bin, QueryFold * fold)
4130 {
4131   GstFormat format;
4132
4133   gst_query_parse_position (fold->query, &format, NULL);
4134   /* store max in query result */
4135   gst_query_set_position (fold->query, format, fold->max);
4136
4137   GST_DEBUG_OBJECT (bin, "max position %" G_GINT64_FORMAT, fold->max);
4138 }
4139
4140 static gboolean
4141 bin_query_latency_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4142 {
4143   gboolean res = FALSE;
4144   GstObject *item = g_value_get_object (vitem);
4145   if (GST_IS_PAD (item))
4146     res = gst_pad_query (GST_PAD (item), fold->query);
4147   else
4148     res = gst_element_query (GST_ELEMENT (item), fold->query);
4149   if (res) {
4150     GstClockTime min, max;
4151     gboolean live;
4152
4153     gst_query_parse_latency (fold->query, &live, &min, &max);
4154
4155     GST_DEBUG_OBJECT (item,
4156         "got latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
4157         ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
4158
4159     /* for the combined latency we collect the MAX of all min latencies and
4160      * the MIN of all max latencies */
4161     if (live) {
4162       if (min > fold->min)
4163         fold->min = min;
4164       if (fold->max == -1)
4165         fold->max = max;
4166       else if (max < fold->max)
4167         fold->max = max;
4168       if (!fold->live)
4169         fold->live = live;
4170     }
4171   } else {
4172     g_value_set_boolean (ret, FALSE);
4173     GST_DEBUG_OBJECT (item, "failed query");
4174   }
4175
4176   return TRUE;
4177 }
4178
4179 static void
4180 bin_query_latency_done (GstBin * bin, QueryFold * fold)
4181 {
4182   /* store max in query result */
4183   gst_query_set_latency (fold->query, fold->live, fold->min, fold->max);
4184
4185   GST_DEBUG_OBJECT (bin,
4186       "latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
4187       ", live %d", GST_TIME_ARGS (fold->min), GST_TIME_ARGS (fold->max),
4188       fold->live);
4189 }
4190
4191 /* generic fold, return first valid result */
4192 static gboolean
4193 bin_query_generic_fold (const GValue * vitem, GValue * ret, QueryFold * fold)
4194 {
4195   gboolean res = FALSE;
4196   GstObject *item = g_value_get_object (vitem);
4197   if (GST_IS_PAD (item))
4198     res = gst_pad_query (GST_PAD (item), fold->query);
4199   else
4200     res = gst_element_query (GST_ELEMENT (item), fold->query);
4201   if (res) {
4202     g_value_set_boolean (ret, TRUE);
4203     GST_DEBUG_OBJECT (item, "answered query %p", fold->query);
4204   }
4205
4206   /* and stop as soon as we have a valid result */
4207   return !res;
4208 }
4209
4210 /* Perform a query iteration for the given bin. The query is stored in
4211  * QueryFold and iter should be either a GstPad iterator or a
4212  * GstElement iterator. */
4213 static gboolean
4214 bin_iterate_fold (GstBin * bin, GstIterator * iter, QueryInitFunction fold_init,
4215     QueryDoneFunction fold_done, GstIteratorFoldFunction fold_func,
4216     QueryFold * fold_data, gboolean default_return)
4217 {
4218   gboolean res = default_return;
4219   GValue ret = { 0 };
4220   /* set the result of the query to FALSE initially */
4221   g_value_init (&ret, G_TYPE_BOOLEAN);
4222   g_value_set_boolean (&ret, res);
4223
4224   while (TRUE) {
4225     GstIteratorResult ires;
4226
4227     ires = gst_iterator_fold (iter, fold_func, &ret, fold_data);
4228
4229     switch (ires) {
4230       case GST_ITERATOR_RESYNC:
4231         gst_iterator_resync (iter);
4232         if (fold_init)
4233           fold_init (bin, fold_data);
4234         g_value_set_boolean (&ret, res);
4235         break;
4236       case GST_ITERATOR_OK:
4237       case GST_ITERATOR_DONE:
4238         res = g_value_get_boolean (&ret);
4239         if (fold_done != NULL && res)
4240           fold_done (bin, fold_data);
4241         goto done;
4242       default:
4243         res = FALSE;
4244         goto done;
4245     }
4246   }
4247 done:
4248   return res;
4249 }
4250
4251 static gboolean
4252 gst_bin_query (GstElement * element, GstQuery * query)
4253 {
4254   GstBin *bin = GST_BIN_CAST (element);
4255   GstIterator *iter;
4256   gboolean default_return = FALSE;
4257   gboolean res = FALSE;
4258   gboolean src_pads_query_result = FALSE;
4259   GstIteratorFoldFunction fold_func;
4260   QueryInitFunction fold_init = NULL;
4261   QueryDoneFunction fold_done = NULL;
4262   QueryFold fold_data;
4263
4264   switch (GST_QUERY_TYPE (query)) {
4265     case GST_QUERY_DURATION:
4266     {
4267       /* iterate and collect durations */
4268       fold_func = (GstIteratorFoldFunction) bin_query_duration_fold;
4269       fold_init = bin_query_min_max_init;
4270       fold_done = bin_query_duration_done;
4271       break;
4272     }
4273     case GST_QUERY_POSITION:
4274     {
4275       fold_func = (GstIteratorFoldFunction) bin_query_position_fold;
4276       fold_init = bin_query_min_max_init;
4277       fold_done = bin_query_position_done;
4278       break;
4279     }
4280     case GST_QUERY_LATENCY:
4281     {
4282       fold_func = (GstIteratorFoldFunction) bin_query_latency_fold;
4283       fold_init = bin_query_min_max_init;
4284       fold_done = bin_query_latency_done;
4285       default_return = TRUE;
4286       break;
4287     }
4288     default:
4289       fold_func = (GstIteratorFoldFunction) bin_query_generic_fold;
4290       break;
4291   }
4292
4293   fold_data.query = query;
4294
4295   iter = gst_bin_iterate_sinks (bin);
4296   GST_DEBUG_OBJECT (bin, "Sending query %p (type %s) to sink children",
4297       query, GST_QUERY_TYPE_NAME (query));
4298
4299   if (fold_init)
4300     fold_init (bin, &fold_data);
4301
4302   res =
4303       bin_iterate_fold (bin, iter, fold_init, fold_done, fold_func, &fold_data,
4304       default_return);
4305   gst_iterator_free (iter);
4306
4307   if (!res) {
4308     /* Query the source pads of the element */
4309     iter = gst_element_iterate_src_pads (element);
4310     src_pads_query_result =
4311         bin_iterate_fold (bin, iter, fold_init, fold_done, fold_func,
4312         &fold_data, default_return);
4313     gst_iterator_free (iter);
4314
4315     if (src_pads_query_result)
4316       res = TRUE;
4317   }
4318
4319   GST_DEBUG_OBJECT (bin, "query %p result %d", query, res);
4320
4321   return res;
4322 }
4323
4324 static void
4325 set_context (const GValue * item, gpointer user_data)
4326 {
4327   GstElement *element = g_value_get_object (item);
4328
4329   gst_element_set_context (element, user_data);
4330 }
4331
4332 static void
4333 gst_bin_set_context (GstElement * element, GstContext * context)
4334 {
4335   GstBin *bin;
4336   GstIterator *children;
4337
4338   g_return_if_fail (GST_IS_BIN (element));
4339
4340   bin = GST_BIN (element);
4341
4342   GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
4343
4344   children = gst_bin_iterate_elements (bin);
4345   while (gst_iterator_foreach (children, set_context,
4346           context) == GST_ITERATOR_RESYNC)
4347     gst_iterator_resync (children);
4348   gst_iterator_free (children);
4349 }
4350
4351 static gint
4352 compare_name (const GValue * velement, const gchar * name)
4353 {
4354   gint eq;
4355   GstElement *element = g_value_get_object (velement);
4356
4357   GST_OBJECT_LOCK (element);
4358   eq = strcmp (GST_ELEMENT_NAME (element), name);
4359   GST_OBJECT_UNLOCK (element);
4360
4361   return eq;
4362 }
4363
4364 /**
4365  * gst_bin_get_by_name:
4366  * @bin: a #GstBin
4367  * @name: the element name to search for
4368  *
4369  * Gets the element with the given name from a bin. This
4370  * function recurses into child bins.
4371  *
4372  * Returns: (transfer full) (nullable): the #GstElement with the given
4373  * name
4374  */
4375 GstElement *
4376 gst_bin_get_by_name (GstBin * bin, const gchar * name)
4377 {
4378   GstIterator *children;
4379   GValue result = { 0, };
4380   GstElement *element;
4381   gboolean found;
4382
4383   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4384
4385   GST_CAT_INFO (GST_CAT_PARENTAGE, "[%s]: looking up child element %s",
4386       GST_ELEMENT_NAME (bin), name);
4387
4388   children = gst_bin_iterate_recurse (bin);
4389   found = gst_iterator_find_custom (children,
4390       (GCompareFunc) compare_name, &result, (gpointer) name);
4391   gst_iterator_free (children);
4392
4393   if (found) {
4394     element = g_value_dup_object (&result);
4395     g_value_unset (&result);
4396   } else {
4397     element = NULL;
4398   }
4399
4400   return element;
4401 }
4402
4403 /**
4404  * gst_bin_get_by_name_recurse_up:
4405  * @bin: a #GstBin
4406  * @name: the element name to search for
4407  *
4408  * Gets the element with the given name from this bin. If the
4409  * element is not found, a recursion is performed on the parent bin.
4410  *
4411  * Returns: (transfer full) (nullable): the #GstElement with the given
4412  * name
4413  */
4414 GstElement *
4415 gst_bin_get_by_name_recurse_up (GstBin * bin, const gchar * name)
4416 {
4417   GstElement *result;
4418
4419   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4420   g_return_val_if_fail (name != NULL, NULL);
4421
4422   result = gst_bin_get_by_name (bin, name);
4423
4424   if (!result) {
4425     GstObject *parent;
4426
4427     parent = gst_object_get_parent (GST_OBJECT_CAST (bin));
4428     if (parent) {
4429       if (GST_IS_BIN (parent)) {
4430         result = gst_bin_get_by_name_recurse_up (GST_BIN_CAST (parent), name);
4431       }
4432       gst_object_unref (parent);
4433     }
4434   }
4435
4436   return result;
4437 }
4438
4439 static gint
4440 compare_interface (const GValue * velement, GValue * interface)
4441 {
4442   GstElement *element = g_value_get_object (velement);
4443   GType interface_type = (GType) g_value_get_pointer (interface);
4444   gint ret;
4445
4446   if (G_TYPE_CHECK_INSTANCE_TYPE (element, interface_type)) {
4447     ret = 0;
4448   } else {
4449     ret = 1;
4450   }
4451   return ret;
4452 }
4453
4454 /**
4455  * gst_bin_get_by_interface:
4456  * @bin: a #GstBin
4457  * @iface: the #GType of an interface
4458  *
4459  * Looks for an element inside the bin that implements the given
4460  * interface. If such an element is found, it returns the element.
4461  * You can cast this element to the given interface afterwards.  If you want
4462  * all elements that implement the interface, use
4463  * gst_bin_iterate_all_by_interface(). This function recurses into child bins.
4464  *
4465  * Returns: (transfer full) (nullable): A #GstElement inside the bin
4466  * implementing the interface
4467  */
4468 GstElement *
4469 gst_bin_get_by_interface (GstBin * bin, GType iface)
4470 {
4471   GstIterator *children;
4472   GValue result = { 0, };
4473   GstElement *element;
4474   gboolean found;
4475   GValue viface = { 0, };
4476
4477   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4478   g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface), NULL);
4479
4480   g_value_init (&viface, G_TYPE_POINTER);
4481   g_value_set_pointer (&viface, (gpointer) iface);
4482
4483   children = gst_bin_iterate_recurse (bin);
4484   found = gst_iterator_find_custom (children, (GCompareFunc) compare_interface,
4485       &result, &viface);
4486   gst_iterator_free (children);
4487
4488   if (found) {
4489     element = g_value_dup_object (&result);
4490     g_value_unset (&result);
4491   } else {
4492     element = NULL;
4493   }
4494   g_value_unset (&viface);
4495
4496   return element;
4497 }
4498
4499 /**
4500  * gst_bin_iterate_all_by_interface:
4501  * @bin: a #GstBin
4502  * @iface: the #GType of an interface
4503  *
4504  * Looks for all elements inside the bin that implements the given
4505  * interface. You can safely cast all returned elements to the given interface.
4506  * The function recurses inside child bins. The iterator will yield a series
4507  * of #GstElement.
4508  *
4509  * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
4510  *     for all elements in the bin implementing the given interface
4511  */
4512 GstIterator *
4513 gst_bin_iterate_all_by_interface (GstBin * bin, GType iface)
4514 {
4515   GstIterator *children;
4516   GstIterator *result;
4517   GValue viface = { 0, };
4518
4519   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4520   g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface), NULL);
4521
4522   g_value_init (&viface, G_TYPE_POINTER);
4523   g_value_set_pointer (&viface, (gpointer) iface);
4524
4525   children = gst_bin_iterate_recurse (bin);
4526   result = gst_iterator_filter (children, (GCompareFunc) compare_interface,
4527       &viface);
4528
4529   g_value_unset (&viface);
4530
4531   return result;
4532 }
4533
4534 static gint
4535 compare_factory_names (const GValue * velement, GValue * factory_name_val)
4536 {
4537   GstElement *element = g_value_get_object (velement);
4538   GstElementFactory *factory = gst_element_get_factory (element);
4539   const gchar *factory_name = g_value_get_string (factory_name_val);
4540
4541   if (factory == NULL)
4542     return -1;
4543
4544   return g_strcmp0 (GST_OBJECT_NAME (factory), factory_name);
4545 }
4546
4547 /**
4548  * gst_bin_iterate_all_by_element_factory_name:
4549  * @bin: a #GstBin
4550  * @factory_name: (not nullable): the name of the #GstElementFactory
4551  *
4552  * Looks for all elements inside the bin with the given element factory name.
4553  * The function recurses inside child bins. The iterator will yield a series of
4554  * #GstElement.
4555  *
4556  * Returns: (transfer full) (nullable): a #GstIterator of #GstElement
4557  *     for all elements in the bin with the given element factory name
4558  *
4559  * Since: 1.18
4560  */
4561 GstIterator *
4562 gst_bin_iterate_all_by_element_factory_name (GstBin * bin,
4563     const gchar * factory_name)
4564 {
4565   GstIterator *children;
4566   GstIterator *result;
4567   GValue factory_name_val = G_VALUE_INIT;
4568
4569   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
4570   g_return_val_if_fail (factory_name && *factory_name, NULL);
4571
4572   g_value_init (&factory_name_val, G_TYPE_STRING);
4573   g_value_set_string (&factory_name_val, factory_name);
4574
4575   children = gst_bin_iterate_recurse (bin);
4576   result = gst_iterator_filter (children, (GCompareFunc) compare_factory_names,
4577       &factory_name_val);
4578
4579   g_value_unset (&factory_name_val);
4580
4581   return result;
4582 }