2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstpad.c: Pads for linking elements together
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.
25 * @short_description: Object contained by elements that allows links to
27 * @see_also: #GstPadTemplate, #GstElement, #GstEvent, #GstQuery, #GstBuffer
29 * A #GstElement is linked to other elements via "pads", which are extremely
30 * light-weight generic link points.
32 * Pads have a #GstPadDirection, source pads produce data, sink pads consume
35 * Pads are typically created from a #GstPadTemplate with
36 * gst_pad_new_from_template() and are then added to a #GstElement. This usually
37 * happens when the element is created but it can also happen dynamically based
38 * on the data that the element is processing or based on the pads that the
39 * application requests.
41 * Pads without pad templates can be created with gst_pad_new(),
42 * which takes a direction and a name as an argument. If the name is %NULL,
43 * then a guaranteed unique name will be assigned to it.
45 * A #GstElement creating a pad will typically use the various
46 * gst_pad_set_*_function\() calls to register callbacks for events, queries or
47 * dataflow on the pads.
49 * gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
51 * After two pads are retrieved from an element by gst_element_get_static_pad(),
52 * the pads can be linked with gst_pad_link(). (For quick links,
53 * you can also use gst_element_link(), which will make the obvious
54 * link for you if it's straightforward.). Pads can be unlinked again with
55 * gst_pad_unlink(). gst_pad_get_peer() can be used to check what the pad is
58 * Before dataflow is possible on the pads, they need to be activated with
59 * gst_pad_set_active().
61 * gst_pad_query() and gst_pad_peer_query() can be used to query various
62 * properties of the pad and the stream.
64 * To send a #GstEvent on a pad, use gst_pad_send_event() and
65 * gst_pad_push_event(). Some events will be sticky on the pad, meaning that
66 * after they pass on the pad they can be queried later with
67 * gst_pad_get_sticky_event() and gst_pad_sticky_events_foreach().
68 * gst_pad_get_current_caps() and gst_pad_has_current_caps() are convenience
69 * functions to query the current sticky CAPS event on a pad.
71 * GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
72 * or pull in a buffer.
74 * The dataflow, events and queries that happen on a pad can be monitored with
75 * probes that can be installed with gst_pad_add_probe(). gst_pad_is_blocked()
76 * can be used to check if a block probe is installed on the pad.
77 * gst_pad_is_blocking() checks if the blocking probe is currently blocking the
78 * pad. gst_pad_remove_probe() is used to remove a previously installed probe
79 * and unblock blocking probes if any.
81 * Pad have an offset that can be retrieved with gst_pad_get_offset(). This
82 * offset will be applied to the running_time of all data passing over the pad.
83 * gst_pad_set_offset() can be used to change the offset.
85 * Convenience functions exist to start, pause and stop the task on a pad with
86 * gst_pad_start_task(), gst_pad_pause_task() and gst_pad_stop_task()
90 #include "gst_private.h"
93 #include "gstpadtemplate.h"
94 #include "gstenumtypes.h"
98 #include "gsttracerutils.h"
100 #include "glib-compat-private.h"
102 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
103 #define GST_CAT_DEFAULT GST_CAT_PADS
105 /* Pad signals and args */
124 #define _PAD_PROBE_TYPE_ALL_BOTH_AND_FLUSH (GST_PAD_PROBE_TYPE_ALL_BOTH | GST_PAD_PROBE_TYPE_EVENT_FLUSH)
126 /* we have a pending and an active event on the pad. On source pads only the
127 * active event is used. On sinkpads, events are copied to the pending entry and
128 * moved to the active event when the eventfunc returned %TRUE. */
135 struct _GstPadPrivate
142 guint probe_list_cookie;
144 /* counter of how many idle probes are running directly from the add_probe
145 * call. Used to block any data flowing in the pad while the idle callback
146 * Doesn't finish its work */
149 /* conditional and variable used to ensure pads only get (de)activated
150 * by a single thread at a time. Protected by the object lock */
151 GCond activation_cond;
152 gboolean in_activation;
160 #define GST_PAD_IS_RUNNING_IDLE_PROBE(p) \
161 (((GstPad *)(p))->priv->idle_running > 0)
166 GstPadProbeInfo *info;
172 gulong *called_probes;
173 guint n_called_probes;
174 guint called_probes_size;
178 static void gst_pad_dispose (GObject * object);
179 static void gst_pad_finalize (GObject * object);
180 static void gst_pad_set_property (GObject * object, guint prop_id,
181 const GValue * value, GParamSpec * pspec);
182 static void gst_pad_get_property (GObject * object, guint prop_id,
183 GValue * value, GParamSpec * pspec);
185 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
186 static gboolean gst_pad_activate_default (GstPad * pad, GstObject * parent);
187 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
188 GstObject * parent, GstBufferList * list);
190 static GstFlowReturn gst_pad_send_event_unchecked (GstPad * pad,
191 GstEvent * event, GstPadProbeType type);
192 static GstFlowReturn gst_pad_push_event_unchecked (GstPad * pad,
193 GstEvent * event, GstPadProbeType type);
195 static gboolean activate_mode_internal (GstPad * pad, GstObject * parent,
196 GstPadMode mode, gboolean active);
198 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
200 static GParamSpec *pspec_caps = NULL;
202 /* quarks for probe signals */
203 static GQuark buffer_quark;
204 static GQuark buffer_list_quark;
205 static GQuark event_quark;
214 static GstFlowQuarks flow_quarks[] = {
215 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
216 {GST_FLOW_OK, "ok", 0},
217 {GST_FLOW_NOT_LINKED, "not-linked", 0},
218 {GST_FLOW_FLUSHING, "flushing", 0},
219 {GST_FLOW_EOS, "eos", 0},
220 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
221 {GST_FLOW_ERROR, "error", 0},
222 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
223 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
228 * @ret: a #GstFlowReturn to get the name of.
230 * Gets a string representing the given flow return.
232 * Returns: a static string with the name of the flow return.
235 gst_flow_get_name (GstFlowReturn ret)
239 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
241 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
242 if (ret == flow_quarks[i].ret)
243 return flow_quarks[i].name;
250 * @ret: a #GstFlowReturn to get the quark of.
252 * Get the unique quark for the given GstFlowReturn.
254 * Returns: the quark associated with the flow return or 0 if an
255 * invalid return was specified.
258 gst_flow_to_quark (GstFlowReturn ret)
262 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
264 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
265 if (ret == flow_quarks[i].ret)
266 return flow_quarks[i].quark;
272 * gst_pad_link_get_name:
273 * @ret: a #GstPadLinkReturn to get the name of.
275 * Gets a string representing the given pad-link return.
277 * Returns: a static string with the name of the pad-link return.
282 gst_pad_link_get_name (GstPadLinkReturn ret)
285 case GST_PAD_LINK_OK:
287 case GST_PAD_LINK_WRONG_HIERARCHY:
288 return "wrong hierarchy";
289 case GST_PAD_LINK_WAS_LINKED:
291 case GST_PAD_LINK_WRONG_DIRECTION:
292 return "wrong direction";
293 case GST_PAD_LINK_NOFORMAT:
294 return "no common format";
295 case GST_PAD_LINK_NOSCHED:
296 return "incompatible scheduling";
297 case GST_PAD_LINK_REFUSED:
300 g_return_val_if_reached ("unknown");
307 buffer_quark = g_quark_from_static_string ("buffer"); \
308 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
309 event_quark = g_quark_from_static_string ("event"); \
311 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
312 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
315 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
316 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
319 #define gst_pad_parent_class parent_class
320 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT,
321 G_ADD_PRIVATE (GstPad) _do_init);
324 gst_pad_class_init (GstPadClass * klass)
326 GObjectClass *gobject_class;
327 GstObjectClass *gstobject_class;
329 gobject_class = G_OBJECT_CLASS (klass);
330 gstobject_class = GST_OBJECT_CLASS (klass);
332 gobject_class->dispose = gst_pad_dispose;
333 gobject_class->finalize = gst_pad_finalize;
334 gobject_class->set_property = gst_pad_set_property;
335 gobject_class->get_property = gst_pad_get_property;
339 * @pad: the pad that emitted the signal
340 * @peer: the peer pad that has been connected
342 * Signals that a pad has been linked to the peer pad.
344 gst_pad_signals[PAD_LINKED] =
345 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
346 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
347 NULL, G_TYPE_NONE, 1, GST_TYPE_PAD);
350 * @pad: the pad that emitted the signal
351 * @peer: the peer pad that has been disconnected
353 * Signals that a pad has been unlinked from the peer pad.
355 gst_pad_signals[PAD_UNLINKED] =
356 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
357 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
358 NULL, G_TYPE_NONE, 1, GST_TYPE_PAD);
360 pspec_caps = g_param_spec_boxed ("caps", "Caps",
361 "The capabilities of the pad", GST_TYPE_CAPS,
362 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
363 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
365 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
366 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
367 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
368 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
370 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
371 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
372 g_param_spec_object ("template", "Template",
373 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
374 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
379 * The offset that will be applied to the running time of the pad.
383 g_object_class_install_property (gobject_class, PAD_PROP_OFFSET,
384 g_param_spec_int64 ("offset", "Offset",
385 "The running time offset of the pad", 0, G_MAXINT64, 0,
386 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
388 gstobject_class->path_string_separator = ".";
390 /* Register common function pointer descriptions */
391 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
392 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
393 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
394 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
395 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
399 gst_pad_init (GstPad * pad)
401 pad->priv = gst_pad_get_instance_private (pad);
403 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
405 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
406 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
407 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
408 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
409 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
411 GST_PAD_SET_FLUSHING (pad);
413 g_rec_mutex_init (&pad->stream_rec_lock);
415 g_cond_init (&pad->block_cond);
417 g_hook_list_init (&pad->probes, sizeof (GstProbe));
419 pad->priv->events = g_array_sized_new (FALSE, TRUE, sizeof (PadEvent), 16);
420 pad->priv->events_cookie = 0;
421 pad->priv->last_cookie = -1;
422 g_cond_init (&pad->priv->activation_cond);
424 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
427 /* called when setting the pad inactive. It removes all sticky events from
428 * the pad. must be called with object lock */
430 remove_events (GstPad * pad)
434 gboolean notify = FALSE;
436 events = pad->priv->events;
439 for (i = 0; i < len; i++) {
440 PadEvent *ev = &g_array_index (events, PadEvent, i);
441 GstEvent *event = ev->event;
445 if (event && GST_EVENT_TYPE (event) == GST_EVENT_CAPS)
448 gst_event_unref (event);
451 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
452 g_array_set_size (events, 0);
453 pad->priv->events_cookie++;
456 GST_OBJECT_UNLOCK (pad);
458 GST_DEBUG_OBJECT (pad, "notify caps");
459 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
461 GST_OBJECT_LOCK (pad);
465 /* should be called with object lock */
467 find_event_by_type (GstPad * pad, GstEventType type, guint idx)
473 events = pad->priv->events;
476 for (i = 0; i < len; i++) {
477 ev = &g_array_index (events, PadEvent, i);
478 if (ev->event == NULL)
481 if (GST_EVENT_TYPE (ev->event) == type) {
485 } else if (GST_EVENT_TYPE (ev->event) > type) {
494 /* should be called with OBJECT lock */
496 find_event (GstPad * pad, GstEvent * event)
502 events = pad->priv->events;
505 for (i = 0; i < len; i++) {
506 ev = &g_array_index (events, PadEvent, i);
507 if (event == ev->event)
509 else if (GST_EVENT_TYPE (ev->event) > GST_EVENT_TYPE (event))
517 /* should be called with OBJECT lock */
519 remove_event_by_type (GstPad * pad, GstEventType type)
525 events = pad->priv->events;
530 ev = &g_array_index (events, PadEvent, i);
531 if (ev->event == NULL)
534 if (GST_EVENT_TYPE (ev->event) > type)
536 else if (GST_EVENT_TYPE (ev->event) != type)
539 gst_event_unref (ev->event);
540 g_array_remove_index (events, i);
542 pad->priv->events_cookie++;
550 /* check all events on srcpad against those on sinkpad. All events that are not
551 * on sinkpad are marked as received=%FALSE and the PENDING_EVENTS is set on the
552 * srcpad so that the events will be sent next time */
553 /* should be called with srcpad and sinkpad LOCKS */
555 schedule_events (GstPad * srcpad, GstPad * sinkpad)
560 gboolean pending = FALSE;
562 events = srcpad->priv->events;
565 for (i = 0; i < len; i++) {
566 ev = &g_array_index (events, PadEvent, i);
567 if (ev->event == NULL)
570 if (sinkpad == NULL || !find_event (sinkpad, ev->event)) {
571 ev->received = FALSE;
576 GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PENDING_EVENTS);
579 typedef gboolean (*PadEventFunction) (GstPad * pad, PadEvent * ev,
582 /* should be called with pad LOCK */
584 events_foreach (GstPad * pad, PadEventFunction func, gpointer user_data)
591 events = pad->priv->events;
594 cookie = pad->priv->events_cookie;
598 PadEvent *ev, ev_ret;
600 ev = &g_array_index (events, PadEvent, i);
601 if (G_UNLIKELY (ev->event == NULL))
604 /* take additional ref, func might release the lock */
605 ev_ret.event = gst_event_ref (ev->event);
606 ev_ret.received = ev->received;
608 ret = func (pad, &ev_ret, user_data);
610 /* recheck the cookie, lock might have been released and the list could have
612 if (G_UNLIKELY (cookie != pad->priv->events_cookie)) {
613 if (G_LIKELY (ev_ret.event))
614 gst_event_unref (ev_ret.event);
618 /* store the received state */
619 ev->received = ev_ret.received;
621 /* if the event changed, we need to do something */
622 if (G_UNLIKELY (ev->event != ev_ret.event)) {
623 if (G_UNLIKELY (ev_ret.event == NULL)) {
624 /* function unreffed and set the event to NULL, remove it */
625 gst_event_unref (ev->event);
626 g_array_remove_index (events, i);
628 cookie = ++pad->priv->events_cookie;
631 /* function gave a new event for us */
632 gst_event_take (&ev->event, ev_ret.event);
635 /* just unref, nothing changed */
636 gst_event_unref (ev_ret.event);
645 /* should be called with LOCK */
647 _apply_pad_offset (GstPad * pad, GstEvent * event, gboolean upstream,
652 GST_DEBUG_OBJECT (pad, "apply pad offset %" GST_STIME_FORMAT,
653 GST_STIME_ARGS (pad_offset));
655 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
658 g_assert (!upstream);
660 /* copy segment values */
661 gst_event_copy_segment (event, &segment);
662 gst_event_unref (event);
664 gst_segment_offset_running_time (&segment, segment.format, pad_offset);
665 event = gst_event_new_segment (&segment);
668 event = gst_event_make_writable (event);
669 offset = gst_event_get_running_time_offset (event);
671 offset -= pad_offset;
673 offset += pad_offset;
674 gst_event_set_running_time_offset (event, offset);
679 static inline GstEvent *
680 apply_pad_offset (GstPad * pad, GstEvent * event, gboolean upstream)
682 if (G_UNLIKELY (pad->offset != 0))
683 return _apply_pad_offset (pad, event, upstream, pad->offset);
687 /* should be called with the OBJECT_LOCK */
689 get_pad_caps (GstPad * pad)
691 GstCaps *caps = NULL;
694 ev = find_event_by_type (pad, GST_EVENT_CAPS, 0);
696 gst_event_parse_caps (ev->event, &caps);
702 gst_pad_dispose (GObject * object)
704 GstPad *pad = GST_PAD_CAST (object);
707 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "%p dispose", pad);
709 /* unlink the peer pad */
710 if ((peer = gst_pad_get_peer (pad))) {
711 /* window for MT unsafeness, someone else could unlink here
712 * and then we call unlink with wrong pads. The unlink
713 * function would catch this and safely return failed. */
714 if (GST_PAD_IS_SRC (pad))
715 gst_pad_unlink (pad, peer);
717 gst_pad_unlink (peer, pad);
719 gst_object_unref (peer);
722 gst_pad_set_pad_template (pad, NULL);
724 GST_OBJECT_LOCK (pad);
726 g_hook_list_clear (&pad->probes);
727 GST_OBJECT_UNLOCK (pad);
729 G_OBJECT_CLASS (parent_class)->dispose (object);
733 gst_pad_finalize (GObject * object)
735 GstPad *pad = GST_PAD_CAST (object);
738 /* in case the task is still around, clean it up */
739 if ((task = GST_PAD_TASK (pad))) {
740 gst_task_join (task);
741 GST_PAD_TASK (pad) = NULL;
742 gst_object_unref (task);
745 if (pad->activatenotify)
746 pad->activatenotify (pad->activatedata);
747 if (pad->activatemodenotify)
748 pad->activatemodenotify (pad->activatemodedata);
750 pad->linknotify (pad->linkdata);
751 if (pad->unlinknotify)
752 pad->unlinknotify (pad->unlinkdata);
753 if (pad->chainnotify)
754 pad->chainnotify (pad->chaindata);
755 if (pad->chainlistnotify)
756 pad->chainlistnotify (pad->chainlistdata);
757 if (pad->getrangenotify)
758 pad->getrangenotify (pad->getrangedata);
759 if (pad->eventnotify)
760 pad->eventnotify (pad->eventdata);
761 if (pad->querynotify)
762 pad->querynotify (pad->querydata);
763 if (pad->iterintlinknotify)
764 pad->iterintlinknotify (pad->iterintlinkdata);
766 g_rec_mutex_clear (&pad->stream_rec_lock);
767 g_cond_clear (&pad->block_cond);
768 g_cond_clear (&pad->priv->activation_cond);
769 g_array_free (pad->priv->events, TRUE);
771 G_OBJECT_CLASS (parent_class)->finalize (object);
775 gst_pad_set_property (GObject * object, guint prop_id,
776 const GValue * value, GParamSpec * pspec)
778 g_return_if_fail (GST_IS_PAD (object));
781 case PAD_PROP_DIRECTION:
782 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
784 case PAD_PROP_TEMPLATE:
785 gst_pad_set_pad_template (GST_PAD_CAST (object),
786 (GstPadTemplate *) g_value_get_object (value));
788 case PAD_PROP_OFFSET:
789 gst_pad_set_offset (GST_PAD_CAST (object), g_value_get_int64 (value));
792 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
798 gst_pad_get_property (GObject * object, guint prop_id,
799 GValue * value, GParamSpec * pspec)
801 g_return_if_fail (GST_IS_PAD (object));
805 GST_OBJECT_LOCK (object);
806 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
807 GST_OBJECT_UNLOCK (object);
809 case PAD_PROP_DIRECTION:
810 g_value_set_enum (value, GST_PAD_DIRECTION (object));
812 case PAD_PROP_TEMPLATE:
813 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
815 case PAD_PROP_OFFSET:
816 g_value_set_int64 (value, gst_pad_get_offset (GST_PAD_CAST (object)));
819 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
826 * @name: (allow-none): the name of the new pad.
827 * @direction: the #GstPadDirection of the pad.
829 * Creates a new pad with the given name in the given direction.
830 * If name is %NULL, a guaranteed unique name (across all pads)
832 * This function makes a copy of the name so you can safely free the name.
834 * Returns: (transfer floating): a new #GstPad.
839 gst_pad_new (const gchar * name, GstPadDirection direction)
841 return g_object_new (GST_TYPE_PAD,
842 "name", name, "direction", direction, NULL);
846 * gst_pad_new_from_template:
847 * @templ: the pad template to use
848 * @name: (allow-none): the name of the pad
850 * Creates a new pad with the given name from the given template.
851 * If name is %NULL, a guaranteed unique name (across all pads)
853 * This function makes a copy of the name so you can safely free the name.
855 * Returns: (transfer floating): a new #GstPad.
858 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
861 GST_PAD_TEMPLATE_GTYPE (templ) ==
862 G_TYPE_NONE ? GST_TYPE_PAD : GST_PAD_TEMPLATE_GTYPE (templ);
864 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
866 return g_object_new (pad_type,
867 "name", name, "direction", templ->direction, "template", templ, NULL);
871 * gst_pad_new_from_static_template:
872 * @templ: the #GstStaticPadTemplate to use
873 * @name: the name of the pad
875 * Creates a new pad with the given name from the given static template.
876 * If name is %NULL, a guaranteed unique name (across all pads)
878 * This function makes a copy of the name so you can safely free the name.
880 * Returns: (transfer floating): a new #GstPad.
883 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
887 GstPadTemplate *template;
889 template = gst_static_pad_template_get (templ);
890 pad = gst_pad_new_from_template (template, name);
891 gst_object_unref (template);
895 #define ACQUIRE_PARENT(pad, parent, label) \
897 if (G_LIKELY ((parent = GST_OBJECT_PARENT (pad)))) \
898 gst_object_ref (parent); \
899 else if (G_LIKELY (GST_PAD_NEEDS_PARENT (pad))) \
903 #define RELEASE_PARENT(parent) \
905 if (G_LIKELY (parent)) \
906 gst_object_unref (parent); \
910 * gst_pad_get_direction:
911 * @pad: a #GstPad to get the direction of.
913 * Gets the direction of the pad. The direction of the pad is
914 * decided at construction time so this function does not take
917 * Returns: the #GstPadDirection of the pad.
922 gst_pad_get_direction (GstPad * pad)
924 GstPadDirection result;
926 /* PAD_UNKNOWN is a little silly but we need some sort of
927 * error return value */
928 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
930 result = GST_PAD_DIRECTION (pad);
936 gst_pad_activate_default (GstPad * pad, GstObject * parent)
938 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
940 return activate_mode_internal (pad, parent, GST_PAD_MODE_PUSH, TRUE);
944 * gst_pad_mode_get_name:
945 * @mode: the pad mode
947 * Return the name of a pad mode, for use in debug messages mostly.
949 * Returns: short mnemonic for pad mode @mode
952 gst_pad_mode_get_name (GstPadMode mode)
955 case GST_PAD_MODE_NONE:
957 case GST_PAD_MODE_PUSH:
959 case GST_PAD_MODE_PULL:
967 /* Returns TRUE if pad wasn't already in the new_mode */
969 pre_activate (GstPad * pad, GstPadMode new_mode)
972 case GST_PAD_MODE_NONE:
973 GST_OBJECT_LOCK (pad);
974 while (G_UNLIKELY (pad->priv->in_activation))
975 g_cond_wait (&pad->priv->activation_cond, GST_OBJECT_GET_LOCK (pad));
976 if (new_mode == GST_PAD_MODE (pad)) {
977 GST_WARNING_OBJECT (pad,
978 "Pad is already in the process of being deactivated");
979 GST_OBJECT_UNLOCK (pad);
982 pad->priv->in_activation = TRUE;
983 GST_DEBUG_OBJECT (pad, "setting PAD_MODE NONE, set flushing");
984 GST_PAD_SET_FLUSHING (pad);
985 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
986 GST_PAD_MODE (pad) = new_mode;
987 /* unlock blocked pads so element can resume and stop */
988 GST_PAD_BLOCK_BROADCAST (pad);
989 GST_OBJECT_UNLOCK (pad);
991 case GST_PAD_MODE_PUSH:
992 case GST_PAD_MODE_PULL:
993 GST_OBJECT_LOCK (pad);
994 while (G_UNLIKELY (pad->priv->in_activation))
995 g_cond_wait (&pad->priv->activation_cond, GST_OBJECT_GET_LOCK (pad));
996 if (new_mode == GST_PAD_MODE (pad)) {
997 GST_WARNING_OBJECT (pad,
998 "Pad is already in the process of being activated");
999 GST_OBJECT_UNLOCK (pad);
1002 pad->priv->in_activation = TRUE;
1003 GST_DEBUG_OBJECT (pad, "setting pad into %s mode, unset flushing",
1004 gst_pad_mode_get_name (new_mode));
1005 GST_PAD_UNSET_FLUSHING (pad);
1006 pad->ABI.abi.last_flowret = GST_FLOW_OK;
1007 GST_PAD_MODE (pad) = new_mode;
1008 if (GST_PAD_IS_SINK (pad)) {
1010 /* make sure the peer src pad sends us all events */
1011 if ((peer = GST_PAD_PEER (pad))) {
1012 gst_object_ref (peer);
1013 GST_OBJECT_UNLOCK (pad);
1015 GST_DEBUG_OBJECT (pad, "reschedule events on peer %s:%s",
1016 GST_DEBUG_PAD_NAME (peer));
1018 GST_OBJECT_LOCK (peer);
1019 schedule_events (peer, NULL);
1020 GST_OBJECT_UNLOCK (peer);
1022 gst_object_unref (peer);
1024 GST_OBJECT_UNLOCK (pad);
1027 GST_OBJECT_UNLOCK (pad);
1035 post_activate (GstPad * pad, GstPadMode new_mode)
1038 case GST_PAD_MODE_NONE:
1039 GST_OBJECT_LOCK (pad);
1040 pad->priv->in_activation = FALSE;
1041 g_cond_broadcast (&pad->priv->activation_cond);
1042 GST_OBJECT_UNLOCK (pad);
1044 /* ensures that streaming stops */
1045 GST_PAD_STREAM_LOCK (pad);
1046 GST_DEBUG_OBJECT (pad, "stopped streaming");
1047 GST_OBJECT_LOCK (pad);
1048 remove_events (pad);
1049 GST_OBJECT_UNLOCK (pad);
1050 GST_PAD_STREAM_UNLOCK (pad);
1052 case GST_PAD_MODE_PUSH:
1053 case GST_PAD_MODE_PULL:
1054 GST_OBJECT_LOCK (pad);
1055 pad->priv->in_activation = FALSE;
1056 g_cond_broadcast (&pad->priv->activation_cond);
1057 GST_OBJECT_UNLOCK (pad);
1064 * gst_pad_set_active:
1065 * @pad: the #GstPad to activate or deactivate.
1066 * @active: whether or not the pad should be active.
1068 * Activates or deactivates the given pad.
1069 * Normally called from within core state change functions.
1071 * If @active, makes sure the pad is active. If it is already active, either in
1072 * push or pull mode, just return. Otherwise dispatches to the pad's activate
1073 * function to perform the actual activation.
1075 * If not @active, calls gst_pad_activate_mode() with the pad's current mode
1076 * and a %FALSE argument.
1078 * Returns: %TRUE if the operation was successful.
1083 gst_pad_set_active (GstPad * pad, gboolean active)
1087 gboolean ret = FALSE;
1089 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1091 GST_OBJECT_LOCK (pad);
1092 old = GST_PAD_MODE (pad);
1093 ACQUIRE_PARENT (pad, parent, no_parent);
1094 GST_OBJECT_UNLOCK (pad);
1097 if (old == GST_PAD_MODE_NONE) {
1098 GST_DEBUG_OBJECT (pad, "activating pad from none");
1099 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad, parent);
1101 pad->ABI.abi.last_flowret = GST_FLOW_OK;
1103 GST_DEBUG_OBJECT (pad, "pad was active in %s mode",
1104 gst_pad_mode_get_name (old));
1108 if (old == GST_PAD_MODE_NONE) {
1109 GST_DEBUG_OBJECT (pad, "pad was inactive");
1112 GST_DEBUG_OBJECT (pad, "deactivating pad from %s mode",
1113 gst_pad_mode_get_name (old));
1114 ret = activate_mode_internal (pad, parent, old, FALSE);
1116 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
1120 RELEASE_PARENT (parent);
1122 if (G_UNLIKELY (!ret))
1130 GST_DEBUG_OBJECT (pad, "no parent");
1131 GST_OBJECT_UNLOCK (pad);
1136 GST_OBJECT_LOCK (pad);
1138 g_critical ("Failed to deactivate pad %s:%s, very bad",
1139 GST_DEBUG_PAD_NAME (pad));
1141 GST_WARNING_OBJECT (pad, "Failed to activate pad");
1143 GST_OBJECT_UNLOCK (pad);
1149 activate_mode_internal (GstPad * pad, GstObject * parent, GstPadMode mode,
1152 gboolean res = FALSE;
1153 GstPadMode old, new;
1154 GstPadDirection dir;
1157 GST_OBJECT_LOCK (pad);
1158 old = GST_PAD_MODE (pad);
1159 dir = GST_PAD_DIRECTION (pad);
1160 GST_OBJECT_UNLOCK (pad);
1162 new = active ? mode : GST_PAD_MODE_NONE;
1167 if (active && old != mode && old != GST_PAD_MODE_NONE) {
1168 /* pad was activate in the wrong direction, deactivate it
1169 * and reactivate it in the requested mode */
1170 GST_DEBUG_OBJECT (pad, "deactivating pad from %s mode",
1171 gst_pad_mode_get_name (old));
1173 if (G_UNLIKELY (!activate_mode_internal (pad, parent, old, FALSE)))
1174 goto deactivate_failed;
1175 old = GST_PAD_MODE_NONE;
1179 case GST_PAD_MODE_PULL:
1181 if (dir == GST_PAD_SINK) {
1182 if ((peer = gst_pad_get_peer (pad))) {
1183 GST_DEBUG_OBJECT (pad, "calling peer");
1184 if (G_UNLIKELY (!gst_pad_activate_mode (peer, mode, active)))
1186 gst_object_unref (peer);
1188 /* there is no peer, this is only fatal when we activate. When we
1189 * deactivate, we must assume the application has unlinked the peer and
1190 * will deactivate it eventually. */
1194 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
1197 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
1198 goto failure; /* Can't activate pull on a src without a
1199 getrange function */
1207 /* Mark pad as needing reconfiguration */
1209 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1211 /* pre_activate returns TRUE if we weren't already in the process of
1212 * switching to the 'new' mode */
1213 if (pre_activate (pad, new)) {
1215 if (GST_PAD_ACTIVATEMODEFUNC (pad)) {
1216 if (G_UNLIKELY (!GST_PAD_ACTIVATEMODEFUNC (pad) (pad, parent, mode,
1220 /* can happen for sinks of passthrough elements */
1223 post_activate (pad, new);
1226 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in %s mode",
1227 active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1232 /* Clear sticky flags on deactivation */
1234 GST_OBJECT_LOCK (pad);
1235 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1236 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
1237 GST_OBJECT_UNLOCK (pad);
1245 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in %s mode",
1246 active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1251 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1252 "failed to %s in switch to %s mode from %s mode",
1253 (active ? "activate" : "deactivate"), gst_pad_mode_get_name (mode),
1254 gst_pad_mode_get_name (old));
1259 GST_OBJECT_LOCK (peer);
1260 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1261 "activate_mode on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
1262 GST_OBJECT_UNLOCK (peer);
1263 gst_object_unref (peer);
1268 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
1269 "pad in pull mode");
1274 GST_OBJECT_LOCK (pad);
1275 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in %s mode",
1276 active ? "activate" : "deactivate", gst_pad_mode_get_name (mode));
1277 GST_PAD_SET_FLUSHING (pad);
1278 GST_PAD_MODE (pad) = old;
1279 pad->priv->in_activation = FALSE;
1280 g_cond_broadcast (&pad->priv->activation_cond);
1281 GST_OBJECT_UNLOCK (pad);
1287 * gst_pad_activate_mode:
1288 * @pad: the #GstPad to activate or deactivate.
1289 * @mode: the requested activation mode
1290 * @active: whether or not the pad should be active.
1292 * Activates or deactivates the given pad in @mode via dispatching to the
1293 * pad's activatemodefunc. For use from within pad activation functions only.
1295 * If you don't know what this is, you probably don't want to call it.
1297 * Returns: %TRUE if the operation was successful.
1302 gst_pad_activate_mode (GstPad * pad, GstPadMode mode, gboolean active)
1306 GstPadMode old, new;
1308 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1310 GST_OBJECT_LOCK (pad);
1312 old = GST_PAD_MODE (pad);
1313 new = active ? mode : GST_PAD_MODE_NONE;
1317 ACQUIRE_PARENT (pad, parent, no_parent);
1319 GST_OBJECT_UNLOCK (pad);
1321 res = activate_mode_internal (pad, parent, mode, active);
1323 RELEASE_PARENT (parent);
1329 GST_OBJECT_UNLOCK (pad);
1330 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in %s mode",
1331 active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1336 GST_WARNING_OBJECT (pad, "no parent");
1337 GST_OBJECT_UNLOCK (pad);
1343 * gst_pad_is_active:
1344 * @pad: the #GstPad to query
1346 * Query if a pad is active
1348 * Returns: %TRUE if the pad is active.
1353 gst_pad_is_active (GstPad * pad)
1355 gboolean result = FALSE;
1357 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1359 GST_OBJECT_LOCK (pad);
1360 result = GST_PAD_IS_ACTIVE (pad);
1361 GST_OBJECT_UNLOCK (pad);
1367 cleanup_hook (GstPad * pad, GHook * hook)
1369 GstPadProbeType type;
1371 GST_DEBUG_OBJECT (pad,
1372 "cleaning up hook %lu with flags %08x", hook->hook_id, hook->flags);
1374 if (!G_HOOK_IS_VALID (hook)) {
1375 /* We've already destroyed this hook */
1379 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1381 if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1382 /* unblock when we remove the last blocking probe */
1384 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1387 /* Might have new probes now that want to be called */
1388 GST_PAD_BLOCK_BROADCAST (pad);
1390 if (pad->num_blocked == 0) {
1391 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1392 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKED);
1395 g_hook_destroy_link (&pad->probes, hook);
1400 * gst_pad_add_probe:
1401 * @pad: the #GstPad to add the probe to
1402 * @mask: the probe mask
1403 * @callback: #GstPadProbeCallback that will be called with notifications of
1405 * @user_data: (closure): user data passed to the callback
1406 * @destroy_data: #GDestroyNotify for user_data
1408 * Be notified of different states of pads. The provided callback is called for
1409 * every state that matches @mask.
1411 * Probes are called in groups: First GST_PAD_PROBE_TYPE_BLOCK probes are
1412 * called, then others, then finally GST_PAD_PROBE_TYPE_IDLE. The only
1413 * exception here are GST_PAD_PROBE_TYPE_IDLE probes that are called
1414 * immediately if the pad is already idle while calling gst_pad_add_probe().
1415 * In each of the groups, probes are called in the order in which they were
1418 * Returns: an id or 0 if no probe is pending. The id can be used to remove the
1419 * probe with gst_pad_remove_probe(). When using GST_PAD_PROBE_TYPE_IDLE it can
1420 * happen that the probe can be run immediately and if the probe returns
1421 * GST_PAD_PROBE_REMOVE this functions returns 0.
1426 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
1427 GstPadProbeCallback callback, gpointer user_data,
1428 GDestroyNotify destroy_data)
1433 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1434 g_return_val_if_fail (mask != 0, 0);
1436 GST_OBJECT_LOCK (pad);
1438 /* make a new probe */
1439 hook = g_hook_alloc (&pad->probes);
1441 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1444 /* when no constraints are given for the types, assume all types are
1446 if ((mask & _PAD_PROBE_TYPE_ALL_BOTH_AND_FLUSH) == 0)
1447 mask |= GST_PAD_PROBE_TYPE_ALL_BOTH;
1448 if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1449 mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1451 /* store our flags and other fields */
1452 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1453 hook->func = callback;
1454 hook->data = user_data;
1455 hook->destroy = destroy_data;
1458 g_hook_append (&pad->probes, hook);
1460 /* incremenent cookie so that the new hook gets called */
1461 pad->priv->probe_list_cookie++;
1463 /* get the id of the hook, we return this and it can be used to remove the
1465 res = hook->hook_id;
1467 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1469 if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1470 /* we have a block probe */
1472 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKED);
1473 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1474 "now %d blocking probes", pad->num_blocked);
1476 /* Might have new probes now that want to be called */
1477 GST_PAD_BLOCK_BROADCAST (pad);
1480 /* call the callback if we need to be called for idle callbacks */
1481 if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1482 if (pad->priv->using > 0) {
1483 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1484 * flag above, the last thread to leave the push will do the callback. New
1485 * threads going into the push will block. */
1486 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1487 "pad is in use, delay idle callback");
1488 GST_OBJECT_UNLOCK (pad);
1490 GstPadProbeInfo info = { GST_PAD_PROBE_TYPE_IDLE, res, };
1491 GstPadProbeReturn ret;
1493 /* Keep another ref, the callback could destroy the pad */
1494 gst_object_ref (pad);
1495 pad->priv->idle_running++;
1497 /* Ref the hook, it could be destroyed by the callback or concurrently */
1498 g_hook_ref (&pad->probes, hook);
1500 /* the pad is idle now, we can signal the idle callback now */
1501 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1502 "pad is idle, trigger idle callback");
1503 GST_OBJECT_UNLOCK (pad);
1505 ret = callback (pad, &info, user_data);
1507 GST_OBJECT_LOCK (pad);
1509 case GST_PAD_PROBE_REMOVE:
1510 /* remove the probe */
1511 GST_DEBUG_OBJECT (pad, "asked to remove hook");
1512 cleanup_hook (pad, hook);
1515 case GST_PAD_PROBE_DROP:
1516 GST_DEBUG_OBJECT (pad, "asked to drop item");
1518 case GST_PAD_PROBE_PASS:
1519 GST_DEBUG_OBJECT (pad, "asked to pass item");
1521 case GST_PAD_PROBE_OK:
1522 GST_DEBUG_OBJECT (pad, "probe returned OK");
1524 case GST_PAD_PROBE_HANDLED:
1525 GST_DEBUG_OBJECT (pad, "probe handled the data");
1528 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
1531 g_hook_unref (&pad->probes, hook);
1532 pad->priv->idle_running--;
1533 if (pad->priv->idle_running == 0) {
1534 GST_PAD_BLOCK_BROADCAST (pad);
1536 GST_OBJECT_UNLOCK (pad);
1538 gst_object_unref (pad);
1541 GST_OBJECT_UNLOCK (pad);
1547 * gst_pad_remove_probe:
1548 * @pad: the #GstPad with the probe
1549 * @id: the probe id to remove
1551 * Remove the probe with @id from @pad.
1556 gst_pad_remove_probe (GstPad * pad, gulong id)
1560 g_return_if_fail (GST_IS_PAD (pad));
1562 GST_OBJECT_LOCK (pad);
1564 hook = g_hook_get (&pad->probes, id);
1568 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1570 cleanup_hook (pad, hook);
1571 GST_OBJECT_UNLOCK (pad);
1577 GST_OBJECT_UNLOCK (pad);
1578 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1584 * gst_pad_is_blocked:
1585 * @pad: the #GstPad to query
1587 * Checks if the pad is blocked or not. This function returns the
1588 * last requested state of the pad. It is not certain that the pad
1589 * is actually blocking at this point (see gst_pad_is_blocking()).
1591 * Returns: %TRUE if the pad is blocked.
1596 gst_pad_is_blocked (GstPad * pad)
1598 gboolean result = FALSE;
1600 g_return_val_if_fail (GST_IS_PAD (pad), result);
1602 GST_OBJECT_LOCK (pad);
1603 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED);
1604 GST_OBJECT_UNLOCK (pad);
1610 * gst_pad_is_blocking:
1611 * @pad: the #GstPad to query
1613 * Checks if the pad is blocking or not. This is a guaranteed state
1614 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1616 * Returns: %TRUE if the pad is blocking.
1621 gst_pad_is_blocking (GstPad * pad)
1623 gboolean result = FALSE;
1625 g_return_val_if_fail (GST_IS_PAD (pad), result);
1627 GST_OBJECT_LOCK (pad);
1628 /* the blocking flag is only valid if the pad is not flushing */
1629 result = GST_PAD_IS_BLOCKING (pad) && !GST_PAD_IS_FLUSHING (pad);
1630 GST_OBJECT_UNLOCK (pad);
1636 * gst_pad_needs_reconfigure:
1637 * @pad: the #GstPad to check
1639 * Check the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1640 * if the flag was set.
1642 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag is set on @pad.
1645 gst_pad_needs_reconfigure (GstPad * pad)
1647 gboolean reconfigure;
1649 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1651 GST_OBJECT_LOCK (pad);
1652 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1653 GST_DEBUG_OBJECT (pad, "peeking RECONFIGURE flag %d", reconfigure);
1654 GST_OBJECT_UNLOCK (pad);
1660 * gst_pad_check_reconfigure:
1661 * @pad: the #GstPad to check
1663 * Check and clear the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1664 * if the flag was set.
1666 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag was set on @pad.
1669 gst_pad_check_reconfigure (GstPad * pad)
1671 gboolean reconfigure;
1673 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1675 GST_OBJECT_LOCK (pad);
1676 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1678 GST_DEBUG_OBJECT (pad, "remove RECONFIGURE flag");
1679 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1681 GST_OBJECT_UNLOCK (pad);
1687 * gst_pad_mark_reconfigure:
1688 * @pad: the #GstPad to mark
1690 * Mark a pad for needing reconfiguration. The next call to
1691 * gst_pad_check_reconfigure() will return %TRUE after this call.
1694 gst_pad_mark_reconfigure (GstPad * pad)
1696 g_return_if_fail (GST_IS_PAD (pad));
1698 GST_OBJECT_LOCK (pad);
1699 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1700 GST_OBJECT_UNLOCK (pad);
1704 * gst_pad_set_activate_function:
1706 * @f: the #GstPadActivateFunction to set.
1708 * Calls gst_pad_set_activate_function_full() with %NULL for the user_data and
1712 * gst_pad_set_activate_function_full:
1714 * @activate: the #GstPadActivateFunction to set.
1715 * @user_data: user_data passed to @notify
1716 * @notify: notify called when @activate will not be used anymore.
1718 * Sets the given activate function for @pad. The activate function will
1719 * dispatch to gst_pad_activate_mode() to perform the actual activation.
1720 * Only makes sense to set on sink pads.
1722 * Call this function if your sink pad can start a pull-based task.
1725 gst_pad_set_activate_function_full (GstPad * pad,
1726 GstPadActivateFunction activate, gpointer user_data, GDestroyNotify notify)
1728 g_return_if_fail (GST_IS_PAD (pad));
1730 if (pad->activatenotify)
1731 pad->activatenotify (pad->activatedata);
1732 GST_PAD_ACTIVATEFUNC (pad) = activate;
1733 pad->activatedata = user_data;
1734 pad->activatenotify = notify;
1736 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1737 GST_DEBUG_FUNCPTR_NAME (activate));
1741 * gst_pad_set_activatemode_function:
1743 * @f: the #GstPadActivateModeFunction to set.
1745 * Calls gst_pad_set_activatemode_function_full() with %NULL for the user_data and
1749 * gst_pad_set_activatemode_function_full:
1751 * @activatemode: the #GstPadActivateModeFunction to set.
1752 * @user_data: user_data passed to @notify
1753 * @notify: notify called when @activatemode will not be used anymore.
1755 * Sets the given activate_mode function for the pad. An activate_mode function
1756 * prepares the element for data passing.
1759 gst_pad_set_activatemode_function_full (GstPad * pad,
1760 GstPadActivateModeFunction activatemode, gpointer user_data,
1761 GDestroyNotify notify)
1763 g_return_if_fail (GST_IS_PAD (pad));
1765 if (pad->activatemodenotify)
1766 pad->activatemodenotify (pad->activatemodedata);
1767 GST_PAD_ACTIVATEMODEFUNC (pad) = activatemode;
1768 pad->activatemodedata = user_data;
1769 pad->activatemodenotify = notify;
1771 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatemodefunc set to %s",
1772 GST_DEBUG_FUNCPTR_NAME (activatemode));
1776 * gst_pad_set_chain_function:
1777 * @p: a sink #GstPad.
1778 * @f: the #GstPadChainFunction to set.
1780 * Calls gst_pad_set_chain_function_full() with %NULL for the user_data and
1784 * gst_pad_set_chain_function_full:
1785 * @pad: a sink #GstPad.
1786 * @chain: the #GstPadChainFunction to set.
1787 * @user_data: user_data passed to @notify
1788 * @notify: notify called when @chain will not be used anymore.
1790 * Sets the given chain function for the pad. The chain function is called to
1791 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1794 gst_pad_set_chain_function_full (GstPad * pad, GstPadChainFunction chain,
1795 gpointer user_data, GDestroyNotify notify)
1797 g_return_if_fail (GST_IS_PAD (pad));
1798 g_return_if_fail (GST_PAD_IS_SINK (pad));
1800 if (pad->chainnotify)
1801 pad->chainnotify (pad->chaindata);
1802 GST_PAD_CHAINFUNC (pad) = chain;
1803 pad->chaindata = user_data;
1804 pad->chainnotify = notify;
1806 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1807 GST_DEBUG_FUNCPTR_NAME (chain));
1811 * gst_pad_set_chain_list_function:
1812 * @p: a sink #GstPad.
1813 * @f: the #GstPadChainListFunction to set.
1815 * Calls gst_pad_set_chain_list_function_full() with %NULL for the user_data and
1819 * gst_pad_set_chain_list_function_full:
1820 * @pad: a sink #GstPad.
1821 * @chainlist: the #GstPadChainListFunction to set.
1822 * @user_data: user_data passed to @notify
1823 * @notify: notify called when @chainlist will not be used anymore.
1825 * Sets the given chain list function for the pad. The chainlist function is
1826 * called to process a #GstBufferList input buffer list. See
1827 * #GstPadChainListFunction for more details.
1830 gst_pad_set_chain_list_function_full (GstPad * pad,
1831 GstPadChainListFunction chainlist, gpointer user_data,
1832 GDestroyNotify notify)
1834 g_return_if_fail (GST_IS_PAD (pad));
1835 g_return_if_fail (GST_PAD_IS_SINK (pad));
1837 if (pad->chainlistnotify)
1838 pad->chainlistnotify (pad->chainlistdata);
1839 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1840 pad->chainlistdata = user_data;
1841 pad->chainlistnotify = notify;
1843 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1844 GST_DEBUG_FUNCPTR_NAME (chainlist));
1848 * gst_pad_set_getrange_function:
1849 * @p: a source #GstPad.
1850 * @f: the #GstPadGetRangeFunction to set.
1852 * Calls gst_pad_set_getrange_function_full() with %NULL for the user_data and
1856 * gst_pad_set_getrange_function_full:
1857 * @pad: a source #GstPad.
1858 * @get: the #GstPadGetRangeFunction to set.
1859 * @user_data: user_data passed to @notify
1860 * @notify: notify called when @get will not be used anymore.
1862 * Sets the given getrange function for the pad. The getrange function is
1863 * called to produce a new #GstBuffer to start the processing pipeline. see
1864 * #GstPadGetRangeFunction for a description of the getrange function.
1867 gst_pad_set_getrange_function_full (GstPad * pad, GstPadGetRangeFunction get,
1868 gpointer user_data, GDestroyNotify notify)
1870 g_return_if_fail (GST_IS_PAD (pad));
1871 g_return_if_fail (GST_PAD_IS_SRC (pad));
1873 if (pad->getrangenotify)
1874 pad->getrangenotify (pad->getrangedata);
1875 GST_PAD_GETRANGEFUNC (pad) = get;
1876 pad->getrangedata = user_data;
1877 pad->getrangenotify = notify;
1879 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1880 GST_DEBUG_FUNCPTR_NAME (get));
1884 * gst_pad_set_event_function:
1885 * @p: a #GstPad of either direction.
1886 * @f: the #GstPadEventFunction to set.
1888 * Calls gst_pad_set_event_function_full() with %NULL for the user_data and
1892 * gst_pad_set_event_function_full:
1893 * @pad: a #GstPad of either direction.
1894 * @event: the #GstPadEventFunction to set.
1895 * @user_data: user_data passed to @notify
1896 * @notify: notify called when @event will not be used anymore.
1898 * Sets the given event handler for the pad.
1901 gst_pad_set_event_function_full (GstPad * pad, GstPadEventFunction event,
1902 gpointer user_data, GDestroyNotify notify)
1904 g_return_if_fail (GST_IS_PAD (pad));
1906 if (pad->eventnotify)
1907 pad->eventnotify (pad->eventdata);
1908 GST_PAD_EVENTFUNC (pad) = event;
1909 pad->eventdata = user_data;
1910 pad->eventnotify = notify;
1912 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1913 GST_DEBUG_FUNCPTR_NAME (event));
1917 event_wrap (GstPad * pad, GstObject * object, GstEvent * event)
1921 ret = GST_PAD_EVENTFULLFUNC (pad) (pad, object, event);
1922 if (ret == GST_FLOW_OK)
1928 * gst_pad_set_event_full_function:
1929 * @p: a #GstPad of either direction.
1930 * @f: the #GstPadEventFullFunction to set.
1932 * Calls gst_pad_set_event_full_function_full() with %NULL for the user_data and
1936 * gst_pad_set_event_full_function_full:
1937 * @pad: a #GstPad of either direction.
1938 * @event: the #GstPadEventFullFunction to set.
1939 * @user_data: user_data passed to @notify
1940 * @notify: notify called when @event will not be used anymore.
1942 * Sets the given event handler for the pad.
1947 gst_pad_set_event_full_function_full (GstPad * pad,
1948 GstPadEventFullFunction event, gpointer user_data, GDestroyNotify notify)
1950 g_return_if_fail (GST_IS_PAD (pad));
1952 if (pad->eventnotify)
1953 pad->eventnotify (pad->eventdata);
1954 GST_PAD_EVENTFULLFUNC (pad) = event;
1955 GST_PAD_EVENTFUNC (pad) = event_wrap;
1956 pad->eventdata = user_data;
1957 pad->eventnotify = notify;
1959 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfullfunc for set to %s",
1960 GST_DEBUG_FUNCPTR_NAME (event));
1964 * gst_pad_set_query_function:
1965 * @p: a #GstPad of either direction.
1966 * @f: the #GstPadQueryFunction to set.
1968 * Calls gst_pad_set_query_function_full() with %NULL for the user_data and
1972 * gst_pad_set_query_function_full:
1973 * @pad: a #GstPad of either direction.
1974 * @query: the #GstPadQueryFunction to set.
1975 * @user_data: user_data passed to @notify
1976 * @notify: notify called when @query will not be used anymore.
1978 * Set the given query function for the pad.
1981 gst_pad_set_query_function_full (GstPad * pad, GstPadQueryFunction query,
1982 gpointer user_data, GDestroyNotify notify)
1984 g_return_if_fail (GST_IS_PAD (pad));
1986 if (pad->querynotify)
1987 pad->querynotify (pad->querydata);
1988 GST_PAD_QUERYFUNC (pad) = query;
1989 pad->querydata = user_data;
1990 pad->querynotify = notify;
1992 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1993 GST_DEBUG_FUNCPTR_NAME (query));
1997 * gst_pad_set_iterate_internal_links_function:
1998 * @p: a #GstPad of either direction.
1999 * @f: the #GstPadIterIntLinkFunction to set.
2001 * Calls gst_pad_set_iterate_internal_links_function_full() with %NULL
2002 * for the user_data and notify.
2005 * gst_pad_set_iterate_internal_links_function_full:
2006 * @pad: a #GstPad of either direction.
2007 * @iterintlink: the #GstPadIterIntLinkFunction to set.
2008 * @user_data: user_data passed to @notify
2009 * @notify: notify called when @iterintlink will not be used anymore.
2011 * Sets the given internal link iterator function for the pad.
2014 gst_pad_set_iterate_internal_links_function_full (GstPad * pad,
2015 GstPadIterIntLinkFunction iterintlink, gpointer user_data,
2016 GDestroyNotify notify)
2018 g_return_if_fail (GST_IS_PAD (pad));
2020 if (pad->iterintlinknotify)
2021 pad->iterintlinknotify (pad->iterintlinkdata);
2022 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
2023 pad->iterintlinkdata = user_data;
2024 pad->iterintlinknotify = notify;
2026 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
2027 GST_DEBUG_FUNCPTR_NAME (iterintlink));
2031 * gst_pad_set_link_function:
2033 * @f: the #GstPadLinkFunction to set.
2035 * Calls gst_pad_set_link_function_full() with %NULL
2036 * for the user_data and notify.
2039 * gst_pad_set_link_function_full:
2041 * @link: the #GstPadLinkFunction to set.
2042 * @user_data: user_data passed to @notify
2043 * @notify: notify called when @link will not be used anymore.
2045 * Sets the given link function for the pad. It will be called when
2046 * the pad is linked with another pad.
2048 * The return value #GST_PAD_LINK_OK should be used when the connection can be
2051 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
2052 * cannot be made for some reason.
2054 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
2055 * of the peer sink pad, if present.
2058 gst_pad_set_link_function_full (GstPad * pad, GstPadLinkFunction link,
2059 gpointer user_data, GDestroyNotify notify)
2061 g_return_if_fail (GST_IS_PAD (pad));
2063 if (pad->linknotify)
2064 pad->linknotify (pad->linkdata);
2065 GST_PAD_LINKFUNC (pad) = link;
2066 pad->linkdata = user_data;
2067 pad->linknotify = notify;
2069 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
2070 GST_DEBUG_FUNCPTR_NAME (link));
2074 * gst_pad_set_unlink_function:
2076 * @f: the #GstPadUnlinkFunction to set.
2078 * Calls gst_pad_set_unlink_function_full() with %NULL
2079 * for the user_data and notify.
2082 * gst_pad_set_unlink_function_full:
2084 * @unlink: the #GstPadUnlinkFunction to set.
2085 * @user_data: user_data passed to @notify
2086 * @notify: notify called when @unlink will not be used anymore.
2088 * Sets the given unlink function for the pad. It will be called
2089 * when the pad is unlinked.
2091 * Note that the pad's lock is already held when the unlink
2092 * function is called, so most pad functions cannot be called
2093 * from within the callback.
2096 gst_pad_set_unlink_function_full (GstPad * pad, GstPadUnlinkFunction unlink,
2097 gpointer user_data, GDestroyNotify notify)
2099 g_return_if_fail (GST_IS_PAD (pad));
2101 if (pad->unlinknotify)
2102 pad->unlinknotify (pad->unlinkdata);
2103 GST_PAD_UNLINKFUNC (pad) = unlink;
2104 pad->unlinkdata = user_data;
2105 pad->unlinknotify = notify;
2107 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
2108 GST_DEBUG_FUNCPTR_NAME (unlink));
2113 * @srcpad: the source #GstPad to unlink.
2114 * @sinkpad: the sink #GstPad to unlink.
2116 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
2117 * signal on both pads.
2119 * Returns: %TRUE if the pads were unlinked. This function returns %FALSE if
2120 * the pads were not linked together.
2125 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
2127 gboolean result = FALSE;
2128 GstElement *parent = NULL;
2130 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2131 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
2132 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2133 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
2135 GST_TRACER_PAD_UNLINK_PRE (srcpad, sinkpad);
2137 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
2138 GST_DEBUG_PAD_NAME (srcpad), srcpad,
2139 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
2141 /* We need to notify the parent before taking any pad locks as the bin in
2142 * question might be waiting for a lock on the pad while holding its lock
2143 * that our message will try to take. */
2144 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
2145 if (GST_IS_ELEMENT (parent)) {
2146 gst_element_post_message (parent,
2147 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2148 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
2150 gst_object_unref (parent);
2155 GST_OBJECT_LOCK (srcpad);
2156 GST_OBJECT_LOCK (sinkpad);
2158 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
2159 goto not_linked_together;
2161 if (GST_PAD_UNLINKFUNC (srcpad)) {
2162 GstObject *tmpparent;
2164 ACQUIRE_PARENT (srcpad, tmpparent, no_src_parent);
2166 GST_PAD_UNLINKFUNC (srcpad) (srcpad, tmpparent);
2167 RELEASE_PARENT (tmpparent);
2170 if (GST_PAD_UNLINKFUNC (sinkpad)) {
2171 GstObject *tmpparent;
2173 ACQUIRE_PARENT (sinkpad, tmpparent, no_sink_parent);
2175 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad, tmpparent);
2176 RELEASE_PARENT (tmpparent);
2180 /* first clear peers */
2181 GST_PAD_PEER (srcpad) = NULL;
2182 GST_PAD_PEER (sinkpad) = NULL;
2184 GST_OBJECT_UNLOCK (sinkpad);
2185 GST_OBJECT_UNLOCK (srcpad);
2187 /* fire off a signal to each of the pads telling them
2188 * that they've been unlinked */
2189 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
2190 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
2192 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
2193 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2198 if (parent != NULL) {
2199 gst_element_post_message (parent,
2200 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2201 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
2202 gst_object_unref (parent);
2204 GST_TRACER_PAD_UNLINK_POST (srcpad, sinkpad, result);
2208 not_linked_together:
2210 /* we do not emit a warning in this case because unlinking cannot
2211 * be made MT safe.*/
2212 GST_OBJECT_UNLOCK (sinkpad);
2213 GST_OBJECT_UNLOCK (srcpad);
2219 * gst_pad_is_linked:
2220 * @pad: pad to check
2222 * Checks if a @pad is linked to another pad or not.
2224 * Returns: %TRUE if the pad is linked, %FALSE otherwise.
2229 gst_pad_is_linked (GstPad * pad)
2233 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2235 GST_OBJECT_LOCK (pad);
2236 result = (GST_PAD_PEER (pad) != NULL);
2237 GST_OBJECT_UNLOCK (pad);
2242 /* get the caps from both pads and see if the intersection
2245 * This function should be called with the pad LOCK on both
2249 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
2250 GstPadLinkCheck flags)
2252 GstCaps *srccaps = NULL;
2253 GstCaps *sinkcaps = NULL;
2254 gboolean compatible = FALSE;
2256 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
2259 /* Doing the expensive caps checking takes priority over only checking the template caps */
2260 if (flags & GST_PAD_LINK_CHECK_CAPS) {
2261 GST_OBJECT_UNLOCK (sink);
2262 GST_OBJECT_UNLOCK (src);
2264 srccaps = gst_pad_query_caps (src, NULL);
2265 sinkcaps = gst_pad_query_caps (sink, NULL);
2267 GST_OBJECT_LOCK (src);
2268 GST_OBJECT_LOCK (sink);
2270 /* If one of the two pads doesn't have a template, consider the intersection
2272 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
2273 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
2277 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
2279 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
2282 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
2284 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
2287 /* if we have caps on both pads we can check the intersection. If one
2288 * of the caps is %NULL, we return %TRUE. */
2289 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
2291 gst_caps_unref (srccaps);
2293 gst_caps_unref (sinkcaps);
2297 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
2298 gst_caps_unref (srccaps);
2299 gst_caps_unref (sinkcaps);
2302 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
2303 (compatible ? "" : "not "));
2308 /* check if the grandparents of both pads are the same.
2309 * This check is required so that we don't try to link
2310 * pads from elements in different bins without ghostpads.
2312 * The LOCK should be held on both pads
2315 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
2317 GstObject *psrc, *psink;
2319 psrc = GST_OBJECT_PARENT (src);
2320 psink = GST_OBJECT_PARENT (sink);
2322 /* if one of the pads has no parent, we allow the link */
2323 if (G_UNLIKELY (psrc == NULL || psink == NULL))
2326 /* only care about parents that are elements */
2327 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
2328 goto no_element_parent;
2330 /* if the parents are the same, we have a loop */
2331 if (G_UNLIKELY (psrc == psink))
2334 /* if they both have a parent, we check the grandparents. We can not lock
2335 * the parent because we hold on the child (pad) and the locking order is
2336 * parent >> child. */
2337 psrc = GST_OBJECT_PARENT (psrc);
2338 psink = GST_OBJECT_PARENT (psink);
2340 /* if they have grandparents but they are not the same */
2341 if (G_UNLIKELY (psrc != psink))
2342 goto wrong_grandparents;
2349 GST_CAT_DEBUG (GST_CAT_CAPS,
2350 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
2351 GST_PTR_FORMAT, psrc, psink);
2356 GST_CAT_DEBUG (GST_CAT_CAPS,
2357 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
2358 GST_PTR_FORMAT, psrc, psink);
2363 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
2369 GST_CAT_DEBUG (GST_CAT_CAPS,
2370 "pads have different grandparents %" GST_PTR_FORMAT " and %"
2371 GST_PTR_FORMAT, psrc, psink);
2376 /* FIXME leftover from an attempt at refactoring... */
2377 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
2378 * the two pads will be locked in the srcpad, sinkpad order. */
2379 static GstPadLinkReturn
2380 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2382 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
2383 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2385 GST_OBJECT_LOCK (srcpad);
2387 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
2388 goto src_was_linked;
2390 GST_OBJECT_LOCK (sinkpad);
2392 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
2393 goto sink_was_linked;
2395 /* check hierarchy, pads can only be linked if the grandparents
2397 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
2398 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
2399 goto wrong_hierarchy;
2401 /* check pad caps for non-empty intersection */
2402 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
2405 /* FIXME check pad scheduling for non-empty intersection */
2407 return GST_PAD_LINK_OK;
2411 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
2412 GST_DEBUG_PAD_NAME (srcpad),
2413 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
2414 /* we do not emit a warning in this case because unlinking cannot
2415 * be made MT safe.*/
2416 GST_OBJECT_UNLOCK (srcpad);
2417 return GST_PAD_LINK_WAS_LINKED;
2421 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
2422 GST_DEBUG_PAD_NAME (sinkpad),
2423 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
2424 /* we do not emit a warning in this case because unlinking cannot
2425 * be made MT safe.*/
2426 GST_OBJECT_UNLOCK (sinkpad);
2427 GST_OBJECT_UNLOCK (srcpad);
2428 return GST_PAD_LINK_WAS_LINKED;
2432 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
2433 GST_OBJECT_UNLOCK (sinkpad);
2434 GST_OBJECT_UNLOCK (srcpad);
2435 return GST_PAD_LINK_WRONG_HIERARCHY;
2439 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
2440 GST_OBJECT_UNLOCK (sinkpad);
2441 GST_OBJECT_UNLOCK (srcpad);
2442 return GST_PAD_LINK_NOFORMAT;
2448 * @srcpad: the source #GstPad.
2449 * @sinkpad: the sink #GstPad.
2451 * Checks if the source pad and the sink pad are compatible so they can be
2454 * Returns: %TRUE if the pads can be linked.
2457 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2459 GstPadLinkReturn result;
2461 /* generic checks */
2462 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2463 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2465 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2466 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2468 /* gst_pad_link_prepare does everything for us, we only release the locks
2469 * on the pads that it gets us. If this function returns !OK the locks are not
2471 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2472 if (result != GST_PAD_LINK_OK)
2475 GST_OBJECT_UNLOCK (srcpad);
2476 GST_OBJECT_UNLOCK (sinkpad);
2479 return result == GST_PAD_LINK_OK;
2483 * gst_pad_link_full:
2484 * @srcpad: the source #GstPad to link.
2485 * @sinkpad: the sink #GstPad to link.
2486 * @flags: the checks to validate when linking
2488 * Links the source pad and the sink pad.
2490 * This variant of #gst_pad_link provides a more granular control on the
2491 * checks being done when linking. While providing some considerable speedups
2492 * the caller of this method must be aware that wrong usage of those flags
2493 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2494 * for more information.
2498 * Returns: A result code indicating if the connection worked or
2502 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2504 GstPadLinkReturn result;
2506 GstPadLinkFunction srcfunc, sinkfunc;
2508 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2509 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2510 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2511 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2512 GST_PAD_LINK_WRONG_DIRECTION);
2514 GST_TRACER_PAD_LINK_PRE (srcpad, sinkpad);
2516 /* Notify the parent early. See gst_pad_unlink for details. */
2517 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2518 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2519 gst_element_post_message (parent,
2520 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2521 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2523 gst_object_unref (parent);
2528 /* prepare will also lock the two pads */
2529 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2531 if (G_UNLIKELY (result != GST_PAD_LINK_OK)) {
2532 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed: %s",
2533 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad),
2534 gst_pad_link_get_name (result));
2538 /* must set peers before calling the link function */
2539 GST_PAD_PEER (srcpad) = sinkpad;
2540 GST_PAD_PEER (sinkpad) = srcpad;
2542 /* check events, when something is different, mark pending */
2543 schedule_events (srcpad, sinkpad);
2545 /* get the link functions */
2546 srcfunc = GST_PAD_LINKFUNC (srcpad);
2547 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2549 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2550 /* custom link functions, execute them */
2551 GST_OBJECT_UNLOCK (sinkpad);
2552 GST_OBJECT_UNLOCK (srcpad);
2555 GstObject *tmpparent;
2557 ACQUIRE_PARENT (srcpad, tmpparent, no_parent);
2558 /* this one will call the peer link function */
2559 result = srcfunc (srcpad, tmpparent, sinkpad);
2560 RELEASE_PARENT (tmpparent);
2561 } else if (sinkfunc) {
2562 GstObject *tmpparent;
2564 ACQUIRE_PARENT (sinkpad, tmpparent, no_parent);
2565 /* if no source link function, we need to call the sink link
2566 * function ourselves. */
2567 result = sinkfunc (sinkpad, tmpparent, srcpad);
2568 RELEASE_PARENT (tmpparent);
2572 GST_OBJECT_LOCK (srcpad);
2573 GST_OBJECT_LOCK (sinkpad);
2575 /* we released the lock, check if the same pads are linked still */
2576 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2577 goto concurrent_link;
2579 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2582 GST_OBJECT_UNLOCK (sinkpad);
2583 GST_OBJECT_UNLOCK (srcpad);
2585 /* fire off a signal to each of the pads telling them
2586 * that they've been linked */
2587 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2588 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2590 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2591 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2593 if (!(flags & GST_PAD_LINK_CHECK_NO_RECONFIGURE))
2594 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2597 if (G_LIKELY (parent)) {
2598 gst_element_post_message (parent,
2599 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2600 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2601 gst_object_unref (parent);
2604 GST_TRACER_PAD_LINK_POST (srcpad, sinkpad, result);
2610 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2611 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2612 GST_OBJECT_UNLOCK (sinkpad);
2613 GST_OBJECT_UNLOCK (srcpad);
2615 /* The other link operation succeeded first */
2616 result = GST_PAD_LINK_WAS_LINKED;
2621 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed: %s",
2622 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad),
2623 gst_pad_link_get_name (result));
2625 GST_PAD_PEER (srcpad) = NULL;
2626 GST_PAD_PEER (sinkpad) = NULL;
2628 GST_OBJECT_UNLOCK (sinkpad);
2629 GST_OBJECT_UNLOCK (srcpad);
2637 * @srcpad: the source #GstPad to link.
2638 * @sinkpad: the sink #GstPad to link.
2640 * Links the source pad and the sink pad.
2642 * Returns: A result code indicating if the connection worked or
2648 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2650 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2654 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2656 GstPadTemplate **template_p;
2658 /* this function would need checks if it weren't static */
2660 GST_OBJECT_LOCK (pad);
2661 template_p = &pad->padtemplate;
2662 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2663 GST_OBJECT_UNLOCK (pad);
2666 gst_pad_template_pad_created (templ, pad);
2670 * gst_pad_get_pad_template:
2673 * Gets the template for @pad.
2675 * Returns: (transfer full) (nullable): the #GstPadTemplate from which
2676 * this pad was instantiated, or %NULL if this pad has no
2677 * template. Unref after usage.
2680 gst_pad_get_pad_template (GstPad * pad)
2682 GstPadTemplate *templ;
2684 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2686 templ = GST_PAD_PAD_TEMPLATE (pad);
2688 return (templ ? gst_object_ref (templ) : NULL);
2692 * gst_pad_has_current_caps:
2693 * @pad: a #GstPad to check
2695 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2697 * Returns: %TRUE when @pad has caps associated with it.
2700 gst_pad_has_current_caps (GstPad * pad)
2705 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2707 GST_OBJECT_LOCK (pad);
2708 caps = get_pad_caps (pad);
2709 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2710 "check current pad caps %" GST_PTR_FORMAT, caps);
2711 result = (caps != NULL);
2712 GST_OBJECT_UNLOCK (pad);
2718 * gst_pad_get_current_caps:
2719 * @pad: a #GstPad to get the current capabilities of.
2721 * Gets the capabilities currently configured on @pad with the last
2722 * #GST_EVENT_CAPS event.
2724 * Returns: (transfer full) (nullable): the current caps of the pad with
2725 * incremented ref-count or %NULL when pad has no caps. Unref after usage.
2728 gst_pad_get_current_caps (GstPad * pad)
2732 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2734 GST_OBJECT_LOCK (pad);
2735 if ((result = get_pad_caps (pad)))
2736 gst_caps_ref (result);
2737 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2738 "get current pad caps %" GST_PTR_FORMAT, result);
2739 GST_OBJECT_UNLOCK (pad);
2745 * gst_pad_get_pad_template_caps:
2746 * @pad: a #GstPad to get the template capabilities from.
2748 * Gets the capabilities for @pad's template.
2750 * Returns: (transfer full): the #GstCaps of this pad template.
2751 * Unref after usage.
2754 gst_pad_get_pad_template_caps (GstPad * pad)
2756 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2758 if (GST_PAD_PAD_TEMPLATE (pad))
2759 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2761 return gst_caps_ref (GST_CAPS_ANY);
2766 * @pad: a #GstPad to get the peer of.
2768 * Gets the peer of @pad. This function refs the peer pad so
2769 * you need to unref it after use.
2771 * Returns: (transfer full) (nullable): the peer #GstPad. Unref after usage.
2776 gst_pad_get_peer (GstPad * pad)
2780 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2782 GST_OBJECT_LOCK (pad);
2783 result = GST_PAD_PEER (pad);
2785 gst_object_ref (result);
2786 GST_OBJECT_UNLOCK (pad);
2792 * gst_pad_get_allowed_caps:
2795 * Gets the capabilities of the allowed media types that can flow through
2796 * @pad and its peer.
2798 * The allowed capabilities is calculated as the intersection of the results of
2799 * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2800 * on the resulting caps.
2802 * Returns: (transfer full) (nullable): the allowed #GstCaps of the
2803 * pad link. Unref the caps when you no longer need it. This
2804 * function returns %NULL when @pad has no peer.
2809 gst_pad_get_allowed_caps (GstPad * pad)
2812 GstCaps *caps = NULL;
2815 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2817 GST_OBJECT_LOCK (pad);
2818 if (G_UNLIKELY (GST_PAD_PEER (pad) == NULL))
2820 GST_OBJECT_UNLOCK (pad);
2822 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2824 mycaps = gst_pad_query_caps (pad, NULL);
2826 /* Query peer caps */
2827 query = gst_query_new_caps (mycaps);
2828 if (!gst_pad_peer_query (pad, query)) {
2829 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "Caps query failed");
2833 gst_query_parse_caps_result (query, &caps);
2835 g_warn_if_fail (caps != NULL);
2838 gst_caps_ref (caps);
2840 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2844 gst_query_unref (query);
2845 gst_caps_unref (mycaps);
2851 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2852 GST_OBJECT_UNLOCK (pad);
2859 * gst_pad_get_single_internal_link:
2860 * @pad: the #GstPad to get the internal link of.
2862 * If there is a single internal link of the given pad, this function will
2863 * return it. Otherwise, it will return NULL.
2865 * Returns: (transfer full) (nullable): a #GstPad, or %NULL if @pad has none
2866 * or more than one internal links. Unref returned pad with
2867 * gst_object_unref().
2872 gst_pad_get_single_internal_link (GstPad * pad)
2875 gboolean done = FALSE;
2876 GValue item = { 0, };
2879 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2881 iter = gst_pad_iterate_internal_links (pad);
2887 switch (gst_iterator_next (iter, &item)) {
2888 case GST_ITERATOR_OK:
2891 ret = g_value_dup_object (&item);
2893 /* More than one internal link found - don't bother reffing */
2894 gst_clear_object (&ret);
2895 GST_DEBUG_OBJECT (pad,
2896 "Requested single internally linked pad, multiple found");
2899 g_value_reset (&item);
2902 case GST_ITERATOR_RESYNC:
2903 gst_clear_object (&ret);
2904 gst_iterator_resync (iter);
2906 case GST_ITERATOR_ERROR:
2907 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2909 case GST_ITERATOR_DONE:
2911 GST_DEBUG_OBJECT (pad,
2912 "Requested single internally linked pad, none found");
2918 g_value_unset (&item);
2919 gst_iterator_free (iter);
2925 * gst_pad_iterate_internal_links_default:
2926 * @pad: the #GstPad to get the internal links of.
2927 * @parent: (allow-none): the parent of @pad or %NULL
2929 * Iterate the list of pads to which the given pad is linked to inside of
2930 * the parent element.
2931 * This is the default handler, and thus returns an iterator of all of the
2932 * pads inside the parent element with opposite direction.
2934 * The caller must free this iterator after use with gst_iterator_free().
2936 * Returns: (nullable): a #GstIterator of #GstPad, or %NULL if @pad
2937 * has no parent. Unref each returned pad with gst_object_unref().
2940 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2947 GstElement *eparent;
2949 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2951 if (parent != NULL && GST_IS_ELEMENT (parent)) {
2952 eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2954 GST_OBJECT_LOCK (pad);
2955 eparent = GST_PAD_PARENT (pad);
2956 if (!eparent || !GST_IS_ELEMENT (eparent))
2959 gst_object_ref (eparent);
2960 GST_OBJECT_UNLOCK (pad);
2963 if (pad->direction == GST_PAD_SRC)
2964 padlist = &eparent->sinkpads;
2966 padlist = &eparent->srcpads;
2968 GST_DEBUG_OBJECT (pad, "Making iterator");
2970 cookie = &eparent->pads_cookie;
2972 lock = GST_OBJECT_GET_LOCK (eparent);
2974 res = gst_iterator_new_list (GST_TYPE_PAD,
2975 lock, cookie, padlist, (GObject *) owner, NULL);
2977 gst_object_unref (owner);
2984 GST_OBJECT_UNLOCK (pad);
2985 GST_DEBUG_OBJECT (pad, "no parent element");
2991 * gst_pad_iterate_internal_links:
2992 * @pad: the GstPad to get the internal links of.
2994 * Gets an iterator for the pads to which the given pad is linked to inside
2995 * of the parent element.
2997 * Each #GstPad element yielded by the iterator will have its refcount increased,
2998 * so unref after use.
3000 * Free-function: gst_iterator_free
3002 * Returns: (transfer full) (nullable): a new #GstIterator of #GstPad
3003 * or %NULL when the pad does not have an iterator function
3004 * configured. Use gst_iterator_free() after usage.
3007 gst_pad_iterate_internal_links (GstPad * pad)
3009 GstIterator *res = NULL;
3012 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3014 GST_OBJECT_LOCK (pad);
3015 ACQUIRE_PARENT (pad, parent, no_parent);
3016 GST_OBJECT_UNLOCK (pad);
3018 if (GST_PAD_ITERINTLINKFUNC (pad))
3019 res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
3021 RELEASE_PARENT (parent);
3028 GST_DEBUG_OBJECT (pad, "no parent");
3029 GST_OBJECT_UNLOCK (pad);
3037 * @forward: (scope call): a #GstPadForwardFunction
3038 * @user_data: user data passed to @forward
3040 * Calls @forward for all internally linked pads of @pad. This function deals with
3041 * dynamically changing internal pads and will make sure that the @forward
3042 * function is only called once for each pad.
3044 * When @forward returns %TRUE, no further pads will be processed.
3046 * Returns: %TRUE if one of the dispatcher functions returned %TRUE.
3049 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
3052 gboolean result = FALSE;
3054 gboolean done = FALSE;
3055 GValue item = { 0, };
3056 GList *pushed_pads = NULL;
3058 iter = gst_pad_iterate_internal_links (pad);
3064 switch (gst_iterator_next (iter, &item)) {
3065 case GST_ITERATOR_OK:
3069 intpad = g_value_get_object (&item);
3071 /* if already pushed, skip. FIXME, find something faster to tag pads */
3072 if (intpad == NULL || g_list_find (pushed_pads, intpad)) {
3073 g_value_reset (&item);
3077 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
3078 GST_DEBUG_PAD_NAME (intpad));
3079 done = result = forward (intpad, user_data);
3081 pushed_pads = g_list_prepend (pushed_pads, intpad);
3083 g_value_reset (&item);
3086 case GST_ITERATOR_RESYNC:
3087 /* We don't reset the result here because we don't push the event
3088 * again on pads that got the event already and because we need
3089 * to consider the result of the previous pushes */
3090 gst_iterator_resync (iter);
3092 case GST_ITERATOR_ERROR:
3093 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3096 case GST_ITERATOR_DONE:
3101 g_value_unset (&item);
3102 gst_iterator_free (iter);
3104 g_list_free (pushed_pads);
3114 gboolean dispatched;
3118 event_forward_func (GstPad * pad, EventData * data)
3120 /* for each pad we send to, we should ref the event; it's up
3121 * to downstream to unref again when handled. */
3122 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
3123 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
3125 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
3127 data->dispatched = TRUE;
3134 * gst_pad_event_default:
3135 * @pad: a #GstPad to call the default event handler on.
3136 * @parent: (allow-none): the parent of @pad or %NULL
3137 * @event: (transfer full): the #GstEvent to handle.
3139 * Invokes the default event handler for the given pad.
3141 * The EOS event will pause the task associated with @pad before it is forwarded
3142 * to all internally linked pads,
3144 * The event is sent to all pads internally linked to @pad. This function
3145 * takes ownership of @event.
3147 * Returns: %TRUE if the event was sent successfully.
3150 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
3152 gboolean result, forward = TRUE;
3154 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3155 g_return_val_if_fail (event != NULL, FALSE);
3157 GST_LOG_OBJECT (pad, "default event handler for event %" GST_PTR_FORMAT,
3160 switch (GST_EVENT_TYPE (event)) {
3161 case GST_EVENT_CAPS:
3162 forward = GST_PAD_IS_PROXY_CAPS (pad);
3173 data.dispatched = FALSE;
3174 data.result = FALSE;
3176 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
3178 /* for sinkpads without a parent element or without internal links, nothing
3179 * will be dispatched but we still want to return TRUE. */
3180 if (data.dispatched)
3181 result = data.result;
3186 gst_event_unref (event);
3191 /* Default accept caps implementation just checks against
3192 * the allowed caps for the pad */
3194 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
3196 /* get the caps and see if it intersects to something not empty */
3197 GstCaps *caps, *allowed = NULL;
3200 GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
3202 /* first forward the query to internally linked pads when we are dealing with
3204 if (GST_PAD_IS_PROXY_CAPS (pad)) {
3205 result = gst_pad_proxy_query_accept_caps (pad, query);
3207 allowed = gst_pad_get_pad_template_caps (pad);
3212 gst_query_parse_accept_caps (query, &caps);
3214 if (GST_PAD_IS_ACCEPT_TEMPLATE (pad)) {
3215 allowed = gst_pad_get_pad_template_caps (pad);
3217 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pad,
3218 "fallback ACCEPT_CAPS query, consider implementing a specialized version");
3219 allowed = gst_pad_query_caps (pad, caps);
3224 if (GST_PAD_IS_ACCEPT_INTERSECT (pad)) {
3225 GST_DEBUG_OBJECT (pad,
3226 "allowed caps intersect %" GST_PTR_FORMAT ", caps %" GST_PTR_FORMAT,
3228 result = gst_caps_can_intersect (caps, allowed);
3230 GST_DEBUG_OBJECT (pad, "allowed caps subset %" GST_PTR_FORMAT ", caps %"
3231 GST_PTR_FORMAT, allowed, caps);
3232 result = gst_caps_is_subset (caps, allowed);
3235 GST_CAT_WARNING_OBJECT (GST_CAT_CAPS, pad, "caps: %" GST_PTR_FORMAT
3236 " were not compatible with: %" GST_PTR_FORMAT, caps, allowed);
3238 gst_caps_unref (allowed);
3240 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3241 "no compatible caps allowed on the pad");
3245 gst_query_set_accept_caps_result (query, result);
3251 /* Default caps implementation */
3253 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
3255 GstCaps *result = NULL, *filter;
3256 GstPadTemplate *templ;
3257 gboolean fixed_caps;
3259 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "query caps %" GST_PTR_FORMAT,
3262 /* first try to proxy if we must */
3263 if (GST_PAD_IS_PROXY_CAPS (pad)) {
3264 if ((gst_pad_proxy_query_caps (pad, query))) {
3269 gst_query_parse_caps (query, &filter);
3271 /* no proxy or it failed, do default handling */
3272 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
3274 GST_OBJECT_LOCK (pad);
3276 /* fixed caps, try the negotiated caps first */
3277 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
3278 if ((result = get_pad_caps (pad)))
3279 goto filter_done_unlock;
3282 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
3283 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
3284 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
3285 goto filter_done_unlock;
3289 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3290 "non-fixed pad caps: trying pad caps");
3291 /* non fixed caps, try the negotiated caps */
3292 if ((result = get_pad_caps (pad)))
3293 goto filter_done_unlock;
3296 /* this almost never happens */
3297 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
3298 result = GST_CAPS_ANY;
3301 GST_OBJECT_UNLOCK (pad);
3303 /* run the filter on the result */
3305 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3306 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
3307 GST_PTR_FORMAT, result, result, filter, filter);
3308 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
3309 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
3312 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3313 "using caps %p %" GST_PTR_FORMAT, result, result);
3314 result = gst_caps_ref (result);
3316 gst_query_set_caps_result (query, result);
3317 gst_caps_unref (result);
3323 /* Default latency implementation */
3327 GstClockTime min, max;
3331 query_latency_default_fold (const GValue * item, GValue * ret,
3334 GstPad *pad = g_value_get_object (item), *peer;
3335 LatencyFoldData *fold_data = user_data;
3337 gboolean res = FALSE;
3339 query = gst_query_new_latency ();
3341 peer = gst_pad_get_peer (pad);
3343 res = gst_pad_peer_query (pad, query);
3345 GST_LOG_OBJECT (pad, "No peer pad found, ignoring this pad");
3350 GstClockTime min, max;
3352 gst_query_parse_latency (query, &live, &min, &max);
3354 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3355 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
3358 if (min > fold_data->min)
3359 fold_data->min = min;
3361 if (fold_data->max == GST_CLOCK_TIME_NONE)
3362 fold_data->max = max;
3363 else if (max < fold_data->max)
3364 fold_data->max = max;
3366 fold_data->live = TRUE;
3369 GST_DEBUG_OBJECT (pad, "latency query failed");
3370 g_value_set_boolean (ret, FALSE);
3373 gst_query_unref (query);
3375 gst_object_unref (peer);
3381 gst_pad_query_latency_default (GstPad * pad, GstQuery * query)
3384 GstIteratorResult res;
3385 GValue ret = G_VALUE_INIT;
3387 LatencyFoldData fold_data;
3389 it = gst_pad_iterate_internal_links (pad);
3391 GST_DEBUG_OBJECT (pad, "Can't iterate internal links");
3395 g_value_init (&ret, G_TYPE_BOOLEAN);
3398 fold_data.live = FALSE;
3400 fold_data.max = GST_CLOCK_TIME_NONE;
3402 g_value_set_boolean (&ret, TRUE);
3403 res = gst_iterator_fold (it, query_latency_default_fold, &ret, &fold_data);
3405 case GST_ITERATOR_OK:
3406 g_assert_not_reached ();
3408 case GST_ITERATOR_DONE:
3410 case GST_ITERATOR_ERROR:
3411 g_value_set_boolean (&ret, FALSE);
3413 case GST_ITERATOR_RESYNC:
3414 gst_iterator_resync (it);
3417 g_assert_not_reached ();
3420 gst_iterator_free (it);
3422 query_ret = g_value_get_boolean (&ret);
3424 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3425 " max:%" G_GINT64_FORMAT, fold_data.live ? "true" : "false",
3426 fold_data.min, fold_data.max);
3428 if (fold_data.min > fold_data.max) {
3429 GST_ERROR_OBJECT (pad, "minimum latency bigger than maximum latency");
3432 gst_query_set_latency (query, fold_data.live, fold_data.min, fold_data.max);
3434 GST_LOG_OBJECT (pad, "latency query failed");
3444 gboolean dispatched;
3448 query_forward_func (GstPad * pad, QueryData * data)
3450 GST_LOG_OBJECT (pad, "query peer %p (%s) of %s:%s",
3451 data->query, GST_QUERY_TYPE_NAME (data->query), GST_DEBUG_PAD_NAME (pad));
3453 data->result |= gst_pad_peer_query (pad, data->query);
3455 data->dispatched = TRUE;
3457 /* stop on first successful reply */
3458 return data->result;
3462 * gst_pad_query_default:
3463 * @pad: a #GstPad to call the default query handler on.
3464 * @parent: (allow-none): the parent of @pad or %NULL
3465 * @query: (transfer none): the #GstQuery to handle.
3467 * Invokes the default query handler for the given pad.
3468 * The query is sent to all pads internally linked to @pad. Note that
3469 * if there are many possible sink pads that are internally linked to
3470 * @pad, only one will be sent the query.
3471 * Multi-sinkpad elements should implement custom query handlers.
3473 * Returns: %TRUE if the query was performed successfully.
3476 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
3478 gboolean forward, ret = FALSE;
3480 switch (GST_QUERY_TYPE (query)) {
3481 case GST_QUERY_SCHEDULING:
3482 forward = GST_PAD_IS_PROXY_SCHEDULING (pad);
3484 case GST_QUERY_ALLOCATION:
3485 forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
3487 case GST_QUERY_ACCEPT_CAPS:
3488 ret = gst_pad_query_accept_caps_default (pad, query);
3491 case GST_QUERY_CAPS:
3492 ret = gst_pad_query_caps_default (pad, query);
3495 case GST_QUERY_LATENCY:
3496 ret = gst_pad_query_latency_default (pad, query);
3499 case GST_QUERY_BITRATE:
3500 /* FIXME: better default handling */
3503 case GST_QUERY_POSITION:
3504 case GST_QUERY_SEEKING:
3505 case GST_QUERY_FORMATS:
3506 case GST_QUERY_JITTER:
3507 case GST_QUERY_RATE:
3508 case GST_QUERY_CONVERT:
3514 GST_DEBUG_OBJECT (pad, "%sforwarding %p (%s) query", (forward ? "" : "not "),
3515 query, GST_QUERY_TYPE_NAME (query));
3521 data.dispatched = FALSE;
3522 data.result = FALSE;
3524 gst_pad_forward (pad, (GstPadForwardFunction) query_forward_func, &data);
3526 if (data.dispatched) {
3529 /* nothing dispatched, assume drained */
3530 if (GST_QUERY_TYPE (query) == GST_QUERY_DRAIN)
3539 #define N_STACK_ALLOCATE_PROBES (16)
3541 /* A helper that checks if a probe was already
3542 * in the called_probes list, and adds it if
3543 * not. Used to avoid calling probes a 2nd time when
3544 * looping again after probe removal */
3546 check_probe_already_called (GHook * hook, ProbeMarshall * data)
3550 /* if we have called this callback, do nothing. But only check
3551 * if we're actually calling probes a second time */
3553 for (i = 0; i < data->n_called_probes; i++) {
3554 if (data->called_probes[i] == hook->hook_id) {
3560 /* reallocate on the heap if we had more than 16 probes */
3561 if (data->n_called_probes == data->called_probes_size) {
3562 if (data->called_probes_size > N_STACK_ALLOCATE_PROBES) {
3563 data->called_probes_size *= 2;
3564 data->called_probes =
3565 g_renew (gulong, data->called_probes, data->called_probes_size);
3567 gulong *tmp = data->called_probes;
3569 data->called_probes_size *= 2;
3570 data->called_probes = g_new (gulong, data->called_probes_size);
3571 memcpy (data->called_probes, tmp,
3572 N_STACK_ALLOCATE_PROBES * sizeof (gulong));
3575 data->called_probes[data->n_called_probes++] = hook->hook_id;
3577 /* This probe was not alraedy called */
3582 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3584 GstPad *pad = data->pad;
3585 GstPadProbeInfo *info = data->info;
3586 GstPadProbeType type, flags;
3587 GstPadProbeCallback callback;
3588 GstPadProbeReturn ret;
3589 gpointer original_data;
3591 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3593 original_data = info->data;
3595 /* one of the scheduling types */
3596 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
3599 if (G_UNLIKELY (data->handled)) {
3600 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3601 "probe previously returned HANDLED, not calling again");
3603 } else if (G_UNLIKELY (data->dropped)) {
3604 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3605 "probe previously returned DROPPED, not calling again");
3609 if (type & GST_PAD_PROBE_TYPE_PUSH) {
3610 /* one of the data types for non-idle probes */
3611 if ((type & GST_PAD_PROBE_TYPE_IDLE) == 0
3612 && (flags & _PAD_PROBE_TYPE_ALL_BOTH_AND_FLUSH & type) == 0)
3614 } else if (type & GST_PAD_PROBE_TYPE_PULL) {
3615 /* one of the data types for non-idle probes */
3616 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) == 0
3617 && (flags & _PAD_PROBE_TYPE_ALL_BOTH_AND_FLUSH & type) == 0)
3620 /* Type must have PULL or PUSH probe types */
3621 g_assert_not_reached ();
3624 /* one of the blocking types must match */
3625 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
3626 (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
3628 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) == 0 &&
3629 (flags & GST_PAD_PROBE_TYPE_BLOCKING))
3631 /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
3632 if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
3633 (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
3636 if (check_probe_already_called (hook, data)) {
3637 /* Reset marshalled = TRUE here, because the probe
3638 * was already called and set it the first time around,
3639 * and we may want to keep blocking on it.
3641 * https://gitlab.freedesktop.org/gstreamer/gstreamer/-/issues/658
3643 data->marshalled = TRUE;
3644 goto already_called;
3647 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3648 "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
3650 callback = (GstPadProbeCallback) hook->func;
3651 if (callback == NULL) {
3652 /* No callback is equivalent to just returning GST_PAD_PROBE_OK */
3653 data->marshalled = TRUE;
3657 info->id = hook->hook_id;
3659 if ((flags & GST_PAD_PROBE_TYPE_IDLE))
3660 pad->priv->idle_running++;
3662 GST_OBJECT_UNLOCK (pad);
3664 ret = callback (pad, info, hook->data);
3666 GST_OBJECT_LOCK (pad);
3668 /* If the probe callback asked for the
3669 * probe to be removed, don't set the marshalled flag
3670 * otherwise, you can get a case where you return
3671 * GST_PAD_PROBE_REMOVE from a buffer probe and
3672 * then the pad blocks anyway if there's any other
3673 * blocking probes installed.
3675 * https://gitlab.freedesktop.org/gstreamer/gstreamer/-/issues/658
3677 if (ret != GST_PAD_PROBE_REMOVE)
3678 data->marshalled = TRUE;
3680 if ((flags & GST_PAD_PROBE_TYPE_IDLE))
3681 pad->priv->idle_running--;
3683 if (ret != GST_PAD_PROBE_HANDLED && original_data != NULL
3684 && info->data == NULL) {
3685 GST_DEBUG_OBJECT (pad, "data item in pad probe info was dropped");
3686 info->type = GST_PAD_PROBE_TYPE_INVALID;
3687 data->dropped = TRUE;
3691 case GST_PAD_PROBE_REMOVE:
3692 /* remove the probe */
3693 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3694 cleanup_hook (pad, hook);
3696 case GST_PAD_PROBE_DROP:
3697 /* need to drop the data, make sure other probes don't get called
3699 GST_DEBUG_OBJECT (pad, "asked to drop item");
3700 info->type = GST_PAD_PROBE_TYPE_INVALID;
3701 data->dropped = TRUE;
3703 case GST_PAD_PROBE_HANDLED:
3704 GST_DEBUG_OBJECT (pad, "probe handled data");
3705 data->handled = TRUE;
3707 case GST_PAD_PROBE_PASS:
3708 /* inform the pad block to let things pass */
3709 GST_DEBUG_OBJECT (pad, "asked to pass item");
3712 case GST_PAD_PROBE_OK:
3713 GST_DEBUG_OBJECT (pad, "probe returned OK");
3716 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3723 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3724 "hook %lu with flags 0x%08x does not match %08x",
3725 hook->hook_id, flags, info->type);
3730 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3731 "hook %lu already called", hook->hook_id);
3736 /* a probe that does not take or return any data */
3737 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
3739 if (G_UNLIKELY (pad->num_probes)) { \
3740 GstFlowReturn pval = defaultval; \
3741 /* pass NULL as the data item */ \
3742 GstPadProbeInfo info = { mask, 0, NULL, 0, 0 }; \
3743 info.ABI.abi.flow_ret = defaultval; \
3744 ret = do_probe_callbacks (pad, &info, defaultval); \
3745 if (G_UNLIKELY (ret != pval && ret != GST_FLOW_OK)) \
3750 #define PROBE_FULL(pad,mask,data,offs,size,label,handleable,handle_label) \
3752 if (G_UNLIKELY (pad->num_probes)) { \
3753 /* pass the data item */ \
3754 GstPadProbeInfo info = { mask, 0, data, offs, size }; \
3755 info.ABI.abi.flow_ret = GST_FLOW_OK; \
3756 ret = do_probe_callbacks (pad, &info, GST_FLOW_OK); \
3757 /* store the possibly updated data item */ \
3758 data = GST_PAD_PROBE_INFO_DATA (&info); \
3759 /* if something went wrong, exit */ \
3760 if (G_UNLIKELY (ret != GST_FLOW_OK)) { \
3761 if (handleable && ret == GST_FLOW_CUSTOM_SUCCESS_1) { \
3762 ret = info.ABI.abi.flow_ret; \
3763 goto handle_label; \
3770 #define PROBE_PUSH(pad,mask,data,label) \
3771 PROBE_FULL(pad, mask, data, -1, -1, label, FALSE, label);
3772 #define PROBE_HANDLE(pad,mask,data,label,handle_label) \
3773 PROBE_FULL(pad, mask, data, -1, -1, label, TRUE, handle_label);
3774 #define PROBE_PULL(pad,mask,data,offs,size,label) \
3775 PROBE_FULL(pad, mask, data, offs, size, label, FALSE, label);
3777 static GstFlowReturn
3778 do_pad_idle_probe_wait (GstPad * pad)
3780 while (GST_PAD_IS_RUNNING_IDLE_PROBE (pad)) {
3781 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3782 "waiting idle probe to be removed");
3783 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3784 GST_PAD_BLOCK_WAIT (pad);
3785 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3786 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3788 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3789 return GST_FLOW_FLUSHING;
3794 #define PROBE_TYPE_IS_SERIALIZED(i) \
3797 (((i)->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM | \
3798 GST_PAD_PROBE_TYPE_EVENT_FLUSH)) && \
3799 GST_EVENT_IS_SERIALIZED ((i)->data)) \
3801 (((i)->type & GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM) && \
3802 GST_QUERY_IS_SERIALIZED ((i)->data)) \
3804 ((i)->type & (GST_PAD_PROBE_TYPE_BUFFER | \
3805 GST_PAD_PROBE_TYPE_BUFFER_LIST)) \
3809 static GstFlowReturn
3810 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
3811 GstFlowReturn defaultval)
3816 gulong called_probes[N_STACK_ALLOCATE_PROBES];
3821 data.handled = FALSE;
3822 data.dropped = FALSE;
3824 /* We stack-allocate for N_STACK_ALLOCATE_PROBES hooks as a first step. If more are needed,
3825 * we will re-allocate with g_malloc(). This should usually never be needed
3827 data.called_probes = called_probes;
3828 data.n_called_probes = 0;
3829 data.called_probes_size = N_STACK_ALLOCATE_PROBES;
3833 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
3835 if (is_block && PROBE_TYPE_IS_SERIALIZED (info)) {
3836 if (do_pad_idle_probe_wait (pad) == GST_FLOW_FLUSHING)
3841 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "do probes");
3842 cookie = pad->priv->probe_list_cookie;
3844 /* Clear the marshalled flag before doing callbacks. Only if
3845 * there are matching callbacks still will it get set */
3846 data.marshalled = FALSE;
3848 g_hook_list_marshal (&pad->probes, TRUE,
3849 (GHookMarshaller) probe_hook_marshal, &data);
3851 /* if the list changed, call the new callbacks (they will not be in
3852 * called_probes yet) */
3853 if (cookie != pad->priv->probe_list_cookie) {
3854 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3855 "probe list changed, restarting");
3860 /* the first item that dropped will stop the hooks and then we drop here */
3864 /* If one handler took care of it, let the the item pass */
3869 /* if no handler matched and we are blocking, let the item pass */
3870 if (!data.marshalled && is_block)
3873 /* At this point, all handlers returned either OK or PASS. If one handler
3874 * returned PASS, let the item pass */
3879 while (GST_PAD_IS_BLOCKED (pad)) {
3880 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3881 "we are blocked %d times", pad->num_blocked);
3883 /* we might have released the lock */
3884 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3887 /* now we block the streaming thread. It can be unlocked when we
3888 * deactivate the pad (which will also set the FLUSHING flag) or
3889 * when the pad is unblocked. A flushing event will also unblock
3890 * the pad after setting the FLUSHING flag. */
3891 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3892 "Waiting to be unblocked or set flushing");
3893 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3894 GST_PAD_BLOCK_WAIT (pad);
3895 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3896 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3898 /* if the list changed, call the new callbacks (they will not be in
3899 * called_probes yet) */
3900 if (cookie != pad->priv->probe_list_cookie) {
3901 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3902 "probe list changed, restarting");
3907 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3912 if (data.called_probes_size > N_STACK_ALLOCATE_PROBES)
3913 g_free (data.called_probes);
3920 GST_DEBUG_OBJECT (pad, "pad is flushing");
3921 if (data.called_probes_size > N_STACK_ALLOCATE_PROBES)
3922 g_free (data.called_probes);
3923 return GST_FLOW_FLUSHING;
3927 GST_DEBUG_OBJECT (pad, "data is dropped");
3928 if (data.called_probes_size > N_STACK_ALLOCATE_PROBES)
3929 g_free (data.called_probes);
3930 return GST_FLOW_CUSTOM_SUCCESS;
3934 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3935 GST_DEBUG_OBJECT (pad, "data is passed");
3936 if (data.called_probes_size > N_STACK_ALLOCATE_PROBES)
3937 g_free (data.called_probes);
3942 GST_DEBUG_OBJECT (pad, "data was handled");
3943 if (data.called_probes_size > N_STACK_ALLOCATE_PROBES)
3944 g_free (data.called_probes);
3945 return GST_FLOW_CUSTOM_SUCCESS_1;
3952 * gst_pad_get_offset:
3955 * Get the offset applied to the running time of @pad. @pad has to be a source
3958 * Returns: the offset.
3961 gst_pad_get_offset (GstPad * pad)
3965 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3967 GST_OBJECT_LOCK (pad);
3968 result = pad->offset;
3969 GST_OBJECT_UNLOCK (pad);
3975 mark_event_not_received (GstPad * pad, PadEvent * ev, gpointer user_data)
3977 ev->received = FALSE;
3982 * gst_pad_set_offset:
3984 * @offset: the offset
3986 * Set the offset that will be applied to the running time of @pad.
3989 gst_pad_set_offset (GstPad * pad, gint64 offset)
3991 g_return_if_fail (GST_IS_PAD (pad));
3993 GST_OBJECT_LOCK (pad);
3994 /* if nothing changed, do nothing */
3995 if (pad->offset == offset)
3998 pad->offset = offset;
3999 GST_DEBUG_OBJECT (pad, "changed offset to %" GST_STIME_FORMAT,
4000 GST_STIME_ARGS (offset));
4002 /* resend all sticky events with updated offset on next buffer push */
4003 events_foreach (pad, mark_event_not_received, NULL);
4004 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4007 GST_OBJECT_UNLOCK (pad);
4014 /* If TRUE and ret is not OK this means
4015 * that pushing the EOS event failed
4019 /* If called for an event this is
4020 * the event that would be pushed
4021 * next. Don't forward sticky events
4022 * that would come after that */
4026 /* should be called with pad LOCK */
4028 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
4030 PushStickyData *data = user_data;
4031 GstEvent *event = ev->event;
4034 GST_DEBUG_OBJECT (pad, "event %s was already received",
4035 GST_EVENT_TYPE_NAME (event));
4039 /* If we're called because of an sticky event, only forward
4040 * events that would come before this new event and the
4042 if (data->event && GST_EVENT_IS_STICKY (data->event) &&
4043 GST_EVENT_TYPE (data->event) <= GST_EVENT_SEGMENT &&
4044 GST_EVENT_TYPE (data->event) < GST_EVENT_TYPE (event)) {
4045 data->ret = GST_FLOW_CUSTOM_SUCCESS_1;
4047 data->ret = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
4048 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
4049 if (data->ret == GST_FLOW_CUSTOM_SUCCESS_1)
4050 data->ret = GST_FLOW_OK;
4053 switch (data->ret) {
4055 ev->received = TRUE;
4056 GST_DEBUG_OBJECT (pad, "event %s marked received",
4057 GST_EVENT_TYPE_NAME (event));
4059 case GST_FLOW_CUSTOM_SUCCESS:
4060 /* we can't assume the event is received when it was dropped */
4061 GST_DEBUG_OBJECT (pad, "event %s was dropped, mark pending",
4062 GST_EVENT_TYPE_NAME (event));
4063 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4064 data->ret = GST_FLOW_OK;
4066 case GST_FLOW_CUSTOM_SUCCESS_1:
4067 /* event was ignored and should be sent later */
4068 GST_DEBUG_OBJECT (pad, "event %s was ignored, mark pending",
4069 GST_EVENT_TYPE_NAME (event));
4070 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4071 data->ret = GST_FLOW_OK;
4073 case GST_FLOW_NOT_LINKED:
4074 /* not linked is not a problem, we are sticky so the event will be
4075 * rescheduled to be sent later on re-link, but only for non-EOS events */
4076 GST_DEBUG_OBJECT (pad, "pad was not linked, mark pending");
4077 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS) {
4078 data->ret = GST_FLOW_OK;
4079 ev->received = TRUE;
4083 GST_DEBUG_OBJECT (pad, "result %s, mark pending events",
4084 gst_flow_get_name (data->ret));
4085 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4089 if (data->ret != GST_FLOW_OK && GST_EVENT_TYPE (event) == GST_EVENT_EOS)
4090 data->was_eos = TRUE;
4092 return data->ret == GST_FLOW_OK;
4095 /* check sticky events and push them when needed. should be called
4097 static inline GstFlowReturn
4098 check_sticky (GstPad * pad, GstEvent * event)
4100 PushStickyData data = { GST_FLOW_OK, FALSE, event };
4102 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
4103 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4105 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
4106 events_foreach (pad, push_sticky, &data);
4108 /* If there's an EOS event we must push it downstream
4109 * even if sending a previous sticky event failed.
4110 * Otherwise the pipeline might wait forever for EOS.
4112 * Only do this if pushing another event than the EOS
4115 if (data.ret != GST_FLOW_OK && !data.was_eos) {
4116 PadEvent *ev = find_event_by_type (pad, GST_EVENT_EOS, 0);
4118 if (ev && !ev->received) {
4119 data.ret = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
4120 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
4121 /* the event could have been dropped. Because this can only
4122 * happen if the user asked for it, it's not an error */
4123 if (data.ret == GST_FLOW_CUSTOM_SUCCESS)
4124 data.ret = GST_FLOW_OK;
4134 * @pad: a #GstPad to invoke the default query on.
4135 * @query: (transfer none): the #GstQuery to perform.
4137 * Dispatches a query to a pad. The query should have been allocated by the
4138 * caller via one of the type-specific allocation functions. The element that
4139 * the pad belongs to is responsible for filling the query with an appropriate
4140 * response, which should then be parsed with a type-specific query parsing
4143 * Again, the caller is responsible for both the allocation and deallocation of
4144 * the query structure.
4146 * Please also note that some queries might need a running pipeline to work.
4148 * Returns: %TRUE if the query could be performed.
4151 gst_pad_query (GstPad * pad, GstQuery * query)
4154 gboolean res, serialized;
4155 GstPadQueryFunction func;
4156 GstPadProbeType type;
4159 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4160 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
4162 if (GST_PAD_IS_SRC (pad)) {
4163 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
4164 goto wrong_direction;
4165 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
4166 } else if (GST_PAD_IS_SINK (pad)) {
4167 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
4168 goto wrong_direction;
4169 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
4171 goto unknown_direction;
4173 GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
4174 GST_QUERY_TYPE_NAME (query));
4175 GST_TRACER_PAD_QUERY_PRE (pad, query);
4177 serialized = GST_QUERY_IS_SERIALIZED (query);
4178 if (G_UNLIKELY (serialized))
4179 GST_PAD_STREAM_LOCK (pad);
4181 GST_OBJECT_LOCK (pad);
4182 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4183 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
4184 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
4186 ACQUIRE_PARENT (pad, parent, no_parent);
4187 GST_OBJECT_UNLOCK (pad);
4189 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
4192 res = func (pad, parent, query);
4194 RELEASE_PARENT (parent);
4196 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
4197 GST_QUERY_TYPE_NAME (query), res);
4198 GST_TRACER_PAD_QUERY_POST (pad, query, res);
4203 GST_OBJECT_LOCK (pad);
4204 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
4205 GST_OBJECT_UNLOCK (pad);
4207 if (G_UNLIKELY (serialized))
4208 GST_PAD_STREAM_UNLOCK (pad);
4215 g_warning ("pad %s:%s query %s in wrong direction",
4216 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
4221 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4226 GST_DEBUG_OBJECT (pad, "had no parent");
4227 GST_OBJECT_UNLOCK (pad);
4228 if (G_UNLIKELY (serialized))
4229 GST_PAD_STREAM_UNLOCK (pad);
4234 GST_DEBUG_OBJECT (pad, "had no query function");
4235 RELEASE_PARENT (parent);
4236 if (G_UNLIKELY (serialized))
4237 GST_PAD_STREAM_UNLOCK (pad);
4242 GST_DEBUG_OBJECT (pad, "query failed");
4243 if (G_UNLIKELY (serialized))
4244 GST_PAD_STREAM_UNLOCK (pad);
4249 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
4250 GST_OBJECT_UNLOCK (pad);
4251 if (G_UNLIKELY (serialized))
4252 GST_PAD_STREAM_UNLOCK (pad);
4254 /* if a probe dropped without handling, we don't sent it further but assume
4255 * that the probe did not answer the query and return FALSE */
4256 if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
4266 * gst_pad_peer_query:
4267 * @pad: a #GstPad to invoke the peer query on.
4268 * @query: (transfer none): the #GstQuery to perform.
4270 * Performs gst_pad_query() on the peer of @pad.
4272 * The caller is responsible for both the allocation and deallocation of
4273 * the query structure.
4275 * Returns: %TRUE if the query could be performed. This function returns %FALSE
4276 * if @pad has no peer.
4279 gst_pad_peer_query (GstPad * pad, GstQuery * query)
4282 GstPadProbeType type;
4283 gboolean res, serialized;
4286 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4287 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
4289 if (GST_PAD_IS_SRC (pad)) {
4290 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
4291 goto wrong_direction;
4292 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
4293 } else if (GST_PAD_IS_SINK (pad)) {
4294 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
4295 goto wrong_direction;
4296 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
4298 goto unknown_direction;
4300 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
4301 GST_QUERY_TYPE_NAME (query));
4303 serialized = GST_QUERY_IS_SERIALIZED (query);
4305 GST_OBJECT_LOCK (pad);
4306 if (GST_PAD_IS_SRC (pad) && serialized) {
4307 /* all serialized queries on the srcpad trigger push of
4309 if (check_sticky (pad, NULL) != GST_FLOW_OK)
4313 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4314 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
4315 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
4317 peerpad = GST_PAD_PEER (pad);
4318 if (G_UNLIKELY (peerpad == NULL))
4321 gst_object_ref (peerpad);
4322 GST_OBJECT_UNLOCK (pad);
4324 res = gst_pad_query (peerpad, query);
4326 gst_object_unref (peerpad);
4331 GST_OBJECT_LOCK (pad);
4332 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
4333 GST_OBJECT_UNLOCK (pad);
4340 g_warning ("pad %s:%s query %s in wrong direction",
4341 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
4346 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4351 GST_WARNING_OBJECT (pad, "could not send sticky events");
4352 GST_OBJECT_UNLOCK (pad);
4357 GST_INFO_OBJECT (pad, "pad has no peer");
4358 GST_OBJECT_UNLOCK (pad);
4363 GST_DEBUG_OBJECT (pad, "query failed");
4368 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
4369 GST_OBJECT_UNLOCK (pad);
4371 /* if a probe dropped without handling, we don't sent it further but
4372 * assume that the probe did not answer the query and return FALSE */
4373 if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
4382 /**********************************************************************
4383 * Data passing functions
4386 /* this is the chain function that does not perform the additional argument
4387 * checking for that little extra speed.
4389 static inline GstFlowReturn
4390 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
4394 gboolean handled = FALSE;
4396 GST_PAD_STREAM_LOCK (pad);
4398 GST_OBJECT_LOCK (pad);
4399 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4402 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4405 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4408 #ifdef GST_ENABLE_EXTRA_CHECKS
4409 if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4410 if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4412 ":%s:<%s:%s> Got data flow before stream-start event",
4413 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4415 if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4417 ":%s:<%s:%s> Got data flow before segment event",
4418 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4420 pad->priv->last_cookie = pad->priv->events_cookie;
4424 PROBE_HANDLE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped,
4427 PROBE_HANDLE (pad, type, data, probe_stopped, probe_handled);
4429 ACQUIRE_PARENT (pad, parent, no_parent);
4430 GST_OBJECT_UNLOCK (pad);
4432 /* NOTE: we read the chainfunc unlocked.
4433 * we cannot hold the lock for the pad so we might send
4434 * the data to the wrong function. This is not really a
4435 * problem since functions are assigned at creation time
4436 * and don't change that often... */
4437 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
4438 GstPadChainFunction chainfunc;
4440 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4443 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4444 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
4445 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
4447 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
4449 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4450 "called chainfunction &%s with buffer %p, returned %s",
4451 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
4453 GstPadChainListFunction chainlistfunc;
4455 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4458 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4459 "calling chainlistfunction &%s",
4460 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4462 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
4464 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4465 "called chainlistfunction &%s, returned %s",
4466 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4469 pad->ABI.abi.last_flowret = ret;
4471 RELEASE_PARENT (parent);
4473 GST_PAD_STREAM_UNLOCK (pad);
4480 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4481 "chaining, but pad was flushing");
4482 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4483 GST_OBJECT_UNLOCK (pad);
4484 GST_PAD_STREAM_UNLOCK (pad);
4485 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4486 return GST_FLOW_FLUSHING;
4490 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
4491 pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4492 GST_OBJECT_UNLOCK (pad);
4493 GST_PAD_STREAM_UNLOCK (pad);
4494 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4495 return GST_FLOW_EOS;
4499 g_critical ("chain on pad %s:%s but it was not in push mode",
4500 GST_DEBUG_PAD_NAME (pad));
4501 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4502 GST_OBJECT_UNLOCK (pad);
4503 GST_PAD_STREAM_UNLOCK (pad);
4504 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4505 return GST_FLOW_ERROR;
4512 /* We unref the buffer, except if the probe handled it (CUSTOM_SUCCESS_1) */
4514 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4517 case GST_FLOW_CUSTOM_SUCCESS:
4518 case GST_FLOW_CUSTOM_SUCCESS_1:
4519 GST_DEBUG_OBJECT (pad, "dropped or handled buffer");
4523 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4526 pad->ABI.abi.last_flowret = ret;
4527 GST_OBJECT_UNLOCK (pad);
4528 GST_PAD_STREAM_UNLOCK (pad);
4533 GST_DEBUG_OBJECT (pad, "No parent when chaining %" GST_PTR_FORMAT, data);
4534 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4535 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4536 GST_OBJECT_UNLOCK (pad);
4537 GST_PAD_STREAM_UNLOCK (pad);
4538 return GST_FLOW_FLUSHING;
4542 pad->ABI.abi.last_flowret = GST_FLOW_NOT_SUPPORTED;
4543 RELEASE_PARENT (parent);
4544 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4545 g_critical ("chain on pad %s:%s but it has no chainfunction",
4546 GST_DEBUG_PAD_NAME (pad));
4547 GST_PAD_STREAM_UNLOCK (pad);
4548 return GST_FLOW_NOT_SUPPORTED;
4554 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4555 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
4558 * Chain a buffer to @pad.
4560 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4562 * If the buffer type is not acceptable for @pad (as negotiated with a
4563 * preceding GST_EVENT_CAPS event), this function returns
4564 * #GST_FLOW_NOT_NEGOTIATED.
4566 * The function proceeds calling the chain function installed on @pad (see
4567 * gst_pad_set_chain_function()) and the return value of that function is
4568 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4571 * In all cases, success or failure, the caller loses its reference to @buffer
4572 * after calling this function.
4574 * Returns: a #GstFlowReturn from the pad.
4579 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4581 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4582 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4583 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4585 return gst_pad_chain_data_unchecked (pad,
4586 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4589 static GstFlowReturn
4590 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
4591 GstBufferList * list)
4597 GST_LOG_OBJECT (pad, "chaining each buffer in list individually");
4599 len = gst_buffer_list_length (list);
4602 for (i = 0; i < len; i++) {
4603 buffer = gst_buffer_list_get (list, i);
4605 gst_pad_chain_data_unchecked (pad,
4606 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
4607 gst_buffer_ref (buffer));
4608 if (ret != GST_FLOW_OK)
4611 gst_buffer_list_unref (list);
4617 * gst_pad_chain_list:
4618 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4619 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
4622 * Chain a bufferlist to @pad.
4624 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4626 * If @pad was not negotiated properly with a CAPS event, this function
4627 * returns #GST_FLOW_NOT_NEGOTIATED.
4629 * The function proceeds calling the chainlist function installed on @pad (see
4630 * gst_pad_set_chain_list_function()) and the return value of that function is
4631 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4632 * chainlist function.
4634 * In all cases, success or failure, the caller loses its reference to @list
4635 * after calling this function.
4639 * Returns: a #GstFlowReturn from the pad.
4642 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4644 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4645 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4646 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4648 return gst_pad_chain_data_unchecked (pad,
4649 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4652 static GstFlowReturn
4653 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
4657 gboolean handled = FALSE;
4659 GST_OBJECT_LOCK (pad);
4660 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4663 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4666 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4669 #ifdef GST_ENABLE_EXTRA_CHECKS
4670 if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4671 if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4673 ":%s:<%s:%s> Got data flow before stream-start event",
4674 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4676 if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4678 ":%s:<%s:%s> Got data flow before segment event",
4679 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4681 pad->priv->last_cookie = pad->priv->events_cookie;
4685 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4688 /* do block probes */
4689 PROBE_HANDLE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped,
4692 /* recheck sticky events because the probe might have cause a relink */
4693 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4696 /* do post-blocking probes */
4697 PROBE_HANDLE (pad, type, data, probe_stopped, probe_handled);
4699 /* recheck sticky events because the probe might have cause a relink */
4700 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4703 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4706 /* take ref to peer pad before releasing the lock */
4707 gst_object_ref (peer);
4709 GST_OBJECT_UNLOCK (pad);
4711 ret = gst_pad_chain_data_unchecked (peer, type, data);
4714 gst_object_unref (peer);
4716 GST_OBJECT_LOCK (pad);
4717 pad->ABI.abi.last_flowret = ret;
4719 if (pad->priv->using == 0) {
4720 /* pad is not active anymore, trigger idle callbacks */
4721 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4722 probe_stopped, ret);
4724 GST_OBJECT_UNLOCK (pad);
4728 /* ERROR recovery here */
4732 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4733 "pushing, but pad was flushing");
4734 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4735 GST_OBJECT_UNLOCK (pad);
4736 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4737 return GST_FLOW_FLUSHING;
4741 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
4742 pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4743 GST_OBJECT_UNLOCK (pad);
4744 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4745 return GST_FLOW_EOS;
4749 g_critical ("pushing on pad %s:%s but it was not activated in push mode",
4750 GST_DEBUG_PAD_NAME (pad));
4751 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4752 GST_OBJECT_UNLOCK (pad);
4753 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4754 return GST_FLOW_ERROR;
4758 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4759 "error pushing events, return %s", gst_flow_get_name (ret));
4760 pad->ABI.abi.last_flowret = ret;
4761 GST_OBJECT_UNLOCK (pad);
4762 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4770 GST_OBJECT_UNLOCK (pad);
4771 if (data != NULL && !handled)
4772 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4775 case GST_FLOW_CUSTOM_SUCCESS:
4776 case GST_FLOW_CUSTOM_SUCCESS_1:
4777 GST_DEBUG_OBJECT (pad, "dropped or handled buffer");
4781 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4784 pad->ABI.abi.last_flowret = ret;
4789 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4790 "pushing, but it was not linked");
4791 pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4792 GST_OBJECT_UNLOCK (pad);
4793 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4794 return GST_FLOW_NOT_LINKED;
4800 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4801 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4804 * Pushes a buffer to the peer of @pad.
4806 * This function will call installed block probes before triggering any
4807 * installed data probes.
4809 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4810 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4813 * In all cases, success or failure, the caller loses its reference to @buffer
4814 * after calling this function.
4816 * Returns: a #GstFlowReturn from the peer pad.
4821 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4825 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4826 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4827 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4829 GST_TRACER_PAD_PUSH_PRE (pad, buffer);
4830 res = gst_pad_push_data (pad,
4831 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4832 GST_TRACER_PAD_PUSH_POST (pad, res);
4837 * gst_pad_push_list:
4838 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4839 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4842 * Pushes a buffer list to the peer of @pad.
4844 * This function will call installed block probes before triggering any
4845 * installed data probes.
4847 * The function proceeds calling the chain function on the peer pad and returns
4848 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4849 * be returned. If the peer pad does not have any installed chainlist function
4850 * every group buffer of the list will be merged into a normal #GstBuffer and
4851 * chained via gst_pad_chain().
4853 * In all cases, success or failure, the caller loses its reference to @list
4854 * after calling this function.
4856 * Returns: a #GstFlowReturn from the peer pad.
4861 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4865 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4866 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4867 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4869 GST_TRACER_PAD_PUSH_LIST_PRE (pad, list);
4870 res = gst_pad_push_data (pad,
4871 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4872 GST_TRACER_PAD_PUSH_LIST_POST (pad, res);
4876 static GstFlowReturn
4877 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4878 GstBuffer ** buffer)
4881 GstPadGetRangeFunction getrangefunc;
4885 GST_PAD_STREAM_LOCK (pad);
4887 GST_OBJECT_LOCK (pad);
4888 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4891 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4894 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4899 /* when one of the probes returns DROPPED, probe_stopped will be called
4900 * and we skip calling the getrange function, res_buf should then contain a
4901 * valid result buffer */
4902 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4903 res_buf, offset, size, probe_stopped);
4905 /* recheck sticky events because the probe might have cause a relink */
4906 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4909 ACQUIRE_PARENT (pad, parent, no_parent);
4910 GST_OBJECT_UNLOCK (pad);
4912 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4915 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4916 "calling getrangefunc %s, offset %"
4917 G_GUINT64_FORMAT ", size %u",
4918 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4920 ret = getrangefunc (pad, parent, offset, size, &res_buf);
4922 RELEASE_PARENT (parent);
4924 GST_OBJECT_LOCK (pad);
4925 if (G_UNLIKELY (ret != GST_FLOW_OK))
4926 goto get_range_failed;
4928 /* can only fire the signal if we have a valid buffer */
4930 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4931 res_buf, offset, size, probe_stopped_unref);
4932 pad->ABI.abi.last_flowret = ret;
4933 GST_OBJECT_UNLOCK (pad);
4935 GST_PAD_STREAM_UNLOCK (pad);
4937 /* If the caller provided a buffer it must be filled by the getrange
4938 * function instead of it returning a new buffer */
4939 g_return_val_if_fail (!*buffer || res_buf == *buffer, GST_FLOW_ERROR);
4948 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4949 "getrange, but pad was flushing");
4950 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4951 GST_OBJECT_UNLOCK (pad);
4952 GST_PAD_STREAM_UNLOCK (pad);
4953 return GST_FLOW_FLUSHING;
4957 g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4958 GST_DEBUG_PAD_NAME (pad));
4959 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4960 GST_OBJECT_UNLOCK (pad);
4961 GST_PAD_STREAM_UNLOCK (pad);
4962 return GST_FLOW_ERROR;
4966 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4967 pad->ABI.abi.last_flowret = ret;
4968 GST_OBJECT_UNLOCK (pad);
4969 GST_PAD_STREAM_UNLOCK (pad);
4974 GST_DEBUG_OBJECT (pad, "no parent");
4975 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4976 GST_OBJECT_UNLOCK (pad);
4977 GST_PAD_STREAM_UNLOCK (pad);
4978 return GST_FLOW_FLUSHING;
4982 g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4983 GST_DEBUG_PAD_NAME (pad));
4984 RELEASE_PARENT (parent);
4985 GST_PAD_STREAM_UNLOCK (pad);
4986 return GST_FLOW_NOT_SUPPORTED;
4990 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4991 "probe returned %s", gst_flow_get_name (ret));
4992 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4994 /* the probe filled the buffer and asks us to not call the getrange
4995 * anymore, we continue with the post probes then. */
4996 GST_DEBUG_OBJECT (pad, "handled buffer");
5000 /* no buffer, we are EOS */
5001 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
5005 pad->ABI.abi.last_flowret = ret;
5006 GST_OBJECT_UNLOCK (pad);
5007 GST_PAD_STREAM_UNLOCK (pad);
5011 probe_stopped_unref:
5013 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
5014 "probe returned %s", gst_flow_get_name (ret));
5015 /* if we drop here, it signals EOS */
5016 if (ret == GST_FLOW_CUSTOM_SUCCESS)
5018 pad->ABI.abi.last_flowret = ret;
5019 GST_OBJECT_UNLOCK (pad);
5020 GST_PAD_STREAM_UNLOCK (pad);
5021 if (*buffer == NULL)
5022 gst_buffer_unref (res_buf);
5027 pad->ABI.abi.last_flowret = ret;
5028 GST_OBJECT_UNLOCK (pad);
5029 GST_PAD_STREAM_UNLOCK (pad);
5030 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
5031 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
5032 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
5038 * gst_pad_get_range:
5039 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
5040 * @offset: The start offset of the buffer
5041 * @size: The length of the buffer
5042 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
5043 * returns #GST_FLOW_ERROR if %NULL.
5045 * When @pad is flushing this function returns #GST_FLOW_FLUSHING
5046 * immediately and @buffer is %NULL.
5048 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
5049 * description of a getrange function. If @pad has no getrange function
5050 * installed (see gst_pad_set_getrange_function()) this function returns
5051 * #GST_FLOW_NOT_SUPPORTED.
5053 * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
5054 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
5055 * must be freed with gst_buffer_unref() after usage.
5057 * When @buffer points to a variable that points to a valid #GstBuffer, the
5058 * buffer will be filled with the result data when this function returns
5059 * #GST_FLOW_OK. If the provided buffer is larger than @size, only
5060 * @size bytes will be filled in the result buffer and its size will be updated
5063 * Note that less than @size bytes can be returned in @buffer when, for example,
5064 * an EOS condition is near or when @buffer is not large enough to hold @size
5065 * bytes. The caller should check the result buffer size to get the result size.
5067 * When this function returns any other result value than #GST_FLOW_OK, @buffer
5068 * will be unchanged.
5070 * This is a lowlevel function. Usually gst_pad_pull_range() is used.
5072 * Returns: a #GstFlowReturn from the pad.
5077 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
5078 GstBuffer ** buffer)
5080 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
5081 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
5082 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
5083 g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
5084 && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
5086 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
5090 * gst_pad_pull_range:
5091 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
5092 * @offset: The start offset of the buffer
5093 * @size: The length of the buffer
5094 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
5095 * GST_FLOW_ERROR if %NULL.
5097 * Pulls a @buffer from the peer pad or fills up a provided buffer.
5099 * This function will first trigger the pad block signal if it was
5102 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
5103 * function returns the result of gst_pad_get_range() on the peer pad.
5104 * See gst_pad_get_range() for a list of return values and for the
5105 * semantics of the arguments of this function.
5107 * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
5108 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
5109 * must be freed with gst_buffer_unref() after usage. When this function
5110 * returns any other result value, @buffer will still point to %NULL.
5112 * When @buffer points to a variable that points to a valid #GstBuffer, the
5113 * buffer will be filled with the result data when this function returns
5114 * #GST_FLOW_OK. When this function returns any other result value,
5115 * @buffer will be unchanged. If the provided buffer is larger than @size, only
5116 * @size bytes will be filled in the result buffer and its size will be updated
5119 * Note that less than @size bytes can be returned in @buffer when, for example,
5120 * an EOS condition is near or when @buffer is not large enough to hold @size
5121 * bytes. The caller should check the result buffer size to get the result size.
5123 * Returns: a #GstFlowReturn from the peer pad.
5128 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
5129 GstBuffer ** buffer)
5135 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
5136 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
5137 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
5138 g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
5139 && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
5141 GST_TRACER_PAD_PULL_RANGE_PRE (pad, offset, size);
5143 GST_OBJECT_LOCK (pad);
5144 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5147 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
5152 /* when one of the probes returns DROPPED, probe_stopped will be
5153 * called and we skip calling the peer getrange function. *buffer should then
5154 * contain a valid buffer */
5155 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
5156 res_buf, offset, size, probe_stopped);
5158 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
5161 gst_object_ref (peer);
5163 GST_OBJECT_UNLOCK (pad);
5165 ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
5167 gst_object_unref (peer);
5169 GST_OBJECT_LOCK (pad);
5171 pad->ABI.abi.last_flowret = ret;
5172 if (pad->priv->using == 0) {
5173 /* pad is not active anymore, trigger idle callbacks */
5174 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
5175 probe_stopped_unref, ret);
5178 if (G_UNLIKELY (ret != GST_FLOW_OK))
5179 goto pull_range_failed;
5182 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
5183 res_buf, offset, size, probe_stopped_unref);
5185 GST_OBJECT_UNLOCK (pad);
5189 GST_TRACER_PAD_PULL_RANGE_POST (pad, *buffer, ret);
5192 /* ERROR recovery here */
5195 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
5196 "pullrange, but pad was flushing");
5197 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
5198 GST_OBJECT_UNLOCK (pad);
5199 ret = GST_FLOW_FLUSHING;
5204 g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
5205 GST_DEBUG_PAD_NAME (pad));
5206 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
5207 GST_OBJECT_UNLOCK (pad);
5208 ret = GST_FLOW_ERROR;
5213 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
5214 gst_flow_get_name (ret));
5215 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
5217 /* the probe filled the buffer and asks us to not forward to the peer
5218 * anymore, we continue with the post probes then */
5219 GST_DEBUG_OBJECT (pad, "handled buffer");
5223 /* no buffer, we are EOS then */
5224 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
5228 pad->ABI.abi.last_flowret = ret;
5229 GST_OBJECT_UNLOCK (pad);
5234 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
5235 "pulling range, but it was not linked");
5236 pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
5237 GST_OBJECT_UNLOCK (pad);
5238 ret = GST_FLOW_NOT_LINKED;
5243 pad->ABI.abi.last_flowret = ret;
5244 GST_OBJECT_UNLOCK (pad);
5245 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
5246 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
5247 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
5250 probe_stopped_unref:
5252 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
5253 "post probe returned %s", gst_flow_get_name (ret));
5255 /* if we drop here, it signals EOS */
5256 if (ret == GST_FLOW_CUSTOM_SUCCESS)
5259 pad->ABI.abi.last_flowret = ret;
5260 GST_OBJECT_UNLOCK (pad);
5262 if (*buffer == NULL)
5263 gst_buffer_unref (res_buf);
5267 GST_TRACER_PAD_PULL_RANGE_POST (pad, NULL, ret);
5271 /* must be called with pad object lock */
5272 static GstFlowReturn
5273 store_sticky_event (GstPad * pad, GstEvent * event)
5278 gboolean res = FALSE;
5280 gboolean insert = TRUE;
5282 type = GST_EVENT_TYPE (event);
5284 /* Store all sticky events except SEGMENT/EOS when we're flushing,
5285 * otherwise they can be dropped and nothing would ever resend them.
5286 * Only do that for activated pads though, everything else is a bug!
5288 if (G_UNLIKELY (GST_PAD_MODE (pad) == GST_PAD_MODE_NONE
5289 || (GST_PAD_IS_FLUSHING (pad) && (type == GST_EVENT_SEGMENT
5290 || type == GST_EVENT_EOS))))
5293 /* Unset the EOS flag when received STREAM_START event, so pad can
5294 * store sticky event and then push it later */
5295 if (type == GST_EVENT_STREAM_START) {
5296 GST_LOG_OBJECT (pad, "Removing pending EOS, StreamGroupDone, TAG events");
5297 remove_event_by_type (pad, GST_EVENT_EOS);
5298 remove_event_by_type (pad, GST_EVENT_STREAM_GROUP_DONE);
5299 remove_event_by_type (pad, GST_EVENT_TAG);
5300 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5303 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5306 if (type & GST_EVENT_TYPE_STICKY_MULTI)
5307 name_id = gst_structure_get_name_id (gst_event_get_structure (event));
5309 events = pad->priv->events;
5312 for (i = 0; i < len; i++) {
5313 PadEvent *ev = &g_array_index (events, PadEvent, i);
5315 if (ev->event == NULL)
5318 if (type == GST_EVENT_TYPE (ev->event)) {
5319 /* matching types, check matching name if needed */
5320 if (name_id && !gst_event_has_name_id (ev->event, name_id))
5324 if ((res = gst_event_replace (&ev->event, event)))
5325 ev->received = FALSE;
5331 if (type < GST_EVENT_TYPE (ev->event) || (type != GST_EVENT_TYPE (ev->event)
5332 && GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS)) {
5333 /* STREAM_START, CAPS and SEGMENT must be delivered in this order. By
5334 * storing the sticky ordered we can check that this is respected. */
5335 if (G_UNLIKELY (GST_EVENT_TYPE (ev->event) <= GST_EVENT_SEGMENT
5336 || GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS))
5338 ":%s:<%s:%s> Sticky event misordering, got '%s' before '%s'",
5339 G_STRFUNC, GST_DEBUG_PAD_NAME (pad),
5340 gst_event_type_get_name (GST_EVENT_TYPE (ev->event)),
5341 gst_event_type_get_name (type));
5347 ev.event = gst_event_ref (event);
5348 ev.received = FALSE;
5349 g_array_insert_val (events, i, ev);
5354 pad->priv->events_cookie++;
5355 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5357 GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
5359 switch (GST_EVENT_TYPE (event)) {
5360 case GST_EVENT_CAPS:
5361 GST_OBJECT_UNLOCK (pad);
5363 GST_DEBUG_OBJECT (pad, "notify caps");
5364 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
5366 GST_OBJECT_LOCK (pad);
5372 if (type == GST_EVENT_EOS) {
5373 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
5374 pad->ABI.abi.last_flowret = GST_FLOW_EOS;
5377 return GST_PAD_IS_FLUSHING (pad) ? GST_FLOW_FLUSHING : GST_FLOW_OK;
5382 GST_DEBUG_OBJECT (pad, "pad is flushing");
5383 return GST_FLOW_FLUSHING;
5387 GST_DEBUG_OBJECT (pad, "pad is EOS");
5388 return GST_FLOW_EOS;
5393 * gst_pad_store_sticky_event:
5395 * @event: (transfer none): a #GstEvent
5397 * Store the sticky @event on @pad
5399 * Returns: #GST_FLOW_OK on success, #GST_FLOW_FLUSHING when the pad
5400 * was flushing or #GST_FLOW_EOS when the pad was EOS.
5405 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event)
5409 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5410 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5412 GST_OBJECT_LOCK (pad);
5413 ret = store_sticky_event (pad, event);
5414 GST_OBJECT_UNLOCK (pad);
5420 sticky_changed (GstPad * pad, PadEvent * ev, gpointer user_data)
5422 PushStickyData *data = user_data;
5424 /* Forward all sticky events before our current one that are pending */
5425 if (ev->event != data->event
5426 && GST_EVENT_TYPE (ev->event) < GST_EVENT_TYPE (data->event))
5427 return push_sticky (pad, ev, data);
5432 /* should be called with pad LOCK */
5433 static GstFlowReturn
5434 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
5435 GstPadProbeType type)
5439 GstEventType event_type;
5440 gint64 old_pad_offset = pad->offset;
5442 /* pass the adjusted event on. We need to do this even if
5443 * there is no peer pad because of the probes. */
5444 event = apply_pad_offset (pad, event, GST_PAD_IS_SINK (pad));
5446 /* Two checks to be made:
5447 * . (un)set the FLUSHING flag for flushing events,
5448 * . handle pad blocking */
5449 event_type = GST_EVENT_TYPE (event);
5450 switch (event_type) {
5451 case GST_EVENT_FLUSH_START:
5452 GST_PAD_SET_FLUSHING (pad);
5454 GST_PAD_BLOCK_BROADCAST (pad);
5455 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5457 case GST_EVENT_FLUSH_STOP:
5458 if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5461 GST_PAD_UNSET_FLUSHING (pad);
5463 /* Remove sticky EOS events */
5464 GST_LOG_OBJECT (pad, "Removing pending EOS events");
5465 remove_event_by_type (pad, GST_EVENT_EOS);
5466 remove_event_by_type (pad, GST_EVENT_STREAM_GROUP_DONE);
5467 remove_event_by_type (pad, GST_EVENT_SEGMENT);
5468 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5469 pad->ABI.abi.last_flowret = GST_FLOW_OK;
5471 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5475 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5478 /* No need to check for EOS here as either the caller (gst_pad_push_event())
5479 * checked already or this is called as part of pushing sticky events,
5480 * in which case we still want to forward the EOS event downstream.
5483 switch (GST_EVENT_TYPE (event)) {
5484 case GST_EVENT_RECONFIGURE:
5485 if (GST_PAD_IS_SINK (pad))
5486 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5491 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
5492 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5493 /* recheck sticky events because the probe might have cause a relink */
5494 if (GST_PAD_HAS_PENDING_EVENTS (pad) && GST_PAD_IS_SRC (pad)
5495 && (GST_EVENT_IS_SERIALIZED (event)
5496 || GST_EVENT_IS_STICKY (event))) {
5497 PushStickyData data = { GST_FLOW_OK, FALSE, event };
5498 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5500 /* Push all sticky events before our current one
5501 * that have changed */
5502 events_foreach (pad, sticky_changed, &data);
5508 /* send probes after modifying the events above */
5509 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5511 /* recheck sticky events because the probe might have cause a relink */
5512 if (GST_PAD_HAS_PENDING_EVENTS (pad) && GST_PAD_IS_SRC (pad)
5513 && (GST_EVENT_IS_SERIALIZED (event)
5514 || GST_EVENT_IS_STICKY (event))) {
5515 PushStickyData data = { GST_FLOW_OK, FALSE, event };
5516 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5518 /* Push all sticky events before our current one
5519 * that have changed */
5520 events_foreach (pad, sticky_changed, &data);
5523 /* the pad offset might've been changed by any of the probes above. It
5524 * would've been taken into account when repushing any of the sticky events
5525 * above but not for our current event here */
5526 if (G_UNLIKELY (old_pad_offset != pad->offset)) {
5528 _apply_pad_offset (pad, event, GST_PAD_IS_SINK (pad),
5529 pad->offset - old_pad_offset);
5532 /* now check the peer pad */
5533 peerpad = GST_PAD_PEER (pad);
5534 if (peerpad == NULL)
5537 gst_object_ref (peerpad);
5539 GST_OBJECT_UNLOCK (pad);
5541 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
5542 event, gst_event_type_get_name (event_type), peerpad);
5544 ret = gst_pad_send_event_unchecked (peerpad, event, type);
5546 /* Note: we gave away ownership of the event at this point but we can still
5547 * print the old pointer */
5548 GST_LOG_OBJECT (pad,
5549 "sent event %p (%s) to peerpad %" GST_PTR_FORMAT ", ret %s", event,
5550 gst_event_type_get_name (event_type), peerpad, gst_flow_get_name (ret));
5552 gst_object_unref (peerpad);
5554 GST_OBJECT_LOCK (pad);
5556 if (pad->priv->using == 0) {
5557 /* pad is not active anymore, trigger idle callbacks */
5558 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
5559 idle_probe_stopped, ret);
5563 /* ERROR handling */
5566 GST_DEBUG_OBJECT (pad, "We're flushing");
5567 gst_event_unref (event);
5568 return GST_FLOW_FLUSHING;
5572 GST_DEBUG_OBJECT (pad, "flush-stop on inactive pad");
5573 gst_event_unref (event);
5574 return GST_FLOW_FLUSHING;
5578 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5579 if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
5580 gst_event_unref (event);
5583 case GST_FLOW_CUSTOM_SUCCESS_1:
5584 GST_DEBUG_OBJECT (pad, "handled event");
5586 case GST_FLOW_CUSTOM_SUCCESS:
5587 GST_DEBUG_OBJECT (pad, "dropped event");
5590 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5597 GST_DEBUG_OBJECT (pad, "Dropping event %s because pad is not linked",
5598 gst_event_type_get_name (GST_EVENT_TYPE (event)));
5599 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5600 gst_event_unref (event);
5602 /* unlinked pads should not influence latency configuration */
5603 if (event_type == GST_EVENT_LATENCY)
5606 return GST_FLOW_NOT_LINKED;
5610 GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
5616 * gst_pad_push_event:
5617 * @pad: a #GstPad to push the event to.
5618 * @event: (transfer full): the #GstEvent to send to the pad.
5620 * Sends the event to the peer of the given pad. This function is
5621 * mainly used by elements to send events to their peer
5624 * This function takes ownership of the provided event so you should
5625 * gst_event_ref() it if you want to reuse the event after this call.
5627 * Returns: %TRUE if the event was handled.
5632 gst_pad_push_event (GstPad * pad, GstEvent * event)
5634 gboolean res = FALSE;
5635 GstPadProbeType type;
5636 gboolean sticky, serialized;
5638 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5639 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5641 GST_TRACER_PAD_PUSH_EVENT_PRE (pad, event);
5643 if (GST_PAD_IS_SRC (pad)) {
5644 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5645 goto wrong_direction;
5646 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5647 } else if (GST_PAD_IS_SINK (pad)) {
5648 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5649 goto wrong_direction;
5650 /* events pushed on sinkpad never are sticky */
5651 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5653 goto unknown_direction;
5655 GST_OBJECT_LOCK (pad);
5656 sticky = GST_EVENT_IS_STICKY (event);
5657 serialized = GST_EVENT_IS_SERIALIZED (event);
5660 /* srcpad sticky events are stored immediately, the received flag is set
5661 * to FALSE and will be set to TRUE when we can successfully push the
5662 * event to the peer pad */
5663 switch (store_sticky_event (pad, event)) {
5664 case GST_FLOW_FLUSHING:
5672 if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
5673 /* all serialized or sticky events on the srcpad trigger push of
5675 res = (check_sticky (pad, event) == GST_FLOW_OK);
5680 /* other events are pushed right away */
5681 ret = gst_pad_push_event_unchecked (pad, event, type);
5682 /* dropped events by a probe are not an error */
5683 res = (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_SUCCESS
5684 || ret == GST_FLOW_CUSTOM_SUCCESS_1);
5686 /* Errors in sticky event pushing are no problem and ignored here
5687 * as they will cause more meaningful errors during data flow.
5688 * For EOS events, that are not followed by data flow, we still
5689 * return FALSE here though.
5691 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
5693 gst_event_unref (event);
5695 GST_OBJECT_UNLOCK (pad);
5697 GST_TRACER_PAD_PUSH_EVENT_POST (pad, res);
5700 /* ERROR handling */
5703 g_warning ("pad %s:%s pushing %s event in wrong direction",
5704 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5705 gst_event_unref (event);
5710 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5711 gst_event_unref (event);
5716 GST_DEBUG_OBJECT (pad, "We're flushing");
5717 GST_OBJECT_UNLOCK (pad);
5718 gst_event_unref (event);
5723 GST_DEBUG_OBJECT (pad, "We're EOS");
5724 GST_OBJECT_UNLOCK (pad);
5725 gst_event_unref (event);
5729 GST_TRACER_PAD_PUSH_EVENT_POST (pad, FALSE);
5733 /* Check if we can call the event function with the given event */
5734 static GstFlowReturn
5735 pre_eventfunc_check (GstPad * pad, GstEvent * event)
5739 switch (GST_EVENT_TYPE (event)) {
5740 case GST_EVENT_CAPS:
5742 /* backwards compatibility mode for caps */
5743 gst_event_parse_caps (event, &caps);
5745 if (!gst_pad_query_accept_caps (pad, caps))
5757 GST_CAT_WARNING_OBJECT (GST_CAT_CAPS, pad,
5758 "caps %" GST_PTR_FORMAT " not accepted", caps);
5759 return GST_FLOW_NOT_NEGOTIATED;
5763 static GstFlowReturn
5764 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
5765 GstPadProbeType type)
5768 GstEventType event_type;
5769 gboolean serialized, need_unlock = FALSE, sticky;
5770 GstPadEventFunction eventfunc;
5771 GstPadEventFullFunction eventfullfunc = NULL;
5773 gint64 old_pad_offset;
5775 GST_OBJECT_LOCK (pad);
5777 old_pad_offset = pad->offset;
5778 event = apply_pad_offset (pad, event, GST_PAD_IS_SRC (pad));
5780 if (GST_PAD_IS_SINK (pad))
5781 serialized = GST_EVENT_IS_SERIALIZED (event);
5784 sticky = GST_EVENT_IS_STICKY (event);
5785 event_type = GST_EVENT_TYPE (event);
5786 switch (event_type) {
5787 case GST_EVENT_FLUSH_START:
5788 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5789 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5791 /* can't even accept a flush begin event when flushing */
5792 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5795 GST_PAD_SET_FLUSHING (pad);
5796 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5797 GST_PAD_BLOCK_BROADCAST (pad);
5798 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5800 case GST_EVENT_FLUSH_STOP:
5801 /* we can't accept flush-stop on inactive pads else the flushing flag
5802 * would be cleared and it would look like the pad can accept data.
5803 * Also, some elements restart a streaming thread in flush-stop which we
5804 * can't allow on inactive pads */
5805 if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5808 GST_PAD_UNSET_FLUSHING (pad);
5809 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5810 /* Remove pending EOS events */
5811 GST_LOG_OBJECT (pad, "Removing pending EOS and SEGMENT events");
5812 remove_event_by_type (pad, GST_EVENT_EOS);
5813 remove_event_by_type (pad, GST_EVENT_STREAM_GROUP_DONE);
5814 remove_event_by_type (pad, GST_EVENT_SEGMENT);
5815 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5816 pad->ABI.abi.last_flowret = GST_FLOW_OK;
5818 GST_OBJECT_UNLOCK (pad);
5819 /* grab stream lock */
5820 GST_PAD_STREAM_LOCK (pad);
5822 GST_OBJECT_LOCK (pad);
5823 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5826 case GST_EVENT_RECONFIGURE:
5827 if (GST_PAD_IS_SRC (pad))
5828 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5830 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5831 "have event type %" GST_PTR_FORMAT, event);
5833 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5836 switch (event_type) {
5837 case GST_EVENT_STREAM_START:
5838 /* Remove sticky EOS events */
5839 GST_LOG_OBJECT (pad, "Removing pending EOS events");
5840 remove_event_by_type (pad, GST_EVENT_EOS);
5841 remove_event_by_type (pad, GST_EVENT_STREAM_GROUP_DONE);
5842 remove_event_by_type (pad, GST_EVENT_TAG);
5843 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5850 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5853 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5854 GST_OBJECT_UNLOCK (pad);
5855 GST_PAD_STREAM_LOCK (pad);
5857 GST_OBJECT_LOCK (pad);
5858 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5861 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5867 /* now do the probe */
5869 type | GST_PAD_PROBE_TYPE_PUSH |
5870 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5872 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5874 /* the pad offset might've been changed by any of the probes above. It
5875 * would've been taken into account when repushing any of the sticky events
5876 * above but not for our current event here */
5877 if (G_UNLIKELY (old_pad_offset != pad->offset)) {
5879 _apply_pad_offset (pad, event, GST_PAD_IS_SRC (pad),
5880 pad->offset - old_pad_offset);
5883 eventfullfunc = GST_PAD_EVENTFULLFUNC (pad);
5884 eventfunc = GST_PAD_EVENTFUNC (pad);
5885 if (G_UNLIKELY (eventfunc == NULL && eventfullfunc == NULL))
5888 ACQUIRE_PARENT (pad, parent, no_parent);
5889 GST_OBJECT_UNLOCK (pad);
5891 ret = pre_eventfunc_check (pad, event);
5892 if (G_UNLIKELY (ret != GST_FLOW_OK))
5893 goto precheck_failed;
5896 gst_event_ref (event);
5898 if (eventfullfunc) {
5899 ret = eventfullfunc (pad, parent, event);
5900 } else if (eventfunc (pad, parent, event)) {
5903 /* something went wrong */
5904 switch (event_type) {
5905 case GST_EVENT_CAPS:
5906 ret = GST_FLOW_NOT_NEGOTIATED;
5909 ret = GST_FLOW_ERROR;
5913 RELEASE_PARENT (parent);
5915 GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
5918 if (ret == GST_FLOW_OK) {
5919 GST_OBJECT_LOCK (pad);
5920 /* after the event function accepted the event, we can store the sticky
5921 * event on the pad */
5922 switch (store_sticky_event (pad, event)) {
5923 case GST_FLOW_FLUSHING:
5930 GST_OBJECT_UNLOCK (pad);
5932 gst_event_unref (event);
5936 GST_PAD_STREAM_UNLOCK (pad);
5940 /* ERROR handling */
5943 GST_OBJECT_UNLOCK (pad);
5945 GST_PAD_STREAM_UNLOCK (pad);
5946 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5947 "Received event on flushing pad. Discarding");
5948 gst_event_unref (event);
5949 return GST_FLOW_FLUSHING;
5953 GST_OBJECT_UNLOCK (pad);
5955 GST_PAD_STREAM_UNLOCK (pad);
5956 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5957 "Received flush-stop on inactive pad. Discarding");
5958 gst_event_unref (event);
5959 return GST_FLOW_FLUSHING;
5963 GST_OBJECT_UNLOCK (pad);
5965 GST_PAD_STREAM_UNLOCK (pad);
5966 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5967 "Received event on EOS pad. Discarding");
5968 gst_event_unref (event);
5969 return GST_FLOW_EOS;
5973 GST_OBJECT_UNLOCK (pad);
5975 GST_PAD_STREAM_UNLOCK (pad);
5976 /* Only unref if unhandled */
5977 if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
5978 gst_event_unref (event);
5981 case GST_FLOW_CUSTOM_SUCCESS_1:
5982 case GST_FLOW_CUSTOM_SUCCESS:
5983 GST_DEBUG_OBJECT (pad, "dropped or handled event");
5987 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5994 g_warning ("pad %s:%s has no event handler, file a bug.",
5995 GST_DEBUG_PAD_NAME (pad));
5996 GST_OBJECT_UNLOCK (pad);
5998 GST_PAD_STREAM_UNLOCK (pad);
5999 gst_event_unref (event);
6000 return GST_FLOW_NOT_SUPPORTED;
6004 GST_DEBUG_OBJECT (pad, "no parent");
6005 GST_OBJECT_UNLOCK (pad);
6007 GST_PAD_STREAM_UNLOCK (pad);
6008 gst_event_unref (event);
6009 return GST_FLOW_FLUSHING;
6013 GST_DEBUG_OBJECT (pad, "pre event check failed");
6014 RELEASE_PARENT (parent);
6016 GST_PAD_STREAM_UNLOCK (pad);
6017 gst_event_unref (event);
6023 * gst_pad_send_event:
6024 * @pad: a #GstPad to send the event to.
6025 * @event: (transfer full): the #GstEvent to send to the pad.
6027 * Sends the event to the pad. This function can be used
6028 * by applications to send events in the pipeline.
6030 * If @pad is a source pad, @event should be an upstream event. If @pad is a
6031 * sink pad, @event should be a downstream event. For example, you would not
6032 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
6033 * Furthermore, some downstream events have to be serialized with data flow,
6034 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
6035 * the event needs to be serialized with data flow, this function will take the
6036 * pad's stream lock while calling its event function.
6038 * To find out whether an event type is upstream, downstream, or downstream and
6039 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
6040 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
6041 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
6042 * plugin doesn't need to bother itself with this information; the core handles
6043 * all necessary locks and checks.
6045 * This function takes ownership of the provided event so you should
6046 * gst_event_ref() it if you want to reuse the event after this call.
6048 * Returns: %TRUE if the event was handled.
6051 gst_pad_send_event (GstPad * pad, GstEvent * event)
6054 GstPadProbeType type;
6056 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
6057 g_return_val_if_fail (event != NULL, FALSE);
6059 if (GST_PAD_IS_SINK (pad)) {
6060 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
6061 goto wrong_direction;
6062 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
6063 } else if (GST_PAD_IS_SRC (pad)) {
6064 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
6065 goto wrong_direction;
6066 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
6068 goto unknown_direction;
6070 if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
6077 /* ERROR handling */
6080 g_warning ("pad %s:%s sending %s event in wrong direction",
6081 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
6082 gst_event_unref (event);
6087 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
6088 gst_event_unref (event);
6094 * gst_pad_set_element_private:
6095 * @pad: the #GstPad to set the private data of.
6096 * @priv: The private data to attach to the pad.
6098 * Set the given private data gpointer on the pad.
6099 * This function can only be used by the element that owns the pad.
6100 * No locking is performed in this function.
6103 gst_pad_set_element_private (GstPad * pad, gpointer priv)
6105 pad->element_private = priv;
6109 * gst_pad_get_element_private:
6110 * @pad: the #GstPad to get the private data of.
6112 * Gets the private data of a pad.
6113 * No locking is performed in this function.
6115 * Returns: (transfer none) (nullable): a #gpointer to the private data.
6118 gst_pad_get_element_private (GstPad * pad)
6120 return pad->element_private;
6124 * gst_pad_get_sticky_event:
6125 * @pad: the #GstPad to get the event from.
6126 * @event_type: the #GstEventType that should be retrieved.
6127 * @idx: the index of the event
6129 * Returns a new reference of the sticky event of type @event_type
6132 * Returns: (transfer full) (nullable): a #GstEvent of type
6133 * @event_type or %NULL when no event of @event_type was on
6134 * @pad. Unref after usage.
6137 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
6139 GstEvent *event = NULL;
6142 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
6143 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
6145 GST_OBJECT_LOCK (pad);
6146 ev = find_event_by_type (pad, event_type, idx);
6147 if (ev && (event = ev->event))
6148 gst_event_ref (event);
6149 GST_OBJECT_UNLOCK (pad);
6156 GstPadStickyEventsForeachFunction func;
6161 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
6163 ForeachDispatch *data = user_data;
6164 gboolean ret = TRUE;
6167 GST_OBJECT_UNLOCK (pad);
6169 ret = data->func (pad, &ev->event, data->user_data);
6171 GST_OBJECT_LOCK (pad);
6178 * gst_pad_sticky_events_foreach:
6179 * @pad: the #GstPad that should be used for iteration.
6180 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
6181 * should be called for every event.
6182 * @user_data: (closure): the optional user data.
6184 * Iterates all sticky events on @pad and calls @foreach_func for every
6185 * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
6188 gst_pad_sticky_events_foreach (GstPad * pad,
6189 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
6191 ForeachDispatch data;
6193 g_return_if_fail (GST_IS_PAD (pad));
6194 g_return_if_fail (foreach_func != NULL);
6196 data.func = foreach_func;
6197 data.user_data = user_data;
6199 GST_OBJECT_LOCK (pad);
6200 events_foreach (pad, foreach_dispatch_function, &data);
6201 GST_OBJECT_UNLOCK (pad);
6205 do_stream_status (GstPad * pad, GstStreamStatusType type,
6206 GThread * thread, GstTask * task)
6210 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
6212 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
6213 if (GST_IS_ELEMENT (parent)) {
6214 GstMessage *message;
6215 GValue value = { 0 };
6217 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
6220 /* create a good task name (we can directly grab the parent and pad
6221 * names since they both exist at this point, and changing the name of
6222 * parent and pad when a pad is activating is a big no-no). */
6223 tname = g_strdup_printf ("%s:%s", GST_DEBUG_PAD_NAME (pad));
6225 gst_object_set_name (GST_OBJECT_CAST (task), tname);
6229 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
6232 g_value_init (&value, GST_TYPE_TASK);
6233 g_value_set_object (&value, task);
6234 gst_message_set_stream_status_object (message, &value);
6235 g_value_unset (&value);
6237 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
6238 gst_element_post_message (parent, message);
6240 gst_object_unref (parent);
6245 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
6247 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
6252 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
6254 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
6259 * gst_pad_start_task:
6260 * @pad: the #GstPad to start the task of
6261 * @func: the task function to call
6262 * @user_data: user data passed to the task function
6263 * @notify: called when @user_data is no longer referenced
6265 * Starts a task that repeatedly calls @func with @user_data. This function
6266 * is mostly used in pad activation functions to start the dataflow.
6267 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
6268 * before @func is called.
6270 * Returns: a %TRUE if the task could be started.
6273 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
6274 GDestroyNotify notify)
6279 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
6280 g_return_val_if_fail (func != NULL, FALSE);
6282 GST_DEBUG_OBJECT (pad, "start task");
6284 GST_OBJECT_LOCK (pad);
6285 task = GST_PAD_TASK (pad);
6287 task = gst_task_new (func, user_data, notify);
6288 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
6289 gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
6290 gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
6291 GST_INFO_OBJECT (pad, "created task %p", task);
6292 GST_PAD_TASK (pad) = task;
6293 gst_object_ref (task);
6294 /* release lock to post the message */
6295 GST_OBJECT_UNLOCK (pad);
6297 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
6299 gst_object_unref (task);
6301 GST_OBJECT_LOCK (pad);
6302 /* nobody else is supposed to have changed the pad now */
6303 if (GST_PAD_TASK (pad) != task)
6304 goto concurrent_stop;
6306 res = gst_task_set_state (task, GST_TASK_STARTED);
6307 GST_OBJECT_UNLOCK (pad);
6314 GST_OBJECT_UNLOCK (pad);
6320 * gst_pad_pause_task:
6321 * @pad: the #GstPad to pause the task of
6323 * Pause the task of @pad. This function will also wait until the
6324 * function executed by the task is finished if this function is not
6325 * called from the task function.
6327 * Returns: a %TRUE if the task could be paused or %FALSE when the pad
6331 gst_pad_pause_task (GstPad * pad)
6336 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
6338 GST_DEBUG_OBJECT (pad, "pause task");
6340 GST_OBJECT_LOCK (pad);
6341 task = GST_PAD_TASK (pad);
6344 res = gst_task_set_state (task, GST_TASK_PAUSED);
6345 /* unblock activation waits if any */
6346 pad->priv->in_activation = FALSE;
6347 g_cond_broadcast (&pad->priv->activation_cond);
6348 GST_OBJECT_UNLOCK (pad);
6350 /* wait for task function to finish, this lock is recursive so it does nothing
6351 * when the pause is called from the task itself */
6352 GST_PAD_STREAM_LOCK (pad);
6353 GST_PAD_STREAM_UNLOCK (pad);
6359 GST_DEBUG_OBJECT (pad, "pad has no task");
6360 GST_OBJECT_UNLOCK (pad);
6366 * gst_pad_get_task_state:
6367 * @pad: the #GstPad to get task state from
6369 * Get @pad task state. If no task is currently
6370 * set, #GST_TASK_STOPPED is returned.
6372 * Returns: The current state of @pad's task.
6377 gst_pad_get_task_state (GstPad * pad)
6382 g_return_val_if_fail (GST_IS_PAD (pad), GST_TASK_STOPPED);
6384 GST_OBJECT_LOCK (pad);
6385 task = GST_PAD_TASK (pad);
6388 res = gst_task_get_state (task);
6389 GST_OBJECT_UNLOCK (pad);
6395 GST_DEBUG_OBJECT (pad, "pad has no task");
6396 GST_OBJECT_UNLOCK (pad);
6397 return GST_TASK_STOPPED;
6402 * gst_pad_stop_task:
6403 * @pad: the #GstPad to stop the task of
6405 * Stop the task of @pad. This function will also make sure that the
6406 * function executed by the task will effectively stop if not called
6407 * from the GstTaskFunction.
6409 * This function will deadlock if called from the GstTaskFunction of
6410 * the task. Use gst_task_pause() instead.
6412 * Regardless of whether the pad has a task, the stream lock is acquired and
6413 * released so as to ensure that streaming through this pad has finished.
6415 * Returns: a %TRUE if the task could be stopped or %FALSE on error.
6418 gst_pad_stop_task (GstPad * pad)
6423 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
6425 GST_DEBUG_OBJECT (pad, "stop task");
6427 GST_OBJECT_LOCK (pad);
6428 task = GST_PAD_TASK (pad);
6431 GST_PAD_TASK (pad) = NULL;
6432 res = gst_task_set_state (task, GST_TASK_STOPPED);
6433 /* unblock activation waits if any */
6434 pad->priv->in_activation = FALSE;
6435 g_cond_broadcast (&pad->priv->activation_cond);
6436 GST_OBJECT_UNLOCK (pad);
6438 GST_PAD_STREAM_LOCK (pad);
6439 GST_PAD_STREAM_UNLOCK (pad);
6441 if (!gst_task_join (task))
6444 gst_object_unref (task);
6450 GST_DEBUG_OBJECT (pad, "no task");
6451 GST_OBJECT_UNLOCK (pad);
6453 GST_PAD_STREAM_LOCK (pad);
6454 GST_PAD_STREAM_UNLOCK (pad);
6456 /* this is not an error */
6461 /* this is bad, possibly the application tried to join the task from
6462 * the task's thread. We install the task again so that it will be stopped
6463 * again from the right thread next time hopefully. */
6464 GST_OBJECT_LOCK (pad);
6465 GST_DEBUG_OBJECT (pad, "join failed");
6466 /* we can only install this task if there was no other task */
6467 if (GST_PAD_TASK (pad) == NULL)
6468 GST_PAD_TASK (pad) = task;
6469 GST_OBJECT_UNLOCK (pad);
6476 * gst_pad_probe_info_get_event:
6477 * @info: a #GstPadProbeInfo
6479 * Returns: (transfer none) (nullable): The #GstEvent from the probe
6483 gst_pad_probe_info_get_event (GstPadProbeInfo * info)
6485 g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
6486 GST_PAD_PROBE_TYPE_EVENT_UPSTREAM), NULL);
6488 return GST_PAD_PROBE_INFO_EVENT (info);
6493 * gst_pad_probe_info_get_query:
6494 * @info: a #GstPadProbeInfo
6496 * Returns: (transfer none) (nullable): The #GstQuery from the probe
6500 gst_pad_probe_info_get_query (GstPadProbeInfo * info)
6502 g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM |
6503 GST_PAD_PROBE_TYPE_QUERY_UPSTREAM), NULL);
6505 return GST_PAD_PROBE_INFO_QUERY (info);
6509 * gst_pad_probe_info_get_buffer:
6510 * @info: a #GstPadProbeInfo
6512 * Returns: (transfer none) (nullable): The #GstBuffer from the probe
6516 gst_pad_probe_info_get_buffer (GstPadProbeInfo * info)
6518 g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER, NULL);
6520 return GST_PAD_PROBE_INFO_BUFFER (info);
6524 * gst_pad_probe_info_get_buffer_list:
6525 * @info: a #GstPadProbeInfo
6527 * Returns: (transfer none) (nullable): The #GstBufferList from the probe
6531 gst_pad_probe_info_get_buffer_list (GstPadProbeInfo * info)
6533 g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST, NULL);
6535 return GST_PAD_PROBE_INFO_BUFFER_LIST (info);
6539 * gst_pad_get_last_flow_return:
6542 * Gets the #GstFlowReturn return from the last data passed by this pad.
6547 gst_pad_get_last_flow_return (GstPad * pad)
6551 GST_OBJECT_LOCK (pad);
6552 ret = GST_PAD_LAST_FLOW_RETURN (pad);
6553 GST_OBJECT_UNLOCK (pad);