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().
65 #include "gst_private.h"
68 #include <gobject/gvaluecollector.h>
70 #include "gstelement.h"
72 #include "gstmarshal.h"
77 #include "gst-i18n-lib.h"
79 /* Element signals and args */
96 extern void __gst_element_details_clear (GstElementDetails * dp);
97 extern void __gst_element_details_copy (GstElementDetails * dest,
98 const GstElementDetails * src);
100 static void gst_element_class_init (GstElementClass * klass);
101 static void gst_element_init (GstElement * element);
102 static void gst_element_base_class_init (gpointer g_class);
103 static void gst_element_base_class_finalize (gpointer g_class);
105 static void gst_element_dispose (GObject * object);
106 static void gst_element_finalize (GObject * object);
108 static GstStateChangeReturn gst_element_change_state (GstElement * element,
109 GstStateChange transition);
110 static GstStateChangeReturn gst_element_get_state_func (GstElement * element,
111 GstState * state, GstState * pending, GTimeVal * timeout);
112 static void gst_element_set_bus_func (GstElement * element, GstBus * bus);
114 #ifndef GST_DISABLE_LOADSAVE
115 static xmlNodePtr gst_element_save_thyself (GstObject * object,
117 static void gst_element_restore_thyself (GstObject * parent, xmlNodePtr self);
120 GType _gst_element_type = 0;
122 static GstObjectClass *parent_class = NULL;
123 static guint gst_element_signals[LAST_SIGNAL] = { 0 };
126 gst_element_get_type (void)
128 if (!_gst_element_type) {
129 static const GTypeInfo element_info = {
130 sizeof (GstElementClass),
131 gst_element_base_class_init,
132 gst_element_base_class_finalize,
133 (GClassInitFunc) gst_element_class_init,
138 (GInstanceInitFunc) gst_element_init,
142 _gst_element_type = g_type_register_static (GST_TYPE_OBJECT, "GstElement",
143 &element_info, G_TYPE_FLAG_ABSTRACT);
145 return _gst_element_type;
149 gst_element_class_init (GstElementClass * klass)
151 GObjectClass *gobject_class;
152 GstObjectClass *gstobject_class;
154 gobject_class = (GObjectClass *) klass;
155 gstobject_class = (GstObjectClass *) klass;
157 parent_class = g_type_class_ref (GST_TYPE_OBJECT);
160 * GstElement::state-changed:
161 * @gstelement: the object which received the signal
162 * @old_state: the GST_STATE_XXX before the change
163 * @new_state: the GST_STATE_XXX after the change
165 * the #GstState of the element has been changed
167 gst_element_signals[STATE_CHANGE] =
168 g_signal_new ("state-changed", G_TYPE_FROM_CLASS (klass),
169 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstElementClass, state_changed), NULL,
170 NULL, gst_marshal_VOID__INT_INT, G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT);
172 * GstElement::pad-added:
173 * @gstelement: the object which received the signal
174 * @new_pad: the pad that has been added
176 * a new #GstPad has been added to the element
178 gst_element_signals[NEW_PAD] =
179 g_signal_new ("pad-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
180 G_STRUCT_OFFSET (GstElementClass, pad_added), NULL, NULL,
181 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
183 * GstElement::pad-removed:
184 * @gstelement: the object which received the signal
185 * @old_pad: the pad that has been removed
187 * a #GstPad has been removed from the element
189 gst_element_signals[PAD_REMOVED] =
190 g_signal_new ("pad-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
191 G_STRUCT_OFFSET (GstElementClass, pad_removed), NULL, NULL,
192 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
194 * GstElement::no-more-pads:
195 * @gstelement: the object which received the signal
197 * This signals that the element will not generate more dynamic pads.
199 gst_element_signals[NO_MORE_PADS] =
200 g_signal_new ("no-more-pads", G_TYPE_FROM_CLASS (klass),
201 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstElementClass, no_more_pads), NULL,
202 NULL, gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
204 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_element_dispose);
205 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_element_finalize);
207 #ifndef GST_DISABLE_LOADSAVE
208 gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_element_save_thyself);
209 gstobject_class->restore_thyself =
210 GST_DEBUG_FUNCPTR (gst_element_restore_thyself);
213 klass->change_state = GST_DEBUG_FUNCPTR (gst_element_change_state);
214 klass->get_state = GST_DEBUG_FUNCPTR (gst_element_get_state_func);
215 klass->set_bus = GST_DEBUG_FUNCPTR (gst_element_set_bus_func);
216 klass->numpadtemplates = 0;
218 klass->elementfactory = NULL;
222 gst_element_base_class_init (gpointer g_class)
224 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
226 memset (&element_class->details, 0, sizeof (GstElementDetails));
227 element_class->padtemplates = NULL;
231 gst_element_base_class_finalize (gpointer g_class)
233 GstElementClass *klass = GST_ELEMENT_CLASS (g_class);
235 g_list_foreach (klass->padtemplates, (GFunc) gst_object_unref, NULL);
236 g_list_free (klass->padtemplates);
237 __gst_element_details_clear (&klass->details);
241 gst_element_init (GstElement * element)
243 element->current_state = GST_STATE_NULL;
244 element->pending_state = GST_STATE_VOID_PENDING;
245 element->state_lock = g_mutex_new ();
246 element->state_cond = g_cond_new ();
250 * gst_element_default_error:
251 * @object: a #GObject that signalled the error.
252 * @orig: the #GstObject that initiated the error.
253 * @error: the GError.
254 * @debug: an additional debug information string, or NULL.
256 * A default error signal callback to attach to an element.
257 * The user data passed to the g_signal_connect is ignored.
259 * The default handler will simply print the error string using g_print.
264 gst_element_default_error (GObject * object, GstObject * source, GError * error,
267 gchar *name = gst_object_get_path_string (source);
269 g_print (_("ERROR: from element %s: %s\n"), name, error->message);
271 g_print (_("Additional debug info:\n%s\n"), debug);
277 * gst_element_release_request_pad:
278 * @element: a #GstElement to release the request pad of.
279 * @pad: the #GstPad to release.
281 * Makes the element free the previously requested pad as obtained
282 * with gst_element_get_request_pad().
287 gst_element_release_request_pad (GstElement * element, GstPad * pad)
289 GstElementClass *oclass;
291 g_return_if_fail (GST_IS_ELEMENT (element));
292 g_return_if_fail (GST_IS_PAD (pad));
294 oclass = GST_ELEMENT_GET_CLASS (element);
296 if (oclass->release_pad)
297 (oclass->release_pad) (element, pad);
299 gst_element_remove_pad (element, pad);
303 * gst_element_requires_clock:
304 * @element: a #GstElement to query
306 * Query if the element requires a clock.
308 * Returns: TRUE if the element requires a clock
313 gst_element_requires_clock (GstElement * element)
315 gboolean result = FALSE;
317 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
319 result = (GST_ELEMENT_GET_CLASS (element)->set_clock != NULL);
325 * gst_element_provides_clock:
326 * @element: a #GstElement to query
328 * Query if the element provides a clock.
330 * Returns: TRUE if the element provides a clock
335 gst_element_provides_clock (GstElement * element)
337 gboolean result = FALSE;
339 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
341 result = (GST_ELEMENT_GET_CLASS (element)->get_clock != NULL);
347 * gst_element_set_clock:
348 * @element: a #GstElement to set the clock for.
349 * @clock: the #GstClock to set for the element.
351 * Sets the clock for the element. This function increases the
352 * refcount on the clock. Any previously set clock on the object
358 gst_element_set_clock (GstElement * element, GstClock * clock)
360 GstElementClass *oclass;
362 g_return_if_fail (GST_IS_ELEMENT (element));
364 oclass = GST_ELEMENT_GET_CLASS (element);
366 if (oclass->set_clock)
367 oclass->set_clock (element, clock);
370 gst_object_replace ((GstObject **) & element->clock, (GstObject *) clock);
371 GST_UNLOCK (element);
375 * gst_element_get_clock:
376 * @element: a #GstElement to get the clock of.
378 * Gets the clock of the element. If the element provides a clock,
379 * this function will return this clock. For elements that do not
380 * provide a clock, this function returns NULL.
382 * Returns: the #GstClock of the element. unref after usage.
387 gst_element_get_clock (GstElement * element)
389 GstElementClass *oclass;
391 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
393 oclass = GST_ELEMENT_GET_CLASS (element);
395 if (oclass->get_clock)
396 return oclass->get_clock (element);
402 * gst_element_set_base_time:
403 * @element: a #GstElement.
404 * @time: the base time to set.
406 * Set the base time of an element. See @gst_element_get_base_time().
411 gst_element_set_base_time (GstElement * element, GstClockTime time)
413 g_return_if_fail (GST_IS_ELEMENT (element));
416 element->base_time = time;
417 GST_UNLOCK (element);
421 * gst_element_get_base_time:
422 * @element: a #GstElement.
424 * Returns the base time of the element. The base time is the
425 * absolute time of the clock when this element was last put to
426 * PLAYING. Substracting the base time from the clock time gives
427 * the stream time of the element.
429 * Returns: the base time of the element.
434 gst_element_get_base_time (GstElement * element)
438 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
441 result = element->base_time;
442 GST_UNLOCK (element);
447 #ifndef GST_DISABLE_INDEX
449 * gst_element_is_indexable:
450 * @element: a #GstElement.
452 * Queries if the element can be indexed.
454 * Returns: TRUE if the element can be indexed.
459 gst_element_is_indexable (GstElement * element)
461 gboolean result = FALSE;
463 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
465 result = (GST_ELEMENT_GET_CLASS (element)->set_index != NULL);
471 * gst_element_set_index:
472 * @element: a #GstElement.
473 * @index: a #GstIndex.
475 * Set the specified GstIndex on the element.
480 gst_element_set_index (GstElement * element, GstIndex * index)
482 GstElementClass *oclass;
484 g_return_if_fail (GST_IS_ELEMENT (element));
485 g_return_if_fail (GST_IS_INDEX (index));
487 oclass = GST_ELEMENT_GET_CLASS (element);
489 if (oclass->set_index)
490 oclass->set_index (element, index);
494 * gst_element_get_index:
495 * @element: a #GstElement.
497 * Gets the index from the element.
499 * Returns: a #GstIndex or NULL when no index was set on the
500 * element. unref after usage.
505 gst_element_get_index (GstElement * element)
507 GstElementClass *oclass;
509 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
511 oclass = GST_ELEMENT_GET_CLASS (element);
513 if (oclass->get_index)
514 return oclass->get_index (element);
521 * gst_element_add_pad:
522 * @element: a #GstElement to add the pad to.
523 * @pad: the #GstPad to add to the element.
525 * Adds a pad (link point) to @element. @pad's parent will be set to @element;
526 * see gst_object_set_parent() for refcounting information.
528 * Pads are not automatically activated so elements should perform the needed
529 * steps to activate the pad.
531 * The pad and the element should be unlocked when calling this function.
533 * Returns: TRUE if the pad could be added. This function can fail when
534 * passing bad arguments or when a pad with the same name already existed.
539 gst_element_add_pad (GstElement * element, GstPad * pad)
543 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
544 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
546 /* locking pad to look at the name */
548 pad_name = g_strdup (GST_PAD_NAME (pad));
549 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "adding pad '%s'",
550 GST_STR_NULL (pad_name));
553 /* then check to see if there's already a pad by that name here */
555 if (G_UNLIKELY (!gst_object_check_uniqueness (element->pads, pad_name)))
558 /* try to set the pad's parent */
559 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (pad),
560 GST_OBJECT_CAST (element))))
565 /* add it to the list */
566 switch (gst_pad_get_direction (pad)) {
568 element->srcpads = g_list_prepend (element->srcpads, pad);
569 element->numsrcpads++;
572 element->sinkpads = g_list_prepend (element->sinkpads, pad);
573 element->numsinkpads++;
578 element->pads = g_list_prepend (element->pads, pad);
580 element->pads_cookie++;
581 GST_UNLOCK (element);
583 /* emit the NEW_PAD signal */
584 g_signal_emit (G_OBJECT (element), gst_element_signals[NEW_PAD], 0, pad);
591 g_critical ("Padname %s is not unique in element %s, not adding",
592 pad_name, GST_ELEMENT_NAME (element));
593 GST_UNLOCK (element);
600 ("Pad %s already has parent when trying to add to element %s",
601 pad_name, GST_ELEMENT_NAME (element));
602 GST_UNLOCK (element);
610 ("Trying to add pad %s to element %s, but it has no direction",
611 GST_OBJECT_NAME (pad), GST_ELEMENT_NAME (element));
613 GST_UNLOCK (element);
619 * gst_element_remove_pad:
620 * @element: a #GstElement to remove pad from.
621 * @pad: the #GstPad to remove from the element.
623 * Removes @pad from @element. @pad will be destroyed if it has not been
624 * referenced elsewhere.
626 * Returns: TRUE if the pad could be removed. Can return FALSE if the
627 * pad is not belonging to the provided element or when wrong parameters
628 * are passed to this function.
633 gst_element_remove_pad (GstElement * element, GstPad * pad)
637 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
638 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
640 /* locking pad to look at the name and parent */
642 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "removing pad '%s'",
643 GST_STR_NULL (GST_PAD_NAME (pad)));
645 if (G_UNLIKELY (GST_PAD_PARENT (pad) != element))
650 if ((peer = gst_pad_get_peer (pad))) {
651 /* window for MT unsafeness, someone else could unlink here
652 * and then we call unlink with wrong pads. The unlink
653 * function would catch this and safely return failed. */
654 if (GST_PAD_IS_SRC (pad))
655 gst_pad_unlink (pad, peer);
657 gst_pad_unlink (peer, pad);
659 gst_object_unref (peer);
663 /* remove it from the list */
664 switch (gst_pad_get_direction (pad)) {
666 element->srcpads = g_list_remove (element->srcpads, pad);
667 element->numsrcpads--;
670 element->sinkpads = g_list_remove (element->sinkpads, pad);
671 element->numsinkpads--;
674 g_critical ("Removing pad without direction???");
677 element->pads = g_list_remove (element->pads, pad);
679 element->pads_cookie++;
680 GST_UNLOCK (element);
682 g_signal_emit (G_OBJECT (element), gst_element_signals[PAD_REMOVED], 0, pad);
684 gst_object_unparent (GST_OBJECT (pad));
690 /* FIXME, locking order? */
692 g_critical ("Padname %s:%s does not belong to element %s when removing",
693 GST_ELEMENT_NAME (GST_PAD_PARENT (pad)), GST_PAD_NAME (pad),
694 GST_ELEMENT_NAME (element));
695 GST_UNLOCK (element);
702 * gst_element_no_more_pads:
703 * @element: a #GstElement
705 * Use this function to signal that the element does not expect any more pads
706 * to show up in the current pipeline. This function should be called whenever
707 * pads have been added by the element itself. Elements with GST_PAD_SOMETIMES
708 * pad templates use this in combination with autopluggers to figure out that
709 * the element is done initializing its pads.
714 gst_element_no_more_pads (GstElement * element)
716 g_return_if_fail (GST_IS_ELEMENT (element));
718 g_signal_emit (element, gst_element_signals[NO_MORE_PADS], 0);
722 pad_compare_name (GstPad * pad1, const gchar * name)
727 result = strcmp (GST_PAD_NAME (pad1), name);
734 * gst_element_get_static_pad:
735 * @element: a #GstElement to find a static pad of.
736 * @name: the name of the static #GstPad to retrieve.
738 * Retrieves a pad from @element by name. This version only retrieves
739 * already-existing (i.e. 'static') pads.
741 * Returns: the requested #GstPad if found, otherwise NULL. unref after
747 gst_element_get_static_pad (GstElement * element, const gchar * name)
750 GstPad *result = NULL;
752 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
753 g_return_val_if_fail (name != NULL, NULL);
757 g_list_find_custom (element->pads, name, (GCompareFunc) pad_compare_name);
759 result = GST_PAD_CAST (find->data);
760 gst_object_ref (result);
763 if (result == NULL) {
764 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "no such pad '%s' in element \"%s\"",
765 name, GST_ELEMENT_NAME (element));
767 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
768 GST_ELEMENT_NAME (element), name);
770 GST_UNLOCK (element);
776 gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
779 GstPad *newpad = NULL;
780 GstElementClass *oclass;
782 oclass = GST_ELEMENT_GET_CLASS (element);
784 if (oclass->request_new_pad)
785 newpad = (oclass->request_new_pad) (element, templ, name);
788 gst_object_ref (newpad);
794 * gst_element_get_request_pad:
795 * @element: a #GstElement to find a request pad of.
796 * @name: the name of the request #GstPad to retrieve.
798 * Retrieves a pad from the element by name. This version only retrieves
801 * Returns: requested #GstPad if found, otherwise NULL. Unref after usage.
804 gst_element_get_request_pad (GstElement * element, const gchar * name)
806 GstPadTemplate *templ = NULL;
808 const gchar *req_name = NULL;
809 gboolean templ_found = FALSE;
813 gchar *str, *endptr = NULL;
814 GstElementClass *class;
816 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
817 g_return_val_if_fail (name != NULL, NULL);
819 class = GST_ELEMENT_GET_CLASS (element);
821 if (strstr (name, "%")) {
822 templ = gst_element_class_get_pad_template (class, name);
827 list = gst_element_class_get_pad_template_list (class);
828 while (!templ_found && list) {
829 templ = (GstPadTemplate *) list->data;
830 if (templ->presence == GST_PAD_REQUEST) {
831 /* Because of sanity checks in gst_pad_template_new(), we know that %s
832 and %d, occurring at the end of the name_template, are the only
834 GST_CAT_DEBUG (GST_CAT_PADS, "comparing %s to %s", name,
835 templ->name_template);
836 if ((str = strchr (templ->name_template, '%'))
837 && strncmp (templ->name_template, name,
838 str - templ->name_template) == 0
839 && strlen (name) > str - templ->name_template) {
840 data = name + (str - templ->name_template);
841 if (*(str + 1) == 'd') {
843 n = (gint) strtol (data, &endptr, 10);
844 if (endptr && *endptr == '\0') {
864 pad = gst_element_request_pad (element, templ, req_name);
870 * gst_element_get_pad:
871 * @element: a #GstElement.
872 * @name: the name of the pad to retrieve.
874 * Retrieves a pad from @element by name. Tries gst_element_get_static_pad()
875 * first, then gst_element_get_request_pad().
877 * Returns: the #GstPad if found, otherwise %NULL. Unref after usage.
880 gst_element_get_pad (GstElement * element, const gchar * name)
884 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
885 g_return_val_if_fail (name != NULL, NULL);
887 pad = gst_element_get_static_pad (element, name);
889 pad = gst_element_get_request_pad (element, name);
895 iterate_pad (GstIterator * it, GstPad * pad)
897 gst_object_ref (pad);
898 return GST_ITERATOR_ITEM_PASS;
902 * gst_element_iterate_pads:
903 * @element: a #GstElement to iterate pads of.
905 * Retrieves an iterattor of @element's pads.
907 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
912 gst_element_iterate_pads (GstElement * element)
916 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
919 gst_object_ref (element);
920 result = gst_iterator_new_list (GST_GET_LOCK (element),
921 &element->pads_cookie,
924 (GstIteratorItemFunction) iterate_pad,
925 (GstIteratorDisposeFunction) gst_object_unref);
926 GST_UNLOCK (element);
932 direction_filter (gconstpointer pad, gconstpointer direction)
934 if (GST_PAD_DIRECTION (pad) == GPOINTER_TO_INT (direction)) {
935 /* pass the ref through */
939 /* FIXME: this is very stupid */
940 gst_object_unref (GST_OBJECT_CAST (pad));
946 * gst_element_iterate_src_pads:
947 * @element: a #GstElement.
949 * Retrieves an iterator of @element's source pads.
951 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
956 gst_element_iterate_src_pads (GstElement * element)
958 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
960 return gst_iterator_filter (gst_element_iterate_pads (element),
961 direction_filter, GINT_TO_POINTER (GST_PAD_SRC));
965 * gst_element_iterate_sink_pads:
966 * @element: a #GstElement.
968 * Retrieves an iterator of @element's sink pads.
970 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
975 gst_element_iterate_sink_pads (GstElement * element)
977 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
979 return gst_iterator_filter (gst_element_iterate_pads (element),
980 direction_filter, GINT_TO_POINTER (GST_PAD_SINK));
984 * gst_element_class_add_pad_template:
985 * @klass: the #GstElementClass to add the pad template to.
986 * @templ: a #GstPadTemplate to add to the element class.
988 * Adds a padtemplate to an element class. This is mainly used in the _base_init
989 * functions of classes.
992 gst_element_class_add_pad_template (GstElementClass * klass,
993 GstPadTemplate * templ)
995 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
996 g_return_if_fail (GST_IS_PAD_TEMPLATE (templ));
998 /* avoid registering pad templates with the same name */
999 g_return_if_fail (gst_element_class_get_pad_template (klass,
1000 templ->name_template) == NULL);
1002 klass->padtemplates = g_list_append (klass->padtemplates,
1003 gst_object_ref (templ));
1004 klass->numpadtemplates++;
1008 * gst_element_class_set_details:
1009 * @klass: class to set details for
1010 * @details: details to set
1012 * Sets the detailed information for a #GstElementClass.
1013 * <note>This function is for use in _base_init functions only.</note>
1016 gst_element_class_set_details (GstElementClass * klass,
1017 const GstElementDetails * details)
1019 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1020 g_return_if_fail (GST_IS_ELEMENT_DETAILS (details));
1022 __gst_element_details_copy (&klass->details, details);
1026 * gst_element_class_get_pad_template_list:
1027 * @element_class: a #GstElementClass to get pad templates of.
1029 * Retrieves a list of the pad templates associated with @element_class. The
1030 * list must not be modified by the calling code.
1031 * <note>If you use this function in the #GInstanceInitFunc of an object class
1032 * that has subclasses, make sure to pass the g_class parameter of the
1033 * #GInstanceInitFunc here.</note>
1035 * Returns: the #GList of padtemplates.
1038 gst_element_class_get_pad_template_list (GstElementClass * element_class)
1040 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1042 return element_class->padtemplates;
1046 * gst_element_class_get_pad_template:
1047 * @element_class: a #GstElementClass to get the pad template of.
1048 * @name: the name of the #GstPadTemplate to get.
1050 * Retrieves a padtemplate from @element_class with the given name.
1051 * <note>If you use this function in the #GInstanceInitFunc of an object class
1052 * that has subclasses, make sure to pass the g_class parameter of the
1053 * #GInstanceInitFunc here.</note>
1055 * Returns: the #GstPadTemplate with the given name, or NULL if none was found.
1056 * No unreferencing is necessary.
1059 gst_element_class_get_pad_template (GstElementClass * element_class,
1064 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1065 g_return_val_if_fail (name != NULL, NULL);
1067 padlist = gst_element_class_get_pad_template_list (element_class);
1070 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
1072 if (strcmp (padtempl->name_template, name) == 0)
1075 padlist = g_list_next (padlist);
1082 gst_element_get_random_pad (GstElement * element, GstPadDirection dir)
1084 GstPad *result = NULL;
1087 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "getting a random pad");
1092 pads = element->srcpads;
1096 pads = element->sinkpads;
1099 goto wrong_direction;
1101 for (; pads; pads = g_list_next (pads)) {
1102 GstPad *pad = GST_PAD (pads->data);
1105 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "checking pad %s:%s",
1106 GST_DEBUG_PAD_NAME (pad));
1108 if (GST_PAD_IS_LINKED (pad)) {
1113 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is not linked",
1114 GST_DEBUG_PAD_NAME (pad));
1119 gst_object_ref (result);
1121 GST_UNLOCK (element);
1125 /* ERROR handling */
1128 g_warning ("unknown pad direction %d", dir);
1134 * gst_element_send_event:
1135 * @element: a #GstElement to send the event to.
1136 * @event: the #GstEvent to send to the element.
1138 * Sends an event to an element. If the element doesn't
1139 * implement an event handler, the event will be forwarded
1140 * to a random sink pad. This function takes owership of the
1141 * provided event so you should _ref it if you want to reuse
1142 * the event after this call.
1144 * Returns: TRUE if the event was handled.
1149 gst_element_send_event (GstElement * element, GstEvent * event)
1151 GstElementClass *oclass;
1152 gboolean result = FALSE;
1154 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1155 g_return_val_if_fail (event != NULL, FALSE);
1157 oclass = GST_ELEMENT_GET_CLASS (element);
1159 if (oclass->send_event) {
1160 result = oclass->send_event (element, event);
1162 GstPad *pad = gst_element_get_random_pad (element, GST_PAD_SINK);
1165 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1166 "pushing event to random pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1168 result = gst_pad_push_event (pad, event);
1169 gst_object_unref (pad);
1171 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "can't send event on element %s",
1172 GST_ELEMENT_NAME (element));
1180 * @element: a #GstElement to send the event to.
1181 * @rate: The new playback rate
1182 * @format: The format of the seek values
1183 * @flags: The optional seek flags.
1184 * @cur_type: The type and flags for the new current position
1185 * @cur: The value of the new current position
1186 * @stop_type: The type and flags for the new stop position
1187 * @stop: The value of the new stop position
1189 * Sends a seek event to an element.
1191 * Returns: %TRUE if the event was handled.
1196 gst_element_seek (GstElement * element, gdouble rate, GstFormat format,
1197 GstSeekFlags flags, GstSeekType cur_type, gint64 cur,
1198 GstSeekType stop_type, gint64 stop)
1203 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1206 gst_event_new_seek (rate, format, flags, cur_type, cur, stop_type, stop);
1207 result = gst_element_send_event (element, event);
1213 * gst_element_get_query_types:
1214 * @element: a #GstElement to query
1216 * Get an array of query types from the element.
1217 * If the element doesn't implement a query types function,
1218 * the query will be forwarded to a random sink pad.
1220 * Returns: An array of #GstQueryType elements that should not
1221 * be freed or modified.
1225 const GstQueryType *
1226 gst_element_get_query_types (GstElement * element)
1228 GstElementClass *oclass;
1229 const GstQueryType *result = NULL;
1231 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1233 oclass = GST_ELEMENT_GET_CLASS (element);
1235 if (oclass->get_query_types) {
1236 result = oclass->get_query_types (element);
1238 GstPad *pad = gst_element_get_random_pad (element, GST_PAD_SINK);
1241 GstPad *peer = gst_pad_get_peer (pad);
1244 result = gst_pad_get_query_types (peer);
1246 gst_object_unref (peer);
1248 gst_object_unref (pad);
1255 * gst_element_query:
1256 * @element: a #GstElement to perform the query on.
1257 * @query: the #GstQuery.
1259 * Performs a query on the given element. If the format is set
1260 * to GST_FORMAT_DEFAULT and this function returns TRUE, the
1261 * format pointer will hold the default format.
1262 * For element that don't implement a query handler, this function
1263 * forwards the query to a random usable sinkpad of this element.
1265 * Returns: TRUE if the query could be performed.
1270 gst_element_query (GstElement * element, GstQuery * query)
1272 GstElementClass *oclass;
1273 gboolean result = FALSE;
1275 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1276 g_return_val_if_fail (query != NULL, FALSE);
1278 oclass = GST_ELEMENT_GET_CLASS (element);
1280 if (oclass->query) {
1281 result = oclass->query (element, query);
1283 GstPad *pad = gst_element_get_random_pad (element, GST_PAD_SRC);
1286 result = gst_pad_query (pad, query);
1288 gst_object_unref (pad);
1290 pad = gst_element_get_random_pad (element, GST_PAD_SINK);
1292 GstPad *peer = gst_pad_get_peer (pad);
1295 result = gst_pad_query (peer, query);
1297 gst_object_unref (peer);
1299 gst_object_unref (pad);
1307 * gst_element_post_message:
1308 * @element: a #GstElement posting the message
1309 * @message: a #GstMessage to post
1311 * Post a message on the element's #GstBus.
1313 * Returns: TRUE if the message was successfully posted.
1318 gst_element_post_message (GstElement * element, GstMessage * message)
1321 gboolean result = FALSE;
1323 GST_DEBUG ("posting message %p ...", message);
1325 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1326 g_return_val_if_fail (message != NULL, FALSE);
1331 if (G_UNLIKELY (bus == NULL)) {
1332 GST_DEBUG ("... but I won't because I have no bus");
1333 GST_UNLOCK (element);
1334 gst_message_unref (message);
1337 gst_object_ref (bus);
1338 GST_DEBUG ("... on bus %" GST_PTR_FORMAT, bus);
1339 GST_UNLOCK (element);
1341 result = gst_bus_post (bus, message);
1342 gst_object_unref (bus);
1348 * _gst_element_error_printf:
1349 * @format: the printf-like format to use, or NULL
1351 * This function is only used internally by the #gst_element_error macro.
1353 * Returns: a newly allocated string, or NULL if the format was NULL or ""
1358 _gst_element_error_printf (const gchar * format, ...)
1368 va_start (args, format);
1369 buffer = g_strdup_vprintf (format, args);
1375 * gst_element_message_full:
1376 * @element: a #GstElement to send message from
1377 * @type: the #GstMessageType
1378 * @domain: the GStreamer GError domain this message belongs to
1379 * @code: the GError code belonging to the domain
1380 * @text: an allocated text string to be used as a replacement for the
1381 * default message connected to code, or NULL
1382 * @debug: an allocated debug message to be used as a replacement for the
1383 * default debugging information, or NULL
1384 * @file: the source code file where the error was generated
1385 * @function: the source code function where the error was generated
1386 * @line: the source code line where the error was generated
1388 * Post an error or warning message on the bus from inside an element.
1392 void gst_element_message_full
1393 (GstElement * element, GstMessageType type,
1394 GQuark domain, gint code, gchar * text,
1395 gchar * debug, const gchar * file, const gchar * function, gint line)
1397 GError *gerror = NULL;
1401 GstMessage *message = NULL;
1404 GST_DEBUG ("start");
1405 g_return_if_fail (GST_IS_ELEMENT (element));
1406 g_return_if_fail ((type == GST_MESSAGE_ERROR) ||
1407 (type == GST_MESSAGE_WARNING));
1409 /* check if we send the given text or the default error text */
1410 if ((text == NULL) || (text[0] == 0)) {
1411 /* text could have come from g_strdup_printf (""); */
1413 sent_text = gst_error_get_message (domain, code);
1417 /* construct a sent_debug with extra information from source */
1418 if ((debug == NULL) || (debug[0] == 0)) {
1419 /* debug could have come from g_strdup_printf (""); */
1422 name = gst_object_get_path_string (GST_OBJECT (element));
1423 sent_debug = g_strdup_printf ("%s(%d): %s: %s:\n%s",
1424 file, line, function, name, debug ? debug : "");
1429 /* create gerror and post message */
1430 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posting message: %s",
1432 gerror = g_error_new_literal (domain, code, sent_text);
1434 if (type == GST_MESSAGE_ERROR) {
1435 message = gst_message_new_error (GST_OBJECT (element), gerror, sent_debug);
1436 } else if (type == GST_MESSAGE_WARNING) {
1437 message = gst_message_new_warning (GST_OBJECT (element), gerror,
1440 g_assert_not_reached ();
1442 gst_element_post_message (element, message);
1444 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posted message: %s",
1448 g_error_free (gerror);
1449 g_free (sent_debug);
1454 * gst_element_is_locked_state:
1455 * @element: a #GstElement.
1457 * Checks if the state of an element is locked.
1458 * If the state of an element is locked, state changes of the parent don't
1459 * affect the element.
1460 * This way you can leave currently unused elements inside bins. Just lock their
1461 * state before changing the state from #GST_STATE_NULL.
1463 * Returns: TRUE, if the element's state is locked.
1468 gst_element_is_locked_state (GstElement * element)
1470 gboolean result = FALSE;
1472 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1475 result = GST_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1476 GST_UNLOCK (element);
1482 * gst_element_set_locked_state:
1483 * @element: a #GstElement
1484 * @locked_state: TRUE to lock the element's state
1486 * Locks the state of an element, so state changes of the parent don't affect
1487 * this element anymore.
1489 * Returns: TRUE if the state was changed, FALSE if bad params were given or
1490 * the element was already in the correct state.
1495 gst_element_set_locked_state (GstElement * element, gboolean locked_state)
1499 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1502 old = GST_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1504 if (G_UNLIKELY (old == locked_state))
1508 GST_CAT_DEBUG (GST_CAT_STATES, "locking state of element %s",
1509 GST_ELEMENT_NAME (element));
1510 GST_FLAG_SET (element, GST_ELEMENT_LOCKED_STATE);
1512 GST_CAT_DEBUG (GST_CAT_STATES, "unlocking state of element %s",
1513 GST_ELEMENT_NAME (element));
1514 GST_FLAG_UNSET (element, GST_ELEMENT_LOCKED_STATE);
1516 GST_UNLOCK (element);
1521 GST_UNLOCK (element);
1527 * gst_element_sync_state_with_parent:
1528 * @element: a #GstElement.
1530 * Tries to change the state of the element to the same as its parent.
1531 * If this function returns FALSE, the state of element is undefined.
1533 * Returns: TRUE, if the element's state could be synced to the parent's state.
1536 gst_element_sync_state_with_parent (GstElement * element)
1540 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1541 parent = GST_ELEMENT (GST_ELEMENT_PARENT (element));
1542 g_return_val_if_fail (GST_IS_BIN (parent), FALSE);
1544 GST_CAT_DEBUG (GST_CAT_STATES, "syncing state of element %s (%s) to %s (%s)",
1545 GST_ELEMENT_NAME (element),
1546 gst_element_state_get_name (GST_STATE (element)),
1547 GST_ELEMENT_NAME (parent),
1548 gst_element_state_get_name (GST_STATE (parent)));
1550 if (gst_element_set_state (element,
1551 GST_STATE (parent)) == GST_STATE_CHANGE_FAILURE) {
1558 static GstStateChangeReturn
1559 gst_element_get_state_func (GstElement * element,
1560 GstState * state, GstState * pending, GTimeVal * timeout)
1562 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
1563 GstState old_pending;
1565 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "getting state");
1567 GST_STATE_LOCK (element);
1568 /* we got an error, report immediatly */
1569 if (GST_STATE_NO_PREROLL (element)) {
1570 ret = GST_STATE_CHANGE_NO_PREROLL;
1574 /* we got an error, report immediatly */
1575 if (GST_STATE_ERROR (element)) {
1576 ret = GST_STATE_CHANGE_FAILURE;
1580 old_pending = GST_STATE_PENDING (element);
1581 if (old_pending != GST_STATE_VOID_PENDING) {
1582 GTimeVal *timeval, abstimeout;
1584 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "wait for pending");
1586 /* make timeout absolute */
1587 g_get_current_time (&abstimeout);
1588 g_time_val_add (&abstimeout,
1589 timeout->tv_sec * G_USEC_PER_SEC + timeout->tv_usec);
1590 timeval = &abstimeout;
1594 /* we have a pending state change, wait for it to complete */
1595 if (!GST_STATE_TIMED_WAIT (element, timeval)) {
1596 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timeout");
1597 /* timeout triggered */
1598 ret = GST_STATE_CHANGE_ASYNC;
1600 /* could be success or failure */
1601 if (old_pending == GST_STATE (element)) {
1602 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "got success");
1603 ret = GST_STATE_CHANGE_SUCCESS;
1605 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "got failure");
1606 ret = GST_STATE_CHANGE_FAILURE;
1610 /* if nothing is pending anymore we can return SUCCESS */
1611 if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
1612 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "nothing pending");
1613 ret = GST_STATE_CHANGE_SUCCESS;
1618 *state = GST_STATE (element);
1620 *pending = GST_STATE_PENDING (element);
1622 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1623 "state current: %s, pending: %s, error: %d, no_preroll: %d, result: %d",
1624 gst_element_state_get_name (GST_STATE (element)),
1625 gst_element_state_get_name (GST_STATE_PENDING (element)),
1626 GST_STATE_ERROR (element), GST_STATE_NO_PREROLL (element), ret);
1628 GST_STATE_UNLOCK (element);
1634 * gst_element_get_state:
1635 * @element: a #GstElement to get the state of.
1636 * @state: a pointer to #GstState to hold the state. Can be NULL.
1637 * @pending: a pointer to #GstState to hold the pending state.
1639 * @timeout: a #GTimeVal to specify the timeout for an async
1640 * state change or NULL for infinite timeout.
1642 * Gets the state of the element.
1644 * For elements that performed an ASYNC state change, as reported by
1645 * #gst_element_set_state(), this function will block up to the
1646 * specified timeout value for the state change to complete.
1647 * If the element completes the state change or goes into
1648 * an error, this function returns immediatly with a return value of
1649 * GST_STATE_CHANGE_SUCCESS or GST_STATE_CHANGE_FAILURE respectively.
1651 * Returns: GST_STATE_CHANGE_SUCCESS if the element has no more pending state and
1652 * the last state change succeeded, GST_STATE_CHANGE_ASYNC
1653 * if the element is still performing a state change or
1654 * GST_STATE_CHANGE_FAILURE if the last state change failed.
1658 GstStateChangeReturn
1659 gst_element_get_state (GstElement * element,
1660 GstState * state, GstState * pending, GTimeVal * timeout)
1662 GstElementClass *oclass;
1663 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
1665 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
1667 oclass = GST_ELEMENT_GET_CLASS (element);
1669 if (oclass->get_state)
1670 result = (oclass->get_state) (element, state, pending, timeout);
1676 * gst_element_abort_state:
1677 * @element: a #GstElement to abort the state of.
1679 * Abort the state change of the element. This function is used
1680 * by elements that do asynchronous state changes and find out
1681 * something is wrong.
1683 * This function should be called with the STATE_LOCK held.
1688 gst_element_abort_state (GstElement * element)
1692 g_return_if_fail (GST_IS_ELEMENT (element));
1694 pending = GST_STATE_PENDING (element);
1696 if (pending != GST_STATE_VOID_PENDING && !GST_STATE_ERROR (element)) {
1697 #ifndef GST_DISABLE_GST_DEBUG
1698 GstState old_state = GST_STATE (element);
1701 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1702 "aborting state from %s to %s", gst_element_state_get_name (old_state),
1703 gst_element_state_get_name (pending));
1706 GST_STATE_ERROR (element) = TRUE;
1708 GST_STATE_BROADCAST (element);
1713 * gst_element_commit_state:
1714 * @element: a #GstElement to commit the state of.
1716 * Commit the state change of the element. This function is used
1717 * by elements that do asynchronous state changes.
1719 * This function can only be called with the STATE_LOCK held.
1724 gst_element_commit_state (GstElement * element)
1727 GstMessage *message;
1729 g_return_if_fail (GST_IS_ELEMENT (element));
1731 pending = GST_STATE_PENDING (element);
1733 if (pending != GST_STATE_VOID_PENDING) {
1734 GstState old_state = GST_STATE (element);
1736 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1737 "committing state from %s to %s",
1738 gst_element_state_get_name (old_state),
1739 gst_element_state_get_name (pending));
1741 GST_STATE (element) = pending;
1742 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
1743 GST_STATE_ERROR (element) = FALSE;
1745 g_signal_emit (G_OBJECT (element), gst_element_signals[STATE_CHANGE],
1746 0, old_state, pending);
1747 message = gst_message_new_state_changed (GST_OBJECT (element),
1748 old_state, pending);
1749 gst_element_post_message (element, message);
1750 GST_STATE_BROADCAST (element);
1755 * gst_element_lost_state:
1756 * @element: a #GstElement the state is lost of
1758 * Brings the element to the lost state. The current state of the
1759 * element is copied to the pending state so that any call to
1760 * #gst_element_get_state() will return ASYNC.
1761 * This is mostly used for elements that lost their preroll buffer
1762 * in the PAUSED state after a flush, they become PAUSED again
1763 * if a new preroll buffer is queued.
1764 * This function can only be called when the element is currently
1765 * not in error or an async state change.
1767 * This function can only be called with the STATE_LOCK held.
1772 gst_element_lost_state (GstElement * element)
1774 g_return_if_fail (GST_IS_ELEMENT (element));
1776 if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING &&
1777 !GST_STATE_ERROR (element)) {
1778 GstState current_state = GST_STATE (element);
1780 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1781 "lost state of %s", gst_element_state_get_name (current_state));
1783 GST_STATE_PENDING (element) = current_state;
1784 GST_STATE_ERROR (element) = FALSE;
1789 * gst_element_set_state:
1790 * @element: a #GstElement to change state of.
1791 * @state: the element's new #GstState.
1793 * Sets the state of the element. This function will try to set the
1794 * requested state by going through all the intermediary states and calling
1795 * the class's state change function for each.
1797 * Returns: Result of the state change using #GstStateChangeReturn.
1801 GstStateChangeReturn
1802 gst_element_set_state (GstElement * element, GstState state)
1804 GstElementClass *oclass;
1806 GstStateChangeReturn return_val = GST_STATE_CHANGE_SUCCESS;
1807 GstStateChangeReturn ret;
1811 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
1813 GST_DEBUG_OBJECT (element, "set_state to %s",
1814 gst_element_state_get_name (state));
1815 /* get current element state, need to call the method so that
1816 * we call the virtual method and subclasses can implement their
1818 GST_TIME_TO_TIMEVAL (0, tv);
1819 ret = gst_element_get_state (element, ¤t, &pending, &tv);
1821 /* get the element state lock */
1822 GST_STATE_LOCK (element);
1823 /* this is the state we should go to */
1824 GST_STATE_FINAL (element) = state;
1825 if (ret == GST_STATE_CHANGE_ASYNC) {
1826 /* force next state keeping ASYNC, this is atomic as we hold
1828 gst_element_commit_state (element);
1829 gst_element_lost_state (element);
1832 /* start with the current state */
1833 current = GST_STATE (element);
1835 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "setting state from %s to %s",
1836 gst_element_state_get_name (current), gst_element_state_get_name (state));
1838 oclass = GST_ELEMENT_GET_CLASS (element);
1840 /* We always perform at least one state change, even if the
1841 * current state is equal to the required state. This is needed
1842 * for bins that sync their children. */
1846 /* calculate the pending state */
1847 if (current < state)
1848 pending = current + 1;
1849 else if (current > state)
1850 pending = current - 1;
1854 /* set the pending state variable */
1855 GST_STATE_PENDING (element) = pending;
1857 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1858 "%s: setting state from %s to %s",
1859 (pending != state ? "intermediate" : "final"),
1860 gst_element_state_get_name (current),
1861 gst_element_state_get_name (pending));
1863 /* call the state change function so it can set the state */
1864 if (oclass->change_state)
1865 return_val = (oclass->change_state) (element, GST_STATE_CHANGE (element));
1867 return_val = GST_STATE_CHANGE_FAILURE;
1869 /* clear the error and preroll flag, we need to do that after
1870 * calling the virtual change_state function so that it can use the
1871 * old previous value. */
1872 GST_STATE_ERROR (element) = FALSE;
1873 GST_STATE_NO_PREROLL (element) = FALSE;
1875 switch (return_val) {
1876 case GST_STATE_CHANGE_FAILURE:
1877 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1878 "have failed change_state return");
1879 /* state change failure exits the loop */
1880 gst_element_abort_state (element);
1882 case GST_STATE_CHANGE_ASYNC:
1883 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1884 "element will change state async");
1885 /* an async state change exits the loop, we can only
1886 * go to the next state change when this one completes. */
1888 case GST_STATE_CHANGE_SUCCESS:
1889 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1890 "element changed state successfully");
1891 /* we can commit the state now and proceed to the next state */
1892 gst_element_commit_state (element);
1893 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "committed state");
1895 case GST_STATE_CHANGE_NO_PREROLL:
1896 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1897 "element changed state successfully and can't preroll");
1898 /* we can commit the state now and proceed to the next state */
1899 gst_element_commit_state (element);
1900 GST_STATE_NO_PREROLL (element) = TRUE;
1901 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "committed state");
1904 goto invalid_return;
1906 /* get the current state of the element and see if we need to do more
1908 current = GST_STATE (element);
1910 while (current != state);
1913 GST_STATE_FINAL (element) = GST_STATE_VOID_PENDING;
1914 GST_STATE_UNLOCK (element);
1916 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "exit state change");
1923 GST_STATE_FINAL (element) = GST_STATE_VOID_PENDING;
1924 GST_STATE_UNLOCK (element);
1925 /* somebody added a GST_STATE_ and forgot to do stuff here ! */
1926 g_critical ("unknown return value %d from a state change function",
1928 return GST_STATE_CHANGE_FAILURE;
1932 /* gst_iterator_fold functions for pads_activate */
1935 activate_pads (GstPad * pad, GValue * ret, gboolean * active)
1937 if (!gst_pad_set_active (pad, *active))
1938 g_value_set_boolean (ret, FALSE);
1940 gst_pad_set_caps (pad, NULL);
1942 gst_object_unref (pad);
1946 /* returns false on error or early cutout of the fold, true otherwise */
1948 iterator_fold_with_resync (GstIterator * iter, GstIteratorFoldFunction func,
1949 GValue * ret, gpointer user_data)
1951 GstIteratorResult ires;
1952 gboolean res = TRUE;
1955 ires = gst_iterator_fold (iter, func, ret, user_data);
1958 case GST_ITERATOR_RESYNC:
1959 gst_iterator_resync (iter);
1961 case GST_ITERATOR_DONE:
1974 /* is called with STATE_LOCK
1977 gst_element_pads_activate (GstElement * element, gboolean active)
1979 GValue ret = { 0, };
1983 /* no need to unset this later, it's just a boolean */
1984 g_value_init (&ret, G_TYPE_BOOLEAN);
1985 g_value_set_boolean (&ret, TRUE);
1987 iter = gst_element_iterate_src_pads (element);
1988 fold_ok = iterator_fold_with_resync
1989 (iter, (GstIteratorFoldFunction) activate_pads, &ret, &active);
1990 gst_iterator_free (iter);
1991 if (!fold_ok || !g_value_get_boolean (&ret))
1994 iter = gst_element_iterate_sink_pads (element);
1995 fold_ok = iterator_fold_with_resync
1996 (iter, (GstIteratorFoldFunction) activate_pads, &ret, &active);
1997 gst_iterator_free (iter);
1998 if (!fold_ok || !g_value_get_boolean (&ret))
2004 /* is called with STATE_LOCK */
2005 static GstStateChangeReturn
2006 gst_element_change_state (GstElement * element, GstStateChange transition)
2008 GstState state, pending;
2009 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
2011 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2013 state = GST_STATE (element);
2014 pending = GST_STATE_PENDING (element);
2016 /* if the element already is in the given state, we just return success */
2017 if (pending == GST_STATE_VOID_PENDING || state == GST_STATE_PENDING (element)) {
2018 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2019 "element is already in the %s state",
2020 gst_element_state_get_name (state));
2021 if (GST_STATE_NO_PREROLL (element))
2022 return GST_STATE_CHANGE_NO_PREROLL;
2024 return GST_STATE_CHANGE_SUCCESS;
2027 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element,
2028 "default handler tries setting state from %s to %s (%04x)",
2029 gst_element_state_get_name (state),
2030 gst_element_state_get_name (pending), transition);
2032 switch (transition) {
2033 case GST_STATE_CHANGE_NULL_TO_READY:
2035 case GST_STATE_CHANGE_READY_TO_PAUSED:
2036 if (!gst_element_pads_activate (element, TRUE)) {
2037 result = GST_STATE_CHANGE_FAILURE;
2040 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2042 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2044 case GST_STATE_CHANGE_PAUSED_TO_READY:
2045 case GST_STATE_CHANGE_READY_TO_NULL:
2046 /* deactivate pads in both cases, since they are activated on
2047 ready->paused but the element might not have made it to paused */
2048 if (!gst_element_pads_activate (element, FALSE)) {
2049 result = GST_STATE_CHANGE_FAILURE;
2052 element->base_time = 0;
2053 GST_UNLOCK (element);
2057 /* this will catch real but unhandled state changes;
2058 * can only be caused by:
2059 * - a new state was added
2060 * - somehow the element was asked to jump across an intermediate state
2062 g_warning ("Unhandled state change from %s to %s",
2063 gst_element_state_get_name (state),
2064 gst_element_state_get_name (pending));
2072 * gst_element_get_factory:
2073 * @element: a #GstElement to request the element factory of.
2075 * Retrieves the factory that was used to create this element.
2077 * Returns: the #GstElementFactory used for creating this element.
2080 gst_element_get_factory (GstElement * element)
2082 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
2084 return GST_ELEMENT_GET_CLASS (element)->elementfactory;
2088 gst_element_dispose (GObject * object)
2090 GstElement *element = GST_ELEMENT (object);
2092 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "dispose");
2094 /* first we break all our links with the outside */
2095 while (element->pads) {
2096 gst_element_remove_pad (element, GST_PAD_CAST (element->pads->data));
2098 if (G_UNLIKELY (element->pads != 0)) {
2099 g_critical ("could not remove pads from element %s",
2100 GST_STR_NULL (GST_OBJECT_NAME (object)));
2104 gst_object_replace ((GstObject **) & element->clock, NULL);
2105 gst_object_replace ((GstObject **) & element->bus, NULL);
2106 GST_UNLOCK (element);
2108 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "dispose parent");
2110 G_OBJECT_CLASS (parent_class)->dispose (object);
2114 gst_element_finalize (GObject * object)
2116 GstElement *element = GST_ELEMENT (object);
2118 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize");
2120 GST_STATE_LOCK (element);
2121 if (element->state_cond)
2122 g_cond_free (element->state_cond);
2123 element->state_cond = NULL;
2124 GST_STATE_UNLOCK (element);
2125 g_mutex_free (element->state_lock);
2127 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize parent");
2129 G_OBJECT_CLASS (parent_class)->finalize (object);
2132 #ifndef GST_DISABLE_LOADSAVE
2134 * gst_element_save_thyself:
2135 * @element: a #GstElement to save.
2136 * @parent: the xml parent node.
2138 * Saves the element as part of the given XML structure.
2140 * Returns: the new #xmlNodePtr.
2143 gst_element_save_thyself (GstObject * object, xmlNodePtr parent)
2146 GstElementClass *oclass;
2147 GParamSpec **specs, *spec;
2150 GValue value = { 0, };
2151 GstElement *element;
2153 g_return_val_if_fail (GST_IS_ELEMENT (object), parent);
2155 element = GST_ELEMENT (object);
2157 oclass = GST_ELEMENT_GET_CLASS (element);
2159 xmlNewChild (parent, NULL, (xmlChar *) "name",
2160 (xmlChar *) GST_ELEMENT_NAME (element));
2162 if (oclass->elementfactory != NULL) {
2163 GstElementFactory *factory = (GstElementFactory *) oclass->elementfactory;
2165 xmlNewChild (parent, NULL, (xmlChar *) "type",
2166 (xmlChar *) GST_PLUGIN_FEATURE (factory)->name);
2170 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &nspecs);
2172 for (i = 0; i < nspecs; i++) {
2174 if (spec->flags & G_PARAM_READABLE) {
2178 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
2180 g_object_get_property (G_OBJECT (element), spec->name, &value);
2181 param = xmlNewChild (parent, NULL, (xmlChar *) "param", NULL);
2182 xmlNewChild (param, NULL, (xmlChar *) "name", (xmlChar *) spec->name);
2184 if (G_IS_PARAM_SPEC_STRING (spec))
2185 contents = g_value_dup_string (&value);
2186 else if (G_IS_PARAM_SPEC_ENUM (spec))
2187 contents = g_strdup_printf ("%d", g_value_get_enum (&value));
2188 else if (G_IS_PARAM_SPEC_INT64 (spec))
2189 contents = g_strdup_printf ("%" G_GINT64_FORMAT,
2190 g_value_get_int64 (&value));
2192 contents = g_strdup_value_contents (&value);
2194 xmlNewChild (param, NULL, (xmlChar *) "value", (xmlChar *) contents);
2197 g_value_unset (&value);
2203 pads = GST_ELEMENT_PADS (element);
2206 GstPad *pad = GST_PAD (pads->data);
2208 /* figure out if it's a direct pad or a ghostpad */
2209 if (GST_ELEMENT (GST_OBJECT_PARENT (pad)) == element) {
2210 xmlNodePtr padtag = xmlNewChild (parent, NULL, (xmlChar *) "pad", NULL);
2212 gst_object_save_thyself (GST_OBJECT (pad), padtag);
2214 pads = g_list_next (pads);
2221 gst_element_restore_thyself (GstObject * object, xmlNodePtr self)
2223 xmlNodePtr children;
2224 GstElement *element;
2226 gchar *value = NULL;
2228 element = GST_ELEMENT (object);
2229 g_return_if_fail (element != NULL);
2232 children = self->xmlChildrenNode;
2234 if (!strcmp ((char *) children->name, "param")) {
2235 xmlNodePtr child = children->xmlChildrenNode;
2238 if (!strcmp ((char *) child->name, "name")) {
2239 name = (gchar *) xmlNodeGetContent (child);
2240 } else if (!strcmp ((char *) child->name, "value")) {
2241 value = (gchar *) xmlNodeGetContent (child);
2243 child = child->next;
2245 /* FIXME: can this just be g_object_set ? */
2246 gst_util_set_object_arg (G_OBJECT (element), name, value);
2247 /* g_object_set (G_OBJECT (element), name, value, NULL); */
2251 children = children->next;
2255 children = self->xmlChildrenNode;
2257 if (!strcmp ((char *) children->name, "pad")) {
2258 gst_pad_load_and_link (children, GST_OBJECT (element));
2260 children = children->next;
2263 if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
2264 (GST_OBJECT_CLASS (parent_class)->restore_thyself) (object, self);
2266 #endif /* GST_DISABLE_LOADSAVE */
2269 gst_element_set_bus_func (GstElement * element, GstBus * bus)
2271 g_return_if_fail (GST_IS_ELEMENT (element));
2273 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element, "setting bus to %p", bus);
2276 gst_object_replace ((GstObject **) & GST_ELEMENT_BUS (element),
2278 GST_UNLOCK (element);
2282 * gst_element_set_bus:
2283 * @element: a #GstElement to set the bus of.
2284 * @bus: the #GstBus to set.
2286 * Sets the bus of the element. For internal use only, unless you're
2292 gst_element_set_bus (GstElement * element, GstBus * bus)
2294 GstElementClass *oclass;
2296 g_return_if_fail (GST_IS_ELEMENT (element));
2298 oclass = GST_ELEMENT_GET_CLASS (element);
2300 if (oclass->set_bus)
2301 oclass->set_bus (element, bus);
2305 * gst_element_get_bus:
2306 * @element: a #GstElement to get the bus of.
2308 * Returns the bus of the element.
2310 * Returns: the element's #GstBus. unref after usage.
2315 gst_element_get_bus (GstElement * element)
2317 GstBus *result = NULL;
2319 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
2322 result = GST_ELEMENT_BUS (element);
2323 gst_object_ref (result);
2324 GST_UNLOCK (element);
2326 GST_DEBUG_OBJECT (element, "got bus %" GST_PTR_FORMAT, result);