2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2004 Wim Taymans <wim@fluendo.com>
5 * gstelement.c: The base element, all elements derive from this
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 * @short_description: Abstract base class for all pipeline elements
26 * @see_also: #GstElementFactory, #GstPad
28 * GstElement is the base class needed to construct an element that can be
29 * used in a GStreamer pipeline. As such, it is not a functional entity, and
30 * cannot do anything when placed in a pipeline.
32 * The name of a #GstElement can be get with gst_element_get_name() and set with
33 * gst_element_set_name(). For speed, GST_ELEMENT_NAME() can be used in the
34 * core when using the appropriate locking. Do not use this in plug-ins or
35 * applications in order to retain ABI compatibility.
37 * All elements have pads (of the type #GstPad). These pads link to pads on
38 * other elements. #GstBuffer flow between these linked pads.
39 * A #GstElement has a #GList of #GstPad structures for all their input (or sink)
40 * and output (or source) pads.
41 * Core and plug-in writers can add and remove pads with gst_element_add_pad()
42 * and gst_element_remove_pad().
44 * A pad of an element can be retrieved by name with gst_element_get_pad().
45 * An iterator of all pads can be retrieved with gst_element_iterate_pads().
47 * Elements can be linked through their pads.
48 * If the link is straightforward, use the gst_element_link()
49 * convenience function to link two elements, or gst_element_link_many()
50 * for more elements in a row.
51 * Use gst_element_link_filtered() to link two elements constrained by
52 * a specified set of #GstCaps.
53 * For finer control, use gst_element_link_pads() and
54 * gst_element_link_pads_filtered() to specify the pads to link on
55 * each element by name.
57 * Each element has a state (see #GstState). You can get and set the state
58 * of an element with gst_element_get_state() and gst_element_set_state().
59 * To get a string representation of a #GstState, use
60 * gst_element_state_get_name().
62 * You can get and set a #GstClock on an element using gst_element_get_clock()
63 * and gst_element_set_clock().
64 * Some elements can provide a clock for the pipeline if
65 * gst_element_provides_clock() returns %TRUE. With the
66 * gst_element_provide_clock() method one can retrieve the clock provided by
68 * Not all elements require a clock to operate correctly. If
69 * gst_element_requires_clock() returns %TRUE, a clock should be set on the
70 * element with gst_element_set_clock().
72 * Note that clock slection and distribution is normally handled by the
73 * toplevel #GstPipeline so the clock functions are only to be used in very
74 * specific situations.
76 * Last reviewed on 2005-11-23 (0.9.5)
79 #include "gst_private.h"
82 #include <gobject/gvaluecollector.h>
84 #include "gstelement.h"
86 #include "gstmarshal.h"
91 #include "gst-i18n-lib.h"
93 /* Element signals and args */
109 extern void __gst_element_details_clear (GstElementDetails * dp);
110 extern void __gst_element_details_copy (GstElementDetails * dest,
111 const GstElementDetails * src);
113 static void gst_element_class_init (GstElementClass * klass);
114 static void gst_element_init (GstElement * element);
115 static void gst_element_base_class_init (gpointer g_class);
116 static void gst_element_base_class_finalize (gpointer g_class);
118 static void gst_element_dispose (GObject * object);
119 static void gst_element_finalize (GObject * object);
121 static GstStateChangeReturn gst_element_change_state (GstElement * element,
122 GstStateChange transition);
123 static GstStateChangeReturn gst_element_change_state_func (GstElement * element,
124 GstStateChange transition);
125 static GstStateChangeReturn gst_element_get_state_func (GstElement * element,
126 GstState * state, GstState * pending, GstClockTime timeout);
127 static GstStateChangeReturn gst_element_set_state_func (GstElement * element,
129 static void gst_element_set_bus_func (GstElement * element, GstBus * bus);
131 #ifndef GST_DISABLE_LOADSAVE
132 static xmlNodePtr gst_element_save_thyself (GstObject * object,
134 static void gst_element_restore_thyself (GstObject * parent, xmlNodePtr self);
137 static GstObjectClass *parent_class = NULL;
138 static guint gst_element_signals[LAST_SIGNAL] = { 0 };
141 gst_element_get_type (void)
143 static GType gst_element_type = 0;
145 if (G_UNLIKELY (gst_element_type == 0)) {
146 static const GTypeInfo element_info = {
147 sizeof (GstElementClass),
148 gst_element_base_class_init,
149 gst_element_base_class_finalize,
150 (GClassInitFunc) gst_element_class_init,
155 (GInstanceInitFunc) gst_element_init,
159 gst_element_type = g_type_register_static (GST_TYPE_OBJECT, "GstElement",
160 &element_info, G_TYPE_FLAG_ABSTRACT);
162 return gst_element_type;
166 gst_element_class_init (GstElementClass * klass)
168 GObjectClass *gobject_class;
169 GstObjectClass *gstobject_class;
171 gobject_class = (GObjectClass *) klass;
172 gstobject_class = (GstObjectClass *) klass;
174 parent_class = g_type_class_ref (GST_TYPE_OBJECT);
177 * GstElement::pad-added:
178 * @gstelement: the object which received the signal
179 * @new_pad: the pad that has been added
181 * a new #GstPad has been added to the element.
183 gst_element_signals[NEW_PAD] =
184 g_signal_new ("pad-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
185 G_STRUCT_OFFSET (GstElementClass, pad_added), NULL, NULL,
186 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
188 * GstElement::pad-removed:
189 * @gstelement: the object which received the signal
190 * @old_pad: the pad that has been removed
192 * a #GstPad has been removed from the element
194 gst_element_signals[PAD_REMOVED] =
195 g_signal_new ("pad-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
196 G_STRUCT_OFFSET (GstElementClass, pad_removed), NULL, NULL,
197 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
199 * GstElement::no-more-pads:
200 * @gstelement: the object which received the signal
202 * This signals that the element will not generate more dynamic pads.
204 gst_element_signals[NO_MORE_PADS] =
205 g_signal_new ("no-more-pads", G_TYPE_FROM_CLASS (klass),
206 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstElementClass, no_more_pads), NULL,
207 NULL, gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
209 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_element_dispose);
210 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_element_finalize);
212 #ifndef GST_DISABLE_LOADSAVE
213 gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_element_save_thyself);
214 gstobject_class->restore_thyself =
215 GST_DEBUG_FUNCPTR (gst_element_restore_thyself);
218 klass->change_state = GST_DEBUG_FUNCPTR (gst_element_change_state_func);
219 klass->set_state = GST_DEBUG_FUNCPTR (gst_element_set_state_func);
220 klass->get_state = GST_DEBUG_FUNCPTR (gst_element_get_state_func);
221 klass->set_bus = GST_DEBUG_FUNCPTR (gst_element_set_bus_func);
222 klass->numpadtemplates = 0;
224 klass->elementfactory = NULL;
228 gst_element_base_class_init (gpointer g_class)
230 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
232 memset (&element_class->details, 0, sizeof (GstElementDetails));
233 element_class->padtemplates = NULL;
237 gst_element_base_class_finalize (gpointer g_class)
239 GstElementClass *klass = GST_ELEMENT_CLASS (g_class);
241 g_list_foreach (klass->padtemplates, (GFunc) gst_object_unref, NULL);
242 g_list_free (klass->padtemplates);
243 __gst_element_details_clear (&klass->details);
247 gst_element_init (GstElement * element)
249 GST_STATE (element) = GST_STATE_NULL;
250 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
251 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
252 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
254 element->state_lock = g_new0 (GStaticRecMutex, 1);
255 g_static_rec_mutex_init (element->state_lock);
256 element->state_cond = g_cond_new ();
260 * gst_element_default_error:
261 * @object: a #GObject that signalled the error.
262 * @orig: the #GstObject that initiated the error.
263 * @error: the GError.
264 * @debug: an additional debug information string, or NULL.
266 * A default error signal callback to attach to an element.
267 * The user data passed to the g_signal_connect is ignored.
269 * The default handler will simply print the error string using g_print.
274 gst_element_default_error (GObject * object, GstObject * source, GError * error,
277 gchar *name = gst_object_get_path_string (source);
279 g_print (_("ERROR: from element %s: %s\n"), name, error->message);
281 g_print (_("Additional debug info:\n%s\n"), debug);
287 * gst_element_release_request_pad:
288 * @element: a #GstElement to release the request pad of.
289 * @pad: the #GstPad to release.
291 * Makes the element free the previously requested pad as obtained
292 * with gst_element_get_request_pad().
297 gst_element_release_request_pad (GstElement * element, GstPad * pad)
299 GstElementClass *oclass;
301 g_return_if_fail (GST_IS_ELEMENT (element));
302 g_return_if_fail (GST_IS_PAD (pad));
304 oclass = GST_ELEMENT_GET_CLASS (element);
306 if (oclass->release_pad)
307 (oclass->release_pad) (element, pad);
309 gst_element_remove_pad (element, pad);
313 * gst_element_requires_clock:
314 * @element: a #GstElement to query
316 * Query if the element requires a clock.
318 * Returns: TRUE if the element requires a clock
323 gst_element_requires_clock (GstElement * element)
327 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
329 result = (GST_ELEMENT_GET_CLASS (element)->set_clock != NULL);
335 * gst_element_provides_clock:
336 * @element: a #GstElement to query
338 * Query if the element provides a clock. A #GstClock provided by an
339 * element can be used as the global #GstClock for the pipeline.
341 * Returns: TRUE if the element provides a clock
346 gst_element_provides_clock (GstElement * element)
350 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
352 result = (GST_ELEMENT_GET_CLASS (element)->provide_clock != NULL);
358 * gst_element_provide_clock:
359 * @element: a #GstElement to query
361 * Get the clock provided by the given element.
363 * Returns: the GstClock provided by the element or NULL
364 * if no clock could be provided. Unref after usage.
369 gst_element_provide_clock (GstElement * element)
371 GstClock *result = NULL;
372 GstElementClass *oclass;
374 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
376 oclass = GST_ELEMENT_GET_CLASS (element);
378 if (oclass->provide_clock)
379 result = oclass->provide_clock (element);
385 * gst_element_set_clock:
386 * @element: a #GstElement to set the clock for.
387 * @clock: the #GstClock to set for the element.
389 * Sets the clock for the element. This function increases the
390 * refcount on the clock. Any previously set clock on the object
393 * Returns: TRUE if the element accepted the clock.
398 gst_element_set_clock (GstElement * element, GstClock * clock)
400 GstElementClass *oclass;
403 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
405 oclass = GST_ELEMENT_GET_CLASS (element);
407 GST_DEBUG_OBJECT (element, "setting clock %p", clock);
409 if (oclass->set_clock)
410 res = oclass->set_clock (element, clock);
413 GST_OBJECT_LOCK (element);
414 gst_object_replace ((GstObject **) & element->clock, (GstObject *) clock);
415 GST_OBJECT_UNLOCK (element);
421 * gst_element_get_clock:
422 * @element: a #GstElement to get the clock of.
424 * Gets the currently configured clock of the element.
426 * Returns: the #GstClock of the element. unref after usage.
431 gst_element_get_clock (GstElement * element)
435 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
437 GST_OBJECT_LOCK (element);
438 if ((result = element->clock))
439 gst_object_ref (result);
440 GST_OBJECT_UNLOCK (element);
446 * gst_element_set_base_time:
447 * @element: a #GstElement.
448 * @time: the base time to set.
450 * Set the base time of an element. See gst_element_get_base_time().
455 gst_element_set_base_time (GstElement * element, GstClockTime time)
457 g_return_if_fail (GST_IS_ELEMENT (element));
459 GST_OBJECT_LOCK (element);
460 element->base_time = time;
461 GST_OBJECT_UNLOCK (element);
463 GST_DEBUG_OBJECT (element, "set base_time=%" GST_TIME_FORMAT,
464 GST_TIME_ARGS (time));
468 * gst_element_get_base_time:
469 * @element: a #GstElement.
471 * Returns the base time of the element. The base time is the
472 * absolute time of the clock when this element was last put to
473 * PLAYING. Substracting the base time from the clock time gives
474 * the stream time of the element.
476 * Returns: the base time of the element.
481 gst_element_get_base_time (GstElement * element)
485 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
487 GST_OBJECT_LOCK (element);
488 result = element->base_time;
489 GST_OBJECT_UNLOCK (element);
494 #ifndef GST_DISABLE_INDEX
496 * gst_element_is_indexable:
497 * @element: a #GstElement.
499 * Queries if the element can be indexed.
501 * Returns: TRUE if the element can be indexed.
506 gst_element_is_indexable (GstElement * element)
510 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
512 result = (GST_ELEMENT_GET_CLASS (element)->set_index != NULL);
518 * gst_element_set_index:
519 * @element: a #GstElement.
520 * @index: a #GstIndex.
522 * Set @index on the element. The refcount of the index
523 * will be increased, any previously set index is unreffed.
528 gst_element_set_index (GstElement * element, GstIndex * index)
530 GstElementClass *oclass;
532 g_return_if_fail (GST_IS_ELEMENT (element));
533 g_return_if_fail (GST_IS_INDEX (index));
535 oclass = GST_ELEMENT_GET_CLASS (element);
537 if (oclass->set_index)
538 oclass->set_index (element, index);
542 * gst_element_get_index:
543 * @element: a #GstElement.
545 * Gets the index from the element.
547 * Returns: a #GstIndex or NULL when no index was set on the
548 * element. unref after usage.
553 gst_element_get_index (GstElement * element)
555 GstElementClass *oclass;
557 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
559 oclass = GST_ELEMENT_GET_CLASS (element);
561 if (oclass->get_index)
562 return oclass->get_index (element);
569 * gst_element_add_pad:
570 * @element: a #GstElement to add the pad to.
571 * @pad: the #GstPad to add to the element.
573 * Adds a pad (link point) to @element. @pad's parent will be set to @element;
574 * see gst_object_set_parent() for refcounting information.
576 * Pads are not automatically activated so elements should perform the needed
577 * steps to activate the pad in case this pad is added in the PAUSED or PLAYING
580 * The pad and the element should be unlocked when calling this function.
582 * Returns: TRUE if the pad could be added. This function can fail when
583 * a pad with the same name already existed or the pad already had another
589 gst_element_add_pad (GstElement * element, GstPad * pad)
593 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
594 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
596 /* locking pad to look at the name */
597 GST_OBJECT_LOCK (pad);
598 pad_name = g_strdup (GST_PAD_NAME (pad));
599 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "adding pad '%s'",
600 GST_STR_NULL (pad_name));
601 GST_OBJECT_UNLOCK (pad);
603 /* then check to see if there's already a pad by that name here */
604 GST_OBJECT_LOCK (element);
605 if (G_UNLIKELY (!gst_object_check_uniqueness (element->pads, pad_name)))
608 /* try to set the pad's parent */
609 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (pad),
610 GST_OBJECT_CAST (element))))
615 /* add it to the list */
616 switch (gst_pad_get_direction (pad)) {
618 element->srcpads = g_list_prepend (element->srcpads, pad);
619 element->numsrcpads++;
622 element->sinkpads = g_list_prepend (element->sinkpads, pad);
623 element->numsinkpads++;
628 element->pads = g_list_prepend (element->pads, pad);
630 element->pads_cookie++;
631 GST_OBJECT_UNLOCK (element);
633 /* emit the NEW_PAD signal */
634 g_signal_emit (G_OBJECT (element), gst_element_signals[NEW_PAD], 0, pad);
641 g_critical ("Padname %s is not unique in element %s, not adding",
642 pad_name, GST_ELEMENT_NAME (element));
643 GST_OBJECT_UNLOCK (element);
650 ("Pad %s already has parent when trying to add to element %s",
651 pad_name, GST_ELEMENT_NAME (element));
652 GST_OBJECT_UNLOCK (element);
658 GST_OBJECT_LOCK (pad);
660 ("Trying to add pad %s to element %s, but it has no direction",
661 GST_OBJECT_NAME (pad), GST_ELEMENT_NAME (element));
662 GST_OBJECT_UNLOCK (pad);
663 GST_OBJECT_UNLOCK (element);
669 * gst_element_remove_pad:
670 * @element: a #GstElement to remove pad from.
671 * @pad: the #GstPad to remove from the element.
673 * Removes @pad from @element. @pad will be destroyed if it has not been
674 * referenced elsewhere.
676 * Returns: TRUE if the pad could be removed. Can return FALSE if the
677 * pad is not belonging to the provided element.
682 gst_element_remove_pad (GstElement * element, GstPad * pad)
686 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
687 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
689 /* locking pad to look at the name and parent */
690 GST_OBJECT_LOCK (pad);
691 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "removing pad '%s'",
692 GST_STR_NULL (GST_PAD_NAME (pad)));
694 if (G_UNLIKELY (GST_PAD_PARENT (pad) != element))
696 GST_OBJECT_UNLOCK (pad);
699 if ((peer = gst_pad_get_peer (pad))) {
700 /* window for MT unsafeness, someone else could unlink here
701 * and then we call unlink with wrong pads. The unlink
702 * function would catch this and safely return failed. */
703 if (GST_PAD_IS_SRC (pad))
704 gst_pad_unlink (pad, peer);
706 gst_pad_unlink (peer, pad);
708 gst_object_unref (peer);
711 GST_OBJECT_LOCK (element);
712 /* remove it from the list */
713 switch (gst_pad_get_direction (pad)) {
715 element->srcpads = g_list_remove (element->srcpads, pad);
716 element->numsrcpads--;
719 element->sinkpads = g_list_remove (element->sinkpads, pad);
720 element->numsinkpads--;
723 g_critical ("Removing pad without direction???");
726 element->pads = g_list_remove (element->pads, pad);
728 element->pads_cookie++;
729 GST_OBJECT_UNLOCK (element);
731 g_signal_emit (G_OBJECT (element), gst_element_signals[PAD_REMOVED], 0, pad);
733 gst_object_unparent (GST_OBJECT (pad));
739 /* FIXME, locking order? */
740 GST_OBJECT_LOCK (element);
741 g_critical ("Padname %s:%s does not belong to element %s when removing",
742 GST_ELEMENT_NAME (GST_PAD_PARENT (pad)), GST_PAD_NAME (pad),
743 GST_ELEMENT_NAME (element));
744 GST_OBJECT_UNLOCK (element);
745 GST_OBJECT_UNLOCK (pad);
751 * gst_element_no_more_pads:
752 * @element: a #GstElement
754 * Use this function to signal that the element does not expect any more pads
755 * to show up in the current pipeline. This function should be called whenever
756 * pads have been added by the element itself. Elements with #GST_PAD_SOMETIMES
757 * pad templates use this in combination with autopluggers to figure out that
758 * the element is done initializing its pads.
763 gst_element_no_more_pads (GstElement * element)
765 g_return_if_fail (GST_IS_ELEMENT (element));
767 g_signal_emit (element, gst_element_signals[NO_MORE_PADS], 0);
771 pad_compare_name (GstPad * pad1, const gchar * name)
775 GST_OBJECT_LOCK (pad1);
776 result = strcmp (GST_PAD_NAME (pad1), name);
777 GST_OBJECT_UNLOCK (pad1);
783 * gst_element_get_static_pad:
784 * @element: a #GstElement to find a static pad of.
785 * @name: the name of the static #GstPad to retrieve.
787 * Retrieves a pad from @element by name. This version only retrieves
788 * already-existing (i.e. 'static') pads.
790 * Returns: the requested #GstPad if found, otherwise NULL. unref after
796 gst_element_get_static_pad (GstElement * element, const gchar * name)
799 GstPad *result = NULL;
801 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
802 g_return_val_if_fail (name != NULL, NULL);
804 GST_OBJECT_LOCK (element);
806 g_list_find_custom (element->pads, name, (GCompareFunc) pad_compare_name);
808 result = GST_PAD_CAST (find->data);
809 gst_object_ref (result);
812 if (result == NULL) {
813 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "no such pad '%s' in element \"%s\"",
814 name, GST_ELEMENT_NAME (element));
816 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
817 GST_ELEMENT_NAME (element), name);
819 GST_OBJECT_UNLOCK (element);
825 gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
828 GstPad *newpad = NULL;
829 GstElementClass *oclass;
831 oclass = GST_ELEMENT_GET_CLASS (element);
833 if (oclass->request_new_pad)
834 newpad = (oclass->request_new_pad) (element, templ, name);
837 gst_object_ref (newpad);
843 * gst_element_get_request_pad:
844 * @element: a #GstElement to find a request pad of.
845 * @name: the name of the request #GstPad to retrieve.
847 * Retrieves a pad from the element by name. This version only retrieves
850 * Returns: requested #GstPad if found, otherwise NULL. Unref after usage.
853 gst_element_get_request_pad (GstElement * element, const gchar * name)
855 GstPadTemplate *templ = NULL;
857 const gchar *req_name = NULL;
858 gboolean templ_found = FALSE;
862 gchar *str, *endptr = NULL;
863 GstElementClass *class;
865 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
866 g_return_val_if_fail (name != NULL, NULL);
868 class = GST_ELEMENT_GET_CLASS (element);
870 if (strstr (name, "%")) {
871 templ = gst_element_class_get_pad_template (class, name);
876 list = gst_element_class_get_pad_template_list (class);
877 while (!templ_found && list) {
878 templ = (GstPadTemplate *) list->data;
879 if (templ->presence == GST_PAD_REQUEST) {
880 /* Because of sanity checks in gst_pad_template_new(), we know that %s
881 and %d, occurring at the end of the name_template, are the only
883 GST_CAT_DEBUG (GST_CAT_PADS, "comparing %s to %s", name,
884 templ->name_template);
885 if ((str = strchr (templ->name_template, '%'))
886 && strncmp (templ->name_template, name,
887 str - templ->name_template) == 0
888 && strlen (name) > str - templ->name_template) {
889 data = name + (str - templ->name_template);
890 if (*(str + 1) == 'd') {
892 n = (gint) strtol (data, &endptr, 10);
893 if (endptr && *endptr == '\0') {
913 pad = gst_element_request_pad (element, templ, req_name);
919 * gst_element_get_pad:
920 * @element: a #GstElement.
921 * @name: the name of the pad to retrieve.
923 * Retrieves a pad from @element by name. Tries gst_element_get_static_pad()
924 * first, then gst_element_get_request_pad().
926 * Returns: the #GstPad if found, otherwise %NULL. Unref after usage.
929 gst_element_get_pad (GstElement * element, const gchar * name)
933 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
934 g_return_val_if_fail (name != NULL, NULL);
936 pad = gst_element_get_static_pad (element, name);
938 pad = gst_element_get_request_pad (element, name);
943 static GstIteratorItem
944 iterate_pad (GstIterator * it, GstPad * pad)
946 gst_object_ref (pad);
947 return GST_ITERATOR_ITEM_PASS;
951 gst_element_iterate_pad_list (GstElement * element, GList ** padlist)
955 GST_OBJECT_LOCK (element);
956 gst_object_ref (element);
957 result = gst_iterator_new_list (GST_TYPE_PAD,
958 GST_OBJECT_GET_LOCK (element),
959 &element->pads_cookie,
962 (GstIteratorItemFunction) iterate_pad,
963 (GstIteratorDisposeFunction) gst_object_unref);
964 GST_OBJECT_UNLOCK (element);
970 * gst_element_iterate_pads:
971 * @element: a #GstElement to iterate pads of.
973 * Retrieves an iterattor of @element's pads. The iterator should
974 * be freed after usage.
976 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
981 gst_element_iterate_pads (GstElement * element)
983 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
985 return gst_element_iterate_pad_list (element, &element->pads);
989 * gst_element_iterate_src_pads:
990 * @element: a #GstElement.
992 * Retrieves an iterator of @element's source pads.
994 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
999 gst_element_iterate_src_pads (GstElement * element)
1001 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1003 return gst_element_iterate_pad_list (element, &element->srcpads);
1007 * gst_element_iterate_sink_pads:
1008 * @element: a #GstElement.
1010 * Retrieves an iterator of @element's sink pads.
1012 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1017 gst_element_iterate_sink_pads (GstElement * element)
1019 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1021 return gst_element_iterate_pad_list (element, &element->sinkpads);
1025 * gst_element_class_add_pad_template:
1026 * @klass: the #GstElementClass to add the pad template to.
1027 * @templ: a #GstPadTemplate to add to the element class.
1029 * Adds a padtemplate to an element class. This is mainly used in the _base_init
1030 * functions of classes.
1033 gst_element_class_add_pad_template (GstElementClass * klass,
1034 GstPadTemplate * templ)
1036 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1037 g_return_if_fail (GST_IS_PAD_TEMPLATE (templ));
1039 /* avoid registering pad templates with the same name */
1040 g_return_if_fail (gst_element_class_get_pad_template (klass,
1041 templ->name_template) == NULL);
1043 klass->padtemplates = g_list_append (klass->padtemplates,
1044 gst_object_ref (templ));
1045 klass->numpadtemplates++;
1049 * gst_element_class_set_details:
1050 * @klass: class to set details for
1051 * @details: details to set
1053 * Sets the detailed information for a #GstElementClass.
1054 * <note>This function is for use in _base_init functions only.</note>
1057 gst_element_class_set_details (GstElementClass * klass,
1058 const GstElementDetails * details)
1060 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1061 g_return_if_fail (GST_IS_ELEMENT_DETAILS (details));
1063 __gst_element_details_copy (&klass->details, details);
1067 * gst_element_class_get_pad_template_list:
1068 * @element_class: a #GstElementClass to get pad templates of.
1070 * Retrieves a list of the pad templates associated with @element_class. The
1071 * list must not be modified by the calling code.
1072 * <note>If you use this function in the #GInstanceInitFunc of an object class
1073 * that has subclasses, make sure to pass the g_class parameter of the
1074 * #GInstanceInitFunc here.</note>
1076 * Returns: the #GList of padtemplates.
1079 gst_element_class_get_pad_template_list (GstElementClass * element_class)
1081 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1083 return element_class->padtemplates;
1087 * gst_element_class_get_pad_template:
1088 * @element_class: a #GstElementClass to get the pad template of.
1089 * @name: the name of the #GstPadTemplate to get.
1091 * Retrieves a padtemplate from @element_class with the given name.
1092 * <note>If you use this function in the #GInstanceInitFunc of an object class
1093 * that has subclasses, make sure to pass the g_class parameter of the
1094 * #GInstanceInitFunc here.</note>
1096 * Returns: the #GstPadTemplate with the given name, or NULL if none was found.
1097 * No unreferencing is necessary.
1100 gst_element_class_get_pad_template (GstElementClass * element_class,
1105 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1106 g_return_val_if_fail (name != NULL, NULL);
1108 padlist = gst_element_class_get_pad_template_list (element_class);
1111 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
1113 if (strcmp (padtempl->name_template, name) == 0)
1116 padlist = g_list_next (padlist);
1123 gst_element_get_random_pad (GstElement * element, GstPadDirection dir)
1125 GstPad *result = NULL;
1128 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "getting a random pad");
1132 GST_OBJECT_LOCK (element);
1133 pads = element->srcpads;
1136 GST_OBJECT_LOCK (element);
1137 pads = element->sinkpads;
1140 goto wrong_direction;
1142 for (; pads; pads = g_list_next (pads)) {
1143 GstPad *pad = GST_PAD (pads->data);
1145 GST_OBJECT_LOCK (pad);
1146 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "checking pad %s:%s",
1147 GST_DEBUG_PAD_NAME (pad));
1149 if (GST_PAD_IS_LINKED (pad)) {
1150 GST_OBJECT_UNLOCK (pad);
1154 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is not linked",
1155 GST_DEBUG_PAD_NAME (pad));
1157 GST_OBJECT_UNLOCK (pad);
1160 gst_object_ref (result);
1162 GST_OBJECT_UNLOCK (element);
1166 /* ERROR handling */
1169 g_warning ("unknown pad direction %d", dir);
1175 * gst_element_send_event:
1176 * @element: a #GstElement to send the event to.
1177 * @event: the #GstEvent to send to the element.
1179 * Sends an event to an element. If the element doesn't
1180 * implement an event handler, the event will be forwarded
1181 * to a random sink pad. This function takes owership of the
1182 * provided event so you should gst_event_ref() it if you want to reuse
1183 * the event after this call.
1185 * Returns: TRUE if the event was handled.
1190 gst_element_send_event (GstElement * element, GstEvent * event)
1192 GstElementClass *oclass;
1193 gboolean result = FALSE;
1195 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1196 g_return_val_if_fail (event != NULL, FALSE);
1198 oclass = GST_ELEMENT_GET_CLASS (element);
1200 if (oclass->send_event) {
1201 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send %s event on element %s",
1202 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1203 result = oclass->send_event (element, event);
1205 GstPad *pad = GST_EVENT_IS_DOWNSTREAM (event) ?
1206 gst_element_get_random_pad (element, GST_PAD_SRC) :
1207 gst_element_get_random_pad (element, GST_PAD_SINK);
1210 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1211 "pushing %s event to random %s pad %s:%s",
1212 GST_EVENT_TYPE_NAME (event),
1213 (GST_PAD_DIRECTION (pad) == GST_PAD_SRC ? "src" : "sink"),
1214 GST_DEBUG_PAD_NAME (pad));
1216 result = gst_pad_push_event (pad, event);
1217 gst_object_unref (pad);
1219 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "can't send %s event on element %s",
1220 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1228 * @element: a #GstElement to send the event to.
1229 * @rate: The new playback rate
1230 * @format: The format of the seek values
1231 * @flags: The optional seek flags.
1232 * @cur_type: The type and flags for the new current position
1233 * @cur: The value of the new current position
1234 * @stop_type: The type and flags for the new stop position
1235 * @stop: The value of the new stop position
1237 * Sends a seek event to an element.
1239 * Returns: %TRUE if the event was handled.
1244 gst_element_seek (GstElement * element, gdouble rate, GstFormat format,
1245 GstSeekFlags flags, GstSeekType cur_type, gint64 cur,
1246 GstSeekType stop_type, gint64 stop)
1251 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1254 gst_event_new_seek (rate, format, flags, cur_type, cur, stop_type, stop);
1255 result = gst_element_send_event (element, event);
1261 * gst_element_get_query_types:
1262 * @element: a #GstElement to query
1264 * Get an array of query types from the element.
1265 * If the element doesn't implement a query types function,
1266 * the query will be forwarded to a random sink pad.
1268 * Returns: An array of #GstQueryType elements that should not
1269 * be freed or modified.
1273 const GstQueryType *
1274 gst_element_get_query_types (GstElement * element)
1276 GstElementClass *oclass;
1277 const GstQueryType *result = NULL;
1279 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1281 oclass = GST_ELEMENT_GET_CLASS (element);
1283 if (oclass->get_query_types) {
1284 result = oclass->get_query_types (element);
1286 GstPad *pad = gst_element_get_random_pad (element, GST_PAD_SINK);
1289 GstPad *peer = gst_pad_get_peer (pad);
1292 result = gst_pad_get_query_types (peer);
1294 gst_object_unref (peer);
1296 gst_object_unref (pad);
1303 * gst_element_query:
1304 * @element: a #GstElement to perform the query on.
1305 * @query: the #GstQuery.
1307 * Performs a query on the given element. If the format is set
1308 * to #GST_FORMAT_DEFAULT and this function returns TRUE, the
1309 * format pointer will hold the default format.
1310 * For element that don't implement a query handler, this function
1311 * forwards the query to a random usable sinkpad of this element.
1313 * Returns: TRUE if the query could be performed.
1318 gst_element_query (GstElement * element, GstQuery * query)
1320 GstElementClass *oclass;
1321 gboolean result = FALSE;
1323 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1324 g_return_val_if_fail (query != NULL, FALSE);
1326 oclass = GST_ELEMENT_GET_CLASS (element);
1328 if (oclass->query) {
1329 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send query on element %s",
1330 GST_ELEMENT_NAME (element));
1331 result = oclass->query (element, query);
1333 GstPad *pad = gst_element_get_random_pad (element, GST_PAD_SRC);
1336 result = gst_pad_query (pad, query);
1338 gst_object_unref (pad);
1340 pad = gst_element_get_random_pad (element, GST_PAD_SINK);
1342 GstPad *peer = gst_pad_get_peer (pad);
1345 result = gst_pad_query (peer, query);
1347 gst_object_unref (peer);
1349 gst_object_unref (pad);
1357 * gst_element_post_message:
1358 * @element: a #GstElement posting the message
1359 * @message: a #GstMessage to post
1361 * Post a message on the element's #GstBus. This function takes ownership of the
1362 * message; if you want to access the message after this call, you should add an
1363 * additional reference before calling.
1365 * Returns: TRUE if the message was successfully posted.
1370 gst_element_post_message (GstElement * element, GstMessage * message)
1373 gboolean result = FALSE;
1375 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1376 g_return_val_if_fail (message != NULL, FALSE);
1378 GST_OBJECT_LOCK (element);
1381 if (G_UNLIKELY (bus == NULL))
1384 gst_object_ref (bus);
1385 GST_OBJECT_UNLOCK (element);
1387 result = gst_bus_post (bus, message);
1388 gst_object_unref (bus);
1394 GST_DEBUG ("not posting message %p: no bus", message);
1395 GST_OBJECT_UNLOCK (element);
1396 gst_message_unref (message);
1402 * _gst_element_error_printf:
1403 * @format: the printf-like format to use, or NULL
1405 * This function is only used internally by the gst_element_error() macro.
1407 * Returns: a newly allocated string, or NULL if the format was NULL or ""
1412 _gst_element_error_printf (const gchar * format, ...)
1422 va_start (args, format);
1423 buffer = g_strdup_vprintf (format, args);
1429 * gst_element_message_full:
1430 * @element: a #GstElement to send message from
1431 * @type: the #GstMessageType
1432 * @domain: the GStreamer GError domain this message belongs to
1433 * @code: the GError code belonging to the domain
1434 * @text: an allocated text string to be used as a replacement for the
1435 * default message connected to code, or NULL
1436 * @debug: an allocated debug message to be used as a replacement for the
1437 * default debugging information, or NULL
1438 * @file: the source code file where the error was generated
1439 * @function: the source code function where the error was generated
1440 * @line: the source code line where the error was generated
1442 * Post an error or warning message on the bus from inside an element.
1446 void gst_element_message_full
1447 (GstElement * element, GstMessageType type,
1448 GQuark domain, gint code, gchar * text,
1449 gchar * debug, const gchar * file, const gchar * function, gint line)
1451 GError *gerror = NULL;
1455 gboolean has_debug = TRUE;
1456 GstMessage *message = NULL;
1459 GST_DEBUG_OBJECT (element, "start");
1460 g_return_if_fail (GST_IS_ELEMENT (element));
1461 g_return_if_fail ((type == GST_MESSAGE_ERROR) ||
1462 (type == GST_MESSAGE_WARNING));
1464 /* check if we send the given text or the default error text */
1465 if ((text == NULL) || (text[0] == 0)) {
1466 /* text could have come from g_strdup_printf (""); */
1468 sent_text = gst_error_get_message (domain, code);
1472 /* construct a sent_debug with extra information from source */
1473 if ((debug == NULL) || (debug[0] == 0)) {
1474 /* debug could have come from g_strdup_printf (""); */
1478 name = gst_object_get_path_string (GST_OBJECT (element));
1480 sent_debug = g_strdup_printf ("%s(%d): %s (): %s:\n%s",
1481 file, line, function, name, debug);
1483 sent_debug = g_strdup_printf ("%s(%d): %s (): %s",
1484 file, line, function, name);
1488 /* create gerror and post message */
1489 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posting message: %s",
1491 gerror = g_error_new_literal (domain, code, sent_text);
1493 if (type == GST_MESSAGE_ERROR) {
1494 message = gst_message_new_error (GST_OBJECT (element), gerror, sent_debug);
1495 } else if (type == GST_MESSAGE_WARNING) {
1496 message = gst_message_new_warning (GST_OBJECT (element), gerror,
1499 g_assert_not_reached ();
1501 gst_element_post_message (element, message);
1503 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posted message: %s",
1507 g_error_free (gerror);
1508 g_free (sent_debug);
1513 * gst_element_is_locked_state:
1514 * @element: a #GstElement.
1516 * Checks if the state of an element is locked.
1517 * If the state of an element is locked, state changes of the parent don't
1518 * affect the element.
1519 * This way you can leave currently unused elements inside bins. Just lock their
1520 * state before changing the state from #GST_STATE_NULL.
1524 * Returns: TRUE, if the element's state is locked.
1527 gst_element_is_locked_state (GstElement * element)
1531 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1533 GST_OBJECT_LOCK (element);
1534 result = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1535 GST_OBJECT_UNLOCK (element);
1541 * gst_element_set_locked_state:
1542 * @element: a #GstElement
1543 * @locked_state: TRUE to lock the element's state
1545 * Locks the state of an element, so state changes of the parent don't affect
1546 * this element anymore.
1550 * Returns: TRUE if the state was changed, FALSE if bad parameterss were given
1551 * or the elements state-locking needed no change.
1554 gst_element_set_locked_state (GstElement * element, gboolean locked_state)
1558 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1560 GST_OBJECT_LOCK (element);
1561 old = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1563 if (G_UNLIKELY (old == locked_state))
1567 GST_CAT_DEBUG (GST_CAT_STATES, "locking state of element %s",
1568 GST_ELEMENT_NAME (element));
1569 GST_OBJECT_FLAG_SET (element, GST_ELEMENT_LOCKED_STATE);
1571 GST_CAT_DEBUG (GST_CAT_STATES, "unlocking state of element %s",
1572 GST_ELEMENT_NAME (element));
1573 GST_OBJECT_FLAG_UNSET (element, GST_ELEMENT_LOCKED_STATE);
1575 GST_OBJECT_UNLOCK (element);
1580 GST_CAT_DEBUG (GST_CAT_STATES, "elements %s was in locked state %d",
1581 GST_ELEMENT_NAME (element), old);
1582 GST_OBJECT_UNLOCK (element);
1588 * gst_element_sync_state_with_parent:
1589 * @element: a #GstElement.
1591 * Tries to change the state of the element to the same as its parent.
1592 * If this function returns FALSE, the state of element is undefined.
1594 * Returns: TRUE, if the element's state could be synced to the parent's state.
1599 gst_element_sync_state_with_parent (GstElement * element)
1603 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1605 if ((parent = GST_ELEMENT_CAST (gst_element_get_parent (element)))) {
1606 GstState parent_current, parent_pending;
1607 GstStateChangeReturn ret;
1609 GST_OBJECT_LOCK (parent);
1610 parent_current = GST_STATE (parent);
1611 parent_pending = GST_STATE_PENDING (parent);
1612 GST_OBJECT_UNLOCK (parent);
1614 GST_CAT_DEBUG (GST_CAT_STATES,
1615 "syncing state of element %s (%s) to %s (%s, %s)",
1616 GST_ELEMENT_NAME (element),
1617 gst_element_state_get_name (GST_STATE (element)),
1618 GST_ELEMENT_NAME (parent), gst_element_state_get_name (parent_current),
1619 gst_element_state_get_name (parent_pending));
1621 ret = gst_element_set_state (element, parent_current);
1622 if (ret == GST_STATE_CHANGE_FAILURE)
1625 gst_object_unref (parent);
1639 static GstStateChangeReturn
1640 gst_element_get_state_func (GstElement * element,
1641 GstState * state, GstState * pending, GstClockTime timeout)
1643 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
1644 GstState old_pending;
1646 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "getting state");
1648 GST_OBJECT_LOCK (element);
1649 ret = GST_STATE_RETURN (element);
1651 /* we got an error, report immediatly */
1652 if (ret == GST_STATE_CHANGE_FAILURE)
1655 /* we got no_preroll, report immediatly */
1656 if (ret == GST_STATE_CHANGE_NO_PREROLL)
1659 /* no need to wait async if we are not async */
1660 if (ret != GST_STATE_CHANGE_ASYNC)
1663 old_pending = GST_STATE_PENDING (element);
1664 if (old_pending != GST_STATE_VOID_PENDING) {
1665 GTimeVal *timeval, abstimeout;
1668 if (timeout != GST_CLOCK_TIME_NONE) {
1669 glong add = timeout / 1000;
1674 /* make timeout absolute */
1675 g_get_current_time (&abstimeout);
1676 g_time_val_add (&abstimeout, add);
1677 timeval = &abstimeout;
1681 /* get cookie to dected state change during waiting */
1682 cookie = element->state_cookie;
1684 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1685 "waiting for element to commit state");
1687 /* we have a pending state change, wait for it to complete */
1688 if (!GST_STATE_TIMED_WAIT (element, timeval)) {
1689 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timed out");
1690 /* timeout triggered */
1691 ret = GST_STATE_CHANGE_ASYNC;
1693 if (cookie != element->state_cookie)
1696 /* could be success or failure */
1697 if (old_pending == GST_STATE (element)) {
1698 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got success");
1699 ret = GST_STATE_CHANGE_SUCCESS;
1701 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got failure");
1702 ret = GST_STATE_CHANGE_FAILURE;
1706 /* if nothing is pending anymore we can return SUCCESS */
1707 if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
1708 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "nothing pending");
1709 ret = GST_STATE_CHANGE_SUCCESS;
1714 *state = GST_STATE (element);
1716 *pending = GST_STATE_PENDING (element);
1718 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1719 "state current: %s, pending: %s, result: %d",
1720 gst_element_state_get_name (GST_STATE (element)),
1721 gst_element_state_get_name (GST_STATE_PENDING (element)), ret);
1722 GST_OBJECT_UNLOCK (element);
1729 *state = GST_STATE_VOID_PENDING;
1731 *pending = GST_STATE_VOID_PENDING;
1733 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "get_state() interruped");
1735 GST_OBJECT_UNLOCK (element);
1737 return GST_STATE_CHANGE_FAILURE;
1742 * gst_element_get_state:
1743 * @element: a #GstElement to get the state of.
1744 * @state: a pointer to #GstState to hold the state. Can be NULL.
1745 * @pending: a pointer to #GstState to hold the pending state.
1747 * @timeout: a #GstClockTime to specify the timeout for an async
1748 * state change or %GST_CLOCK_TIME_NONE for infinite timeout.
1750 * Gets the state of the element.
1752 * For elements that performed an ASYNC state change, as reported by
1753 * gst_element_set_state(), this function will block up to the
1754 * specified timeout value for the state change to complete.
1755 * If the element completes the state change or goes into
1756 * an error, this function returns immediately with a return value of
1757 * %GST_STATE_CHANGE_SUCCESS or %GST_STATE_CHANGE_FAILURE respectively.
1759 * For elements that did not return %GST_STATE_CHANGE_ASYNC, this function
1760 * returns the current and pending state immediately.
1762 * This function returns %GST_STATE_CHANGE_NO_PREROLL if the element
1763 * successfully changed its state but is not able to provide data yet.
1765 * happens for live sources that only produce data in the PLAYING state.
1766 * While the state change return is equivalent to %GST_STATE_CHANGE_SUCCESS, it
1767 * is returned to the application to signal that some sink elements might not
1768 * be able to complete their state change because an element is not producing
1769 * data to complete the preroll. When setting the element to playing,
1770 * the preroll will complete and playback will start.
1772 * Returns: %GST_STATE_CHANGE_SUCCESS if the element has no more pending state
1773 * and the last state change succeeded, %GST_STATE_CHANGE_ASYNC if the
1774 * element is still performing a state change or
1775 * %GST_STATE_CHANGE_FAILURE if the last state change failed.
1779 GstStateChangeReturn
1780 gst_element_get_state (GstElement * element,
1781 GstState * state, GstState * pending, GstClockTime timeout)
1783 GstElementClass *oclass;
1784 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
1786 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
1788 oclass = GST_ELEMENT_GET_CLASS (element);
1790 if (oclass->get_state)
1791 result = (oclass->get_state) (element, state, pending, timeout);
1797 * gst_element_abort_state:
1798 * @element: a #GstElement to abort the state of.
1800 * Abort the state change of the element. This function is used
1801 * by elements that do asynchronous state changes and find out
1802 * something is wrong.
1804 * This function should be called with the STATE_LOCK held.
1809 gst_element_abort_state (GstElement * element)
1813 #ifndef GST_DISABLE_GST_DEBUG
1817 g_return_if_fail (GST_IS_ELEMENT (element));
1819 GST_OBJECT_LOCK (element);
1820 pending = GST_STATE_PENDING (element);
1822 if (pending == GST_STATE_VOID_PENDING ||
1823 GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
1824 goto nothing_aborted;
1826 #ifndef GST_DISABLE_GST_DEBUG
1827 old_state = GST_STATE (element);
1829 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1830 "aborting state from %s to %s", gst_element_state_get_name (old_state),
1831 gst_element_state_get_name (pending));
1835 GST_STATE_RETURN (element) = GST_STATE_CHANGE_FAILURE;
1837 GST_STATE_BROADCAST (element);
1838 GST_OBJECT_UNLOCK (element);
1844 GST_OBJECT_UNLOCK (element);
1850 * gst_element_continue_state:
1851 * @element: a #GstElement to continue the state change of.
1852 * @ret: The previous state return value
1854 * Commit the state change of the element and proceed to the next
1855 * pending state if any. This function is used
1856 * by elements that do asynchronous state changes.
1857 * The core will normally call this method automatically when an
1858 * element returned SUCCESS from the state change function.
1859 * Elements that return ASYNC from the change_state function should
1860 * eventually call this method from the streaming thread to signal
1861 * successfull state change completion.
1863 * If after calling this method the element still has not reached
1864 * the pending state, the next state change is performed.
1866 * Returns: The result of the commit state change.
1870 GstStateChangeReturn
1871 gst_element_continue_state (GstElement * element, GstStateChangeReturn ret)
1874 GstState old_ret, old_state, old_next;
1875 GstState current, next;
1876 GstMessage *message;
1877 GstStateChange transition;
1879 GST_OBJECT_LOCK (element);
1880 old_ret = GST_STATE_RETURN (element);
1881 GST_STATE_RETURN (element) = ret;
1882 pending = GST_STATE_PENDING (element);
1884 /* check if there is something to commit */
1885 if (pending == GST_STATE_VOID_PENDING)
1886 goto nothing_pending;
1888 old_state = GST_STATE (element);
1889 /* this is the state we should go to next */
1890 old_next = GST_STATE_NEXT (element);
1891 /* update current state */
1892 current = GST_STATE (element) = old_next;
1894 /* see if we reached the final state */
1895 if (pending == current)
1898 next = GST_STATE_GET_NEXT (current, pending);
1899 transition = GST_STATE_TRANSITION (current, next);
1901 GST_STATE_NEXT (element) = next;
1903 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
1904 GST_OBJECT_UNLOCK (element);
1906 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1907 "committing state from %s to %s, pending %s",
1908 gst_element_state_get_name (old_state),
1909 gst_element_state_get_name (old_next),
1910 gst_element_state_get_name (pending));
1912 message = gst_message_new_state_changed (GST_OBJECT (element),
1913 old_state, old_next, pending);
1914 gst_element_post_message (element, message);
1916 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1917 "continue state change %s to %s, final %s",
1918 gst_element_state_get_name (current),
1919 gst_element_state_get_name (next), gst_element_state_get_name (pending));
1921 ret = gst_element_change_state (element, transition);
1927 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "nothing pending");
1928 GST_OBJECT_UNLOCK (element);
1933 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
1934 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
1936 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "completed state change");
1937 GST_OBJECT_UNLOCK (element);
1939 /* don't post silly messages with the same state. This can happen
1940 * when an element state is changed to what it already was. For bins
1941 * this can be the result of a lost state, which we check with the
1942 * previous return value.
1943 * We do signal the cond though as a _get_state() might be blocking
1945 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC) {
1946 message = gst_message_new_state_changed (GST_OBJECT (element),
1947 old_state, old_next, GST_STATE_VOID_PENDING);
1948 gst_element_post_message (element, message);
1951 GST_STATE_BROADCAST (element);
1958 * gst_element_lost_state:
1959 * @element: a #GstElement the state is lost of
1961 * Brings the element to the lost state. The current state of the
1962 * element is copied to the pending state so that any call to
1963 * gst_element_get_state() will return ASYNC.
1965 * This is mostly used for elements that lost their preroll buffer
1966 * in the PAUSED state after a flush, they become PAUSED again
1967 * if a new preroll buffer is queued.
1968 * This function can only be called when the element is currently
1969 * not in error or an async state change.
1971 * This function can only be called with the STATE_LOCK held.
1976 gst_element_lost_state (GstElement * element)
1978 GstState current_state;
1979 GstMessage *message;
1981 g_return_if_fail (GST_IS_ELEMENT (element));
1983 GST_OBJECT_LOCK (element);
1984 if (GST_STATE_PENDING (element) != GST_STATE_VOID_PENDING ||
1985 GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
1988 current_state = GST_STATE (element);
1990 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1991 "lost state of %s", gst_element_state_get_name (current_state));
1993 GST_STATE_NEXT (element) = current_state;
1994 GST_STATE_PENDING (element) = current_state;
1995 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
1996 GST_OBJECT_UNLOCK (element);
1998 message = gst_message_new_state_changed (GST_OBJECT (element),
1999 current_state, current_state, current_state);
2000 gst_element_post_message (element, message);
2002 /* and mark us dirty */
2003 message = gst_message_new_state_dirty (GST_OBJECT (element));
2004 gst_element_post_message (element, message);
2010 GST_OBJECT_UNLOCK (element);
2016 * gst_element_set_state:
2017 * @element: a #GstElement to change state of.
2018 * @state: the element's new #GstState.
2020 * Sets the state of the element. This function will try to set the
2021 * requested state by going through all the intermediary states and calling
2022 * the class's state change function for each.
2024 * This function can return #GST_STATE_CHANGE_ASYNC, in which case the
2025 * element will perform the remainder of the state change asynchronously in
2027 * An application can use gst_element_get_state() to wait for the completion
2028 * of the state change or it can wait for a state change message on the bus.
2030 * Returns: Result of the state change using #GstStateChangeReturn.
2034 GstStateChangeReturn
2035 gst_element_set_state (GstElement * element, GstState state)
2037 GstElementClass *oclass;
2038 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2040 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2042 oclass = GST_ELEMENT_GET_CLASS (element);
2044 if (oclass->set_state)
2045 result = (oclass->set_state) (element, state);
2051 * default set state function, calculates the next state based
2052 * on current state and calls the change_state function
2054 static GstStateChangeReturn
2055 gst_element_set_state_func (GstElement * element, GstState state)
2057 GstState current, next, old_pending;
2058 GstStateChangeReturn ret;
2059 GstStateChange transition;
2060 GstStateChangeReturn old_ret;
2062 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2064 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "set_state to %s",
2065 gst_element_state_get_name (state));
2067 /* state lock is taken to protect the set_state() and get_state()
2068 * procedures, it does not lock any variables. */
2069 GST_STATE_LOCK (element);
2071 /* now calculate how to get to the new state */
2072 GST_OBJECT_LOCK (element);
2073 old_ret = GST_STATE_RETURN (element);
2074 /* previous state change returned an error, remove all pending
2075 * and next states */
2076 if (old_ret == GST_STATE_CHANGE_FAILURE) {
2077 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2078 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2079 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
2082 current = GST_STATE (element);
2083 next = GST_STATE_NEXT (element);
2084 old_pending = GST_STATE_PENDING (element);
2085 element->state_cookie++;
2087 /* this is the (new) state we should go to */
2088 GST_STATE_PENDING (element) = state;
2090 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2091 "current %s, old_pending %s, next %s, old return %d",
2092 gst_element_state_get_name (current),
2093 gst_element_state_get_name (old_pending),
2094 gst_element_state_get_name (next), old_ret);
2096 /* if the element was busy doing a state change, we just update the
2097 * target state, it'll get to it async then. */
2098 if (old_pending != GST_STATE_VOID_PENDING) {
2099 /* upwards state change will happen ASYNC */
2100 if (old_pending <= state)
2102 /* element is going to this state already */
2103 else if (next == state)
2105 /* element was performing an ASYNC upward state change and
2106 * we request to go downward again. Start from the next pending
2108 else if (next > state
2109 && GST_STATE_RETURN (element) == GST_STATE_CHANGE_ASYNC) {
2113 next = GST_STATE_GET_NEXT (current, state);
2114 /* now we store the next state */
2115 GST_STATE_NEXT (element) = next;
2116 /* mark busy, we need to check that there is actually a state change
2117 * to be done else we could accidentally override SUCCESS/NO_PREROLL and
2118 * the default element change_state function has no way to know what the
2119 * old value was... could consider this a FIXME...*/
2120 if (current != next)
2121 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2123 transition = GST_STATE_TRANSITION (current, next);
2125 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2126 "%s: setting state from %s to %s",
2127 (next != state ? "intermediate" : "final"),
2128 gst_element_state_get_name (current), gst_element_state_get_name (next));
2130 /* now signal any waiters, they will error since the cookie was increased */
2131 GST_STATE_BROADCAST (element);
2133 GST_OBJECT_UNLOCK (element);
2135 ret = gst_element_change_state (element, transition);
2137 GST_STATE_UNLOCK (element);
2139 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "returned %d", ret);
2145 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2146 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2147 "element was busy with async state change");
2148 GST_OBJECT_UNLOCK (element);
2150 GST_STATE_UNLOCK (element);
2152 return GST_STATE_CHANGE_ASYNC;
2156 /* with STATE_LOCK */
2157 static GstStateChangeReturn
2158 gst_element_change_state (GstElement * element, GstStateChange transition)
2160 GstElementClass *oclass;
2161 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2165 oclass = GST_ELEMENT_GET_CLASS (element);
2167 /* start with the current state. */
2168 current = GST_STATE_TRANSITION_CURRENT (transition);
2169 next = GST_STATE_TRANSITION_NEXT (transition);
2171 /* call the state change function so it can set the state */
2172 if (oclass->change_state)
2173 ret = (oclass->change_state) (element, transition);
2175 ret = GST_STATE_CHANGE_FAILURE;
2178 case GST_STATE_CHANGE_FAILURE:
2179 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2180 "have FAILURE change_state return");
2181 /* state change failure */
2182 gst_element_abort_state (element);
2184 case GST_STATE_CHANGE_ASYNC:
2185 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2186 "element will change state ASYNC");
2188 /* if we go upwards, we give the app a change to wait for
2193 /* else we just continue the state change downwards */
2194 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2195 "forcing commit state %s < %s",
2196 gst_element_state_get_name (current),
2197 gst_element_state_get_name (next));
2199 ret = gst_element_continue_state (element, GST_STATE_CHANGE_SUCCESS);
2201 case GST_STATE_CHANGE_SUCCESS:
2202 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2203 "element changed state SUCCESS");
2204 /* we can commit the state now which will proceeed to
2206 ret = gst_element_continue_state (element, ret);
2208 case GST_STATE_CHANGE_NO_PREROLL:
2209 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2210 "element changed state NO_PREROLL");
2211 /* we can commit the state now which will proceeed to
2213 ret = gst_element_continue_state (element, ret);
2216 goto invalid_return;
2219 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit state change %d", ret);
2224 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit async state change %d",
2232 GST_OBJECT_LOCK (element);
2233 /* somebody added a GST_STATE_ and forgot to do stuff here ! */
2234 g_critical ("%s: unknown return value %d from a state change function",
2235 GST_ELEMENT_NAME (element), ret);
2237 ret = GST_STATE_CHANGE_FAILURE;
2238 GST_STATE_RETURN (element) = ret;
2239 GST_OBJECT_UNLOCK (element);
2245 /* gst_iterator_fold functions for pads_activate */
2248 activate_pads (GstPad * pad, GValue * ret, gboolean * active)
2250 if (!gst_pad_set_active (pad, *active))
2251 g_value_set_boolean (ret, FALSE);
2253 gst_pad_set_caps (pad, NULL);
2255 gst_object_unref (pad);
2259 /* returns false on error or early cutout of the fold, true otherwise */
2261 iterator_fold_with_resync (GstIterator * iter, GstIteratorFoldFunction func,
2262 GValue * ret, gpointer user_data)
2264 GstIteratorResult ires;
2265 gboolean res = TRUE;
2268 ires = gst_iterator_fold (iter, func, ret, user_data);
2271 case GST_ITERATOR_RESYNC:
2272 gst_iterator_resync (iter);
2274 case GST_ITERATOR_DONE:
2287 /* is called with STATE_LOCK
2290 gst_element_pads_activate (GstElement * element, gboolean active)
2292 GValue ret = { 0, };
2296 GST_DEBUG_OBJECT (element, "pads_activate with active %d", active);
2297 /* no need to unset this later, it's just a boolean */
2298 g_value_init (&ret, G_TYPE_BOOLEAN);
2299 g_value_set_boolean (&ret, TRUE);
2302 iter = gst_element_iterate_src_pads (element);
2304 iter = gst_element_iterate_sink_pads (element);
2305 fold_ok = iterator_fold_with_resync
2306 (iter, (GstIteratorFoldFunction) activate_pads, &ret, &active);
2307 gst_iterator_free (iter);
2308 if (!fold_ok || !g_value_get_boolean (&ret)) {
2309 GST_DEBUG_OBJECT (element, "pads_activate failed");
2314 iter = gst_element_iterate_sink_pads (element);
2316 iter = gst_element_iterate_src_pads (element);
2317 fold_ok = iterator_fold_with_resync
2318 (iter, (GstIteratorFoldFunction) activate_pads, &ret, &active);
2319 gst_iterator_free (iter);
2320 if (!fold_ok || !g_value_get_boolean (&ret)) {
2321 GST_DEBUG_OBJECT (element, "pads_activate failed");
2325 GST_DEBUG_OBJECT (element, "pads_activate successful");
2329 /* is called with STATE_LOCK */
2330 static GstStateChangeReturn
2331 gst_element_change_state_func (GstElement * element, GstStateChange transition)
2333 GstState state, next;
2334 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
2336 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2338 state = GST_STATE_TRANSITION_CURRENT (transition);
2339 next = GST_STATE_TRANSITION_NEXT (transition);
2341 /* if the element already is in the given state, we just return success */
2342 if (next == GST_STATE_VOID_PENDING || state == next)
2345 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element,
2346 "default handler tries setting state from %s to %s (%04x)",
2347 gst_element_state_get_name (state),
2348 gst_element_state_get_name (next), transition);
2350 switch (transition) {
2351 case GST_STATE_CHANGE_NULL_TO_READY:
2353 case GST_STATE_CHANGE_READY_TO_PAUSED:
2354 if (!gst_element_pads_activate (element, TRUE)) {
2355 result = GST_STATE_CHANGE_FAILURE;
2358 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2360 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2362 case GST_STATE_CHANGE_PAUSED_TO_READY:
2363 case GST_STATE_CHANGE_READY_TO_NULL:
2364 /* deactivate pads in both cases, since they are activated on
2365 ready->paused but the element might not have made it to paused */
2366 if (!gst_element_pads_activate (element, FALSE)) {
2367 result = GST_STATE_CHANGE_FAILURE;
2369 gst_element_set_base_time (element, 0);
2373 /* this will catch real but unhandled state changes;
2374 * can only be caused by:
2375 * - a new state was added
2376 * - somehow the element was asked to jump across an intermediate state
2378 g_warning ("Unhandled state change from %s to %s",
2379 gst_element_state_get_name (state),
2380 gst_element_state_get_name (next));
2387 GST_OBJECT_LOCK (element);
2388 result = GST_STATE_RETURN (element);
2389 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2390 "element is already in the %s state",
2391 gst_element_state_get_name (state));
2392 GST_OBJECT_UNLOCK (element);
2399 * gst_element_get_factory:
2400 * @element: a #GstElement to request the element factory of.
2402 * Retrieves the factory that was used to create this element.
2404 * Returns: the #GstElementFactory used for creating this element.
2405 * no refcounting is needed.
2408 gst_element_get_factory (GstElement * element)
2410 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
2412 return GST_ELEMENT_GET_CLASS (element)->elementfactory;
2416 gst_element_dispose (GObject * object)
2418 GstElement *element = GST_ELEMENT (object);
2420 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "dispose");
2422 if (GST_STATE (element) != GST_STATE_NULL) {
2424 ("\nTrying to dispose element %s, but it is not in the NULL state.\n"
2425 "You need to explicitly set elements to the NULL state before\n"
2426 "dropping the final reference, to allow them to clean up.\n",
2427 GST_OBJECT_NAME (element));
2430 g_return_if_fail (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING);
2432 GST_DEBUG ("removing %d pads", g_list_length (element->pads));
2433 /* first we break all our links with the outside */
2434 while (element->pads && element->pads->data) {
2435 /* don't call _remove_pad with NULL */
2436 gst_element_remove_pad (element, GST_PAD_CAST (element->pads->data));
2438 if (G_UNLIKELY (element->pads != 0)) {
2439 g_critical ("could not remove pads from element %s",
2440 GST_STR_NULL (GST_OBJECT_NAME (object)));
2443 GST_OBJECT_LOCK (element);
2444 gst_object_replace ((GstObject **) & element->clock, NULL);
2445 gst_object_replace ((GstObject **) & element->bus, NULL);
2446 GST_OBJECT_UNLOCK (element);
2448 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "parent class dispose");
2450 G_OBJECT_CLASS (parent_class)->dispose (object);
2454 gst_element_finalize (GObject * object)
2456 GstElement *element = GST_ELEMENT (object);
2458 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize");
2460 GST_STATE_LOCK (element);
2461 if (element->state_cond)
2462 g_cond_free (element->state_cond);
2463 element->state_cond = NULL;
2464 GST_STATE_UNLOCK (element);
2465 g_static_rec_mutex_free (element->state_lock);
2466 g_free (element->state_lock);
2467 element->state_lock = NULL;
2469 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize parent");
2471 G_OBJECT_CLASS (parent_class)->finalize (object);
2474 #ifndef GST_DISABLE_LOADSAVE
2476 * gst_element_save_thyself:
2477 * @element: a #GstElement to save.
2478 * @parent: the xml parent node.
2480 * Saves the element as part of the given XML structure.
2482 * Returns: the new #xmlNodePtr.
2485 gst_element_save_thyself (GstObject * object, xmlNodePtr parent)
2488 GstElementClass *oclass;
2489 GParamSpec **specs, *spec;
2492 GValue value = { 0, };
2493 GstElement *element;
2495 g_return_val_if_fail (GST_IS_ELEMENT (object), parent);
2497 element = GST_ELEMENT (object);
2499 oclass = GST_ELEMENT_GET_CLASS (element);
2501 xmlNewChild (parent, NULL, (xmlChar *) "name",
2502 (xmlChar *) GST_ELEMENT_NAME (element));
2504 if (oclass->elementfactory != NULL) {
2505 GstElementFactory *factory = (GstElementFactory *) oclass->elementfactory;
2507 xmlNewChild (parent, NULL, (xmlChar *) "type",
2508 (xmlChar *) GST_PLUGIN_FEATURE (factory)->name);
2512 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &nspecs);
2514 for (i = 0; i < nspecs; i++) {
2516 if (spec->flags & G_PARAM_READABLE) {
2520 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
2522 g_object_get_property (G_OBJECT (element), spec->name, &value);
2523 param = xmlNewChild (parent, NULL, (xmlChar *) "param", NULL);
2524 xmlNewChild (param, NULL, (xmlChar *) "name", (xmlChar *) spec->name);
2526 if (G_IS_PARAM_SPEC_STRING (spec))
2527 contents = g_value_dup_string (&value);
2528 else if (G_IS_PARAM_SPEC_ENUM (spec))
2529 contents = g_strdup_printf ("%d", g_value_get_enum (&value));
2530 else if (G_IS_PARAM_SPEC_INT64 (spec))
2531 contents = g_strdup_printf ("%" G_GINT64_FORMAT,
2532 g_value_get_int64 (&value));
2534 contents = g_strdup_value_contents (&value);
2536 xmlNewChild (param, NULL, (xmlChar *) "value", (xmlChar *) contents);
2539 g_value_unset (&value);
2545 pads = GST_ELEMENT_PADS (element);
2548 GstPad *pad = GST_PAD (pads->data);
2550 /* figure out if it's a direct pad or a ghostpad */
2551 if (GST_ELEMENT (GST_OBJECT_PARENT (pad)) == element) {
2552 xmlNodePtr padtag = xmlNewChild (parent, NULL, (xmlChar *) "pad", NULL);
2554 gst_object_save_thyself (GST_OBJECT (pad), padtag);
2556 pads = g_list_next (pads);
2563 gst_element_restore_thyself (GstObject * object, xmlNodePtr self)
2565 xmlNodePtr children;
2566 GstElement *element;
2568 gchar *value = NULL;
2570 element = GST_ELEMENT (object);
2571 g_return_if_fail (element != NULL);
2574 children = self->xmlChildrenNode;
2576 if (!strcmp ((char *) children->name, "param")) {
2577 xmlNodePtr child = children->xmlChildrenNode;
2580 if (!strcmp ((char *) child->name, "name")) {
2581 name = (gchar *) xmlNodeGetContent (child);
2582 } else if (!strcmp ((char *) child->name, "value")) {
2583 value = (gchar *) xmlNodeGetContent (child);
2585 child = child->next;
2587 /* FIXME: can this just be g_object_set ? */
2588 gst_util_set_object_arg (G_OBJECT (element), name, value);
2589 /* g_object_set (G_OBJECT (element), name, value, NULL); */
2593 children = children->next;
2597 children = self->xmlChildrenNode;
2599 if (!strcmp ((char *) children->name, "pad")) {
2600 gst_pad_load_and_link (children, GST_OBJECT (element));
2602 children = children->next;
2605 if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
2606 (GST_OBJECT_CLASS (parent_class)->restore_thyself) (object, self);
2608 #endif /* GST_DISABLE_LOADSAVE */
2611 gst_element_set_bus_func (GstElement * element, GstBus * bus)
2613 g_return_if_fail (GST_IS_ELEMENT (element));
2615 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element, "setting bus to %p", bus);
2617 GST_OBJECT_LOCK (element);
2618 gst_object_replace ((GstObject **) & GST_ELEMENT_BUS (element),
2619 GST_OBJECT_CAST (bus));
2620 GST_OBJECT_UNLOCK (element);
2624 * gst_element_set_bus:
2625 * @element: a #GstElement to set the bus of.
2626 * @bus: the #GstBus to set.
2628 * Sets the bus of the element. For internal use only, unless you're
2634 gst_element_set_bus (GstElement * element, GstBus * bus)
2636 GstElementClass *oclass;
2638 g_return_if_fail (GST_IS_ELEMENT (element));
2640 oclass = GST_ELEMENT_GET_CLASS (element);
2642 if (oclass->set_bus)
2643 oclass->set_bus (element, bus);
2647 * gst_element_get_bus:
2648 * @element: a #GstElement to get the bus of.
2650 * Returns the bus of the element.
2652 * Returns: the element's #GstBus. unref after usage.
2657 gst_element_get_bus (GstElement * element)
2659 GstBus *result = NULL;
2661 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
2663 GST_OBJECT_LOCK (element);
2664 result = GST_ELEMENT_BUS (element);
2665 gst_object_ref (result);
2666 GST_OBJECT_UNLOCK (element);
2668 GST_DEBUG_OBJECT (element, "got bus %" GST_PTR_FORMAT, result);