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 #ifndef GST_DISABLE_LOADSAVE
136 static xmlNodePtr gst_element_save_thyself (GstObject * object,
138 static void gst_element_restore_thyself (GstObject * parent, xmlNodePtr self);
141 static GstObjectClass *parent_class = NULL;
142 static guint gst_element_signals[LAST_SIGNAL] = { 0 };
145 gst_element_get_type (void)
147 static GType gst_element_type = 0;
149 if (G_UNLIKELY (gst_element_type == 0)) {
150 static const GTypeInfo element_info = {
151 sizeof (GstElementClass),
152 gst_element_base_class_init,
153 gst_element_base_class_finalize,
154 (GClassInitFunc) gst_element_class_init,
159 (GInstanceInitFunc) gst_element_init,
163 gst_element_type = g_type_register_static (GST_TYPE_OBJECT, "GstElement",
164 &element_info, G_TYPE_FLAG_ABSTRACT);
166 return gst_element_type;
170 gst_element_class_init (GstElementClass * klass)
172 GObjectClass *gobject_class;
173 GstObjectClass *gstobject_class;
175 gobject_class = (GObjectClass *) klass;
176 gstobject_class = (GstObjectClass *) klass;
178 parent_class = g_type_class_peek_parent (klass);
181 * GstElement::pad-added:
182 * @gstelement: the object which received the signal
183 * @new_pad: the pad that has been added
185 * a new #GstPad has been added to the element.
187 gst_element_signals[PAD_ADDED] =
188 g_signal_new ("pad-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
189 G_STRUCT_OFFSET (GstElementClass, pad_added), NULL, NULL,
190 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
192 * GstElement::pad-removed:
193 * @gstelement: the object which received the signal
194 * @old_pad: the pad that has been removed
196 * a #GstPad has been removed from the element
198 gst_element_signals[PAD_REMOVED] =
199 g_signal_new ("pad-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
200 G_STRUCT_OFFSET (GstElementClass, pad_removed), NULL, NULL,
201 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
203 * GstElement::no-more-pads:
204 * @gstelement: the object which received the signal
206 * This signals that the element will not generate more dynamic pads.
208 gst_element_signals[NO_MORE_PADS] =
209 g_signal_new ("no-more-pads", G_TYPE_FROM_CLASS (klass),
210 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstElementClass, no_more_pads), NULL,
211 NULL, gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
213 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_element_dispose);
214 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_element_finalize);
216 #ifndef GST_DISABLE_LOADSAVE
217 gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_element_save_thyself);
218 gstobject_class->restore_thyself =
219 GST_DEBUG_FUNCPTR (gst_element_restore_thyself);
222 klass->change_state = GST_DEBUG_FUNCPTR (gst_element_change_state_func);
223 klass->set_state = GST_DEBUG_FUNCPTR (gst_element_set_state_func);
224 klass->get_state = GST_DEBUG_FUNCPTR (gst_element_get_state_func);
225 klass->set_bus = GST_DEBUG_FUNCPTR (gst_element_set_bus_func);
226 klass->query = GST_DEBUG_FUNCPTR (gst_element_default_query);
227 klass->send_event = GST_DEBUG_FUNCPTR (gst_element_default_send_event);
228 klass->numpadtemplates = 0;
230 klass->elementfactory = NULL;
234 gst_element_base_class_init (gpointer g_class)
236 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
238 memset (&element_class->details, 0, sizeof (GstElementDetails));
239 element_class->padtemplates = NULL;
243 gst_element_base_class_finalize (gpointer g_class)
245 GstElementClass *klass = GST_ELEMENT_CLASS (g_class);
247 g_list_foreach (klass->padtemplates, (GFunc) gst_object_unref, NULL);
248 g_list_free (klass->padtemplates);
249 __gst_element_details_clear (&klass->details);
253 gst_element_init (GstElement * element)
255 GST_STATE (element) = GST_STATE_NULL;
256 GST_STATE_TARGET (element) = GST_STATE_NULL;
257 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
258 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
259 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
261 element->state_lock = g_new0 (GStaticRecMutex, 1);
262 g_static_rec_mutex_init (element->state_lock);
263 element->state_cond = g_cond_new ();
267 * gst_element_default_error:
268 * @object: a #GObject that signalled the error.
269 * @orig: the #GstObject that initiated the error.
270 * @error: the GError.
271 * @debug: an additional debug information string, or %NULL.
273 * A default error signal callback to attach to an element.
274 * The user data passed to the g_signal_connect is ignored.
276 * The default handler will simply print the error string using g_print.
281 gst_element_default_error (GObject * object, GstObject * source, GError * error,
284 gchar *name = gst_object_get_path_string (source);
286 g_print (_("ERROR: from element %s: %s\n"), name, error->message);
288 g_print (_("Additional debug info:\n%s\n"), debug);
294 * gst_element_release_request_pad:
295 * @element: a #GstElement to release the request pad of.
296 * @pad: the #GstPad to release.
298 * Makes the element free the previously requested pad as obtained
299 * with gst_element_get_request_pad().
304 gst_element_release_request_pad (GstElement * element, GstPad * pad)
306 GstElementClass *oclass;
308 g_return_if_fail (GST_IS_ELEMENT (element));
309 g_return_if_fail (GST_IS_PAD (pad));
311 oclass = GST_ELEMENT_GET_CLASS (element);
313 /* if the element implements a custom release function we call that, else we
314 * simply remove the pad from the element */
315 if (oclass->release_pad)
316 (oclass->release_pad) (element, pad);
318 gst_element_remove_pad (element, pad);
322 * gst_element_requires_clock:
323 * @element: a #GstElement to query
325 * Query if the element requires a clock.
327 * Returns: %TRUE if the element requires a clock
332 gst_element_requires_clock (GstElement * element)
336 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
338 result = (GST_ELEMENT_GET_CLASS (element)->set_clock != NULL);
344 * gst_element_provides_clock:
345 * @element: a #GstElement to query
347 * Query if the element provides a clock. A #GstClock provided by an
348 * element can be used as the global #GstClock for the pipeline.
349 * An element that can provide a clock is only required to do so in the PAUSED
350 * state, this means when it is fully negotiated and has allocated the resources
351 * to operate the clock.
353 * Returns: %TRUE if the element provides a clock
358 gst_element_provides_clock (GstElement * element)
362 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
364 result = (GST_ELEMENT_GET_CLASS (element)->provide_clock != NULL);
370 * gst_element_provide_clock:
371 * @element: a #GstElement to query
373 * Get the clock provided by the given element.
374 * <note>An element is only required to provide a clock in the PAUSED
375 * state. Some elements can provide a clock in other states.</note>
377 * Returns: the GstClock provided by the element or %NULL
378 * if no clock could be provided. Unref after usage.
383 gst_element_provide_clock (GstElement * element)
385 GstClock *result = NULL;
386 GstElementClass *oclass;
388 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
390 oclass = GST_ELEMENT_GET_CLASS (element);
392 if (oclass->provide_clock)
393 result = oclass->provide_clock (element);
399 * gst_element_set_clock:
400 * @element: a #GstElement to set the clock for.
401 * @clock: the #GstClock to set for the element.
403 * Sets the clock for the element. This function increases the
404 * refcount on the clock. Any previously set clock on the object
407 * Returns: %TRUE if the element accepted the clock. An element can refuse a
408 * clock when it, for example, is not able to slave its internal clock to the
409 * @clock or when it requires a specific clock to operate.
414 gst_element_set_clock (GstElement * element, GstClock * clock)
416 GstElementClass *oclass;
420 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
422 oclass = GST_ELEMENT_GET_CLASS (element);
424 GST_DEBUG_OBJECT (element, "setting clock %p", clock);
426 if (oclass->set_clock)
427 res = oclass->set_clock (element, clock);
430 /* only update the clock pointer if the element accepted the clock */
431 GST_OBJECT_LOCK (element);
432 clock_p = &element->clock;
433 gst_object_replace ((GstObject **) clock_p, (GstObject *) clock);
434 GST_OBJECT_UNLOCK (element);
440 * gst_element_get_clock:
441 * @element: a #GstElement to get the clock of.
443 * Gets the currently configured clock of the element. This is the clock as was
444 * last set with gst_element_set_clock().
446 * Returns: the #GstClock of the element. unref after usage.
451 gst_element_get_clock (GstElement * element)
455 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
457 GST_OBJECT_LOCK (element);
458 if ((result = element->clock))
459 gst_object_ref (result);
460 GST_OBJECT_UNLOCK (element);
466 * gst_element_set_base_time:
467 * @element: a #GstElement.
468 * @time: the base time to set.
470 * Set the base time of an element. See gst_element_get_base_time().
475 gst_element_set_base_time (GstElement * element, GstClockTime time)
477 g_return_if_fail (GST_IS_ELEMENT (element));
479 GST_OBJECT_LOCK (element);
480 element->base_time = time;
481 GST_OBJECT_UNLOCK (element);
483 GST_DEBUG_OBJECT (element, "set base_time=%" GST_TIME_FORMAT,
484 GST_TIME_ARGS (time));
488 * gst_element_get_base_time:
489 * @element: a #GstElement.
491 * Returns the base time of the element. The base time is the
492 * absolute time of the clock when this element was last put to
493 * PLAYING. Subtracting the base time from the clock time gives
494 * the stream time of the element.
496 * Returns: the base time of the element.
501 gst_element_get_base_time (GstElement * element)
505 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
507 GST_OBJECT_LOCK (element);
508 result = element->base_time;
509 GST_OBJECT_UNLOCK (element);
514 #ifndef GST_DISABLE_INDEX
516 * gst_element_is_indexable:
517 * @element: a #GstElement.
519 * Queries if the element can be indexed.
521 * Returns: TRUE if the element can be indexed.
526 gst_element_is_indexable (GstElement * element)
530 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
532 result = (GST_ELEMENT_GET_CLASS (element)->set_index != NULL);
538 * gst_element_set_index:
539 * @element: a #GstElement.
540 * @index: a #GstIndex.
542 * Set @index on the element. The refcount of the index
543 * will be increased, any previously set index is unreffed.
548 gst_element_set_index (GstElement * element, GstIndex * index)
550 GstElementClass *oclass;
552 g_return_if_fail (GST_IS_ELEMENT (element));
553 g_return_if_fail (GST_IS_INDEX (index));
555 oclass = GST_ELEMENT_GET_CLASS (element);
557 if (oclass->set_index)
558 oclass->set_index (element, index);
562 * gst_element_get_index:
563 * @element: a #GstElement.
565 * Gets the index from the element.
567 * Returns: a #GstIndex or %NULL when no index was set on the
568 * element. unref after usage.
573 gst_element_get_index (GstElement * element)
575 GstElementClass *oclass;
576 GstIndex *result = NULL;
578 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
580 oclass = GST_ELEMENT_GET_CLASS (element);
582 if (oclass->get_index)
583 result = oclass->get_index (element);
590 * gst_element_add_pad:
591 * @element: a #GstElement to add the pad to.
592 * @pad: the #GstPad to add to the element.
594 * Adds a pad (link point) to @element. @pad's parent will be set to @element;
595 * see gst_object_set_parent() for refcounting information.
597 * Pads are not automatically activated so elements should perform the needed
598 * steps to activate the pad in case this pad is added in the PAUSED or PLAYING
599 * state. See gst_pad_set_active() for more information about activating pads.
601 * The pad and the element should be unlocked when calling this function.
603 * This function will emit the #GstElement::pad-added signal on the element.
605 * Returns: %TRUE if the pad could be added. This function can fail when
606 * a pad with the same name already existed or the pad already had another
612 gst_element_add_pad (GstElement * element, GstPad * pad)
617 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
618 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
620 /* locking pad to look at the name */
621 GST_OBJECT_LOCK (pad);
622 pad_name = g_strdup (GST_PAD_NAME (pad));
623 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "adding pad '%s'",
624 GST_STR_NULL (pad_name));
625 flushing = GST_PAD_IS_FLUSHING (pad);
626 GST_OBJECT_UNLOCK (pad);
628 /* then check to see if there's already a pad by that name here */
629 GST_OBJECT_LOCK (element);
630 if (G_UNLIKELY (!gst_object_check_uniqueness (element->pads, pad_name)))
633 /* try to set the pad's parent */
634 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (pad),
635 GST_OBJECT_CAST (element))))
638 /* check for flushing pads */
639 if (flushing && (GST_STATE (element) > GST_STATE_READY ||
640 GST_STATE_NEXT (element) == GST_STATE_PAUSED)) {
641 g_warning ("adding flushing pad '%s' to running element '%s'",
642 GST_STR_NULL (pad_name), GST_ELEMENT_NAME (element));
644 GST_OBJECT_LOCK (pad);
645 GST_PAD_UNSET_FLUSHING (pad);
646 GST_OBJECT_UNLOCK (pad);
651 /* add it to the list */
652 switch (gst_pad_get_direction (pad)) {
654 element->srcpads = g_list_prepend (element->srcpads, pad);
655 element->numsrcpads++;
658 element->sinkpads = g_list_prepend (element->sinkpads, pad);
659 element->numsinkpads++;
664 element->pads = g_list_prepend (element->pads, pad);
666 element->pads_cookie++;
667 GST_OBJECT_UNLOCK (element);
669 /* emit the PAD_ADDED signal */
670 g_signal_emit (element, gst_element_signals[PAD_ADDED], 0, pad);
677 g_critical ("Padname %s is not unique in element %s, not adding",
678 pad_name, GST_ELEMENT_NAME (element));
679 GST_OBJECT_UNLOCK (element);
686 ("Pad %s already has parent when trying to add to element %s",
687 pad_name, GST_ELEMENT_NAME (element));
688 GST_OBJECT_UNLOCK (element);
694 GST_OBJECT_LOCK (pad);
696 ("Trying to add pad %s to element %s, but it has no direction",
697 GST_OBJECT_NAME (pad), GST_ELEMENT_NAME (element));
698 GST_OBJECT_UNLOCK (pad);
699 GST_OBJECT_UNLOCK (element);
705 * gst_element_remove_pad:
706 * @element: a #GstElement to remove pad from.
707 * @pad: the #GstPad to remove from the element.
709 * Removes @pad from @element. @pad will be destroyed if it has not been
710 * referenced elsewhere using gst_object_unparent().
712 * This function is used by plugin developers and should not be used
713 * by applications. Pads that were dynamically requested from elements
714 * with gst_element_get_request_pad() should be released with the
715 * gst_element_release_request_pad() function instead.
717 * Pads are not automatically deactivated so elements should perform the needed
718 * steps to deactivate the pad in case this pad is removed in the PAUSED or
719 * PLAYING state. See gst_pad_set_active() for more information about
722 * The pad and the element should be unlocked when calling this function.
724 * This function will emit the #GstElement::pad-removed signal on the element.
726 * Returns: %TRUE if the pad could be removed. Can return %FALSE if the
727 * pad does not belong to the provided element.
732 gst_element_remove_pad (GstElement * element, GstPad * pad)
736 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
737 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
739 /* locking pad to look at the name and parent */
740 GST_OBJECT_LOCK (pad);
741 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "removing pad '%s'",
742 GST_STR_NULL (GST_PAD_NAME (pad)));
744 if (G_UNLIKELY (GST_PAD_PARENT (pad) != element))
746 GST_OBJECT_UNLOCK (pad);
749 if ((peer = gst_pad_get_peer (pad))) {
750 /* window for MT unsafeness, someone else could unlink here
751 * and then we call unlink with wrong pads. The unlink
752 * function would catch this and safely return failed. */
753 if (GST_PAD_IS_SRC (pad))
754 gst_pad_unlink (pad, peer);
756 gst_pad_unlink (peer, pad);
758 gst_object_unref (peer);
761 GST_OBJECT_LOCK (element);
762 /* remove it from the list */
763 switch (gst_pad_get_direction (pad)) {
765 element->srcpads = g_list_remove (element->srcpads, pad);
766 element->numsrcpads--;
769 element->sinkpads = g_list_remove (element->sinkpads, pad);
770 element->numsinkpads--;
773 g_critical ("Removing pad without direction???");
776 element->pads = g_list_remove (element->pads, pad);
778 element->pads_cookie++;
779 GST_OBJECT_UNLOCK (element);
781 /* emit the PAD_REMOVED signal before unparenting and losing the last ref. */
782 g_signal_emit (element, gst_element_signals[PAD_REMOVED], 0, pad);
784 gst_object_unparent (GST_OBJECT_CAST (pad));
791 /* FIXME, locking order? */
792 GST_OBJECT_LOCK (element);
793 g_critical ("Padname %s:%s does not belong to element %s when removing",
794 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
795 GST_OBJECT_UNLOCK (element);
796 GST_OBJECT_UNLOCK (pad);
802 * gst_element_no_more_pads:
803 * @element: a #GstElement
805 * Use this function to signal that the element does not expect any more pads
806 * to show up in the current pipeline. This function should be called whenever
807 * pads have been added by the element itself. Elements with #GST_PAD_SOMETIMES
808 * pad templates use this in combination with autopluggers to figure out that
809 * the element is done initializing its pads.
811 * This function emits the #GstElement::no-more-pads signal.
816 gst_element_no_more_pads (GstElement * element)
818 g_return_if_fail (GST_IS_ELEMENT (element));
820 g_signal_emit (element, gst_element_signals[NO_MORE_PADS], 0);
824 pad_compare_name (GstPad * pad1, const gchar * name)
828 GST_OBJECT_LOCK (pad1);
829 result = strcmp (GST_PAD_NAME (pad1), name);
830 GST_OBJECT_UNLOCK (pad1);
836 * gst_element_get_static_pad:
837 * @element: a #GstElement to find a static pad of.
838 * @name: the name of the static #GstPad to retrieve.
840 * Retrieves a pad from @element by name. This version only retrieves
841 * already-existing (i.e. 'static') pads.
843 * Returns: the requested #GstPad if found, otherwise %NULL. unref after
849 gst_element_get_static_pad (GstElement * element, const gchar * name)
852 GstPad *result = NULL;
854 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
855 g_return_val_if_fail (name != NULL, NULL);
857 GST_OBJECT_LOCK (element);
859 g_list_find_custom (element->pads, name, (GCompareFunc) pad_compare_name);
861 result = GST_PAD_CAST (find->data);
862 gst_object_ref (result);
865 if (result == NULL) {
866 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "no such pad '%s' in element \"%s\"",
867 name, GST_ELEMENT_NAME (element));
869 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
870 GST_ELEMENT_NAME (element), name);
872 GST_OBJECT_UNLOCK (element);
878 gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
881 GstPad *newpad = NULL;
882 GstElementClass *oclass;
884 oclass = GST_ELEMENT_GET_CLASS (element);
886 if (oclass->request_new_pad)
887 newpad = (oclass->request_new_pad) (element, templ, name);
890 gst_object_ref (newpad);
896 * gst_element_get_request_pad:
897 * @element: a #GstElement to find a request pad of.
898 * @name: the name of the request #GstPad to retrieve.
900 * Retrieves a pad from the element by name. This version only retrieves
901 * request pads. The pad should be released with
902 * gst_element_release_request_pad().
904 * Returns: requested #GstPad if found, otherwise %NULL. Release after usage.
907 gst_element_get_request_pad (GstElement * element, const gchar * name)
909 GstPadTemplate *templ = NULL;
911 const gchar *req_name = NULL;
912 gboolean templ_found = FALSE;
916 gchar *str, *endptr = NULL;
917 GstElementClass *class;
919 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
920 g_return_val_if_fail (name != NULL, NULL);
922 class = GST_ELEMENT_GET_CLASS (element);
924 if (strstr (name, "%")) {
925 templ = gst_element_class_get_pad_template (class, name);
930 list = gst_element_class_get_pad_template_list (class);
931 while (!templ_found && list) {
932 templ = (GstPadTemplate *) list->data;
933 if (templ->presence == GST_PAD_REQUEST) {
934 /* Because of sanity checks in gst_pad_template_new(), we know that %s
935 and %d, occurring at the end of the name_template, are the only
937 GST_CAT_DEBUG (GST_CAT_PADS, "comparing %s to %s", name,
938 templ->name_template);
939 if ((str = strchr (templ->name_template, '%'))
940 && strncmp (templ->name_template, name,
941 str - templ->name_template) == 0
942 && strlen (name) > str - templ->name_template) {
943 data = name + (str - templ->name_template);
944 if (*(str + 1) == 'd') {
946 n = (gint) strtol (data, &endptr, 10);
947 if (endptr && *endptr == '\0') {
967 pad = gst_element_request_pad (element, templ, req_name);
973 * gst_element_get_pad:
974 * @element: a #GstElement.
975 * @name: the name of the pad to retrieve.
977 * Retrieves a pad from @element by name. Tries gst_element_get_static_pad()
978 * first, then gst_element_get_request_pad().
980 * <note>Usage of this function is not recommended as it is unclear if the reference
981 * to the result pad should be released with gst_object_unref() in case of a static pad
982 * or gst_element_release_request_pad() in case of a request pad.</note>
984 * Returns: the #GstPad if found, otherwise %NULL. Unref or Release after usage,
985 * depending on the type of the pad.
988 gst_element_get_pad (GstElement * element, const gchar * name)
992 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
993 g_return_val_if_fail (name != NULL, NULL);
995 pad = gst_element_get_static_pad (element, name);
997 pad = gst_element_get_request_pad (element, name);
1002 static GstIteratorItem
1003 iterate_pad (GstIterator * it, GstPad * pad)
1005 gst_object_ref (pad);
1006 return GST_ITERATOR_ITEM_PASS;
1009 static GstIterator *
1010 gst_element_iterate_pad_list (GstElement * element, GList ** padlist)
1012 GstIterator *result;
1014 GST_OBJECT_LOCK (element);
1015 gst_object_ref (element);
1016 result = gst_iterator_new_list (GST_TYPE_PAD,
1017 GST_OBJECT_GET_LOCK (element),
1018 &element->pads_cookie,
1021 (GstIteratorItemFunction) iterate_pad,
1022 (GstIteratorDisposeFunction) gst_object_unref);
1023 GST_OBJECT_UNLOCK (element);
1029 * gst_element_iterate_pads:
1030 * @element: a #GstElement to iterate pads of.
1032 * Retrieves an iterattor of @element's pads. The iterator should
1033 * be freed after usage.
1035 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1040 gst_element_iterate_pads (GstElement * element)
1042 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1044 return gst_element_iterate_pad_list (element, &element->pads);
1048 * gst_element_iterate_src_pads:
1049 * @element: a #GstElement.
1051 * Retrieves an iterator of @element's source pads.
1053 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1058 gst_element_iterate_src_pads (GstElement * element)
1060 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1062 return gst_element_iterate_pad_list (element, &element->srcpads);
1066 * gst_element_iterate_sink_pads:
1067 * @element: a #GstElement.
1069 * Retrieves an iterator of @element's sink pads.
1071 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1076 gst_element_iterate_sink_pads (GstElement * element)
1078 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1080 return gst_element_iterate_pad_list (element, &element->sinkpads);
1084 * gst_element_class_add_pad_template:
1085 * @klass: the #GstElementClass to add the pad template to.
1086 * @templ: a #GstPadTemplate to add to the element class.
1088 * Adds a padtemplate to an element class. This is mainly used in the _base_init
1089 * functions of classes.
1092 gst_element_class_add_pad_template (GstElementClass * klass,
1093 GstPadTemplate * templ)
1095 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1096 g_return_if_fail (GST_IS_PAD_TEMPLATE (templ));
1098 /* avoid registering pad templates with the same name */
1099 g_return_if_fail (gst_element_class_get_pad_template (klass,
1100 templ->name_template) == NULL);
1102 klass->padtemplates = g_list_append (klass->padtemplates,
1103 gst_object_ref (templ));
1104 klass->numpadtemplates++;
1108 * gst_element_class_set_details:
1109 * @klass: class to set details for
1110 * @details: details to set
1112 * Sets the detailed information for a #GstElementClass.
1113 * <note>This function is for use in _base_init functions only.</note>
1115 * The @details are copied.
1118 gst_element_class_set_details (GstElementClass * klass,
1119 const GstElementDetails * details)
1121 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1122 g_return_if_fail (GST_IS_ELEMENT_DETAILS (details));
1124 __gst_element_details_copy (&klass->details, details);
1128 * gst_element_class_get_pad_template_list:
1129 * @element_class: a #GstElementClass to get pad templates of.
1131 * Retrieves a list of the pad templates associated with @element_class. The
1132 * list must not be modified by the calling code.
1133 * <note>If you use this function in the #GInstanceInitFunc of an object class
1134 * that has subclasses, make sure to pass the g_class parameter of the
1135 * #GInstanceInitFunc here.</note>
1137 * Returns: the #GList of padtemplates.
1140 gst_element_class_get_pad_template_list (GstElementClass * element_class)
1142 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1144 return element_class->padtemplates;
1148 * gst_element_class_get_pad_template:
1149 * @element_class: a #GstElementClass to get the pad template of.
1150 * @name: the name of the #GstPadTemplate to get.
1152 * Retrieves a padtemplate from @element_class with the given name.
1153 * <note>If you use this function in the #GInstanceInitFunc of an object class
1154 * that has subclasses, make sure to pass the g_class parameter of the
1155 * #GInstanceInitFunc here.</note>
1157 * Returns: the #GstPadTemplate with the given name, or %NULL if none was found.
1158 * No unreferencing is necessary.
1161 gst_element_class_get_pad_template (GstElementClass * element_class,
1166 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1167 g_return_val_if_fail (name != NULL, NULL);
1169 padlist = gst_element_class_get_pad_template_list (element_class);
1172 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
1174 if (strcmp (padtempl->name_template, name) == 0)
1177 padlist = g_list_next (padlist);
1183 /* get a random pad on element of the given direction.
1184 * The pad is random in a sense that it is the first pad that is (optionaly) linked.
1187 gst_element_get_random_pad (GstElement * element, gboolean need_linked,
1188 GstPadDirection dir)
1190 GstPad *result = NULL;
1193 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "getting a random pad");
1197 GST_OBJECT_LOCK (element);
1198 pads = element->srcpads;
1201 GST_OBJECT_LOCK (element);
1202 pads = element->sinkpads;
1205 goto wrong_direction;
1207 for (; pads; pads = g_list_next (pads)) {
1208 GstPad *pad = GST_PAD_CAST (pads->data);
1210 GST_OBJECT_LOCK (pad);
1211 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "checking pad %s:%s",
1212 GST_DEBUG_PAD_NAME (pad));
1214 if (need_linked && !GST_PAD_IS_LINKED (pad)) {
1215 /* if we require a linked pad, and it is not linked, continue the
1217 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is not linked",
1218 GST_DEBUG_PAD_NAME (pad));
1219 GST_OBJECT_UNLOCK (pad);
1222 /* found a pad, stop search */
1223 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
1224 GST_DEBUG_PAD_NAME (pad));
1225 GST_OBJECT_UNLOCK (pad);
1231 gst_object_ref (result);
1233 GST_OBJECT_UNLOCK (element);
1237 /* ERROR handling */
1240 g_warning ("unknown pad direction %d", dir);
1246 gst_element_default_send_event (GstElement * element, GstEvent * event)
1248 gboolean result = FALSE;
1251 pad = GST_EVENT_IS_DOWNSTREAM (event) ?
1252 gst_element_get_random_pad (element, TRUE, GST_PAD_SRC) :
1253 gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1256 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1257 "pushing %s event to random %s pad %s:%s",
1258 GST_EVENT_TYPE_NAME (event),
1259 (GST_PAD_DIRECTION (pad) == GST_PAD_SRC ? "src" : "sink"),
1260 GST_DEBUG_PAD_NAME (pad));
1262 result = gst_pad_push_event (pad, event);
1263 gst_object_unref (pad);
1265 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "can't send %s event on element %s",
1266 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1272 * gst_element_send_event:
1273 * @element: a #GstElement to send the event to.
1274 * @event: the #GstEvent to send to the element.
1276 * Sends an event to an element. If the element doesn't implement an
1277 * event handler, the event will be pushed on a random linked sink pad for
1278 * upstream events or a random linked source pad for downstream events.
1280 * This function takes owership of the provided event so you should
1281 * gst_event_ref() it if you want to reuse the event after this call.
1283 * Returns: %TRUE if the event was handled.
1288 gst_element_send_event (GstElement * element, GstEvent * event)
1290 GstElementClass *oclass;
1291 gboolean result = FALSE;
1293 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1294 g_return_val_if_fail (event != NULL, FALSE);
1296 oclass = GST_ELEMENT_GET_CLASS (element);
1298 GST_STATE_LOCK (element);
1299 if (oclass->send_event) {
1300 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send %s event on element %s",
1301 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1302 result = oclass->send_event (element, event);
1304 result = gst_element_default_send_event (element, event);
1306 GST_STATE_UNLOCK (element);
1313 * @element: a #GstElement to send the event to.
1314 * @rate: The new playback rate
1315 * @format: The format of the seek values
1316 * @flags: The optional seek flags.
1317 * @cur_type: The type and flags for the new current position
1318 * @cur: The value of the new current position
1319 * @stop_type: The type and flags for the new stop position
1320 * @stop: The value of the new stop position
1322 * Sends a seek event to an element. See gst_event_new_seek() for the details of
1323 * the parameters. The seek event is sent to the element using
1324 * gst_element_send_event().
1326 * Returns: %TRUE if the event was handled.
1331 gst_element_seek (GstElement * element, gdouble rate, GstFormat format,
1332 GstSeekFlags flags, GstSeekType cur_type, gint64 cur,
1333 GstSeekType stop_type, gint64 stop)
1338 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1341 gst_event_new_seek (rate, format, flags, cur_type, cur, stop_type, stop);
1342 result = gst_element_send_event (element, event);
1348 * gst_element_get_query_types:
1349 * @element: a #GstElement to query
1351 * Get an array of query types from the element.
1352 * If the element doesn't implement a query types function,
1353 * the query will be forwarded to the peer of a random linked sink pad.
1355 * Returns: An array of #GstQueryType elements that should not
1356 * be freed or modified.
1360 const GstQueryType *
1361 gst_element_get_query_types (GstElement * element)
1363 GstElementClass *oclass;
1364 const GstQueryType *result = NULL;
1366 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1368 oclass = GST_ELEMENT_GET_CLASS (element);
1370 if (oclass->get_query_types) {
1371 result = oclass->get_query_types (element);
1373 GstPad *pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1376 GstPad *peer = gst_pad_get_peer (pad);
1379 result = gst_pad_get_query_types (peer);
1381 gst_object_unref (peer);
1383 gst_object_unref (pad);
1390 gst_element_default_query (GstElement * element, GstQuery * query)
1392 gboolean result = FALSE;
1395 pad = gst_element_get_random_pad (element, FALSE, GST_PAD_SRC);
1397 result = gst_pad_query (pad, query);
1399 gst_object_unref (pad);
1401 pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1403 GstPad *peer = gst_pad_get_peer (pad);
1406 result = gst_pad_query (peer, query);
1408 gst_object_unref (peer);
1410 gst_object_unref (pad);
1417 * gst_element_query:
1418 * @element: a #GstElement to perform the query on.
1419 * @query: the #GstQuery.
1421 * Performs a query on the given element.
1423 * For elements that don't implement a query handler, this function
1424 * forwards the query to a random srcpad or to the peer of a
1425 * random linked sinkpad of this element.
1427 * Returns: TRUE if the query could be performed.
1432 gst_element_query (GstElement * element, GstQuery * query)
1434 GstElementClass *oclass;
1435 gboolean result = FALSE;
1437 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1438 g_return_val_if_fail (query != NULL, FALSE);
1440 oclass = GST_ELEMENT_GET_CLASS (element);
1442 if (oclass->query) {
1443 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send query on element %s",
1444 GST_ELEMENT_NAME (element));
1445 result = oclass->query (element, query);
1447 result = gst_element_default_query (element, query);
1453 * gst_element_post_message:
1454 * @element: a #GstElement posting the message
1455 * @message: a #GstMessage to post
1457 * Post a message on the element's #GstBus. This function takes ownership of the
1458 * message; if you want to access the message after this call, you should add an
1459 * additional reference before calling.
1461 * Returns: %TRUE if the message was successfully posted. The function returns
1462 * %FALSE if the element did not have a bus.
1467 gst_element_post_message (GstElement * element, GstMessage * message)
1470 gboolean result = FALSE;
1472 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1473 g_return_val_if_fail (message != NULL, FALSE);
1475 GST_OBJECT_LOCK (element);
1478 if (G_UNLIKELY (bus == NULL))
1481 gst_object_ref (bus);
1482 GST_OBJECT_UNLOCK (element);
1484 /* we release the element lock when posting the message so that any
1485 * (synchronous) message handlers can operate on the element */
1486 result = gst_bus_post (bus, message);
1487 gst_object_unref (bus);
1494 GST_DEBUG_OBJECT (element, "not posting message %p: no bus", message);
1495 GST_OBJECT_UNLOCK (element);
1496 gst_message_unref (message);
1502 * _gst_element_error_printf:
1503 * @format: the printf-like format to use, or %NULL
1505 * This function is only used internally by the gst_element_error() macro.
1507 * Returns: a newly allocated string, or %NULL if the format was %NULL or ""
1512 _gst_element_error_printf (const gchar * format, ...)
1522 va_start (args, format);
1523 buffer = g_strdup_vprintf (format, args);
1529 * gst_element_message_full:
1530 * @element: a #GstElement to send message from
1531 * @type: the #GstMessageType
1532 * @domain: the GStreamer GError domain this message belongs to
1533 * @code: the GError code belonging to the domain
1534 * @text: an allocated text string to be used as a replacement for the
1535 * default message connected to code, or %NULL
1536 * @debug: an allocated debug message to be used as a replacement for the
1537 * default debugging information, or %NULL
1538 * @file: the source code file where the error was generated
1539 * @function: the source code function where the error was generated
1540 * @line: the source code line where the error was generated
1542 * Post an error, warning or info message on the bus from inside an element.
1544 * @type must be of #GST_MESSAGE_ERROR, #GST_MESSAGE_WARNING or
1545 * #GST_MESSAGE_INFO.
1549 void gst_element_message_full
1550 (GstElement * element, GstMessageType type,
1551 GQuark domain, gint code, gchar * text,
1552 gchar * debug, const gchar * file, const gchar * function, gint line)
1554 GError *gerror = NULL;
1558 gboolean has_debug = TRUE;
1559 GstMessage *message = NULL;
1562 GST_DEBUG_OBJECT (element, "start");
1563 g_return_if_fail (GST_IS_ELEMENT (element));
1564 g_return_if_fail ((type == GST_MESSAGE_ERROR) ||
1565 (type == GST_MESSAGE_WARNING) || (type == GST_MESSAGE_INFO));
1567 /* check if we send the given text or the default error text */
1568 if ((text == NULL) || (text[0] == 0)) {
1569 /* text could have come from g_strdup_printf (""); */
1571 sent_text = gst_error_get_message (domain, code);
1575 /* construct a sent_debug with extra information from source */
1576 if ((debug == NULL) || (debug[0] == 0)) {
1577 /* debug could have come from g_strdup_printf (""); */
1581 name = gst_object_get_path_string (GST_OBJECT_CAST (element));
1583 sent_debug = g_strdup_printf ("%s(%d): %s (): %s:\n%s",
1584 file, line, function, name, debug);
1586 sent_debug = g_strdup_printf ("%s(%d): %s (): %s",
1587 file, line, function, name);
1591 /* create gerror and post message */
1592 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posting message: %s",
1594 gerror = g_error_new_literal (domain, code, sent_text);
1597 case GST_MESSAGE_ERROR:
1599 gst_message_new_error (GST_OBJECT_CAST (element), gerror, sent_debug);
1601 case GST_MESSAGE_WARNING:
1602 message = gst_message_new_warning (GST_OBJECT_CAST (element), gerror,
1605 case GST_MESSAGE_INFO:
1606 message = gst_message_new_info (GST_OBJECT_CAST (element), gerror,
1610 g_assert_not_reached ();
1613 gst_element_post_message (element, message);
1615 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posted %s message: %s",
1616 (type == GST_MESSAGE_ERROR ? "error" : "warning"), sent_text);
1619 g_error_free (gerror);
1620 g_free (sent_debug);
1625 * gst_element_is_locked_state:
1626 * @element: a #GstElement.
1628 * Checks if the state of an element is locked.
1629 * If the state of an element is locked, state changes of the parent don't
1630 * affect the element.
1631 * This way you can leave currently unused elements inside bins. Just lock their
1632 * state before changing the state from #GST_STATE_NULL.
1636 * Returns: TRUE, if the element's state is locked.
1639 gst_element_is_locked_state (GstElement * element)
1643 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1645 GST_OBJECT_LOCK (element);
1646 result = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1647 GST_OBJECT_UNLOCK (element);
1653 * gst_element_set_locked_state:
1654 * @element: a #GstElement
1655 * @locked_state: TRUE to lock the element's state
1657 * Locks the state of an element, so state changes of the parent don't affect
1658 * this element anymore.
1662 * Returns: TRUE if the state was changed, FALSE if bad parameters were given
1663 * or the elements state-locking needed no change.
1666 gst_element_set_locked_state (GstElement * element, gboolean locked_state)
1670 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1672 GST_OBJECT_LOCK (element);
1673 old = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1675 if (G_UNLIKELY (old == locked_state))
1679 GST_CAT_DEBUG (GST_CAT_STATES, "locking state of element %s",
1680 GST_ELEMENT_NAME (element));
1681 GST_OBJECT_FLAG_SET (element, GST_ELEMENT_LOCKED_STATE);
1683 GST_CAT_DEBUG (GST_CAT_STATES, "unlocking state of element %s",
1684 GST_ELEMENT_NAME (element));
1685 GST_OBJECT_FLAG_UNSET (element, GST_ELEMENT_LOCKED_STATE);
1687 GST_OBJECT_UNLOCK (element);
1693 GST_CAT_DEBUG (GST_CAT_STATES, "elements %s was in locked state %d",
1694 GST_ELEMENT_NAME (element), old);
1695 GST_OBJECT_UNLOCK (element);
1702 * gst_element_sync_state_with_parent:
1703 * @element: a #GstElement.
1705 * Tries to change the state of the element to the same as its parent.
1706 * If this function returns FALSE, the state of element is undefined.
1708 * Returns: TRUE, if the element's state could be synced to the parent's state.
1713 gst_element_sync_state_with_parent (GstElement * element)
1717 GstStateChangeReturn ret;
1719 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1721 if ((parent = GST_ELEMENT_CAST (gst_element_get_parent (element)))) {
1722 GstState parent_current, parent_pending;
1724 GST_OBJECT_LOCK (parent);
1725 parent_current = GST_STATE (parent);
1726 parent_pending = GST_STATE_PENDING (parent);
1727 GST_OBJECT_UNLOCK (parent);
1729 /* set to pending if there is one, else we set it to the current state of
1731 if (parent_pending != GST_STATE_VOID_PENDING)
1732 target = parent_pending;
1734 target = parent_current;
1736 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1737 "syncing state (%s) to parent %s %s (%s, %s)",
1738 gst_element_state_get_name (GST_STATE (element)),
1739 GST_ELEMENT_NAME (parent), gst_element_state_get_name (target),
1740 gst_element_state_get_name (parent_current),
1741 gst_element_state_get_name (parent_pending));
1743 ret = gst_element_set_state (element, target);
1744 if (ret == GST_STATE_CHANGE_FAILURE)
1747 gst_object_unref (parent);
1751 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "element has no parent");
1758 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1759 "syncing state failed (%s)",
1760 gst_element_state_change_return_get_name (ret));
1766 static GstStateChangeReturn
1767 gst_element_get_state_func (GstElement * element,
1768 GstState * state, GstState * pending, GstClockTime timeout)
1770 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
1771 GstState old_pending;
1773 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "getting state, timeout %"
1774 GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
1776 GST_OBJECT_LOCK (element);
1777 ret = GST_STATE_RETURN (element);
1778 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "RETURN is %s",
1779 gst_element_state_change_return_get_name (ret));
1781 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "RETURN is %s",
1782 gst_element_state_change_return_get_name (ret));
1784 /* we got an error, report immediatly */
1785 if (ret == GST_STATE_CHANGE_FAILURE)
1788 /* we got no_preroll, report immediatly */
1789 if (ret == GST_STATE_CHANGE_NO_PREROLL)
1792 /* no need to wait async if we are not async */
1793 if (ret != GST_STATE_CHANGE_ASYNC)
1796 old_pending = GST_STATE_PENDING (element);
1797 if (old_pending != GST_STATE_VOID_PENDING) {
1798 GTimeVal *timeval, abstimeout;
1801 if (timeout != GST_CLOCK_TIME_NONE) {
1802 glong add = timeout / 1000;
1807 /* make timeout absolute */
1808 g_get_current_time (&abstimeout);
1809 g_time_val_add (&abstimeout, add);
1810 timeval = &abstimeout;
1814 /* get cookie to dected state change during waiting */
1815 cookie = element->state_cookie;
1817 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1818 "waiting for element to commit state");
1820 /* we have a pending state change, wait for it to complete */
1821 if (!GST_STATE_TIMED_WAIT (element, timeval)) {
1822 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timed out");
1823 /* timeout triggered */
1824 ret = GST_STATE_CHANGE_ASYNC;
1826 if (cookie != element->state_cookie)
1829 /* could be success or failure */
1830 if (old_pending == GST_STATE (element)) {
1831 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got success");
1832 ret = GST_STATE_CHANGE_SUCCESS;
1834 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got failure");
1835 ret = GST_STATE_CHANGE_FAILURE;
1839 /* if nothing is pending anymore we can return SUCCESS */
1840 if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
1841 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "nothing pending");
1842 ret = GST_STATE_CHANGE_SUCCESS;
1847 *state = GST_STATE (element);
1849 *pending = GST_STATE_PENDING (element);
1851 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1852 "state current: %s, pending: %s, result: %s",
1853 gst_element_state_get_name (GST_STATE (element)),
1854 gst_element_state_get_name (GST_STATE_PENDING (element)),
1855 gst_element_state_change_return_get_name (ret));
1856 GST_OBJECT_UNLOCK (element);
1863 *state = GST_STATE_VOID_PENDING;
1865 *pending = GST_STATE_VOID_PENDING;
1867 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "get_state() interruped");
1869 GST_OBJECT_UNLOCK (element);
1871 return GST_STATE_CHANGE_FAILURE;
1876 * gst_element_get_state:
1877 * @element: a #GstElement to get the state of.
1878 * @state: a pointer to #GstState to hold the state. Can be %NULL.
1879 * @pending: a pointer to #GstState to hold the pending state.
1881 * @timeout: a #GstClockTime to specify the timeout for an async
1882 * state change or %GST_CLOCK_TIME_NONE for infinite timeout.
1884 * Gets the state of the element.
1886 * For elements that performed an ASYNC state change, as reported by
1887 * gst_element_set_state(), this function will block up to the
1888 * specified timeout value for the state change to complete.
1889 * If the element completes the state change or goes into
1890 * an error, this function returns immediately with a return value of
1891 * %GST_STATE_CHANGE_SUCCESS or %GST_STATE_CHANGE_FAILURE respectively.
1893 * For elements that did not return %GST_STATE_CHANGE_ASYNC, this function
1894 * returns the current and pending state immediately.
1896 * This function returns %GST_STATE_CHANGE_NO_PREROLL if the element
1897 * successfully changed its state but is not able to provide data yet.
1898 * This mostly happens for live sources that only produce data in the PLAYING
1899 * state. While the state change return is equivalent to
1900 * %GST_STATE_CHANGE_SUCCESS, it is returned to the application to signal that
1901 * some sink elements might not be able to complete their state change because
1902 * an element is not producing data to complete the preroll. When setting the
1903 * element to playing, the preroll will complete and playback will start.
1905 * Returns: %GST_STATE_CHANGE_SUCCESS if the element has no more pending state
1906 * and the last state change succeeded, %GST_STATE_CHANGE_ASYNC if the
1907 * element is still performing a state change or
1908 * %GST_STATE_CHANGE_FAILURE if the last state change failed.
1912 GstStateChangeReturn
1913 gst_element_get_state (GstElement * element,
1914 GstState * state, GstState * pending, GstClockTime timeout)
1916 GstElementClass *oclass;
1917 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
1919 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
1921 oclass = GST_ELEMENT_GET_CLASS (element);
1923 if (oclass->get_state)
1924 result = (oclass->get_state) (element, state, pending, timeout);
1930 * gst_element_abort_state:
1931 * @element: a #GstElement to abort the state of.
1933 * Abort the state change of the element. This function is used
1934 * by elements that do asynchronous state changes and find out
1935 * something is wrong.
1937 * This function should be called with the STATE_LOCK held.
1942 gst_element_abort_state (GstElement * element)
1946 #ifndef GST_DISABLE_GST_DEBUG
1950 g_return_if_fail (GST_IS_ELEMENT (element));
1952 GST_OBJECT_LOCK (element);
1953 pending = GST_STATE_PENDING (element);
1955 if (pending == GST_STATE_VOID_PENDING ||
1956 GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
1957 goto nothing_aborted;
1959 #ifndef GST_DISABLE_GST_DEBUG
1960 old_state = GST_STATE (element);
1962 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1963 "aborting state from %s to %s", gst_element_state_get_name (old_state),
1964 gst_element_state_get_name (pending));
1968 GST_STATE_RETURN (element) = GST_STATE_CHANGE_FAILURE;
1970 GST_STATE_BROADCAST (element);
1971 GST_OBJECT_UNLOCK (element);
1977 GST_OBJECT_UNLOCK (element);
1983 * gst_element_continue_state:
1984 * @element: a #GstElement to continue the state change of.
1985 * @ret: The previous state return value
1987 * Commit the state change of the element and proceed to the next
1988 * pending state if any. This function is used
1989 * by elements that do asynchronous state changes.
1990 * The core will normally call this method automatically when an
1991 * element returned %GST_STATE_CHANGE_SUCCESS from the state change function.
1993 * If after calling this method the element still has not reached
1994 * the pending state, the next state change is performed.
1996 * This method is used internally and should normally not be called by plugins
1999 * Returns: The result of the commit state change.
2003 GstStateChangeReturn
2004 gst_element_continue_state (GstElement * element, GstStateChangeReturn ret)
2006 GstStateChangeReturn old_ret;
2007 GstState old_state, old_next;
2008 GstState current, next, pending;
2009 GstMessage *message;
2010 GstStateChange transition;
2012 GST_OBJECT_LOCK (element);
2013 old_ret = GST_STATE_RETURN (element);
2014 GST_STATE_RETURN (element) = ret;
2015 pending = GST_STATE_PENDING (element);
2017 /* check if there is something to commit */
2018 if (pending == GST_STATE_VOID_PENDING)
2019 goto nothing_pending;
2021 old_state = GST_STATE (element);
2022 /* this is the state we should go to next */
2023 old_next = GST_STATE_NEXT (element);
2024 /* update current state */
2025 current = GST_STATE (element) = old_next;
2027 /* see if we reached the final state */
2028 if (pending == current)
2031 next = GST_STATE_GET_NEXT (current, pending);
2032 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2034 GST_STATE_NEXT (element) = next;
2036 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2037 GST_OBJECT_UNLOCK (element);
2039 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2040 "committing state from %s to %s, pending %s",
2041 gst_element_state_get_name (old_state),
2042 gst_element_state_get_name (old_next),
2043 gst_element_state_get_name (pending));
2045 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2046 old_state, old_next, pending);
2047 gst_element_post_message (element, message);
2049 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2050 "continue state change %s to %s, final %s",
2051 gst_element_state_get_name (current),
2052 gst_element_state_get_name (next), gst_element_state_get_name (pending));
2054 ret = gst_element_change_state (element, transition);
2060 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "nothing pending");
2061 GST_OBJECT_UNLOCK (element);
2066 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2067 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2069 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2070 "completed state change to %s", gst_element_state_get_name (pending));
2071 GST_OBJECT_UNLOCK (element);
2073 /* don't post silly messages with the same state. This can happen
2074 * when an element state is changed to what it already was. For bins
2075 * this can be the result of a lost state, which we check with the
2076 * previous return value.
2077 * We do signal the cond though as a _get_state() might be blocking
2079 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC) {
2080 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2081 old_state, old_next, GST_STATE_VOID_PENDING);
2082 gst_element_post_message (element, message);
2085 GST_STATE_BROADCAST (element);
2092 * gst_element_lost_state:
2093 * @element: a #GstElement the state is lost of
2095 * Brings the element to the lost state. The current state of the
2096 * element is copied to the pending state so that any call to
2097 * gst_element_get_state() will return %GST_STATE_CHANGE_ASYNC.
2099 * An ASYNC_START message is posted with an indication to distribute a new
2100 * base_time to the element.
2101 * If the element was PLAYING, it will go to PAUSED. The element
2102 * will be restored to its PLAYING state by the parent pipeline when it
2105 * This is mostly used for elements that lost their preroll buffer
2106 * in the %GST_STATE_PAUSED or %GST_STATE_PLAYING state after a flush,
2107 * they will go to their pending state again when a new preroll buffer is
2108 * queued. This function can only be called when the element is currently
2109 * not in error or an async state change.
2111 * This function is used internally and should normally not be called from
2112 * plugins or applications.
2117 gst_element_lost_state (GstElement * element)
2119 GstState old_state, new_state;
2120 GstMessage *message;
2122 g_return_if_fail (GST_IS_ELEMENT (element));
2124 GST_OBJECT_LOCK (element);
2125 if (GST_STATE_PENDING (element) != GST_STATE_VOID_PENDING ||
2126 GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2129 old_state = GST_STATE (element);
2131 /* when we were PLAYING, the new state is PAUSED. We will also not
2132 * automatically go to PLAYING but let the parent bin(s) set us to PLAYING
2133 * when we preroll. */
2134 if (old_state > GST_STATE_PAUSED)
2135 new_state = GST_STATE_PAUSED;
2137 new_state = old_state;
2139 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2140 "lost state of %s to %s", gst_element_state_get_name (old_state),
2141 gst_element_state_get_name (new_state));
2143 GST_STATE (element) = new_state;
2144 GST_STATE_NEXT (element) = new_state;
2145 GST_STATE_PENDING (element) = new_state;
2146 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2147 GST_OBJECT_UNLOCK (element);
2149 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2150 new_state, new_state, new_state);
2151 gst_element_post_message (element, message);
2153 message = gst_message_new_async_start (GST_OBJECT_CAST (element), TRUE);
2154 gst_element_post_message (element, message);
2160 GST_OBJECT_UNLOCK (element);
2166 * gst_element_set_state:
2167 * @element: a #GstElement to change state of.
2168 * @state: the element's new #GstState.
2170 * Sets the state of the element. This function will try to set the
2171 * requested state by going through all the intermediary states and calling
2172 * the class's state change function for each.
2174 * This function can return #GST_STATE_CHANGE_ASYNC, in which case the
2175 * element will perform the remainder of the state change asynchronously in
2177 * An application can use gst_element_get_state() to wait for the completion
2178 * of the state change or it can wait for a state change message on the bus.
2180 * Returns: Result of the state change using #GstStateChangeReturn.
2184 GstStateChangeReturn
2185 gst_element_set_state (GstElement * element, GstState state)
2187 GstElementClass *oclass;
2188 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2190 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2192 oclass = GST_ELEMENT_GET_CLASS (element);
2194 if (oclass->set_state)
2195 result = (oclass->set_state) (element, state);
2201 * default set state function, calculates the next state based
2202 * on current state and calls the change_state function
2204 static GstStateChangeReturn
2205 gst_element_set_state_func (GstElement * element, GstState state)
2207 GstState current, next, old_pending;
2208 GstStateChangeReturn ret;
2209 GstStateChange transition;
2210 GstStateChangeReturn old_ret;
2212 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2214 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "set_state to %s",
2215 gst_element_state_get_name (state));
2217 /* state lock is taken to protect the set_state() and get_state()
2218 * procedures, it does not lock any variables. */
2219 GST_STATE_LOCK (element);
2221 /* now calculate how to get to the new state */
2222 GST_OBJECT_LOCK (element);
2223 old_ret = GST_STATE_RETURN (element);
2224 /* previous state change returned an error, remove all pending
2225 * and next states */
2226 if (old_ret == GST_STATE_CHANGE_FAILURE) {
2227 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2228 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2229 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
2232 current = GST_STATE (element);
2233 next = GST_STATE_NEXT (element);
2234 old_pending = GST_STATE_PENDING (element);
2235 /* increment state cookie so that we can track each state change */
2236 element->state_cookie++;
2238 /* this is the (new) state we should go to. TARGET is the last state we set on
2240 GST_STATE_TARGET (element) = state;
2241 GST_STATE_PENDING (element) = state;
2243 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2244 "current %s, old_pending %s, next %s, old return %s",
2245 gst_element_state_get_name (current),
2246 gst_element_state_get_name (old_pending),
2247 gst_element_state_get_name (next),
2248 gst_element_state_change_return_get_name (old_ret));
2250 /* if the element was busy doing a state change, we just update the
2251 * target state, it'll get to it async then. */
2252 if (old_pending != GST_STATE_VOID_PENDING) {
2253 /* upwards state change will happen ASYNC */
2254 if (old_pending <= state)
2256 /* element is going to this state already */
2257 else if (next == state)
2259 /* element was performing an ASYNC upward state change and
2260 * we request to go downward again. Start from the next pending
2262 else if (next > state
2263 && GST_STATE_RETURN (element) == GST_STATE_CHANGE_ASYNC) {
2267 next = GST_STATE_GET_NEXT (current, state);
2268 /* now we store the next state */
2269 GST_STATE_NEXT (element) = next;
2270 /* mark busy, we need to check that there is actually a state change
2271 * to be done else we could accidentally override SUCCESS/NO_PREROLL and
2272 * the default element change_state function has no way to know what the
2273 * old value was... could consider this a FIXME...*/
2274 if (current != next)
2275 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2277 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2279 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2280 "%s: setting state from %s to %s",
2281 (next != state ? "intermediate" : "final"),
2282 gst_element_state_get_name (current), gst_element_state_get_name (next));
2284 /* now signal any waiters, they will error since the cookie was increased */
2285 GST_STATE_BROADCAST (element);
2287 GST_OBJECT_UNLOCK (element);
2289 ret = gst_element_change_state (element, transition);
2291 GST_STATE_UNLOCK (element);
2293 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "returned %s",
2294 gst_element_state_change_return_get_name (ret));
2300 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2301 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2302 "element was busy with async state change");
2303 GST_OBJECT_UNLOCK (element);
2305 GST_STATE_UNLOCK (element);
2307 return GST_STATE_CHANGE_ASYNC;
2312 * gst_element_change_state:
2313 * @element: a #GstElement
2314 * @transition: the requested transition
2316 * Perform @transition on @element.
2318 * This function must be called with STATE_LOCK held and is mainly used
2321 * Returns: the #GstStateChangeReturn of the state transition.
2323 GstStateChangeReturn
2324 gst_element_change_state (GstElement * element, GstStateChange transition)
2326 GstElementClass *oclass;
2327 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2331 oclass = GST_ELEMENT_GET_CLASS (element);
2333 /* start with the current state. */
2334 current = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2335 next = GST_STATE_TRANSITION_NEXT (transition);
2337 /* call the state change function so it can set the state */
2338 if (oclass->change_state)
2339 ret = (oclass->change_state) (element, transition);
2341 ret = GST_STATE_CHANGE_FAILURE;
2344 case GST_STATE_CHANGE_FAILURE:
2345 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2346 "have FAILURE change_state return");
2347 /* state change failure */
2348 gst_element_abort_state (element);
2350 case GST_STATE_CHANGE_ASYNC:
2354 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2355 "element will change state ASYNC");
2357 target = GST_STATE_TARGET (element);
2359 if (target > GST_STATE_READY)
2362 /* else we just continue the state change downwards */
2363 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2364 "forcing commit state %s <= %s",
2365 gst_element_state_get_name (target),
2366 gst_element_state_get_name (GST_STATE_READY));
2368 ret = gst_element_continue_state (element, GST_STATE_CHANGE_SUCCESS);
2371 case GST_STATE_CHANGE_SUCCESS:
2372 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2373 "element changed state SUCCESS");
2374 /* we can commit the state now which will proceeed to
2376 ret = gst_element_continue_state (element, ret);
2378 case GST_STATE_CHANGE_NO_PREROLL:
2379 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2380 "element changed state NO_PREROLL");
2381 /* we can commit the state now which will proceeed to
2383 ret = gst_element_continue_state (element, ret);
2386 goto invalid_return;
2389 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit state change %d", ret);
2394 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit async state change %d",
2402 GST_OBJECT_LOCK (element);
2403 /* somebody added a GST_STATE_ and forgot to do stuff here ! */
2404 g_critical ("%s: unknown return value %d from a state change function",
2405 GST_ELEMENT_NAME (element), ret);
2407 /* we are in error now */
2408 ret = GST_STATE_CHANGE_FAILURE;
2409 GST_STATE_RETURN (element) = ret;
2410 GST_OBJECT_UNLOCK (element);
2416 /* gst_iterator_fold functions for pads_activate
2417 * Note how we don't stop the iterator when we fail an activation. This is
2418 * probably a FIXME since when one pad activation fails, we don't want to
2419 * continue our state change. */
2421 activate_pads (GstPad * pad, GValue * ret, gboolean * active)
2423 if (!gst_pad_set_active (pad, *active))
2424 g_value_set_boolean (ret, FALSE);
2426 /* unref the object that was reffed for us by _fold */
2427 gst_object_unref (pad);
2431 /* set the caps on the pad to NULL */
2433 clear_caps (GstPad * pad, GValue * ret, gboolean * active)
2435 gst_pad_set_caps (pad, NULL);
2436 gst_object_unref (pad);
2440 /* returns false on error or early cutout (will never happen because the fold
2441 * function always returns TRUE, see FIXME above) of the fold, true if all
2442 * pads in @iter were (de)activated successfully. */
2444 iterator_activate_fold_with_resync (GstIterator * iter,
2445 GstIteratorFoldFunction func, gpointer user_data)
2447 GstIteratorResult ires;
2450 /* no need to unset this later, it's just a boolean */
2451 g_value_init (&ret, G_TYPE_BOOLEAN);
2452 g_value_set_boolean (&ret, TRUE);
2455 ires = gst_iterator_fold (iter, func, &ret, user_data);
2457 case GST_ITERATOR_RESYNC:
2458 /* need to reset the result again */
2459 g_value_set_boolean (&ret, TRUE);
2460 gst_iterator_resync (iter);
2462 case GST_ITERATOR_DONE:
2463 /* all pads iterated, return collected value */
2466 /* iterator returned _ERROR or premature end with _OK,
2467 * mark an error and exit */
2468 g_value_set_boolean (&ret, FALSE);
2473 /* return collected value */
2474 return g_value_get_boolean (&ret);
2477 /* is called with STATE_LOCK
2479 * Pads are activated from source pads to sinkpads.
2482 gst_element_pads_activate (GstElement * element, gboolean active)
2487 GST_DEBUG_OBJECT (element, "pads_activate with active %d", active);
2489 iter = gst_element_iterate_src_pads (element);
2491 iterator_activate_fold_with_resync (iter,
2492 (GstIteratorFoldFunction) activate_pads, &active);
2493 gst_iterator_free (iter);
2494 if (G_UNLIKELY (!res))
2497 iter = gst_element_iterate_sink_pads (element);
2499 iterator_activate_fold_with_resync (iter,
2500 (GstIteratorFoldFunction) activate_pads, &active);
2501 gst_iterator_free (iter);
2502 if (G_UNLIKELY (!res))
2506 /* clear the caps on all pads, this should never fail */
2507 iter = gst_element_iterate_pads (element);
2509 iterator_activate_fold_with_resync (iter,
2510 (GstIteratorFoldFunction) clear_caps, &active);
2511 gst_iterator_free (iter);
2512 if (G_UNLIKELY (!res))
2516 GST_DEBUG_OBJECT (element, "pads_activate successful");
2523 GST_DEBUG_OBJECT (element, "source pads_activate failed");
2528 GST_DEBUG_OBJECT (element, "sink pads_activate failed");
2533 GST_DEBUG_OBJECT (element, "failed to clear caps on pads");
2538 /* is called with STATE_LOCK */
2539 static GstStateChangeReturn
2540 gst_element_change_state_func (GstElement * element, GstStateChange transition)
2542 GstState state, next;
2543 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
2545 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2547 state = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2548 next = GST_STATE_TRANSITION_NEXT (transition);
2550 /* if the element already is in the given state, we just return success */
2551 if (next == GST_STATE_VOID_PENDING || state == next)
2554 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element,
2555 "default handler tries setting state from %s to %s (%04x)",
2556 gst_element_state_get_name (state),
2557 gst_element_state_get_name (next), transition);
2559 switch (transition) {
2560 case GST_STATE_CHANGE_NULL_TO_READY:
2562 case GST_STATE_CHANGE_READY_TO_PAUSED:
2563 if (!gst_element_pads_activate (element, TRUE)) {
2564 result = GST_STATE_CHANGE_FAILURE;
2567 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2569 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2571 case GST_STATE_CHANGE_PAUSED_TO_READY:
2572 case GST_STATE_CHANGE_READY_TO_NULL:
2573 /* deactivate pads in both cases, since they are activated on
2574 ready->paused but the element might not have made it to paused */
2575 if (!gst_element_pads_activate (element, FALSE)) {
2576 result = GST_STATE_CHANGE_FAILURE;
2578 gst_element_set_base_time (element, 0);
2582 /* this will catch real but unhandled state changes;
2583 * can only be caused by:
2584 * - a new state was added
2585 * - somehow the element was asked to jump across an intermediate state
2587 g_warning ("Unhandled state change from %s to %s",
2588 gst_element_state_get_name (state),
2589 gst_element_state_get_name (next));
2596 GST_OBJECT_LOCK (element);
2597 result = GST_STATE_RETURN (element);
2598 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2599 "element is already in the %s state",
2600 gst_element_state_get_name (state));
2601 GST_OBJECT_UNLOCK (element);
2608 * gst_element_get_factory:
2609 * @element: a #GstElement to request the element factory of.
2611 * Retrieves the factory that was used to create this element.
2613 * Returns: the #GstElementFactory used for creating this element.
2614 * no refcounting is needed.
2617 gst_element_get_factory (GstElement * element)
2619 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
2621 return GST_ELEMENT_GET_CLASS (element)->elementfactory;
2625 gst_element_dispose (GObject * object)
2627 GstElement *element = GST_ELEMENT (object);
2631 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "dispose");
2633 if (GST_STATE (element) != GST_STATE_NULL)
2636 GST_DEBUG_OBJECT (element, "removing %d pads", g_list_length (element->pads));
2637 /* first we break all our links with the outside */
2638 while (element->pads && element->pads->data) {
2639 /* don't call _remove_pad with NULL */
2640 gst_element_remove_pad (element, GST_PAD_CAST (element->pads->data));
2642 if (G_UNLIKELY (element->pads != 0)) {
2643 g_critical ("could not remove pads from element %s",
2644 GST_STR_NULL (GST_OBJECT_NAME (object)));
2647 GST_OBJECT_LOCK (element);
2648 clock_p = &element->clock;
2649 bus_p = &element->bus;
2650 gst_object_replace ((GstObject **) clock_p, NULL);
2651 gst_object_replace ((GstObject **) bus_p, NULL);
2652 GST_OBJECT_UNLOCK (element);
2654 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "parent class dispose");
2656 G_OBJECT_CLASS (parent_class)->dispose (object);
2664 ("\nTrying to dispose element %s, but it is not in the NULL state.\n"
2665 "You need to explicitly set elements to the NULL state before\n"
2666 "dropping the final reference, to allow them to clean up.\n",
2667 GST_OBJECT_NAME (element));
2673 gst_element_finalize (GObject * object)
2675 GstElement *element = GST_ELEMENT (object);
2677 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize");
2679 GST_STATE_LOCK (element);
2680 if (element->state_cond)
2681 g_cond_free (element->state_cond);
2682 element->state_cond = NULL;
2683 GST_STATE_UNLOCK (element);
2684 g_static_rec_mutex_free (element->state_lock);
2685 g_free (element->state_lock);
2686 element->state_lock = NULL;
2688 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize parent");
2690 G_OBJECT_CLASS (parent_class)->finalize (object);
2693 #ifndef GST_DISABLE_LOADSAVE
2695 * gst_element_save_thyself:
2696 * @element: a #GstElement to save.
2697 * @parent: the xml parent node.
2699 * Saves the element as part of the given XML structure.
2701 * Returns: the new #xmlNodePtr.
2704 gst_element_save_thyself (GstObject * object, xmlNodePtr parent)
2707 GstElementClass *oclass;
2708 GParamSpec **specs, *spec;
2711 GValue value = { 0, };
2712 GstElement *element;
2714 g_return_val_if_fail (GST_IS_ELEMENT (object), parent);
2716 element = GST_ELEMENT (object);
2718 oclass = GST_ELEMENT_GET_CLASS (element);
2720 xmlNewChild (parent, NULL, (xmlChar *) "name",
2721 (xmlChar *) GST_ELEMENT_NAME (element));
2723 if (oclass->elementfactory != NULL) {
2724 GstElementFactory *factory = (GstElementFactory *) oclass->elementfactory;
2726 xmlNewChild (parent, NULL, (xmlChar *) "type",
2727 (xmlChar *) GST_PLUGIN_FEATURE (factory)->name);
2731 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &nspecs);
2733 for (i = 0; i < nspecs; i++) {
2735 if (spec->flags & G_PARAM_READABLE) {
2739 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
2741 g_object_get_property (G_OBJECT (element), spec->name, &value);
2742 param = xmlNewChild (parent, NULL, (xmlChar *) "param", NULL);
2743 xmlNewChild (param, NULL, (xmlChar *) "name", (xmlChar *) spec->name);
2745 if (G_IS_PARAM_SPEC_STRING (spec))
2746 contents = g_value_dup_string (&value);
2747 else if (G_IS_PARAM_SPEC_ENUM (spec))
2748 contents = g_strdup_printf ("%d", g_value_get_enum (&value));
2749 else if (G_IS_PARAM_SPEC_INT64 (spec))
2750 contents = g_strdup_printf ("%" G_GINT64_FORMAT,
2751 g_value_get_int64 (&value));
2753 contents = g_strdup_value_contents (&value);
2755 xmlNewChild (param, NULL, (xmlChar *) "value", (xmlChar *) contents);
2758 g_value_unset (&value);
2764 pads = GST_ELEMENT_PADS (element);
2767 GstPad *pad = GST_PAD_CAST (pads->data);
2769 /* figure out if it's a direct pad or a ghostpad */
2770 if (GST_ELEMENT (GST_OBJECT_PARENT (pad)) == element) {
2771 xmlNodePtr padtag = xmlNewChild (parent, NULL, (xmlChar *) "pad", NULL);
2773 gst_object_save_thyself (GST_OBJECT_CAST (pad), padtag);
2775 pads = g_list_next (pads);
2782 gst_element_restore_thyself (GstObject * object, xmlNodePtr self)
2784 xmlNodePtr children;
2785 GstElement *element;
2787 gchar *value = NULL;
2789 element = GST_ELEMENT (object);
2790 g_return_if_fail (element != NULL);
2793 children = self->xmlChildrenNode;
2795 if (!strcmp ((char *) children->name, "param")) {
2796 xmlNodePtr child = children->xmlChildrenNode;
2799 if (!strcmp ((char *) child->name, "name")) {
2800 name = (gchar *) xmlNodeGetContent (child);
2801 } else if (!strcmp ((char *) child->name, "value")) {
2802 value = (gchar *) xmlNodeGetContent (child);
2804 child = child->next;
2806 /* FIXME: can this just be g_object_set ? */
2807 gst_util_set_object_arg (G_OBJECT (element), name, value);
2808 /* g_object_set (G_OBJECT (element), name, value, NULL); */
2812 children = children->next;
2816 children = self->xmlChildrenNode;
2818 if (!strcmp ((char *) children->name, "pad")) {
2819 gst_pad_load_and_link (children, GST_OBJECT_CAST (element));
2821 children = children->next;
2824 if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
2825 (GST_OBJECT_CLASS (parent_class)->restore_thyself) (object, self);
2827 #endif /* GST_DISABLE_LOADSAVE */
2830 gst_element_set_bus_func (GstElement * element, GstBus * bus)
2834 g_return_if_fail (GST_IS_ELEMENT (element));
2836 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element, "setting bus to %p", bus);
2838 GST_OBJECT_LOCK (element);
2839 bus_p = &GST_ELEMENT_BUS (element);
2840 gst_object_replace ((GstObject **) bus_p, GST_OBJECT_CAST (bus));
2841 GST_OBJECT_UNLOCK (element);
2845 * gst_element_set_bus:
2846 * @element: a #GstElement to set the bus of.
2847 * @bus: the #GstBus to set.
2849 * Sets the bus of the element. Increases the refcount on the bus.
2850 * For internal use only, unless you're testing elements.
2855 gst_element_set_bus (GstElement * element, GstBus * bus)
2857 GstElementClass *oclass;
2859 g_return_if_fail (GST_IS_ELEMENT (element));
2861 oclass = GST_ELEMENT_GET_CLASS (element);
2863 if (oclass->set_bus)
2864 oclass->set_bus (element, bus);
2868 * gst_element_get_bus:
2869 * @element: a #GstElement to get the bus of.
2871 * Returns the bus of the element.
2873 * Returns: the element's #GstBus. unref after usage.
2878 gst_element_get_bus (GstElement * element)
2880 GstBus *result = NULL;
2882 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
2884 GST_OBJECT_LOCK (element);
2885 if ((result = GST_ELEMENT_BUS (element)))
2886 gst_object_ref (result);
2887 GST_OBJECT_UNLOCK (element);
2889 GST_DEBUG_OBJECT (element, "got bus %" GST_PTR_FORMAT, result);