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 volatile gsize gst_element_type = 0;
153 if (g_once_init_enter (&gst_element_type)) {
155 static const GTypeInfo element_info = {
156 sizeof (GstElementClass),
157 gst_element_base_class_init,
158 gst_element_base_class_finalize,
159 (GClassInitFunc) gst_element_class_init,
164 (GInstanceInitFunc) gst_element_init,
168 _type = g_type_register_static (GST_TYPE_OBJECT, "GstElement",
169 &element_info, G_TYPE_FLAG_ABSTRACT);
170 g_once_init_leave (&gst_element_type, _type);
172 return gst_element_type;
176 gst_element_class_init (GstElementClass * klass)
178 GObjectClass *gobject_class;
179 GstObjectClass *gstobject_class;
181 gobject_class = (GObjectClass *) klass;
182 gstobject_class = (GstObjectClass *) klass;
184 parent_class = g_type_class_peek_parent (klass);
187 * GstElement::pad-added:
188 * @gstelement: the object which received the signal
189 * @new_pad: the pad that has been added
191 * a new #GstPad has been added to the element.
193 gst_element_signals[PAD_ADDED] =
194 g_signal_new ("pad-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
195 G_STRUCT_OFFSET (GstElementClass, pad_added), NULL, NULL,
196 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
198 * GstElement::pad-removed:
199 * @gstelement: the object which received the signal
200 * @old_pad: the pad that has been removed
202 * a #GstPad has been removed from the element
204 gst_element_signals[PAD_REMOVED] =
205 g_signal_new ("pad-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
206 G_STRUCT_OFFSET (GstElementClass, pad_removed), NULL, NULL,
207 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
209 * GstElement::no-more-pads:
210 * @gstelement: the object which received the signal
212 * This signals that the element will not generate more dynamic pads.
214 gst_element_signals[NO_MORE_PADS] =
215 g_signal_new ("no-more-pads", G_TYPE_FROM_CLASS (klass),
216 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstElementClass, no_more_pads), NULL,
217 NULL, gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
219 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_element_dispose);
220 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_element_finalize);
222 #ifndef GST_DISABLE_LOADSAVE
223 gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_element_save_thyself);
224 gstobject_class->restore_thyself =
225 GST_DEBUG_FUNCPTR (gst_element_restore_thyself);
228 klass->change_state = GST_DEBUG_FUNCPTR (gst_element_change_state_func);
229 klass->set_state = GST_DEBUG_FUNCPTR (gst_element_set_state_func);
230 klass->get_state = GST_DEBUG_FUNCPTR (gst_element_get_state_func);
231 klass->set_bus = GST_DEBUG_FUNCPTR (gst_element_set_bus_func);
232 klass->query = GST_DEBUG_FUNCPTR (gst_element_default_query);
233 klass->send_event = GST_DEBUG_FUNCPTR (gst_element_default_send_event);
234 klass->numpadtemplates = 0;
236 klass->elementfactory = NULL;
240 gst_element_base_class_init (gpointer g_class)
242 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
244 /* FIXME 0.11: Copy the element details and instead of clearing the
245 * pad template list copy the list and increase the refcount of
246 * the pad templates by one.
248 * This will make it possible to add pad templates and set element
249 * details in the class_init functions and is the real GObject way
251 * See http://bugzilla.gnome.org/show_bug.cgi?id=491501
253 memset (&element_class->details, 0, sizeof (GstElementDetails));
254 element_class->padtemplates = NULL;
258 gst_element_base_class_finalize (gpointer g_class)
260 GstElementClass *klass = GST_ELEMENT_CLASS (g_class);
262 g_list_foreach (klass->padtemplates, (GFunc) gst_object_unref, NULL);
263 g_list_free (klass->padtemplates);
264 __gst_element_details_clear (&klass->details);
268 gst_element_init (GstElement * element)
270 GST_STATE (element) = GST_STATE_NULL;
271 GST_STATE_TARGET (element) = GST_STATE_NULL;
272 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
273 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
274 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
276 element->state_lock = g_new0 (GStaticRecMutex, 1);
277 g_static_rec_mutex_init (element->state_lock);
278 element->state_cond = g_cond_new ();
282 * gst_element_default_error:
283 * @object: a #GObject that signalled the error.
284 * @orig: the #GstObject that initiated the error.
285 * @error: the GError.
286 * @debug: an additional debug information string, or %NULL.
288 * A default error signal callback to attach to an element.
289 * The user data passed to the g_signal_connect is ignored.
291 * The default handler will simply print the error string using g_print.
296 gst_element_default_error (GObject * object, GstObject * source, GError * error,
299 gchar *name = gst_object_get_path_string (source);
301 g_print (_("ERROR: from element %s: %s\n"), name, error->message);
303 g_print (_("Additional debug info:\n%s\n"), debug);
309 * gst_element_release_request_pad:
310 * @element: a #GstElement to release the request pad of.
311 * @pad: the #GstPad to release.
313 * Makes the element free the previously requested pad as obtained
314 * with gst_element_get_request_pad().
319 gst_element_release_request_pad (GstElement * element, GstPad * pad)
321 GstElementClass *oclass;
323 g_return_if_fail (GST_IS_ELEMENT (element));
324 g_return_if_fail (GST_IS_PAD (pad));
326 oclass = GST_ELEMENT_GET_CLASS (element);
328 /* if the element implements a custom release function we call that, else we
329 * simply remove the pad from the element */
330 if (oclass->release_pad)
331 (oclass->release_pad) (element, pad);
333 gst_element_remove_pad (element, pad);
337 * gst_element_requires_clock:
338 * @element: a #GstElement to query
340 * Query if the element requires a clock.
342 * Returns: %TRUE if the element requires a clock
347 gst_element_requires_clock (GstElement * element)
351 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
353 result = (GST_ELEMENT_GET_CLASS (element)->set_clock != NULL);
359 * gst_element_provides_clock:
360 * @element: a #GstElement to query
362 * Query if the element provides a clock. A #GstClock provided by an
363 * element can be used as the global #GstClock for the pipeline.
364 * An element that can provide a clock is only required to do so in the PAUSED
365 * state, this means when it is fully negotiated and has allocated the resources
366 * to operate the clock.
368 * Returns: %TRUE if the element provides a clock
373 gst_element_provides_clock (GstElement * element)
377 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
379 result = (GST_ELEMENT_GET_CLASS (element)->provide_clock != NULL);
385 * gst_element_provide_clock:
386 * @element: a #GstElement to query
388 * Get the clock provided by the given element.
389 * <note>An element is only required to provide a clock in the PAUSED
390 * state. Some elements can provide a clock in other states.</note>
392 * Returns: the GstClock provided by the element or %NULL
393 * if no clock could be provided. Unref after usage.
398 gst_element_provide_clock (GstElement * element)
400 GstClock *result = NULL;
401 GstElementClass *oclass;
403 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
405 oclass = GST_ELEMENT_GET_CLASS (element);
407 if (oclass->provide_clock)
408 result = oclass->provide_clock (element);
414 * gst_element_set_clock:
415 * @element: a #GstElement to set the clock for.
416 * @clock: the #GstClock to set for the element.
418 * Sets the clock for the element. This function increases the
419 * refcount on the clock. Any previously set clock on the object
422 * Returns: %TRUE if the element accepted the clock. An element can refuse a
423 * clock when it, for example, is not able to slave its internal clock to the
424 * @clock or when it requires a specific clock to operate.
429 gst_element_set_clock (GstElement * element, GstClock * clock)
431 GstElementClass *oclass;
435 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
437 oclass = GST_ELEMENT_GET_CLASS (element);
439 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element, "setting clock %p", clock);
441 if (oclass->set_clock)
442 res = oclass->set_clock (element, clock);
445 /* only update the clock pointer if the element accepted the clock */
446 GST_OBJECT_LOCK (element);
447 clock_p = &element->clock;
448 gst_object_replace ((GstObject **) clock_p, (GstObject *) clock);
449 GST_OBJECT_UNLOCK (element);
455 * gst_element_get_clock:
456 * @element: a #GstElement to get the clock of.
458 * Gets the currently configured clock of the element. This is the clock as was
459 * last set with gst_element_set_clock().
461 * Returns: the #GstClock of the element. unref after usage.
466 gst_element_get_clock (GstElement * element)
470 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
472 GST_OBJECT_LOCK (element);
473 if ((result = element->clock))
474 gst_object_ref (result);
475 GST_OBJECT_UNLOCK (element);
481 * gst_element_set_base_time:
482 * @element: a #GstElement.
483 * @time: the base time to set.
485 * Set the base time of an element. See gst_element_get_base_time().
490 gst_element_set_base_time (GstElement * element, GstClockTime time)
494 g_return_if_fail (GST_IS_ELEMENT (element));
496 GST_OBJECT_LOCK (element);
497 old = element->base_time;
498 element->base_time = time;
499 GST_OBJECT_UNLOCK (element);
501 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element,
502 "set base_time=%" GST_TIME_FORMAT ", old %" GST_TIME_FORMAT,
503 GST_TIME_ARGS (time), GST_TIME_ARGS (old));
507 * gst_element_get_base_time:
508 * @element: a #GstElement.
510 * Returns the base time of the element. The base time is the
511 * absolute time of the clock when this element was last put to
512 * PLAYING. Subtracting the base time from the clock time gives
513 * the stream time of the element.
515 * Returns: the base time of the element.
520 gst_element_get_base_time (GstElement * element)
524 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
526 GST_OBJECT_LOCK (element);
527 result = element->base_time;
528 GST_OBJECT_UNLOCK (element);
534 * gst_element_is_indexable:
535 * @element: a #GstElement.
537 * Queries if the element can be indexed.
539 * Returns: TRUE if the element can be indexed.
544 gst_element_is_indexable (GstElement * element)
548 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
550 result = (GST_ELEMENT_GET_CLASS (element)->set_index != NULL);
556 * gst_element_set_index:
557 * @element: a #GstElement.
558 * @index: a #GstIndex.
560 * Set @index on the element. The refcount of the index
561 * will be increased, any previously set index is unreffed.
566 gst_element_set_index (GstElement * element, GstIndex * index)
568 GstElementClass *oclass;
570 g_return_if_fail (GST_IS_ELEMENT (element));
571 g_return_if_fail (GST_IS_INDEX (index));
573 oclass = GST_ELEMENT_GET_CLASS (element);
575 if (oclass->set_index)
576 oclass->set_index (element, index);
580 * gst_element_get_index:
581 * @element: a #GstElement.
583 * Gets the index from the element.
585 * Returns: a #GstIndex or %NULL when no index was set on the
586 * element. unref after usage.
591 gst_element_get_index (GstElement * element)
593 GstElementClass *oclass;
594 GstIndex *result = NULL;
596 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
598 oclass = GST_ELEMENT_GET_CLASS (element);
600 if (oclass->get_index)
601 result = oclass->get_index (element);
607 * gst_element_add_pad:
608 * @element: a #GstElement to add the pad to.
609 * @pad: the #GstPad to add to the element.
611 * Adds a pad (link point) to @element. @pad's parent will be set to @element;
612 * see gst_object_set_parent() for refcounting information.
614 * Pads are not automatically activated so elements should perform the needed
615 * steps to activate the pad in case this pad is added in the PAUSED or PLAYING
616 * state. See gst_pad_set_active() for more information about activating pads.
618 * The pad and the element should be unlocked when calling this function.
620 * This function will emit the #GstElement::pad-added signal on the element.
622 * Returns: %TRUE if the pad could be added. This function can fail when
623 * a pad with the same name already existed or the pad already had another
629 gst_element_add_pad (GstElement * element, GstPad * pad)
634 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
635 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
637 /* locking pad to look at the name */
638 GST_OBJECT_LOCK (pad);
639 pad_name = g_strdup (GST_PAD_NAME (pad));
640 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "adding pad '%s'",
641 GST_STR_NULL (pad_name));
642 flushing = GST_PAD_IS_FLUSHING (pad);
643 GST_OBJECT_UNLOCK (pad);
645 /* then check to see if there's already a pad by that name here */
646 GST_OBJECT_LOCK (element);
647 if (G_UNLIKELY (!gst_object_check_uniqueness (element->pads, pad_name)))
650 /* try to set the pad's parent */
651 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (pad),
652 GST_OBJECT_CAST (element))))
655 /* check for flushing pads */
656 if (flushing && (GST_STATE (element) > GST_STATE_READY ||
657 GST_STATE_NEXT (element) == GST_STATE_PAUSED)) {
658 g_warning ("adding flushing pad '%s' to running element '%s'",
659 GST_STR_NULL (pad_name), GST_ELEMENT_NAME (element));
661 GST_OBJECT_LOCK (pad);
662 GST_PAD_UNSET_FLUSHING (pad);
663 GST_OBJECT_UNLOCK (pad);
668 /* add it to the list */
669 switch (gst_pad_get_direction (pad)) {
671 element->srcpads = g_list_prepend (element->srcpads, pad);
672 element->numsrcpads++;
675 element->sinkpads = g_list_prepend (element->sinkpads, pad);
676 element->numsinkpads++;
681 element->pads = g_list_prepend (element->pads, pad);
683 element->pads_cookie++;
684 GST_OBJECT_UNLOCK (element);
686 /* emit the PAD_ADDED signal */
687 g_signal_emit (element, gst_element_signals[PAD_ADDED], 0, pad);
694 g_critical ("Padname %s is not unique in element %s, not adding",
695 pad_name, GST_ELEMENT_NAME (element));
696 GST_OBJECT_UNLOCK (element);
703 ("Pad %s already has parent when trying to add to element %s",
704 pad_name, GST_ELEMENT_NAME (element));
705 GST_OBJECT_UNLOCK (element);
711 GST_OBJECT_LOCK (pad);
713 ("Trying to add pad %s to element %s, but it has no direction",
714 GST_OBJECT_NAME (pad), GST_ELEMENT_NAME (element));
715 GST_OBJECT_UNLOCK (pad);
716 GST_OBJECT_UNLOCK (element);
722 * gst_element_remove_pad:
723 * @element: a #GstElement to remove pad from.
724 * @pad: the #GstPad to remove from the element.
726 * Removes @pad from @element. @pad will be destroyed if it has not been
727 * referenced elsewhere using gst_object_unparent().
729 * This function is used by plugin developers and should not be used
730 * by applications. Pads that were dynamically requested from elements
731 * with gst_element_get_request_pad() should be released with the
732 * gst_element_release_request_pad() function instead.
734 * Pads are not automatically deactivated so elements should perform the needed
735 * steps to deactivate the pad in case this pad is removed in the PAUSED or
736 * PLAYING state. See gst_pad_set_active() for more information about
739 * The pad and the element should be unlocked when calling this function.
741 * This function will emit the #GstElement::pad-removed signal on the element.
743 * Returns: %TRUE if the pad could be removed. Can return %FALSE if the
744 * pad does not belong to the provided element.
749 gst_element_remove_pad (GstElement * element, GstPad * pad)
753 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
754 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
756 /* locking pad to look at the name and parent */
757 GST_OBJECT_LOCK (pad);
758 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "removing pad '%s'",
759 GST_STR_NULL (GST_PAD_NAME (pad)));
761 if (G_UNLIKELY (GST_PAD_PARENT (pad) != element))
763 GST_OBJECT_UNLOCK (pad);
766 if ((peer = gst_pad_get_peer (pad))) {
767 /* window for MT unsafeness, someone else could unlink here
768 * and then we call unlink with wrong pads. The unlink
769 * function would catch this and safely return failed. */
770 if (GST_PAD_IS_SRC (pad))
771 gst_pad_unlink (pad, peer);
773 gst_pad_unlink (peer, pad);
775 gst_object_unref (peer);
778 GST_OBJECT_LOCK (element);
779 /* remove it from the list */
780 switch (gst_pad_get_direction (pad)) {
782 element->srcpads = g_list_remove (element->srcpads, pad);
783 element->numsrcpads--;
786 element->sinkpads = g_list_remove (element->sinkpads, pad);
787 element->numsinkpads--;
790 g_critical ("Removing pad without direction???");
793 element->pads = g_list_remove (element->pads, pad);
795 element->pads_cookie++;
796 GST_OBJECT_UNLOCK (element);
798 /* emit the PAD_REMOVED signal before unparenting and losing the last ref. */
799 g_signal_emit (element, gst_element_signals[PAD_REMOVED], 0, pad);
801 gst_object_unparent (GST_OBJECT_CAST (pad));
808 /* FIXME, locking order? */
809 GST_OBJECT_LOCK (element);
810 g_critical ("Padname %s:%s does not belong to element %s when removing",
811 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
812 GST_OBJECT_UNLOCK (element);
813 GST_OBJECT_UNLOCK (pad);
819 * gst_element_no_more_pads:
820 * @element: a #GstElement
822 * Use this function to signal that the element does not expect any more pads
823 * to show up in the current pipeline. This function should be called whenever
824 * pads have been added by the element itself. Elements with #GST_PAD_SOMETIMES
825 * pad templates use this in combination with autopluggers to figure out that
826 * the element is done initializing its pads.
828 * This function emits the #GstElement::no-more-pads signal.
833 gst_element_no_more_pads (GstElement * element)
835 g_return_if_fail (GST_IS_ELEMENT (element));
837 g_signal_emit (element, gst_element_signals[NO_MORE_PADS], 0);
841 pad_compare_name (GstPad * pad1, const gchar * name)
845 GST_OBJECT_LOCK (pad1);
846 result = strcmp (GST_PAD_NAME (pad1), name);
847 GST_OBJECT_UNLOCK (pad1);
853 * gst_element_get_static_pad:
854 * @element: a #GstElement to find a static pad of.
855 * @name: the name of the static #GstPad to retrieve.
857 * Retrieves a pad from @element by name. This version only retrieves
858 * already-existing (i.e. 'static') pads.
860 * Returns: the requested #GstPad if found, otherwise %NULL. unref after
866 gst_element_get_static_pad (GstElement * element, const gchar * name)
869 GstPad *result = NULL;
871 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
872 g_return_val_if_fail (name != NULL, NULL);
874 GST_OBJECT_LOCK (element);
876 g_list_find_custom (element->pads, name, (GCompareFunc) pad_compare_name);
878 result = GST_PAD_CAST (find->data);
879 gst_object_ref (result);
882 if (result == NULL) {
883 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "no such pad '%s' in element \"%s\"",
884 name, GST_ELEMENT_NAME (element));
886 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
887 GST_ELEMENT_NAME (element), name);
889 GST_OBJECT_UNLOCK (element);
895 gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
898 GstPad *newpad = NULL;
899 GstElementClass *oclass;
901 oclass = GST_ELEMENT_GET_CLASS (element);
903 if (oclass->request_new_pad)
904 newpad = (oclass->request_new_pad) (element, templ, name);
907 gst_object_ref (newpad);
913 * gst_element_get_request_pad:
914 * @element: a #GstElement to find a request pad of.
915 * @name: the name of the request #GstPad to retrieve.
917 * Retrieves a pad from the element by name. This version only retrieves
918 * request pads. The pad should be released with
919 * gst_element_release_request_pad().
921 * Returns: requested #GstPad if found, otherwise %NULL. Release after usage.
924 gst_element_get_request_pad (GstElement * element, const gchar * name)
926 GstPadTemplate *templ = NULL;
928 const gchar *req_name = NULL;
929 gboolean templ_found = FALSE;
933 gchar *str, *endptr = NULL;
934 GstElementClass *class;
936 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
937 g_return_val_if_fail (name != NULL, NULL);
939 class = GST_ELEMENT_GET_CLASS (element);
941 /* if the name contains a %, we assume it's the complete template name. Get
942 * the template and try to get a pad */
943 if (strstr (name, "%")) {
944 templ = gst_element_class_get_request_pad_template (class, name);
949 /* there is no % in the name, try to find a matching template */
950 list = gst_element_class_get_pad_template_list (class);
951 while (!templ_found && list) {
952 templ = (GstPadTemplate *) list->data;
953 if (templ->presence == GST_PAD_REQUEST) {
954 GST_CAT_DEBUG (GST_CAT_PADS, "comparing %s to %s", name,
955 templ->name_template);
956 /* see if we find an exact match */
957 if (strcmp (name, templ->name_template) == 0) {
962 /* Because of sanity checks in gst_pad_template_new(), we know that %s
963 and %d, occurring at the end of the name_template, are the only
965 else if ((str = strchr (templ->name_template, '%'))
966 && strncmp (templ->name_template, name,
967 str - templ->name_template) == 0
968 && strlen (name) > str - templ->name_template) {
969 data = name + (str - templ->name_template);
970 if (*(str + 1) == 'd') {
972 n = (gint) strtol (data, &endptr, 10);
973 if (endptr && *endptr == '\0') {
993 pad = gst_element_request_pad (element, templ, req_name);
999 * gst_element_get_pad:
1000 * @element: a #GstElement.
1001 * @name: the name of the pad to retrieve.
1003 * Retrieves a pad from @element by name. Tries gst_element_get_static_pad()
1004 * first, then gst_element_get_request_pad().
1006 * Deprecated: This function is deprecated as it's unclear if the reference
1007 * to the result pad should be released with gst_object_unref() in case of a static pad
1008 * or gst_element_release_request_pad() in case of a request pad.
1009 * Use gst_element_get_static_pad() or gst_element_get_request_pad() instead.
1011 * Returns: the #GstPad if found, otherwise %NULL. Unref or Release after usage,
1012 * depending on the type of the pad.
1014 #ifndef GST_REMOVE_DEPRECATED
1016 gst_element_get_pad (GstElement * element, const gchar * name)
1020 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1021 g_return_val_if_fail (name != NULL, NULL);
1023 pad = gst_element_get_static_pad (element, name);
1025 pad = gst_element_get_request_pad (element, name);
1029 #endif /* GST_REMOVE_DEPRECATED */
1031 static GstIteratorItem
1032 iterate_pad (GstIterator * it, GstPad * pad)
1034 gst_object_ref (pad);
1035 return GST_ITERATOR_ITEM_PASS;
1038 static GstIterator *
1039 gst_element_iterate_pad_list (GstElement * element, GList ** padlist)
1041 GstIterator *result;
1043 GST_OBJECT_LOCK (element);
1044 gst_object_ref (element);
1045 result = gst_iterator_new_list (GST_TYPE_PAD,
1046 GST_OBJECT_GET_LOCK (element),
1047 &element->pads_cookie,
1050 (GstIteratorItemFunction) iterate_pad,
1051 (GstIteratorDisposeFunction) gst_object_unref);
1052 GST_OBJECT_UNLOCK (element);
1058 * gst_element_iterate_pads:
1059 * @element: a #GstElement to iterate pads of.
1061 * Retrieves an iterattor of @element's pads. The iterator should
1062 * be freed after usage.
1064 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1069 gst_element_iterate_pads (GstElement * element)
1071 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1073 return gst_element_iterate_pad_list (element, &element->pads);
1077 * gst_element_iterate_src_pads:
1078 * @element: a #GstElement.
1080 * Retrieves an iterator of @element's source pads.
1082 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1087 gst_element_iterate_src_pads (GstElement * element)
1089 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1091 return gst_element_iterate_pad_list (element, &element->srcpads);
1095 * gst_element_iterate_sink_pads:
1096 * @element: a #GstElement.
1098 * Retrieves an iterator of @element's sink pads.
1100 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1105 gst_element_iterate_sink_pads (GstElement * element)
1107 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1109 return gst_element_iterate_pad_list (element, &element->sinkpads);
1113 * gst_element_class_add_pad_template:
1114 * @klass: the #GstElementClass to add the pad template to.
1115 * @templ: a #GstPadTemplate to add to the element class.
1117 * Adds a padtemplate to an element class. This is mainly used in the _base_init
1118 * functions of classes.
1121 gst_element_class_add_pad_template (GstElementClass * klass,
1122 GstPadTemplate * templ)
1124 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1125 g_return_if_fail (GST_IS_PAD_TEMPLATE (templ));
1127 /* FIXME 0.11: allow replacing the pad templates by
1128 * calling this with the same name as an already existing pad
1129 * template. For this we _must_ _not_ ref the added pad template
1130 * a second time and _must_ document that this function takes
1131 * ownership of the pad template. Otherwise we will leak pad templates
1132 * or the caller unref's the pad template and it disappears */
1133 /* avoid registering pad templates with the same name */
1134 g_return_if_fail (gst_element_class_get_pad_template (klass,
1135 templ->name_template) == NULL);
1137 klass->padtemplates = g_list_append (klass->padtemplates,
1138 gst_object_ref (templ));
1139 klass->numpadtemplates++;
1143 * gst_element_class_set_details:
1144 * @klass: class to set details for
1145 * @details: details to set
1147 * Sets the detailed information for a #GstElementClass.
1148 * <note>This function is for use in _base_init functions only.</note>
1150 * The @details are copied.
1153 gst_element_class_set_details (GstElementClass * klass,
1154 const GstElementDetails * details)
1156 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1157 g_return_if_fail (GST_IS_ELEMENT_DETAILS (details));
1159 __gst_element_details_copy (&klass->details, details);
1163 * gst_element_class_set_details_simple:
1164 * @klass: class to set details for
1165 * @longname: The long English name of the element. E.g. "File Sink"
1166 * @classification: String describing the type of element, as an unordered list
1167 * separated with slashes ('/'). See draft-klass.txt of the design docs
1168 * for more details and common types. E.g: "Sink/File"
1169 * @description: Sentence describing the purpose of the element.
1170 * E.g: "Write stream to a file"
1171 * @author: Name and contact details of the author(s). Use \n to separate
1172 * multiple author details. E.g: "Joe Bloggs <joe.blogs at foo.com>"
1174 * Sets the detailed information for a #GstElementClass. Simpler version of
1175 * gst_element_class_set_details() that generates less linker overhead.
1176 * <note>This function is for use in _base_init functions only.</note>
1178 * The detail parameter strings are copied into the #GstElementDetails for
1179 * the element class.
1184 gst_element_class_set_details_simple (GstElementClass * klass,
1185 const gchar * longname, const gchar * classification,
1186 const gchar * description, const gchar * author)
1188 const GstElementDetails details =
1189 GST_ELEMENT_DETAILS ((gchar *) longname, (gchar *) classification,
1190 (gchar *) description, (gchar *) author);
1192 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1194 __gst_element_details_copy (&klass->details, &details);
1198 * gst_element_class_get_pad_template_list:
1199 * @element_class: a #GstElementClass to get pad templates of.
1201 * Retrieves a list of the pad templates associated with @element_class. The
1202 * list must not be modified by the calling code.
1203 * <note>If you use this function in the #GInstanceInitFunc of an object class
1204 * that has subclasses, make sure to pass the g_class parameter of the
1205 * #GInstanceInitFunc here.</note>
1207 * Returns: the #GList of padtemplates.
1210 gst_element_class_get_pad_template_list (GstElementClass * element_class)
1212 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1214 return element_class->padtemplates;
1218 * gst_element_class_get_pad_template:
1219 * @element_class: a #GstElementClass to get the pad template of.
1220 * @name: the name of the #GstPadTemplate to get.
1222 * Retrieves a padtemplate from @element_class with the given name.
1223 * <note>If you use this function in the #GInstanceInitFunc of an object class
1224 * that has subclasses, make sure to pass the g_class parameter of the
1225 * #GInstanceInitFunc here.</note>
1227 * Returns: the #GstPadTemplate with the given name, or %NULL if none was found.
1228 * No unreferencing is necessary.
1231 gst_element_class_get_pad_template (GstElementClass * element_class,
1236 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1237 g_return_val_if_fail (name != NULL, NULL);
1239 padlist = gst_element_class_get_pad_template_list (element_class);
1242 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
1244 if (strcmp (padtempl->name_template, name) == 0)
1247 padlist = g_list_next (padlist);
1253 static GstPadTemplate *
1254 gst_element_class_get_request_pad_template (GstElementClass * element_class,
1257 GstPadTemplate *tmpl;
1259 tmpl = gst_element_class_get_pad_template (element_class, name);
1260 if (tmpl != NULL && tmpl->presence == GST_PAD_REQUEST)
1266 /* get a random pad on element of the given direction.
1267 * The pad is random in a sense that it is the first pad that is (optionaly) linked.
1270 gst_element_get_random_pad (GstElement * element, gboolean need_linked,
1271 GstPadDirection dir)
1273 GstPad *result = NULL;
1276 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "getting a random pad");
1280 GST_OBJECT_LOCK (element);
1281 pads = element->srcpads;
1284 GST_OBJECT_LOCK (element);
1285 pads = element->sinkpads;
1288 goto wrong_direction;
1290 for (; pads; pads = g_list_next (pads)) {
1291 GstPad *pad = GST_PAD_CAST (pads->data);
1293 GST_OBJECT_LOCK (pad);
1294 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "checking pad %s:%s",
1295 GST_DEBUG_PAD_NAME (pad));
1297 if (need_linked && !GST_PAD_IS_LINKED (pad)) {
1298 /* if we require a linked pad, and it is not linked, continue the
1300 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is not linked",
1301 GST_DEBUG_PAD_NAME (pad));
1302 GST_OBJECT_UNLOCK (pad);
1305 /* found a pad, stop search */
1306 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
1307 GST_DEBUG_PAD_NAME (pad));
1308 GST_OBJECT_UNLOCK (pad);
1314 gst_object_ref (result);
1316 GST_OBJECT_UNLOCK (element);
1320 /* ERROR handling */
1323 g_warning ("unknown pad direction %d", dir);
1329 gst_element_default_send_event (GstElement * element, GstEvent * event)
1331 gboolean result = FALSE;
1334 pad = GST_EVENT_IS_DOWNSTREAM (event) ?
1335 gst_element_get_random_pad (element, TRUE, GST_PAD_SRC) :
1336 gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1339 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1340 "pushing %s event to random %s pad %s:%s",
1341 GST_EVENT_TYPE_NAME (event),
1342 (GST_PAD_DIRECTION (pad) == GST_PAD_SRC ? "src" : "sink"),
1343 GST_DEBUG_PAD_NAME (pad));
1345 result = gst_pad_push_event (pad, event);
1346 gst_object_unref (pad);
1348 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "can't send %s event on element %s",
1349 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1355 * gst_element_send_event:
1356 * @element: a #GstElement to send the event to.
1357 * @event: the #GstEvent to send to the element.
1359 * Sends an event to an element. If the element doesn't implement an
1360 * event handler, the event will be pushed on a random linked sink pad for
1361 * upstream events or a random linked source pad for downstream events.
1363 * This function takes owership of the provided event so you should
1364 * gst_event_ref() it if you want to reuse the event after this call.
1366 * Returns: %TRUE if the event was handled.
1371 gst_element_send_event (GstElement * element, GstEvent * event)
1373 GstElementClass *oclass;
1374 gboolean result = FALSE;
1376 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1377 g_return_val_if_fail (event != NULL, FALSE);
1379 oclass = GST_ELEMENT_GET_CLASS (element);
1381 GST_STATE_LOCK (element);
1382 if (oclass->send_event) {
1383 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send %s event on element %s",
1384 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1385 result = oclass->send_event (element, event);
1387 result = gst_element_default_send_event (element, event);
1389 GST_STATE_UNLOCK (element);
1396 * @element: a #GstElement to send the event to.
1397 * @rate: The new playback rate
1398 * @format: The format of the seek values
1399 * @flags: The optional seek flags.
1400 * @cur_type: The type and flags for the new current position
1401 * @cur: The value of the new current position
1402 * @stop_type: The type and flags for the new stop position
1403 * @stop: The value of the new stop position
1405 * Sends a seek event to an element. See gst_event_new_seek() for the details of
1406 * the parameters. The seek event is sent to the element using
1407 * gst_element_send_event().
1409 * Returns: %TRUE if the event was handled.
1414 gst_element_seek (GstElement * element, gdouble rate, GstFormat format,
1415 GstSeekFlags flags, GstSeekType cur_type, gint64 cur,
1416 GstSeekType stop_type, gint64 stop)
1421 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1424 gst_event_new_seek (rate, format, flags, cur_type, cur, stop_type, stop);
1425 result = gst_element_send_event (element, event);
1431 * gst_element_get_query_types:
1432 * @element: a #GstElement to query
1434 * Get an array of query types from the element.
1435 * If the element doesn't implement a query types function,
1436 * the query will be forwarded to the peer of a random linked sink pad.
1438 * Returns: An array of #GstQueryType elements that should not
1439 * be freed or modified.
1443 const GstQueryType *
1444 gst_element_get_query_types (GstElement * element)
1446 GstElementClass *oclass;
1447 const GstQueryType *result = NULL;
1449 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1451 oclass = GST_ELEMENT_GET_CLASS (element);
1453 if (oclass->get_query_types) {
1454 result = oclass->get_query_types (element);
1456 GstPad *pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1459 GstPad *peer = gst_pad_get_peer (pad);
1462 result = gst_pad_get_query_types (peer);
1464 gst_object_unref (peer);
1466 gst_object_unref (pad);
1473 gst_element_default_query (GstElement * element, GstQuery * query)
1475 gboolean result = FALSE;
1478 pad = gst_element_get_random_pad (element, FALSE, GST_PAD_SRC);
1480 result = gst_pad_query (pad, query);
1482 gst_object_unref (pad);
1484 pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1486 GstPad *peer = gst_pad_get_peer (pad);
1489 result = gst_pad_query (peer, query);
1491 gst_object_unref (peer);
1493 gst_object_unref (pad);
1500 * gst_element_query:
1501 * @element: a #GstElement to perform the query on.
1502 * @query: the #GstQuery.
1504 * Performs a query on the given element.
1506 * For elements that don't implement a query handler, this function
1507 * forwards the query to a random srcpad or to the peer of a
1508 * random linked sinkpad of this element.
1510 * Returns: TRUE if the query could be performed.
1515 gst_element_query (GstElement * element, GstQuery * query)
1517 GstElementClass *oclass;
1518 gboolean result = FALSE;
1520 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1521 g_return_val_if_fail (query != NULL, FALSE);
1523 oclass = GST_ELEMENT_GET_CLASS (element);
1525 if (oclass->query) {
1526 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send query on element %s",
1527 GST_ELEMENT_NAME (element));
1528 result = oclass->query (element, query);
1530 result = gst_element_default_query (element, query);
1536 * gst_element_post_message:
1537 * @element: a #GstElement posting the message
1538 * @message: a #GstMessage to post
1540 * Post a message on the element's #GstBus. This function takes ownership of the
1541 * message; if you want to access the message after this call, you should add an
1542 * additional reference before calling.
1544 * Returns: %TRUE if the message was successfully posted. The function returns
1545 * %FALSE if the element did not have a bus.
1550 gst_element_post_message (GstElement * element, GstMessage * message)
1553 gboolean result = FALSE;
1555 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1556 g_return_val_if_fail (message != NULL, FALSE);
1558 GST_OBJECT_LOCK (element);
1561 if (G_UNLIKELY (bus == NULL))
1564 gst_object_ref (bus);
1565 GST_OBJECT_UNLOCK (element);
1567 /* we release the element lock when posting the message so that any
1568 * (synchronous) message handlers can operate on the element */
1569 result = gst_bus_post (bus, message);
1570 gst_object_unref (bus);
1577 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, element,
1578 "not posting message %p: no bus", message);
1579 GST_OBJECT_UNLOCK (element);
1580 gst_message_unref (message);
1586 * _gst_element_error_printf:
1587 * @format: the printf-like format to use, or %NULL
1589 * This function is only used internally by the gst_element_error() macro.
1591 * Returns: a newly allocated string, or %NULL if the format was %NULL or ""
1596 _gst_element_error_printf (const gchar * format, ...)
1606 va_start (args, format);
1607 buffer = g_strdup_vprintf (format, args);
1613 * gst_element_message_full:
1614 * @element: a #GstElement to send message from
1615 * @type: the #GstMessageType
1616 * @domain: the GStreamer GError domain this message belongs to
1617 * @code: the GError code belonging to the domain
1618 * @text: an allocated text string to be used as a replacement for the
1619 * default message connected to code, or %NULL
1620 * @debug: an allocated debug message to be used as a replacement for the
1621 * default debugging information, or %NULL
1622 * @file: the source code file where the error was generated
1623 * @function: the source code function where the error was generated
1624 * @line: the source code line where the error was generated
1626 * Post an error, warning or info message on the bus from inside an element.
1628 * @type must be of #GST_MESSAGE_ERROR, #GST_MESSAGE_WARNING or
1629 * #GST_MESSAGE_INFO.
1633 void gst_element_message_full
1634 (GstElement * element, GstMessageType type,
1635 GQuark domain, gint code, gchar * text,
1636 gchar * debug, const gchar * file, const gchar * function, gint line)
1638 GError *gerror = NULL;
1642 gboolean has_debug = TRUE;
1643 GstMessage *message = NULL;
1646 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, element, "start");
1647 g_return_if_fail (GST_IS_ELEMENT (element));
1648 g_return_if_fail ((type == GST_MESSAGE_ERROR) ||
1649 (type == GST_MESSAGE_WARNING) || (type == GST_MESSAGE_INFO));
1651 /* check if we send the given text or the default error text */
1652 if ((text == NULL) || (text[0] == 0)) {
1653 /* text could have come from g_strdup_printf (""); */
1655 sent_text = gst_error_get_message (domain, code);
1659 /* construct a sent_debug with extra information from source */
1660 if ((debug == NULL) || (debug[0] == 0)) {
1661 /* debug could have come from g_strdup_printf (""); */
1665 name = gst_object_get_path_string (GST_OBJECT_CAST (element));
1667 sent_debug = g_strdup_printf ("%s(%d): %s (): %s:\n%s",
1668 file, line, function, name, debug);
1670 sent_debug = g_strdup_printf ("%s(%d): %s (): %s",
1671 file, line, function, name);
1675 /* create gerror and post message */
1676 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posting message: %s",
1678 gerror = g_error_new_literal (domain, code, sent_text);
1681 case GST_MESSAGE_ERROR:
1683 gst_message_new_error (GST_OBJECT_CAST (element), gerror, sent_debug);
1685 case GST_MESSAGE_WARNING:
1686 message = gst_message_new_warning (GST_OBJECT_CAST (element), gerror,
1689 case GST_MESSAGE_INFO:
1690 message = gst_message_new_info (GST_OBJECT_CAST (element), gerror,
1694 g_assert_not_reached ();
1697 gst_element_post_message (element, message);
1699 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posted %s message: %s",
1700 (type == GST_MESSAGE_ERROR ? "error" : "warning"), sent_text);
1703 g_error_free (gerror);
1704 g_free (sent_debug);
1709 * gst_element_is_locked_state:
1710 * @element: a #GstElement.
1712 * Checks if the state of an element is locked.
1713 * If the state of an element is locked, state changes of the parent don't
1714 * affect the element.
1715 * This way you can leave currently unused elements inside bins. Just lock their
1716 * state before changing the state from #GST_STATE_NULL.
1720 * Returns: TRUE, if the element's state is locked.
1723 gst_element_is_locked_state (GstElement * element)
1727 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1729 GST_OBJECT_LOCK (element);
1730 result = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1731 GST_OBJECT_UNLOCK (element);
1737 * gst_element_set_locked_state:
1738 * @element: a #GstElement
1739 * @locked_state: TRUE to lock the element's state
1741 * Locks the state of an element, so state changes of the parent don't affect
1742 * this element anymore.
1746 * Returns: TRUE if the state was changed, FALSE if bad parameters were given
1747 * or the elements state-locking needed no change.
1750 gst_element_set_locked_state (GstElement * element, gboolean locked_state)
1754 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1756 GST_OBJECT_LOCK (element);
1757 old = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1759 if (G_UNLIKELY (old == locked_state))
1763 GST_CAT_DEBUG (GST_CAT_STATES, "locking state of element %s",
1764 GST_ELEMENT_NAME (element));
1765 GST_OBJECT_FLAG_SET (element, GST_ELEMENT_LOCKED_STATE);
1767 GST_CAT_DEBUG (GST_CAT_STATES, "unlocking state of element %s",
1768 GST_ELEMENT_NAME (element));
1769 GST_OBJECT_FLAG_UNSET (element, GST_ELEMENT_LOCKED_STATE);
1771 GST_OBJECT_UNLOCK (element);
1777 GST_CAT_DEBUG (GST_CAT_STATES, "elements %s was already in locked state %d",
1778 GST_ELEMENT_NAME (element), old);
1779 GST_OBJECT_UNLOCK (element);
1786 * gst_element_sync_state_with_parent:
1787 * @element: a #GstElement.
1789 * Tries to change the state of the element to the same as its parent.
1790 * If this function returns FALSE, the state of element is undefined.
1792 * Returns: TRUE, if the element's state could be synced to the parent's state.
1797 gst_element_sync_state_with_parent (GstElement * element)
1801 GstStateChangeReturn ret;
1803 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1805 if ((parent = GST_ELEMENT_CAST (gst_element_get_parent (element)))) {
1806 GstState parent_current, parent_pending;
1808 GST_OBJECT_LOCK (parent);
1809 parent_current = GST_STATE (parent);
1810 parent_pending = GST_STATE_PENDING (parent);
1811 GST_OBJECT_UNLOCK (parent);
1813 /* set to pending if there is one, else we set it to the current state of
1815 if (parent_pending != GST_STATE_VOID_PENDING)
1816 target = parent_pending;
1818 target = parent_current;
1820 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1821 "syncing state (%s) to parent %s %s (%s, %s)",
1822 gst_element_state_get_name (GST_STATE (element)),
1823 GST_ELEMENT_NAME (parent), gst_element_state_get_name (target),
1824 gst_element_state_get_name (parent_current),
1825 gst_element_state_get_name (parent_pending));
1827 ret = gst_element_set_state (element, target);
1828 if (ret == GST_STATE_CHANGE_FAILURE)
1831 gst_object_unref (parent);
1835 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "element has no parent");
1842 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1843 "syncing state failed (%s)",
1844 gst_element_state_change_return_get_name (ret));
1845 gst_object_unref (parent);
1851 static GstStateChangeReturn
1852 gst_element_get_state_func (GstElement * element,
1853 GstState * state, GstState * pending, GstClockTime timeout)
1855 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
1856 GstState old_pending;
1858 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "getting state, timeout %"
1859 GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
1861 GST_OBJECT_LOCK (element);
1862 ret = GST_STATE_RETURN (element);
1863 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "RETURN is %s",
1864 gst_element_state_change_return_get_name (ret));
1866 /* we got an error, report immediatly */
1867 if (ret == GST_STATE_CHANGE_FAILURE)
1870 /* we got no_preroll, report immediatly */
1871 if (ret == GST_STATE_CHANGE_NO_PREROLL)
1874 /* no need to wait async if we are not async */
1875 if (ret != GST_STATE_CHANGE_ASYNC)
1878 old_pending = GST_STATE_PENDING (element);
1879 if (old_pending != GST_STATE_VOID_PENDING) {
1880 GTimeVal *timeval, abstimeout;
1883 if (timeout != GST_CLOCK_TIME_NONE) {
1884 glong add = timeout / 1000;
1889 /* make timeout absolute */
1890 g_get_current_time (&abstimeout);
1891 g_time_val_add (&abstimeout, add);
1892 timeval = &abstimeout;
1896 /* get cookie to dected state change during waiting */
1897 cookie = element->state_cookie;
1899 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1900 "waiting for element to commit state");
1902 /* we have a pending state change, wait for it to complete */
1903 if (!GST_STATE_TIMED_WAIT (element, timeval)) {
1904 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timed out");
1905 /* timeout triggered */
1906 ret = GST_STATE_CHANGE_ASYNC;
1908 if (cookie != element->state_cookie)
1911 /* could be success or failure */
1912 if (old_pending == GST_STATE (element)) {
1913 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got success");
1914 ret = GST_STATE_CHANGE_SUCCESS;
1916 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got failure");
1917 ret = GST_STATE_CHANGE_FAILURE;
1920 /* if nothing is pending anymore we can return SUCCESS */
1921 if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
1922 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "nothing pending");
1923 ret = GST_STATE_CHANGE_SUCCESS;
1929 *state = GST_STATE (element);
1931 *pending = GST_STATE_PENDING (element);
1933 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1934 "state current: %s, pending: %s, result: %s",
1935 gst_element_state_get_name (GST_STATE (element)),
1936 gst_element_state_get_name (GST_STATE_PENDING (element)),
1937 gst_element_state_change_return_get_name (ret));
1938 GST_OBJECT_UNLOCK (element);
1945 *state = GST_STATE_VOID_PENDING;
1947 *pending = GST_STATE_VOID_PENDING;
1949 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "interruped");
1951 GST_OBJECT_UNLOCK (element);
1953 return GST_STATE_CHANGE_FAILURE;
1958 * gst_element_get_state:
1959 * @element: a #GstElement to get the state of.
1960 * @state: a pointer to #GstState to hold the state. Can be %NULL.
1961 * @pending: a pointer to #GstState to hold the pending state.
1963 * @timeout: a #GstClockTime to specify the timeout for an async
1964 * state change or %GST_CLOCK_TIME_NONE for infinite timeout.
1966 * Gets the state of the element.
1968 * For elements that performed an ASYNC state change, as reported by
1969 * gst_element_set_state(), this function will block up to the
1970 * specified timeout value for the state change to complete.
1971 * If the element completes the state change or goes into
1972 * an error, this function returns immediately with a return value of
1973 * %GST_STATE_CHANGE_SUCCESS or %GST_STATE_CHANGE_FAILURE respectively.
1975 * For elements that did not return %GST_STATE_CHANGE_ASYNC, this function
1976 * returns the current and pending state immediately.
1978 * This function returns %GST_STATE_CHANGE_NO_PREROLL if the element
1979 * successfully changed its state but is not able to provide data yet.
1980 * This mostly happens for live sources that only produce data in the PLAYING
1981 * state. While the state change return is equivalent to
1982 * %GST_STATE_CHANGE_SUCCESS, it is returned to the application to signal that
1983 * some sink elements might not be able to complete their state change because
1984 * an element is not producing data to complete the preroll. When setting the
1985 * element to playing, the preroll will complete and playback will start.
1987 * Returns: %GST_STATE_CHANGE_SUCCESS if the element has no more pending state
1988 * and the last state change succeeded, %GST_STATE_CHANGE_ASYNC if the
1989 * element is still performing a state change or
1990 * %GST_STATE_CHANGE_FAILURE if the last state change failed.
1994 GstStateChangeReturn
1995 gst_element_get_state (GstElement * element,
1996 GstState * state, GstState * pending, GstClockTime timeout)
1998 GstElementClass *oclass;
1999 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2001 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2003 oclass = GST_ELEMENT_GET_CLASS (element);
2005 if (oclass->get_state)
2006 result = (oclass->get_state) (element, state, pending, timeout);
2012 * gst_element_abort_state:
2013 * @element: a #GstElement to abort the state of.
2015 * Abort the state change of the element. This function is used
2016 * by elements that do asynchronous state changes and find out
2017 * something is wrong.
2019 * This function should be called with the STATE_LOCK held.
2024 gst_element_abort_state (GstElement * element)
2028 #ifndef GST_DISABLE_GST_DEBUG
2032 g_return_if_fail (GST_IS_ELEMENT (element));
2034 GST_OBJECT_LOCK (element);
2035 pending = GST_STATE_PENDING (element);
2037 if (pending == GST_STATE_VOID_PENDING ||
2038 GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2039 goto nothing_aborted;
2041 #ifndef GST_DISABLE_GST_DEBUG
2042 old_state = GST_STATE (element);
2044 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2045 "aborting state from %s to %s", gst_element_state_get_name (old_state),
2046 gst_element_state_get_name (pending));
2050 GST_STATE_RETURN (element) = GST_STATE_CHANGE_FAILURE;
2052 GST_STATE_BROADCAST (element);
2053 GST_OBJECT_UNLOCK (element);
2059 GST_OBJECT_UNLOCK (element);
2065 * gst_element_continue_state:
2066 * @element: a #GstElement to continue the state change of.
2067 * @ret: The previous state return value
2069 * Commit the state change of the element and proceed to the next
2070 * pending state if any. This function is used
2071 * by elements that do asynchronous state changes.
2072 * The core will normally call this method automatically when an
2073 * element returned %GST_STATE_CHANGE_SUCCESS from the state change function.
2075 * If after calling this method the element still has not reached
2076 * the pending state, the next state change is performed.
2078 * This method is used internally and should normally not be called by plugins
2081 * Returns: The result of the commit state change.
2085 GstStateChangeReturn
2086 gst_element_continue_state (GstElement * element, GstStateChangeReturn ret)
2088 GstStateChangeReturn old_ret;
2089 GstState old_state, old_next;
2090 GstState current, next, pending;
2091 GstMessage *message;
2092 GstStateChange transition;
2094 GST_OBJECT_LOCK (element);
2095 old_ret = GST_STATE_RETURN (element);
2096 GST_STATE_RETURN (element) = ret;
2097 pending = GST_STATE_PENDING (element);
2099 /* check if there is something to commit */
2100 if (pending == GST_STATE_VOID_PENDING)
2101 goto nothing_pending;
2103 old_state = GST_STATE (element);
2104 /* this is the state we should go to next */
2105 old_next = GST_STATE_NEXT (element);
2106 /* update current state */
2107 current = GST_STATE (element) = old_next;
2109 /* see if we reached the final state */
2110 if (pending == current)
2113 next = GST_STATE_GET_NEXT (current, pending);
2114 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2116 GST_STATE_NEXT (element) = next;
2118 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2119 GST_OBJECT_UNLOCK (element);
2121 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2122 "committing state from %s to %s, pending %s",
2123 gst_element_state_get_name (old_state),
2124 gst_element_state_get_name (old_next),
2125 gst_element_state_get_name (pending));
2127 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2128 old_state, old_next, pending);
2129 gst_element_post_message (element, message);
2131 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2132 "continue state change %s to %s, final %s",
2133 gst_element_state_get_name (current),
2134 gst_element_state_get_name (next), gst_element_state_get_name (pending));
2136 ret = gst_element_change_state (element, transition);
2142 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "nothing pending");
2143 GST_OBJECT_UNLOCK (element);
2148 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2149 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2151 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2152 "completed state change to %s", gst_element_state_get_name (pending));
2153 GST_OBJECT_UNLOCK (element);
2155 /* don't post silly messages with the same state. This can happen
2156 * when an element state is changed to what it already was. For bins
2157 * this can be the result of a lost state, which we check with the
2158 * previous return value.
2159 * We do signal the cond though as a _get_state() might be blocking
2161 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC) {
2162 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2163 "posting state-changed %s to %s",
2164 gst_element_state_get_name (old_state),
2165 gst_element_state_get_name (old_next));
2167 gst_message_new_state_changed (GST_OBJECT_CAST (element), old_state,
2168 old_next, GST_STATE_VOID_PENDING);
2169 gst_element_post_message (element, message);
2172 GST_STATE_BROADCAST (element);
2179 * gst_element_lost_state:
2180 * @element: a #GstElement the state is lost of
2182 * Brings the element to the lost state. The current state of the
2183 * element is copied to the pending state so that any call to
2184 * gst_element_get_state() will return %GST_STATE_CHANGE_ASYNC.
2186 * An ASYNC_START message is posted with an indication to distribute a new
2187 * base_time to the element.
2188 * If the element was PLAYING, it will go to PAUSED. The element
2189 * will be restored to its PLAYING state by the parent pipeline when it
2192 * This is mostly used for elements that lost their preroll buffer
2193 * in the %GST_STATE_PAUSED or %GST_STATE_PLAYING state after a flush,
2194 * they will go to their pending state again when a new preroll buffer is
2195 * queued. This function can only be called when the element is currently
2196 * not in error or an async state change.
2198 * This function is used internally and should normally not be called from
2199 * plugins or applications.
2204 gst_element_lost_state (GstElement * element)
2206 GstState old_state, new_state;
2207 GstMessage *message;
2209 g_return_if_fail (GST_IS_ELEMENT (element));
2211 GST_OBJECT_LOCK (element);
2212 if (GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2215 if (GST_STATE_PENDING (element) != GST_STATE_VOID_PENDING)
2216 goto only_async_start;
2218 old_state = GST_STATE (element);
2220 /* when we were PLAYING, the new state is PAUSED. We will also not
2221 * automatically go to PLAYING but let the parent bin(s) set us to PLAYING
2222 * when we preroll. */
2223 if (old_state > GST_STATE_PAUSED)
2224 new_state = GST_STATE_PAUSED;
2226 new_state = old_state;
2228 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2229 "lost state of %s to %s", gst_element_state_get_name (old_state),
2230 gst_element_state_get_name (new_state));
2232 GST_STATE (element) = new_state;
2233 GST_STATE_NEXT (element) = new_state;
2234 GST_STATE_PENDING (element) = new_state;
2235 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2236 GST_OBJECT_UNLOCK (element);
2238 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2239 new_state, new_state, new_state);
2240 gst_element_post_message (element, message);
2242 message = gst_message_new_async_start (GST_OBJECT_CAST (element), TRUE);
2243 gst_element_post_message (element, message);
2249 GST_OBJECT_UNLOCK (element);
2254 GST_OBJECT_UNLOCK (element);
2256 message = gst_message_new_async_start (GST_OBJECT_CAST (element), TRUE);
2257 gst_element_post_message (element, message);
2263 * gst_element_set_state:
2264 * @element: a #GstElement to change state of.
2265 * @state: the element's new #GstState.
2267 * Sets the state of the element. This function will try to set the
2268 * requested state by going through all the intermediary states and calling
2269 * the class's state change function for each.
2271 * This function can return #GST_STATE_CHANGE_ASYNC, in which case the
2272 * element will perform the remainder of the state change asynchronously in
2274 * An application can use gst_element_get_state() to wait for the completion
2275 * of the state change or it can wait for a state change message on the bus.
2277 * State changes to %GST_STATE_READY or %GST_STATE_NULL never return
2278 * #GST_STATE_CHANGE_ASYNC.
2280 * Returns: Result of the state change using #GstStateChangeReturn.
2284 GstStateChangeReturn
2285 gst_element_set_state (GstElement * element, GstState state)
2287 GstElementClass *oclass;
2288 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2290 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2292 oclass = GST_ELEMENT_GET_CLASS (element);
2294 if (oclass->set_state)
2295 result = (oclass->set_state) (element, state);
2301 * default set state function, calculates the next state based
2302 * on current state and calls the change_state function
2304 static GstStateChangeReturn
2305 gst_element_set_state_func (GstElement * element, GstState state)
2307 GstState current, next, old_pending;
2308 GstStateChangeReturn ret;
2309 GstStateChange transition;
2310 GstStateChangeReturn old_ret;
2312 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2314 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "set_state to %s",
2315 gst_element_state_get_name (state));
2317 /* state lock is taken to protect the set_state() and get_state()
2318 * procedures, it does not lock any variables. */
2319 GST_STATE_LOCK (element);
2321 /* now calculate how to get to the new state */
2322 GST_OBJECT_LOCK (element);
2323 old_ret = GST_STATE_RETURN (element);
2324 /* previous state change returned an error, remove all pending
2325 * and next states */
2326 if (old_ret == GST_STATE_CHANGE_FAILURE) {
2327 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2328 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2329 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
2332 current = GST_STATE (element);
2333 next = GST_STATE_NEXT (element);
2334 old_pending = GST_STATE_PENDING (element);
2336 /* this is the (new) state we should go to. TARGET is the last state we set on
2338 if (state != GST_STATE_TARGET (element)) {
2339 GST_STATE_TARGET (element) = state;
2340 /* increment state cookie so that we can track each state change. We only do
2341 * this if this is actually a new state change. */
2342 element->state_cookie++;
2344 GST_STATE_PENDING (element) = state;
2346 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2347 "current %s, old_pending %s, next %s, old return %s",
2348 gst_element_state_get_name (current),
2349 gst_element_state_get_name (old_pending),
2350 gst_element_state_get_name (next),
2351 gst_element_state_change_return_get_name (old_ret));
2353 /* if the element was busy doing a state change, we just update the
2354 * target state, it'll get to it async then. */
2355 if (old_pending != GST_STATE_VOID_PENDING) {
2356 /* upwards state change will happen ASYNC */
2357 if (old_pending <= state)
2359 /* element is going to this state already */
2360 else if (next == state)
2362 /* element was performing an ASYNC upward state change and
2363 * we request to go downward again. Start from the next pending
2365 else if (next > state
2366 && GST_STATE_RETURN (element) == GST_STATE_CHANGE_ASYNC) {
2370 next = GST_STATE_GET_NEXT (current, state);
2371 /* now we store the next state */
2372 GST_STATE_NEXT (element) = next;
2373 /* mark busy, we need to check that there is actually a state change
2374 * to be done else we could accidentally override SUCCESS/NO_PREROLL and
2375 * the default element change_state function has no way to know what the
2376 * old value was... could consider this a FIXME...*/
2377 if (current != next)
2378 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2380 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2382 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2383 "%s: setting state from %s to %s",
2384 (next != state ? "intermediate" : "final"),
2385 gst_element_state_get_name (current), gst_element_state_get_name (next));
2387 /* now signal any waiters, they will error since the cookie was incremented */
2388 GST_STATE_BROADCAST (element);
2390 GST_OBJECT_UNLOCK (element);
2392 ret = gst_element_change_state (element, transition);
2394 GST_STATE_UNLOCK (element);
2396 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "returned %s",
2397 gst_element_state_change_return_get_name (ret));
2403 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2404 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2405 "element was busy with async state change");
2406 GST_OBJECT_UNLOCK (element);
2408 GST_STATE_UNLOCK (element);
2410 return GST_STATE_CHANGE_ASYNC;
2415 * gst_element_change_state:
2416 * @element: a #GstElement
2417 * @transition: the requested transition
2419 * Perform @transition on @element.
2421 * This function must be called with STATE_LOCK held and is mainly used
2424 * Returns: the #GstStateChangeReturn of the state transition.
2426 GstStateChangeReturn
2427 gst_element_change_state (GstElement * element, GstStateChange transition)
2429 GstElementClass *oclass;
2430 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2432 oclass = GST_ELEMENT_GET_CLASS (element);
2434 /* call the state change function so it can set the state */
2435 if (oclass->change_state)
2436 ret = (oclass->change_state) (element, transition);
2438 ret = GST_STATE_CHANGE_FAILURE;
2441 case GST_STATE_CHANGE_FAILURE:
2442 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2443 "have FAILURE change_state return");
2444 /* state change failure */
2445 gst_element_abort_state (element);
2447 case GST_STATE_CHANGE_ASYNC:
2451 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2452 "element will change state ASYNC");
2454 target = GST_STATE_TARGET (element);
2456 if (target > GST_STATE_READY)
2459 /* else we just continue the state change downwards */
2460 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2461 "forcing commit state %s <= %s",
2462 gst_element_state_get_name (target),
2463 gst_element_state_get_name (GST_STATE_READY));
2465 ret = gst_element_continue_state (element, GST_STATE_CHANGE_SUCCESS);
2468 case GST_STATE_CHANGE_SUCCESS:
2469 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2470 "element changed state SUCCESS");
2471 /* we can commit the state now which will proceeed to
2473 ret = gst_element_continue_state (element, ret);
2475 case GST_STATE_CHANGE_NO_PREROLL:
2476 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2477 "element changed state NO_PREROLL");
2478 /* we can commit the state now which will proceeed to
2480 ret = gst_element_continue_state (element, ret);
2483 goto invalid_return;
2486 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit state change %d", ret);
2491 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit async state change %d",
2499 GST_OBJECT_LOCK (element);
2500 /* somebody added a GST_STATE_ and forgot to do stuff here ! */
2501 g_critical ("%s: unknown return value %d from a state change function",
2502 GST_ELEMENT_NAME (element), ret);
2504 /* we are in error now */
2505 ret = GST_STATE_CHANGE_FAILURE;
2506 GST_STATE_RETURN (element) = ret;
2507 GST_OBJECT_UNLOCK (element);
2513 /* gst_iterator_fold functions for pads_activate
2514 * Note how we don't stop the iterator when we fail an activation. This is
2515 * probably a FIXME since when one pad activation fails, we don't want to
2516 * continue our state change. */
2518 activate_pads (GstPad * pad, GValue * ret, gboolean * active)
2520 if (!gst_pad_set_active (pad, *active))
2521 g_value_set_boolean (ret, FALSE);
2523 /* unref the object that was reffed for us by _fold */
2524 gst_object_unref (pad);
2528 /* set the caps on the pad to NULL */
2530 clear_caps (GstPad * pad, GValue * ret, gboolean * active)
2532 gst_pad_set_caps (pad, NULL);
2533 gst_object_unref (pad);
2537 /* returns false on error or early cutout (will never happen because the fold
2538 * function always returns TRUE, see FIXME above) of the fold, true if all
2539 * pads in @iter were (de)activated successfully. */
2541 iterator_activate_fold_with_resync (GstIterator * iter,
2542 GstIteratorFoldFunction func, gpointer user_data)
2544 GstIteratorResult ires;
2547 /* no need to unset this later, it's just a boolean */
2548 g_value_init (&ret, G_TYPE_BOOLEAN);
2549 g_value_set_boolean (&ret, TRUE);
2552 ires = gst_iterator_fold (iter, func, &ret, user_data);
2554 case GST_ITERATOR_RESYNC:
2555 /* need to reset the result again */
2556 g_value_set_boolean (&ret, TRUE);
2557 gst_iterator_resync (iter);
2559 case GST_ITERATOR_DONE:
2560 /* all pads iterated, return collected value */
2563 /* iterator returned _ERROR or premature end with _OK,
2564 * mark an error and exit */
2565 g_value_set_boolean (&ret, FALSE);
2570 /* return collected value */
2571 return g_value_get_boolean (&ret);
2574 /* is called with STATE_LOCK
2576 * Pads are activated from source pads to sinkpads.
2579 gst_element_pads_activate (GstElement * element, gboolean active)
2584 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2585 "pads_activate with active %d", active);
2587 iter = gst_element_iterate_src_pads (element);
2589 iterator_activate_fold_with_resync (iter,
2590 (GstIteratorFoldFunction) activate_pads, &active);
2591 gst_iterator_free (iter);
2592 if (G_UNLIKELY (!res))
2595 iter = gst_element_iterate_sink_pads (element);
2597 iterator_activate_fold_with_resync (iter,
2598 (GstIteratorFoldFunction) activate_pads, &active);
2599 gst_iterator_free (iter);
2600 if (G_UNLIKELY (!res))
2604 /* clear the caps on all pads, this should never fail */
2605 iter = gst_element_iterate_pads (element);
2607 iterator_activate_fold_with_resync (iter,
2608 (GstIteratorFoldFunction) clear_caps, &active);
2609 gst_iterator_free (iter);
2610 if (G_UNLIKELY (!res))
2614 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2615 "pads_activate successful");
2622 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2623 "source pads_activate failed");
2628 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2629 "sink pads_activate failed");
2634 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2635 "failed to clear caps on pads");
2640 /* is called with STATE_LOCK */
2641 static GstStateChangeReturn
2642 gst_element_change_state_func (GstElement * element, GstStateChange transition)
2644 GstState state, next;
2645 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
2647 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2649 state = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2650 next = GST_STATE_TRANSITION_NEXT (transition);
2652 /* if the element already is in the given state, we just return success */
2653 if (next == GST_STATE_VOID_PENDING || state == next)
2656 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element,
2657 "default handler tries setting state from %s to %s (%04x)",
2658 gst_element_state_get_name (state),
2659 gst_element_state_get_name (next), transition);
2661 switch (transition) {
2662 case GST_STATE_CHANGE_NULL_TO_READY:
2664 case GST_STATE_CHANGE_READY_TO_PAUSED:
2665 if (!gst_element_pads_activate (element, TRUE)) {
2666 result = GST_STATE_CHANGE_FAILURE;
2669 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2671 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2673 case GST_STATE_CHANGE_PAUSED_TO_READY:
2674 case GST_STATE_CHANGE_READY_TO_NULL:
2675 /* deactivate pads in both cases, since they are activated on
2676 ready->paused but the element might not have made it to paused */
2677 if (!gst_element_pads_activate (element, FALSE)) {
2678 result = GST_STATE_CHANGE_FAILURE;
2680 gst_element_set_base_time (element, 0);
2684 /* this will catch real but unhandled state changes;
2685 * can only be caused by:
2686 * - a new state was added
2687 * - somehow the element was asked to jump across an intermediate state
2689 g_warning ("Unhandled state change from %s to %s",
2690 gst_element_state_get_name (state),
2691 gst_element_state_get_name (next));
2698 GST_OBJECT_LOCK (element);
2699 result = GST_STATE_RETURN (element);
2700 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2701 "element is already in the %s state",
2702 gst_element_state_get_name (state));
2703 GST_OBJECT_UNLOCK (element);
2710 * gst_element_get_factory:
2711 * @element: a #GstElement to request the element factory of.
2713 * Retrieves the factory that was used to create this element.
2715 * Returns: the #GstElementFactory used for creating this element.
2716 * no refcounting is needed.
2719 gst_element_get_factory (GstElement * element)
2721 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
2723 return GST_ELEMENT_GET_CLASS (element)->elementfactory;
2727 gst_element_dispose (GObject * object)
2729 GstElement *element = GST_ELEMENT (object);
2733 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "dispose");
2735 if (GST_STATE (element) != GST_STATE_NULL)
2738 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2739 "removing %d pads", g_list_length (element->pads));
2740 /* first we break all our links with the outside */
2741 while (element->pads && element->pads->data) {
2742 /* don't call _remove_pad with NULL */
2743 gst_element_remove_pad (element, GST_PAD_CAST (element->pads->data));
2745 if (G_UNLIKELY (element->pads != 0)) {
2746 g_critical ("could not remove pads from element %s",
2747 GST_STR_NULL (GST_OBJECT_NAME (object)));
2750 GST_OBJECT_LOCK (element);
2751 clock_p = &element->clock;
2752 bus_p = &element->bus;
2753 gst_object_replace ((GstObject **) clock_p, NULL);
2754 gst_object_replace ((GstObject **) bus_p, NULL);
2755 GST_OBJECT_UNLOCK (element);
2757 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "parent class dispose");
2759 G_OBJECT_CLASS (parent_class)->dispose (object);
2767 ("\nTrying to dispose element %s, but it is not in the NULL state.\n"
2768 "You need to explicitly set elements to the NULL state before\n"
2769 "dropping the final reference, to allow them to clean up.\n",
2770 GST_OBJECT_NAME (element));
2776 gst_element_finalize (GObject * object)
2778 GstElement *element = GST_ELEMENT (object);
2780 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize");
2782 GST_STATE_LOCK (element);
2783 if (element->state_cond)
2784 g_cond_free (element->state_cond);
2785 element->state_cond = NULL;
2786 GST_STATE_UNLOCK (element);
2787 g_static_rec_mutex_free (element->state_lock);
2788 g_free (element->state_lock);
2789 element->state_lock = NULL;
2791 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize parent");
2793 G_OBJECT_CLASS (parent_class)->finalize (object);
2796 #ifndef GST_DISABLE_LOADSAVE
2798 * gst_element_save_thyself:
2799 * @element: a #GstElement to save.
2800 * @parent: the xml parent node.
2802 * Saves the element as part of the given XML structure.
2804 * Returns: the new #xmlNodePtr.
2807 gst_element_save_thyself (GstObject * object, xmlNodePtr parent)
2810 GstElementClass *oclass;
2811 GParamSpec **specs, *spec;
2814 GValue value = { 0, };
2815 GstElement *element;
2817 g_return_val_if_fail (GST_IS_ELEMENT (object), parent);
2819 element = GST_ELEMENT (object);
2821 oclass = GST_ELEMENT_GET_CLASS (element);
2823 xmlNewChild (parent, NULL, (xmlChar *) "name",
2824 (xmlChar *) GST_ELEMENT_NAME (element));
2826 if (oclass->elementfactory != NULL) {
2827 GstElementFactory *factory = (GstElementFactory *) oclass->elementfactory;
2829 xmlNewChild (parent, NULL, (xmlChar *) "type",
2830 (xmlChar *) GST_PLUGIN_FEATURE (factory)->name);
2834 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &nspecs);
2836 for (i = 0; i < nspecs; i++) {
2838 if (spec->flags & G_PARAM_READABLE) {
2842 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
2844 g_object_get_property (G_OBJECT (element), spec->name, &value);
2845 param = xmlNewChild (parent, NULL, (xmlChar *) "param", NULL);
2846 xmlNewChild (param, NULL, (xmlChar *) "name", (xmlChar *) spec->name);
2848 if (G_IS_PARAM_SPEC_STRING (spec))
2849 contents = g_value_dup_string (&value);
2850 else if (G_IS_PARAM_SPEC_ENUM (spec))
2851 contents = g_strdup_printf ("%d", g_value_get_enum (&value));
2852 else if (G_IS_PARAM_SPEC_INT64 (spec))
2853 contents = g_strdup_printf ("%" G_GINT64_FORMAT,
2854 g_value_get_int64 (&value));
2856 contents = g_strdup_value_contents (&value);
2858 xmlNewChild (param, NULL, (xmlChar *) "value", (xmlChar *) contents);
2861 g_value_unset (&value);
2867 pads = GST_ELEMENT_PADS (element);
2870 GstPad *pad = GST_PAD_CAST (pads->data);
2872 /* figure out if it's a direct pad or a ghostpad */
2873 if (GST_ELEMENT (GST_OBJECT_PARENT (pad)) == element) {
2874 xmlNodePtr padtag = xmlNewChild (parent, NULL, (xmlChar *) "pad", NULL);
2876 gst_object_save_thyself (GST_OBJECT_CAST (pad), padtag);
2878 pads = g_list_next (pads);
2885 gst_element_restore_thyself (GstObject * object, xmlNodePtr self)
2887 xmlNodePtr children;
2888 GstElement *element;
2890 gchar *value = NULL;
2892 element = GST_ELEMENT (object);
2893 g_return_if_fail (element != NULL);
2896 children = self->xmlChildrenNode;
2898 if (!strcmp ((char *) children->name, "param")) {
2899 xmlNodePtr child = children->xmlChildrenNode;
2902 if (!strcmp ((char *) child->name, "name")) {
2903 name = (gchar *) xmlNodeGetContent (child);
2904 } else if (!strcmp ((char *) child->name, "value")) {
2905 value = (gchar *) xmlNodeGetContent (child);
2907 child = child->next;
2909 /* FIXME: can this just be g_object_set ? */
2910 gst_util_set_object_arg (G_OBJECT (element), name, value);
2911 /* g_object_set (G_OBJECT (element), name, value, NULL); */
2915 children = children->next;
2919 children = self->xmlChildrenNode;
2921 if (!strcmp ((char *) children->name, "pad")) {
2922 gst_pad_load_and_link (children, GST_OBJECT_CAST (element));
2924 children = children->next;
2927 if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
2928 (GST_OBJECT_CLASS (parent_class)->restore_thyself) (object, self);
2930 #endif /* GST_DISABLE_LOADSAVE */
2933 gst_element_set_bus_func (GstElement * element, GstBus * bus)
2937 g_return_if_fail (GST_IS_ELEMENT (element));
2939 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element, "setting bus to %p", bus);
2941 GST_OBJECT_LOCK (element);
2942 bus_p = &GST_ELEMENT_BUS (element);
2943 gst_object_replace ((GstObject **) bus_p, GST_OBJECT_CAST (bus));
2944 GST_OBJECT_UNLOCK (element);
2948 * gst_element_set_bus:
2949 * @element: a #GstElement to set the bus of.
2950 * @bus: the #GstBus to set.
2952 * Sets the bus of the element. Increases the refcount on the bus.
2953 * For internal use only, unless you're testing elements.
2958 gst_element_set_bus (GstElement * element, GstBus * bus)
2960 GstElementClass *oclass;
2962 g_return_if_fail (GST_IS_ELEMENT (element));
2964 oclass = GST_ELEMENT_GET_CLASS (element);
2966 if (oclass->set_bus)
2967 oclass->set_bus (element, bus);
2971 * gst_element_get_bus:
2972 * @element: a #GstElement to get the bus of.
2974 * Returns the bus of the element. Note that only a #GstPipeline will provide a
2975 * bus for the application.
2977 * Returns: the element's #GstBus. unref after usage.
2982 gst_element_get_bus (GstElement * element)
2984 GstBus *result = NULL;
2986 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
2988 GST_OBJECT_LOCK (element);
2989 if ((result = GST_ELEMENT_BUS (element)))
2990 gst_object_ref (result);
2991 GST_OBJECT_UNLOCK (element);
2993 GST_CAT_DEBUG_OBJECT (GST_CAT_BUS, element, "got bus %" GST_PTR_FORMAT,