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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * @short_description: Object contained by elements that allows links to
26 * @see_also: #GstPadTemplate, #GstElement, #GstEvent
28 * A #GstElement is linked to other elements via "pads", which are extremely
29 * light-weight generic link points.
30 * After two pads are retrieved from an element with gst_element_get_pad(),
31 * the pads can be link with gst_pad_link(). (For quick links,
32 * you can also use gst_element_link(), which will make the obvious
33 * link for you if it's straightforward.)
35 * Pads are typically created from a #GstPadTemplate with
36 * gst_pad_new_from_template().
38 * Pads have #GstCaps attached to it to describe the media type they are
39 * capable of dealing with. gst_pad_query_caps() and gst_pad_set_caps() are
40 * used to manipulate the caps of the pads.
41 * Pads created from a pad template cannot set capabilities that are
42 * incompatible with the pad template capabilities.
44 * Pads without pad templates can be created with gst_pad_new(),
45 * which takes a direction and a name as an argument. If the name is NULL,
46 * then a guaranteed unique name will be assigned to it.
48 * gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
50 * A #GstElement creating a pad will typically use the various
51 * gst_pad_set_*_function() calls to register callbacks for various events
54 * GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
55 * or pull in a buffer.
57 * To send a #GstEvent on a pad, use gst_pad_send_event() and
58 * gst_pad_push_event().
60 * Last reviewed on 2006-07-06 (0.10.9)
63 #include "gst_private.h"
66 #include "gstpadtemplate.h"
67 #include "gstenumtypes.h"
72 #include "glib-compat-private.h"
74 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
75 #define GST_CAT_DEFAULT GST_CAT_PADS
77 /* Pad signals and args */
95 #define GST_PAD_GET_PRIVATE(obj) \
96 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
98 /* we have a pending and an active event on the pad. On source pads only the
99 * active event is used. On sinkpads, events are copied to the pending entry and
100 * moved to the active event when the eventfunc returned TRUE. */
107 struct _GstPadPrivate
113 guint probe_list_cookie;
123 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
128 GstPadProbeInfo *info;
135 static void gst_pad_dispose (GObject * object);
136 static void gst_pad_finalize (GObject * object);
137 static void gst_pad_set_property (GObject * object, guint prop_id,
138 const GValue * value, GParamSpec * pspec);
139 static void gst_pad_get_property (GObject * object, guint prop_id,
140 GValue * value, GParamSpec * pspec);
142 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
143 static gboolean gst_pad_activate_default (GstPad * pad, GstObject * parent);
144 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
145 GstObject * parent, GstBufferList * list);
147 static GstFlowReturn gst_pad_send_event_unchecked (GstPad * pad,
148 GstEvent * event, GstPadProbeType type);
149 static GstFlowReturn gst_pad_push_event_unchecked (GstPad * pad,
150 GstEvent * event, GstPadProbeType type);
152 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
154 static GParamSpec *pspec_caps = NULL;
156 /* quarks for probe signals */
157 static GQuark buffer_quark;
158 static GQuark buffer_list_quark;
159 static GQuark event_quark;
168 static GstFlowQuarks flow_quarks[] = {
169 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
170 {GST_FLOW_OK, "ok", 0},
171 {GST_FLOW_NOT_LINKED, "not-linked", 0},
172 {GST_FLOW_FLUSHING, "flushing", 0},
173 {GST_FLOW_EOS, "eos", 0},
174 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
175 {GST_FLOW_ERROR, "error", 0},
176 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
177 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
182 * @ret: a #GstFlowReturn to get the name of.
184 * Gets a string representing the given flow return.
186 * Returns: a static string with the name of the flow return.
189 gst_flow_get_name (GstFlowReturn ret)
193 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
195 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
196 if (ret == flow_quarks[i].ret)
197 return flow_quarks[i].name;
204 * @ret: a #GstFlowReturn to get the quark of.
206 * Get the unique quark for the given GstFlowReturn.
208 * Returns: the quark associated with the flow return or 0 if an
209 * invalid return was specified.
212 gst_flow_to_quark (GstFlowReturn ret)
216 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
218 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
219 if (ret == flow_quarks[i].ret)
220 return flow_quarks[i].quark;
229 buffer_quark = g_quark_from_static_string ("buffer"); \
230 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
231 event_quark = g_quark_from_static_string ("event"); \
233 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
234 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
237 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
238 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
241 #define gst_pad_parent_class parent_class
242 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
245 gst_pad_class_init (GstPadClass * klass)
247 GObjectClass *gobject_class;
248 GstObjectClass *gstobject_class;
250 gobject_class = G_OBJECT_CLASS (klass);
251 gstobject_class = GST_OBJECT_CLASS (klass);
253 g_type_class_add_private (klass, sizeof (GstPadPrivate));
255 gobject_class->dispose = gst_pad_dispose;
256 gobject_class->finalize = gst_pad_finalize;
257 gobject_class->set_property = gst_pad_set_property;
258 gobject_class->get_property = gst_pad_get_property;
262 * @pad: the pad that emitted the signal
263 * @peer: the peer pad that has been connected
265 * Signals that a pad has been linked to the peer pad.
267 gst_pad_signals[PAD_LINKED] =
268 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
269 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
270 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
273 * @pad: the pad that emitted the signal
274 * @peer: the peer pad that has been disconnected
276 * Signals that a pad has been unlinked from the peer pad.
278 gst_pad_signals[PAD_UNLINKED] =
279 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
280 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
281 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
283 pspec_caps = g_param_spec_boxed ("caps", "Caps",
284 "The capabilities of the pad", GST_TYPE_CAPS,
285 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
286 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
288 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
289 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
290 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
291 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
293 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
294 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
295 g_param_spec_object ("template", "Template",
296 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
297 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 gstobject_class->path_string_separator = ".";
301 /* Register common function pointer descriptions */
302 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
303 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
304 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
305 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
306 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
310 gst_pad_init (GstPad * pad)
312 pad->priv = GST_PAD_GET_PRIVATE (pad);
314 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
316 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
317 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
318 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
319 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
320 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
322 GST_PAD_SET_FLUSHING (pad);
324 g_rec_mutex_init (&pad->stream_rec_lock);
326 g_cond_init (&pad->block_cond);
328 g_hook_list_init (&pad->probes, sizeof (GstProbe));
330 pad->priv->events = g_array_sized_new (FALSE, TRUE, sizeof (PadEvent), 16);
333 /* called when setting the pad inactive. It removes all sticky events from
336 remove_events (GstPad * pad)
341 events = pad->priv->events;
344 for (i = 0; i < len; i++) {
345 PadEvent *ev = &g_array_index (events, PadEvent, i);
346 gst_event_unref (ev->event);
348 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
349 g_array_set_size (events, 0);
350 pad->priv->events_cookie++;
354 find_event_by_type (GstPad * pad, GstEventType type, guint idx)
360 events = pad->priv->events;
363 for (i = 0; i < len; i++) {
364 ev = &g_array_index (events, PadEvent, i);
365 if (ev->event == NULL)
368 if (GST_EVENT_TYPE (ev->event) == type) {
380 find_event (GstPad * pad, GstEvent * event)
386 events = pad->priv->events;
389 for (i = 0; i < len; i++) {
390 ev = &g_array_index (events, PadEvent, i);
391 if (event == ev->event)
400 remove_event_by_type (GstPad * pad, GstEventType type)
406 events = pad->priv->events;
411 ev = &g_array_index (events, PadEvent, i);
412 if (ev->event == NULL)
415 if (GST_EVENT_TYPE (ev->event) != type)
418 gst_event_unref (ev->event);
419 g_array_remove_index (events, i);
421 pad->priv->events_cookie++;
429 /* check all events on srcpad against those on sinkpad. All events that are not
430 * on sinkpad are marked as received=FALSE and the PENDING_EVENTS is set on the
431 * srcpad so that the events will be sent next time */
433 schedule_events (GstPad * srcpad, GstPad * sinkpad)
438 gboolean pending = FALSE;
440 events = srcpad->priv->events;
443 for (i = 0; i < len; i++) {
444 ev = &g_array_index (events, PadEvent, i);
445 if (ev->event == NULL)
448 if (sinkpad == NULL || !find_event (sinkpad, ev->event)) {
449 ev->received = FALSE;
454 GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PENDING_EVENTS);
457 typedef gboolean (*PadEventFunction) (GstPad * pad, PadEvent * ev,
460 /* should be called with pad LOCK */
462 events_foreach (GstPad * pad, PadEventFunction func, gpointer user_data)
469 events = pad->priv->events;
472 cookie = pad->priv->events_cookie;
476 PadEvent *ev, ev_ret;
478 ev = &g_array_index (events, PadEvent, i);
479 if (G_UNLIKELY (ev->event == NULL))
482 /* take aditional ref, func might release the lock */
483 ev_ret.event = gst_event_ref (ev->event);
484 ev_ret.received = ev->received;
486 ret = func (pad, &ev_ret, user_data);
488 /* recheck the cookie, lock might have been released and the list could have
490 if (G_UNLIKELY (cookie != pad->priv->events_cookie)) {
491 if (G_LIKELY (ev_ret.event))
492 gst_event_unref (ev_ret.event);
496 /* store the received state */
497 ev->received = ev_ret.received;
499 /* if the event changed, we need to do something */
500 if (G_UNLIKELY (ev->event != ev_ret.event)) {
501 if (G_UNLIKELY (ev_ret.event == NULL)) {
502 /* function unreffed and set the event to NULL, remove it */
503 g_array_remove_index (events, i);
505 cookie = ++pad->priv->events_cookie;
508 /* function gave a new event for us */
509 gst_event_take (&ev->event, ev_ret.event);
512 /* just unref, nothing changed */
513 gst_event_unref (ev_ret.event);
522 /* should be called with LOCK */
524 apply_pad_offset (GstPad * pad, GstEvent * event)
526 /* check if we need to adjust the segment */
527 if (pad->offset != 0) {
530 /* copy segment values */
531 gst_event_copy_segment (event, &segment);
532 gst_event_unref (event);
534 /* adjust and make a new event with the offset applied */
535 segment.base += pad->offset;
536 event = gst_event_new_segment (&segment);
541 /* should be called with the OBJECT_LOCK */
543 get_pad_caps (GstPad * pad)
545 GstCaps *caps = NULL;
548 ev = find_event_by_type (pad, GST_EVENT_CAPS, 0);
550 gst_event_parse_caps (ev->event, &caps);
556 gst_pad_dispose (GObject * object)
558 GstPad *pad = GST_PAD_CAST (object);
561 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
563 /* unlink the peer pad */
564 if ((peer = gst_pad_get_peer (pad))) {
565 /* window for MT unsafeness, someone else could unlink here
566 * and then we call unlink with wrong pads. The unlink
567 * function would catch this and safely return failed. */
568 if (GST_PAD_IS_SRC (pad))
569 gst_pad_unlink (pad, peer);
571 gst_pad_unlink (peer, pad);
573 gst_object_unref (peer);
576 gst_pad_set_pad_template (pad, NULL);
580 g_hook_list_clear (&pad->probes);
582 G_OBJECT_CLASS (parent_class)->dispose (object);
586 gst_pad_finalize (GObject * object)
588 GstPad *pad = GST_PAD_CAST (object);
591 /* in case the task is still around, clean it up */
592 if ((task = GST_PAD_TASK (pad))) {
593 gst_task_join (task);
594 GST_PAD_TASK (pad) = NULL;
595 gst_object_unref (task);
598 if (pad->activatenotify)
599 pad->activatenotify (pad->activatedata);
600 if (pad->activatemodenotify)
601 pad->activatemodenotify (pad->activatemodedata);
603 pad->linknotify (pad->linkdata);
604 if (pad->unlinknotify)
605 pad->unlinknotify (pad->unlinkdata);
606 if (pad->chainnotify)
607 pad->chainnotify (pad->chaindata);
608 if (pad->chainlistnotify)
609 pad->chainlistnotify (pad->chainlistdata);
610 if (pad->getrangenotify)
611 pad->getrangenotify (pad->getrangedata);
612 if (pad->eventnotify)
613 pad->eventnotify (pad->eventdata);
614 if (pad->querynotify)
615 pad->querynotify (pad->querydata);
616 if (pad->iterintlinknotify)
617 pad->iterintlinknotify (pad->iterintlinkdata);
619 g_rec_mutex_clear (&pad->stream_rec_lock);
620 g_cond_clear (&pad->block_cond);
621 g_array_free (pad->priv->events, TRUE);
623 G_OBJECT_CLASS (parent_class)->finalize (object);
627 gst_pad_set_property (GObject * object, guint prop_id,
628 const GValue * value, GParamSpec * pspec)
630 g_return_if_fail (GST_IS_PAD (object));
633 case PAD_PROP_DIRECTION:
634 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
636 case PAD_PROP_TEMPLATE:
637 gst_pad_set_pad_template (GST_PAD_CAST (object),
638 (GstPadTemplate *) g_value_get_object (value));
641 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
647 gst_pad_get_property (GObject * object, guint prop_id,
648 GValue * value, GParamSpec * pspec)
650 g_return_if_fail (GST_IS_PAD (object));
654 GST_OBJECT_LOCK (object);
655 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
656 GST_OBJECT_UNLOCK (object);
658 case PAD_PROP_DIRECTION:
659 g_value_set_enum (value, GST_PAD_DIRECTION (object));
661 case PAD_PROP_TEMPLATE:
662 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
665 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
672 * @name: the name of the new pad.
673 * @direction: the #GstPadDirection of the pad.
675 * Creates a new pad with the given name in the given direction.
676 * If name is NULL, a guaranteed unique name (across all pads)
678 * This function makes a copy of the name so you can safely free the name.
680 * Returns: (transfer floating): a new #GstPad, or NULL in case of an error.
685 gst_pad_new (const gchar * name, GstPadDirection direction)
687 return g_object_new (GST_TYPE_PAD,
688 "name", name, "direction", direction, NULL);
692 * gst_pad_new_from_template:
693 * @templ: the pad template to use
694 * @name: the name of the element
696 * Creates a new pad with the given name from the given template.
697 * If name is NULL, a guaranteed unique name (across all pads)
699 * This function makes a copy of the name so you can safely free the name.
701 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
704 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
706 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
708 return g_object_new (GST_TYPE_PAD,
709 "name", name, "direction", templ->direction, "template", templ, NULL);
713 * gst_pad_new_from_static_template:
714 * @templ: the #GstStaticPadTemplate to use
715 * @name: the name of the element
717 * Creates a new pad with the given name from the given static template.
718 * If name is NULL, a guaranteed unique name (across all pads)
720 * This function makes a copy of the name so you can safely free the name.
722 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
725 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
729 GstPadTemplate *template;
731 template = gst_static_pad_template_get (templ);
732 pad = gst_pad_new_from_template (template, name);
733 gst_object_unref (template);
737 #define ACQUIRE_PARENT(pad, parent, label) \
739 if (G_LIKELY ((parent = GST_OBJECT_PARENT (pad)))) \
740 gst_object_ref (parent); \
741 else if (G_LIKELY (GST_PAD_NEEDS_PARENT (pad))) \
745 #define RELEASE_PARENT(parent) \
747 if (G_LIKELY (parent)) \
748 gst_object_unref (parent); \
752 * gst_pad_get_direction:
753 * @pad: a #GstPad to get the direction of.
755 * Gets the direction of the pad. The direction of the pad is
756 * decided at construction time so this function does not take
759 * Returns: the #GstPadDirection of the pad.
764 gst_pad_get_direction (GstPad * pad)
766 GstPadDirection result;
768 /* PAD_UNKNOWN is a little silly but we need some sort of
769 * error return value */
770 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
772 result = GST_PAD_DIRECTION (pad);
778 gst_pad_activate_default (GstPad * pad, GstObject * parent)
780 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
784 pre_activate (GstPad * pad, GstPadMode new_mode)
787 case GST_PAD_MODE_NONE:
788 GST_OBJECT_LOCK (pad);
789 GST_DEBUG_OBJECT (pad, "setting PAD_MODE NONE, set flushing");
790 GST_PAD_SET_FLUSHING (pad);
791 GST_PAD_MODE (pad) = new_mode;
792 /* unlock blocked pads so element can resume and stop */
793 GST_PAD_BLOCK_BROADCAST (pad);
794 GST_OBJECT_UNLOCK (pad);
796 case GST_PAD_MODE_PUSH:
797 case GST_PAD_MODE_PULL:
798 GST_OBJECT_LOCK (pad);
799 GST_DEBUG_OBJECT (pad, "setting PAD_MODE %d, unset flushing", new_mode);
800 GST_PAD_UNSET_FLUSHING (pad);
801 GST_PAD_MODE (pad) = new_mode;
802 if (GST_PAD_IS_SINK (pad)) {
804 /* make sure the peer src pad sends us all events */
805 if ((peer = GST_PAD_PEER (pad))) {
806 gst_object_ref (peer);
807 GST_OBJECT_UNLOCK (pad);
809 GST_DEBUG_OBJECT (pad, "reschedule events on peer %s:%s",
810 GST_DEBUG_PAD_NAME (peer));
812 GST_OBJECT_LOCK (peer);
813 schedule_events (peer, NULL);
814 GST_OBJECT_UNLOCK (peer);
816 gst_object_unref (peer);
818 GST_OBJECT_UNLOCK (pad);
821 GST_OBJECT_UNLOCK (pad);
828 post_activate (GstPad * pad, GstPadMode new_mode)
831 case GST_PAD_MODE_NONE:
832 /* ensures that streaming stops */
833 GST_PAD_STREAM_LOCK (pad);
834 GST_DEBUG_OBJECT (pad, "stopped streaming");
835 GST_OBJECT_LOCK (pad);
837 GST_OBJECT_UNLOCK (pad);
838 GST_PAD_STREAM_UNLOCK (pad);
840 case GST_PAD_MODE_PUSH:
841 case GST_PAD_MODE_PULL:
848 * gst_pad_set_active:
849 * @pad: the #GstPad to activate or deactivate.
850 * @active: whether or not the pad should be active.
852 * Activates or deactivates the given pad.
853 * Normally called from within core state change functions.
855 * If @active, makes sure the pad is active. If it is already active, either in
856 * push or pull mode, just return. Otherwise dispatches to the pad's activate
857 * function to perform the actual activation.
859 * If not @active, checks the pad's current mode and calls
860 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
863 * Returns: #TRUE if the operation was successful.
868 gst_pad_set_active (GstPad * pad, gboolean active)
872 gboolean ret = FALSE;
874 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
876 GST_OBJECT_LOCK (pad);
877 old = GST_PAD_MODE (pad);
878 ACQUIRE_PARENT (pad, parent, no_parent);
879 GST_OBJECT_UNLOCK (pad);
882 if (old == GST_PAD_MODE_NONE) {
883 GST_DEBUG_OBJECT (pad, "activating pad from none");
884 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad, parent);
886 GST_DEBUG_OBJECT (pad, "pad was active in mode %d", old);
890 if (old == GST_PAD_MODE_NONE) {
891 GST_DEBUG_OBJECT (pad, "pad was inactive");
894 GST_DEBUG_OBJECT (pad, "deactivating pad from mode %d", old);
895 ret = gst_pad_activate_mode (pad, old, FALSE);
899 RELEASE_PARENT (parent);
901 if (G_UNLIKELY (!ret))
905 GST_OBJECT_LOCK (pad);
906 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
907 GST_OBJECT_UNLOCK (pad);
914 GST_DEBUG_OBJECT (pad, "no parent");
915 GST_OBJECT_UNLOCK (pad);
920 GST_OBJECT_LOCK (pad);
922 g_critical ("Failed to deactivate pad %s:%s, very bad",
923 GST_DEBUG_PAD_NAME (pad));
925 GST_WARNING_OBJECT (pad, "Failed to activate pad");
927 GST_OBJECT_UNLOCK (pad);
933 * gst_pad_activate_mode:
934 * @pad: the #GstPad to activate or deactivate.
935 * @mode: the requested activation mode
936 * @active: whether or not the pad should be active.
938 * Activates or deactivates the given pad in @mode via dispatching to the
939 * pad's activatemodefunc. For use from within pad activation functions only.
941 * If you don't know what this is, you probably don't want to call it.
943 * Returns: TRUE if the operation was successful.
948 gst_pad_activate_mode (GstPad * pad, GstPadMode mode, gboolean active)
950 gboolean res = FALSE;
956 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
958 GST_OBJECT_LOCK (pad);
959 old = GST_PAD_MODE (pad);
960 dir = GST_PAD_DIRECTION (pad);
961 ACQUIRE_PARENT (pad, parent, no_parent);
962 GST_OBJECT_UNLOCK (pad);
964 new = active ? mode : GST_PAD_MODE_NONE;
969 if (active && old != mode) {
970 /* pad was activate in the wrong direction, deactivate it
971 * and reactivate it in the requested mode */
972 GST_DEBUG_OBJECT (pad, "deactivating pad from mode %d", old);
973 if (G_UNLIKELY (!gst_pad_activate_mode (pad, old, FALSE)))
974 goto deactivate_failed;
978 case GST_PAD_MODE_PULL:
980 if (dir == GST_PAD_SINK) {
981 if ((peer = gst_pad_get_peer (pad))) {
982 GST_DEBUG_OBJECT (pad, "calling peer");
983 if (G_UNLIKELY (!gst_pad_activate_mode (peer, mode, active)))
985 gst_object_unref (peer);
987 /* there is no peer, this is only fatal when we activate. When we
988 * deactivate, we must assume the application has unlinked the peer and
989 * will deactivate it eventually. */
993 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
996 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
997 goto failure; /* Can't activate pull on a src without a
1006 pre_activate (pad, new);
1008 if (GST_PAD_ACTIVATEMODEFUNC (pad)) {
1009 if (G_UNLIKELY (!GST_PAD_ACTIVATEMODEFUNC (pad) (pad, parent, mode,
1013 /* can happen for sinks of passthrough elements */
1016 post_activate (pad, new);
1018 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in mode %d",
1019 active ? "activated" : "deactivated", mode);
1024 RELEASE_PARENT (parent);
1030 GST_DEBUG_OBJECT (pad, "no parent");
1031 GST_OBJECT_UNLOCK (pad);
1036 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in mode %d",
1037 active ? "activated" : "deactivated", mode);
1042 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1043 "failed to %s in switch to mode %d from mode %d",
1044 (active ? "activate" : "deactivate"), mode, old);
1049 GST_OBJECT_LOCK (peer);
1050 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1051 "activate_mode on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
1052 GST_OBJECT_UNLOCK (peer);
1053 gst_object_unref (peer);
1058 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
1059 "pad in pull mode");
1064 GST_OBJECT_LOCK (pad);
1065 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in mode %d",
1066 active ? "activate" : "deactivate", mode);
1067 GST_PAD_SET_FLUSHING (pad);
1068 GST_PAD_MODE (pad) = old;
1069 GST_OBJECT_UNLOCK (pad);
1075 * gst_pad_is_active:
1076 * @pad: the #GstPad to query
1078 * Query if a pad is active
1080 * Returns: TRUE if the pad is active.
1085 gst_pad_is_active (GstPad * pad)
1087 gboolean result = FALSE;
1089 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1091 GST_OBJECT_LOCK (pad);
1092 result = GST_PAD_IS_ACTIVE (pad);
1093 GST_OBJECT_UNLOCK (pad);
1099 * gst_pad_add_probe:
1100 * @pad: the #GstPad to add the probe to
1101 * @mask: the probe mask
1102 * @callback: #GstPadProbeCallback that will be called with notifications of
1104 * @user_data: (closure): user data passed to the callback
1105 * @destroy_data: #GDestroyNotify for user_data
1107 * Be notified of different states of pads. The provided callback is called for
1108 * every state that matches @mask.
1110 * Returns: an id or 0 on error. The id can be used to remove the probe with
1111 * gst_pad_remove_probe().
1116 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
1117 GstPadProbeCallback callback, gpointer user_data,
1118 GDestroyNotify destroy_data)
1123 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1124 g_return_val_if_fail (mask != 0, 0);
1126 GST_OBJECT_LOCK (pad);
1128 /* make a new probe */
1129 hook = g_hook_alloc (&pad->probes);
1131 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1134 /* when no contraints are given for the types, assume all types are
1136 if ((mask & GST_PAD_PROBE_TYPE_ALL_BOTH) == 0)
1137 mask |= GST_PAD_PROBE_TYPE_ALL_BOTH;
1138 if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1139 mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1141 /* store our flags and other fields */
1142 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1143 hook->func = callback;
1144 hook->data = user_data;
1145 hook->destroy = destroy_data;
1146 PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
1149 g_hook_prepend (&pad->probes, hook);
1151 /* incremenent cookie so that the new hook get's called */
1152 pad->priv->probe_list_cookie++;
1154 /* get the id of the hook, we return this and it can be used to remove the
1156 res = hook->hook_id;
1158 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1160 if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1161 /* we have a block probe */
1163 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKED);
1164 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1165 "now %d blocking probes", pad->num_blocked);
1168 /* call the callback if we need to be called for idle callbacks */
1169 if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1170 if (pad->priv->using > 0) {
1171 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1172 * flag above, the last thread to leave the push will do the callback. New
1173 * threads going into the push will block. */
1174 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1175 "pad is in use, delay idle callback");
1176 GST_OBJECT_UNLOCK (pad);
1178 GstPadProbeInfo info = { GST_PAD_PROBE_TYPE_IDLE, res, };
1180 /* the pad is idle now, we can signal the idle callback now */
1181 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1182 "pad is idle, trigger idle callback");
1183 GST_OBJECT_UNLOCK (pad);
1185 callback (pad, &info, user_data);
1188 GST_OBJECT_UNLOCK (pad);
1194 cleanup_hook (GstPad * pad, GHook * hook)
1196 GstPadProbeType type;
1198 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1200 if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1201 /* unblock when we remove the last blocking probe */
1203 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1205 if (pad->num_blocked == 0) {
1206 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1207 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKED);
1208 GST_PAD_BLOCK_BROADCAST (pad);
1211 g_hook_destroy_link (&pad->probes, hook);
1216 * gst_pad_remove_probe:
1217 * @pad: the #GstPad with the probe
1218 * @id: the probe id to remove
1220 * Remove the probe with @id from @pad.
1225 gst_pad_remove_probe (GstPad * pad, gulong id)
1229 g_return_if_fail (GST_IS_PAD (pad));
1231 GST_OBJECT_LOCK (pad);
1233 hook = g_hook_get (&pad->probes, id);
1237 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1239 cleanup_hook (pad, hook);
1240 GST_OBJECT_UNLOCK (pad);
1246 GST_OBJECT_UNLOCK (pad);
1247 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1253 * gst_pad_is_blocked:
1254 * @pad: the #GstPad to query
1256 * Checks if the pad is blocked or not. This function returns the
1257 * last requested state of the pad. It is not certain that the pad
1258 * is actually blocking at this point (see gst_pad_is_blocking()).
1260 * Returns: TRUE if the pad is blocked.
1265 gst_pad_is_blocked (GstPad * pad)
1267 gboolean result = FALSE;
1269 g_return_val_if_fail (GST_IS_PAD (pad), result);
1271 GST_OBJECT_LOCK (pad);
1272 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED);
1273 GST_OBJECT_UNLOCK (pad);
1279 * gst_pad_is_blocking:
1280 * @pad: the #GstPad to query
1282 * Checks if the pad is blocking or not. This is a guaranteed state
1283 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1285 * Returns: TRUE if the pad is blocking.
1292 gst_pad_is_blocking (GstPad * pad)
1294 gboolean result = FALSE;
1296 g_return_val_if_fail (GST_IS_PAD (pad), result);
1298 GST_OBJECT_LOCK (pad);
1299 /* the blocking flag is only valid if the pad is not flushing */
1300 result = GST_PAD_IS_BLOCKING (pad) && !GST_PAD_IS_FLUSHING (pad);
1301 GST_OBJECT_UNLOCK (pad);
1307 * gst_pad_check_reconfigure:
1308 * @pad: the #GstPad to check
1310 * Check and clear the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1311 * if the flag was set.
1313 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag was set on @pad.
1316 gst_pad_check_reconfigure (GstPad * pad)
1318 gboolean reconfigure;
1320 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1322 GST_OBJECT_LOCK (pad);
1323 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1325 GST_DEBUG_OBJECT (pad, "remove RECONFIGURE flag");
1326 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1327 GST_OBJECT_UNLOCK (pad);
1333 * gst_pad_mark_reconfigure:
1334 * @pad: the #GstPad to mark
1336 * Mark a pad for needing reconfiguration. The next call to
1337 * gst_pad_check_reconfigure() will return %TRUE after this call.
1340 gst_pad_mark_reconfigure (GstPad * pad)
1342 g_return_if_fail (GST_IS_PAD (pad));
1344 GST_OBJECT_LOCK (pad);
1345 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1346 GST_OBJECT_UNLOCK (pad);
1350 * gst_pad_set_activate_function_full:
1352 * @activate: the #GstPadActivateFunction to set.
1353 * @user_data: user_data passed to @notify
1354 * @notify: notify called when @activate will not be used anymore.
1356 * Sets the given activate function for @pad. The activate function will
1357 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1358 * the actual activation. Only makes sense to set on sink pads.
1360 * Call this function if your sink pad can start a pull-based task.
1363 gst_pad_set_activate_function_full (GstPad * pad,
1364 GstPadActivateFunction activate, gpointer user_data, GDestroyNotify notify)
1366 g_return_if_fail (GST_IS_PAD (pad));
1368 if (pad->activatenotify)
1369 pad->activatenotify (pad->activatedata);
1370 GST_PAD_ACTIVATEFUNC (pad) = activate;
1371 pad->activatedata = user_data;
1372 pad->activatenotify = notify;
1374 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1375 GST_DEBUG_FUNCPTR_NAME (activate));
1379 * gst_pad_set_activatemode_function_full:
1381 * @activatemode: the #GstPadActivateModeFunction to set.
1382 * @user_data: user_data passed to @notify
1383 * @notify: notify called when @activatemode will not be used anymore.
1385 * Sets the given activate_mode function for the pad. An activate_mode function
1386 * prepares the element for data passing.
1389 gst_pad_set_activatemode_function_full (GstPad * pad,
1390 GstPadActivateModeFunction activatemode, gpointer user_data,
1391 GDestroyNotify notify)
1393 g_return_if_fail (GST_IS_PAD (pad));
1395 if (pad->activatemodenotify)
1396 pad->activatemodenotify (pad->activatemodedata);
1397 GST_PAD_ACTIVATEMODEFUNC (pad) = activatemode;
1398 pad->activatemodedata = user_data;
1399 pad->activatemodenotify = notify;
1401 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatemodefunc set to %s",
1402 GST_DEBUG_FUNCPTR_NAME (activatemode));
1406 * gst_pad_set_chain_function_full:
1407 * @pad: a sink #GstPad.
1408 * @chain: the #GstPadChainFunction to set.
1409 * @user_data: user_data passed to @notify
1410 * @notify: notify called when @chain will not be used anymore.
1412 * Sets the given chain function for the pad. The chain function is called to
1413 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1416 gst_pad_set_chain_function_full (GstPad * pad, GstPadChainFunction chain,
1417 gpointer user_data, GDestroyNotify notify)
1419 g_return_if_fail (GST_IS_PAD (pad));
1420 g_return_if_fail (GST_PAD_IS_SINK (pad));
1422 if (pad->chainnotify)
1423 pad->chainnotify (pad->chaindata);
1424 GST_PAD_CHAINFUNC (pad) = chain;
1425 pad->chaindata = user_data;
1426 pad->chainnotify = notify;
1428 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1429 GST_DEBUG_FUNCPTR_NAME (chain));
1433 * gst_pad_set_chain_list_function_full:
1434 * @pad: a sink #GstPad.
1435 * @chainlist: the #GstPadChainListFunction to set.
1436 * @user_data: user_data passed to @notify
1437 * @notify: notify called when @chainlist will not be used anymore.
1439 * Sets the given chain list function for the pad. The chainlist function is
1440 * called to process a #GstBufferList input buffer list. See
1441 * #GstPadChainListFunction for more details.
1446 gst_pad_set_chain_list_function_full (GstPad * pad,
1447 GstPadChainListFunction chainlist, gpointer user_data,
1448 GDestroyNotify notify)
1450 g_return_if_fail (GST_IS_PAD (pad));
1451 g_return_if_fail (GST_PAD_IS_SINK (pad));
1453 if (pad->chainlistnotify)
1454 pad->chainlistnotify (pad->chainlistdata);
1455 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1456 pad->chainlistdata = user_data;
1457 pad->chainlistnotify = notify;
1459 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1460 GST_DEBUG_FUNCPTR_NAME (chainlist));
1464 * gst_pad_set_getrange_function_full:
1465 * @pad: a source #GstPad.
1466 * @get: the #GstPadGetRangeFunction to set.
1467 * @user_data: user_data passed to @notify
1468 * @notify: notify called when @get will not be used anymore.
1470 * Sets the given getrange function for the pad. The getrange function is
1471 * called to produce a new #GstBuffer to start the processing pipeline. see
1472 * #GstPadGetRangeFunction for a description of the getrange function.
1475 gst_pad_set_getrange_function_full (GstPad * pad, GstPadGetRangeFunction get,
1476 gpointer user_data, GDestroyNotify notify)
1478 g_return_if_fail (GST_IS_PAD (pad));
1479 g_return_if_fail (GST_PAD_IS_SRC (pad));
1481 if (pad->getrangenotify)
1482 pad->getrangenotify (pad->getrangedata);
1483 GST_PAD_GETRANGEFUNC (pad) = get;
1484 pad->getrangedata = user_data;
1485 pad->getrangenotify = notify;
1487 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1488 GST_DEBUG_FUNCPTR_NAME (get));
1492 * gst_pad_set_event_function_full:
1493 * @pad: a #GstPad of either direction.
1494 * @event: the #GstPadEventFunction to set.
1495 * @user_data: user_data passed to @notify
1496 * @notify: notify called when @event will not be used anymore.
1498 * Sets the given event handler for the pad.
1501 gst_pad_set_event_function_full (GstPad * pad, GstPadEventFunction event,
1502 gpointer user_data, GDestroyNotify notify)
1504 g_return_if_fail (GST_IS_PAD (pad));
1506 if (pad->eventnotify)
1507 pad->eventnotify (pad->eventdata);
1508 GST_PAD_EVENTFUNC (pad) = event;
1509 pad->eventdata = user_data;
1510 pad->eventnotify = notify;
1512 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1513 GST_DEBUG_FUNCPTR_NAME (event));
1517 * gst_pad_set_query_function_full:
1518 * @pad: a #GstPad of either direction.
1519 * @query: the #GstPadQueryFunction to set.
1520 * @user_data: user_data passed to @notify
1521 * @notify: notify called when @query will not be used anymore.
1523 * Set the given query function for the pad.
1526 gst_pad_set_query_function_full (GstPad * pad, GstPadQueryFunction query,
1527 gpointer user_data, GDestroyNotify notify)
1529 g_return_if_fail (GST_IS_PAD (pad));
1531 if (pad->querynotify)
1532 pad->querynotify (pad->querydata);
1533 GST_PAD_QUERYFUNC (pad) = query;
1534 pad->querydata = user_data;
1535 pad->querynotify = notify;
1537 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1538 GST_DEBUG_FUNCPTR_NAME (query));
1542 * gst_pad_set_iterate_internal_links_function_full:
1543 * @pad: a #GstPad of either direction.
1544 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1545 * @user_data: user_data passed to @notify
1546 * @notify: notify called when @iterintlink will not be used anymore.
1548 * Sets the given internal link iterator function for the pad.
1553 gst_pad_set_iterate_internal_links_function_full (GstPad * pad,
1554 GstPadIterIntLinkFunction iterintlink, gpointer user_data,
1555 GDestroyNotify notify)
1557 g_return_if_fail (GST_IS_PAD (pad));
1559 if (pad->iterintlinknotify)
1560 pad->iterintlinknotify (pad->iterintlinkdata);
1561 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1562 pad->iterintlinkdata = user_data;
1563 pad->iterintlinknotify = notify;
1565 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1566 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1570 * gst_pad_set_link_function_full:
1572 * @link: the #GstPadLinkFunction to set.
1573 * @user_data: user_data passed to @notify
1574 * @notify: notify called when @link will not be used anymore.
1576 * Sets the given link function for the pad. It will be called when
1577 * the pad is linked with another pad.
1579 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1582 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1583 * cannot be made for some reason.
1585 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1586 * of the peer sink pad, if present.
1589 gst_pad_set_link_function_full (GstPad * pad, GstPadLinkFunction link,
1590 gpointer user_data, GDestroyNotify notify)
1592 g_return_if_fail (GST_IS_PAD (pad));
1594 if (pad->linknotify)
1595 pad->linknotify (pad->linkdata);
1596 GST_PAD_LINKFUNC (pad) = link;
1597 pad->linkdata = user_data;
1598 pad->linknotify = notify;
1600 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1601 GST_DEBUG_FUNCPTR_NAME (link));
1605 * gst_pad_set_unlink_function_full:
1607 * @unlink: the #GstPadUnlinkFunction to set.
1608 * @user_data: user_data passed to @notify
1609 * @notify: notify called when @unlink will not be used anymore.
1611 * Sets the given unlink function for the pad. It will be called
1612 * when the pad is unlinked.
1615 gst_pad_set_unlink_function_full (GstPad * pad, GstPadUnlinkFunction unlink,
1616 gpointer user_data, GDestroyNotify notify)
1618 g_return_if_fail (GST_IS_PAD (pad));
1620 if (pad->unlinknotify)
1621 pad->unlinknotify (pad->unlinkdata);
1622 GST_PAD_UNLINKFUNC (pad) = unlink;
1623 pad->unlinkdata = user_data;
1624 pad->unlinknotify = notify;
1626 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1627 GST_DEBUG_FUNCPTR_NAME (unlink));
1632 * @srcpad: the source #GstPad to unlink.
1633 * @sinkpad: the sink #GstPad to unlink.
1635 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1636 * signal on both pads.
1638 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1639 * the pads were not linked together.
1644 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1646 gboolean result = FALSE;
1647 GstElement *parent = NULL;
1649 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1650 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1651 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1652 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1654 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1655 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1656 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1658 /* We need to notify the parent before taking any pad locks as the bin in
1659 * question might be waiting for a lock on the pad while holding its lock
1660 * that our message will try to take. */
1661 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1662 if (GST_IS_ELEMENT (parent)) {
1663 gst_element_post_message (parent,
1664 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1665 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1667 gst_object_unref (parent);
1672 GST_OBJECT_LOCK (srcpad);
1673 GST_OBJECT_LOCK (sinkpad);
1675 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1676 goto not_linked_together;
1678 if (GST_PAD_UNLINKFUNC (srcpad)) {
1679 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1681 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1682 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1685 /* first clear peers */
1686 GST_PAD_PEER (srcpad) = NULL;
1687 GST_PAD_PEER (sinkpad) = NULL;
1689 GST_OBJECT_UNLOCK (sinkpad);
1690 GST_OBJECT_UNLOCK (srcpad);
1692 /* fire off a signal to each of the pads telling them
1693 * that they've been unlinked */
1694 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1695 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1697 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1698 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1703 if (parent != NULL) {
1704 gst_element_post_message (parent,
1705 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1706 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1707 gst_object_unref (parent);
1712 not_linked_together:
1714 /* we do not emit a warning in this case because unlinking cannot
1715 * be made MT safe.*/
1716 GST_OBJECT_UNLOCK (sinkpad);
1717 GST_OBJECT_UNLOCK (srcpad);
1723 * gst_pad_is_linked:
1724 * @pad: pad to check
1726 * Checks if a @pad is linked to another pad or not.
1728 * Returns: TRUE if the pad is linked, FALSE otherwise.
1733 gst_pad_is_linked (GstPad * pad)
1737 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1739 GST_OBJECT_LOCK (pad);
1740 result = (GST_PAD_PEER (pad) != NULL);
1741 GST_OBJECT_UNLOCK (pad);
1746 /* get the caps from both pads and see if the intersection
1749 * This function should be called with the pad LOCK on both
1753 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1754 GstPadLinkCheck flags)
1756 GstCaps *srccaps = NULL;
1757 GstCaps *sinkcaps = NULL;
1758 gboolean compatible = FALSE;
1760 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1763 /* Doing the expensive caps checking takes priority over only checking the template caps */
1764 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1765 GST_OBJECT_UNLOCK (sink);
1766 GST_OBJECT_UNLOCK (src);
1768 srccaps = gst_pad_query_caps (src, NULL);
1769 sinkcaps = gst_pad_query_caps (sink, NULL);
1771 GST_OBJECT_LOCK (src);
1772 GST_OBJECT_LOCK (sink);
1774 /* If one of the two pads doesn't have a template, consider the intersection
1776 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1777 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1781 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1783 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1786 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1788 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1791 /* if we have caps on both pads we can check the intersection. If one
1792 * of the caps is NULL, we return TRUE. */
1793 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1795 gst_caps_unref (srccaps);
1797 gst_caps_unref (sinkcaps);
1801 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1802 gst_caps_unref (srccaps);
1803 gst_caps_unref (sinkcaps);
1806 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1807 (compatible ? "" : "not"));
1812 /* check if the grandparents of both pads are the same.
1813 * This check is required so that we don't try to link
1814 * pads from elements in different bins without ghostpads.
1816 * The LOCK should be held on both pads
1819 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1821 GstObject *psrc, *psink;
1823 psrc = GST_OBJECT_PARENT (src);
1824 psink = GST_OBJECT_PARENT (sink);
1826 /* if one of the pads has no parent, we allow the link */
1827 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1830 /* only care about parents that are elements */
1831 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1832 goto no_element_parent;
1834 /* if the parents are the same, we have a loop */
1835 if (G_UNLIKELY (psrc == psink))
1838 /* if they both have a parent, we check the grandparents. We can not lock
1839 * the parent because we hold on the child (pad) and the locking order is
1840 * parent >> child. */
1841 psrc = GST_OBJECT_PARENT (psrc);
1842 psink = GST_OBJECT_PARENT (psink);
1844 /* if they have grandparents but they are not the same */
1845 if (G_UNLIKELY (psrc != psink))
1846 goto wrong_grandparents;
1853 GST_CAT_DEBUG (GST_CAT_CAPS,
1854 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1855 GST_PTR_FORMAT, psrc, psink);
1860 GST_CAT_DEBUG (GST_CAT_CAPS,
1861 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1862 GST_PTR_FORMAT, psrc, psink);
1867 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1873 GST_CAT_DEBUG (GST_CAT_CAPS,
1874 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1875 GST_PTR_FORMAT, psrc, psink);
1880 /* FIXME leftover from an attempt at refactoring... */
1881 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1882 * the two pads will be locked in the srcpad, sinkpad order. */
1883 static GstPadLinkReturn
1884 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1886 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1887 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1889 GST_OBJECT_LOCK (srcpad);
1891 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1892 goto src_was_linked;
1894 GST_OBJECT_LOCK (sinkpad);
1896 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1897 goto sink_was_linked;
1899 /* check hierarchy, pads can only be linked if the grandparents
1901 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1902 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1903 goto wrong_hierarchy;
1905 /* check pad caps for non-empty intersection */
1906 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1909 /* FIXME check pad scheduling for non-empty intersection */
1911 return GST_PAD_LINK_OK;
1915 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1916 GST_DEBUG_PAD_NAME (srcpad),
1917 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1918 /* we do not emit a warning in this case because unlinking cannot
1919 * be made MT safe.*/
1920 GST_OBJECT_UNLOCK (srcpad);
1921 return GST_PAD_LINK_WAS_LINKED;
1925 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1926 GST_DEBUG_PAD_NAME (sinkpad),
1927 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1928 /* we do not emit a warning in this case because unlinking cannot
1929 * be made MT safe.*/
1930 GST_OBJECT_UNLOCK (sinkpad);
1931 GST_OBJECT_UNLOCK (srcpad);
1932 return GST_PAD_LINK_WAS_LINKED;
1936 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1937 GST_OBJECT_UNLOCK (sinkpad);
1938 GST_OBJECT_UNLOCK (srcpad);
1939 return GST_PAD_LINK_WRONG_HIERARCHY;
1943 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1944 GST_OBJECT_UNLOCK (sinkpad);
1945 GST_OBJECT_UNLOCK (srcpad);
1946 return GST_PAD_LINK_NOFORMAT;
1952 * @srcpad: the source #GstPad.
1953 * @sinkpad: the sink #GstPad.
1955 * Checks if the source pad and the sink pad are compatible so they can be
1958 * Returns: TRUE if the pads can be linked.
1961 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1963 GstPadLinkReturn result;
1965 /* generic checks */
1966 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1967 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1969 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1970 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1972 /* gst_pad_link_prepare does everything for us, we only release the locks
1973 * on the pads that it gets us. If this function returns !OK the locks are not
1975 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1976 if (result != GST_PAD_LINK_OK)
1979 GST_OBJECT_UNLOCK (srcpad);
1980 GST_OBJECT_UNLOCK (sinkpad);
1983 return result == GST_PAD_LINK_OK;
1987 * gst_pad_link_full:
1988 * @srcpad: the source #GstPad to link.
1989 * @sinkpad: the sink #GstPad to link.
1990 * @flags: the checks to validate when linking
1992 * Links the source pad and the sink pad.
1994 * This variant of #gst_pad_link provides a more granular control on the
1995 * checks being done when linking. While providing some considerable speedups
1996 * the caller of this method must be aware that wrong usage of those flags
1997 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
1998 * for more information.
2002 * Returns: A result code indicating if the connection worked or
2008 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2010 GstPadLinkReturn result;
2012 GstPadLinkFunction srcfunc, sinkfunc;
2014 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2015 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2016 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2017 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2018 GST_PAD_LINK_WRONG_DIRECTION);
2020 /* Notify the parent early. See gst_pad_unlink for details. */
2021 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2022 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2023 gst_element_post_message (parent,
2024 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2025 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2027 gst_object_unref (parent);
2032 /* prepare will also lock the two pads */
2033 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2035 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2038 /* must set peers before calling the link function */
2039 GST_PAD_PEER (srcpad) = sinkpad;
2040 GST_PAD_PEER (sinkpad) = srcpad;
2042 /* check events, when something is different, mark pending */
2043 schedule_events (srcpad, sinkpad);
2045 /* get the link functions */
2046 srcfunc = GST_PAD_LINKFUNC (srcpad);
2047 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2049 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2050 /* custom link functions, execute them */
2051 GST_OBJECT_UNLOCK (sinkpad);
2052 GST_OBJECT_UNLOCK (srcpad);
2055 /* this one will call the peer link function */
2056 result = srcfunc (srcpad, sinkpad);
2057 } else if (sinkfunc) {
2058 /* if no source link function, we need to call the sink link
2059 * function ourselves. */
2060 result = sinkfunc (sinkpad, srcpad);
2063 GST_OBJECT_LOCK (srcpad);
2064 GST_OBJECT_LOCK (sinkpad);
2066 /* we released the lock, check if the same pads are linked still */
2067 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2068 goto concurrent_link;
2070 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2073 GST_OBJECT_UNLOCK (sinkpad);
2074 GST_OBJECT_UNLOCK (srcpad);
2076 /* fire off a signal to each of the pads telling them
2077 * that they've been linked */
2078 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2079 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2081 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2082 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2084 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2087 if (G_LIKELY (parent)) {
2088 gst_element_post_message (parent,
2089 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2090 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2091 gst_object_unref (parent);
2099 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2100 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2101 GST_OBJECT_UNLOCK (sinkpad);
2102 GST_OBJECT_UNLOCK (srcpad);
2104 /* The other link operation succeeded first */
2105 result = GST_PAD_LINK_WAS_LINKED;
2110 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2111 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2113 GST_PAD_PEER (srcpad) = NULL;
2114 GST_PAD_PEER (sinkpad) = NULL;
2116 GST_OBJECT_UNLOCK (sinkpad);
2117 GST_OBJECT_UNLOCK (srcpad);
2125 * @srcpad: the source #GstPad to link.
2126 * @sinkpad: the sink #GstPad to link.
2128 * Links the source pad and the sink pad.
2130 * Returns: A result code indicating if the connection worked or
2136 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2138 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2142 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2144 GstPadTemplate **template_p;
2146 /* this function would need checks if it weren't static */
2148 GST_OBJECT_LOCK (pad);
2149 template_p = &pad->padtemplate;
2150 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2151 GST_OBJECT_UNLOCK (pad);
2154 gst_pad_template_pad_created (templ, pad);
2158 * gst_pad_get_pad_template:
2161 * Gets the template for @pad.
2163 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2164 * instantiated, or %NULL if this pad has no template. Unref after
2168 gst_pad_get_pad_template (GstPad * pad)
2170 GstPadTemplate *templ;
2172 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2174 templ = GST_PAD_PAD_TEMPLATE (pad);
2176 return (templ ? gst_object_ref (templ) : NULL);
2180 * gst_pad_has_current_caps:
2181 * @pad: a #GstPad to check
2183 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2185 * Returns: TRUE when @pad has caps associated with it.
2188 gst_pad_has_current_caps (GstPad * pad)
2193 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2195 GST_OBJECT_LOCK (pad);
2196 caps = get_pad_caps (pad);
2197 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2198 "check current pad caps %" GST_PTR_FORMAT, caps);
2199 result = (caps != NULL);
2200 GST_OBJECT_UNLOCK (pad);
2206 * gst_pad_get_current_caps:
2207 * @pad: a #GstPad to get the current capabilities of.
2209 * Gets the capabilities currently configured on @pad with the last
2210 * #GST_EVENT_CAPS event.
2212 * Returns: the current caps of the pad with incremented ref-count.
2215 gst_pad_get_current_caps (GstPad * pad)
2219 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2221 GST_OBJECT_LOCK (pad);
2222 if ((result = get_pad_caps (pad)))
2223 gst_caps_ref (result);
2224 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2225 "get current pad caps %" GST_PTR_FORMAT, result);
2226 GST_OBJECT_UNLOCK (pad);
2233 * @pad: a #GstPad to set the capabilities of.
2234 * @caps: (transfer none): a #GstCaps to set.
2236 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2237 * caps on the pad will be unreffed. This function refs the caps so you should
2238 * unref if as soon as you don't need it anymore.
2239 * It is possible to set NULL caps, which will make the pad unnegotiated
2242 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2243 * or bad parameters were provided to this function.
2248 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2251 gboolean res = TRUE;
2253 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2254 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2256 event = gst_event_new_caps (caps);
2258 if (GST_PAD_IS_SRC (pad))
2259 res = gst_pad_push_event (pad, event);
2261 res = gst_pad_send_event (pad, event);
2267 * gst_pad_get_pad_template_caps:
2268 * @pad: a #GstPad to get the template capabilities from.
2270 * Gets the capabilities for @pad's template.
2272 * Returns: (transfer full): the #GstCaps of this pad template.
2273 * Unref after usage.
2276 gst_pad_get_pad_template_caps (GstPad * pad)
2278 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2280 if (GST_PAD_PAD_TEMPLATE (pad))
2281 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2283 return gst_caps_ref (GST_CAPS_ANY);
2288 * @pad: a #GstPad to get the peer of.
2290 * Gets the peer of @pad. This function refs the peer pad so
2291 * you need to unref it after use.
2293 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2298 gst_pad_get_peer (GstPad * pad)
2302 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2304 GST_OBJECT_LOCK (pad);
2305 result = GST_PAD_PEER (pad);
2307 gst_object_ref (result);
2308 GST_OBJECT_UNLOCK (pad);
2314 * gst_pad_get_allowed_caps:
2317 * Gets the capabilities of the allowed media types that can flow through
2318 * @pad and its peer.
2320 * The allowed capabilities is calculated as the intersection of the results of
2321 * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2322 * on the resulting caps.
2324 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2325 * caps when you no longer need it. This function returns NULL when @pad
2331 gst_pad_get_allowed_caps (GstPad * pad)
2338 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2340 GST_OBJECT_LOCK (pad);
2341 peer = GST_PAD_PEER (pad);
2342 if (G_UNLIKELY (peer == NULL))
2345 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2347 gst_object_ref (peer);
2348 GST_OBJECT_UNLOCK (pad);
2349 mycaps = gst_pad_query_caps (pad, NULL);
2351 peercaps = gst_pad_query_caps (peer, NULL);
2352 gst_object_unref (peer);
2354 caps = gst_caps_intersect (mycaps, peercaps);
2355 gst_caps_unref (peercaps);
2356 gst_caps_unref (mycaps);
2358 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2365 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2366 GST_OBJECT_UNLOCK (pad);
2373 * gst_pad_iterate_internal_links_default:
2374 * @pad: the #GstPad to get the internal links of.
2375 * @parent: the parent of @pad or NULL
2377 * Iterate the list of pads to which the given pad is linked to inside of
2378 * the parent element.
2379 * This is the default handler, and thus returns an iterator of all of the
2380 * pads inside the parent element with opposite direction.
2382 * The caller must free this iterator after use with gst_iterator_free().
2384 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2385 * returned pad with gst_object_unref().
2390 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2397 GstElement *eparent;
2399 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2401 if (parent != NULL && GST_IS_ELEMENT (parent)) {
2402 eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2404 GST_OBJECT_LOCK (pad);
2405 eparent = GST_PAD_PARENT (pad);
2406 if (!eparent || !GST_IS_ELEMENT (eparent))
2409 gst_object_ref (eparent);
2410 GST_OBJECT_UNLOCK (pad);
2413 if (pad->direction == GST_PAD_SRC)
2414 padlist = &eparent->sinkpads;
2416 padlist = &eparent->srcpads;
2418 GST_DEBUG_OBJECT (pad, "Making iterator");
2420 cookie = &eparent->pads_cookie;
2422 lock = GST_OBJECT_GET_LOCK (eparent);
2424 res = gst_iterator_new_list (GST_TYPE_PAD,
2425 lock, cookie, padlist, (GObject *) owner, NULL);
2427 gst_object_unref (owner);
2434 GST_OBJECT_UNLOCK (pad);
2435 GST_DEBUG_OBJECT (pad, "no parent element");
2441 * gst_pad_iterate_internal_links:
2442 * @pad: the GstPad to get the internal links of.
2444 * Gets an iterator for the pads to which the given pad is linked to inside
2445 * of the parent element.
2447 * Each #GstPad element yielded by the iterator will have its refcount increased,
2448 * so unref after use.
2450 * Free-function: gst_iterator_free
2452 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
2453 * pad does not have an iterator function configured. Use
2454 * gst_iterator_free() after usage.
2459 gst_pad_iterate_internal_links (GstPad * pad)
2461 GstIterator *res = NULL;
2464 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2466 GST_OBJECT_LOCK (pad);
2467 ACQUIRE_PARENT (pad, parent, no_parent);
2468 GST_OBJECT_UNLOCK (pad);
2470 if (GST_PAD_ITERINTLINKFUNC (pad))
2471 res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2473 RELEASE_PARENT (parent);
2480 GST_DEBUG_OBJECT (pad, "no parent");
2481 GST_OBJECT_UNLOCK (pad);
2489 * @forward: a #GstPadForwardFunction
2490 * @user_data: user data passed to @forward
2492 * Calls @forward for all internally linked pads of @pad. This function deals with
2493 * dynamically changing internal pads and will make sure that the @forward
2494 * function is only called once for each pad.
2496 * When @forward returns TRUE, no further pads will be processed.
2498 * Returns: TRUE if one of the dispatcher functions returned TRUE.
2501 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2504 gboolean result = FALSE;
2506 gboolean done = FALSE;
2507 GValue item = { 0, };
2508 GList *pushed_pads = NULL;
2510 iter = gst_pad_iterate_internal_links (pad);
2516 switch (gst_iterator_next (iter, &item)) {
2517 case GST_ITERATOR_OK:
2521 intpad = g_value_get_object (&item);
2523 /* if already pushed, skip. FIXME, find something faster to tag pads */
2524 if (intpad == NULL || g_list_find (pushed_pads, intpad)) {
2525 g_value_reset (&item);
2529 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2530 GST_DEBUG_PAD_NAME (intpad));
2531 done = result = forward (intpad, user_data);
2533 pushed_pads = g_list_prepend (pushed_pads, intpad);
2535 g_value_reset (&item);
2538 case GST_ITERATOR_RESYNC:
2539 /* We don't reset the result here because we don't push the event
2540 * again on pads that got the event already and because we need
2541 * to consider the result of the previous pushes */
2542 gst_iterator_resync (iter);
2544 case GST_ITERATOR_ERROR:
2545 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2548 case GST_ITERATOR_DONE:
2553 g_value_unset (&item);
2554 gst_iterator_free (iter);
2556 g_list_free (pushed_pads);
2566 gboolean dispatched;
2570 event_forward_func (GstPad * pad, EventData * data)
2572 /* for each pad we send to, we should ref the event; it's up
2573 * to downstream to unref again when handled. */
2574 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2575 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2577 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2579 data->dispatched = TRUE;
2586 * gst_pad_event_default:
2587 * @pad: a #GstPad to call the default event handler on.
2588 * @parent: the parent of @pad or NULL
2589 * @event: (transfer full): the #GstEvent to handle.
2591 * Invokes the default event handler for the given pad.
2593 * The EOS event will pause the task associated with @pad before it is forwarded
2594 * to all internally linked pads,
2596 * The the event is sent to all pads internally linked to @pad. This function
2597 * takes ownership of @event.
2599 * Returns: TRUE if the event was sent successfully.
2602 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2604 gboolean result, forward = TRUE;
2606 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2607 g_return_val_if_fail (event != NULL, FALSE);
2609 GST_LOG_OBJECT (pad, "default event handler");
2611 switch (GST_EVENT_TYPE (event)) {
2614 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
2615 gst_pad_pause_task (pad);
2618 case GST_EVENT_CAPS:
2619 forward = GST_PAD_IS_PROXY_CAPS (pad);
2630 data.dispatched = FALSE;
2631 data.result = FALSE;
2633 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
2635 /* for sinkpads without a parent element or without internal links, nothing
2636 * will be dispatched but we still want to return TRUE. */
2637 if (data.dispatched)
2638 result = data.result;
2643 gst_event_unref (event);
2648 /* Default accept caps implementation just checks against
2649 * the allowed caps for the pad */
2651 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
2653 /* get the caps and see if it intersects to something not empty */
2654 GstCaps *caps, *allowed;
2657 GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
2659 /* first forward the query to internally linked pads when we are dealing with
2661 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2662 if ((result = gst_pad_proxy_query_accept_caps (pad, query))) {
2667 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pad,
2668 "fallback ACCEPT_CAPS query, consider implementing a specialized version");
2670 allowed = gst_pad_query_caps (pad, NULL);
2671 gst_query_parse_accept_caps (query, &caps);
2674 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2675 result = gst_caps_is_subset (caps, allowed);
2676 gst_caps_unref (allowed);
2678 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2681 gst_query_set_accept_caps_result (query, result);
2687 /* Default caps implementation */
2689 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
2691 GstCaps *result = NULL, *filter;
2692 GstPadTemplate *templ;
2693 gboolean fixed_caps;
2695 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2697 gst_query_parse_caps (query, &filter);
2699 /* first try to proxy if we must */
2700 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2701 if ((gst_pad_proxy_query_caps (pad, query))) {
2702 gst_query_parse_caps_result (query, &result);
2707 /* no proxy or it failed, do default handling */
2708 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2710 GST_OBJECT_LOCK (pad);
2712 /* fixed caps, try the negotiated caps first */
2713 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
2714 if ((result = get_pad_caps (pad)))
2715 goto filter_done_unlock;
2718 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2719 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
2720 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
2721 goto filter_done_unlock;
2725 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2726 "non-fixed pad caps: trying pad caps");
2727 /* non fixed caps, try the negotiated caps */
2728 if ((result = get_pad_caps (pad)))
2729 goto filter_done_unlock;
2732 /* this almost never happens */
2733 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2734 result = GST_CAPS_ANY;
2737 GST_OBJECT_UNLOCK (pad);
2740 /* run the filter on the result */
2742 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2743 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
2744 GST_PTR_FORMAT, result, result, filter, filter);
2745 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2746 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2749 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2750 "using caps %p %" GST_PTR_FORMAT, result, result);
2751 result = gst_caps_ref (result);
2754 gst_query_set_caps_result (query, result);
2755 gst_caps_unref (result);
2764 gboolean dispatched;
2768 query_forward_func (GstPad * pad, QueryData * data)
2770 GST_LOG_OBJECT (pad, "query peer %p (%s) of %s:%s",
2771 data->query, GST_EVENT_TYPE_NAME (data->query), GST_DEBUG_PAD_NAME (pad));
2773 data->result |= gst_pad_peer_query (pad, data->query);
2775 data->dispatched = TRUE;
2777 /* stop on first successful reply */
2778 return data->result;
2782 * gst_pad_query_default:
2783 * @pad: a #GstPad to call the default query handler on.
2784 * @parent: the parent of @pad or NULL
2785 * @query: (transfer none): the #GstQuery to handle.
2787 * Invokes the default query handler for the given pad.
2788 * The query is sent to all pads internally linked to @pad. Note that
2789 * if there are many possible sink pads that are internally linked to
2790 * @pad, only one will be sent the query.
2791 * Multi-sinkpad elements should implement custom query handlers.
2793 * Returns: TRUE if the query was performed successfully.
2796 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
2798 gboolean forward, ret = FALSE;
2800 switch (GST_QUERY_TYPE (query)) {
2801 case GST_QUERY_SCHEDULING:
2804 case GST_QUERY_ALLOCATION:
2805 forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
2807 case GST_QUERY_ACCEPT_CAPS:
2808 ret = gst_pad_query_accept_caps_default (pad, query);
2811 case GST_QUERY_CAPS:
2812 ret = gst_pad_query_caps_default (pad, query);
2815 case GST_QUERY_POSITION:
2816 case GST_QUERY_SEEKING:
2817 case GST_QUERY_FORMATS:
2818 case GST_QUERY_LATENCY:
2819 case GST_QUERY_JITTER:
2820 case GST_QUERY_RATE:
2821 case GST_QUERY_CONVERT:
2827 GST_DEBUG_OBJECT (pad, "%sforwarding %p (%s) query", (forward ? "" : "not "),
2828 query, GST_QUERY_TYPE_NAME (query));
2834 data.dispatched = FALSE;
2835 data.result = FALSE;
2837 gst_pad_forward (pad, (GstPadForwardFunction) query_forward_func, &data);
2839 if (data.dispatched) {
2842 /* nothing dispatched, assume drained */
2843 if (GST_QUERY_TYPE (query) == GST_QUERY_DRAIN)
2853 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
2855 GstPad *pad = data->pad;
2856 GstPadProbeInfo *info = data->info;
2857 GstPadProbeType type, flags;
2858 GstPadProbeCallback callback;
2859 GstPadProbeReturn ret;
2861 /* if we have called this callback, do nothing */
2862 if (PROBE_COOKIE (hook) == data->cookie) {
2863 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2864 "hook %lu, cookie %u already called", hook->hook_id,
2865 PROBE_COOKIE (hook));
2869 PROBE_COOKIE (hook) = data->cookie;
2871 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
2874 /* one of the data types */
2875 if ((flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
2877 /* one of the scheduling types */
2878 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
2880 /* one of the blocking types must match */
2881 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
2882 (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
2884 /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
2885 if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
2886 (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
2889 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2890 "hook %lu, cookie %u with flags 0x%08x matches", hook->hook_id,
2891 PROBE_COOKIE (hook), flags);
2893 data->marshalled = TRUE;
2895 callback = (GstPadProbeCallback) hook->func;
2896 if (callback == NULL)
2899 info->id = hook->hook_id;
2901 GST_OBJECT_UNLOCK (pad);
2903 ret = callback (pad, info, hook->data);
2905 GST_OBJECT_LOCK (pad);
2908 case GST_PAD_PROBE_REMOVE:
2909 /* remove the probe */
2910 GST_DEBUG_OBJECT (pad, "asked to remove hook");
2911 cleanup_hook (pad, hook);
2913 case GST_PAD_PROBE_DROP:
2914 /* need to drop the data, make sure other probes don't get called
2916 GST_DEBUG_OBJECT (pad, "asked to drop item");
2917 info->type = GST_PAD_PROBE_TYPE_INVALID;
2918 data->dropped = TRUE;
2920 case GST_PAD_PROBE_PASS:
2921 /* inform the pad block to let things pass */
2922 GST_DEBUG_OBJECT (pad, "asked to pass item");
2926 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
2933 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2934 "hook %lu, cookie %u with flags 0x%08x does not match %08x",
2935 hook->hook_id, PROBE_COOKIE (hook), flags, info->type);
2940 /* a probe that does not take or return any data */
2941 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
2943 if (G_UNLIKELY (pad->num_probes)) { \
2944 /* pass NULL as the data item */ \
2945 GstPadProbeInfo info = { mask, 0, NULL, 0, 0 }; \
2946 ret = do_probe_callbacks (pad, &info, defaultval); \
2947 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2952 #define PROBE_FULL(pad,mask,data,offs,size,label) \
2954 if (G_UNLIKELY (pad->num_probes)) { \
2955 /* pass the data item */ \
2956 GstPadProbeInfo info = { mask, 0, data, offs, size }; \
2957 ret = do_probe_callbacks (pad, &info, GST_FLOW_OK); \
2958 /* store the possibly updated data item */ \
2959 data = GST_PAD_PROBE_INFO_DATA (&info); \
2960 /* if something went wrong, exit */ \
2961 if (G_UNLIKELY (ret != GST_FLOW_OK)) \
2966 #define PROBE_PUSH(pad,mask,data,label) \
2967 PROBE_FULL(pad, mask, data, -1, -1, label);
2968 #define PROBE_PULL(pad,mask,data,offs,size,label) \
2969 PROBE_FULL(pad, mask, data, offs, size, label);
2971 static GstFlowReturn
2972 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
2973 GstFlowReturn defaultval)
2982 data.marshalled = FALSE;
2983 data.dropped = FALSE;
2984 data.cookie = ++pad->priv->probe_cookie;
2987 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
2990 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2991 "do probes cookie %u", data.cookie);
2992 cookie = pad->priv->probe_list_cookie;
2994 g_hook_list_marshal (&pad->probes, TRUE,
2995 (GHookMarshaller) probe_hook_marshal, &data);
2997 /* if the list changed, call the new callbacks (they will not have their
2998 * cookie set to data.cookie */
2999 if (cookie != pad->priv->probe_list_cookie) {
3000 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3001 "probe list changed, restarting");
3005 /* the first item that dropped will stop the hooks and then we drop here */
3009 /* if no handler matched and we are blocking, let the item pass */
3010 if (!data.marshalled && is_block)
3013 /* At this point, all handlers returned either OK or PASS. If one handler
3014 * returned PASS, let the item pass */
3019 while (GST_PAD_IS_BLOCKED (pad)) {
3020 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3021 "we are blocked %d times", pad->num_blocked);
3023 /* we might have released the lock */
3024 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3027 /* now we block the streaming thread. It can be unlocked when we
3028 * deactivate the pad (which will also set the FLUSHING flag) or
3029 * when the pad is unblocked. A flushing event will also unblock
3030 * the pad after setting the FLUSHING flag. */
3031 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3032 "Waiting to be unblocked or set flushing");
3033 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3034 GST_PAD_BLOCK_WAIT (pad);
3035 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3036 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3038 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3048 GST_DEBUG_OBJECT (pad, "pad is flushing");
3049 return GST_FLOW_FLUSHING;
3053 GST_DEBUG_OBJECT (pad, "data is dropped");
3054 return GST_FLOW_CUSTOM_SUCCESS;
3058 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3059 GST_DEBUG_OBJECT (pad, "data is passed");
3067 * gst_pad_get_offset:
3070 * Get the offset applied to the running time of @pad. @pad has to be a source
3073 * Returns: the offset.
3076 gst_pad_get_offset (GstPad * pad)
3080 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3082 GST_OBJECT_LOCK (pad);
3083 result = pad->offset;
3084 GST_OBJECT_UNLOCK (pad);
3090 * gst_pad_set_offset:
3092 * @offset: the offset
3094 * Set the offset that will be applied to the running time of @pad.
3097 gst_pad_set_offset (GstPad * pad, gint64 offset)
3101 g_return_if_fail (GST_IS_PAD (pad));
3103 GST_OBJECT_LOCK (pad);
3104 /* if nothing changed, do nothing */
3105 if (pad->offset == offset)
3108 pad->offset = offset;
3109 GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3111 /* sinkpads will apply their offset the next time a segment
3112 * event is received. */
3113 if (GST_PAD_IS_SINK (pad))
3116 /* resend the last segment event on next buffer push */
3117 if ((ev = find_event_by_type (pad, GST_EVENT_SEGMENT, 0))) {
3118 ev->received = FALSE;
3119 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3123 GST_OBJECT_UNLOCK (pad);
3126 /* should be called with pad LOCK */
3128 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3130 GstFlowReturn *data = user_data;
3131 GstEvent *event = ev->event;
3134 GST_DEBUG_OBJECT (pad, "event %s was already received",
3135 GST_EVENT_TYPE_NAME (event));
3139 *data = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
3140 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3144 ev->received = TRUE;
3145 GST_DEBUG_OBJECT (pad, "event %s marked received",
3146 GST_EVENT_TYPE_NAME (event));
3148 case GST_FLOW_NOT_LINKED:
3149 /* not linked is not a problem, we are sticky so the event will be
3151 GST_DEBUG_OBJECT (pad, "pad was not linked");
3152 *data = GST_FLOW_OK;
3155 GST_DEBUG_OBJECT (pad, "mark pending events");
3156 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3159 return *data == GST_FLOW_OK;
3162 /* check sticky events and push them when needed. should be called
3164 static inline GstFlowReturn
3165 check_sticky (GstPad * pad)
3167 GstFlowReturn ret = GST_FLOW_OK;
3169 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3170 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3172 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3173 events_foreach (pad, push_sticky, &ret);
3181 * @pad: a #GstPad to invoke the default query on.
3182 * @query: (transfer none): the #GstQuery to perform.
3184 * Dispatches a query to a pad. The query should have been allocated by the
3185 * caller via one of the type-specific allocation functions. The element that
3186 * the pad belongs to is responsible for filling the query with an appropriate
3187 * response, which should then be parsed with a type-specific query parsing
3190 * Again, the caller is responsible for both the allocation and deallocation of
3191 * the query structure.
3193 * Please also note that some queries might need a running pipeline to work.
3195 * Returns: TRUE if the query could be performed.
3198 gst_pad_query (GstPad * pad, GstQuery * query)
3201 gboolean res, serialized;
3202 GstPadQueryFunction func;
3203 GstPadProbeType type;
3206 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3207 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3209 if (GST_PAD_IS_SRC (pad)) {
3210 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3211 goto wrong_direction;
3212 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3213 } else if (GST_PAD_IS_SINK (pad)) {
3214 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3215 goto wrong_direction;
3216 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3218 goto unknown_direction;
3220 GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
3221 GST_QUERY_TYPE_NAME (query));
3223 serialized = GST_QUERY_IS_SERIALIZED (query);
3224 if (G_UNLIKELY (serialized))
3225 GST_PAD_STREAM_LOCK (pad);
3227 GST_OBJECT_LOCK (pad);
3228 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3229 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3230 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3232 ACQUIRE_PARENT (pad, parent, no_parent);
3233 GST_OBJECT_UNLOCK (pad);
3235 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3238 res = func (pad, parent, query);
3240 RELEASE_PARENT (parent);
3242 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3243 GST_QUERY_TYPE_NAME (query), res);
3248 GST_OBJECT_LOCK (pad);
3249 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3250 GST_OBJECT_UNLOCK (pad);
3252 if (G_UNLIKELY (serialized))
3253 GST_PAD_STREAM_UNLOCK (pad);
3260 g_warning ("pad %s:%s query %s in wrong direction",
3261 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3266 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3271 GST_DEBUG_OBJECT (pad, "had no parent");
3272 GST_OBJECT_UNLOCK (pad);
3273 if (G_UNLIKELY (serialized))
3274 GST_PAD_STREAM_UNLOCK (pad);
3279 GST_DEBUG_OBJECT (pad, "had no query function");
3280 RELEASE_PARENT (parent);
3281 if (G_UNLIKELY (serialized))
3282 GST_PAD_STREAM_UNLOCK (pad);
3287 GST_DEBUG_OBJECT (pad, "query failed");
3288 if (G_UNLIKELY (serialized))
3289 GST_PAD_STREAM_UNLOCK (pad);
3294 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3295 GST_OBJECT_UNLOCK (pad);
3296 if (G_UNLIKELY (serialized))
3297 GST_PAD_STREAM_UNLOCK (pad);
3299 /* if a probe dropped, we don't sent it further but assume that the probe
3300 * answered the query and return TRUE */
3301 if (ret == GST_FLOW_CUSTOM_SUCCESS)
3311 * gst_pad_peer_query:
3312 * @pad: a #GstPad to invoke the peer query on.
3313 * @query: (transfer none): the #GstQuery to perform.
3315 * Performs gst_pad_query() on the peer of @pad.
3317 * The caller is responsible for both the allocation and deallocation of
3318 * the query structure.
3320 * Returns: TRUE if the query could be performed. This function returns %FALSE
3321 * if @pad has no peer.
3326 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3329 GstPadProbeType type;
3330 gboolean res, serialized;
3333 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3334 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3336 if (GST_PAD_IS_SRC (pad)) {
3337 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3338 goto wrong_direction;
3339 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3340 } else if (GST_PAD_IS_SINK (pad)) {
3341 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3342 goto wrong_direction;
3343 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3345 goto unknown_direction;
3347 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3348 GST_QUERY_TYPE_NAME (query));
3350 serialized = GST_QUERY_IS_SERIALIZED (query);
3352 GST_OBJECT_LOCK (pad);
3353 if (GST_PAD_IS_SRC (pad) && serialized) {
3354 /* all serialized queries on the srcpad trigger push of
3356 if (!check_sticky (pad) == GST_FLOW_OK)
3360 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3361 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3362 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3364 peerpad = GST_PAD_PEER (pad);
3365 if (G_UNLIKELY (peerpad == NULL))
3368 gst_object_ref (peerpad);
3369 GST_OBJECT_UNLOCK (pad);
3371 res = gst_pad_query (peerpad, query);
3373 gst_object_unref (peerpad);
3378 GST_OBJECT_LOCK (pad);
3379 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3380 GST_OBJECT_UNLOCK (pad);
3387 g_warning ("pad %s:%s query %s in wrong direction",
3388 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3393 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3398 GST_WARNING_OBJECT (pad, "could not send sticky events");
3399 GST_OBJECT_UNLOCK (pad);
3404 GST_WARNING_OBJECT (pad, "pad has no peer");
3405 GST_OBJECT_UNLOCK (pad);
3410 GST_DEBUG_OBJECT (pad, "query failed");
3415 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3416 GST_OBJECT_UNLOCK (pad);
3418 /* if a probe dropped, we don't sent it further but assume that the probe
3419 * answered the query and return TRUE */
3420 if (ret == GST_FLOW_CUSTOM_SUCCESS)
3429 /**********************************************************************
3430 * Data passing functions
3433 /* this is the chain function that does not perform the additional argument
3434 * checking for that little extra speed.
3436 static inline GstFlowReturn
3437 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3442 GST_PAD_STREAM_LOCK (pad);
3444 GST_OBJECT_LOCK (pad);
3445 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3448 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3451 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3453 PROBE_PUSH (pad, type, data, probe_stopped);
3455 parent = GST_OBJECT_PARENT (pad);
3456 GST_OBJECT_UNLOCK (pad);
3458 /* NOTE: we read the chainfunc unlocked.
3459 * we cannot hold the lock for the pad so we might send
3460 * the data to the wrong function. This is not really a
3461 * problem since functions are assigned at creation time
3462 * and don't change that often... */
3463 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3464 GstPadChainFunction chainfunc;
3466 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3469 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3470 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3471 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3473 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
3475 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3476 "called chainfunction &%s with buffer %p, returned %s",
3477 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3479 GstPadChainListFunction chainlistfunc;
3481 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3484 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3485 "calling chainlistfunction &%s",
3486 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3488 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
3490 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3491 "called chainlistfunction &%s, returned %s",
3492 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3495 GST_PAD_STREAM_UNLOCK (pad);
3502 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3503 "chaining, but pad was flushing");
3504 GST_OBJECT_UNLOCK (pad);
3505 GST_PAD_STREAM_UNLOCK (pad);
3506 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3507 return GST_FLOW_FLUSHING;
3511 g_critical ("chain on pad %s:%s but it was not in push mode",
3512 GST_DEBUG_PAD_NAME (pad));
3513 GST_OBJECT_UNLOCK (pad);
3514 GST_PAD_STREAM_UNLOCK (pad);
3515 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3516 return GST_FLOW_ERROR;
3520 GST_OBJECT_UNLOCK (pad);
3521 GST_PAD_STREAM_UNLOCK (pad);
3522 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3525 case GST_FLOW_CUSTOM_SUCCESS:
3526 GST_DEBUG_OBJECT (pad, "dropped buffer");
3530 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3537 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3538 g_critical ("chain on pad %s:%s but it has no chainfunction",
3539 GST_DEBUG_PAD_NAME (pad));
3540 GST_PAD_STREAM_UNLOCK (pad);
3541 return GST_FLOW_NOT_SUPPORTED;
3547 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3548 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3551 * Chain a buffer to @pad.
3553 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3555 * If the buffer type is not acceptable for @pad (as negotiated with a
3556 * preceeding GST_EVENT_CAPS event), this function returns
3557 * #GST_FLOW_NOT_NEGOTIATED.
3559 * The function proceeds calling the chain function installed on @pad (see
3560 * gst_pad_set_chain_function()) and the return value of that function is
3561 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3564 * In all cases, success or failure, the caller loses its reference to @buffer
3565 * after calling this function.
3567 * Returns: a #GstFlowReturn from the pad.
3572 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3574 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3575 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3576 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3578 return gst_pad_chain_data_unchecked (pad,
3579 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3582 static GstFlowReturn
3583 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
3584 GstBufferList * list)
3590 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3592 len = gst_buffer_list_length (list);
3595 for (i = 0; i < len; i++) {
3596 buffer = gst_buffer_list_get (list, i);
3598 gst_pad_chain_data_unchecked (pad,
3599 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3600 gst_buffer_ref (buffer));
3601 if (ret != GST_FLOW_OK)
3604 gst_buffer_list_unref (list);
3610 * gst_pad_chain_list:
3611 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3612 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3615 * Chain a bufferlist to @pad.
3617 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3619 * If @pad was not negotiated properly with a CAPS event, this function
3620 * returns #GST_FLOW_NOT_NEGOTIATED.
3622 * The function proceeds calling the chainlist function installed on @pad (see
3623 * gst_pad_set_chain_list_function()) and the return value of that function is
3624 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3625 * chainlist function.
3627 * In all cases, success or failure, the caller loses its reference to @list
3628 * after calling this function.
3632 * Returns: a #GstFlowReturn from the pad.
3637 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3639 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3640 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3641 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3643 return gst_pad_chain_data_unchecked (pad,
3644 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3647 static GstFlowReturn
3648 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3653 GST_OBJECT_LOCK (pad);
3654 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3657 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3660 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
3663 /* do block probes */
3664 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3666 /* do post-blocking probes */
3667 PROBE_PUSH (pad, type, data, probe_stopped);
3669 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3672 /* take ref to peer pad before releasing the lock */
3673 gst_object_ref (peer);
3675 GST_OBJECT_UNLOCK (pad);
3677 ret = gst_pad_chain_data_unchecked (peer, type, data);
3679 gst_object_unref (peer);
3681 GST_OBJECT_LOCK (pad);
3683 if (pad->priv->using == 0) {
3684 /* pad is not active anymore, trigger idle callbacks */
3685 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3686 probe_stopped, ret);
3688 GST_OBJECT_UNLOCK (pad);
3692 /* ERROR recovery here */
3696 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3697 "pushing, but pad was flushing");
3698 GST_OBJECT_UNLOCK (pad);
3699 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3700 return GST_FLOW_FLUSHING;
3704 g_critical ("pushing on pad %s:%s but it was not activated in push mode",
3705 GST_DEBUG_PAD_NAME (pad));
3706 GST_OBJECT_UNLOCK (pad);
3707 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3708 return GST_FLOW_ERROR;
3712 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3713 "error pushing events, return %s", gst_flow_get_name (ret));
3714 GST_OBJECT_UNLOCK (pad);
3715 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3720 GST_OBJECT_UNLOCK (pad);
3721 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3724 case GST_FLOW_CUSTOM_SUCCESS:
3725 GST_DEBUG_OBJECT (pad, "dropped buffer");
3729 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3736 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3737 "pushing, but it was not linked");
3738 GST_OBJECT_UNLOCK (pad);
3739 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3740 return GST_FLOW_NOT_LINKED;
3746 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3747 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3750 * Pushes a buffer to the peer of @pad.
3752 * This function will call installed block probes before triggering any
3753 * installed data probes.
3755 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3756 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3759 * In all cases, success or failure, the caller loses its reference to @buffer
3760 * after calling this function.
3762 * Returns: a #GstFlowReturn from the peer pad.
3767 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3769 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3770 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3771 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3773 return gst_pad_push_data (pad,
3774 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3778 * gst_pad_push_list:
3779 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3780 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3783 * Pushes a buffer list to the peer of @pad.
3785 * This function will call installed block probes before triggering any
3786 * installed data probes.
3788 * The function proceeds calling the chain function on the peer pad and returns
3789 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3790 * be returned. If the peer pad does not have any installed chainlist function
3791 * every group buffer of the list will be merged into a normal #GstBuffer and
3792 * chained via gst_pad_chain().
3794 * In all cases, success or failure, the caller loses its reference to @list
3795 * after calling this function.
3797 * Returns: a #GstFlowReturn from the peer pad.
3804 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3806 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3807 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3808 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3810 return gst_pad_push_data (pad,
3811 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3814 static GstFlowReturn
3815 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3816 GstBuffer ** buffer)
3819 GstPadGetRangeFunction getrangefunc;
3822 GST_PAD_STREAM_LOCK (pad);
3824 GST_OBJECT_LOCK (pad);
3825 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3828 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
3831 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
3834 /* when one of the probes returns DROPPED, probe_stopped will be called
3835 * and we skip calling the getrange function, *buffer should then contain a
3836 * valid result buffer */
3837 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3838 *buffer, offset, size, probe_stopped);
3840 ACQUIRE_PARENT (pad, parent, no_parent);
3841 GST_OBJECT_UNLOCK (pad);
3843 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3846 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3847 "calling getrangefunc %s, offset %"
3848 G_GUINT64_FORMAT ", size %u",
3849 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3851 ret = getrangefunc (pad, parent, offset, size, buffer);
3853 RELEASE_PARENT (parent);
3855 if (G_UNLIKELY (ret != GST_FLOW_OK))
3856 goto get_range_failed;
3858 /* can only fire the signal if we have a valid buffer */
3859 GST_OBJECT_LOCK (pad);
3861 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3862 *buffer, offset, size, probe_stopped_unref);
3863 GST_OBJECT_UNLOCK (pad);
3865 GST_PAD_STREAM_UNLOCK (pad);
3872 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3873 "getrange, but pad was flushing");
3874 GST_OBJECT_UNLOCK (pad);
3875 GST_PAD_STREAM_UNLOCK (pad);
3876 return GST_FLOW_FLUSHING;
3880 g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
3881 GST_DEBUG_PAD_NAME (pad));
3882 GST_OBJECT_UNLOCK (pad);
3883 GST_PAD_STREAM_UNLOCK (pad);
3884 return GST_FLOW_ERROR;
3888 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
3889 GST_OBJECT_UNLOCK (pad);
3890 GST_PAD_STREAM_UNLOCK (pad);
3895 GST_DEBUG_OBJECT (pad, "no parent");
3896 GST_OBJECT_UNLOCK (pad);
3897 GST_PAD_STREAM_UNLOCK (pad);
3898 return GST_FLOW_FLUSHING;
3902 g_critical ("getrange on pad %s:%s but it has no getrangefunction",
3903 GST_DEBUG_PAD_NAME (pad));
3904 RELEASE_PARENT (parent);
3905 GST_PAD_STREAM_UNLOCK (pad);
3906 return GST_FLOW_NOT_SUPPORTED;
3910 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3911 "probe returned %s", gst_flow_get_name (ret));
3912 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
3914 /* the probe filled the buffer and asks us to not call the getrange
3915 * anymore, we continue with the post probes then. */
3916 GST_DEBUG_OBJECT (pad, "handled buffer");
3920 /* no buffer, we are EOS */
3921 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
3925 GST_OBJECT_UNLOCK (pad);
3926 GST_PAD_STREAM_UNLOCK (pad);
3930 probe_stopped_unref:
3932 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3933 "probe returned %s", gst_flow_get_name (ret));
3934 /* if we drop here, it signals EOS */
3935 if (ret == GST_FLOW_CUSTOM_SUCCESS)
3937 GST_OBJECT_UNLOCK (pad);
3938 GST_PAD_STREAM_UNLOCK (pad);
3939 gst_buffer_unref (*buffer);
3945 GST_PAD_STREAM_UNLOCK (pad);
3947 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3948 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3949 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
3955 * gst_pad_get_range:
3956 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
3957 * @offset: The start offset of the buffer
3958 * @size: The length of the buffer
3959 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
3960 * returns #GST_FLOW_ERROR if %NULL.
3962 * When @pad is flushing this function returns #GST_FLOW_FLUSHING
3963 * immediately and @buffer is %NULL.
3965 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
3966 * description of a getrange function. If @pad has no getrange function
3967 * installed (see gst_pad_set_getrange_function()) this function returns
3968 * #GST_FLOW_NOT_SUPPORTED.
3970 * @buffer must point to a variable holding NULL or a variable that
3971 * points to a #GstBuffer that will be filled with the result data.
3973 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
3975 * Returns: a #GstFlowReturn from the pad.
3980 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
3981 GstBuffer ** buffer)
3983 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3984 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3985 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3986 g_return_val_if_fail (*buffer == NULL
3987 || GST_IS_BUFFER (*buffer), GST_FLOW_ERROR);
3989 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
3993 * gst_pad_pull_range:
3994 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3995 * @offset: The start offset of the buffer
3996 * @size: The length of the buffer
3997 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
3998 * GST_FLOW_ERROR if %NULL.
4000 * Pulls a @buffer from the peer pad.
4002 * This function will first trigger the pad block signal if it was
4005 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4006 * function returns the result of gst_pad_get_range() on the peer pad.
4007 * See gst_pad_get_range() for a list of return values and for the
4008 * semantics of the arguments of this function.
4010 * @buffer must point to a variable holding NULL or a variable that
4011 * points to a #GstBuffer that will be filled with the result data.
4013 * Returns: a #GstFlowReturn from the peer pad.
4014 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4015 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4016 * @buffer may not be used or freed when any other return value than
4017 * #GST_FLOW_OK is returned.
4022 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4023 GstBuffer ** buffer)
4028 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4029 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4030 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4031 g_return_val_if_fail (*buffer == NULL
4032 || GST_IS_BUFFER (*buffer), GST_FLOW_ERROR);
4034 GST_OBJECT_LOCK (pad);
4035 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4038 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4041 /* when one of the probes returns DROPPED, probe_stopped will be
4042 * called and we skip calling the peer getrange function. *buffer should then
4043 * contain a valid buffer */
4044 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4045 *buffer, offset, size, probe_stopped);
4047 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4050 gst_object_ref (peer);
4052 GST_OBJECT_UNLOCK (pad);
4054 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
4056 gst_object_unref (peer);
4058 GST_OBJECT_LOCK (pad);
4060 if (pad->priv->using == 0) {
4061 /* pad is not active anymore, trigger idle callbacks */
4062 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4063 probe_stopped_unref, ret);
4066 if (G_UNLIKELY (ret != GST_FLOW_OK))
4067 goto pull_range_failed;
4070 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4071 *buffer, offset, size, probe_stopped_unref);
4073 GST_OBJECT_UNLOCK (pad);
4077 /* ERROR recovery here */
4080 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4081 "pullrange, but pad was flushing");
4082 GST_OBJECT_UNLOCK (pad);
4083 return GST_FLOW_FLUSHING;
4087 g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
4088 GST_DEBUG_PAD_NAME (pad));
4089 GST_OBJECT_UNLOCK (pad);
4090 return GST_FLOW_ERROR;
4094 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4095 gst_flow_get_name (ret));
4096 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4098 /* the probe filled the buffer and asks us to not forward to the peer
4099 * anymore, we continue with the post probes then */
4100 GST_DEBUG_OBJECT (pad, "handled buffer");
4104 /* no buffer, we are EOS then */
4105 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4109 GST_OBJECT_UNLOCK (pad);
4114 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4115 "pulling range, but it was not linked");
4116 GST_OBJECT_UNLOCK (pad);
4117 return GST_FLOW_NOT_LINKED;
4122 GST_OBJECT_UNLOCK (pad);
4123 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4124 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4125 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4128 probe_stopped_unref:
4130 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4131 "post probe returned %s", gst_flow_get_name (ret));
4132 GST_OBJECT_UNLOCK (pad);
4133 /* if we drop here, it signals EOS */
4134 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4136 gst_buffer_unref (*buffer);
4137 } else if (ret == GST_FLOW_OK)
4138 gst_buffer_unref (*buffer);
4145 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event, gboolean locked)
4150 gboolean res = FALSE;
4151 const gchar *name = NULL;
4153 type = GST_EVENT_TYPE (event);
4154 if (type & GST_EVENT_TYPE_STICKY_MULTI)
4155 name = gst_structure_get_name (gst_event_get_structure (event));
4157 events = pad->priv->events;
4160 for (i = 0; i < len; i++) {
4161 PadEvent *ev = &g_array_index (events, PadEvent, i);
4163 if (ev->event == NULL)
4166 if (type == GST_EVENT_TYPE (ev->event)) {
4167 /* matching types, check matching name if needed */
4168 if (name && !gst_event_has_name (ev->event, name))
4172 if ((res = gst_event_replace (&ev->event, event)))
4173 ev->received = FALSE;
4179 ev.event = gst_event_ref (event);
4180 ev.received = FALSE;
4181 g_array_append_val (events, ev);
4186 pad->priv->events_cookie++;
4187 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4189 GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4191 switch (GST_EVENT_TYPE (event)) {
4192 case GST_EVENT_CAPS:
4194 GST_OBJECT_UNLOCK (pad);
4196 GST_DEBUG_OBJECT (pad, "notify caps");
4197 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4200 GST_OBJECT_LOCK (pad);
4209 /* should be called with pad LOCK */
4210 static GstFlowReturn
4211 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
4212 GstPadProbeType type)
4216 GstEventType event_type;
4218 /* Two checks to be made:
4219 * . (un)set the FLUSHING flag for flushing events,
4220 * . handle pad blocking */
4221 event_type = GST_EVENT_TYPE (event);
4222 switch (event_type) {
4223 case GST_EVENT_FLUSH_START:
4224 GST_PAD_SET_FLUSHING (pad);
4226 GST_PAD_BLOCK_BROADCAST (pad);
4227 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4229 case GST_EVENT_FLUSH_STOP:
4230 GST_PAD_UNSET_FLUSHING (pad);
4232 /* Remove sticky EOS events */
4233 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4234 remove_event_by_type (pad, GST_EVENT_EOS);
4236 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4240 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4243 switch (GST_EVENT_TYPE (event)) {
4244 case GST_EVENT_SEGMENT:
4245 /* pass the adjusted segment event on. We need to do this even if
4246 * there is no peer pad because of the probes. */
4247 event = apply_pad_offset (pad, event);
4249 case GST_EVENT_RECONFIGURE:
4250 if (GST_PAD_IS_SINK (pad))
4251 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4256 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4257 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4262 /* send probes after modifying the events above */
4263 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4265 /* now check the peer pad */
4266 peerpad = GST_PAD_PEER (pad);
4267 if (peerpad == NULL)
4270 gst_object_ref (peerpad);
4272 GST_OBJECT_UNLOCK (pad);
4274 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4275 event, GST_EVENT_TYPE_NAME (event), peerpad);
4277 ret = gst_pad_send_event_unchecked (peerpad, event, type);
4279 /* Note: we gave away ownership of the event at this point but we can still
4280 * print the old pointer */
4281 GST_LOG_OBJECT (pad,
4282 "sent event %p to peerpad %" GST_PTR_FORMAT ", ret %s", event, peerpad,
4283 gst_flow_get_name (ret));
4285 gst_object_unref (peerpad);
4287 GST_OBJECT_LOCK (pad);
4289 if (pad->priv->using == 0) {
4290 /* pad is not active anymore, trigger idle callbacks */
4291 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4292 idle_probe_stopped, ret);
4296 /* ERROR handling */
4299 GST_DEBUG_OBJECT (pad, "We're flushing");
4300 gst_event_unref (event);
4301 return GST_FLOW_FLUSHING;
4305 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4306 gst_event_unref (event);
4309 case GST_FLOW_CUSTOM_SUCCESS:
4310 GST_DEBUG_OBJECT (pad, "dropped event");
4314 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4321 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4322 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4323 gst_event_unref (event);
4324 return GST_FLOW_NOT_LINKED;
4328 GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
4334 * gst_pad_push_event:
4335 * @pad: a #GstPad to push the event to.
4336 * @event: (transfer full): the #GstEvent to send to the pad.
4338 * Sends the event to the peer of the given pad. This function is
4339 * mainly used by elements to send events to their peer
4342 * This function takes owership of the provided event so you should
4343 * gst_event_ref() it if you want to reuse the event after this call.
4345 * Returns: TRUE if the event was handled.
4350 gst_pad_push_event (GstPad * pad, GstEvent * event)
4353 GstPadProbeType type;
4354 gboolean sticky, serialized;
4356 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4357 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4359 if (GST_PAD_IS_SRC (pad)) {
4360 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4361 goto wrong_direction;
4362 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4363 } else if (GST_PAD_IS_SINK (pad)) {
4364 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4365 goto wrong_direction;
4366 /* events pushed on sinkpad never are sticky */
4367 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4369 goto unknown_direction;
4371 GST_OBJECT_LOCK (pad);
4372 sticky = GST_EVENT_IS_STICKY (event);
4373 serialized = GST_EVENT_IS_SERIALIZED (event);
4376 /* can't store on flushing pads */
4377 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4380 /* srcpad sticky events are store immediately, the received flag is set
4381 * to FALSE and will be set to TRUE when we can successfully push the
4382 * event to the peer pad */
4383 if (gst_pad_store_sticky_event (pad, event, TRUE)) {
4384 GST_DEBUG_OBJECT (pad, "event %s updated", GST_EVENT_TYPE_NAME (event));
4387 if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
4388 /* all serialized or sticky events on the srcpad trigger push of
4390 res = (check_sticky (pad) == GST_FLOW_OK);
4393 /* other events are pushed right away */
4394 res = (gst_pad_push_event_unchecked (pad, event, type) == GST_FLOW_OK);
4396 gst_event_unref (event);
4399 GST_OBJECT_UNLOCK (pad);
4403 /* ERROR handling */
4406 g_warning ("pad %s:%s pushing %s event in wrong direction",
4407 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4408 gst_event_unref (event);
4413 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4414 gst_event_unref (event);
4419 GST_DEBUG_OBJECT (pad, "We're flushing");
4420 GST_OBJECT_UNLOCK (pad);
4421 gst_event_unref (event);
4426 /* Check if we can call the event function with the given event */
4427 static GstFlowReturn
4428 pre_eventfunc_check (GstPad * pad, GstEvent * event)
4430 GstCaps *caps, *templ;
4432 switch (GST_EVENT_TYPE (event)) {
4433 case GST_EVENT_CAPS:
4435 /* backwards compatibility mode for caps */
4436 gst_event_parse_caps (event, &caps);
4438 /* See if pad accepts the caps */
4439 templ = gst_pad_get_pad_template_caps (pad);
4440 if (!gst_caps_is_subset (caps, templ))
4443 gst_caps_unref (templ);
4454 gst_caps_unref (templ);
4455 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4456 "caps %" GST_PTR_FORMAT " not accepted", caps);
4457 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4458 "no intersection with template %" GST_PTR_FORMAT, templ);
4459 return GST_FLOW_NOT_NEGOTIATED;
4463 static GstFlowReturn
4464 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
4465 GstPadProbeType type)
4468 GstEventType event_type;
4469 gboolean serialized, need_unlock = FALSE, sticky;
4470 GstPadEventFunction eventfunc;
4473 GST_OBJECT_LOCK (pad);
4474 if (GST_PAD_IS_SINK (pad))
4475 serialized = GST_EVENT_IS_SERIALIZED (event);
4478 sticky = GST_EVENT_IS_STICKY (event);
4479 event_type = GST_EVENT_TYPE (event);
4480 switch (event_type) {
4481 case GST_EVENT_FLUSH_START:
4482 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4483 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4485 /* can't even accept a flush begin event when flushing */
4486 if (GST_PAD_IS_FLUSHING (pad))
4489 GST_PAD_SET_FLUSHING (pad);
4490 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4492 case GST_EVENT_FLUSH_STOP:
4493 if (G_LIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_NONE)) {
4494 GST_PAD_UNSET_FLUSHING (pad);
4495 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4497 /* Remove pending EOS events */
4498 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4499 remove_event_by_type (pad, GST_EVENT_EOS);
4501 GST_OBJECT_UNLOCK (pad);
4502 /* grab stream lock */
4503 GST_PAD_STREAM_LOCK (pad);
4505 GST_OBJECT_LOCK (pad);
4506 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4509 case GST_EVENT_RECONFIGURE:
4510 if (GST_PAD_IS_SRC (pad))
4511 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4513 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4514 "have event type %" GST_PTR_FORMAT, event);
4516 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4520 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4521 GST_OBJECT_UNLOCK (pad);
4522 GST_PAD_STREAM_LOCK (pad);
4524 GST_OBJECT_LOCK (pad);
4525 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4529 switch (GST_EVENT_TYPE (event)) {
4530 case GST_EVENT_SEGMENT:
4531 event = apply_pad_offset (pad, event);
4537 /* now do the probe */
4539 type | GST_PAD_PROBE_TYPE_PUSH |
4540 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4542 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4546 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4549 ACQUIRE_PARENT (pad, parent, no_parent);
4550 GST_OBJECT_UNLOCK (pad);
4552 ret = pre_eventfunc_check (pad, event);
4553 if (G_UNLIKELY (ret != GST_FLOW_OK))
4554 goto precheck_failed;
4557 gst_event_ref (event);
4559 if (eventfunc (pad, parent, event)) {
4562 /* something went wrong */
4563 switch (event_type) {
4564 case GST_EVENT_CAPS:
4565 ret = GST_FLOW_NOT_NEGOTIATED;
4568 ret = GST_FLOW_ERROR;
4572 RELEASE_PARENT (parent);
4574 GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
4577 if (ret == GST_FLOW_OK) {
4578 /* after the event function accepted the event, we can store the sticky
4579 * event on the pad */
4580 gst_pad_store_sticky_event (pad, event, FALSE);
4582 gst_event_unref (event);
4586 GST_PAD_STREAM_UNLOCK (pad);
4590 /* ERROR handling */
4593 GST_OBJECT_UNLOCK (pad);
4595 GST_PAD_STREAM_UNLOCK (pad);
4596 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4597 "Received event on flushing pad. Discarding");
4598 gst_event_unref (event);
4599 return GST_FLOW_FLUSHING;
4603 GST_OBJECT_UNLOCK (pad);
4605 GST_PAD_STREAM_UNLOCK (pad);
4606 gst_event_unref (event);
4609 case GST_FLOW_CUSTOM_SUCCESS:
4610 GST_DEBUG_OBJECT (pad, "dropped event");
4614 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4621 g_warning ("pad %s:%s has no event handler, file a bug.",
4622 GST_DEBUG_PAD_NAME (pad));
4623 GST_OBJECT_UNLOCK (pad);
4625 GST_PAD_STREAM_UNLOCK (pad);
4626 gst_event_unref (event);
4627 return GST_FLOW_NOT_SUPPORTED;
4631 GST_DEBUG_OBJECT (pad, "no parent");
4632 GST_OBJECT_UNLOCK (pad);
4634 GST_PAD_STREAM_UNLOCK (pad);
4635 gst_event_unref (event);
4636 return GST_FLOW_FLUSHING;
4640 GST_DEBUG_OBJECT (pad, "pre event check failed");
4641 RELEASE_PARENT (parent);
4643 GST_PAD_STREAM_UNLOCK (pad);
4644 gst_event_unref (event);
4650 * gst_pad_send_event:
4651 * @pad: a #GstPad to send the event to.
4652 * @event: (transfer full): the #GstEvent to send to the pad.
4654 * Sends the event to the pad. This function can be used
4655 * by applications to send events in the pipeline.
4657 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4658 * sink pad, @event should be a downstream event. For example, you would not
4659 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4660 * Furthermore, some downstream events have to be serialized with data flow,
4661 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4662 * the event needs to be serialized with data flow, this function will take the
4663 * pad's stream lock while calling its event function.
4665 * To find out whether an event type is upstream, downstream, or downstream and
4666 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4667 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4668 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4669 * plugin doesn't need to bother itself with this information; the core handles
4670 * all necessary locks and checks.
4672 * This function takes owership of the provided event so you should
4673 * gst_event_ref() it if you want to reuse the event after this call.
4675 * Returns: TRUE if the event was handled.
4678 gst_pad_send_event (GstPad * pad, GstEvent * event)
4681 GstPadProbeType type;
4683 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4684 g_return_val_if_fail (event != NULL, FALSE);
4686 if (GST_PAD_IS_SINK (pad)) {
4687 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4688 goto wrong_direction;
4689 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4690 } else if (GST_PAD_IS_SRC (pad)) {
4691 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4692 goto wrong_direction;
4693 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4695 goto unknown_direction;
4697 if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
4704 /* ERROR handling */
4707 g_warning ("pad %s:%s sending %s event in wrong direction",
4708 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4709 gst_event_unref (event);
4714 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4715 gst_event_unref (event);
4721 * gst_pad_set_element_private:
4722 * @pad: the #GstPad to set the private data of.
4723 * @priv: The private data to attach to the pad.
4725 * Set the given private data gpointer on the pad.
4726 * This function can only be used by the element that owns the pad.
4727 * No locking is performed in this function.
4730 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4732 pad->element_private = priv;
4736 * gst_pad_get_element_private:
4737 * @pad: the #GstPad to get the private data of.
4739 * Gets the private data of a pad.
4740 * No locking is performed in this function.
4742 * Returns: (transfer none): a #gpointer to the private data.
4745 gst_pad_get_element_private (GstPad * pad)
4747 return pad->element_private;
4751 * gst_pad_get_sticky_event:
4752 * @pad: the #GstPad to get the event from.
4753 * @event_type: the #GstEventType that should be retrieved.
4754 * @idx: the index of the event
4756 * Returns a new reference of the sticky event of type @event_type
4759 * Returns: (transfer full): a #GstEvent of type @event_type or NULL when no
4760 * event of @event_type was on @pad. Unref after usage.
4763 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
4765 GstEvent *event = NULL;
4768 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4769 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4771 GST_OBJECT_LOCK (pad);
4772 ev = find_event_by_type (pad, event_type, idx);
4773 if (ev && (event = ev->event))
4774 gst_event_ref (event);
4775 GST_OBJECT_UNLOCK (pad);
4782 GstPadStickyEventsForeachFunction func;
4787 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
4789 ForeachDispatch *data = user_data;
4792 GST_OBJECT_UNLOCK (pad);
4794 ret = data->func (pad, &ev->event, data->user_data);
4796 GST_OBJECT_LOCK (pad);
4802 * gst_pad_sticky_events_foreach:
4803 * @pad: the #GstPad that should be used for iteration.
4804 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
4805 * should be called for every event.
4806 * @user_data: (closure): the optional user data.
4808 * Iterates all sticky events on @pad and calls @foreach_func for every
4809 * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
4812 gst_pad_sticky_events_foreach (GstPad * pad,
4813 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4815 ForeachDispatch data;
4817 g_return_if_fail (GST_IS_PAD (pad));
4818 g_return_if_fail (foreach_func != NULL);
4820 data.func = foreach_func;
4821 data.user_data = user_data;
4823 GST_OBJECT_LOCK (pad);
4824 events_foreach (pad, foreach_dispatch_function, &data);
4825 GST_OBJECT_UNLOCK (pad);
4829 do_stream_status (GstPad * pad, GstStreamStatusType type,
4830 GThread * thread, GstTask * task)
4834 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4836 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4837 if (GST_IS_ELEMENT (parent)) {
4838 GstMessage *message;
4839 GValue value = { 0 };
4841 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4842 gchar *tname, *ename, *pname;
4844 /* create a good task name */
4845 ename = gst_element_get_name (parent);
4846 pname = gst_pad_get_name (pad);
4847 tname = g_strdup_printf ("%s:%s", ename, pname);
4851 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4855 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4858 g_value_init (&value, GST_TYPE_TASK);
4859 g_value_set_object (&value, task);
4860 gst_message_set_stream_status_object (message, &value);
4861 g_value_unset (&value);
4863 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4864 gst_element_post_message (parent, message);
4866 gst_object_unref (parent);
4871 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4873 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4878 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4880 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4884 static GstTaskThreadCallbacks thr_callbacks = {
4890 * gst_pad_start_task:
4891 * @pad: the #GstPad to start the task of
4892 * @func: the task function to call
4893 * @data: data passed to the task function
4895 * Starts a task that repeatedly calls @func with @data. This function
4896 * is mostly used in pad activation functions to start the dataflow.
4897 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4898 * before @func is called.
4900 * Returns: a %TRUE if the task could be started.
4903 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4908 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4909 g_return_val_if_fail (func != NULL, FALSE);
4911 GST_DEBUG_OBJECT (pad, "start task");
4913 GST_OBJECT_LOCK (pad);
4914 task = GST_PAD_TASK (pad);
4916 task = gst_task_new (func, data);
4917 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4918 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4919 GST_INFO_OBJECT (pad, "created task %p", task);
4920 GST_PAD_TASK (pad) = task;
4921 gst_object_ref (task);
4922 /* release lock to post the message */
4923 GST_OBJECT_UNLOCK (pad);
4925 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4927 gst_object_unref (task);
4929 GST_OBJECT_LOCK (pad);
4930 /* nobody else is supposed to have changed the pad now */
4931 if (GST_PAD_TASK (pad) != task)
4932 goto concurrent_stop;
4934 res = gst_task_set_state (task, GST_TASK_STARTED);
4935 GST_OBJECT_UNLOCK (pad);
4942 GST_OBJECT_UNLOCK (pad);
4948 * gst_pad_pause_task:
4949 * @pad: the #GstPad to pause the task of
4951 * Pause the task of @pad. This function will also wait until the
4952 * function executed by the task is finished if this function is not
4953 * called from the task function.
4955 * Returns: a TRUE if the task could be paused or FALSE when the pad
4959 gst_pad_pause_task (GstPad * pad)
4964 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4966 GST_DEBUG_OBJECT (pad, "pause task");
4968 GST_OBJECT_LOCK (pad);
4969 task = GST_PAD_TASK (pad);
4972 res = gst_task_set_state (task, GST_TASK_PAUSED);
4973 GST_OBJECT_UNLOCK (pad);
4975 /* wait for task function to finish, this lock is recursive so it does nothing
4976 * when the pause is called from the task itself */
4977 GST_PAD_STREAM_LOCK (pad);
4978 GST_PAD_STREAM_UNLOCK (pad);
4984 GST_DEBUG_OBJECT (pad, "pad has no task");
4985 GST_OBJECT_UNLOCK (pad);
4991 * gst_pad_stop_task:
4992 * @pad: the #GstPad to stop the task of
4994 * Stop the task of @pad. This function will also make sure that the
4995 * function executed by the task will effectively stop if not called
4996 * from the GstTaskFunction.
4998 * This function will deadlock if called from the GstTaskFunction of
4999 * the task. Use gst_task_pause() instead.
5001 * Regardless of whether the pad has a task, the stream lock is acquired and
5002 * released so as to ensure that streaming through this pad has finished.
5004 * Returns: a TRUE if the task could be stopped or FALSE on error.
5007 gst_pad_stop_task (GstPad * pad)
5012 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5014 GST_DEBUG_OBJECT (pad, "stop task");
5016 GST_OBJECT_LOCK (pad);
5017 task = GST_PAD_TASK (pad);
5020 GST_PAD_TASK (pad) = NULL;
5021 res = gst_task_set_state (task, GST_TASK_STOPPED);
5022 GST_OBJECT_UNLOCK (pad);
5024 GST_PAD_STREAM_LOCK (pad);
5025 GST_PAD_STREAM_UNLOCK (pad);
5027 if (!gst_task_join (task))
5030 gst_object_unref (task);
5036 GST_DEBUG_OBJECT (pad, "no task");
5037 GST_OBJECT_UNLOCK (pad);
5039 GST_PAD_STREAM_LOCK (pad);
5040 GST_PAD_STREAM_UNLOCK (pad);
5042 /* this is not an error */
5047 /* this is bad, possibly the application tried to join the task from
5048 * the task's thread. We install the task again so that it will be stopped
5049 * again from the right thread next time hopefully. */
5050 GST_OBJECT_LOCK (pad);
5051 GST_DEBUG_OBJECT (pad, "join failed");
5052 /* we can only install this task if there was no other task */
5053 if (GST_PAD_TASK (pad) == NULL)
5054 GST_PAD_TASK (pad) = task;
5055 GST_OBJECT_UNLOCK (pad);