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 "gst-i18n-lib.h"
105 #include "glib-compat-private.h"
107 #ifndef GST_DISABLE_GST_DEBUG
108 #include "printf/printf.h"
111 /* Element signals and args */
127 static void gst_element_class_init (GstElementClass * klass);
128 static void gst_element_init (GstElement * element);
129 static void gst_element_base_class_init (gpointer g_class);
131 static void gst_element_constructed (GObject * object);
132 static void gst_element_dispose (GObject * object);
133 static void gst_element_finalize (GObject * object);
135 static GstStateChangeReturn gst_element_change_state_func (GstElement * element,
136 GstStateChange transition);
137 static GstStateChangeReturn gst_element_get_state_func (GstElement * element,
138 GstState * state, GstState * pending, GstClockTime timeout);
139 static GstStateChangeReturn gst_element_set_state_func (GstElement * element,
141 static gboolean gst_element_set_clock_func (GstElement * element,
143 static void gst_element_set_bus_func (GstElement * element, GstBus * bus);
144 static gboolean gst_element_post_message_default (GstElement * element,
145 GstMessage * message);
146 static void gst_element_set_context_default (GstElement * element,
147 GstContext * context);
149 static gboolean gst_element_default_send_event (GstElement * element,
151 static gboolean gst_element_default_query (GstElement * element,
154 static GstPadTemplate
155 * gst_element_class_request_pad_simple_template (GstElementClass *
156 element_class, const gchar * name);
158 static void gst_element_call_async_func (gpointer data, gpointer user_data);
160 static GstObjectClass *parent_class = NULL;
161 static guint gst_element_signals[LAST_SIGNAL] = { 0 };
163 static GMutex _element_pool_lock;
164 static GThreadPool *gst_element_pool = NULL;
166 /* this is used in gstelementfactory.c:gst_element_register() */
167 GQuark __gst_elementclass_factory = 0;
169 /* used for gst_element_type_set_skip_documentation() and
170 * gst_element_factory_get_skip_documentation() */
171 GQuark __gst_elementclass_skip_doc = 0;
174 gst_element_get_type (void)
176 static gsize gst_element_type = 0;
178 if (g_once_init_enter (&gst_element_type)) {
180 static const GTypeInfo element_info = {
181 sizeof (GstElementClass),
182 gst_element_base_class_init,
183 NULL, /* base_class_finalize */
184 (GClassInitFunc) gst_element_class_init,
189 (GInstanceInitFunc) gst_element_init,
193 _type = g_type_register_static (GST_TYPE_OBJECT, "GstElement",
194 &element_info, G_TYPE_FLAG_ABSTRACT);
196 __gst_elementclass_factory =
197 g_quark_from_static_string ("GST_ELEMENTCLASS_FACTORY");
198 __gst_elementclass_skip_doc =
199 g_quark_from_static_string ("GST_ELEMENTCLASS_SKIP_DOCUMENTATION");
200 g_once_init_leave (&gst_element_type, _type);
202 return gst_element_type;
206 gst_element_setup_thread_pool (void)
211 GST_DEBUG ("creating element thread pool");
213 g_thread_pool_new ((GFunc) gst_element_call_async_func, NULL, -1, FALSE,
216 g_critical ("could not alloc threadpool %s", err->message);
217 g_clear_error (&err);
224 gst_element_class_init (GstElementClass * klass)
226 GObjectClass *gobject_class;
228 gobject_class = (GObjectClass *) klass;
230 parent_class = g_type_class_peek_parent (klass);
233 * GstElement::pad-added:
234 * @gstelement: the object which received the signal
235 * @new_pad: the pad that has been added
237 * a new #GstPad has been added to the element. Note that this signal will
238 * usually be emitted from the context of the streaming thread. Also keep in
239 * mind that if you add new elements to the pipeline in the signal handler
240 * you will need to set them to the desired target state with
241 * gst_element_set_state() or gst_element_sync_state_with_parent().
243 gst_element_signals[PAD_ADDED] =
244 g_signal_new ("pad-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
245 G_STRUCT_OFFSET (GstElementClass, pad_added), NULL, NULL,
246 NULL, G_TYPE_NONE, 1, GST_TYPE_PAD);
248 * GstElement::pad-removed:
249 * @gstelement: the object which received the signal
250 * @old_pad: the pad that has been removed
252 * a #GstPad has been removed from the element
254 gst_element_signals[PAD_REMOVED] =
255 g_signal_new ("pad-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
256 G_STRUCT_OFFSET (GstElementClass, pad_removed), NULL, NULL,
257 NULL, G_TYPE_NONE, 1, GST_TYPE_PAD);
259 * GstElement::no-more-pads:
260 * @gstelement: the object which received the signal
262 * This signals that the element will not generate more dynamic pads.
263 * Note that this signal will usually be emitted from the context of
264 * the streaming thread.
266 gst_element_signals[NO_MORE_PADS] =
267 g_signal_new ("no-more-pads", G_TYPE_FROM_CLASS (klass),
268 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstElementClass, no_more_pads), NULL,
269 NULL, NULL, G_TYPE_NONE, 0);
271 gobject_class->dispose = gst_element_dispose;
272 gobject_class->finalize = gst_element_finalize;
273 gobject_class->constructed = gst_element_constructed;
275 klass->change_state = GST_DEBUG_FUNCPTR (gst_element_change_state_func);
276 klass->set_state = GST_DEBUG_FUNCPTR (gst_element_set_state_func);
277 klass->get_state = GST_DEBUG_FUNCPTR (gst_element_get_state_func);
278 klass->set_clock = GST_DEBUG_FUNCPTR (gst_element_set_clock_func);
279 klass->set_bus = GST_DEBUG_FUNCPTR (gst_element_set_bus_func);
280 klass->query = GST_DEBUG_FUNCPTR (gst_element_default_query);
281 klass->send_event = GST_DEBUG_FUNCPTR (gst_element_default_send_event);
282 klass->numpadtemplates = 0;
283 klass->post_message = GST_DEBUG_FUNCPTR (gst_element_post_message_default);
284 klass->set_context = GST_DEBUG_FUNCPTR (gst_element_set_context_default);
286 klass->elementfactory = NULL;
290 gst_element_base_class_init (gpointer g_class)
292 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
293 GList *node, *padtemplates;
295 /* Copy the element details here so elements can inherit the
296 * details from their base class and classes only need to set
297 * the details in class_init instead of base_init */
298 element_class->metadata =
299 element_class->metadata ? gst_structure_copy (element_class->metadata) :
300 gst_structure_new_empty ("metadata");
302 /* Copy the pad templates so elements inherit them
303 * from their base class but elements can add pad templates in class_init
304 * instead of base_init.
306 padtemplates = g_list_copy (element_class->padtemplates);
307 for (node = padtemplates; node != NULL; node = node->next) {
308 GstPadTemplate *tmpl = (GstPadTemplate *) node->data;
309 gst_object_ref (tmpl);
311 element_class->padtemplates = padtemplates;
313 /* set the factory, see gst_element_register() */
314 element_class->elementfactory =
315 g_type_get_qdata (G_TYPE_FROM_CLASS (element_class),
316 __gst_elementclass_factory);
317 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "type %s : factory %p",
318 G_OBJECT_CLASS_NAME (element_class), element_class->elementfactory);
322 gst_element_init (GstElement * element)
324 GST_STATE (element) = GST_STATE_NULL;
325 GST_STATE_TARGET (element) = GST_STATE_NULL;
326 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
327 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
328 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
330 g_rec_mutex_init (&element->state_lock);
331 g_cond_init (&element->state_cond);
335 gst_element_constructed (GObject * object)
337 GST_TRACER_ELEMENT_NEW (GST_ELEMENT_CAST (object));
338 G_OBJECT_CLASS (parent_class)->constructed (object);
342 * gst_element_release_request_pad:
343 * @element: a #GstElement to release the request pad of.
344 * @pad: the #GstPad to release.
346 * Makes the element free the previously requested pad as obtained
347 * with gst_element_request_pad().
349 * This does not unref the pad. If the pad was created by using
350 * gst_element_request_pad(), gst_element_release_request_pad() needs to be
351 * followed by gst_object_unref() to free the @pad.
356 gst_element_release_request_pad (GstElement * element, GstPad * pad)
358 GstElementClass *oclass;
360 g_return_if_fail (GST_IS_ELEMENT (element));
361 g_return_if_fail (GST_IS_PAD (pad));
362 g_return_if_fail (GST_PAD_PAD_TEMPLATE (pad) == NULL ||
363 GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (pad)) ==
365 g_return_if_fail (GST_PAD_PARENT (pad) == element);
367 oclass = GST_ELEMENT_GET_CLASS (element);
369 /* if the element implements a custom release function we call that, else we
370 * simply remove the pad from the element */
371 if (oclass->release_pad)
372 oclass->release_pad (element, pad);
374 gst_element_remove_pad (element, pad);
378 * gst_element_provide_clock:
379 * @element: a #GstElement to query
381 * Get the clock provided by the given element.
382 * > An element is only required to provide a clock in the PAUSED
383 * > state. Some elements can provide a clock in other states.
385 * Returns: (transfer full) (nullable): the GstClock provided by the
386 * element or %NULL if no clock could be provided. Unref after usage.
391 gst_element_provide_clock (GstElement * element)
393 GstClock *result = NULL;
394 GstElementClass *oclass;
396 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
398 oclass = GST_ELEMENT_GET_CLASS (element);
400 if (oclass->provide_clock)
401 result = oclass->provide_clock (element);
407 gst_element_set_clock_func (GstElement * element, GstClock * clock)
411 GST_OBJECT_LOCK (element);
412 clock_p = &element->clock;
413 gst_object_replace ((GstObject **) clock_p, (GstObject *) clock);
414 GST_OBJECT_UNLOCK (element);
420 * gst_element_set_clock:
421 * @element: a #GstElement to set the clock for.
422 * @clock: (transfer none) (allow-none): the #GstClock to set for the element.
424 * Sets the clock for the element. This function increases the
425 * refcount on the clock. Any previously set clock on the object
428 * Returns: %TRUE if the element accepted the clock. An element can refuse a
429 * clock when it, for example, is not able to slave its internal clock to the
430 * @clock or when it requires a specific clock to operate.
435 gst_element_set_clock (GstElement * element, GstClock * clock)
437 GstElementClass *oclass;
438 gboolean res = FALSE;
440 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
441 g_return_val_if_fail (clock == NULL || GST_IS_CLOCK (clock), FALSE);
443 oclass = GST_ELEMENT_GET_CLASS (element);
445 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element, "setting clock %p", clock);
447 if (oclass->set_clock)
448 res = oclass->set_clock (element, clock);
454 * gst_element_get_clock:
455 * @element: a #GstElement to get the clock of.
457 * Gets the currently configured clock of the element. This is the clock as was
458 * last set with gst_element_set_clock().
460 * Elements in a pipeline will only have their clock set when the
461 * pipeline is in the PLAYING state.
463 * Returns: (transfer full) (nullable): the #GstClock of the element. unref after usage.
468 gst_element_get_clock (GstElement * element)
472 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
474 GST_OBJECT_LOCK (element);
475 if ((result = element->clock))
476 gst_object_ref (result);
477 GST_OBJECT_UNLOCK (element);
483 * gst_element_set_base_time:
484 * @element: a #GstElement.
485 * @time: the base time to set.
487 * Set the base time of an element. See gst_element_get_base_time().
492 gst_element_set_base_time (GstElement * element, GstClockTime time)
496 g_return_if_fail (GST_IS_ELEMENT (element));
497 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (time));
499 GST_OBJECT_LOCK (element);
500 old = element->base_time;
501 element->base_time = time;
502 GST_OBJECT_UNLOCK (element);
504 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element,
505 "set base_time=%" GST_TIME_FORMAT ", old %" GST_TIME_FORMAT,
506 GST_TIME_ARGS (time), GST_TIME_ARGS (old));
510 * gst_element_get_base_time:
511 * @element: a #GstElement.
513 * Returns the base time of the element. The base time is the
514 * absolute time of the clock when this element was last put to
515 * PLAYING. Subtracting the base time from the clock time gives
516 * the running time of the element.
518 * Returns: the base time of the element.
523 gst_element_get_base_time (GstElement * element)
527 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
529 GST_OBJECT_LOCK (element);
530 result = element->base_time;
531 GST_OBJECT_UNLOCK (element);
537 * gst_element_set_start_time:
538 * @element: a #GstElement.
539 * @time: the base time to set.
541 * Set the start time of an element. The start time of the element is the
542 * running time of the element when it last went to the PAUSED state. In READY
543 * or after a flushing seek, it is set to 0.
545 * Toplevel elements like #GstPipeline will manage the start_time and
546 * base_time on its children. Setting the start_time to #GST_CLOCK_TIME_NONE
547 * on such a toplevel element will disable the distribution of the base_time to
548 * the children and can be useful if the application manages the base_time
549 * itself, for example if you want to synchronize capture from multiple
550 * pipelines, and you can also ensure that the pipelines have the same clock.
555 gst_element_set_start_time (GstElement * element, GstClockTime time)
559 g_return_if_fail (GST_IS_ELEMENT (element));
561 GST_OBJECT_LOCK (element);
562 old = GST_ELEMENT_START_TIME (element);
563 GST_ELEMENT_START_TIME (element) = time;
564 GST_OBJECT_UNLOCK (element);
566 GST_CAT_DEBUG_OBJECT (GST_CAT_CLOCK, element,
567 "set start_time=%" GST_TIME_FORMAT ", old %" GST_TIME_FORMAT,
568 GST_TIME_ARGS (time), GST_TIME_ARGS (old));
572 * gst_element_get_start_time:
573 * @element: a #GstElement.
575 * Returns the start time of the element. The start time is the
576 * running time of the clock when this element was last put to PAUSED.
578 * Usually the start_time is managed by a toplevel element such as
583 * Returns: the start time of the element.
586 gst_element_get_start_time (GstElement * element)
590 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
592 GST_OBJECT_LOCK (element);
593 result = GST_ELEMENT_START_TIME (element);
594 GST_OBJECT_UNLOCK (element);
600 * gst_element_get_current_running_time:
601 * @element: a #GstElement.
603 * Returns the running time of the element. The running time is the
604 * element's clock time minus its base time. Will return GST_CLOCK_TIME_NONE
605 * if the element has no clock, or if its base time has not been set.
607 * Returns: the running time of the element, or GST_CLOCK_TIME_NONE if the
608 * element has no clock or its base time has not been set.
613 gst_element_get_current_running_time (GstElement * element)
615 GstClockTime base_time, clock_time;
617 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
619 base_time = gst_element_get_base_time (element);
621 if (!GST_CLOCK_TIME_IS_VALID (base_time)) {
622 GST_DEBUG_OBJECT (element, "Could not determine base time");
623 return GST_CLOCK_TIME_NONE;
626 clock_time = gst_element_get_current_clock_time (element);
628 if (!GST_CLOCK_TIME_IS_VALID (clock_time)) {
629 return GST_CLOCK_TIME_NONE;
632 if (clock_time < base_time) {
633 GST_DEBUG_OBJECT (element, "Got negative current running time");
634 return GST_CLOCK_TIME_NONE;
637 return clock_time - base_time;
641 * gst_element_get_current_clock_time:
642 * @element: a #GstElement.
644 * Returns the current clock time of the element, as in, the time of the
645 * element's clock, or GST_CLOCK_TIME_NONE if there is no clock.
647 * Returns: the clock time of the element, or GST_CLOCK_TIME_NONE if there is
653 gst_element_get_current_clock_time (GstElement * element)
655 GstClock *clock = NULL;
658 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
660 clock = gst_element_get_clock (element);
663 GST_DEBUG_OBJECT (element, "Element has no clock");
664 return GST_CLOCK_TIME_NONE;
667 ret = gst_clock_get_time (clock);
668 gst_object_unref (clock);
675 * gst_element_set_index:
676 * @element: a #GstElement.
677 * @index: (transfer none): a #GstIndex.
679 * Set @index on the element. The refcount of the index
680 * will be increased, any previously set index is unreffed.
685 gst_element_set_index (GstElement * element, GstIndex * index)
687 GstElementClass *oclass;
689 g_return_if_fail (GST_IS_ELEMENT (element));
690 g_return_if_fail (index == NULL || GST_IS_INDEX (index));
692 oclass = GST_ELEMENT_GET_CLASS (element);
694 if (oclass->set_index)
695 oclass->set_index (element, index);
699 * gst_element_get_index:
700 * @element: a #GstElement.
702 * Gets the index from the element.
704 * Returns: (transfer full) (nullable): a #GstIndex or %NULL when no
705 * index was set on the element. unref after usage.
710 gst_element_get_index (GstElement * element)
712 GstElementClass *oclass;
713 GstIndex *result = NULL;
715 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
717 oclass = GST_ELEMENT_GET_CLASS (element);
719 if (oclass->get_index)
720 result = oclass->get_index (element);
727 * gst_element_add_pad:
728 * @element: a #GstElement to add the pad to.
729 * @pad: (transfer floating): the #GstPad to add to the element.
731 * Adds a pad (link point) to @element. @pad's parent will be set to @element;
732 * see gst_object_set_parent() for refcounting information.
734 * Pads are automatically activated when added in the PAUSED or PLAYING
737 * The pad and the element should be unlocked when calling this function.
739 * This function will emit the #GstElement::pad-added signal on the element.
741 * Returns: %TRUE if the pad could be added. This function can fail when
742 * a pad with the same name already existed or the pad already had another
748 gst_element_add_pad (GstElement * element, GstPad * pad)
753 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
754 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
756 /* locking pad to look at the name */
757 GST_OBJECT_LOCK (pad);
758 pad_name = g_strdup (GST_PAD_NAME (pad));
759 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "adding pad '%s'",
760 GST_STR_NULL (pad_name));
761 active = GST_PAD_IS_ACTIVE (pad);
762 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_PARENT);
763 GST_OBJECT_UNLOCK (pad);
765 /* then check to see if there's already a pad by that name here */
766 GST_OBJECT_LOCK (element);
767 if (G_UNLIKELY (!gst_object_check_uniqueness (element->pads, pad_name)))
770 /* try to set the pad's parent */
771 if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (pad),
772 GST_OBJECT_CAST (element))))
775 /* check for active pads */
776 if (!active && (GST_STATE (element) > GST_STATE_READY ||
777 GST_STATE_NEXT (element) == GST_STATE_PAUSED)) {
778 gst_pad_set_active (pad, TRUE);
783 /* add it to the list */
784 switch (gst_pad_get_direction (pad)) {
786 element->srcpads = g_list_append (element->srcpads, pad);
787 element->numsrcpads++;
790 element->sinkpads = g_list_append (element->sinkpads, pad);
791 element->numsinkpads++;
796 element->pads = g_list_append (element->pads, pad);
798 element->pads_cookie++;
799 GST_OBJECT_UNLOCK (element);
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' && g_strcmp0 (templ_name, name) == 0) {
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 if (oclass->request_new_pad)
1169 newpad = (oclass->request_new_pad) (element, templ, name, caps);
1172 gst_object_ref (newpad);
1177 #ifndef GST_REMOVE_DEPRECATED
1179 * gst_element_get_request_pad:
1180 * @element: a #GstElement to find a request pad of.
1181 * @name: the name of the request #GstPad to retrieve.
1183 * The name of this function is confusing to people learning GStreamer.
1184 * gst_element_request_pad_simple() aims at making it more explicit it is
1185 * a simplified gst_element_request_pad().
1187 * Deprecated: 1.20: Prefer using gst_element_request_pad_simple() which
1188 * provides the exact same functionality.
1190 * Returns: (transfer full) (nullable): requested #GstPad if found,
1191 * otherwise %NULL. Release after usage.
1194 gst_element_get_request_pad (GstElement * element, const gchar * name)
1196 return gst_element_request_pad_simple (element, name);
1201 * gst_element_request_pad_simple:
1202 * @element: a #GstElement to find a request pad of.
1203 * @name: the name of the request #GstPad to retrieve.
1205 * Retrieves a pad from the element by name (e.g. "src_\%d"). This version only
1206 * retrieves request pads. The pad should be released with
1207 * gst_element_release_request_pad().
1209 * This method is slower than manually getting the pad template and calling
1210 * gst_element_request_pad() if the pads should have a specific name (e.g.
1211 * @name is "src_1" instead of "src_\%u").
1213 * Note that this function was introduced in GStreamer 1.20 in order to provide
1214 * a better name to gst_element_get_request_pad(). Prior to 1.20, users
1215 * should use gst_element_get_request_pad() which provides the same
1218 * Returns: (transfer full) (nullable): requested #GstPad if found,
1219 * otherwise %NULL. Release after usage.
1224 gst_element_request_pad_simple (GstElement * element, const gchar * name)
1226 GstPadTemplate *templ = NULL;
1228 const gchar *req_name = NULL;
1229 gboolean templ_found = FALSE;
1231 GstElementClass *class;
1233 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1234 g_return_val_if_fail (name != NULL, NULL);
1236 class = GST_ELEMENT_GET_CLASS (element);
1238 templ = gst_element_class_request_pad_simple_template (class, name);
1240 req_name = strstr (name, "%") ? NULL : name;
1243 /* there is no % in the name, try to find a matching template */
1244 list = class->padtemplates;
1245 while (!templ_found && list) {
1246 templ = (GstPadTemplate *) list->data;
1247 if (templ->presence == GST_PAD_REQUEST) {
1248 GST_CAT_DEBUG (GST_CAT_PADS, "comparing %s to %s", name,
1249 templ->name_template);
1250 if (gst_element_is_valid_request_template_name (templ->name_template,
1264 pad = _gst_element_request_pad (element, templ, req_name, NULL);
1270 * gst_element_request_pad: (virtual request_new_pad)
1271 * @element: a #GstElement to find a request pad of.
1272 * @templ: a #GstPadTemplate of which we want a pad of.
1273 * @name: (transfer none) (allow-none): the name of the request #GstPad
1274 * to retrieve. Can be %NULL.
1275 * @caps: (transfer none) (allow-none): the caps of the pad we want to
1276 * request. Can be %NULL.
1278 * Retrieves a request pad from the element according to the provided template.
1279 * Pad templates can be looked up using
1280 * gst_element_factory_get_static_pad_templates().
1282 * The pad should be released with gst_element_release_request_pad().
1284 * Returns: (transfer full) (nullable): requested #GstPad if found,
1285 * otherwise %NULL. Release after usage.
1288 gst_element_request_pad (GstElement * element,
1289 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
1291 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1292 g_return_val_if_fail (templ != NULL, NULL);
1293 g_return_val_if_fail (templ->presence == GST_PAD_REQUEST, NULL);
1295 return _gst_element_request_pad (element, templ, name, caps);
1298 static GstIterator *
1299 gst_element_iterate_pad_list (GstElement * element, GList ** padlist)
1301 GstIterator *result;
1303 GST_OBJECT_LOCK (element);
1304 result = gst_iterator_new_list (GST_TYPE_PAD,
1305 GST_OBJECT_GET_LOCK (element),
1306 &element->pads_cookie, padlist, (GObject *) element, NULL);
1307 GST_OBJECT_UNLOCK (element);
1313 * gst_element_iterate_pads:
1314 * @element: a #GstElement to iterate pads of.
1316 * Retrieves an iterator of @element's pads. The iterator should
1317 * be freed after usage. Also more specialized iterators exists such as
1318 * gst_element_iterate_src_pads() or gst_element_iterate_sink_pads().
1320 * The order of pads returned by the iterator will be the order in which
1321 * the pads were added to the element.
1323 * Returns: (transfer full): the #GstIterator of #GstPad.
1328 gst_element_iterate_pads (GstElement * element)
1330 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1332 return gst_element_iterate_pad_list (element, &element->pads);
1336 * gst_element_iterate_src_pads:
1337 * @element: a #GstElement.
1339 * Retrieves an iterator of @element's source pads.
1341 * The order of pads returned by the iterator will be the order in which
1342 * the pads were added to the element.
1344 * Returns: (transfer full): the #GstIterator of #GstPad.
1349 gst_element_iterate_src_pads (GstElement * element)
1351 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1353 return gst_element_iterate_pad_list (element, &element->srcpads);
1357 * gst_element_iterate_sink_pads:
1358 * @element: a #GstElement.
1360 * Retrieves an iterator of @element's sink pads.
1362 * The order of pads returned by the iterator will be the order in which
1363 * the pads were added to the element.
1365 * Returns: (transfer full): the #GstIterator of #GstPad.
1370 gst_element_iterate_sink_pads (GstElement * element)
1372 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1374 return gst_element_iterate_pad_list (element, &element->sinkpads);
1378 gst_element_do_foreach_pad (GstElement * element,
1379 GstElementForeachPadFunc func, gpointer user_data,
1380 GList ** p_pads, guint16 * p_npads)
1382 gboolean ret = TRUE;
1387 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1388 g_return_val_if_fail (func != NULL, FALSE);
1390 GST_OBJECT_LOCK (element);
1392 pads = g_newa (GstPad *, n_pads + 1);
1393 for (l = *p_pads, i = 0; l != NULL; l = l->next) {
1394 g_assert (i < n_pads);
1395 pads[i++] = gst_object_ref (l->data);
1397 GST_OBJECT_UNLOCK (element);
1402 for (i = 0; i < n_pads; ++i) {
1403 ret = func (element, pads[i], user_data);
1408 for (i = 0; i < n_pads; ++i)
1409 gst_object_unref (pads[i]);
1415 * gst_element_foreach_sink_pad:
1416 * @element: a #GstElement to iterate sink pads of
1417 * @func: (scope call): function to call for each sink pad
1418 * @user_data: (closure): user data passed to @func
1420 * Call @func with @user_data for each of @element's sink pads. @func will be
1421 * called exactly once for each sink pad that exists at the time of this call,
1422 * unless one of the calls to @func returns %FALSE in which case we will stop
1423 * iterating pads and return early. If new sink pads are added or sink pads
1424 * are removed while the sink pads are being iterated, this will not be taken
1425 * into account until next time this function is used.
1427 * Returns: %FALSE if @element had no sink pads or if one of the calls to @func
1433 gst_element_foreach_sink_pad (GstElement * element,
1434 GstElementForeachPadFunc func, gpointer user_data)
1436 return gst_element_do_foreach_pad (element, func, user_data,
1437 &element->sinkpads, &element->numsinkpads);
1441 * gst_element_foreach_src_pad:
1442 * @element: a #GstElement to iterate source pads of
1443 * @func: (scope call): function to call for each source pad
1444 * @user_data: (closure): user data passed to @func
1446 * Call @func with @user_data for each of @element's source pads. @func will be
1447 * called exactly once for each source pad that exists at the time of this call,
1448 * unless one of the calls to @func returns %FALSE in which case we will stop
1449 * iterating pads and return early. If new source pads are added or source pads
1450 * are removed while the source pads are being iterated, this will not be taken
1451 * into account until next time this function is used.
1453 * Returns: %FALSE if @element had no source pads or if one of the calls
1454 * to @func returned %FALSE.
1459 gst_element_foreach_src_pad (GstElement * element,
1460 GstElementForeachPadFunc func, gpointer user_data)
1462 return gst_element_do_foreach_pad (element, func, user_data,
1463 &element->srcpads, &element->numsrcpads);
1467 * gst_element_foreach_pad:
1468 * @element: a #GstElement to iterate pads of
1469 * @func: (scope call): function to call for each pad
1470 * @user_data: (closure): user data passed to @func
1472 * Call @func with @user_data for each of @element's pads. @func will be called
1473 * exactly once for each pad that exists at the time of this call, unless
1474 * one of the calls to @func returns %FALSE in which case we will stop
1475 * iterating pads and return early. If new pads are added or pads are removed
1476 * while pads are being iterated, this will not be taken into account until
1477 * next time this function is used.
1479 * Returns: %FALSE if @element had no pads or if one of the calls to @func
1485 gst_element_foreach_pad (GstElement * element, GstElementForeachPadFunc func,
1488 return gst_element_do_foreach_pad (element, func, user_data,
1489 &element->pads, &element->numpads);
1493 * gst_element_class_add_pad_template:
1494 * @klass: the #GstElementClass to add the pad template to.
1495 * @templ: (transfer floating): a #GstPadTemplate to add to the element class.
1497 * Adds a padtemplate to an element class. This is mainly used in the _class_init
1498 * functions of classes. If a pad template with the same name as an already
1499 * existing one is added the old one is replaced by the new one.
1501 * @templ's reference count will be incremented, and any floating
1502 * reference will be removed (see gst_object_ref_sink())
1506 gst_element_class_add_pad_template (GstElementClass * klass,
1507 GstPadTemplate * templ)
1509 GList *template_list = klass->padtemplates;
1511 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1512 g_return_if_fail (GST_IS_PAD_TEMPLATE (templ));
1514 /* If we already have a pad template with the same name replace the
1516 while (template_list) {
1517 GstPadTemplate *padtempl = (GstPadTemplate *) template_list->data;
1519 /* Found pad with the same name, replace and return */
1520 if (strcmp (templ->name_template, padtempl->name_template) == 0) {
1521 gst_object_ref_sink (padtempl);
1522 gst_object_unref (padtempl);
1523 template_list->data = templ;
1526 template_list = g_list_next (template_list);
1529 /* Take ownership of the floating ref */
1530 gst_object_ref_sink (templ);
1532 klass->padtemplates = g_list_append (klass->padtemplates, templ);
1533 klass->numpadtemplates++;
1537 * gst_element_class_add_static_pad_template:
1538 * @klass: the #GstElementClass to add the pad template to.
1539 * @static_templ: #GstStaticPadTemplate to add as pad template to the element class.
1541 * Adds a pad template to an element class based on the static pad template
1542 * @templ. This is mainly used in the _class_init functions of element
1543 * implementations. If a pad template with the same name already exists,
1544 * the old one is replaced by the new one.
1549 gst_element_class_add_static_pad_template (GstElementClass * klass,
1550 GstStaticPadTemplate * static_templ)
1552 gst_element_class_add_pad_template (klass,
1553 gst_static_pad_template_get (static_templ));
1557 * gst_element_class_add_static_pad_template_with_gtype:
1558 * @klass: the #GstElementClass to add the pad template to.
1559 * @static_templ: #GstStaticPadTemplate to add as pad template to the element class.
1560 * @pad_type: The #GType of the pad to create
1562 * Adds a pad template to an element class based on the static pad template
1563 * @templ. This is mainly used in the _class_init functions of element
1564 * implementations. If a pad template with the same name already exists,
1565 * the old one is replaced by the new one.
1570 gst_element_class_add_static_pad_template_with_gtype (GstElementClass * klass,
1571 GstStaticPadTemplate * static_templ, GType pad_type)
1573 gst_element_class_add_pad_template (klass,
1574 gst_pad_template_new_from_static_pad_template_with_gtype (static_templ,
1579 * gst_element_class_add_metadata:
1580 * @klass: class to set metadata for
1581 * @key: the key to set
1582 * @value: the value to set
1584 * Set @key with @value as metadata in @klass.
1587 gst_element_class_add_metadata (GstElementClass * klass,
1588 const gchar * key, const gchar * value)
1590 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1591 g_return_if_fail (key != NULL);
1592 g_return_if_fail (value != NULL);
1594 gst_structure_set ((GstStructure *) klass->metadata,
1595 key, G_TYPE_STRING, value, NULL);
1599 * gst_element_class_add_static_metadata:
1600 * @klass: class to set metadata for
1601 * @key: the key to set
1602 * @value: the value to set
1604 * Set @key with @value as metadata in @klass.
1606 * Same as gst_element_class_add_metadata(), but @value must be a static string
1607 * or an inlined string, as it will not be copied. (GStreamer plugins will
1608 * be made resident once loaded, so this function can be used even from
1609 * dynamically loaded plugins.)
1612 gst_element_class_add_static_metadata (GstElementClass * klass,
1613 const gchar * key, const gchar * value)
1615 GValue val = G_VALUE_INIT;
1617 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1618 g_return_if_fail (key != NULL);
1619 g_return_if_fail (value != NULL);
1621 g_value_init (&val, G_TYPE_STRING);
1622 g_value_set_static_string (&val, value);
1623 gst_structure_take_value ((GstStructure *) klass->metadata, key, &val);
1627 * gst_element_class_set_metadata:
1628 * @klass: class to set metadata for
1629 * @longname: The long English name of the element. E.g. "File Sink"
1630 * @classification: String describing the type of element, as an unordered list
1631 * separated with slashes ('/'). See draft-klass.txt of the design docs
1632 * for more details and common types. E.g: "Sink/File"
1633 * @description: Sentence describing the purpose of the element.
1634 * E.g: "Write stream to a file"
1635 * @author: Name and contact details of the author(s). Use \n to separate
1636 * multiple author metadata. E.g: "Joe Bloggs <joe.blogs at foo.com>"
1638 * Sets the detailed information for a #GstElementClass.
1639 * > This function is for use in _class_init functions only.
1642 gst_element_class_set_metadata (GstElementClass * klass,
1643 const gchar * longname, const gchar * classification,
1644 const gchar * description, const gchar * author)
1646 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1647 g_return_if_fail (longname != NULL && *longname != '\0');
1648 g_return_if_fail (classification != NULL && *classification != '\0');
1649 g_return_if_fail (description != NULL && *description != '\0');
1650 g_return_if_fail (author != NULL && *author != '\0');
1652 gst_structure_id_set ((GstStructure *) klass->metadata,
1653 GST_QUARK (ELEMENT_METADATA_LONGNAME), G_TYPE_STRING, longname,
1654 GST_QUARK (ELEMENT_METADATA_KLASS), G_TYPE_STRING, classification,
1655 GST_QUARK (ELEMENT_METADATA_DESCRIPTION), G_TYPE_STRING, description,
1656 GST_QUARK (ELEMENT_METADATA_AUTHOR), G_TYPE_STRING, author, NULL);
1660 * gst_element_class_set_static_metadata:
1661 * @klass: class to set metadata for
1662 * @longname: The long English name of the element. E.g. "File Sink"
1663 * @classification: String describing the type of element, as an unordered list
1664 * separated with slashes ('/'). See draft-klass.txt of the design docs
1665 * for more details and common types. E.g: "Sink/File"
1666 * @description: Sentence describing the purpose of the element.
1667 * E.g: "Write stream to a file"
1668 * @author: Name and contact details of the author(s). Use \n to separate
1669 * multiple author metadata. E.g: "Joe Bloggs <joe.blogs at foo.com>"
1671 * Sets the detailed information for a #GstElementClass.
1673 * > This function is for use in _class_init functions only.
1675 * Same as gst_element_class_set_metadata(), but @longname, @classification,
1676 * @description, and @author must be static strings or inlined strings, as
1677 * they will not be copied. (GStreamer plugins will be made resident once
1678 * loaded, so this function can be used even from dynamically loaded plugins.)
1681 gst_element_class_set_static_metadata (GstElementClass * klass,
1682 const gchar * longname, const gchar * classification,
1683 const gchar * description, const gchar * author)
1685 GstStructure *s = (GstStructure *) klass->metadata;
1686 GValue val = G_VALUE_INIT;
1688 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1689 g_return_if_fail (longname != NULL && *longname != '\0');
1690 g_return_if_fail (classification != NULL && *classification != '\0');
1691 g_return_if_fail (description != NULL && *description != '\0');
1692 g_return_if_fail (author != NULL && *author != '\0');
1694 g_value_init (&val, G_TYPE_STRING);
1696 g_value_set_static_string (&val, longname);
1697 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_LONGNAME), &val);
1699 g_value_set_static_string (&val, classification);
1700 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_KLASS), &val);
1702 g_value_set_static_string (&val, description);
1703 gst_structure_id_set_value (s, GST_QUARK (ELEMENT_METADATA_DESCRIPTION),
1706 g_value_set_static_string (&val, author);
1707 gst_structure_id_take_value (s, GST_QUARK (ELEMENT_METADATA_AUTHOR), &val);
1711 * gst_element_class_get_metadata:
1712 * @klass: class to get metadata for
1713 * @key: the key to get
1715 * Get metadata with @key in @klass.
1717 * Returns: the metadata for @key.
1720 gst_element_class_get_metadata (GstElementClass * klass, const gchar * key)
1722 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (klass), NULL);
1723 g_return_val_if_fail (key != NULL, NULL);
1725 return gst_structure_get_string ((GstStructure *) klass->metadata, key);
1729 * gst_element_get_metadata:
1730 * @element: class to get metadata for
1731 * @key: the key to get
1733 * Get metadata with @key in @klass.
1735 * Returns: the metadata for @key.
1740 gst_element_get_metadata (GstElement * element, const gchar * key)
1742 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1743 g_return_val_if_fail (key != NULL, NULL);
1745 return gst_element_class_get_metadata (GST_ELEMENT_GET_CLASS (element), key);
1749 * gst_element_class_get_pad_template_list:
1750 * @element_class: a #GstElementClass to get pad templates of.
1752 * Retrieves a list of the pad templates associated with @element_class. The
1753 * list must not be modified by the calling code.
1754 * > If you use this function in the #GInstanceInitFunc of an object class
1755 * > that has subclasses, make sure to pass the g_class parameter of the
1756 * > #GInstanceInitFunc here.
1758 * Returns: (transfer none) (element-type Gst.PadTemplate): the #GList of
1762 gst_element_class_get_pad_template_list (GstElementClass * element_class)
1764 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1766 return element_class->padtemplates;
1770 * gst_element_get_pad_template_list:
1771 * @element: a #GstElement to get pad templates of.
1773 * Retrieves a list of the pad templates associated with @element. The
1774 * list must not be modified by the calling code.
1776 * Returns: (transfer none) (element-type Gst.PadTemplate): the #GList of
1782 gst_element_get_pad_template_list (GstElement * element)
1784 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1787 gst_element_class_get_pad_template_list (GST_ELEMENT_GET_CLASS (element));
1791 * gst_element_class_get_pad_template:
1792 * @element_class: a #GstElementClass to get the pad template of.
1793 * @name: the name of the #GstPadTemplate to get.
1795 * Retrieves a padtemplate from @element_class with the given name.
1796 * > If you use this function in the #GInstanceInitFunc of an object class
1797 * > that has subclasses, make sure to pass the g_class parameter of the
1798 * > #GInstanceInitFunc here.
1800 * Returns: (transfer none) (nullable): the #GstPadTemplate with the
1801 * given name, or %NULL if none was found. No unreferencing is
1805 gst_element_class_get_pad_template (GstElementClass *
1806 element_class, const gchar * name)
1810 g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1811 g_return_val_if_fail (name != NULL, NULL);
1813 padlist = element_class->padtemplates;
1816 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
1818 if (strcmp (padtempl->name_template, name) == 0)
1821 padlist = g_list_next (padlist);
1828 * gst_element_get_pad_template:
1829 * @element: a #GstElement to get the pad template of.
1830 * @name: the name of the #GstPadTemplate to get.
1832 * Retrieves a padtemplate from @element with the given name.
1834 * Returns: (transfer none) (nullable): the #GstPadTemplate with the
1835 * given name, or %NULL if none was found. No unreferencing is
1841 gst_element_get_pad_template (GstElement * element, const gchar * name)
1843 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1844 g_return_val_if_fail (name != NULL, NULL);
1846 return gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (element),
1850 static GstPadTemplate *
1851 gst_element_class_request_pad_simple_template (GstElementClass *
1852 element_class, const gchar * name)
1854 GstPadTemplate *tmpl;
1856 tmpl = gst_element_class_get_pad_template (element_class, name);
1857 if (tmpl != NULL && tmpl->presence == GST_PAD_REQUEST)
1863 /* get a random pad on element of the given direction.
1864 * The pad is random in a sense that it is the first pad that is (optionally) linked.
1867 gst_element_get_random_pad (GstElement * element,
1868 gboolean need_linked, GstPadDirection dir)
1870 GstPad *result = NULL;
1873 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "getting a random pad");
1877 GST_OBJECT_LOCK (element);
1878 pads = element->srcpads;
1881 GST_OBJECT_LOCK (element);
1882 pads = element->sinkpads;
1885 goto wrong_direction;
1887 for (; pads; pads = g_list_next (pads)) {
1888 GstPad *pad = GST_PAD_CAST (pads->data);
1890 GST_OBJECT_LOCK (pad);
1891 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "checking pad %s:%s",
1892 GST_DEBUG_PAD_NAME (pad));
1894 if (need_linked && !GST_PAD_IS_LINKED (pad)) {
1895 /* if we require a linked pad, and it is not linked, continue the
1897 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is not linked",
1898 GST_DEBUG_PAD_NAME (pad));
1899 GST_OBJECT_UNLOCK (pad);
1902 /* found a pad, stop search */
1903 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
1904 GST_DEBUG_PAD_NAME (pad));
1905 GST_OBJECT_UNLOCK (pad);
1911 gst_object_ref (result);
1913 GST_OBJECT_UNLOCK (element);
1917 /* ERROR handling */
1920 g_warning ("unknown pad direction %d", dir);
1926 gst_element_default_send_event (GstElement * element, GstEvent * event)
1928 gboolean result = FALSE;
1931 pad = GST_EVENT_IS_DOWNSTREAM (event) ?
1932 gst_element_get_random_pad (element, TRUE, GST_PAD_SINK) :
1933 gst_element_get_random_pad (element, TRUE, GST_PAD_SRC);
1936 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1937 "pushing %s event to random %s pad %s:%s",
1938 GST_EVENT_TYPE_NAME (event),
1939 (GST_PAD_DIRECTION (pad) == GST_PAD_SRC ? "src" : "sink"),
1940 GST_DEBUG_PAD_NAME (pad));
1942 result = gst_pad_send_event (pad, event);
1943 gst_object_unref (pad);
1945 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "can't send %s event on element %s",
1946 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1947 gst_event_unref (event);
1953 * gst_element_send_event:
1954 * @element: a #GstElement to send the event to.
1955 * @event: (transfer full): the #GstEvent to send to the element.
1957 * Sends an event to an element. If the element doesn't implement an
1958 * event handler, the event will be pushed on a random linked sink pad for
1959 * downstream events or a random linked source pad for upstream events.
1961 * This function takes ownership of the provided event so you should
1962 * gst_event_ref() it if you want to reuse the event after this call.
1966 * Returns: %TRUE if the event was handled. Events that trigger a preroll (such
1967 * as flushing seeks and steps) will emit %GST_MESSAGE_ASYNC_DONE.
1970 gst_element_send_event (GstElement * element, GstEvent * event)
1972 GstElementClass *oclass;
1973 gboolean result = FALSE;
1975 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1976 g_return_val_if_fail (event != NULL, FALSE);
1978 oclass = GST_ELEMENT_GET_CLASS (element);
1980 GST_STATE_LOCK (element);
1981 if (oclass->send_event) {
1982 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send %s event on element %s",
1983 GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1984 result = oclass->send_event (element, event);
1986 gst_event_unref (event);
1988 GST_STATE_UNLOCK (element);
1995 * @element: a #GstElement to send the event to.
1996 * @rate: The new playback rate
1997 * @format: The format of the seek values
1998 * @flags: The optional seek flags.
1999 * @start_type: The type and flags for the new start position
2000 * @start: The value of the new start position
2001 * @stop_type: The type and flags for the new stop position
2002 * @stop: The value of the new stop position
2004 * Sends a seek event to an element. See gst_event_new_seek() for the details of
2005 * the parameters. The seek event is sent to the element using
2006 * gst_element_send_event().
2010 * Returns: %TRUE if the event was handled. Flushing seeks will trigger a
2011 * preroll, which will emit %GST_MESSAGE_ASYNC_DONE.
2014 gst_element_seek (GstElement * element, gdouble rate, GstFormat format,
2015 GstSeekFlags flags, GstSeekType start_type, gint64 start,
2016 GstSeekType stop_type, gint64 stop)
2021 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2024 gst_event_new_seek (rate, format, flags, start_type, start, stop_type,
2026 result = gst_element_send_event (element, event);
2032 gst_element_default_query (GstElement * element, GstQuery * query)
2034 gboolean result = FALSE;
2037 pad = gst_element_get_random_pad (element, FALSE, GST_PAD_SRC);
2039 result = gst_pad_query (pad, query);
2041 gst_object_unref (pad);
2043 pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
2045 GstPad *peer = gst_pad_get_peer (pad);
2048 result = gst_pad_query (peer, query);
2050 gst_object_unref (peer);
2052 gst_object_unref (pad);
2059 * gst_element_query:
2060 * @element: a #GstElement to perform the query on.
2061 * @query: (transfer none): the #GstQuery.
2063 * Performs a query on the given element.
2065 * For elements that don't implement a query handler, this function
2066 * forwards the query to a random srcpad or to the peer of a
2067 * random linked sinkpad of this element.
2069 * Please note that some queries might need a running pipeline to work.
2071 * Returns: %TRUE if the query could be performed.
2076 gst_element_query (GstElement * element, GstQuery * query)
2078 GstElementClass *klass;
2079 gboolean res = FALSE;
2081 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2082 g_return_val_if_fail (query != NULL, FALSE);
2084 GST_TRACER_ELEMENT_QUERY_PRE (element, query);
2086 klass = GST_ELEMENT_GET_CLASS (element);
2088 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send query on element %s",
2089 GST_ELEMENT_NAME (element));
2090 res = klass->query (element, query);
2093 GST_TRACER_ELEMENT_QUERY_POST (element, query, res);
2098 gst_element_post_message_default (GstElement * element, GstMessage * message)
2101 gboolean result = FALSE;
2103 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2104 g_return_val_if_fail (message != NULL, FALSE);
2106 GST_OBJECT_LOCK (element);
2109 if (G_UNLIKELY (bus == NULL))
2112 gst_object_ref (bus);
2113 GST_OBJECT_UNLOCK (element);
2115 /* we release the element lock when posting the message so that any
2116 * (synchronous) message handlers can operate on the element */
2117 result = gst_bus_post (bus, message);
2118 gst_object_unref (bus);
2125 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, element,
2126 "not posting message %p: no bus", message);
2127 GST_OBJECT_UNLOCK (element);
2128 gst_message_unref (message);
2134 * gst_element_post_message:
2135 * @element: a #GstElement posting the message
2136 * @message: (transfer full): a #GstMessage to post
2138 * Post a message on the element's #GstBus. This function takes ownership of the
2139 * message; if you want to access the message after this call, you should add an
2140 * additional reference before calling.
2142 * Returns: %TRUE if the message was successfully posted. The function returns
2143 * %FALSE if the element did not have a bus.
2148 gst_element_post_message (GstElement * element, GstMessage * message)
2150 GstElementClass *klass;
2151 gboolean res = FALSE;
2153 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2154 g_return_val_if_fail (message != NULL, FALSE);
2156 GST_TRACER_ELEMENT_POST_MESSAGE_PRE (element, message);
2158 klass = GST_ELEMENT_GET_CLASS (element);
2159 if (klass->post_message)
2160 res = klass->post_message (element, message);
2162 gst_message_unref (message);
2164 GST_TRACER_ELEMENT_POST_MESSAGE_POST (element, res);
2169 * _gst_element_error_printf:
2170 * @format: (allow-none): the printf-like format to use, or %NULL
2172 * This function is only used internally by the gst_element_error() macro.
2174 * Returns: (transfer full) (nullable): a newly allocated string, or
2175 * %NULL if the format was %NULL or ""
2180 _gst_element_error_printf (const gchar * format, ...)
2191 va_start (args, format);
2193 len = __gst_vasprintf (&buffer, format, args);
2204 * gst_element_message_full_with_details:
2205 * @element: a #GstElement to send message from
2206 * @type: the #GstMessageType
2207 * @domain: the GStreamer GError domain this message belongs to
2208 * @code: the GError code belonging to the domain
2209 * @text: (allow-none) (transfer full): an allocated text string to be used
2210 * as a replacement for the default message connected to code,
2212 * @debug: (allow-none) (transfer full): an allocated debug message to be
2213 * used as a replacement for the default debugging information,
2215 * @file: the source code file where the error was generated
2216 * @function: the source code function where the error was generated
2217 * @line: the source code line where the error was generated
2218 * @structure:(transfer full): optional details structure
2220 * Post an error, warning or info message on the bus from inside an element.
2222 * @type must be of #GST_MESSAGE_ERROR, #GST_MESSAGE_WARNING or
2223 * #GST_MESSAGE_INFO.
2227 void gst_element_message_full_with_details
2228 (GstElement * element, GstMessageType type,
2229 GQuark domain, gint code, gchar * text,
2230 gchar * debug, const gchar * file, const gchar * function, gint line,
2231 GstStructure * structure)
2233 GError *gerror = NULL;
2237 gboolean has_debug = TRUE;
2238 GstMessage *message = NULL;
2241 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, element, "start");
2242 g_return_if_fail (GST_IS_ELEMENT (element));
2243 g_return_if_fail ((type == GST_MESSAGE_ERROR) ||
2244 (type == GST_MESSAGE_WARNING) || (type == GST_MESSAGE_INFO));
2246 /* check if we send the given text or the default error text */
2247 if ((text == NULL) || (text[0] == 0)) {
2248 /* text could have come from g_strdup_printf (""); */
2250 sent_text = gst_error_get_message (domain, code);
2254 /* construct a sent_debug with extra information from source */
2255 if ((debug == NULL) || (debug[0] == 0)) {
2256 /* debug could have come from g_strdup_printf (""); */
2260 name = gst_object_get_path_string (GST_OBJECT_CAST (element));
2262 sent_debug = g_strdup_printf ("%s(%d): %s (): %s:\n%s",
2263 file, line, function, name, debug);
2265 sent_debug = g_strdup_printf ("%s(%d): %s (): %s",
2266 file, line, function, name);
2270 /* create gerror and post message */
2271 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posting message: %s",
2273 gerror = g_error_new_literal (domain, code, sent_text);
2276 case GST_MESSAGE_ERROR:
2278 gst_message_new_error_with_details (GST_OBJECT_CAST (element), gerror,
2279 sent_debug, structure);
2281 case GST_MESSAGE_WARNING:
2283 gst_message_new_warning_with_details (GST_OBJECT_CAST (element),
2284 gerror, sent_debug, structure);
2286 case GST_MESSAGE_INFO:
2288 gst_message_new_info_with_details (GST_OBJECT_CAST (element), gerror,
2289 sent_debug, structure);
2292 g_assert_not_reached ();
2296 gst_element_post_message (element, message);
2298 GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posted %s message: %s",
2299 (type == GST_MESSAGE_ERROR ? "error" : "warning"), sent_text);
2302 g_error_free (gerror);
2303 g_free (sent_debug);
2308 * gst_element_message_full:
2309 * @element: a #GstElement to send message from
2310 * @type: the #GstMessageType
2311 * @domain: the GStreamer GError domain this message belongs to
2312 * @code: the GError code belonging to the domain
2313 * @text: (allow-none) (transfer full): an allocated text string to be used
2314 * as a replacement for the default message connected to code,
2316 * @debug: (allow-none) (transfer full): an allocated debug message to be
2317 * used as a replacement for the default debugging information,
2319 * @file: the source code file where the error was generated
2320 * @function: the source code function where the error was generated
2321 * @line: the source code line where the error was generated
2323 * Post an error, warning or info message on the bus from inside an element.
2325 * @type must be of #GST_MESSAGE_ERROR, #GST_MESSAGE_WARNING or
2326 * #GST_MESSAGE_INFO.
2330 void gst_element_message_full
2331 (GstElement * element, GstMessageType type,
2332 GQuark domain, gint code, gchar * text,
2333 gchar * debug, const gchar * file, const gchar * function, gint line)
2335 gst_element_message_full_with_details (element, type, domain, code, text,
2336 debug, file, function, line, NULL);
2340 * gst_element_is_locked_state:
2341 * @element: a #GstElement.
2343 * Checks if the state of an element is locked.
2344 * If the state of an element is locked, state changes of the parent don't
2345 * affect the element.
2346 * This way you can leave currently unused elements inside bins. Just lock their
2347 * state before changing the state from #GST_STATE_NULL.
2351 * Returns: %TRUE, if the element's state is locked.
2354 gst_element_is_locked_state (GstElement * element)
2358 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2360 GST_OBJECT_LOCK (element);
2361 result = GST_ELEMENT_IS_LOCKED_STATE (element);
2362 GST_OBJECT_UNLOCK (element);
2368 * gst_element_set_locked_state:
2369 * @element: a #GstElement
2370 * @locked_state: %TRUE to lock the element's state
2372 * Locks the state of an element, so state changes of the parent don't affect
2373 * this element anymore.
2375 * Note that this is racy if the state lock of the parent bin is not taken.
2376 * The parent bin might've just checked the flag in another thread and as the
2377 * next step proceed to change the child element's state.
2381 * Returns: %TRUE if the state was changed, %FALSE if bad parameters were given
2382 * or the elements state-locking needed no change.
2385 gst_element_set_locked_state (GstElement * element, gboolean locked_state)
2389 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2391 GST_OBJECT_LOCK (element);
2392 old = GST_ELEMENT_IS_LOCKED_STATE (element);
2394 if (G_UNLIKELY (old == locked_state))
2398 GST_CAT_DEBUG (GST_CAT_STATES, "locking state of element %s",
2399 GST_ELEMENT_NAME (element));
2400 GST_OBJECT_FLAG_SET (element, GST_ELEMENT_FLAG_LOCKED_STATE);
2402 GST_CAT_DEBUG (GST_CAT_STATES, "unlocking state of element %s",
2403 GST_ELEMENT_NAME (element));
2404 GST_OBJECT_FLAG_UNSET (element, GST_ELEMENT_FLAG_LOCKED_STATE);
2406 GST_OBJECT_UNLOCK (element);
2412 GST_CAT_DEBUG (GST_CAT_STATES,
2413 "elements %s was already in locked state %d",
2414 GST_ELEMENT_NAME (element), old);
2415 GST_OBJECT_UNLOCK (element);
2422 * gst_element_sync_state_with_parent:
2423 * @element: a #GstElement.
2425 * Tries to change the state of the element to the same as its parent.
2426 * If this function returns %FALSE, the state of element is undefined.
2428 * Returns: %TRUE, if the element's state could be synced to the parent's state.
2433 gst_element_sync_state_with_parent (GstElement * element)
2437 GstStateChangeReturn ret;
2439 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2441 if ((parent = GST_ELEMENT_CAST (gst_element_get_parent (element)))) {
2442 GstState parent_current, parent_pending;
2444 GST_OBJECT_LOCK (parent);
2445 parent_current = GST_STATE (parent);
2446 parent_pending = GST_STATE_PENDING (parent);
2447 GST_OBJECT_UNLOCK (parent);
2449 /* set to pending if there is one, else we set it to the current state of
2451 if (parent_pending != GST_STATE_VOID_PENDING)
2452 target = parent_pending;
2454 target = parent_current;
2456 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2457 "syncing state (%s) to parent %s %s (%s, %s)",
2458 gst_element_state_get_name (GST_STATE (element)),
2459 GST_ELEMENT_NAME (parent), gst_element_state_get_name (target),
2460 gst_element_state_get_name (parent_current),
2461 gst_element_state_get_name (parent_pending));
2463 ret = gst_element_set_state (element, target);
2464 if (ret == GST_STATE_CHANGE_FAILURE)
2467 gst_object_unref (parent);
2471 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "element has no parent");
2478 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2479 "syncing state failed (%s)",
2480 gst_element_state_change_return_get_name (ret));
2481 gst_object_unref (parent);
2487 static GstStateChangeReturn
2488 gst_element_get_state_func (GstElement * element,
2489 GstState * state, GstState * pending, GstClockTime timeout)
2491 GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
2492 GstState old_pending;
2494 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "getting state, timeout %"
2495 GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
2497 GST_OBJECT_LOCK (element);
2498 ret = GST_STATE_RETURN (element);
2499 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "RETURN is %s",
2500 gst_element_state_change_return_get_name (ret));
2502 /* we got an error, report immediately */
2503 if (ret == GST_STATE_CHANGE_FAILURE)
2506 /* we got no_preroll, report immediately */
2507 if (ret == GST_STATE_CHANGE_NO_PREROLL)
2510 /* no need to wait async if we are not async */
2511 if (ret != GST_STATE_CHANGE_ASYNC)
2514 old_pending = GST_STATE_PENDING (element);
2515 if (old_pending != GST_STATE_VOID_PENDING) {
2519 /* get cookie to detect state changes during waiting */
2520 cookie = element->state_cookie;
2522 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2523 "waiting for element to commit state");
2525 /* we have a pending state change, wait for it to complete */
2526 if (timeout != GST_CLOCK_TIME_NONE) {
2528 /* make timeout absolute */
2529 end_time = g_get_monotonic_time () + (timeout / 1000);
2530 signaled = GST_STATE_WAIT_UNTIL (element, end_time);
2532 GST_STATE_WAIT (element);
2537 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timed out");
2538 /* timeout triggered */
2539 ret = GST_STATE_CHANGE_ASYNC;
2541 if (cookie != element->state_cookie)
2544 /* could be success or failure */
2545 if (old_pending == GST_STATE (element)) {
2546 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got success");
2547 ret = GST_STATE_CHANGE_SUCCESS;
2549 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got failure");
2550 ret = GST_STATE_CHANGE_FAILURE;
2553 /* if nothing is pending anymore we can return SUCCESS */
2554 if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
2555 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "nothing pending");
2556 ret = GST_STATE_CHANGE_SUCCESS;
2562 *state = GST_STATE (element);
2564 *pending = GST_STATE_PENDING (element);
2566 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2567 "state current: %s, pending: %s, result: %s",
2568 gst_element_state_get_name (GST_STATE (element)),
2569 gst_element_state_get_name (GST_STATE_PENDING (element)),
2570 gst_element_state_change_return_get_name (ret));
2571 GST_OBJECT_UNLOCK (element);
2578 *state = GST_STATE_VOID_PENDING;
2580 *pending = GST_STATE_VOID_PENDING;
2582 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "interrupted");
2584 GST_OBJECT_UNLOCK (element);
2586 return GST_STATE_CHANGE_FAILURE;
2591 * gst_element_get_state:
2592 * @element: a #GstElement to get the state of.
2593 * @state: (out) (allow-none): a pointer to #GstState to hold the state.
2595 * @pending: (out) (allow-none): a pointer to #GstState to hold the pending
2596 * state. Can be %NULL.
2597 * @timeout: a #GstClockTime to specify the timeout for an async
2598 * state change or %GST_CLOCK_TIME_NONE for infinite timeout.
2600 * Gets the state of the element.
2602 * For elements that performed an ASYNC state change, as reported by
2603 * gst_element_set_state(), this function will block up to the
2604 * specified timeout value for the state change to complete.
2605 * If the element completes the state change or goes into
2606 * an error, this function returns immediately with a return value of
2607 * %GST_STATE_CHANGE_SUCCESS or %GST_STATE_CHANGE_FAILURE respectively.
2609 * For elements that did not return %GST_STATE_CHANGE_ASYNC, this function
2610 * returns the current and pending state immediately.
2612 * This function returns %GST_STATE_CHANGE_NO_PREROLL if the element
2613 * successfully changed its state but is not able to provide data yet.
2614 * This mostly happens for live sources that only produce data in
2615 * %GST_STATE_PLAYING. While the state change return is equivalent to
2616 * %GST_STATE_CHANGE_SUCCESS, it is returned to the application to signal that
2617 * some sink elements might not be able to complete their state change because
2618 * an element is not producing data to complete the preroll. When setting the
2619 * element to playing, the preroll will complete and playback will start.
2621 * Returns: %GST_STATE_CHANGE_SUCCESS if the element has no more pending state
2622 * and the last state change succeeded, %GST_STATE_CHANGE_ASYNC if the
2623 * element is still performing a state change or
2624 * %GST_STATE_CHANGE_FAILURE if the last state change failed.
2628 GstStateChangeReturn
2629 gst_element_get_state (GstElement * element,
2630 GstState * state, GstState * pending, GstClockTime timeout)
2632 GstElementClass *oclass;
2633 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2635 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2637 oclass = GST_ELEMENT_GET_CLASS (element);
2639 if (oclass->get_state)
2640 result = (oclass->get_state) (element, state, pending, timeout);
2646 * gst_element_abort_state:
2647 * @element: a #GstElement to abort the state of.
2649 * Abort the state change of the element. This function is used
2650 * by elements that do asynchronous state changes and find out
2651 * something is wrong.
2653 * This function should be called with the STATE_LOCK held.
2658 gst_element_abort_state (GstElement * element)
2662 #ifndef GST_DISABLE_GST_DEBUG
2666 g_return_if_fail (GST_IS_ELEMENT (element));
2668 GST_OBJECT_LOCK (element);
2669 pending = GST_STATE_PENDING (element);
2671 if (pending == GST_STATE_VOID_PENDING ||
2672 GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2673 goto nothing_aborted;
2675 #ifndef GST_DISABLE_GST_DEBUG
2676 old_state = GST_STATE (element);
2678 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2679 "aborting state from %s to %s", gst_element_state_get_name (old_state),
2680 gst_element_state_get_name (pending));
2684 GST_STATE_RETURN (element) = GST_STATE_CHANGE_FAILURE;
2686 GST_STATE_BROADCAST (element);
2687 GST_OBJECT_UNLOCK (element);
2693 GST_OBJECT_UNLOCK (element);
2698 /* Not static because GstBin has manual state handling too */
2700 _priv_gst_element_state_changed (GstElement * element, GstState oldstate,
2701 GstState newstate, GstState pending)
2703 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
2704 GstMessage *message;
2706 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2707 "notifying about state-changed %s to %s (%s pending)",
2708 gst_element_state_get_name (oldstate),
2709 gst_element_state_get_name (newstate),
2710 gst_element_state_get_name (pending));
2712 if (klass->state_changed)
2713 klass->state_changed (element, oldstate, newstate, pending);
2715 message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2716 oldstate, newstate, pending);
2717 gst_element_post_message (element, message);
2721 * gst_element_continue_state:
2722 * @element: a #GstElement to continue the state change of.
2723 * @ret: The previous state return value
2725 * Commit the state change of the element and proceed to the next
2726 * pending state if any. This function is used
2727 * by elements that do asynchronous state changes.
2728 * The core will normally call this method automatically when an
2729 * element returned %GST_STATE_CHANGE_SUCCESS from the state change function.
2731 * If after calling this method the element still has not reached
2732 * the pending state, the next state change is performed.
2734 * This method is used internally and should normally not be called by plugins
2737 * This function must be called with STATE_LOCK held.
2739 * Returns: The result of the commit state change.
2743 GstStateChangeReturn
2744 gst_element_continue_state (GstElement * element, GstStateChangeReturn ret)
2746 GstStateChangeReturn old_ret;
2747 GstState old_state, old_next;
2748 GstState current, next, pending;
2749 GstStateChange transition;
2751 GST_OBJECT_LOCK (element);
2752 old_ret = GST_STATE_RETURN (element);
2753 GST_STATE_RETURN (element) = ret;
2754 pending = GST_STATE_PENDING (element);
2756 /* check if there is something to commit */
2757 if (pending == GST_STATE_VOID_PENDING)
2758 goto nothing_pending;
2760 old_state = GST_STATE (element);
2761 /* this is the state we should go to next */
2762 old_next = GST_STATE_NEXT (element);
2763 /* update current state */
2764 current = GST_STATE (element) = old_next;
2766 /* see if we reached the final state */
2767 if (pending == current)
2770 next = GST_STATE_GET_NEXT (current, pending);
2771 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2773 GST_STATE_NEXT (element) = next;
2775 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2776 GST_OBJECT_UNLOCK (element);
2778 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2779 "committing state from %s to %s, pending %s, next %s",
2780 gst_element_state_get_name (old_state),
2781 gst_element_state_get_name (old_next),
2782 gst_element_state_get_name (pending), gst_element_state_get_name (next));
2784 _priv_gst_element_state_changed (element, old_state, old_next, pending);
2786 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2787 "continue state change %s to %s, final %s",
2788 gst_element_state_get_name (current),
2789 gst_element_state_get_name (next), gst_element_state_get_name (pending));
2791 ret = gst_element_change_state (element, transition);
2797 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "nothing pending");
2798 GST_OBJECT_UNLOCK (element);
2803 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2804 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2806 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2807 "completed state change to %s", gst_element_state_get_name (pending));
2808 GST_OBJECT_UNLOCK (element);
2810 /* don't post silly messages with the same state. This can happen
2811 * when an element state is changed to what it already was. For bins
2812 * this can be the result of a lost state, which we check with the
2813 * previous return value.
2814 * We do signal the cond though as a _get_state() might be blocking
2816 if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC)
2817 _priv_gst_element_state_changed (element, old_state, old_next,
2818 GST_STATE_VOID_PENDING);
2820 GST_STATE_BROADCAST (element);
2827 * gst_element_lost_state:
2828 * @element: a #GstElement the state is lost of
2830 * Brings the element to the lost state. The current state of the
2831 * element is copied to the pending state so that any call to
2832 * gst_element_get_state() will return %GST_STATE_CHANGE_ASYNC.
2834 * An ASYNC_START message is posted. If the element was PLAYING, it will
2835 * go to PAUSED. The element will be restored to its PLAYING state by
2836 * the parent pipeline when it prerolls again.
2838 * This is mostly used for elements that lost their preroll buffer
2839 * in the %GST_STATE_PAUSED or %GST_STATE_PLAYING state after a flush,
2840 * they will go to their pending state again when a new preroll buffer is
2841 * queued. This function can only be called when the element is currently
2842 * not in error or an async state change.
2844 * This function is used internally and should normally not be called from
2845 * plugins or applications.
2848 gst_element_lost_state (GstElement * element)
2850 GstState old_state, new_state;
2851 GstMessage *message;
2853 g_return_if_fail (GST_IS_ELEMENT (element));
2855 GST_OBJECT_LOCK (element);
2856 if (GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2859 if (GST_STATE_PENDING (element) != GST_STATE_VOID_PENDING)
2860 goto only_async_start;
2862 old_state = GST_STATE (element);
2864 /* when we were PLAYING, the new state is PAUSED. We will also not
2865 * automatically go to PLAYING but let the parent bin(s) set us to PLAYING
2866 * when we preroll. */
2867 if (old_state > GST_STATE_PAUSED)
2868 new_state = GST_STATE_PAUSED;
2870 new_state = old_state;
2872 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2873 "lost state of %s to %s", gst_element_state_get_name (old_state),
2874 gst_element_state_get_name (new_state));
2876 GST_STATE (element) = new_state;
2877 GST_STATE_NEXT (element) = new_state;
2878 GST_STATE_PENDING (element) = new_state;
2879 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2880 GST_OBJECT_UNLOCK (element);
2882 _priv_gst_element_state_changed (element, new_state, new_state, new_state);
2884 message = gst_message_new_async_start (GST_OBJECT_CAST (element));
2885 gst_element_post_message (element, message);
2891 GST_OBJECT_UNLOCK (element);
2896 GST_OBJECT_UNLOCK (element);
2898 message = gst_message_new_async_start (GST_OBJECT_CAST (element));
2899 gst_element_post_message (element, message);
2905 * gst_element_set_state:
2906 * @element: a #GstElement to change state of.
2907 * @state: the element's new #GstState.
2909 * Sets the state of the element. This function will try to set the
2910 * requested state by going through all the intermediary states and calling
2911 * the class's state change function for each.
2913 * This function can return #GST_STATE_CHANGE_ASYNC, in which case the
2914 * element will perform the remainder of the state change asynchronously in
2916 * An application can use gst_element_get_state() to wait for the completion
2917 * of the state change or it can wait for a %GST_MESSAGE_ASYNC_DONE or
2918 * %GST_MESSAGE_STATE_CHANGED on the bus.
2920 * State changes to %GST_STATE_READY or %GST_STATE_NULL never return
2921 * #GST_STATE_CHANGE_ASYNC.
2923 * Returns: Result of the state change using #GstStateChangeReturn.
2927 GstStateChangeReturn
2928 gst_element_set_state (GstElement * element, GstState state)
2930 GstElementClass *oclass;
2931 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2933 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2935 oclass = GST_ELEMENT_GET_CLASS (element);
2937 if (oclass->set_state)
2938 result = (oclass->set_state) (element, state);
2944 * default set state function, calculates the next state based
2945 * on current state and calls the change_state function
2947 static GstStateChangeReturn
2948 gst_element_set_state_func (GstElement * element, GstState state)
2950 GstState current, next, old_pending;
2951 GstStateChangeReturn ret;
2952 GstStateChange transition;
2953 GstStateChangeReturn old_ret;
2955 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2957 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "set_state to %s",
2958 gst_element_state_get_name (state));
2960 /* state lock is taken to protect the set_state() and get_state()
2961 * procedures, it does not lock any variables. */
2962 GST_STATE_LOCK (element);
2964 /* now calculate how to get to the new state */
2965 GST_OBJECT_LOCK (element);
2966 old_ret = GST_STATE_RETURN (element);
2967 /* previous state change returned an error, remove all pending
2968 * and next states */
2969 if (old_ret == GST_STATE_CHANGE_FAILURE) {
2970 GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2971 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2972 GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
2975 current = GST_STATE (element);
2976 next = GST_STATE_NEXT (element);
2977 old_pending = GST_STATE_PENDING (element);
2979 /* this is the (new) state we should go to. TARGET is the last state we set on
2981 if (state != GST_STATE_TARGET (element)) {
2982 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2983 "setting target state to %s", gst_element_state_get_name (state));
2984 GST_STATE_TARGET (element) = state;
2985 /* increment state cookie so that we can track each state change. We only do
2986 * this if this is actually a new state change. */
2987 element->state_cookie++;
2989 GST_STATE_PENDING (element) = state;
2991 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2992 "current %s, old_pending %s, next %s, old return %s",
2993 gst_element_state_get_name (current),
2994 gst_element_state_get_name (old_pending),
2995 gst_element_state_get_name (next),
2996 gst_element_state_change_return_get_name (old_ret));
2998 /* if the element was busy doing a state change, we just update the
2999 * target state, it'll get to it async then. */
3000 if (old_pending != GST_STATE_VOID_PENDING) {
3001 /* upwards state change will happen ASYNC */
3002 if (old_pending <= state)
3004 /* element is going to this state already */
3005 else if (next == state)
3007 /* element was performing an ASYNC upward state change and
3008 * we request to go downward again. Start from the next pending
3010 else if (next > state
3011 && GST_STATE_RETURN (element) == GST_STATE_CHANGE_ASYNC) {
3015 next = GST_STATE_GET_NEXT (current, state);
3016 /* now we store the next state */
3017 GST_STATE_NEXT (element) = next;
3018 /* mark busy, we need to check that there is actually a state change
3019 * to be done else we could accidentally override SUCCESS/NO_PREROLL and
3020 * the default element change_state function has no way to know what the
3021 * old value was... could consider this a FIXME...*/
3022 if (current != next)
3023 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
3025 transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
3027 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3028 "%s: setting state from %s to %s",
3029 (next != state ? "intermediate" : "final"),
3030 gst_element_state_get_name (current), gst_element_state_get_name (next));
3032 /* now signal any waiters, they will error since the cookie was incremented */
3033 GST_STATE_BROADCAST (element);
3035 GST_OBJECT_UNLOCK (element);
3037 ret = gst_element_change_state (element, transition);
3039 GST_STATE_UNLOCK (element);
3041 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "returned %s",
3042 gst_element_state_change_return_get_name (ret));
3048 GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
3049 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3050 "element was busy with async state change");
3051 GST_OBJECT_UNLOCK (element);
3053 GST_STATE_UNLOCK (element);
3055 return GST_STATE_CHANGE_ASYNC;
3060 * gst_element_change_state:
3061 * @element: a #GstElement
3062 * @transition: the requested transition
3064 * Perform @transition on @element.
3066 * This function must be called with STATE_LOCK held and is mainly used
3069 * Returns: the #GstStateChangeReturn of the state transition.
3071 GstStateChangeReturn
3072 gst_element_change_state (GstElement * element, GstStateChange transition)
3074 GstElementClass *oclass;
3075 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3077 oclass = GST_ELEMENT_GET_CLASS (element);
3079 GST_TRACER_ELEMENT_CHANGE_STATE_PRE (element, transition);
3081 /* call the state change function so it can set the state */
3082 if (oclass->change_state)
3083 ret = (oclass->change_state) (element, transition);
3085 ret = GST_STATE_CHANGE_FAILURE;
3087 GST_TRACER_ELEMENT_CHANGE_STATE_POST (element, transition, ret);
3090 case GST_STATE_CHANGE_FAILURE:
3091 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
3092 "have FAILURE change_state return");
3093 /* state change failure */
3094 gst_element_abort_state (element);
3096 case GST_STATE_CHANGE_ASYNC:
3100 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3101 "element will change state ASYNC");
3103 target = GST_STATE_TARGET (element);
3105 if (target > GST_STATE_READY)
3108 /* else we just continue the state change downwards */
3109 GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
3110 "forcing commit state %s <= %s",
3111 gst_element_state_get_name (target),
3112 gst_element_state_get_name (GST_STATE_READY));
3114 ret = gst_element_continue_state (element, GST_STATE_CHANGE_SUCCESS);
3117 case GST_STATE_CHANGE_SUCCESS:
3118 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3119 "element changed state SUCCESS");
3120 /* we can commit the state now which will proceed to
3122 ret = gst_element_continue_state (element, ret);
3124 case GST_STATE_CHANGE_NO_PREROLL:
3125 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3126 "element changed state NO_PREROLL");
3127 /* we can commit the state now which will proceed to
3129 ret = gst_element_continue_state (element, ret);
3132 goto invalid_return;
3135 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit state change %d", ret);
3140 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit async state change %d",
3148 GST_OBJECT_LOCK (element);
3149 /* somebody added a GST_STATE_ and forgot to do stuff here ! */
3150 g_critical ("%s: unknown return value %d from a state change function",
3151 GST_ELEMENT_NAME (element), ret);
3153 /* we are in error now */
3154 ret = GST_STATE_CHANGE_FAILURE;
3155 GST_STATE_RETURN (element) = ret;
3156 GST_OBJECT_UNLOCK (element);
3162 /* gst_iterator_fold functions for pads_activate
3163 * Stop the iterator if activating one pad failed, but only if that pad
3164 * has not been removed from the element. */
3166 activate_pads (const GValue * vpad, GValue * ret, gboolean * active)
3168 GstPad *pad = g_value_get_object (vpad);
3169 gboolean cont = TRUE;
3171 if (!gst_pad_set_active (pad, *active)) {
3172 if (GST_PAD_PARENT (pad) != NULL) {
3174 g_value_set_boolean (ret, FALSE);
3181 /* returns false on error or early cutout of the fold, true if all
3182 * pads in @iter were (de)activated successfully. */
3184 iterator_activate_fold_with_resync (GstIterator * iter,
3185 GstIteratorFoldFunction func, gpointer user_data)
3187 GstIteratorResult ires;
3190 /* no need to unset this later, it's just a boolean */
3191 g_value_init (&ret, G_TYPE_BOOLEAN);
3192 g_value_set_boolean (&ret, TRUE);
3195 ires = gst_iterator_fold (iter, func, &ret, user_data);
3197 case GST_ITERATOR_RESYNC:
3198 /* need to reset the result again */
3199 g_value_set_boolean (&ret, TRUE);
3200 gst_iterator_resync (iter);
3202 case GST_ITERATOR_DONE:
3203 /* all pads iterated, return collected value */
3206 /* iterator returned _ERROR or premature end with _OK,
3207 * mark an error and exit */
3208 g_value_set_boolean (&ret, FALSE);
3213 /* return collected value */
3214 return g_value_get_boolean (&ret);
3217 /* is called with STATE_LOCK
3219 * Pads are activated from source pads to sinkpads.
3222 gst_element_pads_activate (GstElement * element, gboolean active)
3227 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3228 "%s pads", active ? "activate" : "deactivate");
3230 iter = gst_element_iterate_src_pads (element);
3232 iterator_activate_fold_with_resync (iter,
3233 (GstIteratorFoldFunction) activate_pads, &active);
3234 gst_iterator_free (iter);
3235 if (G_UNLIKELY (!res))
3238 iter = gst_element_iterate_sink_pads (element);
3240 iterator_activate_fold_with_resync (iter,
3241 (GstIteratorFoldFunction) activate_pads, &active);
3242 gst_iterator_free (iter);
3243 if (G_UNLIKELY (!res))
3246 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3247 "pad %sactivation successful", active ? "" : "de");
3254 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3255 "pad %sactivation failed", active ? "" : "de");
3260 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3261 "sink pads_activate failed");
3266 /* is called with STATE_LOCK */
3267 static GstStateChangeReturn
3268 gst_element_change_state_func (GstElement * element, GstStateChange transition)
3270 GstState state, next;
3271 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
3273 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
3275 state = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
3276 next = GST_STATE_TRANSITION_NEXT (transition);
3278 /* if the element already is in the given state, we just return success */
3279 if (next == GST_STATE_VOID_PENDING || state == next)
3282 GST_CAT_LOG_OBJECT (GST_CAT_STATES, element,
3283 "default handler tries setting state from %s to %s (%04x)",
3284 gst_element_state_get_name (state),
3285 gst_element_state_get_name (next), transition);
3287 switch (transition) {
3288 case GST_STATE_CHANGE_NULL_TO_READY:
3290 case GST_STATE_CHANGE_READY_TO_PAUSED:
3291 if (!gst_element_pads_activate (element, TRUE)) {
3292 result = GST_STATE_CHANGE_FAILURE;
3295 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3297 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3299 case GST_STATE_CHANGE_PAUSED_TO_READY:
3300 case GST_STATE_CHANGE_READY_TO_NULL:{
3303 /* deactivate pads in both cases, since they are activated on
3304 ready->paused but the element might not have made it to paused */
3305 if (!gst_element_pads_activate (element, FALSE)) {
3306 result = GST_STATE_CHANGE_FAILURE;
3309 /* Remove all non-persistent contexts */
3310 GST_OBJECT_LOCK (element);
3311 for (l = element->contexts; l;) {
3312 GstContext *context = l->data;
3314 if (!gst_context_is_persistent (context)) {
3317 gst_context_unref (context);
3319 element->contexts = g_list_delete_link (element->contexts, l);
3325 GST_OBJECT_UNLOCK (element);
3329 /* this will catch real but unhandled state changes;
3330 * can only be caused by:
3331 * - a new state was added
3332 * - somehow the element was asked to jump across an intermediate state
3334 g_warning ("Unhandled state change from %s to %s",
3335 gst_element_state_get_name (state),
3336 gst_element_state_get_name (next));
3343 GST_OBJECT_LOCK (element);
3344 result = GST_STATE_RETURN (element);
3345 GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
3346 "element is already in the %s state",
3347 gst_element_state_get_name (state));
3348 GST_OBJECT_UNLOCK (element);
3355 * gst_element_get_factory:
3356 * @element: a #GstElement to request the element factory of.
3358 * Retrieves the factory that was used to create this element.
3360 * Returns: (transfer none) (nullable): the #GstElementFactory used for creating this
3361 * element or %NULL if element has not been registered (static element). no refcounting is needed.
3364 gst_element_get_factory (GstElement * element)
3366 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3368 return GST_ELEMENT_GET_CLASS (element)->elementfactory;
3372 gst_element_dispose (GObject * object)
3374 GstElement *element = GST_ELEMENT_CAST (object);
3377 GstElementClass *oclass;
3380 oclass = GST_ELEMENT_GET_CLASS (element);
3382 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p dispose", element);
3384 if (GST_STATE (element) != GST_STATE_NULL)
3387 /* start by releasing all request pads, this might also remove some dynamic
3389 walk = element->pads;
3391 GstPad *pad = GST_PAD_CAST (walk->data);
3395 if (oclass->release_pad && GST_PAD_PAD_TEMPLATE (pad) &&
3396 GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (pad))
3397 == GST_PAD_REQUEST) {
3398 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3399 "removing request pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3400 oclass->release_pad (element, pad);
3402 /* in case the release_pad function removed the next pad too */
3403 if (walk && g_list_position (element->pads, walk) == -1)
3404 walk = element->pads;
3407 /* remove the remaining pads */
3408 while (element->pads) {
3409 GstPad *pad = GST_PAD_CAST (element->pads->data);
3410 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3411 "removing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3412 if (!gst_element_remove_pad (element, pad)) {
3413 /* only happens when someone unparented our pad.. */
3414 g_critical ("failed to remove pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3419 GST_OBJECT_LOCK (element);
3420 clock_p = &element->clock;
3421 bus_p = &element->bus;
3422 gst_object_replace ((GstObject **) clock_p, NULL);
3423 gst_object_replace ((GstObject **) bus_p, NULL);
3424 g_list_free_full (element->contexts, (GDestroyNotify) gst_context_unref);
3425 element->contexts = NULL;
3426 GST_OBJECT_UNLOCK (element);
3428 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p parent class dispose",
3431 G_OBJECT_CLASS (parent_class)->dispose (object);
3440 is_locked = GST_ELEMENT_IS_LOCKED_STATE (element);
3442 ("\nTrying to dispose element %s, but it is in %s%s instead of the NULL"
3444 "You need to explicitly set elements to the NULL state before\n"
3445 "dropping the final reference, to allow them to clean up.\n"
3446 "This problem may also be caused by a refcounting bug in the\n"
3447 "application or some element.\n",
3448 GST_OBJECT_NAME (element),
3449 gst_element_state_get_name (GST_STATE (element)),
3450 is_locked ? " (locked)" : "");
3456 gst_element_finalize (GObject * object)
3458 GstElement *element = GST_ELEMENT_CAST (object);
3460 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p finalize", element);
3462 g_cond_clear (&element->state_cond);
3463 g_rec_mutex_clear (&element->state_lock);
3465 GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "%p finalize parent",
3468 G_OBJECT_CLASS (parent_class)->finalize (object);
3472 gst_element_set_bus_func (GstElement * element, GstBus * bus)
3476 g_return_if_fail (GST_IS_ELEMENT (element));
3478 GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element, "setting bus to %p", bus);
3480 GST_OBJECT_LOCK (element);
3481 bus_p = &GST_ELEMENT_BUS (element);
3482 gst_object_replace ((GstObject **) bus_p, GST_OBJECT_CAST (bus));
3483 GST_OBJECT_UNLOCK (element);
3487 * gst_element_set_bus:
3488 * @element: a #GstElement to set the bus of.
3489 * @bus: (transfer none) (allow-none): the #GstBus to set.
3491 * Sets the bus of the element. Increases the refcount on the bus.
3492 * For internal use only, unless you're testing elements.
3497 gst_element_set_bus (GstElement * element, GstBus * bus)
3499 GstElementClass *oclass;
3501 g_return_if_fail (GST_IS_ELEMENT (element));
3503 oclass = GST_ELEMENT_GET_CLASS (element);
3505 if (oclass->set_bus)
3506 oclass->set_bus (element, bus);
3510 * gst_element_get_bus:
3511 * @element: a #GstElement to get the bus of.
3513 * Returns the bus of the element. Note that only a #GstPipeline will provide a
3514 * bus for the application.
3516 * Returns: (transfer full) (nullable): the element's #GstBus. unref after
3522 gst_element_get_bus (GstElement * element)
3524 GstBus *result = NULL;
3526 g_return_val_if_fail (GST_IS_ELEMENT (element), result);
3528 GST_OBJECT_LOCK (element);
3529 if ((result = GST_ELEMENT_BUS (element)))
3530 gst_object_ref (result);
3531 GST_OBJECT_UNLOCK (element);
3533 GST_CAT_DEBUG_OBJECT (GST_CAT_BUS, element, "got bus %" GST_PTR_FORMAT,
3540 gst_element_set_context_default (GstElement * element, GstContext * context)
3542 const gchar *context_type;
3545 g_return_if_fail (GST_IS_CONTEXT (context));
3546 context_type = gst_context_get_context_type (context);
3547 g_return_if_fail (context_type != NULL);
3549 GST_OBJECT_LOCK (element);
3550 for (l = element->contexts; l; l = l->next) {
3551 GstContext *tmp = l->data;
3552 const gchar *tmp_type = gst_context_get_context_type (tmp);
3554 /* Always store newest context but never replace
3555 * a persistent one by a non-persistent one */
3556 if (g_strcmp0 (context_type, tmp_type) == 0 &&
3557 (gst_context_is_persistent (context) ||
3558 !gst_context_is_persistent (tmp))) {
3559 gst_context_replace ((GstContext **) & l->data, context);
3563 /* Not found? Add */
3566 g_list_prepend (element->contexts, gst_context_ref (context));
3568 GST_OBJECT_UNLOCK (element);
3572 * gst_element_set_context:
3573 * @element: a #GstElement to set the context of.
3574 * @context: (transfer none): the #GstContext to set.
3576 * Sets the context of the element. Increases the refcount of the context.
3581 gst_element_set_context (GstElement * element, GstContext * context)
3583 GstElementClass *oclass;
3585 g_return_if_fail (GST_IS_ELEMENT (element));
3586 g_return_if_fail (GST_IS_CONTEXT (context));
3588 oclass = GST_ELEMENT_GET_CLASS (element);
3590 GST_CAT_DEBUG_OBJECT (GST_CAT_CONTEXT, element,
3591 "set context %p %" GST_PTR_FORMAT, context,
3592 gst_context_get_structure (context));
3594 if (oclass->set_context)
3595 oclass->set_context (element, context);
3599 * gst_element_get_contexts:
3600 * @element: a #GstElement to set the context of.
3602 * Gets the contexts set on the element.
3606 * Returns: (element-type Gst.Context) (transfer full): List of #GstContext
3611 gst_element_get_contexts (GstElement * element)
3615 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3617 GST_OBJECT_LOCK (element);
3618 ret = g_list_copy_deep (element->contexts, (GCopyFunc) gst_context_ref, NULL);
3619 GST_OBJECT_UNLOCK (element);
3625 _match_context_type (GstContext * c1, const gchar * context_type)
3627 const gchar *c1_type;
3629 c1_type = gst_context_get_context_type (c1);
3631 return g_strcmp0 (c1_type, context_type);
3635 * gst_element_get_context_unlocked:
3636 * @element: a #GstElement to get the context of.
3637 * @context_type: a name of a context to retrieve
3639 * Gets the context with @context_type set on the element or NULL.
3641 * Returns: (transfer full) (nullable): A #GstContext or NULL
3646 gst_element_get_context_unlocked (GstElement * element,
3647 const gchar * context_type)
3649 GstContext *ret = NULL;
3652 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3655 g_list_find_custom (element->contexts, context_type,
3656 (GCompareFunc) _match_context_type);
3657 if (node && node->data)
3658 ret = gst_context_ref (node->data);
3664 * gst_element_get_context:
3665 * @element: a #GstElement to get the context of.
3666 * @context_type: a name of a context to retrieve
3668 * Gets the context with @context_type set on the element or NULL.
3672 * Returns: (transfer full) (nullable): A #GstContext or NULL
3677 gst_element_get_context (GstElement * element, const gchar * context_type)
3679 GstContext *ret = NULL;
3681 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
3683 GST_OBJECT_LOCK (element);
3684 ret = gst_element_get_context_unlocked (element, context_type);
3685 GST_OBJECT_UNLOCK (element);
3691 gst_element_property_post_notify_msg (GstElement * element, GObject * obj,
3692 GParamSpec * pspec, gboolean include_value)
3694 GValue val = G_VALUE_INIT;
3697 GST_LOG_OBJECT (element, "property '%s' of object %" GST_PTR_FORMAT " has "
3698 "changed, posting message with%s value", pspec->name, obj,
3699 include_value ? "" : "out");
3701 if (include_value && (pspec->flags & G_PARAM_READABLE) != 0) {
3702 g_value_init (&val, pspec->value_type);
3703 g_object_get_property (obj, pspec->name, &val);
3708 gst_element_post_message (element,
3709 gst_message_new_property_notify (GST_OBJECT_CAST (obj), pspec->name, v));
3713 gst_element_property_deep_notify_cb (GstElement * element, GObject * prop_obj,
3714 GParamSpec * pspec, gpointer user_data)
3716 gboolean include_value = GPOINTER_TO_INT (user_data);
3718 gst_element_property_post_notify_msg (element, prop_obj, pspec,
3723 gst_element_property_notify_cb (GObject * obj, GParamSpec * pspec,
3726 gboolean include_value = GPOINTER_TO_INT (user_data);
3728 gst_element_property_post_notify_msg (GST_ELEMENT_CAST (obj), obj, pspec,
3733 * gst_element_add_property_notify_watch:
3734 * @element: a #GstElement to watch for property changes
3735 * @property_name: (allow-none): name of property to watch for changes, or
3736 * NULL to watch all properties
3737 * @include_value: whether to include the new property value in the message
3739 * Returns: a watch id, which can be used in connection with
3740 * gst_element_remove_property_notify_watch() to remove the watch again.
3745 gst_element_add_property_notify_watch (GstElement * element,
3746 const gchar * property_name, gboolean include_value)
3752 g_return_val_if_fail (GST_IS_ELEMENT (element), 0);
3754 sep = (property_name != NULL) ? "::" : NULL;
3755 signal_name = g_strconcat ("notify", sep, property_name, NULL);
3756 id = g_signal_connect (element, signal_name,
3757 G_CALLBACK (gst_element_property_notify_cb),
3758 GINT_TO_POINTER (include_value));
3759 g_free (signal_name);
3765 * gst_element_add_property_deep_notify_watch:
3766 * @element: a #GstElement to watch (recursively) for property changes
3767 * @property_name: (allow-none): name of property to watch for changes, or
3768 * NULL to watch all properties
3769 * @include_value: whether to include the new property value in the message
3771 * Returns: a watch id, which can be used in connection with
3772 * gst_element_remove_property_notify_watch() to remove the watch again.
3777 gst_element_add_property_deep_notify_watch (GstElement * element,
3778 const gchar * property_name, gboolean include_value)
3784 g_return_val_if_fail (GST_IS_ELEMENT (element), 0);
3786 sep = (property_name != NULL) ? "::" : NULL;
3787 signal_name = g_strconcat ("deep-notify", sep, property_name, NULL);
3788 id = g_signal_connect (element, signal_name,
3789 G_CALLBACK (gst_element_property_deep_notify_cb),
3790 GINT_TO_POINTER (include_value));
3791 g_free (signal_name);
3797 * gst_element_remove_property_notify_watch:
3798 * @element: a #GstElement being watched for property changes
3799 * @watch_id: watch id to remove
3804 gst_element_remove_property_notify_watch (GstElement * element, gulong watch_id)
3806 g_signal_handler_disconnect (element, watch_id);
3811 GstElement *element;
3812 GstElementCallAsyncFunc func;
3814 GDestroyNotify destroy_notify;
3815 } GstElementCallAsyncData;
3818 gst_element_call_async_func (gpointer data, gpointer user_data)
3820 GstElementCallAsyncData *async_data = data;
3822 async_data->func (async_data->element, async_data->user_data);
3823 if (async_data->destroy_notify)
3824 async_data->destroy_notify (async_data->user_data);
3825 gst_object_unref (async_data->element);
3826 g_free (async_data);
3830 * gst_element_call_async:
3831 * @element: a #GstElement
3832 * @func: Function to call asynchronously from another thread
3833 * @user_data: Data to pass to @func
3834 * @destroy_notify: GDestroyNotify for @user_data
3836 * Calls @func from another thread and passes @user_data to it. This is to be
3837 * used for cases when a state change has to be performed from a streaming
3838 * thread, directly via gst_element_set_state() or indirectly e.g. via SEEK
3841 * Calling those functions directly from the streaming thread will cause
3842 * deadlocks in many situations, as they might involve waiting for the
3843 * streaming thread to shut down from this very streaming thread.
3850 gst_element_call_async (GstElement * element, GstElementCallAsyncFunc func,
3851 gpointer user_data, GDestroyNotify destroy_notify)
3853 GstElementCallAsyncData *async_data;
3855 g_return_if_fail (GST_IS_ELEMENT (element));
3857 async_data = g_new0 (GstElementCallAsyncData, 1);
3858 async_data->element = gst_object_ref (element);
3859 async_data->func = func;
3860 async_data->user_data = user_data;
3861 async_data->destroy_notify = destroy_notify;
3863 g_mutex_lock (&_element_pool_lock);
3864 if (G_UNLIKELY (gst_element_pool == NULL))
3865 gst_element_pool = gst_element_setup_thread_pool ();
3866 g_thread_pool_push ((GThreadPool *) gst_element_pool, async_data, NULL);
3867 g_mutex_unlock (&_element_pool_lock);
3871 _priv_gst_element_cleanup (void)
3873 g_mutex_lock (&_element_pool_lock);
3874 if (gst_element_pool) {
3875 g_thread_pool_free ((GThreadPool *) gst_element_pool, FALSE, TRUE);
3876 gst_element_pool = NULL;
3878 g_mutex_unlock (&_element_pool_lock);
3882 * gst_make_element_message_details:
3883 * @name: Name of the first field to set
3884 * @...: variable arguments in the same form as #GstStructure
3886 * Create a #GstStructure to be used with #gst_element_message_full_with_details.
3887 * %NULL terminator required.
3892 gst_make_element_message_details (const char *name, ...)
3894 GstStructure *structure;
3900 va_start (varargs, name);
3901 structure = gst_structure_new_valist ("details", name, varargs);