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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
26 * @short_description: Abstract base class for all pipeline elements
27 * @see_also: #GstElementFactory, #GstPad
29 * GstElement is the abstract base class needed to construct an element that
30 * can be used in a GStreamer pipeline. Please refer to the plugin writers
31 * guide for more information on creating #GstElement subclasses.
33 * The name of a #GstElement can be get with gst_element_get_name() and set with
34 * gst_element_set_name(). For speed, GST_ELEMENT_NAME() can be used in the
35 * core when using the appropriate locking. Do not use this in plug-ins or
36 * applications in order to retain ABI compatibility.
38 * Elements can have pads (of the type #GstPad). These pads link to pads on
39 * other elements. #GstBuffer flow between these linked pads.
40 * A #GstElement has a #GList of #GstPad structures for all their input (or sink)
41 * and output (or source) pads.
42 * Core and plug-in writers can add and remove pads with gst_element_add_pad()
43 * and gst_element_remove_pad().
45 * An existing pad of an element can be retrieved by name with
46 * gst_element_get_static_pad(). A new dynamic pad can be created using
47 * gst_element_request_pad() with a #GstPadTemplate.
48 * An iterator of all pads can be retrieved with gst_element_iterate_pads().
50 * Elements can be linked through their pads.
51 * If the link is straightforward, use the gst_element_link()
52 * convenience function to link two elements, or gst_element_link_many()
53 * for more elements in a row.
54 * Use gst_element_link_filtered() to link two elements constrained by
55 * a specified set of #GstCaps.
56 * For finer control, use gst_element_link_pads() and
57 * gst_element_link_pads_filtered() to specify the pads to link on
58 * each element by name.
60 * Each element has a state (see #GstState). You can get and set the state
61 * of an element with gst_element_get_state() and gst_element_set_state().
62 * Setting a state triggers a #GstStateChange. To get a string representation
63 * of a #GstState, use gst_element_state_get_name().
65 * You can get and set a #GstClock on an element using gst_element_get_clock()
66 * and gst_element_set_clock().
67 * Some elements can provide a clock for the pipeline if
68 * the #GST_ELEMENT_FLAG_PROVIDE_CLOCK flag is set. With the
69 * gst_element_provide_clock() method one can retrieve the clock provided by
71 * Not all elements require a clock to operate correctly. If the
72 * #GST_ELEMENT_FLAG_REQUIRE_CLOCK() flag is set, a clock should be set on the
73 * element with gst_element_set_clock().
75 * Note that clock selection and distribution is normally handled by the
76 * toplevel #GstPipeline so the clock functions are only to be used in very
77 * specific situations.
80 #include "gst_private.h"
83 #include <gobject/gvaluecollector.h>
85 #include "gstelement.h"
86 #include "gstelementmetadata.h"
87 #include "gstenumtypes.h"
94 #include "gsttracerutils.h"
96 #include "gst-i18n-lib.h"
97 #include "glib-compat-private.h"
99 #ifndef GST_DISABLE_GST_DEBUG
100 #include "printf/printf.h"
103 /* Element signals and args */
119 static void gst_element_class_init (GstElementClass * klass);
120 static void gst_element_init (GstElement * element);
121 static void gst_element_base_class_init (gpointer g_class);
123 static void gst_element_constructed (GObject * object);
124 static void gst_element_dispose (GObject * object);
125 static void gst_element_finalize (GObject * object);
127 static GstStateChangeReturn gst_element_change_state_func (GstElement * element,
128 GstStateChange transition);
129 static GstStateChangeReturn gst_element_get_state_func (GstElement * element,
130 GstState * state, GstState * pending, GstClockTime timeout);
131 static GstStateChangeReturn gst_element_set_state_func (GstElement * element,
133 static gboolean gst_element_set_clock_func (GstElement * element,
135 static void gst_element_set_bus_func (GstElement * element, GstBus * bus);
136 static gboolean gst_element_post_message_default (GstElement * element,
137 GstMessage * message);
138 static void gst_element_set_context_default (GstElement * element,
139 GstContext * context);
141 static gboolean gst_element_default_send_event (GstElement * element,
143 static gboolean gst_element_default_query (GstElement * element,
146 static GstPadTemplate
147 * gst_element_class_get_request_pad_template (GstElementClass *
148 element_class, const gchar * name);
150 static void gst_element_call_async_func (gpointer data, gpointer user_data);
152 static GstObjectClass *parent_class = NULL;
153 static guint gst_element_signals[LAST_SIGNAL] = { 0 };
155 static GThreadPool *gst_element_pool = NULL;
157 /* this is used in gstelementfactory.c:gst_element_register() */
158 GQuark __gst_elementclass_factory = 0;
161 gst_element_get_type (void)
163 static volatile gsize gst_element_type = 0;
165 if (g_once_init_enter (&gst_element_type)) {
167 static const GTypeInfo element_info = {
168 sizeof (GstElementClass),
169 gst_element_base_class_init,
170 NULL, /* base_class_finalize */
171 (GClassInitFunc) gst_element_class_init,
176 (GInstanceInitFunc) gst_element_init,
180 _type = g_type_register_static (GST_TYPE_OBJECT, "GstElement",
181 &element_info, G_TYPE_FLAG_ABSTRACT);
183 __gst_elementclass_factory =
184 g_quark_from_static_string ("GST_ELEMENTCLASS_FACTORY");
185 g_once_init_leave (&gst_element_type, _type);
187 return gst_element_type;
191 gst_element_setup_thread_pool (void)
195 GST_DEBUG ("creating element thread pool");
197 g_thread_pool_new ((GFunc) gst_element_call_async_func, NULL, -1, FALSE,
200 g_critical ("could not alloc threadpool %s", err->message);
201 g_clear_error (&err);
206 gst_element_class_init (GstElementClass * klass)
208 GObjectClass *gobject_class;
210 gobject_class = (GObjectClass *) klass;
212 parent_class = g_type_class_peek_parent (klass);
215 * GstElement::pad-added:
216 * @gstelement: the object which received the signal
217 * @new_pad: the pad that has been added
219 * a new #GstPad has been added to the element. Note that this signal will
220 * usually be emitted from the context of the streaming thread. Also keep in
221 * mind that if you add new elements to the pipeline in the signal handler
222 * you will need to set them to the desired target state with
223 * gst_element_set_state() or gst_element_sync_state_with_parent().
225 gst_element_signals[PAD_ADDED] =
226 g_signal_new ("pad-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
227 G_STRUCT_OFFSET (GstElementClass, pad_added), NULL, NULL,
228 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
230 * GstElement::pad-removed:
231 * @gstelement: the object which received the signal
232 * @old_pad: the pad that has been removed
234 * a #GstPad has been removed from the element
236 gst_element_signals[PAD_REMOVED] =
237 g_signal_new ("pad-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
238 G_STRUCT_OFFSET (GstElementClass, pad_removed), NULL, NULL,
239 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
241 * GstElement::no-more-pads:
242 * @gstelement: the object which received the signal
244 * This signals that the element will not generate more dynamic pads.
245 * Note that this signal will usually be emitted from the context of
246 * the streaming thread.
248 gst_element_signals[NO_MORE_PADS] =
249 g_signal_new ("no-more-pads", G_TYPE_FROM_CLASS (klass),
250 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstElementClass, no_more_pads), NULL,
251 NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0);
253 gobject_class->dispose = gst_element_dispose;
254 gobject_class->finalize = gst_element_finalize;
255 gobject_class->constructed = gst_element_constructed;
257 klass->change_state = GST_DEBUG_FUNCPTR (gst_element_change_state_func);
258 klass->set_state = GST_DEBUG_FUNCPTR (gst_element_set_state_func);
259 klass->get_state = GST_DEBUG_FUNCPTR (gst_element_get_state_func);
260 klass->set_clock = GST_DEBUG_FUNCPTR (gst_element_set_clock_func);
261 klass->set_bus = GST_DEBUG_FUNCPTR (gst_element_set_bus_func);
262 klass->query = GST_DEBUG_FUNCPTR (gst_element_default_query);
263 klass->send_event = GST_DEBUG_FUNCPTR (gst_element_default_send_event);
264 klass->numpadtemplates = 0;
265 klass->post_message = GST_DEBUG_FUNCPTR (gst_element_post_message_default);
266 klass->set_context = GST_DEBUG_FUNCPTR (gst_element_set_context_default);
268 klass->elementfactory = NULL;
270 gst_element_setup_thread_pool ();
274 gst_element_base_class_init (gpointer g_class)
276 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
277 GList *node, *padtemplates;
279 /* Copy the element details here so elements can inherit the
280 * details from their base class and classes only need to set
281 * the details in class_init instead of base_init */
282 element_class->metadata =
283 element_class->metadata ? gst_structure_copy (element_class->metadata) :
284 gst_structure_new_empty ("metadata");
286 /* Copy the pad templates so elements inherit them
287 * from their base class but elements can add pad templates in class_init
288 * instead of base_init.
290 padtemplates = g_list_copy (element_class->padtemplates);
291 for (node = padtemplates; node != NULL; node = node->next) {
292 GstPadTemplate *tmpl = (GstPadTemplate *) node->data;
293 gst_object_ref (tmpl);
295 element_class->padtemplates = padtemplates;
297 /* set the factory, see gst_element_register() */
298 element_class->elementfactory =
299 g_type_get_qdata (G_TYPE_FROM_CLASS (element_class),
300 __gst_elementclass_factory);
301 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "type %s : factory %p",
302 G_OBJECT_CLASS_NAME (element_class), element_class->elementfactory);
306 gst_element_init (GstElement * element)
308 GST_STATE (element) = GST_STATE_NULL;
309 GST_STATE_TARGET (element) = GST_STATE_NULL;
310 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
311 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
312 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
314 g_rec_mutex_init (&element->state_lock);
315 g_cond_init (&element->state_cond);
319 gst_element_constructed (GObject * object)
321 GST_TRACER_ELEMENT_NEW (GST_ELEMENT_CAST (object));
322 G_OBJECT_CLASS (parent_class)->constructed (object);
326 * gst_element_release_request_pad:
327 * @element: a #GstElement to release the request pad of.
328 * @pad: the #GstPad to release.
330 * Makes the element free the previously requested pad as obtained
331 * with gst_element_request_pad().
333 * This does not unref the pad. If the pad was created by using
334 * gst_element_request_pad(), gst_element_release_request_pad() needs to be
335 * followed by gst_object_unref() to free the @pad.
340 gst_element_release_request_pad (GstElement * element, GstPad * pad)
342 GstElementClass *oclass;
344 g_return_if_fail (GST_IS_ELEMENT (element));
345 g_return_if_fail (GST_IS_PAD (pad));
346 g_return_if_fail (GST_PAD_PAD_TEMPLATE (pad) == NULL ||
347 GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (pad)) ==
349 g_return_if_fail (GST_PAD_PARENT (pad) == element);
351 oclass = GST_ELEMENT_GET_CLASS (element);
353 /* if the element implements a custom release function we call that, else we
354 * simply remove the pad from the element */
355 if (oclass->release_pad)
356 oclass->release_pad (element, pad);
358 gst_element_remove_pad (element, pad);
362 * gst_element_provide_clock:
363 * @element: a #GstElement to query
365 * Get the clock provided by the given element.
366 * > An element is only required to provide a clock in the PAUSED
367 * > state. Some elements can provide a clock in other states.
369 * Returns: (transfer full) (nullable): the GstClock provided by the
370 * element or %NULL if no clock could be provided. Unref after usage.
375 gst_element_provide_clock (GstElement * element)
377 GstClock *result = NULL;
378 GstElementClass *oclass;
380 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
382 oclass = GST_ELEMENT_GET_CLASS (element);
384 if (oclass->provide_clock)
385 result = oclass->provide_clock (element);
391 gst_element_set_clock_func (GstElement * element, GstClock * clock)
395 GST_OBJECT_LOCK (element);
396 clock_p = &element->clock;
397 gst_object_replace ((GstObject **) clock_p, (GstObject *) clock);
398 GST_OBJECT_UNLOCK (element);
404 * gst_element_set_clock:
405 * @element: a #GstElement to set the clock for.
406 * @clock: the #GstClock to set for the element.
408 * Sets the clock for the element. This function increases the
409 * refcount on the clock. Any previously set clock on the object
412 * Returns: %TRUE if the element accepted the clock. An element can refuse a
413 * clock when it, for example, is not able to slave its internal clock to the
414 * @clock or when it requires a specific clock to operate.
419 gst_element_set_clock (GstElement * element, GstClock * clock)
421 GstElementClass *oclass;
422 gboolean res = FALSE;
424 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
425 g_return_val_if_fail (clock == NULL || GST_IS_CLOCK (clock), FALSE);
427 oclass = GST_ELEMENT_GET_CLASS (element);
429 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element, "setting clock %p", clock);
431 if (oclass->set_clock)
432 res = oclass->set_clock (element, clock);
438 * gst_element_get_clock:
439 * @element: a #GstElement to get the clock of.
441 * Gets the currently configured clock of the element. This is the clock as was
442 * last set with gst_element_set_clock().
444 * Elements in a pipeline will only have their clock set when the
445 * pipeline is in the PLAYING state.
447 * Returns: (transfer full): the #GstClock of the element. unref after usage.
452 gst_element_get_clock (GstElement * element)
456 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
458 GST_OBJECT_LOCK (element);
459 if ((result = element->clock))
460 gst_object_ref (result);
461 GST_OBJECT_UNLOCK (element);
467 * gst_element_set_base_time:
468 * @element: a #GstElement.
469 * @time: the base time to set.
471 * Set the base time of an element. See gst_element_get_base_time().
476 gst_element_set_base_time (GstElement * element, GstClockTime time)
480 g_return_if_fail (GST_IS_ELEMENT (element));
482 GST_OBJECT_LOCK (element);
483 old = element->base_time;
484 element->base_time = time;
485 GST_OBJECT_UNLOCK (element);
487 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element,
488 "set base_time=%" GST_TIME_FORMAT ", old %" GST_TIME_FORMAT,
489 GST_TIME_ARGS (time), GST_TIME_ARGS (old));
493 * gst_element_get_base_time:
494 * @element: a #GstElement.
496 * Returns the base time of the element. The base time is the
497 * absolute time of the clock when this element was last put to
498 * PLAYING. Subtracting the base time from the clock time gives
499 * the running time of the element.
501 * Returns: the base time of the element.
506 gst_element_get_base_time (GstElement * element)
510 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
512 GST_OBJECT_LOCK (element);
513 result = element->base_time;
514 GST_OBJECT_UNLOCK (element);
520 * gst_element_set_start_time:
521 * @element: a #GstElement.
522 * @time: the base time to set.
524 * Set the start time of an element. The start time of the element is the
525 * running time of the element when it last went to the PAUSED state. In READY
526 * or after a flushing seek, it is set to 0.
528 * Toplevel elements like #GstPipeline will manage the start_time and
529 * base_time on its children. Setting the start_time to #GST_CLOCK_TIME_NONE
530 * on such a toplevel element will disable the distribution of the base_time to
531 * the children and can be useful if the application manages the base_time
532 * itself, for example if you want to synchronize capture from multiple
533 * pipelines, and you can also ensure that the pipelines have the same clock.
538 gst_element_set_start_time (GstElement * element, GstClockTime time)
542 g_return_if_fail (GST_IS_ELEMENT (element));
544 GST_OBJECT_LOCK (element);
545 old = GST_ELEMENT_START_TIME (element);
546 GST_ELEMENT_START_TIME (element) = time;
547 GST_OBJECT_UNLOCK (element);
549 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element,
550 "set start_time=%" GST_TIME_FORMAT ", old %" GST_TIME_FORMAT,
551 GST_TIME_ARGS (time), GST_TIME_ARGS (old));
555 * gst_element_get_start_time:
556 * @element: a #GstElement.
558 * Returns the start time of the element. The start time is the
559 * running time of the clock when this element was last put to PAUSED.
561 * Usually the start_time is managed by a toplevel element such as
566 * Returns: the start time of the element.
569 gst_element_get_start_time (GstElement * element)
573 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
575 GST_OBJECT_LOCK (element);
576 result = GST_ELEMENT_START_TIME (element);
577 GST_OBJECT_UNLOCK (element);
584 * gst_element_set_index:
585 * @element: a #GstElement.
586 * @index: (transfer none): a #GstIndex.
588 * Set @index on the element. The refcount of the index
589 * will be increased, any previously set index is unreffed.
594 gst_element_set_index (GstElement * element, GstIndex * index)
596 GstElementClass *oclass;
598 g_return_if_fail (GST_IS_ELEMENT (element));
599 g_return_if_fail (index == NULL || GST_IS_INDEX (index));
601 oclass = GST_ELEMENT_GET_CLASS (element);
603 if (oclass->set_index)
604 oclass->set_index (element, index);
608 * gst_element_get_index:
609 * @element: a #GstElement.
611 * Gets the index from the element.
613 * Returns: (transfer full) (nullable): a #GstIndex or %NULL when no
614 * index was set on the element. unref after usage.
619 gst_element_get_index (GstElement * element)
621 GstElementClass *oclass;
622 GstIndex *result = NULL;
624 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
626 oclass = GST_ELEMENT_GET_CLASS (element);
628 if (oclass->get_index)
629 result = oclass->get_index (element);
636 * gst_element_add_pad:
637 * @element: a #GstElement to add the pad to.
638 * @pad: (transfer floating): the #GstPad to add to the element.
640 * Adds a pad (link point) to @element. @pad's parent will be set to @element;
641 * see gst_object_set_parent() for refcounting information.
643 * Pads are not automatically activated so elements should perform the needed
644 * steps to activate the pad in case this pad is added in the PAUSED or PLAYING
645 * state. See gst_pad_set_active() for more information about activating pads.
647 * The pad and the element should be unlocked when calling this function.
649 * This function will emit the #GstElement::pad-added signal on the element.
651 * Returns: %TRUE if the pad could be added. This function can fail when
652 * a pad with the same name already existed or the pad already had another
658 gst_element_add_pad (GstElement * element, GstPad * pad)
663 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
664 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
666 /* locking pad to look at the name */
667 GST_OBJECT_LOCK (pad);
668 pad_name = g_strdup (GST_PAD_NAME (pad));
669 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "adding pad '%s'",
670 GST_STR_NULL (pad_name));
671 active = GST_PAD_IS_ACTIVE (pad);
672 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_PARENT);
673 GST_OBJECT_UNLOCK (pad);
675 /* then check to see if there's already a pad by that name here */
676 GST_OBJECT_LOCK (element);
677 if (G_UNLIKELY (!gst_object_check_uniqueness (element->pads, pad_name)))
680 /* try to set the pad's parent */
681 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (pad),
682 GST_OBJECT_CAST (element))))
685 /* check for active pads */
686 if (!active && (GST_STATE (element) > GST_STATE_READY ||
687 GST_STATE_NEXT (element) == GST_STATE_PAUSED)) {
688 g_warning ("adding inactive pad '%s' to running element '%s', you need to "
689 "use gst_pad_set_active(pad,TRUE) before adding it.",
690 GST_STR_NULL (pad_name), GST_ELEMENT_NAME (element));
691 gst_pad_set_active (pad, TRUE);
696 /* add it to the list */
697 switch (gst_pad_get_direction (pad)) {
699 element->srcpads = g_list_append (element->srcpads, pad);
700 element->numsrcpads++;
703 element->sinkpads = g_list_append (element->sinkpads, pad);
704 element->numsinkpads++;
709 element->pads = g_list_append (element->pads, pad);
711 element->pads_cookie++;
712 GST_OBJECT_UNLOCK (element);
714 /* emit the PAD_ADDED signal */
715 g_signal_emit (element, gst_element_signals[PAD_ADDED], 0, pad);
716 GST_TRACER_ELEMENT_ADD_PAD (element, pad);
722 g_critical ("Padname %s is not unique in element %s, not adding",
723 pad_name, GST_ELEMENT_NAME (element));
724 GST_OBJECT_UNLOCK (element);
726 gst_object_ref_sink (pad);
727 gst_object_unref (pad);
733 ("Pad %s already has parent when trying to add to element %s",
734 pad_name, GST_ELEMENT_NAME (element));
735 GST_OBJECT_UNLOCK (element);
741 GST_OBJECT_LOCK (pad);
743 ("Trying to add pad %s to element %s, but it has no direction",
744 GST_OBJECT_NAME (pad), GST_ELEMENT_NAME (element));
745 GST_OBJECT_UNLOCK (pad);
746 GST_OBJECT_UNLOCK (element);
752 * gst_element_remove_pad:
753 * @element: a #GstElement to remove pad from.
754 * @pad: (transfer full): the #GstPad to remove from the element.
756 * Removes @pad from @element. @pad will be destroyed if it has not been
757 * referenced elsewhere using gst_object_unparent().
759 * This function is used by plugin developers and should not be used
760 * by applications. Pads that were dynamically requested from elements
761 * with gst_element_request_pad() should be released with the
762 * gst_element_release_request_pad() function instead.
764 * Pads are not automatically deactivated so elements should perform the needed
765 * steps to deactivate the pad in case this pad is removed in the PAUSED or
766 * PLAYING state. See gst_pad_set_active() for more information about
769 * The pad and the element should be unlocked when calling this function.
771 * This function will emit the #GstElement::pad-removed signal on the element.
773 * Returns: %TRUE if the pad could be removed. Can return %FALSE if the
774 * pad does not belong to the provided element.
779 gst_element_remove_pad (GstElement * element, GstPad * pad)
783 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
784 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
786 /* locking pad to look at the name and parent */
787 GST_OBJECT_LOCK (pad);
788 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "removing pad '%s'",
789 GST_STR_NULL (GST_PAD_NAME (pad)));
791 if (G_UNLIKELY (GST_PAD_PARENT (pad) != element))
793 GST_OBJECT_UNLOCK (pad);
796 if ((peer = gst_pad_get_peer (pad))) {
797 /* window for MT unsafeness, someone else could unlink here
798 * and then we call unlink with wrong pads. The unlink
799 * function would catch this and safely return failed. */
800 if (GST_PAD_IS_SRC (pad))
801 gst_pad_unlink (pad, peer);
803 gst_pad_unlink (peer, pad);
805 gst_object_unref (peer);
808 GST_OBJECT_LOCK (element);
809 /* remove it from the list */
810 switch (gst_pad_get_direction (pad)) {
812 element->srcpads = g_list_remove (element->srcpads, pad);
813 element->numsrcpads--;
816 element->sinkpads = g_list_remove (element->sinkpads, pad);
817 element->numsinkpads--;
820 g_critical ("Removing pad without direction???");
823 element->pads = g_list_remove (element->pads, pad);
825 element->pads_cookie++;
826 GST_OBJECT_UNLOCK (element);
828 /* emit the PAD_REMOVED signal before unparenting and losing the last ref. */
829 g_signal_emit (element, gst_element_signals[PAD_REMOVED], 0, pad);
830 GST_TRACER_ELEMENT_REMOVE_PAD (element, pad);
831 gst_object_unparent (GST_OBJECT_CAST (pad));
838 /* locking order is element > pad */
839 GST_OBJECT_UNLOCK (pad);
841 GST_OBJECT_LOCK (element);
842 GST_OBJECT_LOCK (pad);
843 g_critical ("Padname %s:%s does not belong to element %s when removing",
844 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
845 GST_OBJECT_UNLOCK (pad);
846 GST_OBJECT_UNLOCK (element);
852 * gst_element_no_more_pads:
853 * @element: a #GstElement
855 * Use this function to signal that the element does not expect any more pads
856 * to show up in the current pipeline. This function should be called whenever
857 * pads have been added by the element itself. Elements with #GST_PAD_SOMETIMES
858 * pad templates use this in combination with autopluggers to figure out that
859 * the element is done initializing its pads.
861 * This function emits the #GstElement::no-more-pads signal.
866 gst_element_no_more_pads (GstElement * element)
868 g_return_if_fail (GST_IS_ELEMENT (element));
870 g_signal_emit (element, gst_element_signals[NO_MORE_PADS], 0);
874 pad_compare_name (GstPad * pad1, const gchar * name)
878 GST_OBJECT_LOCK (pad1);
879 result = strcmp (GST_PAD_NAME (pad1), name);
880 GST_OBJECT_UNLOCK (pad1);
886 * gst_element_get_static_pad:
887 * @element: a #GstElement to find a static pad of.
888 * @name: the name of the static #GstPad to retrieve.
890 * Retrieves a pad from @element by name. This version only retrieves
891 * already-existing (i.e. 'static') pads.
893 * Returns: (transfer full) (nullable): the requested #GstPad if
894 * found, otherwise %NULL. unref after usage.
899 gst_element_get_static_pad (GstElement * element, const gchar * name)
902 GstPad *result = NULL;
904 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
905 g_return_val_if_fail (name != NULL, NULL);
907 GST_OBJECT_LOCK (element);
909 g_list_find_custom (element->pads, name, (GCompareFunc) pad_compare_name);
911 result = GST_PAD_CAST (find->data);
912 gst_object_ref (result);
915 if (result == NULL) {
916 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "no such pad '%s' in element \"%s\"",
917 name, GST_ELEMENT_NAME (element));
919 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
920 GST_ELEMENT_NAME (element), name);
922 GST_OBJECT_UNLOCK (element);
928 gst_element_is_valid_request_template_name (const gchar * templ_name,
932 const gchar *templ_name_ptr, *name_ptr;
933 gboolean next_specifier;
934 guint templ_postfix_len = 0, name_postfix_len = 0;
936 g_return_val_if_fail (templ_name != NULL, FALSE);
937 g_return_val_if_fail (name != NULL, FALSE);
939 /* Is this the template name? */
940 if (strcmp (templ_name, name) == 0)
943 /* otherwise check all the specifiers */
945 /* Because of sanity checks in gst_pad_template_new(), we know that %s
946 * and %d and %u, occurring at the template_name */
947 templ_name_ptr = strchr (templ_name, '%');
949 /* check characters ahead of the specifier */
950 if (!templ_name_ptr || strlen (name) <= templ_name_ptr - templ_name
951 || strncmp (templ_name, name, templ_name_ptr - templ_name) != 0) {
955 /* %s is not allowed for multiple specifiers, just a single specifier can be
956 * accepted in gst_pad_template_new() and can not be mixed with other
957 * specifier '%u' and '%d' */
958 if (*(templ_name_ptr + 1) == 's' && g_strcmp0 (templ_name, name) == 0) {
962 name_ptr = name + (templ_name_ptr - templ_name);
964 /* search next specifier, each of specifier should be separated by '_' */
965 templ_name = strchr (templ_name_ptr, '_');
966 name = strchr (name_ptr, '_');
968 /* don't match the number of specifiers */
969 if ((templ_name && !name) || (!templ_name && name))
972 if (templ_name && name)
973 next_specifier = TRUE;
975 next_specifier = FALSE;
977 /* check characters followed by the specifier */
978 if (*(templ_name_ptr + 2) != '\0' && *(templ_name_ptr + 2) != '_') {
979 if (next_specifier) {
980 templ_postfix_len = templ_name - (templ_name_ptr + 2);
981 name_postfix_len = name - name_ptr;
983 templ_postfix_len = strlen (templ_name_ptr + 2);
984 name_postfix_len = strlen (name_ptr);
987 if (strncmp (templ_name_ptr + 2,
988 name_ptr + name_postfix_len - templ_postfix_len,
989 templ_postfix_len) != 0) {
994 /* verify the specifier */
995 if (*(name_ptr) == '%') {
998 len = (next_specifier) ? name - name_ptr : strlen (name_ptr);
1000 if (strncmp (name_ptr, templ_name_ptr, len) != 0)
1004 const gchar *specifier;
1005 gchar *target = NULL;
1007 /* extract specifier when it has postfix characters */
1008 if (name_postfix_len > templ_postfix_len) {
1009 target = g_strndup (name_ptr, name_postfix_len - templ_postfix_len);
1011 specifier = target ? target : name_ptr;
1013 if (*(templ_name_ptr + 1) == 'd') {
1017 tmp = g_ascii_strtoll (specifier, &endptr, 10);
1018 if (tmp < G_MININT || tmp > G_MAXINT || (*endptr != '\0'
1021 } else if (*(templ_name_ptr + 1) == 'u') {
1025 tmp = g_ascii_strtoull (specifier, &endptr, 10);
1026 if (tmp > G_MAXUINT || (*endptr != '\0' && *endptr != '_'))
1033 /* otherwise we increment these from NULL to 1 */
1034 if (next_specifier) {
1038 } while (next_specifier);
1044 _gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
1045 const gchar * name, const GstCaps * caps)
1047 GstPad *newpad = NULL;
1048 GstElementClass *oclass;
1050 oclass = GST_ELEMENT_GET_CLASS (element);
1052 #ifndef G_DISABLE_CHECKS
1053 /* Some sanity checking here */
1057 g_return_val_if_fail (gst_element_is_valid_request_template_name
1058 (templ->name_template, name), NULL);
1060 pad = gst_element_get_static_pad (element, name);
1062 gst_object_unref (pad);
1063 /* FIXME 2.0: Change this to g_return_val_if_fail() */
1064 g_critical ("Element %s already has a pad named %s, the behaviour of "
1065 " gst_element_get_request_pad() for existing pads is undefined!",
1066 GST_ELEMENT_NAME (element), name);
1071 if (oclass->request_new_pad)
1072 newpad = (oclass->request_new_pad) (element, templ, name, caps);
1075 gst_object_ref (newpad);
1081 * gst_element_get_request_pad:
1082 * @element: a #GstElement to find a request pad of.
1083 * @name: the name of the request #GstPad to retrieve.
1085 * Retrieves a pad from the element by name (e.g. "src_\%d"). This version only
1086 * retrieves request pads. The pad should be released with
1087 * gst_element_release_request_pad().
1089 * This method is slower than manually getting the pad template and calling
1090 * gst_element_request_pad() if the pads should have a specific name (e.g.
1091 * @name is "src_1" instead of "src_\%u").
1093 * Returns: (transfer full) (nullable): requested #GstPad if found,
1094 * otherwise %NULL. Release after usage.
1097 gst_element_get_request_pad (GstElement * element, const gchar * name)
1099 GstPadTemplate *templ = NULL;
1101 const gchar *req_name = NULL;
1102 gboolean templ_found = FALSE;
1104 GstElementClass *class;
1106 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1107 g_return_val_if_fail (name != NULL, NULL);
1109 class = GST_ELEMENT_GET_CLASS (element);
1111 templ = gst_element_class_get_request_pad_template (class, name);
1113 req_name = strstr (name, "%") ? NULL : name;
1116 /* there is no % in the name, try to find a matching template */
1117 list = class->padtemplates;
1118 while (!templ_found && list) {
1119 templ = (GstPadTemplate *) list->data;
1120 if (templ->presence == GST_PAD_REQUEST) {
1121 GST_CAT_DEBUG (GST_CAT_PADS, "comparing %s to %s", name,
1122 templ->name_template);
1123 if (gst_element_is_valid_request_template_name (templ->name_template,
1137 pad = _gst_element_request_pad (element, templ, req_name, NULL);
1143 * gst_element_request_pad: (virtual request_new_pad)
1144 * @element: a #GstElement to find a request pad of.
1145 * @templ: a #GstPadTemplate of which we want a pad of.
1146 * @name: (transfer none) (allow-none): the name of the request #GstPad
1147 * to retrieve. Can be %NULL.
1148 * @caps: (transfer none) (allow-none): the caps of the pad we want to
1149 * request. Can be %NULL.
1151 * Retrieves a request pad from the element according to the provided template.
1152 * Pad templates can be looked up using
1153 * gst_element_factory_get_static_pad_templates().
1155 * The pad should be released with gst_element_release_request_pad().
1157 * Returns: (transfer full) (nullable): requested #GstPad if found,
1158 * otherwise %NULL. Release after usage.
1161 gst_element_request_pad (GstElement * element,
1162 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
1164 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1165 g_return_val_if_fail (templ != NULL, NULL);
1166 g_return_val_if_fail (templ->presence == GST_PAD_REQUEST, NULL);
1168 return _gst_element_request_pad (element, templ, name, caps);
1171 static GstIterator *
1172 gst_element_iterate_pad_list (GstElement * element, GList ** padlist)
1174 GstIterator *result;
1176 GST_OBJECT_LOCK (element);
1177 result = gst_iterator_new_list (GST_TYPE_PAD,
1178 GST_OBJECT_GET_LOCK (element),
1179 &element->pads_cookie, padlist, (GObject *) element, NULL);
1180 GST_OBJECT_UNLOCK (element);
1186 * gst_element_iterate_pads:
1187 * @element: a #GstElement to iterate pads of.
1189 * Retrieves an iterator of @element's pads. The iterator should
1190 * be freed after usage. Also more specialized iterators exists such as
1191 * gst_element_iterate_src_pads() or gst_element_iterate_sink_pads().
1193 * The order of pads returned by the iterator will be the order in which
1194 * the pads were added to the element.
1196 * Returns: (transfer full): the #GstIterator of #GstPad.
1201 gst_element_iterate_pads (GstElement * element)
1203 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1205 return gst_element_iterate_pad_list (element, &element->pads);
1209 * gst_element_iterate_src_pads:
1210 * @element: a #GstElement.
1212 * Retrieves an iterator of @element's source pads.
1214 * The order of pads returned by the iterator will be the order in which
1215 * the pads were added to the element.
1217 * Returns: (transfer full): the #GstIterator of #GstPad.
1222 gst_element_iterate_src_pads (GstElement * element)
1224 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1226 return gst_element_iterate_pad_list (element, &element->srcpads);
1230 * gst_element_iterate_sink_pads:
1231 * @element: a #GstElement.
1233 * Retrieves an iterator of @element's sink pads.
1235 * The order of pads returned by the iterator will be the order in which
1236 * the pads were added to the element.
1238 * Returns: (transfer full): the #GstIterator of #GstPad.
1243 gst_element_iterate_sink_pads (GstElement * element)
1245 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1247 return gst_element_iterate_pad_list (element, &element->sinkpads);
1251 gst_element_do_foreach_pad (GstElement * element,
1252 GstElementForeachPadFunc func, gpointer user_data,
1253 GList ** p_pads, guint16 * p_npads)
1255 gboolean ret = TRUE;
1260 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1261 g_return_val_if_fail (func != NULL, FALSE);
1263 GST_OBJECT_LOCK (element);
1265 pads = g_newa (GstPad *, n_pads + 1);
1266 for (l = *p_pads, i = 0; l != NULL; l = l->next) {
1267 g_assert (i < n_pads);
1268 pads[i++] = gst_object_ref (l->data);
1270 GST_OBJECT_UNLOCK (element);
1275 for (i = 0; i < n_pads; ++i) {
1276 ret = func (element, pads[i], user_data);
1281 for (i = 0; i < n_pads; ++i)
1282 gst_object_unref (pads[i]);
1288 * gst_element_foreach_sink_pad:
1289 * @element: a #GstElement to iterate sink pads of
1290 * @func: (scope call): function to call for each sink pad
1291 * @user_data: (closure): user data passed to @func
1293 * Call @func with @user_data for each of @element's sink pads. @func will be
1294 * called exactly once for each sink pad that exists at the time of this call,
1295 * unless one of the calls to @func returns %FALSE in which case we will stop
1296 * iterating pads and return early. If new sink pads are added or sink pads
1297 * are removed while the sink pads are being iterated, this will not be taken
1298 * into account until next time this function is used.
1300 * Returns: %FALSE if @element had no sink pads or if one of the calls to @func
1306 gst_element_foreach_sink_pad (GstElement * element,
1307 GstElementForeachPadFunc func, gpointer user_data)
1309 return gst_element_do_foreach_pad (element, func, user_data,
1310 &element->sinkpads, &element->numsinkpads);
1314 * gst_element_foreach_src_pad:
1315 * @element: a #GstElement to iterate source pads of
1316 * @func: (scope call): function to call for each source pad
1317 * @user_data: (closure): user data passed to @func
1319 * Call @func with @user_data for each of @element's source pads. @func will be
1320 * called exactly once for each source pad that exists at the time of this call,
1321 * unless one of the calls to @func returns %FALSE in which case we will stop
1322 * iterating pads and return early. If new source pads are added or source pads
1323 * are removed while the source pads are being iterated, this will not be taken
1324 * into account until next time this function is used.
1326 * Returns: %FALSE if @element had no source pads or if one of the calls
1327 * to @func returned %FALSE.
1332 gst_element_foreach_src_pad (GstElement * element,
1333 GstElementForeachPadFunc func, gpointer user_data)
1335 return gst_element_do_foreach_pad (element, func, user_data,
1336 &element->srcpads, &element->numsrcpads);
1340 * gst_element_foreach_pad:
1341 * @element: a #GstElement to iterate pads of
1342 * @func: (scope call): function to call for each pad
1343 * @user_data: (closure): user data passed to @func
1345 * Call @func with @user_data for each of @element's pads. @func will be called
1346 * exactly once for each pad that exists at the time of this call, unless
1347 * one of the calls to @func returns %FALSE in which case we will stop
1348 * iterating pads and return early. If new pads are added or pads are removed
1349 * while pads are being iterated, this will not be taken into account until
1350 * next time this function is used.
1352 * Returns: %FALSE if @element had no pads or if one of the calls to @func
1358 gst_element_foreach_pad (GstElement * element, GstElementForeachPadFunc func,
1361 return gst_element_do_foreach_pad (element, func, user_data,
1362 &element->pads, &element->numpads);
1366 * gst_element_class_add_pad_template:
1367 * @klass: the #GstElementClass to add the pad template to.
1368 * @templ: (transfer floating): a #GstPadTemplate to add to the element class.
1370 * Adds a padtemplate to an element class. This is mainly used in the _class_init
1371 * functions of classes. If a pad template with the same name as an already
1372 * existing one is added the old one is replaced by the new one.
1374 * @templ's reference count will be incremented, and any floating
1375 * reference will be removed (see gst_object_ref_sink())
1379 gst_element_class_add_pad_template (GstElementClass * klass,
1380 GstPadTemplate * templ)
1382 GList *template_list = klass->padtemplates;
1384 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1385 g_return_if_fail (GST_IS_PAD_TEMPLATE (templ));
1387 /* If we already have a pad template with the same name replace the
1389 while (template_list) {
1390 GstPadTemplate *padtempl = (GstPadTemplate *) template_list->data;
1392 /* Found pad with the same name, replace and return */
1393 if (strcmp (templ->name_template, padtempl->name_template) == 0) {
1394 gst_object_ref_sink (padtempl);
1395 gst_object_unref (padtempl);
1396 template_list->data = templ;
1399 template_list = g_list_next (template_list);
1402 /* Take ownership of the floating ref */
1403 gst_object_ref_sink (templ);
1405 klass->padtemplates = g_list_append (klass->padtemplates, templ);
1406 klass->numpadtemplates++;
1410 * gst_element_class_add_static_pad_template:
1411 * @klass: the #GstElementClass to add the pad template to.
1412 * @static_templ: #GstStaticPadTemplate to add as pad template to the element class.
1414 * Adds a pad template to an element class based on the static pad template
1415 * @templ. This is mainly used in the _class_init functions of element
1416 * implementations. If a pad template with the same name already exists,
1417 * the old one is replaced by the new one.
1422 gst_element_class_add_static_pad_template (GstElementClass * klass,
1423 GstStaticPadTemplate * static_templ)
1425 gst_element_class_add_pad_template (klass,
1426 gst_static_pad_template_get (static_templ));
1430 * gst_element_class_add_metadata:
1431 * @klass: class to set metadata for
1432 * @key: the key to set
1433 * @value: the value to set
1435 * Set @key with @value as metadata in @klass.
1438 gst_element_class_add_metadata (GstElementClass * klass,
1439 const gchar * key, const gchar * value)
1441 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1442 g_return_if_fail (key != NULL);
1443 g_return_if_fail (value != NULL);
1445 gst_structure_set ((GstStructure *) klass->metadata,
1446 key, G_TYPE_STRING, value, NULL);
1450 * gst_element_class_add_static_metadata:
1451 * @klass: class to set metadata for
1452 * @key: the key to set
1453 * @value: the value to set
1455 * Set @key with @value as metadata in @klass.
1457 * Same as gst_element_class_add_metadata(), but @value must be a static string
1458 * or an inlined string, as it will not be copied. (GStreamer plugins will
1459 * be made resident once loaded, so this function can be used even from
1460 * dynamically loaded plugins.)
1463 gst_element_class_add_static_metadata (GstElementClass * klass,
1464 const gchar * key, const gchar * value)
1466 GValue val = G_VALUE_INIT;
1468 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1469 g_return_if_fail (key != NULL);
1470 g_return_if_fail (value != NULL);
1472 g_value_init (&val, G_TYPE_STRING);
1473 g_value_set_static_string (&val, value);
1474 gst_structure_take_value ((GstStructure *) klass->metadata, key, &val);
1478 * gst_element_class_set_metadata:
1479 * @klass: class to set metadata for
1480 * @longname: The long English name of the element. E.g. "File Sink"
1481 * @classification: String describing the type of element, as an unordered list
1482 * separated with slashes ('/'). See draft-klass.txt of the design docs
1483 * for more details and common types. E.g: "Sink/File"
1484 * @description: Sentence describing the purpose of the element.
1485 * E.g: "Write stream to a file"
1486 * @author: Name and contact details of the author(s). Use \n to separate
1487 * multiple author metadata. E.g: "Joe Bloggs <joe.blogs at foo.com>"
1489 * Sets the detailed information for a #GstElementClass.
1490 * > This function is for use in _class_init functions only.
1493 gst_element_class_set_metadata (GstElementClass * klass,
1494 const gchar * longname, const gchar * classification,
1495 const gchar * description, const gchar * author)
1497 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1498 g_return_if_fail (longname != NULL && *longname != '\0');
1499 g_return_if_fail (classification != NULL && *classification != '\0');
1500 g_return_if_fail (description != NULL && *description != '\0');
1501 g_return_if_fail (author != NULL && *author != '\0');
1503 gst_structure_id_set ((GstStructure *) klass->metadata,
1504 GST_QUARK (ELEMENT_METADATA_LONGNAME), G_TYPE_STRING, longname,
1505 GST_QUARK (ELEMENT_METADATA_KLASS), G_TYPE_STRING, classification,
1506 GST_QUARK (ELEMENT_METADATA_DESCRIPTION), G_TYPE_STRING, description,
1507 GST_QUARK (ELEMENT_METADATA_AUTHOR), G_TYPE_STRING, author, NULL);
1511 * gst_element_class_set_static_metadata:
1512 * @klass: class to set metadata for
1513 * @longname: The long English name of the element. E.g. "File Sink"
1514 * @classification: String describing the type of element, as an unordered list
1515 * separated with slashes ('/'). See draft-klass.txt of the design docs
1516 * for more details and common types. E.g: "Sink/File"
1517 * @description: Sentence describing the purpose of the element.
1518 * E.g: "Write stream to a file"
1519 * @author: Name and contact details of the author(s). Use \n to separate
1520 * multiple author metadata. E.g: "Joe Bloggs <joe.blogs at foo.com>"
1522 * Sets the detailed information for a #GstElementClass.
1524 * > This function is for use in _class_init functions only.
1526 * Same as gst_element_class_set_metadata(), but @longname, @classification,
1527 * @description, and @author must be static strings or inlined strings, as
1528 * they will not be copied. (GStreamer plugins will be made resident once
1529 * loaded, so this function can be used even from dynamically loaded plugins.)
1532 gst_element_class_set_static_metadata (GstElementClass * klass,
1533 const gchar * longname, const gchar * classification,
1534 const gchar * description, const gchar * author)
1536 GstStructure *s = (GstStructure *) klass->metadata;
1537 GValue val = G_VALUE_INIT;
1539 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1540 g_return_if_fail (longname != NULL && *longname != '\0');
1541 g_return_if_fail (classification != NULL && *classification != '\0');
1542 g_return_if_fail (description != NULL && *description != '\0');
1543 g_return_if_fail (author != NULL && *author != '\0');
1545 g_value_init (&val, G_TYPE_STRING);
1547 g_value_set_static_string (&val, longname);
1548 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_LONGNAME), &val);
1550 g_value_set_static_string (&val, classification);
1551 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_KLASS), &val);
1553 g_value_set_static_string (&val, description);
1554 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_DESCRIPTION),
1557 g_value_set_static_string (&val, author);
1558 gst_structure_id_take_value (s, GST_QUARK (ELEMENT_METADATA_AUTHOR), &val);
1562 * gst_element_class_get_metadata:
1563 * @klass: class to get metadata for
1564 * @key: the key to get
1566 * Get metadata with @key in @klass.
1568 * Returns: the metadata for @key.
1571 gst_element_class_get_metadata (GstElementClass * klass, const gchar * key)
1573 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (klass), NULL);
1574 g_return_val_if_fail (key != NULL, NULL);
1576 return gst_structure_get_string ((GstStructure *) klass->metadata, key);
1580 * gst_element_get_metadata:
1581 * @element: class to get metadata for
1582 * @key: the key to get
1584 * Get metadata with @key in @klass.
1586 * Returns: the metadata for @key.
1591 gst_element_get_metadata (GstElement * element, const gchar * key)
1593 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1594 g_return_val_if_fail (key != NULL, NULL);
1596 return gst_element_class_get_metadata (GST_ELEMENT_GET_CLASS (element), key);
1600 * gst_element_class_get_pad_template_list:
1601 * @element_class: a #GstElementClass to get pad templates of.
1603 * Retrieves a list of the pad templates associated with @element_class. The
1604 * list must not be modified by the calling code.
1605 * > If you use this function in the #GInstanceInitFunc of an object class
1606 * > that has subclasses, make sure to pass the g_class parameter of the
1607 * > #GInstanceInitFunc here.
1609 * Returns: (transfer none) (element-type Gst.PadTemplate): the #GList of
1613 gst_element_class_get_pad_template_list (GstElementClass * element_class)
1615 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1617 return element_class->padtemplates;
1621 * gst_element_get_pad_template_list:
1622 * @element: a #GstElement to get pad templates of.
1624 * Retrieves a list of the pad templates associated with @element. The
1625 * list must not be modified by the calling code.
1627 * Returns: (transfer none) (element-type Gst.PadTemplate): the #GList of
1633 gst_element_get_pad_template_list (GstElement * element)
1635 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1638 gst_element_class_get_pad_template_list (GST_ELEMENT_GET_CLASS (element));
1642 * gst_element_class_get_pad_template:
1643 * @element_class: a #GstElementClass to get the pad template of.
1644 * @name: the name of the #GstPadTemplate to get.
1646 * Retrieves a padtemplate from @element_class with the given name.
1647 * > If you use this function in the #GInstanceInitFunc of an object class
1648 * > that has subclasses, make sure to pass the g_class parameter of the
1649 * > #GInstanceInitFunc here.
1651 * Returns: (transfer none) (nullable): the #GstPadTemplate with the
1652 * given name, or %NULL if none was found. No unreferencing is
1656 gst_element_class_get_pad_template (GstElementClass *
1657 element_class, const gchar * name)
1661 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1662 g_return_val_if_fail (name != NULL, NULL);
1664 padlist = element_class->padtemplates;
1667 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
1669 if (strcmp (padtempl->name_template, name) == 0)
1672 padlist = g_list_next (padlist);
1679 * gst_element_get_pad_template:
1680 * @element: a #GstElement to get the pad template of.
1681 * @name: the name of the #GstPadTemplate to get.
1683 * Retrieves a padtemplate from @element with the given name.
1685 * Returns: (transfer none) (nullable): the #GstPadTemplate with the
1686 * given name, or %NULL if none was found. No unreferencing is
1692 gst_element_get_pad_template (GstElement * element, const gchar * name)
1694 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1695 g_return_val_if_fail (name != NULL, NULL);
1697 return gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (element),
1701 static GstPadTemplate *
1702 gst_element_class_get_request_pad_template (GstElementClass *
1703 element_class, const gchar * name)
1705 GstPadTemplate *tmpl;
1707 tmpl = gst_element_class_get_pad_template (element_class, name);
1708 if (tmpl != NULL && tmpl->presence == GST_PAD_REQUEST)
1714 /* get a random pad on element of the given direction.
1715 * The pad is random in a sense that it is the first pad that is (optionaly) linked.
1718 gst_element_get_random_pad (GstElement * element,
1719 gboolean need_linked, GstPadDirection dir)
1721 GstPad *result = NULL;
1724 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "getting a random pad");
1728 GST_OBJECT_LOCK (element);
1729 pads = element->srcpads;
1732 GST_OBJECT_LOCK (element);
1733 pads = element->sinkpads;
1736 goto wrong_direction;
1738 for (; pads; pads = g_list_next (pads)) {
1739 GstPad *pad = GST_PAD_CAST (pads->data);
1741 GST_OBJECT_LOCK (pad);
1742 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "checking pad %s:%s",
1743 GST_DEBUG_PAD_NAME (pad));
1745 if (need_linked && !GST_PAD_IS_LINKED (pad)) {
1746 /* if we require a linked pad, and it is not linked, continue the
1748 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is not linked",
1749 GST_DEBUG_PAD_NAME (pad));
1750 GST_OBJECT_UNLOCK (pad);
1753 /* found a pad, stop search */
1754 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
1755 GST_DEBUG_PAD_NAME (pad));
1756 GST_OBJECT_UNLOCK (pad);
1762 gst_object_ref (result);
1764 GST_OBJECT_UNLOCK (element);
1768 /* ERROR handling */
1771 g_warning ("unknown pad direction %d", dir);
1777 gst_element_default_send_event (GstElement * element, GstEvent * event)
1779 gboolean result = FALSE;
1782 pad = GST_EVENT_IS_DOWNSTREAM (event) ?
1783 gst_element_get_random_pad (element, TRUE, GST_PAD_SINK) :
1784 gst_element_get_random_pad (element, TRUE, GST_PAD_SRC);
1787 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1788 "pushing %s event to random %s pad %s:%s",
1789 GST_EVENT_TYPE_NAME (event),
1790 (GST_PAD_DIRECTION (pad) == GST_PAD_SRC ? "src" : "sink"),
1791 GST_DEBUG_PAD_NAME (pad));
1793 result = gst_pad_send_event (pad, event);
1794 gst_object_unref (pad);
1796 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "can't send %s event on element %s",
1797 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1798 gst_event_unref (event);
1804 * gst_element_send_event:
1805 * @element: a #GstElement to send the event to.
1806 * @event: (transfer full): the #GstEvent to send to the element.
1808 * Sends an event to an element. If the element doesn't implement an
1809 * event handler, the event will be pushed on a random linked sink pad for
1810 * downstream events or a random linked source pad for upstream events.
1812 * This function takes ownership of the provided event so you should
1813 * gst_event_ref() it if you want to reuse the event after this call.
1817 * Returns: %TRUE if the event was handled. Events that trigger a preroll (such
1818 * as flushing seeks and steps) will emit %GST_MESSAGE_ASYNC_DONE.
1821 gst_element_send_event (GstElement * element, GstEvent * event)
1823 GstElementClass *oclass;
1824 gboolean result = FALSE;
1826 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1827 g_return_val_if_fail (event != NULL, FALSE);
1829 oclass = GST_ELEMENT_GET_CLASS (element);
1831 GST_STATE_LOCK (element);
1832 if (oclass->send_event) {
1833 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send %s event on element %s",
1834 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1835 result = oclass->send_event (element, event);
1837 gst_event_unref (event);
1839 GST_STATE_UNLOCK (element);
1846 * @element: a #GstElement to send the event to.
1847 * @rate: The new playback rate
1848 * @format: The format of the seek values
1849 * @flags: The optional seek flags.
1850 * @start_type: The type and flags for the new start position
1851 * @start: The value of the new start position
1852 * @stop_type: The type and flags for the new stop position
1853 * @stop: The value of the new stop position
1855 * Sends a seek event to an element. See gst_event_new_seek() for the details of
1856 * the parameters. The seek event is sent to the element using
1857 * gst_element_send_event().
1861 * Returns: %TRUE if the event was handled. Flushing seeks will trigger a
1862 * preroll, which will emit %GST_MESSAGE_ASYNC_DONE.
1865 gst_element_seek (GstElement * element, gdouble rate, GstFormat format,
1866 GstSeekFlags flags, GstSeekType start_type, gint64 start,
1867 GstSeekType stop_type, gint64 stop)
1872 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1875 gst_event_new_seek (rate, format, flags, start_type, start, stop_type,
1877 result = gst_element_send_event (element, event);
1883 gst_element_default_query (GstElement * element, GstQuery * query)
1885 gboolean result = FALSE;
1888 pad = gst_element_get_random_pad (element, FALSE, GST_PAD_SRC);
1890 result = gst_pad_query (pad, query);
1892 gst_object_unref (pad);
1894 pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1896 GstPad *peer = gst_pad_get_peer (pad);
1899 result = gst_pad_query (peer, query);
1901 gst_object_unref (peer);
1903 gst_object_unref (pad);
1910 * gst_element_query:
1911 * @element: a #GstElement to perform the query on.
1912 * @query: (transfer none): the #GstQuery.
1914 * Performs a query on the given element.
1916 * For elements that don't implement a query handler, this function
1917 * forwards the query to a random srcpad or to the peer of a
1918 * random linked sinkpad of this element.
1920 * Please note that some queries might need a running pipeline to work.
1922 * Returns: %TRUE if the query could be performed.
1927 gst_element_query (GstElement * element, GstQuery * query)
1929 GstElementClass *klass;
1930 gboolean res = FALSE;
1932 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1933 g_return_val_if_fail (query != NULL, FALSE);
1935 GST_TRACER_ELEMENT_QUERY_PRE (element, query);
1937 klass = GST_ELEMENT_GET_CLASS (element);
1939 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send query on element %s",
1940 GST_ELEMENT_NAME (element));
1941 res = klass->query (element, query);
1944 GST_TRACER_ELEMENT_QUERY_POST (element, query, res);
1949 gst_element_post_message_default (GstElement * element, GstMessage * message)
1952 gboolean result = FALSE;
1954 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1955 g_return_val_if_fail (message != NULL, FALSE);
1957 GST_OBJECT_LOCK (element);
1960 if (G_UNLIKELY (bus == NULL))
1963 gst_object_ref (bus);
1964 GST_OBJECT_UNLOCK (element);
1966 /* we release the element lock when posting the message so that any
1967 * (synchronous) message handlers can operate on the element */
1968 result = gst_bus_post (bus, message);
1969 gst_object_unref (bus);
1976 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, element,
1977 "not posting message %p: no bus", message);
1978 GST_OBJECT_UNLOCK (element);
1979 gst_message_unref (message);
1985 * gst_element_post_message:
1986 * @element: a #GstElement posting the message
1987 * @message: (transfer full): a #GstMessage to post
1989 * Post a message on the element's #GstBus. This function takes ownership of the
1990 * message; if you want to access the message after this call, you should add an
1991 * additional reference before calling.
1993 * Returns: %TRUE if the message was successfully posted. The function returns
1994 * %FALSE if the element did not have a bus.
1999 gst_element_post_message (GstElement * element, GstMessage * message)
2001 GstElementClass *klass;
2002 gboolean res = FALSE;
2004 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2005 g_return_val_if_fail (message != NULL, FALSE);
2007 GST_TRACER_ELEMENT_POST_MESSAGE_PRE (element, message);
2009 klass = GST_ELEMENT_GET_CLASS (element);
2010 if (klass->post_message)
2011 res = klass->post_message (element, message);
2013 gst_message_unref (message);
2015 GST_TRACER_ELEMENT_POST_MESSAGE_POST (element, res);
2020 * _gst_element_error_printf:
2021 * @format: (allow-none): the printf-like format to use, or %NULL
2023 * This function is only used internally by the gst_element_error() macro.
2025 * Returns: (transfer full) (nullable): a newly allocated string, or
2026 * %NULL if the format was %NULL or ""
2031 _gst_element_error_printf (const gchar * format, ...)
2042 va_start (args, format);
2044 len = __gst_vasprintf (&buffer, format, args);
2055 * gst_element_message_full_with_details:
2056 * @element: a #GstElement to send message from
2057 * @type: the #GstMessageType
2058 * @domain: the GStreamer GError domain this message belongs to
2059 * @code: the GError code belonging to the domain
2060 * @text: (allow-none) (transfer full): an allocated text string to be used
2061 * as a replacement for the default message connected to code,
2063 * @debug: (allow-none) (transfer full): an allocated debug message to be
2064 * used as a replacement for the default debugging information,
2066 * @file: the source code file where the error was generated
2067 * @function: the source code function where the error was generated
2068 * @line: the source code line where the error was generated
2069 * @structure:(transfer full): optional details structure
2071 * Post an error, warning or info message on the bus from inside an element.
2073 * @type must be of #GST_MESSAGE_ERROR, #GST_MESSAGE_WARNING or
2074 * #GST_MESSAGE_INFO.
2078 void gst_element_message_full_with_details
2079 (GstElement * element, GstMessageType type,
2080 GQuark domain, gint code, gchar * text,
2081 gchar * debug, const gchar * file, const gchar * function, gint line,
2082 GstStructure * structure)
2084 GError *gerror = NULL;
2088 gboolean has_debug = TRUE;
2089 GstMessage *message = NULL;
2092 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, element, "start");
2093 g_return_if_fail (GST_IS_ELEMENT (element));
2094 g_return_if_fail ((type == GST_MESSAGE_ERROR) ||
2095 (type == GST_MESSAGE_WARNING) || (type == GST_MESSAGE_INFO));
2097 /* check if we send the given text or the default error text */
2098 if ((text == NULL) || (text[0] == 0)) {
2099 /* text could have come from g_strdup_printf (""); */
2101 sent_text = gst_error_get_message (domain, code);
2105 /* construct a sent_debug with extra information from source */
2106 if ((debug == NULL) || (debug[0] == 0)) {
2107 /* debug could have come from g_strdup_printf (""); */
2111 name = gst_object_get_path_string (GST_OBJECT_CAST (element));
2113 sent_debug = g_strdup_printf ("%s(%d): %s (): %s:\n%s",
2114 file, line, function, name, debug);
2116 sent_debug = g_strdup_printf ("%s(%d): %s (): %s",
2117 file, line, function, name);
2121 /* create gerror and post message */
2122 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posting message: %s",
2124 gerror = g_error_new_literal (domain, code, sent_text);
2127 case GST_MESSAGE_ERROR:
2129 gst_message_new_error_with_details (GST_OBJECT_CAST (element), gerror,
2130 sent_debug, structure);
2132 case GST_MESSAGE_WARNING:
2134 gst_message_new_warning_with_details (GST_OBJECT_CAST (element),
2135 gerror, sent_debug, structure);
2137 case GST_MESSAGE_INFO:
2139 gst_message_new_info_with_details (GST_OBJECT_CAST (element), gerror,
2140 sent_debug, structure);
2143 g_assert_not_reached ();
2147 gst_element_post_message (element, message);
2149 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posted %s message: %s",
2150 (type == GST_MESSAGE_ERROR ? "error" : "warning"), sent_text);
2153 g_error_free (gerror);
2154 g_free (sent_debug);
2159 * gst_element_message_full:
2160 * @element: a #GstElement to send message from
2161 * @type: the #GstMessageType
2162 * @domain: the GStreamer GError domain this message belongs to
2163 * @code: the GError code belonging to the domain
2164 * @text: (allow-none) (transfer full): an allocated text string to be used
2165 * as a replacement for the default message connected to code,
2167 * @debug: (allow-none) (transfer full): an allocated debug message to be
2168 * used as a replacement for the default debugging information,
2170 * @file: the source code file where the error was generated
2171 * @function: the source code function where the error was generated
2172 * @line: the source code line where the error was generated
2174 * Post an error, warning or info message on the bus from inside an element.
2176 * @type must be of #GST_MESSAGE_ERROR, #GST_MESSAGE_WARNING or
2177 * #GST_MESSAGE_INFO.
2181 void gst_element_message_full
2182 (GstElement * element, GstMessageType type,
2183 GQuark domain, gint code, gchar * text,
2184 gchar * debug, const gchar * file, const gchar * function, gint line)
2186 gst_element_message_full_with_details (element, type, domain, code, text,
2187 debug, file, function, line, NULL);
2191 * gst_element_is_locked_state:
2192 * @element: a #GstElement.
2194 * Checks if the state of an element is locked.
2195 * If the state of an element is locked, state changes of the parent don't
2196 * affect the element.
2197 * This way you can leave currently unused elements inside bins. Just lock their
2198 * state before changing the state from #GST_STATE_NULL.
2202 * Returns: %TRUE, if the element's state is locked.
2205 gst_element_is_locked_state (GstElement * element)
2209 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2211 GST_OBJECT_LOCK (element);
2212 result = GST_ELEMENT_IS_LOCKED_STATE (element);
2213 GST_OBJECT_UNLOCK (element);
2219 * gst_element_set_locked_state:
2220 * @element: a #GstElement
2221 * @locked_state: %TRUE to lock the element's state
2223 * Locks the state of an element, so state changes of the parent don't affect
2224 * this element anymore.
2228 * Returns: %TRUE if the state was changed, %FALSE if bad parameters were given
2229 * or the elements state-locking needed no change.
2232 gst_element_set_locked_state (GstElement * element, gboolean locked_state)
2236 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2238 GST_OBJECT_LOCK (element);
2239 old = GST_ELEMENT_IS_LOCKED_STATE (element);
2241 if (G_UNLIKELY (old == locked_state))
2245 GST_CAT_DEBUG (GST_CAT_STATES, "locking state of element %s",
2246 GST_ELEMENT_NAME (element));
2247 GST_OBJECT_FLAG_SET (element, GST_ELEMENT_FLAG_LOCKED_STATE);
2249 GST_CAT_DEBUG (GST_CAT_STATES, "unlocking state of element %s",
2250 GST_ELEMENT_NAME (element));
2251 GST_OBJECT_FLAG_UNSET (element, GST_ELEMENT_FLAG_LOCKED_STATE);
2253 GST_OBJECT_UNLOCK (element);
2259 GST_CAT_DEBUG (GST_CAT_STATES,
2260 "elements %s was already in locked state %d",
2261 GST_ELEMENT_NAME (element), old);
2262 GST_OBJECT_UNLOCK (element);
2269 * gst_element_sync_state_with_parent:
2270 * @element: a #GstElement.
2272 * Tries to change the state of the element to the same as its parent.
2273 * If this function returns %FALSE, the state of element is undefined.
2275 * Returns: %TRUE, if the element's state could be synced to the parent's state.
2280 gst_element_sync_state_with_parent (GstElement * element)
2284 GstStateChangeReturn ret;
2286 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2288 if ((parent = GST_ELEMENT_CAST (gst_element_get_parent (element)))) {
2289 GstState parent_current, parent_pending;
2291 GST_OBJECT_LOCK (parent);
2292 parent_current = GST_STATE (parent);
2293 parent_pending = GST_STATE_PENDING (parent);
2294 GST_OBJECT_UNLOCK (parent);
2296 /* set to pending if there is one, else we set it to the current state of
2298 if (parent_pending != GST_STATE_VOID_PENDING)
2299 target = parent_pending;
2301 target = parent_current;
2303 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2304 "syncing state (%s) to parent %s %s (%s, %s)",
2305 gst_element_state_get_name (GST_STATE (element)),
2306 GST_ELEMENT_NAME (parent), gst_element_state_get_name (target),
2307 gst_element_state_get_name (parent_current),
2308 gst_element_state_get_name (parent_pending));
2310 ret = gst_element_set_state (element, target);
2311 if (ret == GST_STATE_CHANGE_FAILURE)
2314 gst_object_unref (parent);
2318 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "element has no parent");
2325 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2326 "syncing state failed (%s)",
2327 gst_element_state_change_return_get_name (ret));
2328 gst_object_unref (parent);
2334 static GstStateChangeReturn
2335 gst_element_get_state_func (GstElement * element,
2336 GstState * state, GstState * pending, GstClockTime timeout)
2338 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
2339 GstState old_pending;
2341 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "getting state, timeout %"
2342 GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
2344 GST_OBJECT_LOCK (element);
2345 ret = GST_STATE_RETURN (element);
2346 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "RETURN is %s",
2347 gst_element_state_change_return_get_name (ret));
2349 /* we got an error, report immediately */
2350 if (ret == GST_STATE_CHANGE_FAILURE)
2353 /* we got no_preroll, report immediately */
2354 if (ret == GST_STATE_CHANGE_NO_PREROLL)
2357 /* no need to wait async if we are not async */
2358 if (ret != GST_STATE_CHANGE_ASYNC)
2361 old_pending = GST_STATE_PENDING (element);
2362 if (old_pending != GST_STATE_VOID_PENDING) {
2366 /* get cookie to detect state changes during waiting */
2367 cookie = element->state_cookie;
2369 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2370 "waiting for element to commit state");
2372 /* we have a pending state change, wait for it to complete */
2373 if (timeout != GST_CLOCK_TIME_NONE) {
2375 /* make timeout absolute */
2376 end_time = g_get_monotonic_time () + (timeout / 1000);
2377 signaled = GST_STATE_WAIT_UNTIL (element, end_time);
2379 GST_STATE_WAIT (element);
2384 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timed out");
2385 /* timeout triggered */
2386 ret = GST_STATE_CHANGE_ASYNC;
2388 if (cookie != element->state_cookie)
2391 /* could be success or failure */
2392 if (old_pending == GST_STATE (element)) {
2393 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got success");
2394 ret = GST_STATE_CHANGE_SUCCESS;
2396 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got failure");
2397 ret = GST_STATE_CHANGE_FAILURE;
2400 /* if nothing is pending anymore we can return SUCCESS */
2401 if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
2402 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "nothing pending");
2403 ret = GST_STATE_CHANGE_SUCCESS;
2409 *state = GST_STATE (element);
2411 *pending = GST_STATE_PENDING (element);
2413 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2414 "state current: %s, pending: %s, result: %s",
2415 gst_element_state_get_name (GST_STATE (element)),
2416 gst_element_state_get_name (GST_STATE_PENDING (element)),
2417 gst_element_state_change_return_get_name (ret));
2418 GST_OBJECT_UNLOCK (element);
2425 *state = GST_STATE_VOID_PENDING;
2427 *pending = GST_STATE_VOID_PENDING;
2429 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "interruped");
2431 GST_OBJECT_UNLOCK (element);
2433 return GST_STATE_CHANGE_FAILURE;
2438 * gst_element_get_state:
2439 * @element: a #GstElement to get the state of.
2440 * @state: (out) (allow-none): a pointer to #GstState to hold the state.
2442 * @pending: (out) (allow-none): a pointer to #GstState to hold the pending
2443 * state. Can be %NULL.
2444 * @timeout: a #GstClockTime to specify the timeout for an async
2445 * state change or %GST_CLOCK_TIME_NONE for infinite timeout.
2447 * Gets the state of the element.
2449 * For elements that performed an ASYNC state change, as reported by
2450 * gst_element_set_state(), this function will block up to the
2451 * specified timeout value for the state change to complete.
2452 * If the element completes the state change or goes into
2453 * an error, this function returns immediately with a return value of
2454 * %GST_STATE_CHANGE_SUCCESS or %GST_STATE_CHANGE_FAILURE respectively.
2456 * For elements that did not return %GST_STATE_CHANGE_ASYNC, this function
2457 * returns the current and pending state immediately.
2459 * This function returns %GST_STATE_CHANGE_NO_PREROLL if the element
2460 * successfully changed its state but is not able to provide data yet.
2461 * This mostly happens for live sources that only produce data in
2462 * %GST_STATE_PLAYING. While the state change return is equivalent to
2463 * %GST_STATE_CHANGE_SUCCESS, it is returned to the application to signal that
2464 * some sink elements might not be able to complete their state change because
2465 * an element is not producing data to complete the preroll. When setting the
2466 * element to playing, the preroll will complete and playback will start.
2468 * Returns: %GST_STATE_CHANGE_SUCCESS if the element has no more pending state
2469 * and the last state change succeeded, %GST_STATE_CHANGE_ASYNC if the
2470 * element is still performing a state change or
2471 * %GST_STATE_CHANGE_FAILURE if the last state change failed.
2475 GstStateChangeReturn
2476 gst_element_get_state (GstElement * element,
2477 GstState * state, GstState * pending, GstClockTime timeout)
2479 GstElementClass *oclass;
2480 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2482 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2484 oclass = GST_ELEMENT_GET_CLASS (element);
2486 if (oclass->get_state)
2487 result = (oclass->get_state) (element, state, pending, timeout);
2493 * gst_element_abort_state:
2494 * @element: a #GstElement to abort the state of.
2496 * Abort the state change of the element. This function is used
2497 * by elements that do asynchronous state changes and find out
2498 * something is wrong.
2500 * This function should be called with the STATE_LOCK held.
2505 gst_element_abort_state (GstElement * element)
2509 #ifndef GST_DISABLE_GST_DEBUG
2513 g_return_if_fail (GST_IS_ELEMENT (element));
2515 GST_OBJECT_LOCK (element);
2516 pending = GST_STATE_PENDING (element);
2518 if (pending == GST_STATE_VOID_PENDING ||
2519 GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2520 goto nothing_aborted;
2522 #ifndef GST_DISABLE_GST_DEBUG
2523 old_state = GST_STATE (element);
2525 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2526 "aborting state from %s to %s", gst_element_state_get_name (old_state),
2527 gst_element_state_get_name (pending));
2531 GST_STATE_RETURN (element) = GST_STATE_CHANGE_FAILURE;
2533 GST_STATE_BROADCAST (element);
2534 GST_OBJECT_UNLOCK (element);
2540 GST_OBJECT_UNLOCK (element);
2545 /* Not static because GstBin has manual state handling too */
2547 _priv_gst_element_state_changed (GstElement * element, GstState oldstate,
2548 GstState newstate, GstState pending)
2550 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
2551 GstMessage *message;
2553 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2554 "notifying about state-changed %s to %s (%s pending)",
2555 gst_element_state_get_name (oldstate),
2556 gst_element_state_get_name (newstate),
2557 gst_element_state_get_name (pending));
2559 if (klass->state_changed)
2560 klass->state_changed (element, oldstate, newstate, pending);
2562 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2563 oldstate, newstate, pending);
2564 gst_element_post_message (element, message);
2568 * gst_element_continue_state:
2569 * @element: a #GstElement to continue the state change of.
2570 * @ret: The previous state return value
2572 * Commit the state change of the element and proceed to the next
2573 * pending state if any. This function is used
2574 * by elements that do asynchronous state changes.
2575 * The core will normally call this method automatically when an
2576 * element returned %GST_STATE_CHANGE_SUCCESS from the state change function.
2578 * If after calling this method the element still has not reached
2579 * the pending state, the next state change is performed.
2581 * This method is used internally and should normally not be called by plugins
2584 * This function must be called with STATE_LOCK held.
2586 * Returns: The result of the commit state change.
2590 GstStateChangeReturn
2591 gst_element_continue_state (GstElement * element, GstStateChangeReturn ret)
2593 GstStateChangeReturn old_ret;
2594 GstState old_state, old_next;
2595 GstState current, next, pending;
2596 GstStateChange transition;
2598 GST_OBJECT_LOCK (element);
2599 old_ret = GST_STATE_RETURN (element);
2600 GST_STATE_RETURN (element) = ret;
2601 pending = GST_STATE_PENDING (element);
2603 /* check if there is something to commit */
2604 if (pending == GST_STATE_VOID_PENDING)
2605 goto nothing_pending;
2607 old_state = GST_STATE (element);
2608 /* this is the state we should go to next */
2609 old_next = GST_STATE_NEXT (element);
2610 /* update current state */
2611 current = GST_STATE (element) = old_next;
2613 /* see if we reached the final state */
2614 if (pending == current)
2617 next = GST_STATE_GET_NEXT (current, pending);
2618 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2620 GST_STATE_NEXT (element) = next;
2622 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2623 GST_OBJECT_UNLOCK (element);
2625 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2626 "committing state from %s to %s, pending %s, next %s",
2627 gst_element_state_get_name (old_state),
2628 gst_element_state_get_name (old_next),
2629 gst_element_state_get_name (pending), gst_element_state_get_name (next));
2631 _priv_gst_element_state_changed (element, old_state, old_next, pending);
2633 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2634 "continue state change %s to %s, final %s",
2635 gst_element_state_get_name (current),
2636 gst_element_state_get_name (next), gst_element_state_get_name (pending));
2638 ret = gst_element_change_state (element, transition);
2644 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "nothing pending");
2645 GST_OBJECT_UNLOCK (element);
2650 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2651 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2653 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2654 "completed state change to %s", gst_element_state_get_name (pending));
2655 GST_OBJECT_UNLOCK (element);
2657 /* don't post silly messages with the same state. This can happen
2658 * when an element state is changed to what it already was. For bins
2659 * this can be the result of a lost state, which we check with the
2660 * previous return value.
2661 * We do signal the cond though as a _get_state() might be blocking
2663 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC)
2664 _priv_gst_element_state_changed (element, old_state, old_next,
2665 GST_STATE_VOID_PENDING);
2667 GST_STATE_BROADCAST (element);
2674 * gst_element_lost_state:
2675 * @element: a #GstElement the state is lost of
2677 * Brings the element to the lost state. The current state of the
2678 * element is copied to the pending state so that any call to
2679 * gst_element_get_state() will return %GST_STATE_CHANGE_ASYNC.
2681 * An ASYNC_START message is posted. If the element was PLAYING, it will
2682 * go to PAUSED. The element will be restored to its PLAYING state by
2683 * the parent pipeline when it prerolls again.
2685 * This is mostly used for elements that lost their preroll buffer
2686 * in the %GST_STATE_PAUSED or %GST_STATE_PLAYING state after a flush,
2687 * they will go to their pending state again when a new preroll buffer is
2688 * queued. This function can only be called when the element is currently
2689 * not in error or an async state change.
2691 * This function is used internally and should normally not be called from
2692 * plugins or applications.
2695 gst_element_lost_state (GstElement * element)
2697 GstState old_state, new_state;
2698 GstMessage *message;
2700 g_return_if_fail (GST_IS_ELEMENT (element));
2702 GST_OBJECT_LOCK (element);
2703 if (GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2706 if (GST_STATE_PENDING (element) != GST_STATE_VOID_PENDING)
2707 goto only_async_start;
2709 old_state = GST_STATE (element);
2711 /* when we were PLAYING, the new state is PAUSED. We will also not
2712 * automatically go to PLAYING but let the parent bin(s) set us to PLAYING
2713 * when we preroll. */
2714 if (old_state > GST_STATE_PAUSED)
2715 new_state = GST_STATE_PAUSED;
2717 new_state = old_state;
2719 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2720 "lost state of %s to %s", gst_element_state_get_name (old_state),
2721 gst_element_state_get_name (new_state));
2723 GST_STATE (element) = new_state;
2724 GST_STATE_NEXT (element) = new_state;
2725 GST_STATE_PENDING (element) = new_state;
2726 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2727 GST_OBJECT_UNLOCK (element);
2729 _priv_gst_element_state_changed (element, new_state, new_state, new_state);
2731 message = gst_message_new_async_start (GST_OBJECT_CAST (element));
2732 gst_element_post_message (element, message);
2738 GST_OBJECT_UNLOCK (element);
2743 GST_OBJECT_UNLOCK (element);
2745 message = gst_message_new_async_start (GST_OBJECT_CAST (element));
2746 gst_element_post_message (element, message);
2752 * gst_element_set_state:
2753 * @element: a #GstElement to change state of.
2754 * @state: the element's new #GstState.
2756 * Sets the state of the element. This function will try to set the
2757 * requested state by going through all the intermediary states and calling
2758 * the class's state change function for each.
2760 * This function can return #GST_STATE_CHANGE_ASYNC, in which case the
2761 * element will perform the remainder of the state change asynchronously in
2763 * An application can use gst_element_get_state() to wait for the completion
2764 * of the state change or it can wait for a %GST_MESSAGE_ASYNC_DONE or
2765 * %GST_MESSAGE_STATE_CHANGED on the bus.
2767 * State changes to %GST_STATE_READY or %GST_STATE_NULL never return
2768 * #GST_STATE_CHANGE_ASYNC.
2770 * Returns: Result of the state change using #GstStateChangeReturn.
2774 GstStateChangeReturn
2775 gst_element_set_state (GstElement * element, GstState state)
2777 GstElementClass *oclass;
2778 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2780 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2782 oclass = GST_ELEMENT_GET_CLASS (element);
2784 if (oclass->set_state)
2785 result = (oclass->set_state) (element, state);
2791 * default set state function, calculates the next state based
2792 * on current state and calls the change_state function
2794 static GstStateChangeReturn
2795 gst_element_set_state_func (GstElement * element, GstState state)
2797 GstState current, next, old_pending;
2798 GstStateChangeReturn ret;
2799 GstStateChange transition;
2800 GstStateChangeReturn old_ret;
2802 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2804 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "set_state to %s",
2805 gst_element_state_get_name (state));
2807 /* state lock is taken to protect the set_state() and get_state()
2808 * procedures, it does not lock any variables. */
2809 GST_STATE_LOCK (element);
2811 /* now calculate how to get to the new state */
2812 GST_OBJECT_LOCK (element);
2813 old_ret = GST_STATE_RETURN (element);
2814 /* previous state change returned an error, remove all pending
2815 * and next states */
2816 if (old_ret == GST_STATE_CHANGE_FAILURE) {
2817 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2818 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2819 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
2822 current = GST_STATE (element);
2823 next = GST_STATE_NEXT (element);
2824 old_pending = GST_STATE_PENDING (element);
2826 /* this is the (new) state we should go to. TARGET is the last state we set on
2828 if (state != GST_STATE_TARGET (element)) {
2829 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2830 "setting target state to %s", gst_element_state_get_name (state));
2831 GST_STATE_TARGET (element) = state;
2832 /* increment state cookie so that we can track each state change. We only do
2833 * this if this is actually a new state change. */
2834 element->state_cookie++;
2836 GST_STATE_PENDING (element) = state;
2838 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2839 "current %s, old_pending %s, next %s, old return %s",
2840 gst_element_state_get_name (current),
2841 gst_element_state_get_name (old_pending),
2842 gst_element_state_get_name (next),
2843 gst_element_state_change_return_get_name (old_ret));
2845 /* if the element was busy doing a state change, we just update the
2846 * target state, it'll get to it async then. */
2847 if (old_pending != GST_STATE_VOID_PENDING) {
2848 /* upwards state change will happen ASYNC */
2849 if (old_pending <= state)
2851 /* element is going to this state already */
2852 else if (next == state)
2854 /* element was performing an ASYNC upward state change and
2855 * we request to go downward again. Start from the next pending
2857 else if (next > state
2858 && GST_STATE_RETURN (element) == GST_STATE_CHANGE_ASYNC) {
2862 next = GST_STATE_GET_NEXT (current, state);
2863 /* now we store the next state */
2864 GST_STATE_NEXT (element) = next;
2865 /* mark busy, we need to check that there is actually a state change
2866 * to be done else we could accidentally override SUCCESS/NO_PREROLL and
2867 * the default element change_state function has no way to know what the
2868 * old value was... could consider this a FIXME...*/
2869 if (current != next)
2870 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2872 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2874 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2875 "%s: setting state from %s to %s",
2876 (next != state ? "intermediate" : "final"),
2877 gst_element_state_get_name (current), gst_element_state_get_name (next));
2879 /* now signal any waiters, they will error since the cookie was incremented */
2880 GST_STATE_BROADCAST (element);
2882 GST_OBJECT_UNLOCK (element);
2884 ret = gst_element_change_state (element, transition);
2886 GST_STATE_UNLOCK (element);
2888 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "returned %s",
2889 gst_element_state_change_return_get_name (ret));
2895 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2896 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2897 "element was busy with async state change");
2898 GST_OBJECT_UNLOCK (element);
2900 GST_STATE_UNLOCK (element);
2902 return GST_STATE_CHANGE_ASYNC;
2907 * gst_element_change_state:
2908 * @element: a #GstElement
2909 * @transition: the requested transition
2911 * Perform @transition on @element.
2913 * This function must be called with STATE_LOCK held and is mainly used
2916 * Returns: the #GstStateChangeReturn of the state transition.
2918 GstStateChangeReturn
2919 gst_element_change_state (GstElement * element, GstStateChange transition)
2921 GstElementClass *oclass;
2922 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2924 oclass = GST_ELEMENT_GET_CLASS (element);
2926 GST_TRACER_ELEMENT_CHANGE_STATE_PRE (element, transition);
2928 /* call the state change function so it can set the state */
2929 if (oclass->change_state)
2930 ret = (oclass->change_state) (element, transition);
2932 ret = GST_STATE_CHANGE_FAILURE;
2934 GST_TRACER_ELEMENT_CHANGE_STATE_POST (element, transition, ret);
2937 case GST_STATE_CHANGE_FAILURE:
2938 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2939 "have FAILURE change_state return");
2940 /* state change failure */
2941 gst_element_abort_state (element);
2943 case GST_STATE_CHANGE_ASYNC:
2947 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2948 "element will change state ASYNC");
2950 target = GST_STATE_TARGET (element);
2952 if (target > GST_STATE_READY)
2955 /* else we just continue the state change downwards */
2956 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2957 "forcing commit state %s <= %s",
2958 gst_element_state_get_name (target),
2959 gst_element_state_get_name (GST_STATE_READY));
2961 ret = gst_element_continue_state (element, GST_STATE_CHANGE_SUCCESS);
2964 case GST_STATE_CHANGE_SUCCESS:
2965 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2966 "element changed state SUCCESS");
2967 /* we can commit the state now which will proceeed to
2969 ret = gst_element_continue_state (element, ret);
2971 case GST_STATE_CHANGE_NO_PREROLL:
2972 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2973 "element changed state NO_PREROLL");
2974 /* we can commit the state now which will proceeed to
2976 ret = gst_element_continue_state (element, ret);
2979 goto invalid_return;
2982 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit state change %d", ret);
2987 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit async state change %d",
2995 GST_OBJECT_LOCK (element);
2996 /* somebody added a GST_STATE_ and forgot to do stuff here ! */
2997 g_critical ("%s: unknown return value %d from a state change function",
2998 GST_ELEMENT_NAME (element), ret);
3000 /* we are in error now */
3001 ret = GST_STATE_CHANGE_FAILURE;
3002 GST_STATE_RETURN (element) = ret;
3003 GST_OBJECT_UNLOCK (element);
3009 /* gst_iterator_fold functions for pads_activate
3010 * Stop the iterator if activating one pad failed, but only if that pad
3011 * has not been removed from the element. */
3013 activate_pads (const GValue * vpad, GValue * ret, gboolean * active)
3015 GstPad *pad = g_value_get_object (vpad);
3016 gboolean cont = TRUE;
3018 if (!gst_pad_set_active (pad, *active)) {
3019 if (GST_PAD_PARENT (pad) != NULL) {
3021 g_value_set_boolean (ret, FALSE);
3028 /* returns false on error or early cutout of the fold, true if all
3029 * pads in @iter were (de)activated successfully. */
3031 iterator_activate_fold_with_resync (GstIterator * iter,
3032 GstIteratorFoldFunction func, gpointer user_data)
3034 GstIteratorResult ires;
3037 /* no need to unset this later, it's just a boolean */
3038 g_value_init (&ret, G_TYPE_BOOLEAN);
3039 g_value_set_boolean (&ret, TRUE);
3042 ires = gst_iterator_fold (iter, func, &ret, user_data);
3044 case GST_ITERATOR_RESYNC:
3045 /* need to reset the result again */
3046 g_value_set_boolean (&ret, TRUE);
3047 gst_iterator_resync (iter);
3049 case GST_ITERATOR_DONE:
3050 /* all pads iterated, return collected value */
3053 /* iterator returned _ERROR or premature end with _OK,
3054 * mark an error and exit */
3055 g_value_set_boolean (&ret, FALSE);
3060 /* return collected value */
3061 return g_value_get_boolean (&ret);
3064 /* is called with STATE_LOCK
3066 * Pads are activated from source pads to sinkpads.
3069 gst_element_pads_activate (GstElement * element, gboolean active)
3074 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3075 "%s pads", active ? "activate" : "deactivate");
3077 iter = gst_element_iterate_src_pads (element);
3079 iterator_activate_fold_with_resync (iter,
3080 (GstIteratorFoldFunction) activate_pads, &active);
3081 gst_iterator_free (iter);
3082 if (G_UNLIKELY (!res))
3085 iter = gst_element_iterate_sink_pads (element);
3087 iterator_activate_fold_with_resync (iter,
3088 (GstIteratorFoldFunction) activate_pads, &active);
3089 gst_iterator_free (iter);
3090 if (G_UNLIKELY (!res))
3093 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3094 "pad %sactivation successful", active ? "" : "de");
3101 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3102 "pad %sactivation failed", active ? "" : "de");
3107 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3108 "sink pads_activate failed");
3113 /* is called with STATE_LOCK */
3114 static GstStateChangeReturn
3115 gst_element_change_state_func (GstElement * element, GstStateChange transition)
3117 GstState state, next;
3118 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
3120 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
3122 state = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
3123 next = GST_STATE_TRANSITION_NEXT (transition);
3125 /* if the element already is in the given state, we just return success */
3126 if (next == GST_STATE_VOID_PENDING || state == next)
3129 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element,
3130 "default handler tries setting state from %s to %s (%04x)",
3131 gst_element_state_get_name (state),
3132 gst_element_state_get_name (next), transition);
3134 switch (transition) {
3135 case GST_STATE_CHANGE_NULL_TO_READY:
3137 case GST_STATE_CHANGE_READY_TO_PAUSED:
3138 if (!gst_element_pads_activate (element, TRUE)) {
3139 result = GST_STATE_CHANGE_FAILURE;
3142 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3144 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3146 case GST_STATE_CHANGE_PAUSED_TO_READY:
3147 case GST_STATE_CHANGE_READY_TO_NULL:{
3150 /* deactivate pads in both cases, since they are activated on
3151 ready->paused but the element might not have made it to paused */
3152 if (!gst_element_pads_activate (element, FALSE)) {
3153 result = GST_STATE_CHANGE_FAILURE;
3156 /* Remove all non-persistent contexts */
3157 GST_OBJECT_LOCK (element);
3158 for (l = element->contexts; l;) {
3159 GstContext *context = l->data;
3161 if (!gst_context_is_persistent (context)) {
3164 gst_context_unref (context);
3166 element->contexts = g_list_delete_link (element->contexts, l);
3172 GST_OBJECT_UNLOCK (element);
3176 /* this will catch real but unhandled state changes;
3177 * can only be caused by:
3178 * - a new state was added
3179 * - somehow the element was asked to jump across an intermediate state
3181 g_warning ("Unhandled state change from %s to %s",
3182 gst_element_state_get_name (state),
3183 gst_element_state_get_name (next));
3190 GST_OBJECT_LOCK (element);
3191 result = GST_STATE_RETURN (element);
3192 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3193 "element is already in the %s state",
3194 gst_element_state_get_name (state));
3195 GST_OBJECT_UNLOCK (element);
3202 * gst_element_get_factory:
3203 * @element: a #GstElement to request the element factory of.
3205 * Retrieves the factory that was used to create this element.
3207 * Returns: (transfer none): the #GstElementFactory used for creating this
3208 * element. no refcounting is needed.
3211 gst_element_get_factory (GstElement * element)
3213 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3215 return GST_ELEMENT_GET_CLASS (element)->elementfactory;
3219 gst_element_dispose (GObject * object)
3221 GstElement *element = GST_ELEMENT_CAST (object);
3224 GstElementClass *oclass;
3227 oclass = GST_ELEMENT_GET_CLASS (element);
3229 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p dispose", element);
3231 if (GST_STATE (element) != GST_STATE_NULL)
3234 /* start by releasing all request pads, this might also remove some dynamic
3236 walk = element->pads;
3238 GstPad *pad = GST_PAD_CAST (walk->data);
3242 if (oclass->release_pad && GST_PAD_PAD_TEMPLATE (pad) &&
3243 GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (pad))
3244 == GST_PAD_REQUEST) {
3245 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3246 "removing request pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3247 oclass->release_pad (element, pad);
3249 /* in case the release_pad function removed the next pad too */
3250 if (walk && g_list_position (element->pads, walk) == -1)
3251 walk = element->pads;
3254 /* remove the remaining pads */
3255 while (element->pads) {
3256 GstPad *pad = GST_PAD_CAST (element->pads->data);
3257 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3258 "removing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3259 if (!gst_element_remove_pad (element, pad)) {
3260 /* only happens when someone unparented our pad.. */
3261 g_critical ("failed to remove pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3266 GST_OBJECT_LOCK (element);
3267 clock_p = &element->clock;
3268 bus_p = &element->bus;
3269 gst_object_replace ((GstObject **) clock_p, NULL);
3270 gst_object_replace ((GstObject **) bus_p, NULL);
3271 g_list_free_full (element->contexts, (GDestroyNotify) gst_context_unref);
3272 GST_OBJECT_UNLOCK (element);
3274 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p parent class dispose",
3277 G_OBJECT_CLASS (parent_class)->dispose (object);
3286 is_locked = GST_ELEMENT_IS_LOCKED_STATE (element);
3288 ("\nTrying to dispose element %s, but it is in %s%s instead of the NULL"
3290 "You need to explicitly set elements to the NULL state before\n"
3291 "dropping the final reference, to allow them to clean up.\n"
3292 "This problem may also be caused by a refcounting bug in the\n"
3293 "application or some element.\n",
3294 GST_OBJECT_NAME (element),
3295 gst_element_state_get_name (GST_STATE (element)),
3296 is_locked ? " (locked)" : "");
3302 gst_element_finalize (GObject * object)
3304 GstElement *element = GST_ELEMENT_CAST (object);
3306 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p finalize", element);
3308 g_cond_clear (&element->state_cond);
3309 g_rec_mutex_clear (&element->state_lock);
3311 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p finalize parent",
3314 G_OBJECT_CLASS (parent_class)->finalize (object);
3318 gst_element_set_bus_func (GstElement * element, GstBus * bus)
3322 g_return_if_fail (GST_IS_ELEMENT (element));
3324 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element, "setting bus to %p", bus);
3326 GST_OBJECT_LOCK (element);
3327 bus_p = &GST_ELEMENT_BUS (element);
3328 gst_object_replace ((GstObject **) bus_p, GST_OBJECT_CAST (bus));
3329 GST_OBJECT_UNLOCK (element);
3333 * gst_element_set_bus:
3334 * @element: a #GstElement to set the bus of.
3335 * @bus: (transfer none): the #GstBus to set.
3337 * Sets the bus of the element. Increases the refcount on the bus.
3338 * For internal use only, unless you're testing elements.
3343 gst_element_set_bus (GstElement * element, GstBus * bus)
3345 GstElementClass *oclass;
3347 g_return_if_fail (GST_IS_ELEMENT (element));
3349 oclass = GST_ELEMENT_GET_CLASS (element);
3351 if (oclass->set_bus)
3352 oclass->set_bus (element, bus);
3356 * gst_element_get_bus:
3357 * @element: a #GstElement to get the bus of.
3359 * Returns the bus of the element. Note that only a #GstPipeline will provide a
3360 * bus for the application.
3362 * Returns: (transfer full): the element's #GstBus. unref after usage.
3367 gst_element_get_bus (GstElement * element)
3369 GstBus *result = NULL;
3371 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
3373 GST_OBJECT_LOCK (element);
3374 if ((result = GST_ELEMENT_BUS (element)))
3375 gst_object_ref (result);
3376 GST_OBJECT_UNLOCK (element);
3378 GST_CAT_DEBUG_OBJECT (GST_CAT_BUS, element, "got bus %" GST_PTR_FORMAT,
3385 gst_element_set_context_default (GstElement * element, GstContext * context)
3387 const gchar *context_type;
3390 g_return_if_fail (GST_IS_CONTEXT (context));
3391 context_type = gst_context_get_context_type (context);
3392 g_return_if_fail (context_type != NULL);
3394 GST_OBJECT_LOCK (element);
3395 for (l = element->contexts; l; l = l->next) {
3396 GstContext *tmp = l->data;
3397 const gchar *tmp_type = gst_context_get_context_type (tmp);
3399 /* Always store newest context but never replace
3400 * a persistent one by a non-persistent one */
3401 if (g_strcmp0 (context_type, tmp_type) == 0 &&
3402 (gst_context_is_persistent (context) ||
3403 !gst_context_is_persistent (tmp))) {
3404 gst_context_replace ((GstContext **) & l->data, context);
3408 /* Not found? Add */
3411 g_list_prepend (element->contexts, gst_context_ref (context));
3413 GST_OBJECT_UNLOCK (element);
3417 * gst_element_set_context:
3418 * @element: a #GstElement to set the context of.
3419 * @context: (transfer none): the #GstContext to set.
3421 * Sets the context of the element. Increases the refcount of the context.
3426 gst_element_set_context (GstElement * element, GstContext * context)
3428 GstElementClass *oclass;
3430 g_return_if_fail (GST_IS_ELEMENT (element));
3431 g_return_if_fail (GST_IS_CONTEXT (context));
3433 oclass = GST_ELEMENT_GET_CLASS (element);
3435 GST_CAT_DEBUG_OBJECT (GST_CAT_CONTEXT, element,
3436 "set context %p %" GST_PTR_FORMAT, context,
3437 gst_context_get_structure (context));
3439 if (oclass->set_context)
3440 oclass->set_context (element, context);
3444 * gst_element_get_contexts:
3445 * @element: a #GstElement to set the context of.
3447 * Gets the contexts set on the element.
3451 * Returns: (element-type Gst.Context) (transfer full): List of #GstContext
3456 gst_element_get_contexts (GstElement * element)
3460 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3462 GST_OBJECT_LOCK (element);
3463 ret = g_list_copy_deep (element->contexts, (GCopyFunc) gst_context_ref, NULL);
3464 GST_OBJECT_UNLOCK (element);
3470 _match_context_type (GstContext * c1, const gchar * context_type)
3472 const gchar *c1_type;
3474 c1_type = gst_context_get_context_type (c1);
3476 return g_strcmp0 (c1_type, context_type);
3480 * gst_element_get_context_unlocked:
3481 * @element: a #GstElement to get the context of.
3482 * @context_type: a name of a context to retrieve
3484 * Gets the context with @context_type set on the element or NULL.
3486 * Returns: (transfer full): A #GstContext or NULL
3491 gst_element_get_context_unlocked (GstElement * element,
3492 const gchar * context_type)
3494 GstContext *ret = NULL;
3497 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3500 g_list_find_custom (element->contexts, context_type,
3501 (GCompareFunc) _match_context_type);
3502 if (node && node->data)
3503 ret = gst_context_ref (node->data);
3509 * gst_element_get_context:
3510 * @element: a #GstElement to get the context of.
3511 * @context_type: a name of a context to retrieve
3513 * Gets the context with @context_type set on the element or NULL.
3517 * Returns: (transfer full): A #GstContext or NULL
3522 gst_element_get_context (GstElement * element, const gchar * context_type)
3524 GstContext *ret = NULL;
3526 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3528 GST_OBJECT_LOCK (element);
3529 ret = gst_element_get_context_unlocked (element, context_type);
3530 GST_OBJECT_UNLOCK (element);
3536 gst_element_property_post_notify_msg (GstElement * element, GObject * obj,
3537 GParamSpec * pspec, gboolean include_value)
3539 GValue val = G_VALUE_INIT;
3542 GST_LOG_OBJECT (element, "property '%s' of object %" GST_PTR_FORMAT " has "
3543 "changed, posting message with%s value", pspec->name, obj,
3544 include_value ? "" : "out");
3546 if (include_value && (pspec->flags & G_PARAM_READABLE) != 0) {
3547 g_value_init (&val, pspec->value_type);
3548 g_object_get_property (obj, pspec->name, &val);
3553 gst_element_post_message (element,
3554 gst_message_new_property_notify (GST_OBJECT_CAST (obj), pspec->name, v));
3558 gst_element_property_deep_notify_cb (GstElement * element, GObject * prop_obj,
3559 GParamSpec * pspec, gpointer user_data)
3561 gboolean include_value = GPOINTER_TO_INT (user_data);
3563 gst_element_property_post_notify_msg (element, prop_obj, pspec,
3568 gst_element_property_notify_cb (GObject * obj, GParamSpec * pspec,
3571 gboolean include_value = GPOINTER_TO_INT (user_data);
3573 gst_element_property_post_notify_msg (GST_ELEMENT_CAST (obj), obj, pspec,
3578 * gst_element_add_property_notify_watch:
3579 * @element: a #GstElement to watch for property changes
3580 * @property_name: (allow-none): name of property to watch for changes, or
3581 * NULL to watch all properties
3582 * @include_value: whether to include the new property value in the message
3584 * Returns: a watch id, which can be used in connection with
3585 * gst_element_remove_property_notify_watch() to remove the watch again.
3590 gst_element_add_property_notify_watch (GstElement * element,
3591 const gchar * property_name, gboolean include_value)
3597 g_return_val_if_fail (GST_IS_ELEMENT (element), 0);
3599 sep = (property_name != NULL) ? "::" : NULL;
3600 signal_name = g_strconcat ("notify", sep, property_name, NULL);
3601 id = g_signal_connect (element, signal_name,
3602 G_CALLBACK (gst_element_property_notify_cb),
3603 GINT_TO_POINTER (include_value));
3604 g_free (signal_name);
3610 * gst_element_add_property_deep_notify_watch:
3611 * @element: a #GstElement to watch (recursively) for property changes
3612 * @property_name: (allow-none): name of property to watch for changes, or
3613 * NULL to watch all properties
3614 * @include_value: whether to include the new property value in the message
3616 * Returns: a watch id, which can be used in connection with
3617 * gst_element_remove_property_notify_watch() to remove the watch again.
3622 gst_element_add_property_deep_notify_watch (GstElement * element,
3623 const gchar * property_name, gboolean include_value)
3629 g_return_val_if_fail (GST_IS_ELEMENT (element), 0);
3631 sep = (property_name != NULL) ? "::" : NULL;
3632 signal_name = g_strconcat ("deep-notify", sep, property_name, NULL);
3633 id = g_signal_connect (element, signal_name,
3634 G_CALLBACK (gst_element_property_deep_notify_cb),
3635 GINT_TO_POINTER (include_value));
3636 g_free (signal_name);
3642 * gst_element_remove_property_notify_watch:
3643 * @element: a #GstElement being watched for property changes
3644 * @watch_id: watch id to remove
3649 gst_element_remove_property_notify_watch (GstElement * element, gulong watch_id)
3651 g_signal_handler_disconnect (element, watch_id);
3656 GstElement *element;
3657 GstElementCallAsyncFunc func;
3659 GDestroyNotify destroy_notify;
3660 } GstElementCallAsyncData;
3663 gst_element_call_async_func (gpointer data, gpointer user_data)
3665 GstElementCallAsyncData *async_data = data;
3667 async_data->func (async_data->element, async_data->user_data);
3668 if (async_data->destroy_notify)
3669 async_data->destroy_notify (async_data->user_data);
3670 gst_object_unref (async_data->element);
3671 g_free (async_data);
3675 * gst_element_call_async:
3676 * @element: a #GstElement
3677 * @func: Function to call asynchronously from another thread
3678 * @user_data: Data to pass to @func
3679 * @destroy_notify: GDestroyNotify for @user_data
3681 * Calls @func from another thread and passes @user_data to it. This is to be
3682 * used for cases when a state change has to be performed from a streaming
3683 * thread, directly via gst_element_set_state() or indirectly e.g. via SEEK
3686 * Calling those functions directly from the streaming thread will cause
3687 * deadlocks in many situations, as they might involve waiting for the
3688 * streaming thread to shut down from this very streaming thread.
3695 gst_element_call_async (GstElement * element, GstElementCallAsyncFunc func,
3696 gpointer user_data, GDestroyNotify destroy_notify)
3698 GstElementCallAsyncData *async_data;
3700 g_return_if_fail (GST_IS_ELEMENT (element));
3702 async_data = g_new0 (GstElementCallAsyncData, 1);
3703 async_data->element = gst_object_ref (element);
3704 async_data->func = func;
3705 async_data->user_data = user_data;
3706 async_data->destroy_notify = destroy_notify;
3708 g_thread_pool_push (gst_element_pool, async_data, NULL);
3712 _priv_gst_element_cleanup (void)
3714 if (gst_element_pool) {
3715 g_thread_pool_free (gst_element_pool, FALSE, TRUE);
3716 gst_element_setup_thread_pool ();
3721 gst_make_element_message_details (const char *name, ...)
3723 GstStructure *structure;
3729 va_start (varargs, name);
3730 structure = gst_structure_new_valist ("details", name, varargs);