2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstpad.c: Pads for linking elements together
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * @short_description: Object contained by elements that allows links to
26 * @see_also: #GstPadTemplate, #GstElement, #GstEvent
28 * A #GstElement is linked to other elements via "pads", which are extremely
29 * light-weight generic link points.
30 * After two pads are retrieved from an element with gst_element_get_pad(),
31 * the pads can be link with gst_pad_link(). (For quick links,
32 * you can also use gst_element_link(), which will make the obvious
33 * link for you if it's straightforward.)
35 * Pads are typically created from a #GstPadTemplate with
36 * gst_pad_new_from_template().
38 * Pads have #GstCaps attached to it to describe the media type they are
39 * capable of dealing with. gst_pad_query_caps() and gst_pad_set_caps() are
40 * used to manipulate the caps of the pads.
41 * Pads created from a pad template cannot set capabilities that are
42 * incompatible with the pad template capabilities.
44 * Pads without pad templates can be created with gst_pad_new(),
45 * which takes a direction and a name as an argument. If the name is NULL,
46 * then a guaranteed unique name will be assigned to it.
48 * gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
50 * A #GstElement creating a pad will typically use the various
51 * gst_pad_set_*_function() calls to register callbacks for various events
54 * GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
55 * or pull in a buffer.
57 * To send a #GstEvent on a pad, use gst_pad_send_event() and
58 * gst_pad_push_event().
60 * Last reviewed on 2006-07-06 (0.10.9)
63 #include "gst_private.h"
66 #include "gstpadtemplate.h"
67 #include "gstenumtypes.h"
72 #include "glib-compat-private.h"
74 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
75 #define GST_CAT_DEFAULT GST_CAT_PADS
77 /* Pad signals and args */
95 #define GST_PAD_GET_PRIVATE(obj) \
96 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
98 /* we have a pending and an active event on the pad. On source pads only the
99 * active event is used. On sinkpads, events are copied to the pending entry and
100 * moved to the active event when the eventfunc returned TRUE. */
107 struct _GstPadPrivate
113 guint probe_list_cookie;
123 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
128 GstPadProbeInfo *info;
135 static void gst_pad_dispose (GObject * object);
136 static void gst_pad_finalize (GObject * object);
137 static void gst_pad_set_property (GObject * object, guint prop_id,
138 const GValue * value, GParamSpec * pspec);
139 static void gst_pad_get_property (GObject * object, guint prop_id,
140 GValue * value, GParamSpec * pspec);
142 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
143 static gboolean gst_pad_activate_default (GstPad * pad, GstObject * parent);
144 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
145 GstObject * parent, GstBufferList * list);
147 static GstFlowReturn gst_pad_send_event_unchecked (GstPad * pad,
148 GstEvent * event, GstPadProbeType type);
149 static GstFlowReturn gst_pad_push_event_unchecked (GstPad * pad,
150 GstEvent * event, GstPadProbeType type, gboolean * stored);
152 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
154 static GParamSpec *pspec_caps = NULL;
156 /* quarks for probe signals */
157 static GQuark buffer_quark;
158 static GQuark buffer_list_quark;
159 static GQuark event_quark;
168 static GstFlowQuarks flow_quarks[] = {
169 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
170 {GST_FLOW_OK, "ok", 0},
171 {GST_FLOW_NOT_LINKED, "not-linked", 0},
172 {GST_FLOW_FLUSHING, "flushing", 0},
173 {GST_FLOW_EOS, "eos", 0},
174 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
175 {GST_FLOW_ERROR, "error", 0},
176 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
177 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
182 * @ret: a #GstFlowReturn to get the name of.
184 * Gets a string representing the given flow return.
186 * Returns: a static string with the name of the flow return.
189 gst_flow_get_name (GstFlowReturn ret)
193 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
195 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
196 if (ret == flow_quarks[i].ret)
197 return flow_quarks[i].name;
204 * @ret: a #GstFlowReturn to get the quark of.
206 * Get the unique quark for the given GstFlowReturn.
208 * Returns: the quark associated with the flow return or 0 if an
209 * invalid return was specified.
212 gst_flow_to_quark (GstFlowReturn ret)
216 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
218 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
219 if (ret == flow_quarks[i].ret)
220 return flow_quarks[i].quark;
229 buffer_quark = g_quark_from_static_string ("buffer"); \
230 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
231 event_quark = g_quark_from_static_string ("event"); \
233 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
234 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
237 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
238 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
241 #define gst_pad_parent_class parent_class
242 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
245 gst_pad_class_init (GstPadClass * klass)
247 GObjectClass *gobject_class;
248 GstObjectClass *gstobject_class;
250 gobject_class = G_OBJECT_CLASS (klass);
251 gstobject_class = GST_OBJECT_CLASS (klass);
253 g_type_class_add_private (klass, sizeof (GstPadPrivate));
255 gobject_class->dispose = gst_pad_dispose;
256 gobject_class->finalize = gst_pad_finalize;
257 gobject_class->set_property = gst_pad_set_property;
258 gobject_class->get_property = gst_pad_get_property;
262 * @pad: the pad that emitted the signal
263 * @peer: the peer pad that has been connected
265 * Signals that a pad has been linked to the peer pad.
267 gst_pad_signals[PAD_LINKED] =
268 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
269 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
270 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
273 * @pad: the pad that emitted the signal
274 * @peer: the peer pad that has been disconnected
276 * Signals that a pad has been unlinked from the peer pad.
278 gst_pad_signals[PAD_UNLINKED] =
279 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
280 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
281 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
283 pspec_caps = g_param_spec_boxed ("caps", "Caps",
284 "The capabilities of the pad", GST_TYPE_CAPS,
285 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
286 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
288 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
289 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
290 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
291 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
293 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
294 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
295 g_param_spec_object ("template", "Template",
296 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
297 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 gstobject_class->path_string_separator = ".";
301 /* Register common function pointer descriptions */
302 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
303 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
304 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
305 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
306 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
310 gst_pad_init (GstPad * pad)
312 pad->priv = GST_PAD_GET_PRIVATE (pad);
314 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
316 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
317 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
318 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
319 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
320 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
322 GST_PAD_SET_FLUSHING (pad);
324 g_rec_mutex_init (&pad->stream_rec_lock);
326 g_cond_init (&pad->block_cond);
328 g_hook_list_init (&pad->probes, sizeof (GstProbe));
330 pad->priv->events = g_array_sized_new (FALSE, TRUE, sizeof (PadEvent), 16);
333 /* called when setting the pad inactive. It removes all sticky events from
336 remove_events (GstPad * pad)
341 events = pad->priv->events;
344 for (i = 0; i < len; i++) {
345 PadEvent *ev = &g_array_index (events, PadEvent, i);
346 gst_event_unref (ev->event);
348 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
349 g_array_set_size (events, 0);
350 pad->priv->events_cookie++;
354 find_event_by_type (GstPad * pad, GstEventType type, guint idx)
360 events = pad->priv->events;
363 for (i = 0; i < len; i++) {
364 ev = &g_array_index (events, PadEvent, i);
365 if (ev->event == NULL)
368 if (GST_EVENT_TYPE (ev->event) == type) {
380 find_event (GstPad * pad, GstEvent * event)
386 events = pad->priv->events;
389 for (i = 0; i < len; i++) {
390 ev = &g_array_index (events, PadEvent, i);
391 if (event == ev->event)
400 remove_event_by_type (GstPad * pad, GstEventType type)
406 events = pad->priv->events;
411 ev = &g_array_index (events, PadEvent, i);
412 if (ev->event == NULL)
415 if (GST_EVENT_TYPE (ev->event) != type)
418 gst_event_unref (ev->event);
419 g_array_remove_index (events, i);
421 pad->priv->events_cookie++;
429 /* check all events on srcpad against those on sinkpad. All events that are not
430 * on sinkpad are marked as received=FALSE and the PENDING_EVENTS is set on the
431 * srcpad so that the events will be sent next time */
433 schedule_events (GstPad * srcpad, GstPad * sinkpad)
438 gboolean pending = FALSE;
440 events = srcpad->priv->events;
443 for (i = 0; i < len; i++) {
444 ev = &g_array_index (events, PadEvent, i);
445 if (ev->event == NULL)
448 if (sinkpad == NULL || !find_event (sinkpad, ev->event)) {
449 ev->received = FALSE;
454 GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PENDING_EVENTS);
457 typedef gboolean (*PadEventFunction) (GstPad * pad, PadEvent * ev,
460 /* should be called with pad LOCK */
462 events_foreach (GstPad * pad, PadEventFunction func, gpointer user_data)
469 events = pad->priv->events;
472 cookie = pad->priv->events_cookie;
476 PadEvent *ev, ev_ret;
478 ev = &g_array_index (events, PadEvent, i);
479 if (G_UNLIKELY (ev->event == NULL))
482 /* take aditional ref, func might release the lock */
483 ev_ret.event = gst_event_ref (ev->event);
484 ev_ret.received = ev->received;
486 ret = func (pad, &ev_ret, user_data);
488 /* recheck the cookie, lock might have been released and the list could have
490 if (G_UNLIKELY (cookie != pad->priv->events_cookie)) {
491 if (G_LIKELY (ev_ret.event))
492 gst_event_unref (ev_ret.event);
496 /* if the event changed, we need to do something */
497 if (G_UNLIKELY (ev->event != ev_ret.event)) {
498 if (G_UNLIKELY (ev_ret.event == NULL)) {
499 /* function unreffed and set the event to NULL, remove it */
500 g_array_remove_index (events, i);
502 cookie = ++pad->priv->events_cookie;
505 /* function gave a new event for us */
506 gst_event_take (&ev->event, ev_ret.event);
509 /* just unref, nothing changed */
510 gst_event_unref (ev_ret.event);
519 /* should be called with LOCK */
521 apply_pad_offset (GstPad * pad, GstEvent * event)
523 /* check if we need to adjust the segment */
524 if (pad->offset != 0) {
527 /* copy segment values */
528 gst_event_copy_segment (event, &segment);
529 gst_event_unref (event);
531 /* adjust and make a new event with the offset applied */
532 segment.base += pad->offset;
533 event = gst_event_new_segment (&segment);
538 /* should be called with the OBJECT_LOCK */
540 get_pad_caps (GstPad * pad)
542 GstCaps *caps = NULL;
545 ev = find_event_by_type (pad, GST_EVENT_CAPS, 0);
547 gst_event_parse_caps (ev->event, &caps);
553 gst_pad_dispose (GObject * object)
555 GstPad *pad = GST_PAD_CAST (object);
558 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
560 /* unlink the peer pad */
561 if ((peer = gst_pad_get_peer (pad))) {
562 /* window for MT unsafeness, someone else could unlink here
563 * and then we call unlink with wrong pads. The unlink
564 * function would catch this and safely return failed. */
565 if (GST_PAD_IS_SRC (pad))
566 gst_pad_unlink (pad, peer);
568 gst_pad_unlink (peer, pad);
570 gst_object_unref (peer);
573 gst_pad_set_pad_template (pad, NULL);
577 g_hook_list_clear (&pad->probes);
579 G_OBJECT_CLASS (parent_class)->dispose (object);
583 gst_pad_finalize (GObject * object)
585 GstPad *pad = GST_PAD_CAST (object);
588 /* in case the task is still around, clean it up */
589 if ((task = GST_PAD_TASK (pad))) {
590 gst_task_join (task);
591 GST_PAD_TASK (pad) = NULL;
592 gst_object_unref (task);
595 if (pad->activatenotify)
596 pad->activatenotify (pad->activatedata);
597 if (pad->activatemodenotify)
598 pad->activatemodenotify (pad->activatemodedata);
600 pad->linknotify (pad->linkdata);
601 if (pad->unlinknotify)
602 pad->unlinknotify (pad->unlinkdata);
603 if (pad->chainnotify)
604 pad->chainnotify (pad->chaindata);
605 if (pad->chainlistnotify)
606 pad->chainlistnotify (pad->chainlistdata);
607 if (pad->getrangenotify)
608 pad->getrangenotify (pad->getrangedata);
609 if (pad->eventnotify)
610 pad->eventnotify (pad->eventdata);
611 if (pad->querynotify)
612 pad->querynotify (pad->querydata);
613 if (pad->iterintlinknotify)
614 pad->iterintlinknotify (pad->iterintlinkdata);
616 g_rec_mutex_clear (&pad->stream_rec_lock);
617 g_cond_clear (&pad->block_cond);
618 g_array_free (pad->priv->events, TRUE);
620 G_OBJECT_CLASS (parent_class)->finalize (object);
624 gst_pad_set_property (GObject * object, guint prop_id,
625 const GValue * value, GParamSpec * pspec)
627 g_return_if_fail (GST_IS_PAD (object));
630 case PAD_PROP_DIRECTION:
631 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
633 case PAD_PROP_TEMPLATE:
634 gst_pad_set_pad_template (GST_PAD_CAST (object),
635 (GstPadTemplate *) g_value_get_object (value));
638 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
644 gst_pad_get_property (GObject * object, guint prop_id,
645 GValue * value, GParamSpec * pspec)
647 g_return_if_fail (GST_IS_PAD (object));
651 GST_OBJECT_LOCK (object);
652 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
653 GST_OBJECT_UNLOCK (object);
655 case PAD_PROP_DIRECTION:
656 g_value_set_enum (value, GST_PAD_DIRECTION (object));
658 case PAD_PROP_TEMPLATE:
659 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
662 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
669 * @name: the name of the new pad.
670 * @direction: the #GstPadDirection of the pad.
672 * Creates a new pad with the given name in the given direction.
673 * If name is NULL, a guaranteed unique name (across all pads)
675 * This function makes a copy of the name so you can safely free the name.
677 * Returns: (transfer floating): a new #GstPad, or NULL in case of an error.
682 gst_pad_new (const gchar * name, GstPadDirection direction)
684 return g_object_new (GST_TYPE_PAD,
685 "name", name, "direction", direction, NULL);
689 * gst_pad_new_from_template:
690 * @templ: the pad template to use
691 * @name: the name of the element
693 * Creates a new pad with the given name from the given template.
694 * If name is NULL, a guaranteed unique name (across all pads)
696 * This function makes a copy of the name so you can safely free the name.
698 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
701 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
703 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
705 return g_object_new (GST_TYPE_PAD,
706 "name", name, "direction", templ->direction, "template", templ, NULL);
710 * gst_pad_new_from_static_template:
711 * @templ: the #GstStaticPadTemplate to use
712 * @name: the name of the element
714 * Creates a new pad with the given name from the given static template.
715 * If name is NULL, a guaranteed unique name (across all pads)
717 * This function makes a copy of the name so you can safely free the name.
719 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
722 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
726 GstPadTemplate *template;
728 template = gst_static_pad_template_get (templ);
729 pad = gst_pad_new_from_template (template, name);
730 gst_object_unref (template);
734 #define ACQUIRE_PARENT(pad, parent, label) \
736 if (G_LIKELY ((parent = GST_OBJECT_PARENT (pad)))) \
737 gst_object_ref (parent); \
738 else if (G_LIKELY (GST_PAD_NEEDS_PARENT (pad))) \
742 #define RELEASE_PARENT(parent) \
744 if (G_LIKELY (parent)) \
745 gst_object_unref (parent); \
749 * gst_pad_get_direction:
750 * @pad: a #GstPad to get the direction of.
752 * Gets the direction of the pad. The direction of the pad is
753 * decided at construction time so this function does not take
756 * Returns: the #GstPadDirection of the pad.
761 gst_pad_get_direction (GstPad * pad)
763 GstPadDirection result;
765 /* PAD_UNKNOWN is a little silly but we need some sort of
766 * error return value */
767 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
769 result = GST_PAD_DIRECTION (pad);
775 gst_pad_activate_default (GstPad * pad, GstObject * parent)
777 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
781 pre_activate (GstPad * pad, GstPadMode new_mode)
784 case GST_PAD_MODE_NONE:
785 GST_OBJECT_LOCK (pad);
786 GST_DEBUG_OBJECT (pad, "setting PAD_MODE NONE, set flushing");
787 GST_PAD_SET_FLUSHING (pad);
788 GST_PAD_MODE (pad) = new_mode;
789 /* unlock blocked pads so element can resume and stop */
790 GST_PAD_BLOCK_BROADCAST (pad);
791 GST_OBJECT_UNLOCK (pad);
793 case GST_PAD_MODE_PUSH:
794 case GST_PAD_MODE_PULL:
795 GST_OBJECT_LOCK (pad);
796 GST_DEBUG_OBJECT (pad, "setting PAD_MODE %d, unset flushing", new_mode);
797 GST_PAD_UNSET_FLUSHING (pad);
798 GST_PAD_MODE (pad) = new_mode;
799 if (GST_PAD_IS_SINK (pad)) {
801 /* make sure the peer src pad sends us all events */
802 if ((peer = GST_PAD_PEER (pad))) {
803 gst_object_ref (peer);
804 GST_OBJECT_UNLOCK (pad);
806 GST_DEBUG_OBJECT (pad, "reschedule events on peer %s:%s",
807 GST_DEBUG_PAD_NAME (peer));
809 GST_OBJECT_LOCK (peer);
810 schedule_events (peer, NULL);
811 GST_OBJECT_UNLOCK (peer);
813 gst_object_unref (peer);
815 GST_OBJECT_UNLOCK (pad);
818 GST_OBJECT_UNLOCK (pad);
825 post_activate (GstPad * pad, GstPadMode new_mode)
828 case GST_PAD_MODE_NONE:
829 /* ensures that streaming stops */
830 GST_PAD_STREAM_LOCK (pad);
831 GST_DEBUG_OBJECT (pad, "stopped streaming");
832 GST_OBJECT_LOCK (pad);
834 GST_OBJECT_UNLOCK (pad);
835 GST_PAD_STREAM_UNLOCK (pad);
837 case GST_PAD_MODE_PUSH:
838 case GST_PAD_MODE_PULL:
845 * gst_pad_set_active:
846 * @pad: the #GstPad to activate or deactivate.
847 * @active: whether or not the pad should be active.
849 * Activates or deactivates the given pad.
850 * Normally called from within core state change functions.
852 * If @active, makes sure the pad is active. If it is already active, either in
853 * push or pull mode, just return. Otherwise dispatches to the pad's activate
854 * function to perform the actual activation.
856 * If not @active, checks the pad's current mode and calls
857 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
860 * Returns: #TRUE if the operation was successful.
865 gst_pad_set_active (GstPad * pad, gboolean active)
869 gboolean ret = FALSE;
871 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
873 GST_OBJECT_LOCK (pad);
874 old = GST_PAD_MODE (pad);
875 ACQUIRE_PARENT (pad, parent, no_parent);
876 GST_OBJECT_UNLOCK (pad);
879 if (old == GST_PAD_MODE_NONE) {
880 GST_DEBUG_OBJECT (pad, "activating pad from none");
881 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad, parent);
883 GST_DEBUG_OBJECT (pad, "pad was active in mode %d", old);
887 if (old == GST_PAD_MODE_NONE) {
888 GST_DEBUG_OBJECT (pad, "pad was inactive");
891 GST_DEBUG_OBJECT (pad, "deactivating pad from mode %d", old);
892 ret = gst_pad_activate_mode (pad, old, FALSE);
896 RELEASE_PARENT (parent);
898 if (G_UNLIKELY (!ret))
902 GST_OBJECT_LOCK (pad);
903 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
904 GST_OBJECT_UNLOCK (pad);
911 GST_DEBUG_OBJECT (pad, "no parent");
912 GST_OBJECT_UNLOCK (pad);
917 GST_OBJECT_LOCK (pad);
919 g_critical ("Failed to deactivate pad %s:%s, very bad",
920 GST_DEBUG_PAD_NAME (pad));
922 GST_WARNING_OBJECT (pad, "Failed to activate pad");
924 GST_OBJECT_UNLOCK (pad);
930 * gst_pad_activate_mode:
931 * @pad: the #GstPad to activate or deactivate.
932 * @mode: the requested activation mode
933 * @active: whether or not the pad should be active.
935 * Activates or deactivates the given pad in @mode via dispatching to the
936 * pad's activatemodefunc. For use from within pad activation functions only.
938 * If you don't know what this is, you probably don't want to call it.
940 * Returns: TRUE if the operation was successful.
945 gst_pad_activate_mode (GstPad * pad, GstPadMode mode, gboolean active)
947 gboolean res = FALSE;
953 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
955 GST_OBJECT_LOCK (pad);
956 old = GST_PAD_MODE (pad);
957 dir = GST_PAD_DIRECTION (pad);
958 ACQUIRE_PARENT (pad, parent, no_parent);
959 GST_OBJECT_UNLOCK (pad);
961 new = active ? mode : GST_PAD_MODE_NONE;
966 if (active && old != mode) {
967 /* pad was activate in the wrong direction, deactivate it
968 * and reactivate it in the requested mode */
969 GST_DEBUG_OBJECT (pad, "deactivating pad from mode %d", old);
970 if (G_UNLIKELY (!gst_pad_activate_mode (pad, old, FALSE)))
971 goto deactivate_failed;
975 case GST_PAD_MODE_PULL:
977 if (dir == GST_PAD_SINK) {
978 if ((peer = gst_pad_get_peer (pad))) {
979 GST_DEBUG_OBJECT (pad, "calling peer");
980 if (G_UNLIKELY (!gst_pad_activate_mode (peer, mode, active)))
982 gst_object_unref (peer);
984 /* there is no peer, this is only fatal when we activate. When we
985 * deactivate, we must assume the application has unlinked the peer and
986 * will deactivate it eventually. */
990 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
993 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
994 goto failure; /* Can't activate pull on a src without a
1003 pre_activate (pad, new);
1005 if (GST_PAD_ACTIVATEMODEFUNC (pad)) {
1006 if (G_UNLIKELY (!GST_PAD_ACTIVATEMODEFUNC (pad) (pad, parent, mode,
1010 /* can happen for sinks of passthrough elements */
1013 post_activate (pad, new);
1015 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in mode %d",
1016 active ? "activated" : "deactivated", mode);
1021 RELEASE_PARENT (parent);
1027 GST_DEBUG_OBJECT (pad, "no parent");
1028 GST_OBJECT_UNLOCK (pad);
1033 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in mode %d",
1034 active ? "activated" : "deactivated", mode);
1039 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1040 "failed to %s in switch to mode %d from mode %d",
1041 (active ? "activate" : "deactivate"), mode, old);
1046 GST_OBJECT_LOCK (peer);
1047 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1048 "activate_mode on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
1049 GST_OBJECT_UNLOCK (peer);
1050 gst_object_unref (peer);
1055 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
1056 "pad in pull mode");
1061 GST_OBJECT_LOCK (pad);
1062 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in mode %d",
1063 active ? "activate" : "deactivate", mode);
1064 GST_PAD_SET_FLUSHING (pad);
1065 GST_PAD_MODE (pad) = old;
1066 GST_OBJECT_UNLOCK (pad);
1072 * gst_pad_is_active:
1073 * @pad: the #GstPad to query
1075 * Query if a pad is active
1077 * Returns: TRUE if the pad is active.
1082 gst_pad_is_active (GstPad * pad)
1084 gboolean result = FALSE;
1086 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1088 GST_OBJECT_LOCK (pad);
1089 result = GST_PAD_IS_ACTIVE (pad);
1090 GST_OBJECT_UNLOCK (pad);
1096 * gst_pad_add_probe:
1097 * @pad: the #GstPad to add the probe to
1098 * @mask: the probe mask
1099 * @callback: #GstPadProbeCallback that will be called with notifications of
1101 * @user_data: (closure): user data passed to the callback
1102 * @destroy_data: #GDestroyNotify for user_data
1104 * Be notified of different states of pads. The provided callback is called for
1105 * every state that matches @mask.
1107 * Returns: an id or 0 on error. The id can be used to remove the probe with
1108 * gst_pad_remove_probe().
1113 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
1114 GstPadProbeCallback callback, gpointer user_data,
1115 GDestroyNotify destroy_data)
1120 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1121 g_return_val_if_fail (mask != 0, 0);
1123 GST_OBJECT_LOCK (pad);
1125 /* make a new probe */
1126 hook = g_hook_alloc (&pad->probes);
1128 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1131 /* when no contraints are given for the types, assume all types are
1133 if ((mask & GST_PAD_PROBE_TYPE_ALL_BOTH) == 0)
1134 mask |= GST_PAD_PROBE_TYPE_ALL_BOTH;
1135 if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1136 mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1138 /* store our flags and other fields */
1139 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1140 hook->func = callback;
1141 hook->data = user_data;
1142 hook->destroy = destroy_data;
1143 PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
1146 g_hook_prepend (&pad->probes, hook);
1148 /* incremenent cookie so that the new hook get's called */
1149 pad->priv->probe_list_cookie++;
1151 /* get the id of the hook, we return this and it can be used to remove the
1153 res = hook->hook_id;
1155 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1157 if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1158 /* we have a block probe */
1160 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKED);
1161 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1162 "now %d blocking probes", pad->num_blocked);
1165 /* call the callback if we need to be called for idle callbacks */
1166 if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1167 if (pad->priv->using > 0) {
1168 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1169 * flag above, the last thread to leave the push will do the callback. New
1170 * threads going into the push will block. */
1171 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1172 "pad is in use, delay idle callback");
1173 GST_OBJECT_UNLOCK (pad);
1175 GstPadProbeInfo info = { GST_PAD_PROBE_TYPE_IDLE, res, };
1177 /* the pad is idle now, we can signal the idle callback now */
1178 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1179 "pad is idle, trigger idle callback");
1180 GST_OBJECT_UNLOCK (pad);
1182 callback (pad, &info, user_data);
1185 GST_OBJECT_UNLOCK (pad);
1191 cleanup_hook (GstPad * pad, GHook * hook)
1193 GstPadProbeType type;
1195 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1197 if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1198 /* unblock when we remove the last blocking probe */
1200 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1202 if (pad->num_blocked == 0) {
1203 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1204 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKED);
1205 GST_PAD_BLOCK_BROADCAST (pad);
1208 g_hook_destroy_link (&pad->probes, hook);
1213 * gst_pad_remove_probe:
1214 * @pad: the #GstPad with the probe
1215 * @id: the probe id to remove
1217 * Remove the probe with @id from @pad.
1222 gst_pad_remove_probe (GstPad * pad, gulong id)
1226 g_return_if_fail (GST_IS_PAD (pad));
1228 GST_OBJECT_LOCK (pad);
1230 hook = g_hook_get (&pad->probes, id);
1234 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1236 cleanup_hook (pad, hook);
1237 GST_OBJECT_UNLOCK (pad);
1243 GST_OBJECT_UNLOCK (pad);
1244 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1250 * gst_pad_is_blocked:
1251 * @pad: the #GstPad to query
1253 * Checks if the pad is blocked or not. This function returns the
1254 * last requested state of the pad. It is not certain that the pad
1255 * is actually blocking at this point (see gst_pad_is_blocking()).
1257 * Returns: TRUE if the pad is blocked.
1262 gst_pad_is_blocked (GstPad * pad)
1264 gboolean result = FALSE;
1266 g_return_val_if_fail (GST_IS_PAD (pad), result);
1268 GST_OBJECT_LOCK (pad);
1269 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED);
1270 GST_OBJECT_UNLOCK (pad);
1276 * gst_pad_is_blocking:
1277 * @pad: the #GstPad to query
1279 * Checks if the pad is blocking or not. This is a guaranteed state
1280 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1282 * Returns: TRUE if the pad is blocking.
1289 gst_pad_is_blocking (GstPad * pad)
1291 gboolean result = FALSE;
1293 g_return_val_if_fail (GST_IS_PAD (pad), result);
1295 GST_OBJECT_LOCK (pad);
1296 /* the blocking flag is only valid if the pad is not flushing */
1297 result = GST_PAD_IS_BLOCKING (pad) && !GST_PAD_IS_FLUSHING (pad);
1298 GST_OBJECT_UNLOCK (pad);
1304 * gst_pad_check_reconfigure:
1305 * @pad: the #GstPad to check
1307 * Check and clear the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1308 * if the flag was set.
1310 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag was set on @pad.
1313 gst_pad_check_reconfigure (GstPad * pad)
1315 gboolean reconfigure;
1317 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1319 GST_OBJECT_LOCK (pad);
1320 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1322 GST_DEBUG_OBJECT (pad, "remove RECONFIGURE flag");
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 * @user_data: user_data passed to @notify
1351 * @notify: notify called when @activate will not be used anymore.
1353 * Sets the given activate function for @pad. The activate function will
1354 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1355 * the actual activation. Only makes sense to set on sink pads.
1357 * Call this function if your sink pad can start a pull-based task.
1360 gst_pad_set_activate_function_full (GstPad * pad,
1361 GstPadActivateFunction activate, gpointer user_data, GDestroyNotify notify)
1363 g_return_if_fail (GST_IS_PAD (pad));
1365 if (pad->activatenotify)
1366 pad->activatenotify (pad->activatedata);
1367 GST_PAD_ACTIVATEFUNC (pad) = activate;
1368 pad->activatedata = user_data;
1369 pad->activatenotify = notify;
1371 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1372 GST_DEBUG_FUNCPTR_NAME (activate));
1376 * gst_pad_set_activatemode_function_full:
1378 * @activatemode: the #GstPadActivateModeFunction to set.
1379 * @user_data: user_data passed to @notify
1380 * @notify: notify called when @activatemode will not be used anymore.
1382 * Sets the given activate_mode function for the pad. An activate_mode function
1383 * prepares the element for data passing.
1386 gst_pad_set_activatemode_function_full (GstPad * pad,
1387 GstPadActivateModeFunction activatemode, gpointer user_data,
1388 GDestroyNotify notify)
1390 g_return_if_fail (GST_IS_PAD (pad));
1392 if (pad->activatemodenotify)
1393 pad->activatemodenotify (pad->activatemodedata);
1394 GST_PAD_ACTIVATEMODEFUNC (pad) = activatemode;
1395 pad->activatemodedata = user_data;
1396 pad->activatemodenotify = notify;
1398 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatemodefunc set to %s",
1399 GST_DEBUG_FUNCPTR_NAME (activatemode));
1403 * gst_pad_set_chain_function_full:
1404 * @pad: a sink #GstPad.
1405 * @chain: the #GstPadChainFunction to set.
1406 * @user_data: user_data passed to @notify
1407 * @notify: notify called when @chain will not be used anymore.
1409 * Sets the given chain function for the pad. The chain function is called to
1410 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1413 gst_pad_set_chain_function_full (GstPad * pad, GstPadChainFunction chain,
1414 gpointer user_data, GDestroyNotify notify)
1416 g_return_if_fail (GST_IS_PAD (pad));
1417 g_return_if_fail (GST_PAD_IS_SINK (pad));
1419 if (pad->chainnotify)
1420 pad->chainnotify (pad->chaindata);
1421 GST_PAD_CHAINFUNC (pad) = chain;
1422 pad->chaindata = user_data;
1423 pad->chainnotify = notify;
1425 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1426 GST_DEBUG_FUNCPTR_NAME (chain));
1430 * gst_pad_set_chain_list_function_full:
1431 * @pad: a sink #GstPad.
1432 * @chainlist: the #GstPadChainListFunction to set.
1433 * @user_data: user_data passed to @notify
1434 * @notify: notify called when @chainlist will not be used anymore.
1436 * Sets the given chain list function for the pad. The chainlist function is
1437 * called to process a #GstBufferList input buffer list. See
1438 * #GstPadChainListFunction for more details.
1443 gst_pad_set_chain_list_function_full (GstPad * pad,
1444 GstPadChainListFunction chainlist, gpointer user_data,
1445 GDestroyNotify notify)
1447 g_return_if_fail (GST_IS_PAD (pad));
1448 g_return_if_fail (GST_PAD_IS_SINK (pad));
1450 if (pad->chainlistnotify)
1451 pad->chainlistnotify (pad->chainlistdata);
1452 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1453 pad->chainlistdata = user_data;
1454 pad->chainlistnotify = notify;
1456 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1457 GST_DEBUG_FUNCPTR_NAME (chainlist));
1461 * gst_pad_set_getrange_function_full:
1462 * @pad: a source #GstPad.
1463 * @get: the #GstPadGetRangeFunction to set.
1464 * @user_data: user_data passed to @notify
1465 * @notify: notify called when @get will not be used anymore.
1467 * Sets the given getrange function for the pad. The getrange function is
1468 * called to produce a new #GstBuffer to start the processing pipeline. see
1469 * #GstPadGetRangeFunction for a description of the getrange function.
1472 gst_pad_set_getrange_function_full (GstPad * pad, GstPadGetRangeFunction get,
1473 gpointer user_data, GDestroyNotify notify)
1475 g_return_if_fail (GST_IS_PAD (pad));
1476 g_return_if_fail (GST_PAD_IS_SRC (pad));
1478 if (pad->getrangenotify)
1479 pad->getrangenotify (pad->getrangedata);
1480 GST_PAD_GETRANGEFUNC (pad) = get;
1481 pad->getrangedata = user_data;
1482 pad->getrangenotify = notify;
1484 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1485 GST_DEBUG_FUNCPTR_NAME (get));
1489 * gst_pad_set_event_function_full:
1490 * @pad: a #GstPad of either direction.
1491 * @event: the #GstPadEventFunction to set.
1492 * @user_data: user_data passed to @notify
1493 * @notify: notify called when @event will not be used anymore.
1495 * Sets the given event handler for the pad.
1498 gst_pad_set_event_function_full (GstPad * pad, GstPadEventFunction event,
1499 gpointer user_data, GDestroyNotify notify)
1501 g_return_if_fail (GST_IS_PAD (pad));
1503 if (pad->eventnotify)
1504 pad->eventnotify (pad->eventdata);
1505 GST_PAD_EVENTFUNC (pad) = event;
1506 pad->eventdata = user_data;
1507 pad->eventnotify = notify;
1509 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1510 GST_DEBUG_FUNCPTR_NAME (event));
1514 * gst_pad_set_query_function_full:
1515 * @pad: a #GstPad of either direction.
1516 * @query: the #GstPadQueryFunction to set.
1517 * @user_data: user_data passed to @notify
1518 * @notify: notify called when @query will not be used anymore.
1520 * Set the given query function for the pad.
1523 gst_pad_set_query_function_full (GstPad * pad, GstPadQueryFunction query,
1524 gpointer user_data, GDestroyNotify notify)
1526 g_return_if_fail (GST_IS_PAD (pad));
1528 if (pad->querynotify)
1529 pad->querynotify (pad->querydata);
1530 GST_PAD_QUERYFUNC (pad) = query;
1531 pad->querydata = user_data;
1532 pad->querynotify = notify;
1534 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1535 GST_DEBUG_FUNCPTR_NAME (query));
1539 * gst_pad_set_iterate_internal_links_function_full:
1540 * @pad: a #GstPad of either direction.
1541 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1542 * @user_data: user_data passed to @notify
1543 * @notify: notify called when @iterintlink will not be used anymore.
1545 * Sets the given internal link iterator function for the pad.
1550 gst_pad_set_iterate_internal_links_function_full (GstPad * pad,
1551 GstPadIterIntLinkFunction iterintlink, gpointer user_data,
1552 GDestroyNotify notify)
1554 g_return_if_fail (GST_IS_PAD (pad));
1556 if (pad->iterintlinknotify)
1557 pad->iterintlinknotify (pad->iterintlinkdata);
1558 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1559 pad->iterintlinkdata = user_data;
1560 pad->iterintlinknotify = notify;
1562 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1563 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1567 * gst_pad_set_link_function_full:
1569 * @link: the #GstPadLinkFunction to set.
1570 * @user_data: user_data passed to @notify
1571 * @notify: notify called when @link will not be used anymore.
1573 * Sets the given link function for the pad. It will be called when
1574 * the pad is linked with another pad.
1576 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1579 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1580 * cannot be made for some reason.
1582 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1583 * of the peer sink pad, if present.
1586 gst_pad_set_link_function_full (GstPad * pad, GstPadLinkFunction link,
1587 gpointer user_data, GDestroyNotify notify)
1589 g_return_if_fail (GST_IS_PAD (pad));
1591 if (pad->linknotify)
1592 pad->linknotify (pad->linkdata);
1593 GST_PAD_LINKFUNC (pad) = link;
1594 pad->linkdata = user_data;
1595 pad->linknotify = notify;
1597 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1598 GST_DEBUG_FUNCPTR_NAME (link));
1602 * gst_pad_set_unlink_function_full:
1604 * @unlink: the #GstPadUnlinkFunction to set.
1605 * @user_data: user_data passed to @notify
1606 * @notify: notify called when @unlink will not be used anymore.
1608 * Sets the given unlink function for the pad. It will be called
1609 * when the pad is unlinked.
1612 gst_pad_set_unlink_function_full (GstPad * pad, GstPadUnlinkFunction unlink,
1613 gpointer user_data, GDestroyNotify notify)
1615 g_return_if_fail (GST_IS_PAD (pad));
1617 if (pad->unlinknotify)
1618 pad->unlinknotify (pad->unlinkdata);
1619 GST_PAD_UNLINKFUNC (pad) = unlink;
1620 pad->unlinkdata = user_data;
1621 pad->unlinknotify = notify;
1623 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1624 GST_DEBUG_FUNCPTR_NAME (unlink));
1629 * @srcpad: the source #GstPad to unlink.
1630 * @sinkpad: the sink #GstPad to unlink.
1632 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1633 * signal on both pads.
1635 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1636 * the pads were not linked together.
1641 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1643 gboolean result = FALSE;
1644 GstElement *parent = NULL;
1646 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1647 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1648 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1649 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1651 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1652 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1653 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1655 /* We need to notify the parent before taking any pad locks as the bin in
1656 * question might be waiting for a lock on the pad while holding its lock
1657 * that our message will try to take. */
1658 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1659 if (GST_IS_ELEMENT (parent)) {
1660 gst_element_post_message (parent,
1661 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1662 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1664 gst_object_unref (parent);
1669 GST_OBJECT_LOCK (srcpad);
1670 GST_OBJECT_LOCK (sinkpad);
1672 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1673 goto not_linked_together;
1675 if (GST_PAD_UNLINKFUNC (srcpad)) {
1676 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1678 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1679 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1682 /* first clear peers */
1683 GST_PAD_PEER (srcpad) = NULL;
1684 GST_PAD_PEER (sinkpad) = NULL;
1686 GST_OBJECT_UNLOCK (sinkpad);
1687 GST_OBJECT_UNLOCK (srcpad);
1689 /* fire off a signal to each of the pads telling them
1690 * that they've been unlinked */
1691 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1692 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1694 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1695 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1700 if (parent != NULL) {
1701 gst_element_post_message (parent,
1702 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1703 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1704 gst_object_unref (parent);
1709 not_linked_together:
1711 /* we do not emit a warning in this case because unlinking cannot
1712 * be made MT safe.*/
1713 GST_OBJECT_UNLOCK (sinkpad);
1714 GST_OBJECT_UNLOCK (srcpad);
1720 * gst_pad_is_linked:
1721 * @pad: pad to check
1723 * Checks if a @pad is linked to another pad or not.
1725 * Returns: TRUE if the pad is linked, FALSE otherwise.
1730 gst_pad_is_linked (GstPad * pad)
1734 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1736 GST_OBJECT_LOCK (pad);
1737 result = (GST_PAD_PEER (pad) != NULL);
1738 GST_OBJECT_UNLOCK (pad);
1743 /* get the caps from both pads and see if the intersection
1746 * This function should be called with the pad LOCK on both
1750 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1751 GstPadLinkCheck flags)
1753 GstCaps *srccaps = NULL;
1754 GstCaps *sinkcaps = NULL;
1755 gboolean compatible = FALSE;
1757 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1760 /* Doing the expensive caps checking takes priority over only checking the template caps */
1761 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1762 GST_OBJECT_UNLOCK (sink);
1763 GST_OBJECT_UNLOCK (src);
1765 srccaps = gst_pad_query_caps (src, NULL);
1766 sinkcaps = gst_pad_query_caps (sink, NULL);
1768 GST_OBJECT_LOCK (src);
1769 GST_OBJECT_LOCK (sink);
1771 /* If one of the two pads doesn't have a template, consider the intersection
1773 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1774 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1778 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1780 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1783 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1785 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1788 /* if we have caps on both pads we can check the intersection. If one
1789 * of the caps is NULL, we return TRUE. */
1790 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1792 gst_caps_unref (srccaps);
1794 gst_caps_unref (sinkcaps);
1798 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1799 gst_caps_unref (srccaps);
1800 gst_caps_unref (sinkcaps);
1803 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1804 (compatible ? "" : "not"));
1809 /* check if the grandparents of both pads are the same.
1810 * This check is required so that we don't try to link
1811 * pads from elements in different bins without ghostpads.
1813 * The LOCK should be held on both pads
1816 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1818 GstObject *psrc, *psink;
1820 psrc = GST_OBJECT_PARENT (src);
1821 psink = GST_OBJECT_PARENT (sink);
1823 /* if one of the pads has no parent, we allow the link */
1824 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1827 /* only care about parents that are elements */
1828 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1829 goto no_element_parent;
1831 /* if the parents are the same, we have a loop */
1832 if (G_UNLIKELY (psrc == psink))
1835 /* if they both have a parent, we check the grandparents. We can not lock
1836 * the parent because we hold on the child (pad) and the locking order is
1837 * parent >> child. */
1838 psrc = GST_OBJECT_PARENT (psrc);
1839 psink = GST_OBJECT_PARENT (psink);
1841 /* if they have grandparents but they are not the same */
1842 if (G_UNLIKELY (psrc != psink))
1843 goto wrong_grandparents;
1850 GST_CAT_DEBUG (GST_CAT_CAPS,
1851 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1852 GST_PTR_FORMAT, psrc, psink);
1857 GST_CAT_DEBUG (GST_CAT_CAPS,
1858 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1859 GST_PTR_FORMAT, psrc, psink);
1864 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1870 GST_CAT_DEBUG (GST_CAT_CAPS,
1871 "pads have different grandparents %" GST_PTR_FORMAT " and %"
1872 GST_PTR_FORMAT, psrc, psink);
1877 /* FIXME leftover from an attempt at refactoring... */
1878 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1879 * the two pads will be locked in the srcpad, sinkpad order. */
1880 static GstPadLinkReturn
1881 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
1883 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1884 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1886 GST_OBJECT_LOCK (srcpad);
1888 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1889 goto src_was_linked;
1891 GST_OBJECT_LOCK (sinkpad);
1893 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1894 goto sink_was_linked;
1896 /* check hierarchy, pads can only be linked if the grandparents
1898 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
1899 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
1900 goto wrong_hierarchy;
1902 /* check pad caps for non-empty intersection */
1903 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
1906 /* FIXME check pad scheduling for non-empty intersection */
1908 return GST_PAD_LINK_OK;
1912 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1913 GST_DEBUG_PAD_NAME (srcpad),
1914 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1915 /* we do not emit a warning in this case because unlinking cannot
1916 * be made MT safe.*/
1917 GST_OBJECT_UNLOCK (srcpad);
1918 return GST_PAD_LINK_WAS_LINKED;
1922 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1923 GST_DEBUG_PAD_NAME (sinkpad),
1924 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1925 /* we do not emit a warning in this case because unlinking cannot
1926 * be made MT safe.*/
1927 GST_OBJECT_UNLOCK (sinkpad);
1928 GST_OBJECT_UNLOCK (srcpad);
1929 return GST_PAD_LINK_WAS_LINKED;
1933 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1934 GST_OBJECT_UNLOCK (sinkpad);
1935 GST_OBJECT_UNLOCK (srcpad);
1936 return GST_PAD_LINK_WRONG_HIERARCHY;
1940 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1941 GST_OBJECT_UNLOCK (sinkpad);
1942 GST_OBJECT_UNLOCK (srcpad);
1943 return GST_PAD_LINK_NOFORMAT;
1949 * @srcpad: the source #GstPad.
1950 * @sinkpad: the sink #GstPad.
1952 * Checks if the source pad and the sink pad are compatible so they can be
1955 * Returns: TRUE if the pads can be linked.
1958 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1960 GstPadLinkReturn result;
1962 /* generic checks */
1963 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1964 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1966 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1967 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1969 /* gst_pad_link_prepare does everything for us, we only release the locks
1970 * on the pads that it gets us. If this function returns !OK the locks are not
1972 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
1973 if (result != GST_PAD_LINK_OK)
1976 GST_OBJECT_UNLOCK (srcpad);
1977 GST_OBJECT_UNLOCK (sinkpad);
1980 return result == GST_PAD_LINK_OK;
1984 * gst_pad_link_full:
1985 * @srcpad: the source #GstPad to link.
1986 * @sinkpad: the sink #GstPad to link.
1987 * @flags: the checks to validate when linking
1989 * Links the source pad and the sink pad.
1991 * This variant of #gst_pad_link provides a more granular control on the
1992 * checks being done when linking. While providing some considerable speedups
1993 * the caller of this method must be aware that wrong usage of those flags
1994 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
1995 * for more information.
1999 * Returns: A result code indicating if the connection worked or
2005 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2007 GstPadLinkReturn result;
2009 GstPadLinkFunction srcfunc, sinkfunc;
2011 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2012 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2013 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2014 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2015 GST_PAD_LINK_WRONG_DIRECTION);
2017 /* Notify the parent early. See gst_pad_unlink for details. */
2018 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2019 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2020 gst_element_post_message (parent,
2021 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2022 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2024 gst_object_unref (parent);
2029 /* prepare will also lock the two pads */
2030 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2032 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2035 /* must set peers before calling the link function */
2036 GST_PAD_PEER (srcpad) = sinkpad;
2037 GST_PAD_PEER (sinkpad) = srcpad;
2039 /* check events, when something is different, mark pending */
2040 schedule_events (srcpad, sinkpad);
2042 /* get the link functions */
2043 srcfunc = GST_PAD_LINKFUNC (srcpad);
2044 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2046 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2047 /* custom link functions, execute them */
2048 GST_OBJECT_UNLOCK (sinkpad);
2049 GST_OBJECT_UNLOCK (srcpad);
2052 /* this one will call the peer link function */
2053 result = srcfunc (srcpad, sinkpad);
2054 } else if (sinkfunc) {
2055 /* if no source link function, we need to call the sink link
2056 * function ourselves. */
2057 result = sinkfunc (sinkpad, srcpad);
2060 GST_OBJECT_LOCK (srcpad);
2061 GST_OBJECT_LOCK (sinkpad);
2063 /* we released the lock, check if the same pads are linked still */
2064 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2065 goto concurrent_link;
2067 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2070 GST_OBJECT_UNLOCK (sinkpad);
2071 GST_OBJECT_UNLOCK (srcpad);
2073 /* fire off a signal to each of the pads telling them
2074 * that they've been linked */
2075 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2076 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2078 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2079 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2081 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2084 if (G_LIKELY (parent)) {
2085 gst_element_post_message (parent,
2086 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2087 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2088 gst_object_unref (parent);
2096 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2097 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2098 GST_OBJECT_UNLOCK (sinkpad);
2099 GST_OBJECT_UNLOCK (srcpad);
2101 /* The other link operation succeeded first */
2102 result = GST_PAD_LINK_WAS_LINKED;
2107 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2108 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2110 GST_PAD_PEER (srcpad) = NULL;
2111 GST_PAD_PEER (sinkpad) = NULL;
2113 GST_OBJECT_UNLOCK (sinkpad);
2114 GST_OBJECT_UNLOCK (srcpad);
2122 * @srcpad: the source #GstPad to link.
2123 * @sinkpad: the sink #GstPad to link.
2125 * Links the source pad and the sink pad.
2127 * Returns: A result code indicating if the connection worked or
2133 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2135 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2139 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2141 GstPadTemplate **template_p;
2143 /* this function would need checks if it weren't static */
2145 GST_OBJECT_LOCK (pad);
2146 template_p = &pad->padtemplate;
2147 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2148 GST_OBJECT_UNLOCK (pad);
2151 gst_pad_template_pad_created (templ, pad);
2155 * gst_pad_get_pad_template:
2158 * Gets the template for @pad.
2160 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2161 * instantiated, or %NULL if this pad has no template. Unref after
2165 gst_pad_get_pad_template (GstPad * pad)
2167 GstPadTemplate *templ;
2169 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2171 templ = GST_PAD_PAD_TEMPLATE (pad);
2173 return (templ ? gst_object_ref (templ) : NULL);
2177 * gst_pad_has_current_caps:
2178 * @pad: a #GstPad to check
2180 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2182 * Returns: TRUE when @pad has caps associated with it.
2185 gst_pad_has_current_caps (GstPad * pad)
2190 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2192 GST_OBJECT_LOCK (pad);
2193 caps = get_pad_caps (pad);
2194 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2195 "check current pad caps %" GST_PTR_FORMAT, caps);
2196 result = (caps != NULL);
2197 GST_OBJECT_UNLOCK (pad);
2203 * gst_pad_get_current_caps:
2204 * @pad: a #GstPad to get the current capabilities of.
2206 * Gets the capabilities currently configured on @pad with the last
2207 * #GST_EVENT_CAPS event.
2209 * Returns: the current caps of the pad with incremented ref-count.
2212 gst_pad_get_current_caps (GstPad * pad)
2216 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2218 GST_OBJECT_LOCK (pad);
2219 if ((result = get_pad_caps (pad)))
2220 gst_caps_ref (result);
2221 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2222 "get current pad caps %" GST_PTR_FORMAT, result);
2223 GST_OBJECT_UNLOCK (pad);
2230 * @pad: a #GstPad to set the capabilities of.
2231 * @caps: (transfer none): a #GstCaps to set.
2233 * Sets the capabilities of this pad. The caps must be fixed. Any previous
2234 * caps on the pad will be unreffed. This function refs the caps so you should
2235 * unref if as soon as you don't need it anymore.
2236 * It is possible to set NULL caps, which will make the pad unnegotiated
2239 * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2240 * or bad parameters were provided to this function.
2245 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2248 gboolean res = TRUE;
2250 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2251 g_return_val_if_fail (caps != NULL && gst_caps_is_fixed (caps), FALSE);
2253 event = gst_event_new_caps (caps);
2255 if (GST_PAD_IS_SRC (pad))
2256 res = gst_pad_push_event (pad, event);
2258 res = gst_pad_send_event (pad, event);
2264 * gst_pad_get_pad_template_caps:
2265 * @pad: a #GstPad to get the template capabilities from.
2267 * Gets the capabilities for @pad's template.
2269 * Returns: (transfer full): the #GstCaps of this pad template.
2270 * Unref after usage.
2273 gst_pad_get_pad_template_caps (GstPad * pad)
2275 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2277 if (GST_PAD_PAD_TEMPLATE (pad))
2278 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2280 return gst_caps_ref (GST_CAPS_ANY);
2285 * @pad: a #GstPad to get the peer of.
2287 * Gets the peer of @pad. This function refs the peer pad so
2288 * you need to unref it after use.
2290 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2295 gst_pad_get_peer (GstPad * pad)
2299 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2301 GST_OBJECT_LOCK (pad);
2302 result = GST_PAD_PEER (pad);
2304 gst_object_ref (result);
2305 GST_OBJECT_UNLOCK (pad);
2311 * gst_pad_get_allowed_caps:
2314 * Gets the capabilities of the allowed media types that can flow through
2315 * @pad and its peer.
2317 * The allowed capabilities is calculated as the intersection of the results of
2318 * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2319 * on the resulting caps.
2321 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2322 * caps when you no longer need it. This function returns NULL when @pad
2328 gst_pad_get_allowed_caps (GstPad * pad)
2335 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2337 GST_OBJECT_LOCK (pad);
2338 peer = GST_PAD_PEER (pad);
2339 if (G_UNLIKELY (peer == NULL))
2342 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2344 gst_object_ref (peer);
2345 GST_OBJECT_UNLOCK (pad);
2346 mycaps = gst_pad_query_caps (pad, NULL);
2348 peercaps = gst_pad_query_caps (peer, NULL);
2349 gst_object_unref (peer);
2351 caps = gst_caps_intersect (mycaps, peercaps);
2352 gst_caps_unref (peercaps);
2353 gst_caps_unref (mycaps);
2355 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2362 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2363 GST_OBJECT_UNLOCK (pad);
2370 * gst_pad_iterate_internal_links_default:
2371 * @pad: the #GstPad to get the internal links of.
2372 * @parent: the parent of @pad or NULL
2374 * Iterate the list of pads to which the given pad is linked to inside of
2375 * the parent element.
2376 * This is the default handler, and thus returns an iterator of all of the
2377 * pads inside the parent element with opposite direction.
2379 * The caller must free this iterator after use with gst_iterator_free().
2381 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2382 * returned pad with gst_object_unref().
2387 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2394 GstElement *eparent;
2396 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2398 if (parent != NULL && GST_IS_ELEMENT (parent)) {
2399 eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2401 GST_OBJECT_LOCK (pad);
2402 eparent = GST_PAD_PARENT (pad);
2403 if (!eparent || !GST_IS_ELEMENT (eparent))
2406 gst_object_ref (eparent);
2407 GST_OBJECT_UNLOCK (pad);
2410 if (pad->direction == GST_PAD_SRC)
2411 padlist = &eparent->sinkpads;
2413 padlist = &eparent->srcpads;
2415 GST_DEBUG_OBJECT (pad, "Making iterator");
2417 cookie = &eparent->pads_cookie;
2419 lock = GST_OBJECT_GET_LOCK (eparent);
2421 res = gst_iterator_new_list (GST_TYPE_PAD,
2422 lock, cookie, padlist, (GObject *) owner, NULL);
2424 gst_object_unref (owner);
2431 GST_OBJECT_UNLOCK (pad);
2432 GST_DEBUG_OBJECT (pad, "no parent element");
2438 * gst_pad_iterate_internal_links:
2439 * @pad: the GstPad to get the internal links of.
2441 * Gets an iterator for the pads to which the given pad is linked to inside
2442 * of the parent element.
2444 * Each #GstPad element yielded by the iterator will have its refcount increased,
2445 * so unref after use.
2447 * Free-function: gst_iterator_free
2449 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
2450 * pad does not have an iterator function configured. Use
2451 * gst_iterator_free() after usage.
2456 gst_pad_iterate_internal_links (GstPad * pad)
2458 GstIterator *res = NULL;
2461 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2463 GST_OBJECT_LOCK (pad);
2464 ACQUIRE_PARENT (pad, parent, no_parent);
2465 GST_OBJECT_UNLOCK (pad);
2467 if (GST_PAD_ITERINTLINKFUNC (pad))
2468 res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2470 RELEASE_PARENT (parent);
2477 GST_DEBUG_OBJECT (pad, "no parent");
2478 GST_OBJECT_UNLOCK (pad);
2486 * @forward: a #GstPadForwardFunction
2487 * @user_data: user data passed to @forward
2489 * Calls @forward for all internally linked pads of @pad. This function deals with
2490 * dynamically changing internal pads and will make sure that the @forward
2491 * function is only called once for each pad.
2493 * When @forward returns TRUE, no further pads will be processed.
2495 * Returns: TRUE if one of the dispatcher functions returned TRUE.
2498 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2501 gboolean result = FALSE;
2503 gboolean done = FALSE;
2504 GValue item = { 0, };
2505 GList *pushed_pads = NULL;
2507 iter = gst_pad_iterate_internal_links (pad);
2513 switch (gst_iterator_next (iter, &item)) {
2514 case GST_ITERATOR_OK:
2518 intpad = g_value_get_object (&item);
2520 /* if already pushed, skip. FIXME, find something faster to tag pads */
2521 if (intpad == NULL || g_list_find (pushed_pads, intpad)) {
2522 g_value_reset (&item);
2526 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2527 GST_DEBUG_PAD_NAME (intpad));
2528 done = result = forward (intpad, user_data);
2530 pushed_pads = g_list_prepend (pushed_pads, intpad);
2532 g_value_reset (&item);
2535 case GST_ITERATOR_RESYNC:
2536 /* We don't reset the result here because we don't push the event
2537 * again on pads that got the event already and because we need
2538 * to consider the result of the previous pushes */
2539 gst_iterator_resync (iter);
2541 case GST_ITERATOR_ERROR:
2542 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2545 case GST_ITERATOR_DONE:
2550 g_value_unset (&item);
2551 gst_iterator_free (iter);
2553 g_list_free (pushed_pads);
2563 gboolean dispatched;
2567 event_forward_func (GstPad * pad, EventData * data)
2569 /* for each pad we send to, we should ref the event; it's up
2570 * to downstream to unref again when handled. */
2571 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2572 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2574 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2576 data->dispatched = TRUE;
2583 * gst_pad_event_default:
2584 * @pad: a #GstPad to call the default event handler on.
2585 * @parent: the parent of @pad or NULL
2586 * @event: (transfer full): the #GstEvent to handle.
2588 * Invokes the default event handler for the given pad.
2590 * The EOS event will pause the task associated with @pad before it is forwarded
2591 * to all internally linked pads,
2593 * The the event is sent to all pads internally linked to @pad. This function
2594 * takes ownership of @event.
2596 * Returns: TRUE if the event was sent successfully.
2599 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2601 gboolean result, forward = TRUE;
2603 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2604 g_return_val_if_fail (event != NULL, FALSE);
2606 GST_LOG_OBJECT (pad, "default event handler");
2608 switch (GST_EVENT_TYPE (event)) {
2611 GST_DEBUG_OBJECT (pad, "pausing task because of eos");
2612 gst_pad_pause_task (pad);
2615 case GST_EVENT_CAPS:
2616 forward = GST_PAD_IS_PROXY_CAPS (pad);
2627 data.dispatched = FALSE;
2628 data.result = FALSE;
2630 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
2632 /* for sinkpads without a parent element or without internal links, nothing
2633 * will be dispatched but we still want to return TRUE. */
2634 if (data.dispatched)
2635 result = data.result;
2640 gst_event_unref (event);
2645 /* Default accept caps implementation just checks against
2646 * the allowed caps for the pad */
2648 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
2650 /* get the caps and see if it intersects to something not empty */
2651 GstCaps *caps, *allowed;
2654 GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
2656 /* first forward the query to internally linked pads when we are dealing with
2658 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2659 if ((result = gst_pad_proxy_query_accept_caps (pad, query))) {
2664 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pad,
2665 "fallback ACCEPT_CAPS query, consider implementing a specialized version");
2667 allowed = gst_pad_query_caps (pad, NULL);
2668 gst_query_parse_accept_caps (query, &caps);
2671 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2672 result = gst_caps_is_subset (caps, allowed);
2673 gst_caps_unref (allowed);
2675 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2678 gst_query_set_accept_caps_result (query, result);
2684 /* Default caps implementation */
2686 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
2688 GstCaps *result = NULL, *filter;
2689 GstPadTemplate *templ;
2690 gboolean fixed_caps;
2692 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2694 gst_query_parse_caps (query, &filter);
2696 /* first try to proxy if we must */
2697 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2698 if ((gst_pad_proxy_query_caps (pad, query))) {
2699 gst_query_parse_caps_result (query, &result);
2704 /* no proxy or it failed, do default handling */
2705 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2707 GST_OBJECT_LOCK (pad);
2709 /* fixed caps, try the negotiated caps first */
2710 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
2711 if ((result = get_pad_caps (pad)))
2712 goto filter_done_unlock;
2715 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2716 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
2717 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
2718 goto filter_done_unlock;
2722 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2723 "non-fixed pad caps: trying pad caps");
2724 /* non fixed caps, try the negotiated caps */
2725 if ((result = get_pad_caps (pad)))
2726 goto filter_done_unlock;
2729 /* this almost never happens */
2730 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2731 result = GST_CAPS_ANY;
2734 GST_OBJECT_UNLOCK (pad);
2737 /* run the filter on the result */
2739 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2740 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
2741 GST_PTR_FORMAT, result, result, filter, filter);
2742 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2743 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2746 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2747 "using caps %p %" GST_PTR_FORMAT, result, result);
2748 result = gst_caps_ref (result);
2751 gst_query_set_caps_result (query, result);
2752 gst_caps_unref (result);
2758 * gst_pad_query_default:
2759 * @pad: a #GstPad to call the default query handler on.
2760 * @parent: the parent of @pad or NULL
2761 * @query: (transfer none): the #GstQuery to handle.
2763 * Invokes the default query handler for the given pad.
2764 * The query is sent to all pads internally linked to @pad. Note that
2765 * if there are many possible sink pads that are internally linked to
2766 * @pad, only one will be sent the query.
2767 * Multi-sinkpad elements should implement custom query handlers.
2769 * Returns: TRUE if the query was performed successfully.
2772 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
2774 gboolean forward, ret = FALSE;
2776 switch (GST_QUERY_TYPE (query)) {
2777 case GST_QUERY_SCHEDULING:
2780 case GST_QUERY_ALLOCATION:
2781 forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
2783 case GST_QUERY_ACCEPT_CAPS:
2784 ret = gst_pad_query_accept_caps_default (pad, query);
2787 case GST_QUERY_CAPS:
2788 ret = gst_pad_query_caps_default (pad, query);
2791 case GST_QUERY_POSITION:
2792 case GST_QUERY_SEEKING:
2793 case GST_QUERY_FORMATS:
2794 case GST_QUERY_LATENCY:
2795 case GST_QUERY_JITTER:
2796 case GST_QUERY_RATE:
2797 case GST_QUERY_CONVERT:
2804 ret = gst_pad_forward
2805 (pad, (GstPadForwardFunction) gst_pad_peer_query, query);
2811 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
2813 GstPad *pad = data->pad;
2814 GstPadProbeInfo *info = data->info;
2815 GstPadProbeType type, flags;
2816 GstPadProbeCallback callback;
2817 GstPadProbeReturn ret;
2819 /* if we have called this callback, do nothing */
2820 if (PROBE_COOKIE (hook) == data->cookie) {
2821 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2822 "hook %lu, cookie %u already called", hook->hook_id,
2823 PROBE_COOKIE (hook));
2827 PROBE_COOKIE (hook) = data->cookie;
2829 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
2832 /* one of the data types */
2833 if ((flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
2835 /* one of the scheduling types */
2836 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
2838 /* one of the blocking types must match */
2839 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
2840 (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
2842 /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
2843 if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
2844 (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
2847 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2848 "hook %lu, cookie %u with flags 0x%08x matches", hook->hook_id,
2849 PROBE_COOKIE (hook), flags);
2851 data->marshalled = TRUE;
2853 callback = (GstPadProbeCallback) hook->func;
2854 if (callback == NULL)
2857 info->id = hook->hook_id;
2859 GST_OBJECT_UNLOCK (pad);
2861 ret = callback (pad, info, hook->data);
2863 GST_OBJECT_LOCK (pad);
2866 case GST_PAD_PROBE_REMOVE:
2867 /* remove the probe */
2868 GST_DEBUG_OBJECT (pad, "asked to remove hook");
2869 cleanup_hook (pad, hook);
2871 case GST_PAD_PROBE_DROP:
2872 /* need to drop the data, make sure other probes don't get called
2874 GST_DEBUG_OBJECT (pad, "asked to drop item");
2875 info->type = GST_PAD_PROBE_TYPE_INVALID;
2876 data->dropped = TRUE;
2878 case GST_PAD_PROBE_PASS:
2879 /* inform the pad block to let things pass */
2880 GST_DEBUG_OBJECT (pad, "asked to pass item");
2884 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
2891 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2892 "hook %lu, cookie %u with flags 0x%08x does not match %08x",
2893 hook->hook_id, PROBE_COOKIE (hook), flags, info->type);
2898 #define PROBE_PRE_PULL(pad,mask,data,offs,size,label,probed,defaultval) \
2900 if (G_UNLIKELY (pad->num_probes)) { \
2901 /* we start with passing NULL as the data item */ \
2902 GstPadProbeInfo info = { mask, 0, NULL, offs, size }; \
2903 ret = do_probe_callbacks (pad, &info, defaultval); \
2904 /* store the possibly updated data item */ \
2905 data = GST_PAD_PROBE_INFO_DATA (&info); \
2906 /* if something went wrong, exit */ \
2907 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2909 /* otherwise check if the probe retured data */ \
2910 if (G_UNLIKELY (data != NULL)) \
2916 /* a probe that does not take or return any data */
2917 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
2919 if (G_UNLIKELY (pad->num_probes)) { \
2920 /* pass NULL as the data item */ \
2921 GstPadProbeInfo info = { mask, 0, NULL, 0, 0 }; \
2922 ret = do_probe_callbacks (pad, &info, defaultval); \
2923 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2928 #define PROBE_FULL(pad,mask,data,offs,size,label,defaultval) \
2930 if (G_UNLIKELY (pad->num_probes)) { \
2931 GstPadProbeInfo info = { mask, 0, data, offs, size }; \
2932 ret = do_probe_callbacks (pad, &info, defaultval); \
2933 data = GST_PAD_PROBE_INFO_DATA (&info); \
2934 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
2939 #define PROBE_PUSH(pad,mask,data,label) \
2940 PROBE_FULL(pad, mask, data, -1, -1, label, GST_FLOW_OK);
2941 #define PROBE_PULL(pad,mask,data,offs,size,label) \
2942 PROBE_FULL(pad, mask, data, offs, size, label, GST_FLOW_OK);
2944 static GstFlowReturn
2945 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
2946 GstFlowReturn defaultval)
2955 data.marshalled = FALSE;
2956 data.dropped = FALSE;
2957 data.cookie = ++pad->priv->probe_cookie;
2960 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
2963 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2964 "do probes cookie %u", data.cookie);
2965 cookie = pad->priv->probe_list_cookie;
2967 g_hook_list_marshal (&pad->probes, TRUE,
2968 (GHookMarshaller) probe_hook_marshal, &data);
2970 /* if the list changed, call the new callbacks (they will not have their
2971 * cookie set to data.cookie */
2972 if (cookie != pad->priv->probe_list_cookie) {
2973 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2974 "probe list changed, restarting");
2978 /* the first item that dropped will stop the hooks and then we drop here */
2982 /* if no handler matched and we are blocking, let the item pass */
2983 if (!data.marshalled && is_block)
2986 /* At this point, all handlers returned either OK or PASS. If one handler
2987 * returned PASS, let the item pass */
2992 while (GST_PAD_IS_BLOCKED (pad)) {
2993 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2994 "we are blocked %d times", pad->num_blocked);
2996 /* we might have released the lock */
2997 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3000 /* now we block the streaming thread. It can be unlocked when we
3001 * deactivate the pad (which will also set the FLUSHING flag) or
3002 * when the pad is unblocked. A flushing event will also unblock
3003 * the pad after setting the FLUSHING flag. */
3004 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3005 "Waiting to be unblocked or set flushing");
3006 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3007 GST_PAD_BLOCK_WAIT (pad);
3008 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3009 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3011 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3021 GST_DEBUG_OBJECT (pad, "pad is flushing");
3022 return GST_FLOW_FLUSHING;
3026 GST_DEBUG_OBJECT (pad, "data is dropped");
3027 return GST_FLOW_CUSTOM_SUCCESS;
3031 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3032 GST_DEBUG_OBJECT (pad, "data is passed");
3040 * gst_pad_get_offset:
3043 * Get the offset applied to the running time of @pad. @pad has to be a source
3046 * Returns: the offset.
3049 gst_pad_get_offset (GstPad * pad)
3053 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3055 GST_OBJECT_LOCK (pad);
3056 result = pad->offset;
3057 GST_OBJECT_UNLOCK (pad);
3063 * gst_pad_set_offset:
3065 * @offset: the offset
3067 * Set the offset that will be applied to the running time of @pad.
3070 gst_pad_set_offset (GstPad * pad, gint64 offset)
3074 g_return_if_fail (GST_IS_PAD (pad));
3076 GST_OBJECT_LOCK (pad);
3077 /* if nothing changed, do nothing */
3078 if (pad->offset == offset)
3081 pad->offset = offset;
3082 GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3084 /* sinkpads will apply their offset the next time a segment
3085 * event is received. */
3086 if (GST_PAD_IS_SINK (pad))
3089 /* resend the last segment event on next buffer push */
3090 if ((ev = find_event_by_type (pad, GST_EVENT_SEGMENT, 0))) {
3091 ev->received = FALSE;
3092 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3096 GST_OBJECT_UNLOCK (pad);
3102 * @pad: a #GstPad to invoke the default query on.
3103 * @query: (transfer none): the #GstQuery to perform.
3105 * Dispatches a query to a pad. The query should have been allocated by the
3106 * caller via one of the type-specific allocation functions. The element that
3107 * the pad belongs to is responsible for filling the query with an appropriate
3108 * response, which should then be parsed with a type-specific query parsing
3111 * Again, the caller is responsible for both the allocation and deallocation of
3112 * the query structure.
3114 * Please also note that some queries might need a running pipeline to work.
3116 * Returns: TRUE if the query could be performed.
3119 gst_pad_query (GstPad * pad, GstQuery * query)
3123 GstPadQueryFunction func;
3124 GstPadProbeType type;
3127 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3128 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3130 GST_DEBUG_OBJECT (pad, "sending query %p (%s)", query,
3131 GST_QUERY_TYPE_NAME (query));
3133 if (GST_PAD_IS_SRC (pad))
3134 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3136 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3138 GST_OBJECT_LOCK (pad);
3139 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3140 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3141 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3143 ACQUIRE_PARENT (pad, parent, no_parent);
3144 GST_OBJECT_UNLOCK (pad);
3146 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3149 res = func (pad, parent, query);
3151 RELEASE_PARENT (parent);
3153 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3154 GST_QUERY_TYPE_NAME (query), res);
3159 GST_OBJECT_LOCK (pad);
3160 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3161 GST_OBJECT_UNLOCK (pad);
3168 GST_DEBUG_OBJECT (pad, "had no parent");
3169 GST_OBJECT_UNLOCK (pad);
3174 GST_DEBUG_OBJECT (pad, "had no query function");
3175 RELEASE_PARENT (parent);
3180 GST_DEBUG_OBJECT (pad, "query failed");
3185 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3186 GST_OBJECT_UNLOCK (pad);
3192 * gst_pad_peer_query:
3193 * @pad: a #GstPad to invoke the peer query on.
3194 * @query: (transfer none): the #GstQuery to perform.
3196 * Performs gst_pad_query() on the peer of @pad.
3198 * The caller is responsible for both the allocation and deallocation of
3199 * the query structure.
3201 * Returns: TRUE if the query could be performed. This function returns %FALSE
3202 * if @pad has no peer.
3207 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3210 GstPadProbeType type;
3214 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3215 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3217 if (GST_PAD_IS_SRC (pad))
3218 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3220 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3222 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3223 GST_QUERY_TYPE_NAME (query));
3225 GST_OBJECT_LOCK (pad);
3226 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3227 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3228 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3230 peerpad = GST_PAD_PEER (pad);
3231 if (G_UNLIKELY (peerpad == NULL))
3234 gst_object_ref (peerpad);
3235 GST_OBJECT_UNLOCK (pad);
3237 res = gst_pad_query (peerpad, query);
3239 gst_object_unref (peerpad);
3244 GST_OBJECT_LOCK (pad);
3245 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3246 GST_OBJECT_UNLOCK (pad);
3253 GST_WARNING_OBJECT (pad, "pad has no peer");
3254 GST_OBJECT_UNLOCK (pad);
3259 GST_DEBUG_OBJECT (pad, "query failed");
3264 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3265 GST_OBJECT_UNLOCK (pad);
3270 /**********************************************************************
3271 * Data passing functions
3275 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3277 GstFlowReturn *data = user_data;
3281 GST_DEBUG_OBJECT (pad, "event %s was already received",
3282 GST_EVENT_TYPE_NAME (ev->event));
3285 GST_OBJECT_UNLOCK (pad);
3287 *data = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3288 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, &stored);
3290 GST_OBJECT_LOCK (pad);
3291 return *data == GST_FLOW_OK;
3294 /* this is the chain function that does not perform the additional argument
3295 * checking for that little extra speed.
3297 static inline GstFlowReturn
3298 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3303 GST_PAD_STREAM_LOCK (pad);
3305 GST_OBJECT_LOCK (pad);
3306 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3309 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3312 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3314 PROBE_PUSH (pad, type, data, probe_stopped);
3316 parent = GST_OBJECT_PARENT (pad);
3317 GST_OBJECT_UNLOCK (pad);
3319 /* NOTE: we read the chainfunc unlocked.
3320 * we cannot hold the lock for the pad so we might send
3321 * the data to the wrong function. This is not really a
3322 * problem since functions are assigned at creation time
3323 * and don't change that often... */
3324 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3325 GstPadChainFunction chainfunc;
3327 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3330 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3331 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3332 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3334 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
3336 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3337 "called chainfunction &%s with buffer %p, returned %s",
3338 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3340 GstPadChainListFunction chainlistfunc;
3342 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3345 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3346 "calling chainlistfunction &%s",
3347 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3349 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
3351 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3352 "called chainlistfunction &%s, returned %s",
3353 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3356 GST_PAD_STREAM_UNLOCK (pad);
3363 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3364 "chaining, but pad was flushing");
3365 GST_OBJECT_UNLOCK (pad);
3366 GST_PAD_STREAM_UNLOCK (pad);
3367 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3368 return GST_FLOW_FLUSHING;
3372 g_critical ("chain on pad %s:%s but it was not in push mode",
3373 GST_DEBUG_PAD_NAME (pad));
3374 GST_OBJECT_UNLOCK (pad);
3375 GST_PAD_STREAM_UNLOCK (pad);
3376 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3377 return GST_FLOW_ERROR;
3381 GST_OBJECT_UNLOCK (pad);
3382 GST_PAD_STREAM_UNLOCK (pad);
3383 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3386 case GST_FLOW_CUSTOM_SUCCESS:
3387 GST_DEBUG_OBJECT (pad, "dropped buffer");
3391 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3398 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3399 g_critical ("chain on pad %s:%s but it has no chainfunction",
3400 GST_DEBUG_PAD_NAME (pad));
3401 GST_PAD_STREAM_UNLOCK (pad);
3402 return GST_FLOW_NOT_SUPPORTED;
3408 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3409 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3412 * Chain a buffer to @pad.
3414 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3416 * If the buffer type is not acceptable for @pad (as negotiated with a
3417 * preceeding GST_EVENT_CAPS event), this function returns
3418 * #GST_FLOW_NOT_NEGOTIATED.
3420 * The function proceeds calling the chain function installed on @pad (see
3421 * gst_pad_set_chain_function()) and the return value of that function is
3422 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3425 * In all cases, success or failure, the caller loses its reference to @buffer
3426 * after calling this function.
3428 * Returns: a #GstFlowReturn from the pad.
3433 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3435 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3436 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3437 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3439 return gst_pad_chain_data_unchecked (pad,
3440 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3443 static GstFlowReturn
3444 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
3445 GstBufferList * list)
3451 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3453 len = gst_buffer_list_length (list);
3456 for (i = 0; i < len; i++) {
3457 buffer = gst_buffer_list_get (list, i);
3459 gst_pad_chain_data_unchecked (pad,
3460 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3461 gst_buffer_ref (buffer));
3462 if (ret != GST_FLOW_OK)
3465 gst_buffer_list_unref (list);
3471 * gst_pad_chain_list:
3472 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3473 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3476 * Chain a bufferlist to @pad.
3478 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3480 * If @pad was not negotiated properly with a CAPS event, this function
3481 * returns #GST_FLOW_NOT_NEGOTIATED.
3483 * The function proceeds calling the chainlist function installed on @pad (see
3484 * gst_pad_set_chain_list_function()) and the return value of that function is
3485 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3486 * chainlist function.
3488 * In all cases, success or failure, the caller loses its reference to @list
3489 * after calling this function.
3493 * Returns: a #GstFlowReturn from the pad.
3498 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3500 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3501 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3502 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3504 return gst_pad_chain_data_unchecked (pad,
3505 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3508 static GstFlowReturn
3509 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3514 GST_OBJECT_LOCK (pad);
3515 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3518 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3521 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3522 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3524 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3527 events_foreach (pad, push_sticky, &ret);
3528 if (ret != GST_FLOW_OK)
3532 /* do block probes */
3533 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3535 /* do post-blocking probes */
3536 PROBE_PUSH (pad, type, data, probe_stopped);
3538 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3541 /* take ref to peer pad before releasing the lock */
3542 gst_object_ref (peer);
3544 GST_OBJECT_UNLOCK (pad);
3546 ret = gst_pad_chain_data_unchecked (peer, type, data);
3548 gst_object_unref (peer);
3550 GST_OBJECT_LOCK (pad);
3552 if (pad->priv->using == 0) {
3553 /* pad is not active anymore, trigger idle callbacks */
3554 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3555 probe_stopped, ret);
3557 GST_OBJECT_UNLOCK (pad);
3561 /* ERROR recovery here */
3565 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3566 "pushing, but pad was flushing");
3567 GST_OBJECT_UNLOCK (pad);
3568 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3569 return GST_FLOW_FLUSHING;
3573 g_critical ("pushing on pad %s:%s but it was not activated in push mode",
3574 GST_DEBUG_PAD_NAME (pad));
3575 GST_OBJECT_UNLOCK (pad);
3576 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3577 return GST_FLOW_ERROR;
3581 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3582 "error pushing events, return %s", gst_flow_get_name (ret));
3583 GST_OBJECT_UNLOCK (pad);
3584 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3589 GST_OBJECT_UNLOCK (pad);
3590 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3593 case GST_FLOW_CUSTOM_SUCCESS:
3594 GST_DEBUG_OBJECT (pad, "dropped buffer");
3598 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3605 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3606 "pushing, but it was not linked");
3607 GST_OBJECT_UNLOCK (pad);
3608 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3609 return GST_FLOW_NOT_LINKED;
3615 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3616 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3619 * Pushes a buffer to the peer of @pad.
3621 * This function will call installed block probes before triggering any
3622 * installed data probes.
3624 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3625 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3628 * In all cases, success or failure, the caller loses its reference to @buffer
3629 * after calling this function.
3631 * Returns: a #GstFlowReturn from the peer pad.
3636 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3638 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3639 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3640 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3642 return gst_pad_push_data (pad,
3643 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3647 * gst_pad_push_list:
3648 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3649 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3652 * Pushes a buffer list to the peer of @pad.
3654 * This function will call installed block probes before triggering any
3655 * installed data probes.
3657 * The function proceeds calling the chain function on the peer pad and returns
3658 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3659 * be returned. If the peer pad does not have any installed chainlist function
3660 * every group buffer of the list will be merged into a normal #GstBuffer and
3661 * chained via gst_pad_chain().
3663 * In all cases, success or failure, the caller loses its reference to @list
3664 * after calling this function.
3666 * Returns: a #GstFlowReturn from the peer pad.
3673 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3675 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3676 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3677 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3679 return gst_pad_push_data (pad,
3680 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3683 static GstFlowReturn
3684 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3685 GstBuffer ** buffer)
3688 GstPadGetRangeFunction getrangefunc;
3691 GST_PAD_STREAM_LOCK (pad);
3693 GST_OBJECT_LOCK (pad);
3694 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3697 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
3700 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3701 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3703 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3706 events_foreach (pad, push_sticky, &ret);
3707 if (ret != GST_FLOW_OK)
3711 /* when one of the probes returns a buffer, probed_data will be called and we
3712 * skip calling the getrange function */
3713 PROBE_PRE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3714 *buffer, offset, size, probe_stopped, probed_data, GST_FLOW_OK);
3716 ACQUIRE_PARENT (pad, parent, no_parent);
3717 GST_OBJECT_UNLOCK (pad);
3719 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
3722 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3723 "calling getrangefunc %s, offset %"
3724 G_GUINT64_FORMAT ", size %u",
3725 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
3727 ret = getrangefunc (pad, parent, offset, size, buffer);
3729 RELEASE_PARENT (parent);
3731 if (G_UNLIKELY (ret != GST_FLOW_OK))
3732 goto get_range_failed;
3734 /* can only fire the signal if we have a valid buffer */
3735 GST_OBJECT_LOCK (pad);
3737 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3738 *buffer, offset, size, probe_stopped_unref);
3739 GST_OBJECT_UNLOCK (pad);
3741 GST_PAD_STREAM_UNLOCK (pad);
3748 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3749 "getrange, but pad was flushing");
3750 GST_OBJECT_UNLOCK (pad);
3751 GST_PAD_STREAM_UNLOCK (pad);
3752 return GST_FLOW_FLUSHING;
3756 g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
3757 GST_DEBUG_PAD_NAME (pad));
3758 GST_OBJECT_UNLOCK (pad);
3759 GST_PAD_STREAM_UNLOCK (pad);
3760 return GST_FLOW_ERROR;
3764 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
3765 GST_OBJECT_UNLOCK (pad);
3766 GST_PAD_STREAM_UNLOCK (pad);
3771 GST_DEBUG_OBJECT (pad, "no parent");
3772 GST_OBJECT_UNLOCK (pad);
3773 GST_PAD_STREAM_UNLOCK (pad);
3774 return GST_FLOW_FLUSHING;
3778 g_critical ("getrange on pad %s:%s but it has no getrangefunction",
3779 GST_DEBUG_PAD_NAME (pad));
3780 RELEASE_PARENT (parent);
3781 GST_PAD_STREAM_UNLOCK (pad);
3782 return GST_FLOW_NOT_SUPPORTED;
3786 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3787 "probe returned %s", gst_flow_get_name (ret));
3788 GST_OBJECT_UNLOCK (pad);
3789 GST_PAD_STREAM_UNLOCK (pad);
3792 probe_stopped_unref:
3794 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3795 "probe returned %s", gst_flow_get_name (ret));
3796 GST_OBJECT_UNLOCK (pad);
3797 GST_PAD_STREAM_UNLOCK (pad);
3798 gst_buffer_unref (*buffer);
3804 GST_PAD_STREAM_UNLOCK (pad);
3806 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3807 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3808 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
3814 * gst_pad_get_range:
3815 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
3816 * @offset: The start offset of the buffer
3817 * @size: The length of the buffer
3818 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
3819 * returns #GST_FLOW_ERROR if %NULL.
3821 * When @pad is flushing this function returns #GST_FLOW_FLUSHING
3822 * immediately and @buffer is %NULL.
3824 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
3825 * description of a getrange function. If @pad has no getrange function
3826 * installed (see gst_pad_set_getrange_function()) this function returns
3827 * #GST_FLOW_NOT_SUPPORTED.
3829 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
3831 * Returns: a #GstFlowReturn from the pad.
3836 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
3837 GstBuffer ** buffer)
3839 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3840 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3841 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3843 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
3847 * gst_pad_pull_range:
3848 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3849 * @offset: The start offset of the buffer
3850 * @size: The length of the buffer
3851 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
3852 * GST_FLOW_ERROR if %NULL.
3854 * Pulls a @buffer from the peer pad.
3856 * This function will first trigger the pad block signal if it was
3859 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
3860 * function returns the result of gst_pad_get_range() on the peer pad.
3861 * See gst_pad_get_range() for a list of return values and for the
3862 * semantics of the arguments of this function.
3864 * Returns: a #GstFlowReturn from the peer pad.
3865 * When this function returns #GST_FLOW_OK, @buffer will contain a valid
3866 * #GstBuffer that should be freed with gst_buffer_unref() after usage.
3867 * @buffer may not be used or freed when any other return value than
3868 * #GST_FLOW_OK is returned.
3873 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
3874 GstBuffer ** buffer)
3879 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3880 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3881 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3883 GST_OBJECT_LOCK (pad);
3884 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3887 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
3890 /* when one of the probes returns a buffer, probed_data will be called and we
3891 * skip calling the peer getrange function */
3892 PROBE_PRE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
3893 *buffer, offset, size, pre_probe_stopped, probed_data, GST_FLOW_OK);
3895 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3898 gst_object_ref (peer);
3900 GST_OBJECT_UNLOCK (pad);
3902 ret = gst_pad_get_range_unchecked (peer, offset, size, buffer);
3904 gst_object_unref (peer);
3906 GST_OBJECT_LOCK (pad);
3908 if (pad->priv->using == 0) {
3909 /* pad is not active anymore, trigger idle callbacks */
3910 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
3911 post_probe_stopped, ret);
3914 if (G_UNLIKELY (ret != GST_FLOW_OK))
3915 goto pull_range_failed;
3918 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
3919 *buffer, offset, size, post_probe_stopped);
3921 GST_OBJECT_UNLOCK (pad);
3925 /* ERROR recovery here */
3928 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3929 "pullrange, but pad was flushing");
3930 GST_OBJECT_UNLOCK (pad);
3931 return GST_FLOW_FLUSHING;
3935 g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
3936 GST_DEBUG_PAD_NAME (pad));
3937 GST_OBJECT_UNLOCK (pad);
3938 return GST_FLOW_ERROR;
3942 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
3943 gst_flow_get_name (ret));
3944 GST_OBJECT_UNLOCK (pad);
3949 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3950 "pulling range, but it was not linked");
3951 GST_OBJECT_UNLOCK (pad);
3952 return GST_FLOW_NOT_LINKED;
3957 GST_OBJECT_UNLOCK (pad);
3958 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
3959 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
3960 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
3965 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3966 "post probe returned %s", gst_flow_get_name (ret));
3967 GST_OBJECT_UNLOCK (pad);
3968 if (ret == GST_FLOW_OK)
3969 gst_buffer_unref (*buffer);
3976 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event, gboolean locked)
3981 gboolean res = FALSE;
3982 const gchar *name = NULL;
3984 type = GST_EVENT_TYPE (event);
3985 if (type & GST_EVENT_TYPE_STICKY_MULTI)
3986 name = gst_structure_get_name (gst_event_get_structure (event));
3988 events = pad->priv->events;
3991 for (i = 0; i < len; i++) {
3992 PadEvent *ev = &g_array_index (events, PadEvent, i);
3994 if (ev->event == NULL)
3997 if (type == GST_EVENT_TYPE (ev->event)) {
3998 /* matching types, check matching name if needed */
3999 if (name && !gst_event_has_name (ev->event, name))
4003 if ((res = gst_event_replace (&ev->event, event)))
4004 ev->received = FALSE;
4010 ev.event = gst_event_ref (event);
4011 ev.received = FALSE;
4012 g_array_append_val (events, ev);
4017 pad->priv->events_cookie++;
4018 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4020 GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4022 switch (GST_EVENT_TYPE (event)) {
4023 case GST_EVENT_CAPS:
4025 GST_OBJECT_UNLOCK (pad);
4027 GST_DEBUG_OBJECT (pad, "notify caps");
4028 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4031 GST_OBJECT_LOCK (pad);
4040 static GstFlowReturn
4041 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
4042 GstPadProbeType type, gboolean * stored)
4046 GstEventType event_type;
4049 sticky = GST_EVENT_IS_STICKY (event);
4051 GST_OBJECT_LOCK (pad);
4053 /* Two checks to be made:
4054 * . (un)set the FLUSHING flag for flushing events,
4055 * . handle pad blocking */
4056 event_type = GST_EVENT_TYPE (event);
4058 switch (event_type) {
4059 case GST_EVENT_FLUSH_START:
4060 GST_PAD_SET_FLUSHING (pad);
4062 GST_PAD_BLOCK_BROADCAST (pad);
4063 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4065 case GST_EVENT_FLUSH_STOP:
4066 GST_PAD_UNSET_FLUSHING (pad);
4068 /* Remove sticky EOS events */
4069 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4070 remove_event_by_type (pad, GST_EVENT_EOS);
4072 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4076 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4079 /* store the event on the pad, but only on srcpads. We always store the
4080 * event exactly how it was sent */
4082 /* srcpad sticky events are store immediately, the received flag is set
4083 * to FALSE and will be set to TRUE when we can successfully push the
4084 * event to the peer pad */
4085 if (gst_pad_store_sticky_event (pad, event, TRUE)) {
4086 GST_DEBUG_OBJECT (pad, "event %s updated",
4087 GST_EVENT_TYPE_NAME (event));
4092 switch (GST_EVENT_TYPE (event)) {
4093 case GST_EVENT_SEGMENT:
4094 /* pass the adjusted segment event on. We need to do this even if
4095 * there is no peer pad because of the probes. */
4096 event = apply_pad_offset (pad, event);
4098 case GST_EVENT_RECONFIGURE:
4099 if (GST_PAD_IS_SINK (pad))
4100 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4105 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4106 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4111 /* send probes after modifying the events above */
4112 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4114 /* now check the peer pad */
4115 peerpad = GST_PAD_PEER (pad);
4116 if (peerpad == NULL)
4119 gst_object_ref (peerpad);
4121 GST_OBJECT_UNLOCK (pad);
4123 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4124 event, GST_EVENT_TYPE_NAME (event), peerpad);
4126 ret = gst_pad_send_event_unchecked (peerpad, event, type);
4128 /* Note: we gave away ownership of the event at this point but we can still
4129 * print the old pointer */
4130 GST_LOG_OBJECT (pad,
4131 "sent event %p to peerpad %" GST_PTR_FORMAT ", ret %s", event, peerpad,
4132 gst_flow_get_name (ret));
4134 gst_object_unref (peerpad);
4136 GST_OBJECT_LOCK (pad);
4138 if (ret == GST_FLOW_OK) {
4141 if ((ev = find_event (pad, event)))
4142 ev->received = TRUE;
4144 GST_DEBUG_OBJECT (pad, "event marked received");
4146 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4147 GST_DEBUG_OBJECT (pad, "mark pending events");
4151 if (pad->priv->using == 0) {
4152 /* pad is not active anymore, trigger idle callbacks */
4153 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4154 idle_probe_stopped, ret);
4156 GST_OBJECT_UNLOCK (pad);
4160 /* ERROR handling */
4163 GST_DEBUG_OBJECT (pad, "We're flushing");
4164 GST_OBJECT_UNLOCK (pad);
4165 gst_event_unref (event);
4166 return GST_FLOW_FLUSHING;
4170 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4171 GST_OBJECT_UNLOCK (pad);
4172 gst_event_unref (event);
4175 case GST_FLOW_CUSTOM_SUCCESS:
4176 GST_DEBUG_OBJECT (pad, "dropped event");
4180 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4187 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4188 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4189 GST_OBJECT_UNLOCK (pad);
4190 gst_event_unref (event);
4191 return sticky ? GST_FLOW_OK : GST_FLOW_NOT_LINKED;
4195 GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
4196 GST_OBJECT_UNLOCK (pad);
4202 * gst_pad_push_event:
4203 * @pad: a #GstPad to push the event to.
4204 * @event: (transfer full): the #GstEvent to send to the pad.
4206 * Sends the event to the peer of the given pad. This function is
4207 * mainly used by elements to send events to their peer
4210 * This function takes owership of the provided event so you should
4211 * gst_event_ref() it if you want to reuse the event after this call.
4213 * Returns: TRUE if the event was handled.
4218 gst_pad_push_event (GstPad * pad, GstEvent * event)
4221 GstPadProbeType type;
4224 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4225 g_return_val_if_fail (event != NULL, FALSE);
4226 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4228 if (GST_PAD_IS_SRC (pad)) {
4229 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4230 goto wrong_direction;
4231 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4232 } else if (GST_PAD_IS_SINK (pad)) {
4233 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4234 goto wrong_direction;
4235 /* events pushed on sinkpad never are sticky */
4236 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4238 goto unknown_direction;
4240 if (gst_pad_push_event_unchecked (pad, event, type, &stored) != GST_FLOW_OK)
4241 res = stored ? TRUE : FALSE;
4247 /* ERROR handling */
4250 g_warning ("pad %s:%s pushing %s event in wrong direction",
4251 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4252 gst_event_unref (event);
4257 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4258 gst_event_unref (event);
4263 /* Check if we can call the event function with the given event */
4264 static GstFlowReturn
4265 pre_eventfunc_check (GstPad * pad, GstEvent * event)
4267 GstCaps *caps, *templ;
4269 switch (GST_EVENT_TYPE (event)) {
4270 case GST_EVENT_CAPS:
4272 /* backwards compatibility mode for caps */
4273 gst_event_parse_caps (event, &caps);
4275 /* See if pad accepts the caps */
4276 templ = gst_pad_get_pad_template_caps (pad);
4277 if (!gst_caps_is_subset (caps, templ))
4280 gst_caps_unref (templ);
4291 gst_caps_unref (templ);
4292 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4293 "caps %" GST_PTR_FORMAT " not accepted", caps);
4294 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4295 "no intersection with template %" GST_PTR_FORMAT, templ);
4296 return GST_FLOW_NOT_NEGOTIATED;
4300 static GstFlowReturn
4301 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
4302 GstPadProbeType type)
4305 GstEventType event_type;
4306 gboolean serialized, need_unlock = FALSE, sticky;
4307 GstPadEventFunction eventfunc;
4310 GST_OBJECT_LOCK (pad);
4311 if (GST_PAD_IS_SINK (pad))
4312 serialized = GST_EVENT_IS_SERIALIZED (event);
4315 sticky = GST_EVENT_IS_STICKY (event);
4316 event_type = GST_EVENT_TYPE (event);
4317 switch (event_type) {
4318 case GST_EVENT_FLUSH_START:
4319 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4320 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4322 /* can't even accept a flush begin event when flushing */
4323 if (GST_PAD_IS_FLUSHING (pad))
4326 GST_PAD_SET_FLUSHING (pad);
4327 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4329 case GST_EVENT_FLUSH_STOP:
4330 if (G_LIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_NONE)) {
4331 GST_PAD_UNSET_FLUSHING (pad);
4332 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4334 /* Remove pending EOS events */
4335 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4336 remove_event_by_type (pad, GST_EVENT_EOS);
4338 GST_OBJECT_UNLOCK (pad);
4339 /* grab stream lock */
4340 GST_PAD_STREAM_LOCK (pad);
4342 GST_OBJECT_LOCK (pad);
4343 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4346 case GST_EVENT_RECONFIGURE:
4347 if (GST_PAD_IS_SRC (pad))
4348 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4350 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4351 "have event type %" GST_PTR_FORMAT, event);
4353 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4357 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4358 GST_OBJECT_UNLOCK (pad);
4359 GST_PAD_STREAM_LOCK (pad);
4361 GST_OBJECT_LOCK (pad);
4362 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4366 switch (GST_EVENT_TYPE (event)) {
4367 case GST_EVENT_SEGMENT:
4368 event = apply_pad_offset (pad, event);
4374 /* now do the probe */
4376 type | GST_PAD_PROBE_TYPE_PUSH |
4377 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4379 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4383 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4386 ACQUIRE_PARENT (pad, parent, no_parent);
4387 GST_OBJECT_UNLOCK (pad);
4389 ret = pre_eventfunc_check (pad, event);
4390 if (G_UNLIKELY (ret != GST_FLOW_OK))
4391 goto precheck_failed;
4394 gst_event_ref (event);
4396 if (eventfunc (pad, parent, event)) {
4399 /* something went wrong */
4400 switch (event_type) {
4401 case GST_EVENT_CAPS:
4402 ret = GST_FLOW_NOT_NEGOTIATED;
4405 ret = GST_FLOW_ERROR;
4409 RELEASE_PARENT (parent);
4411 GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
4414 if (ret == GST_FLOW_OK) {
4415 /* after the event function accepted the event, we can store the sticky
4416 * event on the pad */
4417 gst_pad_store_sticky_event (pad, event, FALSE);
4419 gst_event_unref (event);
4423 GST_PAD_STREAM_UNLOCK (pad);
4427 /* ERROR handling */
4430 GST_OBJECT_UNLOCK (pad);
4432 GST_PAD_STREAM_UNLOCK (pad);
4433 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4434 "Received event on flushing pad. Discarding");
4435 gst_event_unref (event);
4436 return GST_FLOW_FLUSHING;
4440 GST_OBJECT_UNLOCK (pad);
4442 GST_PAD_STREAM_UNLOCK (pad);
4443 gst_event_unref (event);
4446 case GST_FLOW_CUSTOM_SUCCESS:
4447 GST_DEBUG_OBJECT (pad, "dropped event");
4451 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4458 g_warning ("pad %s:%s has no event handler, file a bug.",
4459 GST_DEBUG_PAD_NAME (pad));
4460 GST_OBJECT_UNLOCK (pad);
4462 GST_PAD_STREAM_UNLOCK (pad);
4463 gst_event_unref (event);
4464 return GST_FLOW_NOT_SUPPORTED;
4468 GST_DEBUG_OBJECT (pad, "no parent");
4469 GST_OBJECT_UNLOCK (pad);
4471 GST_PAD_STREAM_UNLOCK (pad);
4472 gst_event_unref (event);
4473 return GST_FLOW_FLUSHING;
4477 GST_DEBUG_OBJECT (pad, "pre event check failed");
4478 RELEASE_PARENT (parent);
4480 GST_PAD_STREAM_UNLOCK (pad);
4481 gst_event_unref (event);
4487 * gst_pad_send_event:
4488 * @pad: a #GstPad to send the event to.
4489 * @event: (transfer full): the #GstEvent to send to the pad.
4491 * Sends the event to the pad. This function can be used
4492 * by applications to send events in the pipeline.
4494 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4495 * sink pad, @event should be a downstream event. For example, you would not
4496 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4497 * Furthermore, some downstream events have to be serialized with data flow,
4498 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4499 * the event needs to be serialized with data flow, this function will take the
4500 * pad's stream lock while calling its event function.
4502 * To find out whether an event type is upstream, downstream, or downstream and
4503 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4504 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4505 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4506 * plugin doesn't need to bother itself with this information; the core handles
4507 * all necessary locks and checks.
4509 * This function takes owership of the provided event so you should
4510 * gst_event_ref() it if you want to reuse the event after this call.
4512 * Returns: TRUE if the event was handled.
4515 gst_pad_send_event (GstPad * pad, GstEvent * event)
4518 GstPadProbeType type;
4520 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4521 g_return_val_if_fail (event != NULL, FALSE);
4523 if (GST_PAD_IS_SINK (pad)) {
4524 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4525 goto wrong_direction;
4526 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4527 } else if (GST_PAD_IS_SRC (pad)) {
4528 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4529 goto wrong_direction;
4530 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4532 goto unknown_direction;
4534 if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
4541 /* ERROR handling */
4544 g_warning ("pad %s:%s sending %s event in wrong direction",
4545 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4546 gst_event_unref (event);
4551 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4552 gst_event_unref (event);
4558 * gst_pad_set_element_private:
4559 * @pad: the #GstPad to set the private data of.
4560 * @priv: The private data to attach to the pad.
4562 * Set the given private data gpointer on the pad.
4563 * This function can only be used by the element that owns the pad.
4564 * No locking is performed in this function.
4567 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4569 pad->element_private = priv;
4573 * gst_pad_get_element_private:
4574 * @pad: the #GstPad to get the private data of.
4576 * Gets the private data of a pad.
4577 * No locking is performed in this function.
4579 * Returns: (transfer none): a #gpointer to the private data.
4582 gst_pad_get_element_private (GstPad * pad)
4584 return pad->element_private;
4588 * gst_pad_get_sticky_event:
4589 * @pad: the #GstPad to get the event from.
4590 * @event_type: the #GstEventType that should be retrieved.
4591 * @idx: the index of the event
4593 * Returns a new reference of the sticky event of type @event_type
4596 * Returns: (transfer full): a #GstEvent of type @event_type or NULL when no
4597 * event of @event_type was on @pad. Unref after usage.
4600 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
4602 GstEvent *event = NULL;
4605 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4606 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
4608 GST_OBJECT_LOCK (pad);
4609 ev = find_event_by_type (pad, event_type, idx);
4610 if (ev && (event = ev->event))
4611 gst_event_ref (event);
4612 GST_OBJECT_UNLOCK (pad);
4619 GstPadStickyEventsForeachFunction func;
4624 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
4626 ForeachDispatch *data = user_data;
4629 GST_OBJECT_UNLOCK (pad);
4631 ret = data->func (pad, &ev->event, data->user_data);
4633 GST_OBJECT_LOCK (pad);
4639 * gst_pad_sticky_events_foreach:
4640 * @pad: the #GstPad that should be used for iteration.
4641 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
4642 * should be called for every event.
4643 * @user_data: (closure): the optional user data.
4645 * Iterates all sticky events on @pad and calls @foreach_func for every
4646 * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
4649 gst_pad_sticky_events_foreach (GstPad * pad,
4650 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
4652 ForeachDispatch data;
4654 g_return_if_fail (GST_IS_PAD (pad));
4655 g_return_if_fail (foreach_func != NULL);
4657 data.func = foreach_func;
4658 data.user_data = user_data;
4660 GST_OBJECT_LOCK (pad);
4661 events_foreach (pad, foreach_dispatch_function, &data);
4662 GST_OBJECT_UNLOCK (pad);
4666 do_stream_status (GstPad * pad, GstStreamStatusType type,
4667 GThread * thread, GstTask * task)
4671 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
4673 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
4674 if (GST_IS_ELEMENT (parent)) {
4675 GstMessage *message;
4676 GValue value = { 0 };
4678 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
4679 gchar *tname, *ename, *pname;
4681 /* create a good task name */
4682 ename = gst_element_get_name (parent);
4683 pname = gst_pad_get_name (pad);
4684 tname = g_strdup_printf ("%s:%s", ename, pname);
4688 gst_object_set_name (GST_OBJECT_CAST (task), tname);
4692 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
4695 g_value_init (&value, GST_TYPE_TASK);
4696 g_value_set_object (&value, task);
4697 gst_message_set_stream_status_object (message, &value);
4698 g_value_unset (&value);
4700 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
4701 gst_element_post_message (parent, message);
4703 gst_object_unref (parent);
4708 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
4710 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
4715 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
4717 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
4721 static GstTaskThreadCallbacks thr_callbacks = {
4727 * gst_pad_start_task:
4728 * @pad: the #GstPad to start the task of
4729 * @func: the task function to call
4730 * @data: data passed to the task function
4732 * Starts a task that repeatedly calls @func with @data. This function
4733 * is mostly used in pad activation functions to start the dataflow.
4734 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
4735 * before @func is called.
4737 * Returns: a %TRUE if the task could be started.
4740 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
4745 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4746 g_return_val_if_fail (func != NULL, FALSE);
4748 GST_DEBUG_OBJECT (pad, "start task");
4750 GST_OBJECT_LOCK (pad);
4751 task = GST_PAD_TASK (pad);
4753 task = gst_task_new (func, data);
4754 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
4755 gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
4756 GST_INFO_OBJECT (pad, "created task %p", task);
4757 GST_PAD_TASK (pad) = task;
4758 gst_object_ref (task);
4759 /* release lock to post the message */
4760 GST_OBJECT_UNLOCK (pad);
4762 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
4764 gst_object_unref (task);
4766 GST_OBJECT_LOCK (pad);
4767 /* nobody else is supposed to have changed the pad now */
4768 if (GST_PAD_TASK (pad) != task)
4769 goto concurrent_stop;
4771 res = gst_task_set_state (task, GST_TASK_STARTED);
4772 GST_OBJECT_UNLOCK (pad);
4779 GST_OBJECT_UNLOCK (pad);
4785 * gst_pad_pause_task:
4786 * @pad: the #GstPad to pause the task of
4788 * Pause the task of @pad. This function will also wait until the
4789 * function executed by the task is finished if this function is not
4790 * called from the task function.
4792 * Returns: a TRUE if the task could be paused or FALSE when the pad
4796 gst_pad_pause_task (GstPad * pad)
4801 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4803 GST_DEBUG_OBJECT (pad, "pause task");
4805 GST_OBJECT_LOCK (pad);
4806 task = GST_PAD_TASK (pad);
4809 res = gst_task_set_state (task, GST_TASK_PAUSED);
4810 GST_OBJECT_UNLOCK (pad);
4812 /* wait for task function to finish, this lock is recursive so it does nothing
4813 * when the pause is called from the task itself */
4814 GST_PAD_STREAM_LOCK (pad);
4815 GST_PAD_STREAM_UNLOCK (pad);
4821 GST_DEBUG_OBJECT (pad, "pad has no task");
4822 GST_OBJECT_UNLOCK (pad);
4828 * gst_pad_stop_task:
4829 * @pad: the #GstPad to stop the task of
4831 * Stop the task of @pad. This function will also make sure that the
4832 * function executed by the task will effectively stop if not called
4833 * from the GstTaskFunction.
4835 * This function will deadlock if called from the GstTaskFunction of
4836 * the task. Use gst_task_pause() instead.
4838 * Regardless of whether the pad has a task, the stream lock is acquired and
4839 * released so as to ensure that streaming through this pad has finished.
4841 * Returns: a TRUE if the task could be stopped or FALSE on error.
4844 gst_pad_stop_task (GstPad * pad)
4849 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4851 GST_DEBUG_OBJECT (pad, "stop task");
4853 GST_OBJECT_LOCK (pad);
4854 task = GST_PAD_TASK (pad);
4857 GST_PAD_TASK (pad) = NULL;
4858 res = gst_task_set_state (task, GST_TASK_STOPPED);
4859 GST_OBJECT_UNLOCK (pad);
4861 GST_PAD_STREAM_LOCK (pad);
4862 GST_PAD_STREAM_UNLOCK (pad);
4864 if (!gst_task_join (task))
4867 gst_object_unref (task);
4873 GST_DEBUG_OBJECT (pad, "no task");
4874 GST_OBJECT_UNLOCK (pad);
4876 GST_PAD_STREAM_LOCK (pad);
4877 GST_PAD_STREAM_UNLOCK (pad);
4879 /* this is not an error */
4884 /* this is bad, possibly the application tried to join the task from
4885 * the task's thread. We install the task again so that it will be stopped
4886 * again from the right thread next time hopefully. */
4887 GST_OBJECT_LOCK (pad);
4888 GST_DEBUG_OBJECT (pad, "join failed");
4889 /* we can only install this task if there was no other task */
4890 if (GST_PAD_TASK (pad) == NULL)
4891 GST_PAD_TASK (pad) = task;
4892 GST_OBJECT_UNLOCK (pad);