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 * - GST_ELEMENT_METADATA_LONGNAME
30 * - GST_ELEMENT_METADATA_KLASS
31 * - GST_ELEMENT_METADATA_DESCRIPTION
32 * - GST_ELEMENT_METADATA_AUTHOR
33 * - GST_ELEMENT_METADATA_DOC_URI
34 * - GST_ELEMENT_METADATA_ICON_NAME
36 * GstElement is the abstract base class needed to construct an element that
37 * can be used in a GStreamer pipeline. Please refer to the plugin writers
38 * guide for more information on creating #GstElement subclasses.
40 * The name of a #GstElement can be get with gst_element_get_name() and set with
41 * gst_element_set_name(). For speed, GST_ELEMENT_NAME() can be used in the
42 * core when using the appropriate locking. Do not use this in plug-ins or
43 * applications in order to retain ABI compatibility.
45 * Elements can have pads (of the type #GstPad). These pads link to pads on
46 * other elements. #GstBuffer flow between these linked pads.
47 * A #GstElement has a #GList of #GstPad structures for all their input (or sink)
48 * and output (or source) pads.
49 * Core and plug-in writers can add and remove pads with gst_element_add_pad()
50 * and gst_element_remove_pad().
52 * An existing pad of an element can be retrieved by name with
53 * gst_element_get_static_pad(). A new dynamic pad can be created using
54 * gst_element_request_pad() with a #GstPadTemplate.
55 * An iterator of all pads can be retrieved with gst_element_iterate_pads().
57 * Elements can be linked through their pads.
58 * If the link is straightforward, use the gst_element_link()
59 * convenience function to link two elements, or gst_element_link_many()
60 * for more elements in a row.
61 * Use gst_element_link_filtered() to link two elements constrained by
62 * a specified set of #GstCaps.
63 * For finer control, use gst_element_link_pads() and
64 * gst_element_link_pads_filtered() to specify the pads to link on
65 * each element by name.
67 * Each element has a state (see #GstState). You can get and set the state
68 * of an element with gst_element_get_state() and gst_element_set_state().
69 * Setting a state triggers a #GstStateChange. To get a string representation
70 * of a #GstState, use gst_element_state_get_name().
72 * You can get and set a #GstClock on an element using gst_element_get_clock()
73 * and gst_element_set_clock().
74 * Some elements can provide a clock for the pipeline if
75 * the #GST_ELEMENT_FLAG_PROVIDE_CLOCK flag is set. With the
76 * gst_element_provide_clock() method one can retrieve the clock provided by
78 * Not all elements require a clock to operate correctly. If the
79 * #GST_ELEMENT_FLAG_REQUIRE_CLOCK() flag is set, a clock should be set on the
80 * element with gst_element_set_clock().
82 * Note that clock selection and distribution is normally handled by the
83 * toplevel #GstPipeline so the clock functions are only to be used in very
84 * specific situations.
87 #include "gst_private.h"
90 #include <gobject/gvaluecollector.h>
92 #include "gstelement.h"
93 #include "gstelementmetadata.h"
94 #include "gstenumtypes.h"
98 #include "gstghostpad.h"
101 #include "gstquark.h"
102 #include "gsttracerutils.h"
103 #include "gstvalue.h"
104 #include <glib/gi18n-lib.h>
105 #include "glib-compat-private.h"
107 #include "printf/printf.h"
109 /* Element signals and args */
125 static void gst_element_class_init (GstElementClass * klass);
126 static void gst_element_init (GstElement * element);
127 static void gst_element_base_class_init (gpointer g_class);
129 static void gst_element_constructed (GObject * object);
130 static void gst_element_dispose (GObject * object);
131 static void gst_element_finalize (GObject * object);
133 static GstStateChangeReturn gst_element_change_state_func (GstElement * element,
134 GstStateChange transition);
135 static GstStateChangeReturn gst_element_get_state_func (GstElement * element,
136 GstState * state, GstState * pending, GstClockTime timeout);
137 static GstStateChangeReturn gst_element_set_state_func (GstElement * element,
139 static gboolean gst_element_set_clock_func (GstElement * element,
141 static void gst_element_set_bus_func (GstElement * element, GstBus * bus);
142 static gboolean gst_element_post_message_default (GstElement * element,
143 GstMessage * message);
144 static void gst_element_set_context_default (GstElement * element,
145 GstContext * context);
147 static gboolean gst_element_default_send_event (GstElement * element,
149 static gboolean gst_element_default_query (GstElement * element,
152 static GstPadTemplate
153 * gst_element_class_request_pad_simple_template (GstElementClass *
154 element_class, const gchar * name);
156 static void gst_element_call_async_func (gpointer data, gpointer user_data);
158 static GstObjectClass *parent_class = NULL;
159 static guint gst_element_signals[LAST_SIGNAL] = { 0 };
161 static GMutex _element_pool_lock;
162 static GThreadPool *gst_element_pool = NULL;
164 /* this is used in gstelementfactory.c:gst_element_register() */
165 GQuark __gst_elementclass_factory = 0;
167 /* used for gst_element_type_set_skip_documentation() and
168 * gst_element_factory_get_skip_documentation() */
169 GQuark __gst_elementclass_skip_doc = 0;
172 gst_element_get_type (void)
174 static gsize gst_element_type = 0;
176 if (g_once_init_enter (&gst_element_type)) {
178 static const GTypeInfo element_info = {
179 sizeof (GstElementClass),
180 gst_element_base_class_init,
181 NULL, /* base_class_finalize */
182 (GClassInitFunc) gst_element_class_init,
187 (GInstanceInitFunc) gst_element_init,
191 _type = g_type_register_static (GST_TYPE_OBJECT, "GstElement",
192 &element_info, G_TYPE_FLAG_ABSTRACT);
194 __gst_elementclass_factory =
195 g_quark_from_static_string ("GST_ELEMENTCLASS_FACTORY");
196 __gst_elementclass_skip_doc =
197 g_quark_from_static_string ("GST_ELEMENTCLASS_SKIP_DOCUMENTATION");
198 g_once_init_leave (&gst_element_type, _type);
200 return gst_element_type;
204 gst_element_setup_thread_pool (void)
209 GST_DEBUG ("creating element thread pool");
211 g_thread_pool_new ((GFunc) gst_element_call_async_func, NULL, -1, FALSE,
214 g_critical ("could not alloc threadpool %s", err->message);
215 g_clear_error (&err);
222 gst_element_class_init (GstElementClass * klass)
224 GObjectClass *gobject_class;
226 gobject_class = (GObjectClass *) klass;
228 parent_class = g_type_class_peek_parent (klass);
231 * GstElement::pad-added:
232 * @gstelement: the object which received the signal
233 * @new_pad: the pad that has been added
235 * a new #GstPad has been added to the element. Note that this signal will
236 * usually be emitted from the context of the streaming thread. Also keep in
237 * mind that if you add new elements to the pipeline in the signal handler
238 * you will need to set them to the desired target state with
239 * gst_element_set_state() or gst_element_sync_state_with_parent().
241 gst_element_signals[PAD_ADDED] =
242 g_signal_new ("pad-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
243 G_STRUCT_OFFSET (GstElementClass, pad_added), NULL, NULL,
244 NULL, G_TYPE_NONE, 1, GST_TYPE_PAD);
246 * GstElement::pad-removed:
247 * @gstelement: the object which received the signal
248 * @old_pad: the pad that has been removed
250 * a #GstPad has been removed from the element
252 gst_element_signals[PAD_REMOVED] =
253 g_signal_new ("pad-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
254 G_STRUCT_OFFSET (GstElementClass, pad_removed), NULL, NULL,
255 NULL, G_TYPE_NONE, 1, GST_TYPE_PAD);
257 * GstElement::no-more-pads:
258 * @gstelement: the object which received the signal
260 * This signals that the element will not generate more dynamic pads.
261 * Note that this signal will usually be emitted from the context of
262 * the streaming thread.
264 gst_element_signals[NO_MORE_PADS] =
265 g_signal_new ("no-more-pads", G_TYPE_FROM_CLASS (klass),
266 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstElementClass, no_more_pads), NULL,
267 NULL, NULL, G_TYPE_NONE, 0);
269 gobject_class->dispose = gst_element_dispose;
270 gobject_class->finalize = gst_element_finalize;
271 gobject_class->constructed = gst_element_constructed;
273 klass->change_state = GST_DEBUG_FUNCPTR (gst_element_change_state_func);
274 klass->set_state = GST_DEBUG_FUNCPTR (gst_element_set_state_func);
275 klass->get_state = GST_DEBUG_FUNCPTR (gst_element_get_state_func);
276 klass->set_clock = GST_DEBUG_FUNCPTR (gst_element_set_clock_func);
277 klass->set_bus = GST_DEBUG_FUNCPTR (gst_element_set_bus_func);
278 klass->query = GST_DEBUG_FUNCPTR (gst_element_default_query);
279 klass->send_event = GST_DEBUG_FUNCPTR (gst_element_default_send_event);
280 klass->numpadtemplates = 0;
281 klass->post_message = GST_DEBUG_FUNCPTR (gst_element_post_message_default);
282 klass->set_context = GST_DEBUG_FUNCPTR (gst_element_set_context_default);
284 klass->elementfactory = NULL;
288 gst_element_base_class_init (gpointer g_class)
290 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
291 GList *node, *padtemplates;
293 /* Copy the element details here so elements can inherit the
294 * details from their base class and classes only need to set
295 * the details in class_init instead of base_init */
296 element_class->metadata =
297 element_class->metadata ? gst_structure_copy (element_class->metadata) :
298 gst_structure_new_empty ("metadata");
300 /* Copy the pad templates so elements inherit them
301 * from their base class but elements can add pad templates in class_init
302 * instead of base_init.
304 padtemplates = g_list_copy (element_class->padtemplates);
305 for (node = padtemplates; node != NULL; node = node->next) {
306 GstPadTemplate *tmpl = (GstPadTemplate *) node->data;
307 gst_object_ref (tmpl);
309 element_class->padtemplates = padtemplates;
311 /* set the factory, see gst_element_register() */
312 element_class->elementfactory =
313 g_type_get_qdata (G_TYPE_FROM_CLASS (element_class),
314 __gst_elementclass_factory);
315 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "type %s : factory %p",
316 G_OBJECT_CLASS_NAME (element_class), element_class->elementfactory);
320 gst_element_init (GstElement * element)
322 GST_STATE (element) = GST_STATE_NULL;
323 GST_STATE_TARGET (element) = GST_STATE_NULL;
324 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
325 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
326 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
328 g_rec_mutex_init (&element->state_lock);
329 g_cond_init (&element->state_cond);
333 gst_element_constructed (GObject * object)
335 GST_TRACER_ELEMENT_NEW (GST_ELEMENT_CAST (object));
336 G_OBJECT_CLASS (parent_class)->constructed (object);
340 * gst_element_release_request_pad:
341 * @element: a #GstElement to release the request pad of.
342 * @pad: the #GstPad to release.
344 * Makes the element free the previously requested pad as obtained
345 * with gst_element_request_pad().
347 * This does not unref the pad. If the pad was created by using
348 * gst_element_request_pad(), gst_element_release_request_pad() needs to be
349 * followed by gst_object_unref() to free the @pad.
354 gst_element_release_request_pad (GstElement * element, GstPad * pad)
356 GstElementClass *oclass;
358 g_return_if_fail (GST_IS_ELEMENT (element));
359 g_return_if_fail (GST_IS_PAD (pad));
360 g_return_if_fail (GST_PAD_PAD_TEMPLATE (pad) == NULL ||
361 GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (pad)) ==
363 g_return_if_fail (GST_PAD_PARENT (pad) == element);
365 oclass = GST_ELEMENT_GET_CLASS (element);
367 /* if the element implements a custom release function we call that, else we
368 * simply remove the pad from the element */
369 if (oclass->release_pad)
370 oclass->release_pad (element, pad);
372 gst_element_remove_pad (element, pad);
376 * gst_element_provide_clock:
377 * @element: a #GstElement to query
379 * Get the clock provided by the given element.
380 * > An element is only required to provide a clock in the PAUSED
381 * > state. Some elements can provide a clock in other states.
383 * Returns: (transfer full) (nullable): the GstClock provided by the
384 * element or %NULL if no clock could be provided. Unref after usage.
389 gst_element_provide_clock (GstElement * element)
391 GstClock *result = NULL;
392 GstElementClass *oclass;
394 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
396 oclass = GST_ELEMENT_GET_CLASS (element);
398 if (oclass->provide_clock)
399 result = oclass->provide_clock (element);
405 gst_element_set_clock_func (GstElement * element, GstClock * clock)
409 GST_OBJECT_LOCK (element);
410 clock_p = &element->clock;
411 gst_object_replace ((GstObject **) clock_p, (GstObject *) clock);
412 GST_OBJECT_UNLOCK (element);
418 * gst_element_set_clock:
419 * @element: a #GstElement to set the clock for.
420 * @clock: (transfer none) (nullable): the #GstClock to set for the element.
422 * Sets the clock for the element. This function increases the
423 * refcount on the clock. Any previously set clock on the object
426 * Returns: %TRUE if the element accepted the clock. An element can refuse a
427 * clock when it, for example, is not able to slave its internal clock to the
428 * @clock or when it requires a specific clock to operate.
433 gst_element_set_clock (GstElement * element, GstClock * clock)
435 GstElementClass *oclass;
436 gboolean res = FALSE;
438 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
439 g_return_val_if_fail (clock == NULL || GST_IS_CLOCK (clock), FALSE);
441 oclass = GST_ELEMENT_GET_CLASS (element);
443 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element, "setting clock %p", clock);
445 if (oclass->set_clock)
446 res = oclass->set_clock (element, clock);
452 * gst_element_get_clock:
453 * @element: a #GstElement to get the clock of.
455 * Gets the currently configured clock of the element. This is the clock as was
456 * last set with gst_element_set_clock().
458 * Elements in a pipeline will only have their clock set when the
459 * pipeline is in the PLAYING state.
461 * Returns: (transfer full) (nullable): the #GstClock of the element. unref after usage.
466 gst_element_get_clock (GstElement * element)
470 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
472 GST_OBJECT_LOCK (element);
473 if ((result = element->clock))
474 gst_object_ref (result);
475 GST_OBJECT_UNLOCK (element);
481 * gst_element_set_base_time:
482 * @element: a #GstElement.
483 * @time: the base time to set.
485 * Set the base time of an element. See gst_element_get_base_time().
490 gst_element_set_base_time (GstElement * element, GstClockTime time)
494 g_return_if_fail (GST_IS_ELEMENT (element));
495 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (time));
497 GST_OBJECT_LOCK (element);
498 old = element->base_time;
499 element->base_time = time;
500 GST_OBJECT_UNLOCK (element);
502 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element,
503 "set base_time=%" GST_TIME_FORMAT ", old %" GST_TIME_FORMAT,
504 GST_TIME_ARGS (time), GST_TIME_ARGS (old));
508 * gst_element_get_base_time:
509 * @element: a #GstElement.
511 * Returns the base time of the element. The base time is the
512 * absolute time of the clock when this element was last put to
513 * PLAYING. Subtracting the base time from the clock time gives
514 * the running time of the element.
516 * Returns: the base time of the element.
521 gst_element_get_base_time (GstElement * element)
525 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
527 GST_OBJECT_LOCK (element);
528 result = element->base_time;
529 GST_OBJECT_UNLOCK (element);
535 * gst_element_set_start_time:
536 * @element: a #GstElement.
537 * @time: the base time to set.
539 * Set the start time of an element. The start time of the element is the
540 * running time of the element when it last went to the PAUSED state. In READY
541 * or after a flushing seek, it is set to 0.
543 * Toplevel elements like #GstPipeline will manage the start_time and
544 * base_time on its children. Setting the start_time to #GST_CLOCK_TIME_NONE
545 * on such a toplevel element will disable the distribution of the base_time to
546 * the children and can be useful if the application manages the base_time
547 * itself, for example if you want to synchronize capture from multiple
548 * pipelines, and you can also ensure that the pipelines have the same clock.
553 gst_element_set_start_time (GstElement * element, GstClockTime time)
557 g_return_if_fail (GST_IS_ELEMENT (element));
559 GST_OBJECT_LOCK (element);
560 old = GST_ELEMENT_START_TIME (element);
561 GST_ELEMENT_START_TIME (element) = time;
562 GST_OBJECT_UNLOCK (element);
564 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element,
565 "set start_time=%" GST_TIME_FORMAT ", old %" GST_TIME_FORMAT,
566 GST_TIME_ARGS (time), GST_TIME_ARGS (old));
570 * gst_element_get_start_time:
571 * @element: a #GstElement.
573 * Returns the start time of the element. The start time is the
574 * running time of the clock when this element was last put to PAUSED.
576 * Usually the start_time is managed by a toplevel element such as
581 * Returns: the start time of the element.
584 gst_element_get_start_time (GstElement * element)
588 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
590 GST_OBJECT_LOCK (element);
591 result = GST_ELEMENT_START_TIME (element);
592 GST_OBJECT_UNLOCK (element);
598 * gst_element_get_current_running_time:
599 * @element: a #GstElement.
601 * Returns the running time of the element. The running time is the
602 * element's clock time minus its base time. Will return GST_CLOCK_TIME_NONE
603 * if the element has no clock, or if its base time has not been set.
605 * Returns: the running time of the element, or GST_CLOCK_TIME_NONE if the
606 * element has no clock or its base time has not been set.
611 gst_element_get_current_running_time (GstElement * element)
613 GstClockTime base_time, clock_time;
615 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
617 base_time = gst_element_get_base_time (element);
619 if (!GST_CLOCK_TIME_IS_VALID (base_time)) {
620 GST_DEBUG_OBJECT (element, "Could not determine base time");
621 return GST_CLOCK_TIME_NONE;
624 clock_time = gst_element_get_current_clock_time (element);
626 if (!GST_CLOCK_TIME_IS_VALID (clock_time)) {
627 return GST_CLOCK_TIME_NONE;
630 if (clock_time < base_time) {
631 GST_DEBUG_OBJECT (element, "Got negative current running time");
632 return GST_CLOCK_TIME_NONE;
635 return clock_time - base_time;
639 * gst_element_get_current_clock_time:
640 * @element: a #GstElement.
642 * Returns the current clock time of the element, as in, the time of the
643 * element's clock, or GST_CLOCK_TIME_NONE if there is no clock.
645 * Returns: the clock time of the element, or GST_CLOCK_TIME_NONE if there is
651 gst_element_get_current_clock_time (GstElement * element)
653 GstClock *clock = NULL;
656 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
658 clock = gst_element_get_clock (element);
661 GST_DEBUG_OBJECT (element, "Element has no clock");
662 return GST_CLOCK_TIME_NONE;
665 ret = gst_clock_get_time (clock);
666 gst_object_unref (clock);
673 * gst_element_set_index:
674 * @element: a #GstElement.
675 * @index: (transfer none): a #GstIndex.
677 * Set @index on the element. The refcount of the index
678 * will be increased, any previously set index is unreffed.
683 gst_element_set_index (GstElement * element, GstIndex * index)
685 GstElementClass *oclass;
687 g_return_if_fail (GST_IS_ELEMENT (element));
688 g_return_if_fail (index == NULL || GST_IS_INDEX (index));
690 oclass = GST_ELEMENT_GET_CLASS (element);
692 if (oclass->set_index)
693 oclass->set_index (element, index);
697 * gst_element_get_index:
698 * @element: a #GstElement.
700 * Gets the index from the element.
702 * Returns: (transfer full) (nullable): a #GstIndex or %NULL when no
703 * index was set on the element. unref after usage.
708 gst_element_get_index (GstElement * element)
710 GstElementClass *oclass;
711 GstIndex *result = NULL;
713 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
715 oclass = GST_ELEMENT_GET_CLASS (element);
717 if (oclass->get_index)
718 result = oclass->get_index (element);
725 * gst_element_add_pad:
726 * @element: a #GstElement to add the pad to.
727 * @pad: (transfer floating): the #GstPad to add to the element.
729 * Adds a pad (link point) to @element. @pad's parent will be set to @element;
730 * see gst_object_set_parent() for refcounting information.
732 * Pads are automatically activated when added in the PAUSED or PLAYING
735 * The pad and the element should be unlocked when calling this function.
737 * This function will emit the #GstElement::pad-added signal on the element.
739 * Returns: %TRUE if the pad could be added. This function can fail when
740 * a pad with the same name already existed or the pad already had another
746 gst_element_add_pad (GstElement * element, GstPad * pad)
750 gboolean should_activate;
752 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
753 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
755 /* locking pad to look at the name */
756 GST_OBJECT_LOCK (pad);
757 pad_name = g_strdup (GST_PAD_NAME (pad));
758 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "adding pad '%s'",
759 GST_STR_NULL (pad_name));
760 active = GST_PAD_IS_ACTIVE (pad);
761 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_PARENT);
762 GST_OBJECT_UNLOCK (pad);
764 /* then check to see if there's already a pad by that name here */
765 GST_OBJECT_LOCK (element);
766 if (G_UNLIKELY (!gst_object_check_uniqueness (element->pads, pad_name)))
769 /* try to set the pad's parent */
770 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (pad),
771 GST_OBJECT_CAST (element))))
774 /* check for active pads */
775 should_activate = !active && (GST_STATE (element) > GST_STATE_READY ||
776 GST_STATE_NEXT (element) == GST_STATE_PAUSED);
780 /* add it to the list */
781 switch (gst_pad_get_direction (pad)) {
783 element->srcpads = g_list_append (element->srcpads, pad);
784 element->numsrcpads++;
787 element->sinkpads = g_list_append (element->sinkpads, pad);
788 element->numsinkpads++;
793 element->pads = g_list_append (element->pads, pad);
795 element->pads_cookie++;
796 GST_OBJECT_UNLOCK (element);
799 gst_pad_set_active (pad, TRUE);
801 /* emit the PAD_ADDED signal */
802 g_signal_emit (element, gst_element_signals[PAD_ADDED], 0, pad);
803 GST_TRACER_ELEMENT_ADD_PAD (element, pad);
809 g_critical ("Padname %s is not unique in element %s, not adding",
810 pad_name, GST_ELEMENT_NAME (element));
811 GST_OBJECT_UNLOCK (element);
813 gst_object_ref_sink (pad);
814 gst_object_unref (pad);
820 ("Pad %s already has parent when trying to add to element %s",
821 pad_name, GST_ELEMENT_NAME (element));
822 GST_OBJECT_UNLOCK (element);
828 GST_OBJECT_LOCK (pad);
830 ("Trying to add pad %s to element %s, but it has no direction",
831 GST_OBJECT_NAME (pad), GST_ELEMENT_NAME (element));
832 GST_OBJECT_UNLOCK (pad);
833 GST_OBJECT_UNLOCK (element);
839 * gst_element_remove_pad:
840 * @element: a #GstElement to remove pad from.
841 * @pad: (transfer none): the #GstPad to remove from the element.
843 * Removes @pad from @element. @pad will be destroyed if it has not been
844 * referenced elsewhere using gst_object_unparent().
846 * This function is used by plugin developers and should not be used
847 * by applications. Pads that were dynamically requested from elements
848 * with gst_element_request_pad() should be released with the
849 * gst_element_release_request_pad() function instead.
851 * Pads are not automatically deactivated so elements should perform the needed
852 * steps to deactivate the pad in case this pad is removed in the PAUSED or
853 * PLAYING state. See gst_pad_set_active() for more information about
856 * The pad and the element should be unlocked when calling this function.
858 * This function will emit the #GstElement::pad-removed signal on the element.
860 * Returns: %TRUE if the pad could be removed. Can return %FALSE if the
861 * pad does not belong to the provided element.
866 gst_element_remove_pad (GstElement * element, GstPad * pad)
870 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
871 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
873 /* locking pad to look at the name and parent */
874 GST_OBJECT_LOCK (pad);
875 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "removing pad '%s'",
876 GST_STR_NULL (GST_PAD_NAME (pad)));
878 if (G_UNLIKELY (GST_PAD_PARENT (pad) != element))
880 GST_OBJECT_UNLOCK (pad);
883 if ((peer = gst_pad_get_peer (pad))) {
884 /* window for MT unsafeness, someone else could unlink here
885 * and then we call unlink with wrong pads. The unlink
886 * function would catch this and safely return failed. */
887 if (GST_PAD_IS_SRC (pad))
888 gst_pad_unlink (pad, peer);
890 gst_pad_unlink (peer, pad);
892 gst_object_unref (peer);
895 /* if this is a ghost pad we also need to unset the target or it
896 * will stay linked although not allowed according to the topology.
898 * FIXME 2.0: Do this generically somehow from inside GstGhostPad
899 * when it gets unparented.
901 if (GST_IS_GHOST_PAD (pad)) {
902 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), NULL);
905 GST_OBJECT_LOCK (element);
906 /* remove it from the list */
907 switch (gst_pad_get_direction (pad)) {
909 element->srcpads = g_list_remove (element->srcpads, pad);
910 element->numsrcpads--;
913 element->sinkpads = g_list_remove (element->sinkpads, pad);
914 element->numsinkpads--;
917 g_critical ("Removing pad without direction???");
920 element->pads = g_list_remove (element->pads, pad);
922 element->pads_cookie++;
923 GST_OBJECT_UNLOCK (element);
925 /* emit the PAD_REMOVED signal before unparenting and losing the last ref. */
926 g_signal_emit (element, gst_element_signals[PAD_REMOVED], 0, pad);
927 GST_TRACER_ELEMENT_REMOVE_PAD (element, pad);
928 gst_object_unparent (GST_OBJECT_CAST (pad));
935 /* locking order is element > pad */
936 GST_OBJECT_UNLOCK (pad);
938 GST_OBJECT_LOCK (element);
939 GST_OBJECT_LOCK (pad);
940 g_critical ("Padname %s:%s does not belong to element %s when removing",
941 GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
942 GST_OBJECT_UNLOCK (pad);
943 GST_OBJECT_UNLOCK (element);
949 * gst_element_no_more_pads:
950 * @element: a #GstElement
952 * Use this function to signal that the element does not expect any more pads
953 * to show up in the current pipeline. This function should be called whenever
954 * pads have been added by the element itself. Elements with #GST_PAD_SOMETIMES
955 * pad templates use this in combination with autopluggers to figure out that
956 * the element is done initializing its pads.
958 * This function emits the #GstElement::no-more-pads signal.
963 gst_element_no_more_pads (GstElement * element)
965 g_return_if_fail (GST_IS_ELEMENT (element));
967 g_signal_emit (element, gst_element_signals[NO_MORE_PADS], 0);
971 pad_compare_name (GstPad * pad1, const gchar * name)
975 GST_OBJECT_LOCK (pad1);
976 result = strcmp (GST_PAD_NAME (pad1), name);
977 GST_OBJECT_UNLOCK (pad1);
983 * gst_element_get_static_pad:
984 * @element: a #GstElement to find a static pad of.
985 * @name: the name of the static #GstPad to retrieve.
987 * Retrieves a pad from @element by name. This version only retrieves
988 * already-existing (i.e. 'static') pads.
990 * Returns: (transfer full) (nullable): the requested #GstPad if
991 * found, otherwise %NULL. unref after usage.
996 gst_element_get_static_pad (GstElement * element, const gchar * name)
999 GstPad *result = NULL;
1001 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1002 g_return_val_if_fail (name != NULL, NULL);
1004 GST_OBJECT_LOCK (element);
1006 g_list_find_custom (element->pads, name, (GCompareFunc) pad_compare_name);
1008 result = GST_PAD_CAST (find->data);
1009 gst_object_ref (result);
1012 if (result == NULL) {
1013 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "no such pad '%s' in element \"%s\"",
1014 name, GST_ELEMENT_NAME (element));
1016 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
1017 GST_ELEMENT_NAME (element), name);
1019 GST_OBJECT_UNLOCK (element);
1025 gst_element_is_valid_request_template_name (const gchar * templ_name,
1029 const gchar *templ_name_ptr, *name_ptr;
1030 gboolean next_specifier;
1031 guint templ_postfix_len = 0, name_postfix_len = 0;
1033 g_return_val_if_fail (templ_name != NULL, FALSE);
1034 g_return_val_if_fail (name != NULL, FALSE);
1036 /* Is this the template name? */
1037 if (strcmp (templ_name, name) == 0)
1040 /* otherwise check all the specifiers */
1042 /* Because of sanity checks in gst_pad_template_new(), we know that %s
1043 * and %d and %u, occurring at the template_name */
1044 templ_name_ptr = strchr (templ_name, '%');
1046 /* check characters ahead of the specifier */
1047 if (!templ_name_ptr || strlen (name) <= templ_name_ptr - templ_name
1048 || strncmp (templ_name, name, templ_name_ptr - templ_name) != 0) {
1052 /* %s is not allowed for multiple specifiers, just a single specifier can be
1053 * accepted in gst_pad_template_new() and can not be mixed with other
1054 * specifier '%u' and '%d' */
1055 if (*(templ_name_ptr + 1) == 's') {
1059 name_ptr = name + (templ_name_ptr - templ_name);
1061 /* search next specifier, each of specifier should be separated by '_' */
1062 templ_name = strchr (templ_name_ptr, '_');
1063 name = strchr (name_ptr, '_');
1065 /* don't match the number of specifiers */
1066 if ((templ_name && !name) || (!templ_name && name))
1069 if (templ_name && name)
1070 next_specifier = TRUE;
1072 next_specifier = FALSE;
1074 /* check characters followed by the specifier */
1075 if (*(templ_name_ptr + 2) != '\0' && *(templ_name_ptr + 2) != '_') {
1076 if (next_specifier) {
1077 templ_postfix_len = templ_name - (templ_name_ptr + 2);
1078 name_postfix_len = name - name_ptr;
1080 templ_postfix_len = strlen (templ_name_ptr + 2);
1081 name_postfix_len = strlen (name_ptr);
1084 if (strncmp (templ_name_ptr + 2,
1085 name_ptr + name_postfix_len - templ_postfix_len,
1086 templ_postfix_len) != 0) {
1091 /* verify the specifier */
1092 if (*(name_ptr) == '%') {
1095 len = (next_specifier) ? name - name_ptr : strlen (name_ptr);
1097 if (strncmp (name_ptr, templ_name_ptr, len) != 0)
1101 const gchar *specifier;
1102 gchar *target = NULL;
1104 /* extract specifier when it has postfix characters */
1105 if (name_postfix_len > templ_postfix_len) {
1106 target = g_strndup (name_ptr, name_postfix_len - templ_postfix_len);
1108 specifier = target ? target : name_ptr;
1110 if (*(templ_name_ptr + 1) == 'd') {
1114 tmp = g_ascii_strtoll (specifier, &endptr, 10);
1115 if (tmp < G_MININT || tmp > G_MAXINT || (*endptr != '\0'
1118 } else if (*(templ_name_ptr + 1) == 'u') {
1122 tmp = g_ascii_strtoull (specifier, &endptr, 10);
1123 if (tmp > G_MAXUINT || (*endptr != '\0' && *endptr != '_'))
1130 /* otherwise we increment these from NULL to 1 */
1131 if (next_specifier) {
1135 } while (next_specifier);
1141 _gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
1142 const gchar * name, const GstCaps * caps)
1144 GstPad *newpad = NULL;
1145 GstElementClass *oclass;
1147 oclass = GST_ELEMENT_GET_CLASS (element);
1149 #ifndef G_DISABLE_CHECKS
1150 /* Some sanity checking here */
1154 g_return_val_if_fail (gst_element_is_valid_request_template_name
1155 (templ->name_template, name), NULL);
1157 pad = gst_element_get_static_pad (element, name);
1159 gst_object_unref (pad);
1160 /* FIXME 2.0: Change this to g_return_val_if_fail() */
1161 g_critical ("Element %s already has a pad named %s, the behaviour of "
1162 " gst_element_get_request_pad() for existing pads is undefined!",
1163 GST_ELEMENT_NAME (element), name);
1168 #ifdef GST_ENABLE_EXTRA_CHECKS
1170 if (!g_list_find (oclass->padtemplates, templ)) {
1171 /* FIXME 2.0: Change this to g_return_val_if_fail() */
1172 g_critical ("Element type %s does not have a pad template %s (%p)",
1173 g_type_name (G_OBJECT_TYPE (element)), templ->name_template, templ);
1178 if (oclass->request_new_pad)
1179 newpad = (oclass->request_new_pad) (element, templ, name, caps);
1182 gst_object_ref (newpad);
1187 #ifndef GST_REMOVE_DEPRECATED
1189 * gst_element_get_request_pad:
1190 * @element: a #GstElement to find a request pad of.
1191 * @name: the name of the request #GstPad to retrieve.
1193 * The name of this function is confusing to people learning GStreamer.
1194 * gst_element_request_pad_simple() aims at making it more explicit it is
1195 * a simplified gst_element_request_pad().
1197 * Deprecated: 1.20: Prefer using gst_element_request_pad_simple() which
1198 * provides the exact same functionality.
1200 * Returns: (transfer full) (nullable): requested #GstPad if found,
1201 * otherwise %NULL. Release after usage.
1204 gst_element_get_request_pad (GstElement * element, const gchar * name)
1206 return gst_element_request_pad_simple (element, name);
1211 * gst_element_request_pad_simple:
1212 * @element: a #GstElement to find a request pad of.
1213 * @name: the name of the request #GstPad to retrieve.
1215 * Retrieves a pad from the element by name (e.g. "src_\%d"). This version only
1216 * retrieves request pads. The pad should be released with
1217 * gst_element_release_request_pad().
1219 * This method is slower than manually getting the pad template and calling
1220 * gst_element_request_pad() if the pads should have a specific name (e.g.
1221 * @name is "src_1" instead of "src_\%u").
1223 * Note that this function was introduced in GStreamer 1.20 in order to provide
1224 * a better name to gst_element_get_request_pad(). Prior to 1.20, users
1225 * should use gst_element_get_request_pad() which provides the same
1228 * Returns: (transfer full) (nullable): requested #GstPad if found,
1229 * otherwise %NULL. Release after usage.
1234 gst_element_request_pad_simple (GstElement * element, const gchar * name)
1236 GstPadTemplate *templ = NULL;
1238 const gchar *req_name = NULL;
1239 gboolean templ_found = FALSE;
1241 GstElementClass *class;
1243 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1244 g_return_val_if_fail (name != NULL, NULL);
1246 class = GST_ELEMENT_GET_CLASS (element);
1248 templ = gst_element_class_request_pad_simple_template (class, name);
1250 req_name = strstr (name, "%") ? NULL : name;
1253 /* there is no % in the name, try to find a matching template */
1254 list = class->padtemplates;
1255 while (!templ_found && list) {
1256 templ = (GstPadTemplate *) list->data;
1257 if (templ->presence == GST_PAD_REQUEST) {
1258 GST_CAT_DEBUG (GST_CAT_PADS, "comparing %s to %s", name,
1259 templ->name_template);
1260 if (gst_element_is_valid_request_template_name (templ->name_template,
1274 pad = _gst_element_request_pad (element, templ, req_name, NULL);
1280 * gst_element_request_pad: (virtual request_new_pad)
1281 * @element: a #GstElement to find a request pad of.
1282 * @templ: a #GstPadTemplate of which we want a pad of.
1283 * @name: (transfer none) (allow-none): the name of the request #GstPad
1284 * to retrieve. Can be %NULL.
1285 * @caps: (transfer none) (allow-none): the caps of the pad we want to
1286 * request. Can be %NULL.
1288 * Retrieves a request pad from the element according to the provided template.
1289 * Pad templates can be looked up using
1290 * gst_element_factory_get_static_pad_templates().
1292 * The pad should be released with gst_element_release_request_pad().
1294 * Returns: (transfer full) (nullable): requested #GstPad if found,
1295 * otherwise %NULL. Release after usage.
1298 gst_element_request_pad (GstElement * element,
1299 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
1301 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1302 g_return_val_if_fail (templ != NULL, NULL);
1303 g_return_val_if_fail (templ->presence == GST_PAD_REQUEST, NULL);
1305 return _gst_element_request_pad (element, templ, name, caps);
1308 static GstIterator *
1309 gst_element_iterate_pad_list (GstElement * element, GList ** padlist)
1311 GstIterator *result;
1313 GST_OBJECT_LOCK (element);
1314 result = gst_iterator_new_list (GST_TYPE_PAD,
1315 GST_OBJECT_GET_LOCK (element),
1316 &element->pads_cookie, padlist, (GObject *) element, NULL);
1317 GST_OBJECT_UNLOCK (element);
1323 * gst_element_iterate_pads:
1324 * @element: a #GstElement to iterate pads of.
1326 * Retrieves an iterator of @element's pads. The iterator should
1327 * be freed after usage. Also more specialized iterators exists such as
1328 * gst_element_iterate_src_pads() or gst_element_iterate_sink_pads().
1330 * The order of pads returned by the iterator will be the order in which
1331 * the pads were added to the element.
1333 * Returns: (transfer full): the #GstIterator of #GstPad.
1338 gst_element_iterate_pads (GstElement * element)
1340 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1342 return gst_element_iterate_pad_list (element, &element->pads);
1346 * gst_element_iterate_src_pads:
1347 * @element: a #GstElement.
1349 * Retrieves an iterator of @element's source pads.
1351 * The order of pads returned by the iterator will be the order in which
1352 * the pads were added to the element.
1354 * Returns: (transfer full): the #GstIterator of #GstPad.
1359 gst_element_iterate_src_pads (GstElement * element)
1361 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1363 return gst_element_iterate_pad_list (element, &element->srcpads);
1367 * gst_element_iterate_sink_pads:
1368 * @element: a #GstElement.
1370 * Retrieves an iterator of @element's sink pads.
1372 * The order of pads returned by the iterator will be the order in which
1373 * the pads were added to the element.
1375 * Returns: (transfer full): the #GstIterator of #GstPad.
1380 gst_element_iterate_sink_pads (GstElement * element)
1382 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1384 return gst_element_iterate_pad_list (element, &element->sinkpads);
1388 gst_element_do_foreach_pad (GstElement * element,
1389 GstElementForeachPadFunc func, gpointer user_data,
1390 GList ** p_pads, guint16 * p_npads)
1392 gboolean ret = TRUE;
1397 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1398 g_return_val_if_fail (func != NULL, FALSE);
1400 GST_OBJECT_LOCK (element);
1402 pads = g_newa (GstPad *, n_pads + 1);
1403 for (l = *p_pads, i = 0; l != NULL; l = l->next) {
1404 g_assert (i < n_pads);
1405 pads[i++] = gst_object_ref (l->data);
1407 GST_OBJECT_UNLOCK (element);
1412 for (i = 0; i < n_pads; ++i) {
1413 ret = func (element, pads[i], user_data);
1418 for (i = 0; i < n_pads; ++i)
1419 gst_object_unref (pads[i]);
1425 * gst_element_foreach_sink_pad:
1426 * @element: a #GstElement to iterate sink pads of
1427 * @func: (scope call): function to call for each sink pad
1428 * @user_data: (closure): user data passed to @func
1430 * Call @func with @user_data for each of @element's sink pads. @func will be
1431 * called exactly once for each sink pad that exists at the time of this call,
1432 * unless one of the calls to @func returns %FALSE in which case we will stop
1433 * iterating pads and return early. If new sink pads are added or sink pads
1434 * are removed while the sink pads are being iterated, this will not be taken
1435 * into account until next time this function is used.
1437 * Returns: %FALSE if @element had no sink pads or if one of the calls to @func
1443 gst_element_foreach_sink_pad (GstElement * element,
1444 GstElementForeachPadFunc func, gpointer user_data)
1446 return gst_element_do_foreach_pad (element, func, user_data,
1447 &element->sinkpads, &element->numsinkpads);
1451 * gst_element_foreach_src_pad:
1452 * @element: a #GstElement to iterate source pads of
1453 * @func: (scope call): function to call for each source pad
1454 * @user_data: (closure): user data passed to @func
1456 * Call @func with @user_data for each of @element's source pads. @func will be
1457 * called exactly once for each source pad that exists at the time of this call,
1458 * unless one of the calls to @func returns %FALSE in which case we will stop
1459 * iterating pads and return early. If new source pads are added or source pads
1460 * are removed while the source pads are being iterated, this will not be taken
1461 * into account until next time this function is used.
1463 * Returns: %FALSE if @element had no source pads or if one of the calls
1464 * to @func returned %FALSE.
1469 gst_element_foreach_src_pad (GstElement * element,
1470 GstElementForeachPadFunc func, gpointer user_data)
1472 return gst_element_do_foreach_pad (element, func, user_data,
1473 &element->srcpads, &element->numsrcpads);
1477 * gst_element_foreach_pad:
1478 * @element: a #GstElement to iterate pads of
1479 * @func: (scope call): function to call for each pad
1480 * @user_data: (closure): user data passed to @func
1482 * Call @func with @user_data for each of @element's pads. @func will be called
1483 * exactly once for each pad that exists at the time of this call, unless
1484 * one of the calls to @func returns %FALSE in which case we will stop
1485 * iterating pads and return early. If new pads are added or pads are removed
1486 * while pads are being iterated, this will not be taken into account until
1487 * next time this function is used.
1489 * Returns: %FALSE if @element had no pads or if one of the calls to @func
1495 gst_element_foreach_pad (GstElement * element, GstElementForeachPadFunc func,
1498 return gst_element_do_foreach_pad (element, func, user_data,
1499 &element->pads, &element->numpads);
1503 * gst_element_class_add_pad_template:
1504 * @klass: the #GstElementClass to add the pad template to.
1505 * @templ: (transfer floating): a #GstPadTemplate to add to the element class.
1507 * Adds a padtemplate to an element class. This is mainly used in the _class_init
1508 * functions of classes. If a pad template with the same name as an already
1509 * existing one is added the old one is replaced by the new one.
1511 * @templ's reference count will be incremented, and any floating
1512 * reference will be removed (see gst_object_ref_sink())
1516 gst_element_class_add_pad_template (GstElementClass * klass,
1517 GstPadTemplate * templ)
1519 GList *template_list = klass->padtemplates;
1521 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1522 g_return_if_fail (GST_IS_PAD_TEMPLATE (templ));
1524 /* If we already have a pad template with the same name replace the
1526 while (template_list) {
1527 GstPadTemplate *padtempl = (GstPadTemplate *) template_list->data;
1529 /* Found pad with the same name, replace and return */
1530 if (strcmp (templ->name_template, padtempl->name_template) == 0) {
1531 gst_object_ref_sink (padtempl);
1532 gst_object_unref (padtempl);
1533 template_list->data = templ;
1536 template_list = g_list_next (template_list);
1539 /* Take ownership of the floating ref */
1540 gst_object_ref_sink (templ);
1542 klass->padtemplates = g_list_append (klass->padtemplates, templ);
1543 klass->numpadtemplates++;
1547 * gst_element_class_add_static_pad_template:
1548 * @klass: the #GstElementClass to add the pad template to.
1549 * @static_templ: #GstStaticPadTemplate to add as pad template to the element class.
1551 * Adds a pad template to an element class based on the static pad template
1552 * @templ. This is mainly used in the _class_init functions of element
1553 * implementations. If a pad template with the same name already exists,
1554 * the old one is replaced by the new one.
1559 gst_element_class_add_static_pad_template (GstElementClass * klass,
1560 GstStaticPadTemplate * static_templ)
1562 gst_element_class_add_pad_template (klass,
1563 gst_static_pad_template_get (static_templ));
1567 * gst_element_class_add_static_pad_template_with_gtype:
1568 * @klass: the #GstElementClass to add the pad template to.
1569 * @static_templ: #GstStaticPadTemplate to add as pad template to the element class.
1570 * @pad_type: The #GType of the pad to create
1572 * Adds a pad template to an element class based on the static pad template
1573 * @templ. This is mainly used in the _class_init functions of element
1574 * implementations. If a pad template with the same name already exists,
1575 * the old one is replaced by the new one.
1580 gst_element_class_add_static_pad_template_with_gtype (GstElementClass * klass,
1581 GstStaticPadTemplate * static_templ, GType pad_type)
1583 gst_element_class_add_pad_template (klass,
1584 gst_pad_template_new_from_static_pad_template_with_gtype (static_templ,
1589 * gst_element_class_add_metadata:
1590 * @klass: class to set metadata for
1591 * @key: the key to set
1592 * @value: the value to set
1594 * Set @key with @value as metadata in @klass.
1597 gst_element_class_add_metadata (GstElementClass * klass,
1598 const gchar * key, const gchar * value)
1600 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1601 g_return_if_fail (key != NULL);
1602 g_return_if_fail (value != NULL);
1604 gst_structure_set ((GstStructure *) klass->metadata,
1605 key, G_TYPE_STRING, value, NULL);
1609 * gst_element_class_add_static_metadata:
1610 * @klass: class to set metadata for
1611 * @key: the key to set
1612 * @value: the value to set
1614 * Set @key with @value as metadata in @klass.
1616 * Same as gst_element_class_add_metadata(), but @value must be a static string
1617 * or an inlined string, as it will not be copied. (GStreamer plugins will
1618 * be made resident once loaded, so this function can be used even from
1619 * dynamically loaded plugins.)
1622 gst_element_class_add_static_metadata (GstElementClass * klass,
1623 const gchar * key, const gchar * value)
1625 GValue val = G_VALUE_INIT;
1627 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1628 g_return_if_fail (key != NULL);
1629 g_return_if_fail (value != NULL);
1631 g_value_init (&val, G_TYPE_STRING);
1632 g_value_set_static_string (&val, value);
1633 gst_structure_take_value ((GstStructure *) klass->metadata, key, &val);
1637 * gst_element_class_set_metadata:
1638 * @klass: class to set metadata for
1639 * @longname: The long English name of the element. E.g. "File Sink"
1640 * @classification: String describing the type of element, as an unordered list
1641 * separated with slashes ('/'). See draft-klass.txt of the design docs
1642 * for more details and common types. E.g: "Sink/File"
1643 * @description: Sentence describing the purpose of the element.
1644 * E.g: "Write stream to a file"
1645 * @author: Name and contact details of the author(s). Use \n to separate
1646 * multiple author metadata. E.g: "Joe Bloggs <joe.blogs at foo.com>"
1648 * Sets the detailed information for a #GstElementClass.
1649 * > This function is for use in _class_init functions only.
1652 gst_element_class_set_metadata (GstElementClass * klass,
1653 const gchar * longname, const gchar * classification,
1654 const gchar * description, const gchar * author)
1656 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1657 g_return_if_fail (longname != NULL && *longname != '\0');
1658 g_return_if_fail (classification != NULL && *classification != '\0');
1659 g_return_if_fail (description != NULL && *description != '\0');
1660 g_return_if_fail (author != NULL && *author != '\0');
1662 gst_structure_id_set ((GstStructure *) klass->metadata,
1663 GST_QUARK (ELEMENT_METADATA_LONGNAME), G_TYPE_STRING, longname,
1664 GST_QUARK (ELEMENT_METADATA_KLASS), G_TYPE_STRING, classification,
1665 GST_QUARK (ELEMENT_METADATA_DESCRIPTION), G_TYPE_STRING, description,
1666 GST_QUARK (ELEMENT_METADATA_AUTHOR), G_TYPE_STRING, author, NULL);
1670 * gst_element_class_set_static_metadata:
1671 * @klass: class to set metadata for
1672 * @longname: The long English name of the element. E.g. "File Sink"
1673 * @classification: String describing the type of element, as an unordered list
1674 * separated with slashes ('/'). See draft-klass.txt of the design docs
1675 * for more details and common types. E.g: "Sink/File"
1676 * @description: Sentence describing the purpose of the element.
1677 * E.g: "Write stream to a file"
1678 * @author: Name and contact details of the author(s). Use \n to separate
1679 * multiple author metadata. E.g: "Joe Bloggs <joe.blogs at foo.com>"
1681 * Sets the detailed information for a #GstElementClass.
1683 * > This function is for use in _class_init functions only.
1685 * Same as gst_element_class_set_metadata(), but @longname, @classification,
1686 * @description, and @author must be static strings or inlined strings, as
1687 * they will not be copied. (GStreamer plugins will be made resident once
1688 * loaded, so this function can be used even from dynamically loaded plugins.)
1691 gst_element_class_set_static_metadata (GstElementClass * klass,
1692 const gchar * longname, const gchar * classification,
1693 const gchar * description, const gchar * author)
1695 GstStructure *s = (GstStructure *) klass->metadata;
1696 GValue val = G_VALUE_INIT;
1698 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1699 g_return_if_fail (longname != NULL && *longname != '\0');
1700 g_return_if_fail (classification != NULL && *classification != '\0');
1701 g_return_if_fail (description != NULL && *description != '\0');
1702 g_return_if_fail (author != NULL && *author != '\0');
1704 g_value_init (&val, G_TYPE_STRING);
1706 g_value_set_static_string (&val, longname);
1707 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_LONGNAME), &val);
1709 g_value_set_static_string (&val, classification);
1710 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_KLASS), &val);
1712 g_value_set_static_string (&val, description);
1713 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_DESCRIPTION),
1716 g_value_set_static_string (&val, author);
1717 gst_structure_id_take_value (s, GST_QUARK (ELEMENT_METADATA_AUTHOR), &val);
1721 * gst_element_class_get_metadata:
1722 * @klass: class to get metadata for
1723 * @key: the key to get
1725 * Get metadata with @key in @klass.
1727 * Returns: the metadata for @key.
1730 gst_element_class_get_metadata (GstElementClass * klass, const gchar * key)
1732 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (klass), NULL);
1733 g_return_val_if_fail (key != NULL, NULL);
1735 return gst_structure_get_string ((GstStructure *) klass->metadata, key);
1739 * gst_element_get_metadata:
1740 * @element: class to get metadata for
1741 * @key: the key to get
1743 * Get metadata with @key in @klass.
1745 * Returns: the metadata for @key.
1750 gst_element_get_metadata (GstElement * element, const gchar * key)
1752 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1753 g_return_val_if_fail (key != NULL, NULL);
1755 return gst_element_class_get_metadata (GST_ELEMENT_GET_CLASS (element), key);
1759 * gst_element_class_get_pad_template_list:
1760 * @element_class: a #GstElementClass to get pad templates of.
1762 * Retrieves a list of the pad templates associated with @element_class. The
1763 * list must not be modified by the calling code.
1764 * > If you use this function in the GInstanceInitFunc of an object class
1765 * > that has subclasses, make sure to pass the g_class parameter of the
1766 * > GInstanceInitFunc here.
1768 * Returns: (transfer none) (element-type Gst.PadTemplate): the #GList of
1772 gst_element_class_get_pad_template_list (GstElementClass * element_class)
1774 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1776 return element_class->padtemplates;
1780 * gst_element_get_pad_template_list:
1781 * @element: a #GstElement to get pad templates of.
1783 * Retrieves a list of the pad templates associated with @element. The
1784 * list must not be modified by the calling code.
1786 * Returns: (transfer none) (element-type Gst.PadTemplate): the #GList of
1792 gst_element_get_pad_template_list (GstElement * element)
1794 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1797 gst_element_class_get_pad_template_list (GST_ELEMENT_GET_CLASS (element));
1801 * gst_element_class_get_pad_template:
1802 * @element_class: a #GstElementClass to get the pad template of.
1803 * @name: the name of the #GstPadTemplate to get.
1805 * Retrieves a padtemplate from @element_class with the given name.
1806 * > If you use this function in the GInstanceInitFunc of an object class
1807 * > that has subclasses, make sure to pass the g_class parameter of the
1808 * > GInstanceInitFunc here.
1810 * Returns: (transfer none) (nullable): the #GstPadTemplate with the
1811 * given name, or %NULL if none was found. No unreferencing is
1815 gst_element_class_get_pad_template (GstElementClass *
1816 element_class, const gchar * name)
1820 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1821 g_return_val_if_fail (name != NULL, NULL);
1823 padlist = element_class->padtemplates;
1826 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
1828 if (strcmp (padtempl->name_template, name) == 0)
1831 padlist = g_list_next (padlist);
1838 * gst_element_get_pad_template:
1839 * @element: a #GstElement to get the pad template of.
1840 * @name: the name of the #GstPadTemplate to get.
1842 * Retrieves a padtemplate from @element with the given name.
1844 * Returns: (transfer none) (nullable): the #GstPadTemplate with the
1845 * given name, or %NULL if none was found. No unreferencing is
1851 gst_element_get_pad_template (GstElement * element, const gchar * name)
1853 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1854 g_return_val_if_fail (name != NULL, NULL);
1856 return gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (element),
1860 static GstPadTemplate *
1861 gst_element_class_request_pad_simple_template (GstElementClass *
1862 element_class, const gchar * name)
1864 GstPadTemplate *tmpl;
1866 tmpl = gst_element_class_get_pad_template (element_class, name);
1867 if (tmpl != NULL && tmpl->presence == GST_PAD_REQUEST)
1873 /* get a random pad on element of the given direction.
1874 * The pad is random in a sense that it is the first pad that is (optionally) linked.
1877 gst_element_get_random_pad (GstElement * element,
1878 gboolean need_linked, GstPadDirection dir)
1880 GstPad *result = NULL;
1883 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "getting a random pad");
1887 GST_OBJECT_LOCK (element);
1888 pads = element->srcpads;
1891 GST_OBJECT_LOCK (element);
1892 pads = element->sinkpads;
1895 goto wrong_direction;
1897 for (; pads; pads = g_list_next (pads)) {
1898 GstPad *pad = GST_PAD_CAST (pads->data);
1900 GST_OBJECT_LOCK (pad);
1901 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "checking pad %s:%s",
1902 GST_DEBUG_PAD_NAME (pad));
1904 if (need_linked && !GST_PAD_IS_LINKED (pad)) {
1905 /* if we require a linked pad, and it is not linked, continue the
1907 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is not linked",
1908 GST_DEBUG_PAD_NAME (pad));
1909 GST_OBJECT_UNLOCK (pad);
1912 /* found a pad, stop search */
1913 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
1914 GST_DEBUG_PAD_NAME (pad));
1915 GST_OBJECT_UNLOCK (pad);
1921 gst_object_ref (result);
1923 GST_OBJECT_UNLOCK (element);
1927 /* ERROR handling */
1930 g_warning ("unknown pad direction %d", dir);
1936 gst_element_default_send_event (GstElement * element, GstEvent * event)
1938 gboolean result = FALSE;
1941 pad = GST_EVENT_IS_DOWNSTREAM (event) ?
1942 gst_element_get_random_pad (element, TRUE, GST_PAD_SINK) :
1943 gst_element_get_random_pad (element, TRUE, GST_PAD_SRC);
1946 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1947 "pushing %s event to random %s pad %s:%s",
1948 GST_EVENT_TYPE_NAME (event),
1949 (GST_PAD_DIRECTION (pad) == GST_PAD_SRC ? "src" : "sink"),
1950 GST_DEBUG_PAD_NAME (pad));
1952 result = gst_pad_send_event (pad, event);
1953 gst_object_unref (pad);
1955 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "can't send %s event on element %s",
1956 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1957 gst_event_unref (event);
1963 * gst_element_send_event:
1964 * @element: a #GstElement to send the event to.
1965 * @event: (transfer full): the #GstEvent to send to the element.
1967 * Sends an event to an element. If the element doesn't implement an
1968 * event handler, the event will be pushed on a random linked sink pad for
1969 * downstream events or a random linked source pad for upstream events.
1971 * This function takes ownership of the provided event so you should
1972 * gst_event_ref() it if you want to reuse the event after this call.
1976 * Returns: %TRUE if the event was handled. Events that trigger a preroll (such
1977 * as flushing seeks and steps) will emit %GST_MESSAGE_ASYNC_DONE.
1980 gst_element_send_event (GstElement * element, GstEvent * event)
1982 GstElementClass *oclass;
1983 gboolean result = FALSE;
1985 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1986 g_return_val_if_fail (event != NULL, FALSE);
1988 oclass = GST_ELEMENT_GET_CLASS (element);
1990 GST_STATE_LOCK (element);
1991 if (oclass->send_event) {
1992 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send %s event on element %s",
1993 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1994 result = oclass->send_event (element, event);
1996 gst_event_unref (event);
1998 GST_STATE_UNLOCK (element);
2005 * @element: a #GstElement to send the event to.
2006 * @rate: The new playback rate
2007 * @format: The format of the seek values
2008 * @flags: The optional seek flags.
2009 * @start_type: The type and flags for the new start position
2010 * @start: The value of the new start position
2011 * @stop_type: The type and flags for the new stop position
2012 * @stop: The value of the new stop position
2014 * Sends a seek event to an element. See gst_event_new_seek() for the details of
2015 * the parameters. The seek event is sent to the element using
2016 * gst_element_send_event().
2020 * Returns: %TRUE if the event was handled. Flushing seeks will trigger a
2021 * preroll, which will emit %GST_MESSAGE_ASYNC_DONE.
2024 gst_element_seek (GstElement * element, gdouble rate, GstFormat format,
2025 GstSeekFlags flags, GstSeekType start_type, gint64 start,
2026 GstSeekType stop_type, gint64 stop)
2031 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2034 gst_event_new_seek (rate, format, flags, start_type, start, stop_type,
2036 result = gst_element_send_event (element, event);
2042 gst_element_default_query (GstElement * element, GstQuery * query)
2044 gboolean result = FALSE;
2047 pad = gst_element_get_random_pad (element, FALSE, GST_PAD_SRC);
2049 result = gst_pad_query (pad, query);
2051 gst_object_unref (pad);
2053 pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
2055 GstPad *peer = gst_pad_get_peer (pad);
2058 result = gst_pad_query (peer, query);
2060 gst_object_unref (peer);
2062 gst_object_unref (pad);
2069 * gst_element_query:
2070 * @element: a #GstElement to perform the query on.
2071 * @query: (transfer none): the #GstQuery.
2073 * Performs a query on the given element.
2075 * For elements that don't implement a query handler, this function
2076 * forwards the query to a random srcpad or to the peer of a
2077 * random linked sinkpad of this element.
2079 * Please note that some queries might need a running pipeline to work.
2081 * Returns: %TRUE if the query could be performed.
2086 gst_element_query (GstElement * element, GstQuery * query)
2088 GstElementClass *klass;
2089 gboolean res = FALSE;
2091 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2092 g_return_val_if_fail (query != NULL, FALSE);
2094 GST_TRACER_ELEMENT_QUERY_PRE (element, query);
2096 klass = GST_ELEMENT_GET_CLASS (element);
2098 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send query on element %s",
2099 GST_ELEMENT_NAME (element));
2100 res = klass->query (element, query);
2103 GST_TRACER_ELEMENT_QUERY_POST (element, query, res);
2108 gst_element_post_message_default (GstElement * element, GstMessage * message)
2111 gboolean result = FALSE;
2113 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2114 g_return_val_if_fail (message != NULL, FALSE);
2116 GST_OBJECT_LOCK (element);
2119 if (G_UNLIKELY (bus == NULL))
2122 gst_object_ref (bus);
2123 GST_OBJECT_UNLOCK (element);
2125 /* we release the element lock when posting the message so that any
2126 * (synchronous) message handlers can operate on the element */
2127 result = gst_bus_post (bus, message);
2128 gst_object_unref (bus);
2135 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, element,
2136 "not posting message %p: no bus", message);
2137 GST_OBJECT_UNLOCK (element);
2138 gst_message_unref (message);
2144 * gst_element_post_message:
2145 * @element: a #GstElement posting the message
2146 * @message: (transfer full): a #GstMessage to post
2148 * Post a message on the element's #GstBus. This function takes ownership of the
2149 * message; if you want to access the message after this call, you should add an
2150 * additional reference before calling.
2152 * Returns: %TRUE if the message was successfully posted. The function returns
2153 * %FALSE if the element did not have a bus.
2158 gst_element_post_message (GstElement * element, GstMessage * message)
2160 GstElementClass *klass;
2161 gboolean res = FALSE;
2163 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2164 g_return_val_if_fail (message != NULL, FALSE);
2166 GST_TRACER_ELEMENT_POST_MESSAGE_PRE (element, message);
2168 klass = GST_ELEMENT_GET_CLASS (element);
2169 if (klass->post_message)
2170 res = klass->post_message (element, message);
2172 gst_message_unref (message);
2174 GST_TRACER_ELEMENT_POST_MESSAGE_POST (element, res);
2179 * _gst_element_error_printf:
2180 * @format: (allow-none): the printf-like format to use, or %NULL
2182 * This function is only used internally by the gst_element_error() macro.
2184 * Returns: (transfer full) (nullable): a newly allocated string, or
2185 * %NULL if the format was %NULL or ""
2190 _gst_element_error_printf (const gchar * format, ...)
2201 va_start (args, format);
2203 len = __gst_vasprintf (&buffer, format, args);
2214 * gst_element_message_full_with_details:
2215 * @element: a #GstElement to send message from
2216 * @type: the #GstMessageType
2217 * @domain: the GStreamer GError domain this message belongs to
2218 * @code: the GError code belonging to the domain
2219 * @text: (allow-none) (transfer full): an allocated text string to be used
2220 * as a replacement for the default message connected to code,
2222 * @debug: (allow-none) (transfer full): an allocated debug message to be
2223 * used as a replacement for the default debugging information,
2225 * @file: the source code file where the error was generated
2226 * @function: the source code function where the error was generated
2227 * @line: the source code line where the error was generated
2228 * @structure:(transfer full): optional details structure
2230 * Post an error, warning or info message on the bus from inside an element.
2232 * @type must be of #GST_MESSAGE_ERROR, #GST_MESSAGE_WARNING or
2233 * #GST_MESSAGE_INFO.
2237 void gst_element_message_full_with_details
2238 (GstElement * element, GstMessageType type,
2239 GQuark domain, gint code, gchar * text,
2240 gchar * debug, const gchar * file, const gchar * function, gint line,
2241 GstStructure * structure)
2243 GError *gerror = NULL;
2247 gboolean has_debug = TRUE;
2248 GstMessage *message = NULL;
2251 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, element, "start");
2252 g_return_if_fail (GST_IS_ELEMENT (element));
2253 g_return_if_fail ((type == GST_MESSAGE_ERROR) ||
2254 (type == GST_MESSAGE_WARNING) || (type == GST_MESSAGE_INFO));
2256 /* check if we send the given text or the default error text */
2257 if ((text == NULL) || (text[0] == 0)) {
2258 /* text could have come from g_strdup_printf (""); */
2260 sent_text = gst_error_get_message (domain, code);
2264 /* construct a sent_debug with extra information from source */
2265 if ((debug == NULL) || (debug[0] == 0)) {
2266 /* debug could have come from g_strdup_printf (""); */
2270 name = gst_object_get_path_string (GST_OBJECT_CAST (element));
2272 sent_debug = g_strdup_printf ("%s(%d): %s (): %s:\n%s",
2273 file, line, function, name, debug);
2275 sent_debug = g_strdup_printf ("%s(%d): %s (): %s",
2276 file, line, function, name);
2280 /* create gerror and post message */
2281 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posting message: %s",
2283 gerror = g_error_new_literal (domain, code, sent_text);
2286 case GST_MESSAGE_ERROR:
2288 gst_message_new_error_with_details (GST_OBJECT_CAST (element), gerror,
2289 sent_debug, structure);
2291 case GST_MESSAGE_WARNING:
2293 gst_message_new_warning_with_details (GST_OBJECT_CAST (element),
2294 gerror, sent_debug, structure);
2296 case GST_MESSAGE_INFO:
2298 gst_message_new_info_with_details (GST_OBJECT_CAST (element), gerror,
2299 sent_debug, structure);
2302 g_assert_not_reached ();
2306 gst_element_post_message (element, message);
2308 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posted %s message: %s",
2309 (type == GST_MESSAGE_ERROR ? "error" : "warning"), sent_text);
2312 g_error_free (gerror);
2313 g_free (sent_debug);
2318 * gst_element_message_full:
2319 * @element: a #GstElement to send message from
2320 * @type: the #GstMessageType
2321 * @domain: the GStreamer GError domain this message belongs to
2322 * @code: the GError code belonging to the domain
2323 * @text: (allow-none) (transfer full): an allocated text string to be used
2324 * as a replacement for the default message connected to code,
2326 * @debug: (allow-none) (transfer full): an allocated debug message to be
2327 * used as a replacement for the default debugging information,
2329 * @file: the source code file where the error was generated
2330 * @function: the source code function where the error was generated
2331 * @line: the source code line where the error was generated
2333 * Post an error, warning or info message on the bus from inside an element.
2335 * @type must be of #GST_MESSAGE_ERROR, #GST_MESSAGE_WARNING or
2336 * #GST_MESSAGE_INFO.
2340 void gst_element_message_full
2341 (GstElement * element, GstMessageType type,
2342 GQuark domain, gint code, gchar * text,
2343 gchar * debug, const gchar * file, const gchar * function, gint line)
2345 gst_element_message_full_with_details (element, type, domain, code, text,
2346 debug, file, function, line, NULL);
2350 * gst_element_is_locked_state:
2351 * @element: a #GstElement.
2353 * Checks if the state of an element is locked.
2354 * If the state of an element is locked, state changes of the parent don't
2355 * affect the element.
2356 * This way you can leave currently unused elements inside bins. Just lock their
2357 * state before changing the state from #GST_STATE_NULL.
2361 * Returns: %TRUE, if the element's state is locked.
2364 gst_element_is_locked_state (GstElement * element)
2368 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2370 GST_OBJECT_LOCK (element);
2371 result = GST_ELEMENT_IS_LOCKED_STATE (element);
2372 GST_OBJECT_UNLOCK (element);
2378 * gst_element_set_locked_state:
2379 * @element: a #GstElement
2380 * @locked_state: %TRUE to lock the element's state
2382 * Locks the state of an element, so state changes of the parent don't affect
2383 * this element anymore.
2385 * Note that this is racy if the state lock of the parent bin is not taken.
2386 * The parent bin might've just checked the flag in another thread and as the
2387 * next step proceed to change the child element's state.
2391 * Returns: %TRUE if the state was changed, %FALSE if bad parameters were given
2392 * or the elements state-locking needed no change.
2395 gst_element_set_locked_state (GstElement * element, gboolean locked_state)
2399 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2401 GST_OBJECT_LOCK (element);
2402 old = GST_ELEMENT_IS_LOCKED_STATE (element);
2404 if (G_UNLIKELY (old == locked_state))
2408 GST_CAT_DEBUG (GST_CAT_STATES, "locking state of element %s",
2409 GST_ELEMENT_NAME (element));
2410 GST_OBJECT_FLAG_SET (element, GST_ELEMENT_FLAG_LOCKED_STATE);
2412 GST_CAT_DEBUG (GST_CAT_STATES, "unlocking state of element %s",
2413 GST_ELEMENT_NAME (element));
2414 GST_OBJECT_FLAG_UNSET (element, GST_ELEMENT_FLAG_LOCKED_STATE);
2416 GST_OBJECT_UNLOCK (element);
2422 GST_CAT_DEBUG (GST_CAT_STATES,
2423 "elements %s was already in locked state %d",
2424 GST_ELEMENT_NAME (element), old);
2425 GST_OBJECT_UNLOCK (element);
2432 * gst_element_sync_state_with_parent:
2433 * @element: a #GstElement.
2435 * Tries to change the state of the element to the same as its parent.
2436 * If this function returns %FALSE, the state of element is undefined.
2438 * Returns: %TRUE, if the element's state could be synced to the parent's state.
2443 gst_element_sync_state_with_parent (GstElement * element)
2447 GstStateChangeReturn ret;
2449 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2451 if ((parent = GST_ELEMENT_CAST (gst_element_get_parent (element)))) {
2452 GstState parent_current, parent_pending;
2454 GST_OBJECT_LOCK (parent);
2455 parent_current = GST_STATE (parent);
2456 parent_pending = GST_STATE_PENDING (parent);
2457 GST_OBJECT_UNLOCK (parent);
2459 /* set to pending if there is one, else we set it to the current state of
2461 if (parent_pending != GST_STATE_VOID_PENDING)
2462 target = parent_pending;
2464 target = parent_current;
2466 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2467 "syncing state (%s) to parent %s %s (%s, %s)",
2468 gst_element_state_get_name (GST_STATE (element)),
2469 GST_ELEMENT_NAME (parent), gst_element_state_get_name (target),
2470 gst_element_state_get_name (parent_current),
2471 gst_element_state_get_name (parent_pending));
2473 ret = gst_element_set_state (element, target);
2474 if (ret == GST_STATE_CHANGE_FAILURE)
2477 gst_object_unref (parent);
2481 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "element has no parent");
2488 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2489 "syncing state failed (%s)",
2490 gst_element_state_change_return_get_name (ret));
2491 gst_object_unref (parent);
2497 static GstStateChangeReturn
2498 gst_element_get_state_func (GstElement * element,
2499 GstState * state, GstState * pending, GstClockTime timeout)
2501 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
2502 GstState old_pending;
2504 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "getting state, timeout %"
2505 GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
2507 GST_OBJECT_LOCK (element);
2508 ret = GST_STATE_RETURN (element);
2509 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "RETURN is %s",
2510 gst_element_state_change_return_get_name (ret));
2512 /* we got an error, report immediately */
2513 if (ret == GST_STATE_CHANGE_FAILURE)
2516 /* we got no_preroll, report immediately */
2517 if (ret == GST_STATE_CHANGE_NO_PREROLL)
2520 /* no need to wait async if we are not async */
2521 if (ret != GST_STATE_CHANGE_ASYNC)
2524 old_pending = GST_STATE_PENDING (element);
2525 if (old_pending != GST_STATE_VOID_PENDING) {
2529 /* get cookie to detect state changes during waiting */
2530 cookie = element->state_cookie;
2532 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2533 "waiting for element to commit state");
2535 /* we have a pending state change, wait for it to complete */
2536 if (timeout != GST_CLOCK_TIME_NONE) {
2538 /* make timeout absolute */
2539 end_time = g_get_monotonic_time () + (timeout / 1000);
2540 signaled = GST_STATE_WAIT_UNTIL (element, end_time);
2542 GST_STATE_WAIT (element);
2547 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timed out");
2548 /* timeout triggered */
2549 ret = GST_STATE_CHANGE_ASYNC;
2551 if (cookie != element->state_cookie)
2554 /* could be success or failure */
2555 if (old_pending == GST_STATE (element)) {
2556 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got success");
2557 ret = GST_STATE_CHANGE_SUCCESS;
2559 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got failure");
2560 ret = GST_STATE_CHANGE_FAILURE;
2563 /* if nothing is pending anymore we can return SUCCESS */
2564 if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
2565 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "nothing pending");
2566 ret = GST_STATE_CHANGE_SUCCESS;
2572 *state = GST_STATE (element);
2574 *pending = GST_STATE_PENDING (element);
2576 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2577 "state current: %s, pending: %s, result: %s",
2578 gst_element_state_get_name (GST_STATE (element)),
2579 gst_element_state_get_name (GST_STATE_PENDING (element)),
2580 gst_element_state_change_return_get_name (ret));
2581 GST_OBJECT_UNLOCK (element);
2588 *state = GST_STATE_VOID_PENDING;
2590 *pending = GST_STATE_VOID_PENDING;
2592 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "interrupted");
2594 GST_OBJECT_UNLOCK (element);
2596 return GST_STATE_CHANGE_FAILURE;
2601 * gst_element_get_state:
2602 * @element: a #GstElement to get the state of.
2603 * @state: (out) (allow-none): a pointer to #GstState to hold the state.
2605 * @pending: (out) (allow-none): a pointer to #GstState to hold the pending
2606 * state. Can be %NULL.
2607 * @timeout: a #GstClockTime to specify the timeout for an async
2608 * state change or %GST_CLOCK_TIME_NONE for infinite timeout.
2610 * Gets the state of the element.
2612 * For elements that performed an ASYNC state change, as reported by
2613 * gst_element_set_state(), this function will block up to the
2614 * specified timeout value for the state change to complete.
2615 * If the element completes the state change or goes into
2616 * an error, this function returns immediately with a return value of
2617 * %GST_STATE_CHANGE_SUCCESS or %GST_STATE_CHANGE_FAILURE respectively.
2619 * For elements that did not return %GST_STATE_CHANGE_ASYNC, this function
2620 * returns the current and pending state immediately.
2622 * This function returns %GST_STATE_CHANGE_NO_PREROLL if the element
2623 * successfully changed its state but is not able to provide data yet.
2624 * This mostly happens for live sources that only produce data in
2625 * %GST_STATE_PLAYING. While the state change return is equivalent to
2626 * %GST_STATE_CHANGE_SUCCESS, it is returned to the application to signal that
2627 * some sink elements might not be able to complete their state change because
2628 * an element is not producing data to complete the preroll. When setting the
2629 * element to playing, the preroll will complete and playback will start.
2631 * Returns: %GST_STATE_CHANGE_SUCCESS if the element has no more pending state
2632 * and the last state change succeeded, %GST_STATE_CHANGE_ASYNC if the
2633 * element is still performing a state change or
2634 * %GST_STATE_CHANGE_FAILURE if the last state change failed.
2638 GstStateChangeReturn
2639 gst_element_get_state (GstElement * element,
2640 GstState * state, GstState * pending, GstClockTime timeout)
2642 GstElementClass *oclass;
2643 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2645 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2647 oclass = GST_ELEMENT_GET_CLASS (element);
2649 if (oclass->get_state)
2650 result = (oclass->get_state) (element, state, pending, timeout);
2656 * gst_element_abort_state:
2657 * @element: a #GstElement to abort the state of.
2659 * Abort the state change of the element. This function is used
2660 * by elements that do asynchronous state changes and find out
2661 * something is wrong.
2663 * This function should be called with the STATE_LOCK held.
2668 gst_element_abort_state (GstElement * element)
2672 #ifndef GST_DISABLE_GST_DEBUG
2676 g_return_if_fail (GST_IS_ELEMENT (element));
2678 GST_OBJECT_LOCK (element);
2679 pending = GST_STATE_PENDING (element);
2681 if (pending == GST_STATE_VOID_PENDING ||
2682 GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2683 goto nothing_aborted;
2685 #ifndef GST_DISABLE_GST_DEBUG
2686 old_state = GST_STATE (element);
2688 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2689 "aborting state from %s to %s", gst_element_state_get_name (old_state),
2690 gst_element_state_get_name (pending));
2694 GST_STATE_RETURN (element) = GST_STATE_CHANGE_FAILURE;
2696 GST_STATE_BROADCAST (element);
2697 GST_OBJECT_UNLOCK (element);
2703 GST_OBJECT_UNLOCK (element);
2708 /* Not static because GstBin has manual state handling too */
2710 _priv_gst_element_state_changed (GstElement * element, GstState oldstate,
2711 GstState newstate, GstState pending)
2713 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
2714 GstMessage *message;
2716 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2717 "notifying about state-changed %s to %s (%s pending)",
2718 gst_element_state_get_name (oldstate),
2719 gst_element_state_get_name (newstate),
2720 gst_element_state_get_name (pending));
2722 if (klass->state_changed)
2723 klass->state_changed (element, oldstate, newstate, pending);
2725 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2726 oldstate, newstate, pending);
2727 gst_element_post_message (element, message);
2731 * gst_element_continue_state:
2732 * @element: a #GstElement to continue the state change of.
2733 * @ret: The previous state return value
2735 * Commit the state change of the element and proceed to the next
2736 * pending state if any. This function is used
2737 * by elements that do asynchronous state changes.
2738 * The core will normally call this method automatically when an
2739 * element returned %GST_STATE_CHANGE_SUCCESS from the state change function.
2741 * If after calling this method the element still has not reached
2742 * the pending state, the next state change is performed.
2744 * This method is used internally and should normally not be called by plugins
2747 * This function must be called with STATE_LOCK held.
2749 * Returns: The result of the commit state change.
2753 GstStateChangeReturn
2754 gst_element_continue_state (GstElement * element, GstStateChangeReturn ret)
2756 GstStateChangeReturn old_ret;
2757 GstState old_state, old_next;
2758 GstState current, next, pending;
2759 GstStateChange transition;
2761 GST_OBJECT_LOCK (element);
2762 old_ret = GST_STATE_RETURN (element);
2763 GST_STATE_RETURN (element) = ret;
2764 pending = GST_STATE_PENDING (element);
2766 /* check if there is something to commit */
2767 if (pending == GST_STATE_VOID_PENDING)
2768 goto nothing_pending;
2770 old_state = GST_STATE (element);
2771 /* this is the state we should go to next */
2772 old_next = GST_STATE_NEXT (element);
2773 /* update current state */
2774 current = GST_STATE (element) = old_next;
2776 /* see if we reached the final state */
2777 if (pending == current)
2780 next = GST_STATE_GET_NEXT (current, pending);
2781 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2783 GST_STATE_NEXT (element) = next;
2785 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2786 GST_OBJECT_UNLOCK (element);
2788 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2789 "committing state from %s to %s, pending %s, next %s",
2790 gst_element_state_get_name (old_state),
2791 gst_element_state_get_name (old_next),
2792 gst_element_state_get_name (pending), gst_element_state_get_name (next));
2794 _priv_gst_element_state_changed (element, old_state, old_next, pending);
2796 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2797 "continue state change %s to %s, final %s",
2798 gst_element_state_get_name (current),
2799 gst_element_state_get_name (next), gst_element_state_get_name (pending));
2801 ret = gst_element_change_state (element, transition);
2807 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "nothing pending");
2808 GST_OBJECT_UNLOCK (element);
2813 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2814 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2816 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2817 "completed state change to %s", gst_element_state_get_name (pending));
2818 GST_OBJECT_UNLOCK (element);
2820 /* don't post silly messages with the same state. This can happen
2821 * when an element state is changed to what it already was. For bins
2822 * this can be the result of a lost state, which we check with the
2823 * previous return value.
2824 * We do signal the cond though as a _get_state() might be blocking
2826 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC)
2827 _priv_gst_element_state_changed (element, old_state, old_next,
2828 GST_STATE_VOID_PENDING);
2830 GST_STATE_BROADCAST (element);
2837 * gst_element_lost_state:
2838 * @element: a #GstElement the state is lost of
2840 * Brings the element to the lost state. The current state of the
2841 * element is copied to the pending state so that any call to
2842 * gst_element_get_state() will return %GST_STATE_CHANGE_ASYNC.
2844 * An ASYNC_START message is posted. If the element was PLAYING, it will
2845 * go to PAUSED. The element will be restored to its PLAYING state by
2846 * the parent pipeline when it prerolls again.
2848 * This is mostly used for elements that lost their preroll buffer
2849 * in the %GST_STATE_PAUSED or %GST_STATE_PLAYING state after a flush,
2850 * they will go to their pending state again when a new preroll buffer is
2851 * queued. This function can only be called when the element is currently
2852 * not in error or an async state change.
2854 * This function is used internally and should normally not be called from
2855 * plugins or applications.
2858 gst_element_lost_state (GstElement * element)
2860 GstState old_state, new_state;
2861 GstMessage *message;
2863 g_return_if_fail (GST_IS_ELEMENT (element));
2865 GST_OBJECT_LOCK (element);
2866 if (GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2869 if (GST_STATE_PENDING (element) != GST_STATE_VOID_PENDING)
2870 goto only_async_start;
2872 old_state = GST_STATE (element);
2874 /* when we were PLAYING, the new state is PAUSED. We will also not
2875 * automatically go to PLAYING but let the parent bin(s) set us to PLAYING
2876 * when we preroll. */
2877 if (old_state > GST_STATE_PAUSED)
2878 new_state = GST_STATE_PAUSED;
2880 new_state = old_state;
2882 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2883 "lost state of %s to %s", gst_element_state_get_name (old_state),
2884 gst_element_state_get_name (new_state));
2886 GST_STATE (element) = new_state;
2887 GST_STATE_NEXT (element) = new_state;
2888 GST_STATE_PENDING (element) = new_state;
2889 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2890 GST_OBJECT_UNLOCK (element);
2892 _priv_gst_element_state_changed (element, new_state, new_state, new_state);
2894 message = gst_message_new_async_start (GST_OBJECT_CAST (element));
2895 gst_element_post_message (element, message);
2901 GST_OBJECT_UNLOCK (element);
2906 GST_OBJECT_UNLOCK (element);
2908 message = gst_message_new_async_start (GST_OBJECT_CAST (element));
2909 gst_element_post_message (element, message);
2915 * gst_element_set_state:
2916 * @element: a #GstElement to change state of.
2917 * @state: the element's new #GstState.
2919 * Sets the state of the element. This function will try to set the
2920 * requested state by going through all the intermediary states and calling
2921 * the class's state change function for each.
2923 * This function can return #GST_STATE_CHANGE_ASYNC, in which case the
2924 * element will perform the remainder of the state change asynchronously in
2926 * An application can use gst_element_get_state() to wait for the completion
2927 * of the state change or it can wait for a %GST_MESSAGE_ASYNC_DONE or
2928 * %GST_MESSAGE_STATE_CHANGED on the bus.
2930 * State changes to %GST_STATE_READY or %GST_STATE_NULL never return
2931 * #GST_STATE_CHANGE_ASYNC.
2933 * Returns: Result of the state change using #GstStateChangeReturn.
2937 GstStateChangeReturn
2938 gst_element_set_state (GstElement * element, GstState state)
2940 GstElementClass *oclass;
2941 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2943 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2945 oclass = GST_ELEMENT_GET_CLASS (element);
2947 if (oclass->set_state)
2948 result = (oclass->set_state) (element, state);
2954 * default set state function, calculates the next state based
2955 * on current state and calls the change_state function
2957 static GstStateChangeReturn
2958 gst_element_set_state_func (GstElement * element, GstState state)
2960 GstState current, next, old_pending;
2961 GstStateChangeReturn ret;
2962 GstStateChange transition;
2963 GstStateChangeReturn old_ret;
2965 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2967 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "set_state to %s",
2968 gst_element_state_get_name (state));
2970 /* state lock is taken to protect the set_state() and get_state()
2971 * procedures, it does not lock any variables. */
2972 GST_STATE_LOCK (element);
2974 /* now calculate how to get to the new state */
2975 GST_OBJECT_LOCK (element);
2976 old_ret = GST_STATE_RETURN (element);
2977 /* previous state change returned an error, remove all pending
2978 * and next states */
2979 if (old_ret == GST_STATE_CHANGE_FAILURE) {
2980 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2981 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2982 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
2985 current = GST_STATE (element);
2986 next = GST_STATE_NEXT (element);
2987 old_pending = GST_STATE_PENDING (element);
2989 /* this is the (new) state we should go to. TARGET is the last state we set on
2991 if (state != GST_STATE_TARGET (element)) {
2992 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2993 "setting target state to %s", gst_element_state_get_name (state));
2994 GST_STATE_TARGET (element) = state;
2995 /* increment state cookie so that we can track each state change. We only do
2996 * this if this is actually a new state change. */
2997 element->state_cookie++;
2999 GST_STATE_PENDING (element) = state;
3001 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3002 "current %s, old_pending %s, next %s, old return %s",
3003 gst_element_state_get_name (current),
3004 gst_element_state_get_name (old_pending),
3005 gst_element_state_get_name (next),
3006 gst_element_state_change_return_get_name (old_ret));
3008 /* if the element was busy doing a state change, we just update the
3009 * target state, it'll get to it async then. */
3010 if (old_pending != GST_STATE_VOID_PENDING) {
3011 /* upwards state change will happen ASYNC */
3012 if (old_pending <= state)
3014 /* element is going to this state already */
3015 else if (next == state)
3017 /* element was performing an ASYNC upward state change and
3018 * we request to go downward again. Start from the next pending
3020 else if (next > state
3021 && GST_STATE_RETURN (element) == GST_STATE_CHANGE_ASYNC) {
3025 next = GST_STATE_GET_NEXT (current, state);
3026 /* now we store the next state */
3027 GST_STATE_NEXT (element) = next;
3028 /* mark busy, we need to check that there is actually a state change
3029 * to be done else we could accidentally override SUCCESS/NO_PREROLL and
3030 * the default element change_state function has no way to know what the
3031 * old value was... could consider this a FIXME...*/
3032 if (current != next)
3033 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
3035 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
3037 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3038 "%s: setting state from %s to %s",
3039 (next != state ? "intermediate" : "final"),
3040 gst_element_state_get_name (current), gst_element_state_get_name (next));
3042 /* now signal any waiters, they will error since the cookie was incremented */
3043 GST_STATE_BROADCAST (element);
3045 GST_OBJECT_UNLOCK (element);
3047 ret = gst_element_change_state (element, transition);
3049 GST_STATE_UNLOCK (element);
3051 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "returned %s",
3052 gst_element_state_change_return_get_name (ret));
3058 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
3059 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3060 "element was busy with async state change");
3061 GST_OBJECT_UNLOCK (element);
3063 GST_STATE_UNLOCK (element);
3065 return GST_STATE_CHANGE_ASYNC;
3070 * gst_element_change_state:
3071 * @element: a #GstElement
3072 * @transition: the requested transition
3074 * Perform @transition on @element.
3076 * This function must be called with STATE_LOCK held and is mainly used
3079 * Returns: the #GstStateChangeReturn of the state transition.
3081 GstStateChangeReturn
3082 gst_element_change_state (GstElement * element, GstStateChange transition)
3084 GstElementClass *oclass;
3085 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3087 oclass = GST_ELEMENT_GET_CLASS (element);
3089 GST_TRACER_ELEMENT_CHANGE_STATE_PRE (element, transition);
3091 /* call the state change function so it can set the state */
3092 if (oclass->change_state)
3093 ret = (oclass->change_state) (element, transition);
3095 ret = GST_STATE_CHANGE_FAILURE;
3097 GST_TRACER_ELEMENT_CHANGE_STATE_POST (element, transition, ret);
3100 case GST_STATE_CHANGE_FAILURE:
3101 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
3102 "have FAILURE change_state return");
3103 /* state change failure */
3104 gst_element_abort_state (element);
3106 case GST_STATE_CHANGE_ASYNC:
3110 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3111 "element will change state ASYNC");
3113 target = GST_STATE_TARGET (element);
3115 if (target > GST_STATE_READY)
3118 /* else we just continue the state change downwards */
3119 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
3120 "forcing commit state %s <= %s",
3121 gst_element_state_get_name (target),
3122 gst_element_state_get_name (GST_STATE_READY));
3124 ret = gst_element_continue_state (element, GST_STATE_CHANGE_SUCCESS);
3127 case GST_STATE_CHANGE_SUCCESS:
3128 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3129 "element changed state SUCCESS");
3130 /* we can commit the state now which will proceed to
3132 ret = gst_element_continue_state (element, ret);
3134 case GST_STATE_CHANGE_NO_PREROLL:
3135 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3136 "element changed state NO_PREROLL");
3137 /* we can commit the state now which will proceed to
3139 ret = gst_element_continue_state (element, ret);
3142 goto invalid_return;
3145 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit state change %d", ret);
3150 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit async state change %d",
3158 GST_OBJECT_LOCK (element);
3159 /* somebody added a GST_STATE_ and forgot to do stuff here ! */
3160 g_critical ("%s: unknown return value %d from a state change function",
3161 GST_ELEMENT_NAME (element), ret);
3163 /* we are in error now */
3164 ret = GST_STATE_CHANGE_FAILURE;
3165 GST_STATE_RETURN (element) = ret;
3166 GST_OBJECT_UNLOCK (element);
3172 /* gst_iterator_fold functions for pads_activate
3173 * Stop the iterator if activating one pad failed, but only if that pad
3174 * has not been removed from the element. */
3176 activate_pads (const GValue * vpad, GValue * ret, gboolean * active)
3178 GstPad *pad = g_value_get_object (vpad);
3179 gboolean cont = TRUE;
3181 if (!gst_pad_set_active (pad, *active)) {
3182 if (GST_PAD_PARENT (pad) != NULL) {
3184 g_value_set_boolean (ret, FALSE);
3191 /* returns false on error or early cutout of the fold, true if all
3192 * pads in @iter were (de)activated successfully. */
3194 iterator_activate_fold_with_resync (GstIterator * iter,
3195 GstIteratorFoldFunction func, gpointer user_data)
3197 GstIteratorResult ires;
3200 /* no need to unset this later, it's just a boolean */
3201 g_value_init (&ret, G_TYPE_BOOLEAN);
3202 g_value_set_boolean (&ret, TRUE);
3205 ires = gst_iterator_fold (iter, func, &ret, user_data);
3207 case GST_ITERATOR_RESYNC:
3208 /* need to reset the result again */
3209 g_value_set_boolean (&ret, TRUE);
3210 gst_iterator_resync (iter);
3212 case GST_ITERATOR_DONE:
3213 /* all pads iterated, return collected value */
3216 /* iterator returned _ERROR or premature end with _OK,
3217 * mark an error and exit */
3218 g_value_set_boolean (&ret, FALSE);
3223 /* return collected value */
3224 return g_value_get_boolean (&ret);
3227 /* is called with STATE_LOCK
3229 * Pads are activated from source pads to sinkpads.
3232 gst_element_pads_activate (GstElement * element, gboolean active)
3237 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3238 "%s pads", active ? "activate" : "deactivate");
3240 iter = gst_element_iterate_src_pads (element);
3242 iterator_activate_fold_with_resync (iter,
3243 (GstIteratorFoldFunction) activate_pads, &active);
3244 gst_iterator_free (iter);
3245 if (G_UNLIKELY (!res))
3248 iter = gst_element_iterate_sink_pads (element);
3250 iterator_activate_fold_with_resync (iter,
3251 (GstIteratorFoldFunction) activate_pads, &active);
3252 gst_iterator_free (iter);
3253 if (G_UNLIKELY (!res))
3256 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3257 "pad %sactivation successful", active ? "" : "de");
3264 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3265 "pad %sactivation failed", active ? "" : "de");
3270 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3271 "sink pads_activate failed");
3276 /* is called with STATE_LOCK */
3277 static GstStateChangeReturn
3278 gst_element_change_state_func (GstElement * element, GstStateChange transition)
3280 GstState state, next;
3281 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
3283 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
3285 state = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
3286 next = GST_STATE_TRANSITION_NEXT (transition);
3288 /* if the element already is in the given state, we just return success */
3289 if (next == GST_STATE_VOID_PENDING || state == next)
3292 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element,
3293 "default handler tries setting state from %s to %s (%04x)",
3294 gst_element_state_get_name (state),
3295 gst_element_state_get_name (next), transition);
3297 switch (transition) {
3298 case GST_STATE_CHANGE_NULL_TO_READY:
3300 case GST_STATE_CHANGE_READY_TO_PAUSED:
3301 if (!gst_element_pads_activate (element, TRUE)) {
3302 result = GST_STATE_CHANGE_FAILURE;
3305 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3307 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3309 case GST_STATE_CHANGE_PAUSED_TO_READY:
3310 case GST_STATE_CHANGE_READY_TO_NULL:{
3313 /* deactivate pads in both cases, since they are activated on
3314 ready->paused but the element might not have made it to paused */
3315 if (!gst_element_pads_activate (element, FALSE)) {
3316 result = GST_STATE_CHANGE_FAILURE;
3319 /* Remove all non-persistent contexts */
3320 GST_OBJECT_LOCK (element);
3321 for (l = element->contexts; l;) {
3322 GstContext *context = l->data;
3324 if (!gst_context_is_persistent (context)) {
3327 gst_context_unref (context);
3329 element->contexts = g_list_delete_link (element->contexts, l);
3335 GST_OBJECT_UNLOCK (element);
3339 /* this will catch real but unhandled state changes;
3340 * can only be caused by:
3341 * - a new state was added
3342 * - somehow the element was asked to jump across an intermediate state
3344 g_warning ("Unhandled state change from %s to %s",
3345 gst_element_state_get_name (state),
3346 gst_element_state_get_name (next));
3353 GST_OBJECT_LOCK (element);
3354 result = GST_STATE_RETURN (element);
3355 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3356 "element is already in the %s state",
3357 gst_element_state_get_name (state));
3358 GST_OBJECT_UNLOCK (element);
3365 * gst_element_get_factory:
3366 * @element: a #GstElement to request the element factory of.
3368 * Retrieves the factory that was used to create this element.
3370 * Returns: (transfer none) (nullable): the #GstElementFactory used for creating this
3371 * element or %NULL if element has not been registered (static element). no refcounting is needed.
3374 gst_element_get_factory (GstElement * element)
3376 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3378 return GST_ELEMENT_GET_CLASS (element)->elementfactory;
3382 gst_element_dispose (GObject * object)
3384 GstElement *element = GST_ELEMENT_CAST (object);
3387 GstElementClass *oclass;
3390 oclass = GST_ELEMENT_GET_CLASS (element);
3392 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p dispose", element);
3394 if (GST_STATE (element) != GST_STATE_NULL)
3397 /* start by releasing all request pads, this might also remove some dynamic
3399 walk = element->pads;
3401 GstPad *pad = GST_PAD_CAST (walk->data);
3405 if (oclass->release_pad && GST_PAD_PAD_TEMPLATE (pad) &&
3406 GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (pad))
3407 == GST_PAD_REQUEST) {
3408 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3409 "removing request pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3410 oclass->release_pad (element, pad);
3412 /* in case the release_pad function removed the next pad too */
3413 if (walk && g_list_position (element->pads, walk) == -1)
3414 walk = element->pads;
3417 /* remove the remaining pads */
3418 while (element->pads) {
3419 GstPad *pad = GST_PAD_CAST (element->pads->data);
3420 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3421 "removing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3422 if (!gst_element_remove_pad (element, pad)) {
3423 /* only happens when someone unparented our pad.. */
3424 g_critical ("failed to remove pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3429 GST_OBJECT_LOCK (element);
3430 clock_p = &element->clock;
3431 bus_p = &element->bus;
3432 gst_object_replace ((GstObject **) clock_p, NULL);
3433 gst_object_replace ((GstObject **) bus_p, NULL);
3434 g_list_free_full (element->contexts, (GDestroyNotify) gst_context_unref);
3435 element->contexts = NULL;
3436 GST_OBJECT_UNLOCK (element);
3438 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p parent class dispose",
3441 G_OBJECT_CLASS (parent_class)->dispose (object);
3450 is_locked = GST_ELEMENT_IS_LOCKED_STATE (element);
3452 ("\nTrying to dispose element %s, but it is in %s%s instead of the NULL"
3454 "You need to explicitly set elements to the NULL state before\n"
3455 "dropping the final reference, to allow them to clean up.\n"
3456 "This problem may also be caused by a refcounting bug in the\n"
3457 "application or some element.\n",
3458 GST_OBJECT_NAME (element),
3459 gst_element_state_get_name (GST_STATE (element)),
3460 is_locked ? " (locked)" : "");
3466 gst_element_finalize (GObject * object)
3468 GstElement *element = GST_ELEMENT_CAST (object);
3470 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p finalize", element);
3472 g_cond_clear (&element->state_cond);
3473 g_rec_mutex_clear (&element->state_lock);
3475 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p finalize parent",
3478 G_OBJECT_CLASS (parent_class)->finalize (object);
3482 gst_element_set_bus_func (GstElement * element, GstBus * bus)
3486 g_return_if_fail (GST_IS_ELEMENT (element));
3488 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element, "setting bus to %p", bus);
3490 GST_OBJECT_LOCK (element);
3491 bus_p = &GST_ELEMENT_BUS (element);
3492 gst_object_replace ((GstObject **) bus_p, GST_OBJECT_CAST (bus));
3493 GST_OBJECT_UNLOCK (element);
3497 * gst_element_set_bus:
3498 * @element: a #GstElement to set the bus of.
3499 * @bus: (transfer none) (allow-none): the #GstBus to set.
3501 * Sets the bus of the element. Increases the refcount on the bus.
3502 * For internal use only, unless you're testing elements.
3507 gst_element_set_bus (GstElement * element, GstBus * bus)
3509 GstElementClass *oclass;
3511 g_return_if_fail (GST_IS_ELEMENT (element));
3513 oclass = GST_ELEMENT_GET_CLASS (element);
3515 if (oclass->set_bus)
3516 oclass->set_bus (element, bus);
3520 * gst_element_get_bus:
3521 * @element: a #GstElement to get the bus of.
3523 * Returns the bus of the element. Note that only a #GstPipeline will provide a
3524 * bus for the application.
3526 * Returns: (transfer full) (nullable): the element's #GstBus. unref after
3532 gst_element_get_bus (GstElement * element)
3534 GstBus *result = NULL;
3536 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
3538 GST_OBJECT_LOCK (element);
3539 if ((result = GST_ELEMENT_BUS (element)))
3540 gst_object_ref (result);
3541 GST_OBJECT_UNLOCK (element);
3543 GST_CAT_DEBUG_OBJECT (GST_CAT_BUS, element, "got bus %" GST_PTR_FORMAT,
3550 gst_element_set_context_default (GstElement * element, GstContext * context)
3552 const gchar *context_type;
3555 g_return_if_fail (GST_IS_CONTEXT (context));
3556 context_type = gst_context_get_context_type (context);
3557 g_return_if_fail (context_type != NULL);
3559 GST_OBJECT_LOCK (element);
3560 for (l = element->contexts; l; l = l->next) {
3561 GstContext *tmp = l->data;
3562 const gchar *tmp_type = gst_context_get_context_type (tmp);
3564 /* Always store newest context but never replace
3565 * a persistent one by a non-persistent one */
3566 if (g_strcmp0 (context_type, tmp_type) == 0 &&
3567 (gst_context_is_persistent (context) ||
3568 !gst_context_is_persistent (tmp))) {
3569 gst_context_replace ((GstContext **) & l->data, context);
3573 /* Not found? Add */
3576 g_list_prepend (element->contexts, gst_context_ref (context));
3578 GST_OBJECT_UNLOCK (element);
3582 * gst_element_set_context:
3583 * @element: a #GstElement to set the context of.
3584 * @context: (transfer none): the #GstContext to set.
3586 * Sets the context of the element. Increases the refcount of the context.
3591 gst_element_set_context (GstElement * element, GstContext * context)
3593 GstElementClass *oclass;
3595 g_return_if_fail (GST_IS_ELEMENT (element));
3596 g_return_if_fail (GST_IS_CONTEXT (context));
3598 oclass = GST_ELEMENT_GET_CLASS (element);
3600 GST_CAT_DEBUG_OBJECT (GST_CAT_CONTEXT, element,
3601 "set context %p %" GST_PTR_FORMAT, context,
3602 gst_context_get_structure (context));
3604 if (oclass->set_context)
3605 oclass->set_context (element, context);
3609 * gst_element_get_contexts:
3610 * @element: a #GstElement to set the context of.
3612 * Gets the contexts set on the element.
3616 * Returns: (element-type Gst.Context) (transfer full): List of #GstContext
3621 gst_element_get_contexts (GstElement * element)
3625 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3627 GST_OBJECT_LOCK (element);
3628 ret = g_list_copy_deep (element->contexts, (GCopyFunc) gst_context_ref, NULL);
3629 GST_OBJECT_UNLOCK (element);
3635 _match_context_type (GstContext * c1, const gchar * context_type)
3637 const gchar *c1_type;
3639 c1_type = gst_context_get_context_type (c1);
3641 return g_strcmp0 (c1_type, context_type);
3645 * gst_element_get_context_unlocked:
3646 * @element: a #GstElement to get the context of.
3647 * @context_type: a name of a context to retrieve
3649 * Gets the context with @context_type set on the element or NULL.
3651 * Returns: (transfer full) (nullable): A #GstContext or NULL
3656 gst_element_get_context_unlocked (GstElement * element,
3657 const gchar * context_type)
3659 GstContext *ret = NULL;
3662 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3665 g_list_find_custom (element->contexts, context_type,
3666 (GCompareFunc) _match_context_type);
3667 if (node && node->data)
3668 ret = gst_context_ref (node->data);
3674 * gst_element_get_context:
3675 * @element: a #GstElement to get the context of.
3676 * @context_type: a name of a context to retrieve
3678 * Gets the context with @context_type set on the element or NULL.
3682 * Returns: (transfer full) (nullable): A #GstContext or NULL
3687 gst_element_get_context (GstElement * element, const gchar * context_type)
3689 GstContext *ret = NULL;
3691 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3693 GST_OBJECT_LOCK (element);
3694 ret = gst_element_get_context_unlocked (element, context_type);
3695 GST_OBJECT_UNLOCK (element);
3701 gst_element_property_post_notify_msg (GstElement * element, GObject * obj,
3702 GParamSpec * pspec, gboolean include_value)
3704 GValue val = G_VALUE_INIT;
3707 GST_LOG_OBJECT (element, "property '%s' of object %" GST_PTR_FORMAT " has "
3708 "changed, posting message with%s value", pspec->name, obj,
3709 include_value ? "" : "out");
3711 if (include_value && (pspec->flags & G_PARAM_READABLE) != 0) {
3712 g_value_init (&val, pspec->value_type);
3713 g_object_get_property (obj, pspec->name, &val);
3718 gst_element_post_message (element,
3719 gst_message_new_property_notify (GST_OBJECT_CAST (obj), pspec->name, v));
3723 gst_element_property_deep_notify_cb (GstElement * element, GObject * prop_obj,
3724 GParamSpec * pspec, gpointer user_data)
3726 gboolean include_value = GPOINTER_TO_INT (user_data);
3728 gst_element_property_post_notify_msg (element, prop_obj, pspec,
3733 gst_element_property_notify_cb (GObject * obj, GParamSpec * pspec,
3736 gboolean include_value = GPOINTER_TO_INT (user_data);
3738 gst_element_property_post_notify_msg (GST_ELEMENT_CAST (obj), obj, pspec,
3743 * gst_element_add_property_notify_watch:
3744 * @element: a #GstElement to watch for property changes
3745 * @property_name: (allow-none): name of property to watch for changes, or
3746 * NULL to watch all properties
3747 * @include_value: whether to include the new property value in the message
3749 * Returns: a watch id, which can be used in connection with
3750 * gst_element_remove_property_notify_watch() to remove the watch again.
3755 gst_element_add_property_notify_watch (GstElement * element,
3756 const gchar * property_name, gboolean include_value)
3762 g_return_val_if_fail (GST_IS_ELEMENT (element), 0);
3764 sep = (property_name != NULL) ? "::" : NULL;
3765 signal_name = g_strconcat ("notify", sep, property_name, NULL);
3766 id = g_signal_connect (element, signal_name,
3767 G_CALLBACK (gst_element_property_notify_cb),
3768 GINT_TO_POINTER (include_value));
3769 g_free (signal_name);
3775 * gst_element_add_property_deep_notify_watch:
3776 * @element: a #GstElement to watch (recursively) for property changes
3777 * @property_name: (allow-none): name of property to watch for changes, or
3778 * NULL to watch all properties
3779 * @include_value: whether to include the new property value in the message
3781 * Returns: a watch id, which can be used in connection with
3782 * gst_element_remove_property_notify_watch() to remove the watch again.
3787 gst_element_add_property_deep_notify_watch (GstElement * element,
3788 const gchar * property_name, gboolean include_value)
3794 g_return_val_if_fail (GST_IS_ELEMENT (element), 0);
3796 sep = (property_name != NULL) ? "::" : NULL;
3797 signal_name = g_strconcat ("deep-notify", sep, property_name, NULL);
3798 id = g_signal_connect (element, signal_name,
3799 G_CALLBACK (gst_element_property_deep_notify_cb),
3800 GINT_TO_POINTER (include_value));
3801 g_free (signal_name);
3807 * gst_element_remove_property_notify_watch:
3808 * @element: a #GstElement being watched for property changes
3809 * @watch_id: watch id to remove
3814 gst_element_remove_property_notify_watch (GstElement * element, gulong watch_id)
3816 g_signal_handler_disconnect (element, watch_id);
3821 GstElement *element;
3822 GstElementCallAsyncFunc func;
3824 GDestroyNotify destroy_notify;
3825 } GstElementCallAsyncData;
3828 gst_element_call_async_func (gpointer data, gpointer user_data)
3830 GstElementCallAsyncData *async_data = data;
3832 async_data->func (async_data->element, async_data->user_data);
3833 if (async_data->destroy_notify)
3834 async_data->destroy_notify (async_data->user_data);
3835 gst_object_unref (async_data->element);
3836 g_free (async_data);
3840 * gst_element_call_async:
3841 * @element: a #GstElement
3842 * @func: Function to call asynchronously from another thread
3843 * @user_data: Data to pass to @func
3844 * @destroy_notify: GDestroyNotify for @user_data
3846 * Calls @func from another thread and passes @user_data to it. This is to be
3847 * used for cases when a state change has to be performed from a streaming
3848 * thread, directly via gst_element_set_state() or indirectly e.g. via SEEK
3851 * Calling those functions directly from the streaming thread will cause
3852 * deadlocks in many situations, as they might involve waiting for the
3853 * streaming thread to shut down from this very streaming thread.
3860 gst_element_call_async (GstElement * element, GstElementCallAsyncFunc func,
3861 gpointer user_data, GDestroyNotify destroy_notify)
3863 GstElementCallAsyncData *async_data;
3865 g_return_if_fail (GST_IS_ELEMENT (element));
3867 async_data = g_new0 (GstElementCallAsyncData, 1);
3868 async_data->element = gst_object_ref (element);
3869 async_data->func = func;
3870 async_data->user_data = user_data;
3871 async_data->destroy_notify = destroy_notify;
3873 g_mutex_lock (&_element_pool_lock);
3874 if (G_UNLIKELY (gst_element_pool == NULL))
3875 gst_element_pool = gst_element_setup_thread_pool ();
3876 g_thread_pool_push ((GThreadPool *) gst_element_pool, async_data, NULL);
3877 g_mutex_unlock (&_element_pool_lock);
3881 _priv_gst_element_cleanup (void)
3883 g_mutex_lock (&_element_pool_lock);
3884 if (gst_element_pool) {
3885 g_thread_pool_free ((GThreadPool *) gst_element_pool, FALSE, TRUE);
3886 gst_element_pool = NULL;
3888 g_mutex_unlock (&_element_pool_lock);
3892 * gst_make_element_message_details:
3893 * @name: Name of the first field to set
3894 * @...: variable arguments in the same form as #GstStructure
3896 * Create a #GstStructure to be used with #gst_element_message_full_with_details.
3897 * %NULL terminator required.
3902 gst_make_element_message_details (const char *name, ...)
3904 GstStructure *structure;
3910 va_start (varargs, name);
3911 structure = gst_structure_new_valist ("details", name, varargs);