2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2004 Wim Taymans <wim@fluendo.com>
5 * gstelement.c: The base element, all elements derive from this
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 * @short_description: Abstract base class for all pipeline elements
26 * @see_also: #GstElementFactory, #GstPad
28 * GstElement is the abstract base class needed to construct an element that
29 * can be used in a GStreamer pipeline. Please refer to the plugin writers
30 * guide for more information on creating #GstElement subclasses.
32 * The name of a #GstElement can be get with gst_element_get_name() and set with
33 * gst_element_set_name(). For speed, GST_ELEMENT_NAME() can be used in the
34 * core when using the appropriate locking. Do not use this in plug-ins or
35 * applications in order to retain ABI compatibility.
37 * All elements have pads (of the type #GstPad). These pads link to pads on
38 * other elements. #GstBuffer flow between these linked pads.
39 * A #GstElement has a #GList of #GstPad structures for all their input (or sink)
40 * and output (or source) pads.
41 * Core and plug-in writers can add and remove pads with gst_element_add_pad()
42 * and gst_element_remove_pad().
44 * A pad of an element can be retrieved by name with gst_element_get_pad().
45 * An iterator of all pads can be retrieved with gst_element_iterate_pads().
47 * Elements can be linked through their pads.
48 * If the link is straightforward, use the gst_element_link()
49 * convenience function to link two elements, or gst_element_link_many()
50 * for more elements in a row.
51 * Use gst_element_link_filtered() to link two elements constrained by
52 * a specified set of #GstCaps.
53 * For finer control, use gst_element_link_pads() and
54 * gst_element_link_pads_filtered() to specify the pads to link on
55 * each element by name.
57 * Each element has a state (see #GstState). You can get and set the state
58 * of an element with gst_element_get_state() and gst_element_set_state().
59 * Setting a state triggers a #GstStateChange. To get a string representation
60 * of a #GstState, use gst_element_state_get_name().
62 * You can get and set a #GstClock on an element using gst_element_get_clock()
63 * and gst_element_set_clock().
64 * Some elements can provide a clock for the pipeline if
65 * gst_element_provides_clock() returns %TRUE. With the
66 * gst_element_provide_clock() method one can retrieve the clock provided by
68 * Not all elements require a clock to operate correctly. If
69 * gst_element_requires_clock() returns %TRUE, a clock should be set on the
70 * element with gst_element_set_clock().
72 * Note that clock slection and distribution is normally handled by the
73 * toplevel #GstPipeline so the clock functions are only to be used in very
74 * specific situations.
76 * Last reviewed on 2009-05-29 (0.10.24)
79 #include "gst_private.h"
82 #include <gobject/gvaluecollector.h>
84 #include "gstelement.h"
85 #include "gstelementmetadata.h"
86 #include "gstenumtypes.h"
88 #include "gstmarshal.h"
94 #include "gst-i18n-lib.h"
96 /* Element signals and args */
112 static void gst_element_class_init (GstElementClass * klass);
113 static void gst_element_init (GstElement * element);
114 static void gst_element_base_class_init (gpointer g_class);
115 static void gst_element_base_class_finalize (gpointer g_class);
117 static void gst_element_dispose (GObject * object);
118 static void gst_element_finalize (GObject * object);
120 static GstStateChangeReturn gst_element_change_state_func (GstElement * element,
121 GstStateChange transition);
122 static GstStateChangeReturn gst_element_get_state_func (GstElement * element,
123 GstState * state, GstState * pending, GstClockTime timeout);
124 static GstStateChangeReturn gst_element_set_state_func (GstElement * element,
126 static void gst_element_set_bus_func (GstElement * element, GstBus * bus);
128 static gboolean gst_element_default_send_event (GstElement * element,
130 static gboolean gst_element_default_query (GstElement * element,
133 static GstPadTemplate
134 * gst_element_class_get_request_pad_template (GstElementClass *
135 element_class, const gchar * name);
137 static GstObjectClass *parent_class = NULL;
138 static guint gst_element_signals[LAST_SIGNAL] = { 0 };
140 /* this is used in gstelementfactory.c:gst_element_register() */
141 GQuark _gst_elementclass_factory = 0;
144 gst_element_get_type (void)
146 static volatile gsize gst_element_type = 0;
148 if (g_once_init_enter (&gst_element_type)) {
150 static const GTypeInfo element_info = {
151 sizeof (GstElementClass),
152 gst_element_base_class_init,
153 gst_element_base_class_finalize,
154 (GClassInitFunc) gst_element_class_init,
159 (GInstanceInitFunc) gst_element_init,
163 _type = g_type_register_static (GST_TYPE_OBJECT, "GstElement",
164 &element_info, G_TYPE_FLAG_ABSTRACT);
166 _gst_elementclass_factory =
167 g_quark_from_static_string ("GST_ELEMENTCLASS_FACTORY");
168 g_once_init_leave (&gst_element_type, _type);
170 return gst_element_type;
174 gst_element_class_init (GstElementClass * klass)
176 GObjectClass *gobject_class;
177 GstObjectClass *gstobject_class;
179 gobject_class = (GObjectClass *) klass;
180 gstobject_class = (GstObjectClass *) klass;
182 parent_class = g_type_class_peek_parent (klass);
185 * GstElement::pad-added:
186 * @gstelement: the object which received the signal
187 * @new_pad: the pad that has been added
189 * a new #GstPad has been added to the element.
191 gst_element_signals[PAD_ADDED] =
192 g_signal_new ("pad-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
193 G_STRUCT_OFFSET (GstElementClass, pad_added), NULL, NULL,
194 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
196 * GstElement::pad-removed:
197 * @gstelement: the object which received the signal
198 * @old_pad: the pad that has been removed
200 * a #GstPad has been removed from the element
202 gst_element_signals[PAD_REMOVED] =
203 g_signal_new ("pad-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
204 G_STRUCT_OFFSET (GstElementClass, pad_removed), NULL, NULL,
205 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
207 * GstElement::no-more-pads:
208 * @gstelement: the object which received the signal
210 * This signals that the element will not generate more dynamic pads.
212 gst_element_signals[NO_MORE_PADS] =
213 g_signal_new ("no-more-pads", G_TYPE_FROM_CLASS (klass),
214 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstElementClass, no_more_pads), NULL,
215 NULL, gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
217 gobject_class->dispose = gst_element_dispose;
218 gobject_class->finalize = gst_element_finalize;
220 klass->change_state = GST_DEBUG_FUNCPTR (gst_element_change_state_func);
221 klass->set_state = GST_DEBUG_FUNCPTR (gst_element_set_state_func);
222 klass->get_state = GST_DEBUG_FUNCPTR (gst_element_get_state_func);
223 klass->set_bus = GST_DEBUG_FUNCPTR (gst_element_set_bus_func);
224 klass->query = GST_DEBUG_FUNCPTR (gst_element_default_query);
225 klass->send_event = GST_DEBUG_FUNCPTR (gst_element_default_send_event);
226 klass->numpadtemplates = 0;
228 klass->elementfactory = NULL;
232 gst_element_base_class_init (gpointer g_class)
234 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
236 /* FIXME 0.11: Instead of clearing the
237 * pad template list copy the list and increase the refcount of
238 * the pad templates by one.
240 * This will make it possible to add pad templates and set element
241 * details in the class_init functions and is the real GObject way
243 * See http://bugzilla.gnome.org/show_bug.cgi?id=491501
245 element_class->metadata = gst_structure_empty_new ("metadata");
246 element_class->padtemplates = NULL;
248 /* set the factory, see gst_element_register() */
249 element_class->elementfactory =
250 g_type_get_qdata (G_TYPE_FROM_CLASS (element_class),
251 _gst_elementclass_factory);
252 GST_DEBUG ("type %s : factory %p", G_OBJECT_CLASS_NAME (element_class),
253 element_class->elementfactory);
257 gst_element_base_class_finalize (gpointer g_class)
259 GstElementClass *klass = GST_ELEMENT_CLASS (g_class);
261 g_list_foreach (klass->padtemplates, (GFunc) gst_object_unref, NULL);
262 g_list_free (klass->padtemplates);
264 gst_structure_free (klass->metadata);
268 gst_element_init (GstElement * element)
270 GST_STATE (element) = GST_STATE_NULL;
271 GST_STATE_TARGET (element) = GST_STATE_NULL;
272 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
273 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
274 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
276 /* FIXME 0.11: Store this directly in the instance struct */
277 element->state_lock = g_slice_new (GStaticRecMutex);
278 g_static_rec_mutex_init (element->state_lock);
279 element->state_cond = g_cond_new ();
283 * gst_element_release_request_pad:
284 * @element: a #GstElement to release the request pad of.
285 * @pad: the #GstPad to release.
287 * Makes the element free the previously requested pad as obtained
288 * with gst_element_get_request_pad().
290 * This does not unref the pad. If the pad was created by using
291 * gst_element_get_request_pad(), gst_element_release_request_pad() needs to be
292 * followed by gst_object_unref() to free the @pad.
297 gst_element_release_request_pad (GstElement * element, GstPad * pad)
299 GstElementClass *oclass;
301 g_return_if_fail (GST_IS_ELEMENT (element));
302 g_return_if_fail (GST_IS_PAD (pad));
304 oclass = GST_ELEMENT_GET_CLASS (element);
306 /* if the element implements a custom release function we call that, else we
307 * simply remove the pad from the element */
308 if (oclass->release_pad)
309 (oclass->release_pad) (element, pad);
311 gst_element_remove_pad (element, pad);
315 * gst_element_requires_clock:
316 * @element: a #GstElement to query
318 * Query if the element requires a clock.
320 * Returns: %TRUE if the element requires a clock
325 gst_element_requires_clock (GstElement * element)
329 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
331 result = (GST_ELEMENT_GET_CLASS (element)->set_clock != NULL);
337 * gst_element_provides_clock:
338 * @element: a #GstElement to query
340 * Query if the element provides a clock. A #GstClock provided by an
341 * element can be used as the global #GstClock for the pipeline.
342 * An element that can provide a clock is only required to do so in the PAUSED
343 * state, this means when it is fully negotiated and has allocated the resources
344 * to operate the clock.
346 * Returns: %TRUE if the element provides a clock
351 gst_element_provides_clock (GstElement * element)
355 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
357 result = (GST_ELEMENT_GET_CLASS (element)->provide_clock != NULL);
363 * gst_element_provide_clock:
364 * @element: a #GstElement to query
366 * Get the clock provided by the given element.
367 * <note>An element is only required to provide a clock in the PAUSED
368 * state. Some elements can provide a clock in other states.</note>
370 * Returns: the GstClock provided by the element or %NULL
371 * if no clock could be provided. Unref after usage.
376 gst_element_provide_clock (GstElement * element)
378 GstClock *result = NULL;
379 GstElementClass *oclass;
381 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
383 oclass = GST_ELEMENT_GET_CLASS (element);
385 if (oclass->provide_clock)
386 result = oclass->provide_clock (element);
392 * gst_element_set_clock:
393 * @element: a #GstElement to set the clock for.
394 * @clock: the #GstClock to set for the element.
396 * Sets the clock for the element. This function increases the
397 * refcount on the clock. Any previously set clock on the object
400 * Returns: %TRUE if the element accepted the clock. An element can refuse a
401 * clock when it, for example, is not able to slave its internal clock to the
402 * @clock or when it requires a specific clock to operate.
407 gst_element_set_clock (GstElement * element, GstClock * clock)
409 GstElementClass *oclass;
413 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
414 g_return_val_if_fail (clock == NULL || GST_IS_CLOCK (clock), FALSE);
416 oclass = GST_ELEMENT_GET_CLASS (element);
418 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element, "setting clock %p", clock);
420 if (oclass->set_clock)
421 res = oclass->set_clock (element, clock);
424 /* only update the clock pointer if the element accepted the clock */
425 GST_OBJECT_LOCK (element);
426 clock_p = &element->clock;
427 gst_object_replace ((GstObject **) clock_p, (GstObject *) clock);
428 GST_OBJECT_UNLOCK (element);
434 * gst_element_get_clock:
435 * @element: a #GstElement to get the clock of.
437 * Gets the currently configured clock of the element. This is the clock as was
438 * last set with gst_element_set_clock().
440 * Returns: the #GstClock of the element. unref after usage.
445 gst_element_get_clock (GstElement * element)
449 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
451 GST_OBJECT_LOCK (element);
452 if ((result = element->clock))
453 gst_object_ref (result);
454 GST_OBJECT_UNLOCK (element);
460 * gst_element_set_base_time:
461 * @element: a #GstElement.
462 * @time: the base time to set.
464 * Set the base time of an element. See gst_element_get_base_time().
469 gst_element_set_base_time (GstElement * element, GstClockTime time)
473 g_return_if_fail (GST_IS_ELEMENT (element));
475 GST_OBJECT_LOCK (element);
476 old = element->base_time;
477 element->base_time = time;
478 GST_OBJECT_UNLOCK (element);
480 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element,
481 "set base_time=%" GST_TIME_FORMAT ", old %" GST_TIME_FORMAT,
482 GST_TIME_ARGS (time), GST_TIME_ARGS (old));
486 * gst_element_get_base_time:
487 * @element: a #GstElement.
489 * Returns the base time of the element. The base time is the
490 * absolute time of the clock when this element was last put to
491 * PLAYING. Subtracting the base time from the clock time gives
492 * the running time of the element.
494 * Returns: the base time of the element.
499 gst_element_get_base_time (GstElement * element)
503 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
505 GST_OBJECT_LOCK (element);
506 result = element->base_time;
507 GST_OBJECT_UNLOCK (element);
513 * gst_element_set_start_time:
514 * @element: a #GstElement.
515 * @time: the base time to set.
517 * Set the start time of an element. The start time of the element is the
518 * running time of the element when it last went to the PAUSED state. In READY
519 * or after a flushing seek, it is set to 0.
521 * Toplevel elements like #GstPipeline will manage the start_time and
522 * base_time on its children. Setting the start_time to #GST_CLOCK_TIME_NONE
523 * on such a toplevel element will disable the distribution of the base_time to
524 * the children and can be useful if the application manages the base_time
525 * itself, for example if you want to synchronize capture from multiple
526 * pipelines, and you can also ensure that the pipelines have the same clock.
533 gst_element_set_start_time (GstElement * element, GstClockTime time)
537 g_return_if_fail (GST_IS_ELEMENT (element));
539 GST_OBJECT_LOCK (element);
540 old = GST_ELEMENT_START_TIME (element);
541 GST_ELEMENT_START_TIME (element) = time;
542 GST_OBJECT_UNLOCK (element);
544 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element,
545 "set start_time=%" GST_TIME_FORMAT ", old %" GST_TIME_FORMAT,
546 GST_TIME_ARGS (time), GST_TIME_ARGS (old));
550 * gst_element_get_start_time:
551 * @element: a #GstElement.
553 * Returns the start time of the element. The start time is the
554 * running time of the clock when this element was last put to PAUSED.
556 * Usually the start_time is managed by a toplevel element such as
561 * Returns: the start time of the element.
566 gst_element_get_start_time (GstElement * element)
570 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
572 GST_OBJECT_LOCK (element);
573 result = GST_ELEMENT_START_TIME (element);
574 GST_OBJECT_UNLOCK (element);
580 * gst_element_is_indexable:
581 * @element: a #GstElement.
583 * Queries if the element can be indexed.
585 * Returns: TRUE if the element can be indexed.
590 gst_element_is_indexable (GstElement * element)
594 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
596 result = (GST_ELEMENT_GET_CLASS (element)->set_index != NULL);
602 * gst_element_set_index:
603 * @element: a #GstElement.
604 * @index: a #GstIndex.
606 * Set @index on the element. The refcount of the index
607 * will be increased, any previously set index is unreffed.
612 gst_element_set_index (GstElement * element, GstIndex * index)
614 GstElementClass *oclass;
616 g_return_if_fail (GST_IS_ELEMENT (element));
617 g_return_if_fail (index == NULL || GST_IS_INDEX (index));
619 oclass = GST_ELEMENT_GET_CLASS (element);
621 if (oclass->set_index)
622 oclass->set_index (element, index);
626 * gst_element_get_index:
627 * @element: a #GstElement.
629 * Gets the index from the element.
631 * Returns: a #GstIndex or %NULL when no index was set on the
632 * element. unref after usage.
637 gst_element_get_index (GstElement * element)
639 GstElementClass *oclass;
640 GstIndex *result = NULL;
642 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
644 oclass = GST_ELEMENT_GET_CLASS (element);
646 if (oclass->get_index)
647 result = oclass->get_index (element);
653 * gst_element_add_pad:
654 * @element: a #GstElement to add the pad to.
655 * @pad: the #GstPad to add to the element.
657 * Adds a pad (link point) to @element. @pad's parent will be set to @element;
658 * see gst_object_set_parent() for refcounting information.
660 * Pads are not automatically activated so elements should perform the needed
661 * steps to activate the pad in case this pad is added in the PAUSED or PLAYING
662 * state. See gst_pad_set_active() for more information about activating pads.
664 * The pad and the element should be unlocked when calling this function.
666 * This function will emit the #GstElement::pad-added signal on the element.
668 * Returns: %TRUE if the pad could be added. This function can fail when
669 * a pad with the same name already existed or the pad already had another
675 gst_element_add_pad (GstElement * element, GstPad * pad)
680 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
681 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
683 /* locking pad to look at the name */
684 GST_OBJECT_LOCK (pad);
685 pad_name = g_strdup (GST_PAD_NAME (pad));
686 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "adding pad '%s'",
687 GST_STR_NULL (pad_name));
688 flushing = GST_PAD_IS_FLUSHING (pad);
689 GST_OBJECT_UNLOCK (pad);
691 /* then check to see if there's already a pad by that name here */
692 GST_OBJECT_LOCK (element);
693 if (G_UNLIKELY (!gst_object_check_uniqueness (element->pads, pad_name)))
696 /* try to set the pad's parent */
697 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (pad),
698 GST_OBJECT_CAST (element))))
701 /* check for flushing pads */
702 if (flushing && (GST_STATE (element) > GST_STATE_READY ||
703 GST_STATE_NEXT (element) == GST_STATE_PAUSED)) {
704 g_warning ("adding flushing pad '%s' to running element '%s', you need to "
705 "use gst_pad_set_active(pad,TRUE) before adding it.",
706 GST_STR_NULL (pad_name), GST_ELEMENT_NAME (element));
708 GST_OBJECT_LOCK (pad);
709 GST_PAD_UNSET_FLUSHING (pad);
710 GST_OBJECT_UNLOCK (pad);
715 /* add it to the list */
716 switch (gst_pad_get_direction (pad)) {
718 element->srcpads = g_list_prepend (element->srcpads, pad);
719 element->numsrcpads++;
722 element->sinkpads = g_list_prepend (element->sinkpads, pad);
723 element->numsinkpads++;
728 element->pads = g_list_prepend (element->pads, pad);
730 element->pads_cookie++;
731 GST_OBJECT_UNLOCK (element);
733 /* emit the PAD_ADDED signal */
734 g_signal_emit (element, gst_element_signals[PAD_ADDED], 0, pad);
741 g_critical ("Padname %s is not unique in element %s, not adding",
742 pad_name, GST_ELEMENT_NAME (element));
743 GST_OBJECT_UNLOCK (element);
750 ("Pad %s already has parent when trying to add to element %s",
751 pad_name, GST_ELEMENT_NAME (element));
752 GST_OBJECT_UNLOCK (element);
758 GST_OBJECT_LOCK (pad);
760 ("Trying to add pad %s to element %s, but it has no direction",
761 GST_OBJECT_NAME (pad), GST_ELEMENT_NAME (element));
762 GST_OBJECT_UNLOCK (pad);
763 GST_OBJECT_UNLOCK (element);
769 * gst_element_remove_pad:
770 * @element: a #GstElement to remove pad from.
771 * @pad: the #GstPad to remove from the element.
773 * Removes @pad from @element. @pad will be destroyed if it has not been
774 * referenced elsewhere using gst_object_unparent().
776 * This function is used by plugin developers and should not be used
777 * by applications. Pads that were dynamically requested from elements
778 * with gst_element_get_request_pad() should be released with the
779 * gst_element_release_request_pad() function instead.
781 * Pads are not automatically deactivated so elements should perform the needed
782 * steps to deactivate the pad in case this pad is removed in the PAUSED or
783 * PLAYING state. See gst_pad_set_active() for more information about
786 * The pad and the element should be unlocked when calling this function.
788 * This function will emit the #GstElement::pad-removed signal on the element.
790 * Returns: %TRUE if the pad could be removed. Can return %FALSE if the
791 * pad does not belong to the provided element.
796 gst_element_remove_pad (GstElement * element, GstPad * pad)
800 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
801 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
803 /* locking pad to look at the name and parent */
804 GST_OBJECT_LOCK (pad);
805 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "removing pad '%s'",
806 GST_STR_NULL (GST_PAD_NAME (pad)));
808 if (G_UNLIKELY (GST_PAD_PARENT (pad) != element))
810 GST_OBJECT_UNLOCK (pad);
813 if ((peer = gst_pad_get_peer (pad))) {
814 /* window for MT unsafeness, someone else could unlink here
815 * and then we call unlink with wrong pads. The unlink
816 * function would catch this and safely return failed. */
817 if (GST_PAD_IS_SRC (pad))
818 gst_pad_unlink (pad, peer);
820 gst_pad_unlink (peer, pad);
822 gst_object_unref (peer);
825 GST_OBJECT_LOCK (element);
826 /* remove it from the list */
827 switch (gst_pad_get_direction (pad)) {
829 element->srcpads = g_list_remove (element->srcpads, pad);
830 element->numsrcpads--;
833 element->sinkpads = g_list_remove (element->sinkpads, pad);
834 element->numsinkpads--;
837 g_critical ("Removing pad without direction???");
840 element->pads = g_list_remove (element->pads, pad);
842 element->pads_cookie++;
843 GST_OBJECT_UNLOCK (element);
845 /* emit the PAD_REMOVED signal before unparenting and losing the last ref. */
846 g_signal_emit (element, gst_element_signals[PAD_REMOVED], 0, pad);
848 gst_object_unparent (GST_OBJECT_CAST (pad));
855 /* FIXME, locking order? */
856 GST_OBJECT_LOCK (element);
857 g_critical ("Padname %s:%s does not belong to element %s when removing",
858 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
859 GST_OBJECT_UNLOCK (element);
860 GST_OBJECT_UNLOCK (pad);
866 * gst_element_no_more_pads:
867 * @element: a #GstElement
869 * Use this function to signal that the element does not expect any more pads
870 * to show up in the current pipeline. This function should be called whenever
871 * pads have been added by the element itself. Elements with #GST_PAD_SOMETIMES
872 * pad templates use this in combination with autopluggers to figure out that
873 * the element is done initializing its pads.
875 * This function emits the #GstElement::no-more-pads signal.
880 gst_element_no_more_pads (GstElement * element)
882 g_return_if_fail (GST_IS_ELEMENT (element));
884 g_signal_emit (element, gst_element_signals[NO_MORE_PADS], 0);
888 pad_compare_name (GstPad * pad1, const gchar * name)
892 GST_OBJECT_LOCK (pad1);
893 result = strcmp (GST_PAD_NAME (pad1), name);
894 GST_OBJECT_UNLOCK (pad1);
900 * gst_element_get_static_pad:
901 * @element: a #GstElement to find a static pad of.
902 * @name: the name of the static #GstPad to retrieve.
904 * Retrieves a pad from @element by name. This version only retrieves
905 * already-existing (i.e. 'static') pads.
907 * Returns: the requested #GstPad if found, otherwise %NULL. unref after
913 gst_element_get_static_pad (GstElement * element, const gchar * name)
916 GstPad *result = NULL;
918 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
919 g_return_val_if_fail (name != NULL, NULL);
921 GST_OBJECT_LOCK (element);
923 g_list_find_custom (element->pads, name, (GCompareFunc) pad_compare_name);
925 result = GST_PAD_CAST (find->data);
926 gst_object_ref (result);
929 if (result == NULL) {
930 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "no such pad '%s' in element \"%s\"",
931 name, GST_ELEMENT_NAME (element));
933 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
934 GST_ELEMENT_NAME (element), name);
936 GST_OBJECT_UNLOCK (element);
942 gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
945 GstPad *newpad = NULL;
946 GstElementClass *oclass;
948 oclass = GST_ELEMENT_GET_CLASS (element);
950 if (oclass->request_new_pad)
951 newpad = (oclass->request_new_pad) (element, templ, name);
954 gst_object_ref (newpad);
960 * gst_element_get_request_pad:
961 * @element: a #GstElement to find a request pad of.
962 * @name: the name of the request #GstPad to retrieve.
964 * Retrieves a pad from the element by name. This version only retrieves
965 * request pads. The pad should be released with
966 * gst_element_release_request_pad().
968 * Returns: requested #GstPad if found, otherwise %NULL. Release after usage.
971 gst_element_get_request_pad (GstElement * element, const gchar * name)
973 GstPadTemplate *templ = NULL;
975 const gchar *req_name = NULL;
976 gboolean templ_found = FALSE;
979 gchar *str, *endptr = NULL;
980 GstElementClass *class;
982 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
983 g_return_val_if_fail (name != NULL, NULL);
985 class = GST_ELEMENT_GET_CLASS (element);
987 /* if the name contains a %, we assume it's the complete template name. Get
988 * the template and try to get a pad */
989 if (strstr (name, "%")) {
990 templ = gst_element_class_get_request_pad_template (class, name);
995 /* there is no % in the name, try to find a matching template */
996 list = class->padtemplates;
997 while (!templ_found && list) {
998 templ = (GstPadTemplate *) list->data;
999 if (templ->presence == GST_PAD_REQUEST) {
1000 GST_CAT_DEBUG (GST_CAT_PADS, "comparing %s to %s", name,
1001 templ->name_template);
1002 /* see if we find an exact match */
1003 if (strcmp (name, templ->name_template) == 0) {
1008 /* Because of sanity checks in gst_pad_template_new(), we know that %s
1009 and %d, occurring at the end of the name_template, are the only
1011 else if ((str = strchr (templ->name_template, '%'))
1012 && strncmp (templ->name_template, name,
1013 str - templ->name_template) == 0
1014 && strlen (name) > str - templ->name_template) {
1015 data = name + (str - templ->name_template);
1016 if (*(str + 1) == 'd') {
1020 tmp = strtol (data, &endptr, 10);
1021 if (endptr && *endptr == '\0') {
1041 pad = gst_element_request_pad (element, templ, req_name);
1046 static GstIteratorItem
1047 iterate_pad (GstIterator * it, GstPad * pad)
1049 gst_object_ref (pad);
1050 return GST_ITERATOR_ITEM_PASS;
1053 static GstIterator *
1054 gst_element_iterate_pad_list (GstElement * element, GList ** padlist)
1056 GstIterator *result;
1058 GST_OBJECT_LOCK (element);
1059 gst_object_ref (element);
1060 result = gst_iterator_new_list (GST_TYPE_PAD,
1061 GST_OBJECT_GET_LOCK (element),
1062 &element->pads_cookie,
1065 (GstIteratorItemFunction) iterate_pad,
1066 (GstIteratorDisposeFunction) gst_object_unref);
1067 GST_OBJECT_UNLOCK (element);
1073 * gst_element_iterate_pads:
1074 * @element: a #GstElement to iterate pads of.
1076 * Retrieves an iterattor of @element's pads. The iterator should
1077 * be freed after usage.
1079 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1084 gst_element_iterate_pads (GstElement * element)
1086 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1088 return gst_element_iterate_pad_list (element, &element->pads);
1092 * gst_element_iterate_src_pads:
1093 * @element: a #GstElement.
1095 * Retrieves an iterator of @element's source pads.
1097 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1102 gst_element_iterate_src_pads (GstElement * element)
1104 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1106 return gst_element_iterate_pad_list (element, &element->srcpads);
1110 * gst_element_iterate_sink_pads:
1111 * @element: a #GstElement.
1113 * Retrieves an iterator of @element's sink pads.
1115 * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1120 gst_element_iterate_sink_pads (GstElement * element)
1122 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1124 return gst_element_iterate_pad_list (element, &element->sinkpads);
1128 * gst_element_class_add_pad_template:
1129 * @klass: the #GstElementClass to add the pad template to.
1130 * @templ: a #GstPadTemplate to add to the element class.
1132 * Adds a padtemplate to an element class. This is mainly used in the _base_init
1133 * functions of classes.
1136 gst_element_class_add_pad_template (GstElementClass * klass,
1137 GstPadTemplate * templ)
1139 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1140 g_return_if_fail (GST_IS_PAD_TEMPLATE (templ));
1142 /* FIXME 0.11: allow replacing the pad templates by
1143 * calling this with the same name as an already existing pad
1144 * template. For this we _must_ _not_ ref the added pad template
1145 * a second time and _must_ document that this function takes
1146 * ownership of the pad template. Otherwise we will leak pad templates
1147 * or the caller unref's the pad template and it disappears */
1148 /* avoid registering pad templates with the same name */
1149 g_return_if_fail (gst_element_class_get_pad_template (klass,
1150 templ->name_template) == NULL);
1152 klass->padtemplates = g_list_append (klass->padtemplates,
1153 gst_object_ref (templ));
1154 klass->numpadtemplates++;
1158 * gst_element_class_add_metadata:
1159 * @klass: class to set metadata for
1160 * @key: the key to set
1161 * @value: the value to set
1163 * Set @key with @value as metadata in @klass.
1166 gst_element_class_add_metadata (GstElementClass * klass,
1167 const gchar * key, const gchar * value)
1169 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1170 g_return_if_fail (key != NULL);
1171 g_return_if_fail (value != NULL);
1173 gst_structure_set ((GstStructure *) klass->metadata,
1174 key, G_TYPE_STRING, value, NULL);
1178 * gst_element_class_set_metadata:
1179 * @klass: class to set metadata for
1180 * @longname: The long English name of the element. E.g. "File Sink"
1181 * @classification: String describing the type of element, as an unordered list
1182 * separated with slashes ('/'). See draft-klass.txt of the design docs
1183 * for more details and common types. E.g: "Sink/File"
1184 * @description: Sentence describing the purpose of the element.
1185 * E.g: "Write stream to a file"
1186 * @author: Name and contact details of the author(s). Use \n to separate
1187 * multiple author metadata. E.g: "Joe Bloggs <joe.blogs at foo.com>"
1189 * Sets the detailed information for a #GstElementClass.
1190 * <note>This function is for use in _base_init functions only.</note>
1193 gst_element_class_set_metadata (GstElementClass * klass,
1194 const gchar * longname, const gchar * classification,
1195 const gchar * description, const gchar * author)
1197 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1199 gst_structure_set ((GstStructure *) klass->metadata,
1200 GST_ELEMENT_METADATA_LONGNAME, G_TYPE_STRING, longname,
1201 GST_ELEMENT_METADATA_KLASS, G_TYPE_STRING, classification,
1202 GST_ELEMENT_METADATA_DESCRIPTION, G_TYPE_STRING, description,
1203 GST_ELEMENT_METADATA_AUTHOR, G_TYPE_STRING, author, NULL);
1207 * gst_element_class_get_pad_template_list:
1208 * @element_class: a #GstElementClass to get pad templates of.
1210 * Retrieves a list of the pad templates associated with @element_class. The
1211 * list must not be modified by the calling code.
1212 * <note>If you use this function in the #GInstanceInitFunc of an object class
1213 * that has subclasses, make sure to pass the g_class parameter of the
1214 * #GInstanceInitFunc here.</note>
1216 * Returns: the #GList of padtemplates.
1219 gst_element_class_get_pad_template_list (GstElementClass * element_class)
1221 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1223 return element_class->padtemplates;
1227 * gst_element_class_get_pad_template:
1228 * @element_class: a #GstElementClass to get the pad template of.
1229 * @name: the name of the #GstPadTemplate to get.
1231 * Retrieves a padtemplate from @element_class with the given name.
1232 * <note>If you use this function in the #GInstanceInitFunc of an object class
1233 * that has subclasses, make sure to pass the g_class parameter of the
1234 * #GInstanceInitFunc here.</note>
1236 * Returns: the #GstPadTemplate with the given name, or %NULL if none was found.
1237 * No unreferencing is necessary.
1240 gst_element_class_get_pad_template (GstElementClass * element_class,
1245 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1246 g_return_val_if_fail (name != NULL, NULL);
1248 padlist = element_class->padtemplates;
1251 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
1253 if (strcmp (padtempl->name_template, name) == 0)
1256 padlist = g_list_next (padlist);
1262 static GstPadTemplate *
1263 gst_element_class_get_request_pad_template (GstElementClass * element_class,
1266 GstPadTemplate *tmpl;
1268 tmpl = gst_element_class_get_pad_template (element_class, name);
1269 if (tmpl != NULL && tmpl->presence == GST_PAD_REQUEST)
1275 /* get a random pad on element of the given direction.
1276 * The pad is random in a sense that it is the first pad that is (optionaly) linked.
1279 gst_element_get_random_pad (GstElement * element, gboolean need_linked,
1280 GstPadDirection dir)
1282 GstPad *result = NULL;
1285 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "getting a random pad");
1289 GST_OBJECT_LOCK (element);
1290 pads = element->srcpads;
1293 GST_OBJECT_LOCK (element);
1294 pads = element->sinkpads;
1297 goto wrong_direction;
1299 for (; pads; pads = g_list_next (pads)) {
1300 GstPad *pad = GST_PAD_CAST (pads->data);
1302 GST_OBJECT_LOCK (pad);
1303 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "checking pad %s:%s",
1304 GST_DEBUG_PAD_NAME (pad));
1306 if (need_linked && !GST_PAD_IS_LINKED (pad)) {
1307 /* if we require a linked pad, and it is not linked, continue the
1309 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is not linked",
1310 GST_DEBUG_PAD_NAME (pad));
1311 GST_OBJECT_UNLOCK (pad);
1314 /* found a pad, stop search */
1315 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
1316 GST_DEBUG_PAD_NAME (pad));
1317 GST_OBJECT_UNLOCK (pad);
1323 gst_object_ref (result);
1325 GST_OBJECT_UNLOCK (element);
1329 /* ERROR handling */
1332 g_warning ("unknown pad direction %d", dir);
1338 gst_element_default_send_event (GstElement * element, GstEvent * event)
1340 gboolean result = FALSE;
1343 pad = GST_EVENT_IS_DOWNSTREAM (event) ?
1344 gst_element_get_random_pad (element, TRUE, GST_PAD_SRC) :
1345 gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1348 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1349 "pushing %s event to random %s pad %s:%s",
1350 GST_EVENT_TYPE_NAME (event),
1351 (GST_PAD_DIRECTION (pad) == GST_PAD_SRC ? "src" : "sink"),
1352 GST_DEBUG_PAD_NAME (pad));
1354 result = gst_pad_push_event (pad, event);
1355 gst_object_unref (pad);
1357 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "can't send %s event on element %s",
1358 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1364 * gst_element_send_event:
1365 * @element: a #GstElement to send the event to.
1366 * @event: the #GstEvent to send to the element.
1368 * Sends an event to an element. If the element doesn't implement an
1369 * event handler, the event will be pushed on a random linked sink pad for
1370 * upstream events or a random linked source pad for downstream events.
1372 * This function takes owership of the provided event so you should
1373 * gst_event_ref() it if you want to reuse the event after this call.
1375 * Returns: %TRUE if the event was handled.
1380 gst_element_send_event (GstElement * element, GstEvent * event)
1382 GstElementClass *oclass;
1383 gboolean result = FALSE;
1385 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1386 g_return_val_if_fail (event != NULL, FALSE);
1388 oclass = GST_ELEMENT_GET_CLASS (element);
1390 GST_STATE_LOCK (element);
1391 if (oclass->send_event) {
1392 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send %s event on element %s",
1393 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1394 result = oclass->send_event (element, event);
1396 result = gst_element_default_send_event (element, event);
1398 GST_STATE_UNLOCK (element);
1405 * @element: a #GstElement to send the event to.
1406 * @rate: The new playback rate
1407 * @format: The format of the seek values
1408 * @flags: The optional seek flags.
1409 * @cur_type: The type and flags for the new current position
1410 * @cur: The value of the new current position
1411 * @stop_type: The type and flags for the new stop position
1412 * @stop: The value of the new stop position
1414 * Sends a seek event to an element. See gst_event_new_seek() for the details of
1415 * the parameters. The seek event is sent to the element using
1416 * gst_element_send_event().
1418 * Returns: %TRUE if the event was handled.
1423 gst_element_seek (GstElement * element, gdouble rate, GstFormat format,
1424 GstSeekFlags flags, GstSeekType cur_type, gint64 cur,
1425 GstSeekType stop_type, gint64 stop)
1430 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1433 gst_event_new_seek (rate, format, flags, cur_type, cur, stop_type, stop);
1434 result = gst_element_send_event (element, event);
1440 * gst_element_get_query_types:
1441 * @element: a #GstElement to query
1443 * Get an array of query types from the element.
1444 * If the element doesn't implement a query types function,
1445 * the query will be forwarded to the peer of a random linked sink pad.
1447 * Returns: An array of #GstQueryType elements that should not
1448 * be freed or modified.
1452 const GstQueryType *
1453 gst_element_get_query_types (GstElement * element)
1455 GstElementClass *oclass;
1456 const GstQueryType *result = NULL;
1458 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1460 oclass = GST_ELEMENT_GET_CLASS (element);
1462 if (oclass->get_query_types) {
1463 result = oclass->get_query_types (element);
1465 GstPad *pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1468 GstPad *peer = gst_pad_get_peer (pad);
1471 result = gst_pad_get_query_types (peer);
1473 gst_object_unref (peer);
1475 gst_object_unref (pad);
1482 gst_element_default_query (GstElement * element, GstQuery * query)
1484 gboolean result = FALSE;
1487 pad = gst_element_get_random_pad (element, FALSE, GST_PAD_SRC);
1489 result = gst_pad_query (pad, query);
1491 gst_object_unref (pad);
1493 pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1495 GstPad *peer = gst_pad_get_peer (pad);
1498 result = gst_pad_query (peer, query);
1500 gst_object_unref (peer);
1502 gst_object_unref (pad);
1509 * gst_element_query:
1510 * @element: a #GstElement to perform the query on.
1511 * @query: the #GstQuery.
1513 * Performs a query on the given element.
1515 * For elements that don't implement a query handler, this function
1516 * forwards the query to a random srcpad or to the peer of a
1517 * random linked sinkpad of this element.
1519 * Please note that some queries might need a running pipeline to work.
1521 * Returns: TRUE if the query could be performed.
1526 gst_element_query (GstElement * element, GstQuery * query)
1528 GstElementClass *oclass;
1529 gboolean result = FALSE;
1531 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1532 g_return_val_if_fail (query != NULL, FALSE);
1534 oclass = GST_ELEMENT_GET_CLASS (element);
1536 if (oclass->query) {
1537 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send query on element %s",
1538 GST_ELEMENT_NAME (element));
1539 result = oclass->query (element, query);
1541 result = gst_element_default_query (element, query);
1547 * gst_element_post_message:
1548 * @element: a #GstElement posting the message
1549 * @message: a #GstMessage to post
1551 * Post a message on the element's #GstBus. This function takes ownership of the
1552 * message; if you want to access the message after this call, you should add an
1553 * additional reference before calling.
1555 * Returns: %TRUE if the message was successfully posted. The function returns
1556 * %FALSE if the element did not have a bus.
1561 gst_element_post_message (GstElement * element, GstMessage * message)
1564 gboolean result = FALSE;
1566 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1567 g_return_val_if_fail (message != NULL, FALSE);
1569 GST_OBJECT_LOCK (element);
1572 if (G_UNLIKELY (bus == NULL))
1575 gst_object_ref (bus);
1576 GST_OBJECT_UNLOCK (element);
1578 /* we release the element lock when posting the message so that any
1579 * (synchronous) message handlers can operate on the element */
1580 result = gst_bus_post (bus, message);
1581 gst_object_unref (bus);
1588 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, element,
1589 "not posting message %p: no bus", message);
1590 GST_OBJECT_UNLOCK (element);
1591 gst_message_unref (message);
1597 * _gst_element_error_printf:
1598 * @format: the printf-like format to use, or %NULL
1600 * This function is only used internally by the gst_element_error() macro.
1602 * Returns: a newly allocated string, or %NULL if the format was %NULL or ""
1607 _gst_element_error_printf (const gchar * format, ...)
1617 va_start (args, format);
1618 buffer = g_strdup_vprintf (format, args);
1624 * gst_element_message_full:
1625 * @element: a #GstElement to send message from
1626 * @type: the #GstMessageType
1627 * @domain: the GStreamer GError domain this message belongs to
1628 * @code: the GError code belonging to the domain
1629 * @text: an allocated text string to be used as a replacement for the
1630 * default message connected to code, or %NULL
1631 * @debug: an allocated debug message to be used as a replacement for the
1632 * default debugging information, or %NULL
1633 * @file: the source code file where the error was generated
1634 * @function: the source code function where the error was generated
1635 * @line: the source code line where the error was generated
1637 * Post an error, warning or info message on the bus from inside an element.
1639 * @type must be of #GST_MESSAGE_ERROR, #GST_MESSAGE_WARNING or
1640 * #GST_MESSAGE_INFO.
1644 void gst_element_message_full
1645 (GstElement * element, GstMessageType type,
1646 GQuark domain, gint code, gchar * text,
1647 gchar * debug, const gchar * file, const gchar * function, gint line)
1649 GError *gerror = NULL;
1653 gboolean has_debug = TRUE;
1654 GstMessage *message = NULL;
1657 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, element, "start");
1658 g_return_if_fail (GST_IS_ELEMENT (element));
1659 g_return_if_fail ((type == GST_MESSAGE_ERROR) ||
1660 (type == GST_MESSAGE_WARNING) || (type == GST_MESSAGE_INFO));
1662 /* check if we send the given text or the default error text */
1663 if ((text == NULL) || (text[0] == 0)) {
1664 /* text could have come from g_strdup_printf (""); */
1666 sent_text = gst_error_get_message (domain, code);
1670 /* construct a sent_debug with extra information from source */
1671 if ((debug == NULL) || (debug[0] == 0)) {
1672 /* debug could have come from g_strdup_printf (""); */
1676 name = gst_object_get_path_string (GST_OBJECT_CAST (element));
1678 sent_debug = g_strdup_printf ("%s(%d): %s (): %s:\n%s",
1679 file, line, function, name, debug);
1681 sent_debug = g_strdup_printf ("%s(%d): %s (): %s",
1682 file, line, function, name);
1686 /* create gerror and post message */
1687 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posting message: %s",
1689 gerror = g_error_new_literal (domain, code, sent_text);
1692 case GST_MESSAGE_ERROR:
1694 gst_message_new_error (GST_OBJECT_CAST (element), gerror, sent_debug);
1696 case GST_MESSAGE_WARNING:
1697 message = gst_message_new_warning (GST_OBJECT_CAST (element), gerror,
1700 case GST_MESSAGE_INFO:
1701 message = gst_message_new_info (GST_OBJECT_CAST (element), gerror,
1705 g_assert_not_reached ();
1708 gst_element_post_message (element, message);
1710 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posted %s message: %s",
1711 (type == GST_MESSAGE_ERROR ? "error" : "warning"), sent_text);
1714 g_error_free (gerror);
1715 g_free (sent_debug);
1720 * gst_element_is_locked_state:
1721 * @element: a #GstElement.
1723 * Checks if the state of an element is locked.
1724 * If the state of an element is locked, state changes of the parent don't
1725 * affect the element.
1726 * This way you can leave currently unused elements inside bins. Just lock their
1727 * state before changing the state from #GST_STATE_NULL.
1731 * Returns: TRUE, if the element's state is locked.
1734 gst_element_is_locked_state (GstElement * element)
1738 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1740 GST_OBJECT_LOCK (element);
1741 result = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1742 GST_OBJECT_UNLOCK (element);
1748 * gst_element_set_locked_state:
1749 * @element: a #GstElement
1750 * @locked_state: TRUE to lock the element's state
1752 * Locks the state of an element, so state changes of the parent don't affect
1753 * this element anymore.
1757 * Returns: TRUE if the state was changed, FALSE if bad parameters were given
1758 * or the elements state-locking needed no change.
1761 gst_element_set_locked_state (GstElement * element, gboolean locked_state)
1765 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1767 GST_OBJECT_LOCK (element);
1768 old = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1770 if (G_UNLIKELY (old == locked_state))
1774 GST_CAT_DEBUG (GST_CAT_STATES, "locking state of element %s",
1775 GST_ELEMENT_NAME (element));
1776 GST_OBJECT_FLAG_SET (element, GST_ELEMENT_LOCKED_STATE);
1778 GST_CAT_DEBUG (GST_CAT_STATES, "unlocking state of element %s",
1779 GST_ELEMENT_NAME (element));
1780 GST_OBJECT_FLAG_UNSET (element, GST_ELEMENT_LOCKED_STATE);
1782 GST_OBJECT_UNLOCK (element);
1788 GST_CAT_DEBUG (GST_CAT_STATES, "elements %s was already in locked state %d",
1789 GST_ELEMENT_NAME (element), old);
1790 GST_OBJECT_UNLOCK (element);
1797 * gst_element_sync_state_with_parent:
1798 * @element: a #GstElement.
1800 * Tries to change the state of the element to the same as its parent.
1801 * If this function returns FALSE, the state of element is undefined.
1803 * Returns: TRUE, if the element's state could be synced to the parent's state.
1808 gst_element_sync_state_with_parent (GstElement * element)
1812 GstStateChangeReturn ret;
1814 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1816 if ((parent = GST_ELEMENT_CAST (gst_element_get_parent (element)))) {
1817 GstState parent_current, parent_pending;
1819 GST_OBJECT_LOCK (parent);
1820 parent_current = GST_STATE (parent);
1821 parent_pending = GST_STATE_PENDING (parent);
1822 GST_OBJECT_UNLOCK (parent);
1824 /* set to pending if there is one, else we set it to the current state of
1826 if (parent_pending != GST_STATE_VOID_PENDING)
1827 target = parent_pending;
1829 target = parent_current;
1831 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1832 "syncing state (%s) to parent %s %s (%s, %s)",
1833 gst_element_state_get_name (GST_STATE (element)),
1834 GST_ELEMENT_NAME (parent), gst_element_state_get_name (target),
1835 gst_element_state_get_name (parent_current),
1836 gst_element_state_get_name (parent_pending));
1838 ret = gst_element_set_state (element, target);
1839 if (ret == GST_STATE_CHANGE_FAILURE)
1842 gst_object_unref (parent);
1846 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "element has no parent");
1853 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1854 "syncing state failed (%s)",
1855 gst_element_state_change_return_get_name (ret));
1856 gst_object_unref (parent);
1862 static GstStateChangeReturn
1863 gst_element_get_state_func (GstElement * element,
1864 GstState * state, GstState * pending, GstClockTime timeout)
1866 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
1867 GstState old_pending;
1869 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "getting state, timeout %"
1870 GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
1872 GST_OBJECT_LOCK (element);
1873 ret = GST_STATE_RETURN (element);
1874 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "RETURN is %s",
1875 gst_element_state_change_return_get_name (ret));
1877 /* we got an error, report immediately */
1878 if (ret == GST_STATE_CHANGE_FAILURE)
1881 /* we got no_preroll, report immediatly */
1882 if (ret == GST_STATE_CHANGE_NO_PREROLL)
1885 /* no need to wait async if we are not async */
1886 if (ret != GST_STATE_CHANGE_ASYNC)
1889 old_pending = GST_STATE_PENDING (element);
1890 if (old_pending != GST_STATE_VOID_PENDING) {
1891 GTimeVal *timeval, abstimeout;
1894 if (timeout != GST_CLOCK_TIME_NONE) {
1895 glong add = timeout / 1000;
1900 /* make timeout absolute */
1901 g_get_current_time (&abstimeout);
1902 g_time_val_add (&abstimeout, add);
1903 timeval = &abstimeout;
1907 /* get cookie to detect state changes during waiting */
1908 cookie = element->state_cookie;
1910 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1911 "waiting for element to commit state");
1913 /* we have a pending state change, wait for it to complete */
1914 if (!GST_STATE_TIMED_WAIT (element, timeval)) {
1915 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timed out");
1916 /* timeout triggered */
1917 ret = GST_STATE_CHANGE_ASYNC;
1919 if (cookie != element->state_cookie)
1922 /* could be success or failure */
1923 if (old_pending == GST_STATE (element)) {
1924 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got success");
1925 ret = GST_STATE_CHANGE_SUCCESS;
1927 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got failure");
1928 ret = GST_STATE_CHANGE_FAILURE;
1931 /* if nothing is pending anymore we can return SUCCESS */
1932 if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
1933 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "nothing pending");
1934 ret = GST_STATE_CHANGE_SUCCESS;
1940 *state = GST_STATE (element);
1942 *pending = GST_STATE_PENDING (element);
1944 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1945 "state current: %s, pending: %s, result: %s",
1946 gst_element_state_get_name (GST_STATE (element)),
1947 gst_element_state_get_name (GST_STATE_PENDING (element)),
1948 gst_element_state_change_return_get_name (ret));
1949 GST_OBJECT_UNLOCK (element);
1956 *state = GST_STATE_VOID_PENDING;
1958 *pending = GST_STATE_VOID_PENDING;
1960 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "interruped");
1962 GST_OBJECT_UNLOCK (element);
1964 return GST_STATE_CHANGE_FAILURE;
1969 * gst_element_get_state:
1970 * @element: a #GstElement to get the state of.
1971 * @state: (out): a pointer to #GstState to hold the state. Can be %NULL.
1972 * @pending: (out): a pointer to #GstState to hold the pending state.
1974 * @timeout: a #GstClockTime to specify the timeout for an async
1975 * state change or %GST_CLOCK_TIME_NONE for infinite timeout.
1977 * Gets the state of the element.
1979 * For elements that performed an ASYNC state change, as reported by
1980 * gst_element_set_state(), this function will block up to the
1981 * specified timeout value for the state change to complete.
1982 * If the element completes the state change or goes into
1983 * an error, this function returns immediately with a return value of
1984 * %GST_STATE_CHANGE_SUCCESS or %GST_STATE_CHANGE_FAILURE respectively.
1986 * For elements that did not return %GST_STATE_CHANGE_ASYNC, this function
1987 * returns the current and pending state immediately.
1989 * This function returns %GST_STATE_CHANGE_NO_PREROLL if the element
1990 * successfully changed its state but is not able to provide data yet.
1991 * This mostly happens for live sources that only produce data in
1992 * %GST_STATE_PLAYING. While the state change return is equivalent to
1993 * %GST_STATE_CHANGE_SUCCESS, it is returned to the application to signal that
1994 * some sink elements might not be able to complete their state change because
1995 * an element is not producing data to complete the preroll. When setting the
1996 * element to playing, the preroll will complete and playback will start.
1998 * Returns: %GST_STATE_CHANGE_SUCCESS if the element has no more pending state
1999 * and the last state change succeeded, %GST_STATE_CHANGE_ASYNC if the
2000 * element is still performing a state change or
2001 * %GST_STATE_CHANGE_FAILURE if the last state change failed.
2005 GstStateChangeReturn
2006 gst_element_get_state (GstElement * element,
2007 GstState * state, GstState * pending, GstClockTime timeout)
2009 GstElementClass *oclass;
2010 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2012 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2014 oclass = GST_ELEMENT_GET_CLASS (element);
2016 if (oclass->get_state)
2017 result = (oclass->get_state) (element, state, pending, timeout);
2023 * gst_element_abort_state:
2024 * @element: a #GstElement to abort the state of.
2026 * Abort the state change of the element. This function is used
2027 * by elements that do asynchronous state changes and find out
2028 * something is wrong.
2030 * This function should be called with the STATE_LOCK held.
2035 gst_element_abort_state (GstElement * element)
2039 #ifndef GST_DISABLE_GST_DEBUG
2043 g_return_if_fail (GST_IS_ELEMENT (element));
2045 GST_OBJECT_LOCK (element);
2046 pending = GST_STATE_PENDING (element);
2048 if (pending == GST_STATE_VOID_PENDING ||
2049 GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2050 goto nothing_aborted;
2052 #ifndef GST_DISABLE_GST_DEBUG
2053 old_state = GST_STATE (element);
2055 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2056 "aborting state from %s to %s", gst_element_state_get_name (old_state),
2057 gst_element_state_get_name (pending));
2061 GST_STATE_RETURN (element) = GST_STATE_CHANGE_FAILURE;
2063 GST_STATE_BROADCAST (element);
2064 GST_OBJECT_UNLOCK (element);
2070 GST_OBJECT_UNLOCK (element);
2076 * gst_element_continue_state:
2077 * @element: a #GstElement to continue the state change of.
2078 * @ret: The previous state return value
2080 * Commit the state change of the element and proceed to the next
2081 * pending state if any. This function is used
2082 * by elements that do asynchronous state changes.
2083 * The core will normally call this method automatically when an
2084 * element returned %GST_STATE_CHANGE_SUCCESS from the state change function.
2086 * If after calling this method the element still has not reached
2087 * the pending state, the next state change is performed.
2089 * This method is used internally and should normally not be called by plugins
2092 * Returns: The result of the commit state change.
2096 GstStateChangeReturn
2097 gst_element_continue_state (GstElement * element, GstStateChangeReturn ret)
2099 GstStateChangeReturn old_ret;
2100 GstState old_state, old_next;
2101 GstState current, next, pending;
2102 GstMessage *message;
2103 GstStateChange transition;
2105 GST_OBJECT_LOCK (element);
2106 old_ret = GST_STATE_RETURN (element);
2107 GST_STATE_RETURN (element) = ret;
2108 pending = GST_STATE_PENDING (element);
2110 /* check if there is something to commit */
2111 if (pending == GST_STATE_VOID_PENDING)
2112 goto nothing_pending;
2114 old_state = GST_STATE (element);
2115 /* this is the state we should go to next */
2116 old_next = GST_STATE_NEXT (element);
2117 /* update current state */
2118 current = GST_STATE (element) = old_next;
2120 /* see if we reached the final state */
2121 if (pending == current)
2124 next = GST_STATE_GET_NEXT (current, pending);
2125 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2127 GST_STATE_NEXT (element) = next;
2129 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2130 GST_OBJECT_UNLOCK (element);
2132 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2133 "committing state from %s to %s, pending %s, next %s",
2134 gst_element_state_get_name (old_state),
2135 gst_element_state_get_name (old_next),
2136 gst_element_state_get_name (pending), gst_element_state_get_name (next));
2138 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2139 old_state, old_next, pending);
2140 gst_element_post_message (element, message);
2142 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2143 "continue state change %s to %s, final %s",
2144 gst_element_state_get_name (current),
2145 gst_element_state_get_name (next), gst_element_state_get_name (pending));
2147 ret = gst_element_change_state (element, transition);
2153 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "nothing pending");
2154 GST_OBJECT_UNLOCK (element);
2159 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2160 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2162 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2163 "completed state change to %s", gst_element_state_get_name (pending));
2164 GST_OBJECT_UNLOCK (element);
2166 /* don't post silly messages with the same state. This can happen
2167 * when an element state is changed to what it already was. For bins
2168 * this can be the result of a lost state, which we check with the
2169 * previous return value.
2170 * We do signal the cond though as a _get_state() might be blocking
2172 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC) {
2173 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2174 "posting state-changed %s to %s",
2175 gst_element_state_get_name (old_state),
2176 gst_element_state_get_name (old_next));
2178 gst_message_new_state_changed (GST_OBJECT_CAST (element), old_state,
2179 old_next, GST_STATE_VOID_PENDING);
2180 gst_element_post_message (element, message);
2183 GST_STATE_BROADCAST (element);
2190 * gst_element_lost_state_full:
2191 * @element: a #GstElement the state is lost of
2192 * @new_base_time: if a new base time should be distributed
2194 * Brings the element to the lost state. The current state of the
2195 * element is copied to the pending state so that any call to
2196 * gst_element_get_state() will return %GST_STATE_CHANGE_ASYNC.
2198 * An ASYNC_START message is posted with indication to distribute a new
2199 * base_time to the element when @new_base_time is %TRUE.
2200 * If the element was PLAYING, it will go to PAUSED. The element
2201 * will be restored to its PLAYING state by the parent pipeline when it
2204 * This is mostly used for elements that lost their preroll buffer
2205 * in the %GST_STATE_PAUSED or %GST_STATE_PLAYING state after a flush,
2206 * they will go to their pending state again when a new preroll buffer is
2207 * queued. This function can only be called when the element is currently
2208 * not in error or an async state change.
2210 * This function is used internally and should normally not be called from
2211 * plugins or applications.
2218 gst_element_lost_state_full (GstElement * element, gboolean new_base_time)
2220 GstState old_state, new_state;
2221 GstMessage *message;
2223 g_return_if_fail (GST_IS_ELEMENT (element));
2225 GST_OBJECT_LOCK (element);
2226 if (GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2229 if (GST_STATE_PENDING (element) != GST_STATE_VOID_PENDING)
2230 goto only_async_start;
2232 old_state = GST_STATE (element);
2234 /* when we were PLAYING, the new state is PAUSED. We will also not
2235 * automatically go to PLAYING but let the parent bin(s) set us to PLAYING
2236 * when we preroll. */
2237 if (old_state > GST_STATE_PAUSED)
2238 new_state = GST_STATE_PAUSED;
2240 new_state = old_state;
2242 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2243 "lost state of %s to %s", gst_element_state_get_name (old_state),
2244 gst_element_state_get_name (new_state));
2246 GST_STATE (element) = new_state;
2247 GST_STATE_NEXT (element) = new_state;
2248 GST_STATE_PENDING (element) = new_state;
2249 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2251 GST_ELEMENT_START_TIME (element) = 0;
2252 GST_OBJECT_UNLOCK (element);
2254 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2255 new_state, new_state, new_state);
2256 gst_element_post_message (element, message);
2259 gst_message_new_async_start (GST_OBJECT_CAST (element), new_base_time);
2260 gst_element_post_message (element, message);
2266 GST_OBJECT_UNLOCK (element);
2271 GST_OBJECT_UNLOCK (element);
2273 message = gst_message_new_async_start (GST_OBJECT_CAST (element), TRUE);
2274 gst_element_post_message (element, message);
2280 * gst_element_lost_state:
2281 * @element: a #GstElement the state is lost of
2283 * Brings the element to the lost state. This function calls
2284 * gst_element_lost_state_full() with the new_base_time set to %TRUE.
2286 * This function is used internally and should normally not be called from
2287 * plugins or applications.
2292 gst_element_lost_state (GstElement * element)
2294 gst_element_lost_state_full (element, TRUE);
2298 * gst_element_set_state:
2299 * @element: a #GstElement to change state of.
2300 * @state: the element's new #GstState.
2302 * Sets the state of the element. This function will try to set the
2303 * requested state by going through all the intermediary states and calling
2304 * the class's state change function for each.
2306 * This function can return #GST_STATE_CHANGE_ASYNC, in which case the
2307 * element will perform the remainder of the state change asynchronously in
2309 * An application can use gst_element_get_state() to wait for the completion
2310 * of the state change or it can wait for a state change message on the bus.
2312 * State changes to %GST_STATE_READY or %GST_STATE_NULL never return
2313 * #GST_STATE_CHANGE_ASYNC.
2315 * Returns: Result of the state change using #GstStateChangeReturn.
2319 GstStateChangeReturn
2320 gst_element_set_state (GstElement * element, GstState state)
2322 GstElementClass *oclass;
2323 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2325 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2327 oclass = GST_ELEMENT_GET_CLASS (element);
2329 if (oclass->set_state)
2330 result = (oclass->set_state) (element, state);
2336 * default set state function, calculates the next state based
2337 * on current state and calls the change_state function
2339 static GstStateChangeReturn
2340 gst_element_set_state_func (GstElement * element, GstState state)
2342 GstState current, next, old_pending;
2343 GstStateChangeReturn ret;
2344 GstStateChange transition;
2345 GstStateChangeReturn old_ret;
2347 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2349 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "set_state to %s",
2350 gst_element_state_get_name (state));
2352 /* state lock is taken to protect the set_state() and get_state()
2353 * procedures, it does not lock any variables. */
2354 GST_STATE_LOCK (element);
2356 /* now calculate how to get to the new state */
2357 GST_OBJECT_LOCK (element);
2358 old_ret = GST_STATE_RETURN (element);
2359 /* previous state change returned an error, remove all pending
2360 * and next states */
2361 if (old_ret == GST_STATE_CHANGE_FAILURE) {
2362 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2363 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2364 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
2367 current = GST_STATE (element);
2368 next = GST_STATE_NEXT (element);
2369 old_pending = GST_STATE_PENDING (element);
2371 /* this is the (new) state we should go to. TARGET is the last state we set on
2373 if (state != GST_STATE_TARGET (element)) {
2374 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2375 "setting target state to %s", gst_element_state_get_name (state));
2376 GST_STATE_TARGET (element) = state;
2377 /* increment state cookie so that we can track each state change. We only do
2378 * this if this is actually a new state change. */
2379 element->state_cookie++;
2381 GST_STATE_PENDING (element) = state;
2383 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2384 "current %s, old_pending %s, next %s, old return %s",
2385 gst_element_state_get_name (current),
2386 gst_element_state_get_name (old_pending),
2387 gst_element_state_get_name (next),
2388 gst_element_state_change_return_get_name (old_ret));
2390 /* if the element was busy doing a state change, we just update the
2391 * target state, it'll get to it async then. */
2392 if (old_pending != GST_STATE_VOID_PENDING) {
2393 /* upwards state change will happen ASYNC */
2394 if (old_pending <= state)
2396 /* element is going to this state already */
2397 else if (next == state)
2399 /* element was performing an ASYNC upward state change and
2400 * we request to go downward again. Start from the next pending
2402 else if (next > state
2403 && GST_STATE_RETURN (element) == GST_STATE_CHANGE_ASYNC) {
2407 next = GST_STATE_GET_NEXT (current, state);
2408 /* now we store the next state */
2409 GST_STATE_NEXT (element) = next;
2410 /* mark busy, we need to check that there is actually a state change
2411 * to be done else we could accidentally override SUCCESS/NO_PREROLL and
2412 * the default element change_state function has no way to know what the
2413 * old value was... could consider this a FIXME...*/
2414 if (current != next)
2415 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2417 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2419 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2420 "%s: setting state from %s to %s",
2421 (next != state ? "intermediate" : "final"),
2422 gst_element_state_get_name (current), gst_element_state_get_name (next));
2424 /* now signal any waiters, they will error since the cookie was incremented */
2425 GST_STATE_BROADCAST (element);
2427 GST_OBJECT_UNLOCK (element);
2429 ret = gst_element_change_state (element, transition);
2431 GST_STATE_UNLOCK (element);
2433 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "returned %s",
2434 gst_element_state_change_return_get_name (ret));
2440 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2441 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2442 "element was busy with async state change");
2443 GST_OBJECT_UNLOCK (element);
2445 GST_STATE_UNLOCK (element);
2447 return GST_STATE_CHANGE_ASYNC;
2452 * gst_element_change_state:
2453 * @element: a #GstElement
2454 * @transition: the requested transition
2456 * Perform @transition on @element.
2458 * This function must be called with STATE_LOCK held and is mainly used
2461 * Returns: the #GstStateChangeReturn of the state transition.
2463 GstStateChangeReturn
2464 gst_element_change_state (GstElement * element, GstStateChange transition)
2466 GstElementClass *oclass;
2467 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2469 oclass = GST_ELEMENT_GET_CLASS (element);
2471 /* call the state change function so it can set the state */
2472 if (oclass->change_state)
2473 ret = (oclass->change_state) (element, transition);
2475 ret = GST_STATE_CHANGE_FAILURE;
2478 case GST_STATE_CHANGE_FAILURE:
2479 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2480 "have FAILURE change_state return");
2481 /* state change failure */
2482 gst_element_abort_state (element);
2484 case GST_STATE_CHANGE_ASYNC:
2488 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2489 "element will change state ASYNC");
2491 target = GST_STATE_TARGET (element);
2493 if (target > GST_STATE_READY)
2496 /* else we just continue the state change downwards */
2497 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2498 "forcing commit state %s <= %s",
2499 gst_element_state_get_name (target),
2500 gst_element_state_get_name (GST_STATE_READY));
2502 ret = gst_element_continue_state (element, GST_STATE_CHANGE_SUCCESS);
2505 case GST_STATE_CHANGE_SUCCESS:
2506 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2507 "element changed state SUCCESS");
2508 /* we can commit the state now which will proceeed to
2510 ret = gst_element_continue_state (element, ret);
2512 case GST_STATE_CHANGE_NO_PREROLL:
2513 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2514 "element changed state NO_PREROLL");
2515 /* we can commit the state now which will proceeed to
2517 ret = gst_element_continue_state (element, ret);
2520 goto invalid_return;
2523 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit state change %d", ret);
2528 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit async state change %d",
2536 GST_OBJECT_LOCK (element);
2537 /* somebody added a GST_STATE_ and forgot to do stuff here ! */
2538 g_critical ("%s: unknown return value %d from a state change function",
2539 GST_ELEMENT_NAME (element), ret);
2541 /* we are in error now */
2542 ret = GST_STATE_CHANGE_FAILURE;
2543 GST_STATE_RETURN (element) = ret;
2544 GST_OBJECT_UNLOCK (element);
2550 /* gst_iterator_fold functions for pads_activate
2551 * Note how we don't stop the iterator when we fail an activation. This is
2552 * probably a FIXME since when one pad activation fails, we don't want to
2553 * continue our state change. */
2555 activate_pads (GstPad * pad, GValue * ret, gboolean * active)
2557 if (!gst_pad_set_active (pad, *active))
2558 g_value_set_boolean (ret, FALSE);
2560 /* unref the object that was reffed for us by _fold */
2561 gst_object_unref (pad);
2565 /* set the caps on the pad to NULL */
2567 clear_caps (GstPad * pad, GValue * ret, gboolean * active)
2569 gst_pad_set_caps (pad, NULL);
2570 gst_object_unref (pad);
2574 /* returns false on error or early cutout (will never happen because the fold
2575 * function always returns TRUE, see FIXME above) of the fold, true if all
2576 * pads in @iter were (de)activated successfully. */
2578 iterator_activate_fold_with_resync (GstIterator * iter,
2579 GstIteratorFoldFunction func, gpointer user_data)
2581 GstIteratorResult ires;
2584 /* no need to unset this later, it's just a boolean */
2585 g_value_init (&ret, G_TYPE_BOOLEAN);
2586 g_value_set_boolean (&ret, TRUE);
2589 ires = gst_iterator_fold (iter, func, &ret, user_data);
2591 case GST_ITERATOR_RESYNC:
2592 /* need to reset the result again */
2593 g_value_set_boolean (&ret, TRUE);
2594 gst_iterator_resync (iter);
2596 case GST_ITERATOR_DONE:
2597 /* all pads iterated, return collected value */
2600 /* iterator returned _ERROR or premature end with _OK,
2601 * mark an error and exit */
2602 g_value_set_boolean (&ret, FALSE);
2607 /* return collected value */
2608 return g_value_get_boolean (&ret);
2611 /* is called with STATE_LOCK
2613 * Pads are activated from source pads to sinkpads.
2616 gst_element_pads_activate (GstElement * element, gboolean active)
2621 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2622 "pads_activate with active %d", active);
2624 iter = gst_element_iterate_src_pads (element);
2626 iterator_activate_fold_with_resync (iter,
2627 (GstIteratorFoldFunction) activate_pads, &active);
2628 gst_iterator_free (iter);
2629 if (G_UNLIKELY (!res))
2632 iter = gst_element_iterate_sink_pads (element);
2634 iterator_activate_fold_with_resync (iter,
2635 (GstIteratorFoldFunction) activate_pads, &active);
2636 gst_iterator_free (iter);
2637 if (G_UNLIKELY (!res))
2641 /* clear the caps on all pads, this should never fail */
2642 iter = gst_element_iterate_pads (element);
2644 iterator_activate_fold_with_resync (iter,
2645 (GstIteratorFoldFunction) clear_caps, &active);
2646 gst_iterator_free (iter);
2647 if (G_UNLIKELY (!res))
2651 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2652 "pads_activate successful");
2659 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2660 "source pads_activate failed");
2665 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2666 "sink pads_activate failed");
2671 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2672 "failed to clear caps on pads");
2677 /* is called with STATE_LOCK */
2678 static GstStateChangeReturn
2679 gst_element_change_state_func (GstElement * element, GstStateChange transition)
2681 GstState state, next;
2682 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
2685 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2687 state = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2688 next = GST_STATE_TRANSITION_NEXT (transition);
2690 /* if the element already is in the given state, we just return success */
2691 if (next == GST_STATE_VOID_PENDING || state == next)
2694 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element,
2695 "default handler tries setting state from %s to %s (%04x)",
2696 gst_element_state_get_name (state),
2697 gst_element_state_get_name (next), transition);
2699 switch (transition) {
2700 case GST_STATE_CHANGE_NULL_TO_READY:
2702 case GST_STATE_CHANGE_READY_TO_PAUSED:
2703 if (!gst_element_pads_activate (element, TRUE)) {
2704 result = GST_STATE_CHANGE_FAILURE;
2707 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2709 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2711 case GST_STATE_CHANGE_PAUSED_TO_READY:
2712 case GST_STATE_CHANGE_READY_TO_NULL:
2713 /* deactivate pads in both cases, since they are activated on
2714 ready->paused but the element might not have made it to paused */
2715 if (!gst_element_pads_activate (element, FALSE)) {
2716 result = GST_STATE_CHANGE_FAILURE;
2718 gst_element_set_base_time (element, 0);
2721 /* In null state release the reference to the clock */
2722 GST_OBJECT_LOCK (element);
2723 clock_p = &element->clock;
2724 gst_object_replace ((GstObject **) clock_p, NULL);
2725 GST_OBJECT_UNLOCK (element);
2728 /* this will catch real but unhandled state changes;
2729 * can only be caused by:
2730 * - a new state was added
2731 * - somehow the element was asked to jump across an intermediate state
2733 g_warning ("Unhandled state change from %s to %s",
2734 gst_element_state_get_name (state),
2735 gst_element_state_get_name (next));
2742 GST_OBJECT_LOCK (element);
2743 result = GST_STATE_RETURN (element);
2744 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2745 "element is already in the %s state",
2746 gst_element_state_get_name (state));
2747 GST_OBJECT_UNLOCK (element);
2754 * gst_element_get_factory:
2755 * @element: a #GstElement to request the element factory of.
2757 * Retrieves the factory that was used to create this element.
2759 * Returns: the #GstElementFactory used for creating this element.
2760 * no refcounting is needed.
2763 gst_element_get_factory (GstElement * element)
2765 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
2767 return GST_ELEMENT_GET_CLASS (element)->elementfactory;
2771 gst_element_dispose (GObject * object)
2773 GstElement *element = GST_ELEMENT_CAST (object);
2777 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "dispose");
2779 if (GST_STATE (element) != GST_STATE_NULL)
2782 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
2783 "removing %d pads", g_list_length (element->pads));
2784 /* first we break all our links with the outside */
2785 while (element->pads && element->pads->data) {
2786 /* don't call _remove_pad with NULL */
2787 gst_element_remove_pad (element, GST_PAD_CAST (element->pads->data));
2789 if (G_UNLIKELY (element->pads != NULL)) {
2790 g_critical ("could not remove pads from element %s",
2791 GST_STR_NULL (GST_OBJECT_NAME (object)));
2794 GST_OBJECT_LOCK (element);
2795 clock_p = &element->clock;
2796 bus_p = &element->bus;
2797 gst_object_replace ((GstObject **) clock_p, NULL);
2798 gst_object_replace ((GstObject **) bus_p, NULL);
2799 GST_OBJECT_UNLOCK (element);
2801 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "parent class dispose");
2803 G_OBJECT_CLASS (parent_class)->dispose (object);
2812 is_locked = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
2814 ("\nTrying to dispose element %s, but it is in %s%s instead of the NULL"
2816 "You need to explicitly set elements to the NULL state before\n"
2817 "dropping the final reference, to allow them to clean up.\n"
2818 "This problem may also be caused by a refcounting bug in the\n"
2819 "application or some element.\n",
2820 GST_OBJECT_NAME (element),
2821 gst_element_state_get_name (GST_STATE (element)),
2822 is_locked ? " (locked)" : "");
2828 gst_element_finalize (GObject * object)
2830 GstElement *element = GST_ELEMENT_CAST (object);
2832 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize");
2834 GST_STATE_LOCK (element);
2835 if (element->state_cond)
2836 g_cond_free (element->state_cond);
2837 element->state_cond = NULL;
2838 GST_STATE_UNLOCK (element);
2839 g_static_rec_mutex_free (element->state_lock);
2840 g_slice_free (GStaticRecMutex, element->state_lock);
2841 element->state_lock = NULL;
2843 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize parent");
2845 G_OBJECT_CLASS (parent_class)->finalize (object);
2849 gst_element_set_bus_func (GstElement * element, GstBus * bus)
2853 g_return_if_fail (GST_IS_ELEMENT (element));
2855 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element, "setting bus to %p", bus);
2857 GST_OBJECT_LOCK (element);
2858 bus_p = &GST_ELEMENT_BUS (element);
2859 gst_object_replace ((GstObject **) bus_p, GST_OBJECT_CAST (bus));
2860 GST_OBJECT_UNLOCK (element);
2864 * gst_element_set_bus:
2865 * @element: a #GstElement to set the bus of.
2866 * @bus: the #GstBus to set.
2868 * Sets the bus of the element. Increases the refcount on the bus.
2869 * For internal use only, unless you're testing elements.
2874 gst_element_set_bus (GstElement * element, GstBus * bus)
2876 GstElementClass *oclass;
2878 g_return_if_fail (GST_IS_ELEMENT (element));
2880 oclass = GST_ELEMENT_GET_CLASS (element);
2882 if (oclass->set_bus)
2883 oclass->set_bus (element, bus);
2887 * gst_element_get_bus:
2888 * @element: a #GstElement to get the bus of.
2890 * Returns the bus of the element. Note that only a #GstPipeline will provide a
2891 * bus for the application.
2893 * Returns: the element's #GstBus. unref after usage.
2898 gst_element_get_bus (GstElement * element)
2900 GstBus *result = NULL;
2902 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
2904 GST_OBJECT_LOCK (element);
2905 if ((result = GST_ELEMENT_BUS (element)))
2906 gst_object_ref (result);
2907 GST_OBJECT_UNLOCK (element);
2909 GST_CAT_DEBUG_OBJECT (GST_CAT_BUS, element, "got bus %" GST_PTR_FORMAT,