2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2004 Wim Taymans <wim@fluendo.com>
5 * gstelement.c: The base element, all elements derive from this
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 * @short_description: Abstract base class for all pipeline elements
26 * @see_also: #GstElementFactory, #GstPad
28 * GstElement is the abstract base class needed to construct an element that
29 * can be used in a GStreamer pipeline. Please refer to the plugin writers
30 * guide for more information on creating #GstElement subclasses.
32 * The name of a #GstElement can be get with gst_element_get_name() and set with
33 * gst_element_set_name(). For speed, GST_ELEMENT_NAME() can be used in the
34 * core when using the appropriate locking. Do not use this in plug-ins or
35 * applications in order to retain ABI compatibility.
37 * All elements have pads (of the type #GstPad). These pads link to pads on
38 * other elements. #GstBuffer flow between these linked pads.
39 * A #GstElement has a #GList of #GstPad structures for all their input (or sink)
40 * and output (or source) pads.
41 * Core and plug-in writers can add and remove pads with gst_element_add_pad()
42 * and gst_element_remove_pad().
44 * A pad of an element can be retrieved by name with gst_element_get_pad().
45 * An iterator of all pads can be retrieved with gst_element_iterate_pads().
47 * Elements can be linked through their pads.
48 * If the link is straightforward, use the gst_element_link()
49 * convenience function to link two elements, or gst_element_link_many()
50 * for more elements in a row.
51 * Use gst_element_link_filtered() to link two elements constrained by
52 * a specified set of #GstCaps.
53 * For finer control, use gst_element_link_pads() and
54 * gst_element_link_pads_filtered() to specify the pads to link on
55 * each element by name.
57 * Each element has a state (see #GstState). You can get and set the state
58 * of an element with gst_element_get_state() and gst_element_set_state().
59 * To get a string representation of a #GstState, use
60 * gst_element_state_get_name().
62 * You can get and set a #GstClock on an element using gst_element_get_clock()
63 * and gst_element_set_clock().
64 * Some elements can provide a clock for the pipeline if
65 * gst_element_provides_clock() returns %TRUE. With the
66 * gst_element_provide_clock() method one can retrieve the clock provided by
68 * Not all elements require a clock to operate correctly. If
69 * gst_element_requires_clock() returns %TRUE, a clock should be set on the
70 * element with gst_element_set_clock().
72 * Note that clock slection and distribution is normally handled by the
73 * toplevel #GstPipeline so the clock functions are only to be used in very
74 * specific situations.
76 * Last reviewed on 2006-03-12 (0.10.5)
79 #include "gst_private.h"
82 #include <gobject/gvaluecollector.h>
84 #include "gstelement.h"
85 #include "gstenumtypes.h"
87 #include "gstmarshal.h"
92 #include "gst-i18n-lib.h"
94 /* Element signals and args */
110 extern void __gst_element_details_clear (GstElementDetails * dp);
111 extern void __gst_element_details_copy (GstElementDetails * dest,
112 const GstElementDetails * src);
114 static void gst_element_class_init (GstElementClass * klass);
115 static void gst_element_init (GstElement * element);
116 static void gst_element_base_class_init (gpointer g_class);
117 static void gst_element_base_class_finalize (gpointer g_class);
119 static void gst_element_dispose (GObject * object);
120 static void gst_element_finalize (GObject * object);
122 static GstStateChangeReturn gst_element_change_state_func (GstElement * element,
123 GstStateChange transition);
124 static GstStateChangeReturn gst_element_get_state_func (GstElement * element,
125 GstState * state, GstState * pending, GstClockTime timeout);
126 static GstStateChangeReturn gst_element_set_state_func (GstElement * element,
128 static void gst_element_set_bus_func (GstElement * element, GstBus * bus);
130 static gboolean gst_element_default_send_event (GstElement * element,
132 static gboolean gst_element_default_query (GstElement * element,
135 static GstPadTemplate
136 * gst_element_class_get_request_pad_template (GstElementClass *
137 element_class, const gchar * name);
139 #ifndef GST_DISABLE_LOADSAVE
140 static xmlNodePtr gst_element_save_thyself (GstObject * object,
142 static void gst_element_restore_thyself (GstObject * parent, xmlNodePtr self);
145 static GstObjectClass *parent_class = NULL;
146 static guint gst_element_signals[LAST_SIGNAL] = { 0 };
149 gst_element_get_type (void)
151 static GType gst_element_type = 0;
153 if (G_UNLIKELY (gst_element_type == 0)) {
154 static const GTypeInfo element_info = {
155 sizeof (GstElementClass),
156 gst_element_base_class_init,
157 gst_element_base_class_finalize,
158 (GClassInitFunc) gst_element_class_init,
163 (GInstanceInitFunc) gst_element_init,
167 gst_element_type = g_type_register_static (GST_TYPE_OBJECT, "GstElement",
168 &element_info, G_TYPE_FLAG_ABSTRACT);
170 return gst_element_type;
174 gst_element_class_init (GstElementClass * klass)
176 GObjectClass *gobject_class;
177 GstObjectClass *gstobject_class;
179 gobject_class = (GObjectClass *) klass;
180 gstobject_class = (GstObjectClass *) klass;
182 parent_class = g_type_class_peek_parent (klass);
185 * GstElement::pad-added:
186 * @gstelement: the object which received the signal
187 * @new_pad: the pad that has been added
189 * a new #GstPad has been added to the element.
191 gst_element_signals[PAD_ADDED] =
192 g_signal_new ("pad-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
193 G_STRUCT_OFFSET (GstElementClass, pad_added), NULL, NULL,
194 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
196 * GstElement::pad-removed:
197 * @gstelement: the object which received the signal
198 * @old_pad: the pad that has been removed
200 * a #GstPad has been removed from the element
202 gst_element_signals[PAD_REMOVED] =
203 g_signal_new ("pad-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
204 G_STRUCT_OFFSET (GstElementClass, pad_removed), NULL, NULL,
205 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
207 * GstElement::no-more-pads:
208 * @gstelement: the object which received the signal
210 * This signals that the element will not generate more dynamic pads.
212 gst_element_signals[NO_MORE_PADS] =
213 g_signal_new ("no-more-pads", G_TYPE_FROM_CLASS (klass),
214 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstElementClass, no_more_pads), NULL,
215 NULL, gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
217 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_element_dispose);
218 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_element_finalize);
220 #ifndef GST_DISABLE_LOADSAVE
221 gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_element_save_thyself);
222 gstobject_class->restore_thyself =
223 GST_DEBUG_FUNCPTR (gst_element_restore_thyself);
226 klass->change_state = GST_DEBUG_FUNCPTR (gst_element_change_state_func);
227 klass->set_state = GST_DEBUG_FUNCPTR (gst_element_set_state_func);
228 klass->get_state = GST_DEBUG_FUNCPTR (gst_element_get_state_func);
229 klass->set_bus = GST_DEBUG_FUNCPTR (gst_element_set_bus_func);
230 klass->query = GST_DEBUG_FUNCPTR (gst_element_default_query);
231 klass->send_event = GST_DEBUG_FUNCPTR (gst_element_default_send_event);
232 klass->numpadtemplates = 0;
234 klass->elementfactory = NULL;
238 gst_element_base_class_init (gpointer g_class)
240 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
242 /* FIXME 0.11: Copy the element details and instead of clearing the
243 * pad template list copy the list and increase the refcount of
244 * the pad templates by one.
246 * This will make it possible to add pad templates and set element
247 * details in the class_init functions and is the real GObject way
249 * See http://bugzilla.gnome.org/show_bug.cgi?id=491501
251 memset (&element_class->details, 0, sizeof (GstElementDetails));
252 element_class->padtemplates = NULL;
256 gst_element_base_class_finalize (gpointer g_class)
258 GstElementClass *klass = GST_ELEMENT_CLASS (g_class);
260 g_list_foreach (klass->padtemplates, (GFunc) gst_object_unref, NULL);
261 g_list_free (klass->padtemplates);
262 __gst_element_details_clear (&klass->details);
266 gst_element_init (GstElement * element)
268 GST_STATE (element) = GST_STATE_NULL;
269 GST_STATE_TARGET (element) = GST_STATE_NULL;
270 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
271 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
272 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
274 element->state_lock = g_new0 (GStaticRecMutex, 1);
275 g_static_rec_mutex_init (element->state_lock);
276 element->state_cond = g_cond_new ();
280 * gst_element_default_error:
281 * @object: a #GObject that signalled the error.
282 * @orig: the #GstObject that initiated the error.
283 * @error: the GError.
284 * @debug: an additional debug information string, or %NULL.
286 * A default error signal callback to attach to an element.
287 * The user data passed to the g_signal_connect is ignored.
289 * The default handler will simply print the error string using g_print.
294 gst_element_default_error (GObject * object, GstObject * source, GError * error,
297 gchar *name = gst_object_get_path_string (source);
299 g_print (_("ERROR: from element %s: %s\n"), name, error->message);
301 g_print (_("Additional debug info:\n%s\n"), debug);
307 * gst_element_release_request_pad:
308 * @element: a #GstElement to release the request pad of.
309 * @pad: the #GstPad to release.
311 * Makes the element free the previously requested pad as obtained
312 * with gst_element_get_request_pad().
317 gst_element_release_request_pad (GstElement * element, GstPad * pad)
319 GstElementClass *oclass;
321 g_return_if_fail (GST_IS_ELEMENT (element));
322 g_return_if_fail (GST_IS_PAD (pad));
324 oclass = GST_ELEMENT_GET_CLASS (element);
326 /* if the element implements a custom release function we call that, else we
327 * simply remove the pad from the element */
328 if (oclass->release_pad)
329 (oclass->release_pad) (element, pad);
331 gst_element_remove_pad (element, pad);
335 * gst_element_requires_clock:
336 * @element: a #GstElement to query
338 * Query if the element requires a clock.
340 * Returns: %TRUE if the element requires a clock
345 gst_element_requires_clock (GstElement * element)
349 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
351 result = (GST_ELEMENT_GET_CLASS (element)->set_clock != NULL);
357 * gst_element_provides_clock:
358 * @element: a #GstElement to query
360 * Query if the element provides a clock. A #GstClock provided by an
361 * element can be used as the global #GstClock for the pipeline.
362 * An element that can provide a clock is only required to do so in the PAUSED
363 * state, this means when it is fully negotiated and has allocated the resources
364 * to operate the clock.
366 * Returns: %TRUE if the element provides a clock
371 gst_element_provides_clock (GstElement * element)
375 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
377 result = (GST_ELEMENT_GET_CLASS (element)->provide_clock != NULL);
383 * gst_element_provide_clock:
384 * @element: a #GstElement to query
386 * Get the clock provided by the given element.
387 * <note>An element is only required to provide a clock in the PAUSED
388 * state. Some elements can provide a clock in other states.</note>
390 * Returns: the GstClock provided by the element or %NULL
391 * if no clock could be provided. Unref after usage.
396 gst_element_provide_clock (GstElement * element)
398 GstClock *result = NULL;
399 GstElementClass *oclass;
401 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
403 oclass = GST_ELEMENT_GET_CLASS (element);
405 if (oclass->provide_clock)
406 result = oclass->provide_clock (element);
412 * gst_element_set_clock:
413 * @element: a #GstElement to set the clock for.
414 * @clock: the #GstClock to set for the element.
416 * Sets the clock for the element. This function increases the
417 * refcount on the clock. Any previously set clock on the object
420 * Returns: %TRUE if the element accepted the clock. An element can refuse a
421 * clock when it, for example, is not able to slave its internal clock to the
422 * @clock or when it requires a specific clock to operate.
427 gst_element_set_clock (GstElement * element, GstClock * clock)
429 GstElementClass *oclass;
433 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
435 oclass = GST_ELEMENT_GET_CLASS (element);
437 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element, "setting clock %p", clock);
439 if (oclass->set_clock)
440 res = oclass->set_clock (element, clock);
443 /* only update the clock pointer if the element accepted the clock */
444 GST_OBJECT_LOCK (element);
445 clock_p = &element->clock;
446 gst_object_replace ((GstObject **) clock_p, (GstObject *) clock);
447 GST_OBJECT_UNLOCK (element);
453 * gst_element_get_clock:
454 * @element: a #GstElement to get the clock of.
456 * Gets the currently configured clock of the element. This is the clock as was
457 * last set with gst_element_set_clock().
459 * Returns: the #GstClock of the element. unref after usage.
464 gst_element_get_clock (GstElement * element)
468 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
470 GST_OBJECT_LOCK (element);
471 if ((result = element->clock))
472 gst_object_ref (result);
473 GST_OBJECT_UNLOCK (element);
479 * gst_element_set_base_time:
480 * @element: a #GstElement.
481 * @time: the base time to set.
483 * Set the base time of an element. See gst_element_get_base_time().
488 gst_element_set_base_time (GstElement * element, GstClockTime time)
492 g_return_if_fail (GST_IS_ELEMENT (element));
494 GST_OBJECT_LOCK (element);
495 old = element->base_time;
496 element->base_time = time;
497 GST_OBJECT_UNLOCK (element);
499 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element,
500 "set base_time=%" GST_TIME_FORMAT ", old %" GST_TIME_FORMAT,
501 GST_TIME_ARGS (time), GST_TIME_ARGS (old));
505 * gst_element_get_base_time:
506 * @element: a #GstElement.
508 * Returns the base time of the element. The base time is the
509 * absolute time of the clock when this element was last put to
510 * PLAYING. Subtracting the base time from the clock time gives
511 * the stream time of the element.
513 * Returns: the base time of the element.
518 gst_element_get_base_time (GstElement * element)
522 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
524 GST_OBJECT_LOCK (element);
525 result = element->base_time;
526 GST_OBJECT_UNLOCK (element);
531 #ifndef GST_DISABLE_INDEX
533 * gst_element_is_indexable:
534 * @element: a #GstElement.
536 * Queries if the element can be indexed.
538 * Returns: TRUE if the element can be indexed.
543 gst_element_is_indexable (GstElement * element)
547 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
549 result = (GST_ELEMENT_GET_CLASS (element)->set_index != NULL);
555 * gst_element_set_index:
556 * @element: a #GstElement.
557 * @index: a #GstIndex.
559 * Set @index on the element. The refcount of the index
560 * will be increased, any previously set index is unreffed.
565 gst_element_set_index (GstElement * element, GstIndex * index)
567 GstElementClass *oclass;
569 g_return_if_fail (GST_IS_ELEMENT (element));
570 g_return_if_fail (GST_IS_INDEX (index));
572 oclass = GST_ELEMENT_GET_CLASS (element);
574 if (oclass->set_index)
575 oclass->set_index (element, index);
579 * gst_element_get_index:
580 * @element: a #GstElement.
582 * Gets the index from the element.
584 * Returns: a #GstIndex or %NULL when no index was set on the
585 * element. unref after usage.
590 gst_element_get_index (GstElement * element)
592 GstElementClass *oclass;
593 GstIndex *result = NULL;
595 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
597 oclass = GST_ELEMENT_GET_CLASS (element);
599 if (oclass->get_index)
600 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 * <note>Usage of this function is not recommended as it is 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.</note>
1010 * Returns: the #GstPad if found, otherwise %NULL. Unref or Release after usage,
1011 * depending on the type of the pad.
1014 gst_element_get_pad (GstElement * element, const gchar * name)
1018 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1019 g_return_val_if_fail (name != NULL, NULL);
1021 pad = gst_element_get_static_pad (element, name);
1023 pad = gst_element_get_request_pad (element, name);
1028 static GstIteratorItem
1029 iterate_pad (GstIterator * it, GstPad * pad)
1031 gst_object_ref (pad);
1032 return GST_ITERATOR_ITEM_PASS;
1035 static GstIterator *
1036 gst_element_iterate_pad_list (GstElement * element, GList ** padlist)
1038 GstIterator *result;
1040 GST_OBJECT_LOCK (element);
1041 gst_object_ref (element);
1042 result = gst_iterator_new_list (GST_TYPE_PAD,
1043 GST_OBJECT_GET_LOCK (element),
1044 &element->pads_cookie,
1047 (GstIteratorItemFunction) iterate_pad,
1048 (GstIteratorDisposeFunction) gst_object_unref);
1049 GST_OBJECT_UNLOCK (element);
1055 * gst_element_iterate_pads:
1056 * @element: a #GstElement to iterate pads of.
1058 * Retrieves an iterattor of @element's pads. The iterator should
1059 * be freed after usage.
1061 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1066 gst_element_iterate_pads (GstElement * element)
1068 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1070 return gst_element_iterate_pad_list (element, &element->pads);
1074 * gst_element_iterate_src_pads:
1075 * @element: a #GstElement.
1077 * Retrieves an iterator of @element's source pads.
1079 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1084 gst_element_iterate_src_pads (GstElement * element)
1086 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1088 return gst_element_iterate_pad_list (element, &element->srcpads);
1092 * gst_element_iterate_sink_pads:
1093 * @element: a #GstElement.
1095 * Retrieves an iterator of @element's sink pads.
1097 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1102 gst_element_iterate_sink_pads (GstElement * element)
1104 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1106 return gst_element_iterate_pad_list (element, &element->sinkpads);
1110 * gst_element_class_add_pad_template:
1111 * @klass: the #GstElementClass to add the pad template to.
1112 * @templ: a #GstPadTemplate to add to the element class.
1114 * Adds a padtemplate to an element class. This is mainly used in the _base_init
1115 * functions of classes.
1118 gst_element_class_add_pad_template (GstElementClass * klass,
1119 GstPadTemplate * templ)
1121 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1122 g_return_if_fail (GST_IS_PAD_TEMPLATE (templ));
1124 /* FIXME 0.11: allow replacing the pad templates by
1125 * calling this with the same name as an already existing pad
1126 * template. For this we _must_ _not_ ref the added pad template
1127 * a second time and _must_ document that this function takes
1128 * ownership of the pad template. Otherwise we will leak pad templates
1129 * or the caller unref's the pad template and it disappears */
1130 /* avoid registering pad templates with the same name */
1131 g_return_if_fail (gst_element_class_get_pad_template (klass,
1132 templ->name_template) == NULL);
1134 klass->padtemplates = g_list_append (klass->padtemplates,
1135 gst_object_ref (templ));
1136 klass->numpadtemplates++;
1140 * gst_element_class_set_details:
1141 * @klass: class to set details for
1142 * @details: details to set
1144 * Sets the detailed information for a #GstElementClass.
1145 * <note>This function is for use in _base_init functions only.</note>
1147 * The @details are copied.
1150 gst_element_class_set_details (GstElementClass * klass,
1151 const GstElementDetails * details)
1153 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1154 g_return_if_fail (GST_IS_ELEMENT_DETAILS (details));
1156 __gst_element_details_copy (&klass->details, details);
1160 * gst_element_class_set_details_simple:
1161 * @klass: class to set details for
1162 * @longname: The long English name of the element. E.g. "File Sink"
1163 * @classification: String describing the type of element, as an unordered list
1164 * separated with slashes ('/'). See draft-klass.txt of the design docs
1165 * for more details and common types. E.g: "Sink/File"
1166 * @description: Sentence describing the purpose of the element.
1167 * E.g: "Write stream to a file"
1168 * @author: Name and contact details of the author(s). Use \n to separate
1169 * multiple author details. E.g: "Joe Bloggs <joe.blogs at foo.com>"
1171 * Sets the detailed information for a #GstElementClass. Simpler version of
1172 * gst_element_class_set_details() that generates less linker overhead.
1173 * <note>This function is for use in _base_init functions only.</note>
1175 * The detail parameter strings are copied into the #GstElementDetails for
1176 * the element class.
1181 gst_element_class_set_details_simple (GstElementClass * klass,
1182 const gchar * longname, const gchar * classification,
1183 const gchar * description, const gchar * author)
1185 const GstElementDetails details =
1186 GST_ELEMENT_DETAILS ((gchar *) longname, (gchar *) classification,
1187 (gchar *) description, (gchar *) author);
1189 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1191 __gst_element_details_copy (&klass->details, &details);
1195 * gst_element_class_get_pad_template_list:
1196 * @element_class: a #GstElementClass to get pad templates of.
1198 * Retrieves a list of the pad templates associated with @element_class. The
1199 * list must not be modified by the calling code.
1200 * <note>If you use this function in the #GInstanceInitFunc of an object class
1201 * that has subclasses, make sure to pass the g_class parameter of the
1202 * #GInstanceInitFunc here.</note>
1204 * Returns: the #GList of padtemplates.
1207 gst_element_class_get_pad_template_list (GstElementClass * element_class)
1209 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1211 return element_class->padtemplates;
1215 * gst_element_class_get_pad_template:
1216 * @element_class: a #GstElementClass to get the pad template of.
1217 * @name: the name of the #GstPadTemplate to get.
1219 * Retrieves a padtemplate from @element_class with the given name.
1220 * <note>If you use this function in the #GInstanceInitFunc of an object class
1221 * that has subclasses, make sure to pass the g_class parameter of the
1222 * #GInstanceInitFunc here.</note>
1224 * Returns: the #GstPadTemplate with the given name, or %NULL if none was found.
1225 * No unreferencing is necessary.
1228 gst_element_class_get_pad_template (GstElementClass * element_class,
1233 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1234 g_return_val_if_fail (name != NULL, NULL);
1236 padlist = gst_element_class_get_pad_template_list (element_class);
1239 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
1241 if (strcmp (padtempl->name_template, name) == 0)
1244 padlist = g_list_next (padlist);
1250 static GstPadTemplate *
1251 gst_element_class_get_request_pad_template (GstElementClass * element_class,
1254 GstPadTemplate *tmpl;
1256 tmpl = gst_element_class_get_pad_template (element_class, name);
1257 if (tmpl != NULL && tmpl->presence == GST_PAD_REQUEST)
1263 /* get a random pad on element of the given direction.
1264 * The pad is random in a sense that it is the first pad that is (optionaly) linked.
1267 gst_element_get_random_pad (GstElement * element, gboolean need_linked,
1268 GstPadDirection dir)
1270 GstPad *result = NULL;
1273 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "getting a random pad");
1277 GST_OBJECT_LOCK (element);
1278 pads = element->srcpads;
1281 GST_OBJECT_LOCK (element);
1282 pads = element->sinkpads;
1285 goto wrong_direction;
1287 for (; pads; pads = g_list_next (pads)) {
1288 GstPad *pad = GST_PAD_CAST (pads->data);
1290 GST_OBJECT_LOCK (pad);
1291 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "checking pad %s:%s",
1292 GST_DEBUG_PAD_NAME (pad));
1294 if (need_linked && !GST_PAD_IS_LINKED (pad)) {
1295 /* if we require a linked pad, and it is not linked, continue the
1297 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is not linked",
1298 GST_DEBUG_PAD_NAME (pad));
1299 GST_OBJECT_UNLOCK (pad);
1302 /* found a pad, stop search */
1303 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
1304 GST_DEBUG_PAD_NAME (pad));
1305 GST_OBJECT_UNLOCK (pad);
1311 gst_object_ref (result);
1313 GST_OBJECT_UNLOCK (element);
1317 /* ERROR handling */
1320 g_warning ("unknown pad direction %d", dir);
1326 gst_element_default_send_event (GstElement * element, GstEvent * event)
1328 gboolean result = FALSE;
1331 pad = GST_EVENT_IS_DOWNSTREAM (event) ?
1332 gst_element_get_random_pad (element, TRUE, GST_PAD_SRC) :
1333 gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1336 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1337 "pushing %s event to random %s pad %s:%s",
1338 GST_EVENT_TYPE_NAME (event),
1339 (GST_PAD_DIRECTION (pad) == GST_PAD_SRC ? "src" : "sink"),
1340 GST_DEBUG_PAD_NAME (pad));
1342 result = gst_pad_push_event (pad, event);
1343 gst_object_unref (pad);
1345 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "can't send %s event on element %s",
1346 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1352 * gst_element_send_event:
1353 * @element: a #GstElement to send the event to.
1354 * @event: the #GstEvent to send to the element.
1356 * Sends an event to an element. If the element doesn't implement an
1357 * event handler, the event will be pushed on a random linked sink pad for
1358 * upstream events or a random linked source pad for downstream events.
1360 * This function takes owership of the provided event so you should
1361 * gst_event_ref() it if you want to reuse the event after this call.
1363 * Returns: %TRUE if the event was handled.
1368 gst_element_send_event (GstElement * element, GstEvent * event)
1370 GstElementClass *oclass;
1371 gboolean result = FALSE;
1373 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1374 g_return_val_if_fail (event != NULL, FALSE);
1376 oclass = GST_ELEMENT_GET_CLASS (element);
1378 GST_STATE_LOCK (element);
1379 if (oclass->send_event) {
1380 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send %s event on element %s",
1381 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1382 result = oclass->send_event (element, event);
1384 result = gst_element_default_send_event (element, event);
1386 GST_STATE_UNLOCK (element);
1393 * @element: a #GstElement to send the event to.
1394 * @rate: The new playback rate
1395 * @format: The format of the seek values
1396 * @flags: The optional seek flags.
1397 * @cur_type: The type and flags for the new current position
1398 * @cur: The value of the new current position
1399 * @stop_type: The type and flags for the new stop position
1400 * @stop: The value of the new stop position
1402 * Sends a seek event to an element. See gst_event_new_seek() for the details of
1403 * the parameters. The seek event is sent to the element using
1404 * gst_element_send_event().
1406 * Returns: %TRUE if the event was handled.
1411 gst_element_seek (GstElement * element, gdouble rate, GstFormat format,
1412 GstSeekFlags flags, GstSeekType cur_type, gint64 cur,
1413 GstSeekType stop_type, gint64 stop)
1418 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1421 gst_event_new_seek (rate, format, flags, cur_type, cur, stop_type, stop);
1422 result = gst_element_send_event (element, event);
1428 * gst_element_get_query_types:
1429 * @element: a #GstElement to query
1431 * Get an array of query types from the element.
1432 * If the element doesn't implement a query types function,
1433 * the query will be forwarded to the peer of a random linked sink pad.
1435 * Returns: An array of #GstQueryType elements that should not
1436 * be freed or modified.
1440 const GstQueryType *
1441 gst_element_get_query_types (GstElement * element)
1443 GstElementClass *oclass;
1444 const GstQueryType *result = NULL;
1446 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1448 oclass = GST_ELEMENT_GET_CLASS (element);
1450 if (oclass->get_query_types) {
1451 result = oclass->get_query_types (element);
1453 GstPad *pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1456 GstPad *peer = gst_pad_get_peer (pad);
1459 result = gst_pad_get_query_types (peer);
1461 gst_object_unref (peer);
1463 gst_object_unref (pad);
1470 gst_element_default_query (GstElement * element, GstQuery * query)
1472 gboolean result = FALSE;
1475 pad = gst_element_get_random_pad (element, FALSE, GST_PAD_SRC);
1477 result = gst_pad_query (pad, query);
1479 gst_object_unref (pad);
1481 pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1483 GstPad *peer = gst_pad_get_peer (pad);
1486 result = gst_pad_query (peer, query);
1488 gst_object_unref (peer);
1490 gst_object_unref (pad);
1497 * gst_element_query:
1498 * @element: a #GstElement to perform the query on.
1499 * @query: the #GstQuery.
1501 * Performs a query on the given element.
1503 * For elements that don't implement a query handler, this function
1504 * forwards the query to a random srcpad or to the peer of a
1505 * random linked sinkpad of this element.
1507 * Returns: TRUE if the query could be performed.
1512 gst_element_query (GstElement * element, GstQuery * query)
1514 GstElementClass *oclass;
1515 gboolean result = FALSE;
1517 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1518 g_return_val_if_fail (query != NULL, FALSE);
1520 oclass = GST_ELEMENT_GET_CLASS (element);
1522 if (oclass->query) {
1523 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send query on element %s",
1524 GST_ELEMENT_NAME (element));
1525 result = oclass->query (element, query);
1527 result = gst_element_default_query (element, query);
1533 * gst_element_post_message:
1534 * @element: a #GstElement posting the message
1535 * @message: a #GstMessage to post
1537 * Post a message on the element's #GstBus. This function takes ownership of the
1538 * message; if you want to access the message after this call, you should add an
1539 * additional reference before calling.
1541 * Returns: %TRUE if the message was successfully posted. The function returns
1542 * %FALSE if the element did not have a bus.
1547 gst_element_post_message (GstElement * element, GstMessage * message)
1550 gboolean result = FALSE;
1552 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1553 g_return_val_if_fail (message != NULL, FALSE);
1555 GST_OBJECT_LOCK (element);
1558 if (G_UNLIKELY (bus == NULL))
1561 gst_object_ref (bus);
1562 GST_OBJECT_UNLOCK (element);
1564 /* we release the element lock when posting the message so that any
1565 * (synchronous) message handlers can operate on the element */
1566 result = gst_bus_post (bus, message);
1567 gst_object_unref (bus);
1574 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, element,
1575 "not posting message %p: no bus", message);
1576 GST_OBJECT_UNLOCK (element);
1577 gst_message_unref (message);
1583 * _gst_element_error_printf:
1584 * @format: the printf-like format to use, or %NULL
1586 * This function is only used internally by the gst_element_error() macro.
1588 * Returns: a newly allocated string, or %NULL if the format was %NULL or ""
1593 _gst_element_error_printf (const gchar * format, ...)
1603 va_start (args, format);
1604 buffer = g_strdup_vprintf (format, args);
1610 * gst_element_message_full:
1611 * @element: a #GstElement to send message from
1612 * @type: the #GstMessageType
1613 * @domain: the GStreamer GError domain this message belongs to
1614 * @code: the GError code belonging to the domain
1615 * @text: an allocated text string to be used as a replacement for the
1616 * default message connected to code, or %NULL
1617 * @debug: an allocated debug message to be used as a replacement for the
1618 * default debugging information, or %NULL
1619 * @file: the source code file where the error was generated
1620 * @function: the source code function where the error was generated
1621 * @line: the source code line where the error was generated
1623 * Post an error, warning or info message on the bus from inside an element.
1625 * @type must be of #GST_MESSAGE_ERROR, #GST_MESSAGE_WARNING or
1626 * #GST_MESSAGE_INFO.
1630 void gst_element_message_full
1631 (GstElement * element, GstMessageType type,
1632 GQuark domain, gint code, gchar * text,
1633 gchar * debug, const gchar * file, const gchar * function, gint line)
1635 GError *gerror = NULL;
1639 gboolean has_debug = TRUE;
1640 GstMessage *message = NULL;
1643 GST_DEBUG_OBJECT (element, "start");
1644 g_return_if_fail (GST_IS_ELEMENT (element));
1645 g_return_if_fail ((type == GST_MESSAGE_ERROR) ||
1646 (type == GST_MESSAGE_WARNING) || (type == GST_MESSAGE_INFO));
1648 /* check if we send the given text or the default error text */
1649 if ((text == NULL) || (text[0] == 0)) {
1650 /* text could have come from g_strdup_printf (""); */
1652 sent_text = gst_error_get_message (domain, code);
1656 /* construct a sent_debug with extra information from source */
1657 if ((debug == NULL) || (debug[0] == 0)) {
1658 /* debug could have come from g_strdup_printf (""); */
1662 name = gst_object_get_path_string (GST_OBJECT_CAST (element));
1664 sent_debug = g_strdup_printf ("%s(%d): %s (): %s:\n%s",
1665 file, line, function, name, debug);
1667 sent_debug = g_strdup_printf ("%s(%d): %s (): %s",
1668 file, line, function, name);
1672 /* create gerror and post message */
1673 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posting message: %s",
1675 gerror = g_error_new_literal (domain, code, sent_text);
1678 case GST_MESSAGE_ERROR:
1680 gst_message_new_error (GST_OBJECT_CAST (element), gerror, sent_debug);
1682 case GST_MESSAGE_WARNING:
1683 message = gst_message_new_warning (GST_OBJECT_CAST (element), gerror,
1686 case GST_MESSAGE_INFO:
1687 message = gst_message_new_info (GST_OBJECT_CAST (element), gerror,
1691 g_assert_not_reached ();
1694 gst_element_post_message (element, message);
1696 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posted %s message: %s",
1697 (type == GST_MESSAGE_ERROR ? "error" : "warning"), sent_text);
1700 g_error_free (gerror);
1701 g_free (sent_debug);
1706 * gst_element_is_locked_state:
1707 * @element: a #GstElement.
1709 * Checks if the state of an element is locked.
1710 * If the state of an element is locked, state changes of the parent don't
1711 * affect the element.
1712 * This way you can leave currently unused elements inside bins. Just lock their
1713 * state before changing the state from #GST_STATE_NULL.
1717 * Returns: TRUE, if the element's state is locked.
1720 gst_element_is_locked_state (GstElement * element)
1724 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1726 GST_OBJECT_LOCK (element);
1727 result = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1728 GST_OBJECT_UNLOCK (element);
1734 * gst_element_set_locked_state:
1735 * @element: a #GstElement
1736 * @locked_state: TRUE to lock the element's state
1738 * Locks the state of an element, so state changes of the parent don't affect
1739 * this element anymore.
1743 * Returns: TRUE if the state was changed, FALSE if bad parameters were given
1744 * or the elements state-locking needed no change.
1747 gst_element_set_locked_state (GstElement * element, gboolean locked_state)
1751 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1753 GST_OBJECT_LOCK (element);
1754 old = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1756 if (G_UNLIKELY (old == locked_state))
1760 GST_CAT_DEBUG (GST_CAT_STATES, "locking state of element %s",
1761 GST_ELEMENT_NAME (element));
1762 GST_OBJECT_FLAG_SET (element, GST_ELEMENT_LOCKED_STATE);
1764 GST_CAT_DEBUG (GST_CAT_STATES, "unlocking state of element %s",
1765 GST_ELEMENT_NAME (element));
1766 GST_OBJECT_FLAG_UNSET (element, GST_ELEMENT_LOCKED_STATE);
1768 GST_OBJECT_UNLOCK (element);
1774 GST_CAT_DEBUG (GST_CAT_STATES, "elements %s was in locked state %d",
1775 GST_ELEMENT_NAME (element), old);
1776 GST_OBJECT_UNLOCK (element);
1783 * gst_element_sync_state_with_parent:
1784 * @element: a #GstElement.
1786 * Tries to change the state of the element to the same as its parent.
1787 * If this function returns FALSE, the state of element is undefined.
1789 * Returns: TRUE, if the element's state could be synced to the parent's state.
1794 gst_element_sync_state_with_parent (GstElement * element)
1798 GstStateChangeReturn ret;
1800 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1802 if ((parent = GST_ELEMENT_CAST (gst_element_get_parent (element)))) {
1803 GstState parent_current, parent_pending;
1805 GST_OBJECT_LOCK (parent);
1806 parent_current = GST_STATE (parent);
1807 parent_pending = GST_STATE_PENDING (parent);
1808 GST_OBJECT_UNLOCK (parent);
1810 /* set to pending if there is one, else we set it to the current state of
1812 if (parent_pending != GST_STATE_VOID_PENDING)
1813 target = parent_pending;
1815 target = parent_current;
1817 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1818 "syncing state (%s) to parent %s %s (%s, %s)",
1819 gst_element_state_get_name (GST_STATE (element)),
1820 GST_ELEMENT_NAME (parent), gst_element_state_get_name (target),
1821 gst_element_state_get_name (parent_current),
1822 gst_element_state_get_name (parent_pending));
1824 ret = gst_element_set_state (element, target);
1825 if (ret == GST_STATE_CHANGE_FAILURE)
1828 gst_object_unref (parent);
1832 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "element has no parent");
1839 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1840 "syncing state failed (%s)",
1841 gst_element_state_change_return_get_name (ret));
1847 static GstStateChangeReturn
1848 gst_element_get_state_func (GstElement * element,
1849 GstState * state, GstState * pending, GstClockTime timeout)
1851 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
1852 GstState old_pending;
1854 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "getting state, timeout %"
1855 GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
1857 GST_OBJECT_LOCK (element);
1858 ret = GST_STATE_RETURN (element);
1859 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "RETURN is %s",
1860 gst_element_state_change_return_get_name (ret));
1862 /* we got an error, report immediatly */
1863 if (ret == GST_STATE_CHANGE_FAILURE)
1866 /* we got no_preroll, report immediatly */
1867 if (ret == GST_STATE_CHANGE_NO_PREROLL)
1870 /* no need to wait async if we are not async */
1871 if (ret != GST_STATE_CHANGE_ASYNC)
1874 old_pending = GST_STATE_PENDING (element);
1875 if (old_pending != GST_STATE_VOID_PENDING) {
1876 GTimeVal *timeval, abstimeout;
1879 if (timeout != GST_CLOCK_TIME_NONE) {
1880 glong add = timeout / 1000;
1885 /* make timeout absolute */
1886 g_get_current_time (&abstimeout);
1887 g_time_val_add (&abstimeout, add);
1888 timeval = &abstimeout;
1892 /* get cookie to dected state change during waiting */
1893 cookie = element->state_cookie;
1895 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1896 "waiting for element to commit state");
1898 /* we have a pending state change, wait for it to complete */
1899 if (!GST_STATE_TIMED_WAIT (element, timeval)) {
1900 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timed out");
1901 /* timeout triggered */
1902 ret = GST_STATE_CHANGE_ASYNC;
1904 if (cookie != element->state_cookie)
1907 /* could be success or failure */
1908 if (old_pending == GST_STATE (element)) {
1909 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got success");
1910 ret = GST_STATE_CHANGE_SUCCESS;
1912 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got failure");
1913 ret = GST_STATE_CHANGE_FAILURE;
1916 /* if nothing is pending anymore we can return SUCCESS */
1917 if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
1918 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "nothing pending");
1919 ret = GST_STATE_CHANGE_SUCCESS;
1925 *state = GST_STATE (element);
1927 *pending = GST_STATE_PENDING (element);
1929 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1930 "state current: %s, pending: %s, result: %s",
1931 gst_element_state_get_name (GST_STATE (element)),
1932 gst_element_state_get_name (GST_STATE_PENDING (element)),
1933 gst_element_state_change_return_get_name (ret));
1934 GST_OBJECT_UNLOCK (element);
1941 *state = GST_STATE_VOID_PENDING;
1943 *pending = GST_STATE_VOID_PENDING;
1945 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "interruped");
1947 GST_OBJECT_UNLOCK (element);
1949 return GST_STATE_CHANGE_FAILURE;
1954 * gst_element_get_state:
1955 * @element: a #GstElement to get the state of.
1956 * @state: a pointer to #GstState to hold the state. Can be %NULL.
1957 * @pending: a pointer to #GstState to hold the pending state.
1959 * @timeout: a #GstClockTime to specify the timeout for an async
1960 * state change or %GST_CLOCK_TIME_NONE for infinite timeout.
1962 * Gets the state of the element.
1964 * For elements that performed an ASYNC state change, as reported by
1965 * gst_element_set_state(), this function will block up to the
1966 * specified timeout value for the state change to complete.
1967 * If the element completes the state change or goes into
1968 * an error, this function returns immediately with a return value of
1969 * %GST_STATE_CHANGE_SUCCESS or %GST_STATE_CHANGE_FAILURE respectively.
1971 * For elements that did not return %GST_STATE_CHANGE_ASYNC, this function
1972 * returns the current and pending state immediately.
1974 * This function returns %GST_STATE_CHANGE_NO_PREROLL if the element
1975 * successfully changed its state but is not able to provide data yet.
1976 * This mostly happens for live sources that only produce data in the PLAYING
1977 * state. While the state change return is equivalent to
1978 * %GST_STATE_CHANGE_SUCCESS, it is returned to the application to signal that
1979 * some sink elements might not be able to complete their state change because
1980 * an element is not producing data to complete the preroll. When setting the
1981 * element to playing, the preroll will complete and playback will start.
1983 * Returns: %GST_STATE_CHANGE_SUCCESS if the element has no more pending state
1984 * and the last state change succeeded, %GST_STATE_CHANGE_ASYNC if the
1985 * element is still performing a state change or
1986 * %GST_STATE_CHANGE_FAILURE if the last state change failed.
1990 GstStateChangeReturn
1991 gst_element_get_state (GstElement * element,
1992 GstState * state, GstState * pending, GstClockTime timeout)
1994 GstElementClass *oclass;
1995 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
1997 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
1999 oclass = GST_ELEMENT_GET_CLASS (element);
2001 if (oclass->get_state)
2002 result = (oclass->get_state) (element, state, pending, timeout);
2008 * gst_element_abort_state:
2009 * @element: a #GstElement to abort the state of.
2011 * Abort the state change of the element. This function is used
2012 * by elements that do asynchronous state changes and find out
2013 * something is wrong.
2015 * This function should be called with the STATE_LOCK held.
2020 gst_element_abort_state (GstElement * element)
2024 #ifndef GST_DISABLE_GST_DEBUG
2028 g_return_if_fail (GST_IS_ELEMENT (element));
2030 GST_OBJECT_LOCK (element);
2031 pending = GST_STATE_PENDING (element);
2033 if (pending == GST_STATE_VOID_PENDING ||
2034 GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2035 goto nothing_aborted;
2037 #ifndef GST_DISABLE_GST_DEBUG
2038 old_state = GST_STATE (element);
2040 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2041 "aborting state from %s to %s", gst_element_state_get_name (old_state),
2042 gst_element_state_get_name (pending));
2046 GST_STATE_RETURN (element) = GST_STATE_CHANGE_FAILURE;
2048 GST_STATE_BROADCAST (element);
2049 GST_OBJECT_UNLOCK (element);
2055 GST_OBJECT_UNLOCK (element);
2061 * gst_element_continue_state:
2062 * @element: a #GstElement to continue the state change of.
2063 * @ret: The previous state return value
2065 * Commit the state change of the element and proceed to the next
2066 * pending state if any. This function is used
2067 * by elements that do asynchronous state changes.
2068 * The core will normally call this method automatically when an
2069 * element returned %GST_STATE_CHANGE_SUCCESS from the state change function.
2071 * If after calling this method the element still has not reached
2072 * the pending state, the next state change is performed.
2074 * This method is used internally and should normally not be called by plugins
2077 * Returns: The result of the commit state change.
2081 GstStateChangeReturn
2082 gst_element_continue_state (GstElement * element, GstStateChangeReturn ret)
2084 GstStateChangeReturn old_ret;
2085 GstState old_state, old_next;
2086 GstState current, next, pending;
2087 GstMessage *message;
2088 GstStateChange transition;
2090 GST_OBJECT_LOCK (element);
2091 old_ret = GST_STATE_RETURN (element);
2092 GST_STATE_RETURN (element) = ret;
2093 pending = GST_STATE_PENDING (element);
2095 /* check if there is something to commit */
2096 if (pending == GST_STATE_VOID_PENDING)
2097 goto nothing_pending;
2099 old_state = GST_STATE (element);
2100 /* this is the state we should go to next */
2101 old_next = GST_STATE_NEXT (element);
2102 /* update current state */
2103 current = GST_STATE (element) = old_next;
2105 /* see if we reached the final state */
2106 if (pending == current)
2109 next = GST_STATE_GET_NEXT (current, pending);
2110 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2112 GST_STATE_NEXT (element) = next;
2114 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2115 GST_OBJECT_UNLOCK (element);
2117 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2118 "committing state from %s to %s, pending %s",
2119 gst_element_state_get_name (old_state),
2120 gst_element_state_get_name (old_next),
2121 gst_element_state_get_name (pending));
2123 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2124 old_state, old_next, pending);
2125 gst_element_post_message (element, message);
2127 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2128 "continue state change %s to %s, final %s",
2129 gst_element_state_get_name (current),
2130 gst_element_state_get_name (next), gst_element_state_get_name (pending));
2132 ret = gst_element_change_state (element, transition);
2138 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "nothing pending");
2139 GST_OBJECT_UNLOCK (element);
2144 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2145 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2147 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2148 "completed state change to %s", gst_element_state_get_name (pending));
2149 GST_OBJECT_UNLOCK (element);
2151 /* don't post silly messages with the same state. This can happen
2152 * when an element state is changed to what it already was. For bins
2153 * this can be the result of a lost state, which we check with the
2154 * previous return value.
2155 * We do signal the cond though as a _get_state() might be blocking
2157 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC) {
2158 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2159 "posting state-changed %s to %s",
2160 gst_element_state_get_name (old_state),
2161 gst_element_state_get_name (old_next));
2163 gst_message_new_state_changed (GST_OBJECT_CAST (element), old_state,
2164 old_next, GST_STATE_VOID_PENDING);
2165 gst_element_post_message (element, message);
2168 GST_STATE_BROADCAST (element);
2175 * gst_element_lost_state:
2176 * @element: a #GstElement the state is lost of
2178 * Brings the element to the lost state. The current state of the
2179 * element is copied to the pending state so that any call to
2180 * gst_element_get_state() will return %GST_STATE_CHANGE_ASYNC.
2182 * An ASYNC_START message is posted with an indication to distribute a new
2183 * base_time to the element.
2184 * If the element was PLAYING, it will go to PAUSED. The element
2185 * will be restored to its PLAYING state by the parent pipeline when it
2188 * This is mostly used for elements that lost their preroll buffer
2189 * in the %GST_STATE_PAUSED or %GST_STATE_PLAYING state after a flush,
2190 * they will go to their pending state again when a new preroll buffer is
2191 * queued. This function can only be called when the element is currently
2192 * not in error or an async state change.
2194 * This function is used internally and should normally not be called from
2195 * plugins or applications.
2200 gst_element_lost_state (GstElement * element)
2202 GstState old_state, new_state;
2203 GstMessage *message;
2205 g_return_if_fail (GST_IS_ELEMENT (element));
2207 GST_OBJECT_LOCK (element);
2208 if (GST_STATE_PENDING (element) != GST_STATE_VOID_PENDING ||
2209 GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2212 old_state = GST_STATE (element);
2214 /* when we were PLAYING, the new state is PAUSED. We will also not
2215 * automatically go to PLAYING but let the parent bin(s) set us to PLAYING
2216 * when we preroll. */
2217 if (old_state > GST_STATE_PAUSED)
2218 new_state = GST_STATE_PAUSED;
2220 new_state = old_state;
2222 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2223 "lost state of %s to %s", gst_element_state_get_name (old_state),
2224 gst_element_state_get_name (new_state));
2226 GST_STATE (element) = new_state;
2227 GST_STATE_NEXT (element) = new_state;
2228 GST_STATE_PENDING (element) = new_state;
2229 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2230 GST_OBJECT_UNLOCK (element);
2232 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2233 new_state, new_state, new_state);
2234 gst_element_post_message (element, message);
2236 message = gst_message_new_async_start (GST_OBJECT_CAST (element), TRUE);
2237 gst_element_post_message (element, message);
2243 GST_OBJECT_UNLOCK (element);
2249 * gst_element_set_state:
2250 * @element: a #GstElement to change state of.
2251 * @state: the element's new #GstState.
2253 * Sets the state of the element. This function will try to set the
2254 * requested state by going through all the intermediary states and calling
2255 * the class's state change function for each.
2257 * This function can return #GST_STATE_CHANGE_ASYNC, in which case the
2258 * element will perform the remainder of the state change asynchronously in
2260 * An application can use gst_element_get_state() to wait for the completion
2261 * of the state change or it can wait for a state change message on the bus.
2263 * Returns: Result of the state change using #GstStateChangeReturn.
2267 GstStateChangeReturn
2268 gst_element_set_state (GstElement * element, GstState state)
2270 GstElementClass *oclass;
2271 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2273 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2275 oclass = GST_ELEMENT_GET_CLASS (element);
2277 if (oclass->set_state)
2278 result = (oclass->set_state) (element, state);
2284 * default set state function, calculates the next state based
2285 * on current state and calls the change_state function
2287 static GstStateChangeReturn
2288 gst_element_set_state_func (GstElement * element, GstState state)
2290 GstState current, next, old_pending;
2291 GstStateChangeReturn ret;
2292 GstStateChange transition;
2293 GstStateChangeReturn old_ret;
2295 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2297 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "set_state to %s",
2298 gst_element_state_get_name (state));
2300 /* state lock is taken to protect the set_state() and get_state()
2301 * procedures, it does not lock any variables. */
2302 GST_STATE_LOCK (element);
2304 /* now calculate how to get to the new state */
2305 GST_OBJECT_LOCK (element);
2306 old_ret = GST_STATE_RETURN (element);
2307 /* previous state change returned an error, remove all pending
2308 * and next states */
2309 if (old_ret == GST_STATE_CHANGE_FAILURE) {
2310 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2311 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2312 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
2315 current = GST_STATE (element);
2316 next = GST_STATE_NEXT (element);
2317 old_pending = GST_STATE_PENDING (element);
2319 /* this is the (new) state we should go to. TARGET is the last state we set on
2321 if (state != GST_STATE_TARGET (element)) {
2322 GST_STATE_TARGET (element) = state;
2323 /* increment state cookie so that we can track each state change. We only do
2324 * this if this is actually a new state change. */
2325 element->state_cookie++;
2327 GST_STATE_PENDING (element) = state;
2329 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2330 "current %s, old_pending %s, next %s, old return %s",
2331 gst_element_state_get_name (current),
2332 gst_element_state_get_name (old_pending),
2333 gst_element_state_get_name (next),
2334 gst_element_state_change_return_get_name (old_ret));
2336 /* if the element was busy doing a state change, we just update the
2337 * target state, it'll get to it async then. */
2338 if (old_pending != GST_STATE_VOID_PENDING) {
2339 /* upwards state change will happen ASYNC */
2340 if (old_pending <= state)
2342 /* element is going to this state already */
2343 else if (next == state)
2345 /* element was performing an ASYNC upward state change and
2346 * we request to go downward again. Start from the next pending
2348 else if (next > state
2349 && GST_STATE_RETURN (element) == GST_STATE_CHANGE_ASYNC) {
2353 next = GST_STATE_GET_NEXT (current, state);
2354 /* now we store the next state */
2355 GST_STATE_NEXT (element) = next;
2356 /* mark busy, we need to check that there is actually a state change
2357 * to be done else we could accidentally override SUCCESS/NO_PREROLL and
2358 * the default element change_state function has no way to know what the
2359 * old value was... could consider this a FIXME...*/
2360 if (current != next)
2361 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2363 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2365 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2366 "%s: setting state from %s to %s",
2367 (next != state ? "intermediate" : "final"),
2368 gst_element_state_get_name (current), gst_element_state_get_name (next));
2370 /* now signal any waiters, they will error since the cookie was incremented */
2371 GST_STATE_BROADCAST (element);
2373 GST_OBJECT_UNLOCK (element);
2375 ret = gst_element_change_state (element, transition);
2377 GST_STATE_UNLOCK (element);
2379 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "returned %s",
2380 gst_element_state_change_return_get_name (ret));
2386 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2387 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2388 "element was busy with async state change");
2389 GST_OBJECT_UNLOCK (element);
2391 GST_STATE_UNLOCK (element);
2393 return GST_STATE_CHANGE_ASYNC;
2398 * gst_element_change_state:
2399 * @element: a #GstElement
2400 * @transition: the requested transition
2402 * Perform @transition on @element.
2404 * This function must be called with STATE_LOCK held and is mainly used
2407 * Returns: the #GstStateChangeReturn of the state transition.
2409 GstStateChangeReturn
2410 gst_element_change_state (GstElement * element, GstStateChange transition)
2412 GstElementClass *oclass;
2413 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2417 oclass = GST_ELEMENT_GET_CLASS (element);
2419 /* start with the current state. */
2420 current = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2421 next = GST_STATE_TRANSITION_NEXT (transition);
2423 /* call the state change function so it can set the state */
2424 if (oclass->change_state)
2425 ret = (oclass->change_state) (element, transition);
2427 ret = GST_STATE_CHANGE_FAILURE;
2430 case GST_STATE_CHANGE_FAILURE:
2431 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2432 "have FAILURE change_state return");
2433 /* state change failure */
2434 gst_element_abort_state (element);
2436 case GST_STATE_CHANGE_ASYNC:
2440 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2441 "element will change state ASYNC");
2443 target = GST_STATE_TARGET (element);
2445 if (target > GST_STATE_READY)
2448 /* else we just continue the state change downwards */
2449 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2450 "forcing commit state %s <= %s",
2451 gst_element_state_get_name (target),
2452 gst_element_state_get_name (GST_STATE_READY));
2454 ret = gst_element_continue_state (element, GST_STATE_CHANGE_SUCCESS);
2457 case GST_STATE_CHANGE_SUCCESS:
2458 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2459 "element changed state SUCCESS");
2460 /* we can commit the state now which will proceeed to
2462 ret = gst_element_continue_state (element, ret);
2464 case GST_STATE_CHANGE_NO_PREROLL:
2465 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2466 "element changed state NO_PREROLL");
2467 /* we can commit the state now which will proceeed to
2469 ret = gst_element_continue_state (element, ret);
2472 goto invalid_return;
2475 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit state change %d", ret);
2480 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit async state change %d",
2488 GST_OBJECT_LOCK (element);
2489 /* somebody added a GST_STATE_ and forgot to do stuff here ! */
2490 g_critical ("%s: unknown return value %d from a state change function",
2491 GST_ELEMENT_NAME (element), ret);
2493 /* we are in error now */
2494 ret = GST_STATE_CHANGE_FAILURE;
2495 GST_STATE_RETURN (element) = ret;
2496 GST_OBJECT_UNLOCK (element);
2502 /* gst_iterator_fold functions for pads_activate
2503 * Note how we don't stop the iterator when we fail an activation. This is
2504 * probably a FIXME since when one pad activation fails, we don't want to
2505 * continue our state change. */
2507 activate_pads (GstPad * pad, GValue * ret, gboolean * active)
2509 if (!gst_pad_set_active (pad, *active))
2510 g_value_set_boolean (ret, FALSE);
2512 /* unref the object that was reffed for us by _fold */
2513 gst_object_unref (pad);
2517 /* set the caps on the pad to NULL */
2519 clear_caps (GstPad * pad, GValue * ret, gboolean * active)
2521 gst_pad_set_caps (pad, NULL);
2522 gst_object_unref (pad);
2526 /* returns false on error or early cutout (will never happen because the fold
2527 * function always returns TRUE, see FIXME above) of the fold, true if all
2528 * pads in @iter were (de)activated successfully. */
2530 iterator_activate_fold_with_resync (GstIterator * iter,
2531 GstIteratorFoldFunction func, gpointer user_data)
2533 GstIteratorResult ires;
2536 /* no need to unset this later, it's just a boolean */
2537 g_value_init (&ret, G_TYPE_BOOLEAN);
2538 g_value_set_boolean (&ret, TRUE);
2541 ires = gst_iterator_fold (iter, func, &ret, user_data);
2543 case GST_ITERATOR_RESYNC:
2544 /* need to reset the result again */
2545 g_value_set_boolean (&ret, TRUE);
2546 gst_iterator_resync (iter);
2548 case GST_ITERATOR_DONE:
2549 /* all pads iterated, return collected value */
2552 /* iterator returned _ERROR or premature end with _OK,
2553 * mark an error and exit */
2554 g_value_set_boolean (&ret, FALSE);
2559 /* return collected value */
2560 return g_value_get_boolean (&ret);
2563 /* is called with STATE_LOCK
2565 * Pads are activated from source pads to sinkpads.
2568 gst_element_pads_activate (GstElement * element, gboolean active)
2573 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2574 "pads_activate with active %d", active);
2576 iter = gst_element_iterate_src_pads (element);
2578 iterator_activate_fold_with_resync (iter,
2579 (GstIteratorFoldFunction) activate_pads, &active);
2580 gst_iterator_free (iter);
2581 if (G_UNLIKELY (!res))
2584 iter = gst_element_iterate_sink_pads (element);
2586 iterator_activate_fold_with_resync (iter,
2587 (GstIteratorFoldFunction) activate_pads, &active);
2588 gst_iterator_free (iter);
2589 if (G_UNLIKELY (!res))
2593 /* clear the caps on all pads, this should never fail */
2594 iter = gst_element_iterate_pads (element);
2596 iterator_activate_fold_with_resync (iter,
2597 (GstIteratorFoldFunction) clear_caps, &active);
2598 gst_iterator_free (iter);
2599 if (G_UNLIKELY (!res))
2603 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2604 "pads_activate successful");
2611 GST_DEBUG_OBJECT (element, "source pads_activate failed");
2616 GST_DEBUG_OBJECT (element, "sink pads_activate failed");
2621 GST_DEBUG_OBJECT (element, "failed to clear caps on pads");
2626 /* is called with STATE_LOCK */
2627 static GstStateChangeReturn
2628 gst_element_change_state_func (GstElement * element, GstStateChange transition)
2630 GstState state, next;
2631 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
2633 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2635 state = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2636 next = GST_STATE_TRANSITION_NEXT (transition);
2638 /* if the element already is in the given state, we just return success */
2639 if (next == GST_STATE_VOID_PENDING || state == next)
2642 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element,
2643 "default handler tries setting state from %s to %s (%04x)",
2644 gst_element_state_get_name (state),
2645 gst_element_state_get_name (next), transition);
2647 switch (transition) {
2648 case GST_STATE_CHANGE_NULL_TO_READY:
2650 case GST_STATE_CHANGE_READY_TO_PAUSED:
2651 if (!gst_element_pads_activate (element, TRUE)) {
2652 result = GST_STATE_CHANGE_FAILURE;
2655 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2657 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2659 case GST_STATE_CHANGE_PAUSED_TO_READY:
2660 case GST_STATE_CHANGE_READY_TO_NULL:
2661 /* deactivate pads in both cases, since they are activated on
2662 ready->paused but the element might not have made it to paused */
2663 if (!gst_element_pads_activate (element, FALSE)) {
2664 result = GST_STATE_CHANGE_FAILURE;
2666 gst_element_set_base_time (element, 0);
2670 /* this will catch real but unhandled state changes;
2671 * can only be caused by:
2672 * - a new state was added
2673 * - somehow the element was asked to jump across an intermediate state
2675 g_warning ("Unhandled state change from %s to %s",
2676 gst_element_state_get_name (state),
2677 gst_element_state_get_name (next));
2684 GST_OBJECT_LOCK (element);
2685 result = GST_STATE_RETURN (element);
2686 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2687 "element is already in the %s state",
2688 gst_element_state_get_name (state));
2689 GST_OBJECT_UNLOCK (element);
2696 * gst_element_get_factory:
2697 * @element: a #GstElement to request the element factory of.
2699 * Retrieves the factory that was used to create this element.
2701 * Returns: the #GstElementFactory used for creating this element.
2702 * no refcounting is needed.
2705 gst_element_get_factory (GstElement * element)
2707 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
2709 return GST_ELEMENT_GET_CLASS (element)->elementfactory;
2713 gst_element_dispose (GObject * object)
2715 GstElement *element = GST_ELEMENT (object);
2719 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "dispose");
2721 if (GST_STATE (element) != GST_STATE_NULL)
2724 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2725 "removing %d pads", g_list_length (element->pads));
2726 /* first we break all our links with the outside */
2727 while (element->pads && element->pads->data) {
2728 /* don't call _remove_pad with NULL */
2729 gst_element_remove_pad (element, GST_PAD_CAST (element->pads->data));
2731 if (G_UNLIKELY (element->pads != 0)) {
2732 g_critical ("could not remove pads from element %s",
2733 GST_STR_NULL (GST_OBJECT_NAME (object)));
2736 GST_OBJECT_LOCK (element);
2737 clock_p = &element->clock;
2738 bus_p = &element->bus;
2739 gst_object_replace ((GstObject **) clock_p, NULL);
2740 gst_object_replace ((GstObject **) bus_p, NULL);
2741 GST_OBJECT_UNLOCK (element);
2743 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "parent class dispose");
2745 G_OBJECT_CLASS (parent_class)->dispose (object);
2753 ("\nTrying to dispose element %s, but it is not in the NULL state.\n"
2754 "You need to explicitly set elements to the NULL state before\n"
2755 "dropping the final reference, to allow them to clean up.\n",
2756 GST_OBJECT_NAME (element));
2762 gst_element_finalize (GObject * object)
2764 GstElement *element = GST_ELEMENT (object);
2766 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize");
2768 GST_STATE_LOCK (element);
2769 if (element->state_cond)
2770 g_cond_free (element->state_cond);
2771 element->state_cond = NULL;
2772 GST_STATE_UNLOCK (element);
2773 g_static_rec_mutex_free (element->state_lock);
2774 g_free (element->state_lock);
2775 element->state_lock = NULL;
2777 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize parent");
2779 G_OBJECT_CLASS (parent_class)->finalize (object);
2782 #ifndef GST_DISABLE_LOADSAVE
2784 * gst_element_save_thyself:
2785 * @element: a #GstElement to save.
2786 * @parent: the xml parent node.
2788 * Saves the element as part of the given XML structure.
2790 * Returns: the new #xmlNodePtr.
2793 gst_element_save_thyself (GstObject * object, xmlNodePtr parent)
2796 GstElementClass *oclass;
2797 GParamSpec **specs, *spec;
2800 GValue value = { 0, };
2801 GstElement *element;
2803 g_return_val_if_fail (GST_IS_ELEMENT (object), parent);
2805 element = GST_ELEMENT (object);
2807 oclass = GST_ELEMENT_GET_CLASS (element);
2809 xmlNewChild (parent, NULL, (xmlChar *) "name",
2810 (xmlChar *) GST_ELEMENT_NAME (element));
2812 if (oclass->elementfactory != NULL) {
2813 GstElementFactory *factory = (GstElementFactory *) oclass->elementfactory;
2815 xmlNewChild (parent, NULL, (xmlChar *) "type",
2816 (xmlChar *) GST_PLUGIN_FEATURE (factory)->name);
2820 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &nspecs);
2822 for (i = 0; i < nspecs; i++) {
2824 if (spec->flags & G_PARAM_READABLE) {
2828 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
2830 g_object_get_property (G_OBJECT (element), spec->name, &value);
2831 param = xmlNewChild (parent, NULL, (xmlChar *) "param", NULL);
2832 xmlNewChild (param, NULL, (xmlChar *) "name", (xmlChar *) spec->name);
2834 if (G_IS_PARAM_SPEC_STRING (spec))
2835 contents = g_value_dup_string (&value);
2836 else if (G_IS_PARAM_SPEC_ENUM (spec))
2837 contents = g_strdup_printf ("%d", g_value_get_enum (&value));
2838 else if (G_IS_PARAM_SPEC_INT64 (spec))
2839 contents = g_strdup_printf ("%" G_GINT64_FORMAT,
2840 g_value_get_int64 (&value));
2842 contents = g_strdup_value_contents (&value);
2844 xmlNewChild (param, NULL, (xmlChar *) "value", (xmlChar *) contents);
2847 g_value_unset (&value);
2853 pads = GST_ELEMENT_PADS (element);
2856 GstPad *pad = GST_PAD_CAST (pads->data);
2858 /* figure out if it's a direct pad or a ghostpad */
2859 if (GST_ELEMENT (GST_OBJECT_PARENT (pad)) == element) {
2860 xmlNodePtr padtag = xmlNewChild (parent, NULL, (xmlChar *) "pad", NULL);
2862 gst_object_save_thyself (GST_OBJECT_CAST (pad), padtag);
2864 pads = g_list_next (pads);
2871 gst_element_restore_thyself (GstObject * object, xmlNodePtr self)
2873 xmlNodePtr children;
2874 GstElement *element;
2876 gchar *value = NULL;
2878 element = GST_ELEMENT (object);
2879 g_return_if_fail (element != NULL);
2882 children = self->xmlChildrenNode;
2884 if (!strcmp ((char *) children->name, "param")) {
2885 xmlNodePtr child = children->xmlChildrenNode;
2888 if (!strcmp ((char *) child->name, "name")) {
2889 name = (gchar *) xmlNodeGetContent (child);
2890 } else if (!strcmp ((char *) child->name, "value")) {
2891 value = (gchar *) xmlNodeGetContent (child);
2893 child = child->next;
2895 /* FIXME: can this just be g_object_set ? */
2896 gst_util_set_object_arg (G_OBJECT (element), name, value);
2897 /* g_object_set (G_OBJECT (element), name, value, NULL); */
2901 children = children->next;
2905 children = self->xmlChildrenNode;
2907 if (!strcmp ((char *) children->name, "pad")) {
2908 gst_pad_load_and_link (children, GST_OBJECT_CAST (element));
2910 children = children->next;
2913 if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
2914 (GST_OBJECT_CLASS (parent_class)->restore_thyself) (object, self);
2916 #endif /* GST_DISABLE_LOADSAVE */
2919 gst_element_set_bus_func (GstElement * element, GstBus * bus)
2923 g_return_if_fail (GST_IS_ELEMENT (element));
2925 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element, "setting bus to %p", bus);
2927 GST_OBJECT_LOCK (element);
2928 bus_p = &GST_ELEMENT_BUS (element);
2929 gst_object_replace ((GstObject **) bus_p, GST_OBJECT_CAST (bus));
2930 GST_OBJECT_UNLOCK (element);
2934 * gst_element_set_bus:
2935 * @element: a #GstElement to set the bus of.
2936 * @bus: the #GstBus to set.
2938 * Sets the bus of the element. Increases the refcount on the bus.
2939 * For internal use only, unless you're testing elements.
2944 gst_element_set_bus (GstElement * element, GstBus * bus)
2946 GstElementClass *oclass;
2948 g_return_if_fail (GST_IS_ELEMENT (element));
2950 oclass = GST_ELEMENT_GET_CLASS (element);
2952 if (oclass->set_bus)
2953 oclass->set_bus (element, bus);
2957 * gst_element_get_bus:
2958 * @element: a #GstElement to get the bus of.
2960 * Returns the bus of the element. Note that only a #GstPipeline will provide a
2961 * bus for the application.
2963 * Returns: the element's #GstBus. unref after usage.
2968 gst_element_get_bus (GstElement * element)
2970 GstBus *result = NULL;
2972 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
2974 GST_OBJECT_LOCK (element);
2975 if ((result = GST_ELEMENT_BUS (element)))
2976 gst_object_ref (result);
2977 GST_OBJECT_UNLOCK (element);
2979 GST_CAT_DEBUG_OBJECT (GST_CAT_BUS, element, "got bus %" GST_PTR_FORMAT,