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 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
64 * with newer GLib versions (>= 2.31.0) */
65 #define GLIB_DISABLE_DEPRECATION_WARNINGS
66 #include "gst_private.h"
69 #include "gstpadtemplate.h"
70 #include "gstenumtypes.h"
71 #include "gstmarshal.h"
76 #include "glib-compat-private.h"
78 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
79 #define GST_CAT_DEFAULT GST_CAT_PADS
81 /* Pad signals and args */
99 #define GST_PAD_GET_PRIVATE(obj) \
100 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
102 /* we have a pending and an active event on the pad. On source pads only the
103 * active event is used. On sinkpads, events are copied to the pending entry and
104 * moved to the active event when the eventfunc returned TRUE. */
111 struct _GstPadPrivate
117 guint probe_list_cookie;
127 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
132 GstPadProbeInfo *info;
139 static void gst_pad_dispose (GObject * object);
140 static void gst_pad_finalize (GObject * object);
141 static void gst_pad_set_property (GObject * object, guint prop_id,
142 const GValue * value, GParamSpec * pspec);
143 static void gst_pad_get_property (GObject * object, guint prop_id,
144 GValue * value, GParamSpec * pspec);
146 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
147 static gboolean gst_pad_activate_default (GstPad * pad, GstObject * parent);
148 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
149 GstObject * parent, GstBufferList * list);
151 static GstFlowReturn gst_pad_send_event_unchecked (GstPad * pad,
152 GstEvent * event, GstPadProbeType type);
153 static GstFlowReturn gst_pad_push_event_unchecked (GstPad * pad,
154 GstEvent * event, GstPadProbeType type, gboolean * stored);
156 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
158 static GParamSpec *pspec_caps = NULL;
160 /* quarks for probe signals */
161 static GQuark buffer_quark;
162 static GQuark buffer_list_quark;
163 static GQuark event_quark;
172 static GstFlowQuarks flow_quarks[] = {
173 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
174 {GST_FLOW_RESEND, "resend", 0},
175 {GST_FLOW_OK, "ok", 0},
176 {GST_FLOW_NOT_LINKED, "not-linked", 0},
177 {GST_FLOW_WRONG_STATE, "wrong-state", 0},
178 {GST_FLOW_EOS, "eos", 0},
179 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
180 {GST_FLOW_ERROR, "error", 0},
181 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
182 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
187 * @ret: a #GstFlowReturn to get the name of.
189 * Gets a string representing the given flow return.
191 * Returns: a static string with the name of the flow return.
194 gst_flow_get_name (GstFlowReturn ret)
198 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
200 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
201 if (ret == flow_quarks[i].ret)
202 return flow_quarks[i].name;
209 * @ret: a #GstFlowReturn to get the quark of.
211 * Get the unique quark for the given GstFlowReturn.
213 * Returns: the quark associated with the flow return or 0 if an
214 * invalid return was specified.
217 gst_flow_to_quark (GstFlowReturn ret)
221 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
223 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
224 if (ret == flow_quarks[i].ret)
225 return flow_quarks[i].quark;
234 buffer_quark = g_quark_from_static_string ("buffer"); \
235 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
236 event_quark = g_quark_from_static_string ("event"); \
238 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
239 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
242 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
243 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
246 #define gst_pad_parent_class parent_class
247 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
250 gst_pad_class_init (GstPadClass * klass)
252 GObjectClass *gobject_class;
253 GstObjectClass *gstobject_class;
255 gobject_class = G_OBJECT_CLASS (klass);
256 gstobject_class = GST_OBJECT_CLASS (klass);
258 g_type_class_add_private (klass, sizeof (GstPadPrivate));
260 gobject_class->dispose = gst_pad_dispose;
261 gobject_class->finalize = gst_pad_finalize;
262 gobject_class->set_property = gst_pad_set_property;
263 gobject_class->get_property = gst_pad_get_property;
267 * @pad: the pad that emitted the signal
268 * @peer: the peer pad that has been connected
270 * Signals that a pad has been linked to the peer pad.
272 gst_pad_signals[PAD_LINKED] =
273 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
274 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
275 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
278 * @pad: the pad that emitted the signal
279 * @peer: the peer pad that has been disconnected
281 * Signals that a pad has been unlinked from the peer pad.
283 gst_pad_signals[PAD_UNLINKED] =
284 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
285 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
286 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
288 pspec_caps = g_param_spec_boxed ("caps", "Caps",
289 "The capabilities of the pad", GST_TYPE_CAPS,
290 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
291 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
293 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
294 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
295 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
296 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
298 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
299 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
300 g_param_spec_object ("template", "Template",
301 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
302 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304 gstobject_class->path_string_separator = ".";
306 /* Register common function pointer descriptions */
307 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
308 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
309 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
310 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
311 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
315 gst_pad_init (GstPad * pad)
317 pad->priv = GST_PAD_GET_PRIVATE (pad);
319 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
321 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
322 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
323 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
324 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
325 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
327 GST_PAD_SET_FLUSHING (pad);
329 g_static_rec_mutex_init (&pad->stream_rec_lock);
331 pad->block_cond = g_cond_new ();
333 g_hook_list_init (&pad->probes, sizeof (GstProbe));
335 pad->priv->events = g_array_sized_new (FALSE, TRUE,
336 sizeof (PadEvent), GST_EVENT_MAX_STICKY);
339 /* called when setting the pad inactive. It removes all sticky events from
342 remove_events (GstPad * pad)
347 events = pad->priv->events;
350 for (i = 0; i < len; i++) {
351 PadEvent *ev = &g_array_index (events, PadEvent, i);
352 gst_event_unref (ev->event);
354 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
355 g_array_set_size (events, 0);
356 pad->priv->events_cookie++;
360 find_event_by_type (GstPad * pad, GstEventType type, guint idx)
366 events = pad->priv->events;
369 for (i = 0; i < len; i++) {
370 ev = &g_array_index (events, PadEvent, i);
371 if (ev->event == NULL)
374 if (GST_EVENT_TYPE (ev->event) == type) {
386 find_event (GstPad * pad, GstEvent * event)
392 events = pad->priv->events;
395 for (i = 0; i < len; i++) {
396 ev = &g_array_index (events, PadEvent, i);
397 if (event == ev->event)
406 remove_event_by_type (GstPad * pad, GstEventType type)
412 events = pad->priv->events;
417 ev = &g_array_index (events, PadEvent, i);
418 if (ev->event == NULL)
421 if (GST_EVENT_TYPE (ev->event) != type)
424 gst_event_unref (ev->event);
425 g_array_remove_index (events, i);
427 pad->priv->events_cookie++;
436 schedule_events (GstPad * srcpad, GstPad * sinkpad)
441 gboolean pending = FALSE;
443 events = srcpad->priv->events;
446 for (i = 0; i < len; i++) {
447 ev = &g_array_index (events, PadEvent, i);
448 if (ev->event == NULL)
451 if (sinkpad == NULL || !find_event (sinkpad, ev->event)) {
452 ev->received = FALSE;
457 GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PENDING_EVENTS);
460 typedef gboolean (*PadEventFunction) (GstPad * pad, PadEvent * ev,
464 events_foreach (GstPad * pad, PadEventFunction func, gpointer user_data)
471 events = pad->priv->events;
474 cookie = pad->priv->events_cookie;
478 PadEvent *ev, ev_ret;
480 ev = &g_array_index (events, PadEvent, i);
481 if (G_UNLIKELY (ev->event == NULL))
484 /* take aditional ref, func might release the lock */
485 ev_ret.event = gst_event_ref (ev->event);
486 ev_ret.received = ev->received;
488 ret = func (pad, &ev_ret, user_data);
490 /* recheck the cookie, lock might have been released and the list could have
492 if (G_UNLIKELY (cookie != pad->priv->events_cookie)) {
493 if (G_LIKELY (ev_ret.event))
494 gst_event_unref (ev_ret.event);
498 /* if the event changed, we need to do something */
499 if (G_UNLIKELY (ev->event != ev_ret.event)) {
500 if (G_UNLIKELY (ev_ret.event == NULL)) {
501 /* function unreffed and set the event to NULL, remove it */
502 g_array_remove_index (events, i);
504 cookie = ++pad->priv->events_cookie;
507 /* function gave a new event for us */
508 gst_event_take (&ev->event, ev_ret.event);
511 /* just unref, nothing changed */
512 gst_event_unref (ev_ret.event);
521 /* should be called with LOCK */
523 apply_pad_offset (GstPad * pad, GstEvent * event)
525 /* check if we need to adjust the segment */
526 if (pad->offset != 0) {
529 /* copy segment values */
530 gst_event_copy_segment (event, &segment);
531 gst_event_unref (event);
533 /* adjust and make a new event with the offset applied */
534 segment.base += pad->offset;
535 event = gst_event_new_segment (&segment);
540 /* should be called with the OBJECT_LOCK */
542 get_pad_caps (GstPad * pad)
544 GstCaps *caps = NULL;
547 ev = find_event_by_type (pad, GST_EVENT_CAPS, 0);
549 gst_event_parse_caps (ev->event, &caps);
555 gst_pad_dispose (GObject * object)
557 GstPad *pad = GST_PAD_CAST (object);
560 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
562 /* unlink the peer pad */
563 if ((peer = gst_pad_get_peer (pad))) {
564 /* window for MT unsafeness, someone else could unlink here
565 * and then we call unlink with wrong pads. The unlink
566 * function would catch this and safely return failed. */
567 if (GST_PAD_IS_SRC (pad))
568 gst_pad_unlink (pad, peer);
570 gst_pad_unlink (peer, pad);
572 gst_object_unref (peer);
575 gst_pad_set_pad_template (pad, NULL);
579 g_hook_list_clear (&pad->probes);
581 G_OBJECT_CLASS (parent_class)->dispose (object);
585 gst_pad_finalize (GObject * object)
587 GstPad *pad = GST_PAD_CAST (object);
590 /* in case the task is still around, clean it up */
591 if ((task = GST_PAD_TASK (pad))) {
592 gst_task_join (task);
593 GST_PAD_TASK (pad) = NULL;
594 gst_object_unref (task);
597 if (pad->activatenotify)
598 pad->activatenotify (pad);
599 if (pad->activatemodenotify)
600 pad->activatemodenotify (pad);
602 pad->linknotify (pad);
603 if (pad->unlinknotify)
604 pad->unlinknotify (pad);
605 if (pad->chainnotify)
606 pad->chainnotify (pad);
607 if (pad->chainlistnotify)
608 pad->chainlistnotify (pad);
609 if (pad->getrangenotify)
610 pad->getrangenotify (pad);
611 if (pad->eventnotify)
612 pad->eventnotify (pad);
613 if (pad->querynotify)
614 pad->querynotify (pad);
615 if (pad->iterintlinknotify)
616 pad->iterintlinknotify (pad);
618 g_static_rec_mutex_free (&pad->stream_rec_lock);
619 g_cond_free (pad->block_cond);
620 g_array_free (pad->priv->events, TRUE);
622 G_OBJECT_CLASS (parent_class)->finalize (object);
626 gst_pad_set_property (GObject * object, guint prop_id,
627 const GValue * value, GParamSpec * pspec)
629 g_return_if_fail (GST_IS_PAD (object));
632 case PAD_PROP_DIRECTION:
633 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
635 case PAD_PROP_TEMPLATE:
636 gst_pad_set_pad_template (GST_PAD_CAST (object),
637 (GstPadTemplate *) g_value_get_object (value));
640 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
646 gst_pad_get_property (GObject * object, guint prop_id,
647 GValue * value, GParamSpec * pspec)
649 g_return_if_fail (GST_IS_PAD (object));
653 GST_OBJECT_LOCK (object);
654 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
655 GST_OBJECT_UNLOCK (object);
657 case PAD_PROP_DIRECTION:
658 g_value_set_enum (value, GST_PAD_DIRECTION (object));
660 case PAD_PROP_TEMPLATE:
661 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
664 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
671 * @name: the name of the new pad.
672 * @direction: the #GstPadDirection of the pad.
674 * Creates a new pad with the given name in the given direction.
675 * If name is NULL, a guaranteed unique name (across all pads)
677 * This function makes a copy of the name so you can safely free the name.
679 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
684 gst_pad_new (const gchar * name, GstPadDirection direction)
686 return g_object_new (GST_TYPE_PAD,
687 "name", name, "direction", direction, NULL);
691 * gst_pad_new_from_template:
692 * @templ: the pad template to use
693 * @name: the name of the element
695 * Creates a new pad with the given name from the given template.
696 * If name is NULL, a guaranteed unique name (across all pads)
698 * This function makes a copy of the name so you can safely free the name.
700 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
703 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
705 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
707 return g_object_new (GST_TYPE_PAD,
708 "name", name, "direction", templ->direction, "template", templ, NULL);
712 * gst_pad_new_from_static_template:
713 * @templ: the #GstStaticPadTemplate to use
714 * @name: the name of the element
716 * Creates a new pad with the given name from the given static template.
717 * If name is NULL, a guaranteed unique name (across all pads)
719 * This function makes a copy of the name so you can safely free the name.
721 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
724 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
728 GstPadTemplate *template;
730 template = gst_static_pad_template_get (templ);
731 pad = gst_pad_new_from_template (template, name);
732 gst_object_unref (template);
736 #define ACQUIRE_PARENT(pad, parent, label) \
738 if (G_LIKELY ((parent = GST_OBJECT_PARENT (pad)))) \
739 gst_object_ref (parent); \
740 else if (G_LIKELY (GST_PAD_NEEDS_PARENT (pad))) \
744 #define RELEASE_PARENT(parent) \
746 if (G_LIKELY (parent)) \
747 gst_object_unref (parent); \
751 * gst_pad_get_direction:
752 * @pad: a #GstPad to get the direction of.
754 * Gets the direction of the pad. The direction of the pad is
755 * decided at construction time so this function does not take
758 * Returns: the #GstPadDirection of the pad.
763 gst_pad_get_direction (GstPad * pad)
765 GstPadDirection result;
767 /* PAD_UNKNOWN is a little silly but we need some sort of
768 * error return value */
769 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
771 result = GST_PAD_DIRECTION (pad);
777 gst_pad_activate_default (GstPad * pad, GstObject * parent)
779 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
783 pre_activate (GstPad * pad, GstPadMode new_mode)
786 case GST_PAD_MODE_NONE:
787 GST_OBJECT_LOCK (pad);
788 GST_DEBUG_OBJECT (pad, "setting PAD_MODE NONE, set flushing");
789 GST_PAD_SET_FLUSHING (pad);
790 GST_PAD_MODE (pad) = new_mode;
791 /* unlock blocked pads so element can resume and stop */
792 GST_PAD_BLOCK_BROADCAST (pad);
793 GST_OBJECT_UNLOCK (pad);
795 case GST_PAD_MODE_PUSH:
796 case GST_PAD_MODE_PULL:
797 GST_OBJECT_LOCK (pad);
798 GST_DEBUG_OBJECT (pad, "setting PAD_MODE %d, unset flushing", new_mode);
799 GST_PAD_UNSET_FLUSHING (pad);
800 GST_PAD_MODE (pad) = new_mode;
801 if (GST_PAD_IS_SINK (pad)) {
803 /* make sure the peer src pad sends us all events */
804 if ((peer = GST_PAD_PEER (pad))) {
805 gst_object_ref (peer);
806 GST_OBJECT_UNLOCK (pad);
808 GST_DEBUG_OBJECT (pad, "reschedule events on peer %s:%s",
809 GST_DEBUG_PAD_NAME (peer));
811 GST_OBJECT_LOCK (peer);
812 schedule_events (peer, NULL);
813 GST_OBJECT_UNLOCK (peer);
815 gst_object_unref (peer);
817 GST_OBJECT_UNLOCK (pad);
820 GST_OBJECT_UNLOCK (pad);
827 post_activate (GstPad * pad, GstPadMode new_mode)
830 case GST_PAD_MODE_NONE:
831 /* ensures that streaming stops */
832 GST_PAD_STREAM_LOCK (pad);
833 GST_DEBUG_OBJECT (pad, "stopped streaming");
834 GST_OBJECT_LOCK (pad);
836 GST_OBJECT_UNLOCK (pad);
837 GST_PAD_STREAM_UNLOCK (pad);
839 case GST_PAD_MODE_PUSH:
840 case GST_PAD_MODE_PULL:
847 * gst_pad_set_active:
848 * @pad: the #GstPad to activate or deactivate.
849 * @active: whether or not the pad should be active.
851 * Activates or deactivates the given pad.
852 * Normally called from within core state change functions.
854 * If @active, makes sure the pad is active. If it is already active, either in
855 * push or pull mode, just return. Otherwise dispatches to the pad's activate
856 * function to perform the actual activation.
858 * If not @active, checks the pad's current mode and calls
859 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
862 * Returns: #TRUE if the operation was successful.
867 gst_pad_set_active (GstPad * pad, gboolean active)
871 gboolean ret = FALSE;
873 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
875 GST_OBJECT_LOCK (pad);
876 old = GST_PAD_MODE (pad);
877 ACQUIRE_PARENT (pad, parent, no_parent);
878 GST_OBJECT_UNLOCK (pad);
881 if (old == GST_PAD_MODE_NONE) {
882 GST_DEBUG_OBJECT (pad, "activating pad from none");
883 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad, parent);
885 GST_DEBUG_OBJECT (pad, "pad was active in mode %d", old);
889 if (old == GST_PAD_MODE_NONE) {
890 GST_DEBUG_OBJECT (pad, "pad was inactive");
893 GST_DEBUG_OBJECT (pad, "deactivating pad from mode %d", old);
894 ret = gst_pad_activate_mode (pad, old, FALSE);
898 RELEASE_PARENT (parent);
900 if (G_UNLIKELY (!ret))
904 GST_OBJECT_LOCK (pad);
905 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
906 GST_OBJECT_UNLOCK (pad);
913 GST_DEBUG_OBJECT (pad, "no parent");
914 GST_OBJECT_UNLOCK (pad);
919 GST_OBJECT_LOCK (pad);
921 g_critical ("Failed to deactivate pad %s:%s, very bad",
922 GST_DEBUG_PAD_NAME (pad));
924 GST_WARNING_OBJECT (pad, "Failed to activate pad");
926 GST_OBJECT_UNLOCK (pad);
932 * gst_pad_activate_mode:
933 * @pad: the #GstPad to activate or deactivate.
934 * @mode: the requested activation mode
935 * @active: whether or not the pad should be active.
937 * Activates or deactivates the given pad in @mode via dispatching to the
938 * pad's activatemodefunc. For use from within pad activation functions only.
940 * If you don't know what this is, you probably don't want to call it.
942 * Returns: TRUE if the operation was successful.
947 gst_pad_activate_mode (GstPad * pad, GstPadMode mode, gboolean active)
949 gboolean res = FALSE;
955 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
957 GST_OBJECT_LOCK (pad);
958 old = GST_PAD_MODE (pad);
959 dir = GST_PAD_DIRECTION (pad);
960 ACQUIRE_PARENT (pad, parent, no_parent);
961 GST_OBJECT_UNLOCK (pad);
963 new = active ? mode : GST_PAD_MODE_NONE;
968 if (active && old != mode) {
969 /* pad was activate in the wrong direction, deactivate it
970 * and reactivate it in the requested mode */
971 GST_DEBUG_OBJECT (pad, "deactivating pad from mode %d", old);
972 if (G_UNLIKELY (!gst_pad_activate_mode (pad, old, FALSE)))
973 goto deactivate_failed;
977 case GST_PAD_MODE_PULL:
979 if (dir == GST_PAD_SINK) {
980 if ((peer = gst_pad_get_peer (pad))) {
981 GST_DEBUG_OBJECT (pad, "calling peer");
982 if (G_UNLIKELY (!gst_pad_activate_mode (peer, mode, active)))
984 gst_object_unref (peer);
986 /* there is no peer, this is only fatal when we activate. When we
987 * deactivate, we must assume the application has unlinked the peer and
988 * will deactivate it eventually. */
992 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
995 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
996 goto failure; /* Can't activate pull on a src without a
1005 pre_activate (pad, new);
1007 if (GST_PAD_ACTIVATEMODEFUNC (pad)) {
1008 if (G_UNLIKELY (!GST_PAD_ACTIVATEMODEFUNC (pad) (pad, parent, mode,
1012 /* can happen for sinks of passthrough elements */
1015 post_activate (pad, new);
1017 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in mode %d",
1018 active ? "activated" : "deactivated", mode);
1023 RELEASE_PARENT (parent);
1029 GST_DEBUG_OBJECT (pad, "no parent");
1030 GST_OBJECT_UNLOCK (pad);
1035 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in mode %d",
1036 active ? "activated" : "deactivated", mode);
1041 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1042 "failed to %s in switch to mode %d from mode %d",
1043 (active ? "activate" : "deactivate"), mode, old);
1048 GST_OBJECT_LOCK (peer);
1049 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1050 "activate_mode on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
1051 GST_OBJECT_UNLOCK (peer);
1052 gst_object_unref (peer);
1057 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
1058 "pad in pull mode");
1063 GST_OBJECT_LOCK (pad);
1064 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in mode %d",
1065 active ? "activate" : "deactivate", mode);
1066 GST_PAD_SET_FLUSHING (pad);
1067 GST_PAD_MODE (pad) = old;
1068 GST_OBJECT_UNLOCK (pad);
1074 * gst_pad_is_active:
1075 * @pad: the #GstPad to query
1077 * Query if a pad is active
1079 * Returns: TRUE if the pad is active.
1084 gst_pad_is_active (GstPad * pad)
1086 gboolean result = FALSE;
1088 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1090 GST_OBJECT_LOCK (pad);
1091 result = GST_PAD_IS_ACTIVE (pad);
1092 GST_OBJECT_UNLOCK (pad);
1098 * gst_pad_add_probe:
1099 * @pad: the #GstPad to add the probe to
1100 * @mask: the probe mask
1101 * @callback: #GstPadProbeCallback that will be called with notifications of
1103 * @user_data: (closure): user data passed to the callback
1104 * @destroy_data: #GDestroyNotify for user_data
1106 * Be notified of different states of pads. The provided callback is called for
1107 * every state that matches @mask.
1109 * Returns: an id or 0 on error. The id can be used to remove the probe with
1110 * gst_pad_remove_probe().
1115 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
1116 GstPadProbeCallback callback, gpointer user_data,
1117 GDestroyNotify destroy_data)
1122 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1123 g_return_val_if_fail (mask != 0, 0);
1125 GST_OBJECT_LOCK (pad);
1127 /* make a new probe */
1128 hook = g_hook_alloc (&pad->probes);
1130 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1133 /* when no contraints are given for the types, assume all types are
1135 if ((mask & GST_PAD_PROBE_TYPE_ALL_BOTH) == 0)
1136 mask |= GST_PAD_PROBE_TYPE_ALL_BOTH;
1137 if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1138 mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1140 /* store our flags and other fields */
1141 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1142 hook->func = callback;
1143 hook->data = user_data;
1144 hook->destroy = destroy_data;
1145 PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
1148 g_hook_prepend (&pad->probes, hook);
1150 /* incremenent cookie so that the new hook get's called */
1151 pad->priv->probe_list_cookie++;
1153 /* get the id of the hook, we return this and it can be used to remove the
1155 res = hook->hook_id;
1157 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1159 if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1160 /* we have a block probe */
1162 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKED);
1163 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1164 "now %d blocking probes", pad->num_blocked);
1167 /* call the callback if we need to be called for idle callbacks */
1168 if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1169 if (pad->priv->using > 0) {
1170 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1171 * flag above, the last thread to leave the push will do the callback. New
1172 * threads going into the push will block. */
1173 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1174 "pad is in use, delay idle callback");
1175 GST_OBJECT_UNLOCK (pad);
1177 GstPadProbeInfo info = { GST_PAD_PROBE_TYPE_IDLE, };
1179 /* the pad is idle now, we can signal the idle callback now */
1180 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1181 "pad is idle, trigger idle callback");
1182 GST_OBJECT_UNLOCK (pad);
1184 callback (pad, &info, user_data);
1187 GST_OBJECT_UNLOCK (pad);
1193 cleanup_hook (GstPad * pad, GHook * hook)
1195 GstPadProbeType type;
1197 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1199 if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1200 /* unblock when we remove the last blocking probe */
1202 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1204 if (pad->num_blocked == 0) {
1205 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1206 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKED);
1207 GST_PAD_BLOCK_BROADCAST (pad);
1210 g_hook_destroy_link (&pad->probes, hook);
1215 * gst_pad_remove_probe:
1216 * @pad: the #GstPad with the probe
1217 * @id: the probe id to remove
1219 * Remove the probe with @id from @pad.
1224 gst_pad_remove_probe (GstPad * pad, gulong id)
1228 g_return_if_fail (GST_IS_PAD (pad));
1230 GST_OBJECT_LOCK (pad);
1232 hook = g_hook_get (&pad->probes, id);
1236 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1238 cleanup_hook (pad, hook);
1239 GST_OBJECT_UNLOCK (pad);
1245 GST_OBJECT_UNLOCK (pad);
1246 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1252 * gst_pad_is_blocked:
1253 * @pad: the #GstPad to query
1255 * Checks if the pad is blocked or not. This function returns the
1256 * last requested state of the pad. It is not certain that the pad
1257 * is actually blocking at this point (see gst_pad_is_blocking()).
1259 * Returns: TRUE if the pad is blocked.
1264 gst_pad_is_blocked (GstPad * pad)
1266 gboolean result = FALSE;
1268 g_return_val_if_fail (GST_IS_PAD (pad), result);
1270 GST_OBJECT_LOCK (pad);
1271 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED);
1272 GST_OBJECT_UNLOCK (pad);
1278 * gst_pad_is_blocking:
1279 * @pad: the #GstPad to query
1281 * Checks if the pad is blocking or not. This is a guaranteed state
1282 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1284 * Returns: TRUE if the pad is blocking.
1291 gst_pad_is_blocking (GstPad * pad)
1293 gboolean result = FALSE;
1295 g_return_val_if_fail (GST_IS_PAD (pad), result);
1297 GST_OBJECT_LOCK (pad);
1298 /* the blocking flag is only valid if the pad is not flushing */
1299 result = GST_PAD_IS_BLOCKING (pad) && !GST_PAD_IS_FLUSHING (pad);
1300 GST_OBJECT_UNLOCK (pad);
1306 * gst_pad_check_reconfigure:
1307 * @pad: the #GstPad to check
1309 * Check and clear the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1310 * if the flag was set.
1312 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag was set on @pad.
1315 gst_pad_check_reconfigure (GstPad * pad)
1317 gboolean reconfigure;
1319 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1321 GST_OBJECT_LOCK (pad);
1322 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1323 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1324 GST_OBJECT_UNLOCK (pad);
1330 * gst_pad_mark_reconfigure:
1331 * @pad: the #GstPad to mark
1333 * Mark a pad for needing reconfiguration. The next call to
1334 * gst_pad_check_reconfigure() will return %TRUE after this call.
1337 gst_pad_mark_reconfigure (GstPad * pad)
1339 g_return_if_fail (GST_IS_PAD (pad));
1341 GST_OBJECT_LOCK (pad);
1342 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1343 GST_OBJECT_UNLOCK (pad);
1347 * gst_pad_set_activate_function_full:
1349 * @activate: the #GstPadActivateFunction to set.
1350 * @notify: notify called when @activate will not be used anymore.
1352 * Sets the given activate function for @pad. The activate function will
1353 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1354 * the actual activation. Only makes sense to set on sink pads.
1356 * Call this function if your sink pad can start a pull-based task.
1359 gst_pad_set_activate_function_full (GstPad * pad,
1360 GstPadActivateFunction activate, GDestroyNotify notify)
1362 g_return_if_fail (GST_IS_PAD (pad));
1364 if (pad->activatenotify)
1365 pad->activatenotify (pad);
1366 GST_PAD_ACTIVATEFUNC (pad) = activate;
1367 pad->activatenotify = notify;
1369 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1370 GST_DEBUG_FUNCPTR_NAME (activate));
1374 * gst_pad_set_activatemode_function_full:
1376 * @activatemode: the #GstPadActivateModeFunction to set.
1377 * @notify: notify called when @activatemode will not be used anymore.
1379 * Sets the given activate_mode function for the pad. An activate_mode function
1380 * prepares the element for data passing.
1383 gst_pad_set_activatemode_function_full (GstPad * pad,
1384 GstPadActivateModeFunction activatemode, GDestroyNotify notify)
1386 g_return_if_fail (GST_IS_PAD (pad));
1388 if (pad->activatemodenotify)
1389 pad->activatemodenotify (pad);
1390 GST_PAD_ACTIVATEMODEFUNC (pad) = activatemode;
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 * @notify: notify called when @chain will not be used anymore.
1403 * Sets the given chain function for the pad. The chain function is called to
1404 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1407 gst_pad_set_chain_function_full (GstPad * pad, GstPadChainFunction chain,
1408 GDestroyNotify notify)
1410 g_return_if_fail (GST_IS_PAD (pad));
1411 g_return_if_fail (GST_PAD_IS_SINK (pad));
1413 if (pad->chainnotify)
1414 pad->chainnotify (pad);
1415 GST_PAD_CHAINFUNC (pad) = chain;
1416 pad->chainnotify = notify;
1418 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1419 GST_DEBUG_FUNCPTR_NAME (chain));
1423 * gst_pad_set_chain_list_function_full:
1424 * @pad: a sink #GstPad.
1425 * @chainlist: the #GstPadChainListFunction to set.
1426 * @notify: notify called when @chainlist will not be used anymore.
1428 * Sets the given chain list function for the pad. The chainlist function is
1429 * called to process a #GstBufferList input buffer list. See
1430 * #GstPadChainListFunction for more details.
1435 gst_pad_set_chain_list_function_full (GstPad * pad,
1436 GstPadChainListFunction chainlist, GDestroyNotify notify)
1438 g_return_if_fail (GST_IS_PAD (pad));
1439 g_return_if_fail (GST_PAD_IS_SINK (pad));
1441 if (pad->chainlistnotify)
1442 pad->chainlistnotify (pad);
1443 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1444 pad->chainlistnotify = notify;
1446 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1447 GST_DEBUG_FUNCPTR_NAME (chainlist));
1451 * gst_pad_set_getrange_function_full:
1452 * @pad: a source #GstPad.
1453 * @get: the #GstPadGetRangeFunction to set.
1454 * @notify: notify called when @get will not be used anymore.
1456 * Sets the given getrange function for the pad. The getrange function is
1457 * called to produce a new #GstBuffer to start the processing pipeline. see
1458 * #GstPadGetRangeFunction for a description of the getrange function.
1461 gst_pad_set_getrange_function_full (GstPad * pad, GstPadGetRangeFunction get,
1462 GDestroyNotify notify)
1464 g_return_if_fail (GST_IS_PAD (pad));
1465 g_return_if_fail (GST_PAD_IS_SRC (pad));
1467 if (pad->getrangenotify)
1468 pad->getrangenotify (pad);
1469 GST_PAD_GETRANGEFUNC (pad) = get;
1470 pad->getrangenotify = notify;
1472 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1473 GST_DEBUG_FUNCPTR_NAME (get));
1477 * gst_pad_set_event_function_full:
1478 * @pad: a #GstPad of either direction.
1479 * @event: the #GstPadEventFunction to set.
1480 * @notify: notify called when @event will not be used anymore.
1482 * Sets the given event handler for the pad.
1485 gst_pad_set_event_function_full (GstPad * pad, GstPadEventFunction event,
1486 GDestroyNotify notify)
1488 g_return_if_fail (GST_IS_PAD (pad));
1490 if (pad->eventnotify)
1491 pad->eventnotify (pad);
1492 GST_PAD_EVENTFUNC (pad) = event;
1493 pad->eventnotify = notify;
1495 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1496 GST_DEBUG_FUNCPTR_NAME (event));
1500 * gst_pad_set_query_function_full:
1501 * @pad: a #GstPad of either direction.
1502 * @query: the #GstPadQueryFunction to set.
1503 * @notify: notify called when @query will not be used anymore.
1505 * Set the given query function for the pad.
1508 gst_pad_set_query_function_full (GstPad * pad, GstPadQueryFunction query,
1509 GDestroyNotify notify)
1511 g_return_if_fail (GST_IS_PAD (pad));
1513 if (pad->querynotify)
1514 pad->querynotify (pad);
1515 GST_PAD_QUERYFUNC (pad) = query;
1516 pad->querynotify = notify;
1518 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1519 GST_DEBUG_FUNCPTR_NAME (query));
1523 * gst_pad_set_iterate_internal_links_function_full:
1524 * @pad: a #GstPad of either direction.
1525 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1526 * @notify: notify called when @iterintlink will not be used anymore.
1528 * Sets the given internal link iterator function for the pad.
1533 gst_pad_set_iterate_internal_links_function_full (GstPad * pad,
1534 GstPadIterIntLinkFunction iterintlink, GDestroyNotify notify)
1536 g_return_if_fail (GST_IS_PAD (pad));
1538 if (pad->iterintlinknotify)
1539 pad->iterintlinknotify (pad);
1540 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1541 pad->iterintlinknotify = notify;
1543 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1544 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1548 * gst_pad_set_link_function_full:
1550 * @link: the #GstPadLinkFunction to set.
1551 * @notify: notify called when @link will not be used anymore.
1553 * Sets the given link function for the pad. It will be called when
1554 * the pad is linked with another pad.
1556 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1559 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1560 * cannot be made for some reason.
1562 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1563 * of the peer sink pad, if present.
1566 gst_pad_set_link_function_full (GstPad * pad, GstPadLinkFunction link,
1567 GDestroyNotify notify)
1569 g_return_if_fail (GST_IS_PAD (pad));
1571 if (pad->linknotify)
1572 pad->linknotify (pad);
1573 GST_PAD_LINKFUNC (pad) = link;
1574 pad->linknotify = notify;
1576 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1577 GST_DEBUG_FUNCPTR_NAME (link));
1581 * gst_pad_set_unlink_function_full:
1583 * @unlink: the #GstPadUnlinkFunction to set.
1584 * @notify: notify called when @unlink will not be used anymore.
1586 * Sets the given unlink function for the pad. It will be called
1587 * when the pad is unlinked.
1590 gst_pad_set_unlink_function_full (GstPad * pad, GstPadUnlinkFunction unlink,
1591 GDestroyNotify notify)
1593 g_return_if_fail (GST_IS_PAD (pad));
1595 if (pad->unlinknotify)
1596 pad->unlinknotify (pad);
1597 GST_PAD_UNLINKFUNC (pad) = unlink;
1598 pad->unlinknotify = notify;
1600 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1601 GST_DEBUG_FUNCPTR_NAME (unlink));
1606 * @srcpad: the source #GstPad to unlink.
1607 * @sinkpad: the sink #GstPad to unlink.
1609 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1610 * signal on both pads.
1612 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1613 * the pads were not linked together.
1618 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1620 gboolean result = FALSE;
1621 GstElement *parent = NULL;
1623 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1624 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1625 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1626 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1628 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1629 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1630 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1632 /* We need to notify the parent before taking any pad locks as the bin in
1633 * question might be waiting for a lock on the pad while holding its lock
1634 * that our message will try to take. */
1635 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1636 if (GST_IS_ELEMENT (parent)) {
1637 gst_element_post_message (parent,
1638 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1639 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1641 gst_object_unref (parent);
1646 GST_OBJECT_LOCK (srcpad);
1647 GST_OBJECT_LOCK (sinkpad);
1649 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1650 goto not_linked_together;
1652 if (GST_PAD_UNLINKFUNC (srcpad)) {
1653 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1655 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1656 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1659 /* first clear peers */
1660 GST_PAD_PEER (srcpad) = NULL;
1661 GST_PAD_PEER (sinkpad) = NULL;
1663 GST_OBJECT_UNLOCK (sinkpad);
1664 GST_OBJECT_UNLOCK (srcpad);
1666 /* fire off a signal to each of the pads telling them
1667 * that they've been unlinked */
1668 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1669 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1671 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1672 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1677 if (parent != NULL) {
1678 gst_element_post_message (parent,
1679 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1680 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1681 gst_object_unref (parent);
1686 not_linked_together:
1688 /* we do not emit a warning in this case because unlinking cannot
1689 * be made MT safe.*/
1690 GST_OBJECT_UNLOCK (sinkpad);
1691 GST_OBJECT_UNLOCK (srcpad);
1697 * gst_pad_is_linked:
1698 * @pad: pad to check
1700 * Checks if a @pad is linked to another pad or not.
1702 * Returns: TRUE if the pad is linked, FALSE otherwise.
1707 gst_pad_is_linked (GstPad * pad)
1711 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1713 GST_OBJECT_LOCK (pad);
1714 result = (GST_PAD_PEER (pad) != NULL);
1715 GST_OBJECT_UNLOCK (pad);
1720 /* get the caps from both pads and see if the intersection
1723 * This function should be called with the pad LOCK on both
1727 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1728 GstPadLinkCheck flags)
1730 GstCaps *srccaps = NULL;
1731 GstCaps *sinkcaps = NULL;
1732 gboolean compatible = FALSE;
1734 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1737 /* Doing the expensive caps checking takes priority over only checking the template caps */
1738 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1739 GST_OBJECT_UNLOCK (sink);
1740 GST_OBJECT_UNLOCK (src);
1742 srccaps = gst_pad_query_caps (src, NULL);
1743 sinkcaps = gst_pad_query_caps (sink, NULL);
1745 GST_OBJECT_LOCK (src);
1746 GST_OBJECT_LOCK (sink);
1748 /* If one of the two pads doesn't have a template, consider the intersection
1750 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1751 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1755 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1757 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1760 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1762 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1765 /* if we have caps on both pads we can check the intersection. If one
1766 * of the caps is NULL, we return TRUE. */
1767 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1769 gst_caps_unref (srccaps);
1771 gst_caps_unref (sinkcaps);
1775 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1776 gst_caps_unref (srccaps);
1777 gst_caps_unref (sinkcaps);
1780 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1781 (compatible ? "" : "not"));
1786 /* check if the grandparents of both pads are the same.
1787 * This check is required so that we don't try to link
1788 * pads from elements in different bins without ghostpads.
1790 * The LOCK should be held on both pads
1793 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1795 GstObject *psrc, *psink;
1797 psrc = GST_OBJECT_PARENT (src);
1798 psink = GST_OBJECT_PARENT (sink);
1800 /* if one of the pads has no parent, we allow the link */
1801 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1804 /* only care about parents that are elements */
1805 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1806 goto no_element_parent;
1808 /* if the parents are the same, we have a loop */
1809 if (G_UNLIKELY (psrc == psink))
1812 /* if they both have a parent, we check the grandparents. We can not lock
1813 * the parent because we hold on the child (pad) and the locking order is
1814 * parent >> child. */
1815 psrc = GST_OBJECT_PARENT (psrc);
1816 psink = GST_OBJECT_PARENT (psink);
1818 /* if they have grandparents but they are not the same */
1819 if (G_UNLIKELY (psrc != psink))
1820 goto wrong_grandparents;
1827 GST_CAT_DEBUG (GST_CAT_CAPS,
1828 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1829 GST_PTR_FORMAT, psrc, psink);
1834 GST_CAT_DEBUG (GST_CAT_CAPS,
1835 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1836 GST_PTR_FORMAT, psrc, psink);
1841 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1847 GST_CAT_DEBUG (GST_CAT_CAPS,
1848 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1849 GST_PTR_FORMAT, psrc, psink);
1854 /* FIXME leftover from an attempt at refactoring... */
1855 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1856 * the two pads will be locked in the srcpad, sinkpad order. */
1857 static GstPadLinkReturn
1858 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1860 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1861 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1863 GST_OBJECT_LOCK (srcpad);
1865 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1866 goto src_was_linked;
1868 GST_OBJECT_LOCK (sinkpad);
1870 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1871 goto sink_was_linked;
1873 /* check hierarchy, pads can only be linked if the grandparents
1875 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1876 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1877 goto wrong_hierarchy;
1879 /* check pad caps for non-empty intersection */
1880 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1883 /* FIXME check pad scheduling for non-empty intersection */
1885 return GST_PAD_LINK_OK;
1889 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1890 GST_DEBUG_PAD_NAME (srcpad),
1891 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1892 /* we do not emit a warning in this case because unlinking cannot
1893 * be made MT safe.*/
1894 GST_OBJECT_UNLOCK (srcpad);
1895 return GST_PAD_LINK_WAS_LINKED;
1899 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1900 GST_DEBUG_PAD_NAME (sinkpad),
1901 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1902 /* we do not emit a warning in this case because unlinking cannot
1903 * be made MT safe.*/
1904 GST_OBJECT_UNLOCK (sinkpad);
1905 GST_OBJECT_UNLOCK (srcpad);
1906 return GST_PAD_LINK_WAS_LINKED;
1910 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1911 GST_OBJECT_UNLOCK (sinkpad);
1912 GST_OBJECT_UNLOCK (srcpad);
1913 return GST_PAD_LINK_WRONG_HIERARCHY;
1917 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1918 GST_OBJECT_UNLOCK (sinkpad);
1919 GST_OBJECT_UNLOCK (srcpad);
1920 return GST_PAD_LINK_NOFORMAT;
1926 * @srcpad: the source #GstPad.
1927 * @sinkpad: the sink #GstPad.
1929 * Checks if the source pad and the sink pad are compatible so they can be
1932 * Returns: TRUE if the pads can be linked.
1935 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1937 GstPadLinkReturn result;
1939 /* generic checks */
1940 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1941 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1943 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1944 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1946 /* gst_pad_link_prepare does everything for us, we only release the locks
1947 * on the pads that it gets us. If this function returns !OK the locks are not
1949 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1950 if (result != GST_PAD_LINK_OK)
1953 GST_OBJECT_UNLOCK (srcpad);
1954 GST_OBJECT_UNLOCK (sinkpad);
1957 return result == GST_PAD_LINK_OK;
1961 * gst_pad_link_full:
1962 * @srcpad: the source #GstPad to link.
1963 * @sinkpad: the sink #GstPad to link.
1964 * @flags: the checks to validate when linking
1966 * Links the source pad and the sink pad.
1968 * This variant of #gst_pad_link provides a more granular control on the
1969 * checks being done when linking. While providing some considerable speedups
1970 * the caller of this method must be aware that wrong usage of those flags
1971 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
1972 * for more information.
1976 * Returns: A result code indicating if the connection worked or
1982 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1984 GstPadLinkReturn result;
1986 GstPadLinkFunction srcfunc, sinkfunc;
1988 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
1989 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
1990 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
1991 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
1992 GST_PAD_LINK_WRONG_DIRECTION);
1994 /* Notify the parent early. See gst_pad_unlink for details. */
1995 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
1996 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
1997 gst_element_post_message (parent,
1998 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1999 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2001 gst_object_unref (parent);
2006 /* prepare will also lock the two pads */
2007 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2009 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2012 /* must set peers before calling the link function */
2013 GST_PAD_PEER (srcpad) = sinkpad;
2014 GST_PAD_PEER (sinkpad) = srcpad;
2016 /* check events, when something is different, mark pending */
2017 schedule_events (srcpad, sinkpad);
2019 /* get the link functions */
2020 srcfunc = GST_PAD_LINKFUNC (srcpad);
2021 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2023 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2024 /* custom link functions, execute them */
2025 GST_OBJECT_UNLOCK (sinkpad);
2026 GST_OBJECT_UNLOCK (srcpad);
2029 /* this one will call the peer link function */
2030 result = srcfunc (srcpad, sinkpad);
2031 } else if (sinkfunc) {
2032 /* if no source link function, we need to call the sink link
2033 * function ourselves. */
2034 result = sinkfunc (sinkpad, srcpad);
2037 GST_OBJECT_LOCK (srcpad);
2038 GST_OBJECT_LOCK (sinkpad);
2040 /* we released the lock, check if the same pads are linked still */
2041 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2042 goto concurrent_link;
2044 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2047 GST_OBJECT_UNLOCK (sinkpad);
2048 GST_OBJECT_UNLOCK (srcpad);
2050 /* fire off a signal to each of the pads telling them
2051 * that they've been linked */
2052 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2053 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2055 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2056 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2058 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2061 if (G_LIKELY (parent)) {
2062 gst_element_post_message (parent,
2063 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2064 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2065 gst_object_unref (parent);
2073 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2074 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2075 GST_OBJECT_UNLOCK (sinkpad);
2076 GST_OBJECT_UNLOCK (srcpad);
2078 /* The other link operation succeeded first */
2079 result = GST_PAD_LINK_WAS_LINKED;
2084 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2085 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2087 GST_PAD_PEER (srcpad) = NULL;
2088 GST_PAD_PEER (sinkpad) = NULL;
2090 GST_OBJECT_UNLOCK (sinkpad);
2091 GST_OBJECT_UNLOCK (srcpad);
2099 * @srcpad: the source #GstPad to link.
2100 * @sinkpad: the sink #GstPad to link.
2102 * Links the source pad and the sink pad.
2104 * Returns: A result code indicating if the connection worked or
2110 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2112 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2116 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2118 GstPadTemplate **template_p;
2120 /* this function would need checks if it weren't static */
2122 GST_OBJECT_LOCK (pad);
2123 template_p = &pad->padtemplate;
2124 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2125 GST_OBJECT_UNLOCK (pad);
2128 gst_pad_template_pad_created (templ, pad);
2132 * gst_pad_get_pad_template:
2135 * Gets the template for @pad.
2137 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2138 * instantiated, or %NULL if this pad has no template. Unref after
2142 gst_pad_get_pad_template (GstPad * pad)
2144 GstPadTemplate *templ;
2146 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2148 templ = GST_PAD_PAD_TEMPLATE (pad);
2150 return (templ ? gst_object_ref (templ) : NULL);
2154 * gst_pad_has_current_caps:
2155 * @pad: a #GstPad to check
2157 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2159 * Returns: TRUE when @pad has caps associated with it.
2162 gst_pad_has_current_caps (GstPad * pad)
2167 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2169 GST_OBJECT_LOCK (pad);
2170 caps = get_pad_caps (pad);
2171 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2172 "check current pad caps %" GST_PTR_FORMAT, caps);
2173 result = (caps != NULL);
2174 GST_OBJECT_UNLOCK (pad);
2180 * gst_pad_get_current_caps:
2181 * @pad: a #GstPad to get the current capabilities of.
2183 * Gets the capabilities currently configured on @pad with the last
2184 * #GST_EVENT_CAPS event.
2186 * Returns: the current caps of the pad with incremented ref-count.
2189 gst_pad_get_current_caps (GstPad * pad)
2193 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2195 GST_OBJECT_LOCK (pad);
2196 if ((result = get_pad_caps (pad)))
2197 gst_caps_ref (result);
2198 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2199 "get current pad caps %" GST_PTR_FORMAT, result);
2200 GST_OBJECT_UNLOCK (pad);
2207 * @pad: a #GstPad to set the capabilities of.
2208 * @caps: (transfer none): a #GstCaps to set.
2210 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2211 * caps on the pad will be unreffed. This function refs the caps so you should
2212 * unref if as soon as you don't need it anymore.
2213 * It is possible to set NULL caps, which will make the pad unnegotiated
2216 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2217 * or bad parameters were provided to this function.
2222 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2225 gboolean res = TRUE;
2227 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2228 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2230 event = gst_event_new_caps (caps);
2232 if (GST_PAD_IS_SRC (pad))
2233 res = gst_pad_push_event (pad, event);
2235 res = gst_pad_send_event (pad, event);
2241 * gst_pad_get_pad_template_caps:
2242 * @pad: a #GstPad to get the template capabilities from.
2244 * Gets the capabilities for @pad's template.
2246 * Returns: (transfer full): the #GstCaps of this pad template.
2247 * Unref after usage.
2250 gst_pad_get_pad_template_caps (GstPad * pad)
2252 static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2254 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2256 if (GST_PAD_PAD_TEMPLATE (pad))
2257 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2259 return gst_static_caps_get (&anycaps);
2264 * @pad: a #GstPad to get the peer of.
2266 * Gets the peer of @pad. This function refs the peer pad so
2267 * you need to unref it after use.
2269 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2274 gst_pad_get_peer (GstPad * pad)
2278 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2280 GST_OBJECT_LOCK (pad);
2281 result = GST_PAD_PEER (pad);
2283 gst_object_ref (result);
2284 GST_OBJECT_UNLOCK (pad);
2290 * gst_pad_get_allowed_caps:
2293 * Gets the capabilities of the allowed media types that can flow through
2294 * @pad and its peer.
2296 * The allowed capabilities is calculated as the intersection of the results of
2297 * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2298 * on the resulting caps.
2300 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2301 * caps when you no longer need it. This function returns NULL when @pad
2307 gst_pad_get_allowed_caps (GstPad * pad)
2314 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2316 GST_OBJECT_LOCK (pad);
2317 peer = GST_PAD_PEER (pad);
2318 if (G_UNLIKELY (peer == NULL))
2321 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2323 gst_object_ref (peer);
2324 GST_OBJECT_UNLOCK (pad);
2325 mycaps = gst_pad_query_caps (pad, NULL);
2327 peercaps = gst_pad_query_caps (peer, NULL);
2328 gst_object_unref (peer);
2330 caps = gst_caps_intersect (mycaps, peercaps);
2331 gst_caps_unref (peercaps);
2332 gst_caps_unref (mycaps);
2334 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2341 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2342 GST_OBJECT_UNLOCK (pad);
2349 * gst_pad_iterate_internal_links_default:
2350 * @pad: the #GstPad to get the internal links of.
2351 * @parent: the parent of @pad or NULL
2353 * Iterate the list of pads to which the given pad is linked to inside of
2354 * the parent element.
2355 * This is the default handler, and thus returns an iterator of all of the
2356 * pads inside the parent element with opposite direction.
2358 * The caller must free this iterator after use with gst_iterator_free().
2360 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2361 * returned pad with gst_object_unref().
2366 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2373 GstElement *eparent;
2375 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2377 if (parent != NULL && GST_IS_ELEMENT (parent)) {
2378 eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2380 GST_OBJECT_LOCK (pad);
2381 eparent = GST_PAD_PARENT (pad);
2382 if (!eparent || !GST_IS_ELEMENT (eparent))
2385 gst_object_ref (eparent);
2386 GST_OBJECT_UNLOCK (pad);
2389 if (pad->direction == GST_PAD_SRC)
2390 padlist = &eparent->sinkpads;
2392 padlist = &eparent->srcpads;
2394 GST_DEBUG_OBJECT (pad, "Making iterator");
2396 cookie = &eparent->pads_cookie;
2398 lock = GST_OBJECT_GET_LOCK (eparent);
2400 res = gst_iterator_new_list (GST_TYPE_PAD,
2401 lock, cookie, padlist, (GObject *) owner, NULL);
2403 gst_object_unref (owner);
2410 GST_OBJECT_UNLOCK (pad);
2411 GST_DEBUG_OBJECT (pad, "no parent element");
2417 * gst_pad_iterate_internal_links:
2418 * @pad: the GstPad to get the internal links of.
2420 * Gets an iterator for the pads to which the given pad is linked to inside
2421 * of the parent element.
2423 * Each #GstPad element yielded by the iterator will have its refcount increased,
2424 * so unref after use.
2426 * Free-function: gst_iterator_free
2428 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
2429 * pad does not have an iterator function configured. Use
2430 * gst_iterator_free() after usage.
2435 gst_pad_iterate_internal_links (GstPad * pad)
2437 GstIterator *res = NULL;
2440 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2442 GST_OBJECT_LOCK (pad);
2443 ACQUIRE_PARENT (pad, parent, no_parent);
2444 GST_OBJECT_UNLOCK (pad);
2446 if (GST_PAD_ITERINTLINKFUNC (pad))
2447 res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2449 RELEASE_PARENT (parent);
2456 GST_DEBUG_OBJECT (pad, "no parent");
2457 GST_OBJECT_UNLOCK (pad);
2465 * @forward: a #GstPadForwardFunction
2466 * @user_data: user data passed to @forward
2468 * Calls @forward for all internally linked pads of @pad. This function deals with
2469 * dynamically changing internal pads and will make sure that the @forward
2470 * function is only called once for each pad.
2472 * When @forward returns TRUE, no further pads will be processed.
2474 * Returns: TRUE if one of the dispatcher functions returned TRUE.
2477 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2480 gboolean result = FALSE;
2482 gboolean done = FALSE;
2483 GValue item = { 0, };
2484 GList *pushed_pads = NULL;
2486 iter = gst_pad_iterate_internal_links (pad);
2492 switch (gst_iterator_next (iter, &item)) {
2493 case GST_ITERATOR_OK:
2497 intpad = g_value_get_object (&item);
2499 /* if already pushed, skip. FIXME, find something faster to tag pads */
2500 if (g_list_find (pushed_pads, intpad)) {
2501 g_value_reset (&item);
2505 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2506 GST_DEBUG_PAD_NAME (intpad));
2507 done = result = forward (intpad, user_data);
2509 pushed_pads = g_list_prepend (pushed_pads, intpad);
2511 g_value_reset (&item);
2514 case GST_ITERATOR_RESYNC:
2515 /* We don't reset the result here because we don't push the event
2516 * again on pads that got the event already and because we need
2517 * to consider the result of the previous pushes */
2518 gst_iterator_resync (iter);
2520 case GST_ITERATOR_ERROR:
2521 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2524 case GST_ITERATOR_DONE:
2529 g_value_unset (&item);
2530 gst_iterator_free (iter);
2532 g_list_free (pushed_pads);
2542 gboolean dispatched;
2546 event_forward_func (GstPad * pad, EventData * data)
2548 /* for each pad we send to, we should ref the event; it's up
2549 * to downstream to unref again when handled. */
2550 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2551 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2553 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2555 data->dispatched = TRUE;
2562 * gst_pad_event_default:
2563 * @pad: a #GstPad to call the default event handler on.
2564 * @parent: the parent of @pad or NULL
2565 * @event: (transfer full): the #GstEvent to handle.
2567 * Invokes the default event handler for the given pad.
2569 * The EOS event will pause the task associated with @pad before it is forwarded
2570 * to all internally linked pads,
2572 * The the event is sent to all pads internally linked to @pad. This function
2573 * takes ownership of @event.
2575 * Returns: TRUE if the event was sent successfully.
2578 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2580 gboolean result, forward = TRUE;
2582 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2583 g_return_val_if_fail (event != NULL, FALSE);
2585 GST_LOG_OBJECT (pad, "default event handler");
2587 switch (GST_EVENT_TYPE (event)) {
2590 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
2591 gst_pad_pause_task (pad);
2594 case GST_EVENT_CAPS:
2595 forward = GST_PAD_IS_PROXY_CAPS (pad);
2606 data.dispatched = FALSE;
2607 data.result = FALSE;
2609 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
2611 /* for sinkpads without a parent element or without internal links, nothing
2612 * will be dispatched but we still want to return TRUE. */
2613 if (data.dispatched)
2614 result = data.result;
2619 gst_event_unref (event);
2624 /* Default accept caps implementation just checks against
2625 * the allowed caps for the pad */
2627 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
2629 /* get the caps and see if it intersects to something not empty */
2630 GstCaps *caps, *allowed;
2633 GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
2635 /* first forward the query to internally linked pads when we are dealing with
2637 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2638 if ((result = gst_pad_proxy_query_accept_caps (pad, query))) {
2643 allowed = gst_pad_query_caps (pad, NULL);
2644 gst_query_parse_accept_caps (query, &caps);
2647 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2648 result = gst_caps_is_subset (caps, allowed);
2649 gst_caps_unref (allowed);
2651 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2654 gst_query_set_accept_caps_result (query, result);
2660 /* Default caps implementation */
2662 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
2664 GstCaps *result = NULL, *filter;
2665 GstPadTemplate *templ;
2666 gboolean fixed_caps;
2668 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2670 gst_query_parse_caps (query, &filter);
2672 /* first try to proxy if we must */
2673 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2674 if ((gst_pad_proxy_query_caps (pad, query))) {
2675 gst_query_parse_caps_result (query, &result);
2680 /* no proxy or it failed, do default handling */
2681 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2683 GST_OBJECT_LOCK (pad);
2685 /* fixed caps, try the negotiated caps first */
2686 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
2687 if ((result = get_pad_caps (pad)))
2688 goto filter_done_unlock;
2691 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2692 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
2693 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
2694 goto filter_done_unlock;
2698 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2699 "non-fixed pad caps: trying pad caps");
2700 /* non fixed caps, try the negotiated caps */
2701 if ((result = get_pad_caps (pad)))
2702 goto filter_done_unlock;
2704 GST_OBJECT_UNLOCK (pad);
2706 /* this almost never happens */
2707 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2708 result = gst_caps_new_empty ();
2713 GST_OBJECT_UNLOCK (pad);
2716 /* run the filter on the result */
2718 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2719 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
2720 GST_PTR_FORMAT, result, result, filter, filter);
2721 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2722 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2725 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2726 "using caps %p %" GST_PTR_FORMAT, result, result);
2727 result = gst_caps_ref (result);
2731 gst_query_set_caps_result (query, result);
2732 gst_caps_unref (result);
2738 * gst_pad_query_default:
2739 * @pad: a #GstPad to call the default query handler on.
2740 * @parent: the parent of @pad or NULL
2741 * @query: (transfer none): the #GstQuery to handle.
2743 * Invokes the default query handler for the given pad.
2744 * The query is sent to all pads internally linked to @pad. Note that
2745 * if there are many possible sink pads that are internally linked to
2746 * @pad, only one will be sent the query.
2747 * Multi-sinkpad elements should implement custom query handlers.
2749 * Returns: TRUE if the query was performed successfully.
2752 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
2754 gboolean forward = TRUE, ret = FALSE;
2756 switch (GST_QUERY_TYPE (query)) {
2757 case GST_QUERY_SCHEDULING:
2760 case GST_QUERY_ACCEPT_CAPS:
2761 ret = gst_pad_query_accept_caps_default (pad, query);
2764 case GST_QUERY_CAPS:
2765 ret = gst_pad_query_caps_default (pad, query);
2768 case GST_QUERY_POSITION:
2769 case GST_QUERY_SEEKING:
2770 case GST_QUERY_FORMATS:
2771 case GST_QUERY_LATENCY:
2772 case GST_QUERY_JITTER:
2773 case GST_QUERY_RATE:
2774 case GST_QUERY_CONVERT:
2775 case GST_QUERY_ALLOCATION:
2781 ret = gst_pad_forward
2782 (pad, (GstPadForwardFunction) gst_pad_peer_query, query);
2788 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
2790 GstPad *pad = data->pad;
2791 GstPadProbeInfo *info = data->info;
2792 GstPadProbeType type, flags;
2793 GstPadProbeCallback callback;
2794 GstPadProbeReturn ret;
2796 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2797 "hook %lu, cookie %u checking", hook->hook_id, PROBE_COOKIE (hook));
2799 /* if we have called this callback, do nothing */
2800 if (PROBE_COOKIE (hook) == data->cookie) {
2801 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2802 "hook %lu, cookie %u already called", hook->hook_id,
2803 PROBE_COOKIE (hook));
2807 PROBE_COOKIE (hook) = data->cookie;
2809 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
2812 /* one of the data types */
2813 if ((flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
2815 /* one of the scheduling types */
2816 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
2818 /* one of the blocking types must match */
2819 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
2820 (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
2823 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2824 "hook %lu with flags 0x%08x matches", hook->hook_id, flags);
2826 callback = (GstPadProbeCallback) hook->func;
2827 if (callback == NULL)
2830 GST_OBJECT_UNLOCK (pad);
2832 ret = callback (pad, info, hook->data);
2834 GST_OBJECT_LOCK (pad);
2835 data->marshalled = TRUE;
2838 case GST_PAD_PROBE_REMOVE:
2839 /* remove the probe */
2840 GST_DEBUG_OBJECT (pad, "asked to remove hook");
2841 cleanup_hook (pad, hook);
2843 case GST_PAD_PROBE_DROP:
2844 /* need to drop the data, make sure other probes don't get called
2846 GST_DEBUG_OBJECT (pad, "asked to drop item");
2847 info->type = GST_PAD_PROBE_TYPE_INVALID;
2848 data->dropped = TRUE;
2850 case GST_PAD_PROBE_PASS:
2851 /* inform the pad block to let things pass */
2852 GST_DEBUG_OBJECT (pad, "asked to pass item");
2856 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
2863 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2864 "hook %lu with flags 0x%08x does not match %08x", hook->hook_id,
2870 #define PROBE_PRE_PULL(pad,mask,data,offs,size,label,probed,defaultval) \
2872 if (G_UNLIKELY (pad->num_probes)) { \
2873 /* we start with passing NULL as the data item */ \
2874 GstPadProbeInfo info = { mask, NULL, offs, size }; \
2875 ret = do_probe_callbacks (pad, &info, defaultval); \
2876 /* store the possibly updated data item */ \
2877 data = GST_PAD_PROBE_INFO_DATA (&info); \
2878 /* if something went wrong, exit */ \
2879 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2881 /* otherwise check if the probe retured data */ \
2882 if (G_UNLIKELY (data != NULL)) \
2888 /* a probe that does not take or return any data */
2889 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
2891 if (G_UNLIKELY (pad->num_probes)) { \
2892 /* pass NULL as the data item */ \
2893 GstPadProbeInfo info = { mask, NULL, 0, 0 }; \
2894 ret = do_probe_callbacks (pad, &info, defaultval); \
2895 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2900 #define PROBE_FULL(pad,mask,data,offs,size,label,defaultval) \
2902 if (G_UNLIKELY (pad->num_probes)) { \
2903 GstPadProbeInfo info = { mask, data, offs, size }; \
2904 ret = do_probe_callbacks (pad, &info, defaultval); \
2905 data = GST_PAD_PROBE_INFO_DATA (&info); \
2906 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2911 #define PROBE_PUSH(pad,mask,data,label) \
2912 PROBE_FULL(pad, mask, data, -1, -1, label, GST_FLOW_OK);
2913 #define PROBE_PULL(pad,mask,data,offs,size,label) \
2914 PROBE_FULL(pad, mask, data, offs, size, label, GST_FLOW_OK);
2916 static GstFlowReturn
2917 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
2918 GstFlowReturn defaultval)
2927 data.marshalled = FALSE;
2928 data.dropped = FALSE;
2929 data.cookie = ++pad->priv->probe_cookie;
2932 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
2935 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2936 "do probes cookie %u", data.cookie);
2937 cookie = pad->priv->probe_list_cookie;
2939 g_hook_list_marshal (&pad->probes, TRUE,
2940 (GHookMarshaller) probe_hook_marshal, &data);
2942 /* if the list changed, call the new callbacks (they will not have their
2943 * cookie set to data.cookie */
2944 if (cookie != pad->priv->probe_list_cookie) {
2945 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2946 "probe list changed, restarting");
2950 /* the first item that dropped will stop the hooks and then we drop here */
2954 /* if no handler matched and we are blocking, let the item pass */
2955 if (!data.marshalled && is_block)
2958 /* At this point, all handlers returned either OK or PASS. If one handler
2959 * returned PASS, let the item pass */
2964 while (GST_PAD_IS_BLOCKED (pad)) {
2965 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2966 "we are blocked %d times", pad->num_blocked);
2968 /* we might have released the lock */
2969 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2972 /* now we block the streaming thread. It can be unlocked when we
2973 * deactivate the pad (which will also set the FLUSHING flag) or
2974 * when the pad is unblocked. A flushing event will also unblock
2975 * the pad after setting the FLUSHING flag. */
2976 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2977 "Waiting to be unblocked or set flushing");
2978 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
2979 GST_PAD_BLOCK_WAIT (pad);
2980 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
2981 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
2983 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2993 GST_DEBUG_OBJECT (pad, "pad is flushing");
2994 return GST_FLOW_WRONG_STATE;
2998 GST_DEBUG_OBJECT (pad, "data is dropped");
2999 return GST_FLOW_CUSTOM_SUCCESS;
3003 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3004 GST_DEBUG_OBJECT (pad, "data is passed");
3012 * gst_pad_get_offset:
3015 * Get the offset applied to the running time of @pad. @pad has to be a source
3018 * Returns: the offset.
3021 gst_pad_get_offset (GstPad * pad)
3025 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3027 GST_OBJECT_LOCK (pad);
3028 result = pad->offset;
3029 GST_OBJECT_UNLOCK (pad);
3035 * gst_pad_set_offset:
3037 * @offset: the offset
3039 * Set the offset that will be applied to the running time of @pad.
3042 gst_pad_set_offset (GstPad * pad, gint64 offset)
3046 g_return_if_fail (GST_IS_PAD (pad));
3048 GST_OBJECT_LOCK (pad);
3049 /* if nothing changed, do nothing */
3050 if (pad->offset == offset)
3053 pad->offset = offset;
3054 GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3056 /* sinkpads will apply their offset the next time a segment
3057 * event is received. */
3058 if (GST_PAD_IS_SINK (pad))
3061 /* resend the last segment event on next buffer push */
3062 if ((ev = find_event_by_type (pad, GST_EVENT_SEGMENT, 0))) {
3063 ev->received = FALSE;
3064 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3068 GST_OBJECT_UNLOCK (pad);
3074 * @pad: a #GstPad to invoke the default query on.
3075 * @query: (transfer none): the #GstQuery to perform.
3077 * Dispatches a query to a pad. The query should have been allocated by the
3078 * caller via one of the type-specific allocation functions. The element that
3079 * the pad belongs to is responsible for filling the query with an appropriate
3080 * response, which should then be parsed with a type-specific query parsing
3083 * Again, the caller is responsible for both the allocation and deallocation of
3084 * the query structure.
3086 * Please also note that some queries might need a running pipeline to work.
3088 * Returns: TRUE if the query could be performed.
3091 gst_pad_query (GstPad * pad, GstQuery * query)
3095 GstPadQueryFunction func;
3096 GstPadProbeType type;
3099 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3100 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3102 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3103 GST_QUERY_TYPE_NAME (query));
3105 if (GST_PAD_IS_SRC (pad))
3106 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3108 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3110 GST_OBJECT_LOCK (pad);
3111 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3112 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3113 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3115 ACQUIRE_PARENT (pad, parent, no_parent);
3116 GST_OBJECT_UNLOCK (pad);
3118 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3121 res = func (pad, parent, query);
3123 RELEASE_PARENT (parent);
3125 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3126 GST_QUERY_TYPE_NAME (query), res);
3131 GST_OBJECT_LOCK (pad);
3132 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3133 GST_OBJECT_UNLOCK (pad);
3140 GST_DEBUG_OBJECT (pad, "had no parent");
3141 GST_OBJECT_UNLOCK (pad);
3146 GST_DEBUG_OBJECT (pad, "had no query function");
3147 RELEASE_PARENT (parent);
3152 GST_DEBUG_OBJECT (pad, "query failed");
3157 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3158 GST_OBJECT_UNLOCK (pad);
3164 * gst_pad_peer_query:
3165 * @pad: a #GstPad to invoke the peer query on.
3166 * @query: (transfer none): the #GstQuery to perform.
3168 * Performs gst_pad_query() on the peer of @pad.
3170 * The caller is responsible for both the allocation and deallocation of
3171 * the query structure.
3173 * Returns: TRUE if the query could be performed. This function returns %FALSE
3174 * if @pad has no peer.
3179 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3182 GstPadProbeType type;
3186 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3187 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3189 if (GST_PAD_IS_SRC (pad))
3190 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3192 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3194 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3195 GST_QUERY_TYPE_NAME (query));
3197 GST_OBJECT_LOCK (pad);
3198 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3199 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3200 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3202 peerpad = GST_PAD_PEER (pad);
3203 if (G_UNLIKELY (peerpad == NULL))
3206 gst_object_ref (peerpad);
3207 GST_OBJECT_UNLOCK (pad);
3209 res = gst_pad_query (peerpad, query);
3211 gst_object_unref (peerpad);
3216 GST_OBJECT_LOCK (pad);
3217 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3218 GST_OBJECT_UNLOCK (pad);
3225 GST_WARNING_OBJECT (pad, "pad has no peer");
3226 GST_OBJECT_UNLOCK (pad);
3231 GST_DEBUG_OBJECT (pad, "query failed");
3236 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3237 GST_OBJECT_UNLOCK (pad);
3242 /**********************************************************************
3243 * Data passing functions
3247 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3249 GstFlowReturn *data = user_data;
3253 GST_DEBUG_OBJECT (pad, "event %s was already received",
3254 GST_EVENT_TYPE_NAME (ev->event));
3257 GST_OBJECT_UNLOCK (pad);
3259 *data = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3260 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, &stored);
3262 GST_OBJECT_LOCK (pad);
3263 return *data != GST_FLOW_OK;
3266 /* this is the chain function that does not perform the additional argument
3267 * checking for that little extra speed.
3269 static inline GstFlowReturn
3270 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3275 GST_PAD_STREAM_LOCK (pad);
3277 GST_OBJECT_LOCK (pad);
3278 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3281 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3283 PROBE_PUSH (pad, type, data, probe_stopped);
3285 parent = GST_OBJECT_PARENT (pad);
3286 GST_OBJECT_UNLOCK (pad);
3288 /* NOTE: we read the chainfunc unlocked.
3289 * we cannot hold the lock for the pad so we might send
3290 * the data to the wrong function. This is not really a
3291 * problem since functions are assigned at creation time
3292 * and don't change that often... */
3293 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3294 GstPadChainFunction chainfunc;
3296 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3299 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3300 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3301 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3303 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
3305 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3306 "called chainfunction &%s with buffer %p, returned %s",
3307 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3309 GstPadChainListFunction chainlistfunc;
3311 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3314 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3315 "calling chainlistfunction &%s",
3316 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3318 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
3320 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3321 "called chainlistfunction &%s, returned %s",
3322 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3325 GST_PAD_STREAM_UNLOCK (pad);
3332 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3333 "chaining, but pad was flushing");
3334 GST_OBJECT_UNLOCK (pad);
3335 GST_PAD_STREAM_UNLOCK (pad);
3336 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3337 return GST_FLOW_WRONG_STATE;
3341 GST_OBJECT_UNLOCK (pad);
3342 GST_PAD_STREAM_UNLOCK (pad);
3343 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3346 case GST_FLOW_CUSTOM_SUCCESS:
3347 GST_DEBUG_OBJECT (pad, "dropped buffer");
3351 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3358 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3359 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3360 "pushing, but not chainhandler");
3361 GST_ELEMENT_ERROR (parent, CORE, PAD, (NULL),
3362 ("push on pad %s:%s but it has no chainfunction",
3363 GST_DEBUG_PAD_NAME (pad)));
3364 GST_PAD_STREAM_UNLOCK (pad);
3365 return GST_FLOW_NOT_SUPPORTED;
3371 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3372 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3375 * Chain a buffer to @pad.
3377 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3379 * If the buffer type is not acceptable for @pad (as negotiated with a
3380 * preceeding GST_EVENT_CAPS event), this function returns
3381 * #GST_FLOW_NOT_NEGOTIATED.
3383 * The function proceeds calling the chain function installed on @pad (see
3384 * gst_pad_set_chain_function()) and the return value of that function is
3385 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3388 * In all cases, success or failure, the caller loses its reference to @buffer
3389 * after calling this function.
3391 * Returns: a #GstFlowReturn from the pad.
3396 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3398 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3399 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3400 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3402 return gst_pad_chain_data_unchecked (pad,
3403 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3406 static GstFlowReturn
3407 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
3408 GstBufferList * list)
3414 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3416 len = gst_buffer_list_length (list);
3419 for (i = 0; i < len; i++) {
3420 buffer = gst_buffer_list_get (list, i);
3422 gst_pad_chain_data_unchecked (pad,
3423 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3424 gst_buffer_ref (buffer));
3425 if (ret != GST_FLOW_OK)
3428 gst_buffer_list_unref (list);
3434 * gst_pad_chain_list:
3435 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3436 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3439 * Chain a bufferlist to @pad.
3441 * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
3443 * If @pad was not negotiated properly with a CAPS event, this function
3444 * returns #GST_FLOW_NOT_NEGOTIATED.
3446 * The function proceeds calling the chainlist function installed on @pad (see
3447 * gst_pad_set_chain_list_function()) and the return value of that function is
3448 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3449 * chainlist function.
3451 * In all cases, success or failure, the caller loses its reference to @list
3452 * after calling this function.
3456 * Returns: a #GstFlowReturn from the pad.
3461 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3463 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3464 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3465 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3467 return gst_pad_chain_data_unchecked (pad,
3468 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3471 static GstFlowReturn
3472 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3477 GST_OBJECT_LOCK (pad);
3478 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3481 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3482 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3484 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3487 events_foreach (pad, push_sticky, &ret);
3488 if (ret != GST_FLOW_OK)
3492 /* do block probes */
3493 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3495 /* do post-blocking probes */
3496 PROBE_PUSH (pad, type, data, probe_stopped);
3498 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3501 /* take ref to peer pad before releasing the lock */
3502 gst_object_ref (peer);
3504 GST_OBJECT_UNLOCK (pad);
3506 ret = gst_pad_chain_data_unchecked (peer, type, data);
3508 gst_object_unref (peer);
3510 GST_OBJECT_LOCK (pad);
3512 if (pad->priv->using == 0) {
3513 /* pad is not active anymore, trigger idle callbacks */
3514 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3515 probe_stopped, ret);
3517 GST_OBJECT_UNLOCK (pad);
3521 /* ERROR recovery here */
3525 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3526 "pushing, but pad was flushing");
3527 GST_OBJECT_UNLOCK (pad);
3528 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3529 return GST_FLOW_WRONG_STATE;
3533 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3534 "error pushing events, return %s", gst_flow_get_name (ret));
3535 GST_OBJECT_UNLOCK (pad);
3536 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3541 GST_OBJECT_UNLOCK (pad);
3542 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3545 case GST_FLOW_CUSTOM_SUCCESS:
3546 GST_DEBUG_OBJECT (pad, "dropped buffer");
3550 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
3557 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3558 "pushing, but it was not linked");
3559 GST_OBJECT_UNLOCK (pad);
3560 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3561 return GST_FLOW_NOT_LINKED;
3567 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3568 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3571 * Pushes a buffer to the peer of @pad.
3573 * This function will call installed block probes before triggering any
3574 * installed data probes.
3576 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3577 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3580 * In all cases, success or failure, the caller loses its reference to @buffer
3581 * after calling this function.
3583 * Returns: a #GstFlowReturn from the peer pad.
3588 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3590 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3591 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3592 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3594 return gst_pad_push_data (pad,
3595 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3599 * gst_pad_push_list:
3600 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3601 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3604 * Pushes a buffer list to the peer of @pad.
3606 * This function will call installed block probes before triggering any
3607 * installed data probes.
3609 * The function proceeds calling the chain function on the peer pad and returns
3610 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3611 * be returned. If the peer pad does not have any installed chainlist function
3612 * every group buffer of the list will be merged into a normal #GstBuffer and
3613 * chained via gst_pad_chain().
3615 * In all cases, success or failure, the caller loses its reference to @list
3616 * after calling this function.
3618 * Returns: a #GstFlowReturn from the peer pad.
3625 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3627 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3628 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3629 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3631 return gst_pad_push_data (pad,
3632 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3635 static GstFlowReturn
3636 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3637 GstBuffer ** buffer)
3640 GstPadGetRangeFunction getrangefunc;
3643 GST_PAD_STREAM_LOCK (pad);
3645 GST_OBJECT_LOCK (pad);
3646 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3649 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3650 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3652 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3655 events_foreach (pad, push_sticky, &ret);
3656 if (ret != GST_FLOW_OK)
3660 /* when one of the probes returns a buffer, probed_data will be called and we
3661 * skip calling the getrange function */
3662 PROBE_PRE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3663 *buffer, offset, size, probe_stopped, probed_data, GST_FLOW_OK);
3665 ACQUIRE_PARENT (pad, parent, no_parent);
3666 GST_OBJECT_UNLOCK (pad);
3668 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3671 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3672 "calling getrangefunc %s, offset %"
3673 G_GUINT64_FORMAT ", size %u",
3674 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3676 ret = getrangefunc (pad, parent, offset, size, buffer);
3678 RELEASE_PARENT (parent);
3680 if (G_UNLIKELY (ret != GST_FLOW_OK))
3681 goto get_range_failed;
3683 /* can only fire the signal if we have a valid buffer */
3684 GST_OBJECT_LOCK (pad);
3686 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3687 *buffer, offset, size, probe_stopped_unref);
3688 GST_OBJECT_UNLOCK (pad);
3690 GST_PAD_STREAM_UNLOCK (pad);
3697 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3698 "getrange, but pad was flushing");
3699 GST_OBJECT_UNLOCK (pad);
3700 GST_PAD_STREAM_UNLOCK (pad);
3701 return GST_FLOW_WRONG_STATE;
3705 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
3706 GST_OBJECT_UNLOCK (pad);
3707 GST_PAD_STREAM_UNLOCK (pad);
3712 GST_DEBUG_OBJECT (pad, "no parent");
3713 GST_OBJECT_UNLOCK (pad);
3714 GST_PAD_STREAM_UNLOCK (pad);
3715 return GST_FLOW_WRONG_STATE;
3719 GST_ELEMENT_ERROR (parent, CORE, PAD, (NULL),
3720 ("getrange on pad %s:%s but it has no getrangefunction",
3721 GST_DEBUG_PAD_NAME (pad)));
3722 RELEASE_PARENT (parent);
3723 GST_PAD_STREAM_UNLOCK (pad);
3724 return GST_FLOW_NOT_SUPPORTED;
3728 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3729 "probe returned %s", gst_flow_get_name (ret));
3730 GST_OBJECT_UNLOCK (pad);
3731 GST_PAD_STREAM_UNLOCK (pad);
3734 probe_stopped_unref:
3736 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3737 "probe returned %s", gst_flow_get_name (ret));
3738 GST_OBJECT_UNLOCK (pad);
3739 GST_PAD_STREAM_UNLOCK (pad);
3740 gst_buffer_unref (*buffer);
3746 GST_PAD_STREAM_UNLOCK (pad);
3748 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3749 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3750 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
3756 * gst_pad_get_range:
3757 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
3758 * @offset: The start offset of the buffer
3759 * @size: The length of the buffer
3760 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
3761 * returns #GST_FLOW_ERROR if %NULL.
3763 * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
3764 * immediately and @buffer is %NULL.
3766 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
3767 * description of a getrange function. If @pad has no getrange function
3768 * installed (see gst_pad_set_getrange_function()) this function returns
3769 * #GST_FLOW_NOT_SUPPORTED.
3771 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
3773 * Returns: a #GstFlowReturn from the pad.
3778 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
3779 GstBuffer ** buffer)
3781 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3782 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3783 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3785 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
3789 * gst_pad_pull_range:
3790 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3791 * @offset: The start offset of the buffer
3792 * @size: The length of the buffer
3793 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
3794 * GST_FLOW_ERROR if %NULL.
3796 * Pulls a @buffer from the peer pad.
3798 * This function will first trigger the pad block signal if it was
3801 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
3802 * function returns the result of gst_pad_get_range() on the peer pad.
3803 * See gst_pad_get_range() for a list of return values and for the
3804 * semantics of the arguments of this function.
3806 * Returns: a #GstFlowReturn from the peer pad.
3807 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
3808 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
3809 * @buffer may not be used or freed when any other return value than
3810 * #GST_FLOW_OK is returned.
3815 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
3816 GstBuffer ** buffer)
3821 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3822 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3823 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3825 GST_OBJECT_LOCK (pad);
3826 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3829 /* when one of the probes returns a buffer, probed_data will be called and we
3830 * skip calling the peer getrange function */
3831 PROBE_PRE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3832 *buffer, offset, size, pre_probe_stopped, probed_data, GST_FLOW_OK);
3834 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3837 gst_object_ref (peer);
3839 GST_OBJECT_UNLOCK (pad);
3841 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
3843 gst_object_unref (peer);
3845 GST_OBJECT_LOCK (pad);
3847 if (pad->priv->using == 0) {
3848 /* pad is not active anymore, trigger idle callbacks */
3849 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
3850 post_probe_stopped, ret);
3853 if (G_UNLIKELY (ret != GST_FLOW_OK))
3854 goto pull_range_failed;
3857 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3858 *buffer, offset, size, post_probe_stopped);
3860 GST_OBJECT_UNLOCK (pad);
3864 /* ERROR recovery here */
3867 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3868 "pullrange, but pad was flushing");
3869 GST_OBJECT_UNLOCK (pad);
3870 return GST_FLOW_WRONG_STATE;
3874 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
3875 gst_flow_get_name (ret));
3876 GST_OBJECT_UNLOCK (pad);
3881 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3882 "pulling range, but it was not linked");
3883 GST_OBJECT_UNLOCK (pad);
3884 return GST_FLOW_NOT_LINKED;
3889 GST_OBJECT_UNLOCK (pad);
3890 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3891 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3892 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
3897 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3898 "post probe returned %s", gst_flow_get_name (ret));
3899 GST_OBJECT_UNLOCK (pad);
3900 if (ret == GST_FLOW_OK)
3901 gst_buffer_unref (*buffer);
3908 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event, gboolean locked)
3913 gboolean res = FALSE;
3914 const gchar *name = NULL;
3916 type = GST_EVENT_TYPE (event);
3917 if (type & GST_EVENT_TYPE_STICKY_MULTI)
3918 name = gst_structure_get_name (gst_event_get_structure (event));
3920 events = pad->priv->events;
3923 for (i = 0; i < len; i++) {
3924 PadEvent *ev = &g_array_index (events, PadEvent, i);
3926 if (ev->event == NULL)
3929 if (type == GST_EVENT_TYPE (ev->event)) {
3930 /* matching types, check matching name if needed */
3931 if (name && !gst_event_has_name (ev->event, name))
3935 if ((res = gst_event_replace (&ev->event, event)))
3936 ev->received = FALSE;
3942 ev.event = gst_event_ref (event);
3943 ev.received = FALSE;
3944 g_array_append_val (events, ev);
3949 pad->priv->events_cookie++;
3950 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3952 GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
3954 switch (GST_EVENT_TYPE (event)) {
3955 case GST_EVENT_CAPS:
3957 GST_OBJECT_UNLOCK (pad);
3959 GST_DEBUG_OBJECT (pad, "notify caps");
3960 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
3963 GST_OBJECT_LOCK (pad);
3972 static GstFlowReturn
3973 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
3974 GstPadProbeType type, gboolean * stored)
3978 GstEventType event_type;
3981 sticky = GST_EVENT_IS_STICKY (event);
3983 GST_OBJECT_LOCK (pad);
3985 /* Two checks to be made:
3986 * . (un)set the FLUSHING flag for flushing events,
3987 * . handle pad blocking */
3988 event_type = GST_EVENT_TYPE (event);
3990 switch (event_type) {
3991 case GST_EVENT_FLUSH_START:
3992 GST_PAD_SET_FLUSHING (pad);
3994 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
3995 /* flush start will have set the FLUSHING flag and will then
3996 * unlock all threads doing a GCond wait on the blocking pad. This
3997 * will typically unblock the STREAMING thread blocked on a pad. */
3998 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
3999 "doing block signal.");
4000 GST_PAD_BLOCK_BROADCAST (pad);
4004 case GST_EVENT_FLUSH_STOP:
4005 GST_PAD_UNSET_FLUSHING (pad);
4007 /* Remove sticky EOS events */
4008 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4009 remove_event_by_type (pad, GST_EVENT_EOS);
4011 if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4012 GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4018 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4021 /* store the event on the pad, but only on srcpads. We always store the
4022 * event exactly how it was sent */
4024 /* srcpad sticky events are store immediately, the received flag is set
4025 * to FALSE and will be set to TRUE when we can successfully push the
4026 * event to the peer pad */
4027 if (gst_pad_store_sticky_event (pad, event, TRUE)) {
4028 GST_DEBUG_OBJECT (pad, "event %s updated",
4029 GST_EVENT_TYPE_NAME (event));
4034 switch (GST_EVENT_TYPE (event)) {
4035 case GST_EVENT_SEGMENT:
4036 /* pass the adjusted segment event on. We need to do this even if
4037 * there is no peer pad because of the probes. */
4038 event = apply_pad_offset (pad, event);
4040 case GST_EVENT_RECONFIGURE:
4041 if (GST_PAD_IS_SINK (pad))
4042 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4047 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4048 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4053 /* send probes after modifying the events above */
4054 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4056 /* now check the peer pad */
4057 peerpad = GST_PAD_PEER (pad);
4058 if (peerpad == NULL)
4061 gst_object_ref (peerpad);
4063 GST_OBJECT_UNLOCK (pad);
4065 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4066 event, GST_EVENT_TYPE_NAME (event), peerpad);
4068 ret = gst_pad_send_event_unchecked (peerpad, event, type);
4070 /* Note: we gave away ownership of the event at this point but we can still
4071 * print the old pointer */
4072 GST_LOG_OBJECT (pad,
4073 "sent event %p to peerpad %" GST_PTR_FORMAT ", ret %s", event, peerpad,
4074 gst_flow_get_name (ret));
4076 gst_object_unref (peerpad);
4078 GST_OBJECT_LOCK (pad);
4080 if (ret == GST_FLOW_OK) {
4083 if ((ev = find_event (pad, event)))
4084 ev->received = TRUE;
4086 GST_DEBUG_OBJECT (pad, "event marked received");
4088 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4089 GST_DEBUG_OBJECT (pad, "mark pending events");
4093 if (pad->priv->using == 0) {
4094 /* pad is not active anymore, trigger idle callbacks */
4095 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4096 idle_probe_stopped, ret);
4098 GST_OBJECT_UNLOCK (pad);
4102 /* ERROR handling */
4105 GST_DEBUG_OBJECT (pad, "We're flushing");
4106 GST_OBJECT_UNLOCK (pad);
4107 gst_event_unref (event);
4108 return GST_FLOW_WRONG_STATE;
4112 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4113 GST_OBJECT_UNLOCK (pad);
4114 gst_event_unref (event);
4117 case GST_FLOW_CUSTOM_SUCCESS:
4118 GST_DEBUG_OBJECT (pad, "dropped event");
4122 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
4129 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4130 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4131 GST_OBJECT_UNLOCK (pad);
4132 gst_event_unref (event);
4133 return sticky ? GST_FLOW_OK : GST_FLOW_NOT_LINKED;
4137 GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
4138 GST_OBJECT_UNLOCK (pad);
4144 * gst_pad_push_event:
4145 * @pad: a #GstPad to push the event to.
4146 * @event: (transfer full): the #GstEvent to send to the pad.
4148 * Sends the event to the peer of the given pad. This function is
4149 * mainly used by elements to send events to their peer
4152 * This function takes owership of the provided event so you should
4153 * gst_event_ref() it if you want to reuse the event after this call.
4155 * Returns: TRUE if the event was handled.
4160 gst_pad_push_event (GstPad * pad, GstEvent * event)
4163 GstPadProbeType type;
4166 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4167 g_return_val_if_fail (event != NULL, FALSE);
4168 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4170 if (GST_PAD_IS_SRC (pad)) {
4171 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4172 goto wrong_direction;
4173 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4174 } else if (GST_PAD_IS_SINK (pad)) {
4175 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4176 goto wrong_direction;
4177 /* events pushed on sinkpad never are sticky */
4178 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4180 goto unknown_direction;
4182 if (gst_pad_push_event_unchecked (pad, event, type, &stored) != GST_FLOW_OK)
4183 res = stored ? TRUE : FALSE;
4189 /* ERROR handling */
4192 g_warning ("pad %s:%s pushing %s event in wrong direction",
4193 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4194 gst_event_unref (event);
4199 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4200 gst_event_unref (event);
4205 /* Check if we can call the event function with the given event */
4206 static GstFlowReturn
4207 pre_eventfunc_check (GstPad * pad, GstEvent * event)
4209 GstCaps *caps, *templ;
4211 switch (GST_EVENT_TYPE (event)) {
4212 case GST_EVENT_CAPS:
4214 /* backwards compatibility mode for caps */
4215 gst_event_parse_caps (event, &caps);
4217 /* See if pad accepts the caps */
4218 templ = gst_pad_get_pad_template_caps (pad);
4219 if (!gst_caps_is_subset (caps, templ))
4222 gst_caps_unref (templ);
4233 gst_caps_unref (templ);
4234 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4235 "caps %" GST_PTR_FORMAT " not accepted", caps);
4236 return GST_FLOW_NOT_NEGOTIATED;
4240 static GstFlowReturn
4241 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
4242 GstPadProbeType type)
4245 GstEventType event_type;
4246 gboolean serialized, need_unlock = FALSE, sticky;
4247 GstPadEventFunction eventfunc;
4250 GST_OBJECT_LOCK (pad);
4251 if (GST_PAD_IS_SINK (pad))
4252 serialized = GST_EVENT_IS_SERIALIZED (event);
4255 sticky = GST_EVENT_IS_STICKY (event);
4256 event_type = GST_EVENT_TYPE (event);
4257 switch (event_type) {
4258 case GST_EVENT_FLUSH_START:
4259 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4260 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4262 /* can't even accept a flush begin event when flushing */
4263 if (GST_PAD_IS_FLUSHING (pad))
4266 GST_PAD_SET_FLUSHING (pad);
4267 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4269 case GST_EVENT_FLUSH_STOP:
4270 if (G_LIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_NONE)) {
4271 GST_PAD_UNSET_FLUSHING (pad);
4272 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4274 /* Remove pending EOS events */
4275 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4276 remove_event_by_type (pad, GST_EVENT_EOS);
4278 GST_OBJECT_UNLOCK (pad);
4279 /* grab stream lock */
4280 GST_PAD_STREAM_LOCK (pad);
4282 GST_OBJECT_LOCK (pad);
4283 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4286 case GST_EVENT_RECONFIGURE:
4287 if (GST_PAD_IS_SRC (pad))
4288 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4290 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4291 GST_EVENT_TYPE_NAME (event));
4293 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4297 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4298 GST_OBJECT_UNLOCK (pad);
4299 GST_PAD_STREAM_LOCK (pad);
4301 GST_OBJECT_LOCK (pad);
4302 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4306 switch (GST_EVENT_TYPE (event)) {
4307 case GST_EVENT_SEGMENT:
4308 event = apply_pad_offset (pad, event);
4314 /* now do the probe */
4316 type | GST_PAD_PROBE_TYPE_PUSH |
4317 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4319 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4323 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4326 ACQUIRE_PARENT (pad, parent, no_parent);
4327 GST_OBJECT_UNLOCK (pad);
4329 ret = pre_eventfunc_check (pad, event);
4330 if (G_UNLIKELY (ret != GST_FLOW_OK))
4331 goto precheck_failed;
4334 gst_event_ref (event);
4336 if (eventfunc (pad, parent, event)) {
4339 /* something went wrong */
4340 switch (event_type) {
4341 case GST_EVENT_CAPS:
4342 ret = GST_FLOW_NOT_NEGOTIATED;
4345 ret = GST_FLOW_ERROR;
4349 RELEASE_PARENT (parent);
4351 GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
4354 if (ret == GST_FLOW_OK) {
4355 /* after the event function accepted the event, we can store the sticky
4356 * event on the pad */
4357 gst_pad_store_sticky_event (pad, event, FALSE);
4359 gst_event_unref (event);
4363 GST_PAD_STREAM_UNLOCK (pad);
4367 /* ERROR handling */
4370 GST_OBJECT_UNLOCK (pad);
4372 GST_PAD_STREAM_UNLOCK (pad);
4373 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4374 "Received event on flushing pad. Discarding");
4375 gst_event_unref (event);
4376 return GST_FLOW_WRONG_STATE;
4380 GST_OBJECT_UNLOCK (pad);
4382 GST_PAD_STREAM_UNLOCK (pad);
4383 gst_event_unref (event);
4386 case GST_FLOW_CUSTOM_SUCCESS:
4387 GST_DEBUG_OBJECT (pad, "dropped event");
4391 GST_DEBUG_OBJECT (pad, "en error occured %s", gst_flow_get_name (ret));
4398 g_warning ("pad %s:%s has no event handler, file a bug.",
4399 GST_DEBUG_PAD_NAME (pad));
4400 GST_OBJECT_UNLOCK (pad);
4402 GST_PAD_STREAM_UNLOCK (pad);
4403 gst_event_unref (event);
4404 return GST_FLOW_NOT_SUPPORTED;
4408 GST_DEBUG_OBJECT (pad, "no parent");
4409 GST_OBJECT_UNLOCK (pad);
4411 GST_PAD_STREAM_UNLOCK (pad);
4412 gst_event_unref (event);
4413 return GST_FLOW_WRONG_STATE;
4417 GST_DEBUG_OBJECT (pad, "pre event check failed");
4418 RELEASE_PARENT (parent);
4420 GST_PAD_STREAM_UNLOCK (pad);
4421 gst_event_unref (event);
4427 * gst_pad_send_event:
4428 * @pad: a #GstPad to send the event to.
4429 * @event: (transfer full): the #GstEvent to send to the pad.
4431 * Sends the event to the pad. This function can be used
4432 * by applications to send events in the pipeline.
4434 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4435 * sink pad, @event should be a downstream event. For example, you would not
4436 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4437 * Furthermore, some downstream events have to be serialized with data flow,
4438 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4439 * the event needs to be serialized with data flow, this function will take the
4440 * pad's stream lock while calling its event function.
4442 * To find out whether an event type is upstream, downstream, or downstream and
4443 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4444 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4445 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4446 * plugin doesn't need to bother itself with this information; the core handles
4447 * all necessary locks and checks.
4449 * This function takes owership of the provided event so you should
4450 * gst_event_ref() it if you want to reuse the event after this call.
4452 * Returns: TRUE if the event was handled.
4455 gst_pad_send_event (GstPad * pad, GstEvent * event)
4458 GstPadProbeType type;
4460 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4461 g_return_val_if_fail (event != NULL, FALSE);
4463 if (GST_PAD_IS_SINK (pad)) {
4464 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4465 goto wrong_direction;
4466 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4467 } else if (GST_PAD_IS_SRC (pad)) {
4468 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4469 goto wrong_direction;
4470 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4472 goto unknown_direction;
4474 if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
4481 /* ERROR handling */
4484 g_warning ("pad %s:%s sending %s event in wrong direction",
4485 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4486 gst_event_unref (event);
4491 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4492 gst_event_unref (event);
4498 * gst_pad_set_element_private:
4499 * @pad: the #GstPad to set the private data of.
4500 * @priv: The private data to attach to the pad.
4502 * Set the given private data gpointer on the pad.
4503 * This function can only be used by the element that owns the pad.
4504 * No locking is performed in this function.
4507 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4509 pad->element_private = priv;
4513 * gst_pad_get_element_private:
4514 * @pad: the #GstPad to get the private data of.
4516 * Gets the private data of a pad.
4517 * No locking is performed in this function.
4519 * Returns: (transfer none): a #gpointer to the private data.
4522 gst_pad_get_element_private (GstPad * pad)
4524 return pad->element_private;
4528 * gst_pad_get_sticky_event:
4529 * @pad: the #GstPad to get the event from.
4530 * @event_type: the #GstEventType that should be retrieved.
4531 * @idx: the index of the event
4533 * Returns a new reference of the sticky event of type @event_type
4536 * Returns: (transfer full): a #GstEvent of type @event_type or NULL when no
4537 * event of @event_type was on @pad. Unref after usage.
4540 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
4542 GstEvent *event = NULL;
4545 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4546 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4548 GST_OBJECT_LOCK (pad);
4549 ev = find_event_by_type (pad, event_type, idx);
4550 if (ev && (event = ev->event))
4551 gst_event_ref (event);
4552 GST_OBJECT_UNLOCK (pad);
4559 GstPadStickyEventsForeachFunction func;
4564 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
4566 ForeachDispatch *data = user_data;
4569 GST_OBJECT_UNLOCK (pad);
4571 ret = data->func (pad, &ev->event, data->user_data);
4573 GST_OBJECT_LOCK (pad);
4579 * gst_pad_sticky_events_foreach:
4580 * @pad: the #GstPad that should be used for iteration.
4581 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
4582 * should be called for every event.
4583 * @user_data: (closure): the optional user data.
4585 * Iterates all sticky events on @pad and calls @foreach_func for every
4586 * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
4589 gst_pad_sticky_events_foreach (GstPad * pad,
4590 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4592 ForeachDispatch data;
4594 g_return_if_fail (GST_IS_PAD (pad));
4595 g_return_if_fail (foreach_func != NULL);
4597 data.func = foreach_func;
4598 data.user_data = user_data;
4600 GST_OBJECT_LOCK (pad);
4601 events_foreach (pad, foreach_dispatch_function, &data);
4602 GST_OBJECT_UNLOCK (pad);
4606 do_stream_status (GstPad * pad, GstStreamStatusType type,
4607 GThread * thread, GstTask * task)
4611 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4613 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4614 if (GST_IS_ELEMENT (parent)) {
4615 GstMessage *message;
4616 GValue value = { 0 };
4618 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4619 gchar *tname, *ename, *pname;
4621 /* create a good task name */
4622 ename = gst_element_get_name (parent);
4623 pname = gst_pad_get_name (pad);
4624 tname = g_strdup_printf ("%s:%s", ename, pname);
4628 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4632 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4635 g_value_init (&value, GST_TYPE_TASK);
4636 g_value_set_object (&value, task);
4637 gst_message_set_stream_status_object (message, &value);
4638 g_value_unset (&value);
4640 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4641 gst_element_post_message (parent, message);
4643 gst_object_unref (parent);
4648 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4650 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4655 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4657 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4661 static GstTaskThreadCallbacks thr_callbacks = {
4667 * gst_pad_start_task:
4668 * @pad: the #GstPad to start the task of
4669 * @func: the task function to call
4670 * @data: data passed to the task function
4672 * Starts a task that repeatedly calls @func with @data. This function
4673 * is mostly used in pad activation functions to start the dataflow.
4674 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4675 * before @func is called.
4677 * Returns: a %TRUE if the task could be started.
4680 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4685 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4686 g_return_val_if_fail (func != NULL, FALSE);
4688 GST_DEBUG_OBJECT (pad, "start task");
4690 GST_OBJECT_LOCK (pad);
4691 task = GST_PAD_TASK (pad);
4693 task = gst_task_new (func, data);
4694 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4695 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4696 GST_DEBUG_OBJECT (pad, "created task");
4697 GST_PAD_TASK (pad) = task;
4698 gst_object_ref (task);
4699 /* release lock to post the message */
4700 GST_OBJECT_UNLOCK (pad);
4702 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4704 gst_object_unref (task);
4706 GST_OBJECT_LOCK (pad);
4707 /* nobody else is supposed to have changed the pad now */
4708 if (GST_PAD_TASK (pad) != task)
4709 goto concurrent_stop;
4711 res = gst_task_set_state (task, GST_TASK_STARTED);
4712 GST_OBJECT_UNLOCK (pad);
4719 GST_OBJECT_UNLOCK (pad);
4725 * gst_pad_pause_task:
4726 * @pad: the #GstPad to pause the task of
4728 * Pause the task of @pad. This function will also wait until the
4729 * function executed by the task is finished if this function is not
4730 * called from the task function.
4732 * Returns: a TRUE if the task could be paused or FALSE when the pad
4736 gst_pad_pause_task (GstPad * pad)
4741 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4743 GST_DEBUG_OBJECT (pad, "pause task");
4745 GST_OBJECT_LOCK (pad);
4746 task = GST_PAD_TASK (pad);
4749 res = gst_task_set_state (task, GST_TASK_PAUSED);
4750 GST_OBJECT_UNLOCK (pad);
4752 /* wait for task function to finish, this lock is recursive so it does nothing
4753 * when the pause is called from the task itself */
4754 GST_PAD_STREAM_LOCK (pad);
4755 GST_PAD_STREAM_UNLOCK (pad);
4761 GST_DEBUG_OBJECT (pad, "pad has no task");
4762 GST_OBJECT_UNLOCK (pad);
4768 * gst_pad_stop_task:
4769 * @pad: the #GstPad to stop the task of
4771 * Stop the task of @pad. This function will also make sure that the
4772 * function executed by the task will effectively stop if not called
4773 * from the GstTaskFunction.
4775 * This function will deadlock if called from the GstTaskFunction of
4776 * the task. Use gst_task_pause() instead.
4778 * Regardless of whether the pad has a task, the stream lock is acquired and
4779 * released so as to ensure that streaming through this pad has finished.
4781 * Returns: a TRUE if the task could be stopped or FALSE on error.
4784 gst_pad_stop_task (GstPad * pad)
4789 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4791 GST_DEBUG_OBJECT (pad, "stop task");
4793 GST_OBJECT_LOCK (pad);
4794 task = GST_PAD_TASK (pad);
4797 GST_PAD_TASK (pad) = NULL;
4798 res = gst_task_set_state (task, GST_TASK_STOPPED);
4799 GST_OBJECT_UNLOCK (pad);
4801 GST_PAD_STREAM_LOCK (pad);
4802 GST_PAD_STREAM_UNLOCK (pad);
4804 if (!gst_task_join (task))
4807 gst_object_unref (task);
4813 GST_DEBUG_OBJECT (pad, "no task");
4814 GST_OBJECT_UNLOCK (pad);
4816 GST_PAD_STREAM_LOCK (pad);
4817 GST_PAD_STREAM_UNLOCK (pad);
4819 /* this is not an error */
4824 /* this is bad, possibly the application tried to join the task from
4825 * the task's thread. We install the task again so that it will be stopped
4826 * again from the right thread next time hopefully. */
4827 GST_OBJECT_LOCK (pad);
4828 GST_DEBUG_OBJECT (pad, "join failed");
4829 /* we can only install this task if there was no other task */
4830 if (GST_PAD_TASK (pad) == NULL)
4831 GST_PAD_TASK (pad) = task;
4832 GST_OBJECT_UNLOCK (pad);