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 abstract base class needed to construct an element that
29 * can be used in a GStreamer pipeline. Please refer to the plugin writers
30 * guide for more information on creating #GstElement subclasses.
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 2006-03-12 (0.10.5)
79 #include "gst_private.h"
82 #include <gobject/gvaluecollector.h>
84 #include "gstelement.h"
85 #include "gstenumtypes.h"
87 #include "gstmarshal.h"
92 #include "gst-i18n-lib.h"
94 /* Element signals and args */
110 extern void __gst_element_details_clear (GstElementDetails * dp);
111 extern void __gst_element_details_copy (GstElementDetails * dest,
112 const GstElementDetails * src);
114 static void gst_element_class_init (GstElementClass * klass);
115 static void gst_element_init (GstElement * element);
116 static void gst_element_base_class_init (gpointer g_class);
117 static void gst_element_base_class_finalize (gpointer g_class);
119 static void gst_element_dispose (GObject * object);
120 static void gst_element_finalize (GObject * object);
122 static GstStateChangeReturn gst_element_change_state_func (GstElement * element,
123 GstStateChange transition);
124 static GstStateChangeReturn gst_element_get_state_func (GstElement * element,
125 GstState * state, GstState * pending, GstClockTime timeout);
126 static GstStateChangeReturn gst_element_set_state_func (GstElement * element,
128 static void gst_element_set_bus_func (GstElement * element, GstBus * bus);
130 static gboolean gst_element_default_send_event (GstElement * element,
132 static gboolean gst_element_default_query (GstElement * element,
135 static GstPadTemplate
136 * gst_element_class_get_request_pad_template (GstElementClass *
137 element_class, const gchar * name);
139 #ifndef GST_DISABLE_LOADSAVE
140 static xmlNodePtr gst_element_save_thyself (GstObject * object,
142 static void gst_element_restore_thyself (GstObject * parent, xmlNodePtr self);
145 static GstObjectClass *parent_class = NULL;
146 static guint gst_element_signals[LAST_SIGNAL] = { 0 };
149 gst_element_get_type (void)
151 static GType gst_element_type = 0;
153 if (G_UNLIKELY (gst_element_type == 0)) {
154 static const GTypeInfo element_info = {
155 sizeof (GstElementClass),
156 gst_element_base_class_init,
157 gst_element_base_class_finalize,
158 (GClassInitFunc) gst_element_class_init,
163 (GInstanceInitFunc) gst_element_init,
167 gst_element_type = g_type_register_static (GST_TYPE_OBJECT, "GstElement",
168 &element_info, G_TYPE_FLAG_ABSTRACT);
170 return gst_element_type;
174 gst_element_class_init (GstElementClass * klass)
176 GObjectClass *gobject_class;
177 GstObjectClass *gstobject_class;
179 gobject_class = (GObjectClass *) klass;
180 gstobject_class = (GstObjectClass *) klass;
182 parent_class = g_type_class_peek_parent (klass);
185 * GstElement::pad-added:
186 * @gstelement: the object which received the signal
187 * @new_pad: the pad that has been added
189 * a new #GstPad has been added to the element.
191 gst_element_signals[PAD_ADDED] =
192 g_signal_new ("pad-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
193 G_STRUCT_OFFSET (GstElementClass, pad_added), NULL, NULL,
194 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
196 * GstElement::pad-removed:
197 * @gstelement: the object which received the signal
198 * @old_pad: the pad that has been removed
200 * a #GstPad has been removed from the element
202 gst_element_signals[PAD_REMOVED] =
203 g_signal_new ("pad-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
204 G_STRUCT_OFFSET (GstElementClass, pad_removed), NULL, NULL,
205 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
207 * GstElement::no-more-pads:
208 * @gstelement: the object which received the signal
210 * This signals that the element will not generate more dynamic pads.
212 gst_element_signals[NO_MORE_PADS] =
213 g_signal_new ("no-more-pads", G_TYPE_FROM_CLASS (klass),
214 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstElementClass, no_more_pads), NULL,
215 NULL, gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
217 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_element_dispose);
218 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_element_finalize);
220 #ifndef GST_DISABLE_LOADSAVE
221 gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_element_save_thyself);
222 gstobject_class->restore_thyself =
223 GST_DEBUG_FUNCPTR (gst_element_restore_thyself);
226 klass->change_state = GST_DEBUG_FUNCPTR (gst_element_change_state_func);
227 klass->set_state = GST_DEBUG_FUNCPTR (gst_element_set_state_func);
228 klass->get_state = GST_DEBUG_FUNCPTR (gst_element_get_state_func);
229 klass->set_bus = GST_DEBUG_FUNCPTR (gst_element_set_bus_func);
230 klass->query = GST_DEBUG_FUNCPTR (gst_element_default_query);
231 klass->send_event = GST_DEBUG_FUNCPTR (gst_element_default_send_event);
232 klass->numpadtemplates = 0;
234 klass->elementfactory = NULL;
238 gst_element_base_class_init (gpointer g_class)
240 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
242 memset (&element_class->details, 0, sizeof (GstElementDetails));
243 element_class->padtemplates = NULL;
247 gst_element_base_class_finalize (gpointer g_class)
249 GstElementClass *klass = GST_ELEMENT_CLASS (g_class);
251 g_list_foreach (klass->padtemplates, (GFunc) gst_object_unref, NULL);
252 g_list_free (klass->padtemplates);
253 __gst_element_details_clear (&klass->details);
257 gst_element_init (GstElement * element)
259 GST_STATE (element) = GST_STATE_NULL;
260 GST_STATE_TARGET (element) = GST_STATE_NULL;
261 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
262 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
263 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
265 element->state_lock = g_new0 (GStaticRecMutex, 1);
266 g_static_rec_mutex_init (element->state_lock);
267 element->state_cond = g_cond_new ();
271 * gst_element_default_error:
272 * @object: a #GObject that signalled the error.
273 * @orig: the #GstObject that initiated the error.
274 * @error: the GError.
275 * @debug: an additional debug information string, or %NULL.
277 * A default error signal callback to attach to an element.
278 * The user data passed to the g_signal_connect is ignored.
280 * The default handler will simply print the error string using g_print.
285 gst_element_default_error (GObject * object, GstObject * source, GError * error,
288 gchar *name = gst_object_get_path_string (source);
290 g_print (_("ERROR: from element %s: %s\n"), name, error->message);
292 g_print (_("Additional debug info:\n%s\n"), debug);
298 * gst_element_release_request_pad:
299 * @element: a #GstElement to release the request pad of.
300 * @pad: the #GstPad to release.
302 * Makes the element free the previously requested pad as obtained
303 * with gst_element_get_request_pad().
308 gst_element_release_request_pad (GstElement * element, GstPad * pad)
310 GstElementClass *oclass;
312 g_return_if_fail (GST_IS_ELEMENT (element));
313 g_return_if_fail (GST_IS_PAD (pad));
315 oclass = GST_ELEMENT_GET_CLASS (element);
317 /* if the element implements a custom release function we call that, else we
318 * simply remove the pad from the element */
319 if (oclass->release_pad)
320 (oclass->release_pad) (element, pad);
322 gst_element_remove_pad (element, pad);
326 * gst_element_requires_clock:
327 * @element: a #GstElement to query
329 * Query if the element requires a clock.
331 * Returns: %TRUE if the element requires a clock
336 gst_element_requires_clock (GstElement * element)
340 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
342 result = (GST_ELEMENT_GET_CLASS (element)->set_clock != NULL);
348 * gst_element_provides_clock:
349 * @element: a #GstElement to query
351 * Query if the element provides a clock. A #GstClock provided by an
352 * element can be used as the global #GstClock for the pipeline.
353 * An element that can provide a clock is only required to do so in the PAUSED
354 * state, this means when it is fully negotiated and has allocated the resources
355 * to operate the clock.
357 * Returns: %TRUE if the element provides a clock
362 gst_element_provides_clock (GstElement * element)
366 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
368 result = (GST_ELEMENT_GET_CLASS (element)->provide_clock != NULL);
374 * gst_element_provide_clock:
375 * @element: a #GstElement to query
377 * Get the clock provided by the given element.
378 * <note>An element is only required to provide a clock in the PAUSED
379 * state. Some elements can provide a clock in other states.</note>
381 * Returns: the GstClock provided by the element or %NULL
382 * if no clock could be provided. Unref after usage.
387 gst_element_provide_clock (GstElement * element)
389 GstClock *result = NULL;
390 GstElementClass *oclass;
392 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
394 oclass = GST_ELEMENT_GET_CLASS (element);
396 if (oclass->provide_clock)
397 result = oclass->provide_clock (element);
403 * gst_element_set_clock:
404 * @element: a #GstElement to set the clock for.
405 * @clock: the #GstClock to set for the element.
407 * Sets the clock for the element. This function increases the
408 * refcount on the clock. Any previously set clock on the object
411 * Returns: %TRUE if the element accepted the clock. An element can refuse a
412 * clock when it, for example, is not able to slave its internal clock to the
413 * @clock or when it requires a specific clock to operate.
418 gst_element_set_clock (GstElement * element, GstClock * clock)
420 GstElementClass *oclass;
424 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
426 oclass = GST_ELEMENT_GET_CLASS (element);
428 GST_DEBUG_OBJECT (element, "setting clock %p", clock);
430 if (oclass->set_clock)
431 res = oclass->set_clock (element, clock);
434 /* only update the clock pointer if the element accepted the clock */
435 GST_OBJECT_LOCK (element);
436 clock_p = &element->clock;
437 gst_object_replace ((GstObject **) clock_p, (GstObject *) clock);
438 GST_OBJECT_UNLOCK (element);
444 * gst_element_get_clock:
445 * @element: a #GstElement to get the clock of.
447 * Gets the currently configured clock of the element. This is the clock as was
448 * last set with gst_element_set_clock().
450 * Returns: the #GstClock of the element. unref after usage.
455 gst_element_get_clock (GstElement * element)
459 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
461 GST_OBJECT_LOCK (element);
462 if ((result = element->clock))
463 gst_object_ref (result);
464 GST_OBJECT_UNLOCK (element);
470 * gst_element_set_base_time:
471 * @element: a #GstElement.
472 * @time: the base time to set.
474 * Set the base time of an element. See gst_element_get_base_time().
479 gst_element_set_base_time (GstElement * element, GstClockTime time)
483 g_return_if_fail (GST_IS_ELEMENT (element));
485 GST_OBJECT_LOCK (element);
486 old = element->base_time;
487 element->base_time = time;
488 GST_OBJECT_UNLOCK (element);
490 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element,
491 "set base_time=%" GST_TIME_FORMAT ", old %" GST_TIME_FORMAT,
492 GST_TIME_ARGS (time), GST_TIME_ARGS (old));
496 * gst_element_get_base_time:
497 * @element: a #GstElement.
499 * Returns the base time of the element. The base time is the
500 * absolute time of the clock when this element was last put to
501 * PLAYING. Subtracting the base time from the clock time gives
502 * the stream time of the element.
504 * Returns: the base time of the element.
509 gst_element_get_base_time (GstElement * element)
513 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
515 GST_OBJECT_LOCK (element);
516 result = element->base_time;
517 GST_OBJECT_UNLOCK (element);
522 #ifndef GST_DISABLE_INDEX
524 * gst_element_is_indexable:
525 * @element: a #GstElement.
527 * Queries if the element can be indexed.
529 * Returns: TRUE if the element can be indexed.
534 gst_element_is_indexable (GstElement * element)
538 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
540 result = (GST_ELEMENT_GET_CLASS (element)->set_index != NULL);
546 * gst_element_set_index:
547 * @element: a #GstElement.
548 * @index: a #GstIndex.
550 * Set @index on the element. The refcount of the index
551 * will be increased, any previously set index is unreffed.
556 gst_element_set_index (GstElement * element, GstIndex * index)
558 GstElementClass *oclass;
560 g_return_if_fail (GST_IS_ELEMENT (element));
561 g_return_if_fail (GST_IS_INDEX (index));
563 oclass = GST_ELEMENT_GET_CLASS (element);
565 if (oclass->set_index)
566 oclass->set_index (element, index);
570 * gst_element_get_index:
571 * @element: a #GstElement.
573 * Gets the index from the element.
575 * Returns: a #GstIndex or %NULL when no index was set on the
576 * element. unref after usage.
581 gst_element_get_index (GstElement * element)
583 GstElementClass *oclass;
584 GstIndex *result = NULL;
586 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
588 oclass = GST_ELEMENT_GET_CLASS (element);
590 if (oclass->get_index)
591 result = oclass->get_index (element);
598 * gst_element_add_pad:
599 * @element: a #GstElement to add the pad to.
600 * @pad: the #GstPad to add to the element.
602 * Adds a pad (link point) to @element. @pad's parent will be set to @element;
603 * see gst_object_set_parent() for refcounting information.
605 * Pads are not automatically activated so elements should perform the needed
606 * steps to activate the pad in case this pad is added in the PAUSED or PLAYING
607 * state. See gst_pad_set_active() for more information about activating pads.
609 * The pad and the element should be unlocked when calling this function.
611 * This function will emit the #GstElement::pad-added signal on the element.
613 * Returns: %TRUE if the pad could be added. This function can fail when
614 * a pad with the same name already existed or the pad already had another
620 gst_element_add_pad (GstElement * element, GstPad * pad)
625 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
626 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
628 /* locking pad to look at the name */
629 GST_OBJECT_LOCK (pad);
630 pad_name = g_strdup (GST_PAD_NAME (pad));
631 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "adding pad '%s'",
632 GST_STR_NULL (pad_name));
633 flushing = GST_PAD_IS_FLUSHING (pad);
634 GST_OBJECT_UNLOCK (pad);
636 /* then check to see if there's already a pad by that name here */
637 GST_OBJECT_LOCK (element);
638 if (G_UNLIKELY (!gst_object_check_uniqueness (element->pads, pad_name)))
641 /* try to set the pad's parent */
642 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (pad),
643 GST_OBJECT_CAST (element))))
646 /* check for flushing pads */
647 if (flushing && (GST_STATE (element) > GST_STATE_READY ||
648 GST_STATE_NEXT (element) == GST_STATE_PAUSED)) {
649 g_warning ("adding flushing pad '%s' to running element '%s'",
650 GST_STR_NULL (pad_name), GST_ELEMENT_NAME (element));
652 GST_OBJECT_LOCK (pad);
653 GST_PAD_UNSET_FLUSHING (pad);
654 GST_OBJECT_UNLOCK (pad);
659 /* add it to the list */
660 switch (gst_pad_get_direction (pad)) {
662 element->srcpads = g_list_prepend (element->srcpads, pad);
663 element->numsrcpads++;
666 element->sinkpads = g_list_prepend (element->sinkpads, pad);
667 element->numsinkpads++;
672 element->pads = g_list_prepend (element->pads, pad);
674 element->pads_cookie++;
675 GST_OBJECT_UNLOCK (element);
677 /* emit the PAD_ADDED signal */
678 g_signal_emit (element, gst_element_signals[PAD_ADDED], 0, pad);
685 g_critical ("Padname %s is not unique in element %s, not adding",
686 pad_name, GST_ELEMENT_NAME (element));
687 GST_OBJECT_UNLOCK (element);
694 ("Pad %s already has parent when trying to add to element %s",
695 pad_name, GST_ELEMENT_NAME (element));
696 GST_OBJECT_UNLOCK (element);
702 GST_OBJECT_LOCK (pad);
704 ("Trying to add pad %s to element %s, but it has no direction",
705 GST_OBJECT_NAME (pad), GST_ELEMENT_NAME (element));
706 GST_OBJECT_UNLOCK (pad);
707 GST_OBJECT_UNLOCK (element);
713 * gst_element_remove_pad:
714 * @element: a #GstElement to remove pad from.
715 * @pad: the #GstPad to remove from the element.
717 * Removes @pad from @element. @pad will be destroyed if it has not been
718 * referenced elsewhere using gst_object_unparent().
720 * This function is used by plugin developers and should not be used
721 * by applications. Pads that were dynamically requested from elements
722 * with gst_element_get_request_pad() should be released with the
723 * gst_element_release_request_pad() function instead.
725 * Pads are not automatically deactivated so elements should perform the needed
726 * steps to deactivate the pad in case this pad is removed in the PAUSED or
727 * PLAYING state. See gst_pad_set_active() for more information about
730 * The pad and the element should be unlocked when calling this function.
732 * This function will emit the #GstElement::pad-removed signal on the element.
734 * Returns: %TRUE if the pad could be removed. Can return %FALSE if the
735 * pad does not belong to the provided element.
740 gst_element_remove_pad (GstElement * element, GstPad * pad)
744 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
745 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
747 /* locking pad to look at the name and parent */
748 GST_OBJECT_LOCK (pad);
749 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "removing pad '%s'",
750 GST_STR_NULL (GST_PAD_NAME (pad)));
752 if (G_UNLIKELY (GST_PAD_PARENT (pad) != element))
754 GST_OBJECT_UNLOCK (pad);
757 if ((peer = gst_pad_get_peer (pad))) {
758 /* window for MT unsafeness, someone else could unlink here
759 * and then we call unlink with wrong pads. The unlink
760 * function would catch this and safely return failed. */
761 if (GST_PAD_IS_SRC (pad))
762 gst_pad_unlink (pad, peer);
764 gst_pad_unlink (peer, pad);
766 gst_object_unref (peer);
769 GST_OBJECT_LOCK (element);
770 /* remove it from the list */
771 switch (gst_pad_get_direction (pad)) {
773 element->srcpads = g_list_remove (element->srcpads, pad);
774 element->numsrcpads--;
777 element->sinkpads = g_list_remove (element->sinkpads, pad);
778 element->numsinkpads--;
781 g_critical ("Removing pad without direction???");
784 element->pads = g_list_remove (element->pads, pad);
786 element->pads_cookie++;
787 GST_OBJECT_UNLOCK (element);
789 /* emit the PAD_REMOVED signal before unparenting and losing the last ref. */
790 g_signal_emit (element, gst_element_signals[PAD_REMOVED], 0, pad);
792 gst_object_unparent (GST_OBJECT_CAST (pad));
799 /* FIXME, locking order? */
800 GST_OBJECT_LOCK (element);
801 g_critical ("Padname %s:%s does not belong to element %s when removing",
802 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
803 GST_OBJECT_UNLOCK (element);
804 GST_OBJECT_UNLOCK (pad);
810 * gst_element_no_more_pads:
811 * @element: a #GstElement
813 * Use this function to signal that the element does not expect any more pads
814 * to show up in the current pipeline. This function should be called whenever
815 * pads have been added by the element itself. Elements with #GST_PAD_SOMETIMES
816 * pad templates use this in combination with autopluggers to figure out that
817 * the element is done initializing its pads.
819 * This function emits the #GstElement::no-more-pads signal.
824 gst_element_no_more_pads (GstElement * element)
826 g_return_if_fail (GST_IS_ELEMENT (element));
828 g_signal_emit (element, gst_element_signals[NO_MORE_PADS], 0);
832 pad_compare_name (GstPad * pad1, const gchar * name)
836 GST_OBJECT_LOCK (pad1);
837 result = strcmp (GST_PAD_NAME (pad1), name);
838 GST_OBJECT_UNLOCK (pad1);
844 * gst_element_get_static_pad:
845 * @element: a #GstElement to find a static pad of.
846 * @name: the name of the static #GstPad to retrieve.
848 * Retrieves a pad from @element by name. This version only retrieves
849 * already-existing (i.e. 'static') pads.
851 * Returns: the requested #GstPad if found, otherwise %NULL. unref after
857 gst_element_get_static_pad (GstElement * element, const gchar * name)
860 GstPad *result = NULL;
862 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
863 g_return_val_if_fail (name != NULL, NULL);
865 GST_OBJECT_LOCK (element);
867 g_list_find_custom (element->pads, name, (GCompareFunc) pad_compare_name);
869 result = GST_PAD_CAST (find->data);
870 gst_object_ref (result);
873 if (result == NULL) {
874 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "no such pad '%s' in element \"%s\"",
875 name, GST_ELEMENT_NAME (element));
877 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
878 GST_ELEMENT_NAME (element), name);
880 GST_OBJECT_UNLOCK (element);
886 gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
889 GstPad *newpad = NULL;
890 GstElementClass *oclass;
892 oclass = GST_ELEMENT_GET_CLASS (element);
894 if (oclass->request_new_pad)
895 newpad = (oclass->request_new_pad) (element, templ, name);
898 gst_object_ref (newpad);
904 * gst_element_get_request_pad:
905 * @element: a #GstElement to find a request pad of.
906 * @name: the name of the request #GstPad to retrieve.
908 * Retrieves a pad from the element by name. This version only retrieves
909 * request pads. The pad should be released with
910 * gst_element_release_request_pad().
912 * Returns: requested #GstPad if found, otherwise %NULL. Release after usage.
915 gst_element_get_request_pad (GstElement * element, const gchar * name)
917 GstPadTemplate *templ = NULL;
919 const gchar *req_name = NULL;
920 gboolean templ_found = FALSE;
924 gchar *str, *endptr = NULL;
925 GstElementClass *class;
927 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
928 g_return_val_if_fail (name != NULL, NULL);
930 class = GST_ELEMENT_GET_CLASS (element);
932 /* if the name contains a %, we assume it's the complete template name. Get
933 * the template and try to get a pad */
934 if (strstr (name, "%")) {
935 templ = gst_element_class_get_request_pad_template (class, name);
940 /* there is no % in the name, try to find a matching template */
941 list = gst_element_class_get_pad_template_list (class);
942 while (!templ_found && list) {
943 templ = (GstPadTemplate *) list->data;
944 if (templ->presence == GST_PAD_REQUEST) {
945 GST_CAT_DEBUG (GST_CAT_PADS, "comparing %s to %s", name,
946 templ->name_template);
947 /* see if we find an exact match */
948 if (strcmp (name, templ->name_template) == 0) {
953 /* Because of sanity checks in gst_pad_template_new(), we know that %s
954 and %d, occurring at the end of the name_template, are the only
956 else if ((str = strchr (templ->name_template, '%'))
957 && strncmp (templ->name_template, name,
958 str - templ->name_template) == 0
959 && strlen (name) > str - templ->name_template) {
960 data = name + (str - templ->name_template);
961 if (*(str + 1) == 'd') {
963 n = (gint) strtol (data, &endptr, 10);
964 if (endptr && *endptr == '\0') {
984 pad = gst_element_request_pad (element, templ, req_name);
990 * gst_element_get_pad:
991 * @element: a #GstElement.
992 * @name: the name of the pad to retrieve.
994 * Retrieves a pad from @element by name. Tries gst_element_get_static_pad()
995 * first, then gst_element_get_request_pad().
997 * <note>Usage of this function is not recommended as it is unclear if the reference
998 * to the result pad should be released with gst_object_unref() in case of a static pad
999 * or gst_element_release_request_pad() in case of a request pad.</note>
1001 * Returns: the #GstPad if found, otherwise %NULL. Unref or Release after usage,
1002 * depending on the type of the pad.
1005 gst_element_get_pad (GstElement * element, const gchar * name)
1009 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1010 g_return_val_if_fail (name != NULL, NULL);
1012 pad = gst_element_get_static_pad (element, name);
1014 pad = gst_element_get_request_pad (element, name);
1019 static GstIteratorItem
1020 iterate_pad (GstIterator * it, GstPad * pad)
1022 gst_object_ref (pad);
1023 return GST_ITERATOR_ITEM_PASS;
1026 static GstIterator *
1027 gst_element_iterate_pad_list (GstElement * element, GList ** padlist)
1029 GstIterator *result;
1031 GST_OBJECT_LOCK (element);
1032 gst_object_ref (element);
1033 result = gst_iterator_new_list (GST_TYPE_PAD,
1034 GST_OBJECT_GET_LOCK (element),
1035 &element->pads_cookie,
1038 (GstIteratorItemFunction) iterate_pad,
1039 (GstIteratorDisposeFunction) gst_object_unref);
1040 GST_OBJECT_UNLOCK (element);
1046 * gst_element_iterate_pads:
1047 * @element: a #GstElement to iterate pads of.
1049 * Retrieves an iterattor of @element's pads. The iterator should
1050 * be freed after usage.
1052 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1057 gst_element_iterate_pads (GstElement * element)
1059 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1061 return gst_element_iterate_pad_list (element, &element->pads);
1065 * gst_element_iterate_src_pads:
1066 * @element: a #GstElement.
1068 * Retrieves an iterator of @element's source pads.
1070 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1075 gst_element_iterate_src_pads (GstElement * element)
1077 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1079 return gst_element_iterate_pad_list (element, &element->srcpads);
1083 * gst_element_iterate_sink_pads:
1084 * @element: a #GstElement.
1086 * Retrieves an iterator of @element's sink pads.
1088 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1093 gst_element_iterate_sink_pads (GstElement * element)
1095 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1097 return gst_element_iterate_pad_list (element, &element->sinkpads);
1101 * gst_element_class_add_pad_template:
1102 * @klass: the #GstElementClass to add the pad template to.
1103 * @templ: a #GstPadTemplate to add to the element class.
1105 * Adds a padtemplate to an element class. This is mainly used in the _base_init
1106 * functions of classes.
1109 gst_element_class_add_pad_template (GstElementClass * klass,
1110 GstPadTemplate * templ)
1112 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1113 g_return_if_fail (GST_IS_PAD_TEMPLATE (templ));
1115 /* avoid registering pad templates with the same name */
1116 g_return_if_fail (gst_element_class_get_pad_template (klass,
1117 templ->name_template) == NULL);
1119 klass->padtemplates = g_list_append (klass->padtemplates,
1120 gst_object_ref (templ));
1121 klass->numpadtemplates++;
1125 * gst_element_class_set_details:
1126 * @klass: class to set details for
1127 * @details: details to set
1129 * Sets the detailed information for a #GstElementClass.
1130 * <note>This function is for use in _base_init functions only.</note>
1132 * The @details are copied.
1135 gst_element_class_set_details (GstElementClass * klass,
1136 const GstElementDetails * details)
1138 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1139 g_return_if_fail (GST_IS_ELEMENT_DETAILS (details));
1141 __gst_element_details_copy (&klass->details, details);
1145 * gst_element_class_set_details_simple:
1146 * @klass: class to set details for
1147 * @longname: The long English name of the element. E.g. "File Sink"
1148 * @classification: String describing the type of element, as an unordered list
1149 * separated with slashes ('/'). See draft-klass.txt of the design docs
1150 * for more details and common types. E.g: "Sink/File"
1151 * @description: Sentence describing the purpose of the element.
1152 * E.g: "Write stream to a file"
1153 * @author: Name and contact details of the author(s). Use \n to separate
1154 * multiple author details. E.g: "Joe Bloggs <joe.blogs at foo.com>"
1156 * Sets the detailed information for a #GstElementClass. Simpler version of
1157 * gst_element_class_set_details() that generates less linker overhead.
1158 * <note>This function is for use in _base_init functions only.</note>
1160 * The detail parameter strings are copied into the #GstElementDetails for
1161 * the element class.
1166 gst_element_class_set_details_simple (GstElementClass * klass,
1167 const gchar * longname, const gchar * classification,
1168 const gchar * description, const gchar * author)
1170 const GstElementDetails details =
1171 GST_ELEMENT_DETAILS ((gchar *) longname, (gchar *) classification,
1172 (gchar *) description, (gchar *) author);
1174 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1176 __gst_element_details_copy (&klass->details, &details);
1180 * gst_element_class_get_pad_template_list:
1181 * @element_class: a #GstElementClass to get pad templates of.
1183 * Retrieves a list of the pad templates associated with @element_class. The
1184 * list must not be modified by the calling code.
1185 * <note>If you use this function in the #GInstanceInitFunc of an object class
1186 * that has subclasses, make sure to pass the g_class parameter of the
1187 * #GInstanceInitFunc here.</note>
1189 * Returns: the #GList of padtemplates.
1192 gst_element_class_get_pad_template_list (GstElementClass * element_class)
1194 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1196 return element_class->padtemplates;
1200 * gst_element_class_get_pad_template:
1201 * @element_class: a #GstElementClass to get the pad template of.
1202 * @name: the name of the #GstPadTemplate to get.
1204 * Retrieves a padtemplate from @element_class with the given name.
1205 * <note>If you use this function in the #GInstanceInitFunc of an object class
1206 * that has subclasses, make sure to pass the g_class parameter of the
1207 * #GInstanceInitFunc here.</note>
1209 * Returns: the #GstPadTemplate with the given name, or %NULL if none was found.
1210 * No unreferencing is necessary.
1213 gst_element_class_get_pad_template (GstElementClass * element_class,
1218 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1219 g_return_val_if_fail (name != NULL, NULL);
1221 padlist = gst_element_class_get_pad_template_list (element_class);
1224 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
1226 if (strcmp (padtempl->name_template, name) == 0)
1229 padlist = g_list_next (padlist);
1235 static GstPadTemplate *
1236 gst_element_class_get_request_pad_template (GstElementClass * element_class,
1239 GstPadTemplate *tmpl;
1241 tmpl = gst_element_class_get_pad_template (element_class, name);
1242 if (tmpl != NULL && tmpl->presence == GST_PAD_REQUEST)
1248 /* get a random pad on element of the given direction.
1249 * The pad is random in a sense that it is the first pad that is (optionaly) linked.
1252 gst_element_get_random_pad (GstElement * element, gboolean need_linked,
1253 GstPadDirection dir)
1255 GstPad *result = NULL;
1258 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "getting a random pad");
1262 GST_OBJECT_LOCK (element);
1263 pads = element->srcpads;
1266 GST_OBJECT_LOCK (element);
1267 pads = element->sinkpads;
1270 goto wrong_direction;
1272 for (; pads; pads = g_list_next (pads)) {
1273 GstPad *pad = GST_PAD_CAST (pads->data);
1275 GST_OBJECT_LOCK (pad);
1276 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "checking pad %s:%s",
1277 GST_DEBUG_PAD_NAME (pad));
1279 if (need_linked && !GST_PAD_IS_LINKED (pad)) {
1280 /* if we require a linked pad, and it is not linked, continue the
1282 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is not linked",
1283 GST_DEBUG_PAD_NAME (pad));
1284 GST_OBJECT_UNLOCK (pad);
1287 /* found a pad, stop search */
1288 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
1289 GST_DEBUG_PAD_NAME (pad));
1290 GST_OBJECT_UNLOCK (pad);
1296 gst_object_ref (result);
1298 GST_OBJECT_UNLOCK (element);
1302 /* ERROR handling */
1305 g_warning ("unknown pad direction %d", dir);
1311 gst_element_default_send_event (GstElement * element, GstEvent * event)
1313 gboolean result = FALSE;
1316 pad = GST_EVENT_IS_DOWNSTREAM (event) ?
1317 gst_element_get_random_pad (element, TRUE, GST_PAD_SRC) :
1318 gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1321 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1322 "pushing %s event to random %s pad %s:%s",
1323 GST_EVENT_TYPE_NAME (event),
1324 (GST_PAD_DIRECTION (pad) == GST_PAD_SRC ? "src" : "sink"),
1325 GST_DEBUG_PAD_NAME (pad));
1327 result = gst_pad_push_event (pad, event);
1328 gst_object_unref (pad);
1330 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "can't send %s event on element %s",
1331 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1337 * gst_element_send_event:
1338 * @element: a #GstElement to send the event to.
1339 * @event: the #GstEvent to send to the element.
1341 * Sends an event to an element. If the element doesn't implement an
1342 * event handler, the event will be pushed on a random linked sink pad for
1343 * upstream events or a random linked source pad for downstream events.
1345 * This function takes owership of the provided event so you should
1346 * gst_event_ref() it if you want to reuse the event after this call.
1348 * Returns: %TRUE if the event was handled.
1353 gst_element_send_event (GstElement * element, GstEvent * event)
1355 GstElementClass *oclass;
1356 gboolean result = FALSE;
1358 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1359 g_return_val_if_fail (event != NULL, FALSE);
1361 oclass = GST_ELEMENT_GET_CLASS (element);
1363 GST_STATE_LOCK (element);
1364 if (oclass->send_event) {
1365 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send %s event on element %s",
1366 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1367 result = oclass->send_event (element, event);
1369 result = gst_element_default_send_event (element, event);
1371 GST_STATE_UNLOCK (element);
1378 * @element: a #GstElement to send the event to.
1379 * @rate: The new playback rate
1380 * @format: The format of the seek values
1381 * @flags: The optional seek flags.
1382 * @cur_type: The type and flags for the new current position
1383 * @cur: The value of the new current position
1384 * @stop_type: The type and flags for the new stop position
1385 * @stop: The value of the new stop position
1387 * Sends a seek event to an element. See gst_event_new_seek() for the details of
1388 * the parameters. The seek event is sent to the element using
1389 * gst_element_send_event().
1391 * Returns: %TRUE if the event was handled.
1396 gst_element_seek (GstElement * element, gdouble rate, GstFormat format,
1397 GstSeekFlags flags, GstSeekType cur_type, gint64 cur,
1398 GstSeekType stop_type, gint64 stop)
1403 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1406 gst_event_new_seek (rate, format, flags, cur_type, cur, stop_type, stop);
1407 result = gst_element_send_event (element, event);
1413 * gst_element_get_query_types:
1414 * @element: a #GstElement to query
1416 * Get an array of query types from the element.
1417 * If the element doesn't implement a query types function,
1418 * the query will be forwarded to the peer of a random linked sink pad.
1420 * Returns: An array of #GstQueryType elements that should not
1421 * be freed or modified.
1425 const GstQueryType *
1426 gst_element_get_query_types (GstElement * element)
1428 GstElementClass *oclass;
1429 const GstQueryType *result = NULL;
1431 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1433 oclass = GST_ELEMENT_GET_CLASS (element);
1435 if (oclass->get_query_types) {
1436 result = oclass->get_query_types (element);
1438 GstPad *pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1441 GstPad *peer = gst_pad_get_peer (pad);
1444 result = gst_pad_get_query_types (peer);
1446 gst_object_unref (peer);
1448 gst_object_unref (pad);
1455 gst_element_default_query (GstElement * element, GstQuery * query)
1457 gboolean result = FALSE;
1460 pad = gst_element_get_random_pad (element, FALSE, GST_PAD_SRC);
1462 result = gst_pad_query (pad, query);
1464 gst_object_unref (pad);
1466 pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1468 GstPad *peer = gst_pad_get_peer (pad);
1471 result = gst_pad_query (peer, query);
1473 gst_object_unref (peer);
1475 gst_object_unref (pad);
1482 * gst_element_query:
1483 * @element: a #GstElement to perform the query on.
1484 * @query: the #GstQuery.
1486 * Performs a query on the given element.
1488 * For elements that don't implement a query handler, this function
1489 * forwards the query to a random srcpad or to the peer of a
1490 * random linked sinkpad of this element.
1492 * Returns: TRUE if the query could be performed.
1497 gst_element_query (GstElement * element, GstQuery * query)
1499 GstElementClass *oclass;
1500 gboolean result = FALSE;
1502 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1503 g_return_val_if_fail (query != NULL, FALSE);
1505 oclass = GST_ELEMENT_GET_CLASS (element);
1507 if (oclass->query) {
1508 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send query on element %s",
1509 GST_ELEMENT_NAME (element));
1510 result = oclass->query (element, query);
1512 result = gst_element_default_query (element, query);
1518 * gst_element_post_message:
1519 * @element: a #GstElement posting the message
1520 * @message: a #GstMessage to post
1522 * Post a message on the element's #GstBus. This function takes ownership of the
1523 * message; if you want to access the message after this call, you should add an
1524 * additional reference before calling.
1526 * Returns: %TRUE if the message was successfully posted. The function returns
1527 * %FALSE if the element did not have a bus.
1532 gst_element_post_message (GstElement * element, GstMessage * message)
1535 gboolean result = FALSE;
1537 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1538 g_return_val_if_fail (message != NULL, FALSE);
1540 GST_OBJECT_LOCK (element);
1543 if (G_UNLIKELY (bus == NULL))
1546 gst_object_ref (bus);
1547 GST_OBJECT_UNLOCK (element);
1549 /* we release the element lock when posting the message so that any
1550 * (synchronous) message handlers can operate on the element */
1551 result = gst_bus_post (bus, message);
1552 gst_object_unref (bus);
1559 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, element,
1560 "not posting message %p: no bus", message);
1561 GST_OBJECT_UNLOCK (element);
1562 gst_message_unref (message);
1568 * _gst_element_error_printf:
1569 * @format: the printf-like format to use, or %NULL
1571 * This function is only used internally by the gst_element_error() macro.
1573 * Returns: a newly allocated string, or %NULL if the format was %NULL or ""
1578 _gst_element_error_printf (const gchar * format, ...)
1588 va_start (args, format);
1589 buffer = g_strdup_vprintf (format, args);
1595 * gst_element_message_full:
1596 * @element: a #GstElement to send message from
1597 * @type: the #GstMessageType
1598 * @domain: the GStreamer GError domain this message belongs to
1599 * @code: the GError code belonging to the domain
1600 * @text: an allocated text string to be used as a replacement for the
1601 * default message connected to code, or %NULL
1602 * @debug: an allocated debug message to be used as a replacement for the
1603 * default debugging information, or %NULL
1604 * @file: the source code file where the error was generated
1605 * @function: the source code function where the error was generated
1606 * @line: the source code line where the error was generated
1608 * Post an error, warning or info message on the bus from inside an element.
1610 * @type must be of #GST_MESSAGE_ERROR, #GST_MESSAGE_WARNING or
1611 * #GST_MESSAGE_INFO.
1615 void gst_element_message_full
1616 (GstElement * element, GstMessageType type,
1617 GQuark domain, gint code, gchar * text,
1618 gchar * debug, const gchar * file, const gchar * function, gint line)
1620 GError *gerror = NULL;
1624 gboolean has_debug = TRUE;
1625 GstMessage *message = NULL;
1628 GST_DEBUG_OBJECT (element, "start");
1629 g_return_if_fail (GST_IS_ELEMENT (element));
1630 g_return_if_fail ((type == GST_MESSAGE_ERROR) ||
1631 (type == GST_MESSAGE_WARNING) || (type == GST_MESSAGE_INFO));
1633 /* check if we send the given text or the default error text */
1634 if ((text == NULL) || (text[0] == 0)) {
1635 /* text could have come from g_strdup_printf (""); */
1637 sent_text = gst_error_get_message (domain, code);
1641 /* construct a sent_debug with extra information from source */
1642 if ((debug == NULL) || (debug[0] == 0)) {
1643 /* debug could have come from g_strdup_printf (""); */
1647 name = gst_object_get_path_string (GST_OBJECT_CAST (element));
1649 sent_debug = g_strdup_printf ("%s(%d): %s (): %s:\n%s",
1650 file, line, function, name, debug);
1652 sent_debug = g_strdup_printf ("%s(%d): %s (): %s",
1653 file, line, function, name);
1657 /* create gerror and post message */
1658 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posting message: %s",
1660 gerror = g_error_new_literal (domain, code, sent_text);
1663 case GST_MESSAGE_ERROR:
1665 gst_message_new_error (GST_OBJECT_CAST (element), gerror, sent_debug);
1667 case GST_MESSAGE_WARNING:
1668 message = gst_message_new_warning (GST_OBJECT_CAST (element), gerror,
1671 case GST_MESSAGE_INFO:
1672 message = gst_message_new_info (GST_OBJECT_CAST (element), gerror,
1676 g_assert_not_reached ();
1679 gst_element_post_message (element, message);
1681 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posted %s message: %s",
1682 (type == GST_MESSAGE_ERROR ? "error" : "warning"), sent_text);
1685 g_error_free (gerror);
1686 g_free (sent_debug);
1691 * gst_element_is_locked_state:
1692 * @element: a #GstElement.
1694 * Checks if the state of an element is locked.
1695 * If the state of an element is locked, state changes of the parent don't
1696 * affect the element.
1697 * This way you can leave currently unused elements inside bins. Just lock their
1698 * state before changing the state from #GST_STATE_NULL.
1702 * Returns: TRUE, if the element's state is locked.
1705 gst_element_is_locked_state (GstElement * element)
1709 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1711 GST_OBJECT_LOCK (element);
1712 result = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1713 GST_OBJECT_UNLOCK (element);
1719 * gst_element_set_locked_state:
1720 * @element: a #GstElement
1721 * @locked_state: TRUE to lock the element's state
1723 * Locks the state of an element, so state changes of the parent don't affect
1724 * this element anymore.
1728 * Returns: TRUE if the state was changed, FALSE if bad parameters were given
1729 * or the elements state-locking needed no change.
1732 gst_element_set_locked_state (GstElement * element, gboolean locked_state)
1736 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1738 GST_OBJECT_LOCK (element);
1739 old = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1741 if (G_UNLIKELY (old == locked_state))
1745 GST_CAT_DEBUG (GST_CAT_STATES, "locking state of element %s",
1746 GST_ELEMENT_NAME (element));
1747 GST_OBJECT_FLAG_SET (element, GST_ELEMENT_LOCKED_STATE);
1749 GST_CAT_DEBUG (GST_CAT_STATES, "unlocking state of element %s",
1750 GST_ELEMENT_NAME (element));
1751 GST_OBJECT_FLAG_UNSET (element, GST_ELEMENT_LOCKED_STATE);
1753 GST_OBJECT_UNLOCK (element);
1759 GST_CAT_DEBUG (GST_CAT_STATES, "elements %s was in locked state %d",
1760 GST_ELEMENT_NAME (element), old);
1761 GST_OBJECT_UNLOCK (element);
1768 * gst_element_sync_state_with_parent:
1769 * @element: a #GstElement.
1771 * Tries to change the state of the element to the same as its parent.
1772 * If this function returns FALSE, the state of element is undefined.
1774 * Returns: TRUE, if the element's state could be synced to the parent's state.
1779 gst_element_sync_state_with_parent (GstElement * element)
1783 GstStateChangeReturn ret;
1785 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1787 if ((parent = GST_ELEMENT_CAST (gst_element_get_parent (element)))) {
1788 GstState parent_current, parent_pending;
1790 GST_OBJECT_LOCK (parent);
1791 parent_current = GST_STATE (parent);
1792 parent_pending = GST_STATE_PENDING (parent);
1793 GST_OBJECT_UNLOCK (parent);
1795 /* set to pending if there is one, else we set it to the current state of
1797 if (parent_pending != GST_STATE_VOID_PENDING)
1798 target = parent_pending;
1800 target = parent_current;
1802 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1803 "syncing state (%s) to parent %s %s (%s, %s)",
1804 gst_element_state_get_name (GST_STATE (element)),
1805 GST_ELEMENT_NAME (parent), gst_element_state_get_name (target),
1806 gst_element_state_get_name (parent_current),
1807 gst_element_state_get_name (parent_pending));
1809 ret = gst_element_set_state (element, target);
1810 if (ret == GST_STATE_CHANGE_FAILURE)
1813 gst_object_unref (parent);
1817 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "element has no parent");
1824 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1825 "syncing state failed (%s)",
1826 gst_element_state_change_return_get_name (ret));
1832 static GstStateChangeReturn
1833 gst_element_get_state_func (GstElement * element,
1834 GstState * state, GstState * pending, GstClockTime timeout)
1836 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
1837 GstState old_pending;
1839 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "getting state, timeout %"
1840 GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
1842 GST_OBJECT_LOCK (element);
1843 ret = GST_STATE_RETURN (element);
1844 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "RETURN is %s",
1845 gst_element_state_change_return_get_name (ret));
1847 /* we got an error, report immediatly */
1848 if (ret == GST_STATE_CHANGE_FAILURE)
1851 /* we got no_preroll, report immediatly */
1852 if (ret == GST_STATE_CHANGE_NO_PREROLL)
1855 /* no need to wait async if we are not async */
1856 if (ret != GST_STATE_CHANGE_ASYNC)
1859 old_pending = GST_STATE_PENDING (element);
1860 if (old_pending != GST_STATE_VOID_PENDING) {
1861 GTimeVal *timeval, abstimeout;
1864 if (timeout != GST_CLOCK_TIME_NONE) {
1865 glong add = timeout / 1000;
1870 /* make timeout absolute */
1871 g_get_current_time (&abstimeout);
1872 g_time_val_add (&abstimeout, add);
1873 timeval = &abstimeout;
1877 /* get cookie to dected state change during waiting */
1878 cookie = element->state_cookie;
1880 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1881 "waiting for element to commit state");
1883 /* we have a pending state change, wait for it to complete */
1884 if (!GST_STATE_TIMED_WAIT (element, timeval)) {
1885 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timed out");
1886 /* timeout triggered */
1887 ret = GST_STATE_CHANGE_ASYNC;
1889 if (cookie != element->state_cookie)
1892 /* could be success or failure */
1893 if (old_pending == GST_STATE (element)) {
1894 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got success");
1895 ret = GST_STATE_CHANGE_SUCCESS;
1897 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got failure");
1898 ret = GST_STATE_CHANGE_FAILURE;
1901 /* if nothing is pending anymore we can return SUCCESS */
1902 if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
1903 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "nothing pending");
1904 ret = GST_STATE_CHANGE_SUCCESS;
1910 *state = GST_STATE (element);
1912 *pending = GST_STATE_PENDING (element);
1914 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1915 "state current: %s, pending: %s, result: %s",
1916 gst_element_state_get_name (GST_STATE (element)),
1917 gst_element_state_get_name (GST_STATE_PENDING (element)),
1918 gst_element_state_change_return_get_name (ret));
1919 GST_OBJECT_UNLOCK (element);
1926 *state = GST_STATE_VOID_PENDING;
1928 *pending = GST_STATE_VOID_PENDING;
1930 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "interruped");
1932 GST_OBJECT_UNLOCK (element);
1934 return GST_STATE_CHANGE_FAILURE;
1939 * gst_element_get_state:
1940 * @element: a #GstElement to get the state of.
1941 * @state: a pointer to #GstState to hold the state. Can be %NULL.
1942 * @pending: a pointer to #GstState to hold the pending state.
1944 * @timeout: a #GstClockTime to specify the timeout for an async
1945 * state change or %GST_CLOCK_TIME_NONE for infinite timeout.
1947 * Gets the state of the element.
1949 * For elements that performed an ASYNC state change, as reported by
1950 * gst_element_set_state(), this function will block up to the
1951 * specified timeout value for the state change to complete.
1952 * If the element completes the state change or goes into
1953 * an error, this function returns immediately with a return value of
1954 * %GST_STATE_CHANGE_SUCCESS or %GST_STATE_CHANGE_FAILURE respectively.
1956 * For elements that did not return %GST_STATE_CHANGE_ASYNC, this function
1957 * returns the current and pending state immediately.
1959 * This function returns %GST_STATE_CHANGE_NO_PREROLL if the element
1960 * successfully changed its state but is not able to provide data yet.
1961 * This mostly happens for live sources that only produce data in the PLAYING
1962 * state. While the state change return is equivalent to
1963 * %GST_STATE_CHANGE_SUCCESS, it is returned to the application to signal that
1964 * some sink elements might not be able to complete their state change because
1965 * an element is not producing data to complete the preroll. When setting the
1966 * element to playing, the preroll will complete and playback will start.
1968 * Returns: %GST_STATE_CHANGE_SUCCESS if the element has no more pending state
1969 * and the last state change succeeded, %GST_STATE_CHANGE_ASYNC if the
1970 * element is still performing a state change or
1971 * %GST_STATE_CHANGE_FAILURE if the last state change failed.
1975 GstStateChangeReturn
1976 gst_element_get_state (GstElement * element,
1977 GstState * state, GstState * pending, GstClockTime timeout)
1979 GstElementClass *oclass;
1980 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
1982 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
1984 oclass = GST_ELEMENT_GET_CLASS (element);
1986 if (oclass->get_state)
1987 result = (oclass->get_state) (element, state, pending, timeout);
1993 * gst_element_abort_state:
1994 * @element: a #GstElement to abort the state of.
1996 * Abort the state change of the element. This function is used
1997 * by elements that do asynchronous state changes and find out
1998 * something is wrong.
2000 * This function should be called with the STATE_LOCK held.
2005 gst_element_abort_state (GstElement * element)
2009 #ifndef GST_DISABLE_GST_DEBUG
2013 g_return_if_fail (GST_IS_ELEMENT (element));
2015 GST_OBJECT_LOCK (element);
2016 pending = GST_STATE_PENDING (element);
2018 if (pending == GST_STATE_VOID_PENDING ||
2019 GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2020 goto nothing_aborted;
2022 #ifndef GST_DISABLE_GST_DEBUG
2023 old_state = GST_STATE (element);
2025 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2026 "aborting state from %s to %s", gst_element_state_get_name (old_state),
2027 gst_element_state_get_name (pending));
2031 GST_STATE_RETURN (element) = GST_STATE_CHANGE_FAILURE;
2033 GST_STATE_BROADCAST (element);
2034 GST_OBJECT_UNLOCK (element);
2040 GST_OBJECT_UNLOCK (element);
2046 * gst_element_continue_state:
2047 * @element: a #GstElement to continue the state change of.
2048 * @ret: The previous state return value
2050 * Commit the state change of the element and proceed to the next
2051 * pending state if any. This function is used
2052 * by elements that do asynchronous state changes.
2053 * The core will normally call this method automatically when an
2054 * element returned %GST_STATE_CHANGE_SUCCESS from the state change function.
2056 * If after calling this method the element still has not reached
2057 * the pending state, the next state change is performed.
2059 * This method is used internally and should normally not be called by plugins
2062 * Returns: The result of the commit state change.
2066 GstStateChangeReturn
2067 gst_element_continue_state (GstElement * element, GstStateChangeReturn ret)
2069 GstStateChangeReturn old_ret;
2070 GstState old_state, old_next;
2071 GstState current, next, pending;
2072 GstMessage *message;
2073 GstStateChange transition;
2075 GST_OBJECT_LOCK (element);
2076 old_ret = GST_STATE_RETURN (element);
2077 GST_STATE_RETURN (element) = ret;
2078 pending = GST_STATE_PENDING (element);
2080 /* check if there is something to commit */
2081 if (pending == GST_STATE_VOID_PENDING)
2082 goto nothing_pending;
2084 old_state = GST_STATE (element);
2085 /* this is the state we should go to next */
2086 old_next = GST_STATE_NEXT (element);
2087 /* update current state */
2088 current = GST_STATE (element) = old_next;
2090 /* see if we reached the final state */
2091 if (pending == current)
2094 next = GST_STATE_GET_NEXT (current, pending);
2095 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2097 GST_STATE_NEXT (element) = next;
2099 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2100 GST_OBJECT_UNLOCK (element);
2102 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2103 "committing state from %s to %s, pending %s",
2104 gst_element_state_get_name (old_state),
2105 gst_element_state_get_name (old_next),
2106 gst_element_state_get_name (pending));
2108 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2109 old_state, old_next, pending);
2110 gst_element_post_message (element, message);
2112 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2113 "continue state change %s to %s, final %s",
2114 gst_element_state_get_name (current),
2115 gst_element_state_get_name (next), gst_element_state_get_name (pending));
2117 ret = gst_element_change_state (element, transition);
2123 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "nothing pending");
2124 GST_OBJECT_UNLOCK (element);
2129 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2130 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2132 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2133 "completed state change to %s", gst_element_state_get_name (pending));
2134 GST_OBJECT_UNLOCK (element);
2136 /* don't post silly messages with the same state. This can happen
2137 * when an element state is changed to what it already was. For bins
2138 * this can be the result of a lost state, which we check with the
2139 * previous return value.
2140 * We do signal the cond though as a _get_state() might be blocking
2142 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC) {
2143 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2144 "posting state-changed %s to %s",
2145 gst_element_state_get_name (old_state),
2146 gst_element_state_get_name (old_next));
2148 gst_message_new_state_changed (GST_OBJECT_CAST (element), old_state,
2149 old_next, GST_STATE_VOID_PENDING);
2150 gst_element_post_message (element, message);
2153 GST_STATE_BROADCAST (element);
2160 * gst_element_lost_state:
2161 * @element: a #GstElement the state is lost of
2163 * Brings the element to the lost state. The current state of the
2164 * element is copied to the pending state so that any call to
2165 * gst_element_get_state() will return %GST_STATE_CHANGE_ASYNC.
2167 * An ASYNC_START message is posted with an indication to distribute a new
2168 * base_time to the element.
2169 * If the element was PLAYING, it will go to PAUSED. The element
2170 * will be restored to its PLAYING state by the parent pipeline when it
2173 * This is mostly used for elements that lost their preroll buffer
2174 * in the %GST_STATE_PAUSED or %GST_STATE_PLAYING state after a flush,
2175 * they will go to their pending state again when a new preroll buffer is
2176 * queued. This function can only be called when the element is currently
2177 * not in error or an async state change.
2179 * This function is used internally and should normally not be called from
2180 * plugins or applications.
2185 gst_element_lost_state (GstElement * element)
2187 GstState old_state, new_state;
2188 GstMessage *message;
2190 g_return_if_fail (GST_IS_ELEMENT (element));
2192 GST_OBJECT_LOCK (element);
2193 if (GST_STATE_PENDING (element) != GST_STATE_VOID_PENDING ||
2194 GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2197 old_state = GST_STATE (element);
2199 /* when we were PLAYING, the new state is PAUSED. We will also not
2200 * automatically go to PLAYING but let the parent bin(s) set us to PLAYING
2201 * when we preroll. */
2202 if (old_state > GST_STATE_PAUSED)
2203 new_state = GST_STATE_PAUSED;
2205 new_state = old_state;
2207 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2208 "lost state of %s to %s", gst_element_state_get_name (old_state),
2209 gst_element_state_get_name (new_state));
2211 GST_STATE (element) = new_state;
2212 GST_STATE_NEXT (element) = new_state;
2213 GST_STATE_PENDING (element) = new_state;
2214 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2215 GST_OBJECT_UNLOCK (element);
2217 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2218 new_state, new_state, new_state);
2219 gst_element_post_message (element, message);
2221 message = gst_message_new_async_start (GST_OBJECT_CAST (element), TRUE);
2222 gst_element_post_message (element, message);
2228 GST_OBJECT_UNLOCK (element);
2234 * gst_element_set_state:
2235 * @element: a #GstElement to change state of.
2236 * @state: the element's new #GstState.
2238 * Sets the state of the element. This function will try to set the
2239 * requested state by going through all the intermediary states and calling
2240 * the class's state change function for each.
2242 * This function can return #GST_STATE_CHANGE_ASYNC, in which case the
2243 * element will perform the remainder of the state change asynchronously in
2245 * An application can use gst_element_get_state() to wait for the completion
2246 * of the state change or it can wait for a state change message on the bus.
2248 * Returns: Result of the state change using #GstStateChangeReturn.
2252 GstStateChangeReturn
2253 gst_element_set_state (GstElement * element, GstState state)
2255 GstElementClass *oclass;
2256 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2258 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2260 oclass = GST_ELEMENT_GET_CLASS (element);
2262 if (oclass->set_state)
2263 result = (oclass->set_state) (element, state);
2269 * default set state function, calculates the next state based
2270 * on current state and calls the change_state function
2272 static GstStateChangeReturn
2273 gst_element_set_state_func (GstElement * element, GstState state)
2275 GstState current, next, old_pending;
2276 GstStateChangeReturn ret;
2277 GstStateChange transition;
2278 GstStateChangeReturn old_ret;
2280 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2282 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "set_state to %s",
2283 gst_element_state_get_name (state));
2285 /* state lock is taken to protect the set_state() and get_state()
2286 * procedures, it does not lock any variables. */
2287 GST_STATE_LOCK (element);
2289 /* now calculate how to get to the new state */
2290 GST_OBJECT_LOCK (element);
2291 old_ret = GST_STATE_RETURN (element);
2292 /* previous state change returned an error, remove all pending
2293 * and next states */
2294 if (old_ret == GST_STATE_CHANGE_FAILURE) {
2295 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2296 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2297 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
2300 current = GST_STATE (element);
2301 next = GST_STATE_NEXT (element);
2302 old_pending = GST_STATE_PENDING (element);
2303 /* increment state cookie so that we can track each state change */
2304 element->state_cookie++;
2306 /* this is the (new) state we should go to. TARGET is the last state we set on
2308 GST_STATE_TARGET (element) = state;
2309 GST_STATE_PENDING (element) = state;
2311 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2312 "current %s, old_pending %s, next %s, old return %s",
2313 gst_element_state_get_name (current),
2314 gst_element_state_get_name (old_pending),
2315 gst_element_state_get_name (next),
2316 gst_element_state_change_return_get_name (old_ret));
2318 /* if the element was busy doing a state change, we just update the
2319 * target state, it'll get to it async then. */
2320 if (old_pending != GST_STATE_VOID_PENDING) {
2321 /* upwards state change will happen ASYNC */
2322 if (old_pending <= state)
2324 /* element is going to this state already */
2325 else if (next == state)
2327 /* element was performing an ASYNC upward state change and
2328 * we request to go downward again. Start from the next pending
2330 else if (next > state
2331 && GST_STATE_RETURN (element) == GST_STATE_CHANGE_ASYNC) {
2335 next = GST_STATE_GET_NEXT (current, state);
2336 /* now we store the next state */
2337 GST_STATE_NEXT (element) = next;
2338 /* mark busy, we need to check that there is actually a state change
2339 * to be done else we could accidentally override SUCCESS/NO_PREROLL and
2340 * the default element change_state function has no way to know what the
2341 * old value was... could consider this a FIXME...*/
2342 if (current != next)
2343 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2345 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2347 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2348 "%s: setting state from %s to %s",
2349 (next != state ? "intermediate" : "final"),
2350 gst_element_state_get_name (current), gst_element_state_get_name (next));
2352 /* now signal any waiters, they will error since the cookie was incremented */
2353 GST_STATE_BROADCAST (element);
2355 GST_OBJECT_UNLOCK (element);
2357 ret = gst_element_change_state (element, transition);
2359 GST_STATE_UNLOCK (element);
2361 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "returned %s",
2362 gst_element_state_change_return_get_name (ret));
2368 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2369 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2370 "element was busy with async state change");
2371 GST_OBJECT_UNLOCK (element);
2373 GST_STATE_UNLOCK (element);
2375 return GST_STATE_CHANGE_ASYNC;
2380 * gst_element_change_state:
2381 * @element: a #GstElement
2382 * @transition: the requested transition
2384 * Perform @transition on @element.
2386 * This function must be called with STATE_LOCK held and is mainly used
2389 * Returns: the #GstStateChangeReturn of the state transition.
2391 GstStateChangeReturn
2392 gst_element_change_state (GstElement * element, GstStateChange transition)
2394 GstElementClass *oclass;
2395 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2399 oclass = GST_ELEMENT_GET_CLASS (element);
2401 /* start with the current state. */
2402 current = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2403 next = GST_STATE_TRANSITION_NEXT (transition);
2405 /* call the state change function so it can set the state */
2406 if (oclass->change_state)
2407 ret = (oclass->change_state) (element, transition);
2409 ret = GST_STATE_CHANGE_FAILURE;
2412 case GST_STATE_CHANGE_FAILURE:
2413 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2414 "have FAILURE change_state return");
2415 /* state change failure */
2416 gst_element_abort_state (element);
2418 case GST_STATE_CHANGE_ASYNC:
2422 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2423 "element will change state ASYNC");
2425 target = GST_STATE_TARGET (element);
2427 if (target > GST_STATE_READY)
2430 /* else we just continue the state change downwards */
2431 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2432 "forcing commit state %s <= %s",
2433 gst_element_state_get_name (target),
2434 gst_element_state_get_name (GST_STATE_READY));
2436 ret = gst_element_continue_state (element, GST_STATE_CHANGE_SUCCESS);
2439 case GST_STATE_CHANGE_SUCCESS:
2440 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2441 "element changed state SUCCESS");
2442 /* we can commit the state now which will proceeed to
2444 ret = gst_element_continue_state (element, ret);
2446 case GST_STATE_CHANGE_NO_PREROLL:
2447 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2448 "element changed state NO_PREROLL");
2449 /* we can commit the state now which will proceeed to
2451 ret = gst_element_continue_state (element, ret);
2454 goto invalid_return;
2457 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit state change %d", ret);
2462 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit async state change %d",
2470 GST_OBJECT_LOCK (element);
2471 /* somebody added a GST_STATE_ and forgot to do stuff here ! */
2472 g_critical ("%s: unknown return value %d from a state change function",
2473 GST_ELEMENT_NAME (element), ret);
2475 /* we are in error now */
2476 ret = GST_STATE_CHANGE_FAILURE;
2477 GST_STATE_RETURN (element) = ret;
2478 GST_OBJECT_UNLOCK (element);
2484 /* gst_iterator_fold functions for pads_activate
2485 * Note how we don't stop the iterator when we fail an activation. This is
2486 * probably a FIXME since when one pad activation fails, we don't want to
2487 * continue our state change. */
2489 activate_pads (GstPad * pad, GValue * ret, gboolean * active)
2491 if (!gst_pad_set_active (pad, *active))
2492 g_value_set_boolean (ret, FALSE);
2494 /* unref the object that was reffed for us by _fold */
2495 gst_object_unref (pad);
2499 /* set the caps on the pad to NULL */
2501 clear_caps (GstPad * pad, GValue * ret, gboolean * active)
2503 gst_pad_set_caps (pad, NULL);
2504 gst_object_unref (pad);
2508 /* returns false on error or early cutout (will never happen because the fold
2509 * function always returns TRUE, see FIXME above) of the fold, true if all
2510 * pads in @iter were (de)activated successfully. */
2512 iterator_activate_fold_with_resync (GstIterator * iter,
2513 GstIteratorFoldFunction func, gpointer user_data)
2515 GstIteratorResult ires;
2518 /* no need to unset this later, it's just a boolean */
2519 g_value_init (&ret, G_TYPE_BOOLEAN);
2520 g_value_set_boolean (&ret, TRUE);
2523 ires = gst_iterator_fold (iter, func, &ret, user_data);
2525 case GST_ITERATOR_RESYNC:
2526 /* need to reset the result again */
2527 g_value_set_boolean (&ret, TRUE);
2528 gst_iterator_resync (iter);
2530 case GST_ITERATOR_DONE:
2531 /* all pads iterated, return collected value */
2534 /* iterator returned _ERROR or premature end with _OK,
2535 * mark an error and exit */
2536 g_value_set_boolean (&ret, FALSE);
2541 /* return collected value */
2542 return g_value_get_boolean (&ret);
2545 /* is called with STATE_LOCK
2547 * Pads are activated from source pads to sinkpads.
2550 gst_element_pads_activate (GstElement * element, gboolean active)
2555 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2556 "pads_activate with active %d", active);
2558 iter = gst_element_iterate_src_pads (element);
2560 iterator_activate_fold_with_resync (iter,
2561 (GstIteratorFoldFunction) activate_pads, &active);
2562 gst_iterator_free (iter);
2563 if (G_UNLIKELY (!res))
2566 iter = gst_element_iterate_sink_pads (element);
2568 iterator_activate_fold_with_resync (iter,
2569 (GstIteratorFoldFunction) activate_pads, &active);
2570 gst_iterator_free (iter);
2571 if (G_UNLIKELY (!res))
2575 /* clear the caps on all pads, this should never fail */
2576 iter = gst_element_iterate_pads (element);
2578 iterator_activate_fold_with_resync (iter,
2579 (GstIteratorFoldFunction) clear_caps, &active);
2580 gst_iterator_free (iter);
2581 if (G_UNLIKELY (!res))
2585 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2586 "pads_activate successful");
2593 GST_DEBUG_OBJECT (element, "source pads_activate failed");
2598 GST_DEBUG_OBJECT (element, "sink pads_activate failed");
2603 GST_DEBUG_OBJECT (element, "failed to clear caps on pads");
2608 /* is called with STATE_LOCK */
2609 static GstStateChangeReturn
2610 gst_element_change_state_func (GstElement * element, GstStateChange transition)
2612 GstState state, next;
2613 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
2615 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2617 state = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2618 next = GST_STATE_TRANSITION_NEXT (transition);
2620 /* if the element already is in the given state, we just return success */
2621 if (next == GST_STATE_VOID_PENDING || state == next)
2624 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element,
2625 "default handler tries setting state from %s to %s (%04x)",
2626 gst_element_state_get_name (state),
2627 gst_element_state_get_name (next), transition);
2629 switch (transition) {
2630 case GST_STATE_CHANGE_NULL_TO_READY:
2632 case GST_STATE_CHANGE_READY_TO_PAUSED:
2633 if (!gst_element_pads_activate (element, TRUE)) {
2634 result = GST_STATE_CHANGE_FAILURE;
2637 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2639 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2641 case GST_STATE_CHANGE_PAUSED_TO_READY:
2642 case GST_STATE_CHANGE_READY_TO_NULL:
2643 /* deactivate pads in both cases, since they are activated on
2644 ready->paused but the element might not have made it to paused */
2645 if (!gst_element_pads_activate (element, FALSE)) {
2646 result = GST_STATE_CHANGE_FAILURE;
2648 gst_element_set_base_time (element, 0);
2652 /* this will catch real but unhandled state changes;
2653 * can only be caused by:
2654 * - a new state was added
2655 * - somehow the element was asked to jump across an intermediate state
2657 g_warning ("Unhandled state change from %s to %s",
2658 gst_element_state_get_name (state),
2659 gst_element_state_get_name (next));
2666 GST_OBJECT_LOCK (element);
2667 result = GST_STATE_RETURN (element);
2668 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2669 "element is already in the %s state",
2670 gst_element_state_get_name (state));
2671 GST_OBJECT_UNLOCK (element);
2678 * gst_element_get_factory:
2679 * @element: a #GstElement to request the element factory of.
2681 * Retrieves the factory that was used to create this element.
2683 * Returns: the #GstElementFactory used for creating this element.
2684 * no refcounting is needed.
2687 gst_element_get_factory (GstElement * element)
2689 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
2691 return GST_ELEMENT_GET_CLASS (element)->elementfactory;
2695 gst_element_dispose (GObject * object)
2697 GstElement *element = GST_ELEMENT (object);
2701 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "dispose");
2703 if (GST_STATE (element) != GST_STATE_NULL)
2706 GST_DEBUG_OBJECT (element, "removing %d pads", g_list_length (element->pads));
2707 /* first we break all our links with the outside */
2708 while (element->pads && element->pads->data) {
2709 /* don't call _remove_pad with NULL */
2710 gst_element_remove_pad (element, GST_PAD_CAST (element->pads->data));
2712 if (G_UNLIKELY (element->pads != 0)) {
2713 g_critical ("could not remove pads from element %s",
2714 GST_STR_NULL (GST_OBJECT_NAME (object)));
2717 GST_OBJECT_LOCK (element);
2718 clock_p = &element->clock;
2719 bus_p = &element->bus;
2720 gst_object_replace ((GstObject **) clock_p, NULL);
2721 gst_object_replace ((GstObject **) bus_p, NULL);
2722 GST_OBJECT_UNLOCK (element);
2724 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "parent class dispose");
2726 G_OBJECT_CLASS (parent_class)->dispose (object);
2734 ("\nTrying to dispose element %s, but it is not in the NULL state.\n"
2735 "You need to explicitly set elements to the NULL state before\n"
2736 "dropping the final reference, to allow them to clean up.\n",
2737 GST_OBJECT_NAME (element));
2743 gst_element_finalize (GObject * object)
2745 GstElement *element = GST_ELEMENT (object);
2747 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize");
2749 GST_STATE_LOCK (element);
2750 if (element->state_cond)
2751 g_cond_free (element->state_cond);
2752 element->state_cond = NULL;
2753 GST_STATE_UNLOCK (element);
2754 g_static_rec_mutex_free (element->state_lock);
2755 g_free (element->state_lock);
2756 element->state_lock = NULL;
2758 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize parent");
2760 G_OBJECT_CLASS (parent_class)->finalize (object);
2763 #ifndef GST_DISABLE_LOADSAVE
2765 * gst_element_save_thyself:
2766 * @element: a #GstElement to save.
2767 * @parent: the xml parent node.
2769 * Saves the element as part of the given XML structure.
2771 * Returns: the new #xmlNodePtr.
2774 gst_element_save_thyself (GstObject * object, xmlNodePtr parent)
2777 GstElementClass *oclass;
2778 GParamSpec **specs, *spec;
2781 GValue value = { 0, };
2782 GstElement *element;
2784 g_return_val_if_fail (GST_IS_ELEMENT (object), parent);
2786 element = GST_ELEMENT (object);
2788 oclass = GST_ELEMENT_GET_CLASS (element);
2790 xmlNewChild (parent, NULL, (xmlChar *) "name",
2791 (xmlChar *) GST_ELEMENT_NAME (element));
2793 if (oclass->elementfactory != NULL) {
2794 GstElementFactory *factory = (GstElementFactory *) oclass->elementfactory;
2796 xmlNewChild (parent, NULL, (xmlChar *) "type",
2797 (xmlChar *) GST_PLUGIN_FEATURE (factory)->name);
2801 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &nspecs);
2803 for (i = 0; i < nspecs; i++) {
2805 if (spec->flags & G_PARAM_READABLE) {
2809 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
2811 g_object_get_property (G_OBJECT (element), spec->name, &value);
2812 param = xmlNewChild (parent, NULL, (xmlChar *) "param", NULL);
2813 xmlNewChild (param, NULL, (xmlChar *) "name", (xmlChar *) spec->name);
2815 if (G_IS_PARAM_SPEC_STRING (spec))
2816 contents = g_value_dup_string (&value);
2817 else if (G_IS_PARAM_SPEC_ENUM (spec))
2818 contents = g_strdup_printf ("%d", g_value_get_enum (&value));
2819 else if (G_IS_PARAM_SPEC_INT64 (spec))
2820 contents = g_strdup_printf ("%" G_GINT64_FORMAT,
2821 g_value_get_int64 (&value));
2823 contents = g_strdup_value_contents (&value);
2825 xmlNewChild (param, NULL, (xmlChar *) "value", (xmlChar *) contents);
2828 g_value_unset (&value);
2834 pads = GST_ELEMENT_PADS (element);
2837 GstPad *pad = GST_PAD_CAST (pads->data);
2839 /* figure out if it's a direct pad or a ghostpad */
2840 if (GST_ELEMENT (GST_OBJECT_PARENT (pad)) == element) {
2841 xmlNodePtr padtag = xmlNewChild (parent, NULL, (xmlChar *) "pad", NULL);
2843 gst_object_save_thyself (GST_OBJECT_CAST (pad), padtag);
2845 pads = g_list_next (pads);
2852 gst_element_restore_thyself (GstObject * object, xmlNodePtr self)
2854 xmlNodePtr children;
2855 GstElement *element;
2857 gchar *value = NULL;
2859 element = GST_ELEMENT (object);
2860 g_return_if_fail (element != NULL);
2863 children = self->xmlChildrenNode;
2865 if (!strcmp ((char *) children->name, "param")) {
2866 xmlNodePtr child = children->xmlChildrenNode;
2869 if (!strcmp ((char *) child->name, "name")) {
2870 name = (gchar *) xmlNodeGetContent (child);
2871 } else if (!strcmp ((char *) child->name, "value")) {
2872 value = (gchar *) xmlNodeGetContent (child);
2874 child = child->next;
2876 /* FIXME: can this just be g_object_set ? */
2877 gst_util_set_object_arg (G_OBJECT (element), name, value);
2878 /* g_object_set (G_OBJECT (element), name, value, NULL); */
2882 children = children->next;
2886 children = self->xmlChildrenNode;
2888 if (!strcmp ((char *) children->name, "pad")) {
2889 gst_pad_load_and_link (children, GST_OBJECT_CAST (element));
2891 children = children->next;
2894 if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
2895 (GST_OBJECT_CLASS (parent_class)->restore_thyself) (object, self);
2897 #endif /* GST_DISABLE_LOADSAVE */
2900 gst_element_set_bus_func (GstElement * element, GstBus * bus)
2904 g_return_if_fail (GST_IS_ELEMENT (element));
2906 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element, "setting bus to %p", bus);
2908 GST_OBJECT_LOCK (element);
2909 bus_p = &GST_ELEMENT_BUS (element);
2910 gst_object_replace ((GstObject **) bus_p, GST_OBJECT_CAST (bus));
2911 GST_OBJECT_UNLOCK (element);
2915 * gst_element_set_bus:
2916 * @element: a #GstElement to set the bus of.
2917 * @bus: the #GstBus to set.
2919 * Sets the bus of the element. Increases the refcount on the bus.
2920 * For internal use only, unless you're testing elements.
2925 gst_element_set_bus (GstElement * element, GstBus * bus)
2927 GstElementClass *oclass;
2929 g_return_if_fail (GST_IS_ELEMENT (element));
2931 oclass = GST_ELEMENT_GET_CLASS (element);
2933 if (oclass->set_bus)
2934 oclass->set_bus (element, bus);
2938 * gst_element_get_bus:
2939 * @element: a #GstElement to get the bus of.
2941 * Returns the bus of the element. Note that only a #GstPipeline will provide a
2942 * bus for the application.
2944 * Returns: the element's #GstBus. unref after usage.
2949 gst_element_get_bus (GstElement * element)
2951 GstBus *result = NULL;
2953 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
2955 GST_OBJECT_LOCK (element);
2956 if ((result = GST_ELEMENT_BUS (element)))
2957 gst_object_ref (result);
2958 GST_OBJECT_UNLOCK (element);
2960 GST_DEBUG_OBJECT (element, "got bus %" GST_PTR_FORMAT, result);