7cdc38067bb122f30bad97ecce0154ae5a14d54e
1 /* GStreamer
2 *
3 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
4 * 2004 Wim Taymans <wim@fluendo.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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 *
23 * MT safe.
24 */
25 /**
26 * SECTION:gstbin
27 * @short_description: Base class for elements that contain other elements
28 *
29 * GstBin is the simplest of the container elements, allowing elements to
30 * become children of itself. Pads from the child elements can be ghosted to
31 * the bin, making the bin itself look transparently like any other element,
32 * allowing for deep nesting of predefined sub-pipelines.
33 *
34 * A new GstBin is created with gst_bin_new(). Use a #GstPipeline instead if you
35 * want to create a toplevel bin because a normal bin doesn't have a bus or
36 * handle clock distribution of its own.
37 *
38 * After the bin has been created you will typically add elements to it with
39 * gst_bin_add(). You can remove elements with gst_bin_remove().
40 *
41 * An element can be retrieved from a bin with gst_bin_get_by_name(), using the
42 * elements name. gst_bin_get_by_name_recurse_up() is mainly used for internal
43 * purposes and will query the parent bins when the element is not found in the
44 * current bin.
45 *
46 * An iterator of elements in a bin can be retrieved with
47 * gst_bin_iterate_elements(). Various other iterators exist to retrieve the
48 * elements in a bin.
49 *
50 * The "element_added" signal is fired whenever a new element is added to the
51 * bin. Likewise the "element_removed" signal is fired whenever an element is
52 * removed from the bin.
53 *
54 * gst_bin_unref() is used to destroy the bin.
55 */
57 #include "gst_private.h"
59 #include "gstevent.h"
60 #include "gstbin.h"
61 #include "gstmarshal.h"
62 #include "gstxml.h"
63 #include "gstinfo.h"
64 #include "gsterror.h"
66 #include "gstindex.h"
67 #include "gstindexfactory.h"
68 #include "gstutils.h"
69 #include "gstchildproxy.h"
71 GST_DEBUG_CATEGORY_STATIC (bin_debug);
72 #define GST_CAT_DEFAULT bin_debug
73 #define GST_LOG_BIN_CONTENTS(bin, text) GST_LOG_OBJECT ((bin), \
74 text ": %d elements: %u PLAYING, %u PAUSED, %u READY, %u NULL, own state: %s", \
75 (bin)->numchildren, (guint) (bin)->child_states[3], \
76 (guint) (bin)->child_states[2], (bin)->child_states[1], \
77 (bin)->child_states[0], gst_element_state_get_name (GST_STATE (bin)))
80 static GstElementDetails gst_bin_details = GST_ELEMENT_DETAILS ("Generic bin",
81 "Generic/Bin",
82 "Simple container object",
83 "Erik Walthinsen <omega@cse.ogi.edu>," "Wim Taymans <wim@fluendo.com>");
85 GType _gst_bin_type = 0;
87 static void gst_bin_dispose (GObject * object);
89 static GstStateChangeReturn gst_bin_change_state (GstElement * element,
90 GstStateChange transition);
91 static GstStateChangeReturn gst_bin_get_state (GstElement * element,
92 GstState * state, GstState * pending, GTimeVal * timeout);
94 static gboolean gst_bin_add_func (GstBin * bin, GstElement * element);
95 static gboolean gst_bin_remove_func (GstBin * bin, GstElement * element);
97 #ifndef GST_DISABLE_INDEX
98 static void gst_bin_set_index_func (GstElement * element, GstIndex * index);
99 #endif
100 static GstClock *gst_bin_provide_clock_func (GstElement * element);
101 static void gst_bin_set_clock_func (GstElement * element, GstClock * clock);
103 static gboolean gst_bin_send_event (GstElement * element, GstEvent * event);
104 static GstBusSyncReply bin_bus_handler (GstBus * bus,
105 GstMessage * message, GstBin * bin);
106 static gboolean gst_bin_query (GstElement * element, GstQuery * query);
108 #ifndef GST_DISABLE_LOADSAVE
109 static xmlNodePtr gst_bin_save_thyself (GstObject * object, xmlNodePtr parent);
110 static void gst_bin_restore_thyself (GstObject * object, xmlNodePtr self);
111 #endif
113 static gint bin_element_is_sink (GstElement * child, GstBin * bin);
115 /* Bin signals and args */
116 enum
117 {
118 ELEMENT_ADDED,
119 ELEMENT_REMOVED,
120 LAST_SIGNAL
121 };
123 enum
124 {
125 ARG_0
126 /* FILL ME */
127 };
129 static void gst_bin_base_init (gpointer g_class);
130 static void gst_bin_class_init (GstBinClass * klass);
131 static void gst_bin_init (GstBin * bin);
132 static void gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data);
134 static GstElementClass *parent_class = NULL;
135 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
137 /**
138 * gst_bin_get_type:
139 *
140 * Returns: the type of #GstBin
141 */
142 GType
143 gst_bin_get_type (void)
144 {
145 if (!_gst_bin_type) {
146 static const GTypeInfo bin_info = {
147 sizeof (GstBinClass),
148 gst_bin_base_init,
149 NULL,
150 (GClassInitFunc) gst_bin_class_init,
151 NULL,
152 NULL,
153 sizeof (GstBin),
154 0,
155 (GInstanceInitFunc) gst_bin_init,
156 NULL
157 };
158 static const GInterfaceInfo child_proxy_info = {
159 gst_bin_child_proxy_init,
160 NULL,
161 NULL
162 };
164 _gst_bin_type =
165 g_type_register_static (GST_TYPE_ELEMENT, "GstBin", &bin_info, 0);
167 g_type_add_interface_static (_gst_bin_type, GST_TYPE_CHILD_PROXY,
168 &child_proxy_info);
170 GST_DEBUG_CATEGORY_INIT (bin_debug, "bin", GST_DEBUG_BOLD,
171 "debugging info for the 'bin' container element");
172 }
173 return _gst_bin_type;
174 }
176 static void
177 gst_bin_base_init (gpointer g_class)
178 {
179 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
181 gst_element_class_set_details (gstelement_class, &gst_bin_details);
182 }
184 static GstObject *
185 gst_bin_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
186 guint index)
187 {
188 return g_list_nth_data (GST_BIN (child_proxy)->children, index);
189 }
191 guint
192 gst_bin_child_proxy_get_children_count (GstChildProxy * child_proxy)
193 {
194 return GST_BIN (child_proxy)->numchildren;
195 }
197 static void
198 gst_bin_child_proxy_init (gpointer g_iface, gpointer iface_data)
199 {
200 GstChildProxyInterface *iface = g_iface;
202 iface->get_children_count = gst_bin_child_proxy_get_children_count;
203 iface->get_child_by_index = gst_bin_child_proxy_get_child_by_index;
204 }
206 static void
207 gst_bin_class_init (GstBinClass * klass)
208 {
209 GObjectClass *gobject_class;
210 GstObjectClass *gstobject_class;
211 GstElementClass *gstelement_class;
213 gobject_class = (GObjectClass *) klass;
214 gstobject_class = (GstObjectClass *) klass;
215 gstelement_class = (GstElementClass *) klass;
217 parent_class = g_type_class_peek_parent (klass);
219 /**
220 * GstBin::element-added:
221 * @bin: the object which emitted the signal.
222 * @element: the element that was added to the bin
223 *
224 * Will be emitted if a new element was removed/added to this bin.
225 */
226 gst_bin_signals[ELEMENT_ADDED] =
227 g_signal_new ("element-added", G_TYPE_FROM_CLASS (klass),
228 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_added), NULL,
229 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
230 /**
231 * GstBin::element-removed:
232 * @bin: the object which emitted the signal.
233 * @element: the element that was removed from the bin
234 *
235 * Will be emitted if an element was removed from this bin.
236 */
237 gst_bin_signals[ELEMENT_REMOVED] =
238 g_signal_new ("element-removed", G_TYPE_FROM_CLASS (klass),
239 G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GstBinClass, element_removed), NULL,
240 NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
242 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_bin_dispose);
244 #ifndef GST_DISABLE_LOADSAVE
245 gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_bin_save_thyself);
246 gstobject_class->restore_thyself =
247 GST_DEBUG_FUNCPTR (gst_bin_restore_thyself);
248 #endif
250 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_bin_change_state);
251 gstelement_class->get_state = GST_DEBUG_FUNCPTR (gst_bin_get_state);
252 #ifndef GST_DISABLE_INDEX
253 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_bin_set_index_func);
254 #endif
255 gstelement_class->provide_clock =
256 GST_DEBUG_FUNCPTR (gst_bin_provide_clock_func);
257 gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_bin_set_clock_func);
259 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_bin_send_event);
260 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_bin_query);
262 klass->add_element = GST_DEBUG_FUNCPTR (gst_bin_add_func);
263 klass->remove_element = GST_DEBUG_FUNCPTR (gst_bin_remove_func);
264 }
266 static void
267 gst_bin_init (GstBin * bin)
268 {
269 GstBus *bus;
271 bin->numchildren = 0;
272 bin->children = NULL;
273 bin->children_cookie = 0;
274 bin->eosed = NULL;
276 /* Set up a bus for listening to child elements */
277 bus = g_object_new (gst_bus_get_type (), NULL);
278 bin->child_bus = bus;
279 gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bin_bus_handler, bin);
280 }
282 /**
283 * gst_bin_new:
284 * @name: name of new bin
285 *
286 * Create a new bin with given name.
287 *
288 * Returns: new bin
289 */
290 GstElement *
291 gst_bin_new (const gchar * name)
292 {
293 return gst_element_factory_make ("bin", name);
294 }
296 /* set the index on all elements in this bin
297 *
298 * MT safe
299 */
300 #ifndef GST_DISABLE_INDEX
301 static void
302 gst_bin_set_index_func (GstElement * element, GstIndex * index)
303 {
304 GstBin *bin;
305 GList *children;
307 bin = GST_BIN (element);
309 GST_LOCK (bin);
310 for (children = bin->children; children; children = g_list_next (children)) {
311 GstElement *child = GST_ELEMENT (children->data);
313 gst_element_set_index (child, index);
314 }
315 GST_UNLOCK (bin);
316 }
317 #endif
319 /* set the clock on all elements in this bin
320 *
321 * MT safe
322 */
323 static void
324 gst_bin_set_clock_func (GstElement * element, GstClock * clock)
325 {
326 GList *children;
327 GstBin *bin;
329 bin = GST_BIN (element);
331 GST_LOCK (bin);
332 for (children = bin->children; children; children = g_list_next (children)) {
333 GstElement *child = GST_ELEMENT (children->data);
335 gst_element_set_clock (child, clock);
336 }
337 GST_UNLOCK (bin);
338 }
340 /* get the clock for this bin by asking all of the children in this bin
341 *
342 * The ref of the returned clock in increased so unref after usage.
343 *
344 * MT safe
345 *
346 * FIXME, clock selection is not correct here.
347 */
348 static GstClock *
349 gst_bin_provide_clock_func (GstElement * element)
350 {
351 GstClock *result = NULL;
352 GstBin *bin;
353 GList *children;
355 bin = GST_BIN (element);
357 GST_LOCK (bin);
358 for (children = bin->children; children; children = g_list_next (children)) {
359 GstElement *child = GST_ELEMENT (children->data);
361 result = gst_element_provide_clock (child);
362 if (result)
363 break;
364 }
365 GST_UNLOCK (bin);
367 return result;
368 }
370 static gboolean
371 is_eos (GstBin * bin)
372 {
373 GstIterator *sinks;
374 gboolean result = TRUE;
375 gboolean done = FALSE;
377 sinks = gst_bin_iterate_sinks (bin);
378 while (!done) {
379 gpointer data;
381 switch (gst_iterator_next (sinks, &data)) {
382 case GST_ITERATOR_OK:
383 {
384 GstElement *element = GST_ELEMENT (data);
385 GList *eosed;
386 gchar *name;
388 name = gst_element_get_name (element);
389 eosed = g_list_find (bin->eosed, element);
390 if (!eosed) {
391 GST_DEBUG ("element %s did not post EOS yet", name);
392 result = FALSE;
393 done = TRUE;
394 } else {
395 GST_DEBUG ("element %s posted EOS", name);
396 }
397 g_free (name);
398 gst_object_unref (element);
399 break;
400 }
401 case GST_ITERATOR_RESYNC:
402 result = TRUE;
403 gst_iterator_resync (sinks);
404 break;
405 case GST_ITERATOR_DONE:
406 done = TRUE;
407 break;
408 default:
409 g_assert_not_reached ();
410 break;
411 }
412 }
413 gst_iterator_free (sinks);
414 return result;
415 }
417 static void
418 unlink_pads (GstPad * pad)
419 {
420 GstPad *peer;
422 if ((peer = gst_pad_get_peer (pad))) {
423 if (gst_pad_get_direction (pad) == GST_PAD_SRC)
424 gst_pad_unlink (pad, peer);
425 else
426 gst_pad_unlink (peer, pad);
427 gst_object_unref (peer);
428 }
429 gst_object_unref (pad);
430 }
432 /* add an element to this bin
433 *
434 * MT safe
435 */
436 static gboolean
437 gst_bin_add_func (GstBin * bin, GstElement * element)
438 {
439 gchar *elem_name;
440 GstIterator *it;
442 /* we obviously can't add ourself to ourself */
443 if (G_UNLIKELY (GST_ELEMENT_CAST (element) == GST_ELEMENT_CAST (bin)))
444 goto adding_itself;
446 /* get the element name to make sure it is unique in this bin. */
447 GST_LOCK (element);
448 elem_name = g_strdup (GST_ELEMENT_NAME (element));
449 GST_UNLOCK (element);
451 GST_LOCK (bin);
453 /* then check to see if the element's name is already taken in the bin,
454 * we can safely take the lock here. This check is probably bogus because
455 * you can safely change the element name after this check and before setting
456 * the object parent. The window is very small though... */
457 if (G_UNLIKELY (!gst_object_check_uniqueness (bin->children, elem_name)))
458 goto duplicate_name;
460 /* set the element's parent and add the element to the bin's list of children */
461 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (element),
462 GST_OBJECT_CAST (bin))))
463 goto had_parent;
465 /* if we add a sink we become a sink */
466 if (GST_FLAG_IS_SET (element, GST_ELEMENT_IS_SINK)) {
467 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "element \"%s\" was sink",
468 elem_name);
469 GST_FLAG_SET (bin, GST_ELEMENT_IS_SINK);
470 }
472 bin->children = g_list_prepend (bin->children, element);
473 bin->numchildren++;
474 bin->children_cookie++;
476 /* distribute the bus */
477 gst_element_set_bus (element, bin->child_bus);
479 /* propagate the current base time and clock */
480 GST_DEBUG_OBJECT (element, "setting base time %" GST_TIME_FORMAT,
481 GST_TIME_ARGS (GST_ELEMENT (bin)->base_time));
482 gst_element_set_base_time (element, GST_ELEMENT (bin)->base_time);
483 GST_DEBUG_OBJECT (element, "setting clock %p", GST_ELEMENT_CLOCK (bin));
484 gst_element_set_clock (element, GST_ELEMENT_CLOCK (bin));
486 GST_UNLOCK (bin);
488 /* unlink all linked pads */
489 it = gst_element_iterate_pads (element);
490 gst_iterator_foreach (it, (GFunc) unlink_pads, element);
491 gst_iterator_free (it);
493 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, bin, "added element \"%s\"",
494 elem_name);
495 g_free (elem_name);
497 g_signal_emit (G_OBJECT (bin), gst_bin_signals[ELEMENT_ADDED], 0, element);
499 return TRUE;
501 /* ERROR handling here */
502 adding_itself:
503 {
504 GST_LOCK (bin);
505 g_warning ("Cannot add bin %s to itself", GST_ELEMENT_NAME (bin));
506 GST_UNLOCK (bin);
507 return FALSE;
508 }
509 duplicate_name:
510 {
511 g_warning ("Name %s is not unique in bin %s, not adding",
512 elem_name, GST_ELEMENT_NAME (bin));
513 GST_UNLOCK (bin);
514 g_free (elem_name);
515 return FALSE;
516 }
517 had_parent:
518 {
519 g_warning ("Element %s already has parent", elem_name);
520 GST_UNLOCK (bin);
521 g_free (elem_name);
522 return FALSE;
523 }
524 }
527 /**
528 * gst_bin_add:
529 * @bin: #GstBin to add element to
530 * @element: #GstElement to add to bin
531 *
532 * Adds the given element to the bin. Sets the element's parent, and thus
533 * takes ownership of the element. An element can only be added to one bin.
534 *
535 * If the element's pads are linked to other pads, the pads will be unlinked
536 * before the element is added to the bin.
537 *
538 * MT safe.
539 *
540 * Returns: TRUE if the element could be added, FALSE on wrong parameters or
541 * the bin does not want to accept the element.
542 */
543 gboolean
544 gst_bin_add (GstBin * bin, GstElement * element)
545 {
546 GstBinClass *bclass;
547 gboolean result;
549 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
550 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
552 bclass = GST_BIN_GET_CLASS (bin);
554 if (G_UNLIKELY (bclass->add_element == NULL))
555 goto no_function;
557 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "adding element %s to bin %s",
558 GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin));
560 result = bclass->add_element (bin, element);
562 return result;
564 /* ERROR handling */
565 no_function:
566 {
567 g_warning ("adding elements to bin %s is not supported",
568 GST_ELEMENT_NAME (bin));
569 return FALSE;
570 }
571 }
573 /* remove an element from the bin
574 *
575 * MT safe
576 */
577 static gboolean
578 gst_bin_remove_func (GstBin * bin, GstElement * element)
579 {
580 gchar *elem_name;
581 GstIterator *it;
583 GST_LOCK (element);
584 /* Check if the element is already being removed and immediately
585 * return */
586 if (G_UNLIKELY (GST_FLAG_IS_SET (element, GST_ELEMENT_UNPARENTING)))
587 goto already_removing;
589 GST_FLAG_SET (element, GST_ELEMENT_UNPARENTING);
590 /* grab element name so we can print it */
591 elem_name = g_strdup (GST_ELEMENT_NAME (element));
592 GST_UNLOCK (element);
594 /* unlink all linked pads */
595 it = gst_element_iterate_pads (element);
596 gst_iterator_foreach (it, (GFunc) unlink_pads, element);
597 gst_iterator_free (it);
599 GST_LOCK (bin);
600 /* the element must be in the bin's list of children */
601 if (G_UNLIKELY (g_list_find (bin->children, element) == NULL))
602 goto not_in_bin;
604 /* now remove the element from the list of elements */
605 bin->children = g_list_remove (bin->children, element);
606 bin->numchildren--;
607 bin->children_cookie++;
609 /* check if we removed a sink */
610 if (GST_FLAG_IS_SET (element, GST_ELEMENT_IS_SINK)) {
611 GList *other_sink;
613 /* check if we removed the last sink */
614 other_sink = g_list_find_custom (bin->children,
615 bin, (GCompareFunc) bin_element_is_sink);
616 if (!other_sink) {
617 /* yups, we're not a sink anymore */
618 GST_FLAG_UNSET (bin, GST_ELEMENT_IS_SINK);
619 }
620 }
621 GST_UNLOCK (bin);
623 GST_CAT_INFO_OBJECT (GST_CAT_PARENTAGE, bin, "removed child \"%s\"",
624 elem_name);
625 g_free (elem_name);
627 gst_element_set_bus (element, NULL);
629 /* unlock any waiters for the state change. It is possible that
630 * we are waiting for an ASYNC state change on this element. The
631 * element cannot be added to another bin yet as it is not yet
632 * unparented. */
633 GST_STATE_LOCK (element);
634 GST_STATE_BROADCAST (element);
635 GST_STATE_UNLOCK (element);
637 /* we ref here because after the _unparent() the element can be disposed
638 * and we still need it to reset the UNPARENTING flag and fire a signal. */
639 gst_object_ref (element);
640 gst_object_unparent (GST_OBJECT_CAST (element));
642 GST_LOCK (element);
643 GST_FLAG_UNSET (element, GST_ELEMENT_UNPARENTING);
644 GST_UNLOCK (element);
646 g_signal_emit (G_OBJECT (bin), gst_bin_signals[ELEMENT_REMOVED], 0, element);
648 /* element is really out of our control now */
649 gst_object_unref (element);
651 return TRUE;
653 /* ERROR handling */
654 not_in_bin:
655 {
656 g_warning ("Element %s is not in bin %s", elem_name,
657 GST_ELEMENT_NAME (bin));
658 GST_UNLOCK (bin);
659 g_free (elem_name);
660 return FALSE;
661 }
662 already_removing:
663 {
664 GST_UNLOCK (element);
665 return FALSE;
666 }
667 }
669 /**
670 * gst_bin_remove:
671 * @bin: #GstBin to remove element from
672 * @element: #GstElement to remove
673 *
674 * Remove the element from its associated bin, unparenting it as well.
675 * Unparenting the element means that the element will be dereferenced,
676 * so if the bin holds the only reference to the element, the element
677 * will be freed in the process of removing it from the bin. If you
678 * want the element to still exist after removing, you need to call
679 * gst_object_ref() before removing it from the bin.
680 *
681 * If the element's pads are linked to other pads, the pads will be unlinked
682 * before the element is removed from the bin.
683 *
684 * MT safe.
685 *
686 * Returns: TRUE if the element could be removed, FALSE on wrong parameters or
687 * the bin does not want to remove the element.
688 */
689 gboolean
690 gst_bin_remove (GstBin * bin, GstElement * element)
691 {
692 GstBinClass *bclass;
693 gboolean result;
695 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
696 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
698 bclass = GST_BIN_GET_CLASS (bin);
700 if (G_UNLIKELY (bclass->remove_element == NULL))
701 goto no_function;
703 GST_CAT_DEBUG (GST_CAT_PARENTAGE, "removing element %s from bin %s",
704 GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (bin));
706 result = bclass->remove_element (bin, element);
708 return result;
710 /* ERROR handling */
711 no_function:
712 {
713 g_warning ("removing elements from bin %s is not supported",
714 GST_ELEMENT_NAME (bin));
715 return FALSE;
716 }
717 }
719 static GstIteratorItem
720 iterate_child (GstIterator * it, GstElement * child)
721 {
722 gst_object_ref (child);
723 return GST_ITERATOR_ITEM_PASS;
724 }
726 /**
727 * gst_bin_iterate_elements:
728 * @bin: #Gstbin to iterate the elements of
729 *
730 * Get an iterator for the elements in this bin.
731 * Each element will have its refcount increased, so unref
732 * after use.
733 *
734 * MT safe.
735 *
736 * Returns: a #GstIterator of #GstElements. gst_iterator_free after
737 * use. returns NULL when passing bad parameters.
738 */
739 GstIterator *
740 gst_bin_iterate_elements (GstBin * bin)
741 {
742 GstIterator *result;
744 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
746 GST_LOCK (bin);
747 /* add ref because the iterator refs the bin. When the iterator
748 * is freed it will unref the bin again using the provided dispose
749 * function. */
750 gst_object_ref (bin);
751 result = gst_iterator_new_list (GST_TYPE_ELEMENT,
752 GST_GET_LOCK (bin),
753 &bin->children_cookie,
754 &bin->children,
755 bin,
756 (GstIteratorItemFunction) iterate_child,
757 (GstIteratorDisposeFunction) gst_object_unref);
758 GST_UNLOCK (bin);
760 return result;
761 }
763 static GstIteratorItem
764 iterate_child_recurse (GstIterator * it, GstElement * child)
765 {
766 gst_object_ref (child);
767 if (GST_IS_BIN (child)) {
768 GstIterator *other = gst_bin_iterate_recurse (GST_BIN (child));
770 gst_iterator_push (it, other);
771 }
772 return GST_ITERATOR_ITEM_PASS;
773 }
775 /**
776 * gst_bin_iterate_recurse:
777 * @bin: #Gstbin to iterate the elements of
778 *
779 * Get an iterator for the elements in this bin.
780 * Each element will have its refcount increased, so unref
781 * after use. This iterator recurses into GstBin children.
782 *
783 * MT safe.
784 *
785 * Returns: a #GstIterator of #GstElements. gst_iterator_free after
786 * use. returns NULL when passing bad parameters.
787 */
788 GstIterator *
789 gst_bin_iterate_recurse (GstBin * bin)
790 {
791 GstIterator *result;
793 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
795 GST_LOCK (bin);
796 /* add ref because the iterator refs the bin. When the iterator
797 * is freed it will unref the bin again using the provided dispose
798 * function. */
799 gst_object_ref (bin);
800 result = gst_iterator_new_list (GST_TYPE_ELEMENT,
801 GST_GET_LOCK (bin),
802 &bin->children_cookie,
803 &bin->children,
804 bin,
805 (GstIteratorItemFunction) iterate_child_recurse,
806 (GstIteratorDisposeFunction) gst_object_unref);
807 GST_UNLOCK (bin);
809 return result;
810 }
812 /* returns 0 when TRUE because this is a GCompareFunc */
813 /* MT safe */
814 static gint
815 bin_element_is_sink (GstElement * child, GstBin * bin)
816 {
817 gboolean is_sink;
819 /* we lock the child here for the remainder of the function to
820 * get its name safely. */
821 GST_LOCK (child);
822 is_sink = GST_FLAG_IS_SET (child, GST_ELEMENT_IS_SINK);
824 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, bin,
825 "child %s %s sink", GST_OBJECT_NAME (child), is_sink ? "is" : "is not");
827 GST_UNLOCK (child);
828 return is_sink ? 0 : 1;
829 }
831 static gint
832 sink_iterator_filter (GstElement * child, GstBin * bin)
833 {
834 if (bin_element_is_sink (child, bin) == 0) {
835 /* returns 0 because this is a GCompareFunc */
836 return 0;
837 } else {
838 /* child carries a ref from gst_bin_iterate_elements -- drop if not passing
839 through */
840 gst_object_unref ((GstObject *) child);
841 return 1;
842 }
843 }
845 /**
846 * gst_bin_iterate_sinks:
847 * @bin: #Gstbin to iterate on
848 *
849 * Get an iterator for the sink elements in this bin.
850 * Each element will have its refcount increased, so unref
851 * after use.
852 *
853 * The sink elements are those without any linked srcpads.
854 *
855 * MT safe.
856 *
857 * Returns: a #GstIterator of #GstElements. gst_iterator_free after use.
858 */
859 GstIterator *
860 gst_bin_iterate_sinks (GstBin * bin)
861 {
862 GstIterator *children;
863 GstIterator *result;
865 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
867 children = gst_bin_iterate_elements (bin);
868 result = gst_iterator_filter (children,
869 (GCompareFunc) sink_iterator_filter, bin);
871 return result;
872 }
874 /* 2 phases:
875 * 1) check state of all children with 0 timeout to find ERROR and
876 * NO_PREROLL elements. return if found.
877 * 2) perform full blocking wait with requested timeout.
878 *
879 * 2) cannot be performed when 1) returns results as the sinks might
880 * not be able to complete the state change making 2) block forever.
881 *
882 * MT safe
883 */
884 static GstStateChangeReturn
885 gst_bin_get_state (GstElement * element, GstState * state,
886 GstState * pending, GTimeVal * timeout)
887 {
888 GstBin *bin = GST_BIN (element);
889 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
890 GList *children;
891 guint32 children_cookie;
892 gboolean have_no_preroll;
894 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
896 /* lock bin, no element can be added or removed between going into
897 * the quick scan and the blocking wait. */
898 GST_LOCK (bin);
900 restart:
901 have_no_preroll = FALSE;
903 /* first we need to poll with a non zero timeout to make sure we don't block
904 * on the sinks when we have NO_PREROLL elements. This is why we do
905 * a quick check if there are still NO_PREROLL elements. We also
906 * catch the error elements this way. */
907 {
908 GTimeVal tv;
909 gboolean have_async = FALSE;
911 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "checking for NO_PREROLL");
912 /* use 0 timeout so we don't block on the sinks */
913 GST_TIME_TO_TIMEVAL (0, tv);
914 children = bin->children;
915 children_cookie = bin->children_cookie;
916 while (children) {
917 GstElement *child = GST_ELEMENT_CAST (children->data);
919 gst_object_ref (child);
920 /* now we release the lock to enter a non blocking wait. We
921 * release the lock anyway since we can. */
922 GST_UNLOCK (bin);
924 ret = gst_element_get_state (child, NULL, NULL, &tv);
926 gst_object_unref (child);
928 /* now grab the lock to iterate to the next child */
929 GST_LOCK (bin);
930 if (G_UNLIKELY (children_cookie != bin->children_cookie)) {
931 /* child added/removed during state change, restart. We need
932 * to restart with the quick check as a no-preroll element could
933 * have been added here and we don't want to block on sinks then.*/
934 goto restart;
935 }
937 switch (ret) {
938 /* report FAILURE immediatly */
939 case GST_STATE_CHANGE_FAILURE:
940 goto done;
941 case GST_STATE_CHANGE_NO_PREROLL:
942 /* we have to continue scanning as there might be
943 * ERRORS too */
944 have_no_preroll = TRUE;
945 break;
946 case GST_STATE_CHANGE_ASYNC:
947 have_async = TRUE;
948 break;
949 default:
950 break;
951 }
952 children = g_list_next (children);
953 }
954 /* if we get here, we have no FAILURES, check for any NO_PREROLL
955 * elements then. */
956 if (have_no_preroll) {
957 ret = GST_STATE_CHANGE_NO_PREROLL;
958 goto done;
959 }
961 /* if we get here, no NO_PREROLL elements are in the pipeline */
962 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "no NO_PREROLL elements");
964 /* if no ASYNC elements exist we don't even have to poll with a
965 * timeout again */
966 if (!have_async) {
967 ret = GST_STATE_CHANGE_SUCCESS;
968 goto done;
969 }
970 }
972 /* next we poll all children for their state to see if one of them
973 * is still busy with its state change. We did not release the bin lock
974 * yet so the elements are the same as the ones from the quick scan. */
975 children = bin->children;
976 children_cookie = bin->children_cookie;
977 while (children) {
978 GstElement *child = GST_ELEMENT_CAST (children->data);
980 gst_object_ref (child);
981 /* now we release the lock to enter the potentialy blocking wait */
982 GST_UNLOCK (bin);
984 /* ret is ASYNC if some child is still performing the state change
985 * ater the timeout. */
986 ret = gst_element_get_state (child, NULL, NULL, timeout);
988 gst_object_unref (child);
990 /* now grab the lock to iterate to the next child */
991 GST_LOCK (bin);
992 if (G_UNLIKELY (children_cookie != bin->children_cookie)) {
993 /* child added/removed during state change, restart. We need
994 * to restart with the quick check as a no-preroll element could
995 * have been added here and we don't want to block on sinks then.*/
996 goto restart;
997 }
999 switch (ret) {
1000 case GST_STATE_CHANGE_SUCCESS:
1001 break;
1002 case GST_STATE_CHANGE_FAILURE:
1003 case GST_STATE_CHANGE_NO_PREROLL:
1004 /* report FAILURE and NO_PREROLL immediatly */
1005 goto done;
1006 case GST_STATE_CHANGE_ASYNC:
1007 goto done;
1008 default:
1009 g_assert_not_reached ();
1010 }
1011 children = g_list_next (children);
1012 }
1013 /* if we got here, all elements can do preroll */
1014 have_no_preroll = FALSE;
1016 done:
1017 GST_UNLOCK (bin);
1019 /* now we can take the state lock, it is possible that new elements
1020 * are added now and we still report the old state. No problem though as
1021 * the return is still consistent, the effect is as if the element was
1022 * added after this function completed. */
1023 GST_STATE_LOCK (bin);
1024 switch (ret) {
1025 case GST_STATE_CHANGE_SUCCESS:
1026 /* we can commit the state */
1027 gst_element_commit_state (element);
1028 break;
1029 case GST_STATE_CHANGE_FAILURE:
1030 /* some element failed, abort the state change */
1031 gst_element_abort_state (element);
1032 break;
1033 default:
1034 /* other cases are just passed along */
1035 break;
1036 }
1038 /* and report the state if needed */
1039 if (state)
1040 *state = GST_STATE (element);
1041 if (pending)
1042 *pending = GST_STATE_PENDING (element);
1044 GST_STATE_NO_PREROLL (element) = have_no_preroll;
1046 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1047 "state current: %s, pending: %s, error: %d, no_preroll: %d, result: %d",
1048 gst_element_state_get_name (GST_STATE (element)),
1049 gst_element_state_get_name (GST_STATE_PENDING (element)),
1050 GST_STATE_ERROR (element), GST_STATE_NO_PREROLL (element), ret);
1052 GST_STATE_UNLOCK (bin);
1054 return ret;
1055 }
1057 /***********************************************
1058 * Topologically sorted iterator
1059 * see http://en.wikipedia.org/wiki/Topological_sorting
1060 *
1061 * For each element in the graph, an entry is kept in a HashTable
1062 * with its number of srcpad connections (degree).
1063 * We then change state of all elements without dependencies
1064 * (degree 0) and decrement the degree of all elements connected
1065 * on the sinkpads. When an element reaches degree 0, its state is
1066 * changed next.
1067 * When all elements are handled the algorithm stops.
1068 */
1069 typedef struct _GstBinSortIterator
1070 {
1071 GstIterator it;
1072 GQueue *queue; /* elements queued for state change */
1073 GstBin *bin; /* bin we iterate */
1074 gint mode; /* adding or removing dependency */
1075 GstElement *best; /* next element with least dependencies */
1076 gint best_deg; /* best degree */
1077 GHashTable *hash; /* has table with element dependencies */
1078 } GstBinSortIterator;
1080 /* we add and subtract 1 to make sure we don't confuse NULL and 0 */
1081 #define HASH_SET_DEGREE(bit, elem, deg) \
1082 g_hash_table_replace (bit->hash, elem, GINT_TO_POINTER(deg+1))
1083 #define HASH_GET_DEGREE(bit, elem) \
1084 (GPOINTER_TO_INT(g_hash_table_lookup (bit->hash, elem))-1)
1086 /* add element to queue of next elements in the iterator.
1087 * We push at the tail to give higher priority elements a
1088 * chance first */
1089 static void
1090 add_to_queue (GstBinSortIterator * bit, GstElement * element)
1091 {
1092 GST_DEBUG ("%s add to queue", GST_ELEMENT_NAME (element));
1093 gst_object_ref (element);
1094 g_queue_push_tail (bit->queue, element);
1095 HASH_SET_DEGREE (bit, element, -1);
1096 }
1098 /* clear the queue, unref all objects as we took a ref when
1099 * we added them to the queue */
1100 static void
1101 clear_queue (GQueue * queue)
1102 {
1103 gpointer p;
1105 while ((p = g_queue_pop_head (queue)))
1106 gst_object_unref (p);
1107 }
1109 /* set all degrees to 0. Elements marked as a sink are
1110 * added to the queue immediatly. */
1111 static void
1112 reset_degree (GstElement * element, GstBinSortIterator * bit)
1113 {
1114 /* sinks are added right away */
1115 if (GST_FLAG_IS_SET (element, GST_ELEMENT_IS_SINK)) {
1116 add_to_queue (bit, element);
1117 } else {
1118 /* others are marked with 0 and handled when sinks are done */
1119 HASH_SET_DEGREE (bit, element, 0);
1120 }
1121 }
1123 /* adjust the degree of all elements connected to the given
1124 * element. If an degree of an element drops to 0, it is
1125 * added to the queue of elements to schedule next.
1126 *
1127 * We have to make sure not to cross the bin boundary this element
1128 * belongs to.
1129 */
1130 static void
1131 update_degree (GstElement * element, GstBinSortIterator * bit)
1132 {
1133 gboolean linked = FALSE;
1135 GST_LOCK (element);
1136 /* don't touch degree is element has no sourcepads */
1137 if (element->numsinkpads != 0) {
1138 /* loop over all sinkpads, decrement degree for all connected
1139 * elements in this bin */
1140 GList *pads;
1142 for (pads = element->sinkpads; pads; pads = g_list_next (pads)) {
1143 GstPad *peer;
1145 if ((peer = gst_pad_get_peer (GST_PAD_CAST (pads->data)))) {
1146 GstElement *peer_element;
1148 if ((peer_element = gst_pad_get_parent_element (peer))) {
1149 GST_LOCK (peer_element);
1150 if (GST_OBJECT_CAST (peer_element)->parent ==
1151 GST_OBJECT_CAST (bit->bin)) {
1152 gint old_deg, new_deg;
1154 old_deg = HASH_GET_DEGREE (bit, peer_element);
1155 new_deg = old_deg + bit->mode;
1157 GST_DEBUG ("change element %s, degree %d->%d, linked to %s",
1158 GST_ELEMENT_NAME (peer_element),
1159 old_deg, new_deg, GST_ELEMENT_NAME (element));
1161 /* update degree */
1162 if (new_deg == 0) {
1163 /* degree hit 0, add to queue */
1164 add_to_queue (bit, peer_element);
1165 } else {
1166 HASH_SET_DEGREE (bit, peer_element, new_deg);
1167 }
1168 linked = TRUE;
1169 }
1170 GST_UNLOCK (peer_element);
1171 gst_object_unref (peer_element);
1172 }
1173 gst_object_unref (peer);
1174 }
1175 }
1176 }
1177 if (!linked) {
1178 GST_DEBUG ("element %s not linked to anything", GST_ELEMENT_NAME (element));
1179 }
1180 GST_UNLOCK (element);
1181 }
1183 /* find the next best element not handled yet. This is the one
1184 * with the lowest non-negative degree */
1185 static void
1186 find_element (GstElement * element, GstBinSortIterator * bit)
1187 {
1188 gint degree;
1190 /* element is already handled */
1191 if ((degree = HASH_GET_DEGREE (bit, element)) < 0)
1192 return;
1194 /* first element or element with smaller degree */
1195 if (bit->best == NULL || bit->best_deg > degree) {
1196 bit->best = element;
1197 bit->best_deg = degree;
1198 }
1199 }
1201 /* get next element in iterator. the returned element has the
1202 * refcount increased */
1203 static GstIteratorResult
1204 gst_bin_sort_iterator_next (GstBinSortIterator * bit, gpointer * result)
1205 {
1206 /* empty queue, we have to find a next best element */
1207 if (g_queue_is_empty (bit->queue)) {
1208 bit->best = NULL;
1209 bit->best_deg = G_MAXINT;
1210 g_list_foreach (bit->bin->children, (GFunc) find_element, bit);
1211 if (bit->best) {
1212 if (bit->best_deg != 0) {
1213 /* we don't fail on this one yet */
1214 g_warning ("loop detected in the graph !!");
1215 }
1216 /* best unhandled element, schedule as next element */
1217 GST_DEBUG ("queue empty, next best: %s", GST_ELEMENT_NAME (bit->best));
1218 gst_object_ref (bit->best);
1219 HASH_SET_DEGREE (bit, bit->best, -1);
1220 *result = bit->best;
1221 } else {
1222 GST_DEBUG ("queue empty, elements exhausted");
1223 /* no more unhandled elements, we are done */
1224 return GST_ITERATOR_DONE;
1225 }
1226 } else {
1227 /* everything added to the queue got reffed */
1228 *result = g_queue_pop_head (bit->queue);
1229 }
1231 GST_DEBUG ("queue head gives %s", GST_ELEMENT_NAME (*result));
1232 /* update degrees of linked elements */
1233 update_degree (GST_ELEMENT_CAST (*result), bit);
1235 return GST_ITERATOR_OK;
1236 }
1238 /* clear queues, recalculate the degrees and restart. */
1239 static void
1240 gst_bin_sort_iterator_resync (GstBinSortIterator * bit)
1241 {
1242 clear_queue (bit->queue);
1243 /* reset degrees */
1244 g_list_foreach (bit->bin->children, (GFunc) reset_degree, bit);
1245 /* calc degrees, incrementing */
1246 bit->mode = 1;
1247 g_list_foreach (bit->bin->children, (GFunc) update_degree, bit);
1248 /* for the rest of the function we decrement the degrees */
1249 bit->mode = -1;
1250 }
1252 /* clear queues, unref bin and free iterator. */
1253 static void
1254 gst_bin_sort_iterator_free (GstBinSortIterator * bit)
1255 {
1256 clear_queue (bit->queue);
1257 g_queue_free (bit->queue);
1258 g_hash_table_destroy (bit->hash);
1259 gst_object_unref (bit->bin);
1260 g_free (bit);
1261 }
1263 /**
1264 * gst_bin_iterate_sorted:
1265 * @bin: #Gstbin to iterate on
1266 *
1267 * Get an iterator for the elements in this bin in topologically
1268 * sorted order. This means that the elements are returned from
1269 * the most downstream elements (sinks) to the sources.
1270 *
1271 * This function is used internally to perform the state changes
1272 * of the bin elements.
1273 *
1274 * Each element will have its refcount increased, so unref
1275 * after use.
1276 *
1277 * MT safe.
1278 *
1279 * Returns: a #GstIterator of #GstElements. gst_iterator_free after use.
1280 */
1281 GstIterator *
1282 gst_bin_iterate_sorted (GstBin * bin)
1283 {
1284 GstBinSortIterator *result;
1286 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1288 GST_LOCK (bin);
1289 gst_object_ref (bin);
1290 /* we don't need a NextFunction because we ref the items in the _next
1291 * method already */
1292 result = (GstBinSortIterator *)
1293 gst_iterator_new (GST_TYPE_ELEMENT,
1294 sizeof (GstBinSortIterator),
1295 GST_GET_LOCK (bin),
1296 &bin->children_cookie,
1297 (GstIteratorNextFunction) gst_bin_sort_iterator_next,
1298 (GstIteratorItemFunction) NULL,
1299 (GstIteratorResyncFunction) gst_bin_sort_iterator_resync,
1300 (GstIteratorFreeFunction) gst_bin_sort_iterator_free);
1301 result->queue = g_queue_new ();
1302 result->hash = g_hash_table_new (NULL, NULL);
1303 result->bin = bin;
1304 gst_bin_sort_iterator_resync (result);
1305 GST_UNLOCK (bin);
1307 return (GstIterator *) result;
1308 }
1310 static GstStateChangeReturn
1311 gst_bin_element_set_state (GstBin * bin, GstElement * element, GstState pending)
1312 {
1313 GstStateChangeReturn ret;
1314 gboolean locked;
1316 /* peel off the locked flag */
1317 GST_LOCK (element);
1318 locked = GST_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1319 GST_UNLOCK (element);
1321 /* skip locked elements */
1322 if (G_UNLIKELY (locked)) {
1323 ret = GST_STATE_CHANGE_SUCCESS;
1324 goto done;
1325 }
1327 /* change state */
1328 ret = gst_element_set_state (element, pending);
1330 done:
1331 return ret;
1332 }
1334 static GstStateChangeReturn
1335 gst_bin_change_state (GstElement * element, GstStateChange transition)
1336 {
1337 GstBin *bin;
1338 GstStateChangeReturn ret;
1339 GstState old_state, pending;
1340 gboolean have_async;
1341 gboolean have_no_preroll;
1342 GstClockTime base_time;
1343 GstIterator *it;
1344 gboolean done;
1346 /* we don't need to take the STATE_LOCK, it is already taken */
1347 old_state = GST_STATE (element);
1348 pending = GST_STATE_PENDING (element);
1350 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1351 "changing state of children from %s to %s",
1352 gst_element_state_get_name (old_state),
1353 gst_element_state_get_name (pending));
1355 if (pending == GST_STATE_VOID_PENDING)
1356 return GST_STATE_CHANGE_SUCCESS;
1358 bin = GST_BIN_CAST (element);
1360 /* Clear eosed element list on READY-> PAUSED */
1361 if (transition == GST_STATE_CHANGE_READY_TO_PAUSED) {
1362 g_list_free (bin->eosed);
1363 bin->eosed = NULL;
1364 }
1366 /* iterate in state change order */
1367 it = gst_bin_iterate_sorted (bin);
1369 restart:
1370 /* take base time */
1371 base_time = element->base_time;
1373 have_async = FALSE;
1374 have_no_preroll = FALSE;
1376 done = FALSE;
1377 while (!done) {
1378 gpointer data;
1380 switch (gst_iterator_next (it, &data)) {
1381 case GST_ITERATOR_OK:
1382 {
1383 GstElement *element;
1385 element = GST_ELEMENT_CAST (data);
1387 /* set base time on element */
1388 gst_element_set_base_time (element, base_time);
1390 /* set state now */
1391 ret = gst_bin_element_set_state (bin, element, pending);
1393 switch (ret) {
1394 case GST_STATE_CHANGE_SUCCESS:
1395 GST_CAT_DEBUG (GST_CAT_STATES,
1396 "child '%s' changed state to %d(%s) successfully",
1397 GST_ELEMENT_NAME (element), pending,
1398 gst_element_state_get_name (pending));
1399 break;
1400 case GST_STATE_CHANGE_ASYNC:
1401 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1402 "child '%s' is changing state asynchronously",
1403 GST_ELEMENT_NAME (element));
1404 have_async = TRUE;
1405 break;
1406 case GST_STATE_CHANGE_FAILURE:
1407 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1408 "child '%s' failed to go to state %d(%s)",
1409 GST_ELEMENT_NAME (element),
1410 pending, gst_element_state_get_name (pending));
1411 gst_object_unref (element);
1412 goto done;
1413 case GST_STATE_CHANGE_NO_PREROLL:
1414 GST_CAT_DEBUG (GST_CAT_STATES,
1415 "child '%s' changed state to %d(%s) successfully without preroll",
1416 GST_ELEMENT_NAME (element), pending,
1417 gst_element_state_get_name (pending));
1418 have_no_preroll = TRUE;
1419 break;
1420 default:
1421 g_assert_not_reached ();
1422 break;
1423 }
1424 gst_object_unref (element);
1425 break;
1426 }
1427 case GST_ITERATOR_RESYNC:
1428 GST_CAT_DEBUG (GST_CAT_STATES, "iterator doing resync");
1429 gst_iterator_resync (it);
1430 goto restart;
1431 break;
1432 default:
1433 case GST_ITERATOR_DONE:
1434 GST_CAT_DEBUG (GST_CAT_STATES, "iterator done");
1435 done = TRUE;
1436 break;
1437 }
1438 }
1440 if (have_no_preroll) {
1441 ret = GST_STATE_CHANGE_NO_PREROLL;
1442 } else if (have_async) {
1443 ret = GST_STATE_CHANGE_ASYNC;
1444 } else {
1445 ret = parent_class->change_state (element, transition);
1446 }
1448 done:
1449 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1450 "done changing bin's state from %s to %s, now in %s, ret %d",
1451 gst_element_state_get_name (old_state),
1452 gst_element_state_get_name (pending),
1453 gst_element_state_get_name (GST_STATE (element)), ret);
1455 gst_iterator_free (it);
1457 return ret;
1458 }
1460 static void
1461 gst_bin_dispose (GObject * object)
1462 {
1463 GstBin *bin = GST_BIN (object);
1465 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
1467 g_list_free (bin->eosed);
1468 bin->eosed = NULL;
1469 gst_object_unref (bin->child_bus);
1470 bin->child_bus = NULL;
1472 while (bin->children) {
1473 gst_bin_remove (bin, GST_ELEMENT (bin->children->data));
1474 }
1475 if (G_UNLIKELY (bin->children != NULL)) {
1476 g_critical ("could not remove elements from bin %s",
1477 GST_STR_NULL (GST_OBJECT_NAME (object)));
1478 }
1480 G_OBJECT_CLASS (parent_class)->dispose (object);
1481 }
1483 /*
1484 * This function is a utility event handler for seek events.
1485 * It will send the event to all sinks.
1486 * Applications are free to override this behaviour and
1487 * implement their own seek handler, but this will work for
1488 * pretty much all cases in practice.
1489 */
1490 static gboolean
1491 gst_bin_send_event (GstElement * element, GstEvent * event)
1492 {
1493 GstBin *bin = GST_BIN (element);
1494 GstIterator *iter;
1495 gboolean res = TRUE;
1496 gboolean done = FALSE;
1498 iter = gst_bin_iterate_sinks (bin);
1499 GST_DEBUG_OBJECT (bin, "Sending event to sink children");
1501 while (!done) {
1502 gpointer data;
1504 switch (gst_iterator_next (iter, &data)) {
1505 case GST_ITERATOR_OK:
1506 {
1507 GstElement *sink;
1509 gst_event_ref (event);
1510 sink = GST_ELEMENT_CAST (data);
1511 res &= gst_element_send_event (sink, event);
1512 gst_object_unref (sink);
1513 break;
1514 }
1515 case GST_ITERATOR_RESYNC:
1516 gst_iterator_resync (iter);
1517 res = TRUE;
1518 break;
1519 default:
1520 case GST_ITERATOR_DONE:
1521 done = TRUE;
1522 break;
1523 }
1524 }
1525 gst_iterator_free (iter);
1526 gst_event_unref (event);
1528 return res;
1529 }
1531 static GstBusSyncReply
1532 bin_bus_handler (GstBus * bus, GstMessage * message, GstBin * bin)
1533 {
1534 GST_DEBUG_OBJECT (bin, "[msg %p] handling child message of type %s",
1535 message, gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
1537 switch (GST_MESSAGE_TYPE (message)) {
1538 case GST_MESSAGE_EOS:{
1539 GstObject *src = GST_MESSAGE_SRC (message);
1541 if (src) {
1542 gchar *name;
1544 name = gst_object_get_name (src);
1545 GST_DEBUG_OBJECT (bin, "got EOS message from %s", name);
1546 g_free (name);
1548 /* collect all eos messages from the children */
1549 GST_LOCK (bin->child_bus);
1550 bin->eosed = g_list_prepend (bin->eosed, src);
1551 GST_UNLOCK (bin->child_bus);
1553 /* if we are completely EOS, we forward an EOS message */
1554 if (is_eos (bin)) {
1555 GST_DEBUG_OBJECT (bin, "all sinks posted EOS");
1556 gst_element_post_message (GST_ELEMENT (bin),
1557 gst_message_new_eos (GST_OBJECT (bin)));
1558 }
1559 } else {
1560 GST_DEBUG_OBJECT (bin, "got EOS message from (NULL), not processing");
1561 }
1563 /* we drop all EOS messages */
1564 gst_message_unref (message);
1565 break;
1566 }
1567 default:
1568 /* Send all other messages upward */
1569 GST_DEBUG_OBJECT (bin, "posting message upward");
1570 gst_element_post_message (GST_ELEMENT (bin), message);
1571 break;
1572 }
1574 return GST_BUS_DROP;
1575 }
1577 static gboolean
1578 gst_bin_query (GstElement * element, GstQuery * query)
1579 {
1580 GstBin *bin = GST_BIN (element);
1581 GstIterator *iter;
1582 gboolean res = FALSE, done = FALSE;
1584 iter = gst_bin_iterate_sinks (bin);
1585 GST_DEBUG_OBJECT (bin, "Sending event to sink children");
1587 while (!(res || done)) {
1588 gpointer data;
1590 switch (gst_iterator_next (iter, &data)) {
1591 case GST_ITERATOR_OK:
1592 {
1593 GstElement *sink;
1595 sink = GST_ELEMENT_CAST (data);
1596 res = gst_element_query (sink, query);
1597 gst_object_unref (sink);
1598 break;
1599 }
1600 case GST_ITERATOR_RESYNC:
1601 gst_iterator_resync (iter);
1602 break;
1603 default:
1604 case GST_ITERATOR_DONE:
1605 done = TRUE;
1606 break;
1607 }
1608 }
1609 gst_iterator_free (iter);
1611 return res;
1612 }
1614 static gint
1615 compare_name (GstElement * element, const gchar * name)
1616 {
1617 gint eq;
1619 GST_LOCK (element);
1620 eq = strcmp (GST_ELEMENT_NAME (element), name);
1621 GST_UNLOCK (element);
1623 if (eq != 0) {
1624 gst_object_unref (element);
1625 }
1626 return eq;
1627 }
1629 /**
1630 * gst_bin_get_by_name:
1631 * @bin: #Gstbin to search
1632 * @name: the element name to search for
1633 *
1634 * Get the element with the given name from this bin. This
1635 * function recurses into subbins.
1636 *
1637 * MT safe.
1638 *
1639 * Returns: the element with the given name. Returns NULL if the
1640 * element is not found or when bad parameters were given. Unref after
1641 * use.
1642 */
1643 GstElement *
1644 gst_bin_get_by_name (GstBin * bin, const gchar * name)
1645 {
1646 GstIterator *children;
1647 GstIterator *result;
1649 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1651 GST_CAT_INFO (GST_CAT_PARENTAGE, "[%s]: looking up child element %s",
1652 GST_ELEMENT_NAME (bin), name);
1654 children = gst_bin_iterate_recurse (bin);
1655 result = gst_iterator_find_custom (children,
1656 (GCompareFunc) compare_name, (gpointer) name);
1657 gst_iterator_free (children);
1659 return GST_ELEMENT_CAST (result);
1660 }
1662 /**
1663 * gst_bin_get_by_name_recurse_up:
1664 * @bin: #Gstbin to search
1665 * @name: the element name to search for
1666 *
1667 * MT safe.
1668 *
1669 * Get the element with the given name from this bin. If the
1670 * element is not found, a recursion is performed on the parent bin.
1671 *
1672 * Returns: the element with the given name or NULL when the element
1673 * was not found or bad parameters were given. Unref after use.
1674 */
1675 GstElement *
1676 gst_bin_get_by_name_recurse_up (GstBin * bin, const gchar * name)
1677 {
1678 GstElement *result;
1680 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1681 g_return_val_if_fail (name != NULL, NULL);
1683 result = gst_bin_get_by_name (bin, name);
1685 if (!result) {
1686 GstObject *parent;
1688 parent = gst_object_get_parent (GST_OBJECT_CAST (bin));
1689 if (parent) {
1690 if (GST_IS_BIN (parent)) {
1691 result = gst_bin_get_by_name_recurse_up (GST_BIN_CAST (parent), name);
1692 }
1693 gst_object_unref (parent);
1694 }
1695 }
1697 return result;
1698 }
1700 static gint
1701 compare_interface (GstElement * element, gpointer interface)
1702 {
1703 gint ret;
1705 if (G_TYPE_CHECK_INSTANCE_TYPE (element, GPOINTER_TO_INT (interface))) {
1706 ret = 0;
1707 } else {
1708 /* we did not find the element, need to release the ref
1709 * added by the iterator */
1710 gst_object_unref (element);
1711 ret = 1;
1712 }
1713 return ret;
1714 }
1716 /**
1717 * gst_bin_get_by_interface:
1718 * @bin: bin to find element in
1719 * @interface: interface to be implemented by interface
1720 *
1721 * Looks for the first element inside the bin that implements the given
1722 * interface. If such an element is found, it returns the element. You can
1723 * cast this element to the given interface afterwards.
1724 * If you want all elements that implement the interface, use
1725 * gst_bin_iterate_all_by_interface(). The function recurses inside bins.
1726 *
1727 * MT safe.
1728 *
1729 * Returns: An #GstElement inside the bin implementing the interface.
1730 * Unref after use.
1731 */
1732 GstElement *
1733 gst_bin_get_by_interface (GstBin * bin, GType interface)
1734 {
1735 GstIterator *children;
1736 GstIterator *result;
1738 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1740 children = gst_bin_iterate_recurse (bin);
1741 result = gst_iterator_find_custom (children, (GCompareFunc) compare_interface,
1742 GINT_TO_POINTER (interface));
1743 gst_iterator_free (children);
1745 return GST_ELEMENT_CAST (result);
1746 }
1748 /**
1749 * gst_bin_iterate_all_by_interface:
1750 * @bin: bin to find elements in
1751 * @interface: interface to be implemented by interface
1752 *
1753 * Looks for all elements inside the bin that implements the given
1754 * interface. You can safely cast all returned elements to the given interface.
1755 * The function recurses bins inside bins. The iterator will return a series
1756 * of #GstElement that should be unreffed after use.
1757 *
1758 * MT safe.
1759 *
1760 * Returns: A #GstIterator for the elements inside the bin implementing the
1761 * given interface.
1762 */
1763 GstIterator *
1764 gst_bin_iterate_all_by_interface (GstBin * bin, GType interface)
1765 {
1766 GstIterator *children;
1767 GstIterator *result;
1769 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
1771 children = gst_bin_iterate_recurse (bin);
1772 result = gst_iterator_filter (children, (GCompareFunc) compare_interface,
1773 GINT_TO_POINTER (interface));
1775 return result;
1776 }
1778 #ifndef GST_DISABLE_LOADSAVE
1779 static xmlNodePtr
1780 gst_bin_save_thyself (GstObject * object, xmlNodePtr parent)
1781 {
1782 GstBin *bin = GST_BIN (object);
1783 xmlNodePtr childlist, elementnode;
1784 GList *children;
1785 GstElement *child;
1787 if (GST_OBJECT_CLASS (parent_class)->save_thyself)
1788 GST_OBJECT_CLASS (parent_class)->save_thyself (GST_OBJECT (bin), parent);
1790 childlist = xmlNewChild (parent, NULL, (xmlChar *) "children", NULL);
1792 GST_CAT_INFO (GST_CAT_XML, "[%s]: saving %d children",
1793 GST_ELEMENT_NAME (bin), bin->numchildren);
1795 children = bin->children;
1796 while (children) {
1797 child = GST_ELEMENT (children->data);
1798 elementnode = xmlNewChild (childlist, NULL, (xmlChar *) "element", NULL);
1799 gst_object_save_thyself (GST_OBJECT (child), elementnode);
1800 children = g_list_next (children);
1801 }
1802 return childlist;
1803 }
1805 static void
1806 gst_bin_restore_thyself (GstObject * object, xmlNodePtr self)
1807 {
1808 GstBin *bin = GST_BIN (object);
1809 xmlNodePtr field = self->xmlChildrenNode;
1810 xmlNodePtr childlist;
1812 while (field) {
1813 if (!strcmp ((char *) field->name, "children")) {
1814 GST_CAT_INFO (GST_CAT_XML, "[%s]: loading children",
1815 GST_ELEMENT_NAME (object));
1816 childlist = field->xmlChildrenNode;
1817 while (childlist) {
1818 if (!strcmp ((char *) childlist->name, "element")) {
1819 GstElement *element =
1820 gst_xml_make_element (childlist, GST_OBJECT (bin));
1822 /* it had to be parented to find the pads, now we ref and unparent so
1823 * we can add it to the bin */
1824 gst_object_ref (element);
1825 gst_object_unparent (GST_OBJECT (element));
1827 gst_bin_add (bin, element);
1828 }
1829 childlist = childlist->next;
1830 }
1831 }
1833 field = field->next;
1834 }
1835 if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
1836 (GST_OBJECT_CLASS (parent_class)->restore_thyself) (object, self);
1837 }
1838 #endif /* GST_DISABLE_LOADSAVE */