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 GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1841 if (GST_PAD_UNLINKFUNC (sinkpad)) {
1842 GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1845 /* first clear peers */
1846 GST_PAD_PEER (srcpad) = NULL;
1847 GST_PAD_PEER (sinkpad) = NULL;
1849 GST_OBJECT_UNLOCK (sinkpad);
1850 GST_OBJECT_UNLOCK (srcpad);
1852 /* fire off a signal to each of the pads telling them
1853 * that they've been unlinked */
1854 g_signal_emit (srcpad, gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1855 g_signal_emit (sinkpad, gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1857 GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1858 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1863 if (parent != NULL) {
1864 gst_element_post_message (parent,
1865 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
1866 GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1867 gst_object_unref (parent);
1872 not_linked_together:
1874 /* we do not emit a warning in this case because unlinking cannot
1875 * be made MT safe.*/
1876 GST_OBJECT_UNLOCK (sinkpad);
1877 GST_OBJECT_UNLOCK (srcpad);
1883 * gst_pad_is_linked:
1884 * @pad: pad to check
1886 * Checks if a @pad is linked to another pad or not.
1888 * Returns: TRUE if the pad is linked, FALSE otherwise.
1893 gst_pad_is_linked (GstPad * pad)
1897 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1899 GST_OBJECT_LOCK (pad);
1900 result = (GST_PAD_PEER (pad) != NULL);
1901 GST_OBJECT_UNLOCK (pad);
1906 /* get the caps from both pads and see if the intersection
1909 * This function should be called with the pad LOCK on both
1913 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink,
1914 GstPadLinkCheck flags)
1916 GstCaps *srccaps = NULL;
1917 GstCaps *sinkcaps = NULL;
1918 gboolean compatible = FALSE;
1920 if (!(flags & (GST_PAD_LINK_CHECK_CAPS | GST_PAD_LINK_CHECK_TEMPLATE_CAPS)))
1923 /* Doing the expensive caps checking takes priority over only checking the template caps */
1924 if (flags & GST_PAD_LINK_CHECK_CAPS) {
1925 GST_OBJECT_UNLOCK (sink);
1926 GST_OBJECT_UNLOCK (src);
1928 srccaps = gst_pad_query_caps (src, NULL);
1929 sinkcaps = gst_pad_query_caps (sink, NULL);
1931 GST_OBJECT_LOCK (src);
1932 GST_OBJECT_LOCK (sink);
1934 /* If one of the two pads doesn't have a template, consider the intersection
1936 if (G_UNLIKELY ((GST_PAD_PAD_TEMPLATE (src) == NULL)
1937 || (GST_PAD_PAD_TEMPLATE (sink) == NULL))) {
1941 srccaps = gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (src)));
1943 gst_caps_ref (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (sink)));
1946 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, src, "src caps %" GST_PTR_FORMAT,
1948 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, sink, "sink caps %" GST_PTR_FORMAT,
1951 /* if we have caps on both pads we can check the intersection. If one
1952 * of the caps is NULL, we return TRUE. */
1953 if (G_UNLIKELY (srccaps == NULL || sinkcaps == NULL)) {
1955 gst_caps_unref (srccaps);
1957 gst_caps_unref (sinkcaps);
1961 compatible = gst_caps_can_intersect (srccaps, sinkcaps);
1962 gst_caps_unref (srccaps);
1963 gst_caps_unref (sinkcaps);
1966 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
1967 (compatible ? "" : "not"));
1972 /* check if the grandparents of both pads are the same.
1973 * This check is required so that we don't try to link
1974 * pads from elements in different bins without ghostpads.
1976 * The LOCK should be held on both pads
1979 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1981 GstObject *psrc, *psink;
1983 psrc = GST_OBJECT_PARENT (src);
1984 psink = GST_OBJECT_PARENT (sink);
1986 /* if one of the pads has no parent, we allow the link */
1987 if (G_UNLIKELY (psrc == NULL || psink == NULL))
1990 /* only care about parents that are elements */
1991 if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1992 goto no_element_parent;
1994 /* if the parents are the same, we have a loop */
1995 if (G_UNLIKELY (psrc == psink))
1998 /* if they both have a parent, we check the grandparents. We can not lock
1999 * the parent because we hold on the child (pad) and the locking order is
2000 * parent >> child. */
2001 psrc = GST_OBJECT_PARENT (psrc);
2002 psink = GST_OBJECT_PARENT (psink);
2004 /* if they have grandparents but they are not the same */
2005 if (G_UNLIKELY (psrc != psink))
2006 goto wrong_grandparents;
2013 GST_CAT_DEBUG (GST_CAT_CAPS,
2014 "one of the pads has no parent %" GST_PTR_FORMAT " and %"
2015 GST_PTR_FORMAT, psrc, psink);
2020 GST_CAT_DEBUG (GST_CAT_CAPS,
2021 "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
2022 GST_PTR_FORMAT, psrc, psink);
2027 GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
2033 GST_CAT_DEBUG (GST_CAT_CAPS,
2034 "pads have different grandparents %" GST_PTR_FORMAT " and %"
2035 GST_PTR_FORMAT, psrc, psink);
2040 /* FIXME leftover from an attempt at refactoring... */
2041 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
2042 * the two pads will be locked in the srcpad, sinkpad order. */
2043 static GstPadLinkReturn
2044 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2046 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
2047 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2049 GST_OBJECT_LOCK (srcpad);
2051 if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
2052 goto src_was_linked;
2054 GST_OBJECT_LOCK (sinkpad);
2056 if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
2057 goto sink_was_linked;
2059 /* check hierarchy, pads can only be linked if the grandparents
2061 if ((flags & GST_PAD_LINK_CHECK_HIERARCHY)
2062 && !gst_pad_link_check_hierarchy (srcpad, sinkpad))
2063 goto wrong_hierarchy;
2065 /* check pad caps for non-empty intersection */
2066 if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad, flags))
2069 /* FIXME check pad scheduling for non-empty intersection */
2071 return GST_PAD_LINK_OK;
2075 GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
2076 GST_DEBUG_PAD_NAME (srcpad),
2077 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
2078 /* we do not emit a warning in this case because unlinking cannot
2079 * be made MT safe.*/
2080 GST_OBJECT_UNLOCK (srcpad);
2081 return GST_PAD_LINK_WAS_LINKED;
2085 GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
2086 GST_DEBUG_PAD_NAME (sinkpad),
2087 GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
2088 /* we do not emit a warning in this case because unlinking cannot
2089 * be made MT safe.*/
2090 GST_OBJECT_UNLOCK (sinkpad);
2091 GST_OBJECT_UNLOCK (srcpad);
2092 return GST_PAD_LINK_WAS_LINKED;
2096 GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
2097 GST_OBJECT_UNLOCK (sinkpad);
2098 GST_OBJECT_UNLOCK (srcpad);
2099 return GST_PAD_LINK_WRONG_HIERARCHY;
2103 GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
2104 GST_OBJECT_UNLOCK (sinkpad);
2105 GST_OBJECT_UNLOCK (srcpad);
2106 return GST_PAD_LINK_NOFORMAT;
2112 * @srcpad: the source #GstPad.
2113 * @sinkpad: the sink #GstPad.
2115 * Checks if the source pad and the sink pad are compatible so they can be
2118 * Returns: TRUE if the pads can be linked.
2121 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2123 GstPadLinkReturn result;
2125 /* generic checks */
2126 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2127 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2129 GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
2130 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2132 /* gst_pad_link_prepare does everything for us, we only release the locks
2133 * on the pads that it gets us. If this function returns !OK the locks are not
2135 result = gst_pad_link_prepare (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2136 if (result != GST_PAD_LINK_OK)
2139 GST_OBJECT_UNLOCK (srcpad);
2140 GST_OBJECT_UNLOCK (sinkpad);
2143 return result == GST_PAD_LINK_OK;
2147 * gst_pad_link_full:
2148 * @srcpad: the source #GstPad to link.
2149 * @sinkpad: the sink #GstPad to link.
2150 * @flags: the checks to validate when linking
2152 * Links the source pad and the sink pad.
2154 * This variant of #gst_pad_link provides a more granular control on the
2155 * checks being done when linking. While providing some considerable speedups
2156 * the caller of this method must be aware that wrong usage of those flags
2157 * can cause severe issues. Refer to the documentation of #GstPadLinkCheck
2158 * for more information.
2162 * Returns: A result code indicating if the connection worked or
2166 gst_pad_link_full (GstPad * srcpad, GstPad * sinkpad, GstPadLinkCheck flags)
2168 GstPadLinkReturn result;
2170 GstPadLinkFunction srcfunc, sinkfunc;
2172 g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
2173 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
2174 g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
2175 g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
2176 GST_PAD_LINK_WRONG_DIRECTION);
2178 /* Notify the parent early. See gst_pad_unlink for details. */
2179 if (G_LIKELY ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad))))) {
2180 if (G_LIKELY (GST_IS_ELEMENT (parent))) {
2181 gst_element_post_message (parent,
2182 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2183 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2185 gst_object_unref (parent);
2190 /* prepare will also lock the two pads */
2191 result = gst_pad_link_prepare (srcpad, sinkpad, flags);
2193 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2196 /* must set peers before calling the link function */
2197 GST_PAD_PEER (srcpad) = sinkpad;
2198 GST_PAD_PEER (sinkpad) = srcpad;
2200 /* check events, when something is different, mark pending */
2201 schedule_events (srcpad, sinkpad);
2203 /* get the link functions */
2204 srcfunc = GST_PAD_LINKFUNC (srcpad);
2205 sinkfunc = GST_PAD_LINKFUNC (sinkpad);
2207 if (G_UNLIKELY (srcfunc || sinkfunc)) {
2208 /* custom link functions, execute them */
2209 GST_OBJECT_UNLOCK (sinkpad);
2210 GST_OBJECT_UNLOCK (srcpad);
2213 /* this one will call the peer link function */
2214 result = srcfunc (srcpad, sinkpad);
2215 } else if (sinkfunc) {
2216 /* if no source link function, we need to call the sink link
2217 * function ourselves. */
2218 result = sinkfunc (sinkpad, srcpad);
2221 GST_OBJECT_LOCK (srcpad);
2222 GST_OBJECT_LOCK (sinkpad);
2224 /* we released the lock, check if the same pads are linked still */
2225 if (GST_PAD_PEER (srcpad) != sinkpad || GST_PAD_PEER (sinkpad) != srcpad)
2226 goto concurrent_link;
2228 if (G_UNLIKELY (result != GST_PAD_LINK_OK))
2231 GST_OBJECT_UNLOCK (sinkpad);
2232 GST_OBJECT_UNLOCK (srcpad);
2234 /* fire off a signal to each of the pads telling them
2235 * that they've been linked */
2236 g_signal_emit (srcpad, gst_pad_signals[PAD_LINKED], 0, sinkpad);
2237 g_signal_emit (sinkpad, gst_pad_signals[PAD_LINKED], 0, srcpad);
2239 GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2240 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2242 gst_pad_send_event (srcpad, gst_event_new_reconfigure ());
2245 if (G_LIKELY (parent)) {
2246 gst_element_post_message (parent,
2247 gst_message_new_structure_change (GST_OBJECT_CAST (sinkpad),
2248 GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2249 gst_object_unref (parent);
2257 GST_CAT_INFO (GST_CAT_PADS, "concurrent link between %s:%s and %s:%s",
2258 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2259 GST_OBJECT_UNLOCK (sinkpad);
2260 GST_OBJECT_UNLOCK (srcpad);
2262 /* The other link operation succeeded first */
2263 result = GST_PAD_LINK_WAS_LINKED;
2268 GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2269 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2271 GST_PAD_PEER (srcpad) = NULL;
2272 GST_PAD_PEER (sinkpad) = NULL;
2274 GST_OBJECT_UNLOCK (sinkpad);
2275 GST_OBJECT_UNLOCK (srcpad);
2283 * @srcpad: the source #GstPad to link.
2284 * @sinkpad: the sink #GstPad to link.
2286 * Links the source pad and the sink pad.
2288 * Returns: A result code indicating if the connection worked or
2294 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
2296 return gst_pad_link_full (srcpad, sinkpad, GST_PAD_LINK_CHECK_DEFAULT);
2300 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2302 GstPadTemplate **template_p;
2304 /* this function would need checks if it weren't static */
2306 GST_OBJECT_LOCK (pad);
2307 template_p = &pad->padtemplate;
2308 gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2309 GST_OBJECT_UNLOCK (pad);
2312 gst_pad_template_pad_created (templ, pad);
2316 * gst_pad_get_pad_template:
2319 * Gets the template for @pad.
2321 * Returns: (transfer full): the #GstPadTemplate from which this pad was
2322 * instantiated, or %NULL if this pad has no template. Unref after
2326 gst_pad_get_pad_template (GstPad * pad)
2328 GstPadTemplate *templ;
2330 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2332 templ = GST_PAD_PAD_TEMPLATE (pad);
2334 return (templ ? gst_object_ref (templ) : NULL);
2338 * gst_pad_has_current_caps:
2339 * @pad: a #GstPad to check
2341 * Check if @pad has caps set on it with a #GST_EVENT_CAPS event.
2343 * Returns: TRUE when @pad has caps associated with it.
2346 gst_pad_has_current_caps (GstPad * pad)
2351 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2353 GST_OBJECT_LOCK (pad);
2354 caps = get_pad_caps (pad);
2355 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2356 "check current pad caps %" GST_PTR_FORMAT, caps);
2357 result = (caps != NULL);
2358 GST_OBJECT_UNLOCK (pad);
2364 * gst_pad_get_current_caps:
2365 * @pad: a #GstPad to get the current capabilities of.
2367 * Gets the capabilities currently configured on @pad with the last
2368 * #GST_EVENT_CAPS event.
2370 * Returns: the current caps of the pad with incremented ref-count.
2373 gst_pad_get_current_caps (GstPad * pad)
2377 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2379 GST_OBJECT_LOCK (pad);
2380 if ((result = get_pad_caps (pad)))
2381 gst_caps_ref (result);
2382 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2383 "get current pad caps %" GST_PTR_FORMAT, result);
2384 GST_OBJECT_UNLOCK (pad);
2390 * gst_pad_get_pad_template_caps:
2391 * @pad: a #GstPad to get the template capabilities from.
2393 * Gets the capabilities for @pad's template.
2395 * Returns: (transfer full): the #GstCaps of this pad template.
2396 * Unref after usage.
2399 gst_pad_get_pad_template_caps (GstPad * pad)
2401 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2403 if (GST_PAD_PAD_TEMPLATE (pad))
2404 return gst_pad_template_get_caps (GST_PAD_PAD_TEMPLATE (pad));
2406 return gst_caps_ref (GST_CAPS_ANY);
2411 * @pad: a #GstPad to get the peer of.
2413 * Gets the peer of @pad. This function refs the peer pad so
2414 * you need to unref it after use.
2416 * Returns: (transfer full): the peer #GstPad. Unref after usage.
2421 gst_pad_get_peer (GstPad * pad)
2425 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2427 GST_OBJECT_LOCK (pad);
2428 result = GST_PAD_PEER (pad);
2430 gst_object_ref (result);
2431 GST_OBJECT_UNLOCK (pad);
2437 * gst_pad_get_allowed_caps:
2440 * Gets the capabilities of the allowed media types that can flow through
2441 * @pad and its peer.
2443 * The allowed capabilities is calculated as the intersection of the results of
2444 * calling gst_pad_query_caps() on @pad and its peer. The caller owns a reference
2445 * on the resulting caps.
2447 * Returns: (transfer full): the allowed #GstCaps of the pad link. Unref the
2448 * caps when you no longer need it. This function returns NULL when @pad
2454 gst_pad_get_allowed_caps (GstPad * pad)
2461 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2463 GST_OBJECT_LOCK (pad);
2464 peer = GST_PAD_PEER (pad);
2465 if (G_UNLIKELY (peer == NULL))
2468 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2470 gst_object_ref (peer);
2471 GST_OBJECT_UNLOCK (pad);
2472 mycaps = gst_pad_query_caps (pad, NULL);
2474 peercaps = gst_pad_query_caps (peer, NULL);
2475 gst_object_unref (peer);
2477 caps = gst_caps_intersect (mycaps, peercaps);
2478 gst_caps_unref (peercaps);
2479 gst_caps_unref (mycaps);
2481 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2488 GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2489 GST_OBJECT_UNLOCK (pad);
2496 * gst_pad_iterate_internal_links_default:
2497 * @pad: the #GstPad to get the internal links of.
2498 * @parent: the parent of @pad or NULL
2500 * Iterate the list of pads to which the given pad is linked to inside of
2501 * the parent element.
2502 * This is the default handler, and thus returns an iterator of all of the
2503 * pads inside the parent element with opposite direction.
2505 * The caller must free this iterator after use with gst_iterator_free().
2507 * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
2508 * returned pad with gst_object_unref().
2511 gst_pad_iterate_internal_links_default (GstPad * pad, GstObject * parent)
2518 GstElement *eparent;
2520 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2522 if (parent != NULL && GST_IS_ELEMENT (parent)) {
2523 eparent = GST_ELEMENT_CAST (gst_object_ref (parent));
2525 GST_OBJECT_LOCK (pad);
2526 eparent = GST_PAD_PARENT (pad);
2527 if (!eparent || !GST_IS_ELEMENT (eparent))
2530 gst_object_ref (eparent);
2531 GST_OBJECT_UNLOCK (pad);
2534 if (pad->direction == GST_PAD_SRC)
2535 padlist = &eparent->sinkpads;
2537 padlist = &eparent->srcpads;
2539 GST_DEBUG_OBJECT (pad, "Making iterator");
2541 cookie = &eparent->pads_cookie;
2543 lock = GST_OBJECT_GET_LOCK (eparent);
2545 res = gst_iterator_new_list (GST_TYPE_PAD,
2546 lock, cookie, padlist, (GObject *) owner, NULL);
2548 gst_object_unref (owner);
2555 GST_OBJECT_UNLOCK (pad);
2556 GST_DEBUG_OBJECT (pad, "no parent element");
2562 * gst_pad_iterate_internal_links:
2563 * @pad: the GstPad to get the internal links of.
2565 * Gets an iterator for the pads to which the given pad is linked to inside
2566 * of the parent element.
2568 * Each #GstPad element yielded by the iterator will have its refcount increased,
2569 * so unref after use.
2571 * Free-function: gst_iterator_free
2573 * Returns: (transfer full): a new #GstIterator of #GstPad or %NULL when the
2574 * pad does not have an iterator function configured. Use
2575 * gst_iterator_free() after usage.
2578 gst_pad_iterate_internal_links (GstPad * pad)
2580 GstIterator *res = NULL;
2583 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2585 GST_OBJECT_LOCK (pad);
2586 ACQUIRE_PARENT (pad, parent, no_parent);
2587 GST_OBJECT_UNLOCK (pad);
2589 if (GST_PAD_ITERINTLINKFUNC (pad))
2590 res = GST_PAD_ITERINTLINKFUNC (pad) (pad, parent);
2592 RELEASE_PARENT (parent);
2599 GST_DEBUG_OBJECT (pad, "no parent");
2600 GST_OBJECT_UNLOCK (pad);
2608 * @forward: (scope call): a #GstPadForwardFunction
2609 * @user_data: user data passed to @forward
2611 * Calls @forward for all internally linked pads of @pad. This function deals with
2612 * dynamically changing internal pads and will make sure that the @forward
2613 * function is only called once for each pad.
2615 * When @forward returns TRUE, no further pads will be processed.
2617 * Returns: TRUE if one of the dispatcher functions returned TRUE.
2620 gst_pad_forward (GstPad * pad, GstPadForwardFunction forward,
2623 gboolean result = FALSE;
2625 gboolean done = FALSE;
2626 GValue item = { 0, };
2627 GList *pushed_pads = NULL;
2629 iter = gst_pad_iterate_internal_links (pad);
2635 switch (gst_iterator_next (iter, &item)) {
2636 case GST_ITERATOR_OK:
2640 intpad = g_value_get_object (&item);
2642 /* if already pushed, skip. FIXME, find something faster to tag pads */
2643 if (intpad == NULL || g_list_find (pushed_pads, intpad)) {
2644 g_value_reset (&item);
2648 GST_LOG_OBJECT (pad, "calling forward function on pad %s:%s",
2649 GST_DEBUG_PAD_NAME (intpad));
2650 done = result = forward (intpad, user_data);
2652 pushed_pads = g_list_prepend (pushed_pads, intpad);
2654 g_value_reset (&item);
2657 case GST_ITERATOR_RESYNC:
2658 /* We don't reset the result here because we don't push the event
2659 * again on pads that got the event already and because we need
2660 * to consider the result of the previous pushes */
2661 gst_iterator_resync (iter);
2663 case GST_ITERATOR_ERROR:
2664 GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
2667 case GST_ITERATOR_DONE:
2672 g_value_unset (&item);
2673 gst_iterator_free (iter);
2675 g_list_free (pushed_pads);
2685 gboolean dispatched;
2689 event_forward_func (GstPad * pad, EventData * data)
2691 /* for each pad we send to, we should ref the event; it's up
2692 * to downstream to unref again when handled. */
2693 GST_LOG_OBJECT (pad, "Reffing and pushing event %p (%s) to %s:%s",
2694 data->event, GST_EVENT_TYPE_NAME (data->event), GST_DEBUG_PAD_NAME (pad));
2696 data->result |= gst_pad_push_event (pad, gst_event_ref (data->event));
2698 data->dispatched = TRUE;
2705 * gst_pad_event_default:
2706 * @pad: a #GstPad to call the default event handler on.
2707 * @parent: the parent of @pad or NULL
2708 * @event: (transfer full): the #GstEvent to handle.
2710 * Invokes the default event handler for the given pad.
2712 * The EOS event will pause the task associated with @pad before it is forwarded
2713 * to all internally linked pads,
2715 * The the event is sent to all pads internally linked to @pad. This function
2716 * takes ownership of @event.
2718 * Returns: TRUE if the event was sent successfully.
2721 gst_pad_event_default (GstPad * pad, GstObject * parent, GstEvent * event)
2723 gboolean result, forward = TRUE;
2725 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2726 g_return_val_if_fail (event != NULL, FALSE);
2728 GST_LOG_OBJECT (pad, "default event handler for event %" GST_PTR_FORMAT,
2731 switch (GST_EVENT_TYPE (event)) {
2732 case GST_EVENT_CAPS:
2733 forward = GST_PAD_IS_PROXY_CAPS (pad);
2744 data.dispatched = FALSE;
2745 data.result = FALSE;
2747 gst_pad_forward (pad, (GstPadForwardFunction) event_forward_func, &data);
2749 /* for sinkpads without a parent element or without internal links, nothing
2750 * will be dispatched but we still want to return TRUE. */
2751 if (data.dispatched)
2752 result = data.result;
2757 gst_event_unref (event);
2762 /* Default accept caps implementation just checks against
2763 * the allowed caps for the pad */
2765 gst_pad_query_accept_caps_default (GstPad * pad, GstQuery * query)
2767 /* get the caps and see if it intersects to something not empty */
2768 GstCaps *caps, *allowed;
2771 GST_DEBUG_OBJECT (pad, "query accept-caps %" GST_PTR_FORMAT, query);
2773 /* first forward the query to internally linked pads when we are dealing with
2775 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2776 if ((result = gst_pad_proxy_query_accept_caps (pad, query))) {
2781 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, pad,
2782 "fallback ACCEPT_CAPS query, consider implementing a specialized version");
2784 allowed = gst_pad_query_caps (pad, NULL);
2785 gst_query_parse_accept_caps (query, &caps);
2788 GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2789 result = gst_caps_is_subset (caps, allowed);
2790 gst_caps_unref (allowed);
2792 GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2795 gst_query_set_accept_caps_result (query, result);
2801 /* Default caps implementation */
2803 gst_pad_query_caps_default (GstPad * pad, GstQuery * query)
2805 GstCaps *result = NULL, *filter;
2806 GstPadTemplate *templ;
2807 gboolean fixed_caps;
2809 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "query caps %" GST_PTR_FORMAT,
2812 /* first try to proxy if we must */
2813 if (GST_PAD_IS_PROXY_CAPS (pad)) {
2814 if ((gst_pad_proxy_query_caps (pad, query))) {
2819 gst_query_parse_caps (query, &filter);
2821 /* no proxy or it failed, do default handling */
2822 fixed_caps = GST_PAD_IS_FIXED_CAPS (pad);
2824 GST_OBJECT_LOCK (pad);
2826 /* fixed caps, try the negotiated caps first */
2827 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "fixed pad caps: trying pad caps");
2828 if ((result = get_pad_caps (pad)))
2829 goto filter_done_unlock;
2832 if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2833 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "trying pad template caps");
2834 if ((result = GST_PAD_TEMPLATE_CAPS (templ)))
2835 goto filter_done_unlock;
2839 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2840 "non-fixed pad caps: trying pad caps");
2841 /* non fixed caps, try the negotiated caps */
2842 if ((result = get_pad_caps (pad)))
2843 goto filter_done_unlock;
2846 /* this almost never happens */
2847 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2848 result = GST_CAPS_ANY;
2851 GST_OBJECT_UNLOCK (pad);
2853 /* run the filter on the result */
2855 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2856 "using caps %p %" GST_PTR_FORMAT " with filter %p %"
2857 GST_PTR_FORMAT, result, result, filter, filter);
2858 result = gst_caps_intersect_full (filter, result, GST_CAPS_INTERSECT_FIRST);
2859 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "result %p %" GST_PTR_FORMAT,
2862 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2863 "using caps %p %" GST_PTR_FORMAT, result, result);
2864 result = gst_caps_ref (result);
2866 gst_query_set_caps_result (query, result);
2867 gst_caps_unref (result);
2877 gboolean dispatched;
2881 query_forward_func (GstPad * pad, QueryData * data)
2883 GST_LOG_OBJECT (pad, "query peer %p (%s) of %s:%s",
2884 data->query, GST_QUERY_TYPE_NAME (data->query), GST_DEBUG_PAD_NAME (pad));
2886 data->result |= gst_pad_peer_query (pad, data->query);
2888 data->dispatched = TRUE;
2890 /* stop on first successful reply */
2891 return data->result;
2895 * gst_pad_query_default:
2896 * @pad: a #GstPad to call the default query handler on.
2897 * @parent: the parent of @pad or NULL
2898 * @query: (transfer none): the #GstQuery to handle.
2900 * Invokes the default query handler for the given pad.
2901 * The query is sent to all pads internally linked to @pad. Note that
2902 * if there are many possible sink pads that are internally linked to
2903 * @pad, only one will be sent the query.
2904 * Multi-sinkpad elements should implement custom query handlers.
2906 * Returns: TRUE if the query was performed successfully.
2909 gst_pad_query_default (GstPad * pad, GstObject * parent, GstQuery * query)
2911 gboolean forward, ret = FALSE;
2913 switch (GST_QUERY_TYPE (query)) {
2914 case GST_QUERY_SCHEDULING:
2915 forward = GST_PAD_IS_PROXY_SCHEDULING (pad);
2917 case GST_QUERY_ALLOCATION:
2918 forward = GST_PAD_IS_PROXY_ALLOCATION (pad);
2920 case GST_QUERY_ACCEPT_CAPS:
2921 ret = gst_pad_query_accept_caps_default (pad, query);
2924 case GST_QUERY_CAPS:
2925 ret = gst_pad_query_caps_default (pad, query);
2928 case GST_QUERY_POSITION:
2929 case GST_QUERY_SEEKING:
2930 case GST_QUERY_FORMATS:
2931 case GST_QUERY_LATENCY:
2932 case GST_QUERY_JITTER:
2933 case GST_QUERY_RATE:
2934 case GST_QUERY_CONVERT:
2940 GST_DEBUG_OBJECT (pad, "%sforwarding %p (%s) query", (forward ? "" : "not "),
2941 query, GST_QUERY_TYPE_NAME (query));
2947 data.dispatched = FALSE;
2948 data.result = FALSE;
2950 gst_pad_forward (pad, (GstPadForwardFunction) query_forward_func, &data);
2952 if (data.dispatched) {
2955 /* nothing dispatched, assume drained */
2956 if (GST_QUERY_TYPE (query) == GST_QUERY_DRAIN)
2966 probe_hook_marshal (GHook * hook, ProbeMarshall * data)
2968 GstPad *pad = data->pad;
2969 GstPadProbeInfo *info = data->info;
2970 GstPadProbeType type, flags;
2971 GstPadProbeCallback callback;
2972 GstPadProbeReturn ret;
2974 /* if we have called this callback, do nothing */
2975 if (PROBE_COOKIE (hook) == data->cookie) {
2976 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2977 "hook %lu, cookie %u already called", hook->hook_id,
2978 PROBE_COOKIE (hook));
2982 PROBE_COOKIE (hook) = data->cookie;
2984 flags = hook->flags >> G_HOOK_FLAG_USER_SHIFT;
2987 /* one of the data types */
2988 if ((flags & GST_PAD_PROBE_TYPE_ALL_BOTH & type) == 0)
2990 /* one of the scheduling types */
2991 if ((flags & GST_PAD_PROBE_TYPE_SCHEDULING & type) == 0)
2993 /* one of the blocking types must match */
2994 if ((type & GST_PAD_PROBE_TYPE_BLOCKING) &&
2995 (flags & GST_PAD_PROBE_TYPE_BLOCKING & type) == 0)
2997 /* only probes that have GST_PAD_PROBE_TYPE_EVENT_FLUSH set */
2998 if ((type & GST_PAD_PROBE_TYPE_EVENT_FLUSH) &&
2999 (flags & GST_PAD_PROBE_TYPE_EVENT_FLUSH & type) == 0)
3002 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3003 "hook %lu, cookie %u with flags 0x%08x matches", hook->hook_id,
3004 PROBE_COOKIE (hook), flags);
3006 data->marshalled = TRUE;
3008 callback = (GstPadProbeCallback) hook->func;
3009 if (callback == NULL)
3012 info->id = hook->hook_id;
3014 GST_OBJECT_UNLOCK (pad);
3016 ret = callback (pad, info, hook->data);
3018 GST_OBJECT_LOCK (pad);
3021 case GST_PAD_PROBE_REMOVE:
3022 /* remove the probe */
3023 GST_DEBUG_OBJECT (pad, "asked to remove hook");
3024 cleanup_hook (pad, hook);
3026 case GST_PAD_PROBE_DROP:
3027 /* need to drop the data, make sure other probes don't get called
3029 GST_DEBUG_OBJECT (pad, "asked to drop item");
3030 info->type = GST_PAD_PROBE_TYPE_INVALID;
3031 data->dropped = TRUE;
3033 case GST_PAD_PROBE_PASS:
3034 /* inform the pad block to let things pass */
3035 GST_DEBUG_OBJECT (pad, "asked to pass item");
3039 GST_DEBUG_OBJECT (pad, "probe returned %d", ret);
3046 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3047 "hook %lu, cookie %u with flags 0x%08x does not match %08x",
3048 hook->hook_id, PROBE_COOKIE (hook), flags, info->type);
3053 /* a probe that does not take or return any data */
3054 #define PROBE_NO_DATA(pad,mask,label,defaultval) \
3056 if (G_UNLIKELY (pad->num_probes)) { \
3057 /* pass NULL as the data item */ \
3058 GstPadProbeInfo info = { mask, 0, NULL, 0, 0 }; \
3059 ret = do_probe_callbacks (pad, &info, defaultval); \
3060 if (G_UNLIKELY (ret != defaultval && ret != GST_FLOW_OK)) \
3065 #define PROBE_FULL(pad,mask,data,offs,size,label) \
3067 if (G_UNLIKELY (pad->num_probes)) { \
3068 /* pass the data item */ \
3069 GstPadProbeInfo info = { mask, 0, data, offs, size }; \
3070 ret = do_probe_callbacks (pad, &info, GST_FLOW_OK); \
3071 /* store the possibly updated data item */ \
3072 data = GST_PAD_PROBE_INFO_DATA (&info); \
3073 /* if something went wrong, exit */ \
3074 if (G_UNLIKELY (ret != GST_FLOW_OK)) \
3079 #define PROBE_PUSH(pad,mask,data,label) \
3080 PROBE_FULL(pad, mask, data, -1, -1, label);
3081 #define PROBE_PULL(pad,mask,data,offs,size,label) \
3082 PROBE_FULL(pad, mask, data, offs, size, label);
3084 static GstFlowReturn
3085 do_probe_callbacks (GstPad * pad, GstPadProbeInfo * info,
3086 GstFlowReturn defaultval)
3095 data.marshalled = FALSE;
3096 data.dropped = FALSE;
3097 data.cookie = ++pad->priv->probe_cookie;
3100 (info->type & GST_PAD_PROBE_TYPE_BLOCK) == GST_PAD_PROBE_TYPE_BLOCK;
3103 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3104 "do probes cookie %u", data.cookie);
3105 cookie = pad->priv->probe_list_cookie;
3107 g_hook_list_marshal (&pad->probes, TRUE,
3108 (GHookMarshaller) probe_hook_marshal, &data);
3110 /* if the list changed, call the new callbacks (they will not have their
3111 * cookie set to data.cookie */
3112 if (cookie != pad->priv->probe_list_cookie) {
3113 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3114 "probe list changed, restarting");
3118 /* the first item that dropped will stop the hooks and then we drop here */
3122 /* if no handler matched and we are blocking, let the item pass */
3123 if (!data.marshalled && is_block)
3126 /* At this point, all handlers returned either OK or PASS. If one handler
3127 * returned PASS, let the item pass */
3132 while (GST_PAD_IS_BLOCKED (pad)) {
3133 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3134 "we are blocked %d times", pad->num_blocked);
3136 /* we might have released the lock */
3137 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3140 /* now we block the streaming thread. It can be unlocked when we
3141 * deactivate the pad (which will also set the FLUSHING flag) or
3142 * when the pad is unblocked. A flushing event will also unblock
3143 * the pad after setting the FLUSHING flag. */
3144 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3145 "Waiting to be unblocked or set flushing");
3146 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_BLOCKING);
3147 GST_PAD_BLOCK_WAIT (pad);
3148 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_BLOCKING);
3149 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "We got unblocked");
3151 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3161 GST_DEBUG_OBJECT (pad, "pad is flushing");
3162 return GST_FLOW_FLUSHING;
3166 GST_DEBUG_OBJECT (pad, "data is dropped");
3167 return GST_FLOW_CUSTOM_SUCCESS;
3171 /* FIXME : Should we return FLOW_OK or the defaultval ?? */
3172 GST_DEBUG_OBJECT (pad, "data is passed");
3180 * gst_pad_get_offset:
3183 * Get the offset applied to the running time of @pad. @pad has to be a source
3186 * Returns: the offset.
3189 gst_pad_get_offset (GstPad * pad)
3193 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3195 GST_OBJECT_LOCK (pad);
3196 result = pad->offset;
3197 GST_OBJECT_UNLOCK (pad);
3203 * gst_pad_set_offset:
3205 * @offset: the offset
3207 * Set the offset that will be applied to the running time of @pad.
3210 gst_pad_set_offset (GstPad * pad, gint64 offset)
3214 g_return_if_fail (GST_IS_PAD (pad));
3216 GST_OBJECT_LOCK (pad);
3217 /* if nothing changed, do nothing */
3218 if (pad->offset == offset)
3221 pad->offset = offset;
3222 GST_DEBUG_OBJECT (pad, "changed offset to %" G_GINT64_FORMAT, offset);
3224 /* sinkpads will apply their offset the next time a segment
3225 * event is received. */
3226 if (GST_PAD_IS_SINK (pad))
3229 /* resend the last segment event on next buffer push */
3230 if ((ev = find_event_by_type (pad, GST_EVENT_SEGMENT, 0))) {
3231 ev->received = FALSE;
3232 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3236 GST_OBJECT_UNLOCK (pad);
3243 /* If TRUE and ret is not OK this means
3244 * that pushing the EOS event failed
3249 /* should be called with pad LOCK */
3251 push_sticky (GstPad * pad, PadEvent * ev, gpointer user_data)
3253 PushStickyData *data = user_data;
3254 GstEvent *event = ev->event;
3257 GST_DEBUG_OBJECT (pad, "event %s was already received",
3258 GST_EVENT_TYPE_NAME (event));
3262 data->ret = gst_pad_push_event_unchecked (pad, gst_event_ref (event),
3263 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3265 switch (data->ret) {
3267 ev->received = TRUE;
3268 GST_DEBUG_OBJECT (pad, "event %s marked received",
3269 GST_EVENT_TYPE_NAME (event));
3271 case GST_FLOW_NOT_LINKED:
3272 /* not linked is not a problem, we are sticky so the event will be
3273 * sent later but only for non-EOS events */
3274 GST_DEBUG_OBJECT (pad, "pad was not linked");
3275 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
3276 data->ret = GST_FLOW_OK;
3279 GST_DEBUG_OBJECT (pad, "mark pending events");
3280 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3284 if (data->ret != GST_FLOW_OK && GST_EVENT_TYPE (event) == GST_EVENT_EOS)
3285 data->was_eos = TRUE;
3287 return data->ret == GST_FLOW_OK;
3290 /* check sticky events and push them when needed. should be called
3292 static inline GstFlowReturn
3293 check_sticky (GstPad * pad)
3295 PushStickyData data = { GST_FLOW_OK, FALSE };
3297 if (G_UNLIKELY (GST_PAD_HAS_PENDING_EVENTS (pad))) {
3298 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
3300 GST_DEBUG_OBJECT (pad, "pushing all sticky events");
3301 events_foreach (pad, push_sticky, &data);
3303 /* If there's an EOS event we must push it downstream
3304 * even if sending a previous sticky event failed.
3305 * Otherwise the pipeline might wait forever for EOS.
3307 * Only do this if pushing another event than the EOS
3310 if (data.ret != GST_FLOW_OK && !data.was_eos) {
3311 PadEvent *ev = find_event_by_type (pad, GST_EVENT_EOS, 0);
3313 if (ev && !ev->received) {
3314 data.ret = gst_pad_push_event_unchecked (pad, gst_event_ref (ev->event),
3315 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM);
3325 * @pad: a #GstPad to invoke the default query on.
3326 * @query: (transfer none): the #GstQuery to perform.
3328 * Dispatches a query to a pad. The query should have been allocated by the
3329 * caller via one of the type-specific allocation functions. The element that
3330 * the pad belongs to is responsible for filling the query with an appropriate
3331 * response, which should then be parsed with a type-specific query parsing
3334 * Again, the caller is responsible for both the allocation and deallocation of
3335 * the query structure.
3337 * Please also note that some queries might need a running pipeline to work.
3339 * Returns: TRUE if the query could be performed.
3342 gst_pad_query (GstPad * pad, GstQuery * query)
3345 gboolean res, serialized;
3346 GstPadQueryFunction func;
3347 GstPadProbeType type;
3350 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3351 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3353 if (GST_PAD_IS_SRC (pad)) {
3354 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3355 goto wrong_direction;
3356 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3357 } else if (GST_PAD_IS_SINK (pad)) {
3358 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3359 goto wrong_direction;
3360 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3362 goto unknown_direction;
3364 GST_DEBUG_OBJECT (pad, "doing query %p (%s)", query,
3365 GST_QUERY_TYPE_NAME (query));
3367 serialized = GST_QUERY_IS_SERIALIZED (query);
3368 if (G_UNLIKELY (serialized))
3369 GST_PAD_STREAM_LOCK (pad);
3371 GST_OBJECT_LOCK (pad);
3372 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3373 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3374 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3376 ACQUIRE_PARENT (pad, parent, no_parent);
3377 GST_OBJECT_UNLOCK (pad);
3379 if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3382 res = func (pad, parent, query);
3384 RELEASE_PARENT (parent);
3386 GST_DEBUG_OBJECT (pad, "sent query %p (%s), result %d", query,
3387 GST_QUERY_TYPE_NAME (query), res);
3392 GST_OBJECT_LOCK (pad);
3393 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3394 GST_OBJECT_UNLOCK (pad);
3396 if (G_UNLIKELY (serialized))
3397 GST_PAD_STREAM_UNLOCK (pad);
3404 g_warning ("pad %s:%s query %s in wrong direction",
3405 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3410 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3415 GST_DEBUG_OBJECT (pad, "had no parent");
3416 GST_OBJECT_UNLOCK (pad);
3417 if (G_UNLIKELY (serialized))
3418 GST_PAD_STREAM_UNLOCK (pad);
3423 GST_DEBUG_OBJECT (pad, "had no query function");
3424 RELEASE_PARENT (parent);
3425 if (G_UNLIKELY (serialized))
3426 GST_PAD_STREAM_UNLOCK (pad);
3431 GST_DEBUG_OBJECT (pad, "query failed");
3432 if (G_UNLIKELY (serialized))
3433 GST_PAD_STREAM_UNLOCK (pad);
3438 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3439 GST_OBJECT_UNLOCK (pad);
3440 if (G_UNLIKELY (serialized))
3441 GST_PAD_STREAM_UNLOCK (pad);
3443 /* if a probe dropped, we don't sent it further but assume that the probe
3444 * answered the query and return TRUE */
3445 if (ret == GST_FLOW_CUSTOM_SUCCESS)
3455 * gst_pad_peer_query:
3456 * @pad: a #GstPad to invoke the peer query on.
3457 * @query: (transfer none): the #GstQuery to perform.
3459 * Performs gst_pad_query() on the peer of @pad.
3461 * The caller is responsible for both the allocation and deallocation of
3462 * the query structure.
3464 * Returns: TRUE if the query could be performed. This function returns %FALSE
3465 * if @pad has no peer.
3468 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3471 GstPadProbeType type;
3472 gboolean res, serialized;
3475 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3476 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3478 if (GST_PAD_IS_SRC (pad)) {
3479 if (G_UNLIKELY (!GST_QUERY_IS_DOWNSTREAM (query)))
3480 goto wrong_direction;
3481 type = GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM;
3482 } else if (GST_PAD_IS_SINK (pad)) {
3483 if (G_UNLIKELY (!GST_QUERY_IS_UPSTREAM (query)))
3484 goto wrong_direction;
3485 type = GST_PAD_PROBE_TYPE_QUERY_UPSTREAM;
3487 goto unknown_direction;
3489 GST_DEBUG_OBJECT (pad, "peer query %p (%s)", query,
3490 GST_QUERY_TYPE_NAME (query));
3492 serialized = GST_QUERY_IS_SERIALIZED (query);
3494 GST_OBJECT_LOCK (pad);
3495 if (GST_PAD_IS_SRC (pad) && serialized) {
3496 /* all serialized queries on the srcpad trigger push of
3498 if (!check_sticky (pad) == GST_FLOW_OK)
3502 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
3503 GST_PAD_PROBE_TYPE_BLOCK, query, probe_stopped);
3504 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, query, probe_stopped);
3506 peerpad = GST_PAD_PEER (pad);
3507 if (G_UNLIKELY (peerpad == NULL))
3510 gst_object_ref (peerpad);
3511 GST_OBJECT_UNLOCK (pad);
3513 res = gst_pad_query (peerpad, query);
3515 gst_object_unref (peerpad);
3520 GST_OBJECT_LOCK (pad);
3521 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PULL, query, probe_stopped);
3522 GST_OBJECT_UNLOCK (pad);
3529 g_warning ("pad %s:%s query %s in wrong direction",
3530 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3535 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
3540 GST_WARNING_OBJECT (pad, "could not send sticky events");
3541 GST_OBJECT_UNLOCK (pad);
3546 GST_WARNING_OBJECT (pad, "pad has no peer");
3547 GST_OBJECT_UNLOCK (pad);
3552 GST_DEBUG_OBJECT (pad, "query failed");
3557 GST_DEBUG_OBJECT (pad, "probe stopped: %s", gst_flow_get_name (ret));
3558 GST_OBJECT_UNLOCK (pad);
3560 /* if a probe dropped, we don't sent it further but assume that the probe
3561 * answered the query and return TRUE */
3562 if (ret == GST_FLOW_CUSTOM_SUCCESS)
3571 /**********************************************************************
3572 * Data passing functions
3575 /* this is the chain function that does not perform the additional argument
3576 * checking for that little extra speed.
3578 static inline GstFlowReturn
3579 gst_pad_chain_data_unchecked (GstPad * pad, GstPadProbeType type, void *data)
3584 GST_PAD_STREAM_LOCK (pad);
3586 GST_OBJECT_LOCK (pad);
3587 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3590 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
3593 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3596 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3598 PROBE_PUSH (pad, type, data, probe_stopped);
3600 parent = GST_OBJECT_PARENT (pad);
3601 GST_OBJECT_UNLOCK (pad);
3603 /* NOTE: we read the chainfunc unlocked.
3604 * we cannot hold the lock for the pad so we might send
3605 * the data to the wrong function. This is not really a
3606 * problem since functions are assigned at creation time
3607 * and don't change that often... */
3608 if (G_LIKELY (type & GST_PAD_PROBE_TYPE_BUFFER)) {
3609 GstPadChainFunction chainfunc;
3611 if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
3614 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3615 "calling chainfunction &%s with buffer %" GST_PTR_FORMAT,
3616 GST_DEBUG_FUNCPTR_NAME (chainfunc), GST_BUFFER (data));
3618 ret = chainfunc (pad, parent, GST_BUFFER_CAST (data));
3620 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3621 "called chainfunction &%s with buffer %p, returned %s",
3622 GST_DEBUG_FUNCPTR_NAME (chainfunc), data, gst_flow_get_name (ret));
3624 GstPadChainListFunction chainlistfunc;
3626 if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
3629 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3630 "calling chainlistfunction &%s",
3631 GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
3633 ret = chainlistfunc (pad, parent, GST_BUFFER_LIST_CAST (data));
3635 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3636 "called chainlistfunction &%s, returned %s",
3637 GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
3640 GST_PAD_STREAM_UNLOCK (pad);
3647 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3648 "chaining, but pad was flushing");
3649 GST_OBJECT_UNLOCK (pad);
3650 GST_PAD_STREAM_UNLOCK (pad);
3651 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3652 return GST_FLOW_FLUSHING;
3656 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "chaining, but pad was EOS");
3657 GST_OBJECT_UNLOCK (pad);
3658 GST_PAD_STREAM_UNLOCK (pad);
3659 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3660 return GST_FLOW_EOS;
3664 g_critical ("chain on pad %s:%s but it was not in push mode",
3665 GST_DEBUG_PAD_NAME (pad));
3666 GST_OBJECT_UNLOCK (pad);
3667 GST_PAD_STREAM_UNLOCK (pad);
3668 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3669 return GST_FLOW_ERROR;
3673 GST_OBJECT_UNLOCK (pad);
3674 GST_PAD_STREAM_UNLOCK (pad);
3675 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3678 case GST_FLOW_CUSTOM_SUCCESS:
3679 GST_DEBUG_OBJECT (pad, "dropped buffer");
3683 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3690 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3691 g_critical ("chain on pad %s:%s but it has no chainfunction",
3692 GST_DEBUG_PAD_NAME (pad));
3693 GST_PAD_STREAM_UNLOCK (pad);
3694 return GST_FLOW_NOT_SUPPORTED;
3700 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3701 * @buffer: (transfer full): the #GstBuffer to send, return GST_FLOW_ERROR
3704 * Chain a buffer to @pad.
3706 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3708 * If the buffer type is not acceptable for @pad (as negotiated with a
3709 * preceeding GST_EVENT_CAPS event), this function returns
3710 * #GST_FLOW_NOT_NEGOTIATED.
3712 * The function proceeds calling the chain function installed on @pad (see
3713 * gst_pad_set_chain_function()) and the return value of that function is
3714 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3717 * In all cases, success or failure, the caller loses its reference to @buffer
3718 * after calling this function.
3720 * Returns: a #GstFlowReturn from the pad.
3725 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
3727 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3728 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3729 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3731 return gst_pad_chain_data_unchecked (pad,
3732 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3735 static GstFlowReturn
3736 gst_pad_chain_list_default (GstPad * pad, GstObject * parent,
3737 GstBufferList * list)
3743 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3745 len = gst_buffer_list_length (list);
3748 for (i = 0; i < len; i++) {
3749 buffer = gst_buffer_list_get (list, i);
3751 gst_pad_chain_data_unchecked (pad,
3752 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH,
3753 gst_buffer_ref (buffer));
3754 if (ret != GST_FLOW_OK)
3757 gst_buffer_list_unref (list);
3763 * gst_pad_chain_list:
3764 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
3765 * @list: (transfer full): the #GstBufferList to send, return GST_FLOW_ERROR
3768 * Chain a bufferlist to @pad.
3770 * The function returns #GST_FLOW_FLUSHING if the pad was flushing.
3772 * If @pad was not negotiated properly with a CAPS event, this function
3773 * returns #GST_FLOW_NOT_NEGOTIATED.
3775 * The function proceeds calling the chainlist function installed on @pad (see
3776 * gst_pad_set_chain_list_function()) and the return value of that function is
3777 * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
3778 * chainlist function.
3780 * In all cases, success or failure, the caller loses its reference to @list
3781 * after calling this function.
3785 * Returns: a #GstFlowReturn from the pad.
3788 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
3790 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3791 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
3792 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3794 return gst_pad_chain_data_unchecked (pad,
3795 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3798 static GstFlowReturn
3799 gst_pad_push_data (GstPad * pad, GstPadProbeType type, void *data)
3804 GST_OBJECT_LOCK (pad);
3805 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3808 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
3811 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PUSH))
3814 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
3817 /* do block probes */
3818 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_BLOCK, data, probe_stopped);
3820 /* recheck sticky events because the probe might have cause a relink */
3821 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
3824 /* do post-blocking probes */
3825 PROBE_PUSH (pad, type, data, probe_stopped);
3827 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
3830 /* take ref to peer pad before releasing the lock */
3831 gst_object_ref (peer);
3833 GST_OBJECT_UNLOCK (pad);
3835 ret = gst_pad_chain_data_unchecked (peer, type, data);
3837 gst_object_unref (peer);
3839 GST_OBJECT_LOCK (pad);
3841 if (pad->priv->using == 0) {
3842 /* pad is not active anymore, trigger idle callbacks */
3843 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
3844 probe_stopped, ret);
3846 GST_OBJECT_UNLOCK (pad);
3850 /* ERROR recovery here */
3854 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3855 "pushing, but pad was flushing");
3856 GST_OBJECT_UNLOCK (pad);
3857 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3858 return GST_FLOW_FLUSHING;
3862 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pushing, but pad was EOS");
3863 GST_OBJECT_UNLOCK (pad);
3864 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3865 return GST_FLOW_EOS;
3869 g_critical ("pushing on pad %s:%s but it was not activated in push mode",
3870 GST_DEBUG_PAD_NAME (pad));
3871 GST_OBJECT_UNLOCK (pad);
3872 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3873 return GST_FLOW_ERROR;
3877 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3878 "error pushing events, return %s", gst_flow_get_name (ret));
3879 GST_OBJECT_UNLOCK (pad);
3880 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3885 GST_OBJECT_UNLOCK (pad);
3886 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3889 case GST_FLOW_CUSTOM_SUCCESS:
3890 GST_DEBUG_OBJECT (pad, "dropped buffer");
3894 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
3901 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3902 "pushing, but it was not linked");
3903 GST_OBJECT_UNLOCK (pad);
3904 gst_mini_object_unref (GST_MINI_OBJECT_CAST (data));
3905 return GST_FLOW_NOT_LINKED;
3911 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3912 * @buffer: (transfer full): the #GstBuffer to push returns GST_FLOW_ERROR
3915 * Pushes a buffer to the peer of @pad.
3917 * This function will call installed block probes before triggering any
3918 * installed data probes.
3920 * The function proceeds calling gst_pad_chain() on the peer pad and returns
3921 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3924 * In all cases, success or failure, the caller loses its reference to @buffer
3925 * after calling this function.
3927 * Returns: a #GstFlowReturn from the peer pad.
3932 gst_pad_push (GstPad * pad, GstBuffer * buffer)
3934 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3935 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3936 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3938 return gst_pad_push_data (pad,
3939 GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH, buffer);
3943 * gst_pad_push_list:
3944 * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
3945 * @list: (transfer full): the #GstBufferList to push returns GST_FLOW_ERROR
3948 * Pushes a buffer list to the peer of @pad.
3950 * This function will call installed block probes before triggering any
3951 * installed data probes.
3953 * The function proceeds calling the chain function on the peer pad and returns
3954 * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
3955 * be returned. If the peer pad does not have any installed chainlist function
3956 * every group buffer of the list will be merged into a normal #GstBuffer and
3957 * chained via gst_pad_chain().
3959 * In all cases, success or failure, the caller loses its reference to @list
3960 * after calling this function.
3962 * Returns: a #GstFlowReturn from the peer pad.
3967 gst_pad_push_list (GstPad * pad, GstBufferList * list)
3969 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
3970 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
3971 g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
3973 return gst_pad_push_data (pad,
3974 GST_PAD_PROBE_TYPE_BUFFER_LIST | GST_PAD_PROBE_TYPE_PUSH, list);
3977 static GstFlowReturn
3978 gst_pad_get_range_unchecked (GstPad * pad, guint64 offset, guint size,
3979 GstBuffer ** buffer)
3982 GstPadGetRangeFunction getrangefunc;
3986 GST_PAD_STREAM_LOCK (pad);
3988 GST_OBJECT_LOCK (pad);
3989 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
3992 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
3995 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
4000 /* when one of the probes returns DROPPED, probe_stopped will be called
4001 * and we skip calling the getrange function, res_buf should then contain a
4002 * valid result buffer */
4003 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4004 res_buf, offset, size, probe_stopped);
4006 /* recheck sticky events because the probe might have cause a relink */
4007 if (G_UNLIKELY ((ret = check_sticky (pad))) != GST_FLOW_OK)
4010 ACQUIRE_PARENT (pad, parent, no_parent);
4011 GST_OBJECT_UNLOCK (pad);
4013 if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4016 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4017 "calling getrangefunc %s, offset %"
4018 G_GUINT64_FORMAT ", size %u",
4019 GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4021 ret = getrangefunc (pad, parent, offset, size, &res_buf);
4023 RELEASE_PARENT (parent);
4025 if (G_UNLIKELY (ret != GST_FLOW_OK))
4026 goto get_range_failed;
4028 /* can only fire the signal if we have a valid buffer */
4029 GST_OBJECT_LOCK (pad);
4031 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4032 res_buf, offset, size, probe_stopped_unref);
4033 GST_OBJECT_UNLOCK (pad);
4035 GST_PAD_STREAM_UNLOCK (pad);
4044 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4045 "getrange, but pad was flushing");
4046 GST_OBJECT_UNLOCK (pad);
4047 GST_PAD_STREAM_UNLOCK (pad);
4048 return GST_FLOW_FLUSHING;
4052 g_critical ("getrange on pad %s:%s but it was not activated in pull mode",
4053 GST_DEBUG_PAD_NAME (pad));
4054 GST_OBJECT_UNLOCK (pad);
4055 GST_PAD_STREAM_UNLOCK (pad);
4056 return GST_FLOW_ERROR;
4060 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "error pushing events");
4061 GST_OBJECT_UNLOCK (pad);
4062 GST_PAD_STREAM_UNLOCK (pad);
4067 GST_DEBUG_OBJECT (pad, "no parent");
4068 GST_OBJECT_UNLOCK (pad);
4069 GST_PAD_STREAM_UNLOCK (pad);
4070 return GST_FLOW_FLUSHING;
4074 g_critical ("getrange on pad %s:%s but it has no getrangefunction",
4075 GST_DEBUG_PAD_NAME (pad));
4076 RELEASE_PARENT (parent);
4077 GST_PAD_STREAM_UNLOCK (pad);
4078 return GST_FLOW_NOT_SUPPORTED;
4082 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4083 "probe returned %s", gst_flow_get_name (ret));
4084 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4086 /* the probe filled the buffer and asks us to not call the getrange
4087 * anymore, we continue with the post probes then. */
4088 GST_DEBUG_OBJECT (pad, "handled buffer");
4092 /* no buffer, we are EOS */
4093 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4097 GST_OBJECT_UNLOCK (pad);
4098 GST_PAD_STREAM_UNLOCK (pad);
4102 probe_stopped_unref:
4104 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4105 "probe returned %s", gst_flow_get_name (ret));
4106 /* if we drop here, it signals EOS */
4107 if (ret == GST_FLOW_CUSTOM_SUCCESS)
4109 GST_OBJECT_UNLOCK (pad);
4110 GST_PAD_STREAM_UNLOCK (pad);
4111 if (*buffer == NULL)
4112 gst_buffer_unref (res_buf);
4117 GST_PAD_STREAM_UNLOCK (pad);
4118 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4119 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4120 pad, "getrange failed, flow: %s", gst_flow_get_name (ret));
4126 * gst_pad_get_range:
4127 * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4128 * @offset: The start offset of the buffer
4129 * @size: The length of the buffer
4130 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer,
4131 * returns #GST_FLOW_ERROR if %NULL.
4133 * When @pad is flushing this function returns #GST_FLOW_FLUSHING
4134 * immediately and @buffer is %NULL.
4136 * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4137 * description of a getrange function. If @pad has no getrange function
4138 * installed (see gst_pad_set_getrange_function()) this function returns
4139 * #GST_FLOW_NOT_SUPPORTED.
4141 * If @buffer points to a variable holding NULL, a valid new #GstBuffer will be
4142 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4143 * must be freed with gst_buffer_unref() after usage.
4145 * When @buffer points to a variable that points to a valid #GstBuffer, the
4146 * buffer will be filled with the result data when this function returns
4147 * #GST_FLOW_OK. If the provided buffer is larger than @size, only
4148 * @size bytes will be filled in the result buffer and its size will be updated
4151 * Note that less than @size bytes can be returned in @buffer when, for example,
4152 * an EOS condition is near or when @buffer is not large enough to hold @size
4153 * bytes. The caller should check the result buffer size to get the result size.
4155 * When this function returns any other result value than #GST_FLOW_OK, @buffer
4156 * will be unchanged.
4158 * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4160 * Returns: a #GstFlowReturn from the pad.
4165 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4166 GstBuffer ** buffer)
4168 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4169 g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4170 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4171 g_return_val_if_fail (*buffer == NULL
4172 || GST_IS_BUFFER (*buffer), GST_FLOW_ERROR);
4174 return gst_pad_get_range_unchecked (pad, offset, size, buffer);
4178 * gst_pad_pull_range:
4179 * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4180 * @offset: The start offset of the buffer
4181 * @size: The length of the buffer
4182 * @buffer: (out callee-allocates): a pointer to hold the #GstBuffer, returns
4183 * GST_FLOW_ERROR if %NULL.
4185 * Pulls a @buffer from the peer pad or fills up a provided buffer.
4187 * This function will first trigger the pad block signal if it was
4190 * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4191 * function returns the result of gst_pad_get_range() on the peer pad.
4192 * See gst_pad_get_range() for a list of return values and for the
4193 * semantics of the arguments of this function.
4195 * If @buffer points to a variable holding NULL, a valid new #GstBuffer will be
4196 * placed in @buffer when this function returns #GST_FLOW_OK. The new buffer
4197 * must be freed with gst_buffer_unref() after usage. When this function
4198 * returns any other result value, @buffer will still point to NULL.
4200 * When @buffer points to a variable that points to a valid #GstBuffer, the
4201 * buffer will be filled with the result data when this function returns
4202 * #GST_FLOW_OK. When this function returns any other result value,
4203 * @buffer will be unchanged. If the provided buffer is larger than @size, only
4204 * @size bytes will be filled in the result buffer and its size will be updated
4207 * Note that less than @size bytes can be returned in @buffer when, for example,
4208 * an EOS condition is near or when @buffer is not large enough to hold @size
4209 * bytes. The caller should check the result buffer size to get the result size.
4211 * Returns: a #GstFlowReturn from the peer pad.
4216 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4217 GstBuffer ** buffer)
4223 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4224 g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4225 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4226 g_return_val_if_fail (*buffer == NULL
4227 || GST_IS_BUFFER (*buffer), GST_FLOW_ERROR);
4229 GST_OBJECT_LOCK (pad);
4230 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4233 if (G_UNLIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_PULL))
4238 /* when one of the probes returns DROPPED, probe_stopped will be
4239 * called and we skip calling the peer getrange function. *buffer should then
4240 * contain a valid buffer */
4241 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BLOCK,
4242 res_buf, offset, size, probe_stopped);
4244 if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4247 gst_object_ref (peer);
4249 GST_OBJECT_UNLOCK (pad);
4251 ret = gst_pad_get_range_unchecked (peer, offset, size, &res_buf);
4253 gst_object_unref (peer);
4255 GST_OBJECT_LOCK (pad);
4257 if (pad->priv->using == 0) {
4258 /* pad is not active anymore, trigger idle callbacks */
4259 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_IDLE,
4260 probe_stopped_unref, ret);
4263 if (G_UNLIKELY (ret != GST_FLOW_OK))
4264 goto pull_range_failed;
4267 PROBE_PULL (pad, GST_PAD_PROBE_TYPE_PULL | GST_PAD_PROBE_TYPE_BUFFER,
4268 res_buf, offset, size, probe_stopped_unref);
4270 GST_OBJECT_UNLOCK (pad);
4276 /* ERROR recovery here */
4279 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4280 "pullrange, but pad was flushing");
4281 GST_OBJECT_UNLOCK (pad);
4282 return GST_FLOW_FLUSHING;
4286 g_critical ("pullrange on pad %s:%s but it was not activated in pull mode",
4287 GST_DEBUG_PAD_NAME (pad));
4288 GST_OBJECT_UNLOCK (pad);
4289 return GST_FLOW_ERROR;
4293 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pre probe returned %s",
4294 gst_flow_get_name (ret));
4295 if (ret == GST_FLOW_CUSTOM_SUCCESS) {
4297 /* the probe filled the buffer and asks us to not forward to the peer
4298 * anymore, we continue with the post probes then */
4299 GST_DEBUG_OBJECT (pad, "handled buffer");
4303 /* no buffer, we are EOS then */
4304 GST_DEBUG_OBJECT (pad, "no buffer, return EOS");
4308 GST_OBJECT_UNLOCK (pad);
4313 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4314 "pulling range, but it was not linked");
4315 GST_OBJECT_UNLOCK (pad);
4316 return GST_FLOW_NOT_LINKED;
4320 GST_OBJECT_UNLOCK (pad);
4321 GST_CAT_LEVEL_LOG (GST_CAT_SCHEDULING,
4322 (ret >= GST_FLOW_EOS) ? GST_LEVEL_INFO : GST_LEVEL_WARNING,
4323 pad, "pullrange failed, flow: %s", gst_flow_get_name (ret));
4326 probe_stopped_unref:
4328 GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4329 "post probe returned %s", gst_flow_get_name (ret));
4330 GST_OBJECT_UNLOCK (pad);
4331 /* if we drop here, it signals EOS */
4332 if (ret == GST_FLOW_CUSTOM_SUCCESS)
4334 if (*buffer == NULL)
4335 gst_buffer_unref (res_buf);
4341 gst_pad_store_sticky_event (GstPad * pad, GstEvent * event, gboolean locked)
4346 gboolean res = FALSE;
4347 const gchar *name = NULL;
4349 type = GST_EVENT_TYPE (event);
4350 if (type & GST_EVENT_TYPE_STICKY_MULTI)
4351 name = gst_structure_get_name (gst_event_get_structure (event));
4353 events = pad->priv->events;
4356 for (i = 0; i < len; i++) {
4357 PadEvent *ev = &g_array_index (events, PadEvent, i);
4359 if (ev->event == NULL)
4362 if (type == GST_EVENT_TYPE (ev->event)) {
4363 /* matching types, check matching name if needed */
4364 if (name && !gst_event_has_name (ev->event, name))
4368 if ((res = gst_event_replace (&ev->event, event)))
4369 ev->received = FALSE;
4375 ev.event = gst_event_ref (event);
4376 ev.received = FALSE;
4377 g_array_append_val (events, ev);
4382 pad->priv->events_cookie++;
4383 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4385 GST_LOG_OBJECT (pad, "stored sticky event %s", GST_EVENT_TYPE_NAME (event));
4387 switch (GST_EVENT_TYPE (event)) {
4388 case GST_EVENT_CAPS:
4390 GST_OBJECT_UNLOCK (pad);
4392 GST_DEBUG_OBJECT (pad, "notify caps");
4393 g_object_notify_by_pspec ((GObject *) pad, pspec_caps);
4396 GST_OBJECT_LOCK (pad);
4405 /* should be called with pad LOCK */
4406 static GstFlowReturn
4407 gst_pad_push_event_unchecked (GstPad * pad, GstEvent * event,
4408 GstPadProbeType type)
4412 GstEventType event_type;
4414 /* Two checks to be made:
4415 * . (un)set the FLUSHING flag for flushing events,
4416 * . handle pad blocking */
4417 event_type = GST_EVENT_TYPE (event);
4418 switch (event_type) {
4419 case GST_EVENT_FLUSH_START:
4420 GST_PAD_SET_FLUSHING (pad);
4422 GST_PAD_BLOCK_BROADCAST (pad);
4423 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4425 case GST_EVENT_FLUSH_STOP:
4426 GST_PAD_UNSET_FLUSHING (pad);
4428 /* Remove sticky EOS events */
4429 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4430 remove_event_by_type (pad, GST_EVENT_EOS);
4431 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4433 type |= GST_PAD_PROBE_TYPE_EVENT_FLUSH;
4437 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4440 /* No need to check for EOS here as either the caller (gst_pad_push_event())
4441 * checked already or this is called as part of pushing sticky events,
4442 * in which case we still want to forward the EOS event downstream.
4445 switch (GST_EVENT_TYPE (event)) {
4446 case GST_EVENT_SEGMENT:
4447 /* pass the adjusted segment event on. We need to do this even if
4448 * there is no peer pad because of the probes. */
4449 event = apply_pad_offset (pad, event);
4451 case GST_EVENT_RECONFIGURE:
4452 if (GST_PAD_IS_SINK (pad))
4453 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4458 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH |
4459 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4464 /* send probes after modifying the events above */
4465 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4467 /* now check the peer pad */
4468 peerpad = GST_PAD_PEER (pad);
4469 if (peerpad == NULL)
4472 gst_object_ref (peerpad);
4474 GST_OBJECT_UNLOCK (pad);
4476 GST_LOG_OBJECT (pad, "sending event %p (%s) to peerpad %" GST_PTR_FORMAT,
4477 event, GST_EVENT_TYPE_NAME (event), peerpad);
4479 ret = gst_pad_send_event_unchecked (peerpad, event, type);
4481 /* Note: we gave away ownership of the event at this point but we can still
4482 * print the old pointer */
4483 GST_LOG_OBJECT (pad, "sent event %p to peerpad %" GST_PTR_FORMAT ", ret %s",
4484 event, peerpad, gst_flow_get_name (ret));
4486 gst_object_unref (peerpad);
4488 GST_OBJECT_LOCK (pad);
4490 if (pad->priv->using == 0) {
4491 /* pad is not active anymore, trigger idle callbacks */
4492 PROBE_NO_DATA (pad, GST_PAD_PROBE_TYPE_PUSH | GST_PAD_PROBE_TYPE_IDLE,
4493 idle_probe_stopped, ret);
4497 /* ERROR handling */
4500 GST_DEBUG_OBJECT (pad, "We're flushing");
4501 gst_event_unref (event);
4502 return GST_FLOW_FLUSHING;
4506 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4507 gst_event_unref (event);
4510 case GST_FLOW_CUSTOM_SUCCESS:
4511 GST_DEBUG_OBJECT (pad, "dropped event");
4515 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4522 GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4523 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_PENDING_EVENTS);
4524 gst_event_unref (event);
4526 /* unlinked pads should not influence latency configuration */
4527 if (event_type == GST_EVENT_LATENCY)
4530 return GST_FLOW_NOT_LINKED;
4534 GST_DEBUG_OBJECT (pad, "Idle probe returned %s", gst_flow_get_name (ret));
4540 * gst_pad_push_event:
4541 * @pad: a #GstPad to push the event to.
4542 * @event: (transfer full): the #GstEvent to send to the pad.
4544 * Sends the event to the peer of the given pad. This function is
4545 * mainly used by elements to send events to their peer
4548 * This function takes owership of the provided event so you should
4549 * gst_event_ref() it if you want to reuse the event after this call.
4551 * Returns: TRUE if the event was handled.
4556 gst_pad_push_event (GstPad * pad, GstEvent * event)
4558 gboolean res = FALSE;
4559 GstPadProbeType type;
4560 gboolean sticky, serialized;
4562 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4563 g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4565 if (GST_PAD_IS_SRC (pad)) {
4566 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4567 goto wrong_direction;
4568 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4569 } else if (GST_PAD_IS_SINK (pad)) {
4570 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4571 goto wrong_direction;
4572 /* events pushed on sinkpad never are sticky */
4573 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4575 goto unknown_direction;
4577 GST_OBJECT_LOCK (pad);
4578 sticky = GST_EVENT_IS_STICKY (event);
4579 serialized = GST_EVENT_IS_SERIALIZED (event);
4582 /* can't store on flushing pads */
4583 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4586 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4589 /* srcpad sticky events are store immediately, the received flag is set
4590 * to FALSE and will be set to TRUE when we can successfully push the
4591 * event to the peer pad */
4592 if (gst_pad_store_sticky_event (pad, event, TRUE)) {
4593 GST_DEBUG_OBJECT (pad, "event %s updated", GST_EVENT_TYPE_NAME (event));
4595 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS)
4596 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
4598 if (GST_PAD_IS_SRC (pad) && (serialized || sticky)) {
4599 /* all serialized or sticky events on the srcpad trigger push of
4601 res = (check_sticky (pad) == GST_FLOW_OK);
4604 /* other events are pushed right away */
4605 res = (gst_pad_push_event_unchecked (pad, event, type) == GST_FLOW_OK);
4607 /* Errors in sticky event pushing are no problem and ignored here
4608 * as they will cause more meaningful errors during data flow.
4609 * For EOS events, that are not followed by data flow, we still
4610 * return FALSE here though.
4612 if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
4614 gst_event_unref (event);
4616 GST_OBJECT_UNLOCK (pad);
4620 /* ERROR handling */
4623 g_warning ("pad %s:%s pushing %s event in wrong direction",
4624 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4625 gst_event_unref (event);
4630 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4631 gst_event_unref (event);
4636 GST_DEBUG_OBJECT (pad, "We're flushing");
4637 GST_OBJECT_UNLOCK (pad);
4638 gst_event_unref (event);
4643 GST_DEBUG_OBJECT (pad, "We're EOS");
4644 GST_OBJECT_UNLOCK (pad);
4645 gst_event_unref (event);
4650 /* Check if we can call the event function with the given event */
4651 static GstFlowReturn
4652 pre_eventfunc_check (GstPad * pad, GstEvent * event)
4656 switch (GST_EVENT_TYPE (event)) {
4657 case GST_EVENT_CAPS:
4659 /* backwards compatibility mode for caps */
4660 gst_event_parse_caps (event, &caps);
4662 if (!gst_pad_query_accept_caps (pad, caps))
4674 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
4675 "caps %" GST_PTR_FORMAT " not accepted", caps);
4676 return GST_FLOW_NOT_NEGOTIATED;
4680 static GstFlowReturn
4681 gst_pad_send_event_unchecked (GstPad * pad, GstEvent * event,
4682 GstPadProbeType type)
4685 GstEventType event_type;
4686 gboolean serialized, need_unlock = FALSE, sticky;
4687 GstPadEventFunction eventfunc;
4690 GST_OBJECT_LOCK (pad);
4691 if (GST_PAD_IS_SINK (pad))
4692 serialized = GST_EVENT_IS_SERIALIZED (event);
4695 sticky = GST_EVENT_IS_STICKY (event);
4696 event_type = GST_EVENT_TYPE (event);
4697 switch (event_type) {
4698 case GST_EVENT_FLUSH_START:
4699 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4700 "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4702 /* can't even accept a flush begin event when flushing */
4703 if (GST_PAD_IS_FLUSHING (pad))
4706 GST_PAD_SET_FLUSHING (pad);
4707 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4709 case GST_EVENT_FLUSH_STOP:
4710 if (G_LIKELY (GST_PAD_MODE (pad) != GST_PAD_MODE_NONE)) {
4711 GST_PAD_UNSET_FLUSHING (pad);
4712 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4714 /* Remove pending EOS events */
4715 GST_LOG_OBJECT (pad, "Removing pending EOS events");
4716 remove_event_by_type (pad, GST_EVENT_EOS);
4717 GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_EOS);
4719 GST_OBJECT_UNLOCK (pad);
4720 /* grab stream lock */
4721 GST_PAD_STREAM_LOCK (pad);
4723 GST_OBJECT_LOCK (pad);
4724 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4727 case GST_EVENT_RECONFIGURE:
4728 if (GST_PAD_IS_SRC (pad))
4729 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_NEED_RECONFIGURE);
4731 GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4732 "have event type %" GST_PTR_FORMAT, event);
4734 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4738 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4741 /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4742 GST_OBJECT_UNLOCK (pad);
4743 GST_PAD_STREAM_LOCK (pad);
4745 GST_OBJECT_LOCK (pad);
4746 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4749 if (G_UNLIKELY (GST_PAD_IS_EOS (pad)))
4753 switch (GST_EVENT_TYPE (event)) {
4754 case GST_EVENT_SEGMENT:
4755 event = apply_pad_offset (pad, event);
4761 /* now do the probe */
4763 type | GST_PAD_PROBE_TYPE_PUSH |
4764 GST_PAD_PROBE_TYPE_BLOCK, event, probe_stopped);
4766 PROBE_PUSH (pad, type | GST_PAD_PROBE_TYPE_PUSH, event, probe_stopped);
4770 if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4773 ACQUIRE_PARENT (pad, parent, no_parent);
4774 GST_OBJECT_UNLOCK (pad);
4776 ret = pre_eventfunc_check (pad, event);
4777 if (G_UNLIKELY (ret != GST_FLOW_OK))
4778 goto precheck_failed;
4781 gst_event_ref (event);
4783 if (eventfunc (pad, parent, event)) {
4786 /* something went wrong */
4787 switch (event_type) {
4788 case GST_EVENT_CAPS:
4789 ret = GST_FLOW_NOT_NEGOTIATED;
4792 ret = GST_FLOW_ERROR;
4796 RELEASE_PARENT (parent);
4798 GST_DEBUG_OBJECT (pad, "sent event, ret %s", gst_flow_get_name (ret));
4801 if (ret == GST_FLOW_OK) {
4802 /* after the event function accepted the event, we can store the sticky
4803 * event on the pad */
4804 gst_pad_store_sticky_event (pad, event, FALSE);
4805 if (event_type == GST_EVENT_EOS)
4806 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_EOS);
4808 gst_event_unref (event);
4812 GST_PAD_STREAM_UNLOCK (pad);
4816 /* ERROR handling */
4819 GST_OBJECT_UNLOCK (pad);
4821 GST_PAD_STREAM_UNLOCK (pad);
4822 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4823 "Received event on flushing pad. Discarding");
4824 gst_event_unref (event);
4825 return GST_FLOW_FLUSHING;
4829 GST_OBJECT_UNLOCK (pad);
4831 GST_PAD_STREAM_UNLOCK (pad);
4832 GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
4833 "Received event on EOS pad. Discarding");
4834 gst_event_unref (event);
4835 return GST_FLOW_EOS;
4839 GST_OBJECT_UNLOCK (pad);
4841 GST_PAD_STREAM_UNLOCK (pad);
4842 gst_event_unref (event);
4845 case GST_FLOW_CUSTOM_SUCCESS:
4846 GST_DEBUG_OBJECT (pad, "dropped event");
4850 GST_DEBUG_OBJECT (pad, "an error occured %s", gst_flow_get_name (ret));
4857 g_warning ("pad %s:%s has no event handler, file a bug.",
4858 GST_DEBUG_PAD_NAME (pad));
4859 GST_OBJECT_UNLOCK (pad);
4861 GST_PAD_STREAM_UNLOCK (pad);
4862 gst_event_unref (event);
4863 return GST_FLOW_NOT_SUPPORTED;
4867 GST_DEBUG_OBJECT (pad, "no parent");
4868 GST_OBJECT_UNLOCK (pad);
4870 GST_PAD_STREAM_UNLOCK (pad);
4871 gst_event_unref (event);
4872 return GST_FLOW_FLUSHING;
4876 GST_DEBUG_OBJECT (pad, "pre event check failed");
4877 RELEASE_PARENT (parent);
4879 GST_PAD_STREAM_UNLOCK (pad);
4880 gst_event_unref (event);
4886 * gst_pad_send_event:
4887 * @pad: a #GstPad to send the event to.
4888 * @event: (transfer full): the #GstEvent to send to the pad.
4890 * Sends the event to the pad. This function can be used
4891 * by applications to send events in the pipeline.
4893 * If @pad is a source pad, @event should be an upstream event. If @pad is a
4894 * sink pad, @event should be a downstream event. For example, you would not
4895 * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4896 * Furthermore, some downstream events have to be serialized with data flow,
4897 * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4898 * the event needs to be serialized with data flow, this function will take the
4899 * pad's stream lock while calling its event function.
4901 * To find out whether an event type is upstream, downstream, or downstream and
4902 * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4903 * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4904 * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4905 * plugin doesn't need to bother itself with this information; the core handles
4906 * all necessary locks and checks.
4908 * This function takes owership of the provided event so you should
4909 * gst_event_ref() it if you want to reuse the event after this call.
4911 * Returns: TRUE if the event was handled.
4914 gst_pad_send_event (GstPad * pad, GstEvent * event)
4917 GstPadProbeType type;
4919 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4920 g_return_val_if_fail (event != NULL, FALSE);
4922 if (GST_PAD_IS_SINK (pad)) {
4923 if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4924 goto wrong_direction;
4925 type = GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM;
4926 } else if (GST_PAD_IS_SRC (pad)) {
4927 if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4928 goto wrong_direction;
4929 type = GST_PAD_PROBE_TYPE_EVENT_UPSTREAM;
4931 goto unknown_direction;
4933 if (gst_pad_send_event_unchecked (pad, event, type) != GST_FLOW_OK)
4940 /* ERROR handling */
4943 g_warning ("pad %s:%s sending %s event in wrong direction",
4944 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4945 gst_event_unref (event);
4950 g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4951 gst_event_unref (event);
4957 * gst_pad_set_element_private:
4958 * @pad: the #GstPad to set the private data of.
4959 * @priv: The private data to attach to the pad.
4961 * Set the given private data gpointer on the pad.
4962 * This function can only be used by the element that owns the pad.
4963 * No locking is performed in this function.
4966 gst_pad_set_element_private (GstPad * pad, gpointer priv)
4968 pad->element_private = priv;
4972 * gst_pad_get_element_private:
4973 * @pad: the #GstPad to get the private data of.
4975 * Gets the private data of a pad.
4976 * No locking is performed in this function.
4978 * Returns: (transfer none): a #gpointer to the private data.
4981 gst_pad_get_element_private (GstPad * pad)
4983 return pad->element_private;
4987 * gst_pad_get_sticky_event:
4988 * @pad: the #GstPad to get the event from.
4989 * @event_type: the #GstEventType that should be retrieved.
4990 * @idx: the index of the event
4992 * Returns a new reference of the sticky event of type @event_type
4995 * Returns: (transfer full): a #GstEvent of type @event_type or NULL when no
4996 * event of @event_type was on @pad. Unref after usage.
4999 gst_pad_get_sticky_event (GstPad * pad, GstEventType event_type, guint idx)
5001 GstEvent *event = NULL;
5004 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
5005 g_return_val_if_fail ((event_type & GST_EVENT_TYPE_STICKY) != 0, NULL);
5007 GST_OBJECT_LOCK (pad);
5008 ev = find_event_by_type (pad, event_type, idx);
5009 if (ev && (event = ev->event))
5010 gst_event_ref (event);
5011 GST_OBJECT_UNLOCK (pad);
5018 GstPadStickyEventsForeachFunction func;
5023 foreach_dispatch_function (GstPad * pad, PadEvent * ev, gpointer user_data)
5025 ForeachDispatch *data = user_data;
5028 GST_OBJECT_UNLOCK (pad);
5030 ret = data->func (pad, &ev->event, data->user_data);
5032 GST_OBJECT_LOCK (pad);
5038 * gst_pad_sticky_events_foreach:
5039 * @pad: the #GstPad that should be used for iteration.
5040 * @foreach_func: (scope call): the #GstPadStickyEventsForeachFunction that
5041 * should be called for every event.
5042 * @user_data: (closure): the optional user data.
5044 * Iterates all sticky events on @pad and calls @foreach_func for every
5045 * event. If @foreach_func returns %FALSE the iteration is immediately stopped.
5048 gst_pad_sticky_events_foreach (GstPad * pad,
5049 GstPadStickyEventsForeachFunction foreach_func, gpointer user_data)
5051 ForeachDispatch data;
5053 g_return_if_fail (GST_IS_PAD (pad));
5054 g_return_if_fail (foreach_func != NULL);
5056 data.func = foreach_func;
5057 data.user_data = user_data;
5059 GST_OBJECT_LOCK (pad);
5060 events_foreach (pad, foreach_dispatch_function, &data);
5061 GST_OBJECT_UNLOCK (pad);
5065 do_stream_status (GstPad * pad, GstStreamStatusType type,
5066 GThread * thread, GstTask * task)
5070 GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5072 if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5073 if (GST_IS_ELEMENT (parent)) {
5074 GstMessage *message;
5075 GValue value = { 0 };
5077 if (type == GST_STREAM_STATUS_TYPE_ENTER) {
5078 gchar *tname, *ename, *pname;
5080 /* create a good task name */
5081 ename = gst_element_get_name (parent);
5082 pname = gst_pad_get_name (pad);
5083 tname = g_strdup_printf ("%s:%s", ename, pname);
5087 gst_object_set_name (GST_OBJECT_CAST (task), tname);
5091 message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5094 g_value_init (&value, GST_TYPE_TASK);
5095 g_value_set_object (&value, task);
5096 gst_message_set_stream_status_object (message, &value);
5097 g_value_unset (&value);
5099 GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5100 gst_element_post_message (parent, message);
5102 gst_object_unref (parent);
5107 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5109 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5114 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5116 do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5121 * gst_pad_start_task:
5122 * @pad: the #GstPad to start the task of
5123 * @func: the task function to call
5124 * @user_data: user data passed to the task function
5125 * @notify: called when @user_data is no longer referenced
5127 * Starts a task that repeatedly calls @func with @user_data. This function
5128 * is mostly used in pad activation functions to start the dataflow.
5129 * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5130 * before @func is called.
5132 * Returns: a %TRUE if the task could be started.
5135 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer user_data,
5136 GDestroyNotify notify)
5141 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5142 g_return_val_if_fail (func != NULL, FALSE);
5144 GST_DEBUG_OBJECT (pad, "start task");
5146 GST_OBJECT_LOCK (pad);
5147 task = GST_PAD_TASK (pad);
5149 task = gst_task_new (func, user_data, notify);
5150 gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5151 gst_task_set_enter_callback (task, pad_enter_thread, pad, NULL);
5152 gst_task_set_leave_callback (task, pad_leave_thread, pad, NULL);
5153 GST_INFO_OBJECT (pad, "created task %p", task);
5154 GST_PAD_TASK (pad) = task;
5155 gst_object_ref (task);
5156 /* release lock to post the message */
5157 GST_OBJECT_UNLOCK (pad);
5159 do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5161 gst_object_unref (task);
5163 GST_OBJECT_LOCK (pad);
5164 /* nobody else is supposed to have changed the pad now */
5165 if (GST_PAD_TASK (pad) != task)
5166 goto concurrent_stop;
5168 res = gst_task_set_state (task, GST_TASK_STARTED);
5169 GST_OBJECT_UNLOCK (pad);
5176 GST_OBJECT_UNLOCK (pad);
5182 * gst_pad_pause_task:
5183 * @pad: the #GstPad to pause the task of
5185 * Pause the task of @pad. This function will also wait until the
5186 * function executed by the task is finished if this function is not
5187 * called from the task function.
5189 * Returns: a TRUE if the task could be paused or FALSE when the pad
5193 gst_pad_pause_task (GstPad * pad)
5198 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5200 GST_DEBUG_OBJECT (pad, "pause task");
5202 GST_OBJECT_LOCK (pad);
5203 task = GST_PAD_TASK (pad);
5206 res = gst_task_set_state (task, GST_TASK_PAUSED);
5207 GST_OBJECT_UNLOCK (pad);
5209 /* wait for task function to finish, this lock is recursive so it does nothing
5210 * when the pause is called from the task itself */
5211 GST_PAD_STREAM_LOCK (pad);
5212 GST_PAD_STREAM_UNLOCK (pad);
5218 GST_DEBUG_OBJECT (pad, "pad has no task");
5219 GST_OBJECT_UNLOCK (pad);
5225 * gst_pad_stop_task:
5226 * @pad: the #GstPad to stop the task of
5228 * Stop the task of @pad. This function will also make sure that the
5229 * function executed by the task will effectively stop if not called
5230 * from the GstTaskFunction.
5232 * This function will deadlock if called from the GstTaskFunction of
5233 * the task. Use gst_task_pause() instead.
5235 * Regardless of whether the pad has a task, the stream lock is acquired and
5236 * released so as to ensure that streaming through this pad has finished.
5238 * Returns: a TRUE if the task could be stopped or FALSE on error.
5241 gst_pad_stop_task (GstPad * pad)
5246 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5248 GST_DEBUG_OBJECT (pad, "stop task");
5250 GST_OBJECT_LOCK (pad);
5251 task = GST_PAD_TASK (pad);
5254 GST_PAD_TASK (pad) = NULL;
5255 res = gst_task_set_state (task, GST_TASK_STOPPED);
5256 GST_OBJECT_UNLOCK (pad);
5258 GST_PAD_STREAM_LOCK (pad);
5259 GST_PAD_STREAM_UNLOCK (pad);
5261 if (!gst_task_join (task))
5264 gst_object_unref (task);
5270 GST_DEBUG_OBJECT (pad, "no task");
5271 GST_OBJECT_UNLOCK (pad);
5273 GST_PAD_STREAM_LOCK (pad);
5274 GST_PAD_STREAM_UNLOCK (pad);
5276 /* this is not an error */
5281 /* this is bad, possibly the application tried to join the task from
5282 * the task's thread. We install the task again so that it will be stopped
5283 * again from the right thread next time hopefully. */
5284 GST_OBJECT_LOCK (pad);
5285 GST_DEBUG_OBJECT (pad, "join failed");
5286 /* we can only install this task if there was no other task */
5287 if (GST_PAD_TASK (pad) == NULL)
5288 GST_PAD_TASK (pad) = task;
5289 GST_OBJECT_UNLOCK (pad);