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"
68 #include "gstmarshal.h"
73 #include "glib-compat-private.h"
75 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
76 #define GST_CAT_DEFAULT GST_CAT_PADS
78 /* Pad signals and args */
96 #define GST_PAD_GET_PRIVATE(obj) \
97 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
99 /* we have a pending and an active event on the pad. On source pads only the
100 * active event is used. On sinkpads, events are copied to the pending entry and
101 * moved to the active event when the eventfunc returned TRUE. */
108 struct _GstPadPrivate
114 guint probe_list_cookie;
124 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
129 GstPadProbeInfo *info;
136 static void gst_pad_dispose (GObject * object);
137 static void gst_pad_finalize (GObject * object);
138 static void gst_pad_set_property (GObject * object, guint prop_id,
139 const GValue * value, GParamSpec * pspec);
140 static void gst_pad_get_property (GObject * object, guint prop_id,
141 GValue * value, GParamSpec * pspec);
143 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
144 static gboolean gst_pad_activate_default (GstPad * pad, GstObject * parent);
145 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
146 GstObject * parent, GstBufferList * list);
148 static GstFlowReturn gst_pad_send_event_unchecked (GstPad * pad,
149 GstEvent * event, GstPadProbeType type);
150 static GstFlowReturn gst_pad_push_event_unchecked (GstPad * pad,
151 GstEvent * event, GstPadProbeType type, gboolean * stored);
153 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
155 static GParamSpec *pspec_caps = NULL;
157 /* quarks for probe signals */
158 static GQuark buffer_quark;
159 static GQuark buffer_list_quark;
160 static GQuark event_quark;
169 static GstFlowQuarks flow_quarks[] = {
170 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
171 {GST_FLOW_OK, "ok", 0},
172 {GST_FLOW_NOT_LINKED, "not-linked", 0},
173 {GST_FLOW_FLUSHING, "flushing", 0},
174 {GST_FLOW_EOS, "eos", 0},
175 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
176 {GST_FLOW_ERROR, "error", 0},
177 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
178 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
183 * @ret: a #GstFlowReturn to get the name of.
185 * Gets a string representing the given flow return.
187 * Returns: a static string with the name of the flow return.
190 gst_flow_get_name (GstFlowReturn ret)
194 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
196 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
197 if (ret == flow_quarks[i].ret)
198 return flow_quarks[i].name;
205 * @ret: a #GstFlowReturn to get the quark of.
207 * Get the unique quark for the given GstFlowReturn.
209 * Returns: the quark associated with the flow return or 0 if an
210 * invalid return was specified.
213 gst_flow_to_quark (GstFlowReturn ret)
217 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
219 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
220 if (ret == flow_quarks[i].ret)
221 return flow_quarks[i].quark;
230 buffer_quark = g_quark_from_static_string ("buffer"); \
231 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
232 event_quark = g_quark_from_static_string ("event"); \
234 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
235 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
238 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
239 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
242 #define gst_pad_parent_class parent_class
243 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
246 gst_pad_class_init (GstPadClass * klass)
248 GObjectClass *gobject_class;
249 GstObjectClass *gstobject_class;
251 gobject_class = G_OBJECT_CLASS (klass);
252 gstobject_class = GST_OBJECT_CLASS (klass);
254 g_type_class_add_private (klass, sizeof (GstPadPrivate));
256 gobject_class->dispose = gst_pad_dispose;
257 gobject_class->finalize = gst_pad_finalize;
258 gobject_class->set_property = gst_pad_set_property;
259 gobject_class->get_property = gst_pad_get_property;
263 * @pad: the pad that emitted the signal
264 * @peer: the peer pad that has been connected
266 * Signals that a pad has been linked to the peer pad.
268 gst_pad_signals[PAD_LINKED] =
269 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
270 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
271 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
274 * @pad: the pad that emitted the signal
275 * @peer: the peer pad that has been disconnected
277 * Signals that a pad has been unlinked from the peer pad.
279 gst_pad_signals[PAD_UNLINKED] =
280 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
281 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
282 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
284 pspec_caps = g_param_spec_boxed ("caps", "Caps",
285 "The capabilities of the pad", GST_TYPE_CAPS,
286 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
287 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
289 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
290 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
291 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
292 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
294 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
295 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
296 g_param_spec_object ("template", "Template",
297 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
298 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
300 gstobject_class->path_string_separator = ".";
302 /* Register common function pointer descriptions */
303 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
304 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
305 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
306 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
307 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
311 gst_pad_init (GstPad * pad)
313 pad->priv = GST_PAD_GET_PRIVATE (pad);
315 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
317 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
318 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
319 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
320 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
321 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
323 GST_PAD_SET_FLUSHING (pad);
325 g_rec_mutex_init (&pad->stream_rec_lock);
327 g_cond_init (&pad->block_cond);
329 g_hook_list_init (&pad->probes, sizeof (GstProbe));
331 pad->priv->events = g_array_sized_new (FALSE, TRUE, sizeof (PadEvent), 16);
334 /* called when setting the pad inactive. It removes all sticky events from
337 remove_events (GstPad * pad)
342 events = pad->priv->events;
345 for (i = 0; i < len; i++) {
346 PadEvent *ev = &g_array_index (events, PadEvent, i);
347 gst_event_unref (ev->event);
349 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
350 g_array_set_size (events, 0);
351 pad->priv->events_cookie++;
355 find_event_by_type (GstPad * pad, GstEventType type, guint idx)
361 events = pad->priv->events;
364 for (i = 0; i < len; i++) {
365 ev = &g_array_index (events, PadEvent, i);
366 if (ev->event == NULL)
369 if (GST_EVENT_TYPE (ev->event) == type) {
381 find_event (GstPad * pad, GstEvent * event)
387 events = pad->priv->events;
390 for (i = 0; i < len; i++) {
391 ev = &g_array_index (events, PadEvent, i);
392 if (event == ev->event)
401 remove_event_by_type (GstPad * pad, GstEventType type)
407 events = pad->priv->events;
412 ev = &g_array_index (events, PadEvent, i);
413 if (ev->event == NULL)
416 if (GST_EVENT_TYPE (ev->event) != type)
419 gst_event_unref (ev->event);
420 g_array_remove_index (events, i);
422 pad->priv->events_cookie++;
431 schedule_events (GstPad * srcpad, GstPad * sinkpad)
436 gboolean pending = FALSE;
438 events = srcpad->priv->events;
441 for (i = 0; i < len; i++) {
442 ev = &g_array_index (events, PadEvent, i);
443 if (ev->event == NULL)
446 if (sinkpad == NULL || !find_event (sinkpad, ev->event)) {
447 ev->received = FALSE;
452 GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PENDING_EVENTS);
455 typedef gboolean (*PadEventFunction) (GstPad * pad, PadEvent * ev,
459 events_foreach (GstPad * pad, PadEventFunction func, gpointer user_data)
466 events = pad->priv->events;
469 cookie = pad->priv->events_cookie;
473 PadEvent *ev, ev_ret;
475 ev = &g_array_index (events, PadEvent, i);
476 if (G_UNLIKELY (ev->event == NULL))
479 /* take aditional ref, func might release the lock */
480 ev_ret.event = gst_event_ref (ev->event);
481 ev_ret.received = ev->received;
483 ret = func (pad, &ev_ret, user_data);
485 /* recheck the cookie, lock might have been released and the list could have
487 if (G_UNLIKELY (cookie != pad->priv->events_cookie)) {
488 if (G_LIKELY (ev_ret.event))
489 gst_event_unref (ev_ret.event);
493 /* if the event changed, we need to do something */
494 if (G_UNLIKELY (ev->event != ev_ret.event)) {
495 if (G_UNLIKELY (ev_ret.event == NULL)) {
496 /* function unreffed and set the event to NULL, remove it */
497 g_array_remove_index (events, i);
499 cookie = ++pad->priv->events_cookie;
502 /* function gave a new event for us */
503 gst_event_take (&ev->event, ev_ret.event);
506 /* just unref, nothing changed */
507 gst_event_unref (ev_ret.event);
516 /* should be called with LOCK */
518 apply_pad_offset (GstPad * pad, GstEvent * event)
520 /* check if we need to adjust the segment */
521 if (pad->offset != 0) {
524 /* copy segment values */
525 gst_event_copy_segment (event, &segment);
526 gst_event_unref (event);
528 /* adjust and make a new event with the offset applied */
529 segment.base += pad->offset;
530 event = gst_event_new_segment (&segment);
535 /* should be called with the OBJECT_LOCK */
537 get_pad_caps (GstPad * pad)
539 GstCaps *caps = NULL;
542 ev = find_event_by_type (pad, GST_EVENT_CAPS, 0);
544 gst_event_parse_caps (ev->event, &caps);
550 gst_pad_dispose (GObject * object)
552 GstPad *pad = GST_PAD_CAST (object);
555 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
557 /* unlink the peer pad */
558 if ((peer = gst_pad_get_peer (pad))) {
559 /* window for MT unsafeness, someone else could unlink here
560 * and then we call unlink with wrong pads. The unlink
561 * function would catch this and safely return failed. */
562 if (GST_PAD_IS_SRC (pad))
563 gst_pad_unlink (pad, peer);
565 gst_pad_unlink (peer, pad);
567 gst_object_unref (peer);
570 gst_pad_set_pad_template (pad, NULL);
574 g_hook_list_clear (&pad->probes);
576 G_OBJECT_CLASS (parent_class)->dispose (object);
580 gst_pad_finalize (GObject * object)
582 GstPad *pad = GST_PAD_CAST (object);
585 /* in case the task is still around, clean it up */
586 if ((task = GST_PAD_TASK (pad))) {
587 gst_task_join (task);
588 GST_PAD_TASK (pad) = NULL;
589 gst_object_unref (task);
592 if (pad->activatenotify)
593 pad->activatenotify (pad->activatedata);
594 if (pad->activatemodenotify)
595 pad->activatemodenotify (pad->activatemodedata);
597 pad->linknotify (pad->linkdata);
598 if (pad->unlinknotify)
599 pad->unlinknotify (pad->unlinkdata);
600 if (pad->chainnotify)
601 pad->chainnotify (pad->chaindata);
602 if (pad->chainlistnotify)
603 pad->chainlistnotify (pad->chainlistdata);
604 if (pad->getrangenotify)
605 pad->getrangenotify (pad->getrangedata);
606 if (pad->eventnotify)
607 pad->eventnotify (pad->eventdata);
608 if (pad->querynotify)
609 pad->querynotify (pad->querydata);
610 if (pad->iterintlinknotify)
611 pad->iterintlinknotify (pad->iterintlinkdata);
613 g_rec_mutex_clear (&pad->stream_rec_lock);
614 g_cond_clear (&pad->block_cond);
615 g_array_free (pad->priv->events, TRUE);
617 G_OBJECT_CLASS (parent_class)->finalize (object);
621 gst_pad_set_property (GObject * object, guint prop_id,
622 const GValue * value, GParamSpec * pspec)
624 g_return_if_fail (GST_IS_PAD (object));
627 case PAD_PROP_DIRECTION:
628 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
630 case PAD_PROP_TEMPLATE:
631 gst_pad_set_pad_template (GST_PAD_CAST (object),
632 (GstPadTemplate *) g_value_get_object (value));
635 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
641 gst_pad_get_property (GObject * object, guint prop_id,
642 GValue * value, GParamSpec * pspec)
644 g_return_if_fail (GST_IS_PAD (object));
648 GST_OBJECT_LOCK (object);
649 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
650 GST_OBJECT_UNLOCK (object);
652 case PAD_PROP_DIRECTION:
653 g_value_set_enum (value, GST_PAD_DIRECTION (object));
655 case PAD_PROP_TEMPLATE:
656 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
659 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
666 * @name: the name of the new pad.
667 * @direction: the #GstPadDirection of the pad.
669 * Creates a new pad with the given name in the given direction.
670 * If name is NULL, a guaranteed unique name (across all pads)
672 * This function makes a copy of the name so you can safely free the name.
674 * Returns: (transfer floating): a new #GstPad, or NULL in case of an error.
679 gst_pad_new (const gchar * name, GstPadDirection direction)
681 return g_object_new (GST_TYPE_PAD,
682 "name", name, "direction", direction, NULL);
686 * gst_pad_new_from_template:
687 * @templ: the pad template to use
688 * @name: the name of the element
690 * Creates a new pad with the given name from the given template.
691 * If name is NULL, a guaranteed unique name (across all pads)
693 * This function makes a copy of the name so you can safely free the name.
695 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
698 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
700 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
702 return g_object_new (GST_TYPE_PAD,
703 "name", name, "direction", templ->direction, "template", templ, NULL);
707 * gst_pad_new_from_static_template:
708 * @templ: the #GstStaticPadTemplate to use
709 * @name: the name of the element
711 * Creates a new pad with the given name from the given static template.
712 * If name is NULL, a guaranteed unique name (across all pads)
714 * This function makes a copy of the name so you can safely free the name.
716 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
719 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
723 GstPadTemplate *template;
725 template = gst_static_pad_template_get (templ);
726 pad = gst_pad_new_from_template (template, name);
727 gst_object_unref (template);
731 #define ACQUIRE_PARENT(pad, parent, label) \
733 if (G_LIKELY ((parent = GST_OBJECT_PARENT (pad)))) \
734 gst_object_ref (parent); \
735 else if (G_LIKELY (GST_PAD_NEEDS_PARENT (pad))) \
739 #define RELEASE_PARENT(parent) \
741 if (G_LIKELY (parent)) \
742 gst_object_unref (parent); \
746 * gst_pad_get_direction:
747 * @pad: a #GstPad to get the direction of.
749 * Gets the direction of the pad. The direction of the pad is
750 * decided at construction time so this function does not take
753 * Returns: the #GstPadDirection of the pad.
758 gst_pad_get_direction (GstPad * pad)
760 GstPadDirection result;
762 /* PAD_UNKNOWN is a little silly but we need some sort of
763 * error return value */
764 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
766 result = GST_PAD_DIRECTION (pad);
772 gst_pad_activate_default (GstPad * pad, GstObject * parent)
774 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
778 pre_activate (GstPad * pad, GstPadMode new_mode)
781 case GST_PAD_MODE_NONE:
782 GST_OBJECT_LOCK (pad);
783 GST_DEBUG_OBJECT (pad, "setting PAD_MODE NONE, set flushing");
784 GST_PAD_SET_FLUSHING (pad);
785 GST_PAD_MODE (pad) = new_mode;
786 /* unlock blocked pads so element can resume and stop */
787 GST_PAD_BLOCK_BROADCAST (pad);
788 GST_OBJECT_UNLOCK (pad);
790 case GST_PAD_MODE_PUSH:
791 case GST_PAD_MODE_PULL:
792 GST_OBJECT_LOCK (pad);
793 GST_DEBUG_OBJECT (pad, "setting PAD_MODE %d, unset flushing", new_mode);
794 GST_PAD_UNSET_FLUSHING (pad);
795 GST_PAD_MODE (pad) = new_mode;
796 if (GST_PAD_IS_SINK (pad)) {
798 /* make sure the peer src pad sends us all events */
799 if ((peer = GST_PAD_PEER (pad))) {
800 gst_object_ref (peer);
801 GST_OBJECT_UNLOCK (pad);
803 GST_DEBUG_OBJECT (pad, "reschedule events on peer %s:%s",
804 GST_DEBUG_PAD_NAME (peer));
806 GST_OBJECT_LOCK (peer);
807 schedule_events (peer, NULL);
808 GST_OBJECT_UNLOCK (peer);
810 gst_object_unref (peer);
812 GST_OBJECT_UNLOCK (pad);
815 GST_OBJECT_UNLOCK (pad);
822 post_activate (GstPad * pad, GstPadMode new_mode)
825 case GST_PAD_MODE_NONE:
826 /* ensures that streaming stops */
827 GST_PAD_STREAM_LOCK (pad);
828 GST_DEBUG_OBJECT (pad, "stopped streaming");
829 GST_OBJECT_LOCK (pad);
831 GST_OBJECT_UNLOCK (pad);
832 GST_PAD_STREAM_UNLOCK (pad);
834 case GST_PAD_MODE_PUSH:
835 case GST_PAD_MODE_PULL:
842 * gst_pad_set_active:
843 * @pad: the #GstPad to activate or deactivate.
844 * @active: whether or not the pad should be active.
846 * Activates or deactivates the given pad.
847 * Normally called from within core state change functions.
849 * If @active, makes sure the pad is active. If it is already active, either in
850 * push or pull mode, just return. Otherwise dispatches to the pad's activate
851 * function to perform the actual activation.
853 * If not @active, checks the pad's current mode and calls
854 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
857 * Returns: #TRUE if the operation was successful.
862 gst_pad_set_active (GstPad * pad, gboolean active)
866 gboolean ret = FALSE;
868 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
870 GST_OBJECT_LOCK (pad);
871 old = GST_PAD_MODE (pad);
872 ACQUIRE_PARENT (pad, parent, no_parent);
873 GST_OBJECT_UNLOCK (pad);
876 if (old == GST_PAD_MODE_NONE) {
877 GST_DEBUG_OBJECT (pad, "activating pad from none");
878 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad, parent);
880 GST_DEBUG_OBJECT (pad, "pad was active in mode %d", old);
884 if (old == GST_PAD_MODE_NONE) {
885 GST_DEBUG_OBJECT (pad, "pad was inactive");
888 GST_DEBUG_OBJECT (pad, "deactivating pad from mode %d", old);
889 ret = gst_pad_activate_mode (pad, old, FALSE);
893 RELEASE_PARENT (parent);
895 if (G_UNLIKELY (!ret))
899 GST_OBJECT_LOCK (pad);
900 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
901 GST_OBJECT_UNLOCK (pad);
908 GST_DEBUG_OBJECT (pad, "no parent");
909 GST_OBJECT_UNLOCK (pad);
914 GST_OBJECT_LOCK (pad);
916 g_critical ("Failed to deactivate pad %s:%s, very bad",
917 GST_DEBUG_PAD_NAME (pad));
919 GST_WARNING_OBJECT (pad, "Failed to activate pad");
921 GST_OBJECT_UNLOCK (pad);
927 * gst_pad_activate_mode:
928 * @pad: the #GstPad to activate or deactivate.
929 * @mode: the requested activation mode
930 * @active: whether or not the pad should be active.
932 * Activates or deactivates the given pad in @mode via dispatching to the
933 * pad's activatemodefunc. For use from within pad activation functions only.
935 * If you don't know what this is, you probably don't want to call it.
937 * Returns: TRUE if the operation was successful.
942 gst_pad_activate_mode (GstPad * pad, GstPadMode mode, gboolean active)
944 gboolean res = FALSE;
950 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
952 GST_OBJECT_LOCK (pad);
953 old = GST_PAD_MODE (pad);
954 dir = GST_PAD_DIRECTION (pad);
955 ACQUIRE_PARENT (pad, parent, no_parent);
956 GST_OBJECT_UNLOCK (pad);
958 new = active ? mode : GST_PAD_MODE_NONE;
963 if (active && old != mode) {
964 /* pad was activate in the wrong direction, deactivate it
965 * and reactivate it in the requested mode */
966 GST_DEBUG_OBJECT (pad, "deactivating pad from mode %d", old);
967 if (G_UNLIKELY (!gst_pad_activate_mode (pad, old, FALSE)))
968 goto deactivate_failed;
972 case GST_PAD_MODE_PULL:
974 if (dir == GST_PAD_SINK) {
975 if ((peer = gst_pad_get_peer (pad))) {
976 GST_DEBUG_OBJECT (pad, "calling peer");
977 if (G_UNLIKELY (!gst_pad_activate_mode (peer, mode, active)))
979 gst_object_unref (peer);
981 /* there is no peer, this is only fatal when we activate. When we
982 * deactivate, we must assume the application has unlinked the peer and
983 * will deactivate it eventually. */
987 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
990 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
991 goto failure; /* Can't activate pull on a src without a
1000 pre_activate (pad, new);
1002 if (GST_PAD_ACTIVATEMODEFUNC (pad)) {
1003 if (G_UNLIKELY (!GST_PAD_ACTIVATEMODEFUNC (pad) (pad, parent, mode,
1007 /* can happen for sinks of passthrough elements */
1010 post_activate (pad, new);
1012 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in mode %d",
1013 active ? "activated" : "deactivated", mode);
1018 RELEASE_PARENT (parent);
1024 GST_DEBUG_OBJECT (pad, "no parent");
1025 GST_OBJECT_UNLOCK (pad);
1030 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in mode %d",
1031 active ? "activated" : "deactivated", mode);
1036 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1037 "failed to %s in switch to mode %d from mode %d",
1038 (active ? "activate" : "deactivate"), mode, old);
1043 GST_OBJECT_LOCK (peer);
1044 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1045 "activate_mode on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
1046 GST_OBJECT_UNLOCK (peer);
1047 gst_object_unref (peer);
1052 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
1053 "pad in pull mode");
1058 GST_OBJECT_LOCK (pad);
1059 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in mode %d",
1060 active ? "activate" : "deactivate", mode);
1061 GST_PAD_SET_FLUSHING (pad);
1062 GST_PAD_MODE (pad) = old;
1063 GST_OBJECT_UNLOCK (pad);
1069 * gst_pad_is_active:
1070 * @pad: the #GstPad to query
1072 * Query if a pad is active
1074 * Returns: TRUE if the pad is active.
1079 gst_pad_is_active (GstPad * pad)
1081 gboolean result = FALSE;
1083 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1085 GST_OBJECT_LOCK (pad);
1086 result = GST_PAD_IS_ACTIVE (pad);
1087 GST_OBJECT_UNLOCK (pad);
1093 * gst_pad_add_probe:
1094 * @pad: the #GstPad to add the probe to
1095 * @mask: the probe mask
1096 * @callback: #GstPadProbeCallback that will be called with notifications of
1098 * @user_data: (closure): user data passed to the callback
1099 * @destroy_data: #GDestroyNotify for user_data
1101 * Be notified of different states of pads. The provided callback is called for
1102 * every state that matches @mask.
1104 * Returns: an id or 0 on error. The id can be used to remove the probe with
1105 * gst_pad_remove_probe().
1110 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
1111 GstPadProbeCallback callback, gpointer user_data,
1112 GDestroyNotify destroy_data)
1117 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1118 g_return_val_if_fail (mask != 0, 0);
1120 GST_OBJECT_LOCK (pad);
1122 /* make a new probe */
1123 hook = g_hook_alloc (&pad->probes);
1125 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1128 /* when no contraints are given for the types, assume all types are
1130 if ((mask & GST_PAD_PROBE_TYPE_ALL_BOTH) == 0)
1131 mask |= GST_PAD_PROBE_TYPE_ALL_BOTH;
1132 if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1133 mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1135 /* store our flags and other fields */
1136 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1137 hook->func = callback;
1138 hook->data = user_data;
1139 hook->destroy = destroy_data;
1140 PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
1143 g_hook_prepend (&pad->probes, hook);
1145 /* incremenent cookie so that the new hook get's called */
1146 pad->priv->probe_list_cookie++;
1148 /* get the id of the hook, we return this and it can be used to remove the
1150 res = hook->hook_id;
1152 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1154 if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1155 /* we have a block probe */
1157 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKED);
1158 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1159 "now %d blocking probes", pad->num_blocked);
1162 /* call the callback if we need to be called for idle callbacks */
1163 if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1164 if (pad->priv->using > 0) {
1165 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1166 * flag above, the last thread to leave the push will do the callback. New
1167 * threads going into the push will block. */
1168 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1169 "pad is in use, delay idle callback");
1170 GST_OBJECT_UNLOCK (pad);
1172 GstPadProbeInfo info = { GST_PAD_PROBE_TYPE_IDLE, res, };
1174 /* the pad is idle now, we can signal the idle callback now */
1175 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1176 "pad is idle, trigger idle callback");
1177 GST_OBJECT_UNLOCK (pad);
1179 callback (pad, &info, user_data);
1182 GST_OBJECT_UNLOCK (pad);
1188 cleanup_hook (GstPad * pad, GHook * hook)
1190 GstPadProbeType type;
1192 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1194 if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1195 /* unblock when we remove the last blocking probe */
1197 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1199 if (pad->num_blocked == 0) {
1200 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1201 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKED);
1202 GST_PAD_BLOCK_BROADCAST (pad);
1205 g_hook_destroy_link (&pad->probes, hook);
1210 * gst_pad_remove_probe:
1211 * @pad: the #GstPad with the probe
1212 * @id: the probe id to remove
1214 * Remove the probe with @id from @pad.
1219 gst_pad_remove_probe (GstPad * pad, gulong id)
1223 g_return_if_fail (GST_IS_PAD (pad));
1225 GST_OBJECT_LOCK (pad);
1227 hook = g_hook_get (&pad->probes, id);
1231 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1233 cleanup_hook (pad, hook);
1234 GST_OBJECT_UNLOCK (pad);
1240 GST_OBJECT_UNLOCK (pad);
1241 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1247 * gst_pad_is_blocked:
1248 * @pad: the #GstPad to query
1250 * Checks if the pad is blocked or not. This function returns the
1251 * last requested state of the pad. It is not certain that the pad
1252 * is actually blocking at this point (see gst_pad_is_blocking()).
1254 * Returns: TRUE if the pad is blocked.
1259 gst_pad_is_blocked (GstPad * pad)
1261 gboolean result = FALSE;
1263 g_return_val_if_fail (GST_IS_PAD (pad), result);
1265 GST_OBJECT_LOCK (pad);
1266 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED);
1267 GST_OBJECT_UNLOCK (pad);
1273 * gst_pad_is_blocking:
1274 * @pad: the #GstPad to query
1276 * Checks if the pad is blocking or not. This is a guaranteed state
1277 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1279 * Returns: TRUE if the pad is blocking.
1286 gst_pad_is_blocking (GstPad * pad)
1288 gboolean result = FALSE;
1290 g_return_val_if_fail (GST_IS_PAD (pad), result);
1292 GST_OBJECT_LOCK (pad);
1293 /* the blocking flag is only valid if the pad is not flushing */
1294 result = GST_PAD_IS_BLOCKING (pad) && !GST_PAD_IS_FLUSHING (pad);
1295 GST_OBJECT_UNLOCK (pad);
1301 * gst_pad_check_reconfigure:
1302 * @pad: the #GstPad to check
1304 * Check and clear the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1305 * if the flag was set.
1307 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag was set on @pad.
1310 gst_pad_check_reconfigure (GstPad * pad)
1312 gboolean reconfigure;
1314 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1316 GST_OBJECT_LOCK (pad);
1317 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1318 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1319 GST_OBJECT_UNLOCK (pad);
1325 * gst_pad_mark_reconfigure:
1326 * @pad: the #GstPad to mark
1328 * Mark a pad for needing reconfiguration. The next call to
1329 * gst_pad_check_reconfigure() will return %TRUE after this call.
1332 gst_pad_mark_reconfigure (GstPad * pad)
1334 g_return_if_fail (GST_IS_PAD (pad));
1336 GST_OBJECT_LOCK (pad);
1337 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1338 GST_OBJECT_UNLOCK (pad);
1342 * gst_pad_set_activate_function_full:
1344 * @activate: the #GstPadActivateFunction to set.
1345 * @user_data: user_data passed to @notify
1346 * @notify: notify called when @activate will not be used anymore.
1348 * Sets the given activate function for @pad. The activate function will
1349 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1350 * the actual activation. Only makes sense to set on sink pads.
1352 * Call this function if your sink pad can start a pull-based task.
1355 gst_pad_set_activate_function_full (GstPad * pad,
1356 GstPadActivateFunction activate, gpointer user_data, GDestroyNotify notify)
1358 g_return_if_fail (GST_IS_PAD (pad));
1360 if (pad->activatenotify)
1361 pad->activatenotify (pad->activatedata);
1362 GST_PAD_ACTIVATEFUNC (pad) = activate;
1363 pad->activatedata = user_data;
1364 pad->activatenotify = notify;
1366 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1367 GST_DEBUG_FUNCPTR_NAME (activate));
1371 * gst_pad_set_activatemode_function_full:
1373 * @activatemode: the #GstPadActivateModeFunction to set.
1374 * @user_data: user_data passed to @notify
1375 * @notify: notify called when @activatemode will not be used anymore.
1377 * Sets the given activate_mode function for the pad. An activate_mode function
1378 * prepares the element for data passing.
1381 gst_pad_set_activatemode_function_full (GstPad * pad,
1382 GstPadActivateModeFunction activatemode, gpointer user_data,
1383 GDestroyNotify notify)
1385 g_return_if_fail (GST_IS_PAD (pad));
1387 if (pad->activatemodenotify)
1388 pad->activatemodenotify (pad->activatemodedata);
1389 GST_PAD_ACTIVATEMODEFUNC (pad) = activatemode;
1390 pad->activatemodedata = user_data;
1391 pad->activatemodenotify = notify;
1393 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatemodefunc set to %s",
1394 GST_DEBUG_FUNCPTR_NAME (activatemode));
1398 * gst_pad_set_chain_function_full:
1399 * @pad: a sink #GstPad.
1400 * @chain: the #GstPadChainFunction to set.
1401 * @user_data: user_data passed to @notify
1402 * @notify: notify called when @chain will not be used anymore.
1404 * Sets the given chain function for the pad. The chain function is called to
1405 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1408 gst_pad_set_chain_function_full (GstPad * pad, GstPadChainFunction chain,
1409 gpointer user_data, GDestroyNotify notify)
1411 g_return_if_fail (GST_IS_PAD (pad));
1412 g_return_if_fail (GST_PAD_IS_SINK (pad));
1414 if (pad->chainnotify)
1415 pad->chainnotify (pad->chaindata);
1416 GST_PAD_CHAINFUNC (pad) = chain;
1417 pad->chaindata = user_data;
1418 pad->chainnotify = notify;
1420 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1421 GST_DEBUG_FUNCPTR_NAME (chain));
1425 * gst_pad_set_chain_list_function_full:
1426 * @pad: a sink #GstPad.
1427 * @chainlist: the #GstPadChainListFunction to set.
1428 * @user_data: user_data passed to @notify
1429 * @notify: notify called when @chainlist will not be used anymore.
1431 * Sets the given chain list function for the pad. The chainlist function is
1432 * called to process a #GstBufferList input buffer list. See
1433 * #GstPadChainListFunction for more details.
1438 gst_pad_set_chain_list_function_full (GstPad * pad,
1439 GstPadChainListFunction chainlist, gpointer user_data,
1440 GDestroyNotify notify)
1442 g_return_if_fail (GST_IS_PAD (pad));
1443 g_return_if_fail (GST_PAD_IS_SINK (pad));
1445 if (pad->chainlistnotify)
1446 pad->chainlistnotify (pad->chainlistdata);
1447 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1448 pad->chainlistdata = user_data;
1449 pad->chainlistnotify = notify;
1451 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1452 GST_DEBUG_FUNCPTR_NAME (chainlist));
1456 * gst_pad_set_getrange_function_full:
1457 * @pad: a source #GstPad.
1458 * @get: the #GstPadGetRangeFunction to set.
1459 * @user_data: user_data passed to @notify
1460 * @notify: notify called when @get will not be used anymore.
1462 * Sets the given getrange function for the pad. The getrange function is
1463 * called to produce a new #GstBuffer to start the processing pipeline. see
1464 * #GstPadGetRangeFunction for a description of the getrange function.
1467 gst_pad_set_getrange_function_full (GstPad * pad, GstPadGetRangeFunction get,
1468 gpointer user_data, GDestroyNotify notify)
1470 g_return_if_fail (GST_IS_PAD (pad));
1471 g_return_if_fail (GST_PAD_IS_SRC (pad));
1473 if (pad->getrangenotify)
1474 pad->getrangenotify (pad->getrangedata);
1475 GST_PAD_GETRANGEFUNC (pad) = get;
1476 pad->getrangedata = user_data;
1477 pad->getrangenotify = notify;
1479 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1480 GST_DEBUG_FUNCPTR_NAME (get));
1484 * gst_pad_set_event_function_full:
1485 * @pad: a #GstPad of either direction.
1486 * @event: the #GstPadEventFunction to set.
1487 * @user_data: user_data passed to @notify
1488 * @notify: notify called when @event will not be used anymore.
1490 * Sets the given event handler for the pad.
1493 gst_pad_set_event_function_full (GstPad * pad, GstPadEventFunction event,
1494 gpointer user_data, GDestroyNotify notify)
1496 g_return_if_fail (GST_IS_PAD (pad));
1498 if (pad->eventnotify)
1499 pad->eventnotify (pad->eventdata);
1500 GST_PAD_EVENTFUNC (pad) = event;
1501 pad->eventdata = user_data;
1502 pad->eventnotify = notify;
1504 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1505 GST_DEBUG_FUNCPTR_NAME (event));
1509 * gst_pad_set_query_function_full:
1510 * @pad: a #GstPad of either direction.
1511 * @query: the #GstPadQueryFunction to set.
1512 * @user_data: user_data passed to @notify
1513 * @notify: notify called when @query will not be used anymore.
1515 * Set the given query function for the pad.
1518 gst_pad_set_query_function_full (GstPad * pad, GstPadQueryFunction query,
1519 gpointer user_data, GDestroyNotify notify)
1521 g_return_if_fail (GST_IS_PAD (pad));
1523 if (pad->querynotify)
1524 pad->querynotify (pad->querydata);
1525 GST_PAD_QUERYFUNC (pad) = query;
1526 pad->querydata = user_data;
1527 pad->querynotify = notify;
1529 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1530 GST_DEBUG_FUNCPTR_NAME (query));
1534 * gst_pad_set_iterate_internal_links_function_full:
1535 * @pad: a #GstPad of either direction.
1536 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1537 * @user_data: user_data passed to @notify
1538 * @notify: notify called when @iterintlink will not be used anymore.
1540 * Sets the given internal link iterator function for the pad.
1545 gst_pad_set_iterate_internal_links_function_full (GstPad * pad,
1546 GstPadIterIntLinkFunction iterintlink, gpointer user_data,
1547 GDestroyNotify notify)
1549 g_return_if_fail (GST_IS_PAD (pad));
1551 if (pad->iterintlinknotify)
1552 pad->iterintlinknotify (pad->iterintlinkdata);
1553 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1554 pad->iterintlinkdata = user_data;
1555 pad->iterintlinknotify = notify;
1557 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1558 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1562 * gst_pad_set_link_function_full:
1564 * @link: the #GstPadLinkFunction to set.
1565 * @user_data: user_data passed to @notify
1566 * @notify: notify called when @link will not be used anymore.
1568 * Sets the given link function for the pad. It will be called when
1569 * the pad is linked with another pad.
1571 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1574 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1575 * cannot be made for some reason.
1577 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1578 * of the peer sink pad, if present.
1581 gst_pad_set_link_function_full (GstPad * pad, GstPadLinkFunction link,
1582 gpointer user_data, GDestroyNotify notify)
1584 g_return_if_fail (GST_IS_PAD (pad));
1586 if (pad->linknotify)
1587 pad->linknotify (pad->linkdata);
1588 GST_PAD_LINKFUNC (pad) = link;
1589 pad->linkdata = user_data;
1590 pad->linknotify = notify;
1592 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1593 GST_DEBUG_FUNCPTR_NAME (link));
1597 * gst_pad_set_unlink_function_full:
1599 * @unlink: the #GstPadUnlinkFunction to set.
1600 * @user_data: user_data passed to @notify
1601 * @notify: notify called when @unlink will not be used anymore.
1603 * Sets the given unlink function for the pad. It will be called
1604 * when the pad is unlinked.
1607 gst_pad_set_unlink_function_full (GstPad * pad, GstPadUnlinkFunction unlink,
1608 gpointer user_data, GDestroyNotify notify)
1610 g_return_if_fail (GST_IS_PAD (pad));
1612 if (pad->unlinknotify)
1613 pad->unlinknotify (pad->unlinkdata);
1614 GST_PAD_UNLINKFUNC (pad) = unlink;
1615 pad->unlinkdata = user_data;
1616 pad->unlinknotify = notify;
1618 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1619 GST_DEBUG_FUNCPTR_NAME (unlink));
1624 * @srcpad: the source #GstPad to unlink.
1625 * @sinkpad: the sink #GstPad to unlink.
1627 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1628 * signal on both pads.
1630 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1631 * the pads were not linked together.
1636 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1638 gboolean result = FALSE;
1639 GstElement *parent = NULL;
1641 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1642 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1643 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1644 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1646 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1647 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1648 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1650 /* We need to notify the parent before taking any pad locks as the bin in
1651 * question might be waiting for a lock on the pad while holding its lock
1652 * that our message will try to take. */
1653 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1654 if (GST_IS_ELEMENT (parent)) {
1655 gst_element_post_message (parent,
1656 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1657 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1659 gst_object_unref (parent);
1664 GST_OBJECT_LOCK (srcpad);
1665 GST_OBJECT_LOCK (sinkpad);
1667 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1668 goto not_linked_together;
1670 if (GST_PAD_UNLINKFUNC (srcpad)) {
1671 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1673 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1674 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1677 /* first clear peers */
1678 GST_PAD_PEER (srcpad) = NULL;
1679 GST_PAD_PEER (sinkpad) = NULL;
1681 GST_OBJECT_UNLOCK (sinkpad);
1682 GST_OBJECT_UNLOCK (srcpad);
1684 /* fire off a signal to each of the pads telling them
1685 * that they've been unlinked */
1686 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1687 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1689 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1690 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1695 if (parent != NULL) {
1696 gst_element_post_message (parent,
1697 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1698 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1699 gst_object_unref (parent);
1704 not_linked_together:
1706 /* we do not emit a warning in this case because unlinking cannot
1707 * be made MT safe.*/
1708 GST_OBJECT_UNLOCK (sinkpad);
1709 GST_OBJECT_UNLOCK (srcpad);
1715 * gst_pad_is_linked:
1716 * @pad: pad to check
1718 * Checks if a @pad is linked to another pad or not.
1720 * Returns: TRUE if the pad is linked, FALSE otherwise.
1725 gst_pad_is_linked (GstPad * pad)
1729 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1731 GST_OBJECT_LOCK (pad);
1732 result = (GST_PAD_PEER (pad) != NULL);
1733 GST_OBJECT_UNLOCK (pad);
1738 /* get the caps from both pads and see if the intersection
1741 * This function should be called with the pad LOCK on both
1745 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1746 GstPadLinkCheck flags)
1748 GstCaps *srccaps = NULL;
1749 GstCaps *sinkcaps = NULL;
1750 gboolean compatible = FALSE;
1752 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1755 /* Doing the expensive caps checking takes priority over only checking the template caps */
1756 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1757 GST_OBJECT_UNLOCK (sink);
1758 GST_OBJECT_UNLOCK (src);
1760 srccaps = gst_pad_query_caps (src, NULL);
1761 sinkcaps = gst_pad_query_caps (sink, NULL);
1763 GST_OBJECT_LOCK (src);
1764 GST_OBJECT_LOCK (sink);
1766 /* If one of the two pads doesn't have a template, consider the intersection
1768 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1769 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1773 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1775 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1778 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1780 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1783 /* if we have caps on both pads we can check the intersection. If one
1784 * of the caps is NULL, we return TRUE. */
1785 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1787 gst_caps_unref (srccaps);
1789 gst_caps_unref (sinkcaps);
1793 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1794 gst_caps_unref (srccaps);
1795 gst_caps_unref (sinkcaps);
1798 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1799 (compatible ? "" : "not"));
1804 /* check if the grandparents of both pads are the same.
1805 * This check is required so that we don't try to link
1806 * pads from elements in different bins without ghostpads.
1808 * The LOCK should be held on both pads
1811 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1813 GstObject *psrc, *psink;
1815 psrc = GST_OBJECT_PARENT (src);
1816 psink = GST_OBJECT_PARENT (sink);
1818 /* if one of the pads has no parent, we allow the link */
1819 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1822 /* only care about parents that are elements */
1823 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1824 goto no_element_parent;
1826 /* if the parents are the same, we have a loop */
1827 if (G_UNLIKELY (psrc == psink))
1830 /* if they both have a parent, we check the grandparents. We can not lock
1831 * the parent because we hold on the child (pad) and the locking order is
1832 * parent >> child. */
1833 psrc = GST_OBJECT_PARENT (psrc);
1834 psink = GST_OBJECT_PARENT (psink);
1836 /* if they have grandparents but they are not the same */
1837 if (G_UNLIKELY (psrc != psink))
1838 goto wrong_grandparents;
1845 GST_CAT_DEBUG (GST_CAT_CAPS,
1846 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1847 GST_PTR_FORMAT, psrc, psink);
1852 GST_CAT_DEBUG (GST_CAT_CAPS,
1853 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1854 GST_PTR_FORMAT, psrc, psink);
1859 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1865 GST_CAT_DEBUG (GST_CAT_CAPS,
1866 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1867 GST_PTR_FORMAT, psrc, psink);
1872 /* FIXME leftover from an attempt at refactoring... */
1873 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1874 * the two pads will be locked in the srcpad, sinkpad order. */
1875 static GstPadLinkReturn
1876 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1878 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1879 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1881 GST_OBJECT_LOCK (srcpad);
1883 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1884 goto src_was_linked;
1886 GST_OBJECT_LOCK (sinkpad);
1888 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1889 goto sink_was_linked;
1891 /* check hierarchy, pads can only be linked if the grandparents
1893 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1894 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1895 goto wrong_hierarchy;
1897 /* check pad caps for non-empty intersection */
1898 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1901 /* FIXME check pad scheduling for non-empty intersection */
1903 return GST_PAD_LINK_OK;
1907 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1908 GST_DEBUG_PAD_NAME (srcpad),
1909 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1910 /* we do not emit a warning in this case because unlinking cannot
1911 * be made MT safe.*/
1912 GST_OBJECT_UNLOCK (srcpad);
1913 return GST_PAD_LINK_WAS_LINKED;
1917 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1918 GST_DEBUG_PAD_NAME (sinkpad),
1919 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1920 /* we do not emit a warning in this case because unlinking cannot
1921 * be made MT safe.*/
1922 GST_OBJECT_UNLOCK (sinkpad);
1923 GST_OBJECT_UNLOCK (srcpad);
1924 return GST_PAD_LINK_WAS_LINKED;
1928 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1929 GST_OBJECT_UNLOCK (sinkpad);
1930 GST_OBJECT_UNLOCK (srcpad);
1931 return GST_PAD_LINK_WRONG_HIERARCHY;
1935 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1936 GST_OBJECT_UNLOCK (sinkpad);
1937 GST_OBJECT_UNLOCK (srcpad);
1938 return GST_PAD_LINK_NOFORMAT;
1944 * @srcpad: the source #GstPad.
1945 * @sinkpad: the sink #GstPad.
1947 * Checks if the source pad and the sink pad are compatible so they can be
1950 * Returns: TRUE if the pads can be linked.
1953 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1955 GstPadLinkReturn result;
1957 /* generic checks */
1958 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1959 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1961 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1962 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1964 /* gst_pad_link_prepare does everything for us, we only release the locks
1965 * on the pads that it gets us. If this function returns !OK the locks are not
1967 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1968 if (result != GST_PAD_LINK_OK)
1971 GST_OBJECT_UNLOCK (srcpad);
1972 GST_OBJECT_UNLOCK (sinkpad);
1975 return result == GST_PAD_LINK_OK;
1979 * gst_pad_link_full:
1980 * @srcpad: the source #GstPad to link.
1981 * @sinkpad: the sink #GstPad to link.
1982 * @flags: the checks to validate when linking
1984 * Links the source pad and the sink pad.
1986 * This variant of #gst_pad_link provides a more granular control on the
1987 * checks being done when linking. While providing some considerable speedups
1988 * the caller of this method must be aware that wrong usage of those flags
1989 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
1990 * for more information.
1994 * Returns: A result code indicating if the connection worked or
2000 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2002 GstPadLinkReturn result;
2004 GstPadLinkFunction srcfunc, sinkfunc;
2006 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2007 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2008 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2009 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2010 GST_PAD_LINK_WRONG_DIRECTION);
2012 /* Notify the parent early. See gst_pad_unlink for details. */
2013 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2014 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2015 gst_element_post_message (parent,
2016 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2017 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2019 gst_object_unref (parent);
2024 /* prepare will also lock the two pads */
2025 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2027 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2030 /* must set peers before calling the link function */
2031 GST_PAD_PEER (srcpad) = sinkpad;
2032 GST_PAD_PEER (sinkpad) = srcpad;
2034 /* check events, when something is different, mark pending */
2035 schedule_events (srcpad, sinkpad);
2037 /* get the link functions */
2038 srcfunc = GST_PAD_LINKFUNC (srcpad);
2039 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2041 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2042 /* custom link functions, execute them */
2043 GST_OBJECT_UNLOCK (sinkpad);
2044 GST_OBJECT_UNLOCK (srcpad);
2047 /* this one will call the peer link function */
2048 result = srcfunc (srcpad, sinkpad);
2049 } else if (sinkfunc) {
2050 /* if no source link function, we need to call the sink link
2051 * function ourselves. */
2052 result = sinkfunc (sinkpad, srcpad);
2055 GST_OBJECT_LOCK (srcpad);
2056 GST_OBJECT_LOCK (sinkpad);
2058 /* we released the lock, check if the same pads are linked still */
2059 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2060 goto concurrent_link;
2062 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2065 GST_OBJECT_UNLOCK (sinkpad);
2066 GST_OBJECT_UNLOCK (srcpad);
2068 /* fire off a signal to each of the pads telling them
2069 * that they've been linked */
2070 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2071 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2073 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2074 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2076 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2079 if (G_LIKELY (parent)) {
2080 gst_element_post_message (parent,
2081 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2082 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2083 gst_object_unref (parent);
2091 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2092 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2093 GST_OBJECT_UNLOCK (sinkpad);
2094 GST_OBJECT_UNLOCK (srcpad);
2096 /* The other link operation succeeded first */
2097 result = GST_PAD_LINK_WAS_LINKED;
2102 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2103 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2105 GST_PAD_PEER (srcpad) = NULL;
2106 GST_PAD_PEER (sinkpad) = NULL;
2108 GST_OBJECT_UNLOCK (sinkpad);
2109 GST_OBJECT_UNLOCK (srcpad);
2117 * @srcpad: the source #GstPad to link.
2118 * @sinkpad: the sink #GstPad to link.
2120 * Links the source pad and the sink pad.
2122 * Returns: A result code indicating if the connection worked or
2128 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2130 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2134 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2136 GstPadTemplate **template_p;
2138 /* this function would need checks if it weren't static */
2140 GST_OBJECT_LOCK (pad);
2141 template_p = &pad->padtemplate;
2142 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2143 GST_OBJECT_UNLOCK (pad);
2146 gst_pad_template_pad_created (templ, pad);
2150 * gst_pad_get_pad_template:
2153 * Gets the template for @pad.
2155 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2156 * instantiated, or %NULL if this pad has no template. Unref after
2160 gst_pad_get_pad_template (GstPad * pad)
2162 GstPadTemplate *templ;
2164 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2166 templ = GST_PAD_PAD_TEMPLATE (pad);
2168 return (templ ? gst_object_ref (templ) : NULL);
2172 * gst_pad_has_current_caps:
2173 * @pad: a #GstPad to check
2175 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2177 * Returns: TRUE when @pad has caps associated with it.
2180 gst_pad_has_current_caps (GstPad * pad)
2185 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2187 GST_OBJECT_LOCK (pad);
2188 caps = get_pad_caps (pad);
2189 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2190 "check current pad caps %" GST_PTR_FORMAT, caps);
2191 result = (caps != NULL);
2192 GST_OBJECT_UNLOCK (pad);
2198 * gst_pad_get_current_caps:
2199 * @pad: a #GstPad to get the current capabilities of.
2201 * Gets the capabilities currently configured on @pad with the last
2202 * #GST_EVENT_CAPS event.
2204 * Returns: the current caps of the pad with incremented ref-count.
2207 gst_pad_get_current_caps (GstPad * pad)
2211 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2213 GST_OBJECT_LOCK (pad);
2214 if ((result = get_pad_caps (pad)))
2215 gst_caps_ref (result);
2216 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2217 "get current pad caps %" GST_PTR_FORMAT, result);
2218 GST_OBJECT_UNLOCK (pad);
2225 * @pad: a #GstPad to set the capabilities of.
2226 * @caps: (transfer none): a #GstCaps to set.
2228 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2229 * caps on the pad will be unreffed. This function refs the caps so you should
2230 * unref if as soon as you don't need it anymore.
2231 * It is possible to set NULL caps, which will make the pad unnegotiated
2234 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2235 * or bad parameters were provided to this function.
2240 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2243 gboolean res = TRUE;
2245 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2246 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2248 event = gst_event_new_caps (caps);
2250 if (GST_PAD_IS_SRC (pad))
2251 res = gst_pad_push_event (pad, event);
2253 res = gst_pad_send_event (pad, event);
2259 * gst_pad_get_pad_template_caps:
2260 * @pad: a #GstPad to get the template capabilities from.
2262 * Gets the capabilities for @pad's template.
2264 * Returns: (transfer full): the #GstCaps of this pad template.
2265 * Unref after usage.
2268 gst_pad_get_pad_template_caps (GstPad * pad)
2270 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2272 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2274 if (GST_PAD_PAD_TEMPLATE (pad))
2275 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2277 return gst_static_caps_get (&anycaps);
2282 * @pad: a #GstPad to get the peer of.
2284 * Gets the peer of @pad. This function refs the peer pad so
2285 * you need to unref it after use.
2287 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2292 gst_pad_get_peer (GstPad * pad)
2296 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2298 GST_OBJECT_LOCK (pad);
2299 result = GST_PAD_PEER (pad);
2301 gst_object_ref (result);
2302 GST_OBJECT_UNLOCK (pad);
2308 * gst_pad_get_allowed_caps:
2311 * Gets the capabilities of the allowed media types that can flow through
2312 * @pad and its peer.
2314 * The allowed capabilities is calculated as the intersection of the results of
2315 * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2316 * on the resulting caps.
2318 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2319 * caps when you no longer need it. This function returns NULL when @pad
2325 gst_pad_get_allowed_caps (GstPad * pad)
2332 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2334 GST_OBJECT_LOCK (pad);
2335 peer = GST_PAD_PEER (pad);
2336 if (G_UNLIKELY (peer == NULL))
2339 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2341 gst_object_ref (peer);
2342 GST_OBJECT_UNLOCK (pad);
2343 mycaps = gst_pad_query_caps (pad, NULL);
2345 peercaps = gst_pad_query_caps (peer, NULL);
2346 gst_object_unref (peer);
2348 caps = gst_caps_intersect (mycaps, peercaps);
2349 gst_caps_unref (peercaps);
2350 gst_caps_unref (mycaps);
2352 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2359 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2360 GST_OBJECT_UNLOCK (pad);
2367 * gst_pad_iterate_internal_links_default:
2368 * @pad: the #GstPad to get the internal links of.
2369 * @parent: the parent of @pad or NULL
2371 * Iterate the list of pads to which the given pad is linked to inside of
2372 * the parent element.
2373 * This is the default handler, and thus returns an iterator of all of the
2374 * pads inside the parent element with opposite direction.
2376 * The caller must free this iterator after use with gst_iterator_free().
2378 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2379 * returned pad with gst_object_unref().
2384 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2391 GstElement *eparent;
2393 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2395 if (parent != NULL && GST_IS_ELEMENT (parent)) {
2396 eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2398 GST_OBJECT_LOCK (pad);
2399 eparent = GST_PAD_PARENT (pad);
2400 if (!eparent || !GST_IS_ELEMENT (eparent))
2403 gst_object_ref (eparent);
2404 GST_OBJECT_UNLOCK (pad);
2407 if (pad->direction == GST_PAD_SRC)
2408 padlist = &eparent->sinkpads;
2410 padlist = &eparent->srcpads;
2412 GST_DEBUG_OBJECT (pad, "Making iterator");
2414 cookie = &eparent->pads_cookie;
2416 lock = GST_OBJECT_GET_LOCK (eparent);
2418 res = gst_iterator_new_list (GST_TYPE_PAD,
2419 lock, cookie, padlist, (GObject *) owner, NULL);
2421 gst_object_unref (owner);
2428 GST_OBJECT_UNLOCK (pad);
2429 GST_DEBUG_OBJECT (pad, "no parent element");
2435 * gst_pad_iterate_internal_links:
2436 * @pad: the GstPad to get the internal links of.
2438 * Gets an iterator for the pads to which the given pad is linked to inside
2439 * of the parent element.
2441 * Each #GstPad element yielded by the iterator will have its refcount increased,
2442 * so unref after use.
2444 * Free-function: gst_iterator_free
2446 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
2447 * pad does not have an iterator function configured. Use
2448 * gst_iterator_free() after usage.
2453 gst_pad_iterate_internal_links (GstPad * pad)
2455 GstIterator *res = NULL;
2458 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2460 GST_OBJECT_LOCK (pad);
2461 ACQUIRE_PARENT (pad, parent, no_parent);
2462 GST_OBJECT_UNLOCK (pad);
2464 if (GST_PAD_ITERINTLINKFUNC (pad))
2465 res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2467 RELEASE_PARENT (parent);
2474 GST_DEBUG_OBJECT (pad, "no parent");
2475 GST_OBJECT_UNLOCK (pad);
2483 * @forward: a #GstPadForwardFunction
2484 * @user_data: user data passed to @forward
2486 * Calls @forward for all internally linked pads of @pad. This function deals with
2487 * dynamically changing internal pads and will make sure that the @forward
2488 * function is only called once for each pad.
2490 * When @forward returns TRUE, no further pads will be processed.
2492 * Returns: TRUE if one of the dispatcher functions returned TRUE.
2495 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2498 gboolean result = FALSE;
2500 gboolean done = FALSE;
2501 GValue item = { 0, };
2502 GList *pushed_pads = NULL;
2504 iter = gst_pad_iterate_internal_links (pad);
2510 switch (gst_iterator_next (iter, &item)) {
2511 case GST_ITERATOR_OK:
2515 intpad = g_value_get_object (&item);
2517 /* if already pushed, skip. FIXME, find something faster to tag pads */
2518 if (intpad == NULL || g_list_find (pushed_pads, intpad)) {
2519 g_value_reset (&item);
2523 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2524 GST_DEBUG_PAD_NAME (intpad));
2525 done = result = forward (intpad, user_data);
2527 pushed_pads = g_list_prepend (pushed_pads, intpad);
2529 g_value_reset (&item);
2532 case GST_ITERATOR_RESYNC:
2533 /* We don't reset the result here because we don't push the event
2534 * again on pads that got the event already and because we need
2535 * to consider the result of the previous pushes */
2536 gst_iterator_resync (iter);
2538 case GST_ITERATOR_ERROR:
2539 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2542 case GST_ITERATOR_DONE:
2547 g_value_unset (&item);
2548 gst_iterator_free (iter);
2550 g_list_free (pushed_pads);
2560 gboolean dispatched;
2564 event_forward_func (GstPad * pad, EventData * data)
2566 /* for each pad we send to, we should ref the event; it's up
2567 * to downstream to unref again when handled. */
2568 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2569 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2571 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2573 data->dispatched = TRUE;
2580 * gst_pad_event_default:
2581 * @pad: a #GstPad to call the default event handler on.
2582 * @parent: the parent of @pad or NULL
2583 * @event: (transfer full): the #GstEvent to handle.
2585 * Invokes the default event handler for the given pad.
2587 * The EOS event will pause the task associated with @pad before it is forwarded
2588 * to all internally linked pads,
2590 * The the event is sent to all pads internally linked to @pad. This function
2591 * takes ownership of @event.
2593 * Returns: TRUE if the event was sent successfully.
2596 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2598 gboolean result, forward = TRUE;
2600 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2601 g_return_val_if_fail (event != NULL, FALSE);
2603 GST_LOG_OBJECT (pad, "default event handler");
2605 switch (GST_EVENT_TYPE (event)) {
2608 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
2609 gst_pad_pause_task (pad);
2612 case GST_EVENT_CAPS:
2613 forward = GST_PAD_IS_PROXY_CAPS (pad);
2624 data.dispatched = FALSE;
2625 data.result = FALSE;
2627 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
2629 /* for sinkpads without a parent element or without internal links, nothing
2630 * will be dispatched but we still want to return TRUE. */
2631 if (data.dispatched)
2632 result = data.result;
2637 gst_event_unref (event);
2642 /* Default accept caps implementation just checks against
2643 * the allowed caps for the pad */
2645 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
2647 /* get the caps and see if it intersects to something not empty */
2648 GstCaps *caps, *allowed;
2651 GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
2653 /* first forward the query to internally linked pads when we are dealing with
2655 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2656 if ((result = gst_pad_proxy_query_accept_caps (pad, query))) {
2661 allowed = gst_pad_query_caps (pad, NULL);
2662 gst_query_parse_accept_caps (query, &caps);
2665 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2666 result = gst_caps_is_subset (caps, allowed);
2667 gst_caps_unref (allowed);
2669 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2672 gst_query_set_accept_caps_result (query, result);
2678 /* Default caps implementation */
2680 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
2682 GstCaps *result = NULL, *filter;
2683 GstPadTemplate *templ;
2684 gboolean fixed_caps;
2686 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2688 gst_query_parse_caps (query, &filter);
2690 /* first try to proxy if we must */
2691 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2692 if ((gst_pad_proxy_query_caps (pad, query))) {
2693 gst_query_parse_caps_result (query, &result);
2698 /* no proxy or it failed, do default handling */
2699 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2701 GST_OBJECT_LOCK (pad);
2703 /* fixed caps, try the negotiated caps first */
2704 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
2705 if ((result = get_pad_caps (pad)))
2706 goto filter_done_unlock;
2709 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2710 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
2711 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
2712 goto filter_done_unlock;
2716 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2717 "non-fixed pad caps: trying pad caps");
2718 /* non fixed caps, try the negotiated caps */
2719 if ((result = get_pad_caps (pad)))
2720 goto filter_done_unlock;
2722 GST_OBJECT_UNLOCK (pad);
2724 /* this almost never happens */
2725 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2726 result = gst_caps_new_empty ();
2731 GST_OBJECT_UNLOCK (pad);
2734 /* run the filter on the result */
2736 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2737 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
2738 GST_PTR_FORMAT, result, result, filter, filter);
2739 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2740 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2743 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2744 "using caps %p %" GST_PTR_FORMAT, result, result);
2745 result = gst_caps_ref (result);
2749 gst_query_set_caps_result (query, result);
2750 gst_caps_unref (result);
2756 * gst_pad_query_default:
2757 * @pad: a #GstPad to call the default query handler on.
2758 * @parent: the parent of @pad or NULL
2759 * @query: (transfer none): the #GstQuery to handle.
2761 * Invokes the default query handler for the given pad.
2762 * The query is sent to all pads internally linked to @pad. Note that
2763 * if there are many possible sink pads that are internally linked to
2764 * @pad, only one will be sent the query.
2765 * Multi-sinkpad elements should implement custom query handlers.
2767 * Returns: TRUE if the query was performed successfully.
2770 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
2772 gboolean forward, ret = FALSE;
2774 switch (GST_QUERY_TYPE (query)) {
2775 case GST_QUERY_SCHEDULING:
2778 case GST_QUERY_ALLOCATION:
2779 forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
2781 case GST_QUERY_ACCEPT_CAPS:
2782 ret = gst_pad_query_accept_caps_default (pad, query);
2785 case GST_QUERY_CAPS:
2786 ret = gst_pad_query_caps_default (pad, query);
2789 case GST_QUERY_POSITION:
2790 case GST_QUERY_SEEKING:
2791 case GST_QUERY_FORMATS:
2792 case GST_QUERY_LATENCY:
2793 case GST_QUERY_JITTER:
2794 case GST_QUERY_RATE:
2795 case GST_QUERY_CONVERT:
2802 ret = gst_pad_forward
2803 (pad, (GstPadForwardFunction) gst_pad_peer_query, query);
2809 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
2811 GstPad *pad = data->pad;
2812 GstPadProbeInfo *info = data->info;
2813 GstPadProbeType type, flags;
2814 GstPadProbeCallback callback;
2815 GstPadProbeReturn ret;
2817 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2818 "hook %lu, cookie %u checking", hook->hook_id, PROBE_COOKIE (hook));
2820 /* if we have called this callback, do nothing */
2821 if (PROBE_COOKIE (hook) == data->cookie) {
2822 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2823 "hook %lu, cookie %u already called", hook->hook_id,
2824 PROBE_COOKIE (hook));
2828 PROBE_COOKIE (hook) = data->cookie;
2830 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
2833 /* one of the data types */
2834 if ((flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
2836 /* one of the scheduling types */
2837 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
2839 /* one of the blocking types must match */
2840 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
2841 (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
2843 /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
2844 if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
2845 (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
2848 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2849 "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
2851 callback = (GstPadProbeCallback) hook->func;
2852 if (callback == NULL)
2855 info->id = hook->hook_id;
2857 GST_OBJECT_UNLOCK (pad);
2859 ret = callback (pad, info, hook->data);
2861 GST_OBJECT_LOCK (pad);
2862 data->marshalled = TRUE;
2865 case GST_PAD_PROBE_REMOVE:
2866 /* remove the probe */
2867 GST_DEBUG_OBJECT (pad, "asked to remove hook");
2868 cleanup_hook (pad, hook);
2870 case GST_PAD_PROBE_DROP:
2871 /* need to drop the data, make sure other probes don't get called
2873 GST_DEBUG_OBJECT (pad, "asked to drop item");
2874 info->type = GST_PAD_PROBE_TYPE_INVALID;
2875 data->dropped = TRUE;
2877 case GST_PAD_PROBE_PASS:
2878 /* inform the pad block to let things pass */
2879 GST_DEBUG_OBJECT (pad, "asked to pass item");
2883 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
2890 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2891 "hook %lu with flags 0x%08x does not match %08x", hook->hook_id,
2897 #define PROBE_PRE_PULL(pad,mask,data,offs,size,label,probed,defaultval) \
2899 if (G_UNLIKELY (pad->num_probes)) { \
2900 /* we start with passing NULL as the data item */ \
2901 GstPadProbeInfo info = { mask, 0, NULL, offs, size }; \
2902 ret = do_probe_callbacks (pad, &info, defaultval); \
2903 /* store the possibly updated data item */ \
2904 data = GST_PAD_PROBE_INFO_DATA (&info); \
2905 /* if something went wrong, exit */ \
2906 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2908 /* otherwise check if the probe retured data */ \
2909 if (G_UNLIKELY (data != NULL)) \
2915 /* a probe that does not take or return any data */
2916 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
2918 if (G_UNLIKELY (pad->num_probes)) { \
2919 /* pass NULL as the data item */ \
2920 GstPadProbeInfo info = { mask, 0, NULL, 0, 0 }; \
2921 ret = do_probe_callbacks (pad, &info, defaultval); \
2922 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2927 #define PROBE_FULL(pad,mask,data,offs,size,label,defaultval) \
2929 if (G_UNLIKELY (pad->num_probes)) { \
2930 GstPadProbeInfo info = { mask, 0, data, offs, size }; \
2931 ret = do_probe_callbacks (pad, &info, defaultval); \
2932 data = GST_PAD_PROBE_INFO_DATA (&info); \
2933 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2938 #define PROBE_PUSH(pad,mask,data,label) \
2939 PROBE_FULL(pad, mask, data, -1, -1, label, GST_FLOW_OK);
2940 #define PROBE_PULL(pad,mask,data,offs,size,label) \
2941 PROBE_FULL(pad, mask, data, offs, size, label, GST_FLOW_OK);
2943 static GstFlowReturn
2944 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
2945 GstFlowReturn defaultval)
2954 data.marshalled = FALSE;
2955 data.dropped = FALSE;
2956 data.cookie = ++pad->priv->probe_cookie;
2959 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
2962 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2963 "do probes cookie %u", data.cookie);
2964 cookie = pad->priv->probe_list_cookie;
2966 g_hook_list_marshal (&pad->probes, TRUE,
2967 (GHookMarshaller) probe_hook_marshal, &data);
2969 /* if the list changed, call the new callbacks (they will not have their
2970 * cookie set to data.cookie */
2971 if (cookie != pad->priv->probe_list_cookie) {
2972 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2973 "probe list changed, restarting");
2977 /* the first item that dropped will stop the hooks and then we drop here */
2981 /* if no handler matched and we are blocking, let the item pass */
2982 if (!data.marshalled && is_block)
2985 /* At this point, all handlers returned either OK or PASS. If one handler
2986 * returned PASS, let the item pass */
2991 while (GST_PAD_IS_BLOCKED (pad)) {
2992 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2993 "we are blocked %d times", pad->num_blocked);
2995 /* we might have released the lock */
2996 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2999 /* now we block the streaming thread. It can be unlocked when we
3000 * deactivate the pad (which will also set the FLUSHING flag) or
3001 * when the pad is unblocked. A flushing event will also unblock
3002 * the pad after setting the FLUSHING flag. */
3003 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3004 "Waiting to be unblocked or set flushing");
3005 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3006 GST_PAD_BLOCK_WAIT (pad);
3007 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3008 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3010 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3020 GST_DEBUG_OBJECT (pad, "pad is flushing");
3021 return GST_FLOW_FLUSHING;
3025 GST_DEBUG_OBJECT (pad, "data is dropped");
3026 return GST_FLOW_CUSTOM_SUCCESS;
3030 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3031 GST_DEBUG_OBJECT (pad, "data is passed");
3039 * gst_pad_get_offset:
3042 * Get the offset applied to the running time of @pad. @pad has to be a source
3045 * Returns: the offset.
3048 gst_pad_get_offset (GstPad * pad)
3052 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3054 GST_OBJECT_LOCK (pad);
3055 result = pad->offset;
3056 GST_OBJECT_UNLOCK (pad);
3062 * gst_pad_set_offset:
3064 * @offset: the offset
3066 * Set the offset that will be applied to the running time of @pad.
3069 gst_pad_set_offset (GstPad * pad, gint64 offset)
3073 g_return_if_fail (GST_IS_PAD (pad));
3075 GST_OBJECT_LOCK (pad);
3076 /* if nothing changed, do nothing */
3077 if (pad->offset == offset)
3080 pad->offset = offset;
3081 GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3083 /* sinkpads will apply their offset the next time a segment
3084 * event is received. */
3085 if (GST_PAD_IS_SINK (pad))
3088 /* resend the last segment event on next buffer push */
3089 if ((ev = find_event_by_type (pad, GST_EVENT_SEGMENT, 0))) {
3090 ev->received = FALSE;
3091 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3095 GST_OBJECT_UNLOCK (pad);
3101 * @pad: a #GstPad to invoke the default query on.
3102 * @query: (transfer none): the #GstQuery to perform.
3104 * Dispatches a query to a pad. The query should have been allocated by the
3105 * caller via one of the type-specific allocation functions. The element that
3106 * the pad belongs to is responsible for filling the query with an appropriate
3107 * response, which should then be parsed with a type-specific query parsing
3110 * Again, the caller is responsible for both the allocation and deallocation of
3111 * the query structure.
3113 * Please also note that some queries might need a running pipeline to work.
3115 * Returns: TRUE if the query could be performed.
3118 gst_pad_query (GstPad * pad, GstQuery * query)
3122 GstPadQueryFunction func;
3123 GstPadProbeType type;
3126 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3127 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3129 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3130 GST_QUERY_TYPE_NAME (query));
3132 if (GST_PAD_IS_SRC (pad))
3133 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3135 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3137 GST_OBJECT_LOCK (pad);
3138 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3139 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3140 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3142 ACQUIRE_PARENT (pad, parent, no_parent);
3143 GST_OBJECT_UNLOCK (pad);
3145 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3148 res = func (pad, parent, query);
3150 RELEASE_PARENT (parent);
3152 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3153 GST_QUERY_TYPE_NAME (query), res);
3158 GST_OBJECT_LOCK (pad);
3159 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3160 GST_OBJECT_UNLOCK (pad);
3167 GST_DEBUG_OBJECT (pad, "had no parent");
3168 GST_OBJECT_UNLOCK (pad);
3173 GST_DEBUG_OBJECT (pad, "had no query function");
3174 RELEASE_PARENT (parent);
3179 GST_DEBUG_OBJECT (pad, "query failed");
3184 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3185 GST_OBJECT_UNLOCK (pad);
3191 * gst_pad_peer_query:
3192 * @pad: a #GstPad to invoke the peer query on.
3193 * @query: (transfer none): the #GstQuery to perform.
3195 * Performs gst_pad_query() on the peer of @pad.
3197 * The caller is responsible for both the allocation and deallocation of
3198 * the query structure.
3200 * Returns: TRUE if the query could be performed. This function returns %FALSE
3201 * if @pad has no peer.
3206 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3209 GstPadProbeType type;
3213 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3214 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3216 if (GST_PAD_IS_SRC (pad))
3217 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3219 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3221 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3222 GST_QUERY_TYPE_NAME (query));
3224 GST_OBJECT_LOCK (pad);
3225 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3226 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3227 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3229 peerpad = GST_PAD_PEER (pad);
3230 if (G_UNLIKELY (peerpad == NULL))
3233 gst_object_ref (peerpad);
3234 GST_OBJECT_UNLOCK (pad);
3236 res = gst_pad_query (peerpad, query);
3238 gst_object_unref (peerpad);
3243 GST_OBJECT_LOCK (pad);
3244 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3245 GST_OBJECT_UNLOCK (pad);
3252 GST_WARNING_OBJECT (pad, "pad has no peer");
3253 GST_OBJECT_UNLOCK (pad);
3258 GST_DEBUG_OBJECT (pad, "query failed");
3263 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3264 GST_OBJECT_UNLOCK (pad);
3269 /**********************************************************************
3270 * Data passing functions
3274 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3276 GstFlowReturn *data = user_data;
3280 GST_DEBUG_OBJECT (pad, "event %s was already received",
3281 GST_EVENT_TYPE_NAME (ev->event));
3284 GST_OBJECT_UNLOCK (pad);
3286 *data = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3287 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, &stored);
3289 GST_OBJECT_LOCK (pad);
3290 return *data == GST_FLOW_OK;
3293 /* this is the chain function that does not perform the additional argument
3294 * checking for that little extra speed.
3296 static inline GstFlowReturn
3297 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3302 GST_PAD_STREAM_LOCK (pad);
3304 GST_OBJECT_LOCK (pad);
3305 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3308 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3311 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3313 PROBE_PUSH (pad, type, data, probe_stopped);
3315 parent = GST_OBJECT_PARENT (pad);
3316 GST_OBJECT_UNLOCK (pad);
3318 /* NOTE: we read the chainfunc unlocked.
3319 * we cannot hold the lock for the pad so we might send
3320 * the data to the wrong function. This is not really a
3321 * problem since functions are assigned at creation time
3322 * and don't change that often... */
3323 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3324 GstPadChainFunction chainfunc;
3326 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3329 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3330 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3331 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3333 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
3335 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3336 "called chainfunction &%s with buffer %p, returned %s",
3337 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3339 GstPadChainListFunction chainlistfunc;
3341 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3344 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3345 "calling chainlistfunction &%s",
3346 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3348 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
3350 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3351 "called chainlistfunction &%s, returned %s",
3352 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3355 GST_PAD_STREAM_UNLOCK (pad);
3362 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3363 "chaining, but pad was flushing");
3364 GST_OBJECT_UNLOCK (pad);
3365 GST_PAD_STREAM_UNLOCK (pad);
3366 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3367 return GST_FLOW_FLUSHING;
3371 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3372 ("chain on pad %s:%s but it was not in push mode",
3373 GST_DEBUG_PAD_NAME (pad)));
3374 GST_OBJECT_UNLOCK (pad);
3375 GST_PAD_STREAM_UNLOCK (pad);
3376 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3377 return GST_FLOW_ERROR;
3381 GST_OBJECT_UNLOCK (pad);
3382 GST_PAD_STREAM_UNLOCK (pad);
3383 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3386 case GST_FLOW_CUSTOM_SUCCESS:
3387 GST_DEBUG_OBJECT (pad, "dropped buffer");
3391 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3398 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3399 GST_ELEMENT_ERROR (parent, CORE, PAD, (NULL),
3400 ("chain on pad %s:%s but it has no chainfunction",
3401 GST_DEBUG_PAD_NAME (pad)));
3402 GST_PAD_STREAM_UNLOCK (pad);
3403 return GST_FLOW_NOT_SUPPORTED;
3409 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3410 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3413 * Chain a buffer to @pad.
3415 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3417 * If the buffer type is not acceptable for @pad (as negotiated with a
3418 * preceeding GST_EVENT_CAPS event), this function returns
3419 * #GST_FLOW_NOT_NEGOTIATED.
3421 * The function proceeds calling the chain function installed on @pad (see
3422 * gst_pad_set_chain_function()) and the return value of that function is
3423 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3426 * In all cases, success or failure, the caller loses its reference to @buffer
3427 * after calling this function.
3429 * Returns: a #GstFlowReturn from the pad.
3434 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3436 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3437 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3438 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3440 return gst_pad_chain_data_unchecked (pad,
3441 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3444 static GstFlowReturn
3445 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
3446 GstBufferList * list)
3452 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3454 len = gst_buffer_list_length (list);
3457 for (i = 0; i < len; i++) {
3458 buffer = gst_buffer_list_get (list, i);
3460 gst_pad_chain_data_unchecked (pad,
3461 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3462 gst_buffer_ref (buffer));
3463 if (ret != GST_FLOW_OK)
3466 gst_buffer_list_unref (list);
3472 * gst_pad_chain_list:
3473 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3474 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3477 * Chain a bufferlist to @pad.
3479 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3481 * If @pad was not negotiated properly with a CAPS event, this function
3482 * returns #GST_FLOW_NOT_NEGOTIATED.
3484 * The function proceeds calling the chainlist function installed on @pad (see
3485 * gst_pad_set_chain_list_function()) and the return value of that function is
3486 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3487 * chainlist function.
3489 * In all cases, success or failure, the caller loses its reference to @list
3490 * after calling this function.
3494 * Returns: a #GstFlowReturn from the pad.
3499 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3501 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3502 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3503 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3505 return gst_pad_chain_data_unchecked (pad,
3506 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3509 static GstFlowReturn
3510 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3515 GST_OBJECT_LOCK (pad);
3516 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3519 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3522 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3523 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3525 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3528 events_foreach (pad, push_sticky, &ret);
3529 if (ret != GST_FLOW_OK)
3533 /* do block probes */
3534 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3536 /* do post-blocking probes */
3537 PROBE_PUSH (pad, type, data, probe_stopped);
3539 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3542 /* take ref to peer pad before releasing the lock */
3543 gst_object_ref (peer);
3545 GST_OBJECT_UNLOCK (pad);
3547 ret = gst_pad_chain_data_unchecked (peer, type, data);
3549 gst_object_unref (peer);
3551 GST_OBJECT_LOCK (pad);
3553 if (pad->priv->using == 0) {
3554 /* pad is not active anymore, trigger idle callbacks */
3555 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3556 probe_stopped, ret);
3558 GST_OBJECT_UNLOCK (pad);
3562 /* ERROR recovery here */
3566 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3567 "pushing, but pad was flushing");
3568 GST_OBJECT_UNLOCK (pad);
3569 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3570 return GST_FLOW_FLUSHING;
3574 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3575 ("pushing on pad %s:%s but it was not activated in push mode",
3576 GST_DEBUG_PAD_NAME (pad)));
3577 GST_OBJECT_UNLOCK (pad);
3578 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3579 return GST_FLOW_ERROR;
3583 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3584 "error pushing events, return %s", gst_flow_get_name (ret));
3585 GST_OBJECT_UNLOCK (pad);
3586 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3591 GST_OBJECT_UNLOCK (pad);
3592 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3595 case GST_FLOW_CUSTOM_SUCCESS:
3596 GST_DEBUG_OBJECT (pad, "dropped buffer");
3600 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3607 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3608 "pushing, but it was not linked");
3609 GST_OBJECT_UNLOCK (pad);
3610 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3611 return GST_FLOW_NOT_LINKED;
3617 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3618 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3621 * Pushes a buffer to the peer of @pad.
3623 * This function will call installed block probes before triggering any
3624 * installed data probes.
3626 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3627 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3630 * In all cases, success or failure, the caller loses its reference to @buffer
3631 * after calling this function.
3633 * Returns: a #GstFlowReturn from the peer pad.
3638 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3640 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3641 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3642 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3644 return gst_pad_push_data (pad,
3645 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3649 * gst_pad_push_list:
3650 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3651 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3654 * Pushes a buffer list to the peer of @pad.
3656 * This function will call installed block probes before triggering any
3657 * installed data probes.
3659 * The function proceeds calling the chain function on the peer pad and returns
3660 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3661 * be returned. If the peer pad does not have any installed chainlist function
3662 * every group buffer of the list will be merged into a normal #GstBuffer and
3663 * chained via gst_pad_chain().
3665 * In all cases, success or failure, the caller loses its reference to @list
3666 * after calling this function.
3668 * Returns: a #GstFlowReturn from the peer pad.
3675 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3677 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3678 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3679 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3681 return gst_pad_push_data (pad,
3682 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3685 static GstFlowReturn
3686 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3687 GstBuffer ** buffer)
3690 GstPadGetRangeFunction getrangefunc;
3693 GST_PAD_STREAM_LOCK (pad);
3695 GST_OBJECT_LOCK (pad);
3696 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3699 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
3702 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3703 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3705 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3708 events_foreach (pad, push_sticky, &ret);
3709 if (ret != GST_FLOW_OK)
3713 /* when one of the probes returns a buffer, probed_data will be called and we
3714 * skip calling the getrange function */
3715 PROBE_PRE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3716 *buffer, offset, size, probe_stopped, probed_data, GST_FLOW_OK);
3718 ACQUIRE_PARENT (pad, parent, no_parent);
3719 GST_OBJECT_UNLOCK (pad);
3721 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3724 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3725 "calling getrangefunc %s, offset %"
3726 G_GUINT64_FORMAT ", size %u",
3727 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3729 ret = getrangefunc (pad, parent, offset, size, buffer);
3731 RELEASE_PARENT (parent);
3733 if (G_UNLIKELY (ret != GST_FLOW_OK))
3734 goto get_range_failed;
3736 /* can only fire the signal if we have a valid buffer */
3737 GST_OBJECT_LOCK (pad);
3739 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3740 *buffer, offset, size, probe_stopped_unref);
3741 GST_OBJECT_UNLOCK (pad);
3743 GST_PAD_STREAM_UNLOCK (pad);
3750 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3751 "getrange, but pad was flushing");
3752 GST_OBJECT_UNLOCK (pad);
3753 GST_PAD_STREAM_UNLOCK (pad);
3754 return GST_FLOW_FLUSHING;
3758 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3759 ("getrange on pad %s:%s but it was not activated in pull mode",
3760 GST_DEBUG_PAD_NAME (pad)));
3761 GST_OBJECT_UNLOCK (pad);
3762 GST_PAD_STREAM_UNLOCK (pad);
3763 return GST_FLOW_ERROR;
3767 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
3768 GST_OBJECT_UNLOCK (pad);
3769 GST_PAD_STREAM_UNLOCK (pad);
3774 GST_DEBUG_OBJECT (pad, "no parent");
3775 GST_OBJECT_UNLOCK (pad);
3776 GST_PAD_STREAM_UNLOCK (pad);
3777 return GST_FLOW_FLUSHING;
3781 GST_ELEMENT_ERROR (parent, CORE, PAD, (NULL),
3782 ("getrange on pad %s:%s but it has no getrangefunction",
3783 GST_DEBUG_PAD_NAME (pad)));
3784 RELEASE_PARENT (parent);
3785 GST_PAD_STREAM_UNLOCK (pad);
3786 return GST_FLOW_NOT_SUPPORTED;
3790 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3791 "probe returned %s", gst_flow_get_name (ret));
3792 GST_OBJECT_UNLOCK (pad);
3793 GST_PAD_STREAM_UNLOCK (pad);
3796 probe_stopped_unref:
3798 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3799 "probe returned %s", gst_flow_get_name (ret));
3800 GST_OBJECT_UNLOCK (pad);
3801 GST_PAD_STREAM_UNLOCK (pad);
3802 gst_buffer_unref (*buffer);
3808 GST_PAD_STREAM_UNLOCK (pad);
3810 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3811 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3812 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
3818 * gst_pad_get_range:
3819 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
3820 * @offset: The start offset of the buffer
3821 * @size: The length of the buffer
3822 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
3823 * returns #GST_FLOW_ERROR if %NULL.
3825 * When @pad is flushing this function returns #GST_FLOW_FLUSHING
3826 * immediately and @buffer is %NULL.
3828 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
3829 * description of a getrange function. If @pad has no getrange function
3830 * installed (see gst_pad_set_getrange_function()) this function returns
3831 * #GST_FLOW_NOT_SUPPORTED.
3833 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
3835 * Returns: a #GstFlowReturn from the pad.
3840 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
3841 GstBuffer ** buffer)
3843 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3844 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3845 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3847 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
3851 * gst_pad_pull_range:
3852 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3853 * @offset: The start offset of the buffer
3854 * @size: The length of the buffer
3855 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
3856 * GST_FLOW_ERROR if %NULL.
3858 * Pulls a @buffer from the peer pad.
3860 * This function will first trigger the pad block signal if it was
3863 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
3864 * function returns the result of gst_pad_get_range() on the peer pad.
3865 * See gst_pad_get_range() for a list of return values and for the
3866 * semantics of the arguments of this function.
3868 * Returns: a #GstFlowReturn from the peer pad.
3869 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
3870 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
3871 * @buffer may not be used or freed when any other return value than
3872 * #GST_FLOW_OK is returned.
3877 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
3878 GstBuffer ** buffer)
3883 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3884 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3885 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3887 GST_OBJECT_LOCK (pad);
3888 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3891 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
3894 /* when one of the probes returns a buffer, probed_data will be called and we
3895 * skip calling the peer getrange function */
3896 PROBE_PRE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3897 *buffer, offset, size, pre_probe_stopped, probed_data, GST_FLOW_OK);
3899 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3902 gst_object_ref (peer);
3904 GST_OBJECT_UNLOCK (pad);
3906 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
3908 gst_object_unref (peer);
3910 GST_OBJECT_LOCK (pad);
3912 if (pad->priv->using == 0) {
3913 /* pad is not active anymore, trigger idle callbacks */
3914 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
3915 post_probe_stopped, ret);
3918 if (G_UNLIKELY (ret != GST_FLOW_OK))
3919 goto pull_range_failed;
3922 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3923 *buffer, offset, size, post_probe_stopped);
3925 GST_OBJECT_UNLOCK (pad);
3929 /* ERROR recovery here */
3932 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3933 "pullrange, but pad was flushing");
3934 GST_OBJECT_UNLOCK (pad);
3935 return GST_FLOW_FLUSHING;
3939 GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
3940 ("gpulltange on pad %s:%s but it was not activated in pull mode",
3941 GST_DEBUG_PAD_NAME (pad)));
3942 GST_OBJECT_UNLOCK (pad);
3943 return GST_FLOW_ERROR;
3947 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
3948 gst_flow_get_name (ret));
3949 GST_OBJECT_UNLOCK (pad);
3954 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3955 "pulling range, but it was not linked");
3956 GST_OBJECT_UNLOCK (pad);
3957 return GST_FLOW_NOT_LINKED;
3962 GST_OBJECT_UNLOCK (pad);
3963 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3964 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3965 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
3970 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3971 "post probe returned %s", gst_flow_get_name (ret));
3972 GST_OBJECT_UNLOCK (pad);
3973 if (ret == GST_FLOW_OK)
3974 gst_buffer_unref (*buffer);
3981 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event, gboolean locked)
3986 gboolean res = FALSE;
3987 const gchar *name = NULL;
3989 type = GST_EVENT_TYPE (event);
3990 if (type & GST_EVENT_TYPE_STICKY_MULTI)
3991 name = gst_structure_get_name (gst_event_get_structure (event));
3993 events = pad->priv->events;
3996 for (i = 0; i < len; i++) {
3997 PadEvent *ev = &g_array_index (events, PadEvent, i);
3999 if (ev->event == NULL)
4002 if (type == GST_EVENT_TYPE (ev->event)) {
4003 /* matching types, check matching name if needed */
4004 if (name && !gst_event_has_name (ev->event, name))
4008 if ((res = gst_event_replace (&ev->event, event)))
4009 ev->received = FALSE;
4015 ev.event = gst_event_ref (event);
4016 ev.received = FALSE;
4017 g_array_append_val (events, ev);
4022 pad->priv->events_cookie++;
4023 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4025 GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4027 switch (GST_EVENT_TYPE (event)) {
4028 case GST_EVENT_CAPS:
4030 GST_OBJECT_UNLOCK (pad);
4032 GST_DEBUG_OBJECT (pad, "notify caps");
4033 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4036 GST_OBJECT_LOCK (pad);
4045 static GstFlowReturn
4046 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
4047 GstPadProbeType type, gboolean * stored)
4051 GstEventType event_type;
4054 sticky = GST_EVENT_IS_STICKY (event);
4056 GST_OBJECT_LOCK (pad);
4058 /* Two checks to be made:
4059 * . (un)set the FLUSHING flag for flushing events,
4060 * . handle pad blocking */
4061 event_type = GST_EVENT_TYPE (event);
4063 switch (event_type) {
4064 case GST_EVENT_FLUSH_START:
4065 GST_PAD_SET_FLUSHING (pad);
4067 GST_PAD_BLOCK_BROADCAST (pad);
4068 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4070 case GST_EVENT_FLUSH_STOP:
4071 GST_PAD_UNSET_FLUSHING (pad);
4073 /* Remove sticky EOS events */
4074 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4075 remove_event_by_type (pad, GST_EVENT_EOS);
4077 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4081 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4084 /* store the event on the pad, but only on srcpads. We always store the
4085 * event exactly how it was sent */
4087 /* srcpad sticky events are store immediately, the received flag is set
4088 * to FALSE and will be set to TRUE when we can successfully push the
4089 * event to the peer pad */
4090 if (gst_pad_store_sticky_event (pad, event, TRUE)) {
4091 GST_DEBUG_OBJECT (pad, "event %s updated",
4092 GST_EVENT_TYPE_NAME (event));
4097 switch (GST_EVENT_TYPE (event)) {
4098 case GST_EVENT_SEGMENT:
4099 /* pass the adjusted segment event on. We need to do this even if
4100 * there is no peer pad because of the probes. */
4101 event = apply_pad_offset (pad, event);
4103 case GST_EVENT_RECONFIGURE:
4104 if (GST_PAD_IS_SINK (pad))
4105 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4110 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4111 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4116 /* send probes after modifying the events above */
4117 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4119 /* now check the peer pad */
4120 peerpad = GST_PAD_PEER (pad);
4121 if (peerpad == NULL)
4124 gst_object_ref (peerpad);
4126 GST_OBJECT_UNLOCK (pad);
4128 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4129 event, GST_EVENT_TYPE_NAME (event), peerpad);
4131 ret = gst_pad_send_event_unchecked (peerpad, event, type);
4133 /* Note: we gave away ownership of the event at this point but we can still
4134 * print the old pointer */
4135 GST_LOG_OBJECT (pad,
4136 "sent event %p to peerpad %" GST_PTR_FORMAT ", ret %s", event, peerpad,
4137 gst_flow_get_name (ret));
4139 gst_object_unref (peerpad);
4141 GST_OBJECT_LOCK (pad);
4143 if (ret == GST_FLOW_OK) {
4146 if ((ev = find_event (pad, event)))
4147 ev->received = TRUE;
4149 GST_DEBUG_OBJECT (pad, "event marked received");
4151 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4152 GST_DEBUG_OBJECT (pad, "mark pending events");
4156 if (pad->priv->using == 0) {
4157 /* pad is not active anymore, trigger idle callbacks */
4158 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4159 idle_probe_stopped, ret);
4161 GST_OBJECT_UNLOCK (pad);
4165 /* ERROR handling */
4168 GST_DEBUG_OBJECT (pad, "We're flushing");
4169 GST_OBJECT_UNLOCK (pad);
4170 gst_event_unref (event);
4171 return GST_FLOW_FLUSHING;
4175 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4176 GST_OBJECT_UNLOCK (pad);
4177 gst_event_unref (event);
4180 case GST_FLOW_CUSTOM_SUCCESS:
4181 GST_DEBUG_OBJECT (pad, "dropped event");
4185 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
4192 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4193 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4194 GST_OBJECT_UNLOCK (pad);
4195 gst_event_unref (event);
4196 return sticky ? GST_FLOW_OK : GST_FLOW_NOT_LINKED;
4200 GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
4201 GST_OBJECT_UNLOCK (pad);
4207 * gst_pad_push_event:
4208 * @pad: a #GstPad to push the event to.
4209 * @event: (transfer full): the #GstEvent to send to the pad.
4211 * Sends the event to the peer of the given pad. This function is
4212 * mainly used by elements to send events to their peer
4215 * This function takes owership of the provided event so you should
4216 * gst_event_ref() it if you want to reuse the event after this call.
4218 * Returns: TRUE if the event was handled.
4223 gst_pad_push_event (GstPad * pad, GstEvent * event)
4226 GstPadProbeType type;
4229 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4230 g_return_val_if_fail (event != NULL, FALSE);
4231 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4233 if (GST_PAD_IS_SRC (pad)) {
4234 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4235 goto wrong_direction;
4236 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4237 } else if (GST_PAD_IS_SINK (pad)) {
4238 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4239 goto wrong_direction;
4240 /* events pushed on sinkpad never are sticky */
4241 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4243 goto unknown_direction;
4245 if (gst_pad_push_event_unchecked (pad, event, type, &stored) != GST_FLOW_OK)
4246 res = stored ? TRUE : FALSE;
4252 /* ERROR handling */
4255 g_warning ("pad %s:%s pushing %s event in wrong direction",
4256 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4257 gst_event_unref (event);
4262 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4263 gst_event_unref (event);
4268 /* Check if we can call the event function with the given event */
4269 static GstFlowReturn
4270 pre_eventfunc_check (GstPad * pad, GstEvent * event)
4272 GstCaps *caps, *templ;
4274 switch (GST_EVENT_TYPE (event)) {
4275 case GST_EVENT_CAPS:
4277 /* backwards compatibility mode for caps */
4278 gst_event_parse_caps (event, &caps);
4280 /* See if pad accepts the caps */
4281 templ = gst_pad_get_pad_template_caps (pad);
4282 if (!gst_caps_is_subset (caps, templ))
4285 gst_caps_unref (templ);
4296 gst_caps_unref (templ);
4297 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4298 "caps %" GST_PTR_FORMAT " not accepted", caps);
4299 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4300 "no intersection with template %" GST_PTR_FORMAT, templ);
4301 return GST_FLOW_NOT_NEGOTIATED;
4305 static GstFlowReturn
4306 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
4307 GstPadProbeType type)
4310 GstEventType event_type;
4311 gboolean serialized, need_unlock = FALSE, sticky;
4312 GstPadEventFunction eventfunc;
4315 GST_OBJECT_LOCK (pad);
4316 if (GST_PAD_IS_SINK (pad))
4317 serialized = GST_EVENT_IS_SERIALIZED (event);
4320 sticky = GST_EVENT_IS_STICKY (event);
4321 event_type = GST_EVENT_TYPE (event);
4322 switch (event_type) {
4323 case GST_EVENT_FLUSH_START:
4324 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4325 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4327 /* can't even accept a flush begin event when flushing */
4328 if (GST_PAD_IS_FLUSHING (pad))
4331 GST_PAD_SET_FLUSHING (pad);
4332 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4334 case GST_EVENT_FLUSH_STOP:
4335 if (G_LIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_NONE)) {
4336 GST_PAD_UNSET_FLUSHING (pad);
4337 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4339 /* Remove pending EOS events */
4340 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4341 remove_event_by_type (pad, GST_EVENT_EOS);
4343 GST_OBJECT_UNLOCK (pad);
4344 /* grab stream lock */
4345 GST_PAD_STREAM_LOCK (pad);
4347 GST_OBJECT_LOCK (pad);
4348 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4351 case GST_EVENT_RECONFIGURE:
4352 if (GST_PAD_IS_SRC (pad))
4353 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4355 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4356 "have event type %" GST_PTR_FORMAT, event);
4358 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4362 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4363 GST_OBJECT_UNLOCK (pad);
4364 GST_PAD_STREAM_LOCK (pad);
4366 GST_OBJECT_LOCK (pad);
4367 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4371 switch (GST_EVENT_TYPE (event)) {
4372 case GST_EVENT_SEGMENT:
4373 event = apply_pad_offset (pad, event);
4379 /* now do the probe */
4381 type | GST_PAD_PROBE_TYPE_PUSH |
4382 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4384 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4388 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4391 ACQUIRE_PARENT (pad, parent, no_parent);
4392 GST_OBJECT_UNLOCK (pad);
4394 ret = pre_eventfunc_check (pad, event);
4395 if (G_UNLIKELY (ret != GST_FLOW_OK))
4396 goto precheck_failed;
4399 gst_event_ref (event);
4401 if (eventfunc (pad, parent, event)) {
4404 /* something went wrong */
4405 switch (event_type) {
4406 case GST_EVENT_CAPS:
4407 ret = GST_FLOW_NOT_NEGOTIATED;
4410 ret = GST_FLOW_ERROR;
4414 RELEASE_PARENT (parent);
4416 GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
4419 if (ret == GST_FLOW_OK) {
4420 /* after the event function accepted the event, we can store the sticky
4421 * event on the pad */
4422 gst_pad_store_sticky_event (pad, event, FALSE);
4424 gst_event_unref (event);
4428 GST_PAD_STREAM_UNLOCK (pad);
4432 /* ERROR handling */
4435 GST_OBJECT_UNLOCK (pad);
4437 GST_PAD_STREAM_UNLOCK (pad);
4438 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4439 "Received event on flushing pad. Discarding");
4440 gst_event_unref (event);
4441 return GST_FLOW_FLUSHING;
4445 GST_OBJECT_UNLOCK (pad);
4447 GST_PAD_STREAM_UNLOCK (pad);
4448 gst_event_unref (event);
4451 case GST_FLOW_CUSTOM_SUCCESS:
4452 GST_DEBUG_OBJECT (pad, "dropped event");
4456 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
4463 g_warning ("pad %s:%s has no event handler, file a bug.",
4464 GST_DEBUG_PAD_NAME (pad));
4465 GST_OBJECT_UNLOCK (pad);
4467 GST_PAD_STREAM_UNLOCK (pad);
4468 gst_event_unref (event);
4469 return GST_FLOW_NOT_SUPPORTED;
4473 GST_DEBUG_OBJECT (pad, "no parent");
4474 GST_OBJECT_UNLOCK (pad);
4476 GST_PAD_STREAM_UNLOCK (pad);
4477 gst_event_unref (event);
4478 return GST_FLOW_FLUSHING;
4482 GST_DEBUG_OBJECT (pad, "pre event check failed");
4483 RELEASE_PARENT (parent);
4485 GST_PAD_STREAM_UNLOCK (pad);
4486 gst_event_unref (event);
4492 * gst_pad_send_event:
4493 * @pad: a #GstPad to send the event to.
4494 * @event: (transfer full): the #GstEvent to send to the pad.
4496 * Sends the event to the pad. This function can be used
4497 * by applications to send events in the pipeline.
4499 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4500 * sink pad, @event should be a downstream event. For example, you would not
4501 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4502 * Furthermore, some downstream events have to be serialized with data flow,
4503 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4504 * the event needs to be serialized with data flow, this function will take the
4505 * pad's stream lock while calling its event function.
4507 * To find out whether an event type is upstream, downstream, or downstream and
4508 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4509 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4510 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4511 * plugin doesn't need to bother itself with this information; the core handles
4512 * all necessary locks and checks.
4514 * This function takes owership of the provided event so you should
4515 * gst_event_ref() it if you want to reuse the event after this call.
4517 * Returns: TRUE if the event was handled.
4520 gst_pad_send_event (GstPad * pad, GstEvent * event)
4523 GstPadProbeType type;
4525 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4526 g_return_val_if_fail (event != NULL, FALSE);
4528 if (GST_PAD_IS_SINK (pad)) {
4529 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4530 goto wrong_direction;
4531 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4532 } else if (GST_PAD_IS_SRC (pad)) {
4533 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4534 goto wrong_direction;
4535 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4537 goto unknown_direction;
4539 if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
4546 /* ERROR handling */
4549 g_warning ("pad %s:%s sending %s event in wrong direction",
4550 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4551 gst_event_unref (event);
4556 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4557 gst_event_unref (event);
4563 * gst_pad_set_element_private:
4564 * @pad: the #GstPad to set the private data of.
4565 * @priv: The private data to attach to the pad.
4567 * Set the given private data gpointer on the pad.
4568 * This function can only be used by the element that owns the pad.
4569 * No locking is performed in this function.
4572 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4574 pad->element_private = priv;
4578 * gst_pad_get_element_private:
4579 * @pad: the #GstPad to get the private data of.
4581 * Gets the private data of a pad.
4582 * No locking is performed in this function.
4584 * Returns: (transfer none): a #gpointer to the private data.
4587 gst_pad_get_element_private (GstPad * pad)
4589 return pad->element_private;
4593 * gst_pad_get_sticky_event:
4594 * @pad: the #GstPad to get the event from.
4595 * @event_type: the #GstEventType that should be retrieved.
4596 * @idx: the index of the event
4598 * Returns a new reference of the sticky event of type @event_type
4601 * Returns: (transfer full): a #GstEvent of type @event_type or NULL when no
4602 * event of @event_type was on @pad. Unref after usage.
4605 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
4607 GstEvent *event = NULL;
4610 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4611 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4613 GST_OBJECT_LOCK (pad);
4614 ev = find_event_by_type (pad, event_type, idx);
4615 if (ev && (event = ev->event))
4616 gst_event_ref (event);
4617 GST_OBJECT_UNLOCK (pad);
4624 GstPadStickyEventsForeachFunction func;
4629 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
4631 ForeachDispatch *data = user_data;
4634 GST_OBJECT_UNLOCK (pad);
4636 ret = data->func (pad, &ev->event, data->user_data);
4638 GST_OBJECT_LOCK (pad);
4644 * gst_pad_sticky_events_foreach:
4645 * @pad: the #GstPad that should be used for iteration.
4646 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
4647 * should be called for every event.
4648 * @user_data: (closure): the optional user data.
4650 * Iterates all sticky events on @pad and calls @foreach_func for every
4651 * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
4654 gst_pad_sticky_events_foreach (GstPad * pad,
4655 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4657 ForeachDispatch data;
4659 g_return_if_fail (GST_IS_PAD (pad));
4660 g_return_if_fail (foreach_func != NULL);
4662 data.func = foreach_func;
4663 data.user_data = user_data;
4665 GST_OBJECT_LOCK (pad);
4666 events_foreach (pad, foreach_dispatch_function, &data);
4667 GST_OBJECT_UNLOCK (pad);
4671 do_stream_status (GstPad * pad, GstStreamStatusType type,
4672 GThread * thread, GstTask * task)
4676 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4678 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4679 if (GST_IS_ELEMENT (parent)) {
4680 GstMessage *message;
4681 GValue value = { 0 };
4683 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4684 gchar *tname, *ename, *pname;
4686 /* create a good task name */
4687 ename = gst_element_get_name (parent);
4688 pname = gst_pad_get_name (pad);
4689 tname = g_strdup_printf ("%s:%s", ename, pname);
4693 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4697 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4700 g_value_init (&value, GST_TYPE_TASK);
4701 g_value_set_object (&value, task);
4702 gst_message_set_stream_status_object (message, &value);
4703 g_value_unset (&value);
4705 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4706 gst_element_post_message (parent, message);
4708 gst_object_unref (parent);
4713 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4715 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4720 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4722 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4726 static GstTaskThreadCallbacks thr_callbacks = {
4732 * gst_pad_start_task:
4733 * @pad: the #GstPad to start the task of
4734 * @func: the task function to call
4735 * @data: data passed to the task function
4737 * Starts a task that repeatedly calls @func with @data. This function
4738 * is mostly used in pad activation functions to start the dataflow.
4739 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4740 * before @func is called.
4742 * Returns: a %TRUE if the task could be started.
4745 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4750 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4751 g_return_val_if_fail (func != NULL, FALSE);
4753 GST_DEBUG_OBJECT (pad, "start task");
4755 GST_OBJECT_LOCK (pad);
4756 task = GST_PAD_TASK (pad);
4758 task = gst_task_new (func, data);
4759 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4760 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4761 GST_DEBUG_OBJECT (pad, "created task");
4762 GST_PAD_TASK (pad) = task;
4763 gst_object_ref (task);
4764 /* release lock to post the message */
4765 GST_OBJECT_UNLOCK (pad);
4767 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4769 gst_object_unref (task);
4771 GST_OBJECT_LOCK (pad);
4772 /* nobody else is supposed to have changed the pad now */
4773 if (GST_PAD_TASK (pad) != task)
4774 goto concurrent_stop;
4776 res = gst_task_set_state (task, GST_TASK_STARTED);
4777 GST_OBJECT_UNLOCK (pad);
4784 GST_OBJECT_UNLOCK (pad);
4790 * gst_pad_pause_task:
4791 * @pad: the #GstPad to pause the task of
4793 * Pause the task of @pad. This function will also wait until the
4794 * function executed by the task is finished if this function is not
4795 * called from the task function.
4797 * Returns: a TRUE if the task could be paused or FALSE when the pad
4801 gst_pad_pause_task (GstPad * pad)
4806 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4808 GST_DEBUG_OBJECT (pad, "pause task");
4810 GST_OBJECT_LOCK (pad);
4811 task = GST_PAD_TASK (pad);
4814 res = gst_task_set_state (task, GST_TASK_PAUSED);
4815 GST_OBJECT_UNLOCK (pad);
4817 /* wait for task function to finish, this lock is recursive so it does nothing
4818 * when the pause is called from the task itself */
4819 GST_PAD_STREAM_LOCK (pad);
4820 GST_PAD_STREAM_UNLOCK (pad);
4826 GST_DEBUG_OBJECT (pad, "pad has no task");
4827 GST_OBJECT_UNLOCK (pad);
4833 * gst_pad_stop_task:
4834 * @pad: the #GstPad to stop the task of
4836 * Stop the task of @pad. This function will also make sure that the
4837 * function executed by the task will effectively stop if not called
4838 * from the GstTaskFunction.
4840 * This function will deadlock if called from the GstTaskFunction of
4841 * the task. Use gst_task_pause() instead.
4843 * Regardless of whether the pad has a task, the stream lock is acquired and
4844 * released so as to ensure that streaming through this pad has finished.
4846 * Returns: a TRUE if the task could be stopped or FALSE on error.
4849 gst_pad_stop_task (GstPad * pad)
4854 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4856 GST_DEBUG_OBJECT (pad, "stop task");
4858 GST_OBJECT_LOCK (pad);
4859 task = GST_PAD_TASK (pad);
4862 GST_PAD_TASK (pad) = NULL;
4863 res = gst_task_set_state (task, GST_TASK_STOPPED);
4864 GST_OBJECT_UNLOCK (pad);
4866 GST_PAD_STREAM_LOCK (pad);
4867 GST_PAD_STREAM_UNLOCK (pad);
4869 if (!gst_task_join (task))
4872 gst_object_unref (task);
4878 GST_DEBUG_OBJECT (pad, "no task");
4879 GST_OBJECT_UNLOCK (pad);
4881 GST_PAD_STREAM_LOCK (pad);
4882 GST_PAD_STREAM_UNLOCK (pad);
4884 /* this is not an error */
4889 /* this is bad, possibly the application tried to join the task from
4890 * the task's thread. We install the task again so that it will be stopped
4891 * again from the right thread next time hopefully. */
4892 GST_OBJECT_LOCK (pad);
4893 GST_DEBUG_OBJECT (pad, "join failed");
4894 /* we can only install this task if there was no other task */
4895 if (GST_PAD_TASK (pad) == NULL)
4896 GST_PAD_TASK (pad) = task;
4897 GST_OBJECT_UNLOCK (pad);