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);
2761 * gst_pad_query_default:
2762 * @pad: a #GstPad to call the default query handler on.
2763 * @parent: the parent of @pad or NULL
2764 * @query: (transfer none): the #GstQuery to handle.
2766 * Invokes the default query handler for the given pad.
2767 * The query is sent to all pads internally linked to @pad. Note that
2768 * if there are many possible sink pads that are internally linked to
2769 * @pad, only one will be sent the query.
2770 * Multi-sinkpad elements should implement custom query handlers.
2772 * Returns: TRUE if the query was performed successfully.
2775 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
2777 gboolean forward, ret = FALSE;
2779 switch (GST_QUERY_TYPE (query)) {
2780 case GST_QUERY_SCHEDULING:
2783 case GST_QUERY_ALLOCATION:
2784 forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
2786 case GST_QUERY_ACCEPT_CAPS:
2787 ret = gst_pad_query_accept_caps_default (pad, query);
2790 case GST_QUERY_CAPS:
2791 ret = gst_pad_query_caps_default (pad, query);
2794 case GST_QUERY_POSITION:
2795 case GST_QUERY_SEEKING:
2796 case GST_QUERY_FORMATS:
2797 case GST_QUERY_LATENCY:
2798 case GST_QUERY_JITTER:
2799 case GST_QUERY_RATE:
2800 case GST_QUERY_CONVERT:
2807 ret = gst_pad_forward
2808 (pad, (GstPadForwardFunction) gst_pad_peer_query, query);
2814 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
2816 GstPad *pad = data->pad;
2817 GstPadProbeInfo *info = data->info;
2818 GstPadProbeType type, flags;
2819 GstPadProbeCallback callback;
2820 GstPadProbeReturn ret;
2822 /* if we have called this callback, do nothing */
2823 if (PROBE_COOKIE (hook) == data->cookie) {
2824 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2825 "hook %lu, cookie %u already called", hook->hook_id,
2826 PROBE_COOKIE (hook));
2830 PROBE_COOKIE (hook) = data->cookie;
2832 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
2835 /* one of the data types */
2836 if ((flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
2838 /* one of the scheduling types */
2839 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
2841 /* one of the blocking types must match */
2842 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
2843 (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
2845 /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
2846 if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
2847 (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
2850 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2851 "hook %lu, cookie %u with flags 0x%08x matches", hook->hook_id,
2852 PROBE_COOKIE (hook), flags);
2854 data->marshalled = TRUE;
2856 callback = (GstPadProbeCallback) hook->func;
2857 if (callback == NULL)
2860 info->id = hook->hook_id;
2862 GST_OBJECT_UNLOCK (pad);
2864 ret = callback (pad, info, hook->data);
2866 GST_OBJECT_LOCK (pad);
2869 case GST_PAD_PROBE_REMOVE:
2870 /* remove the probe */
2871 GST_DEBUG_OBJECT (pad, "asked to remove hook");
2872 cleanup_hook (pad, hook);
2874 case GST_PAD_PROBE_DROP:
2875 /* need to drop the data, make sure other probes don't get called
2877 GST_DEBUG_OBJECT (pad, "asked to drop item");
2878 info->type = GST_PAD_PROBE_TYPE_INVALID;
2879 data->dropped = TRUE;
2881 case GST_PAD_PROBE_PASS:
2882 /* inform the pad block to let things pass */
2883 GST_DEBUG_OBJECT (pad, "asked to pass item");
2887 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
2894 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2895 "hook %lu, cookie %u with flags 0x%08x does not match %08x",
2896 hook->hook_id, PROBE_COOKIE (hook), flags, info->type);
2901 #define PROBE_PRE_PULL(pad,mask,data,offs,size,label,probed,defaultval) \
2903 if (G_UNLIKELY (pad->num_probes)) { \
2904 /* we start with passing NULL as the data item */ \
2905 GstPadProbeInfo info = { mask, 0, NULL, offs, size }; \
2906 ret = do_probe_callbacks (pad, &info, defaultval); \
2907 /* store the possibly updated data item */ \
2908 data = GST_PAD_PROBE_INFO_DATA (&info); \
2909 /* if something went wrong, exit */ \
2910 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2912 /* otherwise check if the probe retured data */ \
2913 if (G_UNLIKELY (data != NULL)) \
2919 /* a probe that does not take or return any data */
2920 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
2922 if (G_UNLIKELY (pad->num_probes)) { \
2923 /* pass NULL as the data item */ \
2924 GstPadProbeInfo info = { mask, 0, NULL, 0, 0 }; \
2925 ret = do_probe_callbacks (pad, &info, defaultval); \
2926 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2931 #define PROBE_FULL(pad,mask,data,offs,size,label,defaultval) \
2933 if (G_UNLIKELY (pad->num_probes)) { \
2934 GstPadProbeInfo info = { mask, 0, data, offs, size }; \
2935 ret = do_probe_callbacks (pad, &info, defaultval); \
2936 data = GST_PAD_PROBE_INFO_DATA (&info); \
2937 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2942 #define PROBE_PUSH(pad,mask,data,label) \
2943 PROBE_FULL(pad, mask, data, -1, -1, label, GST_FLOW_OK);
2944 #define PROBE_PULL(pad,mask,data,offs,size,label) \
2945 PROBE_FULL(pad, mask, data, offs, size, label, GST_FLOW_OK);
2947 static GstFlowReturn
2948 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
2949 GstFlowReturn defaultval)
2958 data.marshalled = FALSE;
2959 data.dropped = FALSE;
2960 data.cookie = ++pad->priv->probe_cookie;
2963 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
2966 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2967 "do probes cookie %u", data.cookie);
2968 cookie = pad->priv->probe_list_cookie;
2970 g_hook_list_marshal (&pad->probes, TRUE,
2971 (GHookMarshaller) probe_hook_marshal, &data);
2973 /* if the list changed, call the new callbacks (they will not have their
2974 * cookie set to data.cookie */
2975 if (cookie != pad->priv->probe_list_cookie) {
2976 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2977 "probe list changed, restarting");
2981 /* the first item that dropped will stop the hooks and then we drop here */
2985 /* if no handler matched and we are blocking, let the item pass */
2986 if (!data.marshalled && is_block)
2989 /* At this point, all handlers returned either OK or PASS. If one handler
2990 * returned PASS, let the item pass */
2995 while (GST_PAD_IS_BLOCKED (pad)) {
2996 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2997 "we are blocked %d times", pad->num_blocked);
2999 /* we might have released the lock */
3000 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3003 /* now we block the streaming thread. It can be unlocked when we
3004 * deactivate the pad (which will also set the FLUSHING flag) or
3005 * when the pad is unblocked. A flushing event will also unblock
3006 * the pad after setting the FLUSHING flag. */
3007 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3008 "Waiting to be unblocked or set flushing");
3009 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3010 GST_PAD_BLOCK_WAIT (pad);
3011 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3012 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3014 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3024 GST_DEBUG_OBJECT (pad, "pad is flushing");
3025 return GST_FLOW_FLUSHING;
3029 GST_DEBUG_OBJECT (pad, "data is dropped");
3030 return GST_FLOW_CUSTOM_SUCCESS;
3034 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3035 GST_DEBUG_OBJECT (pad, "data is passed");
3043 * gst_pad_get_offset:
3046 * Get the offset applied to the running time of @pad. @pad has to be a source
3049 * Returns: the offset.
3052 gst_pad_get_offset (GstPad * pad)
3056 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3058 GST_OBJECT_LOCK (pad);
3059 result = pad->offset;
3060 GST_OBJECT_UNLOCK (pad);
3066 * gst_pad_set_offset:
3068 * @offset: the offset
3070 * Set the offset that will be applied to the running time of @pad.
3073 gst_pad_set_offset (GstPad * pad, gint64 offset)
3077 g_return_if_fail (GST_IS_PAD (pad));
3079 GST_OBJECT_LOCK (pad);
3080 /* if nothing changed, do nothing */
3081 if (pad->offset == offset)
3084 pad->offset = offset;
3085 GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3087 /* sinkpads will apply their offset the next time a segment
3088 * event is received. */
3089 if (GST_PAD_IS_SINK (pad))
3092 /* resend the last segment event on next buffer push */
3093 if ((ev = find_event_by_type (pad, GST_EVENT_SEGMENT, 0))) {
3094 ev->received = FALSE;
3095 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3099 GST_OBJECT_UNLOCK (pad);
3105 * @pad: a #GstPad to invoke the default query on.
3106 * @query: (transfer none): the #GstQuery to perform.
3108 * Dispatches a query to a pad. The query should have been allocated by the
3109 * caller via one of the type-specific allocation functions. The element that
3110 * the pad belongs to is responsible for filling the query with an appropriate
3111 * response, which should then be parsed with a type-specific query parsing
3114 * Again, the caller is responsible for both the allocation and deallocation of
3115 * the query structure.
3117 * Please also note that some queries might need a running pipeline to work.
3119 * Returns: TRUE if the query could be performed.
3122 gst_pad_query (GstPad * pad, GstQuery * query)
3126 GstPadQueryFunction func;
3127 GstPadProbeType type;
3130 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3131 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3133 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3134 GST_QUERY_TYPE_NAME (query));
3136 if (GST_PAD_IS_SRC (pad))
3137 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3139 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3141 GST_OBJECT_LOCK (pad);
3142 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3143 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3144 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3146 ACQUIRE_PARENT (pad, parent, no_parent);
3147 GST_OBJECT_UNLOCK (pad);
3149 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3152 res = func (pad, parent, query);
3154 RELEASE_PARENT (parent);
3156 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3157 GST_QUERY_TYPE_NAME (query), res);
3162 GST_OBJECT_LOCK (pad);
3163 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3164 GST_OBJECT_UNLOCK (pad);
3171 GST_DEBUG_OBJECT (pad, "had no parent");
3172 GST_OBJECT_UNLOCK (pad);
3177 GST_DEBUG_OBJECT (pad, "had no query function");
3178 RELEASE_PARENT (parent);
3183 GST_DEBUG_OBJECT (pad, "query failed");
3188 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3189 GST_OBJECT_UNLOCK (pad);
3195 * gst_pad_peer_query:
3196 * @pad: a #GstPad to invoke the peer query on.
3197 * @query: (transfer none): the #GstQuery to perform.
3199 * Performs gst_pad_query() on the peer of @pad.
3201 * The caller is responsible for both the allocation and deallocation of
3202 * the query structure.
3204 * Returns: TRUE if the query could be performed. This function returns %FALSE
3205 * if @pad has no peer.
3210 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3213 GstPadProbeType type;
3217 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3218 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3220 if (GST_PAD_IS_SRC (pad))
3221 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3223 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3225 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3226 GST_QUERY_TYPE_NAME (query));
3228 GST_OBJECT_LOCK (pad);
3229 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3230 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3231 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3233 peerpad = GST_PAD_PEER (pad);
3234 if (G_UNLIKELY (peerpad == NULL))
3237 gst_object_ref (peerpad);
3238 GST_OBJECT_UNLOCK (pad);
3240 res = gst_pad_query (peerpad, query);
3242 gst_object_unref (peerpad);
3247 GST_OBJECT_LOCK (pad);
3248 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3249 GST_OBJECT_UNLOCK (pad);
3256 GST_WARNING_OBJECT (pad, "pad has no peer");
3257 GST_OBJECT_UNLOCK (pad);
3262 GST_DEBUG_OBJECT (pad, "query failed");
3267 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3268 GST_OBJECT_UNLOCK (pad);
3273 /**********************************************************************
3274 * Data passing functions
3277 /* should be called with pad LOCK */
3279 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3281 GstFlowReturn *data = user_data;
3282 GstEvent *event = ev->event;
3285 GST_DEBUG_OBJECT (pad, "event %s was already received",
3286 GST_EVENT_TYPE_NAME (event));
3290 *data = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
3291 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3295 ev->received = TRUE;
3296 GST_DEBUG_OBJECT (pad, "event %s marked received",
3297 GST_EVENT_TYPE_NAME (event));
3299 case GST_FLOW_NOT_LINKED:
3300 /* not linked is not a problem, we are sticky so the event will be
3302 GST_DEBUG_OBJECT (pad, "pad was not linked");
3303 *data = GST_FLOW_OK;
3306 GST_DEBUG_OBJECT (pad, "mark pending events");
3307 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3310 return *data == GST_FLOW_OK;
3313 /* check sticky events and push them when needed. should be called
3315 static inline GstFlowReturn
3316 check_sticky (GstPad * pad)
3318 GstFlowReturn ret = GST_FLOW_OK;
3320 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3321 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3323 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3324 events_foreach (pad, push_sticky, &ret);
3329 /* this is the chain function that does not perform the additional argument
3330 * checking for that little extra speed.
3332 static inline GstFlowReturn
3333 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3338 GST_PAD_STREAM_LOCK (pad);
3340 GST_OBJECT_LOCK (pad);
3341 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3344 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3347 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3349 PROBE_PUSH (pad, type, data, probe_stopped);
3351 parent = GST_OBJECT_PARENT (pad);
3352 GST_OBJECT_UNLOCK (pad);
3354 /* NOTE: we read the chainfunc unlocked.
3355 * we cannot hold the lock for the pad so we might send
3356 * the data to the wrong function. This is not really a
3357 * problem since functions are assigned at creation time
3358 * and don't change that often... */
3359 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3360 GstPadChainFunction chainfunc;
3362 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3365 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3366 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3367 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3369 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
3371 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3372 "called chainfunction &%s with buffer %p, returned %s",
3373 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3375 GstPadChainListFunction chainlistfunc;
3377 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3380 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3381 "calling chainlistfunction &%s",
3382 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3384 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
3386 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3387 "called chainlistfunction &%s, returned %s",
3388 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3391 GST_PAD_STREAM_UNLOCK (pad);
3398 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3399 "chaining, but pad was flushing");
3400 GST_OBJECT_UNLOCK (pad);
3401 GST_PAD_STREAM_UNLOCK (pad);
3402 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3403 return GST_FLOW_FLUSHING;
3407 g_critical ("chain on pad %s:%s but it was not in push mode",
3408 GST_DEBUG_PAD_NAME (pad));
3409 GST_OBJECT_UNLOCK (pad);
3410 GST_PAD_STREAM_UNLOCK (pad);
3411 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3412 return GST_FLOW_ERROR;
3416 GST_OBJECT_UNLOCK (pad);
3417 GST_PAD_STREAM_UNLOCK (pad);
3418 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3421 case GST_FLOW_CUSTOM_SUCCESS:
3422 GST_DEBUG_OBJECT (pad, "dropped buffer");
3426 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3433 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3434 g_critical ("chain on pad %s:%s but it has no chainfunction",
3435 GST_DEBUG_PAD_NAME (pad));
3436 GST_PAD_STREAM_UNLOCK (pad);
3437 return GST_FLOW_NOT_SUPPORTED;
3443 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3444 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3447 * Chain a buffer to @pad.
3449 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3451 * If the buffer type is not acceptable for @pad (as negotiated with a
3452 * preceeding GST_EVENT_CAPS event), this function returns
3453 * #GST_FLOW_NOT_NEGOTIATED.
3455 * The function proceeds calling the chain function installed on @pad (see
3456 * gst_pad_set_chain_function()) and the return value of that function is
3457 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3460 * In all cases, success or failure, the caller loses its reference to @buffer
3461 * after calling this function.
3463 * Returns: a #GstFlowReturn from the pad.
3468 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3470 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3471 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3472 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3474 return gst_pad_chain_data_unchecked (pad,
3475 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3478 static GstFlowReturn
3479 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
3480 GstBufferList * list)
3486 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3488 len = gst_buffer_list_length (list);
3491 for (i = 0; i < len; i++) {
3492 buffer = gst_buffer_list_get (list, i);
3494 gst_pad_chain_data_unchecked (pad,
3495 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3496 gst_buffer_ref (buffer));
3497 if (ret != GST_FLOW_OK)
3500 gst_buffer_list_unref (list);
3506 * gst_pad_chain_list:
3507 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3508 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3511 * Chain a bufferlist to @pad.
3513 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3515 * If @pad was not negotiated properly with a CAPS event, this function
3516 * returns #GST_FLOW_NOT_NEGOTIATED.
3518 * The function proceeds calling the chainlist function installed on @pad (see
3519 * gst_pad_set_chain_list_function()) and the return value of that function is
3520 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3521 * chainlist function.
3523 * In all cases, success or failure, the caller loses its reference to @list
3524 * after calling this function.
3528 * Returns: a #GstFlowReturn from the pad.
3533 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3535 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3536 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3537 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3539 return gst_pad_chain_data_unchecked (pad,
3540 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3543 static GstFlowReturn
3544 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3549 GST_OBJECT_LOCK (pad);
3550 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3553 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3556 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
3559 /* do block probes */
3560 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3562 /* do post-blocking probes */
3563 PROBE_PUSH (pad, type, data, probe_stopped);
3565 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3568 /* take ref to peer pad before releasing the lock */
3569 gst_object_ref (peer);
3571 GST_OBJECT_UNLOCK (pad);
3573 ret = gst_pad_chain_data_unchecked (peer, type, data);
3575 gst_object_unref (peer);
3577 GST_OBJECT_LOCK (pad);
3579 if (pad->priv->using == 0) {
3580 /* pad is not active anymore, trigger idle callbacks */
3581 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3582 probe_stopped, ret);
3584 GST_OBJECT_UNLOCK (pad);
3588 /* ERROR recovery here */
3592 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3593 "pushing, but pad was flushing");
3594 GST_OBJECT_UNLOCK (pad);
3595 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3596 return GST_FLOW_FLUSHING;
3600 g_critical ("pushing on pad %s:%s but it was not activated in push mode",
3601 GST_DEBUG_PAD_NAME (pad));
3602 GST_OBJECT_UNLOCK (pad);
3603 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3604 return GST_FLOW_ERROR;
3608 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3609 "error pushing events, return %s", gst_flow_get_name (ret));
3610 GST_OBJECT_UNLOCK (pad);
3611 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3616 GST_OBJECT_UNLOCK (pad);
3617 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3620 case GST_FLOW_CUSTOM_SUCCESS:
3621 GST_DEBUG_OBJECT (pad, "dropped buffer");
3625 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3632 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3633 "pushing, but it was not linked");
3634 GST_OBJECT_UNLOCK (pad);
3635 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3636 return GST_FLOW_NOT_LINKED;
3642 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3643 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3646 * Pushes a buffer to the peer of @pad.
3648 * This function will call installed block probes before triggering any
3649 * installed data probes.
3651 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3652 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3655 * In all cases, success or failure, the caller loses its reference to @buffer
3656 * after calling this function.
3658 * Returns: a #GstFlowReturn from the peer pad.
3663 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3665 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3666 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3667 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3669 return gst_pad_push_data (pad,
3670 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3674 * gst_pad_push_list:
3675 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3676 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3679 * Pushes a buffer list to the peer of @pad.
3681 * This function will call installed block probes before triggering any
3682 * installed data probes.
3684 * The function proceeds calling the chain function on the peer pad and returns
3685 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3686 * be returned. If the peer pad does not have any installed chainlist function
3687 * every group buffer of the list will be merged into a normal #GstBuffer and
3688 * chained via gst_pad_chain().
3690 * In all cases, success or failure, the caller loses its reference to @list
3691 * after calling this function.
3693 * Returns: a #GstFlowReturn from the peer pad.
3700 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3702 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3703 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3704 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3706 return gst_pad_push_data (pad,
3707 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3710 static GstFlowReturn
3711 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3712 GstBuffer ** buffer)
3715 GstPadGetRangeFunction getrangefunc;
3718 GST_PAD_STREAM_LOCK (pad);
3720 GST_OBJECT_LOCK (pad);
3721 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3724 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
3727 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
3730 /* when one of the probes returns a buffer, probed_data will be called and we
3731 * skip calling the getrange function */
3732 PROBE_PRE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3733 *buffer, offset, size, probe_stopped, probed_data, GST_FLOW_OK);
3735 ACQUIRE_PARENT (pad, parent, no_parent);
3736 GST_OBJECT_UNLOCK (pad);
3738 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3741 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3742 "calling getrangefunc %s, offset %"
3743 G_GUINT64_FORMAT ", size %u",
3744 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3746 ret = getrangefunc (pad, parent, offset, size, buffer);
3748 RELEASE_PARENT (parent);
3750 if (G_UNLIKELY (ret != GST_FLOW_OK))
3751 goto get_range_failed;
3753 /* can only fire the signal if we have a valid buffer */
3754 GST_OBJECT_LOCK (pad);
3756 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3757 *buffer, offset, size, probe_stopped_unref);
3758 GST_OBJECT_UNLOCK (pad);
3760 GST_PAD_STREAM_UNLOCK (pad);
3767 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3768 "getrange, but pad was flushing");
3769 GST_OBJECT_UNLOCK (pad);
3770 GST_PAD_STREAM_UNLOCK (pad);
3771 return GST_FLOW_FLUSHING;
3775 g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
3776 GST_DEBUG_PAD_NAME (pad));
3777 GST_OBJECT_UNLOCK (pad);
3778 GST_PAD_STREAM_UNLOCK (pad);
3779 return GST_FLOW_ERROR;
3783 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
3784 GST_OBJECT_UNLOCK (pad);
3785 GST_PAD_STREAM_UNLOCK (pad);
3790 GST_DEBUG_OBJECT (pad, "no parent");
3791 GST_OBJECT_UNLOCK (pad);
3792 GST_PAD_STREAM_UNLOCK (pad);
3793 return GST_FLOW_FLUSHING;
3797 g_critical ("getrange on pad %s:%s but it has no getrangefunction",
3798 GST_DEBUG_PAD_NAME (pad));
3799 RELEASE_PARENT (parent);
3800 GST_PAD_STREAM_UNLOCK (pad);
3801 return GST_FLOW_NOT_SUPPORTED;
3805 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3806 "probe returned %s", gst_flow_get_name (ret));
3807 GST_OBJECT_UNLOCK (pad);
3808 GST_PAD_STREAM_UNLOCK (pad);
3811 probe_stopped_unref:
3813 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3814 "probe returned %s", gst_flow_get_name (ret));
3815 GST_OBJECT_UNLOCK (pad);
3816 GST_PAD_STREAM_UNLOCK (pad);
3817 gst_buffer_unref (*buffer);
3823 GST_PAD_STREAM_UNLOCK (pad);
3825 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3826 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3827 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
3833 * gst_pad_get_range:
3834 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
3835 * @offset: The start offset of the buffer
3836 * @size: The length of the buffer
3837 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
3838 * returns #GST_FLOW_ERROR if %NULL.
3840 * When @pad is flushing this function returns #GST_FLOW_FLUSHING
3841 * immediately and @buffer is %NULL.
3843 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
3844 * description of a getrange function. If @pad has no getrange function
3845 * installed (see gst_pad_set_getrange_function()) this function returns
3846 * #GST_FLOW_NOT_SUPPORTED.
3848 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
3850 * Returns: a #GstFlowReturn from the pad.
3855 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
3856 GstBuffer ** buffer)
3858 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3859 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3860 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3862 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
3866 * gst_pad_pull_range:
3867 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3868 * @offset: The start offset of the buffer
3869 * @size: The length of the buffer
3870 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
3871 * GST_FLOW_ERROR if %NULL.
3873 * Pulls a @buffer from the peer pad.
3875 * This function will first trigger the pad block signal if it was
3878 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
3879 * function returns the result of gst_pad_get_range() on the peer pad.
3880 * See gst_pad_get_range() for a list of return values and for the
3881 * semantics of the arguments of this function.
3883 * Returns: a #GstFlowReturn from the peer pad.
3884 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
3885 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
3886 * @buffer may not be used or freed when any other return value than
3887 * #GST_FLOW_OK is returned.
3892 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
3893 GstBuffer ** buffer)
3898 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3899 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3900 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3902 GST_OBJECT_LOCK (pad);
3903 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3906 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
3909 /* when one of the probes returns a buffer, probed_data will be called and we
3910 * skip calling the peer getrange function */
3911 PROBE_PRE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3912 *buffer, offset, size, pre_probe_stopped, probed_data, GST_FLOW_OK);
3914 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3917 gst_object_ref (peer);
3919 GST_OBJECT_UNLOCK (pad);
3921 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
3923 gst_object_unref (peer);
3925 GST_OBJECT_LOCK (pad);
3927 if (pad->priv->using == 0) {
3928 /* pad is not active anymore, trigger idle callbacks */
3929 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
3930 post_probe_stopped, ret);
3933 if (G_UNLIKELY (ret != GST_FLOW_OK))
3934 goto pull_range_failed;
3937 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3938 *buffer, offset, size, post_probe_stopped);
3940 GST_OBJECT_UNLOCK (pad);
3944 /* ERROR recovery here */
3947 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3948 "pullrange, but pad was flushing");
3949 GST_OBJECT_UNLOCK (pad);
3950 return GST_FLOW_FLUSHING;
3954 g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
3955 GST_DEBUG_PAD_NAME (pad));
3956 GST_OBJECT_UNLOCK (pad);
3957 return GST_FLOW_ERROR;
3961 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
3962 gst_flow_get_name (ret));
3963 GST_OBJECT_UNLOCK (pad);
3968 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3969 "pulling range, but it was not linked");
3970 GST_OBJECT_UNLOCK (pad);
3971 return GST_FLOW_NOT_LINKED;
3976 GST_OBJECT_UNLOCK (pad);
3977 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3978 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3979 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
3984 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3985 "post probe returned %s", gst_flow_get_name (ret));
3986 GST_OBJECT_UNLOCK (pad);
3987 if (ret == GST_FLOW_OK)
3988 gst_buffer_unref (*buffer);
3995 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event, gboolean locked)
4000 gboolean res = FALSE;
4001 const gchar *name = NULL;
4003 type = GST_EVENT_TYPE (event);
4004 if (type & GST_EVENT_TYPE_STICKY_MULTI)
4005 name = gst_structure_get_name (gst_event_get_structure (event));
4007 events = pad->priv->events;
4010 for (i = 0; i < len; i++) {
4011 PadEvent *ev = &g_array_index (events, PadEvent, i);
4013 if (ev->event == NULL)
4016 if (type == GST_EVENT_TYPE (ev->event)) {
4017 /* matching types, check matching name if needed */
4018 if (name && !gst_event_has_name (ev->event, name))
4022 if ((res = gst_event_replace (&ev->event, event)))
4023 ev->received = FALSE;
4029 ev.event = gst_event_ref (event);
4030 ev.received = FALSE;
4031 g_array_append_val (events, ev);
4036 pad->priv->events_cookie++;
4037 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4039 GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4041 switch (GST_EVENT_TYPE (event)) {
4042 case GST_EVENT_CAPS:
4044 GST_OBJECT_UNLOCK (pad);
4046 GST_DEBUG_OBJECT (pad, "notify caps");
4047 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4050 GST_OBJECT_LOCK (pad);
4059 /* should be called with pad LOCK */
4060 static GstFlowReturn
4061 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
4062 GstPadProbeType type)
4066 GstEventType event_type;
4068 /* Two checks to be made:
4069 * . (un)set the FLUSHING flag for flushing events,
4070 * . handle pad blocking */
4071 event_type = GST_EVENT_TYPE (event);
4072 switch (event_type) {
4073 case GST_EVENT_FLUSH_START:
4074 GST_PAD_SET_FLUSHING (pad);
4076 GST_PAD_BLOCK_BROADCAST (pad);
4077 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4079 case GST_EVENT_FLUSH_STOP:
4080 GST_PAD_UNSET_FLUSHING (pad);
4082 /* Remove sticky EOS events */
4083 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4084 remove_event_by_type (pad, GST_EVENT_EOS);
4086 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4090 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4093 switch (GST_EVENT_TYPE (event)) {
4094 case GST_EVENT_SEGMENT:
4095 /* pass the adjusted segment event on. We need to do this even if
4096 * there is no peer pad because of the probes. */
4097 event = apply_pad_offset (pad, event);
4099 case GST_EVENT_RECONFIGURE:
4100 if (GST_PAD_IS_SINK (pad))
4101 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4106 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4107 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4112 /* send probes after modifying the events above */
4113 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4115 /* now check the peer pad */
4116 peerpad = GST_PAD_PEER (pad);
4117 if (peerpad == NULL)
4120 gst_object_ref (peerpad);
4122 GST_OBJECT_UNLOCK (pad);
4124 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4125 event, GST_EVENT_TYPE_NAME (event), peerpad);
4127 ret = gst_pad_send_event_unchecked (peerpad, event, type);
4129 /* Note: we gave away ownership of the event at this point but we can still
4130 * print the old pointer */
4131 GST_LOG_OBJECT (pad,
4132 "sent event %p to peerpad %" GST_PTR_FORMAT ", ret %s", event, peerpad,
4133 gst_flow_get_name (ret));
4135 gst_object_unref (peerpad);
4137 GST_OBJECT_LOCK (pad);
4139 if (pad->priv->using == 0) {
4140 /* pad is not active anymore, trigger idle callbacks */
4141 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4142 idle_probe_stopped, ret);
4146 /* ERROR handling */
4149 GST_DEBUG_OBJECT (pad, "We're flushing");
4150 gst_event_unref (event);
4151 return GST_FLOW_FLUSHING;
4155 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4156 gst_event_unref (event);
4159 case GST_FLOW_CUSTOM_SUCCESS:
4160 GST_DEBUG_OBJECT (pad, "dropped event");
4164 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4171 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4172 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4173 gst_event_unref (event);
4174 return GST_FLOW_NOT_LINKED;
4178 GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
4184 * gst_pad_push_event:
4185 * @pad: a #GstPad to push the event to.
4186 * @event: (transfer full): the #GstEvent to send to the pad.
4188 * Sends the event to the peer of the given pad. This function is
4189 * mainly used by elements to send events to their peer
4192 * This function takes owership of the provided event so you should
4193 * gst_event_ref() it if you want to reuse the event after this call.
4195 * Returns: TRUE if the event was handled.
4200 gst_pad_push_event (GstPad * pad, GstEvent * event)
4203 GstPadProbeType type;
4204 gboolean sticky, serialized;
4206 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4207 g_return_val_if_fail (event != NULL, FALSE);
4208 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4210 if (GST_PAD_IS_SRC (pad)) {
4211 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4212 goto wrong_direction;
4213 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4214 } else if (GST_PAD_IS_SINK (pad)) {
4215 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4216 goto wrong_direction;
4217 /* events pushed on sinkpad never are sticky */
4218 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4220 goto unknown_direction;
4222 GST_OBJECT_LOCK (pad);
4223 sticky = GST_EVENT_IS_STICKY (event);
4224 serialized = GST_EVENT_IS_SERIALIZED (event);
4227 /* can't store on flushing pads */
4228 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4231 /* srcpad sticky events are store immediately, the received flag is set
4232 * to FALSE and will be set to TRUE when we can successfully push the
4233 * event to the peer pad */
4234 if (gst_pad_store_sticky_event (pad, event, TRUE)) {
4235 GST_DEBUG_OBJECT (pad, "event %s updated", GST_EVENT_TYPE_NAME (event));
4238 if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
4239 /* all serialized or sticky events on the srcpad trigger push of
4241 res = (check_sticky (pad) == GST_FLOW_OK);
4244 /* other events are pushed right away */
4245 res = (gst_pad_push_event_unchecked (pad, event, type) == GST_FLOW_OK);
4247 gst_event_unref (event);
4250 GST_OBJECT_UNLOCK (pad);
4254 /* ERROR handling */
4257 g_warning ("pad %s:%s pushing %s event in wrong direction",
4258 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4259 gst_event_unref (event);
4264 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4265 gst_event_unref (event);
4270 GST_DEBUG_OBJECT (pad, "We're flushing");
4271 GST_OBJECT_UNLOCK (pad);
4272 gst_event_unref (event);
4277 /* Check if we can call the event function with the given event */
4278 static GstFlowReturn
4279 pre_eventfunc_check (GstPad * pad, GstEvent * event)
4281 GstCaps *caps, *templ;
4283 switch (GST_EVENT_TYPE (event)) {
4284 case GST_EVENT_CAPS:
4286 /* backwards compatibility mode for caps */
4287 gst_event_parse_caps (event, &caps);
4289 /* See if pad accepts the caps */
4290 templ = gst_pad_get_pad_template_caps (pad);
4291 if (!gst_caps_is_subset (caps, templ))
4294 gst_caps_unref (templ);
4305 gst_caps_unref (templ);
4306 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4307 "caps %" GST_PTR_FORMAT " not accepted", caps);
4308 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4309 "no intersection with template %" GST_PTR_FORMAT, templ);
4310 return GST_FLOW_NOT_NEGOTIATED;
4314 static GstFlowReturn
4315 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
4316 GstPadProbeType type)
4319 GstEventType event_type;
4320 gboolean serialized, need_unlock = FALSE, sticky;
4321 GstPadEventFunction eventfunc;
4324 GST_OBJECT_LOCK (pad);
4325 if (GST_PAD_IS_SINK (pad))
4326 serialized = GST_EVENT_IS_SERIALIZED (event);
4329 sticky = GST_EVENT_IS_STICKY (event);
4330 event_type = GST_EVENT_TYPE (event);
4331 switch (event_type) {
4332 case GST_EVENT_FLUSH_START:
4333 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4334 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4336 /* can't even accept a flush begin event when flushing */
4337 if (GST_PAD_IS_FLUSHING (pad))
4340 GST_PAD_SET_FLUSHING (pad);
4341 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4343 case GST_EVENT_FLUSH_STOP:
4344 if (G_LIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_NONE)) {
4345 GST_PAD_UNSET_FLUSHING (pad);
4346 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4348 /* Remove pending EOS events */
4349 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4350 remove_event_by_type (pad, GST_EVENT_EOS);
4352 GST_OBJECT_UNLOCK (pad);
4353 /* grab stream lock */
4354 GST_PAD_STREAM_LOCK (pad);
4356 GST_OBJECT_LOCK (pad);
4357 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4360 case GST_EVENT_RECONFIGURE:
4361 if (GST_PAD_IS_SRC (pad))
4362 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4364 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4365 "have event type %" GST_PTR_FORMAT, event);
4367 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4371 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4372 GST_OBJECT_UNLOCK (pad);
4373 GST_PAD_STREAM_LOCK (pad);
4375 GST_OBJECT_LOCK (pad);
4376 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4380 switch (GST_EVENT_TYPE (event)) {
4381 case GST_EVENT_SEGMENT:
4382 event = apply_pad_offset (pad, event);
4388 /* now do the probe */
4390 type | GST_PAD_PROBE_TYPE_PUSH |
4391 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4393 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4397 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4400 ACQUIRE_PARENT (pad, parent, no_parent);
4401 GST_OBJECT_UNLOCK (pad);
4403 ret = pre_eventfunc_check (pad, event);
4404 if (G_UNLIKELY (ret != GST_FLOW_OK))
4405 goto precheck_failed;
4408 gst_event_ref (event);
4410 if (eventfunc (pad, parent, event)) {
4413 /* something went wrong */
4414 switch (event_type) {
4415 case GST_EVENT_CAPS:
4416 ret = GST_FLOW_NOT_NEGOTIATED;
4419 ret = GST_FLOW_ERROR;
4423 RELEASE_PARENT (parent);
4425 GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
4428 if (ret == GST_FLOW_OK) {
4429 /* after the event function accepted the event, we can store the sticky
4430 * event on the pad */
4431 gst_pad_store_sticky_event (pad, event, FALSE);
4433 gst_event_unref (event);
4437 GST_PAD_STREAM_UNLOCK (pad);
4441 /* ERROR handling */
4444 GST_OBJECT_UNLOCK (pad);
4446 GST_PAD_STREAM_UNLOCK (pad);
4447 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4448 "Received event on flushing pad. Discarding");
4449 gst_event_unref (event);
4450 return GST_FLOW_FLUSHING;
4454 GST_OBJECT_UNLOCK (pad);
4456 GST_PAD_STREAM_UNLOCK (pad);
4457 gst_event_unref (event);
4460 case GST_FLOW_CUSTOM_SUCCESS:
4461 GST_DEBUG_OBJECT (pad, "dropped event");
4465 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4472 g_warning ("pad %s:%s has no event handler, file a bug.",
4473 GST_DEBUG_PAD_NAME (pad));
4474 GST_OBJECT_UNLOCK (pad);
4476 GST_PAD_STREAM_UNLOCK (pad);
4477 gst_event_unref (event);
4478 return GST_FLOW_NOT_SUPPORTED;
4482 GST_DEBUG_OBJECT (pad, "no parent");
4483 GST_OBJECT_UNLOCK (pad);
4485 GST_PAD_STREAM_UNLOCK (pad);
4486 gst_event_unref (event);
4487 return GST_FLOW_FLUSHING;
4491 GST_DEBUG_OBJECT (pad, "pre event check failed");
4492 RELEASE_PARENT (parent);
4494 GST_PAD_STREAM_UNLOCK (pad);
4495 gst_event_unref (event);
4501 * gst_pad_send_event:
4502 * @pad: a #GstPad to send the event to.
4503 * @event: (transfer full): the #GstEvent to send to the pad.
4505 * Sends the event to the pad. This function can be used
4506 * by applications to send events in the pipeline.
4508 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4509 * sink pad, @event should be a downstream event. For example, you would not
4510 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4511 * Furthermore, some downstream events have to be serialized with data flow,
4512 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4513 * the event needs to be serialized with data flow, this function will take the
4514 * pad's stream lock while calling its event function.
4516 * To find out whether an event type is upstream, downstream, or downstream and
4517 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4518 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4519 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4520 * plugin doesn't need to bother itself with this information; the core handles
4521 * all necessary locks and checks.
4523 * This function takes owership of the provided event so you should
4524 * gst_event_ref() it if you want to reuse the event after this call.
4526 * Returns: TRUE if the event was handled.
4529 gst_pad_send_event (GstPad * pad, GstEvent * event)
4532 GstPadProbeType type;
4534 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4535 g_return_val_if_fail (event != NULL, FALSE);
4537 if (GST_PAD_IS_SINK (pad)) {
4538 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4539 goto wrong_direction;
4540 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4541 } else if (GST_PAD_IS_SRC (pad)) {
4542 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4543 goto wrong_direction;
4544 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4546 goto unknown_direction;
4548 if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
4555 /* ERROR handling */
4558 g_warning ("pad %s:%s sending %s event in wrong direction",
4559 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4560 gst_event_unref (event);
4565 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4566 gst_event_unref (event);
4572 * gst_pad_set_element_private:
4573 * @pad: the #GstPad to set the private data of.
4574 * @priv: The private data to attach to the pad.
4576 * Set the given private data gpointer on the pad.
4577 * This function can only be used by the element that owns the pad.
4578 * No locking is performed in this function.
4581 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4583 pad->element_private = priv;
4587 * gst_pad_get_element_private:
4588 * @pad: the #GstPad to get the private data of.
4590 * Gets the private data of a pad.
4591 * No locking is performed in this function.
4593 * Returns: (transfer none): a #gpointer to the private data.
4596 gst_pad_get_element_private (GstPad * pad)
4598 return pad->element_private;
4602 * gst_pad_get_sticky_event:
4603 * @pad: the #GstPad to get the event from.
4604 * @event_type: the #GstEventType that should be retrieved.
4605 * @idx: the index of the event
4607 * Returns a new reference of the sticky event of type @event_type
4610 * Returns: (transfer full): a #GstEvent of type @event_type or NULL when no
4611 * event of @event_type was on @pad. Unref after usage.
4614 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
4616 GstEvent *event = NULL;
4619 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4620 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4622 GST_OBJECT_LOCK (pad);
4623 ev = find_event_by_type (pad, event_type, idx);
4624 if (ev && (event = ev->event))
4625 gst_event_ref (event);
4626 GST_OBJECT_UNLOCK (pad);
4633 GstPadStickyEventsForeachFunction func;
4638 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
4640 ForeachDispatch *data = user_data;
4643 GST_OBJECT_UNLOCK (pad);
4645 ret = data->func (pad, &ev->event, data->user_data);
4647 GST_OBJECT_LOCK (pad);
4653 * gst_pad_sticky_events_foreach:
4654 * @pad: the #GstPad that should be used for iteration.
4655 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
4656 * should be called for every event.
4657 * @user_data: (closure): the optional user data.
4659 * Iterates all sticky events on @pad and calls @foreach_func for every
4660 * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
4663 gst_pad_sticky_events_foreach (GstPad * pad,
4664 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4666 ForeachDispatch data;
4668 g_return_if_fail (GST_IS_PAD (pad));
4669 g_return_if_fail (foreach_func != NULL);
4671 data.func = foreach_func;
4672 data.user_data = user_data;
4674 GST_OBJECT_LOCK (pad);
4675 events_foreach (pad, foreach_dispatch_function, &data);
4676 GST_OBJECT_UNLOCK (pad);
4680 do_stream_status (GstPad * pad, GstStreamStatusType type,
4681 GThread * thread, GstTask * task)
4685 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4687 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4688 if (GST_IS_ELEMENT (parent)) {
4689 GstMessage *message;
4690 GValue value = { 0 };
4692 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4693 gchar *tname, *ename, *pname;
4695 /* create a good task name */
4696 ename = gst_element_get_name (parent);
4697 pname = gst_pad_get_name (pad);
4698 tname = g_strdup_printf ("%s:%s", ename, pname);
4702 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4706 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4709 g_value_init (&value, GST_TYPE_TASK);
4710 g_value_set_object (&value, task);
4711 gst_message_set_stream_status_object (message, &value);
4712 g_value_unset (&value);
4714 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4715 gst_element_post_message (parent, message);
4717 gst_object_unref (parent);
4722 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4724 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4729 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4731 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4735 static GstTaskThreadCallbacks thr_callbacks = {
4741 * gst_pad_start_task:
4742 * @pad: the #GstPad to start the task of
4743 * @func: the task function to call
4744 * @data: data passed to the task function
4746 * Starts a task that repeatedly calls @func with @data. This function
4747 * is mostly used in pad activation functions to start the dataflow.
4748 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4749 * before @func is called.
4751 * Returns: a %TRUE if the task could be started.
4754 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4759 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4760 g_return_val_if_fail (func != NULL, FALSE);
4762 GST_DEBUG_OBJECT (pad, "start task");
4764 GST_OBJECT_LOCK (pad);
4765 task = GST_PAD_TASK (pad);
4767 task = gst_task_new (func, data);
4768 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4769 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4770 GST_INFO_OBJECT (pad, "created task %p", task);
4771 GST_PAD_TASK (pad) = task;
4772 gst_object_ref (task);
4773 /* release lock to post the message */
4774 GST_OBJECT_UNLOCK (pad);
4776 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4778 gst_object_unref (task);
4780 GST_OBJECT_LOCK (pad);
4781 /* nobody else is supposed to have changed the pad now */
4782 if (GST_PAD_TASK (pad) != task)
4783 goto concurrent_stop;
4785 res = gst_task_set_state (task, GST_TASK_STARTED);
4786 GST_OBJECT_UNLOCK (pad);
4793 GST_OBJECT_UNLOCK (pad);
4799 * gst_pad_pause_task:
4800 * @pad: the #GstPad to pause the task of
4802 * Pause the task of @pad. This function will also wait until the
4803 * function executed by the task is finished if this function is not
4804 * called from the task function.
4806 * Returns: a TRUE if the task could be paused or FALSE when the pad
4810 gst_pad_pause_task (GstPad * pad)
4815 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4817 GST_DEBUG_OBJECT (pad, "pause task");
4819 GST_OBJECT_LOCK (pad);
4820 task = GST_PAD_TASK (pad);
4823 res = gst_task_set_state (task, GST_TASK_PAUSED);
4824 GST_OBJECT_UNLOCK (pad);
4826 /* wait for task function to finish, this lock is recursive so it does nothing
4827 * when the pause is called from the task itself */
4828 GST_PAD_STREAM_LOCK (pad);
4829 GST_PAD_STREAM_UNLOCK (pad);
4835 GST_DEBUG_OBJECT (pad, "pad has no task");
4836 GST_OBJECT_UNLOCK (pad);
4842 * gst_pad_stop_task:
4843 * @pad: the #GstPad to stop the task of
4845 * Stop the task of @pad. This function will also make sure that the
4846 * function executed by the task will effectively stop if not called
4847 * from the GstTaskFunction.
4849 * This function will deadlock if called from the GstTaskFunction of
4850 * the task. Use gst_task_pause() instead.
4852 * Regardless of whether the pad has a task, the stream lock is acquired and
4853 * released so as to ensure that streaming through this pad has finished.
4855 * Returns: a TRUE if the task could be stopped or FALSE on error.
4858 gst_pad_stop_task (GstPad * pad)
4863 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4865 GST_DEBUG_OBJECT (pad, "stop task");
4867 GST_OBJECT_LOCK (pad);
4868 task = GST_PAD_TASK (pad);
4871 GST_PAD_TASK (pad) = NULL;
4872 res = gst_task_set_state (task, GST_TASK_STOPPED);
4873 GST_OBJECT_UNLOCK (pad);
4875 GST_PAD_STREAM_LOCK (pad);
4876 GST_PAD_STREAM_UNLOCK (pad);
4878 if (!gst_task_join (task))
4881 gst_object_unref (task);
4887 GST_DEBUG_OBJECT (pad, "no task");
4888 GST_OBJECT_UNLOCK (pad);
4890 GST_PAD_STREAM_LOCK (pad);
4891 GST_PAD_STREAM_UNLOCK (pad);
4893 /* this is not an error */
4898 /* this is bad, possibly the application tried to join the task from
4899 * the task's thread. We install the task again so that it will be stopped
4900 * again from the right thread next time hopefully. */
4901 GST_OBJECT_LOCK (pad);
4902 GST_DEBUG_OBJECT (pad, "join failed");
4903 /* we can only install this task if there was no other task */
4904 if (GST_PAD_TASK (pad) == NULL)
4905 GST_PAD_TASK (pad) = task;
4906 GST_OBJECT_UNLOCK (pad);