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.
24 * @short_description: Object contained by elements that allows links to
26 * @see_also: #GstPadTemplate, #GstElement, #GstEvent, #GstQuery, #GstBuffer
28 * A #GstElement is linked to other elements via "pads", which are extremely
29 * light-weight generic link points.
31 * Pads have a #GstPadDirection, source pads produce data, sink pads consume
34 * Pads are typically created from a #GstPadTemplate with
35 * gst_pad_new_from_template() and are then added to a #GstElement. This usually
36 * happens when the element is created but it can also happen dynamically based
37 * on the data that the element is processing or based on the pads that the
38 * application requests.
40 * Pads without pad templates can be created with gst_pad_new(),
41 * which takes a direction and a name as an argument. If the name is %NULL,
42 * then a guaranteed unique name will be assigned to it.
44 * A #GstElement creating a pad will typically use the various
45 * gst_pad_set_*_function() calls to register callbacks for events, queries or
46 * dataflow on the pads.
48 * gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
50 * After two pads are retrieved from an element by gst_element_get_static_pad(),
51 * the pads can be linked with gst_pad_link(). (For quick links,
52 * you can also use gst_element_link(), which will make the obvious
53 * link for you if it's straightforward.). Pads can be unlinked again with
54 * gst_pad_unlink(). gst_pad_get_peer() can be used to check what the pad is
57 * Before dataflow is possible on the pads, they need to be activated with
58 * gst_pad_set_active().
60 * gst_pad_query() and gst_pad_peer_query() can be used to query various
61 * properties of the pad and the stream.
63 * To send a #GstEvent on a pad, use gst_pad_send_event() and
64 * gst_pad_push_event(). Some events will be sticky on the pad, meaning that
65 * after they pass on the pad they can be queried later with
66 * gst_pad_get_sticky_event() and gst_pad_sticky_events_foreach().
67 * gst_pad_get_current_caps() and gst_pad_has_current_caps() are convenience
68 * functions to query the current sticky CAPS event on a pad.
70 * GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
71 * or pull in a buffer.
73 * The dataflow, events and queries that happen on a pad can be monitored with
74 * probes that can be installed with gst_pad_add_probe(). gst_pad_is_blocked()
75 * can be used to check if a block probe is installed on the pad.
76 * gst_pad_is_blocking() checks if the blocking probe is currently blocking the
77 * pad. gst_pad_remove_probe() is used to remove a previously installed probe
78 * and unblock blocking probes if any.
80 * Pad have an offset that can be retrieved with gst_pad_get_offset(). This
81 * offset will be applied to the running_time of all data passing over the pad.
82 * gst_pad_set_offset() can be used to change the offset.
84 * Convenience functions exist to start, pause and stop the task on a pad with
85 * gst_pad_start_task(), gst_pad_pause_task() and gst_pad_stop_task()
89 #include "gst_private.h"
92 #include "gstpadtemplate.h"
93 #include "gstenumtypes.h"
97 #include "gsttracer.h"
99 #include "glib-compat-private.h"
101 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
102 #define GST_CAT_DEFAULT GST_CAT_PADS
104 /* Pad signals and args */
123 #define GST_PAD_GET_PRIVATE(obj) \
124 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
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;
145 /* counter of how many idle probes are running directly from the add_probe
146 * call. Used to block any data flowing in the pad while the idle callback
147 * Doesn't finish its work */
157 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
158 #define GST_PAD_IS_RUNNING_IDLE_PROBE(p) \
159 (((GstPad *)(p))->priv->idle_running > 0)
164 GstPadProbeInfo *info;
172 static void gst_pad_dispose (GObject * object);
173 static void gst_pad_finalize (GObject * object);
174 static void gst_pad_set_property (GObject * object, guint prop_id,
175 const GValue * value, GParamSpec * pspec);
176 static void gst_pad_get_property (GObject * object, guint prop_id,
177 GValue * value, GParamSpec * pspec);
179 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
180 static gboolean gst_pad_activate_default (GstPad * pad, GstObject * parent);
181 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
182 GstObject * parent, GstBufferList * list);
184 static GstFlowReturn gst_pad_send_event_unchecked (GstPad * pad,
185 GstEvent * event, GstPadProbeType type);
186 static GstFlowReturn gst_pad_push_event_unchecked (GstPad * pad,
187 GstEvent * event, GstPadProbeType type);
189 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
191 static GParamSpec *pspec_caps = NULL;
193 /* quarks for probe signals */
194 static GQuark buffer_quark;
195 static GQuark buffer_list_quark;
196 static GQuark event_quark;
205 static GstFlowQuarks flow_quarks[] = {
206 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
207 {GST_FLOW_OK, "ok", 0},
208 {GST_FLOW_NOT_LINKED, "not-linked", 0},
209 {GST_FLOW_FLUSHING, "flushing", 0},
210 {GST_FLOW_EOS, "eos", 0},
211 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
212 {GST_FLOW_ERROR, "error", 0},
213 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
214 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
219 * @ret: a #GstFlowReturn to get the name of.
221 * Gets a string representing the given flow return.
223 * Returns: a static string with the name of the flow return.
226 gst_flow_get_name (GstFlowReturn ret)
230 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
232 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
233 if (ret == flow_quarks[i].ret)
234 return flow_quarks[i].name;
241 * @ret: a #GstFlowReturn to get the quark of.
243 * Get the unique quark for the given GstFlowReturn.
245 * Returns: the quark associated with the flow return or 0 if an
246 * invalid return was specified.
249 gst_flow_to_quark (GstFlowReturn ret)
253 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
255 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
256 if (ret == flow_quarks[i].ret)
257 return flow_quarks[i].quark;
263 * gst_pad_link_get_name:
264 * @ret: a #GstPadLinkReturn to get the name of.
266 * Gets a string representing the given pad-link return.
268 * Returns: a static string with the name of the pad-link return.
273 gst_pad_link_get_name (GstPadLinkReturn ret)
276 case GST_PAD_LINK_OK:
278 case GST_PAD_LINK_WRONG_HIERARCHY:
279 return "wrong hierarchy";
280 case GST_PAD_LINK_WAS_LINKED:
282 case GST_PAD_LINK_WRONG_DIRECTION:
283 return "wrong direction";
284 case GST_PAD_LINK_NOFORMAT:
285 return "no common format";
286 case GST_PAD_LINK_NOSCHED:
287 return "incompatible scheduling";
288 case GST_PAD_LINK_REFUSED:
291 g_return_val_if_reached ("unknown");
298 buffer_quark = g_quark_from_static_string ("buffer"); \
299 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
300 event_quark = g_quark_from_static_string ("event"); \
302 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
303 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
306 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
307 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
310 #define gst_pad_parent_class parent_class
311 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
314 gst_pad_class_init (GstPadClass * klass)
316 GObjectClass *gobject_class;
317 GstObjectClass *gstobject_class;
319 gobject_class = G_OBJECT_CLASS (klass);
320 gstobject_class = GST_OBJECT_CLASS (klass);
322 g_type_class_add_private (klass, sizeof (GstPadPrivate));
324 gobject_class->dispose = gst_pad_dispose;
325 gobject_class->finalize = gst_pad_finalize;
326 gobject_class->set_property = gst_pad_set_property;
327 gobject_class->get_property = gst_pad_get_property;
331 * @pad: the pad that emitted the signal
332 * @peer: the peer pad that has been connected
334 * Signals that a pad has been linked to the peer pad.
336 gst_pad_signals[PAD_LINKED] =
337 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
338 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
339 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
342 * @pad: the pad that emitted the signal
343 * @peer: the peer pad that has been disconnected
345 * Signals that a pad has been unlinked from the peer pad.
347 gst_pad_signals[PAD_UNLINKED] =
348 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
349 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
350 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
352 pspec_caps = g_param_spec_boxed ("caps", "Caps",
353 "The capabilities of the pad", GST_TYPE_CAPS,
354 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
355 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
357 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
358 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
359 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
360 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
362 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
363 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
364 g_param_spec_object ("template", "Template",
365 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
366 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 * The offset that will be applied to the running time of the pad.
375 g_object_class_install_property (gobject_class, PAD_PROP_OFFSET,
376 g_param_spec_int64 ("offset", "Offset",
377 "The running time offset of the pad", 0, G_MAXINT64, 0,
378 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
380 gstobject_class->path_string_separator = ".";
382 /* Register common function pointer descriptions */
383 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
384 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
385 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
386 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
387 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
391 gst_pad_init (GstPad * pad)
393 pad->priv = GST_PAD_GET_PRIVATE (pad);
395 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
397 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
398 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
399 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
400 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
401 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
403 GST_PAD_SET_FLUSHING (pad);
405 g_rec_mutex_init (&pad->stream_rec_lock);
407 g_cond_init (&pad->block_cond);
409 g_hook_list_init (&pad->probes, sizeof (GstProbe));
411 pad->priv->events = g_array_sized_new (FALSE, TRUE, sizeof (PadEvent), 16);
412 pad->priv->events_cookie = 0;
413 pad->priv->last_cookie = -1;
414 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
417 /* called when setting the pad inactive. It removes all sticky events from
418 * the pad. must be called with object lock */
420 remove_events (GstPad * pad)
424 gboolean notify = FALSE;
426 events = pad->priv->events;
429 for (i = 0; i < len; i++) {
430 PadEvent *ev = &g_array_index (events, PadEvent, i);
431 GstEvent *event = ev->event;
435 if (event && GST_EVENT_TYPE (event) == GST_EVENT_CAPS)
438 gst_event_unref (event);
441 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
442 g_array_set_size (events, 0);
443 pad->priv->events_cookie++;
446 GST_OBJECT_UNLOCK (pad);
448 GST_DEBUG_OBJECT (pad, "notify caps");
449 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
451 GST_OBJECT_LOCK (pad);
455 /* should be called with object lock */
457 find_event_by_type (GstPad * pad, GstEventType type, guint idx)
463 events = pad->priv->events;
466 for (i = 0; i < len; i++) {
467 ev = &g_array_index (events, PadEvent, i);
468 if (ev->event == NULL)
471 if (GST_EVENT_TYPE (ev->event) == type) {
475 } else if (GST_EVENT_TYPE (ev->event) > type) {
484 /* should be called with OBJECT lock */
486 find_event (GstPad * pad, GstEvent * event)
492 events = pad->priv->events;
495 for (i = 0; i < len; i++) {
496 ev = &g_array_index (events, PadEvent, i);
497 if (event == ev->event)
499 else if (GST_EVENT_TYPE (ev->event) > GST_EVENT_TYPE (event))
507 /* should be called with OBJECT lock */
509 remove_event_by_type (GstPad * pad, GstEventType type)
515 events = pad->priv->events;
520 ev = &g_array_index (events, PadEvent, i);
521 if (ev->event == NULL)
524 if (GST_EVENT_TYPE (ev->event) > type)
526 else if (GST_EVENT_TYPE (ev->event) != type)
529 gst_event_unref (ev->event);
530 g_array_remove_index (events, i);
532 pad->priv->events_cookie++;
540 /* check all events on srcpad against those on sinkpad. All events that are not
541 * on sinkpad are marked as received=%FALSE and the PENDING_EVENTS is set on the
542 * srcpad so that the events will be sent next time */
543 /* should be called with srcpad and sinkpad LOCKS */
545 schedule_events (GstPad * srcpad, GstPad * sinkpad)
550 gboolean pending = FALSE;
552 events = srcpad->priv->events;
555 for (i = 0; i < len; i++) {
556 ev = &g_array_index (events, PadEvent, i);
557 if (ev->event == NULL)
560 if (sinkpad == NULL || !find_event (sinkpad, ev->event)) {
561 ev->received = FALSE;
566 GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PENDING_EVENTS);
569 typedef gboolean (*PadEventFunction) (GstPad * pad, PadEvent * ev,
572 /* should be called with pad LOCK */
574 events_foreach (GstPad * pad, PadEventFunction func, gpointer user_data)
581 events = pad->priv->events;
584 cookie = pad->priv->events_cookie;
588 PadEvent *ev, ev_ret;
590 ev = &g_array_index (events, PadEvent, i);
591 if (G_UNLIKELY (ev->event == NULL))
594 /* take aditional ref, func might release the lock */
595 ev_ret.event = gst_event_ref (ev->event);
596 ev_ret.received = ev->received;
598 ret = func (pad, &ev_ret, user_data);
600 /* recheck the cookie, lock might have been released and the list could have
602 if (G_UNLIKELY (cookie != pad->priv->events_cookie)) {
603 if (G_LIKELY (ev_ret.event))
604 gst_event_unref (ev_ret.event);
608 /* store the received state */
609 ev->received = ev_ret.received;
611 /* if the event changed, we need to do something */
612 if (G_UNLIKELY (ev->event != ev_ret.event)) {
613 if (G_UNLIKELY (ev_ret.event == NULL)) {
614 /* function unreffed and set the event to NULL, remove it */
615 gst_event_unref (ev->event);
616 g_array_remove_index (events, i);
618 cookie = ++pad->priv->events_cookie;
621 /* function gave a new event for us */
622 gst_event_take (&ev->event, ev_ret.event);
625 /* just unref, nothing changed */
626 gst_event_unref (ev_ret.event);
635 /* should be called with LOCK */
637 _apply_pad_offset (GstPad * pad, GstEvent * event, gboolean upstream)
641 GST_DEBUG_OBJECT (pad, "apply pad offset %" GST_TIME_FORMAT,
642 GST_TIME_ARGS (pad->offset));
644 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
647 g_assert (!upstream);
649 /* copy segment values */
650 gst_event_copy_segment (event, &segment);
651 gst_event_unref (event);
653 gst_segment_offset_running_time (&segment, segment.format, pad->offset);
654 event = gst_event_new_segment (&segment);
657 event = gst_event_make_writable (event);
658 offset = gst_event_get_running_time_offset (event);
660 offset -= pad->offset;
662 offset += pad->offset;
663 gst_event_set_running_time_offset (event, offset);
668 static inline GstEvent *
669 apply_pad_offset (GstPad * pad, GstEvent * event, gboolean upstream)
671 if (G_UNLIKELY (pad->offset != 0))
672 return _apply_pad_offset (pad, event, upstream);
677 /* should be called with the OBJECT_LOCK */
679 get_pad_caps (GstPad * pad)
681 GstCaps *caps = NULL;
684 ev = find_event_by_type (pad, GST_EVENT_CAPS, 0);
686 gst_event_parse_caps (ev->event, &caps);
692 gst_pad_dispose (GObject * object)
694 GstPad *pad = GST_PAD_CAST (object);
697 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
699 /* unlink the peer pad */
700 if ((peer = gst_pad_get_peer (pad))) {
701 /* window for MT unsafeness, someone else could unlink here
702 * and then we call unlink with wrong pads. The unlink
703 * function would catch this and safely return failed. */
704 if (GST_PAD_IS_SRC (pad))
705 gst_pad_unlink (pad, peer);
707 gst_pad_unlink (peer, pad);
709 gst_object_unref (peer);
712 gst_pad_set_pad_template (pad, NULL);
714 GST_OBJECT_LOCK (pad);
716 GST_OBJECT_UNLOCK (pad);
718 g_hook_list_clear (&pad->probes);
720 G_OBJECT_CLASS (parent_class)->dispose (object);
724 gst_pad_finalize (GObject * object)
726 GstPad *pad = GST_PAD_CAST (object);
729 /* in case the task is still around, clean it up */
730 if ((task = GST_PAD_TASK (pad))) {
731 gst_task_join (task);
732 GST_PAD_TASK (pad) = NULL;
733 gst_object_unref (task);
736 if (pad->activatenotify)
737 pad->activatenotify (pad->activatedata);
738 if (pad->activatemodenotify)
739 pad->activatemodenotify (pad->activatemodedata);
741 pad->linknotify (pad->linkdata);
742 if (pad->unlinknotify)
743 pad->unlinknotify (pad->unlinkdata);
744 if (pad->chainnotify)
745 pad->chainnotify (pad->chaindata);
746 if (pad->chainlistnotify)
747 pad->chainlistnotify (pad->chainlistdata);
748 if (pad->getrangenotify)
749 pad->getrangenotify (pad->getrangedata);
750 if (pad->eventnotify)
751 pad->eventnotify (pad->eventdata);
752 if (pad->querynotify)
753 pad->querynotify (pad->querydata);
754 if (pad->iterintlinknotify)
755 pad->iterintlinknotify (pad->iterintlinkdata);
757 g_rec_mutex_clear (&pad->stream_rec_lock);
758 g_cond_clear (&pad->block_cond);
759 g_array_free (pad->priv->events, TRUE);
761 G_OBJECT_CLASS (parent_class)->finalize (object);
765 gst_pad_set_property (GObject * object, guint prop_id,
766 const GValue * value, GParamSpec * pspec)
768 g_return_if_fail (GST_IS_PAD (object));
771 case PAD_PROP_DIRECTION:
772 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
774 case PAD_PROP_TEMPLATE:
775 gst_pad_set_pad_template (GST_PAD_CAST (object),
776 (GstPadTemplate *) g_value_get_object (value));
778 case PAD_PROP_OFFSET:
779 gst_pad_set_offset (GST_PAD_CAST (object), g_value_get_int64 (value));
782 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
788 gst_pad_get_property (GObject * object, guint prop_id,
789 GValue * value, GParamSpec * pspec)
791 g_return_if_fail (GST_IS_PAD (object));
795 GST_OBJECT_LOCK (object);
796 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
797 GST_OBJECT_UNLOCK (object);
799 case PAD_PROP_DIRECTION:
800 g_value_set_enum (value, GST_PAD_DIRECTION (object));
802 case PAD_PROP_TEMPLATE:
803 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
805 case PAD_PROP_OFFSET:
806 g_value_set_int64 (value, gst_pad_get_offset (GST_PAD_CAST (object)));
809 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
816 * @name: (allow-none): the name of the new pad.
817 * @direction: the #GstPadDirection of the pad.
819 * Creates a new pad with the given name in the given direction.
820 * If name is %NULL, a guaranteed unique name (across all pads)
822 * This function makes a copy of the name so you can safely free the name.
824 * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
830 gst_pad_new (const gchar * name, GstPadDirection direction)
832 return g_object_new (GST_TYPE_PAD,
833 "name", name, "direction", direction, NULL);
837 * gst_pad_new_from_template:
838 * @templ: the pad template to use
839 * @name: (allow-none): the name of the pad
841 * Creates a new pad with the given name from the given template.
842 * If name is %NULL, a guaranteed unique name (across all pads)
844 * This function makes a copy of the name so you can safely free the name.
846 * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
850 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
852 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
854 return g_object_new (GST_TYPE_PAD,
855 "name", name, "direction", templ->direction, "template", templ, NULL);
859 * gst_pad_new_from_static_template:
860 * @templ: the #GstStaticPadTemplate to use
861 * @name: the name of the pad
863 * Creates a new pad with the given name from the given static template.
864 * If name is %NULL, a guaranteed unique name (across all pads)
866 * This function makes a copy of the name so you can safely free the name.
868 * Returns: (transfer floating) (nullable): a new #GstPad, or %NULL in
872 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
876 GstPadTemplate *template;
878 template = gst_static_pad_template_get (templ);
879 pad = gst_pad_new_from_template (template, name);
880 gst_object_unref (template);
884 #define ACQUIRE_PARENT(pad, parent, label) \
886 if (G_LIKELY ((parent = GST_OBJECT_PARENT (pad)))) \
887 gst_object_ref (parent); \
888 else if (G_LIKELY (GST_PAD_NEEDS_PARENT (pad))) \
892 #define RELEASE_PARENT(parent) \
894 if (G_LIKELY (parent)) \
895 gst_object_unref (parent); \
899 * gst_pad_get_direction:
900 * @pad: a #GstPad to get the direction of.
902 * Gets the direction of the pad. The direction of the pad is
903 * decided at construction time so this function does not take
906 * Returns: the #GstPadDirection of the pad.
911 gst_pad_get_direction (GstPad * pad)
913 GstPadDirection result;
915 /* PAD_UNKNOWN is a little silly but we need some sort of
916 * error return value */
917 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
919 result = GST_PAD_DIRECTION (pad);
925 gst_pad_activate_default (GstPad * pad, GstObject * parent)
927 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
931 * gst_pad_mode_get_name:
932 * @mode: the pad mode
934 * Return the name of a pad mode, for use in debug messages mostly.
936 * Returns: short mnemonic for pad mode @mode
939 gst_pad_mode_get_name (GstPadMode mode)
942 case GST_PAD_MODE_NONE:
944 case GST_PAD_MODE_PUSH:
946 case GST_PAD_MODE_PULL:
955 pre_activate (GstPad * pad, GstPadMode new_mode)
958 case GST_PAD_MODE_NONE:
959 GST_OBJECT_LOCK (pad);
960 GST_DEBUG_OBJECT (pad, "setting PAD_MODE NONE, set flushing");
961 GST_PAD_SET_FLUSHING (pad);
962 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
963 GST_PAD_MODE (pad) = new_mode;
964 /* unlock blocked pads so element can resume and stop */
965 GST_PAD_BLOCK_BROADCAST (pad);
966 GST_OBJECT_UNLOCK (pad);
968 case GST_PAD_MODE_PUSH:
969 case GST_PAD_MODE_PULL:
970 GST_OBJECT_LOCK (pad);
971 GST_DEBUG_OBJECT (pad, "setting pad into %s mode, unset flushing",
972 gst_pad_mode_get_name (new_mode));
973 GST_PAD_UNSET_FLUSHING (pad);
974 pad->ABI.abi.last_flowret = GST_FLOW_OK;
975 GST_PAD_MODE (pad) = new_mode;
976 if (GST_PAD_IS_SINK (pad)) {
978 /* make sure the peer src pad sends us all events */
979 if ((peer = GST_PAD_PEER (pad))) {
980 gst_object_ref (peer);
981 GST_OBJECT_UNLOCK (pad);
983 GST_DEBUG_OBJECT (pad, "reschedule events on peer %s:%s",
984 GST_DEBUG_PAD_NAME (peer));
986 GST_OBJECT_LOCK (peer);
987 schedule_events (peer, NULL);
988 GST_OBJECT_UNLOCK (peer);
990 gst_object_unref (peer);
992 GST_OBJECT_UNLOCK (pad);
995 GST_OBJECT_UNLOCK (pad);
1002 post_activate (GstPad * pad, GstPadMode new_mode)
1005 case GST_PAD_MODE_NONE:
1006 /* ensures that streaming stops */
1007 GST_PAD_STREAM_LOCK (pad);
1008 GST_DEBUG_OBJECT (pad, "stopped streaming");
1009 GST_OBJECT_LOCK (pad);
1010 remove_events (pad);
1011 GST_OBJECT_UNLOCK (pad);
1012 GST_PAD_STREAM_UNLOCK (pad);
1014 case GST_PAD_MODE_PUSH:
1015 case GST_PAD_MODE_PULL:
1022 * gst_pad_set_active:
1023 * @pad: the #GstPad to activate or deactivate.
1024 * @active: whether or not the pad should be active.
1026 * Activates or deactivates the given pad.
1027 * Normally called from within core state change functions.
1029 * If @active, makes sure the pad is active. If it is already active, either in
1030 * push or pull mode, just return. Otherwise dispatches to the pad's activate
1031 * function to perform the actual activation.
1033 * If not @active, calls gst_pad_activate_mode() with the pad's current mode
1034 * and a %FALSE argument.
1036 * Returns: %TRUE if the operation was successful.
1041 gst_pad_set_active (GstPad * pad, gboolean active)
1045 gboolean ret = FALSE;
1047 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1049 GST_OBJECT_LOCK (pad);
1050 old = GST_PAD_MODE (pad);
1051 ACQUIRE_PARENT (pad, parent, no_parent);
1052 GST_OBJECT_UNLOCK (pad);
1055 if (old == GST_PAD_MODE_NONE) {
1056 GST_DEBUG_OBJECT (pad, "activating pad from none");
1057 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad, parent);
1059 pad->ABI.abi.last_flowret = GST_FLOW_OK;
1061 GST_DEBUG_OBJECT (pad, "pad was active in %s mode",
1062 gst_pad_mode_get_name (old));
1066 if (old == GST_PAD_MODE_NONE) {
1067 GST_DEBUG_OBJECT (pad, "pad was inactive");
1070 GST_DEBUG_OBJECT (pad, "deactivating pad from %s mode",
1071 gst_pad_mode_get_name (old));
1072 ret = gst_pad_activate_mode (pad, old, FALSE);
1074 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
1078 RELEASE_PARENT (parent);
1080 if (G_UNLIKELY (!ret))
1088 GST_DEBUG_OBJECT (pad, "no parent");
1089 GST_OBJECT_UNLOCK (pad);
1094 GST_OBJECT_LOCK (pad);
1096 g_critical ("Failed to deactivate pad %s:%s, very bad",
1097 GST_DEBUG_PAD_NAME (pad));
1099 GST_WARNING_OBJECT (pad, "Failed to activate pad");
1101 GST_OBJECT_UNLOCK (pad);
1107 * gst_pad_activate_mode:
1108 * @pad: the #GstPad to activate or deactivate.
1109 * @mode: the requested activation mode
1110 * @active: whether or not the pad should be active.
1112 * Activates or deactivates the given pad in @mode via dispatching to the
1113 * pad's activatemodefunc. For use from within pad activation functions only.
1115 * If you don't know what this is, you probably don't want to call it.
1117 * Returns: %TRUE if the operation was successful.
1122 gst_pad_activate_mode (GstPad * pad, GstPadMode mode, gboolean active)
1124 gboolean res = FALSE;
1126 GstPadMode old, new;
1127 GstPadDirection dir;
1130 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1132 GST_OBJECT_LOCK (pad);
1133 old = GST_PAD_MODE (pad);
1134 dir = GST_PAD_DIRECTION (pad);
1135 ACQUIRE_PARENT (pad, parent, no_parent);
1136 GST_OBJECT_UNLOCK (pad);
1138 new = active ? mode : GST_PAD_MODE_NONE;
1143 if (active && old != mode && old != GST_PAD_MODE_NONE) {
1144 /* pad was activate in the wrong direction, deactivate it
1145 * and reactivate it in the requested mode */
1146 GST_DEBUG_OBJECT (pad, "deactivating pad from %s mode",
1147 gst_pad_mode_get_name (old));
1149 if (G_UNLIKELY (!gst_pad_activate_mode (pad, old, FALSE)))
1150 goto deactivate_failed;
1154 case GST_PAD_MODE_PULL:
1156 if (dir == GST_PAD_SINK) {
1157 if ((peer = gst_pad_get_peer (pad))) {
1158 GST_DEBUG_OBJECT (pad, "calling peer");
1159 if (G_UNLIKELY (!gst_pad_activate_mode (peer, mode, active)))
1161 gst_object_unref (peer);
1163 /* there is no peer, this is only fatal when we activate. When we
1164 * deactivate, we must assume the application has unlinked the peer and
1165 * will deactivate it eventually. */
1169 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
1172 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
1173 goto failure; /* Can't activate pull on a src without a
1174 getrange function */
1182 /* Mark pad as needing reconfiguration */
1184 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1185 pre_activate (pad, new);
1187 if (GST_PAD_ACTIVATEMODEFUNC (pad)) {
1188 if (G_UNLIKELY (!GST_PAD_ACTIVATEMODEFUNC (pad) (pad, parent, mode,
1192 /* can happen for sinks of passthrough elements */
1195 post_activate (pad, new);
1197 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in %s mode",
1198 active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1203 /* Clear sticky flags on deactivation */
1205 GST_OBJECT_LOCK (pad);
1206 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1207 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
1208 GST_OBJECT_UNLOCK (pad);
1212 RELEASE_PARENT (parent);
1218 GST_DEBUG_OBJECT (pad, "no parent");
1219 GST_OBJECT_UNLOCK (pad);
1224 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in %s mode",
1225 active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1230 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1231 "failed to %s in switch to %s mode from %s mode",
1232 (active ? "activate" : "deactivate"), gst_pad_mode_get_name (mode),
1233 gst_pad_mode_get_name (old));
1238 GST_OBJECT_LOCK (peer);
1239 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1240 "activate_mode on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
1241 GST_OBJECT_UNLOCK (peer);
1242 gst_object_unref (peer);
1247 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
1248 "pad in pull mode");
1253 GST_OBJECT_LOCK (pad);
1254 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in %s mode",
1255 active ? "activate" : "deactivate", gst_pad_mode_get_name (mode));
1256 GST_PAD_SET_FLUSHING (pad);
1257 GST_PAD_MODE (pad) = old;
1258 GST_OBJECT_UNLOCK (pad);
1264 * gst_pad_is_active:
1265 * @pad: the #GstPad to query
1267 * Query if a pad is active
1269 * Returns: %TRUE if the pad is active.
1274 gst_pad_is_active (GstPad * pad)
1276 gboolean result = FALSE;
1278 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1280 GST_OBJECT_LOCK (pad);
1281 result = GST_PAD_IS_ACTIVE (pad);
1282 GST_OBJECT_UNLOCK (pad);
1288 cleanup_hook (GstPad * pad, GHook * hook)
1290 GstPadProbeType type;
1292 if (!G_HOOK_IS_VALID (hook))
1295 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1297 if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1298 /* unblock when we remove the last blocking probe */
1300 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1303 /* Might have new probes now that want to be called */
1304 GST_PAD_BLOCK_BROADCAST (pad);
1306 if (pad->num_blocked == 0) {
1307 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1308 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKED);
1311 g_hook_destroy_link (&pad->probes, hook);
1316 * gst_pad_add_probe:
1317 * @pad: the #GstPad to add the probe to
1318 * @mask: the probe mask
1319 * @callback: #GstPadProbeCallback that will be called with notifications of
1321 * @user_data: (closure): user data passed to the callback
1322 * @destroy_data: #GDestroyNotify for user_data
1324 * Be notified of different states of pads. The provided callback is called for
1325 * every state that matches @mask.
1327 * Returns: an id or 0 if no probe is pending. The id can be used to remove the
1328 * probe with gst_pad_remove_probe(). When using GST_PAD_PROBE_TYPE_IDLE it can
1329 * happen that the probe can be run immediately and if the probe returns
1330 * GST_PAD_PROBE_REMOVE this functions returns 0.
1335 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
1336 GstPadProbeCallback callback, gpointer user_data,
1337 GDestroyNotify destroy_data)
1342 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1343 g_return_val_if_fail (mask != 0, 0);
1345 GST_OBJECT_LOCK (pad);
1347 /* make a new probe */
1348 hook = g_hook_alloc (&pad->probes);
1350 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1353 /* when no contraints are given for the types, assume all types are
1355 if ((mask & GST_PAD_PROBE_TYPE_ALL_BOTH) == 0)
1356 mask |= GST_PAD_PROBE_TYPE_ALL_BOTH;
1357 if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1358 mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1360 /* store our flags and other fields */
1361 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1362 hook->func = callback;
1363 hook->data = user_data;
1364 hook->destroy = destroy_data;
1365 PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
1368 g_hook_prepend (&pad->probes, hook);
1370 /* incremenent cookie so that the new hook get's called */
1371 pad->priv->probe_list_cookie++;
1373 /* get the id of the hook, we return this and it can be used to remove the
1375 res = hook->hook_id;
1377 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1379 if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1380 /* we have a block probe */
1382 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKED);
1383 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1384 "now %d blocking probes", pad->num_blocked);
1386 /* Might have new probes now that want to be called */
1387 GST_PAD_BLOCK_BROADCAST (pad);
1390 /* call the callback if we need to be called for idle callbacks */
1391 if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1392 if (pad->priv->using > 0) {
1393 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1394 * flag above, the last thread to leave the push will do the callback. New
1395 * threads going into the push will block. */
1396 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1397 "pad is in use, delay idle callback");
1398 GST_OBJECT_UNLOCK (pad);
1400 GstPadProbeInfo info = { GST_PAD_PROBE_TYPE_IDLE, res, };
1401 GstPadProbeReturn ret;
1403 /* Keep another ref, the callback could destroy the pad */
1404 gst_object_ref (pad);
1405 pad->priv->idle_running++;
1407 /* the pad is idle now, we can signal the idle callback now */
1408 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1409 "pad is idle, trigger idle callback");
1410 GST_OBJECT_UNLOCK (pad);
1412 ret = callback (pad, &info, user_data);
1414 GST_OBJECT_LOCK (pad);
1416 case GST_PAD_PROBE_REMOVE:
1417 /* remove the probe */
1418 GST_DEBUG_OBJECT (pad, "asked to remove hook");
1419 cleanup_hook (pad, hook);
1422 case GST_PAD_PROBE_DROP:
1423 GST_DEBUG_OBJECT (pad, "asked to drop item");
1425 case GST_PAD_PROBE_PASS:
1426 GST_DEBUG_OBJECT (pad, "asked to pass item");
1428 case GST_PAD_PROBE_OK:
1429 GST_DEBUG_OBJECT (pad, "probe returned OK");
1431 case GST_PAD_PROBE_HANDLED:
1432 GST_DEBUG_OBJECT (pad, "probe handled the data");
1435 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
1438 pad->priv->idle_running--;
1439 if (pad->priv->idle_running == 0) {
1440 GST_PAD_BLOCK_BROADCAST (pad);
1442 GST_OBJECT_UNLOCK (pad);
1444 gst_object_unref (pad);
1447 GST_OBJECT_UNLOCK (pad);
1453 * gst_pad_remove_probe:
1454 * @pad: the #GstPad with the probe
1455 * @id: the probe id to remove
1457 * Remove the probe with @id from @pad.
1462 gst_pad_remove_probe (GstPad * pad, gulong id)
1466 g_return_if_fail (GST_IS_PAD (pad));
1468 GST_OBJECT_LOCK (pad);
1470 hook = g_hook_get (&pad->probes, id);
1474 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1476 cleanup_hook (pad, hook);
1477 GST_OBJECT_UNLOCK (pad);
1483 GST_OBJECT_UNLOCK (pad);
1484 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1490 * gst_pad_is_blocked:
1491 * @pad: the #GstPad to query
1493 * Checks if the pad is blocked or not. This function returns the
1494 * last requested state of the pad. It is not certain that the pad
1495 * is actually blocking at this point (see gst_pad_is_blocking()).
1497 * Returns: %TRUE if the pad is blocked.
1502 gst_pad_is_blocked (GstPad * pad)
1504 gboolean result = FALSE;
1506 g_return_val_if_fail (GST_IS_PAD (pad), result);
1508 GST_OBJECT_LOCK (pad);
1509 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED);
1510 GST_OBJECT_UNLOCK (pad);
1516 * gst_pad_is_blocking:
1517 * @pad: the #GstPad to query
1519 * Checks if the pad is blocking or not. This is a guaranteed state
1520 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1522 * Returns: %TRUE if the pad is blocking.
1527 gst_pad_is_blocking (GstPad * pad)
1529 gboolean result = FALSE;
1531 g_return_val_if_fail (GST_IS_PAD (pad), result);
1533 GST_OBJECT_LOCK (pad);
1534 /* the blocking flag is only valid if the pad is not flushing */
1535 result = GST_PAD_IS_BLOCKING (pad) && !GST_PAD_IS_FLUSHING (pad);
1536 GST_OBJECT_UNLOCK (pad);
1542 * gst_pad_needs_reconfigure:
1543 * @pad: the #GstPad to check
1545 * Check the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1546 * if the flag was set.
1548 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag is set on @pad.
1551 gst_pad_needs_reconfigure (GstPad * pad)
1553 gboolean reconfigure;
1555 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1557 GST_OBJECT_LOCK (pad);
1558 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1559 GST_DEBUG_OBJECT (pad, "peeking RECONFIGURE flag %d", reconfigure);
1560 GST_OBJECT_UNLOCK (pad);
1566 * gst_pad_check_reconfigure:
1567 * @pad: the #GstPad to check
1569 * Check and clear the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1570 * if the flag was set.
1572 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag was set on @pad.
1575 gst_pad_check_reconfigure (GstPad * pad)
1577 gboolean reconfigure;
1579 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1581 GST_OBJECT_LOCK (pad);
1582 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1584 GST_DEBUG_OBJECT (pad, "remove RECONFIGURE flag");
1585 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1587 GST_OBJECT_UNLOCK (pad);
1593 * gst_pad_mark_reconfigure:
1594 * @pad: the #GstPad to mark
1596 * Mark a pad for needing reconfiguration. The next call to
1597 * gst_pad_check_reconfigure() will return %TRUE after this call.
1600 gst_pad_mark_reconfigure (GstPad * pad)
1602 g_return_if_fail (GST_IS_PAD (pad));
1604 GST_OBJECT_LOCK (pad);
1605 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1606 GST_OBJECT_UNLOCK (pad);
1610 * gst_pad_set_activate_function:
1612 * @f: the #GstPadActivateFunction to set.
1614 * Calls gst_pad_set_activate_function_full() with %NULL for the user_data and
1618 * gst_pad_set_activate_function_full:
1620 * @activate: the #GstPadActivateFunction to set.
1621 * @user_data: user_data passed to @notify
1622 * @notify: notify called when @activate will not be used anymore.
1624 * Sets the given activate function for @pad. The activate function will
1625 * dispatch to gst_pad_activate_mode() to perform the actual activation.
1626 * Only makes sense to set on sink pads.
1628 * Call this function if your sink pad can start a pull-based task.
1631 gst_pad_set_activate_function_full (GstPad * pad,
1632 GstPadActivateFunction activate, gpointer user_data, GDestroyNotify notify)
1634 g_return_if_fail (GST_IS_PAD (pad));
1636 if (pad->activatenotify)
1637 pad->activatenotify (pad->activatedata);
1638 GST_PAD_ACTIVATEFUNC (pad) = activate;
1639 pad->activatedata = user_data;
1640 pad->activatenotify = notify;
1642 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1643 GST_DEBUG_FUNCPTR_NAME (activate));
1647 * gst_pad_set_activatemode_function:
1649 * @f: the #GstPadActivateModeFunction to set.
1651 * Calls gst_pad_set_activatemode_function_full() with %NULL for the user_data and
1655 * gst_pad_set_activatemode_function_full:
1657 * @activatemode: the #GstPadActivateModeFunction to set.
1658 * @user_data: user_data passed to @notify
1659 * @notify: notify called when @activatemode will not be used anymore.
1661 * Sets the given activate_mode function for the pad. An activate_mode function
1662 * prepares the element for data passing.
1665 gst_pad_set_activatemode_function_full (GstPad * pad,
1666 GstPadActivateModeFunction activatemode, gpointer user_data,
1667 GDestroyNotify notify)
1669 g_return_if_fail (GST_IS_PAD (pad));
1671 if (pad->activatemodenotify)
1672 pad->activatemodenotify (pad->activatemodedata);
1673 GST_PAD_ACTIVATEMODEFUNC (pad) = activatemode;
1674 pad->activatemodedata = user_data;
1675 pad->activatemodenotify = notify;
1677 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatemodefunc set to %s",
1678 GST_DEBUG_FUNCPTR_NAME (activatemode));
1682 * gst_pad_set_chain_function:
1683 * @p: a sink #GstPad.
1684 * @f: the #GstPadChainFunction to set.
1686 * Calls gst_pad_set_chain_function_full() with %NULL for the user_data and
1690 * gst_pad_set_chain_function_full:
1691 * @pad: a sink #GstPad.
1692 * @chain: the #GstPadChainFunction to set.
1693 * @user_data: user_data passed to @notify
1694 * @notify: notify called when @chain will not be used anymore.
1696 * Sets the given chain function for the pad. The chain function is called to
1697 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1700 gst_pad_set_chain_function_full (GstPad * pad, GstPadChainFunction chain,
1701 gpointer user_data, GDestroyNotify notify)
1703 g_return_if_fail (GST_IS_PAD (pad));
1704 g_return_if_fail (GST_PAD_IS_SINK (pad));
1706 if (pad->chainnotify)
1707 pad->chainnotify (pad->chaindata);
1708 GST_PAD_CHAINFUNC (pad) = chain;
1709 pad->chaindata = user_data;
1710 pad->chainnotify = notify;
1712 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1713 GST_DEBUG_FUNCPTR_NAME (chain));
1717 * gst_pad_set_chain_list_function:
1718 * @p: a sink #GstPad.
1719 * @f: the #GstPadChainListFunction to set.
1721 * Calls gst_pad_set_chain_list_function_full() with %NULL for the user_data and
1725 * gst_pad_set_chain_list_function_full:
1726 * @pad: a sink #GstPad.
1727 * @chainlist: the #GstPadChainListFunction to set.
1728 * @user_data: user_data passed to @notify
1729 * @notify: notify called when @chainlist will not be used anymore.
1731 * Sets the given chain list function for the pad. The chainlist function is
1732 * called to process a #GstBufferList input buffer list. See
1733 * #GstPadChainListFunction for more details.
1736 gst_pad_set_chain_list_function_full (GstPad * pad,
1737 GstPadChainListFunction chainlist, gpointer user_data,
1738 GDestroyNotify notify)
1740 g_return_if_fail (GST_IS_PAD (pad));
1741 g_return_if_fail (GST_PAD_IS_SINK (pad));
1743 if (pad->chainlistnotify)
1744 pad->chainlistnotify (pad->chainlistdata);
1745 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1746 pad->chainlistdata = user_data;
1747 pad->chainlistnotify = notify;
1749 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1750 GST_DEBUG_FUNCPTR_NAME (chainlist));
1754 * gst_pad_set_getrange_function:
1755 * @p: a source #GstPad.
1756 * @f: the #GstPadGetRangeFunction to set.
1758 * Calls gst_pad_set_getrange_function_full() with %NULL for the user_data and
1762 * gst_pad_set_getrange_function_full:
1763 * @pad: a source #GstPad.
1764 * @get: the #GstPadGetRangeFunction to set.
1765 * @user_data: user_data passed to @notify
1766 * @notify: notify called when @get will not be used anymore.
1768 * Sets the given getrange function for the pad. The getrange function is
1769 * called to produce a new #GstBuffer to start the processing pipeline. see
1770 * #GstPadGetRangeFunction for a description of the getrange function.
1773 gst_pad_set_getrange_function_full (GstPad * pad, GstPadGetRangeFunction get,
1774 gpointer user_data, GDestroyNotify notify)
1776 g_return_if_fail (GST_IS_PAD (pad));
1777 g_return_if_fail (GST_PAD_IS_SRC (pad));
1779 if (pad->getrangenotify)
1780 pad->getrangenotify (pad->getrangedata);
1781 GST_PAD_GETRANGEFUNC (pad) = get;
1782 pad->getrangedata = user_data;
1783 pad->getrangenotify = notify;
1785 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1786 GST_DEBUG_FUNCPTR_NAME (get));
1790 * gst_pad_set_event_function:
1791 * @p: a #GstPad of either direction.
1792 * @f: the #GstPadEventFunction to set.
1794 * Calls gst_pad_set_event_function_full() with %NULL for the user_data and
1798 * gst_pad_set_event_function_full:
1799 * @pad: a #GstPad of either direction.
1800 * @event: the #GstPadEventFunction to set.
1801 * @user_data: user_data passed to @notify
1802 * @notify: notify called when @event will not be used anymore.
1804 * Sets the given event handler for the pad.
1807 gst_pad_set_event_function_full (GstPad * pad, GstPadEventFunction event,
1808 gpointer user_data, GDestroyNotify notify)
1810 g_return_if_fail (GST_IS_PAD (pad));
1812 if (pad->eventnotify)
1813 pad->eventnotify (pad->eventdata);
1814 GST_PAD_EVENTFUNC (pad) = event;
1815 pad->eventdata = user_data;
1816 pad->eventnotify = notify;
1818 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1819 GST_DEBUG_FUNCPTR_NAME (event));
1823 * gst_pad_set_query_function:
1824 * @p: a #GstPad of either direction.
1825 * @f: the #GstPadQueryFunction to set.
1827 * Calls gst_pad_set_query_function_full() with %NULL for the user_data and
1831 * gst_pad_set_query_function_full:
1832 * @pad: a #GstPad of either direction.
1833 * @query: the #GstPadQueryFunction to set.
1834 * @user_data: user_data passed to @notify
1835 * @notify: notify called when @query will not be used anymore.
1837 * Set the given query function for the pad.
1840 gst_pad_set_query_function_full (GstPad * pad, GstPadQueryFunction query,
1841 gpointer user_data, GDestroyNotify notify)
1843 g_return_if_fail (GST_IS_PAD (pad));
1845 if (pad->querynotify)
1846 pad->querynotify (pad->querydata);
1847 GST_PAD_QUERYFUNC (pad) = query;
1848 pad->querydata = user_data;
1849 pad->querynotify = notify;
1851 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1852 GST_DEBUG_FUNCPTR_NAME (query));
1856 * gst_pad_set_iterate_internal_links_function:
1857 * @p: a #GstPad of either direction.
1858 * @f: the #GstPadIterIntLinkFunction to set.
1860 * Calls gst_pad_set_iterate_internal_links_function_full() with %NULL
1861 * for the user_data and notify.
1864 * gst_pad_set_iterate_internal_links_function_full:
1865 * @pad: a #GstPad of either direction.
1866 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1867 * @user_data: user_data passed to @notify
1868 * @notify: notify called when @iterintlink will not be used anymore.
1870 * Sets the given internal link iterator function for the pad.
1873 gst_pad_set_iterate_internal_links_function_full (GstPad * pad,
1874 GstPadIterIntLinkFunction iterintlink, gpointer user_data,
1875 GDestroyNotify notify)
1877 g_return_if_fail (GST_IS_PAD (pad));
1879 if (pad->iterintlinknotify)
1880 pad->iterintlinknotify (pad->iterintlinkdata);
1881 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1882 pad->iterintlinkdata = user_data;
1883 pad->iterintlinknotify = notify;
1885 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1886 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1890 * gst_pad_set_link_function:
1892 * @f: the #GstPadLinkFunction to set.
1894 * Calls gst_pad_set_link_function_full() with %NULL
1895 * for the user_data and notify.
1898 * gst_pad_set_link_function_full:
1900 * @link: the #GstPadLinkFunction to set.
1901 * @user_data: user_data passed to @notify
1902 * @notify: notify called when @link will not be used anymore.
1904 * Sets the given link function for the pad. It will be called when
1905 * the pad is linked with another pad.
1907 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1910 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1911 * cannot be made for some reason.
1913 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1914 * of the peer sink pad, if present.
1917 gst_pad_set_link_function_full (GstPad * pad, GstPadLinkFunction link,
1918 gpointer user_data, GDestroyNotify notify)
1920 g_return_if_fail (GST_IS_PAD (pad));
1922 if (pad->linknotify)
1923 pad->linknotify (pad->linkdata);
1924 GST_PAD_LINKFUNC (pad) = link;
1925 pad->linkdata = user_data;
1926 pad->linknotify = notify;
1928 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1929 GST_DEBUG_FUNCPTR_NAME (link));
1933 * gst_pad_set_unlink_function:
1935 * @f: the #GstPadUnlinkFunction to set.
1937 * Calls gst_pad_set_unlink_function_full() with %NULL
1938 * for the user_data and notify.
1941 * gst_pad_set_unlink_function_full:
1943 * @unlink: the #GstPadUnlinkFunction to set.
1944 * @user_data: user_data passed to @notify
1945 * @notify: notify called when @unlink will not be used anymore.
1947 * Sets the given unlink function for the pad. It will be called
1948 * when the pad is unlinked.
1951 gst_pad_set_unlink_function_full (GstPad * pad, GstPadUnlinkFunction unlink,
1952 gpointer user_data, GDestroyNotify notify)
1954 g_return_if_fail (GST_IS_PAD (pad));
1956 if (pad->unlinknotify)
1957 pad->unlinknotify (pad->unlinkdata);
1958 GST_PAD_UNLINKFUNC (pad) = unlink;
1959 pad->unlinkdata = user_data;
1960 pad->unlinknotify = notify;
1962 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1963 GST_DEBUG_FUNCPTR_NAME (unlink));
1968 * @srcpad: the source #GstPad to unlink.
1969 * @sinkpad: the sink #GstPad to unlink.
1971 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1972 * signal on both pads.
1974 * Returns: %TRUE if the pads were unlinked. This function returns %FALSE if
1975 * the pads were not linked together.
1980 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1982 gboolean result = FALSE;
1983 GstElement *parent = NULL;
1985 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1986 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1987 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1988 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1990 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1991 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1992 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1994 /* We need to notify the parent before taking any pad locks as the bin in
1995 * question might be waiting for a lock on the pad while holding its lock
1996 * that our message will try to take. */
1997 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1998 if (GST_IS_ELEMENT (parent)) {
1999 gst_element_post_message (parent,
2000 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2001 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
2003 gst_object_unref (parent);
2008 GST_OBJECT_LOCK (srcpad);
2009 GST_OBJECT_LOCK (sinkpad);
2011 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
2012 goto not_linked_together;
2014 if (GST_PAD_UNLINKFUNC (srcpad)) {
2015 GstObject *tmpparent;
2017 ACQUIRE_PARENT (srcpad, tmpparent, no_src_parent);
2019 GST_PAD_UNLINKFUNC (srcpad) (srcpad, tmpparent);
2020 RELEASE_PARENT (tmpparent);
2023 if (GST_PAD_UNLINKFUNC (sinkpad)) {
2024 GstObject *tmpparent;
2026 ACQUIRE_PARENT (sinkpad, tmpparent, no_sink_parent);
2028 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad, tmpparent);
2029 RELEASE_PARENT (tmpparent);
2033 /* first clear peers */
2034 GST_PAD_PEER (srcpad) = NULL;
2035 GST_PAD_PEER (sinkpad) = NULL;
2037 GST_OBJECT_UNLOCK (sinkpad);
2038 GST_OBJECT_UNLOCK (srcpad);
2040 /* fire off a signal to each of the pads telling them
2041 * that they've been unlinked */
2042 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
2043 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
2045 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
2046 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2051 if (parent != NULL) {
2052 gst_element_post_message (parent,
2053 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2054 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
2055 gst_object_unref (parent);
2060 not_linked_together:
2062 /* we do not emit a warning in this case because unlinking cannot
2063 * be made MT safe.*/
2064 GST_OBJECT_UNLOCK (sinkpad);
2065 GST_OBJECT_UNLOCK (srcpad);
2071 * gst_pad_is_linked:
2072 * @pad: pad to check
2074 * Checks if a @pad is linked to another pad or not.
2076 * Returns: %TRUE if the pad is linked, %FALSE otherwise.
2081 gst_pad_is_linked (GstPad * pad)
2085 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2087 GST_OBJECT_LOCK (pad);
2088 result = (GST_PAD_PEER (pad) != NULL);
2089 GST_OBJECT_UNLOCK (pad);
2094 /* get the caps from both pads and see if the intersection
2097 * This function should be called with the pad LOCK on both
2101 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
2102 GstPadLinkCheck flags)
2104 GstCaps *srccaps = NULL;
2105 GstCaps *sinkcaps = NULL;
2106 gboolean compatible = FALSE;
2108 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
2111 /* Doing the expensive caps checking takes priority over only checking the template caps */
2112 if (flags & GST_PAD_LINK_CHECK_CAPS) {
2113 GST_OBJECT_UNLOCK (sink);
2114 GST_OBJECT_UNLOCK (src);
2116 srccaps = gst_pad_query_caps (src, NULL);
2117 sinkcaps = gst_pad_query_caps (sink, NULL);
2119 GST_OBJECT_LOCK (src);
2120 GST_OBJECT_LOCK (sink);
2122 /* If one of the two pads doesn't have a template, consider the intersection
2124 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
2125 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
2129 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
2131 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
2134 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
2136 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
2139 /* if we have caps on both pads we can check the intersection. If one
2140 * of the caps is %NULL, we return %TRUE. */
2141 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
2143 gst_caps_unref (srccaps);
2145 gst_caps_unref (sinkcaps);
2149 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
2150 gst_caps_unref (srccaps);
2151 gst_caps_unref (sinkcaps);
2154 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
2155 (compatible ? "" : "not "));
2160 /* check if the grandparents of both pads are the same.
2161 * This check is required so that we don't try to link
2162 * pads from elements in different bins without ghostpads.
2164 * The LOCK should be held on both pads
2167 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
2169 GstObject *psrc, *psink;
2171 psrc = GST_OBJECT_PARENT (src);
2172 psink = GST_OBJECT_PARENT (sink);
2174 /* if one of the pads has no parent, we allow the link */
2175 if (G_UNLIKELY (psrc == NULL || psink == NULL))
2178 /* only care about parents that are elements */
2179 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
2180 goto no_element_parent;
2182 /* if the parents are the same, we have a loop */
2183 if (G_UNLIKELY (psrc == psink))
2186 /* if they both have a parent, we check the grandparents. We can not lock
2187 * the parent because we hold on the child (pad) and the locking order is
2188 * parent >> child. */
2189 psrc = GST_OBJECT_PARENT (psrc);
2190 psink = GST_OBJECT_PARENT (psink);
2192 /* if they have grandparents but they are not the same */
2193 if (G_UNLIKELY (psrc != psink))
2194 goto wrong_grandparents;
2201 GST_CAT_DEBUG (GST_CAT_CAPS,
2202 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
2203 GST_PTR_FORMAT, psrc, psink);
2208 GST_CAT_DEBUG (GST_CAT_CAPS,
2209 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
2210 GST_PTR_FORMAT, psrc, psink);
2215 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
2221 GST_CAT_DEBUG (GST_CAT_CAPS,
2222 "pads have different grandparents %" GST_PTR_FORMAT " and %"
2223 GST_PTR_FORMAT, psrc, psink);
2228 /* FIXME leftover from an attempt at refactoring... */
2229 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
2230 * the two pads will be locked in the srcpad, sinkpad order. */
2231 static GstPadLinkReturn
2232 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2234 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
2235 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2237 GST_OBJECT_LOCK (srcpad);
2239 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
2240 goto src_was_linked;
2242 GST_OBJECT_LOCK (sinkpad);
2244 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
2245 goto sink_was_linked;
2247 /* check hierarchy, pads can only be linked if the grandparents
2249 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
2250 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
2251 goto wrong_hierarchy;
2253 /* check pad caps for non-empty intersection */
2254 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
2257 /* FIXME check pad scheduling for non-empty intersection */
2259 return GST_PAD_LINK_OK;
2263 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
2264 GST_DEBUG_PAD_NAME (srcpad),
2265 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
2266 /* we do not emit a warning in this case because unlinking cannot
2267 * be made MT safe.*/
2268 GST_OBJECT_UNLOCK (srcpad);
2269 return GST_PAD_LINK_WAS_LINKED;
2273 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
2274 GST_DEBUG_PAD_NAME (sinkpad),
2275 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
2276 /* we do not emit a warning in this case because unlinking cannot
2277 * be made MT safe.*/
2278 GST_OBJECT_UNLOCK (sinkpad);
2279 GST_OBJECT_UNLOCK (srcpad);
2280 return GST_PAD_LINK_WAS_LINKED;
2284 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
2285 GST_OBJECT_UNLOCK (sinkpad);
2286 GST_OBJECT_UNLOCK (srcpad);
2287 return GST_PAD_LINK_WRONG_HIERARCHY;
2291 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
2292 GST_OBJECT_UNLOCK (sinkpad);
2293 GST_OBJECT_UNLOCK (srcpad);
2294 return GST_PAD_LINK_NOFORMAT;
2300 * @srcpad: the source #GstPad.
2301 * @sinkpad: the sink #GstPad.
2303 * Checks if the source pad and the sink pad are compatible so they can be
2306 * Returns: %TRUE if the pads can be linked.
2309 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2311 GstPadLinkReturn result;
2313 /* generic checks */
2314 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2315 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2317 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2318 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2320 /* gst_pad_link_prepare does everything for us, we only release the locks
2321 * on the pads that it gets us. If this function returns !OK the locks are not
2323 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2324 if (result != GST_PAD_LINK_OK)
2327 GST_OBJECT_UNLOCK (srcpad);
2328 GST_OBJECT_UNLOCK (sinkpad);
2331 return result == GST_PAD_LINK_OK;
2335 * gst_pad_link_full:
2336 * @srcpad: the source #GstPad to link.
2337 * @sinkpad: the sink #GstPad to link.
2338 * @flags: the checks to validate when linking
2340 * Links the source pad and the sink pad.
2342 * This variant of #gst_pad_link provides a more granular control on the
2343 * checks being done when linking. While providing some considerable speedups
2344 * the caller of this method must be aware that wrong usage of those flags
2345 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2346 * for more information.
2350 * Returns: A result code indicating if the connection worked or
2354 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2356 GstPadLinkReturn result;
2358 GstPadLinkFunction srcfunc, sinkfunc;
2360 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2361 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2362 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2363 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2364 GST_PAD_LINK_WRONG_DIRECTION);
2366 /* Notify the parent early. See gst_pad_unlink for details. */
2367 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2368 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2369 gst_element_post_message (parent,
2370 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2371 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2373 gst_object_unref (parent);
2378 /* prepare will also lock the two pads */
2379 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2381 if (G_UNLIKELY (result != GST_PAD_LINK_OK)) {
2382 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed: %s",
2383 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad),
2384 gst_pad_link_get_name (result));
2388 /* must set peers before calling the link function */
2389 GST_PAD_PEER (srcpad) = sinkpad;
2390 GST_PAD_PEER (sinkpad) = srcpad;
2392 /* check events, when something is different, mark pending */
2393 schedule_events (srcpad, sinkpad);
2395 /* get the link functions */
2396 srcfunc = GST_PAD_LINKFUNC (srcpad);
2397 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2399 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2400 /* custom link functions, execute them */
2401 GST_OBJECT_UNLOCK (sinkpad);
2402 GST_OBJECT_UNLOCK (srcpad);
2405 GstObject *tmpparent;
2407 ACQUIRE_PARENT (srcpad, tmpparent, no_parent);
2408 /* this one will call the peer link function */
2409 result = srcfunc (srcpad, tmpparent, sinkpad);
2410 RELEASE_PARENT (tmpparent);
2411 } else if (sinkfunc) {
2412 GstObject *tmpparent;
2414 ACQUIRE_PARENT (sinkpad, tmpparent, no_parent);
2415 /* if no source link function, we need to call the sink link
2416 * function ourselves. */
2417 result = sinkfunc (sinkpad, tmpparent, srcpad);
2418 RELEASE_PARENT (tmpparent);
2422 GST_OBJECT_LOCK (srcpad);
2423 GST_OBJECT_LOCK (sinkpad);
2425 /* we released the lock, check if the same pads are linked still */
2426 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2427 goto concurrent_link;
2429 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2432 GST_OBJECT_UNLOCK (sinkpad);
2433 GST_OBJECT_UNLOCK (srcpad);
2435 /* fire off a signal to each of the pads telling them
2436 * that they've been linked */
2437 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2438 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2440 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2441 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2443 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2446 if (G_LIKELY (parent)) {
2447 gst_element_post_message (parent,
2448 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2449 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2450 gst_object_unref (parent);
2458 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2459 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2460 GST_OBJECT_UNLOCK (sinkpad);
2461 GST_OBJECT_UNLOCK (srcpad);
2463 /* The other link operation succeeded first */
2464 result = GST_PAD_LINK_WAS_LINKED;
2469 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed: %s",
2470 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad),
2471 gst_pad_link_get_name (result));
2473 GST_PAD_PEER (srcpad) = NULL;
2474 GST_PAD_PEER (sinkpad) = NULL;
2476 GST_OBJECT_UNLOCK (sinkpad);
2477 GST_OBJECT_UNLOCK (srcpad);
2485 * @srcpad: the source #GstPad to link.
2486 * @sinkpad: the sink #GstPad to link.
2488 * Links the source pad and the sink pad.
2490 * Returns: A result code indicating if the connection worked or
2496 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2498 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2502 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2504 GstPadTemplate **template_p;
2506 /* this function would need checks if it weren't static */
2508 GST_OBJECT_LOCK (pad);
2509 template_p = &pad->padtemplate;
2510 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2511 GST_OBJECT_UNLOCK (pad);
2514 gst_pad_template_pad_created (templ, pad);
2518 * gst_pad_get_pad_template:
2521 * Gets the template for @pad.
2523 * Returns: (transfer full) (nullable): the #GstPadTemplate from which
2524 * this pad was instantiated, or %NULL if this pad has no
2525 * template. Unref after usage.
2528 gst_pad_get_pad_template (GstPad * pad)
2530 GstPadTemplate *templ;
2532 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2534 templ = GST_PAD_PAD_TEMPLATE (pad);
2536 return (templ ? gst_object_ref (templ) : NULL);
2540 * gst_pad_has_current_caps:
2541 * @pad: a #GstPad to check
2543 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2545 * Returns: %TRUE when @pad has caps associated with it.
2548 gst_pad_has_current_caps (GstPad * pad)
2553 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2555 GST_OBJECT_LOCK (pad);
2556 caps = get_pad_caps (pad);
2557 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2558 "check current pad caps %" GST_PTR_FORMAT, caps);
2559 result = (caps != NULL);
2560 GST_OBJECT_UNLOCK (pad);
2566 * gst_pad_get_current_caps:
2567 * @pad: a #GstPad to get the current capabilities of.
2569 * Gets the capabilities currently configured on @pad with the last
2570 * #GST_EVENT_CAPS event.
2572 * Returns: the current caps of the pad with incremented ref-count.
2575 gst_pad_get_current_caps (GstPad * pad)
2579 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2581 GST_OBJECT_LOCK (pad);
2582 if ((result = get_pad_caps (pad)))
2583 gst_caps_ref (result);
2584 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2585 "get current pad caps %" GST_PTR_FORMAT, result);
2586 GST_OBJECT_UNLOCK (pad);
2592 * gst_pad_get_pad_template_caps:
2593 * @pad: a #GstPad to get the template capabilities from.
2595 * Gets the capabilities for @pad's template.
2597 * Returns: (transfer full): the #GstCaps of this pad template.
2598 * Unref after usage.
2601 gst_pad_get_pad_template_caps (GstPad * pad)
2603 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2605 if (GST_PAD_PAD_TEMPLATE (pad))
2606 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2608 return gst_caps_ref (GST_CAPS_ANY);
2613 * @pad: a #GstPad to get the peer of.
2615 * Gets the peer of @pad. This function refs the peer pad so
2616 * you need to unref it after use.
2618 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2623 gst_pad_get_peer (GstPad * pad)
2627 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2629 GST_OBJECT_LOCK (pad);
2630 result = GST_PAD_PEER (pad);
2632 gst_object_ref (result);
2633 GST_OBJECT_UNLOCK (pad);
2639 * gst_pad_get_allowed_caps:
2642 * Gets the capabilities of the allowed media types that can flow through
2643 * @pad and its peer.
2645 * The allowed capabilities is calculated as the intersection of the results of
2646 * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2647 * on the resulting caps.
2649 * Returns: (transfer full) (nullable): the allowed #GstCaps of the
2650 * pad link. Unref the caps when you no longer need it. This
2651 * function returns %NULL when @pad has no peer.
2656 gst_pad_get_allowed_caps (GstPad * pad)
2659 GstCaps *caps = NULL;
2662 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2664 GST_OBJECT_LOCK (pad);
2665 if (G_UNLIKELY (GST_PAD_PEER (pad) == NULL))
2667 GST_OBJECT_UNLOCK (pad);
2669 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2671 mycaps = gst_pad_query_caps (pad, NULL);
2673 /* Query peer caps */
2674 query = gst_query_new_caps (mycaps);
2675 gst_pad_peer_query (pad, query);
2676 gst_query_parse_caps_result (query, &caps);
2677 gst_caps_ref (caps);
2678 gst_query_unref (query);
2680 gst_caps_unref (mycaps);
2682 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2689 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2690 GST_OBJECT_UNLOCK (pad);
2697 * gst_pad_iterate_internal_links_default:
2698 * @pad: the #GstPad to get the internal links of.
2699 * @parent: (allow-none): the parent of @pad or %NULL
2701 * Iterate the list of pads to which the given pad is linked to inside of
2702 * the parent element.
2703 * This is the default handler, and thus returns an iterator of all of the
2704 * pads inside the parent element with opposite direction.
2706 * The caller must free this iterator after use with gst_iterator_free().
2708 * Returns: (nullable): a #GstIterator of #GstPad, or %NULL if @pad
2709 * has no parent. Unref each returned pad with gst_object_unref().
2712 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2719 GstElement *eparent;
2721 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2723 if (parent != NULL && GST_IS_ELEMENT (parent)) {
2724 eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2726 GST_OBJECT_LOCK (pad);
2727 eparent = GST_PAD_PARENT (pad);
2728 if (!eparent || !GST_IS_ELEMENT (eparent))
2731 gst_object_ref (eparent);
2732 GST_OBJECT_UNLOCK (pad);
2735 if (pad->direction == GST_PAD_SRC)
2736 padlist = &eparent->sinkpads;
2738 padlist = &eparent->srcpads;
2740 GST_DEBUG_OBJECT (pad, "Making iterator");
2742 cookie = &eparent->pads_cookie;
2744 lock = GST_OBJECT_GET_LOCK (eparent);
2746 res = gst_iterator_new_list (GST_TYPE_PAD,
2747 lock, cookie, padlist, (GObject *) owner, NULL);
2749 gst_object_unref (owner);
2756 GST_OBJECT_UNLOCK (pad);
2757 GST_DEBUG_OBJECT (pad, "no parent element");
2763 * gst_pad_iterate_internal_links:
2764 * @pad: the GstPad to get the internal links of.
2766 * Gets an iterator for the pads to which the given pad is linked to inside
2767 * of the parent element.
2769 * Each #GstPad element yielded by the iterator will have its refcount increased,
2770 * so unref after use.
2772 * Free-function: gst_iterator_free
2774 * Returns: (transfer full) (nullable): a new #GstIterator of #GstPad
2775 * or %NULL when the pad does not have an iterator function
2776 * configured. Use gst_iterator_free() after usage.
2779 gst_pad_iterate_internal_links (GstPad * pad)
2781 GstIterator *res = NULL;
2784 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2786 GST_OBJECT_LOCK (pad);
2787 ACQUIRE_PARENT (pad, parent, no_parent);
2788 GST_OBJECT_UNLOCK (pad);
2790 if (GST_PAD_ITERINTLINKFUNC (pad))
2791 res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2793 RELEASE_PARENT (parent);
2800 GST_DEBUG_OBJECT (pad, "no parent");
2801 GST_OBJECT_UNLOCK (pad);
2809 * @forward: (scope call): a #GstPadForwardFunction
2810 * @user_data: user data passed to @forward
2812 * Calls @forward for all internally linked pads of @pad. This function deals with
2813 * dynamically changing internal pads and will make sure that the @forward
2814 * function is only called once for each pad.
2816 * When @forward returns %TRUE, no further pads will be processed.
2818 * Returns: %TRUE if one of the dispatcher functions returned %TRUE.
2821 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2824 gboolean result = FALSE;
2826 gboolean done = FALSE;
2827 GValue item = { 0, };
2828 GList *pushed_pads = NULL;
2830 iter = gst_pad_iterate_internal_links (pad);
2836 switch (gst_iterator_next (iter, &item)) {
2837 case GST_ITERATOR_OK:
2841 intpad = g_value_get_object (&item);
2843 /* if already pushed, skip. FIXME, find something faster to tag pads */
2844 if (intpad == NULL || g_list_find (pushed_pads, intpad)) {
2845 g_value_reset (&item);
2849 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2850 GST_DEBUG_PAD_NAME (intpad));
2851 done = result = forward (intpad, user_data);
2853 pushed_pads = g_list_prepend (pushed_pads, intpad);
2855 g_value_reset (&item);
2858 case GST_ITERATOR_RESYNC:
2859 /* We don't reset the result here because we don't push the event
2860 * again on pads that got the event already and because we need
2861 * to consider the result of the previous pushes */
2862 gst_iterator_resync (iter);
2864 case GST_ITERATOR_ERROR:
2865 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2868 case GST_ITERATOR_DONE:
2873 g_value_unset (&item);
2874 gst_iterator_free (iter);
2876 g_list_free (pushed_pads);
2886 gboolean dispatched;
2890 event_forward_func (GstPad * pad, EventData * data)
2892 /* for each pad we send to, we should ref the event; it's up
2893 * to downstream to unref again when handled. */
2894 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2895 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2897 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2899 data->dispatched = TRUE;
2906 * gst_pad_event_default:
2907 * @pad: a #GstPad to call the default event handler on.
2908 * @parent: (allow-none): the parent of @pad or %NULL
2909 * @event: (transfer full): the #GstEvent to handle.
2911 * Invokes the default event handler for the given pad.
2913 * The EOS event will pause the task associated with @pad before it is forwarded
2914 * to all internally linked pads,
2916 * The event is sent to all pads internally linked to @pad. This function
2917 * takes ownership of @event.
2919 * Returns: %TRUE if the event was sent successfully.
2922 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2924 gboolean result, forward = TRUE;
2926 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2927 g_return_val_if_fail (event != NULL, FALSE);
2929 GST_LOG_OBJECT (pad, "default event handler for event %" GST_PTR_FORMAT,
2932 switch (GST_EVENT_TYPE (event)) {
2933 case GST_EVENT_CAPS:
2934 forward = GST_PAD_IS_PROXY_CAPS (pad);
2945 data.dispatched = FALSE;
2946 data.result = FALSE;
2948 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
2950 /* for sinkpads without a parent element or without internal links, nothing
2951 * will be dispatched but we still want to return TRUE. */
2952 if (data.dispatched)
2953 result = data.result;
2958 gst_event_unref (event);
2963 /* Default accept caps implementation just checks against
2964 * the allowed caps for the pad */
2966 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
2968 /* get the caps and see if it intersects to something not empty */
2969 GstCaps *caps, *allowed;
2972 GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
2974 /* first forward the query to internally linked pads when we are dealing with
2976 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2977 result = gst_pad_proxy_query_accept_caps (pad, query);
2981 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pad,
2982 "fallback ACCEPT_CAPS query, consider implementing a specialized version");
2984 gst_query_parse_accept_caps (query, &caps);
2985 if (GST_PAD_IS_ACCEPT_TEMPLATE (pad))
2986 allowed = gst_pad_get_pad_template_caps (pad);
2988 allowed = gst_pad_query_caps (pad, caps);
2991 if (GST_PAD_IS_ACCEPT_INTERSECT (pad)) {
2992 GST_DEBUG_OBJECT (pad,
2993 "allowed caps intersect %" GST_PTR_FORMAT ", caps %" GST_PTR_FORMAT,
2995 result = gst_caps_can_intersect (caps, allowed);
2997 GST_DEBUG_OBJECT (pad, "allowed caps subset %" GST_PTR_FORMAT ", caps %"
2998 GST_PTR_FORMAT, allowed, caps);
2999 result = gst_caps_is_subset (caps, allowed);
3001 gst_caps_unref (allowed);
3003 GST_DEBUG_OBJECT (pad, "no compatible caps allowed on the pad");
3006 gst_query_set_accept_caps_result (query, result);
3012 /* Default caps implementation */
3014 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
3016 GstCaps *result = NULL, *filter;
3017 GstPadTemplate *templ;
3018 gboolean fixed_caps;
3020 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "query caps %" GST_PTR_FORMAT,
3023 /* first try to proxy if we must */
3024 if (GST_PAD_IS_PROXY_CAPS (pad)) {
3025 if ((gst_pad_proxy_query_caps (pad, query))) {
3030 gst_query_parse_caps (query, &filter);
3032 /* no proxy or it failed, do default handling */
3033 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
3035 GST_OBJECT_LOCK (pad);
3037 /* fixed caps, try the negotiated caps first */
3038 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
3039 if ((result = get_pad_caps (pad)))
3040 goto filter_done_unlock;
3043 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
3044 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
3045 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
3046 goto filter_done_unlock;
3050 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3051 "non-fixed pad caps: trying pad caps");
3052 /* non fixed caps, try the negotiated caps */
3053 if ((result = get_pad_caps (pad)))
3054 goto filter_done_unlock;
3057 /* this almost never happens */
3058 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
3059 result = GST_CAPS_ANY;
3062 GST_OBJECT_UNLOCK (pad);
3064 /* run the filter on the result */
3066 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3067 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
3068 GST_PTR_FORMAT, result, result, filter, filter);
3069 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
3070 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
3073 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3074 "using caps %p %" GST_PTR_FORMAT, result, result);
3075 result = gst_caps_ref (result);
3077 gst_query_set_caps_result (query, result);
3078 gst_caps_unref (result);
3084 /* Default latency implementation */
3088 GstClockTime min, max;
3092 query_latency_default_fold (const GValue * item, GValue * ret,
3095 GstPad *pad = g_value_get_object (item), *peer;
3096 LatencyFoldData *fold_data = user_data;
3098 gboolean res = FALSE;
3100 query = gst_query_new_latency ();
3102 peer = gst_pad_get_peer (pad);
3104 res = gst_pad_peer_query (pad, query);
3106 GST_LOG_OBJECT (pad, "No peer pad found, ignoring this pad");
3111 GstClockTime min, max;
3113 gst_query_parse_latency (query, &live, &min, &max);
3115 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3116 " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
3119 if (min > fold_data->min)
3120 fold_data->min = min;
3122 if (fold_data->max == GST_CLOCK_TIME_NONE)
3123 fold_data->max = max;
3124 else if (max < fold_data->max)
3125 fold_data->max = max;
3127 fold_data->live = TRUE;
3130 GST_DEBUG_OBJECT (pad, "latency query failed");
3131 g_value_set_boolean (ret, FALSE);
3134 gst_query_unref (query);
3136 gst_object_unref (peer);
3142 gst_pad_query_latency_default (GstPad * pad, GstQuery * query)
3145 GstIteratorResult res;
3146 GValue ret = G_VALUE_INIT;
3148 LatencyFoldData fold_data;
3150 it = gst_pad_iterate_internal_links (pad);
3152 GST_DEBUG_OBJECT (pad, "Can't iterate internal links");
3156 g_value_init (&ret, G_TYPE_BOOLEAN);
3159 fold_data.live = FALSE;
3161 fold_data.max = GST_CLOCK_TIME_NONE;
3163 g_value_set_boolean (&ret, TRUE);
3164 res = gst_iterator_fold (it, query_latency_default_fold, &ret, &fold_data);
3166 case GST_ITERATOR_OK:
3167 g_assert_not_reached ();
3169 case GST_ITERATOR_DONE:
3171 case GST_ITERATOR_ERROR:
3172 g_value_set_boolean (&ret, FALSE);
3174 case GST_ITERATOR_RESYNC:
3175 gst_iterator_resync (it);
3178 g_assert_not_reached ();
3181 gst_iterator_free (it);
3183 query_ret = g_value_get_boolean (&ret);
3185 GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
3186 " max:%" G_GINT64_FORMAT, fold_data.live ? "true" : "false",
3187 fold_data.min, fold_data.max);
3189 if (fold_data.min > fold_data.max) {
3190 GST_ERROR_OBJECT (pad, "minimum latency bigger than maximum latency");
3193 gst_query_set_latency (query, fold_data.live, fold_data.min, fold_data.max);
3195 GST_LOG_OBJECT (pad, "latency query failed");
3205 gboolean dispatched;
3209 query_forward_func (GstPad * pad, QueryData * data)
3211 GST_LOG_OBJECT (pad, "query peer %p (%s) of %s:%s",
3212 data->query, GST_QUERY_TYPE_NAME (data->query), GST_DEBUG_PAD_NAME (pad));
3214 data->result |= gst_pad_peer_query (pad, data->query);
3216 data->dispatched = TRUE;
3218 /* stop on first successful reply */
3219 return data->result;
3223 * gst_pad_query_default:
3224 * @pad: a #GstPad to call the default query handler on.
3225 * @parent: (allow-none): the parent of @pad or %NULL
3226 * @query: (transfer none): the #GstQuery to handle.
3228 * Invokes the default query handler for the given pad.
3229 * The query is sent to all pads internally linked to @pad. Note that
3230 * if there are many possible sink pads that are internally linked to
3231 * @pad, only one will be sent the query.
3232 * Multi-sinkpad elements should implement custom query handlers.
3234 * Returns: %TRUE if the query was performed successfully.
3237 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
3239 gboolean forward, ret = FALSE;
3241 switch (GST_QUERY_TYPE (query)) {
3242 case GST_QUERY_SCHEDULING:
3243 forward = GST_PAD_IS_PROXY_SCHEDULING (pad);
3245 case GST_QUERY_ALLOCATION:
3246 forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
3248 case GST_QUERY_ACCEPT_CAPS:
3249 ret = gst_pad_query_accept_caps_default (pad, query);
3252 case GST_QUERY_CAPS:
3253 ret = gst_pad_query_caps_default (pad, query);
3256 case GST_QUERY_LATENCY:
3257 ret = gst_pad_query_latency_default (pad, query);
3260 case GST_QUERY_POSITION:
3261 case GST_QUERY_SEEKING:
3262 case GST_QUERY_FORMATS:
3263 case GST_QUERY_JITTER:
3264 case GST_QUERY_RATE:
3265 case GST_QUERY_CONVERT:
3271 GST_DEBUG_OBJECT (pad, "%sforwarding %p (%s) query", (forward ? "" : "not "),
3272 query, GST_QUERY_TYPE_NAME (query));
3278 data.dispatched = FALSE;
3279 data.result = FALSE;
3281 gst_pad_forward (pad, (GstPadForwardFunction) query_forward_func, &data);
3283 if (data.dispatched) {
3286 /* nothing dispatched, assume drained */
3287 if (GST_QUERY_TYPE (query) == GST_QUERY_DRAIN)
3297 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
3299 GstPad *pad = data->pad;
3300 GstPadProbeInfo *info = data->info;
3301 GstPadProbeType type, flags;
3302 GstPadProbeCallback callback;
3303 GstPadProbeReturn ret;
3304 gpointer original_data;
3306 /* if we have called this callback, do nothing */
3307 if (PROBE_COOKIE (hook) == data->cookie) {
3308 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3309 "hook %lu, cookie %u already called", hook->hook_id,
3310 PROBE_COOKIE (hook));
3314 PROBE_COOKIE (hook) = data->cookie;
3316 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3318 original_data = info->data;
3320 /* one of the data types for non-idle probes */
3321 if ((type & GST_PAD_PROBE_TYPE_IDLE) == 0
3322 && (flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
3324 /* one of the scheduling types */
3325 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
3327 /* one of the blocking types must match */
3328 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
3329 (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
3331 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) == 0 &&
3332 (flags & GST_PAD_PROBE_TYPE_BLOCKING))
3334 /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
3335 if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
3336 (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
3339 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3340 "hook %lu, cookie %u with flags 0x%08x matches", hook->hook_id,
3341 PROBE_COOKIE (hook), flags);
3343 data->marshalled = TRUE;
3345 callback = (GstPadProbeCallback) hook->func;
3346 if (callback == NULL)
3349 info->id = hook->hook_id;
3351 GST_OBJECT_UNLOCK (pad);
3353 ret = callback (pad, info, hook->data);
3355 GST_OBJECT_LOCK (pad);
3357 if (original_data != NULL && info->data == NULL) {
3358 GST_DEBUG_OBJECT (pad, "data item in pad probe info was dropped");
3359 info->type = GST_PAD_PROBE_TYPE_INVALID;
3360 data->dropped = TRUE;
3364 case GST_PAD_PROBE_REMOVE:
3365 /* remove the probe */
3366 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3367 cleanup_hook (pad, hook);
3369 case GST_PAD_PROBE_DROP:
3370 /* need to drop the data, make sure other probes don't get called
3372 GST_DEBUG_OBJECT (pad, "asked to drop item");
3373 info->type = GST_PAD_PROBE_TYPE_INVALID;
3374 data->dropped = TRUE;
3376 case GST_PAD_PROBE_HANDLED:
3377 GST_DEBUG_OBJECT (pad, "probe handled data");
3378 data->handled = TRUE;
3380 case GST_PAD_PROBE_PASS:
3381 /* inform the pad block to let things pass */
3382 GST_DEBUG_OBJECT (pad, "asked to pass item");
3385 case GST_PAD_PROBE_OK:
3386 GST_DEBUG_OBJECT (pad, "probe returned OK");
3389 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3396 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3397 "hook %lu, cookie %u with flags 0x%08x does not match %08x",
3398 hook->hook_id, PROBE_COOKIE (hook), flags, info->type);
3403 /* a probe that does not take or return any data */
3404 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
3406 if (G_UNLIKELY (pad->num_probes)) { \
3407 GstFlowReturn pval = defaultval; \
3408 /* pass NULL as the data item */ \
3409 GstPadProbeInfo info = { mask, 0, NULL, 0, 0 }; \
3410 info.ABI.abi.flow_ret = defaultval; \
3411 ret = do_probe_callbacks (pad, &info, defaultval); \
3412 if (G_UNLIKELY (ret != pval && ret != GST_FLOW_OK)) \
3417 #define PROBE_FULL(pad,mask,data,offs,size,label,handleable,handle_label) \
3419 if (G_UNLIKELY (pad->num_probes)) { \
3420 /* pass the data item */ \
3421 GstPadProbeInfo info = { mask, 0, data, offs, size }; \
3422 info.ABI.abi.flow_ret = GST_FLOW_OK; \
3423 ret = do_probe_callbacks (pad, &info, GST_FLOW_OK); \
3424 /* store the possibly updated data item */ \
3425 data = GST_PAD_PROBE_INFO_DATA (&info); \
3426 /* if something went wrong, exit */ \
3427 if (G_UNLIKELY (ret != GST_FLOW_OK)) { \
3428 if (handleable && ret == GST_FLOW_CUSTOM_SUCCESS_1) { \
3429 ret = info.ABI.abi.flow_ret; \
3430 goto handle_label; \
3437 #define PROBE_PUSH(pad,mask,data,label) \
3438 PROBE_FULL(pad, mask, data, -1, -1, label, FALSE, label);
3439 #define PROBE_HANDLE(pad,mask,data,label,handle_label) \
3440 PROBE_FULL(pad, mask, data, -1, -1, label, TRUE, handle_label);
3441 #define PROBE_PULL(pad,mask,data,offs,size,label) \
3442 PROBE_FULL(pad, mask, data, offs, size, label, FALSE, label);
3444 static GstFlowReturn
3445 do_pad_idle_probe_wait (GstPad * pad)
3447 while (GST_PAD_IS_RUNNING_IDLE_PROBE (pad)) {
3448 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3449 "waiting idle probe to be removed");
3450 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3451 GST_PAD_BLOCK_WAIT (pad);
3452 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3453 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3455 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3456 return GST_FLOW_FLUSHING;
3461 #define PROBE_TYPE_IS_SERIALIZED(i) \
3464 (((i)->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM | \
3465 GST_PAD_PROBE_TYPE_EVENT_FLUSH)) && \
3466 GST_EVENT_IS_SERIALIZED ((i)->data)) \
3468 (((i)->type & GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM) && \
3469 GST_QUERY_IS_SERIALIZED ((i)->data)) \
3471 ((i)->type & (GST_PAD_PROBE_TYPE_BUFFER | \
3472 GST_PAD_PROBE_TYPE_BUFFER_LIST)) \
3476 static GstFlowReturn
3477 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
3478 GstFlowReturn defaultval)
3487 data.handled = FALSE;
3488 data.marshalled = FALSE;
3489 data.dropped = FALSE;
3490 data.cookie = ++pad->priv->probe_cookie;
3493 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
3495 if (is_block && PROBE_TYPE_IS_SERIALIZED (info)) {
3496 if (do_pad_idle_probe_wait (pad) == GST_FLOW_FLUSHING)
3501 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3502 "do probes cookie %u", data.cookie);
3503 cookie = pad->priv->probe_list_cookie;
3505 g_hook_list_marshal (&pad->probes, TRUE,
3506 (GHookMarshaller) probe_hook_marshal, &data);
3508 /* if the list changed, call the new callbacks (they will not have their
3509 * cookie set to data.cookie */
3510 if (cookie != pad->priv->probe_list_cookie) {
3511 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3512 "probe list changed, restarting");
3516 /* the first item that dropped will stop the hooks and then we drop here */
3520 /* If one handler took care of it, let the the item pass */
3525 /* if no handler matched and we are blocking, let the item pass */
3526 if (!data.marshalled && is_block)
3529 /* At this point, all handlers returned either OK or PASS. If one handler
3530 * returned PASS, let the item pass */
3535 while (GST_PAD_IS_BLOCKED (pad)) {
3536 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3537 "we are blocked %d times", pad->num_blocked);
3539 /* we might have released the lock */
3540 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3543 /* now we block the streaming thread. It can be unlocked when we
3544 * deactivate the pad (which will also set the FLUSHING flag) or
3545 * when the pad is unblocked. A flushing event will also unblock
3546 * the pad after setting the FLUSHING flag. */
3547 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3548 "Waiting to be unblocked or set flushing");
3549 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3550 GST_PAD_BLOCK_WAIT (pad);
3551 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3552 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3554 /* if the list changed, call the new callbacks (they will not have their
3555 * cookie set to data.cookie */
3556 if (cookie != pad->priv->probe_list_cookie) {
3557 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3558 "probe list changed, restarting");
3562 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3572 GST_DEBUG_OBJECT (pad, "pad is flushing");
3573 return GST_FLOW_FLUSHING;
3577 GST_DEBUG_OBJECT (pad, "data is dropped");
3578 return GST_FLOW_CUSTOM_SUCCESS;
3582 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3583 GST_DEBUG_OBJECT (pad, "data is passed");
3588 GST_DEBUG_OBJECT (pad, "data was handled");
3589 return GST_FLOW_CUSTOM_SUCCESS_1;
3596 * gst_pad_get_offset:
3599 * Get the offset applied to the running time of @pad. @pad has to be a source
3602 * Returns: the offset.
3605 gst_pad_get_offset (GstPad * pad)
3609 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3611 GST_OBJECT_LOCK (pad);
3612 result = pad->offset;
3613 GST_OBJECT_UNLOCK (pad);
3619 mark_event_not_received (GstPad * pad, PadEvent * ev, gpointer user_data)
3621 ev->received = FALSE;
3626 * gst_pad_set_offset:
3628 * @offset: the offset
3630 * Set the offset that will be applied to the running time of @pad.
3633 gst_pad_set_offset (GstPad * pad, gint64 offset)
3635 g_return_if_fail (GST_IS_PAD (pad));
3637 GST_OBJECT_LOCK (pad);
3638 /* if nothing changed, do nothing */
3639 if (pad->offset == offset)
3642 pad->offset = offset;
3643 GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3645 /* resend all sticky events with updated offset on next buffer push */
3646 events_foreach (pad, mark_event_not_received, NULL);
3647 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3650 GST_OBJECT_UNLOCK (pad);
3657 /* If TRUE and ret is not OK this means
3658 * that pushing the EOS event failed
3662 /* If called for an event this is
3663 * the event that would be pushed
3664 * next. Don't forward sticky events
3665 * that would come after that */
3669 /* should be called with pad LOCK */
3671 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3673 PushStickyData *data = user_data;
3674 GstEvent *event = ev->event;
3677 GST_DEBUG_OBJECT (pad, "event %s was already received",
3678 GST_EVENT_TYPE_NAME (event));
3682 /* If we're called because of an sticky event, only forward
3683 * events that would come before this new event and the
3685 if (data->event && GST_EVENT_IS_STICKY (data->event) &&
3686 GST_EVENT_TYPE (data->event) <= GST_EVENT_SEGMENT &&
3687 GST_EVENT_TYPE (data->event) < GST_EVENT_TYPE (event)) {
3688 data->ret = GST_FLOW_CUSTOM_SUCCESS_1;
3690 data->ret = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
3691 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3694 switch (data->ret) {
3696 ev->received = TRUE;
3697 GST_DEBUG_OBJECT (pad, "event %s marked received",
3698 GST_EVENT_TYPE_NAME (event));
3700 case GST_FLOW_CUSTOM_SUCCESS:
3701 /* we can't assume the event is received when it was dropped */
3702 GST_DEBUG_OBJECT (pad, "event %s was dropped, mark pending",
3703 GST_EVENT_TYPE_NAME (event));
3704 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3705 data->ret = GST_FLOW_OK;
3707 case GST_FLOW_CUSTOM_SUCCESS_1:
3708 /* event was ignored and should be sent later */
3709 GST_DEBUG_OBJECT (pad, "event %s was ignored, mark pending",
3710 GST_EVENT_TYPE_NAME (event));
3711 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3712 data->ret = GST_FLOW_OK;
3714 case GST_FLOW_NOT_LINKED:
3715 /* not linked is not a problem, we are sticky so the event will be
3716 * sent later but only for non-EOS events */
3717 GST_DEBUG_OBJECT (pad, "pad was not linked, mark pending");
3718 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
3719 data->ret = GST_FLOW_OK;
3720 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3723 GST_DEBUG_OBJECT (pad, "result %s, mark pending events",
3724 gst_flow_get_name (data->ret));
3725 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3729 if (data->ret != GST_FLOW_OK && GST_EVENT_TYPE (event) == GST_EVENT_EOS)
3730 data->was_eos = TRUE;
3732 return data->ret == GST_FLOW_OK;
3735 /* check sticky events and push them when needed. should be called
3737 static inline GstFlowReturn
3738 check_sticky (GstPad * pad, GstEvent * event)
3740 PushStickyData data = { GST_FLOW_OK, FALSE, event };
3742 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3743 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3745 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3746 events_foreach (pad, push_sticky, &data);
3748 /* If there's an EOS event we must push it downstream
3749 * even if sending a previous sticky event failed.
3750 * Otherwise the pipeline might wait forever for EOS.
3752 * Only do this if pushing another event than the EOS
3755 if (data.ret != GST_FLOW_OK && !data.was_eos) {
3756 PadEvent *ev = find_event_by_type (pad, GST_EVENT_EOS, 0);
3758 if (ev && !ev->received) {
3759 data.ret = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3760 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3761 /* the event could have been dropped. Because this can only
3762 * happen if the user asked for it, it's not an error */
3763 if (data.ret == GST_FLOW_CUSTOM_SUCCESS)
3764 data.ret = GST_FLOW_OK;
3774 * @pad: a #GstPad to invoke the default query on.
3775 * @query: (transfer none): the #GstQuery to perform.
3777 * Dispatches a query to a pad. The query should have been allocated by the
3778 * caller via one of the type-specific allocation functions. The element that
3779 * the pad belongs to is responsible for filling the query with an appropriate
3780 * response, which should then be parsed with a type-specific query parsing
3783 * Again, the caller is responsible for both the allocation and deallocation of
3784 * the query structure.
3786 * Please also note that some queries might need a running pipeline to work.
3788 * Returns: %TRUE if the query could be performed.
3791 gst_pad_query (GstPad * pad, GstQuery * query)
3794 gboolean res, serialized;
3795 GstPadQueryFunction func;
3796 GstPadProbeType type;
3799 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3800 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3802 if (GST_PAD_IS_SRC (pad)) {
3803 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3804 goto wrong_direction;
3805 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3806 } else if (GST_PAD_IS_SINK (pad)) {
3807 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3808 goto wrong_direction;
3809 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3811 goto unknown_direction;
3813 GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
3814 GST_QUERY_TYPE_NAME (query));
3816 serialized = GST_QUERY_IS_SERIALIZED (query);
3817 if (G_UNLIKELY (serialized))
3818 GST_PAD_STREAM_LOCK (pad);
3820 GST_OBJECT_LOCK (pad);
3821 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3822 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3823 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3825 ACQUIRE_PARENT (pad, parent, no_parent);
3826 GST_OBJECT_UNLOCK (pad);
3828 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3831 res = func (pad, parent, query);
3833 RELEASE_PARENT (parent);
3835 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3836 GST_QUERY_TYPE_NAME (query), res);
3841 GST_OBJECT_LOCK (pad);
3842 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3843 GST_OBJECT_UNLOCK (pad);
3845 if (G_UNLIKELY (serialized))
3846 GST_PAD_STREAM_UNLOCK (pad);
3853 g_warning ("pad %s:%s query %s in wrong direction",
3854 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3859 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3864 GST_DEBUG_OBJECT (pad, "had no parent");
3865 GST_OBJECT_UNLOCK (pad);
3866 if (G_UNLIKELY (serialized))
3867 GST_PAD_STREAM_UNLOCK (pad);
3872 GST_DEBUG_OBJECT (pad, "had no query function");
3873 RELEASE_PARENT (parent);
3874 if (G_UNLIKELY (serialized))
3875 GST_PAD_STREAM_UNLOCK (pad);
3880 GST_DEBUG_OBJECT (pad, "query failed");
3881 if (G_UNLIKELY (serialized))
3882 GST_PAD_STREAM_UNLOCK (pad);
3887 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3888 GST_OBJECT_UNLOCK (pad);
3889 if (G_UNLIKELY (serialized))
3890 GST_PAD_STREAM_UNLOCK (pad);
3892 /* if a probe dropped without handling, we don't sent it further but assume
3893 * that the probe did not answer the query and return FALSE */
3894 if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
3904 * gst_pad_peer_query:
3905 * @pad: a #GstPad to invoke the peer query on.
3906 * @query: (transfer none): the #GstQuery to perform.
3908 * Performs gst_pad_query() on the peer of @pad.
3910 * The caller is responsible for both the allocation and deallocation of
3911 * the query structure.
3913 * Returns: %TRUE if the query could be performed. This function returns %FALSE
3914 * if @pad has no peer.
3917 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3920 GstPadProbeType type;
3921 gboolean res, serialized;
3924 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3925 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3927 if (GST_PAD_IS_SRC (pad)) {
3928 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3929 goto wrong_direction;
3930 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3931 } else if (GST_PAD_IS_SINK (pad)) {
3932 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3933 goto wrong_direction;
3934 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3936 goto unknown_direction;
3938 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3939 GST_QUERY_TYPE_NAME (query));
3941 serialized = GST_QUERY_IS_SERIALIZED (query);
3943 GST_OBJECT_LOCK (pad);
3944 if (GST_PAD_IS_SRC (pad) && serialized) {
3945 /* all serialized queries on the srcpad trigger push of
3947 if (check_sticky (pad, NULL) != GST_FLOW_OK)
3951 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3952 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3953 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3955 peerpad = GST_PAD_PEER (pad);
3956 if (G_UNLIKELY (peerpad == NULL))
3959 gst_object_ref (peerpad);
3960 GST_OBJECT_UNLOCK (pad);
3962 res = gst_pad_query (peerpad, query);
3964 gst_object_unref (peerpad);
3969 GST_OBJECT_LOCK (pad);
3970 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3971 GST_OBJECT_UNLOCK (pad);
3978 g_warning ("pad %s:%s query %s in wrong direction",
3979 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3984 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3989 GST_WARNING_OBJECT (pad, "could not send sticky events");
3990 GST_OBJECT_UNLOCK (pad);
3995 GST_INFO_OBJECT (pad, "pad has no peer");
3996 GST_OBJECT_UNLOCK (pad);
4001 GST_DEBUG_OBJECT (pad, "query failed");
4006 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
4007 GST_OBJECT_UNLOCK (pad);
4009 /* if a probe dropped without handling, we don't sent it further but
4010 * assume that the probe did not answer the query and return FALSE */
4011 if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
4020 /**********************************************************************
4021 * Data passing functions
4024 /* this is the chain function that does not perform the additional argument
4025 * checking for that little extra speed.
4027 static inline GstFlowReturn
4028 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
4032 gboolean handled = FALSE;
4034 GST_PAD_STREAM_LOCK (pad);
4036 GST_OBJECT_LOCK (pad);
4037 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4040 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4043 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4046 #ifndef G_DISABLE_ASSERT
4047 if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4048 if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4050 ":%s:<%s:%s> Got data flow before stream-start event",
4051 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4053 if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4055 ":%s:<%s:%s> Got data flow before segment event",
4056 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4058 pad->priv->last_cookie = pad->priv->events_cookie;
4062 PROBE_HANDLE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped,
4065 PROBE_HANDLE (pad, type, data, probe_stopped, probe_handled);
4067 ACQUIRE_PARENT (pad, parent, no_parent);
4068 GST_OBJECT_UNLOCK (pad);
4070 /* NOTE: we read the chainfunc unlocked.
4071 * we cannot hold the lock for the pad so we might send
4072 * the data to the wrong function. This is not really a
4073 * problem since functions are assigned at creation time
4074 * and don't change that often... */
4075 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
4076 GstPadChainFunction chainfunc;
4078 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4081 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4082 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
4083 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
4085 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
4087 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4088 "called chainfunction &%s with buffer %p, returned %s",
4089 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
4091 GstPadChainListFunction chainlistfunc;
4093 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4096 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4097 "calling chainlistfunction &%s",
4098 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4100 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
4102 GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4103 "called chainlistfunction &%s, returned %s",
4104 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4107 RELEASE_PARENT (parent);
4109 GST_PAD_STREAM_UNLOCK (pad);
4116 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4117 "chaining, but pad was flushing");
4118 GST_OBJECT_UNLOCK (pad);
4119 GST_PAD_STREAM_UNLOCK (pad);
4120 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4121 return GST_FLOW_FLUSHING;
4125 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
4126 GST_OBJECT_UNLOCK (pad);
4127 GST_PAD_STREAM_UNLOCK (pad);
4128 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4129 return GST_FLOW_EOS;
4133 g_critical ("chain on pad %s:%s but it was not in push mode",
4134 GST_DEBUG_PAD_NAME (pad));
4135 GST_OBJECT_UNLOCK (pad);
4136 GST_PAD_STREAM_UNLOCK (pad);
4137 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4138 return GST_FLOW_ERROR;
4145 GST_OBJECT_UNLOCK (pad);
4146 GST_PAD_STREAM_UNLOCK (pad);
4147 /* We unref the buffer, except if the probe handled it (CUSTOM_SUCCESS_1) */
4149 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4152 case GST_FLOW_CUSTOM_SUCCESS:
4153 case GST_FLOW_CUSTOM_SUCCESS_1:
4154 GST_DEBUG_OBJECT (pad, "dropped or handled buffer");
4158 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4165 GST_DEBUG_OBJECT (pad, "No parent when chaining %" GST_PTR_FORMAT, data);
4166 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4167 GST_OBJECT_UNLOCK (pad);
4168 GST_PAD_STREAM_UNLOCK (pad);
4169 return GST_FLOW_FLUSHING;
4173 RELEASE_PARENT (parent);
4174 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4175 g_critical ("chain on pad %s:%s but it has no chainfunction",
4176 GST_DEBUG_PAD_NAME (pad));
4177 GST_PAD_STREAM_UNLOCK (pad);
4178 return GST_FLOW_NOT_SUPPORTED;
4184 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4185 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
4188 * Chain a buffer to @pad.
4190 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4192 * If the buffer type is not acceptable for @pad (as negotiated with a
4193 * preceding GST_EVENT_CAPS event), this function returns
4194 * #GST_FLOW_NOT_NEGOTIATED.
4196 * The function proceeds calling the chain function installed on @pad (see
4197 * gst_pad_set_chain_function()) and the return value of that function is
4198 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4201 * In all cases, success or failure, the caller loses its reference to @buffer
4202 * after calling this function.
4204 * Returns: a #GstFlowReturn from the pad.
4209 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4211 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4212 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4213 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4215 return gst_pad_chain_data_unchecked (pad,
4216 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4219 static GstFlowReturn
4220 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
4221 GstBufferList * list)
4227 GST_INFO_OBJECT (pad, "chaining each buffer in list individually");
4229 len = gst_buffer_list_length (list);
4232 for (i = 0; i < len; i++) {
4233 buffer = gst_buffer_list_get (list, i);
4235 gst_pad_chain_data_unchecked (pad,
4236 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
4237 gst_buffer_ref (buffer));
4238 if (ret != GST_FLOW_OK)
4241 gst_buffer_list_unref (list);
4247 * gst_pad_chain_list:
4248 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4249 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
4252 * Chain a bufferlist to @pad.
4254 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
4256 * If @pad was not negotiated properly with a CAPS event, this function
4257 * returns #GST_FLOW_NOT_NEGOTIATED.
4259 * The function proceeds calling the chainlist function installed on @pad (see
4260 * gst_pad_set_chain_list_function()) and the return value of that function is
4261 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4262 * chainlist function.
4264 * In all cases, success or failure, the caller loses its reference to @list
4265 * after calling this function.
4269 * Returns: a #GstFlowReturn from the pad.
4272 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4274 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4275 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4276 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4278 return gst_pad_chain_data_unchecked (pad,
4279 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4282 static GstFlowReturn
4283 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
4287 gboolean handled = FALSE;
4289 GST_OBJECT_LOCK (pad);
4290 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4293 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4296 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
4299 #ifndef G_DISABLE_ASSERT
4300 if (G_UNLIKELY (pad->priv->last_cookie != pad->priv->events_cookie)) {
4301 if (!find_event_by_type (pad, GST_EVENT_STREAM_START, 0)) {
4303 ":%s:<%s:%s> Got data flow before stream-start event",
4304 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4306 if (!find_event_by_type (pad, GST_EVENT_SEGMENT, 0)) {
4308 ":%s:<%s:%s> Got data flow before segment event",
4309 G_STRFUNC, GST_DEBUG_PAD_NAME (pad));
4311 pad->priv->last_cookie = pad->priv->events_cookie;
4315 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4318 /* do block probes */
4319 PROBE_HANDLE (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped,
4322 /* recheck sticky events because the probe might have cause a relink */
4323 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4326 /* do post-blocking probes */
4327 PROBE_HANDLE (pad, type, data, probe_stopped, probe_handled);
4329 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4332 /* take ref to peer pad before releasing the lock */
4333 gst_object_ref (peer);
4335 GST_OBJECT_UNLOCK (pad);
4337 ret = gst_pad_chain_data_unchecked (peer, type, data);
4340 gst_object_unref (peer);
4342 GST_OBJECT_LOCK (pad);
4343 pad->ABI.abi.last_flowret = ret;
4345 if (pad->priv->using == 0) {
4346 /* pad is not active anymore, trigger idle callbacks */
4347 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4348 probe_stopped, ret);
4350 GST_OBJECT_UNLOCK (pad);
4354 /* ERROR recovery here */
4358 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4359 "pushing, but pad was flushing");
4360 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4361 GST_OBJECT_UNLOCK (pad);
4362 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4363 return GST_FLOW_FLUSHING;
4367 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
4368 pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4369 GST_OBJECT_UNLOCK (pad);
4370 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4371 return GST_FLOW_EOS;
4375 g_critical ("pushing on pad %s:%s but it was not activated in push mode",
4376 GST_DEBUG_PAD_NAME (pad));
4377 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4378 GST_OBJECT_UNLOCK (pad);
4379 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4380 return GST_FLOW_ERROR;
4384 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4385 "error pushing events, return %s", gst_flow_get_name (ret));
4386 pad->ABI.abi.last_flowret = ret;
4387 GST_OBJECT_UNLOCK (pad);
4388 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4396 GST_OBJECT_UNLOCK (pad);
4397 if (data != NULL && !handled)
4398 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4401 case GST_FLOW_CUSTOM_SUCCESS:
4402 case GST_FLOW_CUSTOM_SUCCESS_1:
4403 GST_DEBUG_OBJECT (pad, "dropped or handled buffer");
4407 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
4410 pad->ABI.abi.last_flowret = ret;
4415 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4416 "pushing, but it was not linked");
4417 pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4418 GST_OBJECT_UNLOCK (pad);
4419 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
4420 return GST_FLOW_NOT_LINKED;
4426 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4427 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
4430 * Pushes a buffer to the peer of @pad.
4432 * This function will call installed block probes before triggering any
4433 * installed data probes.
4435 * The function proceeds calling gst_pad_chain() on the peer pad and returns
4436 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4439 * In all cases, success or failure, the caller loses its reference to @buffer
4440 * after calling this function.
4442 * Returns: a #GstFlowReturn from the peer pad.
4447 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4451 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4452 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4453 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4455 GST_TRACER_PAD_PUSH_PRE (pad, buffer);
4456 res = gst_pad_push_data (pad,
4457 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
4458 GST_TRACER_PAD_PUSH_POST (pad, res);
4463 * gst_pad_push_list:
4464 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4465 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
4468 * Pushes a buffer list to the peer of @pad.
4470 * This function will call installed block probes before triggering any
4471 * installed data probes.
4473 * The function proceeds calling the chain function on the peer pad and returns
4474 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4475 * be returned. If the peer pad does not have any installed chainlist function
4476 * every group buffer of the list will be merged into a normal #GstBuffer and
4477 * chained via gst_pad_chain().
4479 * In all cases, success or failure, the caller loses its reference to @list
4480 * after calling this function.
4482 * Returns: a #GstFlowReturn from the peer pad.
4487 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4491 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4492 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4493 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4495 GST_TRACER_PAD_PUSH_LIST_PRE (pad, list);
4496 res = gst_pad_push_data (pad,
4497 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
4498 GST_TRACER_PAD_PUSH_LIST_POST (pad, res);
4502 static GstFlowReturn
4503 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4504 GstBuffer ** buffer)
4507 GstPadGetRangeFunction getrangefunc;
4511 GST_PAD_STREAM_LOCK (pad);
4513 GST_OBJECT_LOCK (pad);
4514 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4517 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4520 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4525 /* when one of the probes returns DROPPED, probe_stopped will be called
4526 * and we skip calling the getrange function, res_buf should then contain a
4527 * valid result buffer */
4528 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4529 res_buf, offset, size, probe_stopped);
4531 /* recheck sticky events because the probe might have cause a relink */
4532 if (G_UNLIKELY ((ret = check_sticky (pad, NULL))) != GST_FLOW_OK)
4535 ACQUIRE_PARENT (pad, parent, no_parent);
4536 GST_OBJECT_UNLOCK (pad);
4538 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4541 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4542 "calling getrangefunc %s, offset %"
4543 G_GUINT64_FORMAT ", size %u",
4544 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4546 ret = getrangefunc (pad, parent, offset, size, &res_buf);
4548 RELEASE_PARENT (parent);
4550 GST_OBJECT_LOCK (pad);
4551 if (G_UNLIKELY (ret != GST_FLOW_OK))
4552 goto get_range_failed;
4554 /* can only fire the signal if we have a valid buffer */
4556 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4557 res_buf, offset, size, probe_stopped_unref);
4558 pad->ABI.abi.last_flowret = ret;
4559 GST_OBJECT_UNLOCK (pad);
4561 GST_PAD_STREAM_UNLOCK (pad);
4570 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4571 "getrange, but pad was flushing");
4572 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4573 GST_OBJECT_UNLOCK (pad);
4574 GST_PAD_STREAM_UNLOCK (pad);
4575 return GST_FLOW_FLUSHING;
4579 g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4580 GST_DEBUG_PAD_NAME (pad));
4581 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4582 GST_OBJECT_UNLOCK (pad);
4583 GST_PAD_STREAM_UNLOCK (pad);
4584 return GST_FLOW_ERROR;
4588 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4589 pad->ABI.abi.last_flowret = ret;
4590 GST_OBJECT_UNLOCK (pad);
4591 GST_PAD_STREAM_UNLOCK (pad);
4596 GST_DEBUG_OBJECT (pad, "no parent");
4597 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4598 GST_OBJECT_UNLOCK (pad);
4599 GST_PAD_STREAM_UNLOCK (pad);
4600 return GST_FLOW_FLUSHING;
4604 g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4605 GST_DEBUG_PAD_NAME (pad));
4606 RELEASE_PARENT (parent);
4607 GST_PAD_STREAM_UNLOCK (pad);
4608 return GST_FLOW_NOT_SUPPORTED;
4612 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4613 "probe returned %s", gst_flow_get_name (ret));
4614 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4616 /* the probe filled the buffer and asks us to not call the getrange
4617 * anymore, we continue with the post probes then. */
4618 GST_DEBUG_OBJECT (pad, "handled buffer");
4622 /* no buffer, we are EOS */
4623 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4627 pad->ABI.abi.last_flowret = ret;
4628 GST_OBJECT_UNLOCK (pad);
4629 GST_PAD_STREAM_UNLOCK (pad);
4633 probe_stopped_unref:
4635 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4636 "probe returned %s", gst_flow_get_name (ret));
4637 /* if we drop here, it signals EOS */
4638 if (ret == GST_FLOW_CUSTOM_SUCCESS)
4640 pad->ABI.abi.last_flowret = ret;
4641 GST_OBJECT_UNLOCK (pad);
4642 GST_PAD_STREAM_UNLOCK (pad);
4643 if (*buffer == NULL)
4644 gst_buffer_unref (res_buf);
4649 pad->ABI.abi.last_flowret = ret;
4650 GST_OBJECT_UNLOCK (pad);
4651 GST_PAD_STREAM_UNLOCK (pad);
4652 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4653 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4654 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4660 * gst_pad_get_range:
4661 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4662 * @offset: The start offset of the buffer
4663 * @size: The length of the buffer
4664 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4665 * returns #GST_FLOW_ERROR if %NULL.
4667 * When @pad is flushing this function returns #GST_FLOW_FLUSHING
4668 * immediately and @buffer is %NULL.
4670 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4671 * description of a getrange function. If @pad has no getrange function
4672 * installed (see gst_pad_set_getrange_function()) this function returns
4673 * #GST_FLOW_NOT_SUPPORTED.
4675 * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4676 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4677 * must be freed with gst_buffer_unref() after usage.
4679 * When @buffer points to a variable that points to a valid #GstBuffer, the
4680 * buffer will be filled with the result data when this function returns
4681 * #GST_FLOW_OK. If the provided buffer is larger than @size, only
4682 * @size bytes will be filled in the result buffer and its size will be updated
4685 * Note that less than @size bytes can be returned in @buffer when, for example,
4686 * an EOS condition is near or when @buffer is not large enough to hold @size
4687 * bytes. The caller should check the result buffer size to get the result size.
4689 * When this function returns any other result value than #GST_FLOW_OK, @buffer
4690 * will be unchanged.
4692 * This is a lowlevel function. Usually gst_pad_pull_range() is used.
4694 * Returns: a #GstFlowReturn from the pad.
4699 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4700 GstBuffer ** buffer)
4702 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4703 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4704 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4705 g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4706 && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4708 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4712 * gst_pad_pull_range:
4713 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4714 * @offset: The start offset of the buffer
4715 * @size: The length of the buffer
4716 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4717 * GST_FLOW_ERROR if %NULL.
4719 * Pulls a @buffer from the peer pad or fills up a provided buffer.
4721 * This function will first trigger the pad block signal if it was
4724 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4725 * function returns the result of gst_pad_get_range() on the peer pad.
4726 * See gst_pad_get_range() for a list of return values and for the
4727 * semantics of the arguments of this function.
4729 * If @buffer points to a variable holding %NULL, a valid new #GstBuffer will be
4730 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4731 * must be freed with gst_buffer_unref() after usage. When this function
4732 * returns any other result value, @buffer will still point to %NULL.
4734 * When @buffer points to a variable that points to a valid #GstBuffer, the
4735 * buffer will be filled with the result data when this function returns
4736 * #GST_FLOW_OK. When this function returns any other result value,
4737 * @buffer will be unchanged. If the provided buffer is larger than @size, only
4738 * @size bytes will be filled in the result buffer and its size will be updated
4741 * Note that less than @size bytes can be returned in @buffer when, for example,
4742 * an EOS condition is near or when @buffer is not large enough to hold @size
4743 * bytes. The caller should check the result buffer size to get the result size.
4745 * Returns: a #GstFlowReturn from the peer pad.
4750 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4751 GstBuffer ** buffer)
4757 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4758 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4759 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4760 g_return_val_if_fail (*buffer == NULL || (GST_IS_BUFFER (*buffer)
4761 && gst_buffer_get_size (*buffer) >= size), GST_FLOW_ERROR);
4763 GST_OBJECT_LOCK (pad);
4764 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4767 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4772 /* when one of the probes returns DROPPED, probe_stopped will be
4773 * called and we skip calling the peer getrange function. *buffer should then
4774 * contain a valid buffer */
4775 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4776 res_buf, offset, size, probe_stopped);
4778 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4781 gst_object_ref (peer);
4783 GST_OBJECT_UNLOCK (pad);
4785 ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
4787 gst_object_unref (peer);
4789 GST_OBJECT_LOCK (pad);
4791 pad->ABI.abi.last_flowret = ret;
4792 if (pad->priv->using == 0) {
4793 /* pad is not active anymore, trigger idle callbacks */
4794 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4795 probe_stopped_unref, ret);
4798 if (G_UNLIKELY (ret != GST_FLOW_OK))
4799 goto pull_range_failed;
4802 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4803 res_buf, offset, size, probe_stopped_unref);
4805 GST_OBJECT_UNLOCK (pad);
4811 /* ERROR recovery here */
4814 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4815 "pullrange, but pad was flushing");
4816 pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
4817 GST_OBJECT_UNLOCK (pad);
4818 return GST_FLOW_FLUSHING;
4822 g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
4823 GST_DEBUG_PAD_NAME (pad));
4824 pad->ABI.abi.last_flowret = GST_FLOW_ERROR;
4825 GST_OBJECT_UNLOCK (pad);
4826 return GST_FLOW_ERROR;
4830 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4831 gst_flow_get_name (ret));
4832 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4834 /* the probe filled the buffer and asks us to not forward to the peer
4835 * anymore, we continue with the post probes then */
4836 GST_DEBUG_OBJECT (pad, "handled buffer");
4840 /* no buffer, we are EOS then */
4841 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4845 pad->ABI.abi.last_flowret = ret;
4846 GST_OBJECT_UNLOCK (pad);
4851 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4852 "pulling range, but it was not linked");
4853 pad->ABI.abi.last_flowret = GST_FLOW_NOT_LINKED;
4854 GST_OBJECT_UNLOCK (pad);
4855 return GST_FLOW_NOT_LINKED;
4859 pad->ABI.abi.last_flowret = ret;
4860 GST_OBJECT_UNLOCK (pad);
4861 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4862 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4863 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4866 probe_stopped_unref:
4868 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4869 "post probe returned %s", gst_flow_get_name (ret));
4871 /* if we drop here, it signals EOS */
4872 if (ret == GST_FLOW_CUSTOM_SUCCESS)
4875 pad->ABI.abi.last_flowret = ret;
4876 GST_OBJECT_UNLOCK (pad);
4878 if (*buffer == NULL)
4879 gst_buffer_unref (res_buf);
4884 /* must be called with pad object lock */
4885 static GstFlowReturn
4886 store_sticky_event (GstPad * pad, GstEvent * event)
4891 gboolean res = FALSE;
4892 const gchar *name = NULL;
4893 gboolean insert = TRUE;
4895 type = GST_EVENT_TYPE (event);
4897 /* Store all sticky events except SEGMENT/EOS when we're flushing,
4898 * otherwise they can be dropped and nothing would ever resend them.
4899 * Only do that for activated pads though, everything else is a bug!
4901 if (G_UNLIKELY (GST_PAD_MODE (pad) == GST_PAD_MODE_NONE
4902 || (GST_PAD_IS_FLUSHING (pad) && (type == GST_EVENT_SEGMENT
4903 || type == GST_EVENT_EOS))))
4906 /* Unset the EOS flag when received STREAM_START event, so pad can
4907 * store sticky event and then push it later */
4908 if (type == GST_EVENT_STREAM_START) {
4909 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4910 remove_event_by_type (pad, GST_EVENT_EOS);
4911 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4914 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4917 if (type & GST_EVENT_TYPE_STICKY_MULTI)
4918 name = gst_structure_get_name (gst_event_get_structure (event));
4920 events = pad->priv->events;
4923 for (i = 0; i < len; i++) {
4924 PadEvent *ev = &g_array_index (events, PadEvent, i);
4926 if (ev->event == NULL)
4929 if (type == GST_EVENT_TYPE (ev->event)) {
4930 /* matching types, check matching name if needed */
4931 if (name && !gst_event_has_name (ev->event, name))
4935 if ((res = gst_event_replace (&ev->event, event)))
4936 ev->received = FALSE;
4942 if (type < GST_EVENT_TYPE (ev->event) || (type != GST_EVENT_TYPE (ev->event)
4943 && GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS)) {
4944 /* STREAM_START, CAPS and SEGMENT must be delivered in this order. By
4945 * storing the sticky ordered we can check that this is respected. */
4946 if (G_UNLIKELY (GST_EVENT_TYPE (ev->event) <= GST_EVENT_SEGMENT
4947 || GST_EVENT_TYPE (ev->event) == GST_EVENT_EOS))
4949 ":%s:<%s:%s> Sticky event misordering, got '%s' before '%s'",
4950 G_STRFUNC, GST_DEBUG_PAD_NAME (pad),
4951 gst_event_type_get_name (GST_EVENT_TYPE (ev->event)),
4952 gst_event_type_get_name (type));
4958 ev.event = gst_event_ref (event);
4959 ev.received = FALSE;
4960 g_array_insert_val (events, i, ev);
4965 pad->priv->events_cookie++;
4966 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4968 GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4970 switch (GST_EVENT_TYPE (event)) {
4971 case GST_EVENT_CAPS:
4972 GST_OBJECT_UNLOCK (pad);
4974 GST_DEBUG_OBJECT (pad, "notify caps");
4975 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4977 GST_OBJECT_LOCK (pad);
4983 if (type == GST_EVENT_EOS) {
4984 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
4985 pad->ABI.abi.last_flowret = GST_FLOW_EOS;
4988 return GST_PAD_IS_FLUSHING (pad) ? GST_FLOW_FLUSHING : GST_FLOW_OK;
4993 GST_DEBUG_OBJECT (pad, "pad is flushing");
4994 return GST_FLOW_FLUSHING;
4998 GST_DEBUG_OBJECT (pad, "pad is EOS");
4999 return GST_FLOW_EOS;
5004 * gst_pad_store_sticky_event:
5006 * @event: a #GstEvent
5008 * Store the sticky @event on @pad
5010 * Returns: #GST_FLOW_OK on success, #GST_FLOW_FLUSHING when the pad
5011 * was flushing or #GST_FLOW_EOS when the pad was EOS.
5016 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event)
5020 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5021 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5023 GST_OBJECT_LOCK (pad);
5024 ret = store_sticky_event (pad, event);
5025 GST_OBJECT_UNLOCK (pad);
5031 sticky_changed (GstPad * pad, PadEvent * ev, gpointer user_data)
5033 PushStickyData *data = user_data;
5035 /* Forward all sticky events before our current one that are pending */
5036 if (ev->event != data->event
5037 && GST_EVENT_TYPE (ev->event) < GST_EVENT_TYPE (data->event))
5038 return push_sticky (pad, ev, data);
5043 /* should be called with pad LOCK */
5044 static GstFlowReturn
5045 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
5046 GstPadProbeType type)
5050 GstEventType event_type;
5052 /* pass the adjusted event on. We need to do this even if
5053 * there is no peer pad because of the probes. */
5054 event = apply_pad_offset (pad, event, GST_PAD_IS_SINK (pad));
5056 /* Two checks to be made:
5057 * . (un)set the FLUSHING flag for flushing events,
5058 * . handle pad blocking */
5059 event_type = GST_EVENT_TYPE (event);
5060 switch (event_type) {
5061 case GST_EVENT_FLUSH_START:
5062 GST_PAD_SET_FLUSHING (pad);
5064 GST_PAD_BLOCK_BROADCAST (pad);
5065 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5067 case GST_EVENT_FLUSH_STOP:
5068 if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5071 GST_PAD_UNSET_FLUSHING (pad);
5073 /* Remove sticky EOS events */
5074 GST_LOG_OBJECT (pad, "Removing pending EOS events");
5075 remove_event_by_type (pad, GST_EVENT_EOS);
5076 remove_event_by_type (pad, GST_EVENT_SEGMENT);
5077 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5078 pad->ABI.abi.last_flowret = GST_FLOW_OK;
5080 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
5084 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5087 /* No need to check for EOS here as either the caller (gst_pad_push_event())
5088 * checked already or this is called as part of pushing sticky events,
5089 * in which case we still want to forward the EOS event downstream.
5092 switch (GST_EVENT_TYPE (event)) {
5093 case GST_EVENT_RECONFIGURE:
5094 if (GST_PAD_IS_SINK (pad))
5095 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5100 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
5101 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5106 /* send probes after modifying the events above */
5107 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5109 /* recheck sticky events because the probe might have cause a relink */
5110 if (GST_PAD_HAS_PENDING_EVENTS (pad) && GST_PAD_IS_SRC (pad)
5111 && (GST_EVENT_IS_SERIALIZED (event)
5112 || GST_EVENT_IS_STICKY (event))) {
5113 PushStickyData data = { GST_FLOW_OK, FALSE, event };
5114 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5116 /* Push all sticky events before our current one
5117 * that have changed */
5118 events_foreach (pad, sticky_changed, &data);
5121 /* now check the peer pad */
5122 peerpad = GST_PAD_PEER (pad);
5123 if (peerpad == NULL)
5126 gst_object_ref (peerpad);
5128 GST_OBJECT_UNLOCK (pad);
5130 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
5131 event, gst_event_type_get_name (event_type), peerpad);
5133 ret = gst_pad_send_event_unchecked (peerpad, event, type);
5135 /* Note: we gave away ownership of the event at this point but we can still
5136 * print the old pointer */
5137 GST_LOG_OBJECT (pad,
5138 "sent event %p (%s) to peerpad %" GST_PTR_FORMAT ", ret %s", event,
5139 gst_event_type_get_name (event_type), peerpad, gst_flow_get_name (ret));
5141 gst_object_unref (peerpad);
5143 GST_OBJECT_LOCK (pad);
5145 if (pad->priv->using == 0) {
5146 /* pad is not active anymore, trigger idle callbacks */
5147 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
5148 idle_probe_stopped, ret);
5152 /* ERROR handling */
5155 GST_DEBUG_OBJECT (pad, "We're flushing");
5156 gst_event_unref (event);
5157 return GST_FLOW_FLUSHING;
5161 GST_DEBUG_OBJECT (pad, "flush-stop on inactive pad");
5162 gst_event_unref (event);
5163 return GST_FLOW_FLUSHING;
5167 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5168 if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
5169 gst_event_unref (event);
5172 case GST_FLOW_CUSTOM_SUCCESS_1:
5173 GST_DEBUG_OBJECT (pad, "handled event");
5175 case GST_FLOW_CUSTOM_SUCCESS:
5176 GST_DEBUG_OBJECT (pad, "dropped event");
5179 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5186 GST_DEBUG_OBJECT (pad, "Dropping event %s because pad is not linked",
5187 gst_event_type_get_name (GST_EVENT_TYPE (event)));
5188 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
5189 gst_event_unref (event);
5191 /* unlinked pads should not influence latency configuration */
5192 if (event_type == GST_EVENT_LATENCY)
5195 return GST_FLOW_NOT_LINKED;
5199 GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
5205 * gst_pad_push_event:
5206 * @pad: a #GstPad to push the event to.
5207 * @event: (transfer full): the #GstEvent to send to the pad.
5209 * Sends the event to the peer of the given pad. This function is
5210 * mainly used by elements to send events to their peer
5213 * This function takes ownership of the provided event so you should
5214 * gst_event_ref() it if you want to reuse the event after this call.
5216 * Returns: %TRUE if the event was handled.
5221 gst_pad_push_event (GstPad * pad, GstEvent * event)
5223 gboolean res = FALSE;
5224 GstPadProbeType type;
5225 gboolean sticky, serialized;
5227 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5228 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
5230 if (GST_PAD_IS_SRC (pad)) {
5231 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5232 goto wrong_direction;
5233 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5234 } else if (GST_PAD_IS_SINK (pad)) {
5235 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5236 goto wrong_direction;
5237 /* events pushed on sinkpad never are sticky */
5238 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5240 goto unknown_direction;
5242 GST_OBJECT_LOCK (pad);
5243 sticky = GST_EVENT_IS_STICKY (event);
5244 serialized = GST_EVENT_IS_SERIALIZED (event);
5247 /* srcpad sticky events are stored immediately, the received flag is set
5248 * to FALSE and will be set to TRUE when we can successfully push the
5249 * event to the peer pad */
5250 switch (store_sticky_event (pad, event)) {
5251 case GST_FLOW_FLUSHING:
5259 if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
5260 /* all serialized or sticky events on the srcpad trigger push of
5262 res = (check_sticky (pad, event) == GST_FLOW_OK);
5267 /* other events are pushed right away */
5268 ret = gst_pad_push_event_unchecked (pad, event, type);
5269 /* dropped events by a probe are not an error */
5270 res = (ret == GST_FLOW_OK || ret == GST_FLOW_CUSTOM_SUCCESS
5271 || ret == GST_FLOW_CUSTOM_SUCCESS_1);
5273 /* Errors in sticky event pushing are no problem and ignored here
5274 * as they will cause more meaningful errors during data flow.
5275 * For EOS events, that are not followed by data flow, we still
5276 * return FALSE here though.
5278 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
5280 gst_event_unref (event);
5282 GST_OBJECT_UNLOCK (pad);
5286 /* ERROR handling */
5289 g_warning ("pad %s:%s pushing %s event in wrong direction",
5290 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5291 gst_event_unref (event);
5296 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5297 gst_event_unref (event);
5302 GST_DEBUG_OBJECT (pad, "We're flushing");
5303 GST_OBJECT_UNLOCK (pad);
5304 gst_event_unref (event);
5309 GST_DEBUG_OBJECT (pad, "We're EOS");
5310 GST_OBJECT_UNLOCK (pad);
5311 gst_event_unref (event);
5316 /* Check if we can call the event function with the given event */
5317 static GstFlowReturn
5318 pre_eventfunc_check (GstPad * pad, GstEvent * event)
5322 switch (GST_EVENT_TYPE (event)) {
5323 case GST_EVENT_CAPS:
5325 /* backwards compatibility mode for caps */
5326 gst_event_parse_caps (event, &caps);
5328 if (!gst_pad_query_accept_caps (pad, caps))
5340 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
5341 "caps %" GST_PTR_FORMAT " not accepted", caps);
5342 return GST_FLOW_NOT_NEGOTIATED;
5346 static GstFlowReturn
5347 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
5348 GstPadProbeType type)
5351 GstEventType event_type;
5352 gboolean serialized, need_unlock = FALSE, sticky;
5353 GstPadEventFunction eventfunc;
5356 GST_OBJECT_LOCK (pad);
5358 event = apply_pad_offset (pad, event, GST_PAD_IS_SRC (pad));
5360 if (GST_PAD_IS_SINK (pad))
5361 serialized = GST_EVENT_IS_SERIALIZED (event);
5364 sticky = GST_EVENT_IS_STICKY (event);
5365 event_type = GST_EVENT_TYPE (event);
5366 switch (event_type) {
5367 case GST_EVENT_FLUSH_START:
5368 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5369 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
5371 /* can't even accept a flush begin event when flushing */
5372 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5375 GST_PAD_SET_FLUSHING (pad);
5376 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
5378 case GST_EVENT_FLUSH_STOP:
5379 /* we can't accept flush-stop on inactive pads else the flushing flag
5380 * would be cleared and it would look like the pad can accept data.
5381 * Also, some elements restart a streaming thread in flush-stop which we
5382 * can't allow on inactive pads */
5383 if (G_UNLIKELY (!GST_PAD_IS_ACTIVE (pad)))
5386 GST_PAD_UNSET_FLUSHING (pad);
5387 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
5388 /* Remove pending EOS events */
5389 GST_LOG_OBJECT (pad, "Removing pending EOS and SEGMENT events");
5390 remove_event_by_type (pad, GST_EVENT_EOS);
5391 remove_event_by_type (pad, GST_EVENT_SEGMENT);
5392 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5393 pad->ABI.abi.last_flowret = GST_FLOW_OK;
5395 GST_OBJECT_UNLOCK (pad);
5396 /* grab stream lock */
5397 GST_PAD_STREAM_LOCK (pad);
5399 GST_OBJECT_LOCK (pad);
5400 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5403 case GST_EVENT_RECONFIGURE:
5404 if (GST_PAD_IS_SRC (pad))
5405 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
5407 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
5408 "have event type %" GST_PTR_FORMAT, event);
5410 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5413 switch (event_type) {
5414 case GST_EVENT_STREAM_START:
5415 /* Remove sticky EOS events */
5416 GST_LOG_OBJECT (pad, "Removing pending EOS events");
5417 remove_event_by_type (pad, GST_EVENT_EOS);
5418 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
5425 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5428 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
5429 GST_OBJECT_UNLOCK (pad);
5430 GST_PAD_STREAM_LOCK (pad);
5432 GST_OBJECT_LOCK (pad);
5433 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
5436 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
5442 /* now do the probe */
5444 type | GST_PAD_PROBE_TYPE_PUSH |
5445 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
5447 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
5449 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
5452 ACQUIRE_PARENT (pad, parent, no_parent);
5453 GST_OBJECT_UNLOCK (pad);
5455 ret = pre_eventfunc_check (pad, event);
5456 if (G_UNLIKELY (ret != GST_FLOW_OK))
5457 goto precheck_failed;
5460 gst_event_ref (event);
5462 if (eventfunc (pad, parent, event)) {
5465 /* something went wrong */
5466 switch (event_type) {
5467 case GST_EVENT_CAPS:
5468 ret = GST_FLOW_NOT_NEGOTIATED;
5471 ret = GST_FLOW_ERROR;
5475 RELEASE_PARENT (parent);
5477 GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
5480 if (ret == GST_FLOW_OK) {
5481 GST_OBJECT_LOCK (pad);
5482 /* after the event function accepted the event, we can store the sticky
5483 * event on the pad */
5484 switch (store_sticky_event (pad, event)) {
5485 case GST_FLOW_FLUSHING:
5492 GST_OBJECT_UNLOCK (pad);
5494 gst_event_unref (event);
5498 GST_PAD_STREAM_UNLOCK (pad);
5502 /* ERROR handling */
5505 GST_OBJECT_UNLOCK (pad);
5507 GST_PAD_STREAM_UNLOCK (pad);
5508 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5509 "Received event on flushing pad. Discarding");
5510 gst_event_unref (event);
5511 return GST_FLOW_FLUSHING;
5515 GST_OBJECT_UNLOCK (pad);
5517 GST_PAD_STREAM_UNLOCK (pad);
5518 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5519 "Received flush-stop on inactive pad. Discarding");
5520 gst_event_unref (event);
5521 return GST_FLOW_FLUSHING;
5525 GST_OBJECT_UNLOCK (pad);
5527 GST_PAD_STREAM_UNLOCK (pad);
5528 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5529 "Received event on EOS pad. Discarding");
5530 gst_event_unref (event);
5531 return GST_FLOW_EOS;
5535 GST_OBJECT_UNLOCK (pad);
5537 GST_PAD_STREAM_UNLOCK (pad);
5538 /* Only unref if unhandled */
5539 if (ret != GST_FLOW_CUSTOM_SUCCESS_1)
5540 gst_event_unref (event);
5543 case GST_FLOW_CUSTOM_SUCCESS_1:
5544 case GST_FLOW_CUSTOM_SUCCESS:
5545 GST_DEBUG_OBJECT (pad, "dropped or handled event");
5549 GST_DEBUG_OBJECT (pad, "an error occurred %s", gst_flow_get_name (ret));
5556 g_warning ("pad %s:%s has no event handler, file a bug.",
5557 GST_DEBUG_PAD_NAME (pad));
5558 GST_OBJECT_UNLOCK (pad);
5560 GST_PAD_STREAM_UNLOCK (pad);
5561 gst_event_unref (event);
5562 return GST_FLOW_NOT_SUPPORTED;
5566 GST_DEBUG_OBJECT (pad, "no parent");
5567 GST_OBJECT_UNLOCK (pad);
5569 GST_PAD_STREAM_UNLOCK (pad);
5570 gst_event_unref (event);
5571 return GST_FLOW_FLUSHING;
5575 GST_DEBUG_OBJECT (pad, "pre event check failed");
5576 RELEASE_PARENT (parent);
5578 GST_PAD_STREAM_UNLOCK (pad);
5579 gst_event_unref (event);
5585 * gst_pad_send_event:
5586 * @pad: a #GstPad to send the event to.
5587 * @event: (transfer full): the #GstEvent to send to the pad.
5589 * Sends the event to the pad. This function can be used
5590 * by applications to send events in the pipeline.
5592 * If @pad is a source pad, @event should be an upstream event. If @pad is a
5593 * sink pad, @event should be a downstream event. For example, you would not
5594 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
5595 * Furthermore, some downstream events have to be serialized with data flow,
5596 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
5597 * the event needs to be serialized with data flow, this function will take the
5598 * pad's stream lock while calling its event function.
5600 * To find out whether an event type is upstream, downstream, or downstream and
5601 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
5602 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
5603 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
5604 * plugin doesn't need to bother itself with this information; the core handles
5605 * all necessary locks and checks.
5607 * This function takes ownership of the provided event so you should
5608 * gst_event_ref() it if you want to reuse the event after this call.
5610 * Returns: %TRUE if the event was handled.
5613 gst_pad_send_event (GstPad * pad, GstEvent * event)
5616 GstPadProbeType type;
5618 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5619 g_return_val_if_fail (event != NULL, FALSE);
5621 if (GST_PAD_IS_SINK (pad)) {
5622 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
5623 goto wrong_direction;
5624 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
5625 } else if (GST_PAD_IS_SRC (pad)) {
5626 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
5627 goto wrong_direction;
5628 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
5630 goto unknown_direction;
5632 if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
5639 /* ERROR handling */
5642 g_warning ("pad %s:%s sending %s event in wrong direction",
5643 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
5644 gst_event_unref (event);
5649 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
5650 gst_event_unref (event);
5656 * gst_pad_set_element_private:
5657 * @pad: the #GstPad to set the private data of.
5658 * @priv: The private data to attach to the pad.
5660 * Set the given private data gpointer on the pad.
5661 * This function can only be used by the element that owns the pad.
5662 * No locking is performed in this function.
5665 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5667 pad->element_private = priv;
5671 * gst_pad_get_element_private:
5672 * @pad: the #GstPad to get the private data of.
5674 * Gets the private data of a pad.
5675 * No locking is performed in this function.
5677 * Returns: (transfer none): a #gpointer to the private data.
5680 gst_pad_get_element_private (GstPad * pad)
5682 return pad->element_private;
5686 * gst_pad_get_sticky_event:
5687 * @pad: the #GstPad to get the event from.
5688 * @event_type: the #GstEventType that should be retrieved.
5689 * @idx: the index of the event
5691 * Returns a new reference of the sticky event of type @event_type
5694 * Returns: (transfer full) (nullable): a #GstEvent of type
5695 * @event_type or %NULL when no event of @event_type was on
5696 * @pad. Unref after usage.
5699 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
5701 GstEvent *event = NULL;
5704 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
5705 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
5707 GST_OBJECT_LOCK (pad);
5708 ev = find_event_by_type (pad, event_type, idx);
5709 if (ev && (event = ev->event))
5710 gst_event_ref (event);
5711 GST_OBJECT_UNLOCK (pad);
5718 GstPadStickyEventsForeachFunction func;
5723 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
5725 ForeachDispatch *data = user_data;
5726 gboolean ret = TRUE;
5729 GST_OBJECT_UNLOCK (pad);
5731 ret = data->func (pad, &ev->event, data->user_data);
5733 GST_OBJECT_LOCK (pad);
5740 * gst_pad_sticky_events_foreach:
5741 * @pad: the #GstPad that should be used for iteration.
5742 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
5743 * should be called for every event.
5744 * @user_data: (closure): the optional user data.
5746 * Iterates all sticky events on @pad and calls @foreach_func for every
5747 * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
5750 gst_pad_sticky_events_foreach (GstPad * pad,
5751 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
5753 ForeachDispatch data;
5755 g_return_if_fail (GST_IS_PAD (pad));
5756 g_return_if_fail (foreach_func != NULL);
5758 data.func = foreach_func;
5759 data.user_data = user_data;
5761 GST_OBJECT_LOCK (pad);
5762 events_foreach (pad, foreach_dispatch_function, &data);
5763 GST_OBJECT_UNLOCK (pad);
5767 do_stream_status (GstPad * pad, GstStreamStatusType type,
5768 GThread * thread, GstTask * task)
5772 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5774 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5775 if (GST_IS_ELEMENT (parent)) {
5776 GstMessage *message;
5777 GValue value = { 0 };
5779 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5780 gchar *tname, *ename, *pname;
5782 /* create a good task name */
5783 ename = gst_element_get_name (parent);
5784 pname = gst_pad_get_name (pad);
5785 tname = g_strdup_printf ("%s:%s", ename, pname);
5789 gst_object_set_name (GST_OBJECT_CAST (task), tname);
5793 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5796 g_value_init (&value, GST_TYPE_TASK);
5797 g_value_set_object (&value, task);
5798 gst_message_set_stream_status_object (message, &value);
5799 g_value_unset (&value);
5801 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5802 gst_element_post_message (parent, message);
5804 gst_object_unref (parent);
5809 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5811 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5816 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5818 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5823 * gst_pad_start_task:
5824 * @pad: the #GstPad to start the task of
5825 * @func: the task function to call
5826 * @user_data: user data passed to the task function
5827 * @notify: called when @user_data is no longer referenced
5829 * Starts a task that repeatedly calls @func with @user_data. This function
5830 * is mostly used in pad activation functions to start the dataflow.
5831 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5832 * before @func is called.
5834 * Returns: a %TRUE if the task could be started.
5837 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
5838 GDestroyNotify notify)
5843 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5844 g_return_val_if_fail (func != NULL, FALSE);
5846 GST_DEBUG_OBJECT (pad, "start task");
5848 GST_OBJECT_LOCK (pad);
5849 task = GST_PAD_TASK (pad);
5851 task = gst_task_new (func, user_data, notify);
5852 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5853 gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
5854 gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
5855 GST_INFO_OBJECT (pad, "created task %p", task);
5856 GST_PAD_TASK (pad) = task;
5857 gst_object_ref (task);
5858 /* release lock to post the message */
5859 GST_OBJECT_UNLOCK (pad);
5861 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5863 gst_object_unref (task);
5865 GST_OBJECT_LOCK (pad);
5866 /* nobody else is supposed to have changed the pad now */
5867 if (GST_PAD_TASK (pad) != task)
5868 goto concurrent_stop;
5870 res = gst_task_set_state (task, GST_TASK_STARTED);
5871 GST_OBJECT_UNLOCK (pad);
5878 GST_OBJECT_UNLOCK (pad);
5884 * gst_pad_pause_task:
5885 * @pad: the #GstPad to pause the task of
5887 * Pause the task of @pad. This function will also wait until the
5888 * function executed by the task is finished if this function is not
5889 * called from the task function.
5891 * Returns: a %TRUE if the task could be paused or %FALSE when the pad
5895 gst_pad_pause_task (GstPad * pad)
5900 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5902 GST_DEBUG_OBJECT (pad, "pause task");
5904 GST_OBJECT_LOCK (pad);
5905 task = GST_PAD_TASK (pad);
5908 res = gst_task_set_state (task, GST_TASK_PAUSED);
5909 GST_OBJECT_UNLOCK (pad);
5911 /* wait for task function to finish, this lock is recursive so it does nothing
5912 * when the pause is called from the task itself */
5913 GST_PAD_STREAM_LOCK (pad);
5914 GST_PAD_STREAM_UNLOCK (pad);
5920 GST_DEBUG_OBJECT (pad, "pad has no task");
5921 GST_OBJECT_UNLOCK (pad);
5927 * gst_pad_stop_task:
5928 * @pad: the #GstPad to stop the task of
5930 * Stop the task of @pad. This function will also make sure that the
5931 * function executed by the task will effectively stop if not called
5932 * from the GstTaskFunction.
5934 * This function will deadlock if called from the GstTaskFunction of
5935 * the task. Use gst_task_pause() instead.
5937 * Regardless of whether the pad has a task, the stream lock is acquired and
5938 * released so as to ensure that streaming through this pad has finished.
5940 * Returns: a %TRUE if the task could be stopped or %FALSE on error.
5943 gst_pad_stop_task (GstPad * pad)
5948 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5950 GST_DEBUG_OBJECT (pad, "stop task");
5952 GST_OBJECT_LOCK (pad);
5953 task = GST_PAD_TASK (pad);
5956 GST_PAD_TASK (pad) = NULL;
5957 res = gst_task_set_state (task, GST_TASK_STOPPED);
5958 GST_OBJECT_UNLOCK (pad);
5960 GST_PAD_STREAM_LOCK (pad);
5961 GST_PAD_STREAM_UNLOCK (pad);
5963 if (!gst_task_join (task))
5966 gst_object_unref (task);
5972 GST_DEBUG_OBJECT (pad, "no task");
5973 GST_OBJECT_UNLOCK (pad);
5975 GST_PAD_STREAM_LOCK (pad);
5976 GST_PAD_STREAM_UNLOCK (pad);
5978 /* this is not an error */
5983 /* this is bad, possibly the application tried to join the task from
5984 * the task's thread. We install the task again so that it will be stopped
5985 * again from the right thread next time hopefully. */
5986 GST_OBJECT_LOCK (pad);
5987 GST_DEBUG_OBJECT (pad, "join failed");
5988 /* we can only install this task if there was no other task */
5989 if (GST_PAD_TASK (pad) == NULL)
5990 GST_PAD_TASK (pad) = task;
5991 GST_OBJECT_UNLOCK (pad);
5998 * gst_pad_probe_info_get_event:
5999 * @info: a #GstPadProbeInfo
6001 * Returns: (transfer none): The #GstEvent from the probe
6005 gst_pad_probe_info_get_event (GstPadProbeInfo * info)
6007 g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM |
6008 GST_PAD_PROBE_TYPE_EVENT_UPSTREAM), NULL);
6010 return GST_PAD_PROBE_INFO_EVENT (info);
6015 * gst_pad_probe_info_get_query:
6016 * @info: a #GstPadProbeInfo
6018 * Returns: (transfer none): The #GstQuery from the probe
6022 gst_pad_probe_info_get_query (GstPadProbeInfo * info)
6024 g_return_val_if_fail (info->type & (GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM |
6025 GST_PAD_PROBE_TYPE_QUERY_UPSTREAM), NULL);
6027 return GST_PAD_PROBE_INFO_QUERY (info);
6031 * gst_pad_probe_info_get_buffer:
6032 * @info: a #GstPadProbeInfo
6034 * Returns: (transfer none): The #GstBuffer from the probe
6038 gst_pad_probe_info_get_buffer (GstPadProbeInfo * info)
6040 g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER, NULL);
6042 return GST_PAD_PROBE_INFO_BUFFER (info);
6046 * gst_pad_probe_info_get_bufferlist:
6047 * @info: a #GstPadProbeInfo
6049 * Returns: (transfer none): The #GstBufferlist from the probe
6053 gst_pad_probe_info_get_buffer_list (GstPadProbeInfo * info)
6055 g_return_val_if_fail (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST, NULL);
6057 return GST_PAD_PROBE_INFO_BUFFER_LIST (info);
6061 * gst_pad_get_last_flow_return:
6064 * Gets the #GstFlowReturn return from the last data passed by this pad.
6069 gst_pad_get_last_flow_return (GstPad * pad)
6073 GST_OBJECT_LOCK (pad);
6074 ret = GST_PAD_LAST_FLOW_RETURN (pad);
6075 GST_OBJECT_UNLOCK (pad);