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, #GstQuery, #GstBuffer
28 * A #GstElement is linked to other elements via "pads", which are extremely
29 * light-weight generic link points.
31 * Pads have a #GstPadDirection, source pads produce data, sink pads consume
34 * Pads are typically created from a #GstPadTemplate with
35 * gst_pad_new_from_template() and are then added to a #GstElement. This usually
36 * happens when the element is created but it can also happen dynamically based
37 * on the data that the element is processing or based on the pads that the
38 * application requests.
40 * Pads without pad templates can be created with gst_pad_new(),
41 * which takes a direction and a name as an argument. If the name is NULL,
42 * then a guaranteed unique name will be assigned to it.
44 * A #GstElement creating a pad will typically use the various
45 * gst_pad_set_*_function() calls to register callbacks for events, queries or
46 * dataflow on the pads.
48 * gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
50 * After two pads are retrieved from an element with gst_element_get_pad(),
51 * the pads can be linked with gst_pad_link(). (For quick links,
52 * you can also use gst_element_link(), which will make the obvious
53 * link for you if it's straightforward.). Pads can be unlinked again with
54 * gst_pad_unlink(). gst_pad_get_peer() can be used to check what the pad is
57 * Before dataflow is possible on the pads, they need to be activated with
58 * gst_pad_set_active().
60 * gst_pad_query() and gst_pad_peer_query() can be used to query various
61 * properties of the pad and the stream.
63 * To send a #GstEvent on a pad, use gst_pad_send_event() and
64 * gst_pad_push_event(). Some events will be sticky on the pad, meaning that
65 * after they pass on the pad they can be queried later with
66 * gst_pad_get_sticky_event() and gst_pad_sticky_events_foreach().
67 * gst_pad_get_current_caps() and gst_pad_has_current_caps() are convenience
68 * functions to query the current sticky CAPS event on a pad.
70 * GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
71 * or pull in a buffer.
73 * The dataflow, events and queries that happen on a pad can be monitored with
74 * probes that can be installed with gst_pad_add_probe(). gst_pad_is_blocked()
75 * can be used to check if a block probe is installed on the pad.
76 * gst_pad_is_blocking() checks if the blocking probe is currently blocking the
77 * pad. gst_pad_remove_probe() is used to remove a previously installed probe
78 * and unblock blocking probes if any.
80 * Pad have an offset that can be retrieved with gst_pad_get_offset(). This
81 * offset will be applied to the running_time of all data passing over the pad.
82 * gst_pad_set_offset() can be used to change the offset.
84 * Convenience functions exist to start, pause and stop the task on a pad with
85 * gst_pad_start_task(), gst_pad_pause_task() and gst_pad_stop_task()
88 * Last reviewed on 2012-03-29 (0.11.3)
91 #include "gst_private.h"
94 #include "gstpadtemplate.h"
95 #include "gstenumtypes.h"
100 #include "glib-compat-private.h"
102 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
103 #define GST_CAT_DEFAULT GST_CAT_PADS
105 /* Pad signals and args */
123 #define GST_PAD_GET_PRIVATE(obj) \
124 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_PAD, GstPadPrivate))
126 /* we have a pending and an active event on the pad. On source pads only the
127 * active event is used. On sinkpads, events are copied to the pending entry and
128 * moved to the active event when the eventfunc returned TRUE. */
135 struct _GstPadPrivate
141 guint probe_list_cookie;
151 #define PROBE_COOKIE(h) (((GstProbe *)(h))->cookie)
156 GstPadProbeInfo *info;
163 static void gst_pad_dispose (GObject * object);
164 static void gst_pad_finalize (GObject * object);
165 static void gst_pad_set_property (GObject * object, guint prop_id,
166 const GValue * value, GParamSpec * pspec);
167 static void gst_pad_get_property (GObject * object, guint prop_id,
168 GValue * value, GParamSpec * pspec);
170 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
171 static gboolean gst_pad_activate_default (GstPad * pad, GstObject * parent);
172 static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
173 GstObject * parent, GstBufferList * list);
175 static GstFlowReturn gst_pad_send_event_unchecked (GstPad * pad,
176 GstEvent * event, GstPadProbeType type);
177 static GstFlowReturn gst_pad_push_event_unchecked (GstPad * pad,
178 GstEvent * event, GstPadProbeType type);
180 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
182 static GParamSpec *pspec_caps = NULL;
184 /* quarks for probe signals */
185 static GQuark buffer_quark;
186 static GQuark buffer_list_quark;
187 static GQuark event_quark;
196 static GstFlowQuarks flow_quarks[] = {
197 {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
198 {GST_FLOW_OK, "ok", 0},
199 {GST_FLOW_NOT_LINKED, "not-linked", 0},
200 {GST_FLOW_FLUSHING, "flushing", 0},
201 {GST_FLOW_EOS, "eos", 0},
202 {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
203 {GST_FLOW_ERROR, "error", 0},
204 {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
205 {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
210 * @ret: a #GstFlowReturn to get the name of.
212 * Gets a string representing the given flow return.
214 * Returns: a static string with the name of the flow return.
217 gst_flow_get_name (GstFlowReturn ret)
221 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
223 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
224 if (ret == flow_quarks[i].ret)
225 return flow_quarks[i].name;
232 * @ret: a #GstFlowReturn to get the quark of.
234 * Get the unique quark for the given GstFlowReturn.
236 * Returns: the quark associated with the flow return or 0 if an
237 * invalid return was specified.
240 gst_flow_to_quark (GstFlowReturn ret)
244 ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
246 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
247 if (ret == flow_quarks[i].ret)
248 return flow_quarks[i].quark;
257 buffer_quark = g_quark_from_static_string ("buffer"); \
258 buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
259 event_quark = g_quark_from_static_string ("event"); \
261 for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) { \
262 flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
265 GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
266 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
269 #define gst_pad_parent_class parent_class
270 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
273 gst_pad_class_init (GstPadClass * klass)
275 GObjectClass *gobject_class;
276 GstObjectClass *gstobject_class;
278 gobject_class = G_OBJECT_CLASS (klass);
279 gstobject_class = GST_OBJECT_CLASS (klass);
281 g_type_class_add_private (klass, sizeof (GstPadPrivate));
283 gobject_class->dispose = gst_pad_dispose;
284 gobject_class->finalize = gst_pad_finalize;
285 gobject_class->set_property = gst_pad_set_property;
286 gobject_class->get_property = gst_pad_get_property;
290 * @pad: the pad that emitted the signal
291 * @peer: the peer pad that has been connected
293 * Signals that a pad has been linked to the peer pad.
295 gst_pad_signals[PAD_LINKED] =
296 g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
297 G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
298 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
301 * @pad: the pad that emitted the signal
302 * @peer: the peer pad that has been disconnected
304 * Signals that a pad has been unlinked from the peer pad.
306 gst_pad_signals[PAD_UNLINKED] =
307 g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
308 G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
309 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
311 pspec_caps = g_param_spec_boxed ("caps", "Caps",
312 "The capabilities of the pad", GST_TYPE_CAPS,
313 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
314 g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);
316 g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
317 g_param_spec_enum ("direction", "Direction", "The direction of the pad",
318 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
319 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
321 /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
322 g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
323 g_param_spec_object ("template", "Template",
324 "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
325 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
327 gstobject_class->path_string_separator = ".";
329 /* Register common function pointer descriptions */
330 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
331 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
332 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
333 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
334 GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
338 gst_pad_init (GstPad * pad)
340 pad->priv = GST_PAD_GET_PRIVATE (pad);
342 GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
344 GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
345 GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
346 GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
347 GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
348 GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
350 GST_PAD_SET_FLUSHING (pad);
352 g_rec_mutex_init (&pad->stream_rec_lock);
354 g_cond_init (&pad->block_cond);
356 g_hook_list_init (&pad->probes, sizeof (GstProbe));
358 pad->priv->events = g_array_sized_new (FALSE, TRUE, sizeof (PadEvent), 16);
361 /* called when setting the pad inactive. It removes all sticky events from
364 remove_events (GstPad * pad)
369 events = pad->priv->events;
372 for (i = 0; i < len; i++) {
373 PadEvent *ev = &g_array_index (events, PadEvent, i);
374 gst_event_unref (ev->event);
376 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
377 g_array_set_size (events, 0);
378 pad->priv->events_cookie++;
382 find_event_by_type (GstPad * pad, GstEventType type, guint idx)
388 events = pad->priv->events;
391 for (i = 0; i < len; i++) {
392 ev = &g_array_index (events, PadEvent, i);
393 if (ev->event == NULL)
396 if (GST_EVENT_TYPE (ev->event) == type) {
408 find_event (GstPad * pad, GstEvent * event)
414 events = pad->priv->events;
417 for (i = 0; i < len; i++) {
418 ev = &g_array_index (events, PadEvent, i);
419 if (event == ev->event)
428 remove_event_by_type (GstPad * pad, GstEventType type)
434 events = pad->priv->events;
439 ev = &g_array_index (events, PadEvent, i);
440 if (ev->event == NULL)
443 if (GST_EVENT_TYPE (ev->event) != type)
446 gst_event_unref (ev->event);
447 g_array_remove_index (events, i);
449 pad->priv->events_cookie++;
457 /* check all events on srcpad against those on sinkpad. All events that are not
458 * on sinkpad are marked as received=FALSE and the PENDING_EVENTS is set on the
459 * srcpad so that the events will be sent next time */
461 schedule_events (GstPad * srcpad, GstPad * sinkpad)
466 gboolean pending = FALSE;
468 events = srcpad->priv->events;
471 for (i = 0; i < len; i++) {
472 ev = &g_array_index (events, PadEvent, i);
473 if (ev->event == NULL)
476 if (sinkpad == NULL || !find_event (sinkpad, ev->event)) {
477 ev->received = FALSE;
482 GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PENDING_EVENTS);
485 typedef gboolean (*PadEventFunction) (GstPad * pad, PadEvent * ev,
488 /* should be called with pad LOCK */
490 events_foreach (GstPad * pad, PadEventFunction func, gpointer user_data)
497 events = pad->priv->events;
500 cookie = pad->priv->events_cookie;
504 PadEvent *ev, ev_ret;
506 ev = &g_array_index (events, PadEvent, i);
507 if (G_UNLIKELY (ev->event == NULL))
510 /* take aditional ref, func might release the lock */
511 ev_ret.event = gst_event_ref (ev->event);
512 ev_ret.received = ev->received;
514 ret = func (pad, &ev_ret, user_data);
516 /* recheck the cookie, lock might have been released and the list could have
518 if (G_UNLIKELY (cookie != pad->priv->events_cookie)) {
519 if (G_LIKELY (ev_ret.event))
520 gst_event_unref (ev_ret.event);
524 /* store the received state */
525 ev->received = ev_ret.received;
527 /* if the event changed, we need to do something */
528 if (G_UNLIKELY (ev->event != ev_ret.event)) {
529 if (G_UNLIKELY (ev_ret.event == NULL)) {
530 /* function unreffed and set the event to NULL, remove it */
531 g_array_remove_index (events, i);
533 cookie = ++pad->priv->events_cookie;
536 /* function gave a new event for us */
537 gst_event_take (&ev->event, ev_ret.event);
540 /* just unref, nothing changed */
541 gst_event_unref (ev_ret.event);
550 /* should be called with LOCK */
552 apply_pad_offset (GstPad * pad, GstEvent * event)
554 /* check if we need to adjust the segment */
555 if (pad->offset != 0) {
558 /* copy segment values */
559 gst_event_copy_segment (event, &segment);
560 gst_event_unref (event);
562 /* adjust and make a new event with the offset applied */
563 segment.base += pad->offset;
564 event = gst_event_new_segment (&segment);
569 /* should be called with the OBJECT_LOCK */
571 get_pad_caps (GstPad * pad)
573 GstCaps *caps = NULL;
576 ev = find_event_by_type (pad, GST_EVENT_CAPS, 0);
578 gst_event_parse_caps (ev->event, &caps);
584 gst_pad_dispose (GObject * object)
586 GstPad *pad = GST_PAD_CAST (object);
589 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
591 /* unlink the peer pad */
592 if ((peer = gst_pad_get_peer (pad))) {
593 /* window for MT unsafeness, someone else could unlink here
594 * and then we call unlink with wrong pads. The unlink
595 * function would catch this and safely return failed. */
596 if (GST_PAD_IS_SRC (pad))
597 gst_pad_unlink (pad, peer);
599 gst_pad_unlink (peer, pad);
601 gst_object_unref (peer);
604 gst_pad_set_pad_template (pad, NULL);
608 g_hook_list_clear (&pad->probes);
610 G_OBJECT_CLASS (parent_class)->dispose (object);
614 gst_pad_finalize (GObject * object)
616 GstPad *pad = GST_PAD_CAST (object);
619 /* in case the task is still around, clean it up */
620 if ((task = GST_PAD_TASK (pad))) {
621 gst_task_join (task);
622 GST_PAD_TASK (pad) = NULL;
623 gst_object_unref (task);
626 if (pad->activatenotify)
627 pad->activatenotify (pad->activatedata);
628 if (pad->activatemodenotify)
629 pad->activatemodenotify (pad->activatemodedata);
631 pad->linknotify (pad->linkdata);
632 if (pad->unlinknotify)
633 pad->unlinknotify (pad->unlinkdata);
634 if (pad->chainnotify)
635 pad->chainnotify (pad->chaindata);
636 if (pad->chainlistnotify)
637 pad->chainlistnotify (pad->chainlistdata);
638 if (pad->getrangenotify)
639 pad->getrangenotify (pad->getrangedata);
640 if (pad->eventnotify)
641 pad->eventnotify (pad->eventdata);
642 if (pad->querynotify)
643 pad->querynotify (pad->querydata);
644 if (pad->iterintlinknotify)
645 pad->iterintlinknotify (pad->iterintlinkdata);
647 g_rec_mutex_clear (&pad->stream_rec_lock);
648 g_cond_clear (&pad->block_cond);
649 g_array_free (pad->priv->events, TRUE);
651 G_OBJECT_CLASS (parent_class)->finalize (object);
655 gst_pad_set_property (GObject * object, guint prop_id,
656 const GValue * value, GParamSpec * pspec)
658 g_return_if_fail (GST_IS_PAD (object));
661 case PAD_PROP_DIRECTION:
662 GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
664 case PAD_PROP_TEMPLATE:
665 gst_pad_set_pad_template (GST_PAD_CAST (object),
666 (GstPadTemplate *) g_value_get_object (value));
669 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
675 gst_pad_get_property (GObject * object, guint prop_id,
676 GValue * value, GParamSpec * pspec)
678 g_return_if_fail (GST_IS_PAD (object));
682 GST_OBJECT_LOCK (object);
683 g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
684 GST_OBJECT_UNLOCK (object);
686 case PAD_PROP_DIRECTION:
687 g_value_set_enum (value, GST_PAD_DIRECTION (object));
689 case PAD_PROP_TEMPLATE:
690 g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
693 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
700 * @name: the name of the new pad.
701 * @direction: the #GstPadDirection of the pad.
703 * Creates a new pad with the given name in the given direction.
704 * If name is NULL, a guaranteed unique name (across all pads)
706 * This function makes a copy of the name so you can safely free the name.
708 * Returns: (transfer floating): a new #GstPad, or NULL in case of an error.
713 gst_pad_new (const gchar * name, GstPadDirection direction)
715 return g_object_new (GST_TYPE_PAD,
716 "name", name, "direction", direction, NULL);
720 * gst_pad_new_from_template:
721 * @templ: the pad template to use
722 * @name: the name of the element
724 * Creates a new pad with the given name from the given template.
725 * If name is NULL, a guaranteed unique name (across all pads)
727 * This function makes a copy of the name so you can safely free the name.
729 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
732 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
734 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
736 return g_object_new (GST_TYPE_PAD,
737 "name", name, "direction", templ->direction, "template", templ, NULL);
741 * gst_pad_new_from_static_template:
742 * @templ: the #GstStaticPadTemplate to use
743 * @name: the name of the element
745 * Creates a new pad with the given name from the given static template.
746 * If name is NULL, a guaranteed unique name (across all pads)
748 * This function makes a copy of the name so you can safely free the name.
750 * Returns: (transfer full): a new #GstPad, or NULL in case of an error.
753 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
757 GstPadTemplate *template;
759 template = gst_static_pad_template_get (templ);
760 pad = gst_pad_new_from_template (template, name);
761 gst_object_unref (template);
765 #define ACQUIRE_PARENT(pad, parent, label) \
767 if (G_LIKELY ((parent = GST_OBJECT_PARENT (pad)))) \
768 gst_object_ref (parent); \
769 else if (G_LIKELY (GST_PAD_NEEDS_PARENT (pad))) \
773 #define RELEASE_PARENT(parent) \
775 if (G_LIKELY (parent)) \
776 gst_object_unref (parent); \
780 * gst_pad_get_direction:
781 * @pad: a #GstPad to get the direction of.
783 * Gets the direction of the pad. The direction of the pad is
784 * decided at construction time so this function does not take
787 * Returns: the #GstPadDirection of the pad.
792 gst_pad_get_direction (GstPad * pad)
794 GstPadDirection result;
796 /* PAD_UNKNOWN is a little silly but we need some sort of
797 * error return value */
798 g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
800 result = GST_PAD_DIRECTION (pad);
806 gst_pad_activate_default (GstPad * pad, GstObject * parent)
808 return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
812 * gst_pad_mode_get_name:
813 * @mode: the pad mode
815 * Return the name of a pad mode, for use in debug messages mostly.
817 * Returns: short mnemonic for pad mode @mode
820 gst_pad_mode_get_name (GstPadMode mode)
823 case GST_PAD_MODE_NONE:
825 case GST_PAD_MODE_PUSH:
827 case GST_PAD_MODE_PULL:
836 pre_activate (GstPad * pad, GstPadMode new_mode)
839 case GST_PAD_MODE_NONE:
840 GST_OBJECT_LOCK (pad);
841 GST_DEBUG_OBJECT (pad, "setting PAD_MODE NONE, set flushing");
842 GST_PAD_SET_FLUSHING (pad);
843 GST_PAD_MODE (pad) = new_mode;
844 /* unlock blocked pads so element can resume and stop */
845 GST_PAD_BLOCK_BROADCAST (pad);
846 GST_OBJECT_UNLOCK (pad);
848 case GST_PAD_MODE_PUSH:
849 case GST_PAD_MODE_PULL:
850 GST_OBJECT_LOCK (pad);
851 GST_DEBUG_OBJECT (pad, "setting pad into %s mode, unset flushing",
852 gst_pad_mode_get_name (new_mode));
853 GST_PAD_UNSET_FLUSHING (pad);
854 GST_PAD_MODE (pad) = new_mode;
855 if (GST_PAD_IS_SINK (pad)) {
857 /* make sure the peer src pad sends us all events */
858 if ((peer = GST_PAD_PEER (pad))) {
859 gst_object_ref (peer);
860 GST_OBJECT_UNLOCK (pad);
862 GST_DEBUG_OBJECT (pad, "reschedule events on peer %s:%s",
863 GST_DEBUG_PAD_NAME (peer));
865 GST_OBJECT_LOCK (peer);
866 schedule_events (peer, NULL);
867 GST_OBJECT_UNLOCK (peer);
869 gst_object_unref (peer);
871 GST_OBJECT_UNLOCK (pad);
874 GST_OBJECT_UNLOCK (pad);
881 post_activate (GstPad * pad, GstPadMode new_mode)
884 case GST_PAD_MODE_NONE:
885 /* ensures that streaming stops */
886 GST_PAD_STREAM_LOCK (pad);
887 GST_DEBUG_OBJECT (pad, "stopped streaming");
888 GST_OBJECT_LOCK (pad);
890 GST_OBJECT_UNLOCK (pad);
891 GST_PAD_STREAM_UNLOCK (pad);
893 case GST_PAD_MODE_PUSH:
894 case GST_PAD_MODE_PULL:
901 * gst_pad_set_active:
902 * @pad: the #GstPad to activate or deactivate.
903 * @active: whether or not the pad should be active.
905 * Activates or deactivates the given pad.
906 * Normally called from within core state change functions.
908 * If @active, makes sure the pad is active. If it is already active, either in
909 * push or pull mode, just return. Otherwise dispatches to the pad's activate
910 * function to perform the actual activation.
912 * If not @active, checks the pad's current mode and calls
913 * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
916 * Returns: #TRUE if the operation was successful.
921 gst_pad_set_active (GstPad * pad, gboolean active)
925 gboolean ret = FALSE;
927 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
929 GST_OBJECT_LOCK (pad);
930 old = GST_PAD_MODE (pad);
931 ACQUIRE_PARENT (pad, parent, no_parent);
932 GST_OBJECT_UNLOCK (pad);
935 if (old == GST_PAD_MODE_NONE) {
936 GST_DEBUG_OBJECT (pad, "activating pad from none");
937 ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad, parent);
939 GST_DEBUG_OBJECT (pad, "pad was active in %s mode",
940 gst_pad_mode_get_name (old));
944 if (old == GST_PAD_MODE_NONE) {
945 GST_DEBUG_OBJECT (pad, "pad was inactive");
948 GST_DEBUG_OBJECT (pad, "deactivating pad from %s mode",
949 gst_pad_mode_get_name (old));
950 ret = gst_pad_activate_mode (pad, old, FALSE);
954 RELEASE_PARENT (parent);
956 if (G_UNLIKELY (!ret))
964 GST_DEBUG_OBJECT (pad, "no parent");
965 GST_OBJECT_UNLOCK (pad);
970 GST_OBJECT_LOCK (pad);
972 g_critical ("Failed to deactivate pad %s:%s, very bad",
973 GST_DEBUG_PAD_NAME (pad));
975 GST_WARNING_OBJECT (pad, "Failed to activate pad");
977 GST_OBJECT_UNLOCK (pad);
983 * gst_pad_activate_mode:
984 * @pad: the #GstPad to activate or deactivate.
985 * @mode: the requested activation mode
986 * @active: whether or not the pad should be active.
988 * Activates or deactivates the given pad in @mode via dispatching to the
989 * pad's activatemodefunc. For use from within pad activation functions only.
991 * If you don't know what this is, you probably don't want to call it.
993 * Returns: TRUE if the operation was successful.
998 gst_pad_activate_mode (GstPad * pad, GstPadMode mode, gboolean active)
1000 gboolean res = FALSE;
1002 GstPadMode old, new;
1003 GstPadDirection dir;
1006 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1008 GST_OBJECT_LOCK (pad);
1009 old = GST_PAD_MODE (pad);
1010 dir = GST_PAD_DIRECTION (pad);
1011 ACQUIRE_PARENT (pad, parent, no_parent);
1012 GST_OBJECT_UNLOCK (pad);
1014 new = active ? mode : GST_PAD_MODE_NONE;
1019 if (active && old != mode && old != GST_PAD_MODE_NONE) {
1020 /* pad was activate in the wrong direction, deactivate it
1021 * and reactivate it in the requested mode */
1022 GST_DEBUG_OBJECT (pad, "deactivating pad from %s mode",
1023 gst_pad_mode_get_name (old));
1025 if (G_UNLIKELY (!gst_pad_activate_mode (pad, old, FALSE)))
1026 goto deactivate_failed;
1030 case GST_PAD_MODE_PULL:
1032 if (dir == GST_PAD_SINK) {
1033 if ((peer = gst_pad_get_peer (pad))) {
1034 GST_DEBUG_OBJECT (pad, "calling peer");
1035 if (G_UNLIKELY (!gst_pad_activate_mode (peer, mode, active)))
1037 gst_object_unref (peer);
1039 /* there is no peer, this is only fatal when we activate. When we
1040 * deactivate, we must assume the application has unlinked the peer and
1041 * will deactivate it eventually. */
1045 GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
1048 if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
1049 goto failure; /* Can't activate pull on a src without a
1050 getrange function */
1058 pre_activate (pad, new);
1060 if (GST_PAD_ACTIVATEMODEFUNC (pad)) {
1061 if (G_UNLIKELY (!GST_PAD_ACTIVATEMODEFUNC (pad) (pad, parent, mode,
1065 /* can happen for sinks of passthrough elements */
1068 post_activate (pad, new);
1070 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in %s mode",
1071 active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1076 /* Clear sticky flags on deactivation */
1078 GST_OBJECT_LOCK (pad);
1079 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1080 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
1081 GST_OBJECT_UNLOCK (pad);
1085 RELEASE_PARENT (parent);
1091 GST_DEBUG_OBJECT (pad, "no parent");
1092 GST_OBJECT_UNLOCK (pad);
1097 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in %s mode",
1098 active ? "activated" : "deactivated", gst_pad_mode_get_name (mode));
1103 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1104 "failed to %s in switch to %s mode from %s mode",
1105 (active ? "activate" : "deactivate"), gst_pad_mode_get_name (mode),
1106 gst_pad_mode_get_name (old));
1111 GST_OBJECT_LOCK (peer);
1112 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
1113 "activate_mode on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
1114 GST_OBJECT_UNLOCK (peer);
1115 gst_object_unref (peer);
1120 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
1121 "pad in pull mode");
1126 GST_OBJECT_LOCK (pad);
1127 GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in %s mode",
1128 active ? "activate" : "deactivate", gst_pad_mode_get_name (mode));
1129 GST_PAD_SET_FLUSHING (pad);
1130 GST_PAD_MODE (pad) = old;
1131 GST_OBJECT_UNLOCK (pad);
1137 * gst_pad_is_active:
1138 * @pad: the #GstPad to query
1140 * Query if a pad is active
1142 * Returns: TRUE if the pad is active.
1147 gst_pad_is_active (GstPad * pad)
1149 gboolean result = FALSE;
1151 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1153 GST_OBJECT_LOCK (pad);
1154 result = GST_PAD_IS_ACTIVE (pad);
1155 GST_OBJECT_UNLOCK (pad);
1161 * gst_pad_add_probe:
1162 * @pad: the #GstPad to add the probe to
1163 * @mask: the probe mask
1164 * @callback: #GstPadProbeCallback that will be called with notifications of
1166 * @user_data: (closure): user data passed to the callback
1167 * @destroy_data: #GDestroyNotify for user_data
1169 * Be notified of different states of pads. The provided callback is called for
1170 * every state that matches @mask.
1172 * Returns: an id or 0 on error. The id can be used to remove the probe with
1173 * gst_pad_remove_probe().
1178 gst_pad_add_probe (GstPad * pad, GstPadProbeType mask,
1179 GstPadProbeCallback callback, gpointer user_data,
1180 GDestroyNotify destroy_data)
1185 g_return_val_if_fail (GST_IS_PAD (pad), 0);
1186 g_return_val_if_fail (mask != 0, 0);
1188 GST_OBJECT_LOCK (pad);
1190 /* make a new probe */
1191 hook = g_hook_alloc (&pad->probes);
1193 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "adding probe for mask 0x%08x",
1196 /* when no contraints are given for the types, assume all types are
1198 if ((mask & GST_PAD_PROBE_TYPE_ALL_BOTH) == 0)
1199 mask |= GST_PAD_PROBE_TYPE_ALL_BOTH;
1200 if ((mask & GST_PAD_PROBE_TYPE_SCHEDULING) == 0)
1201 mask |= GST_PAD_PROBE_TYPE_SCHEDULING;
1203 /* store our flags and other fields */
1204 hook->flags |= (mask << G_HOOK_FLAG_USER_SHIFT);
1205 hook->func = callback;
1206 hook->data = user_data;
1207 hook->destroy = destroy_data;
1208 PROBE_COOKIE (hook) = (pad->priv->probe_cookie - 1);
1211 g_hook_prepend (&pad->probes, hook);
1213 /* incremenent cookie so that the new hook get's called */
1214 pad->priv->probe_list_cookie++;
1216 /* get the id of the hook, we return this and it can be used to remove the
1218 res = hook->hook_id;
1220 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got probe id %lu", res);
1222 if (mask & GST_PAD_PROBE_TYPE_BLOCKING) {
1223 /* we have a block probe */
1225 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKED);
1226 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "added blocking probe, "
1227 "now %d blocking probes", pad->num_blocked);
1230 /* call the callback if we need to be called for idle callbacks */
1231 if ((mask & GST_PAD_PROBE_TYPE_IDLE) && (callback != NULL)) {
1232 if (pad->priv->using > 0) {
1233 /* the pad is in use, we can't signal the idle callback yet. Since we set the
1234 * flag above, the last thread to leave the push will do the callback. New
1235 * threads going into the push will block. */
1236 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1237 "pad is in use, delay idle callback");
1238 GST_OBJECT_UNLOCK (pad);
1240 GstPadProbeInfo info = { GST_PAD_PROBE_TYPE_IDLE, res, };
1242 /* the pad is idle now, we can signal the idle callback now */
1243 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1244 "pad is idle, trigger idle callback");
1245 GST_OBJECT_UNLOCK (pad);
1247 callback (pad, &info, user_data);
1250 GST_OBJECT_UNLOCK (pad);
1256 cleanup_hook (GstPad * pad, GHook * hook)
1258 GstPadProbeType type;
1260 type = (hook->flags) >> G_HOOK_FLAG_USER_SHIFT;
1262 if (type & GST_PAD_PROBE_TYPE_BLOCKING) {
1263 /* unblock when we remove the last blocking probe */
1265 GST_DEBUG_OBJECT (pad, "remove blocking probe, now %d left",
1267 if (pad->num_blocked == 0) {
1268 GST_DEBUG_OBJECT (pad, "last blocking probe removed, unblocking");
1269 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKED);
1270 GST_PAD_BLOCK_BROADCAST (pad);
1273 g_hook_destroy_link (&pad->probes, hook);
1278 * gst_pad_remove_probe:
1279 * @pad: the #GstPad with the probe
1280 * @id: the probe id to remove
1282 * Remove the probe with @id from @pad.
1287 gst_pad_remove_probe (GstPad * pad, gulong id)
1291 g_return_if_fail (GST_IS_PAD (pad));
1293 GST_OBJECT_LOCK (pad);
1295 hook = g_hook_get (&pad->probes, id);
1299 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "removing hook %ld",
1301 cleanup_hook (pad, hook);
1302 GST_OBJECT_UNLOCK (pad);
1308 GST_OBJECT_UNLOCK (pad);
1309 g_warning ("%s: pad `%p' has no probe with id `%lu'", G_STRLOC, pad, id);
1315 * gst_pad_is_blocked:
1316 * @pad: the #GstPad to query
1318 * Checks if the pad is blocked or not. This function returns the
1319 * last requested state of the pad. It is not certain that the pad
1320 * is actually blocking at this point (see gst_pad_is_blocking()).
1322 * Returns: TRUE if the pad is blocked.
1327 gst_pad_is_blocked (GstPad * pad)
1329 gboolean result = FALSE;
1331 g_return_val_if_fail (GST_IS_PAD (pad), result);
1333 GST_OBJECT_LOCK (pad);
1334 result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLAG_BLOCKED);
1335 GST_OBJECT_UNLOCK (pad);
1341 * gst_pad_is_blocking:
1342 * @pad: the #GstPad to query
1344 * Checks if the pad is blocking or not. This is a guaranteed state
1345 * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1347 * Returns: TRUE if the pad is blocking.
1352 gst_pad_is_blocking (GstPad * pad)
1354 gboolean result = FALSE;
1356 g_return_val_if_fail (GST_IS_PAD (pad), result);
1358 GST_OBJECT_LOCK (pad);
1359 /* the blocking flag is only valid if the pad is not flushing */
1360 result = GST_PAD_IS_BLOCKING (pad) && !GST_PAD_IS_FLUSHING (pad);
1361 GST_OBJECT_UNLOCK (pad);
1367 * gst_pad_needs_reconfigure:
1368 * @pad: the #GstPad to check
1370 * Check the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1371 * if the flag was set.
1373 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag is set on @pad.
1376 gst_pad_needs_reconfigure (GstPad * pad)
1378 gboolean reconfigure;
1380 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1382 GST_OBJECT_LOCK (pad);
1383 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1384 GST_DEBUG_OBJECT (pad, "peeking RECONFIGURE flag %d", reconfigure);
1385 GST_OBJECT_UNLOCK (pad);
1391 * gst_pad_check_reconfigure:
1392 * @pad: the #GstPad to check
1394 * Check and clear the #GST_PAD_FLAG_NEED_RECONFIGURE flag on @pad and return %TRUE
1395 * if the flag was set.
1397 * Returns: %TRUE is the GST_PAD_FLAG_NEED_RECONFIGURE flag was set on @pad.
1400 gst_pad_check_reconfigure (GstPad * pad)
1402 gboolean reconfigure;
1404 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1406 GST_OBJECT_LOCK (pad);
1407 reconfigure = GST_PAD_NEEDS_RECONFIGURE (pad);
1409 GST_DEBUG_OBJECT (pad, "remove RECONFIGURE flag");
1410 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1411 GST_OBJECT_UNLOCK (pad);
1417 * gst_pad_mark_reconfigure:
1418 * @pad: the #GstPad to mark
1420 * Mark a pad for needing reconfiguration. The next call to
1421 * gst_pad_check_reconfigure() will return %TRUE after this call.
1424 gst_pad_mark_reconfigure (GstPad * pad)
1426 g_return_if_fail (GST_IS_PAD (pad));
1428 GST_OBJECT_LOCK (pad);
1429 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
1430 GST_OBJECT_UNLOCK (pad);
1434 * gst_pad_set_activate_function:
1436 * @f: the #GstPadActivateFunction to set.
1438 * Calls gst_pad_set_activate_function_full() with NULL for the user_data and
1442 * gst_pad_set_activate_function_full:
1444 * @activate: the #GstPadActivateFunction to set.
1445 * @user_data: user_data passed to @notify
1446 * @notify: notify called when @activate will not be used anymore.
1448 * Sets the given activate function for @pad. The activate function will
1449 * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1450 * the actual activation. Only makes sense to set on sink pads.
1452 * Call this function if your sink pad can start a pull-based task.
1455 gst_pad_set_activate_function_full (GstPad * pad,
1456 GstPadActivateFunction activate, gpointer user_data, GDestroyNotify notify)
1458 g_return_if_fail (GST_IS_PAD (pad));
1460 if (pad->activatenotify)
1461 pad->activatenotify (pad->activatedata);
1462 GST_PAD_ACTIVATEFUNC (pad) = activate;
1463 pad->activatedata = user_data;
1464 pad->activatenotify = notify;
1466 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1467 GST_DEBUG_FUNCPTR_NAME (activate));
1471 * gst_pad_set_activatemode_function:
1473 * @f: the #GstPadActivateModeFunction to set.
1475 * Calls gst_pad_set_activatemode_function_full() with NULL for the user_data and
1479 * gst_pad_set_activatemode_function_full:
1481 * @activatemode: the #GstPadActivateModeFunction to set.
1482 * @user_data: user_data passed to @notify
1483 * @notify: notify called when @activatemode will not be used anymore.
1485 * Sets the given activate_mode function for the pad. An activate_mode function
1486 * prepares the element for data passing.
1489 gst_pad_set_activatemode_function_full (GstPad * pad,
1490 GstPadActivateModeFunction activatemode, gpointer user_data,
1491 GDestroyNotify notify)
1493 g_return_if_fail (GST_IS_PAD (pad));
1495 if (pad->activatemodenotify)
1496 pad->activatemodenotify (pad->activatemodedata);
1497 GST_PAD_ACTIVATEMODEFUNC (pad) = activatemode;
1498 pad->activatemodedata = user_data;
1499 pad->activatemodenotify = notify;
1501 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatemodefunc set to %s",
1502 GST_DEBUG_FUNCPTR_NAME (activatemode));
1506 * gst_pad_set_chain_function:
1507 * @p: a sink #GstPad.
1508 * @f: the #GstPadChainFunction to set.
1510 * Calls gst_pad_set_chain_function_full() with NULL for the user_data and
1514 * gst_pad_set_chain_function_full:
1515 * @pad: a sink #GstPad.
1516 * @chain: the #GstPadChainFunction to set.
1517 * @user_data: user_data passed to @notify
1518 * @notify: notify called when @chain will not be used anymore.
1520 * Sets the given chain function for the pad. The chain function is called to
1521 * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1524 gst_pad_set_chain_function_full (GstPad * pad, GstPadChainFunction chain,
1525 gpointer user_data, GDestroyNotify notify)
1527 g_return_if_fail (GST_IS_PAD (pad));
1528 g_return_if_fail (GST_PAD_IS_SINK (pad));
1530 if (pad->chainnotify)
1531 pad->chainnotify (pad->chaindata);
1532 GST_PAD_CHAINFUNC (pad) = chain;
1533 pad->chaindata = user_data;
1534 pad->chainnotify = notify;
1536 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1537 GST_DEBUG_FUNCPTR_NAME (chain));
1541 * gst_pad_set_chain_list_function:
1542 * @p: a sink #GstPad.
1543 * @f: the #GstPadChainListFunction to set.
1545 * Calls gst_pad_set_chain_list_function_full() with NULL for the user_data and
1549 * gst_pad_set_chain_list_function_full:
1550 * @pad: a sink #GstPad.
1551 * @chainlist: the #GstPadChainListFunction to set.
1552 * @user_data: user_data passed to @notify
1553 * @notify: notify called when @chainlist will not be used anymore.
1555 * Sets the given chain list function for the pad. The chainlist function is
1556 * called to process a #GstBufferList input buffer list. See
1557 * #GstPadChainListFunction for more details.
1560 gst_pad_set_chain_list_function_full (GstPad * pad,
1561 GstPadChainListFunction chainlist, gpointer user_data,
1562 GDestroyNotify notify)
1564 g_return_if_fail (GST_IS_PAD (pad));
1565 g_return_if_fail (GST_PAD_IS_SINK (pad));
1567 if (pad->chainlistnotify)
1568 pad->chainlistnotify (pad->chainlistdata);
1569 GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1570 pad->chainlistdata = user_data;
1571 pad->chainlistnotify = notify;
1573 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1574 GST_DEBUG_FUNCPTR_NAME (chainlist));
1578 * gst_pad_set_getrange_function:
1579 * @p: a source #GstPad.
1580 * @f: the #GstPadGetRangeFunction to set.
1582 * Calls gst_pad_set_getrange_function_full() with NULL for the user_data and
1586 * gst_pad_set_getrange_function_full:
1587 * @pad: a source #GstPad.
1588 * @get: the #GstPadGetRangeFunction to set.
1589 * @user_data: user_data passed to @notify
1590 * @notify: notify called when @get will not be used anymore.
1592 * Sets the given getrange function for the pad. The getrange function is
1593 * called to produce a new #GstBuffer to start the processing pipeline. see
1594 * #GstPadGetRangeFunction for a description of the getrange function.
1597 gst_pad_set_getrange_function_full (GstPad * pad, GstPadGetRangeFunction get,
1598 gpointer user_data, GDestroyNotify notify)
1600 g_return_if_fail (GST_IS_PAD (pad));
1601 g_return_if_fail (GST_PAD_IS_SRC (pad));
1603 if (pad->getrangenotify)
1604 pad->getrangenotify (pad->getrangedata);
1605 GST_PAD_GETRANGEFUNC (pad) = get;
1606 pad->getrangedata = user_data;
1607 pad->getrangenotify = notify;
1609 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1610 GST_DEBUG_FUNCPTR_NAME (get));
1614 * gst_pad_set_event_function:
1615 * @p: a #GstPad of either direction.
1616 * @f: the #GstPadEventFunction to set.
1618 * Calls gst_pad_set_event_function_full() with NULL for the user_data and
1622 * gst_pad_set_event_function_full:
1623 * @pad: a #GstPad of either direction.
1624 * @event: the #GstPadEventFunction to set.
1625 * @user_data: user_data passed to @notify
1626 * @notify: notify called when @event will not be used anymore.
1628 * Sets the given event handler for the pad.
1631 gst_pad_set_event_function_full (GstPad * pad, GstPadEventFunction event,
1632 gpointer user_data, GDestroyNotify notify)
1634 g_return_if_fail (GST_IS_PAD (pad));
1636 if (pad->eventnotify)
1637 pad->eventnotify (pad->eventdata);
1638 GST_PAD_EVENTFUNC (pad) = event;
1639 pad->eventdata = user_data;
1640 pad->eventnotify = notify;
1642 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1643 GST_DEBUG_FUNCPTR_NAME (event));
1647 * gst_pad_set_query_function:
1648 * @p: a #GstPad of either direction.
1649 * @f: the #GstPadQueryFunction to set.
1651 * Calls gst_pad_set_query_function_full() with NULL for the user_data and
1655 * gst_pad_set_query_function_full:
1656 * @pad: a #GstPad of either direction.
1657 * @query: the #GstPadQueryFunction to set.
1658 * @user_data: user_data passed to @notify
1659 * @notify: notify called when @query will not be used anymore.
1661 * Set the given query function for the pad.
1664 gst_pad_set_query_function_full (GstPad * pad, GstPadQueryFunction query,
1665 gpointer user_data, GDestroyNotify notify)
1667 g_return_if_fail (GST_IS_PAD (pad));
1669 if (pad->querynotify)
1670 pad->querynotify (pad->querydata);
1671 GST_PAD_QUERYFUNC (pad) = query;
1672 pad->querydata = user_data;
1673 pad->querynotify = notify;
1675 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1676 GST_DEBUG_FUNCPTR_NAME (query));
1680 * gst_pad_set_iterate_internal_links_function:
1681 * @p: a #GstPad of either direction.
1682 * @f: the #GstPadIterIntLinkFunction to set.
1684 * Calls gst_pad_set_iterate_internal_links_function_full() with NULL
1685 * for the user_data and notify.
1688 * gst_pad_set_iterate_internal_links_function_full:
1689 * @pad: a #GstPad of either direction.
1690 * @iterintlink: the #GstPadIterIntLinkFunction to set.
1691 * @user_data: user_data passed to @notify
1692 * @notify: notify called when @iterintlink will not be used anymore.
1694 * Sets the given internal link iterator function for the pad.
1697 gst_pad_set_iterate_internal_links_function_full (GstPad * pad,
1698 GstPadIterIntLinkFunction iterintlink, gpointer user_data,
1699 GDestroyNotify notify)
1701 g_return_if_fail (GST_IS_PAD (pad));
1703 if (pad->iterintlinknotify)
1704 pad->iterintlinknotify (pad->iterintlinkdata);
1705 GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1706 pad->iterintlinkdata = user_data;
1707 pad->iterintlinknotify = notify;
1709 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1710 GST_DEBUG_FUNCPTR_NAME (iterintlink));
1714 * gst_pad_set_link_function:
1716 * @f: the #GstPadLinkFunction to set.
1718 * Calls gst_pad_set_link_function_full() with NULL
1719 * for the user_data and notify.
1722 * gst_pad_set_link_function_full:
1724 * @link: the #GstPadLinkFunction to set.
1725 * @user_data: user_data passed to @notify
1726 * @notify: notify called when @link will not be used anymore.
1728 * Sets the given link function for the pad. It will be called when
1729 * the pad is linked with another pad.
1731 * The return value #GST_PAD_LINK_OK should be used when the connection can be
1734 * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1735 * cannot be made for some reason.
1737 * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1738 * of the peer sink pad, if present.
1741 gst_pad_set_link_function_full (GstPad * pad, GstPadLinkFunction link,
1742 gpointer user_data, GDestroyNotify notify)
1744 g_return_if_fail (GST_IS_PAD (pad));
1746 if (pad->linknotify)
1747 pad->linknotify (pad->linkdata);
1748 GST_PAD_LINKFUNC (pad) = link;
1749 pad->linkdata = user_data;
1750 pad->linknotify = notify;
1752 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1753 GST_DEBUG_FUNCPTR_NAME (link));
1757 * gst_pad_set_unlink_function:
1759 * @f: the #GstPadUnlinkFunction to set.
1761 * Calls gst_pad_set_unlink_function_full() with NULL
1762 * for the user_data and notify.
1765 * gst_pad_set_unlink_function_full:
1767 * @unlink: the #GstPadUnlinkFunction to set.
1768 * @user_data: user_data passed to @notify
1769 * @notify: notify called when @unlink will not be used anymore.
1771 * Sets the given unlink function for the pad. It will be called
1772 * when the pad is unlinked.
1775 gst_pad_set_unlink_function_full (GstPad * pad, GstPadUnlinkFunction unlink,
1776 gpointer user_data, GDestroyNotify notify)
1778 g_return_if_fail (GST_IS_PAD (pad));
1780 if (pad->unlinknotify)
1781 pad->unlinknotify (pad->unlinkdata);
1782 GST_PAD_UNLINKFUNC (pad) = unlink;
1783 pad->unlinkdata = user_data;
1784 pad->unlinknotify = notify;
1786 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1787 GST_DEBUG_FUNCPTR_NAME (unlink));
1792 * @srcpad: the source #GstPad to unlink.
1793 * @sinkpad: the sink #GstPad to unlink.
1795 * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1796 * signal on both pads.
1798 * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1799 * the pads were not linked together.
1804 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1806 gboolean result = FALSE;
1807 GstElement *parent = NULL;
1809 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1810 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1811 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1812 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1814 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1815 GST_DEBUG_PAD_NAME (srcpad), srcpad,
1816 GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1818 /* We need to notify the parent before taking any pad locks as the bin in
1819 * question might be waiting for a lock on the pad while holding its lock
1820 * that our message will try to take. */
1821 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1822 if (GST_IS_ELEMENT (parent)) {
1823 gst_element_post_message (parent,
1824 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1825 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1827 gst_object_unref (parent);
1832 GST_OBJECT_LOCK (srcpad);
1833 GST_OBJECT_LOCK (sinkpad);
1835 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1836 goto not_linked_together;
1838 if (GST_PAD_UNLINKFUNC (srcpad)) {
1839 GstObject *tmpparent;
1841 ACQUIRE_PARENT (srcpad, tmpparent, no_src_parent);
1843 GST_PAD_UNLINKFUNC (srcpad) (srcpad, tmpparent);
1844 RELEASE_PARENT (parent);
1847 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1848 GstObject *tmpparent;
1850 ACQUIRE_PARENT (sinkpad, tmpparent, no_sink_parent);
1852 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad, tmpparent);
1853 RELEASE_PARENT (parent);
1857 /* first clear peers */
1858 GST_PAD_PEER (srcpad) = NULL;
1859 GST_PAD_PEER (sinkpad) = NULL;
1861 GST_OBJECT_UNLOCK (sinkpad);
1862 GST_OBJECT_UNLOCK (srcpad);
1864 /* fire off a signal to each of the pads telling them
1865 * that they've been unlinked */
1866 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1867 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1869 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1870 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1875 if (parent != NULL) {
1876 gst_element_post_message (parent,
1877 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1878 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1879 gst_object_unref (parent);
1884 not_linked_together:
1886 /* we do not emit a warning in this case because unlinking cannot
1887 * be made MT safe.*/
1888 GST_OBJECT_UNLOCK (sinkpad);
1889 GST_OBJECT_UNLOCK (srcpad);
1895 * gst_pad_is_linked:
1896 * @pad: pad to check
1898 * Checks if a @pad is linked to another pad or not.
1900 * Returns: TRUE if the pad is linked, FALSE otherwise.
1905 gst_pad_is_linked (GstPad * pad)
1909 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1911 GST_OBJECT_LOCK (pad);
1912 result = (GST_PAD_PEER (pad) != NULL);
1913 GST_OBJECT_UNLOCK (pad);
1918 /* get the caps from both pads and see if the intersection
1921 * This function should be called with the pad LOCK on both
1925 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1926 GstPadLinkCheck flags)
1928 GstCaps *srccaps = NULL;
1929 GstCaps *sinkcaps = NULL;
1930 gboolean compatible = FALSE;
1932 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1935 /* Doing the expensive caps checking takes priority over only checking the template caps */
1936 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1937 GST_OBJECT_UNLOCK (sink);
1938 GST_OBJECT_UNLOCK (src);
1940 srccaps = gst_pad_query_caps (src, NULL);
1941 sinkcaps = gst_pad_query_caps (sink, NULL);
1943 GST_OBJECT_LOCK (src);
1944 GST_OBJECT_LOCK (sink);
1946 /* If one of the two pads doesn't have a template, consider the intersection
1948 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1949 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1953 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1955 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1958 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1960 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1963 /* if we have caps on both pads we can check the intersection. If one
1964 * of the caps is NULL, we return TRUE. */
1965 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1967 gst_caps_unref (srccaps);
1969 gst_caps_unref (sinkcaps);
1973 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1974 gst_caps_unref (srccaps);
1975 gst_caps_unref (sinkcaps);
1978 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1979 (compatible ? "" : "not"));
1984 /* check if the grandparents of both pads are the same.
1985 * This check is required so that we don't try to link
1986 * pads from elements in different bins without ghostpads.
1988 * The LOCK should be held on both pads
1991 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1993 GstObject *psrc, *psink;
1995 psrc = GST_OBJECT_PARENT (src);
1996 psink = GST_OBJECT_PARENT (sink);
1998 /* if one of the pads has no parent, we allow the link */
1999 if (G_UNLIKELY (psrc == NULL || psink == NULL))
2002 /* only care about parents that are elements */
2003 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
2004 goto no_element_parent;
2006 /* if the parents are the same, we have a loop */
2007 if (G_UNLIKELY (psrc == psink))
2010 /* if they both have a parent, we check the grandparents. We can not lock
2011 * the parent because we hold on the child (pad) and the locking order is
2012 * parent >> child. */
2013 psrc = GST_OBJECT_PARENT (psrc);
2014 psink = GST_OBJECT_PARENT (psink);
2016 /* if they have grandparents but they are not the same */
2017 if (G_UNLIKELY (psrc != psink))
2018 goto wrong_grandparents;
2025 GST_CAT_DEBUG (GST_CAT_CAPS,
2026 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
2027 GST_PTR_FORMAT, psrc, psink);
2032 GST_CAT_DEBUG (GST_CAT_CAPS,
2033 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
2034 GST_PTR_FORMAT, psrc, psink);
2039 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
2045 GST_CAT_DEBUG (GST_CAT_CAPS,
2046 "pads have different grandparents %" GST_PTR_FORMAT " and %"
2047 GST_PTR_FORMAT, psrc, psink);
2052 /* FIXME leftover from an attempt at refactoring... */
2053 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
2054 * the two pads will be locked in the srcpad, sinkpad order. */
2055 static GstPadLinkReturn
2056 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2058 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
2059 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2061 GST_OBJECT_LOCK (srcpad);
2063 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
2064 goto src_was_linked;
2066 GST_OBJECT_LOCK (sinkpad);
2068 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
2069 goto sink_was_linked;
2071 /* check hierarchy, pads can only be linked if the grandparents
2073 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
2074 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
2075 goto wrong_hierarchy;
2077 /* check pad caps for non-empty intersection */
2078 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
2081 /* FIXME check pad scheduling for non-empty intersection */
2083 return GST_PAD_LINK_OK;
2087 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
2088 GST_DEBUG_PAD_NAME (srcpad),
2089 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
2090 /* we do not emit a warning in this case because unlinking cannot
2091 * be made MT safe.*/
2092 GST_OBJECT_UNLOCK (srcpad);
2093 return GST_PAD_LINK_WAS_LINKED;
2097 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
2098 GST_DEBUG_PAD_NAME (sinkpad),
2099 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
2100 /* we do not emit a warning in this case because unlinking cannot
2101 * be made MT safe.*/
2102 GST_OBJECT_UNLOCK (sinkpad);
2103 GST_OBJECT_UNLOCK (srcpad);
2104 return GST_PAD_LINK_WAS_LINKED;
2108 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
2109 GST_OBJECT_UNLOCK (sinkpad);
2110 GST_OBJECT_UNLOCK (srcpad);
2111 return GST_PAD_LINK_WRONG_HIERARCHY;
2115 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
2116 GST_OBJECT_UNLOCK (sinkpad);
2117 GST_OBJECT_UNLOCK (srcpad);
2118 return GST_PAD_LINK_NOFORMAT;
2124 * @srcpad: the source #GstPad.
2125 * @sinkpad: the sink #GstPad.
2127 * Checks if the source pad and the sink pad are compatible so they can be
2130 * Returns: TRUE if the pads can be linked.
2133 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2135 GstPadLinkReturn result;
2137 /* generic checks */
2138 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2139 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2141 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2142 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2144 /* gst_pad_link_prepare does everything for us, we only release the locks
2145 * on the pads that it gets us. If this function returns !OK the locks are not
2147 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2148 if (result != GST_PAD_LINK_OK)
2151 GST_OBJECT_UNLOCK (srcpad);
2152 GST_OBJECT_UNLOCK (sinkpad);
2155 return result == GST_PAD_LINK_OK;
2159 * gst_pad_link_full:
2160 * @srcpad: the source #GstPad to link.
2161 * @sinkpad: the sink #GstPad to link.
2162 * @flags: the checks to validate when linking
2164 * Links the source pad and the sink pad.
2166 * This variant of #gst_pad_link provides a more granular control on the
2167 * checks being done when linking. While providing some considerable speedups
2168 * the caller of this method must be aware that wrong usage of those flags
2169 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2170 * for more information.
2174 * Returns: A result code indicating if the connection worked or
2178 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2180 GstPadLinkReturn result;
2182 GstPadLinkFunction srcfunc, sinkfunc;
2184 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2185 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2186 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2187 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2188 GST_PAD_LINK_WRONG_DIRECTION);
2190 /* Notify the parent early. See gst_pad_unlink for details. */
2191 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2192 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2193 gst_element_post_message (parent,
2194 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2195 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2197 gst_object_unref (parent);
2202 /* prepare will also lock the two pads */
2203 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2205 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2208 /* must set peers before calling the link function */
2209 GST_PAD_PEER (srcpad) = sinkpad;
2210 GST_PAD_PEER (sinkpad) = srcpad;
2212 /* check events, when something is different, mark pending */
2213 schedule_events (srcpad, sinkpad);
2215 /* get the link functions */
2216 srcfunc = GST_PAD_LINKFUNC (srcpad);
2217 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2219 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2220 /* custom link functions, execute them */
2221 GST_OBJECT_UNLOCK (sinkpad);
2222 GST_OBJECT_UNLOCK (srcpad);
2225 GstObject *tmpparent;
2227 ACQUIRE_PARENT (srcpad, tmpparent, no_parent);
2228 /* this one will call the peer link function */
2229 result = srcfunc (srcpad, tmpparent, sinkpad);
2230 RELEASE_PARENT (tmpparent);
2231 } else if (sinkfunc) {
2232 GstObject *tmpparent;
2234 ACQUIRE_PARENT (sinkpad, tmpparent, no_parent);
2235 /* if no source link function, we need to call the sink link
2236 * function ourselves. */
2237 result = sinkfunc (sinkpad, tmpparent, srcpad);
2238 RELEASE_PARENT (tmpparent);
2242 GST_OBJECT_LOCK (srcpad);
2243 GST_OBJECT_LOCK (sinkpad);
2245 /* we released the lock, check if the same pads are linked still */
2246 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2247 goto concurrent_link;
2249 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2252 GST_OBJECT_UNLOCK (sinkpad);
2253 GST_OBJECT_UNLOCK (srcpad);
2255 /* fire off a signal to each of the pads telling them
2256 * that they've been linked */
2257 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2258 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2260 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2261 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2263 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2266 if (G_LIKELY (parent)) {
2267 gst_element_post_message (parent,
2268 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2269 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2270 gst_object_unref (parent);
2278 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2279 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2280 GST_OBJECT_UNLOCK (sinkpad);
2281 GST_OBJECT_UNLOCK (srcpad);
2283 /* The other link operation succeeded first */
2284 result = GST_PAD_LINK_WAS_LINKED;
2289 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2290 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2292 GST_PAD_PEER (srcpad) = NULL;
2293 GST_PAD_PEER (sinkpad) = NULL;
2295 GST_OBJECT_UNLOCK (sinkpad);
2296 GST_OBJECT_UNLOCK (srcpad);
2304 * @srcpad: the source #GstPad to link.
2305 * @sinkpad: the sink #GstPad to link.
2307 * Links the source pad and the sink pad.
2309 * Returns: A result code indicating if the connection worked or
2315 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2317 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2321 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2323 GstPadTemplate **template_p;
2325 /* this function would need checks if it weren't static */
2327 GST_OBJECT_LOCK (pad);
2328 template_p = &pad->padtemplate;
2329 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2330 GST_OBJECT_UNLOCK (pad);
2333 gst_pad_template_pad_created (templ, pad);
2337 * gst_pad_get_pad_template:
2340 * Gets the template for @pad.
2342 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2343 * instantiated, or %NULL if this pad has no template. Unref after
2347 gst_pad_get_pad_template (GstPad * pad)
2349 GstPadTemplate *templ;
2351 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2353 templ = GST_PAD_PAD_TEMPLATE (pad);
2355 return (templ ? gst_object_ref (templ) : NULL);
2359 * gst_pad_has_current_caps:
2360 * @pad: a #GstPad to check
2362 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2364 * Returns: TRUE when @pad has caps associated with it.
2367 gst_pad_has_current_caps (GstPad * pad)
2372 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2374 GST_OBJECT_LOCK (pad);
2375 caps = get_pad_caps (pad);
2376 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2377 "check current pad caps %" GST_PTR_FORMAT, caps);
2378 result = (caps != NULL);
2379 GST_OBJECT_UNLOCK (pad);
2385 * gst_pad_get_current_caps:
2386 * @pad: a #GstPad to get the current capabilities of.
2388 * Gets the capabilities currently configured on @pad with the last
2389 * #GST_EVENT_CAPS event.
2391 * Returns: the current caps of the pad with incremented ref-count.
2394 gst_pad_get_current_caps (GstPad * pad)
2398 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2400 GST_OBJECT_LOCK (pad);
2401 if ((result = get_pad_caps (pad)))
2402 gst_caps_ref (result);
2403 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2404 "get current pad caps %" GST_PTR_FORMAT, result);
2405 GST_OBJECT_UNLOCK (pad);
2411 * gst_pad_get_pad_template_caps:
2412 * @pad: a #GstPad to get the template capabilities from.
2414 * Gets the capabilities for @pad's template.
2416 * Returns: (transfer full): the #GstCaps of this pad template.
2417 * Unref after usage.
2420 gst_pad_get_pad_template_caps (GstPad * pad)
2422 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2424 if (GST_PAD_PAD_TEMPLATE (pad))
2425 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2427 return gst_caps_ref (GST_CAPS_ANY);
2432 * @pad: a #GstPad to get the peer of.
2434 * Gets the peer of @pad. This function refs the peer pad so
2435 * you need to unref it after use.
2437 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2442 gst_pad_get_peer (GstPad * pad)
2446 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2448 GST_OBJECT_LOCK (pad);
2449 result = GST_PAD_PEER (pad);
2451 gst_object_ref (result);
2452 GST_OBJECT_UNLOCK (pad);
2458 * gst_pad_get_allowed_caps:
2461 * Gets the capabilities of the allowed media types that can flow through
2462 * @pad and its peer.
2464 * The allowed capabilities is calculated as the intersection of the results of
2465 * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2466 * on the resulting caps.
2468 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2469 * caps when you no longer need it. This function returns NULL when @pad
2475 gst_pad_get_allowed_caps (GstPad * pad)
2482 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2484 GST_OBJECT_LOCK (pad);
2485 peer = GST_PAD_PEER (pad);
2486 if (G_UNLIKELY (peer == NULL))
2489 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2491 gst_object_ref (peer);
2492 GST_OBJECT_UNLOCK (pad);
2493 mycaps = gst_pad_query_caps (pad, NULL);
2495 peercaps = gst_pad_query_caps (peer, NULL);
2496 gst_object_unref (peer);
2498 caps = gst_caps_intersect (mycaps, peercaps);
2499 gst_caps_unref (peercaps);
2500 gst_caps_unref (mycaps);
2502 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2509 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2510 GST_OBJECT_UNLOCK (pad);
2517 * gst_pad_iterate_internal_links_default:
2518 * @pad: the #GstPad to get the internal links of.
2519 * @parent: the parent of @pad or NULL
2521 * Iterate the list of pads to which the given pad is linked to inside of
2522 * the parent element.
2523 * This is the default handler, and thus returns an iterator of all of the
2524 * pads inside the parent element with opposite direction.
2526 * The caller must free this iterator after use with gst_iterator_free().
2528 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2529 * returned pad with gst_object_unref().
2532 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2539 GstElement *eparent;
2541 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2543 if (parent != NULL && GST_IS_ELEMENT (parent)) {
2544 eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2546 GST_OBJECT_LOCK (pad);
2547 eparent = GST_PAD_PARENT (pad);
2548 if (!eparent || !GST_IS_ELEMENT (eparent))
2551 gst_object_ref (eparent);
2552 GST_OBJECT_UNLOCK (pad);
2555 if (pad->direction == GST_PAD_SRC)
2556 padlist = &eparent->sinkpads;
2558 padlist = &eparent->srcpads;
2560 GST_DEBUG_OBJECT (pad, "Making iterator");
2562 cookie = &eparent->pads_cookie;
2564 lock = GST_OBJECT_GET_LOCK (eparent);
2566 res = gst_iterator_new_list (GST_TYPE_PAD,
2567 lock, cookie, padlist, (GObject *) owner, NULL);
2569 gst_object_unref (owner);
2576 GST_OBJECT_UNLOCK (pad);
2577 GST_DEBUG_OBJECT (pad, "no parent element");
2583 * gst_pad_iterate_internal_links:
2584 * @pad: the GstPad to get the internal links of.
2586 * Gets an iterator for the pads to which the given pad is linked to inside
2587 * of the parent element.
2589 * Each #GstPad element yielded by the iterator will have its refcount increased,
2590 * so unref after use.
2592 * Free-function: gst_iterator_free
2594 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
2595 * pad does not have an iterator function configured. Use
2596 * gst_iterator_free() after usage.
2599 gst_pad_iterate_internal_links (GstPad * pad)
2601 GstIterator *res = NULL;
2604 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2606 GST_OBJECT_LOCK (pad);
2607 ACQUIRE_PARENT (pad, parent, no_parent);
2608 GST_OBJECT_UNLOCK (pad);
2610 if (GST_PAD_ITERINTLINKFUNC (pad))
2611 res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2613 RELEASE_PARENT (parent);
2620 GST_DEBUG_OBJECT (pad, "no parent");
2621 GST_OBJECT_UNLOCK (pad);
2629 * @forward: (scope call): a #GstPadForwardFunction
2630 * @user_data: user data passed to @forward
2632 * Calls @forward for all internally linked pads of @pad. This function deals with
2633 * dynamically changing internal pads and will make sure that the @forward
2634 * function is only called once for each pad.
2636 * When @forward returns TRUE, no further pads will be processed.
2638 * Returns: TRUE if one of the dispatcher functions returned TRUE.
2641 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2644 gboolean result = FALSE;
2646 gboolean done = FALSE;
2647 GValue item = { 0, };
2648 GList *pushed_pads = NULL;
2650 iter = gst_pad_iterate_internal_links (pad);
2656 switch (gst_iterator_next (iter, &item)) {
2657 case GST_ITERATOR_OK:
2661 intpad = g_value_get_object (&item);
2663 /* if already pushed, skip. FIXME, find something faster to tag pads */
2664 if (intpad == NULL || g_list_find (pushed_pads, intpad)) {
2665 g_value_reset (&item);
2669 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2670 GST_DEBUG_PAD_NAME (intpad));
2671 done = result = forward (intpad, user_data);
2673 pushed_pads = g_list_prepend (pushed_pads, intpad);
2675 g_value_reset (&item);
2678 case GST_ITERATOR_RESYNC:
2679 /* We don't reset the result here because we don't push the event
2680 * again on pads that got the event already and because we need
2681 * to consider the result of the previous pushes */
2682 gst_iterator_resync (iter);
2684 case GST_ITERATOR_ERROR:
2685 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2688 case GST_ITERATOR_DONE:
2693 g_value_unset (&item);
2694 gst_iterator_free (iter);
2696 g_list_free (pushed_pads);
2706 gboolean dispatched;
2710 event_forward_func (GstPad * pad, EventData * data)
2712 /* for each pad we send to, we should ref the event; it's up
2713 * to downstream to unref again when handled. */
2714 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2715 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2717 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2719 data->dispatched = TRUE;
2726 * gst_pad_event_default:
2727 * @pad: a #GstPad to call the default event handler on.
2728 * @parent: the parent of @pad or NULL
2729 * @event: (transfer full): the #GstEvent to handle.
2731 * Invokes the default event handler for the given pad.
2733 * The EOS event will pause the task associated with @pad before it is forwarded
2734 * to all internally linked pads,
2736 * The the event is sent to all pads internally linked to @pad. This function
2737 * takes ownership of @event.
2739 * Returns: TRUE if the event was sent successfully.
2742 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2744 gboolean result, forward = TRUE;
2746 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2747 g_return_val_if_fail (event != NULL, FALSE);
2749 GST_LOG_OBJECT (pad, "default event handler for event %" GST_PTR_FORMAT,
2752 switch (GST_EVENT_TYPE (event)) {
2753 case GST_EVENT_CAPS:
2754 forward = GST_PAD_IS_PROXY_CAPS (pad);
2765 data.dispatched = FALSE;
2766 data.result = FALSE;
2768 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
2770 /* for sinkpads without a parent element or without internal links, nothing
2771 * will be dispatched but we still want to return TRUE. */
2772 if (data.dispatched)
2773 result = data.result;
2778 gst_event_unref (event);
2783 /* Default accept caps implementation just checks against
2784 * the allowed caps for the pad */
2786 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
2788 /* get the caps and see if it intersects to something not empty */
2789 GstCaps *caps, *allowed;
2792 GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
2794 /* first forward the query to internally linked pads when we are dealing with
2796 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2797 if ((result = gst_pad_proxy_query_accept_caps (pad, query))) {
2802 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pad,
2803 "fallback ACCEPT_CAPS query, consider implementing a specialized version");
2805 allowed = gst_pad_query_caps (pad, NULL);
2806 gst_query_parse_accept_caps (query, &caps);
2809 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2810 result = gst_caps_is_subset (caps, allowed);
2811 gst_caps_unref (allowed);
2813 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2816 gst_query_set_accept_caps_result (query, result);
2822 /* Default caps implementation */
2824 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
2826 GstCaps *result = NULL, *filter;
2827 GstPadTemplate *templ;
2828 gboolean fixed_caps;
2830 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "query caps %" GST_PTR_FORMAT,
2833 /* first try to proxy if we must */
2834 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2835 if ((gst_pad_proxy_query_caps (pad, query))) {
2840 gst_query_parse_caps (query, &filter);
2842 /* no proxy or it failed, do default handling */
2843 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2845 GST_OBJECT_LOCK (pad);
2847 /* fixed caps, try the negotiated caps first */
2848 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
2849 if ((result = get_pad_caps (pad)))
2850 goto filter_done_unlock;
2853 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2854 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
2855 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
2856 goto filter_done_unlock;
2860 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2861 "non-fixed pad caps: trying pad caps");
2862 /* non fixed caps, try the negotiated caps */
2863 if ((result = get_pad_caps (pad)))
2864 goto filter_done_unlock;
2867 /* this almost never happens */
2868 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2869 result = GST_CAPS_ANY;
2872 GST_OBJECT_UNLOCK (pad);
2874 /* run the filter on the result */
2876 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2877 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
2878 GST_PTR_FORMAT, result, result, filter, filter);
2879 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2880 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2883 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2884 "using caps %p %" GST_PTR_FORMAT, result, result);
2885 result = gst_caps_ref (result);
2887 gst_query_set_caps_result (query, result);
2888 gst_caps_unref (result);
2898 gboolean dispatched;
2902 query_forward_func (GstPad * pad, QueryData * data)
2904 GST_LOG_OBJECT (pad, "query peer %p (%s) of %s:%s",
2905 data->query, GST_QUERY_TYPE_NAME (data->query), GST_DEBUG_PAD_NAME (pad));
2907 data->result |= gst_pad_peer_query (pad, data->query);
2909 data->dispatched = TRUE;
2911 /* stop on first successful reply */
2912 return data->result;
2916 * gst_pad_query_default:
2917 * @pad: a #GstPad to call the default query handler on.
2918 * @parent: the parent of @pad or NULL
2919 * @query: (transfer none): the #GstQuery to handle.
2921 * Invokes the default query handler for the given pad.
2922 * The query is sent to all pads internally linked to @pad. Note that
2923 * if there are many possible sink pads that are internally linked to
2924 * @pad, only one will be sent the query.
2925 * Multi-sinkpad elements should implement custom query handlers.
2927 * Returns: TRUE if the query was performed successfully.
2930 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
2932 gboolean forward, ret = FALSE;
2934 switch (GST_QUERY_TYPE (query)) {
2935 case GST_QUERY_SCHEDULING:
2936 forward = GST_PAD_IS_PROXY_SCHEDULING (pad);
2938 case GST_QUERY_ALLOCATION:
2939 forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
2941 case GST_QUERY_ACCEPT_CAPS:
2942 ret = gst_pad_query_accept_caps_default (pad, query);
2945 case GST_QUERY_CAPS:
2946 ret = gst_pad_query_caps_default (pad, query);
2949 case GST_QUERY_POSITION:
2950 case GST_QUERY_SEEKING:
2951 case GST_QUERY_FORMATS:
2952 case GST_QUERY_LATENCY:
2953 case GST_QUERY_JITTER:
2954 case GST_QUERY_RATE:
2955 case GST_QUERY_CONVERT:
2961 GST_DEBUG_OBJECT (pad, "%sforwarding %p (%s) query", (forward ? "" : "not "),
2962 query, GST_QUERY_TYPE_NAME (query));
2968 data.dispatched = FALSE;
2969 data.result = FALSE;
2971 gst_pad_forward (pad, (GstPadForwardFunction) query_forward_func, &data);
2973 if (data.dispatched) {
2976 /* nothing dispatched, assume drained */
2977 if (GST_QUERY_TYPE (query) == GST_QUERY_DRAIN)
2987 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
2989 GstPad *pad = data->pad;
2990 GstPadProbeInfo *info = data->info;
2991 GstPadProbeType type, flags;
2992 GstPadProbeCallback callback;
2993 GstPadProbeReturn ret;
2995 /* if we have called this callback, do nothing */
2996 if (PROBE_COOKIE (hook) == data->cookie) {
2997 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2998 "hook %lu, cookie %u already called", hook->hook_id,
2999 PROBE_COOKIE (hook));
3003 PROBE_COOKIE (hook) = data->cookie;
3005 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
3008 /* one of the data types */
3009 if ((flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
3011 /* one of the scheduling types */
3012 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
3014 /* one of the blocking types must match */
3015 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
3016 (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
3018 /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
3019 if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
3020 (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
3023 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3024 "hook %lu, cookie %u with flags 0x%08x matches", hook->hook_id,
3025 PROBE_COOKIE (hook), flags);
3027 data->marshalled = TRUE;
3029 callback = (GstPadProbeCallback) hook->func;
3030 if (callback == NULL)
3033 info->id = hook->hook_id;
3035 GST_OBJECT_UNLOCK (pad);
3037 ret = callback (pad, info, hook->data);
3039 GST_OBJECT_LOCK (pad);
3042 case GST_PAD_PROBE_REMOVE:
3043 /* remove the probe */
3044 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3045 cleanup_hook (pad, hook);
3047 case GST_PAD_PROBE_DROP:
3048 /* need to drop the data, make sure other probes don't get called
3050 GST_DEBUG_OBJECT (pad, "asked to drop item");
3051 info->type = GST_PAD_PROBE_TYPE_INVALID;
3052 data->dropped = TRUE;
3054 case GST_PAD_PROBE_PASS:
3055 /* inform the pad block to let things pass */
3056 GST_DEBUG_OBJECT (pad, "asked to pass item");
3060 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3067 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3068 "hook %lu, cookie %u with flags 0x%08x does not match %08x",
3069 hook->hook_id, PROBE_COOKIE (hook), flags, info->type);
3074 /* a probe that does not take or return any data */
3075 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
3077 if (G_UNLIKELY (pad->num_probes)) { \
3078 /* pass NULL as the data item */ \
3079 GstPadProbeInfo info = { mask, 0, NULL, 0, 0 }; \
3080 ret = do_probe_callbacks (pad, &info, defaultval); \
3081 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
3086 #define PROBE_FULL(pad,mask,data,offs,size,label) \
3088 if (G_UNLIKELY (pad->num_probes)) { \
3089 /* pass the data item */ \
3090 GstPadProbeInfo info = { mask, 0, data, offs, size }; \
3091 ret = do_probe_callbacks (pad, &info, GST_FLOW_OK); \
3092 /* store the possibly updated data item */ \
3093 data = GST_PAD_PROBE_INFO_DATA (&info); \
3094 /* if something went wrong, exit */ \
3095 if (G_UNLIKELY (ret != GST_FLOW_OK)) \
3100 #define PROBE_PUSH(pad,mask,data,label) \
3101 PROBE_FULL(pad, mask, data, -1, -1, label);
3102 #define PROBE_PULL(pad,mask,data,offs,size,label) \
3103 PROBE_FULL(pad, mask, data, offs, size, label);
3105 static GstFlowReturn
3106 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
3107 GstFlowReturn defaultval)
3116 data.marshalled = FALSE;
3117 data.dropped = FALSE;
3118 data.cookie = ++pad->priv->probe_cookie;
3121 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
3124 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3125 "do probes cookie %u", data.cookie);
3126 cookie = pad->priv->probe_list_cookie;
3128 g_hook_list_marshal (&pad->probes, TRUE,
3129 (GHookMarshaller) probe_hook_marshal, &data);
3131 /* if the list changed, call the new callbacks (they will not have their
3132 * cookie set to data.cookie */
3133 if (cookie != pad->priv->probe_list_cookie) {
3134 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3135 "probe list changed, restarting");
3139 /* the first item that dropped will stop the hooks and then we drop here */
3143 /* if no handler matched and we are blocking, let the item pass */
3144 if (!data.marshalled && is_block)
3147 /* At this point, all handlers returned either OK or PASS. If one handler
3148 * returned PASS, let the item pass */
3153 while (GST_PAD_IS_BLOCKED (pad)) {
3154 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3155 "we are blocked %d times", pad->num_blocked);
3157 /* we might have released the lock */
3158 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3161 /* now we block the streaming thread. It can be unlocked when we
3162 * deactivate the pad (which will also set the FLUSHING flag) or
3163 * when the pad is unblocked. A flushing event will also unblock
3164 * the pad after setting the FLUSHING flag. */
3165 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3166 "Waiting to be unblocked or set flushing");
3167 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3168 GST_PAD_BLOCK_WAIT (pad);
3169 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3170 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3172 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3182 GST_DEBUG_OBJECT (pad, "pad is flushing");
3183 return GST_FLOW_FLUSHING;
3187 GST_DEBUG_OBJECT (pad, "data is dropped");
3188 return GST_FLOW_CUSTOM_SUCCESS;
3192 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3193 GST_DEBUG_OBJECT (pad, "data is passed");
3201 * gst_pad_get_offset:
3204 * Get the offset applied to the running time of @pad. @pad has to be a source
3207 * Returns: the offset.
3210 gst_pad_get_offset (GstPad * pad)
3214 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3216 GST_OBJECT_LOCK (pad);
3217 result = pad->offset;
3218 GST_OBJECT_UNLOCK (pad);
3224 * gst_pad_set_offset:
3226 * @offset: the offset
3228 * Set the offset that will be applied to the running time of @pad.
3231 gst_pad_set_offset (GstPad * pad, gint64 offset)
3235 g_return_if_fail (GST_IS_PAD (pad));
3237 GST_OBJECT_LOCK (pad);
3238 /* if nothing changed, do nothing */
3239 if (pad->offset == offset)
3242 pad->offset = offset;
3243 GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3245 /* sinkpads will apply their offset the next time a segment
3246 * event is received. */
3247 if (GST_PAD_IS_SINK (pad))
3250 /* resend the last segment event on next buffer push */
3251 if ((ev = find_event_by_type (pad, GST_EVENT_SEGMENT, 0))) {
3252 ev->received = FALSE;
3253 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3257 GST_OBJECT_UNLOCK (pad);
3264 /* If TRUE and ret is not OK this means
3265 * that pushing the EOS event failed
3270 /* should be called with pad LOCK */
3272 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3274 PushStickyData *data = user_data;
3275 GstEvent *event = ev->event;
3278 GST_DEBUG_OBJECT (pad, "event %s was already received",
3279 GST_EVENT_TYPE_NAME (event));
3283 data->ret = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
3284 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3286 switch (data->ret) {
3288 ev->received = TRUE;
3289 GST_DEBUG_OBJECT (pad, "event %s marked received",
3290 GST_EVENT_TYPE_NAME (event));
3292 case GST_FLOW_NOT_LINKED:
3293 /* not linked is not a problem, we are sticky so the event will be
3294 * sent later but only for non-EOS events */
3295 GST_DEBUG_OBJECT (pad, "pad was not linked");
3296 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
3297 data->ret = GST_FLOW_OK;
3300 GST_DEBUG_OBJECT (pad, "mark pending events");
3301 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3305 if (data->ret != GST_FLOW_OK && GST_EVENT_TYPE (event) == GST_EVENT_EOS)
3306 data->was_eos = TRUE;
3308 return data->ret == GST_FLOW_OK;
3311 /* check sticky events and push them when needed. should be called
3313 static inline GstFlowReturn
3314 check_sticky (GstPad * pad)
3316 PushStickyData data = { GST_FLOW_OK, FALSE };
3318 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3319 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3321 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3322 events_foreach (pad, push_sticky, &data);
3324 /* If there's an EOS event we must push it downstream
3325 * even if sending a previous sticky event failed.
3326 * Otherwise the pipeline might wait forever for EOS.
3328 * Only do this if pushing another event than the EOS
3331 if (data.ret != GST_FLOW_OK && !data.was_eos) {
3332 PadEvent *ev = find_event_by_type (pad, GST_EVENT_EOS, 0);
3334 if (ev && !ev->received) {
3335 data.ret = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3336 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3346 * @pad: a #GstPad to invoke the default query on.
3347 * @query: (transfer none): the #GstQuery to perform.
3349 * Dispatches a query to a pad. The query should have been allocated by the
3350 * caller via one of the type-specific allocation functions. The element that
3351 * the pad belongs to is responsible for filling the query with an appropriate
3352 * response, which should then be parsed with a type-specific query parsing
3355 * Again, the caller is responsible for both the allocation and deallocation of
3356 * the query structure.
3358 * Please also note that some queries might need a running pipeline to work.
3360 * Returns: TRUE if the query could be performed.
3363 gst_pad_query (GstPad * pad, GstQuery * query)
3366 gboolean res, serialized;
3367 GstPadQueryFunction func;
3368 GstPadProbeType type;
3371 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3372 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3374 if (GST_PAD_IS_SRC (pad)) {
3375 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3376 goto wrong_direction;
3377 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3378 } else if (GST_PAD_IS_SINK (pad)) {
3379 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3380 goto wrong_direction;
3381 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3383 goto unknown_direction;
3385 GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
3386 GST_QUERY_TYPE_NAME (query));
3388 serialized = GST_QUERY_IS_SERIALIZED (query);
3389 if (G_UNLIKELY (serialized))
3390 GST_PAD_STREAM_LOCK (pad);
3392 GST_OBJECT_LOCK (pad);
3393 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3394 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3395 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3397 ACQUIRE_PARENT (pad, parent, no_parent);
3398 GST_OBJECT_UNLOCK (pad);
3400 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3403 res = func (pad, parent, query);
3405 RELEASE_PARENT (parent);
3407 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3408 GST_QUERY_TYPE_NAME (query), res);
3413 GST_OBJECT_LOCK (pad);
3414 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3415 GST_OBJECT_UNLOCK (pad);
3417 if (G_UNLIKELY (serialized))
3418 GST_PAD_STREAM_UNLOCK (pad);
3425 g_warning ("pad %s:%s query %s in wrong direction",
3426 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3431 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3436 GST_DEBUG_OBJECT (pad, "had no parent");
3437 GST_OBJECT_UNLOCK (pad);
3438 if (G_UNLIKELY (serialized))
3439 GST_PAD_STREAM_UNLOCK (pad);
3444 GST_DEBUG_OBJECT (pad, "had no query function");
3445 RELEASE_PARENT (parent);
3446 if (G_UNLIKELY (serialized))
3447 GST_PAD_STREAM_UNLOCK (pad);
3452 GST_DEBUG_OBJECT (pad, "query failed");
3453 if (G_UNLIKELY (serialized))
3454 GST_PAD_STREAM_UNLOCK (pad);
3459 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3460 GST_OBJECT_UNLOCK (pad);
3461 if (G_UNLIKELY (serialized))
3462 GST_PAD_STREAM_UNLOCK (pad);
3464 /* if a probe dropped, we don't sent it further but assume that the probe
3465 * answered the query and return TRUE */
3466 if (ret == GST_FLOW_CUSTOM_SUCCESS)
3476 * gst_pad_peer_query:
3477 * @pad: a #GstPad to invoke the peer query on.
3478 * @query: (transfer none): the #GstQuery to perform.
3480 * Performs gst_pad_query() on the peer of @pad.
3482 * The caller is responsible for both the allocation and deallocation of
3483 * the query structure.
3485 * Returns: TRUE if the query could be performed. This function returns %FALSE
3486 * if @pad has no peer.
3489 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3492 GstPadProbeType type;
3493 gboolean res, serialized;
3496 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3497 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3499 if (GST_PAD_IS_SRC (pad)) {
3500 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3501 goto wrong_direction;
3502 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3503 } else if (GST_PAD_IS_SINK (pad)) {
3504 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3505 goto wrong_direction;
3506 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3508 goto unknown_direction;
3510 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3511 GST_QUERY_TYPE_NAME (query));
3513 serialized = GST_QUERY_IS_SERIALIZED (query);
3515 GST_OBJECT_LOCK (pad);
3516 if (GST_PAD_IS_SRC (pad) && serialized) {
3517 /* all serialized queries on the srcpad trigger push of
3519 if (!check_sticky (pad) == GST_FLOW_OK)
3523 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3524 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3525 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3527 peerpad = GST_PAD_PEER (pad);
3528 if (G_UNLIKELY (peerpad == NULL))
3531 gst_object_ref (peerpad);
3532 GST_OBJECT_UNLOCK (pad);
3534 res = gst_pad_query (peerpad, query);
3536 gst_object_unref (peerpad);
3541 GST_OBJECT_LOCK (pad);
3542 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3543 GST_OBJECT_UNLOCK (pad);
3550 g_warning ("pad %s:%s query %s in wrong direction",
3551 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3556 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3561 GST_WARNING_OBJECT (pad, "could not send sticky events");
3562 GST_OBJECT_UNLOCK (pad);
3567 GST_WARNING_OBJECT (pad, "pad has no peer");
3568 GST_OBJECT_UNLOCK (pad);
3573 GST_DEBUG_OBJECT (pad, "query failed");
3578 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3579 GST_OBJECT_UNLOCK (pad);
3581 /* if a probe dropped, we don't sent it further but assume that the probe
3582 * answered the query and return TRUE */
3583 if (ret == GST_FLOW_CUSTOM_SUCCESS)
3592 /**********************************************************************
3593 * Data passing functions
3596 /* this is the chain function that does not perform the additional argument
3597 * checking for that little extra speed.
3599 static inline GstFlowReturn
3600 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3605 GST_PAD_STREAM_LOCK (pad);
3607 GST_OBJECT_LOCK (pad);
3608 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3611 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
3614 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3617 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3619 PROBE_PUSH (pad, type, data, probe_stopped);
3621 parent = GST_OBJECT_PARENT (pad);
3622 GST_OBJECT_UNLOCK (pad);
3624 /* NOTE: we read the chainfunc unlocked.
3625 * we cannot hold the lock for the pad so we might send
3626 * the data to the wrong function. This is not really a
3627 * problem since functions are assigned at creation time
3628 * and don't change that often... */
3629 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3630 GstPadChainFunction chainfunc;
3632 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3635 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3636 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3637 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3639 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
3641 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3642 "called chainfunction &%s with buffer %p, returned %s",
3643 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3645 GstPadChainListFunction chainlistfunc;
3647 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3650 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3651 "calling chainlistfunction &%s",
3652 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3654 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
3656 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3657 "called chainlistfunction &%s, returned %s",
3658 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3661 GST_PAD_STREAM_UNLOCK (pad);
3668 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3669 "chaining, but pad was flushing");
3670 GST_OBJECT_UNLOCK (pad);
3671 GST_PAD_STREAM_UNLOCK (pad);
3672 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3673 return GST_FLOW_FLUSHING;
3677 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
3678 GST_OBJECT_UNLOCK (pad);
3679 GST_PAD_STREAM_UNLOCK (pad);
3680 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3681 return GST_FLOW_EOS;
3685 g_critical ("chain on pad %s:%s but it was not in push mode",
3686 GST_DEBUG_PAD_NAME (pad));
3687 GST_OBJECT_UNLOCK (pad);
3688 GST_PAD_STREAM_UNLOCK (pad);
3689 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3690 return GST_FLOW_ERROR;
3694 GST_OBJECT_UNLOCK (pad);
3695 GST_PAD_STREAM_UNLOCK (pad);
3696 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3699 case GST_FLOW_CUSTOM_SUCCESS:
3700 GST_DEBUG_OBJECT (pad, "dropped buffer");
3704 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3711 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3712 g_critical ("chain on pad %s:%s but it has no chainfunction",
3713 GST_DEBUG_PAD_NAME (pad));
3714 GST_PAD_STREAM_UNLOCK (pad);
3715 return GST_FLOW_NOT_SUPPORTED;
3721 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3722 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3725 * Chain a buffer to @pad.
3727 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3729 * If the buffer type is not acceptable for @pad (as negotiated with a
3730 * preceeding GST_EVENT_CAPS event), this function returns
3731 * #GST_FLOW_NOT_NEGOTIATED.
3733 * The function proceeds calling the chain function installed on @pad (see
3734 * gst_pad_set_chain_function()) and the return value of that function is
3735 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3738 * In all cases, success or failure, the caller loses its reference to @buffer
3739 * after calling this function.
3741 * Returns: a #GstFlowReturn from the pad.
3746 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3748 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3749 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3750 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3752 return gst_pad_chain_data_unchecked (pad,
3753 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3756 static GstFlowReturn
3757 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
3758 GstBufferList * list)
3764 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3766 len = gst_buffer_list_length (list);
3769 for (i = 0; i < len; i++) {
3770 buffer = gst_buffer_list_get (list, i);
3772 gst_pad_chain_data_unchecked (pad,
3773 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3774 gst_buffer_ref (buffer));
3775 if (ret != GST_FLOW_OK)
3778 gst_buffer_list_unref (list);
3784 * gst_pad_chain_list:
3785 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3786 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3789 * Chain a bufferlist to @pad.
3791 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3793 * If @pad was not negotiated properly with a CAPS event, this function
3794 * returns #GST_FLOW_NOT_NEGOTIATED.
3796 * The function proceeds calling the chainlist function installed on @pad (see
3797 * gst_pad_set_chain_list_function()) and the return value of that function is
3798 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3799 * chainlist function.
3801 * In all cases, success or failure, the caller loses its reference to @list
3802 * after calling this function.
3806 * Returns: a #GstFlowReturn from the pad.
3809 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3811 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3812 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3813 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3815 return gst_pad_chain_data_unchecked (pad,
3816 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3819 static GstFlowReturn
3820 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3825 GST_OBJECT_LOCK (pad);
3826 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3829 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
3832 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3835 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
3838 /* do block probes */
3839 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3841 /* recheck sticky events because the probe might have cause a relink */
3842 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
3845 /* do post-blocking probes */
3846 PROBE_PUSH (pad, type, data, probe_stopped);
3848 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3851 /* take ref to peer pad before releasing the lock */
3852 gst_object_ref (peer);
3854 GST_OBJECT_UNLOCK (pad);
3856 ret = gst_pad_chain_data_unchecked (peer, type, data);
3858 gst_object_unref (peer);
3860 GST_OBJECT_LOCK (pad);
3862 if (pad->priv->using == 0) {
3863 /* pad is not active anymore, trigger idle callbacks */
3864 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3865 probe_stopped, ret);
3867 GST_OBJECT_UNLOCK (pad);
3871 /* ERROR recovery here */
3875 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3876 "pushing, but pad was flushing");
3877 GST_OBJECT_UNLOCK (pad);
3878 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3879 return GST_FLOW_FLUSHING;
3883 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
3884 GST_OBJECT_UNLOCK (pad);
3885 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3886 return GST_FLOW_EOS;
3890 g_critical ("pushing on pad %s:%s but it was not activated in push mode",
3891 GST_DEBUG_PAD_NAME (pad));
3892 GST_OBJECT_UNLOCK (pad);
3893 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3894 return GST_FLOW_ERROR;
3898 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3899 "error pushing events, return %s", gst_flow_get_name (ret));
3900 GST_OBJECT_UNLOCK (pad);
3901 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3906 GST_OBJECT_UNLOCK (pad);
3907 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3910 case GST_FLOW_CUSTOM_SUCCESS:
3911 GST_DEBUG_OBJECT (pad, "dropped buffer");
3915 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3922 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3923 "pushing, but it was not linked");
3924 GST_OBJECT_UNLOCK (pad);
3925 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3926 return GST_FLOW_NOT_LINKED;
3932 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3933 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3936 * Pushes a buffer to the peer of @pad.
3938 * This function will call installed block probes before triggering any
3939 * installed data probes.
3941 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3942 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3945 * In all cases, success or failure, the caller loses its reference to @buffer
3946 * after calling this function.
3948 * Returns: a #GstFlowReturn from the peer pad.
3953 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3955 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3956 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3957 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3959 return gst_pad_push_data (pad,
3960 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3964 * gst_pad_push_list:
3965 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3966 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3969 * Pushes a buffer list to the peer of @pad.
3971 * This function will call installed block probes before triggering any
3972 * installed data probes.
3974 * The function proceeds calling the chain function on the peer pad and returns
3975 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3976 * be returned. If the peer pad does not have any installed chainlist function
3977 * every group buffer of the list will be merged into a normal #GstBuffer and
3978 * chained via gst_pad_chain().
3980 * In all cases, success or failure, the caller loses its reference to @list
3981 * after calling this function.
3983 * Returns: a #GstFlowReturn from the peer pad.
3988 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3990 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3991 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3992 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3994 return gst_pad_push_data (pad,
3995 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3998 static GstFlowReturn
3999 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
4000 GstBuffer ** buffer)
4003 GstPadGetRangeFunction getrangefunc;
4007 GST_PAD_STREAM_LOCK (pad);
4009 GST_OBJECT_LOCK (pad);
4010 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4013 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4016 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
4021 /* when one of the probes returns DROPPED, probe_stopped will be called
4022 * and we skip calling the getrange function, res_buf should then contain a
4023 * valid result buffer */
4024 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4025 res_buf, offset, size, probe_stopped);
4027 /* recheck sticky events because the probe might have cause a relink */
4028 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
4031 ACQUIRE_PARENT (pad, parent, no_parent);
4032 GST_OBJECT_UNLOCK (pad);
4034 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4037 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4038 "calling getrangefunc %s, offset %"
4039 G_GUINT64_FORMAT ", size %u",
4040 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4042 ret = getrangefunc (pad, parent, offset, size, &res_buf);
4044 RELEASE_PARENT (parent);
4046 if (G_UNLIKELY (ret != GST_FLOW_OK))
4047 goto get_range_failed;
4049 /* can only fire the signal if we have a valid buffer */
4050 GST_OBJECT_LOCK (pad);
4052 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4053 res_buf, offset, size, probe_stopped_unref);
4054 GST_OBJECT_UNLOCK (pad);
4056 GST_PAD_STREAM_UNLOCK (pad);
4065 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4066 "getrange, but pad was flushing");
4067 GST_OBJECT_UNLOCK (pad);
4068 GST_PAD_STREAM_UNLOCK (pad);
4069 return GST_FLOW_FLUSHING;
4073 g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4074 GST_DEBUG_PAD_NAME (pad));
4075 GST_OBJECT_UNLOCK (pad);
4076 GST_PAD_STREAM_UNLOCK (pad);
4077 return GST_FLOW_ERROR;
4081 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4082 GST_OBJECT_UNLOCK (pad);
4083 GST_PAD_STREAM_UNLOCK (pad);
4088 GST_DEBUG_OBJECT (pad, "no parent");
4089 GST_OBJECT_UNLOCK (pad);
4090 GST_PAD_STREAM_UNLOCK (pad);
4091 return GST_FLOW_FLUSHING;
4095 g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4096 GST_DEBUG_PAD_NAME (pad));
4097 RELEASE_PARENT (parent);
4098 GST_PAD_STREAM_UNLOCK (pad);
4099 return GST_FLOW_NOT_SUPPORTED;
4103 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4104 "probe returned %s", gst_flow_get_name (ret));
4105 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4107 /* the probe filled the buffer and asks us to not call the getrange
4108 * anymore, we continue with the post probes then. */
4109 GST_DEBUG_OBJECT (pad, "handled buffer");
4113 /* no buffer, we are EOS */
4114 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4118 GST_OBJECT_UNLOCK (pad);
4119 GST_PAD_STREAM_UNLOCK (pad);
4123 probe_stopped_unref:
4125 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4126 "probe returned %s", gst_flow_get_name (ret));
4127 /* if we drop here, it signals EOS */
4128 if (ret == GST_FLOW_CUSTOM_SUCCESS)
4130 GST_OBJECT_UNLOCK (pad);
4131 GST_PAD_STREAM_UNLOCK (pad);
4132 if (*buffer == NULL)
4133 gst_buffer_unref (res_buf);
4138 GST_PAD_STREAM_UNLOCK (pad);
4139 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4140 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4141 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4147 * gst_pad_get_range:
4148 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4149 * @offset: The start offset of the buffer
4150 * @size: The length of the buffer
4151 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4152 * returns #GST_FLOW_ERROR if %NULL.
4154 * When @pad is flushing this function returns #GST_FLOW_FLUSHING
4155 * immediately and @buffer is %NULL.
4157 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4158 * description of a getrange function. If @pad has no getrange function
4159 * installed (see gst_pad_set_getrange_function()) this function returns
4160 * #GST_FLOW_NOT_SUPPORTED.
4162 * If @buffer points to a variable holding NULL, a valid new #GstBuffer will be
4163 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4164 * must be freed with gst_buffer_unref() after usage.
4166 * When @buffer points to a variable that points to a valid #GstBuffer, the
4167 * buffer will be filled with the result data when this function returns
4168 * #GST_FLOW_OK. If the provided buffer is larger than @size, only
4169 * @size bytes will be filled in the result buffer and its size will be updated
4172 * Note that less than @size bytes can be returned in @buffer when, for example,
4173 * an EOS condition is near or when @buffer is not large enough to hold @size
4174 * bytes. The caller should check the result buffer size to get the result size.
4176 * When this function returns any other result value than #GST_FLOW_OK, @buffer
4177 * will be unchanged.
4179 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4181 * Returns: a #GstFlowReturn from the pad.
4186 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4187 GstBuffer ** buffer)
4189 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4190 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4191 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4192 g_return_val_if_fail (*buffer == NULL
4193 || GST_IS_BUFFER (*buffer), GST_FLOW_ERROR);
4195 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4199 * gst_pad_pull_range:
4200 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4201 * @offset: The start offset of the buffer
4202 * @size: The length of the buffer
4203 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4204 * GST_FLOW_ERROR if %NULL.
4206 * Pulls a @buffer from the peer pad or fills up a provided buffer.
4208 * This function will first trigger the pad block signal if it was
4211 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4212 * function returns the result of gst_pad_get_range() on the peer pad.
4213 * See gst_pad_get_range() for a list of return values and for the
4214 * semantics of the arguments of this function.
4216 * If @buffer points to a variable holding NULL, a valid new #GstBuffer will be
4217 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4218 * must be freed with gst_buffer_unref() after usage. When this function
4219 * returns any other result value, @buffer will still point to NULL.
4221 * When @buffer points to a variable that points to a valid #GstBuffer, the
4222 * buffer will be filled with the result data when this function returns
4223 * #GST_FLOW_OK. When this function returns any other result value,
4224 * @buffer will be unchanged. If the provided buffer is larger than @size, only
4225 * @size bytes will be filled in the result buffer and its size will be updated
4228 * Note that less than @size bytes can be returned in @buffer when, for example,
4229 * an EOS condition is near or when @buffer is not large enough to hold @size
4230 * bytes. The caller should check the result buffer size to get the result size.
4232 * Returns: a #GstFlowReturn from the peer pad.
4237 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4238 GstBuffer ** buffer)
4244 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4245 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4246 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4247 g_return_val_if_fail (*buffer == NULL
4248 || GST_IS_BUFFER (*buffer), GST_FLOW_ERROR);
4250 GST_OBJECT_LOCK (pad);
4251 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4254 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4259 /* when one of the probes returns DROPPED, probe_stopped will be
4260 * called and we skip calling the peer getrange function. *buffer should then
4261 * contain a valid buffer */
4262 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4263 res_buf, offset, size, probe_stopped);
4265 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4268 gst_object_ref (peer);
4270 GST_OBJECT_UNLOCK (pad);
4272 ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
4274 gst_object_unref (peer);
4276 GST_OBJECT_LOCK (pad);
4278 if (pad->priv->using == 0) {
4279 /* pad is not active anymore, trigger idle callbacks */
4280 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4281 probe_stopped_unref, ret);
4284 if (G_UNLIKELY (ret != GST_FLOW_OK))
4285 goto pull_range_failed;
4288 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4289 res_buf, offset, size, probe_stopped_unref);
4291 GST_OBJECT_UNLOCK (pad);
4297 /* ERROR recovery here */
4300 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4301 "pullrange, but pad was flushing");
4302 GST_OBJECT_UNLOCK (pad);
4303 return GST_FLOW_FLUSHING;
4307 g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
4308 GST_DEBUG_PAD_NAME (pad));
4309 GST_OBJECT_UNLOCK (pad);
4310 return GST_FLOW_ERROR;
4314 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4315 gst_flow_get_name (ret));
4316 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4318 /* the probe filled the buffer and asks us to not forward to the peer
4319 * anymore, we continue with the post probes then */
4320 GST_DEBUG_OBJECT (pad, "handled buffer");
4324 /* no buffer, we are EOS then */
4325 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4329 GST_OBJECT_UNLOCK (pad);
4334 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4335 "pulling range, but it was not linked");
4336 GST_OBJECT_UNLOCK (pad);
4337 return GST_FLOW_NOT_LINKED;
4341 GST_OBJECT_UNLOCK (pad);
4342 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4343 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4344 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4347 probe_stopped_unref:
4349 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4350 "post probe returned %s", gst_flow_get_name (ret));
4351 GST_OBJECT_UNLOCK (pad);
4352 /* if we drop here, it signals EOS */
4353 if (ret == GST_FLOW_CUSTOM_SUCCESS)
4355 if (*buffer == NULL)
4356 gst_buffer_unref (res_buf);
4362 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event, gboolean locked)
4367 gboolean res = FALSE;
4368 const gchar *name = NULL;
4370 type = GST_EVENT_TYPE (event);
4371 if (type & GST_EVENT_TYPE_STICKY_MULTI)
4372 name = gst_structure_get_name (gst_event_get_structure (event));
4374 events = pad->priv->events;
4377 for (i = 0; i < len; i++) {
4378 PadEvent *ev = &g_array_index (events, PadEvent, i);
4380 if (ev->event == NULL)
4383 if (type == GST_EVENT_TYPE (ev->event)) {
4384 /* matching types, check matching name if needed */
4385 if (name && !gst_event_has_name (ev->event, name))
4389 if ((res = gst_event_replace (&ev->event, event)))
4390 ev->received = FALSE;
4396 ev.event = gst_event_ref (event);
4397 ev.received = FALSE;
4398 g_array_append_val (events, ev);
4403 pad->priv->events_cookie++;
4404 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4406 GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4408 switch (GST_EVENT_TYPE (event)) {
4409 case GST_EVENT_CAPS:
4411 GST_OBJECT_UNLOCK (pad);
4413 GST_DEBUG_OBJECT (pad, "notify caps");
4414 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4417 GST_OBJECT_LOCK (pad);
4426 /* should be called with pad LOCK */
4427 static GstFlowReturn
4428 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
4429 GstPadProbeType type)
4433 GstEventType event_type;
4435 /* Two checks to be made:
4436 * . (un)set the FLUSHING flag for flushing events,
4437 * . handle pad blocking */
4438 event_type = GST_EVENT_TYPE (event);
4439 switch (event_type) {
4440 case GST_EVENT_FLUSH_START:
4441 GST_PAD_SET_FLUSHING (pad);
4443 GST_PAD_BLOCK_BROADCAST (pad);
4444 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4446 case GST_EVENT_FLUSH_STOP:
4447 GST_PAD_UNSET_FLUSHING (pad);
4449 /* Remove sticky EOS events */
4450 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4451 remove_event_by_type (pad, GST_EVENT_EOS);
4452 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4454 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4458 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4461 /* No need to check for EOS here as either the caller (gst_pad_push_event())
4462 * checked already or this is called as part of pushing sticky events,
4463 * in which case we still want to forward the EOS event downstream.
4466 switch (GST_EVENT_TYPE (event)) {
4467 case GST_EVENT_SEGMENT:
4468 /* pass the adjusted segment event on. We need to do this even if
4469 * there is no peer pad because of the probes. */
4470 event = apply_pad_offset (pad, event);
4472 case GST_EVENT_RECONFIGURE:
4473 if (GST_PAD_IS_SINK (pad))
4474 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4479 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4480 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4485 /* send probes after modifying the events above */
4486 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4488 /* now check the peer pad */
4489 peerpad = GST_PAD_PEER (pad);
4490 if (peerpad == NULL)
4493 gst_object_ref (peerpad);
4495 GST_OBJECT_UNLOCK (pad);
4497 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4498 event, GST_EVENT_TYPE_NAME (event), peerpad);
4500 ret = gst_pad_send_event_unchecked (peerpad, event, type);
4502 /* Note: we gave away ownership of the event at this point but we can still
4503 * print the old pointer */
4504 GST_LOG_OBJECT (pad, "sent event %p to peerpad %" GST_PTR_FORMAT ", ret %s",
4505 event, peerpad, gst_flow_get_name (ret));
4507 gst_object_unref (peerpad);
4509 GST_OBJECT_LOCK (pad);
4511 if (pad->priv->using == 0) {
4512 /* pad is not active anymore, trigger idle callbacks */
4513 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4514 idle_probe_stopped, ret);
4518 /* ERROR handling */
4521 GST_DEBUG_OBJECT (pad, "We're flushing");
4522 gst_event_unref (event);
4523 return GST_FLOW_FLUSHING;
4527 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4528 gst_event_unref (event);
4531 case GST_FLOW_CUSTOM_SUCCESS:
4532 GST_DEBUG_OBJECT (pad, "dropped event");
4536 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4543 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4544 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4545 gst_event_unref (event);
4547 /* unlinked pads should not influence latency configuration */
4548 if (event_type == GST_EVENT_LATENCY)
4551 return GST_FLOW_NOT_LINKED;
4555 GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
4561 * gst_pad_push_event:
4562 * @pad: a #GstPad to push the event to.
4563 * @event: (transfer full): the #GstEvent to send to the pad.
4565 * Sends the event to the peer of the given pad. This function is
4566 * mainly used by elements to send events to their peer
4569 * This function takes owership of the provided event so you should
4570 * gst_event_ref() it if you want to reuse the event after this call.
4572 * Returns: TRUE if the event was handled.
4577 gst_pad_push_event (GstPad * pad, GstEvent * event)
4579 gboolean res = FALSE;
4580 GstPadProbeType type;
4581 gboolean sticky, serialized;
4583 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4584 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4586 if (GST_PAD_IS_SRC (pad)) {
4587 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4588 goto wrong_direction;
4589 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4590 } else if (GST_PAD_IS_SINK (pad)) {
4591 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4592 goto wrong_direction;
4593 /* events pushed on sinkpad never are sticky */
4594 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4596 goto unknown_direction;
4598 GST_OBJECT_LOCK (pad);
4599 sticky = GST_EVENT_IS_STICKY (event);
4600 serialized = GST_EVENT_IS_SERIALIZED (event);
4603 /* can't store on flushing pads */
4604 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4607 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4610 /* srcpad sticky events are store immediately, the received flag is set
4611 * to FALSE and will be set to TRUE when we can successfully push the
4612 * event to the peer pad */
4613 if (gst_pad_store_sticky_event (pad, event, TRUE)) {
4614 GST_DEBUG_OBJECT (pad, "event %s updated", GST_EVENT_TYPE_NAME (event));
4616 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS)
4617 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
4619 if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
4620 /* all serialized or sticky events on the srcpad trigger push of
4622 res = (check_sticky (pad) == GST_FLOW_OK);
4625 /* other events are pushed right away */
4626 res = (gst_pad_push_event_unchecked (pad, event, type) == GST_FLOW_OK);
4628 /* Errors in sticky event pushing are no problem and ignored here
4629 * as they will cause more meaningful errors during data flow.
4630 * For EOS events, that are not followed by data flow, we still
4631 * return FALSE here though.
4633 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
4635 gst_event_unref (event);
4637 GST_OBJECT_UNLOCK (pad);
4641 /* ERROR handling */
4644 g_warning ("pad %s:%s pushing %s event in wrong direction",
4645 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4646 gst_event_unref (event);
4651 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4652 gst_event_unref (event);
4657 GST_DEBUG_OBJECT (pad, "We're flushing");
4658 GST_OBJECT_UNLOCK (pad);
4659 gst_event_unref (event);
4664 GST_DEBUG_OBJECT (pad, "We're EOS");
4665 GST_OBJECT_UNLOCK (pad);
4666 gst_event_unref (event);
4671 /* Check if we can call the event function with the given event */
4672 static GstFlowReturn
4673 pre_eventfunc_check (GstPad * pad, GstEvent * event)
4677 switch (GST_EVENT_TYPE (event)) {
4678 case GST_EVENT_CAPS:
4680 /* backwards compatibility mode for caps */
4681 gst_event_parse_caps (event, &caps);
4683 if (!gst_pad_query_accept_caps (pad, caps))
4695 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4696 "caps %" GST_PTR_FORMAT " not accepted", caps);
4697 return GST_FLOW_NOT_NEGOTIATED;
4701 static GstFlowReturn
4702 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
4703 GstPadProbeType type)
4706 GstEventType event_type;
4707 gboolean serialized, need_unlock = FALSE, sticky;
4708 GstPadEventFunction eventfunc;
4711 GST_OBJECT_LOCK (pad);
4712 if (GST_PAD_IS_SINK (pad))
4713 serialized = GST_EVENT_IS_SERIALIZED (event);
4716 sticky = GST_EVENT_IS_STICKY (event);
4717 event_type = GST_EVENT_TYPE (event);
4718 switch (event_type) {
4719 case GST_EVENT_FLUSH_START:
4720 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4721 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4723 /* can't even accept a flush begin event when flushing */
4724 if (GST_PAD_IS_FLUSHING (pad))
4727 GST_PAD_SET_FLUSHING (pad);
4728 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4730 case GST_EVENT_FLUSH_STOP:
4731 if (G_LIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_NONE)) {
4732 GST_PAD_UNSET_FLUSHING (pad);
4733 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4735 /* Remove pending EOS events */
4736 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4737 remove_event_by_type (pad, GST_EVENT_EOS);
4738 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4740 GST_OBJECT_UNLOCK (pad);
4741 /* grab stream lock */
4742 GST_PAD_STREAM_LOCK (pad);
4744 GST_OBJECT_LOCK (pad);
4745 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4748 case GST_EVENT_RECONFIGURE:
4749 if (GST_PAD_IS_SRC (pad))
4750 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4752 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4753 "have event type %" GST_PTR_FORMAT, event);
4755 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4759 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4762 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4763 GST_OBJECT_UNLOCK (pad);
4764 GST_PAD_STREAM_LOCK (pad);
4766 GST_OBJECT_LOCK (pad);
4767 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4770 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4774 switch (GST_EVENT_TYPE (event)) {
4775 case GST_EVENT_SEGMENT:
4776 event = apply_pad_offset (pad, event);
4782 /* now do the probe */
4784 type | GST_PAD_PROBE_TYPE_PUSH |
4785 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4787 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4791 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4794 ACQUIRE_PARENT (pad, parent, no_parent);
4795 GST_OBJECT_UNLOCK (pad);
4797 ret = pre_eventfunc_check (pad, event);
4798 if (G_UNLIKELY (ret != GST_FLOW_OK))
4799 goto precheck_failed;
4802 gst_event_ref (event);
4804 if (eventfunc (pad, parent, event)) {
4807 /* something went wrong */
4808 switch (event_type) {
4809 case GST_EVENT_CAPS:
4810 ret = GST_FLOW_NOT_NEGOTIATED;
4813 ret = GST_FLOW_ERROR;
4817 RELEASE_PARENT (parent);
4819 GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
4822 if (ret == GST_FLOW_OK) {
4823 /* after the event function accepted the event, we can store the sticky
4824 * event on the pad */
4825 gst_pad_store_sticky_event (pad, event, FALSE);
4826 if (event_type == GST_EVENT_EOS)
4827 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
4829 gst_event_unref (event);
4833 GST_PAD_STREAM_UNLOCK (pad);
4837 /* ERROR handling */
4840 GST_OBJECT_UNLOCK (pad);
4842 GST_PAD_STREAM_UNLOCK (pad);
4843 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4844 "Received event on flushing pad. Discarding");
4845 gst_event_unref (event);
4846 return GST_FLOW_FLUSHING;
4850 GST_OBJECT_UNLOCK (pad);
4852 GST_PAD_STREAM_UNLOCK (pad);
4853 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4854 "Received event on EOS pad. Discarding");
4855 gst_event_unref (event);
4856 return GST_FLOW_EOS;
4860 GST_OBJECT_UNLOCK (pad);
4862 GST_PAD_STREAM_UNLOCK (pad);
4863 gst_event_unref (event);
4866 case GST_FLOW_CUSTOM_SUCCESS:
4867 GST_DEBUG_OBJECT (pad, "dropped event");
4871 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4878 g_warning ("pad %s:%s has no event handler, file a bug.",
4879 GST_DEBUG_PAD_NAME (pad));
4880 GST_OBJECT_UNLOCK (pad);
4882 GST_PAD_STREAM_UNLOCK (pad);
4883 gst_event_unref (event);
4884 return GST_FLOW_NOT_SUPPORTED;
4888 GST_DEBUG_OBJECT (pad, "no parent");
4889 GST_OBJECT_UNLOCK (pad);
4891 GST_PAD_STREAM_UNLOCK (pad);
4892 gst_event_unref (event);
4893 return GST_FLOW_FLUSHING;
4897 GST_DEBUG_OBJECT (pad, "pre event check failed");
4898 RELEASE_PARENT (parent);
4900 GST_PAD_STREAM_UNLOCK (pad);
4901 gst_event_unref (event);
4907 * gst_pad_send_event:
4908 * @pad: a #GstPad to send the event to.
4909 * @event: (transfer full): the #GstEvent to send to the pad.
4911 * Sends the event to the pad. This function can be used
4912 * by applications to send events in the pipeline.
4914 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4915 * sink pad, @event should be a downstream event. For example, you would not
4916 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4917 * Furthermore, some downstream events have to be serialized with data flow,
4918 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4919 * the event needs to be serialized with data flow, this function will take the
4920 * pad's stream lock while calling its event function.
4922 * To find out whether an event type is upstream, downstream, or downstream and
4923 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4924 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4925 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4926 * plugin doesn't need to bother itself with this information; the core handles
4927 * all necessary locks and checks.
4929 * This function takes owership of the provided event so you should
4930 * gst_event_ref() it if you want to reuse the event after this call.
4932 * Returns: TRUE if the event was handled.
4935 gst_pad_send_event (GstPad * pad, GstEvent * event)
4938 GstPadProbeType type;
4940 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4941 g_return_val_if_fail (event != NULL, FALSE);
4943 if (GST_PAD_IS_SINK (pad)) {
4944 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4945 goto wrong_direction;
4946 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4947 } else if (GST_PAD_IS_SRC (pad)) {
4948 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4949 goto wrong_direction;
4950 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4952 goto unknown_direction;
4954 if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
4961 /* ERROR handling */
4964 g_warning ("pad %s:%s sending %s event in wrong direction",
4965 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4966 gst_event_unref (event);
4971 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4972 gst_event_unref (event);
4978 * gst_pad_set_element_private:
4979 * @pad: the #GstPad to set the private data of.
4980 * @priv: The private data to attach to the pad.
4982 * Set the given private data gpointer on the pad.
4983 * This function can only be used by the element that owns the pad.
4984 * No locking is performed in this function.
4987 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4989 pad->element_private = priv;
4993 * gst_pad_get_element_private:
4994 * @pad: the #GstPad to get the private data of.
4996 * Gets the private data of a pad.
4997 * No locking is performed in this function.
4999 * Returns: (transfer none): a #gpointer to the private data.
5002 gst_pad_get_element_private (GstPad * pad)
5004 return pad->element_private;
5008 * gst_pad_get_sticky_event:
5009 * @pad: the #GstPad to get the event from.
5010 * @event_type: the #GstEventType that should be retrieved.
5011 * @idx: the index of the event
5013 * Returns a new reference of the sticky event of type @event_type
5016 * Returns: (transfer full): a #GstEvent of type @event_type or NULL when no
5017 * event of @event_type was on @pad. Unref after usage.
5020 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
5022 GstEvent *event = NULL;
5025 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
5026 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
5028 GST_OBJECT_LOCK (pad);
5029 ev = find_event_by_type (pad, event_type, idx);
5030 if (ev && (event = ev->event))
5031 gst_event_ref (event);
5032 GST_OBJECT_UNLOCK (pad);
5039 GstPadStickyEventsForeachFunction func;
5044 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
5046 ForeachDispatch *data = user_data;
5049 GST_OBJECT_UNLOCK (pad);
5051 ret = data->func (pad, &ev->event, data->user_data);
5053 GST_OBJECT_LOCK (pad);
5059 * gst_pad_sticky_events_foreach:
5060 * @pad: the #GstPad that should be used for iteration.
5061 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
5062 * should be called for every event.
5063 * @user_data: (closure): the optional user data.
5065 * Iterates all sticky events on @pad and calls @foreach_func for every
5066 * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
5069 gst_pad_sticky_events_foreach (GstPad * pad,
5070 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
5072 ForeachDispatch data;
5074 g_return_if_fail (GST_IS_PAD (pad));
5075 g_return_if_fail (foreach_func != NULL);
5077 data.func = foreach_func;
5078 data.user_data = user_data;
5080 GST_OBJECT_LOCK (pad);
5081 events_foreach (pad, foreach_dispatch_function, &data);
5082 GST_OBJECT_UNLOCK (pad);
5086 do_stream_status (GstPad * pad, GstStreamStatusType type,
5087 GThread * thread, GstTask * task)
5091 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5093 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5094 if (GST_IS_ELEMENT (parent)) {
5095 GstMessage *message;
5096 GValue value = { 0 };
5098 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5099 gchar *tname, *ename, *pname;
5101 /* create a good task name */
5102 ename = gst_element_get_name (parent);
5103 pname = gst_pad_get_name (pad);
5104 tname = g_strdup_printf ("%s:%s", ename, pname);
5108 gst_object_set_name (GST_OBJECT_CAST (task), tname);
5112 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5115 g_value_init (&value, GST_TYPE_TASK);
5116 g_value_set_object (&value, task);
5117 gst_message_set_stream_status_object (message, &value);
5118 g_value_unset (&value);
5120 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5121 gst_element_post_message (parent, message);
5123 gst_object_unref (parent);
5128 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5130 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5135 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5137 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5142 * gst_pad_start_task:
5143 * @pad: the #GstPad to start the task of
5144 * @func: the task function to call
5145 * @user_data: user data passed to the task function
5146 * @notify: called when @user_data is no longer referenced
5148 * Starts a task that repeatedly calls @func with @user_data. This function
5149 * is mostly used in pad activation functions to start the dataflow.
5150 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5151 * before @func is called.
5153 * Returns: a %TRUE if the task could be started.
5156 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
5157 GDestroyNotify notify)
5162 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5163 g_return_val_if_fail (func != NULL, FALSE);
5165 GST_DEBUG_OBJECT (pad, "start task");
5167 GST_OBJECT_LOCK (pad);
5168 task = GST_PAD_TASK (pad);
5170 task = gst_task_new (func, user_data, notify);
5171 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5172 gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
5173 gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
5174 GST_INFO_OBJECT (pad, "created task %p", task);
5175 GST_PAD_TASK (pad) = task;
5176 gst_object_ref (task);
5177 /* release lock to post the message */
5178 GST_OBJECT_UNLOCK (pad);
5180 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5182 gst_object_unref (task);
5184 GST_OBJECT_LOCK (pad);
5185 /* nobody else is supposed to have changed the pad now */
5186 if (GST_PAD_TASK (pad) != task)
5187 goto concurrent_stop;
5189 res = gst_task_set_state (task, GST_TASK_STARTED);
5190 GST_OBJECT_UNLOCK (pad);
5197 GST_OBJECT_UNLOCK (pad);
5203 * gst_pad_pause_task:
5204 * @pad: the #GstPad to pause the task of
5206 * Pause the task of @pad. This function will also wait until the
5207 * function executed by the task is finished if this function is not
5208 * called from the task function.
5210 * Returns: a TRUE if the task could be paused or FALSE when the pad
5214 gst_pad_pause_task (GstPad * pad)
5219 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5221 GST_DEBUG_OBJECT (pad, "pause task");
5223 GST_OBJECT_LOCK (pad);
5224 task = GST_PAD_TASK (pad);
5227 res = gst_task_set_state (task, GST_TASK_PAUSED);
5228 GST_OBJECT_UNLOCK (pad);
5230 /* wait for task function to finish, this lock is recursive so it does nothing
5231 * when the pause is called from the task itself */
5232 GST_PAD_STREAM_LOCK (pad);
5233 GST_PAD_STREAM_UNLOCK (pad);
5239 GST_DEBUG_OBJECT (pad, "pad has no task");
5240 GST_OBJECT_UNLOCK (pad);
5246 * gst_pad_stop_task:
5247 * @pad: the #GstPad to stop the task of
5249 * Stop the task of @pad. This function will also make sure that the
5250 * function executed by the task will effectively stop if not called
5251 * from the GstTaskFunction.
5253 * This function will deadlock if called from the GstTaskFunction of
5254 * the task. Use gst_task_pause() instead.
5256 * Regardless of whether the pad has a task, the stream lock is acquired and
5257 * released so as to ensure that streaming through this pad has finished.
5259 * Returns: a TRUE if the task could be stopped or FALSE on error.
5262 gst_pad_stop_task (GstPad * pad)
5267 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5269 GST_DEBUG_OBJECT (pad, "stop task");
5271 GST_OBJECT_LOCK (pad);
5272 task = GST_PAD_TASK (pad);
5275 GST_PAD_TASK (pad) = NULL;
5276 res = gst_task_set_state (task, GST_TASK_STOPPED);
5277 GST_OBJECT_UNLOCK (pad);
5279 GST_PAD_STREAM_LOCK (pad);
5280 GST_PAD_STREAM_UNLOCK (pad);
5282 if (!gst_task_join (task))
5285 gst_object_unref (task);
5291 GST_DEBUG_OBJECT (pad, "no task");
5292 GST_OBJECT_UNLOCK (pad);
5294 GST_PAD_STREAM_LOCK (pad);
5295 GST_PAD_STREAM_UNLOCK (pad);
5297 /* this is not an error */
5302 /* this is bad, possibly the application tried to join the task from
5303 * the task's thread. We install the task again so that it will be stopped
5304 * again from the right thread next time hopefully. */
5305 GST_OBJECT_LOCK (pad);
5306 GST_DEBUG_OBJECT (pad, "join failed");
5307 /* we can only install this task if there was no other task */
5308 if (GST_PAD_TASK (pad) == NULL)
5309 GST_PAD_TASK (pad) = task;
5310 GST_OBJECT_UNLOCK (pad);