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.
24 * @short_description: Abstract base class for all pipeline elements
25 * @see_also: #GstElementFactory, #GstPad
27 * GstElement is the base class needed to construct an element that can be
28 * used in a GStreamer pipeline. As such, it is not a functional entity, and
29 * cannot do anything when placed in a pipeline.
31 * The name of a GstElement can be get with gst_element_get_name() and set with
32 * gst_element_set_name(). For speed, GST_ELEMENT_NAME() can be used in the
33 * core. Do not use this in plug-ins or applications in order to retain ABI
36 * All elements have pads (of the type #GstPad). These pads link to pads on
37 * other elements. Buffers flow between these linked pads.
38 * A GstElement has a GList of #GstPad structures for all their input (or sink)
39 * and output (or source) pads.
40 * Core and plug-in writers can add and remove pads with gst_element_add_pad()
41 * and gst_element_remove_pad().
43 * A pad of an element can be retrieved by name with gst_element_get_pad().
44 * An iterator of all pads can be retrieved with gst_element_iterate_pads().
46 * Elements can be linked through their pads.
47 * If the link is straightforward, use the gst_element_link()
48 * convenience function to link two elements, or gst_element_link_many()
49 * for more elements in a row.
50 * Use gst_element_link_filtered() to link two elements constrained by
51 * a specified set of #GstCaps.
52 * For finer control, use gst_element_link_pads() and
53 * gst_element_link_pads_filtered() to specify the pads to link on
54 * each element by name.
56 * Each element has a state (see #GstState). You can get and set the state
57 * of an element with gst_element_get_state() and gst_element_set_state().
58 * You can wait for an element to change it's state with gst_element_wait_state_change().
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 gst_element_provide_clock()
66 * method one can retrieve the clock provided by such an element.
67 * Not all elements require a clock to operate correctly. If
68 * gst_element_requires_clock() returns TRUE, a clock should be set on the element
69 * with gst_element_set_clock().
70 * Note that clock slection and distribution is normally handled by the toplevel
71 * GstPipeline so the clock functions are only to be used in very specific situations.
73 #include "gst_private.h"
76 #include <gobject/gvaluecollector.h>
78 #include "gstelement.h"
80 #include "gstmarshal.h"
85 #include "gst-i18n-lib.h"
87 /* Element signals and args */
104 extern void __gst_element_details_clear (GstElementDetails * dp);
105 extern void __gst_element_details_copy (GstElementDetails * dest,
106 const GstElementDetails * src);
108 static void gst_element_class_init (GstElementClass * klass);
109 static void gst_element_init (GstElement * element);
110 static void gst_element_base_class_init (gpointer g_class);
111 static void gst_element_base_class_finalize (gpointer g_class);
113 static void gst_element_dispose (GObject * object);
114 static void gst_element_finalize (GObject * object);
116 static GstStateChangeReturn gst_element_change_state (GstElement * element,
117 GstStateChange transition);
118 static GstStateChangeReturn gst_element_get_state_func (GstElement * element,
119 GstState * state, GstState * pending, GTimeVal * timeout);
120 static void gst_element_set_bus_func (GstElement * element, GstBus * bus);
122 #ifndef GST_DISABLE_LOADSAVE
123 static xmlNodePtr gst_element_save_thyself (GstObject * object,
125 static void gst_element_restore_thyself (GstObject * parent, xmlNodePtr self);
128 GType _gst_element_type = 0;
130 static GstObjectClass *parent_class = NULL;
131 static guint gst_element_signals[LAST_SIGNAL] = { 0 };
134 gst_element_get_type (void)
136 if (!_gst_element_type) {
137 static const GTypeInfo element_info = {
138 sizeof (GstElementClass),
139 gst_element_base_class_init,
140 gst_element_base_class_finalize,
141 (GClassInitFunc) gst_element_class_init,
146 (GInstanceInitFunc) gst_element_init,
150 _gst_element_type = g_type_register_static (GST_TYPE_OBJECT, "GstElement",
151 &element_info, G_TYPE_FLAG_ABSTRACT);
153 return _gst_element_type;
157 gst_element_class_init (GstElementClass * klass)
159 GObjectClass *gobject_class;
160 GstObjectClass *gstobject_class;
162 gobject_class = (GObjectClass *) klass;
163 gstobject_class = (GstObjectClass *) klass;
165 parent_class = g_type_class_ref (GST_TYPE_OBJECT);
168 * GstElement::pad-added:
169 * @gstelement: the object which received the signal
170 * @new_pad: the pad that has been added
172 * a new #GstPad has been added to the element.
174 gst_element_signals[NEW_PAD] =
175 g_signal_new ("pad-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
176 G_STRUCT_OFFSET (GstElementClass, pad_added), NULL, NULL,
177 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
179 * GstElement::pad-removed:
180 * @gstelement: the object which received the signal
181 * @old_pad: the pad that has been removed
183 * a #GstPad has been removed from the element
185 gst_element_signals[PAD_REMOVED] =
186 g_signal_new ("pad-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
187 G_STRUCT_OFFSET (GstElementClass, pad_removed), NULL, NULL,
188 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
190 * GstElement::no-more-pads:
191 * @gstelement: the object which received the signal
193 * This signals that the element will not generate more dynamic pads.
195 gst_element_signals[NO_MORE_PADS] =
196 g_signal_new ("no-more-pads", G_TYPE_FROM_CLASS (klass),
197 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstElementClass, no_more_pads), NULL,
198 NULL, gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
200 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_element_dispose);
201 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_element_finalize);
203 #ifndef GST_DISABLE_LOADSAVE
204 gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_element_save_thyself);
205 gstobject_class->restore_thyself =
206 GST_DEBUG_FUNCPTR (gst_element_restore_thyself);
209 klass->change_state = GST_DEBUG_FUNCPTR (gst_element_change_state);
210 klass->get_state = GST_DEBUG_FUNCPTR (gst_element_get_state_func);
211 klass->set_bus = GST_DEBUG_FUNCPTR (gst_element_set_bus_func);
212 klass->numpadtemplates = 0;
214 klass->elementfactory = NULL;
218 gst_element_base_class_init (gpointer g_class)
220 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
222 memset (&element_class->details, 0, sizeof (GstElementDetails));
223 element_class->padtemplates = NULL;
227 gst_element_base_class_finalize (gpointer g_class)
229 GstElementClass *klass = GST_ELEMENT_CLASS (g_class);
231 g_list_foreach (klass->padtemplates, (GFunc) gst_object_unref, NULL);
232 g_list_free (klass->padtemplates);
233 __gst_element_details_clear (&klass->details);
237 gst_element_init (GstElement * element)
239 element->current_state = GST_STATE_NULL;
240 element->pending_state = GST_STATE_VOID_PENDING;
241 element->state_lock = g_mutex_new ();
242 element->state_cond = g_cond_new ();
246 * gst_element_default_error:
247 * @object: a #GObject that signalled the error.
248 * @orig: the #GstObject that initiated the error.
249 * @error: the GError.
250 * @debug: an additional debug information string, or NULL.
252 * A default error signal callback to attach to an element.
253 * The user data passed to the g_signal_connect is ignored.
255 * The default handler will simply print the error string using g_print.
260 gst_element_default_error (GObject * object, GstObject * source, GError * error,
263 gchar *name = gst_object_get_path_string (source);
265 g_print (_("ERROR: from element %s: %s\n"), name, error->message);
267 g_print (_("Additional debug info:\n%s\n"), debug);
273 * gst_element_release_request_pad:
274 * @element: a #GstElement to release the request pad of.
275 * @pad: the #GstPad to release.
277 * Makes the element free the previously requested pad as obtained
278 * with gst_element_get_request_pad().
283 gst_element_release_request_pad (GstElement * element, GstPad * pad)
285 GstElementClass *oclass;
287 g_return_if_fail (GST_IS_ELEMENT (element));
288 g_return_if_fail (GST_IS_PAD (pad));
290 oclass = GST_ELEMENT_GET_CLASS (element);
292 if (oclass->release_pad)
293 (oclass->release_pad) (element, pad);
295 gst_element_remove_pad (element, pad);
299 * gst_element_requires_clock:
300 * @element: a #GstElement to query
302 * Query if the element requires a clock.
304 * Returns: TRUE if the element requires a clock
309 gst_element_requires_clock (GstElement * element)
311 gboolean result = FALSE;
313 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
315 result = (GST_ELEMENT_GET_CLASS (element)->set_clock != NULL);
321 * gst_element_provides_clock:
322 * @element: a #GstElement to query
324 * Query if the element provides a clock.
326 * Returns: TRUE if the element provides a clock
331 gst_element_provides_clock (GstElement * element)
333 gboolean result = FALSE;
335 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
337 result = (GST_ELEMENT_GET_CLASS (element)->provide_clock != NULL);
343 * gst_element_provide_clock:
344 * @element: a #GstElement to query
346 * Get the clock provided by the given element.
348 * Returns: the GstClock provided by the element or NULL
349 * if no clock could be provided.
354 gst_element_provide_clock (GstElement * element)
356 GstClock *result = NULL;
357 GstElementClass *oclass;
359 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
361 oclass = GST_ELEMENT_GET_CLASS (element);
363 if (oclass->provide_clock)
364 result = oclass->provide_clock (element);
370 * gst_element_set_clock:
371 * @element: a #GstElement to set the clock for.
372 * @clock: the #GstClock to set for the element.
374 * Sets the clock for the element. This function increases the
375 * refcount on the clock. Any previously set clock on the object
381 gst_element_set_clock (GstElement * element, GstClock * clock)
383 GstElementClass *oclass;
385 g_return_if_fail (GST_IS_ELEMENT (element));
387 oclass = GST_ELEMENT_GET_CLASS (element);
389 if (oclass->set_clock)
390 oclass->set_clock (element, clock);
393 gst_object_replace ((GstObject **) & element->clock, (GstObject *) clock);
394 GST_UNLOCK (element);
398 * gst_element_get_clock:
399 * @element: a #GstElement to get the clock of.
401 * Gets the currently configured clock of the element.
403 * Returns: the #GstClock of the element. unref after usage.
408 gst_element_get_clock (GstElement * element)
412 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
415 if ((result = element->clock))
416 gst_object_ref (result);
417 GST_UNLOCK (element);
423 * gst_element_set_base_time:
424 * @element: a #GstElement.
425 * @time: the base time to set.
427 * Set the base time of an element. See @gst_element_get_base_time().
432 gst_element_set_base_time (GstElement * element, GstClockTime time)
434 g_return_if_fail (GST_IS_ELEMENT (element));
437 element->base_time = time;
438 GST_UNLOCK (element);
442 * gst_element_get_base_time:
443 * @element: a #GstElement.
445 * Returns the base time of the element. The base time is the
446 * absolute time of the clock when this element was last put to
447 * PLAYING. Substracting the base time from the clock time gives
448 * the stream time of the element.
450 * Returns: the base time of the element.
455 gst_element_get_base_time (GstElement * element)
459 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
462 result = element->base_time;
463 GST_UNLOCK (element);
468 #ifndef GST_DISABLE_INDEX
470 * gst_element_is_indexable:
471 * @element: a #GstElement.
473 * Queries if the element can be indexed.
475 * Returns: TRUE if the element can be indexed.
480 gst_element_is_indexable (GstElement * element)
482 gboolean result = FALSE;
484 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
486 result = (GST_ELEMENT_GET_CLASS (element)->set_index != NULL);
492 * gst_element_set_index:
493 * @element: a #GstElement.
494 * @index: a #GstIndex.
496 * Set the specified GstIndex on the element.
501 gst_element_set_index (GstElement * element, GstIndex * index)
503 GstElementClass *oclass;
505 g_return_if_fail (GST_IS_ELEMENT (element));
506 g_return_if_fail (GST_IS_INDEX (index));
508 oclass = GST_ELEMENT_GET_CLASS (element);
510 if (oclass->set_index)
511 oclass->set_index (element, index);
515 * gst_element_get_index:
516 * @element: a #GstElement.
518 * Gets the index from the element.
520 * Returns: a #GstIndex or NULL when no index was set on the
521 * element. unref after usage.
526 gst_element_get_index (GstElement * element)
528 GstElementClass *oclass;
530 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
532 oclass = GST_ELEMENT_GET_CLASS (element);
534 if (oclass->get_index)
535 return oclass->get_index (element);
542 * gst_element_add_pad:
543 * @element: a #GstElement to add the pad to.
544 * @pad: the #GstPad to add to the element.
546 * Adds a pad (link point) to @element. @pad's parent will be set to @element;
547 * see gst_object_set_parent() for refcounting information.
549 * Pads are not automatically activated so elements should perform the needed
550 * steps to activate the pad.
552 * The pad and the element should be unlocked when calling this function.
554 * Returns: TRUE if the pad could be added. This function can fail when
555 * passing bad arguments or when a pad with the same name already existed.
560 gst_element_add_pad (GstElement * element, GstPad * pad)
564 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
565 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
567 /* locking pad to look at the name */
569 pad_name = g_strdup (GST_PAD_NAME (pad));
570 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "adding pad '%s'",
571 GST_STR_NULL (pad_name));
574 /* then check to see if there's already a pad by that name here */
576 if (G_UNLIKELY (!gst_object_check_uniqueness (element->pads, pad_name)))
579 /* try to set the pad's parent */
580 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (pad),
581 GST_OBJECT_CAST (element))))
586 /* add it to the list */
587 switch (gst_pad_get_direction (pad)) {
589 element->srcpads = g_list_prepend (element->srcpads, pad);
590 element->numsrcpads++;
593 element->sinkpads = g_list_prepend (element->sinkpads, pad);
594 element->numsinkpads++;
599 element->pads = g_list_prepend (element->pads, pad);
601 element->pads_cookie++;
602 GST_UNLOCK (element);
604 /* emit the NEW_PAD signal */
605 g_signal_emit (G_OBJECT (element), gst_element_signals[NEW_PAD], 0, pad);
612 g_critical ("Padname %s is not unique in element %s, not adding",
613 pad_name, GST_ELEMENT_NAME (element));
614 GST_UNLOCK (element);
621 ("Pad %s already has parent when trying to add to element %s",
622 pad_name, GST_ELEMENT_NAME (element));
623 GST_UNLOCK (element);
631 ("Trying to add pad %s to element %s, but it has no direction",
632 GST_OBJECT_NAME (pad), GST_ELEMENT_NAME (element));
634 GST_UNLOCK (element);
640 * gst_element_remove_pad:
641 * @element: a #GstElement to remove pad from.
642 * @pad: the #GstPad to remove from the element.
644 * Removes @pad from @element. @pad will be destroyed if it has not been
645 * referenced elsewhere.
647 * Returns: TRUE if the pad could be removed. Can return FALSE if the
648 * pad is not belonging to the provided element or when wrong parameters
649 * are passed to this function.
654 gst_element_remove_pad (GstElement * element, GstPad * pad)
658 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
659 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
661 /* locking pad to look at the name and parent */
663 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "removing pad '%s'",
664 GST_STR_NULL (GST_PAD_NAME (pad)));
666 if (G_UNLIKELY (GST_PAD_PARENT (pad) != element))
671 if ((peer = gst_pad_get_peer (pad))) {
672 /* window for MT unsafeness, someone else could unlink here
673 * and then we call unlink with wrong pads. The unlink
674 * function would catch this and safely return failed. */
675 if (GST_PAD_IS_SRC (pad))
676 gst_pad_unlink (pad, peer);
678 gst_pad_unlink (peer, pad);
680 gst_object_unref (peer);
684 /* remove it from the list */
685 switch (gst_pad_get_direction (pad)) {
687 element->srcpads = g_list_remove (element->srcpads, pad);
688 element->numsrcpads--;
691 element->sinkpads = g_list_remove (element->sinkpads, pad);
692 element->numsinkpads--;
695 g_critical ("Removing pad without direction???");
698 element->pads = g_list_remove (element->pads, pad);
700 element->pads_cookie++;
701 GST_UNLOCK (element);
703 g_signal_emit (G_OBJECT (element), gst_element_signals[PAD_REMOVED], 0, pad);
705 gst_object_unparent (GST_OBJECT (pad));
711 /* FIXME, locking order? */
713 g_critical ("Padname %s:%s does not belong to element %s when removing",
714 GST_ELEMENT_NAME (GST_PAD_PARENT (pad)), GST_PAD_NAME (pad),
715 GST_ELEMENT_NAME (element));
716 GST_UNLOCK (element);
723 * gst_element_no_more_pads:
724 * @element: a #GstElement
726 * Use this function to signal that the element does not expect any more pads
727 * to show up in the current pipeline. This function should be called whenever
728 * pads have been added by the element itself. Elements with GST_PAD_SOMETIMES
729 * pad templates use this in combination with autopluggers to figure out that
730 * the element is done initializing its pads.
735 gst_element_no_more_pads (GstElement * element)
737 g_return_if_fail (GST_IS_ELEMENT (element));
739 g_signal_emit (element, gst_element_signals[NO_MORE_PADS], 0);
743 pad_compare_name (GstPad * pad1, const gchar * name)
748 result = strcmp (GST_PAD_NAME (pad1), name);
755 * gst_element_get_static_pad:
756 * @element: a #GstElement to find a static pad of.
757 * @name: the name of the static #GstPad to retrieve.
759 * Retrieves a pad from @element by name. This version only retrieves
760 * already-existing (i.e. 'static') pads.
762 * Returns: the requested #GstPad if found, otherwise NULL. unref after
768 gst_element_get_static_pad (GstElement * element, const gchar * name)
771 GstPad *result = NULL;
773 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
774 g_return_val_if_fail (name != NULL, NULL);
778 g_list_find_custom (element->pads, name, (GCompareFunc) pad_compare_name);
780 result = GST_PAD_CAST (find->data);
781 gst_object_ref (result);
784 if (result == NULL) {
785 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "no such pad '%s' in element \"%s\"",
786 name, GST_ELEMENT_NAME (element));
788 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
789 GST_ELEMENT_NAME (element), name);
791 GST_UNLOCK (element);
797 gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
800 GstPad *newpad = NULL;
801 GstElementClass *oclass;
803 oclass = GST_ELEMENT_GET_CLASS (element);
805 if (oclass->request_new_pad)
806 newpad = (oclass->request_new_pad) (element, templ, name);
809 gst_object_ref (newpad);
815 * gst_element_get_request_pad:
816 * @element: a #GstElement to find a request pad of.
817 * @name: the name of the request #GstPad to retrieve.
819 * Retrieves a pad from the element by name. This version only retrieves
822 * Returns: requested #GstPad if found, otherwise NULL. Unref after usage.
825 gst_element_get_request_pad (GstElement * element, const gchar * name)
827 GstPadTemplate *templ = NULL;
829 const gchar *req_name = NULL;
830 gboolean templ_found = FALSE;
834 gchar *str, *endptr = NULL;
835 GstElementClass *class;
837 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
838 g_return_val_if_fail (name != NULL, NULL);
840 class = GST_ELEMENT_GET_CLASS (element);
842 if (strstr (name, "%")) {
843 templ = gst_element_class_get_pad_template (class, name);
848 list = gst_element_class_get_pad_template_list (class);
849 while (!templ_found && list) {
850 templ = (GstPadTemplate *) list->data;
851 if (templ->presence == GST_PAD_REQUEST) {
852 /* Because of sanity checks in gst_pad_template_new(), we know that %s
853 and %d, occurring at the end of the name_template, are the only
855 GST_CAT_DEBUG (GST_CAT_PADS, "comparing %s to %s", name,
856 templ->name_template);
857 if ((str = strchr (templ->name_template, '%'))
858 && strncmp (templ->name_template, name,
859 str - templ->name_template) == 0
860 && strlen (name) > str - templ->name_template) {
861 data = name + (str - templ->name_template);
862 if (*(str + 1) == 'd') {
864 n = (gint) strtol (data, &endptr, 10);
865 if (endptr && *endptr == '\0') {
885 pad = gst_element_request_pad (element, templ, req_name);
891 * gst_element_get_pad:
892 * @element: a #GstElement.
893 * @name: the name of the pad to retrieve.
895 * Retrieves a pad from @element by name. Tries gst_element_get_static_pad()
896 * first, then gst_element_get_request_pad().
898 * Returns: the #GstPad if found, otherwise %NULL. Unref after usage.
901 gst_element_get_pad (GstElement * element, const gchar * name)
905 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
906 g_return_val_if_fail (name != NULL, NULL);
908 pad = gst_element_get_static_pad (element, name);
910 pad = gst_element_get_request_pad (element, name);
916 iterate_pad (GstIterator * it, GstPad * pad)
918 gst_object_ref (pad);
919 return GST_ITERATOR_ITEM_PASS;
923 * gst_element_iterate_pads:
924 * @element: a #GstElement to iterate pads of.
926 * Retrieves an iterattor of @element's pads.
928 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
933 gst_element_iterate_pads (GstElement * element)
937 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
940 gst_object_ref (element);
941 result = gst_iterator_new_list (GST_GET_LOCK (element),
942 &element->pads_cookie,
945 (GstIteratorItemFunction) iterate_pad,
946 (GstIteratorDisposeFunction) gst_object_unref);
947 GST_UNLOCK (element);
953 direction_filter (gconstpointer pad, gconstpointer direction)
955 if (GST_PAD_DIRECTION (pad) == GPOINTER_TO_INT (direction)) {
956 /* pass the ref through */
960 /* FIXME: this is very stupid */
961 gst_object_unref (GST_OBJECT_CAST (pad));
967 * gst_element_iterate_src_pads:
968 * @element: a #GstElement.
970 * Retrieves an iterator of @element's source pads.
972 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
977 gst_element_iterate_src_pads (GstElement * element)
979 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
981 return gst_iterator_filter (gst_element_iterate_pads (element),
982 direction_filter, GINT_TO_POINTER (GST_PAD_SRC));
986 * gst_element_iterate_sink_pads:
987 * @element: a #GstElement.
989 * Retrieves an iterator of @element's sink pads.
991 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
996 gst_element_iterate_sink_pads (GstElement * element)
998 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1000 return gst_iterator_filter (gst_element_iterate_pads (element),
1001 direction_filter, GINT_TO_POINTER (GST_PAD_SINK));
1005 * gst_element_class_add_pad_template:
1006 * @klass: the #GstElementClass to add the pad template to.
1007 * @templ: a #GstPadTemplate to add to the element class.
1009 * Adds a padtemplate to an element class. This is mainly used in the _base_init
1010 * functions of classes.
1013 gst_element_class_add_pad_template (GstElementClass * klass,
1014 GstPadTemplate * templ)
1016 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1017 g_return_if_fail (GST_IS_PAD_TEMPLATE (templ));
1019 /* avoid registering pad templates with the same name */
1020 g_return_if_fail (gst_element_class_get_pad_template (klass,
1021 templ->name_template) == NULL);
1023 klass->padtemplates = g_list_append (klass->padtemplates,
1024 gst_object_ref (templ));
1025 klass->numpadtemplates++;
1029 * gst_element_class_set_details:
1030 * @klass: class to set details for
1031 * @details: details to set
1033 * Sets the detailed information for a #GstElementClass.
1034 * <note>This function is for use in _base_init functions only.</note>
1037 gst_element_class_set_details (GstElementClass * klass,
1038 const GstElementDetails * details)
1040 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1041 g_return_if_fail (GST_IS_ELEMENT_DETAILS (details));
1043 __gst_element_details_copy (&klass->details, details);
1047 * gst_element_class_get_pad_template_list:
1048 * @element_class: a #GstElementClass to get pad templates of.
1050 * Retrieves a list of the pad templates associated with @element_class. The
1051 * list must not be modified by the calling code.
1052 * <note>If you use this function in the #GInstanceInitFunc of an object class
1053 * that has subclasses, make sure to pass the g_class parameter of the
1054 * #GInstanceInitFunc here.</note>
1056 * Returns: the #GList of padtemplates.
1059 gst_element_class_get_pad_template_list (GstElementClass * element_class)
1061 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1063 return element_class->padtemplates;
1067 * gst_element_class_get_pad_template:
1068 * @element_class: a #GstElementClass to get the pad template of.
1069 * @name: the name of the #GstPadTemplate to get.
1071 * Retrieves a padtemplate from @element_class with the given name.
1072 * <note>If you use this function in the #GInstanceInitFunc of an object class
1073 * that has subclasses, make sure to pass the g_class parameter of the
1074 * #GInstanceInitFunc here.</note>
1076 * Returns: the #GstPadTemplate with the given name, or NULL if none was found.
1077 * No unreferencing is necessary.
1080 gst_element_class_get_pad_template (GstElementClass * element_class,
1085 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1086 g_return_val_if_fail (name != NULL, NULL);
1088 padlist = gst_element_class_get_pad_template_list (element_class);
1091 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
1093 if (strcmp (padtempl->name_template, name) == 0)
1096 padlist = g_list_next (padlist);
1103 gst_element_get_random_pad (GstElement * element, GstPadDirection dir)
1105 GstPad *result = NULL;
1108 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "getting a random pad");
1113 pads = element->srcpads;
1117 pads = element->sinkpads;
1120 goto wrong_direction;
1122 for (; pads; pads = g_list_next (pads)) {
1123 GstPad *pad = GST_PAD (pads->data);
1126 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "checking pad %s:%s",
1127 GST_DEBUG_PAD_NAME (pad));
1129 if (GST_PAD_IS_LINKED (pad)) {
1134 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is not linked",
1135 GST_DEBUG_PAD_NAME (pad));
1140 gst_object_ref (result);
1142 GST_UNLOCK (element);
1146 /* ERROR handling */
1149 g_warning ("unknown pad direction %d", dir);
1155 * gst_element_send_event:
1156 * @element: a #GstElement to send the event to.
1157 * @event: the #GstEvent to send to the element.
1159 * Sends an event to an element. If the element doesn't
1160 * implement an event handler, the event will be forwarded
1161 * to a random sink pad. This function takes owership of the
1162 * provided event so you should _ref it if you want to reuse
1163 * the event after this call.
1165 * Returns: TRUE if the event was handled.
1170 gst_element_send_event (GstElement * element, GstEvent * event)
1172 GstElementClass *oclass;
1173 gboolean result = FALSE;
1175 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1176 g_return_val_if_fail (event != NULL, FALSE);
1178 oclass = GST_ELEMENT_GET_CLASS (element);
1180 if (oclass->send_event) {
1181 result = oclass->send_event (element, event);
1183 GstPad *pad = gst_element_get_random_pad (element, GST_PAD_SINK);
1186 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1187 "pushing event to random pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1189 result = gst_pad_push_event (pad, event);
1190 gst_object_unref (pad);
1192 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "can't send event on element %s",
1193 GST_ELEMENT_NAME (element));
1201 * @element: a #GstElement to send the event to.
1202 * @rate: The new playback rate
1203 * @format: The format of the seek values
1204 * @flags: The optional seek flags.
1205 * @cur_type: The type and flags for the new current position
1206 * @cur: The value of the new current position
1207 * @stop_type: The type and flags for the new stop position
1208 * @stop: The value of the new stop position
1210 * Sends a seek event to an element.
1212 * Returns: %TRUE if the event was handled.
1217 gst_element_seek (GstElement * element, gdouble rate, GstFormat format,
1218 GstSeekFlags flags, GstSeekType cur_type, gint64 cur,
1219 GstSeekType stop_type, gint64 stop)
1224 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1227 gst_event_new_seek (rate, format, flags, cur_type, cur, stop_type, stop);
1228 result = gst_element_send_event (element, event);
1234 * gst_element_get_query_types:
1235 * @element: a #GstElement to query
1237 * Get an array of query types from the element.
1238 * If the element doesn't implement a query types function,
1239 * the query will be forwarded to a random sink pad.
1241 * Returns: An array of #GstQueryType elements that should not
1242 * be freed or modified.
1246 const GstQueryType *
1247 gst_element_get_query_types (GstElement * element)
1249 GstElementClass *oclass;
1250 const GstQueryType *result = NULL;
1252 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1254 oclass = GST_ELEMENT_GET_CLASS (element);
1256 if (oclass->get_query_types) {
1257 result = oclass->get_query_types (element);
1259 GstPad *pad = gst_element_get_random_pad (element, GST_PAD_SINK);
1262 GstPad *peer = gst_pad_get_peer (pad);
1265 result = gst_pad_get_query_types (peer);
1267 gst_object_unref (peer);
1269 gst_object_unref (pad);
1276 * gst_element_query:
1277 * @element: a #GstElement to perform the query on.
1278 * @query: the #GstQuery.
1280 * Performs a query on the given element. If the format is set
1281 * to GST_FORMAT_DEFAULT and this function returns TRUE, the
1282 * format pointer will hold the default format.
1283 * For element that don't implement a query handler, this function
1284 * forwards the query to a random usable sinkpad of this element.
1286 * Returns: TRUE if the query could be performed.
1291 gst_element_query (GstElement * element, GstQuery * query)
1293 GstElementClass *oclass;
1294 gboolean result = FALSE;
1296 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1297 g_return_val_if_fail (query != NULL, FALSE);
1299 oclass = GST_ELEMENT_GET_CLASS (element);
1301 if (oclass->query) {
1302 result = oclass->query (element, query);
1304 GstPad *pad = gst_element_get_random_pad (element, GST_PAD_SRC);
1307 result = gst_pad_query (pad, query);
1309 gst_object_unref (pad);
1311 pad = gst_element_get_random_pad (element, GST_PAD_SINK);
1313 GstPad *peer = gst_pad_get_peer (pad);
1316 result = gst_pad_query (peer, query);
1318 gst_object_unref (peer);
1320 gst_object_unref (pad);
1328 * gst_element_post_message:
1329 * @element: a #GstElement posting the message
1330 * @message: a #GstMessage to post
1332 * Post a message on the element's #GstBus.
1334 * Returns: TRUE if the message was successfully posted.
1339 gst_element_post_message (GstElement * element, GstMessage * message)
1342 gboolean result = FALSE;
1344 GST_DEBUG ("posting message %p ...", message);
1346 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1347 g_return_val_if_fail (message != NULL, FALSE);
1352 if (G_UNLIKELY (bus == NULL)) {
1353 GST_DEBUG ("... but I won't because I have no bus");
1354 GST_UNLOCK (element);
1355 gst_message_unref (message);
1358 gst_object_ref (bus);
1359 GST_DEBUG ("... on bus %" GST_PTR_FORMAT, bus);
1360 GST_UNLOCK (element);
1362 result = gst_bus_post (bus, message);
1363 gst_object_unref (bus);
1369 * _gst_element_error_printf:
1370 * @format: the printf-like format to use, or NULL
1372 * This function is only used internally by the #gst_element_error macro.
1374 * Returns: a newly allocated string, or NULL if the format was NULL or ""
1379 _gst_element_error_printf (const gchar * format, ...)
1389 va_start (args, format);
1390 buffer = g_strdup_vprintf (format, args);
1396 * gst_element_message_full:
1397 * @element: a #GstElement to send message from
1398 * @type: the #GstMessageType
1399 * @domain: the GStreamer GError domain this message belongs to
1400 * @code: the GError code belonging to the domain
1401 * @text: an allocated text string to be used as a replacement for the
1402 * default message connected to code, or NULL
1403 * @debug: an allocated debug message to be used as a replacement for the
1404 * default debugging information, or NULL
1405 * @file: the source code file where the error was generated
1406 * @function: the source code function where the error was generated
1407 * @line: the source code line where the error was generated
1409 * Post an error or warning message on the bus from inside an element.
1413 void gst_element_message_full
1414 (GstElement * element, GstMessageType type,
1415 GQuark domain, gint code, gchar * text,
1416 gchar * debug, const gchar * file, const gchar * function, gint line)
1418 GError *gerror = NULL;
1422 GstMessage *message = NULL;
1425 GST_DEBUG ("start");
1426 g_return_if_fail (GST_IS_ELEMENT (element));
1427 g_return_if_fail ((type == GST_MESSAGE_ERROR) ||
1428 (type == GST_MESSAGE_WARNING));
1430 /* check if we send the given text or the default error text */
1431 if ((text == NULL) || (text[0] == 0)) {
1432 /* text could have come from g_strdup_printf (""); */
1434 sent_text = gst_error_get_message (domain, code);
1438 /* construct a sent_debug with extra information from source */
1439 if ((debug == NULL) || (debug[0] == 0)) {
1440 /* debug could have come from g_strdup_printf (""); */
1443 name = gst_object_get_path_string (GST_OBJECT (element));
1444 sent_debug = g_strdup_printf ("%s(%d): %s: %s:\n%s",
1445 file, line, function, name, debug ? debug : "");
1450 /* create gerror and post message */
1451 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posting message: %s",
1453 gerror = g_error_new_literal (domain, code, sent_text);
1455 if (type == GST_MESSAGE_ERROR) {
1456 message = gst_message_new_error (GST_OBJECT (element), gerror, sent_debug);
1457 } else if (type == GST_MESSAGE_WARNING) {
1458 message = gst_message_new_warning (GST_OBJECT (element), gerror,
1461 g_assert_not_reached ();
1463 gst_element_post_message (element, message);
1465 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posted message: %s",
1469 g_error_free (gerror);
1470 g_free (sent_debug);
1475 * gst_element_is_locked_state:
1476 * @element: a #GstElement.
1478 * Checks if the state of an element is locked.
1479 * If the state of an element is locked, state changes of the parent don't
1480 * affect the element.
1481 * This way you can leave currently unused elements inside bins. Just lock their
1482 * state before changing the state from #GST_STATE_NULL.
1484 * Returns: TRUE, if the element's state is locked.
1489 gst_element_is_locked_state (GstElement * element)
1491 gboolean result = FALSE;
1493 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1496 result = GST_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1497 GST_UNLOCK (element);
1503 * gst_element_set_locked_state:
1504 * @element: a #GstElement
1505 * @locked_state: TRUE to lock the element's state
1507 * Locks the state of an element, so state changes of the parent don't affect
1508 * this element anymore.
1510 * Returns: TRUE if the state was changed, FALSE if bad params were given or
1511 * the element was already in the correct state.
1516 gst_element_set_locked_state (GstElement * element, gboolean locked_state)
1520 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1523 old = GST_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1525 if (G_UNLIKELY (old == locked_state))
1529 GST_CAT_DEBUG (GST_CAT_STATES, "locking state of element %s",
1530 GST_ELEMENT_NAME (element));
1531 GST_FLAG_SET (element, GST_ELEMENT_LOCKED_STATE);
1533 GST_CAT_DEBUG (GST_CAT_STATES, "unlocking state of element %s",
1534 GST_ELEMENT_NAME (element));
1535 GST_FLAG_UNSET (element, GST_ELEMENT_LOCKED_STATE);
1537 GST_UNLOCK (element);
1542 GST_UNLOCK (element);
1548 * gst_element_sync_state_with_parent:
1549 * @element: a #GstElement.
1551 * Tries to change the state of the element to the same as its parent.
1552 * If this function returns FALSE, the state of element is undefined.
1554 * Returns: TRUE, if the element's state could be synced to the parent's state.
1557 gst_element_sync_state_with_parent (GstElement * element)
1561 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1562 parent = GST_ELEMENT (GST_ELEMENT_PARENT (element));
1563 g_return_val_if_fail (GST_IS_BIN (parent), FALSE);
1565 GST_CAT_DEBUG (GST_CAT_STATES, "syncing state of element %s (%s) to %s (%s)",
1566 GST_ELEMENT_NAME (element),
1567 gst_element_state_get_name (GST_STATE (element)),
1568 GST_ELEMENT_NAME (parent),
1569 gst_element_state_get_name (GST_STATE (parent)));
1571 if (gst_element_set_state (element,
1572 GST_STATE (parent)) == GST_STATE_CHANGE_FAILURE) {
1579 static GstStateChangeReturn
1580 gst_element_get_state_func (GstElement * element,
1581 GstState * state, GstState * pending, GTimeVal * timeout)
1583 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
1584 GstState old_pending;
1586 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
1588 GST_STATE_LOCK (element);
1589 /* we got an error, report immediatly */
1590 if (GST_STATE_NO_PREROLL (element)) {
1591 ret = GST_STATE_CHANGE_NO_PREROLL;
1595 /* we got an error, report immediatly */
1596 if (GST_STATE_ERROR (element)) {
1597 ret = GST_STATE_CHANGE_FAILURE;
1601 old_pending = GST_STATE_PENDING (element);
1602 if (old_pending != GST_STATE_VOID_PENDING) {
1603 GTimeVal *timeval, abstimeout;
1605 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "wait for pending");
1607 /* make timeout absolute */
1608 g_get_current_time (&abstimeout);
1609 g_time_val_add (&abstimeout,
1610 timeout->tv_sec * G_USEC_PER_SEC + timeout->tv_usec);
1611 timeval = &abstimeout;
1615 /* we have a pending state change, wait for it to complete */
1616 if (!GST_STATE_TIMED_WAIT (element, timeval)) {
1617 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timeout");
1618 /* timeout triggered */
1619 ret = GST_STATE_CHANGE_ASYNC;
1621 /* could be success or failure */
1622 if (old_pending == GST_STATE (element)) {
1623 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "got success");
1624 ret = GST_STATE_CHANGE_SUCCESS;
1626 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "got failure");
1627 ret = GST_STATE_CHANGE_FAILURE;
1631 /* if nothing is pending anymore we can return SUCCESS */
1632 if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
1633 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "nothing pending");
1634 ret = GST_STATE_CHANGE_SUCCESS;
1639 *state = GST_STATE (element);
1641 *pending = GST_STATE_PENDING (element);
1643 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1644 "state current: %s, pending: %s, error: %d, no_preroll: %d, result: %d",
1645 gst_element_state_get_name (GST_STATE (element)),
1646 gst_element_state_get_name (GST_STATE_PENDING (element)),
1647 GST_STATE_ERROR (element), GST_STATE_NO_PREROLL (element), ret);
1649 GST_STATE_UNLOCK (element);
1655 * gst_element_get_state:
1656 * @element: a #GstElement to get the state of.
1657 * @state: a pointer to #GstState to hold the state. Can be NULL.
1658 * @pending: a pointer to #GstState to hold the pending state.
1660 * @timeout: a #GTimeVal to specify the timeout for an async
1661 * state change or NULL for infinite timeout.
1663 * Gets the state of the element.
1665 * For elements that performed an ASYNC state change, as reported by
1666 * #gst_element_set_state(), this function will block up to the
1667 * specified timeout value for the state change to complete.
1668 * If the element completes the state change or goes into
1669 * an error, this function returns immediatly with a return value of
1670 * GST_STATE_CHANGE_SUCCESS or GST_STATE_CHANGE_FAILURE respectively.
1672 * Returns: GST_STATE_CHANGE_SUCCESS if the element has no more pending state and
1673 * the last state change succeeded, GST_STATE_CHANGE_ASYNC
1674 * if the element is still performing a state change or
1675 * GST_STATE_CHANGE_FAILURE if the last state change failed.
1679 GstStateChangeReturn
1680 gst_element_get_state (GstElement * element,
1681 GstState * state, GstState * pending, GTimeVal * timeout)
1683 GstElementClass *oclass;
1684 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
1686 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
1688 oclass = GST_ELEMENT_GET_CLASS (element);
1690 if (oclass->get_state)
1691 result = (oclass->get_state) (element, state, pending, timeout);
1697 * gst_element_abort_state:
1698 * @element: a #GstElement to abort the state of.
1700 * Abort the state change of the element. This function is used
1701 * by elements that do asynchronous state changes and find out
1702 * something is wrong.
1704 * This function should be called with the STATE_LOCK held.
1709 gst_element_abort_state (GstElement * element)
1713 g_return_if_fail (GST_IS_ELEMENT (element));
1715 pending = GST_STATE_PENDING (element);
1717 if (pending != GST_STATE_VOID_PENDING && !GST_STATE_ERROR (element)) {
1718 #ifndef GST_DISABLE_GST_DEBUG
1719 GstState old_state = GST_STATE (element);
1722 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1723 "aborting state from %s to %s", gst_element_state_get_name (old_state),
1724 gst_element_state_get_name (pending));
1727 GST_STATE_ERROR (element) = TRUE;
1729 GST_STATE_BROADCAST (element);
1734 * gst_element_commit_state:
1735 * @element: a #GstElement to commit the state of.
1737 * Commit the state change of the element. This function is used
1738 * by elements that do asynchronous state changes.
1740 * This function can only be called with the STATE_LOCK held.
1745 gst_element_commit_state (GstElement * element)
1749 g_return_if_fail (GST_IS_ELEMENT (element));
1751 pending = GST_STATE_PENDING (element);
1753 if (pending != GST_STATE_VOID_PENDING) {
1754 GstState old_state = GST_STATE (element);
1755 GstMessage *message;
1757 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1758 "committing state from %s to %s",
1759 gst_element_state_get_name (old_state),
1760 gst_element_state_get_name (pending));
1762 GST_STATE (element) = pending;
1763 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
1764 GST_STATE_ERROR (element) = FALSE;
1766 message = gst_message_new_state_changed (GST_OBJECT (element),
1767 old_state, pending);
1768 gst_element_post_message (element, message);
1769 GST_STATE_BROADCAST (element);
1774 * gst_element_lost_state:
1775 * @element: a #GstElement the state is lost of
1777 * Brings the element to the lost state. The current state of the
1778 * element is copied to the pending state so that any call to
1779 * #gst_element_get_state() will return ASYNC.
1780 * This is mostly used for elements that lost their preroll buffer
1781 * in the PAUSED state after a flush, they become PAUSED again
1782 * if a new preroll buffer is queued.
1783 * This function can only be called when the element is currently
1784 * not in error or an async state change.
1786 * This function can only be called with the STATE_LOCK held.
1791 gst_element_lost_state (GstElement * element)
1793 g_return_if_fail (GST_IS_ELEMENT (element));
1795 if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING &&
1796 !GST_STATE_ERROR (element)) {
1797 GstState current_state = GST_STATE (element);
1798 GstMessage *message;
1800 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1801 "lost state of %s", gst_element_state_get_name (current_state));
1803 GST_STATE_PENDING (element) = current_state;
1804 GST_STATE_ERROR (element) = FALSE;
1806 message = gst_message_new_state_changed (GST_OBJECT (element),
1807 current_state, current_state);
1808 gst_element_post_message (element, message);
1813 * gst_element_set_state:
1814 * @element: a #GstElement to change state of.
1815 * @state: the element's new #GstState.
1817 * Sets the state of the element. This function will try to set the
1818 * requested state by going through all the intermediary states and calling
1819 * the class's state change function for each.
1821 * Returns: Result of the state change using #GstStateChangeReturn.
1825 GstStateChangeReturn
1826 gst_element_set_state (GstElement * element, GstState state)
1828 GstElementClass *oclass;
1830 GstStateChangeReturn return_val = GST_STATE_CHANGE_SUCCESS;
1831 GstStateChangeReturn ret;
1835 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
1837 GST_DEBUG_OBJECT (element, "set_state to %s",
1838 gst_element_state_get_name (state));
1839 /* get current element state, need to call the method so that
1840 * we call the virtual method and subclasses can implement their
1842 GST_TIME_TO_TIMEVAL (0, tv);
1843 ret = gst_element_get_state (element, ¤t, &pending, &tv);
1845 /* get the element state lock */
1846 GST_STATE_LOCK (element);
1847 /* this is the state we should go to */
1848 GST_STATE_FINAL (element) = state;
1849 if (ret == GST_STATE_CHANGE_ASYNC) {
1850 /* force next state keeping ASYNC, this is atomic as we hold
1852 gst_element_commit_state (element);
1853 gst_element_lost_state (element);
1856 /* start with the current state */
1857 current = GST_STATE (element);
1859 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "setting state from %s to %s",
1860 gst_element_state_get_name (current), gst_element_state_get_name (state));
1862 oclass = GST_ELEMENT_GET_CLASS (element);
1864 /* We always perform at least one state change, even if the
1865 * current state is equal to the required state. This is needed
1866 * for bins that sync their children. */
1870 /* calculate the pending state */
1871 if (current < state)
1872 pending = current + 1;
1873 else if (current > state)
1874 pending = current - 1;
1878 /* set the pending state variable */
1879 GST_STATE_PENDING (element) = pending;
1881 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1882 "%s: setting state from %s to %s",
1883 (pending != state ? "intermediate" : "final"),
1884 gst_element_state_get_name (current),
1885 gst_element_state_get_name (pending));
1887 /* call the state change function so it can set the state */
1888 if (oclass->change_state)
1889 return_val = (oclass->change_state) (element, GST_STATE_CHANGE (element));
1891 return_val = GST_STATE_CHANGE_FAILURE;
1893 /* clear the error and preroll flag, we need to do that after
1894 * calling the virtual change_state function so that it can use the
1895 * old previous value. */
1896 GST_STATE_ERROR (element) = FALSE;
1897 GST_STATE_NO_PREROLL (element) = FALSE;
1899 switch (return_val) {
1900 case GST_STATE_CHANGE_FAILURE:
1901 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1902 "have failed change_state return");
1903 /* state change failure exits the loop */
1904 gst_element_abort_state (element);
1906 case GST_STATE_CHANGE_ASYNC:
1907 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1908 "element will change state async");
1909 /* an async state change exits the loop, we can only
1910 * go to the next state change when this one completes. */
1912 case GST_STATE_CHANGE_SUCCESS:
1913 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1914 "element changed state successfully");
1915 /* we can commit the state now and proceed to the next state */
1916 gst_element_commit_state (element);
1917 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "committed state");
1919 case GST_STATE_CHANGE_NO_PREROLL:
1920 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1921 "element changed state successfully and can't preroll");
1922 /* we can commit the state now and proceed to the next state */
1923 gst_element_commit_state (element);
1924 GST_STATE_NO_PREROLL (element) = TRUE;
1925 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "committed state");
1928 goto invalid_return;
1930 /* get the current state of the element and see if we need to do more
1932 current = GST_STATE (element);
1934 while (current != state);
1937 GST_STATE_FINAL (element) = GST_STATE_VOID_PENDING;
1938 GST_STATE_UNLOCK (element);
1940 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "exit state change");
1947 GST_STATE_FINAL (element) = GST_STATE_VOID_PENDING;
1948 GST_STATE_UNLOCK (element);
1949 /* somebody added a GST_STATE_ and forgot to do stuff here ! */
1950 g_critical ("unknown return value %d from a state change function",
1952 return GST_STATE_CHANGE_FAILURE;
1956 /* gst_iterator_fold functions for pads_activate */
1959 activate_pads (GstPad * pad, GValue * ret, gboolean * active)
1961 if (!gst_pad_set_active (pad, *active))
1962 g_value_set_boolean (ret, FALSE);
1964 gst_pad_set_caps (pad, NULL);
1966 gst_object_unref (pad);
1970 /* returns false on error or early cutout of the fold, true otherwise */
1972 iterator_fold_with_resync (GstIterator * iter, GstIteratorFoldFunction func,
1973 GValue * ret, gpointer user_data)
1975 GstIteratorResult ires;
1976 gboolean res = TRUE;
1979 ires = gst_iterator_fold (iter, func, ret, user_data);
1982 case GST_ITERATOR_RESYNC:
1983 gst_iterator_resync (iter);
1985 case GST_ITERATOR_DONE:
1998 /* is called with STATE_LOCK
2001 gst_element_pads_activate (GstElement * element, gboolean active)
2003 GValue ret = { 0, };
2007 /* no need to unset this later, it's just a boolean */
2008 g_value_init (&ret, G_TYPE_BOOLEAN);
2009 g_value_set_boolean (&ret, TRUE);
2011 iter = gst_element_iterate_src_pads (element);
2012 fold_ok = iterator_fold_with_resync
2013 (iter, (GstIteratorFoldFunction) activate_pads, &ret, &active);
2014 gst_iterator_free (iter);
2015 if (!fold_ok || !g_value_get_boolean (&ret))
2018 iter = gst_element_iterate_sink_pads (element);
2019 fold_ok = iterator_fold_with_resync
2020 (iter, (GstIteratorFoldFunction) activate_pads, &ret, &active);
2021 gst_iterator_free (iter);
2022 if (!fold_ok || !g_value_get_boolean (&ret))
2028 /* is called with STATE_LOCK */
2029 static GstStateChangeReturn
2030 gst_element_change_state (GstElement * element, GstStateChange transition)
2032 GstState state, pending;
2033 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
2035 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2037 state = GST_STATE (element);
2038 pending = GST_STATE_PENDING (element);
2040 /* if the element already is in the given state, we just return success */
2041 if (pending == GST_STATE_VOID_PENDING || state == GST_STATE_PENDING (element)) {
2042 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2043 "element is already in the %s state",
2044 gst_element_state_get_name (state));
2045 if (GST_STATE_NO_PREROLL (element))
2046 return GST_STATE_CHANGE_NO_PREROLL;
2048 return GST_STATE_CHANGE_SUCCESS;
2051 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element,
2052 "default handler tries setting state from %s to %s (%04x)",
2053 gst_element_state_get_name (state),
2054 gst_element_state_get_name (pending), transition);
2056 switch (transition) {
2057 case GST_STATE_CHANGE_NULL_TO_READY:
2059 case GST_STATE_CHANGE_READY_TO_PAUSED:
2060 if (!gst_element_pads_activate (element, TRUE)) {
2061 result = GST_STATE_CHANGE_FAILURE;
2064 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2066 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2068 case GST_STATE_CHANGE_PAUSED_TO_READY:
2069 case GST_STATE_CHANGE_READY_TO_NULL:
2070 /* deactivate pads in both cases, since they are activated on
2071 ready->paused but the element might not have made it to paused */
2072 if (!gst_element_pads_activate (element, FALSE)) {
2073 result = GST_STATE_CHANGE_FAILURE;
2076 element->base_time = 0;
2077 GST_UNLOCK (element);
2081 /* this will catch real but unhandled state changes;
2082 * can only be caused by:
2083 * - a new state was added
2084 * - somehow the element was asked to jump across an intermediate state
2086 g_warning ("Unhandled state change from %s to %s",
2087 gst_element_state_get_name (state),
2088 gst_element_state_get_name (pending));
2096 * gst_element_get_factory:
2097 * @element: a #GstElement to request the element factory of.
2099 * Retrieves the factory that was used to create this element.
2101 * Returns: the #GstElementFactory used for creating this element.
2104 gst_element_get_factory (GstElement * element)
2106 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
2108 return GST_ELEMENT_GET_CLASS (element)->elementfactory;
2112 gst_element_dispose (GObject * object)
2114 GstElement *element = GST_ELEMENT (object);
2116 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "dispose");
2118 /* first we break all our links with the outside */
2119 while (element->pads) {
2120 gst_element_remove_pad (element, GST_PAD_CAST (element->pads->data));
2122 if (G_UNLIKELY (element->pads != 0)) {
2123 g_critical ("could not remove pads from element %s",
2124 GST_STR_NULL (GST_OBJECT_NAME (object)));
2128 gst_object_replace ((GstObject **) & element->clock, NULL);
2129 gst_object_replace ((GstObject **) & element->bus, NULL);
2130 GST_UNLOCK (element);
2132 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "dispose parent");
2134 G_OBJECT_CLASS (parent_class)->dispose (object);
2138 gst_element_finalize (GObject * object)
2140 GstElement *element = GST_ELEMENT (object);
2142 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize");
2144 GST_STATE_LOCK (element);
2145 if (element->state_cond)
2146 g_cond_free (element->state_cond);
2147 element->state_cond = NULL;
2148 GST_STATE_UNLOCK (element);
2149 g_mutex_free (element->state_lock);
2151 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize parent");
2153 G_OBJECT_CLASS (parent_class)->finalize (object);
2156 #ifndef GST_DISABLE_LOADSAVE
2158 * gst_element_save_thyself:
2159 * @element: a #GstElement to save.
2160 * @parent: the xml parent node.
2162 * Saves the element as part of the given XML structure.
2164 * Returns: the new #xmlNodePtr.
2167 gst_element_save_thyself (GstObject * object, xmlNodePtr parent)
2170 GstElementClass *oclass;
2171 GParamSpec **specs, *spec;
2174 GValue value = { 0, };
2175 GstElement *element;
2177 g_return_val_if_fail (GST_IS_ELEMENT (object), parent);
2179 element = GST_ELEMENT (object);
2181 oclass = GST_ELEMENT_GET_CLASS (element);
2183 xmlNewChild (parent, NULL, (xmlChar *) "name",
2184 (xmlChar *) GST_ELEMENT_NAME (element));
2186 if (oclass->elementfactory != NULL) {
2187 GstElementFactory *factory = (GstElementFactory *) oclass->elementfactory;
2189 xmlNewChild (parent, NULL, (xmlChar *) "type",
2190 (xmlChar *) GST_PLUGIN_FEATURE (factory)->name);
2194 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &nspecs);
2196 for (i = 0; i < nspecs; i++) {
2198 if (spec->flags & G_PARAM_READABLE) {
2202 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
2204 g_object_get_property (G_OBJECT (element), spec->name, &value);
2205 param = xmlNewChild (parent, NULL, (xmlChar *) "param", NULL);
2206 xmlNewChild (param, NULL, (xmlChar *) "name", (xmlChar *) spec->name);
2208 if (G_IS_PARAM_SPEC_STRING (spec))
2209 contents = g_value_dup_string (&value);
2210 else if (G_IS_PARAM_SPEC_ENUM (spec))
2211 contents = g_strdup_printf ("%d", g_value_get_enum (&value));
2212 else if (G_IS_PARAM_SPEC_INT64 (spec))
2213 contents = g_strdup_printf ("%" G_GINT64_FORMAT,
2214 g_value_get_int64 (&value));
2216 contents = g_strdup_value_contents (&value);
2218 xmlNewChild (param, NULL, (xmlChar *) "value", (xmlChar *) contents);
2221 g_value_unset (&value);
2227 pads = GST_ELEMENT_PADS (element);
2230 GstPad *pad = GST_PAD (pads->data);
2232 /* figure out if it's a direct pad or a ghostpad */
2233 if (GST_ELEMENT (GST_OBJECT_PARENT (pad)) == element) {
2234 xmlNodePtr padtag = xmlNewChild (parent, NULL, (xmlChar *) "pad", NULL);
2236 gst_object_save_thyself (GST_OBJECT (pad), padtag);
2238 pads = g_list_next (pads);
2245 gst_element_restore_thyself (GstObject * object, xmlNodePtr self)
2247 xmlNodePtr children;
2248 GstElement *element;
2250 gchar *value = NULL;
2252 element = GST_ELEMENT (object);
2253 g_return_if_fail (element != NULL);
2256 children = self->xmlChildrenNode;
2258 if (!strcmp ((char *) children->name, "param")) {
2259 xmlNodePtr child = children->xmlChildrenNode;
2262 if (!strcmp ((char *) child->name, "name")) {
2263 name = (gchar *) xmlNodeGetContent (child);
2264 } else if (!strcmp ((char *) child->name, "value")) {
2265 value = (gchar *) xmlNodeGetContent (child);
2267 child = child->next;
2269 /* FIXME: can this just be g_object_set ? */
2270 gst_util_set_object_arg (G_OBJECT (element), name, value);
2271 /* g_object_set (G_OBJECT (element), name, value, NULL); */
2275 children = children->next;
2279 children = self->xmlChildrenNode;
2281 if (!strcmp ((char *) children->name, "pad")) {
2282 gst_pad_load_and_link (children, GST_OBJECT (element));
2284 children = children->next;
2287 if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
2288 (GST_OBJECT_CLASS (parent_class)->restore_thyself) (object, self);
2290 #endif /* GST_DISABLE_LOADSAVE */
2293 gst_element_set_bus_func (GstElement * element, GstBus * bus)
2295 g_return_if_fail (GST_IS_ELEMENT (element));
2297 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element, "setting bus to %p", bus);
2300 gst_object_replace ((GstObject **) & GST_ELEMENT_BUS (element),
2302 GST_UNLOCK (element);
2306 * gst_element_set_bus:
2307 * @element: a #GstElement to set the bus of.
2308 * @bus: the #GstBus to set.
2310 * Sets the bus of the element. For internal use only, unless you're
2316 gst_element_set_bus (GstElement * element, GstBus * bus)
2318 GstElementClass *oclass;
2320 g_return_if_fail (GST_IS_ELEMENT (element));
2322 oclass = GST_ELEMENT_GET_CLASS (element);
2324 if (oclass->set_bus)
2325 oclass->set_bus (element, bus);
2329 * gst_element_get_bus:
2330 * @element: a #GstElement to get the bus of.
2332 * Returns the bus of the element.
2334 * Returns: the element's #GstBus. unref after usage.
2339 gst_element_get_bus (GstElement * element)
2341 GstBus *result = NULL;
2343 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
2346 result = GST_ELEMENT_BUS (element);
2347 gst_object_ref (result);
2348 GST_UNLOCK (element);
2350 GST_DEBUG_OBJECT (element, "got bus %" GST_PTR_FORMAT, result);